Skip to content

Commit 6e086fa

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. This un-reverts commit f5f30c2. The original plan was to back-patch that, but the fact that 0ae5b76 proved to be a pre-requisite shows that the subtle API change for GUC hooks might actually break some of them. The problem we're trying to fix seems not worth taking such a risk for in stable branches. Per bug #18545 from Andrey Rachitskiy. Discussion: https://postgr.es/m/18545-feba138862f19aaa@postgresql.org
1 parent 0ae5b76 commit 6e086fa

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
@@ -1414,10 +1414,6 @@ ParallelWorkerMain(Datum main_arg)
14141414
libraryspace = shm_toc_lookup(toc, PARALLEL_KEY_LIBRARY, false);
14151415
StartTransactionCommand();
14161416
RestoreLibraryState(libraryspace);
1417-
1418-
/* Restore GUC values from launching backend. */
1419-
gucspace = shm_toc_lookup(toc, PARALLEL_KEY_GUC, false);
1420-
RestoreGUCState(gucspace);
14211417
CommitTransactionCommand();
14221418

14231419
/* Crank up a transaction state appropriate to a parallel worker. */
@@ -1459,6 +1455,14 @@ ParallelWorkerMain(Datum main_arg)
14591455
*/
14601456
InvalidateSystemCaches();
14611457

1458+
/*
1459+
* Restore GUC values from launching backend. We can't do this earlier,
1460+
* because GUC check hooks that do catalog lookups need to see the same
1461+
* database state as the leader.
1462+
*/
1463+
gucspace = shm_toc_lookup(toc, PARALLEL_KEY_GUC, false);
1464+
RestoreGUCState(gucspace);
1465+
14621466
/*
14631467
* Restore current role id. Skip verifying whether session user is
14641468
* 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)