Skip to content

Commit f5f30c2

Browse files
committed
Allow parallel workers to cope with a newly-created session user ID.
Parallel workers failed after a sequence like BEGIN; CREATE USER foo; SET SESSION AUTHORIZATION foo; because check_session_authorization could not see the uncommitted pg_authid row for "foo". This is because we ran RestoreGUCState() in a separate transaction using an ordinary just-created snapshot. The same disease afflicts any other GUC that requires catalog lookups and isn't forgiving about the lookups failing. To fix, postpone RestoreGUCState() into the worker's main transaction after we've set up a snapshot duplicating the leader's. This affects check_transaction_isolation and check_transaction_deferrable, which think they should only run during transaction start. Make them act like check_transaction_read_only, which already knows it should silently accept the value when InitializingParallelWorker. Per bug #18545 from Andrey Rachitskiy. Back-patch to all supported branches, because this has been wrong for awhile. Discussion: https://postgr.es/m/18545-feba138862f19aaa@postgresql.org
1 parent bd15b7d commit f5f30c2

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

src/backend/access/transam/parallel.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,10 +1410,6 @@ ParallelWorkerMain(Datum main_arg)
14101410
libraryspace = shm_toc_lookup(toc, PARALLEL_KEY_LIBRARY, false);
14111411
StartTransactionCommand();
14121412
RestoreLibraryState(libraryspace);
1413-
1414-
/* Restore GUC values from launching backend. */
1415-
gucspace = shm_toc_lookup(toc, PARALLEL_KEY_GUC, false);
1416-
RestoreGUCState(gucspace);
14171413
CommitTransactionCommand();
14181414

14191415
/* Crank up a transaction state appropriate to a parallel worker. */
@@ -1455,6 +1451,14 @@ ParallelWorkerMain(Datum main_arg)
14551451
*/
14561452
InvalidateSystemCaches();
14571453

1454+
/*
1455+
* Restore GUC values from launching backend. We can't do this earlier,
1456+
* because GUC check hooks that do catalog lookups need to see the same
1457+
* database state as the leader.
1458+
*/
1459+
gucspace = shm_toc_lookup(toc, PARALLEL_KEY_GUC, false);
1460+
RestoreGUCState(gucspace);
1461+
14581462
/*
14591463
* Restore current role id. Skip verifying whether session user is
14601464
* allowed to become this role and blindly restore the leader's state for

src/backend/commands/variable.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,14 +577,16 @@ check_transaction_read_only(bool *newval, void **extra, GucSource source)
577577
* We allow idempotent changes at any time, but otherwise this can only be
578578
* changed in a toplevel transaction that has not yet taken a snapshot.
579579
*
580-
* As in check_transaction_read_only, allow it if not inside a transaction.
580+
* As in check_transaction_read_only, allow it if not inside a transaction,
581+
* or if restoring state in a parallel worker.
581582
*/
582583
bool
583584
check_transaction_isolation(int *newval, void **extra, GucSource source)
584585
{
585586
int newXactIsoLevel = *newval;
586587

587-
if (newXactIsoLevel != XactIsoLevel && IsTransactionState())
588+
if (newXactIsoLevel != XactIsoLevel &&
589+
IsTransactionState() && !InitializingParallelWorker)
588590
{
589591
if (FirstSnapshotSet)
590592
{
@@ -619,6 +621,10 @@ check_transaction_isolation(int *newval, void **extra, GucSource source)
619621
bool
620622
check_transaction_deferrable(bool *newval, void **extra, GucSource source)
621623
{
624+
/* Just accept the value when restoring state in a parallel worker */
625+
if (InitializingParallelWorker)
626+
return true;
627+
622628
if (IsSubTransaction())
623629
{
624630
GUC_check_errcode(ERRCODE_ACTIVE_SQL_TRANSACTION);

src/test/regress/expected/select_parallel.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,3 +1329,21 @@ SELECT 1 FROM tenk1_vw_sec
13291329
(9 rows)
13301330

13311331
rollback;
1332+
-- test that a newly-created session role propagates to workers.
1333+
begin;
1334+
create role regress_parallel_worker;
1335+
set session authorization regress_parallel_worker;
1336+
select current_setting('session_authorization');
1337+
current_setting
1338+
-------------------------
1339+
regress_parallel_worker
1340+
(1 row)
1341+
1342+
set debug_parallel_query = 1;
1343+
select current_setting('session_authorization');
1344+
current_setting
1345+
-------------------------
1346+
regress_parallel_worker
1347+
(1 row)
1348+
1349+
rollback;

src/test/regress/sql/select_parallel.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,12 @@ SELECT 1 FROM tenk1_vw_sec
511511
WHERE (SELECT sum(f1) FROM int4_tbl WHERE f1 < unique1) < 100;
512512

513513
rollback;
514+
515+
-- test that a newly-created session role propagates to workers.
516+
begin;
517+
create role regress_parallel_worker;
518+
set session authorization regress_parallel_worker;
519+
select current_setting('session_authorization');
520+
set debug_parallel_query = 1;
521+
select current_setting('session_authorization');
522+
rollback;

0 commit comments

Comments
 (0)