Skip to content

Commit 2f4e895

Browse files
committed
Allow adjusting session_authorization and role in parallel workers.
The code intends to allow GUCs to be set within parallel workers via function SET clauses, but not otherwise. However, doing so fails for "session_authorization" and "role", because the assign hooks for those attempt to set the subsidiary "is_superuser" GUC, and that call falls foul of the "not otherwise" prohibition. We can't switch to using GUC_ACTION_SAVE for this, so instead add a new GUC variable flag GUC_ALLOW_IN_PARALLEL to mark is_superuser as being safe to set anyway. (This is okay because is_superuser has context PGC_INTERNAL and thus only hard-wired calls can change it. We'd need more thought before applying the flag to other GUCs; but maybe there are other use-cases.) This isn't the prettiest fix perhaps, but other alternatives we thought of would be much more invasive. While here, correct a thinko in commit 059de3c: when rejecting a GUC setting within a parallel worker, we should return 0 not -1 if the ereport doesn't longjmp. (This seems to have no consequences right now because no caller cares, but it's inconsistent.) Improve the comments to try to forestall future confusion of the same kind. Despite the lack of field complaints, this seems worth back-patching. Thanks to Nathan Bossart for the idea to invent a new flag, and for review. Discussion: https://postgr.es/m/2833457.1723229039@sss.pgh.pa.us
1 parent 75bb335 commit 2f4e895

File tree

4 files changed

+74
-11
lines changed

4 files changed

+74
-11
lines changed

src/backend/utils/misc/guc.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ static struct config_bool ConfigureNamesBool[] =
12311231
{"is_superuser", PGC_INTERNAL, UNGROUPED,
12321232
gettext_noop("Shows whether the current user is a superuser."),
12331233
NULL,
1234-
GUC_REPORT | GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
1234+
GUC_REPORT | GUC_NO_SHOW_ALL | GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE | GUC_ALLOW_IN_PARALLEL
12351235
},
12361236
&session_auth_is_superuser,
12371237
false,
@@ -7562,10 +7562,12 @@ parse_and_validate_value(struct config_generic *record,
75627562
*
75637563
* Return value:
75647564
* +1: the value is valid and was successfully applied.
7565-
* 0: the name or value is invalid (but see below).
7566-
* -1: the value was not applied because of context, priority, or changeVal.
7565+
* 0: the name or value is invalid, or it's invalid to try to set
7566+
* this GUC now; but elevel was less than ERROR (see below).
7567+
* -1: no error detected, but the value was not applied, either
7568+
* because changeVal is false or there is some overriding setting.
75677569
*
7568-
* If there is an error (non-existing option, invalid value) then an
7570+
* If there is an error (non-existing option, invalid value, etc) then an
75697571
* ereport(ERROR) is thrown *unless* this is called for a source for which
75707572
* we don't want an ERROR (currently, those are defaults, the config file,
75717573
* and per-database or per-user settings, as well as callers who specify
@@ -7645,26 +7647,30 @@ set_config_option_ext(const char *name, const char *value,
76457647
elevel = ERROR;
76467648
}
76477649

7650+
record = find_option(name, true, false, elevel);
7651+
if (record == NULL)
7652+
return 0;
7653+
76487654
/*
76497655
* GUC_ACTION_SAVE changes are acceptable during a parallel operation,
76507656
* because the current worker will also pop the change. We're probably
76517657
* dealing with a function having a proconfig entry. Only the function's
76527658
* body should observe the change, and peer workers do not share in the
76537659
* execution of a function call started by this worker.
76547660
*
7661+
* Also allow normal setting if the GUC is marked GUC_ALLOW_IN_PARALLEL.
7662+
*
76557663
* Other changes might need to affect other workers, so forbid them.
76567664
*/
7657-
if (IsInParallelMode() && changeVal && action != GUC_ACTION_SAVE)
7665+
if (IsInParallelMode() && changeVal && action != GUC_ACTION_SAVE &&
7666+
(record->flags & GUC_ALLOW_IN_PARALLEL) == 0)
76587667
{
76597668
ereport(elevel,
76607669
(errcode(ERRCODE_INVALID_TRANSACTION_STATE),
7661-
errmsg("cannot set parameters during a parallel operation")));
7662-
return -1;
7663-
}
7664-
7665-
record = find_option(name, true, false, elevel);
7666-
if (record == NULL)
7670+
errmsg("parameter \"%s\" cannot be set during a parallel operation",
7671+
name)));
76677672
return 0;
7673+
}
76687674

76697675
/*
76707676
* Check if the option can be set at this time. See guc.h for the precise

src/include/utils/guc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ typedef enum
238238
* available via 'postgres -C' if the server is not running.
239239
*/
240240
#define GUC_RUNTIME_COMPUTED 0x200000
241+
#define GUC_ALLOW_IN_PARALLEL 0x400000 /* allow setting in parallel mode */
241242

242243
#define GUC_UNIT (GUC_UNIT_MEMORY | GUC_UNIT_TIME)
243244

src/test/regress/expected/select_parallel.out

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,3 +1219,36 @@ SELECT 1 FROM tenk1_vw_sec
12191219
(9 rows)
12201220

12211221
rollback;
1222+
-- test that function option SET ROLE works in parallel workers.
1223+
create role regress_parallel_worker;
1224+
create function set_and_report_role() returns text as
1225+
$$ select current_setting('role') $$ language sql parallel safe
1226+
set role = regress_parallel_worker;
1227+
create function set_role_and_error(int) returns int as
1228+
$$ select 1 / $1 $$ language sql parallel safe
1229+
set role = regress_parallel_worker;
1230+
set force_parallel_mode = 0;
1231+
select set_and_report_role();
1232+
set_and_report_role
1233+
-------------------------
1234+
regress_parallel_worker
1235+
(1 row)
1236+
1237+
select set_role_and_error(0);
1238+
ERROR: division by zero
1239+
CONTEXT: SQL function "set_role_and_error" statement 1
1240+
set force_parallel_mode = 1;
1241+
select set_and_report_role();
1242+
set_and_report_role
1243+
-------------------------
1244+
regress_parallel_worker
1245+
(1 row)
1246+
1247+
select set_role_and_error(0);
1248+
ERROR: division by zero
1249+
CONTEXT: SQL function "set_role_and_error" statement 1
1250+
parallel worker
1251+
reset force_parallel_mode;
1252+
drop function set_and_report_role();
1253+
drop function set_role_and_error(int);
1254+
drop role regress_parallel_worker;

src/test/regress/sql/select_parallel.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,26 @@ SELECT 1 FROM tenk1_vw_sec
462462
WHERE (SELECT sum(f1) FROM int4_tbl WHERE f1 < unique1) < 100;
463463

464464
rollback;
465+
466+
-- test that function option SET ROLE works in parallel workers.
467+
create role regress_parallel_worker;
468+
469+
create function set_and_report_role() returns text as
470+
$$ select current_setting('role') $$ language sql parallel safe
471+
set role = regress_parallel_worker;
472+
473+
create function set_role_and_error(int) returns int as
474+
$$ select 1 / $1 $$ language sql parallel safe
475+
set role = regress_parallel_worker;
476+
477+
set force_parallel_mode = 0;
478+
select set_and_report_role();
479+
select set_role_and_error(0);
480+
set force_parallel_mode = 1;
481+
select set_and_report_role();
482+
select set_role_and_error(0);
483+
reset force_parallel_mode;
484+
485+
drop function set_and_report_role();
486+
drop function set_role_and_error(int);
487+
drop role regress_parallel_worker;

0 commit comments

Comments
 (0)