Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3d332c8

Browse files
committed
Improve and simplify CREATE EXTENSION's management of GUC variables.
CREATE EXTENSION needs to transiently set search_path, as well as client_min_messages and log_min_messages. We were doing this by the expedient of saving the current string value of each variable, doing a SET LOCAL, and then doing another SET LOCAL with the previous value at the end of the command. This is a bit expensive though, and it also fails badly if there is anything funny about the existing search_path value, as seen in a recent report from Roger Niederland. Fortunately, there's a much better way, which is to piggyback on the GUC infrastructure previously developed for functions with SET options. We just open a new GUC nesting level, do our assignments with GUC_ACTION_SAVE, and then close the nesting level when done. This automatically restores the prior settings without a re-parsing pass, so (in principle anyway) there can't be an error. And guc.c still takes care of cleanup in event of an error abort. The CREATE EXTENSION code for this was modeled on some much older code in ri_triggers.c, which I also changed to use the better method, even though there wasn't really much risk of failure there. Also improve the comments in guc.c to reflect this additional usage.
1 parent 99576e0 commit 3d332c8

File tree

4 files changed

+34
-48
lines changed

4 files changed

+34
-48
lines changed

src/backend/commands/extension.c

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -776,9 +776,7 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control,
776776
const char *schemaName, Oid schemaOid)
777777
{
778778
char *filename;
779-
char *save_client_min_messages,
780-
*save_log_min_messages,
781-
*save_search_path;
779+
int save_nestlevel;
782780
StringInfoData pathbuf;
783781
ListCell *lc;
784782

@@ -810,22 +808,20 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control,
810808
* so that we won't spam the user with useless NOTICE messages from common
811809
* script actions like creating shell types.
812810
*
813-
* We use the equivalent of SET LOCAL to ensure the setting is undone upon
814-
* error.
811+
* We use the equivalent of a function SET option to allow the setting to
812+
* persist for exactly the duration of the script execution. guc.c also
813+
* takes care of undoing the setting on error.
815814
*/
816-
save_client_min_messages =
817-
pstrdup(GetConfigOption("client_min_messages", false, false));
815+
save_nestlevel = NewGUCNestLevel();
816+
818817
if (client_min_messages < WARNING)
819818
(void) set_config_option("client_min_messages", "warning",
820819
PGC_USERSET, PGC_S_SESSION,
821-
GUC_ACTION_LOCAL, true);
822-
823-
save_log_min_messages =
824-
pstrdup(GetConfigOption("log_min_messages", false, false));
820+
GUC_ACTION_SAVE, true);
825821
if (log_min_messages < WARNING)
826822
(void) set_config_option("log_min_messages", "warning",
827823
PGC_SUSET, PGC_S_SESSION,
828-
GUC_ACTION_LOCAL, true);
824+
GUC_ACTION_SAVE, true);
829825

830826
/*
831827
* Set up the search path to contain the target schema, then the schemas
@@ -834,10 +830,9 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control,
834830
*
835831
* Note: it might look tempting to use PushOverrideSearchPath for this,
836832
* but we cannot do that. We have to actually set the search_path GUC in
837-
* case the extension script examines or changes it.
833+
* case the extension script examines or changes it. In any case, the
834+
* GUC_ACTION_SAVE method is just as convenient.
838835
*/
839-
save_search_path = pstrdup(GetConfigOption("search_path", false, false));
840-
841836
initStringInfo(&pathbuf);
842837
appendStringInfoString(&pathbuf, quote_identifier(schemaName));
843838
foreach(lc, requiredSchemas)
@@ -851,7 +846,7 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control,
851846

852847
(void) set_config_option("search_path", pathbuf.data,
853848
PGC_USERSET, PGC_S_SESSION,
854-
GUC_ACTION_LOCAL, true);
849+
GUC_ACTION_SAVE, true);
855850

856851
/*
857852
* Set creating_extension and related variables so that
@@ -912,18 +907,9 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control,
912907
CurrentExtensionObject = InvalidOid;
913908

914909
/*
915-
* Restore GUC variables for the remainder of the current transaction.
916-
* Again use SET LOCAL, so we won't affect the session value.
910+
* Restore the GUC variables we set above.
917911
*/
918-
(void) set_config_option("search_path", save_search_path,
919-
PGC_USERSET, PGC_S_SESSION,
920-
GUC_ACTION_LOCAL, true);
921-
(void) set_config_option("client_min_messages", save_client_min_messages,
922-
PGC_USERSET, PGC_S_SESSION,
923-
GUC_ACTION_LOCAL, true);
924-
(void) set_config_option("log_min_messages", save_log_min_messages,
925-
PGC_SUSET, PGC_S_SESSION,
926-
GUC_ACTION_LOCAL, true);
912+
AtEOXact_GUC(true, save_nestlevel);
927913
}
928914

