Age Owner TLA Line data Source code
1 : /*--------------------------------------------------------------------
2 : * guc.c
3 : *
4 : * Support for grand unified configuration scheme, including SET
5 : * command, configuration file, and command line options.
6 : *
7 : * This file contains the generic option processing infrastructure.
8 : * guc_funcs.c contains SQL-level functionality, including SET/SHOW
9 : * commands and various system-administration SQL functions.
10 : * guc_tables.c contains the arrays that define all the built-in
11 : * GUC variables. Code that implements variable-specific behavior
12 : * is scattered around the system in check, assign, and show hooks.
13 : *
14 : * See src/backend/utils/misc/README for more information.
15 : *
16 : *
17 : * Copyright (c) 2000-2023, PostgreSQL Global Development Group
18 : * Written by Peter Eisentraut <peter_e@gmx.net>.
19 : *
20 : * IDENTIFICATION
21 : * src/backend/utils/misc/guc.c
22 : *
23 : *--------------------------------------------------------------------
24 : */
25 : #include "postgres.h"
26 :
27 : #include <limits.h>
28 : #include <sys/stat.h>
29 : #include <unistd.h>
30 :
31 : #include "access/xact.h"
32 : #include "access/xlog.h"
33 : #include "catalog/objectaccess.h"
34 : #include "catalog/pg_authid.h"
35 : #include "catalog/pg_parameter_acl.h"
36 : #include "guc_internal.h"
37 : #include "libpq/pqformat.h"
38 : #include "parser/scansup.h"
39 : #include "port/pg_bitutils.h"
40 : #include "storage/fd.h"
41 : #include "storage/lwlock.h"
42 : #include "storage/shmem.h"
43 : #include "tcop/tcopprot.h"
44 : #include "utils/acl.h"
45 : #include "utils/backend_status.h"
46 : #include "utils/builtins.h"
47 : #include "utils/conffiles.h"
48 : #include "utils/float.h"
49 : #include "utils/guc_tables.h"
50 : #include "utils/memutils.h"
51 : #include "utils/timestamp.h"
52 :
53 :
54 : #define CONFIG_FILENAME "postgresql.conf"
55 : #define HBA_FILENAME "pg_hba.conf"
56 : #define IDENT_FILENAME "pg_ident.conf"
57 :
58 : #ifdef EXEC_BACKEND
59 : #define CONFIG_EXEC_PARAMS "global/config_exec_params"
60 : #define CONFIG_EXEC_PARAMS_NEW "global/config_exec_params.new"
61 : #endif
62 :
63 : /*
64 : * Precision with which REAL type guc values are to be printed for GUC
65 : * serialization.
66 : */
67 : #define REALTYPE_PRECISION 17
68 :
69 : static int GUC_check_errcode_value;
70 :
71 : static List *reserved_class_prefix = NIL;
72 :
73 : /* global variables for check hook support */
74 : char *GUC_check_errmsg_string;
75 : char *GUC_check_errdetail_string;
76 : char *GUC_check_errhint_string;
77 :
78 : /* Kluge: for speed, we examine this GUC variable's value directly */
79 : extern bool in_hot_standby_guc;
80 :
81 :
82 : /*
2967 heikki.linnakangas 83 ECB : * Unit conversion tables.
84 : *
85 : * There are two tables, one for memory units, and another for time units.
86 : * For each supported conversion from one unit to another, we have an entry
87 : * in the table.
88 : *
89 : * To keep things simple, and to avoid possible roundoff error,
90 : * conversions are never chained. There needs to be a direct conversion
91 : * between all units (of the same type).
92 : *
1491 tgl 93 : * The conversions for each base unit must be kept in order from greatest to
94 : * smallest human-friendly unit; convert_xxx_from_base_unit() rely on that.
95 : * (The order of the base-unit groups does not matter.)
2967 heikki.linnakangas 96 : */
97 : #define MAX_UNIT_LEN 3 /* length of longest recognized unit string */
98 :
99 : typedef struct
100 : {
101 : char unit[MAX_UNIT_LEN + 1]; /* unit, as a string, like "kB" or
102 : * "min" */
103 : int base_unit; /* GUC_UNIT_XXX */
104 : double multiplier; /* Factor for converting unit -> base_unit */
105 : } unit_conversion;
106 :
107 : /* Ensure that the constants in the tables don't overflow or underflow */
108 : #if BLCKSZ < 1024 || BLCKSZ > (1024*1024)
109 : #error BLCKSZ must be between 1KB and 1MB
110 : #endif
111 : #if XLOG_BLCKSZ < 1024 || XLOG_BLCKSZ > (1024*1024)
112 : #error XLOG_BLCKSZ must be between 1KB and 1MB
113 : #endif
114 :
1782 115 : static const char *memory_units_hint = gettext_noop("Valid units for this parameter are \"B\", \"kB\", \"MB\", \"GB\", and \"TB\".");
116 :
117 : static const unit_conversion memory_unit_conversion_table[] =
2967 118 : {
1491 tgl 119 : {"TB", GUC_UNIT_BYTE, 1024.0 * 1024.0 * 1024.0 * 1024.0},
120 : {"GB", GUC_UNIT_BYTE, 1024.0 * 1024.0 * 1024.0},
121 : {"MB", GUC_UNIT_BYTE, 1024.0 * 1024.0},
122 : {"kB", GUC_UNIT_BYTE, 1024.0},
123 : {"B", GUC_UNIT_BYTE, 1.0},
124 :
125 : {"TB", GUC_UNIT_KB, 1024.0 * 1024.0 * 1024.0},
126 : {"GB", GUC_UNIT_KB, 1024.0 * 1024.0},
127 : {"MB", GUC_UNIT_KB, 1024.0},
128 : {"kB", GUC_UNIT_KB, 1.0},
129 : {"B", GUC_UNIT_KB, 1.0 / 1024.0},
130 :
131 : {"TB", GUC_UNIT_MB, 1024.0 * 1024.0},
132 : {"GB", GUC_UNIT_MB, 1024.0},
133 : {"MB", GUC_UNIT_MB, 1.0},
134 : {"kB", GUC_UNIT_MB, 1.0 / 1024.0},
135 : {"B", GUC_UNIT_MB, 1.0 / (1024.0 * 1024.0)},
136 :
137 : {"TB", GUC_UNIT_BLOCKS, (1024.0 * 1024.0 * 1024.0) / (BLCKSZ / 1024)},
138 : {"GB", GUC_UNIT_BLOCKS, (1024.0 * 1024.0) / (BLCKSZ / 1024)},
139 : {"MB", GUC_UNIT_BLOCKS, 1024.0 / (BLCKSZ / 1024)},
140 : {"kB", GUC_UNIT_BLOCKS, 1.0 / (BLCKSZ / 1024)},
141 : {"B", GUC_UNIT_BLOCKS, 1.0 / BLCKSZ},
142 :
143 : {"TB", GUC_UNIT_XBLOCKS, (1024.0 * 1024.0 * 1024.0) / (XLOG_BLCKSZ / 1024)},
144 : {"GB", GUC_UNIT_XBLOCKS, (1024.0 * 1024.0) / (XLOG_BLCKSZ / 1024)},
145 : {"MB", GUC_UNIT_XBLOCKS, 1024.0 / (XLOG_BLCKSZ / 1024)},
146 : {"kB", GUC_UNIT_XBLOCKS, 1.0 / (XLOG_BLCKSZ / 1024)},
147 : {"B", GUC_UNIT_XBLOCKS, 1.0 / XLOG_BLCKSZ},
2878 bruce 148 :
149 : {""} /* end of table marker */
2967 heikki.linnakangas 150 : };
151 :
152 : static const char *time_units_hint = gettext_noop("Valid units for this parameter are \"us\", \"ms\", \"s\", \"min\", \"h\", and \"d\".");
153 :
154 : static const unit_conversion time_unit_conversion_table[] =
155 : {
156 : {"d", GUC_UNIT_MS, 1000 * 60 * 60 * 24},
2878 bruce 157 : {"h", GUC_UNIT_MS, 1000 * 60 * 60},
158 : {"min", GUC_UNIT_MS, 1000 * 60},
159 : {"s", GUC_UNIT_MS, 1000},
160 : {"ms", GUC_UNIT_MS, 1},
1491 tgl 161 : {"us", GUC_UNIT_MS, 1.0 / 1000},
2878 bruce 162 :
163 : {"d", GUC_UNIT_S, 60 * 60 * 24},
164 : {"h", GUC_UNIT_S, 60 * 60},
165 : {"min", GUC_UNIT_S, 60},
166 : {"s", GUC_UNIT_S, 1},
167 : {"ms", GUC_UNIT_S, 1.0 / 1000},
168 : {"us", GUC_UNIT_S, 1.0 / (1000 * 1000)},
169 :
170 : {"d", GUC_UNIT_MIN, 60 * 24},
171 : {"h", GUC_UNIT_MIN, 60},
172 : {"min", GUC_UNIT_MIN, 1},
1491 tgl 173 : {"s", GUC_UNIT_MIN, 1.0 / 60},
174 : {"ms", GUC_UNIT_MIN, 1.0 / (1000 * 60)},
175 : {"us", GUC_UNIT_MIN, 1.0 / (1000 * 1000 * 60)},
176 :
2878 bruce 177 : {""} /* end of table marker */
2967 heikki.linnakangas 178 : };
179 :
8326 peter_e 180 : /*
181 : * To allow continued support of obsolete names for GUC variables, we apply
208 tgl 182 : * the following mappings to any unrecognized name. Note that an old name
183 : * should be mapped to a new one only if the new variable has very similar
184 : * semantics to the old.
185 : */
186 : static const char *const map_old_guc_names[] = {
187 : "sort_mem", "work_mem",
188 : "vacuum_mem", "maintenance_work_mem",
189 : "force_parallel_mode", "debug_parallel_query",
190 : NULL
191 : };
192 :
193 :
194 : /* Memory context holding all GUC-related data */
195 : static MemoryContext GUCMemoryContext;
196 :
197 : /*
198 : * We use a dynahash table to look up GUCs by name, or to iterate through
199 : * all the GUCs. The gucname field is redundant with gucvar->name, but
200 : * dynahash makes it too painful to not store the hash key separately.
201 : */
202 : typedef struct
203 : {
204 : const char *gucname; /* hash key */
205 : struct config_generic *gucvar; /* -> GUC's defining structure */
206 : } GUCHashEntry;
207 :
208 : static HTAB *guc_hashtab; /* entries are GUCHashEntrys */
209 :
210 : /*
211 : * In addition to the hash table, variables having certain properties are
212 : * linked into these lists, so that we can find them without scanning the
213 : * whole hash table. In most applications, only a small fraction of the
214 : * GUCs appear in these lists at any given time. The usage of the stack
215 : * and report lists is stylized enough that they can be slists, but the
216 : * nondef list has to be a dlist to avoid O(N) deletes in common cases.
217 : */
218 : static dlist_head guc_nondef_list; /* list of variables that have source
219 : * different from PGC_S_DEFAULT */
220 : static slist_head guc_stack_list; /* list of variables that have non-NULL
221 : * stack */
222 : static slist_head guc_report_list; /* list of variables that have the
223 : * GUC_NEEDS_REPORT bit set in status */
1468 tmunro 224 :
225 : static bool reporting_enabled; /* true to enable GUC_REPORT */
226 :
227 : static int GUCNestLevel = 0; /* 1 when in main transaction */
228 :
229 : static int guc_var_compare(const void *a, const void *b);
230 : static uint32 guc_name_hash(const void *key, Size keysize);
231 : static int guc_name_match(const void *key1, const void *key2, Size keysize);
208 tgl 232 : static void InitializeGUCOptionsFromEnvironment(void);
233 : static void InitializeOneGUCOption(struct config_generic *gconf);
234 : static void RemoveGUCFromLists(struct config_generic *gconf);
235 : static void set_guc_source(struct config_generic *gconf, GucSource newsource);
236 : static void pg_timezone_abbrev_initialize(void);
237 : static void push_old_value(struct config_generic *gconf, GucAction action);
238 : static void ReportGUCOption(struct config_generic *record);
239 : static void set_config_sourcefile(const char *name, char *sourcefile,
240 : int sourceline);
241 : static void reapply_stacked_values(struct config_generic *variable,
242 : struct config_string *pHolder,
243 : GucStack *stack,
244 : const char *curvalue,
245 : GucContext curscontext, GucSource cursource,
246 : Oid cursrole);
247 : static bool validate_option_array_item(const char *name, const char *value,
248 : bool user_set, bool skipIfNoPermissions);
249 : static void write_auto_conf_file(int fd, const char *filename, ConfigVariable *head);
250 : static void replace_auto_config_value(ConfigVariable **head_p, ConfigVariable **tail_p,
251 : const char *name, const char *value);
252 : static bool valid_custom_variable_name(const char *name);
253 : static void do_serialize(char **destptr, Size *maxbytes,
254 : const char *fmt,...) pg_attribute_printf(3, 4);
255 : static bool call_bool_check_hook(struct config_bool *conf, bool *newval,
256 : void **extra, GucSource source, int elevel);
257 : static bool call_int_check_hook(struct config_int *conf, int *newval,
258 : void **extra, GucSource source, int elevel);
259 : static bool call_real_check_hook(struct config_real *conf, double *newval,
260 : void **extra, GucSource source, int elevel);
261 : static bool call_string_check_hook(struct config_string *conf, char **newval,
262 : void **extra, GucSource source, int elevel);
263 : static bool call_enum_check_hook(struct config_enum *conf, int *newval,
264 : void **extra, GucSource source, int elevel);
265 :
266 :
267 : /*
268 : * This function handles both actual config file (re)loads and execution of
269 : * show_all_file_settings() (i.e., the pg_file_settings view). In the latter
270 : * case we don't apply any of the settings, but we make all the usual validity
271 : * checks, and we return the ConfigVariable list so that it can be printed out
272 : * by show_all_file_settings().
273 : */
274 : ConfigVariable *
208 tgl 275 GNC 4007 : ProcessConfigFileInternal(GucContext context, bool applySettings, int elevel)
276 : {
277 4007 : bool error = false;
278 4007 : bool applying = false;
279 : const char *ConfFileWithError;
280 : ConfigVariable *item,
281 : *head,
282 : *tail;
283 : HASH_SEQ_STATUS status;
284 : GUCHashEntry *hentry;
285 :
286 : /* Parse the main config file into a list of option names and values */
287 4007 : ConfFileWithError = ConfigFileName;
288 4007 : head = tail = NULL;
289 :
290 4007 : if (!ParseConfigFile(ConfigFileName, true,
291 : NULL, 0, CONF_FILE_START_DEPTH, elevel,
292 : &head, &tail))
293 : {
294 : /* Syntax error(s) detected in the file, so bail out */
208 tgl 295 UNC 0 : error = true;
296 0 : goto bail_out;
297 : }
298 :
299 : /*
300 : * Parse the PG_AUTOCONF_FILENAME file, if present, after the main file to
301 : * replace any parameters set by ALTER SYSTEM command. Because this file
302 : * is in the data directory, we can't read it until the DataDir has been
303 : * set.
304 : */
208 tgl 305 GNC 4007 : if (DataDir)
306 : {
307 2174 : if (!ParseConfigFile(PG_AUTOCONF_FILENAME, false,
308 : NULL, 0, CONF_FILE_START_DEPTH, elevel,
309 : &head, &tail))
310 : {
311 : /* Syntax error(s) detected in the file, so bail out */
208 tgl 312 UNC 0 : error = true;
313 0 : ConfFileWithError = PG_AUTOCONF_FILENAME;
314 0 : goto bail_out;
315 : }
316 : }
317 : else
318 : {
319 : /*
320 : * If DataDir is not set, the PG_AUTOCONF_FILENAME file cannot be
321 : * read. In this case, we don't want to accept any settings but
322 : * data_directory from postgresql.conf, because they might be
323 : * overwritten with settings in the PG_AUTOCONF_FILENAME file which
324 : * will be read later. OTOH, since data_directory isn't allowed in the
325 : * PG_AUTOCONF_FILENAME file, it will never be overwritten later.
326 : */
208 tgl 327 GNC 1833 : ConfigVariable *newlist = NULL;
328 :
329 : /*
330 : * Prune all items except the last "data_directory" from the list.
331 : */
332 26217 : for (item = head; item; item = item->next)
333 : {
334 24384 : if (!item->ignore &&
335 24384 : strcmp(item->name, "data_directory") == 0)
208 tgl 336 UNC 0 : newlist = item;
337 : }
338 :
208 tgl 339 GNC 1833 : if (newlist)
208 tgl 340 UNC 0 : newlist->next = NULL;
208 tgl 341 GNC 1833 : head = tail = newlist;
342 :
343 : /*
344 : * Quick exit if data_directory is not present in file.
345 : *
346 : * We need not do any further processing, in particular we don't set
347 : * PgReloadTime; that will be set soon by subsequent full loading of
348 : * the config file.
349 : */
350 1833 : if (head == NULL)
351 1833 : goto bail_out;
352 : }
353 :
354 : /*
355 : * Mark all extant GUC variables as not present in the config file. We
356 : * need this so that we can tell below which ones have been removed from
357 : * the file since we last processed it.
358 : */
177 359 2174 : hash_seq_init(&status, guc_hashtab);
360 802254 : while ((hentry = (GUCHashEntry *) hash_seq_search(&status)) != NULL)
361 : {
362 800080 : struct config_generic *gconf = hentry->gucvar;
363 :
208 364 800080 : gconf->status &= ~GUC_IS_IN_FILE;
365 : }
366 :
367 : /*
368 : * Check if all the supplied option names are valid, as an additional
369 : * quasi-syntactic check on the validity of the config file. It is
370 : * important that the postmaster and all backends agree on the results of
371 : * this phase, else we will have strange inconsistencies about which
372 : * processes accept a config file update and which don't. Hence, unknown
373 : * custom variable names have to be accepted without complaint. For the
374 : * same reason, we don't attempt to validate the options' values here.
375 : *
376 : * In addition, the GUC_IS_IN_FILE flag is set on each existing GUC
377 : * variable mentioned in the file; and we detect duplicate entries in the
378 : * file and mark the earlier occurrences as ignorable.
379 : */
380 37604 : for (item = head; item; item = item->next)
381 : {
382 : struct config_generic *record;
383 :
384 : /* Ignore anything already marked as ignorable */
385 35430 : if (item->ignore)
208 tgl 386 UNC 0 : continue;
387 :
388 : /*
389 : * Try to find the variable; but do not create a custom placeholder if
390 : * it's not there already.
391 : */
208 tgl 392 GNC 35430 : record = find_option(item->name, false, true, elevel);
393 :
394 35430 : if (record)
395 : {
396 : /* If it's already marked, then this is a duplicate entry */
397 35415 : if (record->status & GUC_IS_IN_FILE)
398 : {
399 : /*
400 : * Mark the earlier occurrence(s) as dead/ignorable. We could
401 : * avoid the O(N^2) behavior here with some additional state,
402 : * but it seems unlikely to be worth the trouble.
403 : */
404 : ConfigVariable *pitem;
405 :
406 89150 : for (pitem = head; pitem != item; pitem = pitem->next)
407 : {
408 86042 : if (!pitem->ignore &&
409 78121 : strcmp(pitem->name, item->name) == 0)
410 3108 : pitem->ignore = true;
411 : }
412 : }
413 : /* Now mark it as present in file */
414 35415 : record->status |= GUC_IS_IN_FILE;
415 : }
416 15 : else if (!valid_custom_variable_name(item->name))
417 : {
418 : /* Invalid non-custom variable, so complain */
419 1 : ereport(elevel,
420 : (errcode(ERRCODE_UNDEFINED_OBJECT),
421 : errmsg("unrecognized configuration parameter \"%s\" in file \"%s\" line %d",
422 : item->name,
423 : item->filename, item->sourceline)));
424 1 : item->errmsg = pstrdup("unrecognized configuration parameter");
425 1 : error = true;
426 1 : ConfFileWithError = item->filename;
427 : }
428 : }
429 :
430 : /*
431 : * If we've detected any errors so far, we don't want to risk applying any
432 : * changes.
433 : */
434 2174 : if (error)
435 1 : goto bail_out;
436 :
437 : /* Otherwise, set flag that we're beginning to apply changes */
438 2173 : applying = true;
439 :
440 : /*
441 : * Check for variables having been removed from the config file, and
442 : * revert their reset values (and perhaps also effective values) to the
443 : * boot-time defaults. If such a variable can't be changed after startup,
444 : * report that and continue.
445 : */
177 446 2173 : hash_seq_init(&status, guc_hashtab);
447 801885 : while ((hentry = (GUCHashEntry *) hash_seq_search(&status)) != NULL)
448 : {
449 799712 : struct config_generic *gconf = hentry->gucvar;
450 : GucStack *stack;
451 :
208 452 799712 : if (gconf->reset_source != PGC_S_FILE ||
453 9294 : (gconf->status & GUC_IS_IN_FILE))
454 799710 : continue;
455 2 : if (gconf->context < PGC_SIGHUP)
456 : {
457 : /* The removal can't be effective without a restart */
208 tgl 458 UNC 0 : gconf->status |= GUC_PENDING_RESTART;
459 0 : ereport(elevel,
460 : (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
461 : errmsg("parameter \"%s\" cannot be changed without restarting the server",
462 : gconf->name)));
463 0 : record_config_file_error(psprintf("parameter \"%s\" cannot be changed without restarting the server",
464 : gconf->name),
465 : NULL, 0,
466 : &head, &tail);
467 0 : error = true;
468 0 : continue;
469 : }
470 :
471 : /* No more to do if we're just doing show_all_file_settings() */
208 tgl 472 GNC 2 : if (!applySettings)
208 tgl 473 UNC 0 : continue;
474 :
475 : /*
476 : * Reset any "file" sources to "default", else set_config_option will
477 : * not override those settings.
478 : */
208 tgl 479 GNC 2 : if (gconf->reset_source == PGC_S_FILE)
480 2 : gconf->reset_source = PGC_S_DEFAULT;
481 2 : if (gconf->source == PGC_S_FILE)
177 482 2 : set_guc_source(gconf, PGC_S_DEFAULT);
208 483 2 : for (stack = gconf->stack; stack; stack = stack->prev)
484 : {
208 tgl 485 UNC 0 : if (stack->source == PGC_S_FILE)
486 0 : stack->source = PGC_S_DEFAULT;
487 : }
488 :
489 : /* Now we can re-apply the wired-in default (i.e., the boot_val) */
208 tgl 490 GNC 2 : if (set_config_option(gconf->name, NULL,
491 : context, PGC_S_DEFAULT,
492 : GUC_ACTION_SET, true, 0, false) > 0)
493 : {
494 : /* Log the change if appropriate */
495 2 : if (context == PGC_SIGHUP)
496 2 : ereport(elevel,
497 : (errmsg("parameter \"%s\" removed from configuration file, reset to default",
498 : gconf->name)));
499 : }
500 : }
501 :
502 : /*
503 : * Restore any variables determined by environment variables or
504 : * dynamically-computed defaults. This is a no-op except in the case
505 : * where one of these had been in the config file and is now removed.
506 : *
507 : * In particular, we *must not* do this during the postmaster's initial
508 : * loading of the file, since the timezone functions in particular should
509 : * be run only after initialization is complete.
510 : *
511 : * XXX this is an unmaintainable crock, because we have to know how to set
512 : * (or at least what to call to set) every non-PGC_INTERNAL variable that
513 : * could potentially have PGC_S_DYNAMIC_DEFAULT or PGC_S_ENV_VAR source.
514 : */
515 2173 : if (context == PGC_SIGHUP && applySettings)
516 : {
517 338 : InitializeGUCOptionsFromEnvironment();
518 338 : pg_timezone_abbrev_initialize();
519 : /* this selects SQL_ASCII in processes not connected to a database */
520 338 : SetConfigOption("client_encoding", GetDatabaseEncodingName(),
521 : PGC_BACKEND, PGC_S_DYNAMIC_DEFAULT);
522 : }
523 :
524 : /*
525 : * Now apply the values from the config file.
526 : */
527 37589 : for (item = head; item; item = item->next)
528 : {
529 35417 : char *pre_value = NULL;
530 : int scres;
531 :
532 : /* Ignore anything marked as ignorable */
533 35417 : if (item->ignore)
534 3108 : continue;
535 :
536 : /* In SIGHUP cases in the postmaster, we want to report changes */
537 32309 : if (context == PGC_SIGHUP && applySettings && !IsUnderPostmaster)
538 : {
539 2749 : const char *preval = GetConfigOption(item->name, true, false);
540 :
541 : /* If option doesn't exist yet or is NULL, treat as empty string */
542 2749 : if (!preval)
208 tgl 543 UNC 0 : preval = "";
544 : /* must dup, else might have dangling pointer below */
208 tgl 545 GNC 2749 : pre_value = pstrdup(preval);
546 : }
547 :
548 32309 : scres = set_config_option(item->name, item->value,
549 : context, PGC_S_FILE,
550 : GUC_ACTION_SET, applySettings, 0, false);
551 32308 : if (scres > 0)
552 : {
553 : /* variable was updated, so log the change if appropriate */
554 28648 : if (pre_value)
555 : {
556 1736 : const char *post_value = GetConfigOption(item->name, true, false);
557 :
558 1736 : if (!post_value)
208 tgl 559 UNC 0 : post_value = "";
208 tgl 560 GNC 1736 : if (strcmp(pre_value, post_value) != 0)
561 74 : ereport(elevel,
562 : (errmsg("parameter \"%s\" changed to \"%s\"",
563 : item->name, item->value)));
564 : }
565 28648 : item->applied = true;
566 : }
567 3660 : else if (scres == 0)
568 : {
208 tgl 569 UNC 0 : error = true;
570 0 : item->errmsg = pstrdup("setting could not be applied");
571 0 : ConfFileWithError = item->filename;
572 : }
573 : else
574 : {
575 : /* no error, but variable's active value was not changed */
208 tgl 576 GNC 3660 : item->applied = true;
577 : }
578 :
579 : /*
580 : * We should update source location unless there was an error, since
581 : * even if the active value didn't change, the reset value might have.
582 : * (In the postmaster, there won't be a difference, but it does matter
583 : * in backends.)
584 : */
585 32308 : if (scres != 0 && applySettings)
586 32238 : set_config_sourcefile(item->name, item->filename,
587 : item->sourceline);
588 :
589 32308 : if (pre_value)
590 2749 : pfree(pre_value);
591 : }
592 :
593 : /* Remember when we last successfully loaded the config file. */
594 2172 : if (applySettings)
595 2169 : PgReloadTime = GetCurrentTimestamp();
596 :
597 3 : bail_out:
598 4006 : if (error && applySettings)
599 : {
600 : /* During postmaster startup, any error is fatal */
601 1 : if (context == PGC_POSTMASTER)
602 1 : ereport(ERROR,
603 : (errcode(ERRCODE_CONFIG_FILE_ERROR),
604 : errmsg("configuration file \"%s\" contains errors",
605 : ConfFileWithError)));
208 tgl 606 UNC 0 : else if (applying)
607 0 : ereport(elevel,
608 : (errcode(ERRCODE_CONFIG_FILE_ERROR),
609 : errmsg("configuration file \"%s\" contains errors; unaffected changes were applied",
610 : ConfFileWithError)));
611 : else
612 0 : ereport(elevel,
613 : (errcode(ERRCODE_CONFIG_FILE_ERROR),
614 : errmsg("configuration file \"%s\" contains errors; no changes were applied",
615 : ConfFileWithError)));
616 : }
617 :
618 : /* Successful or otherwise, return the collected data list */
208 tgl 619 GNC 4005 : return head;
620 : }
621 :
622 :
623 : /*
624 : * Some infrastructure for GUC-related memory allocation
625 : *
626 : * These functions are generally modeled on libc's malloc/realloc/etc,
627 : * but any OOM issue is reported at the specified elevel.
628 : * (Thus, control returns only if that's less than ERROR.)
629 : */
630 : void *
208 tgl 631 GIC 578760 : guc_malloc(int elevel, size_t size)
208 tgl 632 ECB : {
208 tgl 633 EUB : void *data;
634 :
177 tgl 635 GNC 578760 : data = MemoryContextAllocExtended(GUCMemoryContext, size,
636 : MCXT_ALLOC_NO_OOM);
637 578760 : if (unlikely(data == NULL))
208 tgl 638 LBC 0 : ereport(elevel,
639 : (errcode(ERRCODE_OUT_OF_MEMORY),
208 tgl 640 ECB : errmsg("out of memory")));
208 tgl 641 GIC 578760 : return data;
208 tgl 642 ECB : }
643 :
644 : void *
208 tgl 645 UIC 0 : guc_realloc(int elevel, void *old, size_t size)
208 tgl 646 EUB : {
647 : void *data;
8186 vadim4o 648 :
177 tgl 649 UNC 0 : if (old != NULL)
650 : {
651 : /* This is to help catch old code that malloc's GUC data. */
652 0 : Assert(GetMemoryChunkContext(old) == GUCMemoryContext);
653 0 : data = repalloc_extended(old, size,
654 : MCXT_ALLOC_NO_OOM);
655 : }
656 : else
657 : {
658 : /* Like realloc(3), but not like repalloc(), we allow old == NULL. */
659 0 : data = MemoryContextAllocExtended(GUCMemoryContext, size,
660 : MCXT_ALLOC_NO_OOM);
661 : }
662 0 : if (unlikely(data == NULL))
208 tgl 663 UBC 0 : ereport(elevel,
664 : (errcode(ERRCODE_OUT_OF_MEMORY),
208 tgl 665 EUB : errmsg("out of memory")));
208 tgl 666 UIC 0 : return data;
208 tgl 667 EUB : }
668 :
669 : char *
208 tgl 670 GIC 473153 : guc_strdup(int elevel, const char *src)
208 tgl 671 EUB : {
672 : char *data;
177 tgl 673 GNC 473153 : size_t len = strlen(src) + 1;
2610 andres 674 EUB :
177 tgl 675 GNC 473153 : data = guc_malloc(elevel, len);
676 473153 : if (likely(data != NULL))
677 473153 : memcpy(data, src, len);
208 tgl 678 CBC 473153 : return data;
679 : }
1100 noah 680 ECB :
681 : void
177 tgl 682 GNC 454446 : guc_free(void *ptr)
683 : {
684 : /*
685 : * Historically, GUC-related code has relied heavily on the ability to do
686 : * free(NULL), so we allow that here even though pfree() doesn't.
687 : */
688 454446 : if (ptr != NULL)
689 : {
690 : /* This is to help catch old code that malloc's GUC data. */
691 221259 : Assert(GetMemoryChunkContext(ptr) == GUCMemoryContext);
692 221259 : pfree(ptr);
693 : }
694 454446 : }
695 :
696 :
208 tgl 697 ECB : /*
698 : * Detect whether strval is referenced anywhere in a GUC string item
699 : */
700 : static bool
208 tgl 701 GBC 369531 : string_field_used(struct config_string *conf, char *strval)
208 tgl 702 EUB : {
703 : GucStack *stack;
704 :
208 tgl 705 CBC 369531 : if (strval == *(conf->variable) ||
208 tgl 706 GBC 226906 : strval == conf->reset_val ||
208 tgl 707 GIC 113686 : strval == conf->boot_val)
708 255845 : return true;
208 tgl 709 CBC 123703 : for (stack = conf->gen.stack; stack; stack = stack->prev)
1097 alvherre 710 ECB : {
208 tgl 711 GIC 12809 : if (strval == stack->prior.val.stringval ||
712 10020 : strval == stack->masked.val.stringval)
208 tgl 713 CBC 2792 : return true;
714 : }
208 tgl 715 GIC 110894 : return false;
716 : }
717 :
718 : /*
719 : * Support for assigning to a field of a string GUC item. Free the prior
720 : * value if it's not referenced anywhere else in the item (including stacked
721 : * states).
208 tgl 722 ECB : */
723 : static void
208 tgl 724 GIC 298339 : set_string_field(struct config_string *conf, char **field, char *newval)
725 : {
208 tgl 726 CBC 298339 : char *oldval = *field;
4393 heikki.linnakangas 727 ECB :
728 : /* Do the assignment */
208 tgl 729 CBC 298339 : *field = newval;
8165 vadim4o 730 EUB :
208 tgl 731 ECB : /* Free old value if it's not NULL and isn't referenced anymore */
208 tgl 732 GIC 298339 : if (oldval && !string_field_used(conf, oldval))
177 tgl 733 GNC 109598 : guc_free(oldval);
208 tgl 734 CBC 298339 : }
8077 tgl 735 ECB :
736 : /*
737 : * Detect whether an "extra" struct is referenced anywhere in a GUC item
738 : */
739 : static bool
208 tgl 740 GIC 123749 : extra_field_used(struct config_generic *gconf, void *extra)
208 tgl 741 ECB : {
742 : GucStack *stack;
743 :
208 tgl 744 CBC 123749 : if (extra == gconf->extra)
208 tgl 745 GIC 53377 : return true;
746 70372 : switch (gconf->vartype)
747 : {
208 tgl 748 UIC 0 : case PGC_BOOL:
749 0 : if (extra == ((struct config_bool *) gconf)->reset_extra)
750 0 : return true;
751 0 : break;
752 0 : case PGC_INT:
753 0 : if (extra == ((struct config_int *) gconf)->reset_extra)
754 0 : return true;
208 tgl 755 LBC 0 : break;
208 tgl 756 UIC 0 : case PGC_REAL:
757 0 : if (extra == ((struct config_real *) gconf)->reset_extra)
758 0 : return true;
759 0 : break;
208 tgl 760 CBC 70372 : case PGC_STRING:
208 tgl 761 GIC 70372 : if (extra == ((struct config_string *) gconf)->reset_extra)
208 tgl 762 CBC 34574 : return true;
208 tgl 763 GIC 35798 : break;
208 tgl 764 LBC 0 : case PGC_ENUM:
208 tgl 765 UIC 0 : if (extra == ((struct config_enum *) gconf)->reset_extra)
208 tgl 766 LBC 0 : return true;
767 0 : break;
768 : }
208 tgl 769 CBC 40866 : for (stack = gconf->stack; stack; stack = stack->prev)
770 : {
771 6718 : if (extra == stack->prior.extra ||
208 tgl 772 GIC 5071 : extra == stack->masked.extra)
208 tgl 773 CBC 1650 : return true;
774 : }
7242 bruce 775 ECB :
208 tgl 776 GIC 34148 : return false;
208 tgl 777 ECB : }
5835 alvherre 778 :
779 : /*
780 : * Support for assigning to an "extra" field of a GUC item. Free the prior
781 : * value if it's not referenced anywhere else in the item (including stacked
782 : * states).
783 : */
784 : static void
208 tgl 785 CBC 593027 : set_extra_field(struct config_generic *gconf, void **field, void *newval)
786 : {
208 tgl 787 GIC 593027 : void *oldval = *field;
1102 tgl 788 ECB :
208 789 : /* Do the assignment */
208 tgl 790 CBC 593027 : *field = newval;
791 :
792 : /* Free old value if it's not NULL and isn't referenced anymore */
793 593027 : if (oldval && !extra_field_used(gconf, oldval))
177 tgl 794 GNC 33942 : guc_free(oldval);
208 tgl 795 CBC 593027 : }
796 :
208 tgl 797 ECB : /*
798 : * Support for copying a variable's active value into a stack entry.
799 : * The "extra" field associated with the active value is copied, too.
800 : *
801 : * NB: be sure stringval and extra fields of a new stack entry are
802 : * initialized to NULL before this is used, else we'll try to guc_free() them.
803 : */
804 : static void
208 tgl 805 GIC 41692 : set_stack_value(struct config_generic *gconf, config_var_value *val)
208 tgl 806 ECB : {
208 tgl 807 GBC 41692 : switch (gconf->vartype)
808 : {
208 tgl 809 GIC 7922 : case PGC_BOOL:
208 tgl 810 CBC 7922 : val->val.boolval =
208 tgl 811 GIC 7922 : *((struct config_bool *) gconf)->variable;
812 7922 : break;
208 tgl 813 CBC 8613 : case PGC_INT:
814 8613 : val->val.intval =
815 8613 : *((struct config_int *) gconf)->variable;
208 tgl 816 GIC 8613 : break;
817 3410 : case PGC_REAL:
208 tgl 818 CBC 3410 : val->val.realval =
208 tgl 819 GIC 3410 : *((struct config_real *) gconf)->variable;
208 tgl 820 CBC 3410 : break;
208 tgl 821 GIC 13244 : case PGC_STRING:
208 tgl 822 CBC 13244 : set_string_field((struct config_string *) gconf,
823 : &(val->val.stringval),
208 tgl 824 GIC 13244 : *((struct config_string *) gconf)->variable);
208 tgl 825 CBC 13244 : break;
208 tgl 826 GIC 8503 : case PGC_ENUM:
208 tgl 827 CBC 8503 : val->val.enumval =
208 tgl 828 GIC 8503 : *((struct config_enum *) gconf)->variable;
208 tgl 829 CBC 8503 : break;
830 : }
831 41692 : set_extra_field(gconf, &(val->extra), gconf->extra);
832 41692 : }
7081 JanWieck 833 ECB :
208 tgl 834 : /*
835 : * Support for discarding a no-longer-needed value in a stack entry.
836 : * The "extra" field associated with the stack entry is cleared, too.
837 : */
838 : static void
208 tgl 839 GIC 20507 : discard_stack_value(struct config_generic *gconf, config_var_value *val)
208 tgl 840 ECB : {
208 tgl 841 GIC 20507 : switch (gconf->vartype)
2541 andres 842 ECB : {
208 tgl 843 GIC 14913 : case PGC_BOOL:
208 tgl 844 ECB : case PGC_INT:
208 tgl 845 EUB : case PGC_REAL:
846 : case PGC_ENUM:
208 tgl 847 ECB : /* no need to do anything */
208 tgl 848 GIC 14913 : break;
208 tgl 849 CBC 5594 : case PGC_STRING:
208 tgl 850 GIC 5594 : set_string_field((struct config_string *) gconf,
208 tgl 851 EUB : &(val->val.stringval),
852 : NULL);
208 tgl 853 GBC 5594 : break;
208 tgl 854 EUB : }
208 tgl 855 GIC 20507 : set_extra_field(gconf, &(val->extra), NULL);
856 20507 : }
2541 andres 857 ECB :
858 :
859 : /*
860 : * Fetch a palloc'd, sorted array of GUC struct pointers
861 : *
862 : * The array length is returned into *num_vars.
863 : */
864 : struct config_generic **
177 tgl 865 GNC 1088 : get_guc_variables(int *num_vars)
866 : {
867 : struct config_generic **result;
868 : HASH_SEQ_STATUS status;
869 : GUCHashEntry *hentry;
870 : int i;
871 :
872 1088 : *num_vars = hash_get_num_entries(guc_hashtab);
873 1088 : result = palloc(sizeof(struct config_generic *) * *num_vars);
874 :
875 : /* Extract pointers from the hash table */
876 1088 : i = 0;
877 1088 : hash_seq_init(&status, guc_hashtab);
878 406856 : while ((hentry = (GUCHashEntry *) hash_seq_search(&status)) != NULL)
879 405768 : result[i++] = hentry->gucvar;
880 1088 : Assert(i == *num_vars);
881 :
882 : /* Sort by name */
883 1088 : qsort(result, *num_vars,
884 : sizeof(struct config_generic *), guc_var_compare);
885 :
886 1088 : return result;
887 : }
888 :
889 :
890 : /*
891 : * Build the GUC hash table. This is split out so that help_config.c can
892 : * extract all the variables without running all of InitializeGUCOptions.
893 : * It's not meant for use anyplace else.
894 : */
895 : void
208 tgl 896 GIC 1857 : build_guc_variables(void)
897 : {
898 : int size_vars;
899 1857 : int num_vars = 0;
900 : HASHCTL hash_ctl;
901 : GUCHashEntry *hentry;
902 : bool found;
903 : int i;
904 :
905 : /*
906 : * Create the memory context that will hold all GUC-related data.
907 : */
177 tgl 908 GNC 1857 : Assert(GUCMemoryContext == NULL);
909 1857 : GUCMemoryContext = AllocSetContextCreate(TopMemoryContext,
910 : "GUCMemoryContext",
911 : ALLOCSET_DEFAULT_SIZES);
912 :
913 : /*
914 : * Count all the built-in variables, and set their vartypes correctly.
915 : */
208 tgl 916 GIC 194985 : for (i = 0; ConfigureNamesBool[i].gen.name; i++)
917 : {
918 193128 : struct config_bool *conf = &ConfigureNamesBool[i];
919 :
920 : /* Rather than requiring vartype to be filled in by hand, do this: */
921 193128 : conf->gen.vartype = PGC_BOOL;
922 193128 : num_vars++;
923 : }
924 :
925 243267 : for (i = 0; ConfigureNamesInt[i].gen.name; i++)
926 : {
927 241410 : struct config_int *conf = &ConfigureNamesInt[i];
928 :
929 241410 : conf->gen.vartype = PGC_INT;
930 241410 : num_vars++;
931 : }
932 :
933 48282 : for (i = 0; ConfigureNamesReal[i].gen.name; i++)
934 : {
935 46425 : struct config_real *conf = &ConfigureNamesReal[i];
936 :
937 46425 : conf->gen.vartype = PGC_REAL;
938 46425 : num_vars++;
939 : }
940 :
941 133704 : for (i = 0; ConfigureNamesString[i].gen.name; i++)
942 : {
943 131847 : struct config_string *conf = &ConfigureNamesString[i];
944 :
945 131847 : conf->gen.vartype = PGC_STRING;
946 131847 : num_vars++;
947 : }
948 :
949 72423 : for (i = 0; ConfigureNamesEnum[i].gen.name; i++)
950 : {
951 70566 : struct config_enum *conf = &ConfigureNamesEnum[i];
952 :
953 70566 : conf->gen.vartype = PGC_ENUM;
954 70566 : num_vars++;
955 : }
956 :
957 : /*
958 : * Create hash table with 20% slack
959 : */
960 1857 : size_vars = num_vars + num_vars / 4;
961 :
177 tgl 962 GNC 1857 : hash_ctl.keysize = sizeof(char *);
963 1857 : hash_ctl.entrysize = sizeof(GUCHashEntry);
964 1857 : hash_ctl.hash = guc_name_hash;
965 1857 : hash_ctl.match = guc_name_match;
966 1857 : hash_ctl.hcxt = GUCMemoryContext;
967 1857 : guc_hashtab = hash_create("GUC hash table",
968 : size_vars,
969 : &hash_ctl,
970 : HASH_ELEM | HASH_FUNCTION | HASH_COMPARE | HASH_CONTEXT);
971 :
208 tgl 972 GIC 194985 : for (i = 0; ConfigureNamesBool[i].gen.name; i++)
973 : {
177 tgl 974 GNC 193128 : struct config_generic *gucvar = &ConfigureNamesBool[i].gen;
975 :
976 193128 : hentry = (GUCHashEntry *) hash_search(guc_hashtab,
977 193128 : &gucvar->name,
978 : HASH_ENTER,
979 : &found);
980 193128 : Assert(!found);
981 193128 : hentry->gucvar = gucvar;
982 : }
983 :
208 tgl 984 GIC 243267 : for (i = 0; ConfigureNamesInt[i].gen.name; i++)
985 : {
177 tgl 986 GNC 241410 : struct config_generic *gucvar = &ConfigureNamesInt[i].gen;
987 :
988 241410 : hentry = (GUCHashEntry *) hash_search(guc_hashtab,
989 241410 : &gucvar->name,
990 : HASH_ENTER,
991 : &found);
992 241410 : Assert(!found);
993 241410 : hentry->gucvar = gucvar;
994 : }
995 :
208 tgl 996 GIC 48282 : for (i = 0; ConfigureNamesReal[i].gen.name; i++)
997 : {
177 tgl 998 GNC 46425 : struct config_generic *gucvar = &ConfigureNamesReal[i].gen;
999 :
1000 46425 : hentry = (GUCHashEntry *) hash_search(guc_hashtab,
1001 46425 : &gucvar->name,
1002 : HASH_ENTER,
1003 : &found);
1004 46425 : Assert(!found);
1005 46425 : hentry->gucvar = gucvar;
1006 : }
1007 :
208 tgl 1008 GIC 133704 : for (i = 0; ConfigureNamesString[i].gen.name; i++)
1009 : {
177 tgl 1010 GNC 131847 : struct config_generic *gucvar = &ConfigureNamesString[i].gen;
1011 :
1012 131847 : hentry = (GUCHashEntry *) hash_search(guc_hashtab,
1013 131847 : &gucvar->name,
1014 : HASH_ENTER,
1015 : &found);
1016 131847 : Assert(!found);
1017 131847 : hentry->gucvar = gucvar;
1018 : }
1019 :
208 tgl 1020 GIC 72423 : for (i = 0; ConfigureNamesEnum[i].gen.name; i++)
1021 : {
177 tgl 1022 GNC 70566 : struct config_generic *gucvar = &ConfigureNamesEnum[i].gen;
1023 :
1024 70566 : hentry = (GUCHashEntry *) hash_search(guc_hashtab,
1025 70566 : &gucvar->name,
1026 : HASH_ENTER,
1027 : &found);
1028 70566 : Assert(!found);
1029 70566 : hentry->gucvar = gucvar;
1030 : }
1031 :
1032 1857 : Assert(num_vars == hash_get_num_entries(guc_hashtab));
208 tgl 1033 GIC 1857 : }
1034 :
1035 : /*
1036 : * Add a new GUC variable to the hash of known variables. The
1037 : * hash is expanded if needed.
1038 : */
1039 : static bool
1040 9174 : add_guc_variable(struct config_generic *var, int elevel)
1041 : {
1042 : GUCHashEntry *hentry;
1043 : bool found;
1044 :
177 tgl 1045 GNC 9174 : hentry = (GUCHashEntry *) hash_search(guc_hashtab,
1046 9174 : &var->name,
1047 : HASH_ENTER_NULL,
1048 : &found);
1049 9174 : if (unlikely(hentry == NULL))
1050 : {
177 tgl 1051 UNC 0 : ereport(elevel,
1052 : (errcode(ERRCODE_OUT_OF_MEMORY),
1053 : errmsg("out of memory")));
1054 0 : return false; /* out of memory */
1055 : }
177 tgl 1056 GNC 9174 : Assert(!found);
1057 9174 : hentry->gucvar = var;
208 tgl 1058 GIC 9174 : return true;
1059 : }
1060 :
1061 : /*
1062 : * Decide whether a proposed custom variable name is allowed.
1063 : *
1064 : * It must be two or more identifiers separated by dots, where the rules
1065 : * for what is an identifier agree with scan.l. (If you change this rule,
1066 : * adjust the errdetail in find_option().)
1067 : */
1068 : static bool
1069 83 : valid_custom_variable_name(const char *name)
1070 : {
1071 83 : bool saw_sep = false;
1072 83 : bool name_start = true;
1073 :
1074 2013 : for (const char *p = name; *p; p++)
1075 : {
1076 1936 : if (*p == GUC_QUALIFIER_SEPARATOR)
1077 : {
1078 87 : if (name_start)
208 tgl 1079 UIC 0 : return false; /* empty name component */
208 tgl 1080 GIC 87 : saw_sep = true;
1081 87 : name_start = true;
1082 : }
1083 1849 : else if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1084 1849 : "abcdefghijklmnopqrstuvwxyz_", *p) != NULL ||
1085 8 : IS_HIGHBIT_SET(*p))
1086 : {
1087 : /* okay as first or non-first character */
1088 1841 : name_start = false;
1089 : }
1090 8 : else if (!name_start && strchr("0123456789$", *p) != NULL)
1091 : /* okay as non-first character */ ;
1092 : else
1093 6 : return false;
1094 : }
1095 77 : if (name_start)
208 tgl 1096 UIC 0 : return false; /* empty name component */
1097 : /* OK if we found at least one separator */
208 tgl 1098 GIC 77 : return saw_sep;
1099 : }
1100 :
1101 : /*
1102 : * Create and add a placeholder variable for a custom variable name.
1103 : */
1104 : static struct config_generic *
1105 52 : add_placeholder_variable(const char *name, int elevel)
1106 : {
1107 52 : size_t sz = sizeof(struct config_string) + sizeof(char *);
1108 : struct config_string *var;
1109 : struct config_generic *gen;
1110 :
1111 52 : var = (struct config_string *) guc_malloc(elevel, sz);
1112 52 : if (var == NULL)
208 tgl 1113 UIC 0 : return NULL;
208 tgl 1114 GIC 52 : memset(var, 0, sz);
1115 52 : gen = &var->gen;
1116 :
1117 52 : gen->name = guc_strdup(elevel, name);
1118 52 : if (gen->name == NULL)
1119 : {
177 tgl 1120 UNC 0 : guc_free(var);
208 tgl 1121 UIC 0 : return NULL;
1122 : }
1123 :
208 tgl 1124 GIC 52 : gen->context = PGC_USERSET;
1125 52 : gen->group = CUSTOM_OPTIONS;
1126 52 : gen->short_desc = "GUC placeholder variable";
1127 52 : gen->flags = GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE | GUC_CUSTOM_PLACEHOLDER;
1128 52 : gen->vartype = PGC_STRING;
1129 :
1130 : /*
1131 : * The char* is allocated at the end of the struct since we have no
1132 : * 'static' place to point to. Note that the current value, as well as
1133 : * the boot and reset values, start out NULL.
1134 : */
1135 52 : var->variable = (char **) (var + 1);
1136 :
1137 52 : if (!add_guc_variable((struct config_generic *) var, elevel))
1138 : {
177 tgl 1139 UNC 0 : guc_free(unconstify(char *, gen->name));
1140 0 : guc_free(var);
208 tgl 1141 UIC 0 : return NULL;
1142 : }
1143 :
208 tgl 1144 GIC 52 : return gen;
1145 : }
1146 :
1147 : /*
1148 : * Look up option "name". If it exists, return a pointer to its record.
1149 : * Otherwise, if create_placeholders is true and name is a valid-looking
1150 : * custom variable name, we'll create and return a placeholder record.
1151 : * Otherwise, if skip_errors is true, then we silently return NULL for
1152 : * an unrecognized or invalid name. Otherwise, the error is reported at
1153 : * error level elevel (and we return NULL if that's less than ERROR).
1154 : *
1155 : * Note: internal errors, primarily out-of-memory, draw an elevel-level
1156 : * report and NULL return regardless of skip_errors. Hence, callers must
1157 : * handle a NULL return whenever elevel < ERROR, but they should not need
1158 : * to emit any additional error message. (In practice, internal errors
1159 : * can only happen when create_placeholders is true, so callers passing
1160 : * false need not think terribly hard about this.)
1161 : */
1162 : struct config_generic *
1163 380725 : find_option(const char *name, bool create_placeholders, bool skip_errors,
1164 : int elevel)
1165 : {
1166 : GUCHashEntry *hentry;
208 tgl 1167 ECB : int i;
1168 :
208 tgl 1169 GIC 380725 : Assert(name);
1170 :
1171 : /* Look it up using the hash table. */
177 tgl 1172 GNC 380725 : hentry = (GUCHashEntry *) hash_search(guc_hashtab,
1173 : &name,
1174 : HASH_FIND,
1175 : NULL);
1176 380725 : if (hentry)
1177 380558 : return hentry->gucvar;
1178 :
1179 : /*
1180 : * See if the name is an obsolete name for a variable. We assume that the
1181 : * set of supported old names is short enough that a brute-force search is
1182 : * the best way.
1183 : */
208 tgl 1184 GIC 668 : for (i = 0; map_old_guc_names[i] != NULL; i += 2)
1185 : {
1186 501 : if (guc_name_compare(name, map_old_guc_names[i]) == 0)
208 tgl 1187 UIC 0 : return find_option(map_old_guc_names[i + 1], false,
1188 : skip_errors, elevel);
1189 : }
1190 :
208 tgl 1191 CBC 167 : if (create_placeholders)
5455 tgl 1192 ECB : {
208 1193 : /*
1194 : * Check if the name is valid, and if so, add a placeholder. If it
1195 : * doesn't contain a separator, don't assume that it was meant to be a
1196 : * placeholder.
1197 : */
208 tgl 1198 GIC 88 : const char *sep = strchr(name, GUC_QUALIFIER_SEPARATOR);
1199 :
1200 88 : if (sep != NULL)
1201 : {
1202 61 : size_t classLen = sep - name;
208 tgl 1203 ECB : ListCell *lc;
1204 :
1205 : /* The name must be syntactically acceptable ... */
208 tgl 1206 CBC 61 : if (!valid_custom_variable_name(name))
1207 : {
208 tgl 1208 GIC 6 : if (!skip_errors)
208 tgl 1209 CBC 6 : ereport(elevel,
208 tgl 1210 ECB : (errcode(ERRCODE_INVALID_NAME),
1211 : errmsg("invalid configuration parameter name \"%s\"",
1212 : name),
1213 : errdetail("Custom parameter names must be two or more simple identifiers separated by dots.")));
208 tgl 1214 UIC 0 : return NULL;
1215 : }
208 tgl 1216 ECB : /* ... and it must not match any previously-reserved prefix */
208 tgl 1217 GIC 63 : foreach(lc, reserved_class_prefix)
208 tgl 1218 ECB : {
208 tgl 1219 GIC 11 : const char *rcprefix = lfirst(lc);
7836 bruce 1220 ECB :
208 tgl 1221 GIC 11 : if (strlen(rcprefix) == classLen &&
208 tgl 1222 CBC 3 : strncmp(name, rcprefix, classLen) == 0)
1223 : {
1224 3 : if (!skip_errors)
208 tgl 1225 GIC 3 : ereport(elevel,
208 tgl 1226 ECB : (errcode(ERRCODE_INVALID_NAME),
1227 : errmsg("invalid configuration parameter name \"%s\"",
1228 : name),
1229 : errdetail("\"%s\" is a reserved prefix.",
1230 : rcprefix)));
208 tgl 1231 UIC 0 : return NULL;
1232 : }
1233 : }
208 tgl 1234 ECB : /* OK, create it */
208 tgl 1235 CBC 52 : return add_placeholder_variable(name, elevel);
1236 : }
208 tgl 1237 ECB : }
1238 :
1239 : /* Unknown name */
208 tgl 1240 GIC 106 : if (!skip_errors)
208 tgl 1241 CBC 45 : ereport(elevel,
1242 : (errcode(ERRCODE_UNDEFINED_OBJECT),
1243 : errmsg("unrecognized configuration parameter \"%s\"",
1244 : name)));
208 tgl 1245 GIC 61 : return NULL;
1246 : }
1247 :
1248 :
208 tgl 1249 ECB : /*
1250 : * comparator for qsorting an array of GUC pointers
1251 : */
1252 : static int
208 tgl 1253 CBC 3657974 : guc_var_compare(const void *a, const void *b)
1254 : {
1255 3657974 : const struct config_generic *confa = *(struct config_generic *const *) a;
208 tgl 1256 GIC 3657974 : const struct config_generic *confb = *(struct config_generic *const *) b;
1257 :
1258 3657974 : return guc_name_compare(confa->name, confb->name);
1259 : }
1260 :
1261 : /*
208 tgl 1262 ECB : * the bare comparison function for GUC names
1263 : */
1264 : int
208 tgl 1265 GBC 4245081 : guc_name_compare(const char *namea, const char *nameb)
1266 : {
208 tgl 1267 ECB : /*
1268 : * The temptation to use strcasecmp() here must be resisted, because the
1269 : * hash mapping has to remain stable across setlocale() calls. So, build
1270 : * our own with a simple ASCII-only downcasing.
1271 : */
208 tgl 1272 GIC 17611506 : while (*namea && *nameb)
1252 tomas.vondra 1273 ECB : {
208 tgl 1274 GIC 17184058 : char cha = *namea++;
208 tgl 1275 CBC 17184058 : char chb = *nameb++;
1276 :
208 tgl 1277 GIC 17184058 : if (cha >= 'A' && cha <= 'Z')
1278 88457 : cha += 'a' - 'A';
208 tgl 1279 CBC 17184058 : if (chb >= 'A' && chb <= 'Z')
208 tgl 1280 GIC 24255 : chb += 'a' - 'A';
208 tgl 1281 CBC 17184058 : if (cha != chb)
1282 3817633 : return cha - chb;
1283 : }
208 tgl 1284 GIC 427448 : if (*namea)
208 tgl 1285 CBC 32380 : return 1; /* a is longer */
208 tgl 1286 GIC 395068 : if (*nameb)
1287 14374 : return -1; /* b is longer */
208 tgl 1288 CBC 380694 : return 0;
208 tgl 1289 ECB : }
1290 :
1291 : /*
1292 : * Hash function that's compatible with guc_name_compare
1293 : */
1294 : static uint32
177 tgl 1295 GNC 1082451 : guc_name_hash(const void *key, Size keysize)
1296 : {
1297 1082451 : uint32 result = 0;
1298 1082451 : const char *name = *(const char *const *) key;
1299 :
1300 19927579 : while (*name)
1301 : {
1302 18845128 : char ch = *name++;
1303 :
1304 : /* Case-fold in the same way as guc_name_compare */
1305 18845128 : if (ch >= 'A' && ch <= 'Z')
1306 24316 : ch += 'a' - 'A';
1307 :
1308 : /* Merge into hash ... not very bright, but it needn't be */
1309 18845128 : result = pg_rotate_left32(result, 5);
1310 18845128 : result ^= (uint32) ch;
1311 : }
1312 1082451 : return result;
1313 : }
1314 :
1315 : /*
1316 : * Dynahash match function to use in guc_hashtab
1317 : */
1318 : static int
1319 380612 : guc_name_match(const void *key1, const void *key2, Size keysize)
1320 : {
1321 380612 : const char *name1 = *(const char *const *) key1;
1322 380612 : const char *name2 = *(const char *const *) key2;
1323 :
1324 380612 : return guc_name_compare(name1, name2);
1325 : }
1326 :
8348 peter_e 1327 ECB :
208 tgl 1328 : /*
1329 : * Convert a GUC name to the form that should be used in pg_parameter_acl.
1330 : *
1331 : * We need to canonicalize entries since, for example, case should not be
1332 : * significant. In addition, we apply the map_old_guc_names[] mapping so that
1333 : * any obsolete names will be converted when stored in a new PG version.
1334 : * Note however that this function does not verify legality of the name.
1335 : *
1336 : * The result is a palloc'd string.
1337 : */
1338 : char *
208 tgl 1339 CBC 183 : convert_GUC_name_for_parameter_acl(const char *name)
1340 : {
1341 : char *result;
1342 :
1343 : /* Apply old-GUC-name mapping. */
208 tgl 1344 GIC 732 : for (int i = 0; map_old_guc_names[i] != NULL; i += 2)
1596 peter_e 1345 ECB : {
208 tgl 1346 GIC 549 : if (guc_name_compare(name, map_old_guc_names[i]) == 0)
1347 : {
208 tgl 1348 UIC 0 : name = map_old_guc_names[i + 1];
1349 0 : break;
1350 : }
1351 : }
1596 peter_e 1352 ECB :
1353 : /* Apply case-folding that matches guc_name_compare(). */
208 tgl 1354 CBC 183 : result = pstrdup(name);
208 tgl 1355 GIC 3168 : for (char *ptr = result; *ptr != '\0'; ptr++)
1356 : {
208 tgl 1357 CBC 2985 : char ch = *ptr;
1358 :
208 tgl 1359 GIC 2985 : if (ch >= 'A' && ch <= 'Z')
208 tgl 1360 ECB : {
208 tgl 1361 GIC 6 : ch += 'a' - 'A';
1362 6 : *ptr = ch;
1363 : }
1364 : }
1365 :
1366 183 : return result;
1367 : }
1368 :
1369 : /*
208 tgl 1370 ECB : * Check whether we should allow creation of a pg_parameter_acl entry
1371 : * for the given name. (This can be applied either before or after
1372 : * canonicalizing it.)
1373 : */
1374 : bool
208 tgl 1375 CBC 35 : check_GUC_name_for_parameter_acl(const char *name)
208 tgl 1376 EUB : {
1377 : /* OK if the GUC exists. */
208 tgl 1378 CBC 35 : if (find_option(name, false, true, DEBUG1) != NULL)
1379 28 : return true;
208 tgl 1380 ECB : /* Otherwise, it'd better be a valid custom GUC name. */
208 tgl 1381 GIC 7 : if (valid_custom_variable_name(name))
208 tgl 1382 CBC 6 : return true;
208 tgl 1383 GIC 1 : return false;
1384 : }
6970 bruce 1385 EUB :
1386 : /*
1387 : * Routine in charge of checking various states of a GUC.
1388 : *
1389 : * This performs two sanity checks. First, it checks that the initial
1390 : * value of a GUC is the same when declared and when loaded to prevent
1391 : * anybody looking at the C declarations of these GUCS from being fooled by
1392 : * mismatched values. Second, it checks for incorrect flag combinations.
1393 : *
1394 : * The following validation rules apply for the values:
1395 : * bool - can be false, otherwise must be same as the boot_val
1396 : * int - can be 0, otherwise must be same as the boot_val
1397 : * real - can be 0.0, otherwise must be same as the boot_val
1398 : * string - can be NULL, otherwise must be strcmp equal to the boot_val
1399 : * enum - must be same as the boot_val
1400 : */
1401 : #ifdef USE_ASSERT_CHECKING
1402 : static bool
160 michael 1403 GNC 692549 : check_GUC_init(struct config_generic *gconf)
1404 : {
1405 : /* Checks on values */
1406 692549 : switch (gconf->vartype)
1407 : {
1408 196805 : case PGC_BOOL:
1409 : {
1410 196805 : struct config_bool *conf = (struct config_bool *) gconf;
1411 :
1412 196805 : if (*conf->variable && !conf->boot_val)
1413 : {
160 michael 1414 UNC 0 : elog(LOG, "GUC (PGC_BOOL) %s, boot_val=%d, C-var=%d",
1415 : conf->gen.name, conf->boot_val, *conf->variable);
1416 0 : return false;
1417 : }
160 michael 1418 GNC 196805 : break;
1419 : }
1420 241446 : case PGC_INT:
1421 : {
1422 241446 : struct config_int *conf = (struct config_int *) gconf;
1423 :
1424 241446 : if (*conf->variable != 0 && *conf->variable != conf->boot_val)
1425 : {
160 michael 1426 UNC 0 : elog(LOG, "GUC (PGC_INT) %s, boot_val=%d, C-var=%d",
1427 : conf->gen.name, conf->boot_val, *conf->variable);
1428 0 : return false;
1429 : }
160 michael 1430 GNC 241446 : break;
1431 : }
1432 46446 : case PGC_REAL:
1433 : {
1434 46446 : struct config_real *conf = (struct config_real *) gconf;
1435 :
1436 46446 : if (*conf->variable != 0.0 && *conf->variable != conf->boot_val)
1437 : {
160 michael 1438 UNC 0 : elog(LOG, "GUC (PGC_REAL) %s, boot_val=%g, C-var=%g",
1439 : conf->gen.name, conf->boot_val, *conf->variable);
1440 0 : return false;
1441 : }
160 michael 1442 GNC 46446 : break;
1443 : }
1444 135490 : case PGC_STRING:
1445 : {
1446 135490 : struct config_string *conf = (struct config_string *) gconf;
1447 :
1448 135490 : if (*conf->variable != NULL && strcmp(*conf->variable, conf->boot_val) != 0)
1449 : {
160 michael 1450 UNC 0 : elog(LOG, "GUC (PGC_STRING) %s, boot_val=%s, C-var=%s",
1451 : conf->gen.name, conf->boot_val ? conf->boot_val : "<null>", *conf->variable);
1452 0 : return false;
1453 : }
160 michael 1454 GNC 135490 : break;
1455 : }
1456 72362 : case PGC_ENUM:
1457 : {
1458 72362 : struct config_enum *conf = (struct config_enum *) gconf;
1459 :
1460 72362 : if (*conf->variable != conf->boot_val)
1461 : {
160 michael 1462 UNC 0 : elog(LOG, "GUC (PGC_ENUM) %s, boot_val=%d, C-var=%d",
1463 : conf->gen.name, conf->boot_val, *conf->variable);
1464 0 : return false;
1465 : }
160 michael 1466 GNC 72362 : break;
1467 : }
1468 : }
1469 :
1470 : /* Flag combinations */
1471 :
1472 : /*
1473 : * GUC_NO_SHOW_ALL requires GUC_NOT_IN_SAMPLE, as a parameter not part
1474 : * of SHOW ALL should not be hidden in postgresql.conf.sample.
1475 : */
62 1476 692549 : if ((gconf->flags & GUC_NO_SHOW_ALL) &&
1477 11142 : !(gconf->flags & GUC_NOT_IN_SAMPLE))
1478 : {
62 michael 1479 UNC 0 : elog(LOG, "GUC %s flags: NO_SHOW_ALL and !NOT_IN_SAMPLE",
1480 : gconf->name);
1481 0 : return false;
1482 : }
1483 :
160 michael 1484 GNC 692549 : return true;
1485 : }
1486 : #endif
1487 :
208 tgl 1488 ECB : /*
1489 : * Initialize GUC options during program startup.
1490 : *
208 tgl 1491 EUB : * Note that we cannot read the config file yet, since we have not yet
1492 : * processed command-line switches.
1493 : */
1494 : void
208 tgl 1495 CBC 1857 : InitializeGUCOptions(void)
208 tgl 1496 ECB : {
1497 : HASH_SEQ_STATUS status;
1498 : GUCHashEntry *hentry;
1499 :
1500 : /*
1501 : * Before log_line_prefix could possibly receive a nonempty setting, make
1502 : * sure that timezone processing is minimally alive (see elog.c).
1503 : */
208 tgl 1504 CBC 1857 : pg_timezone_initialize();
6729 tgl 1505 EUB :
1506 : /*
1507 : * Create GUCMemoryContext and build hash table of all GUC variables.
208 tgl 1508 ECB : */
208 tgl 1509 CBC 1857 : build_guc_variables();
5789 tgl 1510 ECB :
1511 : /*
1512 : * Load all variables with their compiled-in defaults, and initialize
1513 : * status fields as needed.
1514 : */
177 tgl 1515 GNC 1857 : hash_seq_init(&status, guc_hashtab);
1516 685233 : while ((hentry = (GUCHashEntry *) hash_seq_search(&status)) != NULL)
1517 : {
1518 : /* Check mapping between initial and default value */
160 michael 1519 683376 : Assert(check_GUC_init(hentry->gucvar));
1520 :
177 tgl 1521 683376 : InitializeOneGUCOption(hentry->gucvar);
1522 : }
7632 tgl 1523 ECB :
208 tgl 1524 CBC 1857 : reporting_enabled = false;
1525 :
208 tgl 1526 ECB : /*
1527 : * Prevent any attempt to override the transaction modes from
1528 : * non-interactive sources.
1529 : */
208 tgl 1530 CBC 1857 : SetConfigOption("transaction_isolation", "read committed",
1531 : PGC_POSTMASTER, PGC_S_OVERRIDE);
1532 1857 : SetConfigOption("transaction_read_only", "no",
1533 : PGC_POSTMASTER, PGC_S_OVERRIDE);
1534 1857 : SetConfigOption("transaction_deferrable", "no",
208 tgl 1535 ECB : PGC_POSTMASTER, PGC_S_OVERRIDE);
1536 :
1537 : /*
1538 : * For historical reasons, some GUC parameters can receive defaults from
1539 : * environment variables. Process those settings.
1540 : */
208 tgl 1541 CBC 1857 : InitializeGUCOptionsFromEnvironment();
208 tgl 1542 GIC 1857 : }
7289 tgl 1543 ECB :
208 1544 : /*
1545 : * Assign any GUC values that can come from the server's environment.
1546 : *
1547 : * This is called from InitializeGUCOptions, and also from ProcessConfigFile
1548 : * to deal with the possibility that a setting has been removed from
1549 : * postgresql.conf and should now get a value from the environment.
1550 : * (The latter is a kludge that should probably go away someday; if so,
1551 : * fold this back into InitializeGUCOptions.)
1552 : */
1553 : static void
208 tgl 1554 CBC 2195 : InitializeGUCOptionsFromEnvironment(void)
1555 : {
208 tgl 1556 ECB : char *env;
1557 : long stack_rlimit;
7289 1558 :
208 tgl 1559 GIC 2195 : env = getenv("PGPORT");
1560 2195 : if (env != NULL)
1561 2116 : SetConfigOption("port", env, PGC_POSTMASTER, PGC_S_ENV_VAR);
1562 :
1563 2195 : env = getenv("PGDATESTYLE");
1564 2195 : if (env != NULL)
1565 418 : SetConfigOption("datestyle", env, PGC_POSTMASTER, PGC_S_ENV_VAR);
1566 :
208 tgl 1567 CBC 2195 : env = getenv("PGCLIENTENCODING");
208 tgl 1568 GIC 2195 : if (env != NULL)
1569 18 : SetConfigOption("client_encoding", env, PGC_POSTMASTER, PGC_S_ENV_VAR);
1570 :
1571 : /*
1572 : * rlimit isn't exactly an "environment variable", but it behaves about
1573 : * the same. If we can identify the platform stack depth rlimit, increase
1574 : * default stack depth setting up to whatever is safe (but at most 2MB).
208 tgl 1575 ECB : * Report the value's source as PGC_S_DYNAMIC_DEFAULT if it's 2MB, or as
1576 : * PGC_S_ENV_VAR if it's reflecting the rlimit limit.
1577 : */
208 tgl 1578 GIC 2195 : stack_rlimit = get_stack_depth_rlimit();
1579 2195 : if (stack_rlimit > 0)
7632 tgl 1580 ECB : {
208 tgl 1581 GIC 2195 : long new_limit = (stack_rlimit - STACK_DEPTH_SLOP) / 1024L;
7676 peter_e 1582 ECB :
208 tgl 1583 GIC 2195 : if (new_limit > 100)
1584 : {
208 tgl 1585 ECB : GucSource source;
1586 : char limbuf[16];
1587 :
208 tgl 1588 GIC 2195 : if (new_limit < 2048)
208 tgl 1589 LBC 0 : source = PGC_S_ENV_VAR;
208 tgl 1590 ECB : else
1591 : {
208 tgl 1592 GIC 2195 : new_limit = 2048;
1593 2195 : source = PGC_S_DYNAMIC_DEFAULT;
1594 : }
1595 2195 : snprintf(limbuf, sizeof(limbuf), "%ld", new_limit);
1596 2195 : SetConfigOption("max_stack_depth", limbuf,
1597 : PGC_POSTMASTER, source);
1598 : }
1599 : }
208 tgl 1600 CBC 2195 : }
1601 :
208 tgl 1602 ECB : /*
1603 : * Initialize one GUC option variable to its compiled-in default.
1604 : *
1605 : * Note: the reason for calling check_hooks is not that we think the boot_val
208 tgl 1606 EUB : * might fail, but that the hooks might wish to compute an "extra" struct.
1607 : */
1608 : static void
208 tgl 1609 CBC 724966 : InitializeOneGUCOption(struct config_generic *gconf)
1610 : {
208 tgl 1611 GIC 724966 : gconf->status = 0;
208 tgl 1612 CBC 724966 : gconf->source = PGC_S_DEFAULT;
208 tgl 1613 GBC 724966 : gconf->reset_source = PGC_S_DEFAULT;
208 tgl 1614 GIC 724966 : gconf->scontext = PGC_INTERNAL;
1615 724966 : gconf->reset_scontext = PGC_INTERNAL;
208 tgl 1616 CBC 724966 : gconf->srole = BOOTSTRAP_SUPERUSERID;
208 tgl 1617 GIC 724966 : gconf->reset_srole = BOOTSTRAP_SUPERUSERID;
208 tgl 1618 CBC 724966 : gconf->stack = NULL;
208 tgl 1619 GIC 724966 : gconf->extra = NULL;
1620 724966 : gconf->last_reported = NULL;
1621 724966 : gconf->sourcefile = NULL;
1622 724966 : gconf->sourceline = 0;
7325 bruce 1623 ECB :
208 tgl 1624 GIC 724966 : switch (gconf->vartype)
7676 peter_e 1625 ECB : {
208 tgl 1626 GBC 203725 : case PGC_BOOL:
1627 : {
208 tgl 1628 CBC 203725 : struct config_bool *conf = (struct config_bool *) gconf;
1629 203725 : bool newval = conf->boot_val;
1630 203725 : void *extra = NULL;
1631 :
208 tgl 1632 GIC 203725 : if (!call_bool_check_hook(conf, &newval, &extra,
1633 : PGC_S_DEFAULT, LOG))
208 tgl 1634 UIC 0 : elog(FATAL, "failed to initialize %s to %d",
1635 : conf->gen.name, (int) newval);
208 tgl 1636 GIC 203725 : if (conf->assign_hook)
208 tgl 1637 UBC 0 : conf->assign_hook(newval, extra);
208 tgl 1638 GIC 203725 : *conf->variable = conf->reset_val = newval;
208 tgl 1639 GBC 203725 : conf->gen.extra = conf->reset_extra = extra;
208 tgl 1640 GIC 203725 : break;
208 tgl 1641 EUB : }
208 tgl 1642 GBC 247505 : case PGC_INT:
1643 : {
1644 247505 : struct config_int *conf = (struct config_int *) gconf;
1645 247505 : int newval = conf->boot_val;
208 tgl 1646 GIC 247505 : void *extra = NULL;
1647 :
1648 247505 : Assert(newval >= conf->min);
1649 247505 : Assert(newval <= conf->max);
1650 247505 : if (!call_int_check_hook(conf, &newval, &extra,
1651 : PGC_S_DEFAULT, LOG))
208 tgl 1652 UIC 0 : elog(FATAL, "failed to initialize %s to %d",
1653 : conf->gen.name, newval);
208 tgl 1654 GIC 247505 : if (conf->assign_hook)
1655 15595 : conf->assign_hook(newval, extra);
1656 247505 : *conf->variable = conf->reset_val = newval;
1657 247505 : conf->gen.extra = conf->reset_extra = extra;
1658 247505 : break;
208 tgl 1659 ECB : }
208 tgl 1660 GIC 46446 : case PGC_REAL:
1661 : {
1662 46446 : struct config_real *conf = (struct config_real *) gconf;
1663 46446 : double newval = conf->boot_val;
1664 46446 : void *extra = NULL;
1665 :
1666 46446 : Assert(newval >= conf->min);
1667 46446 : Assert(newval <= conf->max);
208 tgl 1668 CBC 46446 : if (!call_real_check_hook(conf, &newval, &extra,
1669 : PGC_S_DEFAULT, LOG))
208 tgl 1670 UIC 0 : elog(FATAL, "failed to initialize %s to %g",
1671 : conf->gen.name, newval);
208 tgl 1672 GIC 46446 : if (conf->assign_hook)
1673 3714 : conf->assign_hook(newval, extra);
1674 46446 : *conf->variable = conf->reset_val = newval;
1675 46446 : conf->gen.extra = conf->reset_extra = extra;
1676 46446 : break;
1677 : }
1678 151060 : case PGC_STRING:
1679 : {
1680 151060 : struct config_string *conf = (struct config_string *) gconf;
1681 : char *newval;
1682 151060 : void *extra = NULL;
1683 :
1684 : /* non-NULL boot_val must always get strdup'd */
1685 151060 : if (conf->boot_val != NULL)
1686 135374 : newval = guc_strdup(FATAL, conf->boot_val);
1687 : else
1688 15686 : newval = NULL;
1689 :
1690 151060 : if (!call_string_check_hook(conf, &newval, &extra,
1691 : PGC_S_DEFAULT, LOG))
208 tgl 1692 UIC 0 : elog(FATAL, "failed to initialize %s to \"%s\"",
1693 : conf->gen.name, newval ? newval : "");
208 tgl 1694 GIC 151060 : if (conf->assign_hook)
1695 69804 : conf->assign_hook(newval, extra);
208 tgl 1696 CBC 151060 : *conf->variable = conf->reset_val = newval;
208 tgl 1697 GIC 151060 : conf->gen.extra = conf->reset_extra = extra;
208 tgl 1698 CBC 151060 : break;
1699 : }
208 tgl 1700 GIC 76230 : case PGC_ENUM:
1701 : {
208 tgl 1702 CBC 76230 : struct config_enum *conf = (struct config_enum *) gconf;
1703 76230 : int newval = conf->boot_val;
208 tgl 1704 GIC 76230 : void *extra = NULL;
1705 :
1706 76230 : if (!call_enum_check_hook(conf, &newval, &extra,
1707 : PGC_S_DEFAULT, LOG))
208 tgl 1708 UIC 0 : elog(FATAL, "failed to initialize %s to %d",
1709 : conf->gen.name, newval);
208 tgl 1710 GIC 76230 : if (conf->assign_hook)
1711 9285 : conf->assign_hook(newval, extra);
1712 76230 : *conf->variable = conf->reset_val = newval;
208 tgl 1713 CBC 76230 : conf->gen.extra = conf->reset_extra = extra;
1714 76230 : break;
208 tgl 1715 ECB : }
1716 : }
208 tgl 1717 CBC 724966 : }
1718 :
1719 : /*
1720 : * Summarily remove a GUC variable from any linked lists it's in.
1721 : *
1722 : * We use this in cases where the variable is about to be deleted or reset.
1723 : * These aren't common operations, so it's okay if this is a bit slow.
1724 : */
1725 : static void
177 tgl 1726 GNC 32471 : RemoveGUCFromLists(struct config_generic *gconf)
1727 : {
1728 32471 : if (gconf->source != PGC_S_DEFAULT)
1729 32471 : dlist_delete(&gconf->nondef_link);
1730 32471 : if (gconf->stack != NULL)
177 tgl 1731 UNC 0 : slist_delete(&guc_stack_list, &gconf->stack_link);
177 tgl 1732 GNC 32471 : if (gconf->status & GUC_NEEDS_REPORT)
1733 5192 : slist_delete(&guc_report_list, &gconf->report_link);
1734 32471 : }
1735 :
4184 magnus 1736 ECB :
1737 : /*
208 tgl 1738 : * Select the configuration files and data directory to be used, and
1739 : * do the initial read of postgresql.conf.
1740 : *
208 tgl 1741 EUB : * This is called after processing command-line switches.
208 tgl 1742 ECB : * userDoption is the -D switch value if any (NULL if unspecified).
1743 : * progname is just for use in error messages.
1744 : *
1745 : * Returns true on success; on failure, prints a suitable error message
1746 : * to stderr and returns false.
1747 : */
1748 : bool
208 tgl 1749 GBC 1833 : SelectConfigFiles(const char *userDoption, const char *progname)
208 tgl 1750 ECB : {
1751 : char *configdir;
208 tgl 1752 EUB : char *fname;
1753 : bool fname_is_malloced;
1754 : struct stat stat_buf;
1755 : struct config_string *data_directory_rec;
7632 1756 :
1757 : /* configdir is -D option, or $PGDATA if no -D */
208 tgl 1758 GBC 1833 : if (userDoption)
1759 612 : configdir = make_absolute_path(userDoption);
208 tgl 1760 EUB : else
208 tgl 1761 GIC 1221 : configdir = make_absolute_path(getenv("PGDATA"));
8059 tgl 1762 ECB :
208 tgl 1763 GIC 1833 : if (configdir && stat(configdir, &stat_buf) != 0)
7836 bruce 1764 ECB : {
208 tgl 1765 UIC 0 : write_stderr("%s: could not access directory \"%s\": %s\n",
208 tgl 1766 ECB : progname,
1767 : configdir,
208 tgl 1768 UBC 0 : strerror(errno));
208 tgl 1769 LBC 0 : if (errno == ENOENT)
208 tgl 1770 UBC 0 : write_stderr("Run initdb or pg_basebackup to initialize a PostgreSQL data directory.\n");
208 tgl 1771 LBC 0 : return false;
1772 : }
8182 bruce 1773 ECB :
1774 : /*
208 tgl 1775 : * Find the configuration file: if config_file was specified on the
1776 : * command line, use it, else use configdir/postgresql.conf. In any case
1777 : * ensure the result is an absolute path, so that it will be interpreted
208 tgl 1778 EUB : * the same way by future backends.
208 tgl 1779 ECB : */
208 tgl 1780 GIC 1833 : if (ConfigFileName)
1781 : {
1782 10 : fname = make_absolute_path(ConfigFileName);
177 tgl 1783 GNC 10 : fname_is_malloced = true;
1784 : }
208 tgl 1785 GIC 1823 : else if (configdir)
7836 bruce 1786 ECB : {
208 tgl 1787 GIC 1823 : fname = guc_malloc(FATAL,
208 tgl 1788 CBC 1823 : strlen(configdir) + strlen(CONFIG_FILENAME) + 2);
208 tgl 1789 GIC 1823 : sprintf(fname, "%s/%s", configdir, CONFIG_FILENAME);
177 tgl 1790 GNC 1823 : fname_is_malloced = false;
1791 : }
1792 : else
6846 bruce 1793 ECB : {
208 tgl 1794 UIC 0 : write_stderr("%s does not know where to find the server configuration file.\n"
208 tgl 1795 ECB : "You must specify the --config-file or -D invocation "
1796 : "option or set the PGDATA environment variable.\n",
1797 : progname);
208 tgl 1798 UIC 0 : return false;
208 tgl 1799 ECB : }
6846 bruce 1800 :
208 tgl 1801 : /*
1802 : * Set the ConfigFileName GUC variable to its final value, ensuring that
1803 : * it can't be overridden later.
1804 : */
208 tgl 1805 CBC 1833 : SetConfigOption("config_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
1806 :
177 tgl 1807 GNC 1833 : if (fname_is_malloced)
1808 10 : free(fname);
1809 : else
1810 1823 : guc_free(fname);
1811 :
1812 : /*
1813 : * Now read the config file for the first time.
208 tgl 1814 ECB : */
208 tgl 1815 CBC 1833 : if (stat(ConfigFileName, &stat_buf) != 0)
6756 tgl 1816 ECB : {
208 tgl 1817 LBC 0 : write_stderr("%s: could not access the server configuration file \"%s\": %s\n",
1818 0 : progname, ConfigFileName, strerror(errno));
208 tgl 1819 UIC 0 : free(configdir);
1820 0 : return false;
208 tgl 1821 ECB : }
6846 bruce 1822 :
1823 : /*
208 tgl 1824 : * Read the configuration file for the first time. This time only the
1825 : * data_directory parameter is picked up to determine the data directory,
1826 : * so that we can read the PG_AUTOCONF_FILENAME file next time.
1827 : */
208 tgl 1828 GIC 1833 : ProcessConfigFile(PGC_POSTMASTER);
6846 bruce 1829 ECB :
208 tgl 1830 : /*
1831 : * If the data_directory GUC variable has been set, use that as DataDir;
1832 : * otherwise use configdir if set; else punt.
1833 : *
1834 : * Note: SetDataDir will copy and absolute-ize its argument, so we don't
1835 : * have to.
208 tgl 1836 EUB : */
1837 : data_directory_rec = (struct config_string *)
208 tgl 1838 GNC 1833 : find_option("data_directory", false, false, PANIC);
1839 1833 : if (*data_directory_rec->variable)
208 tgl 1840 UNC 0 : SetDataDir(*data_directory_rec->variable);
208 tgl 1841 CBC 1833 : else if (configdir)
1842 1833 : SetDataDir(configdir);
208 tgl 1843 ECB : else
1844 : {
208 tgl 1845 UIC 0 : write_stderr("%s does not know where to find the database system data.\n"
208 tgl 1846 ECB : "This can be specified as \"data_directory\" in \"%s\", "
1847 : "or by the -D invocation option, or by the "
1848 : "PGDATA environment variable.\n",
1849 : progname, ConfigFileName);
208 tgl 1850 UIC 0 : return false;
1851 : }
1852 :
1853 : /*
1854 : * Reflect the final DataDir value back into the data_directory GUC var.
1855 : * (If you are wondering why we don't just make them a single variable,
1856 : * it's because the EXEC_BACKEND case needs DataDir to be transmitted to
208 tgl 1857 ECB : * child backends specially. XXX is that still true? Given that we now
1858 : * chdir to DataDir, EXEC_BACKEND can read the config file without knowing
1859 : * DataDir in advance.)
1860 : */
208 tgl 1861 GIC 1833 : SetConfigOption("data_directory", DataDir, PGC_POSTMASTER, PGC_S_OVERRIDE);
1748 peter_e 1862 ECB :
208 tgl 1863 : /*
1864 : * Now read the config file a second time, allowing any settings in the
1865 : * PG_AUTOCONF_FILENAME file to take effect. (This is pretty ugly, but
1866 : * since we have to determine the DataDir before we can find the autoconf
1867 : * file, the alternatives seem worse.)
1868 : */
208 tgl 1869 GIC 1833 : ProcessConfigFile(PGC_POSTMASTER);
4064 peter_e 1870 ECB :
208 tgl 1871 : /*
1872 : * If timezone_abbreviations wasn't set in the configuration file, install
1873 : * the default value. We do it this way because we can't safely install a
1874 : * "real" value until my_exec_path is set, which may not have happened
1875 : * when InitializeGUCOptions runs, so the bootstrap default value cannot
1876 : * be the real desired default.
208 tgl 1877 EUB : */
208 tgl 1878 GBC 1831 : pg_timezone_abbrev_initialize();
1879 :
1880 : /*
208 tgl 1881 ECB : * Figure out where pg_hba.conf is, and make sure the path is absolute.
1882 : */
208 tgl 1883 CBC 1831 : if (HbaFileName)
1884 : {
1885 1 : fname = make_absolute_path(HbaFileName);
177 tgl 1886 GNC 1 : fname_is_malloced = true;
1887 : }
208 tgl 1888 GIC 1830 : else if (configdir)
1889 : {
1890 1830 : fname = guc_malloc(FATAL,
1891 1830 : strlen(configdir) + strlen(HBA_FILENAME) + 2);
1892 1830 : sprintf(fname, "%s/%s", configdir, HBA_FILENAME);
177 tgl 1893 GNC 1830 : fname_is_malloced = false;
1894 : }
1895 : else
1896 : {
208 tgl 1897 UIC 0 : write_stderr("%s does not know where to find the \"hba\" configuration file.\n"
208 tgl 1898 ECB : "This can be specified as \"hba_file\" in \"%s\", "
1899 : "or by the -D invocation option, or by the "
1900 : "PGDATA environment variable.\n",
1901 : progname, ConfigFileName);
208 tgl 1902 UIC 0 : return false;
208 tgl 1903 ECB : }
208 tgl 1904 CBC 1831 : SetConfigOption("hba_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
1905 :
177 tgl 1906 GNC 1831 : if (fname_is_malloced)
1907 1 : free(fname);
1908 : else
1909 1830 : guc_free(fname);
6846 bruce 1910 ECB :
1911 : /*
208 tgl 1912 : * Likewise for pg_ident.conf.
1913 : */
208 tgl 1914 GIC 1831 : if (IdentFileName)
1915 : {
208 tgl 1916 CBC 1 : fname = make_absolute_path(IdentFileName);
177 tgl 1917 GNC 1 : fname_is_malloced = true;
1918 : }
208 tgl 1919 GIC 1830 : else if (configdir)
1920 : {
1921 1830 : fname = guc_malloc(FATAL,
1922 1830 : strlen(configdir) + strlen(IDENT_FILENAME) + 2);
1923 1830 : sprintf(fname, "%s/%s", configdir, IDENT_FILENAME);
177 tgl 1924 GNC 1830 : fname_is_malloced = false;
208 tgl 1925 ECB : }
1926 : else
1927 : {
208 tgl 1928 UIC 0 : write_stderr("%s does not know where to find the \"ident\" configuration file.\n"
1929 : "This can be specified as \"ident_file\" in \"%s\", "
1930 : "or by the -D invocation option, or by the "
1931 : "PGDATA environment variable.\n",
208 tgl 1932 ECB : progname, ConfigFileName);
208 tgl 1933 UBC 0 : return false;
1934 : }
208 tgl 1935 CBC 1831 : SetConfigOption("ident_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
1936 :
177 tgl 1937 GNC 1831 : if (fname_is_malloced)
1938 1 : free(fname);
1939 : else
1940 1830 : guc_free(fname);
4417 simon 1941 ECB :
208 tgl 1942 CBC 1831 : free(configdir);
1943 :
208 tgl 1944 GBC 1831 : return true;
1945 : }
1946 :
1947 : /*
1948 : * pg_timezone_abbrev_initialize --- set default value if not done already
1949 : *
1950 : * This is called after initial loading of postgresql.conf. If no
1951 : * timezone_abbreviations setting was found therein, select default.
1952 : * If a non-default value is already installed, nothing will happen.
1953 : *
1954 : * This can also be called from ProcessConfigFile to establish the default
1955 : * value after a postgresql.conf entry for it is removed.
1956 : */
1957 : static void
208 tgl 1958 GNC 2169 : pg_timezone_abbrev_initialize(void)
1959 : {
1960 2169 : SetConfigOption("timezone_abbreviations", "Default",
1961 : PGC_POSTMASTER, PGC_S_DYNAMIC_DEFAULT);
1962 2169 : }
1963 :
1964 :
208 tgl 1965 EUB : /*
1966 : * Reset all options to their saved default values (implements RESET ALL)
1967 : */
1968 : void
208 tgl 1969 CBC 3 : ResetAllOptions(void)
5508 magnus 1970 ECB : {
1971 : dlist_mutable_iter iter;
1972 :
1973 : /* We need only consider GUCs not already at PGC_S_DEFAULT */
177 tgl 1974 GNC 160 : dlist_foreach_modify(iter, &guc_nondef_list)
1975 : {
1976 157 : struct config_generic *gconf = dlist_container(struct config_generic,
1977 : nondef_link, iter.cur);
5508 magnus 1978 ECB :
208 tgl 1979 EUB : /* Don't reset non-SET-able values */
208 tgl 1980 GIC 157 : if (gconf->context != PGC_SUSET &&
1981 144 : gconf->context != PGC_USERSET)
1982 92 : continue;
1983 : /* Don't reset if special exclusion from RESET ALL */
1984 65 : if (gconf->flags & GUC_NO_RESET_ALL)
1985 12 : continue;
208 tgl 1986 ECB : /* No need to reset if wasn't SET */
208 tgl 1987 CBC 53 : if (gconf->source <= PGC_S_OVERRIDE)
208 tgl 1988 GIC 47 : continue;
1989 :
1990 : /* Save old value to support transaction abort */
208 tgl 1991 CBC 6 : push_old_value(gconf, GUC_ACTION_SET);
1992 :
208 tgl 1993 GIC 6 : switch (gconf->vartype)
1994 : {
208 tgl 1995 LBC 0 : case PGC_BOOL:
208 tgl 1996 ECB : {
208 tgl 1997 LBC 0 : struct config_bool *conf = (struct config_bool *) gconf;
1998 :
1999 0 : if (conf->assign_hook)
208 tgl 2000 UIC 0 : conf->assign_hook(conf->reset_val,
2001 : conf->reset_extra);
2002 0 : *conf->variable = conf->reset_val;
2003 0 : set_extra_field(&conf->gen, &conf->gen.extra,
2004 : conf->reset_extra);
2005 0 : break;
2006 : }
208 tgl 2007 LBC 0 : case PGC_INT:
2008 : {
208 tgl 2009 UIC 0 : struct config_int *conf = (struct config_int *) gconf;
2010 :
2011 0 : if (conf->assign_hook)
2012 0 : conf->assign_hook(conf->reset_val,
2013 : conf->reset_extra);
2014 0 : *conf->variable = conf->reset_val;
208 tgl 2015 LBC 0 : set_extra_field(&conf->gen, &conf->gen.extra,
208 tgl 2016 ECB : conf->reset_extra);
208 tgl 2017 UIC 0 : break;
2018 : }
208 tgl 2019 CBC 3 : case PGC_REAL:
2020 : {
208 tgl 2021 GIC 3 : struct config_real *conf = (struct config_real *) gconf;
1643 peter_e 2022 ECB :
208 tgl 2023 CBC 3 : if (conf->assign_hook)
208 tgl 2024 LBC 0 : conf->assign_hook(conf->reset_val,
2025 : conf->reset_extra);
208 tgl 2026 GIC 3 : *conf->variable = conf->reset_val;
208 tgl 2027 CBC 3 : set_extra_field(&conf->gen, &conf->gen.extra,
208 tgl 2028 ECB : conf->reset_extra);
208 tgl 2029 GIC 3 : break;
208 tgl 2030 ECB : }
208 tgl 2031 GIC 3 : case PGC_STRING:
2032 : {
2033 3 : struct config_string *conf = (struct config_string *) gconf;
2034 :
2035 3 : if (conf->assign_hook)
208 tgl 2036 LBC 0 : conf->assign_hook(conf->reset_val,
208 tgl 2037 ECB : conf->reset_extra);
208 tgl 2038 CBC 3 : set_string_field(conf, conf->variable, conf->reset_val);
208 tgl 2039 GIC 3 : set_extra_field(&conf->gen, &conf->gen.extra,
208 tgl 2040 ECB : conf->reset_extra);
208 tgl 2041 GIC 3 : break;
208 tgl 2042 ECB : }
208 tgl 2043 UIC 0 : case PGC_ENUM:
2044 : {
2045 0 : struct config_enum *conf = (struct config_enum *) gconf;
2046 :
208 tgl 2047 LBC 0 : if (conf->assign_hook)
208 tgl 2048 UIC 0 : conf->assign_hook(conf->reset_val,
2049 : conf->reset_extra);
2050 0 : *conf->variable = conf->reset_val;
2051 0 : set_extra_field(&conf->gen, &conf->gen.extra,
2052 : conf->reset_extra);
208 tgl 2053 LBC 0 : break;
208 tgl 2054 EUB : }
208 tgl 2055 ECB : }
2056 :
177 tgl 2057 GNC 6 : set_guc_source(gconf, gconf->reset_source);
208 tgl 2058 CBC 6 : gconf->scontext = gconf->reset_scontext;
208 tgl 2059 GIC 6 : gconf->srole = gconf->reset_srole;
5508 magnus 2060 ECB :
177 tgl 2061 GNC 6 : if ((gconf->flags & GUC_REPORT) && !(gconf->status & GUC_NEEDS_REPORT))
208 tgl 2062 ECB : {
208 tgl 2063 UIC 0 : gconf->status |= GUC_NEEDS_REPORT;
177 tgl 2064 UNC 0 : slist_push_head(&guc_report_list, &gconf->report_link);
2065 : }
2066 : }
208 tgl 2067 GIC 3 : }
2068 :
2069 :
2070 : /*
2071 : * Apply a change to a GUC variable's "source" field.
2072 : *
2073 : * Use this rather than just assigning, to ensure that the variable's
2074 : * membership in guc_nondef_list is updated correctly.
2075 : */
2076 : static void
177 tgl 2077 GNC 278789 : set_guc_source(struct config_generic *gconf, GucSource newsource)
2078 : {
2079 : /* Adjust nondef list membership if appropriate for change */
2080 278789 : if (gconf->source == PGC_S_DEFAULT)
2081 : {
2082 196349 : if (newsource != PGC_S_DEFAULT)
2083 196282 : dlist_push_tail(&guc_nondef_list, &gconf->nondef_link);
2084 : }
2085 : else
2086 : {
2087 82440 : if (newsource == PGC_S_DEFAULT)
2088 11348 : dlist_delete(&gconf->nondef_link);
2089 : }
2090 : /* Now update the source field */
2091 278789 : gconf->source = newsource;
2092 278789 : }
2093 :
2094 :
208 tgl 2095 ECB : /*
208 tgl 2096 EUB : * push_old_value
2097 : * Push previous state during transactional assignment to a GUC variable.
2098 : */
2099 : static void
208 tgl 2100 GIC 47182 : push_old_value(struct config_generic *gconf, GucAction action)
2101 : {
2102 : GucStack *stack;
5508 magnus 2103 ECB :
208 tgl 2104 : /* If we're not inside a nest level, do nothing */
208 tgl 2105 GIC 47182 : if (GUCNestLevel == 0)
208 tgl 2106 LBC 0 : return;
4388 rhaas 2107 ECB :
2108 : /* Do we already have a stack entry of the current nest level? */
208 tgl 2109 GIC 47182 : stack = gconf->stack;
2110 47182 : if (stack && stack->nest_level >= GUCNestLevel)
2886 heikki.linnakangas 2111 ECB : {
2112 : /* Yes, so adjust its state if necessary */
208 tgl 2113 GIC 5496 : Assert(stack->nest_level == GUCNestLevel);
2114 5496 : switch (action)
2115 : {
2116 5463 : case GUC_ACTION_SET:
2117 : /* SET overrides any prior action at same nest level */
2118 5463 : if (stack->state == GUC_SET_LOCAL)
208 tgl 2119 ECB : {
2120 : /* must discard old masked value */
208 tgl 2121 UIC 0 : discard_stack_value(gconf, &stack->masked);
2122 : }
208 tgl 2123 GIC 5463 : stack->state = GUC_SET;
2124 5463 : break;
2125 33 : case GUC_ACTION_LOCAL:
2126 33 : if (stack->state == GUC_SET)
2127 : {
2128 : /* SET followed by SET LOCAL, remember SET's value */
2129 6 : stack->masked_scontext = gconf->scontext;
208 tgl 2130 CBC 6 : stack->masked_srole = gconf->srole;
2131 6 : set_stack_value(gconf, &stack->masked);
208 tgl 2132 GIC 6 : stack->state = GUC_SET_LOCAL;
208 tgl 2133 ECB : }
2134 : /* in all other cases, no change to stack entry */
208 tgl 2135 GIC 33 : break;
208 tgl 2136 UIC 0 : case GUC_ACTION_SAVE:
2137 : /* Could only have a prior SAVE of same variable */
208 tgl 2138 LBC 0 : Assert(stack->state == GUC_SAVE);
208 tgl 2139 UIC 0 : break;
2140 : }
208 tgl 2141 GIC 5496 : return;
2142 : }
2143 :
2144 : /*
2145 : * Push a new stack entry
2146 : *
208 tgl 2147 ECB : * We keep all the stack entries in TopTransactionContext for simplicity.
2148 : */
208 tgl 2149 GIC 41686 : stack = (GucStack *) MemoryContextAllocZero(TopTransactionContext,
2150 : sizeof(GucStack));
2151 :
2152 41686 : stack->prev = gconf->stack;
2153 41686 : stack->nest_level = GUCNestLevel;
2154 41686 : switch (action)
4859 simon 2155 ECB : {
208 tgl 2156 GIC 21368 : case GUC_ACTION_SET:
2157 21368 : stack->state = GUC_SET;
208 tgl 2158 CBC 21368 : break;
2159 3928 : case GUC_ACTION_LOCAL:
2160 3928 : stack->state = GUC_LOCAL;
208 tgl 2161 GIC 3928 : break;
2162 16390 : case GUC_ACTION_SAVE:
208 tgl 2163 CBC 16390 : stack->state = GUC_SAVE;
208 tgl 2164 GBC 16390 : break;
2165 : }
208 tgl 2166 CBC 41686 : stack->source = gconf->source;
2167 41686 : stack->scontext = gconf->scontext;
2168 41686 : stack->srole = gconf->srole;
208 tgl 2169 GIC 41686 : set_stack_value(gconf, &stack->prior);
4859 simon 2170 ECB :
177 tgl 2171 GNC 41686 : if (gconf->stack == NULL)
2172 41607 : slist_push_head(&guc_stack_list, &gconf->stack_link);
208 tgl 2173 GIC 41686 : gconf->stack = stack;
2174 : }
2175 :
368 andres 2176 ECB :
2177 : /*
2178 : * Do GUC processing at main transaction start.
2179 : */
2180 : void
208 tgl 2181 GIC 486644 : AtStart_GUC(void)
208 tgl 2182 ECB : {
2183 : /*
2184 : * The nest level should be 0 between transactions; if it isn't, somebody
2185 : * didn't call AtEOXact_GUC, or called it with the wrong nestLevel. We
2186 : * throw a warning but make no other effort to clean up.
2187 : */
208 tgl 2188 GIC 486644 : if (GUCNestLevel != 0)
208 tgl 2189 UIC 0 : elog(WARNING, "GUC nest level = %d at transaction start",
2190 : GUCNestLevel);
208 tgl 2191 GIC 486644 : GUCNestLevel = 1;
2192 486644 : }
649 michael 2193 ECB :
208 tgl 2194 EUB : /*
2195 : * Enter a new nesting level for GUC values. This is called at subtransaction
2196 : * start, and when entering a function that has proconfig settings, and in
208 tgl 2197 ECB : * some other places where we want to set GUC variables transiently.
2198 : * NOTE we must not risk error here, else subtransaction start will be unhappy.
2199 : */
2200 : int
208 tgl 2201 CBC 308873 : NewGUCNestLevel(void)
208 tgl 2202 ECB : {
208 tgl 2203 CBC 308873 : return ++GUCNestLevel;
2204 : }
2205 :
2206 : /*
208 tgl 2207 ECB : * Do GUC processing at transaction or subtransaction commit or abort, or
2208 : * when exiting a function that has proconfig settings, or when undoing a
208 tgl 2209 EUB : * transient assignment to some GUC variables. (The name is thus a bit of
2210 : * a misnomer; perhaps it should be ExitGUCNestLevel or some such.)
2211 : * During abort, we discard all GUC settings that were applied at nesting
2212 : * levels >= nestLevel. nestLevel == 1 corresponds to the main transaction.
2213 : */
2214 : void
208 tgl 2215 GIC 794987 : AtEOXact_GUC(bool isCommit, int nestLevel)
236 john.naylor 2216 EUB : {
2217 : slist_mutable_iter iter;
2218 :
2219 : /*
2220 : * Note: it's possible to get here with GUCNestLevel == nestLevel-1 during
2221 : * abort, if there is a failure during transaction start before
2222 : * AtStart_GUC is called.
2223 : */
208 tgl 2224 GIC 794987 : Assert(nestLevel > 0 &&
2225 : (nestLevel <= GUCNestLevel ||
208 tgl 2226 ECB : (nestLevel == GUCNestLevel + 1 && !isCommit)));
236 john.naylor 2227 :
2228 : /* We need only process GUCs having nonempty stacks */
177 tgl 2229 GNC 839601 : slist_foreach_modify(iter, &guc_stack_list)
2230 : {
2231 44614 : struct config_generic *gconf = slist_container(struct config_generic,
2232 : stack_link, iter.cur);
2233 : GucStack *stack;
2234 :
2235 : /*
2236 : * Process and pop each stack entry within the nest level. To simplify
2237 : * fmgr_security_definer() and other places that use GUC_ACTION_SAVE,
2238 : * we allow failure exit from code that uses a local nest level to be
2239 : * recovered at the surrounding transaction or subtransaction abort;
2240 : * so there could be more than one stack entry to pop.
236 john.naylor 2241 ECB : */
208 tgl 2242 GIC 86316 : while ((stack = gconf->stack) != NULL &&
2243 44711 : stack->nest_level >= nestLevel)
2244 : {
2245 41702 : GucStack *prev = stack->prev;
2246 41702 : bool restorePrior = false;
2247 41702 : bool restoreMasked = false;
2248 : bool changed;
236 john.naylor 2249 ECB :
208 tgl 2250 EUB : /*
2251 : * In this next bit, if we don't set either restorePrior or
2252 : * restoreMasked, we must "discard" any unwanted fields of the
208 tgl 2253 ECB : * stack entries to avoid leaking memory. If we do set one of
208 tgl 2254 EUB : * those flags, unused fields will be cleaned up after restoring.
2255 : */
208 tgl 2256 CBC 41702 : if (!isCommit) /* if abort, always restore prior value */
2257 2036 : restorePrior = true;
2258 39666 : else if (stack->state == GUC_SAVE)
208 tgl 2259 GIC 16017 : restorePrior = true;
208 tgl 2260 CBC 23649 : else if (stack->nest_level == 1)
2261 : {
2262 : /* transaction commit */
2263 23628 : if (stack->state == GUC_SET_LOCAL)
208 tgl 2264 GIC 6 : restoreMasked = true;
2265 23622 : else if (stack->state == GUC_SET)
2266 : {
2267 : /* we keep the current active value */
208 tgl 2268 CBC 20504 : discard_stack_value(gconf, &stack->prior);
2269 : }
2270 : else /* must be GUC_LOCAL */
208 tgl 2271 GIC 3118 : restorePrior = true;
2272 : }
2273 21 : else if (prev == NULL ||
208 tgl 2274 CBC 6 : prev->nest_level < stack->nest_level - 1)
236 john.naylor 2275 ECB : {
208 tgl 2276 : /* decrement entry's level and do not pop it */
208 tgl 2277 GIC 18 : stack->nest_level--;
208 tgl 2278 CBC 18 : continue;
236 john.naylor 2279 ECB : }
2280 : else
2281 : {
2282 : /*
2283 : * We have to merge this stack entry into prev. See README for
2284 : * discussion of this bit.
208 tgl 2285 : */
208 tgl 2286 GBC 3 : switch (stack->state)
2287 : {
208 tgl 2288 LBC 0 : case GUC_SAVE:
2289 0 : Assert(false); /* can't get here */
2290 : break;
2291 :
208 tgl 2292 CBC 3 : case GUC_SET:
208 tgl 2293 ECB : /* next level always becomes SET */
208 tgl 2294 CBC 3 : discard_stack_value(gconf, &stack->prior);
208 tgl 2295 GIC 3 : if (prev->state == GUC_SET_LOCAL)
208 tgl 2296 UIC 0 : discard_stack_value(gconf, &prev->masked);
208 tgl 2297 CBC 3 : prev->state = GUC_SET;
208 tgl 2298 GIC 3 : break;
6856 tgl 2299 EUB :
208 tgl 2300 UIC 0 : case GUC_LOCAL:
2301 0 : if (prev->state == GUC_SET)
2302 : {
2303 : /* LOCAL migrates down */
2304 0 : prev->masked_scontext = stack->scontext;
2305 0 : prev->masked_srole = stack->srole;
208 tgl 2306 UBC 0 : prev->masked = stack->prior;
2307 0 : prev->state = GUC_SET_LOCAL;
2308 : }
2309 : else
2310 : {
2311 : /* else just forget this stack level */
208 tgl 2312 UIC 0 : discard_stack_value(gconf, &stack->prior);
2313 : }
2314 0 : break;
2315 :
2316 0 : case GUC_SET_LOCAL:
208 tgl 2317 ECB : /* prior state at this level no longer wanted */
208 tgl 2318 LBC 0 : discard_stack_value(gconf, &stack->prior);
208 tgl 2319 ECB : /* copy down the masked state */
208 tgl 2320 UIC 0 : prev->masked_scontext = stack->masked_scontext;
2321 0 : prev->masked_srole = stack->masked_srole;
208 tgl 2322 LBC 0 : if (prev->state == GUC_SET_LOCAL)
2323 0 : discard_stack_value(gconf, &prev->masked);
208 tgl 2324 UIC 0 : prev->masked = stack->masked;
2325 0 : prev->state = GUC_SET_LOCAL;
208 tgl 2326 LBC 0 : break;
208 tgl 2327 ECB : }
2328 : }
5458 alvherre 2329 :
208 tgl 2330 GIC 41684 : changed = false;
2331 :
2332 41684 : if (restorePrior || restoreMasked)
2333 : {
2334 : /* Perform appropriate restoration of the stacked value */
2335 : config_var_value newvalue;
2336 : GucSource newsource;
2337 : GucContext newscontext;
2338 : Oid newsrole;
2339 :
2340 21177 : if (restoreMasked)
2341 : {
2342 6 : newvalue = stack->masked;
2343 6 : newsource = PGC_S_SESSION;
2344 6 : newscontext = stack->masked_scontext;
208 tgl 2345 CBC 6 : newsrole = stack->masked_srole;
2346 : }
2347 : else
2348 : {
208 tgl 2349 GIC 21171 : newvalue = stack->prior;
2350 21171 : newsource = stack->source;
2351 21171 : newscontext = stack->scontext;
2352 21171 : newsrole = stack->srole;
2353 : }
2354 :
2355 21177 : switch (gconf->vartype)
2356 : {
2357 1657 : case PGC_BOOL:
2358 : {
2359 1657 : struct config_bool *conf = (struct config_bool *) gconf;
2360 1657 : bool newval = newvalue.val.boolval;
2361 1657 : void *newextra = newvalue.extra;
2362 :
2363 1657 : if (*conf->variable != newval ||
2364 212 : conf->gen.extra != newextra)
2365 : {
2366 1445 : if (conf->assign_hook)
208 tgl 2367 UIC 0 : conf->assign_hook(newval, newextra);
208 tgl 2368 GIC 1445 : *conf->variable = newval;
2369 1445 : set_extra_field(&conf->gen, &conf->gen.extra,
208 tgl 2370 ECB : newextra);
208 tgl 2371 CBC 1445 : changed = true;
2372 : }
208 tgl 2373 GIC 1657 : break;
208 tgl 2374 EUB : }
208 tgl 2375 GIC 4052 : case PGC_INT:
2376 : {
208 tgl 2377 CBC 4052 : struct config_int *conf = (struct config_int *) gconf;
208 tgl 2378 GIC 4052 : int newval = newvalue.val.intval;
2379 4052 : void *newextra = newvalue.extra;
2380 :
2381 4052 : if (*conf->variable != newval ||
2382 92 : conf->gen.extra != newextra)
208 tgl 2383 ECB : {
208 tgl 2384 CBC 3960 : if (conf->assign_hook)
208 tgl 2385 LBC 0 : conf->assign_hook(newval, newextra);
208 tgl 2386 CBC 3960 : *conf->variable = newval;
2387 3960 : set_extra_field(&conf->gen, &conf->gen.extra,
208 tgl 2388 ECB : newextra);
208 tgl 2389 GBC 3960 : changed = true;
208 tgl 2390 ECB : }
208 tgl 2391 GIC 4052 : break;
2392 : }
2393 686 : case PGC_REAL:
2394 : {
2395 686 : struct config_real *conf = (struct config_real *) gconf;
208 tgl 2396 CBC 686 : double newval = newvalue.val.realval;
208 tgl 2397 GIC 686 : void *newextra = newvalue.extra;
3399 ishii 2398 ECB :
208 tgl 2399 CBC 686 : if (*conf->variable != newval ||
2400 12 : conf->gen.extra != newextra)
2401 : {
2402 674 : if (conf->assign_hook)
208 tgl 2403 LBC 0 : conf->assign_hook(newval, newextra);
208 tgl 2404 GIC 674 : *conf->variable = newval;
2405 674 : set_extra_field(&conf->gen, &conf->gen.extra,
2406 : newextra);
208 tgl 2407 CBC 674 : changed = true;
208 tgl 2408 ECB : }
208 tgl 2409 GIC 686 : break;
2410 : }
208 tgl 2411 CBC 7645 : case PGC_STRING:
2412 : {
208 tgl 2413 GIC 7645 : struct config_string *conf = (struct config_string *) gconf;
2414 7645 : char *newval = newvalue.val.stringval;
2415 7645 : void *newextra = newvalue.extra;
2416 :
2417 7645 : if (*conf->variable != newval ||
2418 7 : conf->gen.extra != newextra)
2419 : {
2420 7638 : if (conf->assign_hook)
2421 7393 : conf->assign_hook(newval, newextra);
2422 7638 : set_string_field(conf, conf->variable, newval);
2423 7638 : set_extra_field(&conf->gen, &conf->gen.extra,
208 tgl 2424 ECB : newextra);
208 tgl 2425 GIC 7638 : changed = true;
208 tgl 2426 ECB : }
3399 ishii 2427 :
2428 : /*
2429 : * Release stacked values if not used anymore. We
2430 : * could use discard_stack_value() here, but since
2431 : * we have type-specific code anyway, might as
2432 : * well inline it.
2433 : */
208 tgl 2434 GIC 7645 : set_string_field(conf, &stack->prior.val.stringval, NULL);
2435 7645 : set_string_field(conf, &stack->masked.val.stringval, NULL);
2436 7645 : break;
208 tgl 2437 ECB : }
208 tgl 2438 GIC 7137 : case PGC_ENUM:
2439 : {
2440 7137 : struct config_enum *conf = (struct config_enum *) gconf;
208 tgl 2441 CBC 7137 : int newval = newvalue.val.enumval;
2442 7137 : void *newextra = newvalue.extra;
2443 :
208 tgl 2444 GIC 7137 : if (*conf->variable != newval ||
208 tgl 2445 CBC 418 : conf->gen.extra != newextra)
208 tgl 2446 ECB : {
208 tgl 2447 CBC 6719 : if (conf->assign_hook)
208 tgl 2448 LBC 0 : conf->assign_hook(newval, newextra);
208 tgl 2449 GIC 6719 : *conf->variable = newval;
208 tgl 2450 CBC 6719 : set_extra_field(&conf->gen, &conf->gen.extra,
2451 : newextra);
2452 6719 : changed = true;
2453 : }
208 tgl 2454 GIC 7137 : break;
2455 : }
2456 : }
2457 :
2458 : /*
2459 : * Release stacked extra values if not used anymore.
2460 : */
2461 21177 : set_extra_field(gconf, &(stack->prior.extra), NULL);
2462 21177 : set_extra_field(gconf, &(stack->masked.extra), NULL);
3399 ishii 2463 EUB :
208 tgl 2464 : /* And restore source information */
177 tgl 2465 GNC 21177 : set_guc_source(gconf, newsource);
208 tgl 2466 GIC 21177 : gconf->scontext = newscontext;
208 tgl 2467 CBC 21177 : gconf->srole = newsrole;
2468 : }
2469 :
2470 : /*
2471 : * Pop the GUC's state stack; if it's now empty, remove the GUC
2472 : * from guc_stack_list.
2473 : */
2474 41684 : gconf->stack = prev;
177 tgl 2475 GNC 41684 : if (prev == NULL)
2476 41605 : slist_delete_current(&iter);
208 tgl 2477 GIC 41684 : pfree(stack);
2478 :
2479 : /* Report new value if we changed it */
177 tgl 2480 GNC 41684 : if (changed && (gconf->flags & GUC_REPORT) &&
2481 6717 : !(gconf->status & GUC_NEEDS_REPORT))
208 tgl 2482 ECB : {
208 tgl 2483 GIC 101 : gconf->status |= GUC_NEEDS_REPORT;
177 tgl 2484 GNC 101 : slist_push_head(&guc_report_list, &gconf->report_link);
208 tgl 2485 ECB : }
2486 : } /* end of stack-popping loop */
2487 : }
2488 :
2489 : /* Update nesting level */
208 tgl 2490 GIC 794987 : GUCNestLevel = nestLevel - 1;
3399 ishii 2491 794987 : }
2492 :
2493 :
2494 : /*
2495 : * Start up automatic reporting of changes to variables marked GUC_REPORT.
2496 : * This is executed at completion of backend startup.
3399 ishii 2497 EUB : */
2498 : void
208 tgl 2499 GIC 8931 : BeginReportingGUCOptions(void)
2500 : {
2501 : HASH_SEQ_STATUS status;
2502 : GUCHashEntry *hentry;
2503 :
2504 : /*
2505 : * Don't do anything unless talking to an interactive frontend.
3399 ishii 2506 ECB : */
208 tgl 2507 GIC 8931 : if (whereToSendOutput != DestRemote)
2508 312 : return;
2509 :
208 tgl 2510 CBC 8619 : reporting_enabled = true;
3399 ishii 2511 ECB :
2512 : /*
2513 : * Hack for in_hot_standby: set the GUC value true if appropriate. This
208 tgl 2514 : * is kind of an ugly place to do it, but there's few better options.
2515 : *
2516 : * (This could be out of date by the time we actually send it, in which
2517 : * case the next ReportChangedGUCOptions call will send a duplicate
2518 : * report.)
2519 : */
208 tgl 2520 GIC 8619 : if (RecoveryInProgress())
208 tgl 2521 GBC 409 : SetConfigOption("in_hot_standby", "true",
2522 : PGC_INTERNAL, PGC_S_OVERRIDE);
2523 :
2524 : /* Transmit initial values of interesting variables */
177 tgl 2525 GNC 8619 : hash_seq_init(&status, guc_hashtab);
2526 3190639 : while ((hentry = (GUCHashEntry *) hash_seq_search(&status)) != NULL)
2527 : {
2528 3173401 : struct config_generic *conf = hentry->gucvar;
2529 :
208 tgl 2530 GIC 3173401 : if (conf->flags & GUC_REPORT)
2531 120666 : ReportGUCOption(conf);
2532 : }
2533 : }
2534 :
208 tgl 2535 ECB : /*
2536 : * ReportChangedGUCOptions: report recently-changed GUC_REPORT variables
2537 : *
2538 : * This is called just before we wait for a new client query.
2539 : *
2540 : * By handling things this way, we ensure that a ParameterStatus message
2541 : * is sent at most once per variable per query, even if the variable
2542 : * changed multiple times within the query. That's quite possible when
2543 : * using features such as function SET clauses. Function SET clauses
2544 : * also tend to cause values to change intraquery but eventually revert
2545 : * to their prevailing values; ReportGUCOption is responsible for avoiding
2546 : * redundant reports in such cases.
2547 : */
2548 : void
208 tgl 2549 GIC 463194 : ReportChangedGUCOptions(void)
2550 : {
2551 : slist_mutable_iter iter;
2552 :
2553 : /* Quick exit if not (yet) enabled */
2554 463194 : if (!reporting_enabled)
208 tgl 2555 CBC 212109 : return;
2556 :
2557 : /*
208 tgl 2558 ECB : * Since in_hot_standby isn't actually changed by normal GUC actions, we
2559 : * need a hack to check whether a new value needs to be reported to the
2560 : * client. For speed, we rely on the assumption that it can never
2561 : * transition from false to true.
2562 : */
208 tgl 2563 GNC 251085 : if (in_hot_standby_guc && !RecoveryInProgress())
208 tgl 2564 GIC 2 : SetConfigOption("in_hot_standby", "false",
208 tgl 2565 ECB : PGC_INTERNAL, PGC_S_OVERRIDE);
2566 :
2567 : /* Transmit new values of interesting variables */
177 tgl 2568 GNC 320234 : slist_foreach_modify(iter, &guc_report_list)
2569 : {
2570 69149 : struct config_generic *conf = slist_container(struct config_generic,
2571 : report_link, iter.cur);
2572 :
2573 69149 : Assert((conf->flags & GUC_REPORT) && (conf->status & GUC_NEEDS_REPORT));
2574 69149 : ReportGUCOption(conf);
2575 69149 : conf->status &= ~GUC_NEEDS_REPORT;
2576 69149 : slist_delete_current(&iter);
2577 : }
2578 : }
208 tgl 2579 ECB :
2580 : /*
2581 : * ReportGUCOption: if appropriate, transmit option value to frontend
2582 : *
2583 : * We need not transmit the value if it's the same as what we last
2584 : * transmitted.
2585 : */
2586 : static void
208 tgl 2587 GIC 189815 : ReportGUCOption(struct config_generic *record)
208 tgl 2588 ECB : {
208 tgl 2589 GNC 189815 : char *val = ShowGUCOption(record, false);
2590 :
208 tgl 2591 GIC 189815 : if (record->last_reported == NULL ||
208 tgl 2592 CBC 69149 : strcmp(val, record->last_reported) != 0)
3141 fujii 2593 ECB : {
2594 : StringInfoData msgbuf;
2595 :
208 tgl 2596 CBC 124672 : pq_beginmessage(&msgbuf, 'S');
2597 124672 : pq_sendstring(&msgbuf, record->name);
2598 124672 : pq_sendstring(&msgbuf, val);
2599 124672 : pq_endmessage(&msgbuf);
2600 :
3141 fujii 2601 ECB : /*
2602 : * We need a long-lifespan copy. If guc_strdup() fails due to OOM,
2603 : * we'll set last_reported to NULL and thereby possibly make a
2604 : * duplicate report later.
2605 : */
177 tgl 2606 GNC 124672 : guc_free(record->last_reported);
2607 124672 : record->last_reported = guc_strdup(LOG, val);
2608 : }
2609 :
208 tgl 2610 GIC 189815 : pfree(val);
2611 189815 : }
2996 tgl 2612 EUB :
208 2613 : /*
2614 : * Convert a value from one of the human-friendly units ("kB", "min" etc.)
2615 : * to the given base unit. 'value' and 'unit' are the input value and unit
208 tgl 2616 ECB : * to convert from (there can be trailing spaces in the unit string).
2617 : * The converted value is stored in *base_value.
2618 : * It's caller's responsibility to round off the converted value as necessary
2619 : * and check for out-of-range.
2620 : *
2621 : * Returns true on success, false if the input unit is not recognized.
2622 : */
2623 : static bool
208 tgl 2624 GIC 6102 : convert_to_base_unit(double value, const char *unit,
2625 : int base_unit, double *base_value)
2626 : {
2627 : char unitstr[MAX_UNIT_LEN + 1];
2628 : int unitlen;
2629 : const unit_conversion *table;
2630 : int i;
2631 :
2632 : /* extract unit string to compare to table entries */
2633 6102 : unitlen = 0;
2634 18249 : while (*unit != '\0' && !isspace((unsigned char) *unit) &&
2635 : unitlen < MAX_UNIT_LEN)
2636 12147 : unitstr[unitlen++] = *(unit++);
2637 6102 : unitstr[unitlen] = '\0';
2638 : /* allow whitespace after unit */
2639 6102 : while (isspace((unsigned char) *unit))
208 tgl 2640 UIC 0 : unit++;
208 tgl 2641 GIC 6102 : if (*unit != '\0')
208 tgl 2642 UIC 0 : return false; /* unit too long, or garbage after it */
2643 :
2644 : /* now search the appropriate table */
208 tgl 2645 GIC 6102 : if (base_unit & GUC_UNIT_MEMORY)
2646 5125 : table = memory_unit_conversion_table;
2647 : else
2648 977 : table = time_unit_conversion_table;
2649 :
2650 77018 : for (i = 0; *table[i].unit; i++)
2651 : {
2652 77018 : if (base_unit == table[i].base_unit &&
2653 19448 : strcmp(unitstr, table[i].unit) == 0)
2654 : {
2655 6102 : double cvalue = value * table[i].multiplier;
2656 :
2657 : /*
2658 : * If the user gave a fractional value such as "30.1GB", round it
2659 : * off to the nearest multiple of the next smaller unit, if there
2660 : * is one.
2661 : */
2662 6102 : if (*table[i + 1].unit &&
2663 6102 : base_unit == table[i + 1].base_unit)
2664 6099 : cvalue = rint(cvalue / table[i + 1].multiplier) *
2665 6099 : table[i + 1].multiplier;
2666 :
2667 6102 : *base_value = cvalue;
2668 6102 : return true;
2669 : }
2670 : }
208 tgl 2671 UIC 0 : return false;
2672 : }
2673 :
2674 : /*
2675 : * Convert an integer value in some base unit to a human-friendly unit.
2676 : *
2677 : * The output unit is chosen so that it's the greatest unit that can represent
2678 : * the value without loss. For example, if the base unit is GUC_UNIT_KB, 1024
2679 : * is converted to 1 MB, but 1025 is represented as 1025 kB.
2680 : */
2681 : static void
208 tgl 2682 GIC 298 : convert_int_from_base_unit(int64 base_value, int base_unit,
2683 : int64 *value, const char **unit)
2684 : {
2685 : const unit_conversion *table;
2686 : int i;
2687 :
2688 298 : *unit = NULL;
2689 :
2690 298 : if (base_unit & GUC_UNIT_MEMORY)
2691 266 : table = memory_unit_conversion_table;
2692 : else
2693 32 : table = time_unit_conversion_table;
2694 :
2695 1988 : for (i = 0; *table[i].unit; i++)
2696 : {
2697 1988 : if (base_unit == table[i].base_unit)
2698 : {
2699 : /*
2700 : * Accept the first conversion that divides the value evenly. We
2701 : * assume that the conversions for each base unit are ordered from
2702 : * greatest unit to the smallest!
2703 : */
2704 939 : if (table[i].multiplier <= 1.0 ||
2705 898 : base_value % (int64) table[i].multiplier == 0)
2706 : {
2707 298 : *value = (int64) rint(base_value / table[i].multiplier);
2708 298 : *unit = table[i].unit;
2709 298 : break;
2710 : }
2711 : }
2712 : }
2713 :
2714 298 : Assert(*unit != NULL);
2715 298 : }
2716 :
2717 : /*
2718 : * Convert a floating-point value in some base unit to a human-friendly unit.
2719 : *
2720 : * Same as above, except we have to do the math a bit differently, and
2721 : * there's a possibility that we don't find any exact divisor.
2722 : */
2723 : static void
2724 134 : convert_real_from_base_unit(double base_value, int base_unit,
2725 : double *value, const char **unit)
2726 : {
2727 : const unit_conversion *table;
2728 : int i;
2729 :
2730 134 : *unit = NULL;
2731 :
2732 134 : if (base_unit & GUC_UNIT_MEMORY)
208 tgl 2733 UIC 0 : table = memory_unit_conversion_table;
2734 : else
208 tgl 2735 GIC 134 : table = time_unit_conversion_table;
2736 :
2737 682 : for (i = 0; *table[i].unit; i++)
2738 : {
2739 682 : if (base_unit == table[i].base_unit)
2740 : {
2741 : /*
2742 : * Accept the first conversion that divides the value evenly; or
2743 : * if there is none, use the smallest (last) target unit.
2744 : *
2745 : * What we actually care about here is whether snprintf with "%g"
2746 : * will print the value as an integer, so the obvious test of
2747 : * "*value == rint(*value)" is too strict; roundoff error might
2748 : * make us choose an unreasonably small unit. As a compromise,
2749 : * accept a divisor that is within 1e-8 of producing an integer.
2750 : */
2751 682 : *value = base_value / table[i].multiplier;
2752 682 : *unit = table[i].unit;
2753 682 : if (*value > 0 &&
2754 682 : fabs((rint(*value) / *value) - 1.0) <= 1e-8)
2755 134 : break;
2756 : }
2757 : }
2758 :
2759 134 : Assert(*unit != NULL);
2760 134 : }
2761 :
2762 : /*
2763 : * Return the name of a GUC's base unit (e.g. "ms") given its flags.
2764 : * Return NULL if the GUC is unitless.
2765 : */
2766 : const char *
2767 398510 : get_config_unit_name(int flags)
2768 : {
2769 398510 : switch (flags & (GUC_UNIT_MEMORY | GUC_UNIT_TIME))
2770 : {
2771 325745 : case 0:
2772 325745 : return NULL; /* GUC has no units */
2773 5430 : case GUC_UNIT_BYTE:
2774 5430 : return "B";
2775 13032 : case GUC_UNIT_KB:
2776 13032 : return "kB";
2777 6516 : case GUC_UNIT_MB:
2778 6516 : return "MB";
2779 9774 : case GUC_UNIT_BLOCKS:
2780 : {
2781 : static char bbuf[8];
2782 :
2783 : /* initialize if first time through */
2784 9774 : if (bbuf[0] == '\0')
2785 21 : snprintf(bbuf, sizeof(bbuf), "%dkB", BLCKSZ / 1024);
2786 9774 : return bbuf;
2787 : }
2788 2172 : case GUC_UNIT_XBLOCKS:
2789 : {
2790 : static char xbuf[8];
2791 :
2792 : /* initialize if first time through */
2793 2172 : if (xbuf[0] == '\0')
2794 21 : snprintf(xbuf, sizeof(xbuf), "%dkB", XLOG_BLCKSZ / 1024);
2795 2172 : return xbuf;
2796 : }
2797 22809 : case GUC_UNIT_MS:
2798 22809 : return "ms";
2799 10860 : case GUC_UNIT_S:
2800 10860 : return "s";
2801 2172 : case GUC_UNIT_MIN:
2802 2172 : return "min";
208 tgl 2803 UIC 0 : default:
2804 0 : elog(ERROR, "unrecognized GUC units value: %d",
2805 : flags & (GUC_UNIT_MEMORY | GUC_UNIT_TIME));
2806 : return NULL;
2807 : }
2808 : }
2809 :
2810 :
2811 : /*
2812 : * Try to parse value as an integer. The accepted formats are the
2813 : * usual decimal, octal, or hexadecimal formats, as well as floating-point
2814 : * formats (which will be rounded to integer after any units conversion).
2815 : * Optionally, the value can be followed by a unit name if "flags" indicates
2816 : * a unit is allowed.
2817 : *
2818 : * If the string parses okay, return true, else false.
2819 : * If okay and result is not NULL, return the value in *result.
2820 : * If not okay and hintmsg is not NULL, *hintmsg is set to a suitable
2821 : * HINT message, or NULL if no hint provided.
2822 : */
2823 : bool
208 tgl 2824 GIC 40457 : parse_int(const char *value, int *result, int flags, const char **hintmsg)
2825 : {
2826 : /*
2827 : * We assume here that double is wide enough to represent any integer
2828 : * value with adequate precision.
2829 : */
2830 : double val;
2831 : char *endptr;
2832 :
2833 : /* To suppress compiler warnings, always set output params */
2834 40457 : if (result)
2835 40457 : *result = 0;
2836 40457 : if (hintmsg)
2837 36994 : *hintmsg = NULL;
2838 :
2839 : /*
2840 : * Try to parse as an integer (allowing octal or hex input). If the
2841 : * conversion stops at a decimal point or 'e', or overflows, re-parse as
2842 : * float. This should work fine as long as we have no unit names starting
2843 : * with 'e'. If we ever do, the test could be extended to check for a
2844 : * sign or digit after 'e', but for now that's unnecessary.
2845 : */
2846 40457 : errno = 0;
2847 40457 : val = strtol(value, &endptr, 0);
2848 40457 : if (*endptr == '.' || *endptr == 'e' || *endptr == 'E' ||
2849 40451 : errno == ERANGE)
2850 : {
2851 6 : errno = 0;
2852 6 : val = strtod(value, &endptr);
2853 : }
2854 :
2855 40457 : if (endptr == value || errno == ERANGE)
2856 11 : return false; /* no HINT for these cases */
2857 :
2858 : /* reject NaN (infinities will fail range check below) */
2859 40446 : if (isnan(val))
208 tgl 2860 UIC 0 : return false; /* treat same as syntax error; no HINT */
2861 :
2862 : /* allow whitespace between number and unit */
208 tgl 2863 GIC 40464 : while (isspace((unsigned char) *endptr))
2864 18 : endptr++;
2865 :
2866 : /* Handle possible unit */
2867 40446 : if (*endptr != '\0')
2868 : {
2869 6098 : if ((flags & GUC_UNIT) == 0)
2870 2 : return false; /* this setting does not accept a unit */
2871 :
2872 6096 : if (!convert_to_base_unit(val,
2873 : endptr, (flags & GUC_UNIT),
2874 : &val))
2875 : {
2876 : /* invalid unit, or garbage after the unit; set hint and fail. */
208 tgl 2877 UIC 0 : if (hintmsg)
2878 : {
2879 0 : if (flags & GUC_UNIT_MEMORY)
2880 0 : *hintmsg = memory_units_hint;
2881 : else
2882 0 : *hintmsg = time_units_hint;
2883 : }
2884 0 : return false;
2885 : }
2886 : }
2887 :
2888 : /* Round to int, then check for overflow */
208 tgl 2889 GIC 40444 : val = rint(val);
2890 :
2891 40444 : if (val > INT_MAX || val < INT_MIN)
2892 : {
2893 3 : if (hintmsg)
2894 3 : *hintmsg = gettext_noop("Value exceeds integer range.");
2895 3 : return false;
2896 : }
2897 :
2898 40441 : if (result)
2899 40441 : *result = (int) val;
2900 40441 : return true;
2901 : }
2902 :
2903 : /*
2904 : * Try to parse value as a floating point number in the usual format.
2905 : * Optionally, the value can be followed by a unit name if "flags" indicates
2906 : * a unit is allowed.
2907 : *
2908 : * If the string parses okay, return true, else false.
2909 : * If okay and result is not NULL, return the value in *result.
2910 : * If not okay and hintmsg is not NULL, *hintmsg is set to a suitable
2911 : * HINT message, or NULL if no hint provided.
2912 : */
2913 : bool
2914 3594 : parse_real(const char *value, double *result, int flags, const char **hintmsg)
2915 : {
2916 : double val;
2917 : char *endptr;
2918 :
2919 : /* To suppress compiler warnings, always set output params */
2920 3594 : if (result)
2921 3594 : *result = 0;
2922 3594 : if (hintmsg)
2923 3356 : *hintmsg = NULL;
2924 :
2925 3594 : errno = 0;
2926 3594 : val = strtod(value, &endptr);
2927 :
2928 3594 : if (endptr == value || errno == ERANGE)
2929 8 : return false; /* no HINT for these cases */
2930 :
2931 : /* reject NaN (infinities will fail range checks later) */
2932 3586 : if (isnan(val))
2933 3 : return false; /* treat same as syntax error; no HINT */
2934 :
2935 : /* allow whitespace between number and unit */
2936 3583 : while (isspace((unsigned char) *endptr))
208 tgl 2937 UIC 0 : endptr++;
2938 :
2939 : /* Handle possible unit */
208 tgl 2940 GIC 3583 : if (*endptr != '\0')
2941 : {
2942 8 : if ((flags & GUC_UNIT) == 0)
2943 2 : return false; /* this setting does not accept a unit */
2944 :
2945 6 : if (!convert_to_base_unit(val,
2946 : endptr, (flags & GUC_UNIT),
2947 : &val))
2948 : {
2949 : /* invalid unit, or garbage after the unit; set hint and fail. */
208 tgl 2950 UIC 0 : if (hintmsg)
2951 : {
2952 0 : if (flags & GUC_UNIT_MEMORY)
2953 0 : *hintmsg = memory_units_hint;
2954 : else
2955 0 : *hintmsg = time_units_hint;
2956 : }
2957 0 : return false;
2958 : }
2959 : }
2960 :
208 tgl 2961 GIC 3581 : if (result)
2962 3581 : *result = val;
2963 3581 : return true;
2964 : }
2965 :
2966 :
2967 : /*
2968 : * Lookup the name for an enum option with the selected value.
2969 : * Should only ever be called with known-valid values, so throws
2970 : * an elog(ERROR) if the enum option is not found.
2971 : *
2972 : * The returned string is a pointer to static data and not
2973 : * allocated for modification.
2974 : */
2975 : const char *
2976 142858 : config_enum_lookup_by_value(struct config_enum *record, int val)
2977 : {
2978 : const struct config_enum_entry *entry;
2979 :
2980 381770 : for (entry = record->options; entry && entry->name; entry++)
2981 : {
2982 381770 : if (entry->val == val)
2983 142858 : return entry->name;
2984 : }
2985 :
208 tgl 2986 UIC 0 : elog(ERROR, "could not find enum option %d for %s",
2987 : val, record->gen.name);
2988 : return NULL; /* silence compiler */
2989 : }
2990 :
2991 :
2992 : /*
2993 : * Lookup the value for an enum option with the selected name
2994 : * (case-insensitive).
2995 : * If the enum option is found, sets the retval value and returns
2996 : * true. If it's not found, return false and retval is set to 0.
2997 : */
2998 : bool
208 tgl 2999 GIC 26446 : config_enum_lookup_by_name(struct config_enum *record, const char *value,
3000 : int *retval)
3001 : {
3002 : const struct config_enum_entry *entry;
3003 :
3004 61803 : for (entry = record->options; entry && entry->name; entry++)
3005 : {
3006 61788 : if (pg_strcasecmp(value, entry->name) == 0)
3007 : {
3008 26431 : *retval = entry->val;
3009 26431 : return true;
3010 : }
3011 : }
3012 :
3013 15 : *retval = 0;
3014 15 : return false;
3015 : }
3016 :
3017 :
3018 : /*
3019 : * Return a palloc'd string listing all the available options for an enum GUC
3020 : * (excluding hidden ones), separated by the given separator.
3021 : * If prefix is non-NULL, it is added before the first enum value.
3022 : * If suffix is non-NULL, it is added to the end of the string.
3023 : */
3024 : char *
3025 42358 : config_enum_get_options(struct config_enum *record, const char *prefix,
3026 : const char *suffix, const char *separator)
3027 : {
3028 : const struct config_enum_entry *entry;
3029 : StringInfoData retstr;
3030 : int seplen;
3031 :
3032 42358 : initStringInfo(&retstr);
3033 42358 : appendStringInfoString(&retstr, prefix);
3034 :
3035 42358 : seplen = strlen(separator);
3036 289990 : for (entry = record->options; entry && entry->name; entry++)
3037 : {
3038 247632 : if (!entry->hidden)
3039 : {
3040 178110 : appendStringInfoString(&retstr, entry->name);
3041 178110 : appendBinaryStringInfo(&retstr, separator, seplen);
3042 : }
3043 : }
3044 :
3045 : /*
3046 : * All the entries may have been hidden, leaving the string empty if no
3047 : * prefix was given. This indicates a broken GUC setup, since there is no
3048 : * use for an enum without any values, so we just check to make sure we
3049 : * don't write to invalid memory instead of actually trying to do
3050 : * something smart with it.
3051 : */
3052 42358 : if (retstr.len >= seplen)
3053 : {
3054 : /* Replace final separator */
3055 42358 : retstr.data[retstr.len - seplen] = '\0';
3056 42358 : retstr.len -= seplen;
3057 : }
3058 :
3059 42358 : appendStringInfoString(&retstr, suffix);
3060 :
3061 42358 : return retstr.data;
3062 : }
3063 :
3064 : /*
3065 : * Parse and validate a proposed value for the specified configuration
3066 : * parameter.
3067 : *
3068 : * This does built-in checks (such as range limits for an integer parameter)
3069 : * and also calls any check hook the parameter may have.
3070 : *
3071 : * record: GUC variable's info record
3072 : * name: variable name (should match the record of course)
3073 : * value: proposed value, as a string
3074 : * source: identifies source of value (check hooks may need this)
3075 : * elevel: level to log any error reports at
3076 : * newval: on success, converted parameter value is returned here
3077 : * newextra: on success, receives any "extra" data returned by check hook
3078 : * (caller must initialize *newextra to NULL)
3079 : *
3080 : * Returns true if OK, false if not (or throws error, if elevel >= ERROR)
3081 : */
3082 : static bool
3083 260557 : parse_and_validate_value(struct config_generic *record,
3084 : const char *name, const char *value,
3085 : GucSource source, int elevel,
3086 : union config_var_val *newval, void **newextra)
3087 : {
3088 260557 : switch (record->vartype)
3089 : {
3090 58426 : case PGC_BOOL:
3091 : {
3092 58426 : struct config_bool *conf = (struct config_bool *) record;
3093 :
3094 58426 : if (!parse_bool(value, &newval->boolval))
3095 : {
208 tgl 3096 UIC 0 : ereport(elevel,
3097 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3098 : errmsg("parameter \"%s\" requires a Boolean value",
3099 : name)));
3100 0 : return false;
3101 : }
3102 :
208 tgl 3103 GIC 58426 : if (!call_bool_check_hook(conf, &newval->boolval, newextra,
3104 : source, elevel))
208 tgl 3105 UIC 0 : return false;
3106 : }
208 tgl 3107 GIC 58414 : break;
3108 36970 : case PGC_INT:
3109 : {
3110 36970 : struct config_int *conf = (struct config_int *) record;
3111 : const char *hintmsg;
3112 :
3113 36970 : if (!parse_int(value, &newval->intval,
3114 : conf->gen.flags, &hintmsg))
3115 : {
208 tgl 3116 UIC 0 : ereport(elevel,
3117 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3118 : errmsg("invalid value for parameter \"%s\": \"%s\"",
3119 : name, value),
3120 : hintmsg ? errhint("%s", _(hintmsg)) : 0));
3121 0 : return false;
3122 : }
3123 :
208 tgl 3124 GIC 36970 : if (newval->intval < conf->min || newval->intval > conf->max)
3125 : {
208 tgl 3126 UIC 0 : const char *unit = get_config_unit_name(conf->gen.flags);
3127 :
3128 0 : ereport(elevel,
3129 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3130 : errmsg("%d%s%s is outside the valid range for parameter \"%s\" (%d .. %d)",
3131 : newval->intval,
3132 : unit ? " " : "",
3133 : unit ? unit : "",
3134 : name,
3135 : conf->min, conf->max)));
3136 0 : return false;
3137 : }
3138 :
208 tgl 3139 GIC 36970 : if (!call_int_check_hook(conf, &newval->intval, newextra,
3140 : source, elevel))
208 tgl 3141 UIC 0 : return false;
3142 : }
208 tgl 3143 GIC 36970 : break;
3144 3356 : case PGC_REAL:
3145 : {
3146 3356 : struct config_real *conf = (struct config_real *) record;
3147 : const char *hintmsg;
3148 :
3149 3356 : if (!parse_real(value, &newval->realval,
3150 : conf->gen.flags, &hintmsg))
3151 : {
3152 3 : ereport(elevel,
3153 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3154 : errmsg("invalid value for parameter \"%s\": \"%s\"",
3155 : name, value),
3156 : hintmsg ? errhint("%s", _(hintmsg)) : 0));
208 tgl 3157 UIC 0 : return false;
3158 : }
3159 :
208 tgl 3160 GIC 3353 : if (newval->realval < conf->min || newval->realval > conf->max)
3161 : {
3162 3 : const char *unit = get_config_unit_name(conf->gen.flags);
3163 :
3164 3 : ereport(elevel,
3165 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3166 : errmsg("%g%s%s is outside the valid range for parameter \"%s\" (%g .. %g)",
3167 : newval->realval,
3168 : unit ? " " : "",
3169 : unit ? unit : "",
3170 : name,
3171 : conf->min, conf->max)));
208 tgl 3172 UIC 0 : return false;
3173 : }
3174 :
208 tgl 3175 GIC 3350 : if (!call_real_check_hook(conf, &newval->realval, newextra,
3176 : source, elevel))
208 tgl 3177 UIC 0 : return false;
3178 : }
208 tgl 3179 GIC 3350 : break;
3180 135359 : case PGC_STRING:
3181 : {
3182 135359 : struct config_string *conf = (struct config_string *) record;
3183 :
3184 : /*
3185 : * The value passed by the caller could be transient, so we
3186 : * always strdup it.
3187 : */
3188 135359 : newval->stringval = guc_strdup(elevel, value);
3189 135359 : if (newval->stringval == NULL)
208 tgl 3190 UIC 0 : return false;
3191 :
3192 : /*
3193 : * The only built-in "parsing" check we have is to apply
3194 : * truncation if GUC_IS_NAME.
3195 : */
208 tgl 3196 GIC 135359 : if (conf->gen.flags & GUC_IS_NAME)
3197 47110 : truncate_identifier(newval->stringval,
3198 47110 : strlen(newval->stringval),
3199 : true);
3200 :
3201 135359 : if (!call_string_check_hook(conf, &newval->stringval, newextra,
3202 : source, elevel))
3203 : {
177 tgl 3204 UNC 0 : guc_free(newval->stringval);
208 tgl 3205 UIC 0 : newval->stringval = NULL;
3206 0 : return false;
3207 : }
3208 : }
208 tgl 3209 GIC 135338 : break;
3210 26446 : case PGC_ENUM:
3211 : {
3212 26446 : struct config_enum *conf = (struct config_enum *) record;
3213 :
3214 26446 : if (!config_enum_lookup_by_name(conf, value, &newval->enumval))
3215 : {
3216 : char *hintmsg;
3217 :
3218 15 : hintmsg = config_enum_get_options(conf,
3219 : "Available values: ",
3220 : ".", ", ");
3221 :
3222 15 : ereport(elevel,
3223 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3224 : errmsg("invalid value for parameter \"%s\": \"%s\"",
3225 : name, value),
3226 : hintmsg ? errhint("%s", _(hintmsg)) : 0));
3227 :
208 tgl 3228 UIC 0 : if (hintmsg)
3229 0 : pfree(hintmsg);
3230 0 : return false;
3231 : }
3232 :
208 tgl 3233 GIC 26431 : if (!call_enum_check_hook(conf, &newval->enumval, newextra,
3234 : source, elevel))
208 tgl 3235 UIC 0 : return false;
3236 : }
208 tgl 3237 GIC 26430 : break;
3238 : }
3239 :
3240 260502 : return true;
3241 : }
3242 :
3243 :
3244 : /*
3245 : * set_config_option: sets option `name' to given value.
3246 : *
3247 : * The value should be a string, which will be parsed and converted to
3248 : * the appropriate data type. The context and source parameters indicate
3249 : * in which context this function is being called, so that it can apply the
3250 : * access restrictions properly.
3251 : *
3252 : * If value is NULL, set the option to its default value (normally the
3253 : * reset_val, but if source == PGC_S_DEFAULT we instead use the boot_val).
3254 : *
3255 : * action indicates whether to set the value globally in the session, locally
3256 : * to the current top transaction, or just for the duration of a function call.
3257 : *
3258 : * If changeVal is false then don't really set the option but do all
3259 : * the checks to see if it would work.
3260 : *
3261 : * elevel should normally be passed as zero, allowing this function to make
3262 : * its standard choice of ereport level. However some callers need to be
3263 : * able to override that choice; they should pass the ereport level to use.
3264 : *
3265 : * is_reload should be true only when called from read_nondefault_variables()
3266 : * or RestoreGUCState(), where we are trying to load some other process's
3267 : * GUC settings into a new process.
3268 : *
3269 : * Return value:
3270 : * +1: the value is valid and was successfully applied.
3271 : * 0: the name or value is invalid (but see below).
3272 : * -1: the value was not applied because of context, priority, or changeVal.
3273 : *
3274 : * If there is an error (non-existing option, invalid value) then an
3275 : * ereport(ERROR) is thrown *unless* this is called for a source for which
3276 : * we don't want an ERROR (currently, those are defaults, the config file,
3277 : * and per-database or per-user settings, as well as callers who specify
3278 : * a less-than-ERROR elevel). In those cases we write a suitable error
3279 : * message via ereport() and return 0.
3280 : *
3281 : * See also SetConfigOption for an external interface.
3282 : */
3283 : int
3284 218157 : set_config_option(const char *name, const char *value,
3285 : GucContext context, GucSource source,
3286 : GucAction action, bool changeVal, int elevel,
3287 : bool is_reload)
3288 : {
3289 : Oid srole;
3290 :
3291 : /*
3292 : * Non-interactive sources should be treated as having all privileges,
3293 : * except for PGC_S_CLIENT. Note in particular that this is true for
3294 : * pg_db_role_setting sources (PGC_S_GLOBAL etc): we assume a suitable
3295 : * privilege check was done when the pg_db_role_setting entry was made.
3296 : */
3297 218157 : if (source >= PGC_S_INTERACTIVE || source == PGC_S_CLIENT)
3298 54916 : srole = GetUserId();
3299 : else
3300 163241 : srole = BOOTSTRAP_SUPERUSERID;
3301 :
3302 218157 : return set_config_option_ext(name, value,
3303 : context, source, srole,
3304 : action, changeVal, elevel,
3305 : is_reload);
3306 : }
3307 :
3308 : /*
3309 : * set_config_option_ext: sets option `name' to given value.
3310 : *
3311 : * This API adds the ability to explicitly specify which role OID
3312 : * is considered to be setting the value. Most external callers can use
3313 : * set_config_option() and let it determine that based on the GucSource,
3314 : * but there are a few that are supplying a value that was determined
3315 : * in some special way and need to override the decision. Also, when
3316 : * restoring a previously-assigned value, it's important to supply the
3317 : * same role OID that set the value originally; so all guc.c callers
3318 : * that are doing that type of thing need to call this directly.
3319 : *
3320 : * Generally, srole should be GetUserId() when the source is a SQL operation,
3321 : * or BOOTSTRAP_SUPERUSERID if the source is a config file or similar.
3322 : */
3323 : int
3324 262422 : set_config_option_ext(const char *name, const char *value,
3325 : GucContext context, GucSource source, Oid srole,
3326 : GucAction action, bool changeVal, int elevel,
3327 : bool is_reload)
3328 : {
3329 : struct config_generic *record;
3330 : union config_var_val newval_union;
3331 262422 : void *newextra = NULL;
3332 262422 : bool prohibitValueChange = false;
3333 : bool makeDefault;
3334 :
3335 262422 : if (elevel == 0)
3336 : {
3337 218164 : if (source == PGC_S_DEFAULT || source == PGC_S_FILE)
3338 : {
3339 : /*
3340 : * To avoid cluttering the log, only the postmaster bleats loudly
3341 : * about problems with the config file.
3342 : */
3343 32311 : elevel = IsUnderPostmaster ? DEBUG3 : LOG;
3344 : }
3345 185853 : else if (source == PGC_S_GLOBAL ||
3346 167745 : source == PGC_S_DATABASE ||
3347 167737 : source == PGC_S_USER ||
3348 : source == PGC_S_DATABASE_USER)
3349 18116 : elevel = WARNING;
3350 : else
3351 167737 : elevel = ERROR;
3352 : }
3353 :
3354 : /*
3355 : * GUC_ACTION_SAVE changes are acceptable during a parallel operation,
3356 : * because the current worker will also pop the change. We're probably
3357 : * dealing with a function having a proconfig entry. Only the function's
3358 : * body should observe the change, and peer workers do not share in the
3359 : * execution of a function call started by this worker.
3360 : *
3361 : * Other changes might need to affect other workers, so forbid them.
3362 : */
3363 262422 : if (IsInParallelMode() && changeVal && action != GUC_ACTION_SAVE)
208 tgl 3364 UIC 0 : ereport(elevel,
3365 : (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3366 : errmsg("cannot set parameters during a parallel operation")));
3367 :
208 tgl 3368 GIC 262422 : record = find_option(name, true, false, elevel);
3369 262386 : if (record == NULL)
208 tgl 3370 UIC 0 : return 0;
3371 :
3372 : /*
3373 : * Check if the option can be set at this time. See guc.h for the precise
3374 : * rules.
3375 : */
208 tgl 3376 GIC 262386 : switch (record->context)
3377 : {
3378 61009 : case PGC_INTERNAL:
3379 61009 : if (context != PGC_INTERNAL)
3380 : {
3381 2 : ereport(elevel,
3382 : (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
3383 : errmsg("parameter \"%s\" cannot be changed",
3384 : name)));
208 tgl 3385 UIC 0 : return 0;
3386 : }
208 tgl 3387 GIC 61007 : break;
3388 25073 : case PGC_POSTMASTER:
3389 25073 : if (context == PGC_SIGHUP)
3390 : {
3391 : /*
3392 : * We are re-reading a PGC_POSTMASTER variable from
3393 : * postgresql.conf. We can't change the setting, so we should
3394 : * give a warning if the DBA tries to change it. However,
3395 : * because of variant formats, canonicalization by check
3396 : * hooks, etc, we can't just compare the given string directly
3397 : * to what's stored. Set a flag to check below after we have
3398 : * the final storable value.
3399 : */
3400 3572 : prohibitValueChange = true;
3401 : }
3402 21501 : else if (context != PGC_POSTMASTER)
3403 : {
3404 4 : ereport(elevel,
3405 : (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
3406 : errmsg("parameter \"%s\" cannot be changed without restarting the server",
3407 : name)));
208 tgl 3408 UIC 0 : return 0;
3409 : }
208 tgl 3410 GIC 25069 : break;
3411 21617 : case PGC_SIGHUP:
3412 21617 : if (context != PGC_SIGHUP && context != PGC_POSTMASTER)
3413 : {
3414 3 : ereport(elevel,
3415 : (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
3416 : errmsg("parameter \"%s\" cannot be changed now",
3417 : name)));
208 tgl 3418 UIC 0 : return 0;
3419 : }
3420 :
3421 : /*
3422 : * Hmm, the idea of the SIGHUP context is "ought to be global, but
3423 : * can be changed after postmaster start". But there's nothing
3424 : * that prevents a crafty administrator from sending SIGHUP
3425 : * signals to individual backends only.
3426 : */
208 tgl 3427 GIC 21614 : break;
3428 108 : case PGC_SU_BACKEND:
3429 108 : if (context == PGC_BACKEND)
3430 : {
3431 : /*
3432 : * Check whether the requesting user has been granted
3433 : * privilege to set this GUC.
3434 : */
3435 : AclResult aclresult;
3436 :
208 tgl 3437 UIC 0 : aclresult = pg_parameter_aclcheck(name, srole, ACL_SET);
3438 0 : if (aclresult != ACLCHECK_OK)
3439 : {
3440 : /* No granted privilege */
3441 0 : ereport(elevel,
3442 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3443 : errmsg("permission denied to set parameter \"%s\"",
3444 : name)));
3445 0 : return 0;
3446 : }
3447 : }
3448 : /* fall through to process the same as PGC_BACKEND */
3449 : /* FALLTHROUGH */
3450 : case PGC_BACKEND:
208 tgl 3451 GIC 110 : if (context == PGC_SIGHUP)
3452 : {
3453 : /*
3454 : * If a PGC_BACKEND or PGC_SU_BACKEND parameter is changed in
3455 : * the config file, we want to accept the new value in the
3456 : * postmaster (whence it will propagate to
3457 : * subsequently-started backends), but ignore it in existing
3458 : * backends. This is a tad klugy, but necessary because we
3459 : * don't re-read the config file during backend start.
3460 : *
3461 : * In EXEC_BACKEND builds, this works differently: we load all
3462 : * non-default settings from the CONFIG_EXEC_PARAMS file
3463 : * during backend start. In that case we must accept
3464 : * PGC_SIGHUP settings, so as to have the same value as if
3465 : * we'd forked from the postmaster. This can also happen when
3466 : * using RestoreGUCState() within a background worker that
3467 : * needs to have the same settings as the user backend that
3468 : * started it. is_reload will be true when either situation
3469 : * applies.
3470 : */
3471 45 : if (IsUnderPostmaster && !is_reload)
3472 2 : return -1;
3473 : }
3474 65 : else if (context != PGC_POSTMASTER &&
3475 4 : context != PGC_BACKEND &&
3476 4 : context != PGC_SU_BACKEND &&
3477 : source != PGC_S_CLIENT)
3478 : {
3479 4 : ereport(elevel,
3480 : (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
3481 : errmsg("parameter \"%s\" cannot be set after connection start",
3482 : name)));
208 tgl 3483 UIC 0 : return 0;
3484 : }
208 tgl 3485 GIC 104 : break;
3486 14468 : case PGC_SUSET:
3487 14468 : if (context == PGC_USERSET || context == PGC_BACKEND)
3488 : {
3489 : /*
3490 : * Check whether the requesting user has been granted
3491 : * privilege to set this GUC.
3492 : */
3493 : AclResult aclresult;
3494 :
3495 17 : aclresult = pg_parameter_aclcheck(name, srole, ACL_SET);
3496 17 : if (aclresult != ACLCHECK_OK)
3497 : {
3498 : /* No granted privilege */
3499 10 : ereport(elevel,
3500 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3501 : errmsg("permission denied to set parameter \"%s\"",
3502 : name)));
3503 4 : return 0;
3504 : }
3505 : }
3506 14458 : break;
3507 140109 : case PGC_USERSET:
3508 : /* always okay */
3509 140109 : break;
3510 : }
3511 :
3512 : /*
3513 : * Disallow changing GUC_NOT_WHILE_SEC_REST values if we are inside a
3514 : * security restriction context. We can reject this regardless of the GUC
3515 : * context or source, mainly because sources that it might be reasonable
3516 : * to override for won't be seen while inside a function.
3517 : *
3518 : * Note: variables marked GUC_NOT_WHILE_SEC_REST should usually be marked
3519 : * GUC_NO_RESET_ALL as well, because ResetAllOptions() doesn't check this.
3520 : * An exception might be made if the reset value is assumed to be "safe".
3521 : *
3522 : * Note: this flag is currently used for "session_authorization" and
3523 : * "role". We need to prohibit changing these inside a local userid
3524 : * context because when we exit it, GUC won't be notified, leaving things
3525 : * out of sync. (This could be fixed by forcing a new GUC nesting level,
3526 : * but that would change behavior in possibly-undesirable ways.) Also, we
3527 : * prohibit changing these in a security-restricted operation because
3528 : * otherwise RESET could be used to regain the session user's privileges.
3529 : */
3530 262361 : if (record->flags & GUC_NOT_WHILE_SEC_REST)
3531 : {
3532 13780 : if (InLocalUserIdChange())
3533 : {
3534 : /*
3535 : * Phrasing of this error message is historical, but it's the most
3536 : * common case.
3537 : */
208 tgl 3538 UIC 0 : ereport(elevel,
3539 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3540 : errmsg("cannot set parameter \"%s\" within security-definer function",
3541 : name)));
3542 0 : return 0;
3543 : }
208 tgl 3544 GIC 13780 : if (InSecurityRestrictedOperation())
3545 : {
208 tgl 3546 UIC 0 : ereport(elevel,
3547 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3548 : errmsg("cannot set parameter \"%s\" within security-restricted operation",
3549 : name)));
3550 0 : return 0;
3551 : }
3552 : }
3553 :
3554 : /* Disallow resetting and saving GUC_NO_RESET values */
194 tgl 3555 GNC 262361 : if (record->flags & GUC_NO_RESET)
3556 : {
3557 13312 : if (value == NULL)
3558 : {
3559 9 : ereport(elevel,
3560 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3561 : errmsg("parameter \"%s\" cannot be reset", name)));
194 tgl 3562 UNC 0 : return 0;
3563 : }
194 tgl 3564 GNC 13303 : if (action == GUC_ACTION_SAVE)
3565 : {
3566 3 : ereport(elevel,
3567 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3568 : errmsg("parameter \"%s\" cannot be set locally in functions",
3569 : name)));
194 tgl 3570 UNC 0 : return 0;
3571 : }
3572 : }
3573 :
3574 : /*
3575 : * Should we set reset/stacked values? (If so, the behavior is not
3576 : * transactional.) This is done either when we get a default value from
3577 : * the database's/user's/client's default settings or when we reset a
3578 : * value to its default.
3579 : */
208 tgl 3580 GIC 262351 : makeDefault = changeVal && (source <= PGC_S_OVERRIDE) &&
3581 2 : ((value != NULL) || source == PGC_S_DEFAULT);
3582 :
3583 : /*
3584 : * Ignore attempted set if overridden by previously processed setting.
3585 : * However, if changeVal is false then plow ahead anyway since we are
3586 : * trying to find out if the value is potentially good, not actually use
3587 : * it. Also keep going if makeDefault is true, since we may want to set
3588 : * the reset/stacked values even if we can't set the variable itself.
3589 : */
3590 262349 : if (record->source > source)
3591 : {
3592 506 : if (changeVal && !makeDefault)
3593 : {
208 tgl 3594 UIC 0 : elog(DEBUG3, "\"%s\": setting ignored because previous source is higher priority",
3595 : name);
3596 0 : return -1;
3597 : }
208 tgl 3598 GIC 506 : changeVal = false;
3599 : }
3600 :
3601 : /*
3602 : * Evaluate value and set variable.
3603 : */
3604 262349 : switch (record->vartype)
3605 : {
3606 58997 : case PGC_BOOL:
3607 : {
3608 58997 : struct config_bool *conf = (struct config_bool *) record;
3609 :
3610 : #define newval (newval_union.boolval)
3611 :
3612 58997 : if (value)
3613 : {
3614 58413 : if (!parse_and_validate_value(record, name, value,
3615 : source, elevel,
3616 : &newval_union, &newextra))
208 tgl 3617 UIC 0 : return 0;
3618 : }
208 tgl 3619 GIC 584 : else if (source == PGC_S_DEFAULT)
3620 : {
208 tgl 3621 UIC 0 : newval = conf->boot_val;
3622 0 : if (!call_bool_check_hook(conf, &newval, &newextra,
3623 : source, elevel))
3624 0 : return 0;
3625 : }
3626 : else
3627 : {
208 tgl 3628 GIC 584 : newval = conf->reset_val;
3629 584 : newextra = conf->reset_extra;
3630 584 : source = conf->gen.reset_source;
3631 584 : context = conf->gen.reset_scontext;
3632 584 : srole = conf->gen.reset_srole;
3633 : }
3634 :
3635 58985 : if (prohibitValueChange)
3636 : {
3637 : /* Release newextra, unless it's reset_extra */
3638 510 : if (newextra && !extra_field_used(&conf->gen, newextra))
177 tgl 3639 UNC 0 : guc_free(newextra);
3640 :
208 tgl 3641 GIC 510 : if (*conf->variable != newval)
3642 : {
208 tgl 3643 UIC 0 : record->status |= GUC_PENDING_RESTART;
3644 0 : ereport(elevel,
3645 : (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
3646 : errmsg("parameter \"%s\" cannot be changed without restarting the server",
3647 : name)));
3648 0 : return 0;
3649 : }
208 tgl 3650 GIC 510 : record->status &= ~GUC_PENDING_RESTART;
3651 510 : return -1;
3652 : }
3653 :
3654 58475 : if (changeVal)
3655 : {
3656 : /* Save old value to support transaction abort */
3657 58449 : if (!makeDefault)
3658 13321 : push_old_value(&conf->gen, action);
3659 :
3660 58449 : if (conf->assign_hook)
208 tgl 3661 UIC 0 : conf->assign_hook(newval, newextra);
208 tgl 3662 GIC 58449 : *conf->variable = newval;
3663 58449 : set_extra_field(&conf->gen, &conf->gen.extra,
3664 : newextra);
177 tgl 3665 GNC 58449 : set_guc_source(&conf->gen, source);
208 tgl 3666 GIC 58449 : conf->gen.scontext = context;
3667 58449 : conf->gen.srole = srole;
3668 : }
3669 58475 : if (makeDefault)
3670 : {
3671 : GucStack *stack;
3672 :
3673 45142 : if (conf->gen.reset_source <= source)
3674 : {
3675 45128 : conf->reset_val = newval;
3676 45128 : set_extra_field(&conf->gen, &conf->reset_extra,
3677 : newextra);
3678 45128 : conf->gen.reset_source = source;
3679 45128 : conf->gen.reset_scontext = context;
3680 45128 : conf->gen.reset_srole = srole;
3681 : }
3682 45142 : for (stack = conf->gen.stack; stack; stack = stack->prev)
3683 : {
208 tgl 3684 UIC 0 : if (stack->source <= source)
3685 : {
3686 0 : stack->prior.val.boolval = newval;
3687 0 : set_extra_field(&conf->gen, &stack->prior.extra,
3688 : newextra);
3689 0 : stack->source = source;
3690 0 : stack->scontext = context;
3691 0 : stack->srole = srole;
3692 : }
3693 : }
3694 : }
3695 :
3696 : /* Perhaps we didn't install newextra anywhere */
208 tgl 3697 GIC 58475 : if (newextra && !extra_field_used(&conf->gen, newextra))
177 tgl 3698 UNC 0 : guc_free(newextra);
208 tgl 3699 GIC 58475 : break;
3700 :
3701 : #undef newval
3702 : }
3703 :
7196 bruce 3704 37084 : case PGC_INT:
3705 : {
208 tgl 3706 37084 : struct config_int *conf = (struct config_int *) record;
3707 :
3708 : #define newval (newval_union.intval)
3709 :
3710 37084 : if (value)
3711 : {
3712 36959 : if (!parse_and_validate_value(record, name, value,
3713 : source, elevel,
3714 : &newval_union, &newextra))
208 tgl 3715 UIC 0 : return 0;
3716 : }
208 tgl 3717 GIC 125 : else if (source == PGC_S_DEFAULT)
3718 : {
208 tgl 3719 UIC 0 : newval = conf->boot_val;
3720 0 : if (!call_int_check_hook(conf, &newval, &newextra,
3721 : source, elevel))
3722 0 : return 0;
3723 : }
3724 : else
3725 : {
208 tgl 3726 GIC 125 : newval = conf->reset_val;
3727 125 : newextra = conf->reset_extra;
3728 125 : source = conf->gen.reset_source;
3729 125 : context = conf->gen.reset_scontext;
3730 125 : srole = conf->gen.reset_srole;
3731 : }
3732 :
3733 37084 : if (prohibitValueChange)
3734 : {
3735 : /* Release newextra, unless it's reset_extra */
3736 1643 : if (newextra && !extra_field_used(&conf->gen, newextra))
177 tgl 3737 UNC 0 : guc_free(newextra);
3738 :
208 tgl 3739 GIC 1643 : if (*conf->variable != newval)
3740 : {
208 tgl 3741 UIC 0 : record->status |= GUC_PENDING_RESTART;
3742 0 : ereport(elevel,
3743 : (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
3744 : errmsg("parameter \"%s\" cannot be changed without restarting the server",
3745 : name)));
3746 0 : return 0;
3747 : }
208 tgl 3748 GIC 1643 : record->status &= ~GUC_PENDING_RESTART;
3749 1643 : return -1;
3750 : }
3751 :
3752 35441 : if (changeVal)
3753 : {
3754 : /* Save old value to support transaction abort */
3755 35065 : if (!makeDefault)
3756 8642 : push_old_value(&conf->gen, action);
3757 :
3758 35065 : if (conf->assign_hook)
3759 6365 : conf->assign_hook(newval, newextra);
3760 35065 : *conf->variable = newval;
3761 35065 : set_extra_field(&conf->gen, &conf->gen.extra,
3762 : newextra);
177 tgl 3763 GNC 35065 : set_guc_source(&conf->gen, source);
208 tgl 3764 GIC 35065 : conf->gen.scontext = context;
3765 35065 : conf->gen.srole = srole;
3766 : }
3767 35441 : if (makeDefault)
3768 : {
3769 : GucStack *stack;
3770 :
3771 26759 : if (conf->gen.reset_source <= source)
3772 : {
3773 26423 : conf->reset_val = newval;
3774 26423 : set_extra_field(&conf->gen, &conf->reset_extra,
3775 : newextra);
3776 26423 : conf->gen.reset_source = source;
3777 26423 : conf->gen.reset_scontext = context;
3778 26423 : conf->gen.reset_srole = srole;
3779 : }
3780 26759 : for (stack = conf->gen.stack; stack; stack = stack->prev)
3781 : {
208 tgl 3782 UIC 0 : if (stack->source <= source)
3783 : {
3784 0 : stack->prior.val.intval = newval;
3785 0 : set_extra_field(&conf->gen, &stack->prior.extra,
3786 : newextra);
3787 0 : stack->source = source;
3788 0 : stack->scontext = context;
3789 0 : stack->srole = srole;
3790 : }
3791 : }
3792 : }
3793 :
3794 : /* Perhaps we didn't install newextra anywhere */
208 tgl 3795 GIC 35441 : if (newextra && !extra_field_used(&conf->gen, newextra))
177 tgl 3796 UNC 0 : guc_free(newextra);
208 tgl 3797 GIC 35441 : break;
3798 :
3799 : #undef newval
3800 : }
3801 :
3802 3415 : case PGC_REAL:
3803 : {
3804 3415 : struct config_real *conf = (struct config_real *) record;
3805 :
3806 : #define newval (newval_union.realval)
3807 :
3808 3415 : if (value)
3809 : {
3810 3356 : if (!parse_and_validate_value(record, name, value,
3811 : source, elevel,
3812 : &newval_union, &newextra))
208 tgl 3813 UIC 0 : return 0;
3814 : }
208 tgl 3815 GIC 59 : else if (source == PGC_S_DEFAULT)
3816 : {
208 tgl 3817 UIC 0 : newval = conf->boot_val;
3818 0 : if (!call_real_check_hook(conf, &newval, &newextra,
3819 : source, elevel))
3820 0 : return 0;
3821 : }
3822 : else
3823 : {
208 tgl 3824 GIC 59 : newval = conf->reset_val;
3825 59 : newextra = conf->reset_extra;
3826 59 : source = conf->gen.reset_source;
3827 59 : context = conf->gen.reset_scontext;
3828 59 : srole = conf->gen.reset_srole;
3829 : }
3830 :
3831 3409 : if (prohibitValueChange)
3832 : {
3833 : /* Release newextra, unless it's reset_extra */
208 tgl 3834 UIC 0 : if (newextra && !extra_field_used(&conf->gen, newextra))
177 tgl 3835 UNC 0 : guc_free(newextra);
3836 :
208 tgl 3837 UIC 0 : if (*conf->variable != newval)
3838 : {
3839 0 : record->status |= GUC_PENDING_RESTART;
3840 0 : ereport(elevel,
3841 : (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
3842 : errmsg("parameter \"%s\" cannot be changed without restarting the server",
3843 : name)));
3844 0 : return 0;
3845 : }
3846 0 : record->status &= ~GUC_PENDING_RESTART;
3847 0 : return -1;
3848 : }
3849 :
208 tgl 3850 GIC 3409 : if (changeVal)
3851 : {
3852 : /* Save old value to support transaction abort */
3853 3409 : if (!makeDefault)
3854 3409 : push_old_value(&conf->gen, action);
3855 :
3856 3409 : if (conf->assign_hook)
208 tgl 3857 UIC 0 : conf->assign_hook(newval, newextra);
208 tgl 3858 GIC 3409 : *conf->variable = newval;
3859 3409 : set_extra_field(&conf->gen, &conf->gen.extra,
3860 : newextra);
177 tgl 3861 GNC 3409 : set_guc_source(&conf->gen, source);
208 tgl 3862 GIC 3409 : conf->gen.scontext = context;
3863 3409 : conf->gen.srole = srole;
3864 : }
3865 3409 : if (makeDefault)
3866 : {
3867 : GucStack *stack;
3868 :
208 tgl 3869 UIC 0 : if (conf->gen.reset_source <= source)
3870 : {
3871 0 : conf->reset_val = newval;
3872 0 : set_extra_field(&conf->gen, &conf->reset_extra,
3873 : newextra);
3874 0 : conf->gen.reset_source = source;
3875 0 : conf->gen.reset_scontext = context;
3876 0 : conf->gen.reset_srole = srole;
3877 : }
3878 0 : for (stack = conf->gen.stack; stack; stack = stack->prev)
3879 : {
3880 0 : if (stack->source <= source)
3881 : {
3882 0 : stack->prior.val.realval = newval;
3883 0 : set_extra_field(&conf->gen, &stack->prior.extra,
3884 : newextra);
3885 0 : stack->source = source;
3886 0 : stack->scontext = context;
3887 0 : stack->srole = srole;
3888 : }
3889 : }
3890 : }
3891 :
3892 : /* Perhaps we didn't install newextra anywhere */
208 tgl 3893 GIC 3409 : if (newextra && !extra_field_used(&conf->gen, newextra))
177 tgl 3894 UNC 0 : guc_free(newextra);
208 tgl 3895 GIC 3409 : break;
3896 :
3897 : #undef newval
3898 : }
3899 :
3900 136208 : case PGC_STRING:
3901 : {
3902 136208 : struct config_string *conf = (struct config_string *) record;
3903 :
3904 : #define newval (newval_union.stringval)
3905 :
3906 136208 : if (value)
3907 : {
3908 135344 : if (!parse_and_validate_value(record, name, value,
3909 : source, elevel,
3910 : &newval_union, &newextra))
208 tgl 3911 UIC 0 : return 0;
3912 : }
208 tgl 3913 GIC 864 : else if (source == PGC_S_DEFAULT)
3914 : {
3915 : /* non-NULL boot_val must always get strdup'd */
3916 2 : if (conf->boot_val != NULL)
3917 : {
3918 2 : newval = guc_strdup(elevel, conf->boot_val);
3919 2 : if (newval == NULL)
208 tgl 3920 UIC 0 : return 0;
3921 : }
3922 : else
3923 0 : newval = NULL;
3924 :
208 tgl 3925 GIC 2 : if (!call_string_check_hook(conf, &newval, &newextra,
3926 : source, elevel))
3927 : {
177 tgl 3928 UNC 0 : guc_free(newval);
208 tgl 3929 UIC 0 : return 0;
3930 : }
3931 : }
3932 : else
3933 : {
3934 : /*
3935 : * strdup not needed, since reset_val is already under
3936 : * guc.c's control
3937 : */
208 tgl 3938 GIC 862 : newval = conf->reset_val;
3939 862 : newextra = conf->reset_extra;
3940 862 : source = conf->gen.reset_source;
3941 862 : context = conf->gen.reset_scontext;
3942 862 : srole = conf->gen.reset_srole;
3943 : }
3944 :
3945 136187 : if (prohibitValueChange)
3946 : {
3947 : bool newval_different;
3948 :
3949 : /* newval shouldn't be NULL, so we're a bit sloppy here */
3950 2049 : newval_different = (*conf->variable == NULL ||
3951 1366 : newval == NULL ||
3952 683 : strcmp(*conf->variable, newval) != 0);
3953 :
3954 : /* Release newval, unless it's reset_val */
3955 683 : if (newval && !string_field_used(conf, newval))
177 tgl 3956 GNC 683 : guc_free(newval);
3957 : /* Release newextra, unless it's reset_extra */
208 tgl 3958 GIC 683 : if (newextra && !extra_field_used(&conf->gen, newextra))
177 tgl 3959 UNC 0 : guc_free(newextra);
3960 :
208 tgl 3961 GIC 683 : if (newval_different)
3962 : {
208 tgl 3963 UIC 0 : record->status |= GUC_PENDING_RESTART;
3964 0 : ereport(elevel,
3965 : (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
3966 : errmsg("parameter \"%s\" cannot be changed without restarting the server",
3967 : name)));
3968 0 : return 0;
3969 : }
208 tgl 3970 GIC 683 : record->status &= ~GUC_PENDING_RESTART;
3971 683 : return -1;
3972 : }
3973 :
3974 135504 : if (changeVal)
3975 : {
3976 : /* Save old value to support transaction abort */
3977 134881 : if (!makeDefault)
3978 13292 : push_old_value(&conf->gen, action);
3979 :
3980 134881 : if (conf->assign_hook)
3981 91944 : conf->assign_hook(newval, newextra);
3982 134880 : set_string_field(conf, conf->variable, newval);
3983 134880 : set_extra_field(&conf->gen, &conf->gen.extra,
3984 : newextra);
177 tgl 3985 GNC 134880 : set_guc_source(&conf->gen, source);
208 tgl 3986 GIC 134880 : conf->gen.scontext = context;
3987 134880 : conf->gen.srole = srole;
3988 : }
3989 :
3990 135503 : if (makeDefault)
3991 : {
3992 : GucStack *stack;
3993 :
3994 121713 : if (conf->gen.reset_source <= source)
3995 : {
3996 121588 : set_string_field(conf, &conf->reset_val, newval);
3997 121588 : set_extra_field(&conf->gen, &conf->reset_extra,
3998 : newextra);
3999 121588 : conf->gen.reset_source = source;
4000 121588 : conf->gen.reset_scontext = context;
4001 121588 : conf->gen.reset_srole = srole;
4002 : }
4003 121713 : for (stack = conf->gen.stack; stack; stack = stack->prev)
4004 : {
208 tgl 4005 UIC 0 : if (stack->source <= source)
4006 : {
4007 0 : set_string_field(conf, &stack->prior.val.stringval,
4008 : newval);
4009 0 : set_extra_field(&conf->gen, &stack->prior.extra,
4010 : newextra);
4011 0 : stack->source = source;
4012 0 : stack->scontext = context;
4013 0 : stack->srole = srole;
4014 : }
4015 : }
4016 : }
4017 :
4018 : /* Perhaps we didn't install newval anywhere */
208 tgl 4019 GIC 135503 : if (newval && !string_field_used(conf, newval))
177 tgl 4020 GNC 613 : guc_free(newval);
4021 : /* Perhaps we didn't install newextra anywhere */
208 tgl 4022 GIC 135503 : if (newextra && !extra_field_used(&conf->gen, newextra))
177 tgl 4023 GNC 206 : guc_free(newextra);
208 tgl 4024 GIC 135503 : break;
4025 :
4026 : #undef newval
4027 : }
4028 :
4029 26645 : case PGC_ENUM:
4030 : {
4031 26645 : struct config_enum *conf = (struct config_enum *) record;
4032 :
4033 : #define newval (newval_union.enumval)
4034 :
4035 26645 : if (value)
4036 : {
4037 26444 : if (!parse_and_validate_value(record, name, value,
4038 : source, elevel,
4039 : &newval_union, &newextra))
208 tgl 4040 UIC 0 : return 0;
4041 : }
208 tgl 4042 GIC 201 : else if (source == PGC_S_DEFAULT)
4043 : {
208 tgl 4044 UIC 0 : newval = conf->boot_val;
4045 0 : if (!call_enum_check_hook(conf, &newval, &newextra,
4046 : source, elevel))
4047 0 : return 0;
4048 : }
4049 : else
4050 : {
208 tgl 4051 GIC 201 : newval = conf->reset_val;
4052 201 : newextra = conf->reset_extra;
4053 201 : source = conf->gen.reset_source;
4054 201 : context = conf->gen.reset_scontext;
4055 201 : srole = conf->gen.reset_srole;
4056 : }
4057 :
4058 26629 : if (prohibitValueChange)
4059 : {
4060 : /* Release newextra, unless it's reset_extra */
4061 736 : if (newextra && !extra_field_used(&conf->gen, newextra))
177 tgl 4062 UNC 0 : guc_free(newextra);
4063 :
208 tgl 4064 GIC 736 : if (*conf->variable != newval)
4065 : {
208 tgl 4066 UIC 0 : record->status |= GUC_PENDING_RESTART;
4067 0 : ereport(elevel,
4068 : (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
4069 : errmsg("parameter \"%s\" cannot be changed without restarting the server",
4070 : name)));
4071 0 : return 0;
4072 : }
208 tgl 4073 GIC 736 : record->status &= ~GUC_PENDING_RESTART;
4074 736 : return -1;
4075 : }
4076 :
4077 25893 : if (changeVal)
4078 : {
4079 : /* Save old value to support transaction abort */
4080 25801 : if (!makeDefault)
4081 8512 : push_old_value(&conf->gen, action);
4082 :
4083 25801 : if (conf->assign_hook)
4084 815 : conf->assign_hook(newval, newextra);
4085 25801 : *conf->variable = newval;
4086 25801 : set_extra_field(&conf->gen, &conf->gen.extra,
4087 : newextra);
177 tgl 4088 GNC 25801 : set_guc_source(&conf->gen, source);
208 tgl 4089 GIC 25801 : conf->gen.scontext = context;
4090 25801 : conf->gen.srole = srole;
4091 : }
4092 25893 : if (makeDefault)
4093 : {
4094 : GucStack *stack;
4095 :
4096 17289 : if (conf->gen.reset_source <= source)
4097 : {
4098 17289 : conf->reset_val = newval;
4099 17289 : set_extra_field(&conf->gen, &conf->reset_extra,
4100 : newextra);
4101 17289 : conf->gen.reset_source = source;
4102 17289 : conf->gen.reset_scontext = context;
4103 17289 : conf->gen.reset_srole = srole;
4104 : }
4105 17289 : for (stack = conf->gen.stack; stack; stack = stack->prev)
4106 : {
208 tgl 4107 UIC 0 : if (stack->source <= source)
4108 : {
4109 0 : stack->prior.val.enumval = newval;
4110 0 : set_extra_field(&conf->gen, &stack->prior.extra,
4111 : newextra);
4112 0 : stack->source = source;
4113 0 : stack->scontext = context;
4114 0 : stack->srole = srole;
4115 : }
4116 : }
4117 : }
4118 :
4119 : /* Perhaps we didn't install newextra anywhere */
208 tgl 4120 GIC 25893 : if (newextra && !extra_field_used(&conf->gen, newextra))
177 tgl 4121 UNC 0 : guc_free(newextra);
208 tgl 4122 GIC 25893 : break;
4123 :
4124 : #undef newval
4125 : }
4126 : }
4127 :
177 tgl 4128 GNC 258721 : if (changeVal && (record->flags & GUC_REPORT) &&
4129 93523 : !(record->status & GUC_NEEDS_REPORT))
4130 : {
208 tgl 4131 GIC 68870 : record->status |= GUC_NEEDS_REPORT;
177 tgl 4132 GNC 68870 : slist_push_head(&guc_report_list, &record->report_link);
4133 : }
4134 :
208 tgl 4135 GIC 258721 : return changeVal ? 1 : -1;
4136 : }
4137 :
4138 :
4139 : /*
4140 : * Set the fields for source file and line number the setting came from.
4141 : */
4142 : static void
4143 51307 : set_config_sourcefile(const char *name, char *sourcefile, int sourceline)
4144 : {
4145 : struct config_generic *record;
4146 : int elevel;
4147 :
4148 : /*
4149 : * To avoid cluttering the log, only the postmaster bleats loudly about
4150 : * problems with the config file.
4151 : */
4152 51307 : elevel = IsUnderPostmaster ? DEBUG3 : LOG;
4153 :
4154 51307 : record = find_option(name, true, false, elevel);
4155 : /* should not happen */
4156 51307 : if (record == NULL)
208 tgl 4157 UIC 0 : return;
4158 :
208 tgl 4159 GIC 51307 : sourcefile = guc_strdup(elevel, sourcefile);
177 tgl 4160 GNC 51307 : guc_free(record->sourcefile);
208 tgl 4161 GIC 51307 : record->sourcefile = sourcefile;
4162 51307 : record->sourceline = sourceline;
4163 : }
4164 :
4165 : /*
4166 : * Set a config option to the given value.
4167 : *
4168 : * See also set_config_option; this is just the wrapper to be called from
4169 : * outside GUC. (This function should be used when possible, because its API
4170 : * is more stable than set_config_option's.)
4171 : *
4172 : * Note: there is no support here for setting source file/line, as it
4173 : * is currently not needed.
4174 : */
4175 : void
4176 129129 : SetConfigOption(const char *name, const char *value,
4177 : GucContext context, GucSource source)
4178 : {
4179 129129 : (void) set_config_option(name, value, context, source,
4180 : GUC_ACTION_SET, true, 0, false);
4181 129105 : }
4182 :
4183 :
4184 :
4185 : /*
4186 : * Fetch the current value of the option `name', as a string.
4187 : *
4188 : * If the option doesn't exist, return NULL if missing_ok is true (NOTE that
4189 : * this cannot be distinguished from a string variable with a NULL value!),
4190 : * otherwise throw an ereport and don't return.
4191 : *
4192 : * If restrict_privileged is true, we also enforce that only superusers and
4193 : * members of the pg_read_all_settings role can see GUC_SUPERUSER_ONLY
4194 : * variables. This should only be passed as true in user-driven calls.
4195 : *
4196 : * The string is *not* allocated for modification and is really only
4197 : * valid until the next call to configuration related functions.
4198 : */
4199 : const char *
4200 4551 : GetConfigOption(const char *name, bool missing_ok, bool restrict_privileged)
4201 : {
4202 : struct config_generic *record;
4203 : static char buffer[256];
4204 :
4205 4551 : record = find_option(name, false, missing_ok, ERROR);
4206 4551 : if (record == NULL)
208 tgl 4207 UIC 0 : return NULL;
208 tgl 4208 GIC 4551 : if (restrict_privileged &&
72 tgl 4209 UNC 0 : !ConfigOptionIsVisible(record))
208 tgl 4210 UIC 0 : ereport(ERROR,
4211 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
4212 : errmsg("permission denied to examine \"%s\"", name),
4213 : errdetail("Only roles with privileges of the \"%s\" role may examine this parameter.",
4214 : "pg_read_all_settings")));
4215 :
7632 tgl 4216 GIC 4551 : switch (record->vartype)
4217 : {
4218 865 : case PGC_BOOL:
208 4219 865 : return *((struct config_bool *) record)->variable ? "on" : "off";
4220 :
7632 4221 1161 : case PGC_INT:
208 4222 1161 : snprintf(buffer, sizeof(buffer), "%d",
4223 1161 : *((struct config_int *) record)->variable);
4224 1161 : return buffer;
4225 :
7632 tgl 4226 UIC 0 : case PGC_REAL:
208 4227 0 : snprintf(buffer, sizeof(buffer), "%g",
4228 0 : *((struct config_real *) record)->variable);
4229 0 : return buffer;
4230 :
7632 tgl 4231 GIC 1995 : case PGC_STRING:
208 4232 1995 : return *((struct config_string *) record)->variable;
4233 :
5508 magnus 4234 530 : case PGC_ENUM:
208 tgl 4235 530 : return config_enum_lookup_by_value((struct config_enum *) record,
4236 530 : *((struct config_enum *) record)->variable);
4237 : }
208 tgl 4238 UIC 0 : return NULL;
4239 : }
4240 :
4241 : /*
4242 : * Get the RESET value associated with the given option.
4243 : *
4244 : * Note: this is not re-entrant, due to use of static result buffer;
4245 : * not to mention that a string variable could have its reset_val changed.
4246 : * Beware of assuming the result value is good for very long.
4247 : */
4248 : const char *
4249 0 : GetConfigOptionResetString(const char *name)
4250 : {
4251 : struct config_generic *record;
4252 : static char buffer[256];
4253 :
4254 0 : record = find_option(name, false, false, ERROR);
4255 0 : Assert(record != NULL);
72 tgl 4256 UNC 0 : if (!ConfigOptionIsVisible(record))
208 tgl 4257 UIC 0 : ereport(ERROR,
4258 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
4259 : errmsg("permission denied to examine \"%s\"", name),
4260 : errdetail("Only roles with privileges of the \"%s\" role may examine this parameter.",
4261 : "pg_read_all_settings")));
4262 :
4263 0 : switch (record->vartype)
4264 : {
5210 4265 0 : case PGC_BOOL:
208 4266 0 : return ((struct config_bool *) record)->reset_val ? "on" : "off";
4267 :
5210 4268 0 : case PGC_INT:
208 4269 0 : snprintf(buffer, sizeof(buffer), "%d",
4270 : ((struct config_int *) record)->reset_val);
4271 0 : return buffer;
4272 :
4273 0 : case PGC_REAL:
4274 0 : snprintf(buffer, sizeof(buffer), "%g",
4275 : ((struct config_real *) record)->reset_val);
4276 0 : return buffer;
4277 :
5210 4278 0 : case PGC_STRING:
208 4279 0 : return ((struct config_string *) record)->reset_val;
4280 :
5210 4281 0 : case PGC_ENUM:
208 4282 0 : return config_enum_lookup_by_value((struct config_enum *) record,
4283 : ((struct config_enum *) record)->reset_val);
4284 : }
4285 0 : return NULL;
4286 : }
4287 :
4288 : /*
4289 : * Get the GUC flags associated with the given option.
4290 : *
4291 : * If the option doesn't exist, return 0 if missing_ok is true,
4292 : * otherwise throw an ereport and don't return.
4293 : */
4294 : int
208 tgl 4295 GIC 17 : GetConfigOptionFlags(const char *name, bool missing_ok)
4296 : {
4297 : struct config_generic *record;
4298 :
4299 17 : record = find_option(name, false, missing_ok, ERROR);
4300 17 : if (record == NULL)
208 tgl 4301 UIC 0 : return 0;
208 tgl 4302 GIC 17 : return record->flags;
4303 : }
4304 :
4305 :
4306 : /*
4307 : * Write updated configuration parameter values into a temporary file.
4308 : * This function traverses the list of parameters and quotes the string
4309 : * values before writing them.
4310 : */
4311 : static void
4312 58 : write_auto_conf_file(int fd, const char *filename, ConfigVariable *head)
4313 : {
4314 : StringInfoData buf;
4315 : ConfigVariable *item;
4316 :
4317 58 : initStringInfo(&buf);
4318 :
4319 : /* Emit file header containing warning comment */
4320 58 : appendStringInfoString(&buf, "# Do not edit this file manually!\n");
4321 58 : appendStringInfoString(&buf, "# It will be overwritten by the ALTER SYSTEM command.\n");
4322 :
4323 58 : errno = 0;
4324 58 : if (write(fd, buf.data, buf.len) != buf.len)
4325 : {
4326 : /* if write didn't set errno, assume problem is no disk space */
208 tgl 4327 UIC 0 : if (errno == 0)
4328 0 : errno = ENOSPC;
4329 0 : ereport(ERROR,
4330 : (errcode_for_file_access(),
4331 : errmsg("could not write to file \"%s\": %m", filename)));
4332 : }
4333 :
4334 : /* Emit each parameter, properly quoting the value */
208 tgl 4335 GIC 117 : for (item = head; item != NULL; item = item->next)
4336 : {
4337 : char *escaped;
4338 :
4339 59 : resetStringInfo(&buf);
4340 :
4341 59 : appendStringInfoString(&buf, item->name);
4342 59 : appendStringInfoString(&buf, " = '");
4343 :
4344 59 : escaped = escape_single_quotes_ascii(item->value);
4345 59 : if (!escaped)
208 tgl 4346 UIC 0 : ereport(ERROR,
4347 : (errcode(ERRCODE_OUT_OF_MEMORY),
4348 : errmsg("out of memory")));
208 tgl 4349 GIC 59 : appendStringInfoString(&buf, escaped);
4350 59 : free(escaped);
4351 :
4352 59 : appendStringInfoString(&buf, "'\n");
4353 :
4354 59 : errno = 0;
4355 59 : if (write(fd, buf.data, buf.len) != buf.len)
4356 : {
4357 : /* if write didn't set errno, assume problem is no disk space */
208 tgl 4358 UIC 0 : if (errno == 0)
4359 0 : errno = ENOSPC;
4360 0 : ereport(ERROR,
4361 : (errcode_for_file_access(),
4362 : errmsg("could not write to file \"%s\": %m", filename)));
4363 : }
4364 : }
4365 :
4366 : /* fsync before considering the write to be successful */
208 tgl 4367 GIC 58 : if (pg_fsync(fd) != 0)
208 tgl 4368 UIC 0 : ereport(ERROR,
4369 : (errcode_for_file_access(),
4370 : errmsg("could not fsync file \"%s\": %m", filename)));
4371 :
208 tgl 4372 GIC 58 : pfree(buf.data);
7282 bruce 4373 58 : }
4374 :
4375 : /*
4376 : * Update the given list of configuration parameters, adding, replacing
4377 : * or deleting the entry for item "name" (delete if "value" == NULL).
4378 : */
4379 : static void
208 tgl 4380 58 : replace_auto_config_value(ConfigVariable **head_p, ConfigVariable **tail_p,
4381 : const char *name, const char *value)
4382 : {
4383 : ConfigVariable *item,
4384 : *next,
4385 58 : *prev = NULL;
4386 :
4387 : /*
4388 : * Remove any existing match(es) for "name". Normally there'd be at most
4389 : * one, but if external tools have modified the config file, there could
4390 : * be more.
4391 : */
4392 104 : for (item = *head_p; item != NULL; item = next)
4393 : {
4394 46 : next = item->next;
4395 46 : if (guc_name_compare(item->name, name) == 0)
4396 : {
4397 : /* found a match, delete it */
4398 28 : if (prev)
4399 3 : prev->next = next;
4400 : else
4401 25 : *head_p = next;
4402 28 : if (next == NULL)
4403 27 : *tail_p = prev;
4404 :
4405 28 : pfree(item->name);
4406 28 : pfree(item->value);
4407 28 : pfree(item->filename);
4408 28 : pfree(item);
4409 : }
4410 : else
4411 18 : prev = item;
4412 : }
4413 :
4414 : /* Done if we're trying to delete it */
4415 58 : if (value == NULL)
4416 17 : return;
4417 :
4418 : /* OK, append a new entry */
4419 41 : item = palloc(sizeof *item);
4420 41 : item->name = pstrdup(name);
4421 41 : item->value = pstrdup(value);
4422 41 : item->errmsg = NULL;
4423 41 : item->filename = pstrdup(""); /* new item has no location */
4424 41 : item->sourceline = 0;
4425 41 : item->ignore = false;
4426 41 : item->applied = false;
4427 41 : item->next = NULL;
4428 :
4429 41 : if (*head_p == NULL)
4430 32 : *head_p = item;
4431 : else
4432 9 : (*tail_p)->next = item;
4433 41 : *tail_p = item;
4434 : }
4435 :
4436 :
4437 : /*
4438 : * Execute ALTER SYSTEM statement.
4439 : *
4440 : * Read the old PG_AUTOCONF_FILENAME file, merge in the new variable value,
4441 : * and write out an updated file. If the command is ALTER SYSTEM RESET ALL,
4442 : * we can skip reading the old file and just write an empty file.
4443 : *
4444 : * An LWLock is used to serialize updates of the configuration file.
4445 : *
4446 : * In case of an error, we leave the original automatic
4447 : * configuration file (PG_AUTOCONF_FILENAME) intact.
4448 : */
4449 : void
4450 77 : AlterSystemSetConfigFile(AlterSystemStmt *altersysstmt)
4451 : {
4452 : char *name;
4453 : char *value;
4454 77 : bool resetall = false;
4455 77 : ConfigVariable *head = NULL;
4456 77 : ConfigVariable *tail = NULL;
4457 : volatile int Tmpfd;
4458 : char AutoConfFileName[MAXPGPATH];
4459 : char AutoConfTmpFileName[MAXPGPATH];
4460 :
4461 : /*
4462 : * Extract statement arguments
4463 : */
4464 77 : name = altersysstmt->setstmt->name;
4465 :
4466 77 : switch (altersysstmt->setstmt->kind)
4467 : {
4468 53 : case VAR_SET_VALUE:
4469 53 : value = ExtractSetVariableArgs(altersysstmt->setstmt);
4470 53 : break;
4471 :
4472 23 : case VAR_SET_DEFAULT:
4473 : case VAR_RESET:
4474 23 : value = NULL;
4475 23 : break;
4476 :
4477 1 : case VAR_RESET_ALL:
4478 1 : value = NULL;
4479 1 : resetall = true;
4480 1 : break;
4481 :
208 tgl 4482 UIC 0 : default:
4483 0 : elog(ERROR, "unrecognized alter system stmt type: %d",
4484 : altersysstmt->setstmt->kind);
4485 : break;
4486 : }
4487 :
4488 : /*
4489 : * Check permission to run ALTER SYSTEM on the target variable
4490 : */
208 tgl 4491 GIC 77 : if (!superuser())
4492 : {
4493 22 : if (resetall)
4494 1 : ereport(ERROR,
4495 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
4496 : errmsg("permission denied to perform ALTER SYSTEM RESET ALL")));
4497 : else
4498 : {
4499 : AclResult aclresult;
4500 :
4501 21 : aclresult = pg_parameter_aclcheck(name, GetUserId(),
4502 : ACL_ALTER_SYSTEM);
4503 21 : if (aclresult != ACLCHECK_OK)
4504 12 : ereport(ERROR,
4505 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
4506 : errmsg("permission denied to set parameter \"%s\"",
4507 : name)));
4508 : }
4509 : }
4510 :
4511 : /*
4512 : * Unless it's RESET_ALL, validate the target variable and value
4513 : */
4514 64 : if (!resetall)
4515 : {
4516 : struct config_generic *record;
4517 :
4518 64 : record = find_option(name, false, false, ERROR);
4519 64 : Assert(record != NULL);
4520 :
4521 : /*
4522 : * Don't allow parameters that can't be set in configuration files to
4523 : * be set in PG_AUTOCONF_FILENAME file.
4524 : */
4525 64 : if ((record->context == PGC_INTERNAL) ||
4526 62 : (record->flags & GUC_DISALLOW_IN_FILE) ||
4527 58 : (record->flags & GUC_DISALLOW_IN_AUTO_FILE))
4528 6 : ereport(ERROR,
4529 : (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
4530 : errmsg("parameter \"%s\" cannot be changed",
4531 : name)));
4532 :
4533 : /*
4534 : * If a value is specified, verify that it's sane.
4535 : */
4536 58 : if (value)
4537 : {
4538 : union config_var_val newval;
4539 41 : void *newextra = NULL;
4540 :
4541 : /* Check that it's acceptable for the indicated parameter */
4542 41 : if (!parse_and_validate_value(record, name, value,
4543 : PGC_S_FILE, ERROR,
4544 : &newval, &newextra))
208 tgl 4545 UIC 0 : ereport(ERROR,
4546 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4547 : errmsg("invalid value for parameter \"%s\": \"%s\"",
4548 : name, value)));
4549 :
208 tgl 4550 GIC 41 : if (record->vartype == PGC_STRING && newval.stringval != NULL)
177 tgl 4551 GNC 15 : guc_free(newval.stringval);
4552 41 : guc_free(newextra);
4553 :
4554 : /*
4555 : * We must also reject values containing newlines, because the
4556 : * grammar for config files doesn't support embedded newlines in
4557 : * string literals.
4558 : */
208 tgl 4559 GIC 41 : if (strchr(value, '\n'))
208 tgl 4560 UIC 0 : ereport(ERROR,
4561 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4562 : errmsg("parameter value for ALTER SYSTEM must not contain a newline")));
4563 : }
4564 : }
4565 :
4566 : /*
4567 : * PG_AUTOCONF_FILENAME and its corresponding temporary file are always in
4568 : * the data directory, so we can reference them by simple relative paths.
4569 : */
208 tgl 4570 GIC 58 : snprintf(AutoConfFileName, sizeof(AutoConfFileName), "%s",
4571 : PG_AUTOCONF_FILENAME);
4572 58 : snprintf(AutoConfTmpFileName, sizeof(AutoConfTmpFileName), "%s.%s",
4573 : AutoConfFileName,
4574 : "tmp");
4575 :
4576 : /*
4577 : * Only one backend is allowed to operate on PG_AUTOCONF_FILENAME at a
4578 : * time. Use AutoFileLock to ensure that. We must hold the lock while
4579 : * reading the old file contents.
4580 : */
4581 58 : LWLockAcquire(AutoFileLock, LW_EXCLUSIVE);
4582 :
4583 : /*
4584 : * If we're going to reset everything, then no need to open or parse the
4585 : * old file. We'll just write out an empty list.
4586 : */
4587 58 : if (!resetall)
4588 : {
4589 : struct stat st;
4590 :
4591 58 : if (stat(AutoConfFileName, &st) == 0)
4592 : {
4593 : /* open old file PG_AUTOCONF_FILENAME */
4594 : FILE *infile;
4595 :
4596 58 : infile = AllocateFile(AutoConfFileName, "r");
4597 58 : if (infile == NULL)
208 tgl 4598 UIC 0 : ereport(ERROR,
4599 : (errcode_for_file_access(),
4600 : errmsg("could not open file \"%s\": %m",
4601 : AutoConfFileName)));
4602 :
4603 : /* parse it */
135 michael 4604 GNC 58 : if (!ParseConfigFp(infile, AutoConfFileName, CONF_FILE_START_DEPTH,
4605 : LOG, &head, &tail))
208 tgl 4606 UIC 0 : ereport(ERROR,
4607 : (errcode(ERRCODE_CONFIG_FILE_ERROR),
4608 : errmsg("could not parse contents of file \"%s\"",
4609 : AutoConfFileName)));
4610 :
208 tgl 4611 GIC 58 : FreeFile(infile);
4612 : }
4613 :
4614 : /*
4615 : * Now, replace any existing entry with the new value, or add it if
4616 : * not present.
4617 : */
4618 58 : replace_auto_config_value(&head, &tail, name, value);
4619 : }
4620 :
4621 : /*
4622 : * Invoke the post-alter hook for setting this GUC variable. GUCs
4623 : * typically do not have corresponding entries in pg_parameter_acl, so we
4624 : * call the hook using the name rather than a potentially-non-existent
4625 : * OID. Nonetheless, we pass ParameterAclRelationId so that this call
4626 : * context can be distinguished from others. (Note that "name" will be
4627 : * NULL in the RESET ALL case.)
4628 : *
4629 : * We do this here rather than at the end, because ALTER SYSTEM is not
4630 : * transactional. If the hook aborts our transaction, it will be cleaner
4631 : * to do so before we touch any files.
4632 : */
4633 58 : InvokeObjectPostAlterHookArgStr(ParameterAclRelationId, name,
4634 : ACL_ALTER_SYSTEM,
4635 : altersysstmt->setstmt->kind,
4636 : false);
4637 :
4638 : /*
4639 : * To ensure crash safety, first write the new file data to a temp file,
4640 : * then atomically rename it into place.
4641 : *
4642 : * If there is a temp file left over due to a previous crash, it's okay to
4643 : * truncate and reuse it.
4644 : */
4645 58 : Tmpfd = BasicOpenFile(AutoConfTmpFileName,
4646 : O_CREAT | O_RDWR | O_TRUNC);
4647 58 : if (Tmpfd < 0)
208 tgl 4648 UIC 0 : ereport(ERROR,
4649 : (errcode_for_file_access(),
4650 : errmsg("could not open file \"%s\": %m",
4651 : AutoConfTmpFileName)));
4652 :
4653 : /*
4654 : * Use a TRY block to clean up the file if we fail. Since we need a TRY
4655 : * block anyway, OK to use BasicOpenFile rather than OpenTransientFile.
4656 : */
208 tgl 4657 GIC 58 : PG_TRY();
4658 : {
4659 : /* Write and sync the new contents to the temporary file */
4660 58 : write_auto_conf_file(Tmpfd, AutoConfTmpFileName, head);
4661 :
4662 : /* Close before renaming; may be required on some platforms */
4663 58 : close(Tmpfd);
4664 58 : Tmpfd = -1;
4665 :
4666 : /*
4667 : * As the rename is atomic operation, if any problem occurs after this
4668 : * at worst it can lose the parameters set by last ALTER SYSTEM
4669 : * command.
4670 : */
4671 58 : durable_rename(AutoConfTmpFileName, AutoConfFileName, ERROR);
4672 : }
208 tgl 4673 UIC 0 : PG_CATCH();
4674 : {
4675 : /* Close file first, else unlink might fail on some platforms */
4676 0 : if (Tmpfd >= 0)
4677 0 : close(Tmpfd);
4678 :
4679 : /* Unlink, but ignore any error */
4680 0 : (void) unlink(AutoConfTmpFileName);
4681 :
4682 0 : PG_RE_THROW();
4683 : }
208 tgl 4684 GIC 58 : PG_END_TRY();
4685 :
4686 58 : FreeConfigVariables(head);
4687 :
4688 58 : LWLockRelease(AutoFileLock);
3058 rhaas 4689 58 : }
4690 :
4691 :
4692 : /*
4693 : * Common code for DefineCustomXXXVariable subroutines: allocate the
4694 : * new variable's config struct and fill in generic fields.
4695 : */
4696 : static struct config_generic *
208 tgl 4697 9173 : init_custom_variable(const char *name,
4698 : const char *short_desc,
4699 : const char *long_desc,
4700 : GucContext context,
4701 : int flags,
4702 : enum config_type type,
4703 : size_t sz)
4704 : {
4705 : struct config_generic *gen;
4706 :
4707 : /*
4708 : * Only allow custom PGC_POSTMASTER variables to be created during shared
4709 : * library preload; any later than that, we can't ensure that the value
4710 : * doesn't change after startup. This is a fatal elog if it happens; just
4711 : * erroring out isn't safe because we don't know what the calling loadable
4712 : * module might already have hooked into.
4713 : */
4714 9173 : if (context == PGC_POSTMASTER &&
4715 7 : !process_shared_preload_libraries_in_progress)
208 tgl 4716 UIC 0 : elog(FATAL, "cannot create PGC_POSTMASTER variables after startup");
4717 :
4718 : /*
4719 : * We can't support custom GUC_LIST_QUOTE variables, because the wrong
4720 : * things would happen if such a variable were set or pg_dump'd when the
4721 : * defining extension isn't loaded. Again, treat this as fatal because
4722 : * the loadable module may be partly initialized already.
4723 : */
208 tgl 4724 GIC 9173 : if (flags & GUC_LIST_QUOTE)
208 tgl 4725 UIC 0 : elog(FATAL, "extensions cannot define GUC_LIST_QUOTE variables");
4726 :
4727 : /*
4728 : * Before pljava commit 398f3b876ed402bdaec8bc804f29e2be95c75139
4729 : * (2015-12-15), two of that module's PGC_USERSET variables facilitated
4730 : * trivial escalation to superuser privileges. Restrict the variables to
4731 : * protect sites that have yet to upgrade pljava.
4732 : */
208 tgl 4733 GIC 9173 : if (context == PGC_USERSET &&
4734 7120 : (strcmp(name, "pljava.classpath") == 0 ||
4735 7120 : strcmp(name, "pljava.vmoptions") == 0))
208 tgl 4736 UIC 0 : context = PGC_SUSET;
4737 :
208 tgl 4738 GIC 9173 : gen = (struct config_generic *) guc_malloc(ERROR, sz);
4739 9173 : memset(gen, 0, sz);
4740 :
4741 9173 : gen->name = guc_strdup(ERROR, name);
4742 9173 : gen->context = context;
4743 9173 : gen->group = CUSTOM_OPTIONS;
4744 9173 : gen->short_desc = short_desc;
4745 9173 : gen->long_desc = long_desc;
4746 9173 : gen->flags = flags;
4747 9173 : gen->vartype = type;
4748 :
4749 9173 : return gen;
4750 : }
4751 :
4752 : /*
4753 : * Common code for DefineCustomXXXVariable subroutines: insert the new
4754 : * variable into the GUC variable hash, replacing any placeholder.
4755 : */
4756 : static void
4757 9173 : define_custom_variable(struct config_generic *variable)
4758 : {
4759 9173 : const char *name = variable->name;
4760 : GUCHashEntry *hentry;
4761 : struct config_string *pHolder;
4762 :
4763 : /* Check mapping between initial and default value */
160 michael 4764 GNC 9173 : Assert(check_GUC_init(variable));
4765 :
4766 : /*
4767 : * See if there's a placeholder by the same name.
4768 : */
177 tgl 4769 9173 : hentry = (GUCHashEntry *) hash_search(guc_hashtab,
4770 : &name,
4771 : HASH_FIND,
4772 : NULL);
4773 9173 : if (hentry == NULL)
4774 : {
4775 : /*
4776 : * No placeholder to replace, so we can just add it ... but first,
4777 : * make sure it's initialized to its default value.
4778 : */
208 tgl 4779 GIC 9122 : InitializeOneGUCOption(variable);
4780 9122 : add_guc_variable(variable, ERROR);
4781 9122 : return;
4782 : }
4783 :
4784 : /*
4785 : * This better be a placeholder
4786 : */
177 tgl 4787 GNC 51 : if ((hentry->gucvar->flags & GUC_CUSTOM_PLACEHOLDER) == 0)
208 tgl 4788 UIC 0 : ereport(ERROR,
4789 : (errcode(ERRCODE_INTERNAL_ERROR),
4790 : errmsg("attempt to redefine parameter \"%s\"", name)));
4791 :
177 tgl 4792 GNC 51 : Assert(hentry->gucvar->vartype == PGC_STRING);
4793 51 : pHolder = (struct config_string *) hentry->gucvar;
4794 :
4795 : /*
4796 : * First, set the variable to its default value. We must do this even
4797 : * though we intend to immediately apply a new value, since it's possible
4798 : * that the new value is invalid.
4799 : */
208 tgl 4800 GIC 51 : InitializeOneGUCOption(variable);
4801 :
4802 : /*
4803 : * Replace the placeholder in the hash table. We aren't changing the name
4804 : * (at least up to case-folding), so the hash value is unchanged.
4805 : */
177 tgl 4806 GNC 51 : hentry->gucname = name;
4807 51 : hentry->gucvar = variable;
4808 :
4809 : /*
4810 : * Remove the placeholder from any lists it's in, too.
4811 : */
4812 51 : RemoveGUCFromLists(&pHolder->gen);
4813 :
4814 : /*
4815 : * Assign the string value(s) stored in the placeholder to the real
4816 : * variable. Essentially, we need to duplicate all the active and stacked
4817 : * values, but with appropriate validation and datatype adjustment.
4818 : *
4819 : * If an assignment fails, we report a WARNING and keep going. We don't
4820 : * want to throw ERROR for bad values, because it'd bollix the add-on
4821 : * module that's presumably halfway through getting loaded. In such cases
4822 : * the default or previous state will become active instead.
4823 : */
4824 :
4825 : /* First, apply the reset value if any */
208 tgl 4826 GIC 51 : if (pHolder->reset_val)
4827 47 : (void) set_config_option_ext(name, pHolder->reset_val,
4828 : pHolder->gen.reset_scontext,
4829 : pHolder->gen.reset_source,
4830 : pHolder->gen.reset_srole,
4831 : GUC_ACTION_SET, true, WARNING, false);
4832 : /* That should not have resulted in stacking anything */
4833 51 : Assert(variable->stack == NULL);
4834 :
4835 : /* Now, apply current and stacked values, in the order they were stacked */
4836 51 : reapply_stacked_values(variable, pHolder, pHolder->gen.stack,
4837 51 : *(pHolder->variable),
4838 : pHolder->gen.scontext, pHolder->gen.source,
4839 : pHolder->gen.srole);
4840 :
4841 : /* Also copy over any saved source-location information */
4842 51 : if (pHolder->gen.sourcefile)
4843 35 : set_config_sourcefile(name, pHolder->gen.sourcefile,
4844 : pHolder->gen.sourceline);
4845 :
4846 : /*
4847 : * Free up as much as we conveniently can of the placeholder structure.
4848 : * (This neglects any stack items, so it's possible for some memory to be
4849 : * leaked. Since this can only happen once per session per variable, it
4850 : * doesn't seem worth spending much code on.)
4851 : */
4852 51 : set_string_field(pHolder, pHolder->variable, NULL);
4853 51 : set_string_field(pHolder, &pHolder->reset_val, NULL);
4854 :
177 tgl 4855 GNC 51 : guc_free(pHolder);
4856 : }
4857 :
4858 : /*
4859 : * Recursive subroutine for define_custom_variable: reapply non-reset values
4860 : *
4861 : * We recurse so that the values are applied in the same order as originally.
4862 : * At each recursion level, apply the upper-level value (passed in) in the
4863 : * fashion implied by the stack entry.
4864 : */
4865 : static void
208 tgl 4866 GIC 51 : reapply_stacked_values(struct config_generic *variable,
4867 : struct config_string *pHolder,
4868 : GucStack *stack,
4869 : const char *curvalue,
4870 : GucContext curscontext, GucSource cursource,
4871 : Oid cursrole)
4872 : {
4873 51 : const char *name = variable->name;
4874 51 : GucStack *oldvarstack = variable->stack;
4875 :
4876 51 : if (stack != NULL)
4877 : {
4878 : /* First, recurse, so that stack items are processed bottom to top */
208 tgl 4879 UIC 0 : reapply_stacked_values(variable, pHolder, stack->prev,
4880 0 : stack->prior.val.stringval,
4881 : stack->scontext, stack->source, stack->srole);
4882 :
4883 : /* See how to apply the passed-in value */
4884 0 : switch (stack->state)
4885 : {
4886 0 : case GUC_SAVE:
4887 0 : (void) set_config_option_ext(name, curvalue,
4888 : curscontext, cursource, cursrole,
4889 : GUC_ACTION_SAVE, true,
4890 : WARNING, false);
4891 0 : break;
4892 :
4893 0 : case GUC_SET:
4894 0 : (void) set_config_option_ext(name, curvalue,
4895 : curscontext, cursource, cursrole,
4896 : GUC_ACTION_SET, true,
4897 : WARNING, false);
4898 0 : break;
4899 :
4900 0 : case GUC_LOCAL:
4901 0 : (void) set_config_option_ext(name, curvalue,
4902 : curscontext, cursource, cursrole,
4903 : GUC_ACTION_LOCAL, true,
4904 : WARNING, false);
4905 0 : break;
4906 :
4907 0 : case GUC_SET_LOCAL:
4908 : /* first, apply the masked value as SET */
4909 0 : (void) set_config_option_ext(name, stack->masked.val.stringval,
4910 : stack->masked_scontext,
4911 : PGC_S_SESSION,
4912 : stack->masked_srole,
4913 : GUC_ACTION_SET, true,
4914 : WARNING, false);
4915 : /* then apply the current value as LOCAL */
4916 0 : (void) set_config_option_ext(name, curvalue,
4917 : curscontext, cursource, cursrole,
4918 : GUC_ACTION_LOCAL, true,
4919 : WARNING, false);
4920 0 : break;
4921 : }
4922 :
4923 : /* If we successfully made a stack entry, adjust its nest level */
4924 0 : if (variable->stack != oldvarstack)
4925 0 : variable->stack->nest_level = stack->nest_level;
4926 : }
4927 : else
4928 : {
4929 : /*
4930 : * We are at the end of the stack. If the active/previous value is
4931 : * different from the reset value, it must represent a previously
4932 : * committed session value. Apply it, and then drop the stack entry
4933 : * that set_config_option will have created under the impression that
4934 : * this is to be just a transactional assignment. (We leak the stack
4935 : * entry.)
4936 : */
208 tgl 4937 GIC 51 : if (curvalue != pHolder->reset_val ||
4938 47 : curscontext != pHolder->gen.reset_scontext ||
4939 47 : cursource != pHolder->gen.reset_source ||
4940 47 : cursrole != pHolder->gen.reset_srole)
4941 : {
4942 4 : (void) set_config_option_ext(name, curvalue,
4943 : curscontext, cursource, cursrole,
4944 : GUC_ACTION_SET, true, WARNING, false);
177 tgl 4945 GNC 4 : if (variable->stack != NULL)
4946 : {
4947 2 : slist_delete(&guc_stack_list, &variable->stack_link);
4948 2 : variable->stack = NULL;
4949 : }
4950 : }
4951 : }
3058 rhaas 4952 GIC 51 : }
4953 :
4954 : /*
4955 : * Functions for extensions to call to define their custom GUC variables.
4956 : */
4957 : void
208 tgl 4958 3677 : DefineCustomBoolVariable(const char *name,
4959 : const char *short_desc,
4960 : const char *long_desc,
4961 : bool *valueAddr,
4962 : bool bootValue,
4963 : GucContext context,
4964 : int flags,
4965 : GucBoolCheckHook check_hook,
4966 : GucBoolAssignHook assign_hook,
4967 : GucShowHook show_hook)
4968 : {
4969 : struct config_bool *var;
4970 :
4971 : var = (struct config_bool *)
4972 3677 : init_custom_variable(name, short_desc, long_desc, context, flags,
4973 : PGC_BOOL, sizeof(struct config_bool));
4974 3677 : var->variable = valueAddr;
4975 3677 : var->boot_val = bootValue;
4976 3677 : var->reset_val = bootValue;
4977 3677 : var->check_hook = check_hook;
4978 3677 : var->assign_hook = assign_hook;
4979 3677 : var->show_hook = show_hook;
4980 3677 : define_custom_variable(&var->gen);
3058 rhaas 4981 3677 : }
4982 :
4983 : void
208 tgl 4984 36 : DefineCustomIntVariable(const char *name,
4985 : const char *short_desc,
4986 : const char *long_desc,
4987 : int *valueAddr,
4988 : int bootValue,
4989 : int minValue,
4990 : int maxValue,
4991 : GucContext context,
4992 : int flags,
4993 : GucIntCheckHook check_hook,
4994 : GucIntAssignHook assign_hook,
4995 : GucShowHook show_hook)
4996 : {
4997 : struct config_int *var;
4998 :
4999 : var = (struct config_int *)
5000 36 : init_custom_variable(name, short_desc, long_desc, context, flags,
5001 : PGC_INT, sizeof(struct config_int));
5002 36 : var->variable = valueAddr;
5003 36 : var->boot_val = bootValue;
5004 36 : var->reset_val = bootValue;
5005 36 : var->min = minValue;
5006 36 : var->max = maxValue;
5007 36 : var->check_hook = check_hook;
5008 36 : var->assign_hook = assign_hook;
5009 36 : var->show_hook = show_hook;
5010 36 : define_custom_variable(&var->gen);
5011 36 : }
5012 :
5013 : void
5014 21 : DefineCustomRealVariable(const char *name,
5015 : const char *short_desc,
5016 : const char *long_desc,
5017 : double *valueAddr,
5018 : double bootValue,
5019 : double minValue,
5020 : double maxValue,
5021 : GucContext context,
5022 : int flags,
5023 : GucRealCheckHook check_hook,
5024 : GucRealAssignHook assign_hook,
5025 : GucShowHook show_hook)
5026 : {
5027 : struct config_real *var;
5028 :
5029 : var = (struct config_real *)
5030 21 : init_custom_variable(name, short_desc, long_desc, context, flags,
5031 : PGC_REAL, sizeof(struct config_real));
5032 21 : var->variable = valueAddr;
5033 21 : var->boot_val = bootValue;
5034 21 : var->reset_val = bootValue;
5035 21 : var->min = minValue;
5036 21 : var->max = maxValue;
5037 21 : var->check_hook = check_hook;
5038 21 : var->assign_hook = assign_hook;
5039 21 : var->show_hook = show_hook;
5040 21 : define_custom_variable(&var->gen);
5041 21 : }
5042 :
5043 : void
5044 3643 : DefineCustomStringVariable(const char *name,
5045 : const char *short_desc,
5046 : const char *long_desc,
5047 : char **valueAddr,
5048 : const char *bootValue,
5049 : GucContext context,
5050 : int flags,
5051 : GucStringCheckHook check_hook,
5052 : GucStringAssignHook assign_hook,
5053 : GucShowHook show_hook)
5054 : {
5055 : struct config_string *var;
5056 :
5057 : var = (struct config_string *)
5058 3643 : init_custom_variable(name, short_desc, long_desc, context, flags,
5059 : PGC_STRING, sizeof(struct config_string));
5060 3643 : var->variable = valueAddr;
5061 3643 : var->boot_val = bootValue;
5062 3643 : var->check_hook = check_hook;
5063 3643 : var->assign_hook = assign_hook;
5064 3643 : var->show_hook = show_hook;
5065 3643 : define_custom_variable(&var->gen);
3058 rhaas 5066 3643 : }
5067 :
5068 : void
208 tgl 5069 1796 : DefineCustomEnumVariable(const char *name,
5070 : const char *short_desc,
5071 : const char *long_desc,
5072 : int *valueAddr,
5073 : int bootValue,
5074 : const struct config_enum_entry *options,
5075 : GucContext context,
5076 : int flags,
5077 : GucEnumCheckHook check_hook,
5078 : GucEnumAssignHook assign_hook,
5079 : GucShowHook show_hook)
5080 : {
5081 : struct config_enum *var;
5082 :
5083 : var = (struct config_enum *)
5084 1796 : init_custom_variable(name, short_desc, long_desc, context, flags,
5085 : PGC_ENUM, sizeof(struct config_enum));
5086 1796 : var->variable = valueAddr;
5087 1796 : var->boot_val = bootValue;
5088 1796 : var->reset_val = bootValue;
5089 1796 : var->options = options;
5090 1796 : var->check_hook = check_hook;
5091 1796 : var->assign_hook = assign_hook;
5092 1796 : var->show_hook = show_hook;
5093 1796 : define_custom_variable(&var->gen);
3058 rhaas 5094 1796 : }
5095 :
5096 : /*
5097 : * Mark the given GUC prefix as "reserved".
5098 : *
5099 : * This deletes any existing placeholders matching the prefix,
5100 : * and then prevents new ones from being created.
5101 : * Extensions should call this after they've defined all of their custom
5102 : * GUCs, to help catch misspelled config-file entries.
5103 : */
5104 : void
208 tgl 5105 1842 : MarkGUCPrefixReserved(const char *className)
5106 : {
5107 1842 : int classLen = strlen(className);
5108 : HASH_SEQ_STATUS status;
5109 : GUCHashEntry *hentry;
5110 : MemoryContext oldcontext;
5111 :
5112 : /*
5113 : * Check for existing placeholders. We must actually remove invalid
5114 : * placeholders, else future parallel worker startups will fail. (We
5115 : * don't bother trying to free associated memory, since this shouldn't
5116 : * happen often.)
5117 : */
177 tgl 5118 GNC 1842 : hash_seq_init(&status, guc_hashtab);
5119 691115 : while ((hentry = (GUCHashEntry *) hash_seq_search(&status)) != NULL)
5120 : {
5121 689273 : struct config_generic *var = hentry->gucvar;
5122 :
208 tgl 5123 GIC 689273 : if ((var->flags & GUC_CUSTOM_PLACEHOLDER) != 0 &&
5124 12 : strncmp(className, var->name, classLen) == 0 &&
5125 3 : var->name[classLen] == GUC_QUALIFIER_SEPARATOR)
5126 : {
5127 3 : ereport(WARNING,
5128 : (errcode(ERRCODE_INVALID_NAME),
5129 : errmsg("invalid configuration parameter name \"%s\", removing it",
5130 : var->name),
5131 : errdetail("\"%s\" is now a reserved prefix.",
5132 : className)));
5133 : /* Remove it from the hash table */
177 tgl 5134 GNC 3 : hash_search(guc_hashtab,
5135 3 : &var->name,
5136 : HASH_REMOVE,
5137 : NULL);
5138 : /* Remove it from any lists it's in, too */
5139 3 : RemoveGUCFromLists(var);
5140 : }
5141 : }
5142 :
5143 : /* And remember the name so we can prevent future mistakes. */
5144 1842 : oldcontext = MemoryContextSwitchTo(GUCMemoryContext);
208 tgl 5145 GIC 1842 : reserved_class_prefix = lappend(reserved_class_prefix, pstrdup(className));
5146 1842 : MemoryContextSwitchTo(oldcontext);
1270 tmunro 5147 1842 : }
5148 :
5149 :
5150 : /*
5151 : * Return an array of modified GUC options to show in EXPLAIN.
5152 : *
5153 : * We only report options related to query planning (marked with GUC_EXPLAIN),
5154 : * with values different from their built-in defaults.
5155 : */
5156 : struct config_generic **
208 tgl 5157 6 : get_explain_guc_options(int *num)
5158 : {
5159 : struct config_generic **result;
5160 : dlist_iter iter;
5161 :
5162 6 : *num = 0;
5163 :
5164 : /*
5165 : * While only a fraction of all the GUC variables are marked GUC_EXPLAIN,
5166 : * it doesn't seem worth dynamically resizing this array.
5167 : */
177 tgl 5168 GNC 6 : result = palloc(sizeof(struct config_generic *) * hash_get_num_entries(guc_hashtab));
5169 :
5170 : /* We need only consider GUCs with source not PGC_S_DEFAULT */
5171 326 : dlist_foreach(iter, &guc_nondef_list)
5172 : {
5173 320 : struct config_generic *conf = dlist_container(struct config_generic,
5174 : nondef_link, iter.cur);
5175 : bool modified;
5176 :
5177 : /* return only parameters marked for inclusion in explain */
208 tgl 5178 GIC 320 : if (!(conf->flags & GUC_EXPLAIN))
751 5179 308 : continue;
5180 :
5181 : /* return only options visible to the current user */
72 tgl 5182 GNC 12 : if (!ConfigOptionIsVisible(conf))
208 tgl 5183 UIC 0 : continue;
5184 :
5185 : /* return only options that are different from their boot values */
208 tgl 5186 GIC 12 : modified = false;
5187 :
5188 12 : switch (conf->vartype)
5189 : {
751 5190 6 : case PGC_BOOL:
5191 : {
208 5192 6 : struct config_bool *lconf = (struct config_bool *) conf;
5193 :
5194 6 : modified = (lconf->boot_val != *(lconf->variable));
5195 : }
5196 6 : break;
5197 :
751 tgl 5198 UIC 0 : case PGC_INT:
5199 : {
208 5200 0 : struct config_int *lconf = (struct config_int *) conf;
5201 :
5202 0 : modified = (lconf->boot_val != *(lconf->variable));
5203 : }
5204 0 : break;
5205 :
751 5206 0 : case PGC_REAL:
5207 : {
208 5208 0 : struct config_real *lconf = (struct config_real *) conf;
5209 :
5210 0 : modified = (lconf->boot_val != *(lconf->variable));
5211 : }
5212 0 : break;
5213 :
751 5214 0 : case PGC_STRING:
5215 : {
208 5216 0 : struct config_string *lconf = (struct config_string *) conf;
5217 :
5218 0 : modified = (strcmp(lconf->boot_val, *(lconf->variable)) != 0);
5219 : }
5220 0 : break;
5221 :
751 tgl 5222 GIC 6 : case PGC_ENUM:
5223 : {
208 5224 6 : struct config_enum *lconf = (struct config_enum *) conf;
5225 :
5226 6 : modified = (lconf->boot_val != *(lconf->variable));
5227 : }
5228 6 : break;
5229 :
208 tgl 5230 UIC 0 : default:
5231 0 : elog(ERROR, "unexpected GUC type: %d", conf->vartype);
5232 : }
5233 :
208 tgl 5234 GIC 12 : if (!modified)
208 tgl 5235 UIC 0 : continue;
5236 :
5237 : /* OK, report it */
208 tgl 5238 GIC 12 : result[*num] = conf;
5239 12 : *num = *num + 1;
5240 : }
5241 :
5242 6 : return result;
5243 : }
5244 :
5245 : /*
5246 : * Return GUC variable value by name; optionally return canonical form of
5247 : * name. If the GUC is unset, then throw an error unless missing_ok is true,
5248 : * in which case return NULL. Return value is palloc'd (but *varname isn't).
5249 : */
5250 : char *
5251 3746 : GetConfigOptionByName(const char *name, const char **varname, bool missing_ok)
5252 : {
5253 : struct config_generic *record;
5254 :
5255 3746 : record = find_option(name, false, missing_ok, ERROR);
5256 3728 : if (record == NULL)
5257 : {
5258 3 : if (varname)
208 tgl 5259 UIC 0 : *varname = NULL;
208 tgl 5260 GIC 3 : return NULL;
5261 : }
5262 :
72 tgl 5263 GNC 3725 : if (!ConfigOptionIsVisible(record))
208 tgl 5264 GIC 1 : ereport(ERROR,
5265 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
5266 : errmsg("permission denied to examine \"%s\"", name),
5267 : errdetail("Only roles with privileges of the \"%s\" role may examine this parameter.",
5268 : "pg_read_all_settings")));
5269 :
5270 3724 : if (varname)
5271 1183 : *varname = record->name;
5272 :
208 tgl 5273 GNC 3724 : return ShowGUCOption(record, true);
5274 : }
5275 :
5276 : /*
5277 : * ShowGUCOption: get string value of variable
5278 : *
5279 : * We express a numeric value in appropriate units if it has units and
5280 : * use_units is true; else you just get the raw number.
5281 : * The result string is palloc'd.
5282 : */
5283 : char *
5284 592770 : ShowGUCOption(struct config_generic *record, bool use_units)
5285 : {
5286 : char buffer[256];
5287 : const char *val;
5288 :
208 tgl 5289 GIC 592770 : switch (record->vartype)
5290 : {
5291 168040 : case PGC_BOOL:
5292 : {
5293 168040 : struct config_bool *conf = (struct config_bool *) record;
5294 :
5295 168040 : if (conf->show_hook)
5296 10118 : val = conf->show_hook();
5297 : else
5298 157922 : val = *conf->variable ? "on" : "off";
5299 : }
5300 168040 : break;
5301 :
5302 149624 : case PGC_INT:
5303 : {
5304 149624 : struct config_int *conf = (struct config_int *) record;
5305 :
5306 149624 : if (conf->show_hook)
5307 7910 : val = conf->show_hook();
5308 : else
5309 : {
5310 : /*
5311 : * Use int64 arithmetic to avoid overflows in units
5312 : * conversion.
5313 : */
5314 141714 : int64 result = *conf->variable;
5315 : const char *unit;
5316 :
5317 141714 : if (use_units && result > 0 && (record->flags & GUC_UNIT))
5318 298 : convert_int_from_base_unit(result,
5319 298 : record->flags & GUC_UNIT,
5320 : &result, &unit);
5321 : else
5322 141416 : unit = "";
5323 :
5324 141714 : snprintf(buffer, sizeof(buffer), INT64_FORMAT "%s",
5325 : result, unit);
5326 141714 : val = buffer;
5327 : }
5328 : }
5329 149624 : break;
5330 :
5331 26250 : case PGC_REAL:
5332 : {
5333 26250 : struct config_real *conf = (struct config_real *) record;
5334 :
5335 26250 : if (conf->show_hook)
208 tgl 5336 UIC 0 : val = conf->show_hook();
5337 : else
5338 : {
208 tgl 5339 GIC 26250 : double result = *conf->variable;
5340 : const char *unit;
5341 :
5342 26250 : if (use_units && result > 0 && (record->flags & GUC_UNIT))
5343 134 : convert_real_from_base_unit(result,
5344 134 : record->flags & GUC_UNIT,
5345 : &result, &unit);
5346 : else
5347 26116 : unit = "";
5348 :
5349 26250 : snprintf(buffer, sizeof(buffer), "%g%s",
5350 : result, unit);
5351 26250 : val = buffer;
5352 : }
5353 : }
5354 26250 : break;
5355 :
5356 194564 : case PGC_STRING:
5357 : {
5358 194564 : struct config_string *conf = (struct config_string *) record;
5359 :
5360 194564 : if (conf->show_hook)
5361 20735 : val = conf->show_hook();
5362 173829 : else if (*conf->variable && **conf->variable)
5363 138795 : val = *conf->variable;
5364 : else
5365 35034 : val = "";
5366 : }
5367 194564 : break;
5368 :
5369 54292 : case PGC_ENUM:
5370 : {
5371 54292 : struct config_enum *conf = (struct config_enum *) record;
5372 :
5373 54292 : if (conf->show_hook)
208 tgl 5374 UIC 0 : val = conf->show_hook();
5375 : else
208 tgl 5376 GIC 54292 : val = config_enum_lookup_by_value(conf, *conf->variable);
5377 : }
5378 54292 : break;
5379 :
208 tgl 5380 UIC 0 : default:
5381 : /* just to keep compiler quiet */
5382 0 : val = "???";
5383 0 : break;
5384 : }
5385 :
208 tgl 5386 GIC 592770 : return pstrdup(val);
5387 : }
5388 :
5389 :
5390 : #ifdef EXEC_BACKEND
5391 :
5392 : /*
5393 : * These routines dump out all non-default GUC options into a binary
5394 : * file that is read by all exec'ed backends. The format is:
5395 : *
5396 : * variable name, string, null terminated
5397 : * variable value, string, null terminated
5398 : * variable sourcefile, string, null terminated (empty if none)
5399 : * variable sourceline, integer
5400 : * variable source, integer
5401 : * variable scontext, integer
5402 : * variable srole, OID
5403 : */
5404 : static void
5405 : write_one_nondefault_variable(FILE *fp, struct config_generic *gconf)
5406 : {
5407 : Assert(gconf->source != PGC_S_DEFAULT);
5408 :
5409 : fprintf(fp, "%s", gconf->name);
5410 : fputc(0, fp);
5411 :
5412 : switch (gconf->vartype)
5413 : {
5414 : case PGC_BOOL:
5415 : {
5416 : struct config_bool *conf = (struct config_bool *) gconf;
5417 :
5418 : if (*conf->variable)
5419 : fprintf(fp, "true");
5420 : else
5421 : fprintf(fp, "false");
5422 : }
5423 : break;
5424 :
5425 : case PGC_INT:
5426 : {
5427 : struct config_int *conf = (struct config_int *) gconf;
5428 :
5429 : fprintf(fp, "%d", *conf->variable);
5430 : }
5431 : break;
5432 :
5433 : case PGC_REAL:
5434 : {
5435 : struct config_real *conf = (struct config_real *) gconf;
5436 :
5437 : fprintf(fp, "%.17g", *conf->variable);
5438 : }
5439 : break;
5440 :
5441 : case PGC_STRING:
5442 : {
5443 : struct config_string *conf = (struct config_string *) gconf;
5444 :
5445 : fprintf(fp, "%s", *conf->variable);
5446 : }
5447 : break;
5448 :
5449 : case PGC_ENUM:
5450 : {
5451 : struct config_enum *conf = (struct config_enum *) gconf;
5452 :
5453 : fprintf(fp, "%s",
5454 : config_enum_lookup_by_value(conf, *conf->variable));
5455 : }
5456 : break;
5457 : }
5458 :
5459 : fputc(0, fp);
5460 :
5461 : if (gconf->sourcefile)
5462 : fprintf(fp, "%s", gconf->sourcefile);
5463 : fputc(0, fp);
5464 :
5465 : fwrite(&gconf->sourceline, 1, sizeof(gconf->sourceline), fp);
5466 : fwrite(&gconf->source, 1, sizeof(gconf->source), fp);
5467 : fwrite(&gconf->scontext, 1, sizeof(gconf->scontext), fp);
5468 : fwrite(&gconf->srole, 1, sizeof(gconf->srole), fp);
5469 : }
5470 :
5471 : void
5472 : write_nondefault_variables(GucContext context)
5473 : {
5474 : int elevel;
5475 : FILE *fp;
5476 : dlist_iter iter;
5477 :
5478 : Assert(context == PGC_POSTMASTER || context == PGC_SIGHUP);
5479 :
5480 : elevel = (context == PGC_SIGHUP) ? LOG : ERROR;
5481 :
5482 : /*
5483 : * Open file
5484 : */
5485 : fp = AllocateFile(CONFIG_EXEC_PARAMS_NEW, "w");
5486 : if (!fp)
5487 : {
5488 : ereport(elevel,
5489 : (errcode_for_file_access(),
5490 : errmsg("could not write to file \"%s\": %m",
5491 : CONFIG_EXEC_PARAMS_NEW)));
5492 : return;
5493 : }
5494 :
5495 : /* We need only consider GUCs with source not PGC_S_DEFAULT */
5496 : dlist_foreach(iter, &guc_nondef_list)
5497 : {
5498 : struct config_generic *gconf = dlist_container(struct config_generic,
5499 : nondef_link, iter.cur);
5500 :
5501 : write_one_nondefault_variable(fp, gconf);
5502 : }
5503 :
5504 : if (FreeFile(fp))
5505 : {
5506 : ereport(elevel,
5507 : (errcode_for_file_access(),
5508 : errmsg("could not write to file \"%s\": %m",
5509 : CONFIG_EXEC_PARAMS_NEW)));
5510 : return;
5511 : }
5512 :
5513 : /*
5514 : * Put new file in place. This could delay on Win32, but we don't hold
5515 : * any exclusive locks.
5516 : */
5517 : rename(CONFIG_EXEC_PARAMS_NEW, CONFIG_EXEC_PARAMS);
5518 : }
5519 :
5520 :
5521 : /*
5522 : * Read string, including null byte from file
5523 : *
5524 : * Return NULL on EOF and nothing read
5525 : */
5526 : static char *
5527 : read_string_with_null(FILE *fp)
5528 : {
5529 : int i = 0,
5530 : ch,
5531 : maxlen = 256;
5532 : char *str = NULL;
5533 :
5534 : do
5535 : {
5536 : if ((ch = fgetc(fp)) == EOF)
5537 : {
5538 : if (i == 0)
5539 : return NULL;
5540 : else
5541 : elog(FATAL, "invalid format of exec config params file");
5542 : }
5543 : if (i == 0)
5544 : str = guc_malloc(FATAL, maxlen);
5545 : else if (i == maxlen)
5546 : str = guc_realloc(FATAL, str, maxlen *= 2);
5547 : str[i++] = ch;
5548 : } while (ch != 0);
5549 :
5550 : return str;
5551 : }
5552 :
5553 :
5554 : /*
5555 : * This routine loads a previous postmaster dump of its non-default
5556 : * settings.
5557 : */
5558 : void
5559 : read_nondefault_variables(void)
5560 : {
5561 : FILE *fp;
5562 : char *varname,
5563 : *varvalue,
5564 : *varsourcefile;
5565 : int varsourceline;
5566 : GucSource varsource;
5567 : GucContext varscontext;
5568 : Oid varsrole;
5569 :
5570 : /*
5571 : * Open file
5572 : */
5573 : fp = AllocateFile(CONFIG_EXEC_PARAMS, "r");
5574 : if (!fp)
5575 : {
5576 : /* File not found is fine */
5577 : if (errno != ENOENT)
5578 : ereport(FATAL,
5579 : (errcode_for_file_access(),
5580 : errmsg("could not read from file \"%s\": %m",
5581 : CONFIG_EXEC_PARAMS)));
5582 : return;
5583 : }
5584 :
5585 : for (;;)
5586 : {
5587 : if ((varname = read_string_with_null(fp)) == NULL)
5588 : break;
5589 :
5590 : if (find_option(varname, true, false, FATAL) == NULL)
5591 : elog(FATAL, "failed to locate variable \"%s\" in exec config params file", varname);
5592 :
5593 : if ((varvalue = read_string_with_null(fp)) == NULL)
5594 : elog(FATAL, "invalid format of exec config params file");
5595 : if ((varsourcefile = read_string_with_null(fp)) == NULL)
5596 : elog(FATAL, "invalid format of exec config params file");
5597 : if (fread(&varsourceline, 1, sizeof(varsourceline), fp) != sizeof(varsourceline))
5598 : elog(FATAL, "invalid format of exec config params file");
5599 : if (fread(&varsource, 1, sizeof(varsource), fp) != sizeof(varsource))
5600 : elog(FATAL, "invalid format of exec config params file");
5601 : if (fread(&varscontext, 1, sizeof(varscontext), fp) != sizeof(varscontext))
5602 : elog(FATAL, "invalid format of exec config params file");
5603 : if (fread(&varsrole, 1, sizeof(varsrole), fp) != sizeof(varsrole))
5604 : elog(FATAL, "invalid format of exec config params file");
5605 :
5606 : (void) set_config_option_ext(varname, varvalue,
5607 : varscontext, varsource, varsrole,
5608 : GUC_ACTION_SET, true, 0, true);
5609 : if (varsourcefile[0])
5610 : set_config_sourcefile(varname, varsourcefile, varsourceline);
5611 :
5612 : guc_free(varname);
5613 : guc_free(varvalue);
5614 : guc_free(varsourcefile);
5615 : }
5616 :
5617 : FreeFile(fp);
5618 : }
5619 : #endif /* EXEC_BACKEND */
5620 :
5621 : /*
5622 : * can_skip_gucvar:
5623 : * Decide whether SerializeGUCState can skip sending this GUC variable,
5624 : * or whether RestoreGUCState can skip resetting this GUC to default.
5625 : *
5626 : * It is somewhat magical and fragile that the same test works for both cases.
5627 : * Realize in particular that we are very likely selecting different sets of
5628 : * GUCs on the leader and worker sides! Be sure you've understood the
5629 : * comments here and in RestoreGUCState thoroughly before changing this.
5630 : */
5631 : static bool
5632 107787 : can_skip_gucvar(struct config_generic *gconf)
5633 : {
5634 : /*
5635 : * We can skip GUCs that are guaranteed to have the same values in leaders
5636 : * and workers. (Note it is critical that the leader and worker have the
5637 : * same idea of which GUCs fall into this category. It's okay to consider
5638 : * context and name for this purpose, since those are unchanging
5639 : * properties of a GUC.)
5640 : *
5641 : * PGC_POSTMASTER variables always have the same value in every child of a
5642 : * particular postmaster, so the worker will certainly have the right
5643 : * value already. Likewise, PGC_INTERNAL variables are set by special
5644 : * mechanisms (if indeed they aren't compile-time constants). So we may
5645 : * always skip these.
5646 : *
5647 : * Role must be handled specially because its current value can be an
5648 : * invalid value (for instance, if someone dropped the role since we set
5649 : * it). So if we tried to serialize it normally, we might get a failure.
5650 : * We skip it here, and use another mechanism to ensure the worker has the
5651 : * right value.
5652 : *
5653 : * For all other GUCs, we skip if the GUC has its compiled-in default
5654 : * value (i.e., source == PGC_S_DEFAULT). On the leader side, this means
5655 : * we don't send GUCs that have their default values, which typically
5656 : * saves lots of work. On the worker side, this means we don't need to
5657 : * reset the GUC to default because it already has that value. See
5658 : * comments in RestoreGUCState for more info.
5659 : */
5660 183476 : return gconf->context == PGC_POSTMASTER ||
5661 166644 : gconf->context == PGC_INTERNAL || gconf->source == PGC_S_DEFAULT ||
5662 58857 : strcmp(gconf->name, "role") == 0;
5663 : }
5664 :
5665 : /*
5666 : * estimate_variable_size:
5667 : * Compute space needed for dumping the given GUC variable.
5668 : *
5669 : * It's OK to overestimate, but not to underestimate.
5670 : */
5671 : static Size
5672 22578 : estimate_variable_size(struct config_generic *gconf)
5673 : {
5674 : Size size;
5675 22578 : Size valsize = 0;
5676 :
5677 : /* Skippable GUCs consume zero space. */
5678 22578 : if (can_skip_gucvar(gconf))
5679 9361 : return 0;
5680 :
5681 : /* Name, plus trailing zero byte. */
5682 13217 : size = strlen(gconf->name) + 1;
5683 :
5684 : /* Get the maximum display length of the GUC value. */
5685 13217 : switch (gconf->vartype)
5686 : {
5687 2834 : case PGC_BOOL:
5688 : {
5689 2834 : valsize = 5; /* max(strlen('true'), strlen('false')) */
5690 : }
5691 2834 : break;
5692 :
5693 2747 : case PGC_INT:
5694 : {
5695 2747 : struct config_int *conf = (struct config_int *) gconf;
5696 :
5697 : /*
5698 : * Instead of getting the exact display length, use max
5699 : * length. Also reduce the max length for typical ranges of
5700 : * small values. Maximum value is 2147483647, i.e. 10 chars.
5701 : * Include one byte for sign.
5702 : */
184 peter 5703 GNC 2747 : if (abs(*conf->variable) < 1000)
208 tgl 5704 GIC 2169 : valsize = 3 + 1;
5705 : else
5706 578 : valsize = 10 + 1;
5707 : }
5708 2747 : break;
5709 :
5710 727 : case PGC_REAL:
5711 : {
5712 : /*
5713 : * We are going to print it with %e with REALTYPE_PRECISION
5714 : * fractional digits. Account for sign, leading digit,
5715 : * decimal point, and exponent with up to 3 digits. E.g.
5716 : * -3.99329042340000021e+110
5717 : */
5718 727 : valsize = 1 + 1 + 1 + REALTYPE_PRECISION + 5;
5719 : }
5720 727 : break;
5721 :
5722 5234 : case PGC_STRING:
5723 : {
5724 5234 : struct config_string *conf = (struct config_string *) gconf;
5725 :
5726 : /*
5727 : * If the value is NULL, we transmit it as an empty string.
5728 : * Although this is not physically the same value, GUC
5729 : * generally treats a NULL the same as empty string.
5730 : */
5731 5234 : if (*conf->variable)
5732 5234 : valsize = strlen(*conf->variable);
5733 : else
208 tgl 5734 UIC 0 : valsize = 0;
5735 : }
208 tgl 5736 GIC 5234 : break;
5737 :
5738 1675 : case PGC_ENUM:
5739 : {
5740 1675 : struct config_enum *conf = (struct config_enum *) gconf;
5741 :
5742 1675 : valsize = strlen(config_enum_lookup_by_value(conf, *conf->variable));
5743 : }
5744 1675 : break;
5745 : }
5746 :
5747 : /* Allow space for terminating zero-byte for value */
5748 13217 : size = add_size(size, valsize + 1);
5749 :
5750 13217 : if (gconf->sourcefile)
5751 5906 : size = add_size(size, strlen(gconf->sourcefile));
5752 :
5753 : /* Allow space for terminating zero-byte for sourcefile */
5754 13217 : size = add_size(size, 1);
5755 :
5756 : /* Include line whenever file is nonempty. */
5757 13217 : if (gconf->sourcefile && gconf->sourcefile[0])
5758 5906 : size = add_size(size, sizeof(gconf->sourceline));
5759 :
5760 13217 : size = add_size(size, sizeof(gconf->source));
5761 13217 : size = add_size(size, sizeof(gconf->scontext));
5762 13217 : size = add_size(size, sizeof(gconf->srole));
5763 :
5764 13217 : return size;
5765 : }
5766 :
5767 : /*
5768 : * EstimateGUCStateSpace:
5769 : * Returns the size needed to store the GUC state for the current process
5770 : */
5771 : Size
5772 403 : EstimateGUCStateSpace(void)
5773 : {
5774 : Size size;
5775 : dlist_iter iter;
5776 :
5777 : /* Add space reqd for saving the data size of the guc state */
5778 403 : size = sizeof(Size);
5779 :
5780 : /*
5781 : * Add up the space needed for each GUC variable.
5782 : *
5783 : * We need only process non-default GUCs.
5784 : */
177 tgl 5785 GNC 22981 : dlist_foreach(iter, &guc_nondef_list)
5786 : {
5787 22578 : struct config_generic *gconf = dlist_container(struct config_generic,
5788 : nondef_link, iter.cur);
5789 :
5790 22578 : size = add_size(size, estimate_variable_size(gconf));
5791 : }
5792 :
208 tgl 5793 GIC 403 : return size;
5794 : }
5795 :
5796 : /*
5797 : * do_serialize:
5798 : * Copies the formatted string into the destination. Moves ahead the
5799 : * destination pointer, and decrements the maxbytes by that many bytes. If
5800 : * maxbytes is not sufficient to copy the string, error out.
5801 : */
5802 : static void
5803 39651 : do_serialize(char **destptr, Size *maxbytes, const char *fmt,...)
5804 : {
5805 : va_list vargs;
5806 : int n;
5807 :
5808 39651 : if (*maxbytes <= 0)
208 tgl 5809 UIC 0 : elog(ERROR, "not enough space to serialize GUC state");
5810 :
208 tgl 5811 GIC 39651 : va_start(vargs, fmt);
5812 39651 : n = vsnprintf(*destptr, *maxbytes, fmt, vargs);
5813 39651 : va_end(vargs);
5814 :
5815 39651 : if (n < 0)
5816 : {
5817 : /* Shouldn't happen. Better show errno description. */
208 tgl 5818 UIC 0 : elog(ERROR, "vsnprintf failed: %m with format string \"%s\"", fmt);
5819 : }
208 tgl 5820 GIC 39651 : if (n >= *maxbytes)
5821 : {
5822 : /* This shouldn't happen either, really. */
208 tgl 5823 UIC 0 : elog(ERROR, "not enough space to serialize GUC state");
5824 : }
5825 :
5826 : /* Shift the destptr ahead of the null terminator */
208 tgl 5827 GIC 39651 : *destptr += n + 1;
5828 39651 : *maxbytes -= n + 1;
4385 5829 39651 : }
5830 :
5831 : /* Binary copy version of do_serialize() */
5832 : static void
208 5833 45557 : do_serialize_binary(char **destptr, Size *maxbytes, void *val, Size valsize)
5834 : {
5835 45557 : if (valsize > *maxbytes)
208 tgl 5836 UIC 0 : elog(ERROR, "not enough space to serialize GUC state");
5837 :
208 tgl 5838 GIC 45557 : memcpy(*destptr, val, valsize);
5839 45557 : *destptr += valsize;
5840 45557 : *maxbytes -= valsize;
4385 5841 45557 : }
5842 :
5843 : /*
5844 : * serialize_variable:
5845 : * Dumps name, value and other information of a GUC variable into destptr.
5846 : */
5847 : static void
208 5848 22578 : serialize_variable(char **destptr, Size *maxbytes,
5849 : struct config_generic *gconf)
5850 : {
5851 : /* Ignore skippable GUCs. */
5852 22578 : if (can_skip_gucvar(gconf))
5853 9361 : return;
5854 :
5855 13217 : do_serialize(destptr, maxbytes, "%s", gconf->name);
5856 :
5857 13217 : switch (gconf->vartype)
5858 : {
5859 2834 : case PGC_BOOL:
5860 : {
5861 2834 : struct config_bool *conf = (struct config_bool *) gconf;
5862 :
5863 2834 : do_serialize(destptr, maxbytes,
5864 2834 : (*conf->variable ? "true" : "false"));
5865 : }
5866 2834 : break;
5867 :
5868 2747 : case PGC_INT:
5869 : {
5870 2747 : struct config_int *conf = (struct config_int *) gconf;
5871 :
5872 2747 : do_serialize(destptr, maxbytes, "%d", *conf->variable);
5873 : }
5874 2747 : break;
5875 :
5876 727 : case PGC_REAL:
5877 : {
5878 727 : struct config_real *conf = (struct config_real *) gconf;
5879 :
5880 727 : do_serialize(destptr, maxbytes, "%.*e",
5881 727 : REALTYPE_PRECISION, *conf->variable);
5882 : }
5883 727 : break;
5884 :
5885 5234 : case PGC_STRING:
5886 : {
5887 5234 : struct config_string *conf = (struct config_string *) gconf;
5888 :
5889 : /* NULL becomes empty string, see estimate_variable_size() */
5890 5234 : do_serialize(destptr, maxbytes, "%s",
5891 5234 : *conf->variable ? *conf->variable : "");
5892 : }
5893 5234 : break;
5894 :
5895 1675 : case PGC_ENUM:
5896 : {
5897 1675 : struct config_enum *conf = (struct config_enum *) gconf;
5898 :
5899 1675 : do_serialize(destptr, maxbytes, "%s",
5900 1675 : config_enum_lookup_by_value(conf, *conf->variable));
5901 : }
5902 1675 : break;
5903 : }
5904 :
5905 13217 : do_serialize(destptr, maxbytes, "%s",
5906 13217 : (gconf->sourcefile ? gconf->sourcefile : ""));
5907 :
5908 13217 : if (gconf->sourcefile && gconf->sourcefile[0])
5909 5906 : do_serialize_binary(destptr, maxbytes, &gconf->sourceline,
5910 : sizeof(gconf->sourceline));
5911 :
5912 13217 : do_serialize_binary(destptr, maxbytes, &gconf->source,
5913 : sizeof(gconf->source));
5914 13217 : do_serialize_binary(destptr, maxbytes, &gconf->scontext,
5915 : sizeof(gconf->scontext));
5916 13217 : do_serialize_binary(destptr, maxbytes, &gconf->srole,
5917 : sizeof(gconf->srole));
5918 : }
5919 :
5920 : /*
5921 : * SerializeGUCState:
5922 : * Dumps the complete GUC state onto the memory location at start_address.
5923 : */
5924 : void
5925 403 : SerializeGUCState(Size maxsize, char *start_address)
5926 : {
5927 : char *curptr;
5928 : Size actual_size;
5929 : Size bytes_left;
5930 : dlist_iter iter;
5931 :
5932 : /* Reserve space for saving the actual size of the guc state */
5933 403 : Assert(maxsize > sizeof(actual_size));
5934 403 : curptr = start_address + sizeof(actual_size);
5935 403 : bytes_left = maxsize - sizeof(actual_size);
5936 :
5937 : /* We need only consider GUCs with source not PGC_S_DEFAULT */
177 tgl 5938 GNC 22981 : dlist_foreach(iter, &guc_nondef_list)
5939 : {
5940 22578 : struct config_generic *gconf = dlist_container(struct config_generic,
5941 : nondef_link, iter.cur);
5942 :
5943 22578 : serialize_variable(&curptr, &bytes_left, gconf);
5944 : }
5945 :
5946 : /* Store actual size without assuming alignment of start_address. */
208 tgl 5947 GIC 403 : actual_size = maxsize - bytes_left - sizeof(actual_size);
5948 403 : memcpy(start_address, &actual_size, sizeof(actual_size));
2251 rhaas 5949 403 : }
5950 :
5951 : /*
5952 : * read_gucstate:
5953 : * Actually it does not read anything, just returns the srcptr. But it does
5954 : * move the srcptr past the terminating zero byte, so that the caller is ready
5955 : * to read the next string.
5956 : */
5957 : static char *
208 tgl 5958 132621 : read_gucstate(char **srcptr, char *srcend)
5959 : {
5960 132621 : char *retptr = *srcptr;
5961 : char *ptr;
5962 :
5963 132621 : if (*srcptr >= srcend)
208 tgl 5964 UIC 0 : elog(ERROR, "incomplete GUC state");
5965 :
5966 : /* The string variables are all null terminated */
208 tgl 5967 GIC 2982721 : for (ptr = *srcptr; ptr < srcend && *ptr != '\0'; ptr++)
5968 : ;
5969 :
5970 132621 : if (ptr >= srcend)
208 tgl 5971 UIC 0 : elog(ERROR, "could not find null terminator in GUC state");
5972 :
5973 : /* Set the new position to the byte following the terminating NUL */
208 tgl 5974 GIC 132621 : *srcptr = ptr + 1;
5975 :
5976 132621 : return retptr;
5977 : }
5978 :
5979 : /* Binary read version of read_gucstate(). Copies into dest */
5980 : static void
5981 151655 : read_gucstate_binary(char **srcptr, char *srcend, void *dest, Size size)
5982 : {
5983 151655 : if (*srcptr + size > srcend)
208 tgl 5984 UIC 0 : elog(ERROR, "incomplete GUC state");
5985 :
208 tgl 5986 GIC 151655 : memcpy(dest, *srcptr, size);
5987 151655 : *srcptr += size;
7289 5988 151655 : }
5989 :
5990 : /*
5991 : * Callback used to add a context message when reporting errors that occur
5992 : * while trying to restore GUCs in parallel workers.
5993 : */
5994 : static void
208 tgl 5995 UIC 0 : guc_restore_error_context_callback(void *arg)
5996 : {
5997 0 : char **error_context_name_and_value = (char **) arg;
5998 :
5999 0 : if (error_context_name_and_value)
6000 0 : errcontext("while setting parameter \"%s\" to \"%s\"",
6001 : error_context_name_and_value[0],
6002 0 : error_context_name_and_value[1]);
5865 JanWieck 6003 0 : }
6004 :
6005 : /*
6006 : * RestoreGUCState:
6007 : * Reads the GUC state at the specified address and sets this process's
6008 : * GUCs to match.
6009 : *
6010 : * Note that this provides the worker with only a very shallow view of the
6011 : * leader's GUC state: we'll know about the currently active values, but not
6012 : * about stacked or reset values. That's fine since the worker is just
6013 : * executing one part of a query, within which the active values won't change
6014 : * and the stacked values are invisible.
6015 : */
6016 : void
208 tgl 6017 GIC 1298 : RestoreGUCState(void *gucstate)
6018 : {
6019 : char *varname,
6020 : *varvalue,
6021 : *varsourcefile;
6022 : int varsourceline;
6023 : GucSource varsource;
6024 : GucContext varscontext;
6025 : Oid varsrole;
6026 1298 : char *srcptr = (char *) gucstate;
6027 : char *srcend;
6028 : Size len;
6029 : dlist_mutable_iter iter;
6030 : ErrorContextCallback error_context_callback;
6031 :
6032 : /*
6033 : * First, ensure that all potentially-shippable GUCs are reset to their
6034 : * default values. We must not touch those GUCs that the leader will
6035 : * never ship, while there is no need to touch those that are shippable
6036 : * but already have their default values. Thus, this ends up being the
6037 : * same test that SerializeGUCState uses, even though the sets of
6038 : * variables involved may well be different since the leader's set of
6039 : * variables-not-at-default-values can differ from the set that are
6040 : * not-default in this freshly started worker.
6041 : *
6042 : * Once we have set all the potentially-shippable GUCs to default values,
6043 : * restoring the GUCs that the leader sent (because they had non-default
6044 : * values over there) leads us to exactly the set of GUC values that the
6045 : * leader has. This is true even though the worker may have initially
6046 : * absorbed postgresql.conf settings that the leader hasn't yet seen, or
6047 : * ALTER USER/DATABASE SET settings that were established after the leader
6048 : * started.
6049 : *
6050 : * Note that ensuring all the potential target GUCs are at PGC_S_DEFAULT
6051 : * also ensures that set_config_option won't refuse to set them because of
6052 : * source-priority comparisons.
6053 : */
177 tgl 6054 GNC 63929 : dlist_foreach_modify(iter, &guc_nondef_list)
6055 : {
6056 62631 : struct config_generic *gconf = dlist_container(struct config_generic,
6057 : nondef_link, iter.cur);
6058 :
6059 : /* Do nothing if non-shippable or if already at PGC_S_DEFAULT. */
208 tgl 6060 GIC 62631 : if (can_skip_gucvar(gconf))
6061 30214 : continue;
6062 :
6063 : /*
6064 : * We can use InitializeOneGUCOption to reset the GUC to default, but
6065 : * first we must free any existing subsidiary data to avoid leaking
6066 : * memory. The stack must be empty, but we have to clean up all other
6067 : * fields. Beware that there might be duplicate value or "extra"
6068 : * pointers. We also have to be sure to take it out of any lists it's
6069 : * in.
6070 : */
6071 32417 : Assert(gconf->stack == NULL);
177 tgl 6072 GNC 32417 : guc_free(gconf->extra);
6073 32417 : guc_free(gconf->last_reported);
6074 32417 : guc_free(gconf->sourcefile);
208 tgl 6075 GIC 32417 : switch (gconf->vartype)
6076 : {
6077 6920 : case PGC_BOOL:
6078 : {
6079 6920 : struct config_bool *conf = (struct config_bool *) gconf;
6080 :
6081 6920 : if (conf->reset_extra && conf->reset_extra != gconf->extra)
177 tgl 6082 UNC 0 : guc_free(conf->reset_extra);
208 tgl 6083 GIC 6920 : break;
6084 : }
6085 6059 : case PGC_INT:
6086 : {
6087 6059 : struct config_int *conf = (struct config_int *) gconf;
6088 :
6089 6059 : if (conf->reset_extra && conf->reset_extra != gconf->extra)
177 tgl 6090 UNC 0 : guc_free(conf->reset_extra);
208 tgl 6091 GIC 6059 : break;
6092 : }
208 tgl 6093 UIC 0 : case PGC_REAL:
6094 : {
6095 0 : struct config_real *conf = (struct config_real *) gconf;
6096 :
6097 0 : if (conf->reset_extra && conf->reset_extra != gconf->extra)
177 tgl 6098 UNC 0 : guc_free(conf->reset_extra);
208 tgl 6099 UIC 0 : break;
6100 : }
208 tgl 6101 GIC 15570 : case PGC_STRING:
6102 : {
6103 15570 : struct config_string *conf = (struct config_string *) gconf;
6104 :
177 tgl 6105 GNC 15570 : guc_free(*conf->variable);
208 tgl 6106 GIC 15570 : if (conf->reset_val && conf->reset_val != *conf->variable)
177 tgl 6107 UNC 0 : guc_free(conf->reset_val);
208 tgl 6108 GIC 15570 : if (conf->reset_extra && conf->reset_extra != gconf->extra)
177 tgl 6109 UNC 0 : guc_free(conf->reset_extra);
208 tgl 6110 GIC 15570 : break;
6111 : }
6112 3868 : case PGC_ENUM:
6113 : {
6114 3868 : struct config_enum *conf = (struct config_enum *) gconf;
6115 :
6116 3868 : if (conf->reset_extra && conf->reset_extra != gconf->extra)
177 tgl 6117 UNC 0 : guc_free(conf->reset_extra);
208 tgl 6118 GIC 3868 : break;
6119 : }
6120 : }
6121 : /* Remove it from any lists it's in. */
177 tgl 6122 GNC 32417 : RemoveGUCFromLists(gconf);
6123 : /* Now we can reset the struct to PGS_S_DEFAULT state. */
208 tgl 6124 GIC 32417 : InitializeOneGUCOption(gconf);
6125 : }
6126 :
6127 : /* First item is the length of the subsequent data */
6128 1298 : memcpy(&len, gucstate, sizeof(len));
6129 :
6130 1298 : srcptr += sizeof(len);
6131 1298 : srcend = srcptr + len;
6132 :
6133 : /* If the GUC value check fails, we want errors to show useful context. */
6134 1298 : error_context_callback.callback = guc_restore_error_context_callback;
6135 1298 : error_context_callback.previous = error_context_stack;
6136 1298 : error_context_callback.arg = NULL;
6137 1298 : error_context_stack = &error_context_callback;
6138 :
6139 : /* Restore all the listed GUCs. */
6140 45505 : while (srcptr < srcend)
6141 : {
6142 : int result;
6143 : char *error_context_name_and_value[2];
6144 :
6145 44207 : varname = read_gucstate(&srcptr, srcend);
6146 44207 : varvalue = read_gucstate(&srcptr, srcend);
6147 44207 : varsourcefile = read_gucstate(&srcptr, srcend);
6148 44207 : if (varsourcefile[0])
6149 19034 : read_gucstate_binary(&srcptr, srcend,
6150 : &varsourceline, sizeof(varsourceline));
6151 : else
6152 25173 : varsourceline = 0;
6153 44207 : read_gucstate_binary(&srcptr, srcend,
6154 : &varsource, sizeof(varsource));
6155 44207 : read_gucstate_binary(&srcptr, srcend,
6156 : &varscontext, sizeof(varscontext));
6157 44207 : read_gucstate_binary(&srcptr, srcend,
6158 : &varsrole, sizeof(varsrole));
6159 :
6160 44207 : error_context_name_and_value[0] = varname;
6161 44207 : error_context_name_and_value[1] = varvalue;
6162 44207 : error_context_callback.arg = &error_context_name_and_value[0];
6163 44207 : result = set_config_option_ext(varname, varvalue,
6164 : varscontext, varsource, varsrole,
6165 : GUC_ACTION_SET, true, ERROR, true);
6166 44207 : if (result <= 0)
208 tgl 6167 UIC 0 : ereport(ERROR,
6168 : (errcode(ERRCODE_INTERNAL_ERROR),
6169 : errmsg("parameter \"%s\" could not be set", varname)));
208 tgl 6170 GIC 44207 : if (varsourcefile[0])
6171 19034 : set_config_sourcefile(varname, varsourcefile, varsourceline);
6172 44207 : error_context_callback.arg = NULL;
6173 : }
6174 :
6175 1298 : error_context_stack = error_context_callback.previous;
6102 6176 1298 : }
6177 :
6178 : /*
6179 : * A little "long argument" simulation, although not quite GNU
6180 : * compliant. Takes a string of the form "some-option=some value" and
6181 : * returns name = "some_option" and value = "some value" in palloc'ed
6182 : * storage. Note that '-' is converted to '_' in the option name. If
6183 : * there is no '=' in the input string then value will be NULL.
6184 : */
6185 : void
208 6186 25936 : ParseLongOption(const char *string, char **name, char **value)
6187 : {
6188 : size_t equal_pos;
6189 : char *cp;
6190 :
163 peter 6191 GNC 25936 : Assert(string);
6192 25936 : Assert(name);
6193 25936 : Assert(value);
6194 :
208 tgl 6195 GIC 25936 : equal_pos = strcspn(string, "=");
6196 :
6197 25936 : if (string[equal_pos] == '=')
6198 : {
177 tgl 6199 GNC 25936 : *name = palloc(equal_pos + 1);
208 tgl 6200 GIC 25936 : strlcpy(*name, string, equal_pos + 1);
177 tgl 6201 GNC 25936 : *value = pstrdup(&string[equal_pos + 1]);
6202 : }
6203 : else
6204 : {
6205 : /* no equal sign in string */
177 tgl 6206 UNC 0 : *name = pstrdup(string);
208 tgl 6207 UIC 0 : *value = NULL;
6208 : }
6209 :
208 tgl 6210 GIC 362912 : for (cp = *name; *cp; cp++)
6211 336976 : if (*cp == '-')
6212 584 : *cp = '_';
6462 bruce 6213 25936 : }
6214 :
6215 :
6216 : /*
6217 : * Handle options fetched from pg_db_role_setting.setconfig,
6218 : * pg_proc.proconfig, etc. Caller must specify proper context/source/action.
6219 : *
6220 : * The array parameter must be an array of TEXT (it must not be NULL).
6221 : */
6222 : void
121 akorotkov 6223 GNC 3066 : ProcessGUCArray(ArrayType *array, ArrayType *usersetArray,
6224 : GucContext context, GucSource source, GucAction action)
6225 : {
6226 : int i;
6227 :
208 tgl 6228 GIC 3066 : Assert(array != NULL);
6229 3066 : Assert(ARR_ELEMTYPE(array) == TEXTOID);
6230 3066 : Assert(ARR_NDIM(array) == 1);
6231 3066 : Assert(ARR_LBOUND(array)[0] == 1);
6232 :
6233 21229 : for (i = 1; i <= ARR_DIMS(array)[0]; i++)
6234 : {
6235 : Datum d;
121 akorotkov 6236 GNC 18172 : Datum userSetDatum = BoolGetDatum(false);
6237 : bool isnull;
6238 : char *s;
6239 : char *name;
6240 : char *value;
6241 :
208 tgl 6242 GIC 18172 : d = array_ref(array, 1, &i,
6243 : -1 /* varlenarray */ ,
6244 : -1 /* TEXT's typlen */ ,
6245 : false /* TEXT's typbyval */ ,
6246 : TYPALIGN_INT /* TEXT's typalign */ ,
6247 : &isnull);
6248 :
6249 18172 : if (isnull)
208 tgl 6250 UIC 0 : continue;
6251 :
208 tgl 6252 GIC 18172 : s = TextDatumGetCString(d);
6253 :
6254 18172 : ParseLongOption(s, &name, &value);
6255 18172 : if (!value)
6256 : {
208 tgl 6257 UIC 0 : ereport(WARNING,
6258 : (errcode(ERRCODE_SYNTAX_ERROR),
6259 : errmsg("could not parse setting for parameter \"%s\"",
6260 : name)));
177 tgl 6261 UNC 0 : pfree(name);
208 tgl 6262 UIC 0 : continue;
6263 : }
6264 :
121 akorotkov 6265 GNC 18172 : if (usersetArray)
6266 18116 : userSetDatum = array_ref(usersetArray, 1, &i,
6267 : -1 /* varlenarray */ ,
6268 : sizeof(bool) /* BOOL's typlen */ ,
6269 : true /* BOOL's typbyval */ ,
6270 : TYPALIGN_CHAR /* BOOL's typalign */ ,
6271 : &isnull);
6272 18172 : if (isnull)
121 akorotkov 6273 UNC 0 : userSetDatum = BoolGetDatum(false);
6274 :
6275 : /*
6276 : * USER SET values are appliciable only for PGC_USERSET parameters. We
6277 : * use InvalidOid as role in order to evade possible privileges of the
6278 : * current user.
6279 : */
121 akorotkov 6280 GNC 18172 : if (!DatumGetBool(userSetDatum))
6281 18167 : (void) set_config_option(name, value,
6282 : context, source,
6283 : action, true, 0, false);
6284 : else
6285 5 : (void) set_config_option_ext(name, value,
6286 : PGC_USERSET, source, InvalidOid,
6287 : action, true, 0, false);
6288 :
177 tgl 6289 18163 : pfree(name);
6290 18163 : pfree(value);
208 tgl 6291 GIC 18163 : pfree(s);
6292 : }
1517 michael 6293 3057 : }
6294 :
6295 :
6296 : /*
6297 : * Add an entry to an option array. The array parameter may be NULL
6298 : * to indicate the current table entry is NULL.
6299 : */
6300 : ArrayType *
121 akorotkov 6301 GNC 591 : GUCArrayAdd(ArrayType *array, ArrayType **usersetArray,
6302 : const char *name, const char *value, bool user_set)
6303 : {
6304 : struct config_generic *record;
6305 : Datum datum;
6306 : char *newval;
6307 : ArrayType *a;
6308 :
208 tgl 6309 GIC 591 : Assert(name);
6310 591 : Assert(value);
6311 :
6312 : /* test if the option is valid and we're allowed to set it */
121 akorotkov 6313 GNC 591 : (void) validate_option_array_item(name, value, user_set, false);
6314 :
6315 : /* normalize name (converts obsolete GUC names to modern spellings) */
208 tgl 6316 GIC 588 : record = find_option(name, false, true, WARNING);
6317 588 : if (record)
6318 588 : name = record->name;
6319 :
6320 : /* build new item for array */
6321 588 : newval = psprintf("%s=%s", name, value);
6322 588 : datum = CStringGetTextDatum(newval);
6323 :
6324 588 : if (array)
6325 : {
6326 : int index;
6327 : bool isnull;
6328 : int i;
6329 :
6330 459 : Assert(ARR_ELEMTYPE(array) == TEXTOID);
6331 459 : Assert(ARR_NDIM(array) == 1);
6332 459 : Assert(ARR_LBOUND(array)[0] == 1);
6333 :
6334 459 : index = ARR_DIMS(array)[0] + 1; /* add after end */
6335 :
6336 1792 : for (i = 1; i <= ARR_DIMS(array)[0]; i++)
6337 : {
6338 : Datum d;
6339 : char *current;
6340 :
6341 1343 : d = array_ref(array, 1, &i,
6342 : -1 /* varlenarray */ ,
6343 : -1 /* TEXT's typlen */ ,
6344 : false /* TEXT's typbyval */ ,
6345 : TYPALIGN_INT /* TEXT's typalign */ ,
6346 : &isnull);
6347 1343 : if (isnull)
208 tgl 6348 UIC 0 : continue;
208 tgl 6349 GIC 1343 : current = TextDatumGetCString(d);
6350 :
6351 : /* check for match up through and including '=' */
6352 1343 : if (strncmp(current, newval, strlen(name) + 1) == 0)
6353 : {
121 akorotkov 6354 GNC 10 : bool currentUserSet = false;
6355 :
6356 10 : if (usersetArray)
6357 : {
6358 7 : currentUserSet = DatumGetBool(array_ref(*usersetArray, 1, &i,
6359 : -1 /* varlenarray */ ,
6360 : sizeof(bool) /* BOOL's typlen */ ,
6361 : true /* BOOL's typbyval */ ,
6362 : TYPALIGN_CHAR /* BOOL's typalign */ ,
6363 : &isnull));
6364 7 : if (isnull)
121 akorotkov 6365 UNC 0 : currentUserSet = false;
6366 : }
6367 :
6368 : /*
6369 : * Recheck permissons if we found an option without USER SET
6370 : * flag while we're setting an optionn with USER SET flag.
6371 : */
121 akorotkov 6372 GNC 10 : if (!currentUserSet && user_set)
6373 1 : (void) validate_option_array_item(name, value,
6374 : false, false);
208 tgl 6375 GIC 9 : index = i;
6376 9 : break;
6377 : }
6378 : }
6379 :
6380 458 : a = array_set(array, 1, &index,
6381 : datum,
6382 : false,
6383 : -1 /* varlena array */ ,
6384 : -1 /* TEXT's typlen */ ,
6385 : false /* TEXT's typbyval */ ,
6386 : TYPALIGN_INT /* TEXT's typalign */ );
6387 :
121 akorotkov 6388 GNC 458 : if (usersetArray)
6389 439 : *usersetArray = array_set(*usersetArray, 1, &index,
6390 : BoolGetDatum(user_set),
6391 : false,
6392 : -1 /* varlena array */ ,
6393 : sizeof(bool) /* BOOL's typlen */ ,
6394 : true /* BOOL's typbyval */ ,
6395 : TYPALIGN_CHAR /* BOOL's typalign */ );
6396 : }
6397 : else
6398 : {
208 tgl 6399 129 : a = construct_array_builtin(&datum, 1, TEXTOID);
121 akorotkov 6400 129 : if (usersetArray)
6401 : {
6402 101 : datum = BoolGetDatum(user_set);
6403 101 : *usersetArray = construct_array_builtin(&datum, 1, BOOLOID);
6404 : }
6405 : }
6406 :
208 tgl 6407 GIC 587 : return a;
6408 : }
6409 :
6410 :
6411 : /*
6412 : * Delete an entry from an option array. The array parameter may be NULL
6413 : * to indicate the current table entry is NULL. Also, if the return value
6414 : * is NULL then a null should be stored.
6415 : */
6416 : ArrayType *
121 akorotkov 6417 GNC 11 : GUCArrayDelete(ArrayType *array, ArrayType **usersetArray, const char *name)
6418 : {
6419 : struct config_generic *record;
6420 : ArrayType *newarray;
6421 : ArrayType *newUsersetArray;
6422 : int i;
6423 : int index;
6424 :
208 tgl 6425 GIC 11 : Assert(name);
6426 :
6427 : /* normalize name (converts obsolete GUC names to modern spellings) */
6428 11 : record = find_option(name, false, true, WARNING);
6429 11 : if (record)
6430 11 : name = record->name;
6431 :
6432 : /* if array is currently null, then surely nothing to delete */
6433 11 : if (!array)
208 tgl 6434 UIC 0 : return NULL;
6435 :
208 tgl 6436 GIC 11 : newarray = NULL;
121 akorotkov 6437 GNC 11 : newUsersetArray = NULL;
208 tgl 6438 GIC 11 : index = 1;
6439 :
6440 23 : for (i = 1; i <= ARR_DIMS(array)[0]; i++)
6441 : {
6442 : Datum d;
121 akorotkov 6443 GNC 12 : Datum userSetDatum = BoolGetDatum(false);
6444 : char *val;
6445 : bool isnull;
6446 :
208 tgl 6447 GIC 12 : d = array_ref(array, 1, &i,
6448 : -1 /* varlenarray */ ,
6449 : -1 /* TEXT's typlen */ ,
6450 : false /* TEXT's typbyval */ ,
6451 : TYPALIGN_INT /* TEXT's typalign */ ,
6452 : &isnull);
6453 12 : if (isnull)
6454 11 : continue;
6455 12 : val = TextDatumGetCString(d);
6456 :
121 akorotkov 6457 GNC 12 : if (usersetArray)
6458 12 : userSetDatum = array_ref(*usersetArray, 1, &i,
6459 : -1 /* varlenarray */ ,
6460 : sizeof(bool) /* BOOL's typlen */ ,
6461 : true /* BOOL's typbyval */ ,
6462 : TYPALIGN_CHAR /* BOOL's typalign */ ,
6463 : &isnull);
6464 12 : if (isnull)
121 akorotkov 6465 UNC 0 : userSetDatum = BoolGetDatum(false);
6466 :
6467 : /* ignore entry if it's what we want to delete */
208 tgl 6468 GIC 12 : if (strncmp(val, name, strlen(name)) == 0
6469 11 : && val[strlen(name)] == '=')
6470 : {
6471 : /* test if the option is valid and we're allowed to set it */
121 akorotkov 6472 GNC 11 : (void) validate_option_array_item(name, NULL,
6473 11 : DatumGetBool(userSetDatum), false);
208 tgl 6474 GIC 11 : continue;
6475 : }
6476 :
6477 : /* else add it to the output array */
6478 1 : if (newarray)
6479 : {
208 tgl 6480 UIC 0 : newarray = array_set(newarray, 1, &index,
6481 : d,
6482 : false,
6483 : -1 /* varlenarray */ ,
6484 : -1 /* TEXT's typlen */ ,
6485 : false /* TEXT's typbyval */ ,
6486 : TYPALIGN_INT /* TEXT's typalign */ );
121 akorotkov 6487 UNC 0 : if (usersetArray)
6488 0 : newUsersetArray = array_set(newUsersetArray, 1, &index,
6489 : userSetDatum,
6490 : false,
6491 : -1 /* varlena array */ ,
6492 : sizeof(bool) /* BOOL's typlen */ ,
6493 : true /* BOOL's typbyval */ ,
6494 : TYPALIGN_CHAR /* BOOL's typalign */ );
6495 : }
6496 : else
6497 : {
208 tgl 6498 GNC 1 : newarray = construct_array_builtin(&d, 1, TEXTOID);
121 akorotkov 6499 1 : if (usersetArray)
6500 1 : newUsersetArray = construct_array_builtin(&d, 1, BOOLOID);
6501 : }
6502 :
208 tgl 6503 GIC 1 : index++;
6504 : }
6505 :
121 akorotkov 6506 GNC 11 : if (usersetArray)
6507 11 : *usersetArray = newUsersetArray;
6508 :
208 tgl 6509 GIC 11 : return newarray;
6510 : }
6511 :
6512 :
6513 : /*
6514 : * Given a GUC array, delete all settings from it that our permission
6515 : * level allows: if superuser, delete them all; if regular user, only
6516 : * those that are PGC_USERSET or we have permission to set
6517 : */
6518 : ArrayType *
121 akorotkov 6519 GNC 1 : GUCArrayReset(ArrayType *array, ArrayType **usersetArray)
6520 : {
6521 : ArrayType *newarray;
6522 : ArrayType *newUsersetArray;
6523 : int i;
6524 : int index;
6525 :
6526 : /* if array is currently null, nothing to do */
208 tgl 6527 GIC 1 : if (!array)
208 tgl 6528 UIC 0 : return NULL;
6529 :
6530 : /* if we're superuser, we can delete everything, so just do it */
208 tgl 6531 GIC 1 : if (superuser())
208 tgl 6532 UIC 0 : return NULL;
6533 :
208 tgl 6534 GIC 1 : newarray = NULL;
121 akorotkov 6535 GNC 1 : newUsersetArray = NULL;
208 tgl 6536 GIC 1 : index = 1;
6537 :
6538 3 : for (i = 1; i <= ARR_DIMS(array)[0]; i++)
6539 : {
6540 : Datum d;
121 akorotkov 6541 GNC 2 : Datum userSetDatum = BoolGetDatum(false);
6542 : char *val;
6543 : char *eqsgn;
6544 : bool isnull;
6545 :
208 tgl 6546 GIC 2 : d = array_ref(array, 1, &i,
6547 : -1 /* varlenarray */ ,
6548 : -1 /* TEXT's typlen */ ,
6549 : false /* TEXT's typbyval */ ,
6550 : TYPALIGN_INT /* TEXT's typalign */ ,
6551 : &isnull);
6552 2 : if (isnull)
6553 1 : continue;
6554 2 : val = TextDatumGetCString(d);
6555 :
121 akorotkov 6556 GNC 2 : if (usersetArray)
6557 2 : userSetDatum = array_ref(*usersetArray, 1, &i,
6558 : -1 /* varlenarray */ ,
6559 : sizeof(bool) /* BOOL's typlen */ ,
6560 : true /* BOOL's typbyval */ ,
6561 : TYPALIGN_CHAR /* BOOL's typalign */ ,
6562 : &isnull);
6563 2 : if (isnull)
121 akorotkov 6564 UNC 0 : userSetDatum = BoolGetDatum(false);
6565 :
208 tgl 6566 GIC 2 : eqsgn = strchr(val, '=');
6567 2 : *eqsgn = '\0';
6568 :
6569 : /* skip if we have permission to delete it */
121 akorotkov 6570 GNC 2 : if (validate_option_array_item(val, NULL,
6571 2 : DatumGetBool(userSetDatum), true))
208 tgl 6572 GIC 1 : continue;
6573 :
6574 : /* else add it to the output array */
6575 1 : if (newarray)
6576 : {
208 tgl 6577 UIC 0 : newarray = array_set(newarray, 1, &index,
6578 : d,
6579 : false,
6580 : -1 /* varlenarray */ ,
6581 : -1 /* TEXT's typlen */ ,
6582 : false /* TEXT's typbyval */ ,
6583 : TYPALIGN_INT /* TEXT's typalign */ );
121 akorotkov 6584 UNC 0 : if (usersetArray)
6585 0 : newUsersetArray = array_set(newUsersetArray, 1, &index,
6586 : userSetDatum,
6587 : false,
6588 : -1 /* varlena array */ ,
6589 : sizeof(bool) /* BOOL's typlen */ ,
6590 : true /* BOOL's typbyval */ ,
6591 : TYPALIGN_CHAR /* BOOL's typalign */ );
6592 : }
6593 : else
6594 : {
208 tgl 6595 GNC 1 : newarray = construct_array_builtin(&d, 1, TEXTOID);
121 akorotkov 6596 1 : if (usersetArray)
6597 1 : newUsersetArray = construct_array_builtin(&userSetDatum, 1, BOOLOID);
6598 : }
6599 :
208 tgl 6600 GIC 1 : index++;
6601 1 : pfree(val);
6602 : }
6603 :
121 akorotkov 6604 GNC 1 : if (usersetArray)
6605 1 : *usersetArray = newUsersetArray;
6606 :
208 tgl 6607 GIC 1 : return newarray;
6608 : }
6609 :
6610 : /*
6611 : * Validate a proposed option setting for GUCArrayAdd/Delete/Reset.
6612 : *
6613 : * name is the option name. value is the proposed value for the Add case,
6614 : * or NULL for the Delete/Reset cases. user_set indicates this is the USER SET
6615 : * option. If skipIfNoPermissions is true, it's not an error to have no
6616 : * permissions to set the option.
6617 : *
6618 : * Returns true if OK, false if skipIfNoPermissions is true and user does not
6619 : * have permission to change this option (all other error cases result in an
6620 : * error being thrown).
6621 : */
6622 : static bool
121 akorotkov 6623 GNC 605 : validate_option_array_item(const char *name, const char *value, bool user_set,
6624 : bool skipIfNoPermissions)
6625 :
6626 : {
6627 : struct config_generic *gconf;
6628 :
6629 : /*
6630 : * There are three cases to consider:
6631 : *
6632 : * name is a known GUC variable. Check the value normally, check
6633 : * permissions normally (i.e., allow if variable is USERSET, or if it's
6634 : * SUSET and user is superuser or holds ACL_SET permissions).
6635 : *
6636 : * name is not known, but exists or can be created as a placeholder (i.e.,
6637 : * it has a valid custom name). We allow this case if you're a superuser,
6638 : * otherwise not. Superusers are assumed to know what they're doing. We
6639 : * can't allow it for other users, because when the placeholder is
6640 : * resolved it might turn out to be a SUSET variable. (With currently
6641 : * available infrastructure, we can actually handle such cases within the
6642 : * current session --- but once an entry is made in pg_db_role_setting,
6643 : * it's assumed to be fully validated.)
6644 : *
6645 : * name is not known and can't be created as a placeholder. Throw error,
6646 : * unless skipIfNoPermissions is true, in which case return false.
6647 : */
208 tgl 6648 GIC 605 : gconf = find_option(name, true, skipIfNoPermissions, ERROR);
6649 605 : if (!gconf)
6650 : {
6651 : /* not known, failed to make a placeholder */
208 tgl 6652 UIC 0 : return false;
6653 : }
6654 :
208 tgl 6655 GIC 605 : if (gconf->flags & GUC_CUSTOM_PLACEHOLDER)
6656 : {
6657 : /*
6658 : * We cannot do any meaningful check on the value, so only permissions
6659 : * are useful to check. USER SET options are always allowed.
6660 : */
121 akorotkov 6661 GNC 8 : if (user_set)
6662 3 : return true;
208 tgl 6663 GIC 9 : if (superuser() ||
6664 4 : pg_parameter_aclcheck(name, GetUserId(), ACL_SET) == ACLCHECK_OK)
6665 2 : return true;
6666 3 : if (skipIfNoPermissions)
1596 peter_e 6667 UIC 0 : return false;
208 tgl 6668 GIC 3 : ereport(ERROR,
6669 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
6670 : errmsg("permission denied to set parameter \"%s\"", name)));
6671 : }
6672 :
6673 : /* manual permissions check so we can avoid an error being thrown */
6674 597 : if (gconf->context == PGC_USERSET)
6675 : /* ok */ ;
6676 186 : else if (gconf->context == PGC_SUSET &&
6677 99 : (superuser() ||
6678 6 : pg_parameter_aclcheck(name, GetUserId(), ACL_SET) == ACLCHECK_OK))
6679 : /* ok */ ;
6680 2 : else if (skipIfNoPermissions)
6681 1 : return false;
6682 : /* if a permissions error should be thrown, let set_config_option do it */
6683 :
6684 : /* test for permissions and valid option value */
6685 596 : (void) set_config_option(name, value,
6686 596 : superuser() ? PGC_SUSET : PGC_USERSET,
6687 : PGC_S_TEST, GUC_ACTION_SET, false, 0, false);
6688 :
1596 peter_e 6689 595 : return true;
6690 : }
6691 :
6692 :
6693 : /*
6694 : * Called by check_hooks that want to override the normal
6695 : * ERRCODE_INVALID_PARAMETER_VALUE SQLSTATE for check hook failures.
6696 : *
6697 : * Note that GUC_check_errmsg() etc are just macros that result in a direct
6698 : * assignment to the associated variables. That is ugly, but forced by the
6699 : * limitations of C's macro mechanisms.
6700 : */
6701 : void
208 tgl 6702 19 : GUC_check_errcode(int sqlerrcode)
6703 : {
6704 19 : GUC_check_errcode_value = sqlerrcode;
1596 peter_e 6705 19 : }
6706 :
6707 :
6708 : /*
6709 : * Convenience functions to manage calling a variable's check_hook.
6710 : * These mostly take care of the protocol for letting check hooks supply
6711 : * portions of the error report on failure.
6712 : */
6713 :
6714 : static bool
208 tgl 6715 262151 : call_bool_check_hook(struct config_bool *conf, bool *newval, void **extra,
6716 : GucSource source, int elevel)
6717 : {
6718 : /* Quick success if no hook */
6719 262151 : if (!conf->check_hook)
6720 235843 : return true;
6721 :
6722 : /* Reset variables that might be set by hook */
6723 26308 : GUC_check_errcode_value = ERRCODE_INVALID_PARAMETER_VALUE;
6724 26308 : GUC_check_errmsg_string = NULL;
6725 26308 : GUC_check_errdetail_string = NULL;
6726 26308 : GUC_check_errhint_string = NULL;
6727 :
6728 26308 : if (!conf->check_hook(newval, extra, source))
6729 : {
6730 12 : ereport(elevel,
6731 : (errcode(GUC_check_errcode_value),
6732 : GUC_check_errmsg_string ?
6733 : errmsg_internal("%s", GUC_check_errmsg_string) :
6734 : errmsg("invalid value for parameter \"%s\": %d",
6735 : conf->gen.name, (int) *newval),
6736 : GUC_check_errdetail_string ?
6737 : errdetail_internal("%s", GUC_check_errdetail_string) : 0,
6738 : GUC_check_errhint_string ?
6739 : errhint("%s", GUC_check_errhint_string) : 0));
6740 : /* Flush any strings created in ErrorContext */
208 tgl 6741 UIC 0 : FlushErrorState();
6742 0 : return false;
6743 : }
6744 :
1596 peter_e 6745 GIC 26296 : return true;
6746 : }
6747 :
6748 : static bool
208 tgl 6749 284475 : call_int_check_hook(struct config_int *conf, int *newval, void **extra,
6750 : GucSource source, int elevel)
6751 : {
6752 : /* Quick success if no hook */
6753 284475 : if (!conf->check_hook)
6754 250351 : return true;
6755 :
6756 : /* Reset variables that might be set by hook */
6757 34124 : GUC_check_errcode_value = ERRCODE_INVALID_PARAMETER_VALUE;
6758 34124 : GUC_check_errmsg_string = NULL;
6759 34124 : GUC_check_errdetail_string = NULL;
6760 34124 : GUC_check_errhint_string = NULL;
6761 :
6762 34124 : if (!conf->check_hook(newval, extra, source))
6763 : {
208 tgl 6764 UIC 0 : ereport(elevel,
6765 : (errcode(GUC_check_errcode_value),
6766 : GUC_check_errmsg_string ?
6767 : errmsg_internal("%s", GUC_check_errmsg_string) :
6768 : errmsg("invalid value for parameter \"%s\": %d",
6769 : conf->gen.name, *newval),
6770 : GUC_check_errdetail_string ?
6771 : errdetail_internal("%s", GUC_check_errdetail_string) : 0,
6772 : GUC_check_errhint_string ?
6773 : errhint("%s", GUC_check_errhint_string) : 0));
6774 : /* Flush any strings created in ErrorContext */
6775 0 : FlushErrorState();
6776 0 : return false;
6777 : }
6778 :
1596 peter_e 6779 GIC 34124 : return true;
6780 : }
6781 :
6782 : static bool
208 tgl 6783 49796 : call_real_check_hook(struct config_real *conf, double *newval, void **extra,
6784 : GucSource source, int elevel)
6785 : {
6786 : /* Quick success if no hook */
6787 49796 : if (!conf->check_hook)
6788 47939 : return true;
6789 :
6790 : /* Reset variables that might be set by hook */
6791 1857 : GUC_check_errcode_value = ERRCODE_INVALID_PARAMETER_VALUE;
6792 1857 : GUC_check_errmsg_string = NULL;
6793 1857 : GUC_check_errdetail_string = NULL;
6794 1857 : GUC_check_errhint_string = NULL;
6795 :
6796 1857 : if (!conf->check_hook(newval, extra, source))
6797 : {
208 tgl 6798 UIC 0 : ereport(elevel,
6799 : (errcode(GUC_check_errcode_value),
6800 : GUC_check_errmsg_string ?
6801 : errmsg_internal("%s", GUC_check_errmsg_string) :
6802 : errmsg("invalid value for parameter \"%s\": %g",
6803 : conf->gen.name, *newval),
6804 : GUC_check_errdetail_string ?
6805 : errdetail_internal("%s", GUC_check_errdetail_string) : 0,
6806 : GUC_check_errhint_string ?
6807 : errhint("%s", GUC_check_errhint_string) : 0));
6808 : /* Flush any strings created in ErrorContext */
6809 0 : FlushErrorState();
1596 peter_e 6810 0 : return false;
6811 : }
6812 :
1596 peter_e 6813 GIC 1857 : return true;
6814 : }
6815 :
6816 : static bool
208 tgl 6817 286421 : call_string_check_hook(struct config_string *conf, char **newval, void **extra,
6818 : GucSource source, int elevel)
6819 : {
6820 286421 : volatile bool result = true;
6821 :
6822 : /* Quick success if no hook */
6823 286421 : if (!conf->check_hook)
6824 114694 : return true;
6825 :
6826 : /*
6827 : * If elevel is ERROR, or if the check_hook itself throws an elog
6828 : * (undesirable, but not always avoidable), make sure we don't leak the
6829 : * already-malloc'd newval string.
6830 : */
6831 171727 : PG_TRY();
6832 : {
6833 : /* Reset variables that might be set by hook */
6834 171727 : GUC_check_errcode_value = ERRCODE_INVALID_PARAMETER_VALUE;
6835 171727 : GUC_check_errmsg_string = NULL;
6836 171727 : GUC_check_errdetail_string = NULL;
6837 171727 : GUC_check_errhint_string = NULL;
6838 :
6839 171727 : if (!conf->check_hook(newval, extra, source))
6840 : {
6841 18 : ereport(elevel,
6842 : (errcode(GUC_check_errcode_value),
6843 : GUC_check_errmsg_string ?
6844 : errmsg_internal("%s", GUC_check_errmsg_string) :
6845 : errmsg("invalid value for parameter \"%s\": \"%s\"",
6846 : conf->gen.name, *newval ? *newval : ""),
6847 : GUC_check_errdetail_string ?
6848 : errdetail_internal("%s", GUC_check_errdetail_string) : 0,
6849 : GUC_check_errhint_string ?
6850 : errhint("%s", GUC_check_errhint_string) : 0));
6851 : /* Flush any strings created in ErrorContext */
208 tgl 6852 UIC 0 : FlushErrorState();
6853 0 : result = false;
6854 : }
6855 : }
208 tgl 6856 GIC 21 : PG_CATCH();
6857 : {
177 tgl 6858 GNC 21 : guc_free(*newval);
208 tgl 6859 GIC 21 : PG_RE_THROW();
6860 : }
6861 171706 : PG_END_TRY();
6862 :
6863 171706 : return result;
6864 : }
6865 :
6866 : static bool
6867 102661 : call_enum_check_hook(struct config_enum *conf, int *newval, void **extra,
6868 : GucSource source, int elevel)
6869 : {
6870 : /* Quick success if no hook */
6871 102661 : if (!conf->check_hook)
6872 91316 : return true;
6873 :
6874 : /* Reset variables that might be set by hook */
6875 11345 : GUC_check_errcode_value = ERRCODE_INVALID_PARAMETER_VALUE;
6876 11345 : GUC_check_errmsg_string = NULL;
6877 11345 : GUC_check_errdetail_string = NULL;
6878 11345 : GUC_check_errhint_string = NULL;
6879 :
6880 11345 : if (!conf->check_hook(newval, extra, source))
6881 : {
6882 1 : ereport(elevel,
6883 : (errcode(GUC_check_errcode_value),
6884 : GUC_check_errmsg_string ?
6885 : errmsg_internal("%s", GUC_check_errmsg_string) :
6886 : errmsg("invalid value for parameter \"%s\": \"%s\"",
6887 : conf->gen.name,
6888 : config_enum_lookup_by_value(conf, *newval)),
6889 : GUC_check_errdetail_string ?
6890 : errdetail_internal("%s", GUC_check_errdetail_string) : 0,
6891 : GUC_check_errhint_string ?
6892 : errhint("%s", GUC_check_errhint_string) : 0));
6893 : /* Flush any strings created in ErrorContext */
208 tgl 6894 UIC 0 : FlushErrorState();
1546 andres 6895 0 : return false;
6896 : }
6897 :
1546 andres 6898 GIC 11344 : return true;
6899 : }
|