929915
/*

src/backend/utils/adt/ri_triggers.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,7 +2633,7 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
26332633
RangeTblEntry *fkrte;
26342634
const char *sep;
26352635
int i;
2636-
int old_work_mem;
2636+
int save_nestlevel;
26372637
char workmembuf[32];
26382638
int spi_result;
26392639
SPIPlanPtr qplan;
@@ -2772,14 +2772,16 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
27722772
* this seems to meet the criteria for being considered a "maintenance"
27732773
* operation, and accordingly we use maintenance_work_mem.
27742774
*
2775-
* We do the equivalent of "SET LOCAL work_mem" so that transaction abort
2776-
* will restore the old value if we lose control due to an error.
2775+
* We use the equivalent of a function SET option to allow the setting to
2776+
* persist for exactly the duration of the check query. guc.c also takes
2777+
* care of undoing the setting on error.
27772778
*/
2778-
old_work_mem = work_mem;
2779+
save_nestlevel = NewGUCNestLevel();
2780+
27792781
snprintf(workmembuf, sizeof(workmembuf), "%d", maintenance_work_mem);
27802782
(void) set_config_option("work_mem", workmembuf,
27812783
PGC_USERSET, PGC_S_SESSION,
2782-
GUC_ACTION_LOCAL, true);
2784+
GUC_ACTION_SAVE, true);
27832785

27842786
if (SPI_connect() != SPI_OK_CONNECT)
27852787
elog(ERROR, "SPI_connect failed");
@@ -2862,13 +2864,9 @@ RI_Initial_Check(Trigger *trigger, Relation fk_rel, Relation pk_rel)
28622864
elog(ERROR, "SPI_finish failed");
28632865

28642866
/*
2865-
* Restore work_mem for the remainder of the current transaction. This is
2866-
* another SET LOCAL, so it won't affect the session value.
2867+
* Restore work_mem.
28672868
*/
2868-
snprintf(workmembuf, sizeof(workmembuf), "%d", old_work_mem);
2869-
(void) set_config_option("work_mem", workmembuf,
2870-
PGC_USERSET, PGC_S_SESSION,
2871-
GUC_ACTION_LOCAL, true);
2869+
AtEOXact_GUC(true, save_nestlevel);
28722870

28732871
return true;
28742872
}

src/backend/utils/misc/guc.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4367,8 +4367,9 @@ AtStart_GUC(void)
43674367

43684368
/*
43694369
* Enter a new nesting level for GUC values. This is called at subtransaction
4370-
* start and when entering a function that has proconfig settings. NOTE that
4371-
* we must not risk error here, else subtransaction start will be unhappy.
4370+
* start, and when entering a function that has proconfig settings, and in
4371+
* some other places where we want to set GUC variables transiently.
4372+
* NOTE we must not risk error here, else subtransaction start will be unhappy.
43724373
*/
43734374
int
43744375
NewGUCNestLevel(void)
@@ -4378,8 +4379,9 @@ NewGUCNestLevel(void)
43784379

43794380
/*
43804381
* Do GUC processing at transaction or subtransaction commit or abort, or
4381-
* when exiting a function that has proconfig settings. (The name is thus
4382-
* a bit of a misnomer; perhaps it should be ExitGUCNestLevel or some such.)
4382+
* when exiting a function that has proconfig settings, or when undoing a
4383+
* transient assignment to some GUC variables. (The name is thus a bit of
4384+
* a misnomer; perhaps it should be ExitGUCNestLevel or some such.)
43834385
* During abort, we discard all GUC settings that were applied at nesting
43844386
* levels >= nestLevel. nestLevel == 1 corresponds to the main transaction.
43854387
*/
@@ -4412,11 +4414,11 @@ AtEOXact_GUC(bool isCommit, int nestLevel)
44124414
GucStack *stack;
44134415

44144416
/*
4415-
* Process and pop each stack entry within the nest level. To
4416-
* simplify fmgr_security_definer(), we allow failure exit from a
4417-
* function-with-SET-options to be recovered at the surrounding
4418-
* transaction or subtransaction abort; so there could be more than
4419-
* one stack entry to pop.
4417+
* Process and pop each stack entry within the nest level. To simplify
4418+
* fmgr_security_definer() and other places that use GUC_ACTION_SAVE,
4419+
* we allow failure exit from code that uses a local nest level to be
4420+
* recovered at the surrounding transaction or subtransaction abort;
4421+
* so there could be more than one stack entry to pop.
44204422
*/
44214423
while ((stack = gconf->stack) != NULL &&
44224424
stack->nest_level >= nestLevel)

src/include/utils/guc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ typedef enum
155155
/* Types of set_config_option actions */
156156
GUC_ACTION_SET, /* regular SET command */
157157
GUC_ACTION_LOCAL, /* SET LOCAL command */
158-
GUC_ACTION_SAVE /* function SET option */
158+
GUC_ACTION_SAVE /* function SET option, or temp assignment */
159159
} GucAction;
160160

161161
#define GUC_QUALIFIER_SEPARATOR '.'

0 commit comments

Comments
 (0)