PostgreSQL Source Code git master
guc_internal.h File Reference
#include "utils/guc.h"
Include dependency graph for guc_internal.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int guc_name_compare (const char *namea, const char *nameb)
 
ConfigVariableProcessConfigFileInternal (GucContext context, bool applySettings, int elevel)
 
void record_config_file_error (const char *errmsg, const char *config_file, int lineno, ConfigVariable **head_p, ConfigVariable **tail_p)
 

Function Documentation

◆ guc_name_compare()

int guc_name_compare ( const char *  namea,
const char *  nameb 
)

Definition at line 1177 of file guc.c.

1178{
1179 /*
1180 * The temptation to use strcasecmp() here must be resisted, because the
1181 * hash mapping has to remain stable across setlocale() calls. So, build
1182 * our own with a simple ASCII-only downcasing.
1183 */
1184 while (*namea && *nameb)
1185 {
1186 char cha = *namea++;
1187 char chb = *nameb++;
1188
1189 if (cha >= 'A' && cha <= 'Z')
1190 cha += 'a' - 'A';
1191 if (chb >= 'A' && chb <= 'Z')
1192 chb += 'a' - 'A';
1193 if (cha != chb)
1194 return cha - chb;
1195 }
1196 if (*namea)
1197 return 1; /* a is longer */
1198 if (*nameb)
1199 return -1; /* b is longer */
1200 return 0;
1201}

Referenced by convert_GUC_name_for_parameter_acl(), find_option(), GetPGVariable(), GetPGVariableResultDesc(), guc_name_match(), guc_var_compare(), ParseConfigFp(), and replace_auto_config_value().

◆ ProcessConfigFileInternal()

ConfigVariable * ProcessConfigFileInternal ( GucContext  context,
bool  applySettings,
int  elevel 
)

Definition at line 284 of file guc.c.

285{
286 bool error = false;
287 bool applying = false;
288 const char *ConfFileWithError;
289 ConfigVariable *head,
290 *tail;
291 HASH_SEQ_STATUS status;
292 GUCHashEntry *hentry;
293
294 /* Parse the main config file into a list of option names and values */
295 ConfFileWithError = ConfigFileName;
296 head = tail = NULL;
297
299 NULL, 0, CONF_FILE_START_DEPTH, elevel,
300 &head, &tail))
301 {
302 /* Syntax error(s) detected in the file, so bail out */
303 error = true;
304 goto bail_out;
305 }
306
307 /*
308 * Parse the PG_AUTOCONF_FILENAME file, if present, after the main file to
309 * replace any parameters set by ALTER SYSTEM command. Because this file
310 * is in the data directory, we can't read it until the DataDir has been
311 * set.
312 */
313 if (DataDir)
314 {
316 NULL, 0, CONF_FILE_START_DEPTH, elevel,
317 &head, &tail))
318 {
319 /* Syntax error(s) detected in the file, so bail out */
320 error = true;
321 ConfFileWithError = PG_AUTOCONF_FILENAME;
322 goto bail_out;
323 }
324 }
325 else
326 {
327 /*
328 * If DataDir is not set, the PG_AUTOCONF_FILENAME file cannot be
329 * read. In this case, we don't want to accept any settings but
330 * data_directory from postgresql.conf, because they might be
331 * overwritten with settings in the PG_AUTOCONF_FILENAME file which
332 * will be read later. OTOH, since data_directory isn't allowed in the
333 * PG_AUTOCONF_FILENAME file, it will never be overwritten later.
334 */
335 ConfigVariable *newlist = NULL;
336
337 /*
338 * Prune all items except the last "data_directory" from the list.
339 */
340 for (ConfigVariable *item = head; item; item = item->next)
341 {
342 if (!item->ignore &&
343 strcmp(item->name, "data_directory") == 0)
344 newlist = item;
345 }
346
347 if (newlist)
348 newlist->next = NULL;
349 head = tail = newlist;
350
351 /*
352 * Quick exit if data_directory is not present in file.
353 *
354 * We need not do any further processing, in particular we don't set
355 * PgReloadTime; that will be set soon by subsequent full loading of
356 * the config file.
357 */
358 if (head == NULL)
359 goto bail_out;
360 }
361
362 /*
363 * Mark all extant GUC variables as not present in the config file. We
364 * need this so that we can tell below which ones have been removed from
365 * the file since we last processed it.
366 */
367 hash_seq_init(&status, guc_hashtab);
368 while ((hentry = (GUCHashEntry *) hash_seq_search(&status)) != NULL)
369 {
370 struct config_generic *gconf = hentry->gucvar;
371
372 gconf->status &= ~GUC_IS_IN_FILE;
373 }
374
375 /*
376 * Check if all the supplied option names are valid, as an additional
377 * quasi-syntactic check on the validity of the config file. It is
378 * important that the postmaster and all backends agree on the results of
379 * this phase, else we will have strange inconsistencies about which
380 * processes accept a config file update and which don't. Hence, unknown
381 * custom variable names have to be accepted without complaint. For the
382 * same reason, we don't attempt to validate the options' values here.
383 *
384 * In addition, the GUC_IS_IN_FILE flag is set on each existing GUC
385 * variable mentioned in the file; and we detect duplicate entries in the
386 * file and mark the earlier occurrences as ignorable.
387 */
388 for (ConfigVariable *item = head; item; item = item->next)
389 {
390 struct config_generic *record;
391
392 /* Ignore anything already marked as ignorable */
393 if (item->ignore)
394 continue;
395
396 /*
397 * Try to find the variable; but do not create a custom placeholder if
398 * it's not there already.
399 */
400 record = find_option(item->name, false, true, elevel);
401
402 if (record)
403 {
404 /* If it's already marked, then this is a duplicate entry */
405 if (record->status & GUC_IS_IN_FILE)
406 {
407 /*
408 * Mark the earlier occurrence(s) as dead/ignorable. We could
409 * avoid the O(N^2) behavior here with some additional state,
410 * but it seems unlikely to be worth the trouble.
411 */
412 for (ConfigVariable *pitem = head; pitem != item; pitem = pitem->next)
413 {
414 if (!pitem->ignore &&
415 strcmp(pitem->name, item->name) == 0)
416 pitem->ignore = true;
417 }
418 }
419 /* Now mark it as present in file */
420 record->status |= GUC_IS_IN_FILE;
421 }
422 else if (!valid_custom_variable_name(item->name))
423 {
424 /* Invalid non-custom variable, so complain */
425 ereport(elevel,
426 (errcode(ERRCODE_UNDEFINED_OBJECT),
427 errmsg("unrecognized configuration parameter \"%s\" in file \"%s\" line %d",
428 item->name,
429 item->filename, item->sourceline)));
430 item->errmsg = pstrdup("unrecognized configuration parameter");
431 error = true;
432 ConfFileWithError = item->filename;
433 }
434 }
435
436 /*
437 * If we've detected any errors so far, we don't want to risk applying any
438 * changes.
439 */
440 if (error)
441 goto bail_out;
442
443 /* Otherwise, set flag that we're beginning to apply changes */
444 applying = true;
445
446 /*
447 * Check for variables having been removed from the config file, and
448 * revert their reset values (and perhaps also effective values) to the
449 * boot-time defaults. If such a variable can't be changed after startup,
450 * report that and continue.
451 */
453 while ((hentry = (GUCHashEntry *) hash_seq_search(&status)) != NULL)
454 {
455 struct config_generic *gconf = hentry->gucvar;
456
457 if (gconf->reset_source != PGC_S_FILE ||
458 (gconf->status & GUC_IS_IN_FILE))
459 continue;
460 if (gconf->context < PGC_SIGHUP)
461 {
462 /* The removal can't be effective without a restart */
463 gconf->status |= GUC_PENDING_RESTART;
464 ereport(elevel,
465 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
466 errmsg("parameter \"%s\" cannot be changed without restarting the server",
467 gconf->name)));
468 record_config_file_error(psprintf("parameter \"%s\" cannot be changed without restarting the server",
469 gconf->name),
470 NULL, 0,
471 &head, &tail);
472 error = true;
473 continue;
474 }
475
476 /* No more to do if we're just doing show_all_file_settings() */
477 if (!applySettings)
478 continue;
479
480 /*
481 * Reset any "file" sources to "default", else set_config_option will
482 * not override those settings.
483 */
484 if (gconf->reset_source == PGC_S_FILE)
486 if (gconf->source == PGC_S_FILE)
488 for (GucStack *stack = gconf->stack; stack; stack = stack->prev)
489 {
490 if (stack->source == PGC_S_FILE)
492 }
493
494 /* Now we can re-apply the wired-in default (i.e., the boot_val) */
495 if (set_config_option(gconf->name, NULL,
497 GUC_ACTION_SET, true, 0, false) > 0)
498 {
499 /* Log the change if appropriate */
500 if (context == PGC_SIGHUP)
501 ereport(elevel,
502 (errmsg("parameter \"%s\" removed from configuration file, reset to default",
503 gconf->name)));
504 }
505 }
506
507 /*
508 * Restore any variables determined by environment variables or
509 * dynamically-computed defaults. This is a no-op except in the case
510 * where one of these had been in the config file and is now removed.
511 *
512 * In particular, we *must not* do this during the postmaster's initial
513 * loading of the file, since the timezone functions in particular should
514 * be run only after initialization is complete.
515 *
516 * XXX this is an unmaintainable crock, because we have to know how to set
517 * (or at least what to call to set) every non-PGC_INTERNAL variable that
518 * could potentially have PGC_S_DYNAMIC_DEFAULT or PGC_S_ENV_VAR source.
519 */
520 if (context == PGC_SIGHUP && applySettings)
521 {
524 /* this selects SQL_ASCII in processes not connected to a database */
525 SetConfigOption("client_encoding", GetDatabaseEncodingName(),
527 }
528
529 /*
530 * Now apply the values from the config file.
531 */
532 for (ConfigVariable *item = head; item; item = item->next)
533 {
534 char *pre_value = NULL;
535 int scres;
536
537 /* Ignore anything marked as ignorable */
538 if (item->ignore)
539 continue;
540
541 /* In SIGHUP cases in the postmaster, we want to report changes */
542 if (context == PGC_SIGHUP && applySettings && !IsUnderPostmaster)
543 {
544 const char *preval = GetConfigOption(item->name, true, false);
545
546 /* If option doesn't exist yet or is NULL, treat as empty string */
547 if (!preval)
548 preval = "";
549 /* must dup, else might have dangling pointer below */
550 pre_value = pstrdup(preval);
551 }
552
553 scres = set_config_option(item->name, item->value,
555 GUC_ACTION_SET, applySettings, 0, false);
556 if (scres > 0)
557 {
558 /* variable was updated, so log the change if appropriate */
559 if (pre_value)
560 {
561 const char *post_value = GetConfigOption(item->name, true, false);
562
563 if (!post_value)
564 post_value = "";
565 if (strcmp(pre_value, post_value) != 0)
566 ereport(elevel,
567 (errmsg("parameter \"%s\" changed to \"%s\"",
568 item->name, item->value)));
569 }
570 item->applied = true;
571 }
572 else if (scres == 0)
573 {
574 error = true;
575 item->errmsg = pstrdup("setting could not be applied");
576 ConfFileWithError = item->filename;
577 }
578 else
579 {
580 /* no error, but variable's active value was not changed */
581 item->applied = true;
582 }
583
584 /*
585 * We should update source location unless there was an error, since
586 * even if the active value didn't change, the reset value might have.
587 * (In the postmaster, there won't be a difference, but it does matter
588 * in backends.)
589 */
590 if (scres != 0 && applySettings)
591 set_config_sourcefile(item->name, item->filename,
592 item->sourceline);
593
594 if (pre_value)
595 pfree(pre_value);
596 }
597
598 /* Remember when we last successfully loaded the config file. */
599 if (applySettings)
601
603 if (error && applySettings)
604 {
605 /* During postmaster startup, any error is fatal */
606 if (context == PGC_POSTMASTER)
608 (errcode(ERRCODE_CONFIG_FILE_ERROR),
609 errmsg("configuration file \"%s\" contains errors",
610 ConfFileWithError)));
611 else if (applying)
612 ereport(elevel,
613 (errcode(ERRCODE_CONFIG_FILE_ERROR),
614 errmsg("configuration file \"%s\" contains errors; unaffected changes were applied",
615 ConfFileWithError)));
616 else
617 ereport(elevel,
618 (errcode(ERRCODE_CONFIG_FILE_ERROR),
619 errmsg("configuration file \"%s\" contains errors; no changes were applied",
620 ConfFileWithError)));
621 }
622
623 /* Successful or otherwise, return the collected data list */
624 return head;
625}
TimestampTz PgReloadTime
Definition: timestamp.c:57
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1645
#define CONF_FILE_START_DEPTH
Definition: conffiles.h:17
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1415
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1380
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
bool IsUnderPostmaster
Definition: globals.c:120
char * DataDir
Definition: globals.c:71
void record_config_file_error(const char *errmsg, const char *config_file, int lineno, ConfigVariable **head_p, ConfigVariable **tail_p)
Definition: guc-file.l:278
bool ParseConfigFile(const char *config_file, bool strict, const char *calling_file, int calling_lineno, int depth, int elevel, ConfigVariable **head_p, ConfigVariable **tail_p)
Definition: guc-file.l:175
static void set_config_sourcefile(const char *name, char *sourcefile, int sourceline)
Definition: guc.c:4163
static bool valid_custom_variable_name(const char *name)
Definition: guc.c:956
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4196
const char * GetConfigOption(const char *name, bool missing_ok, bool restrict_privileged)
Definition: guc.c:4219
static void pg_timezone_abbrev_initialize(void)
Definition: guc.c:1867
struct config_generic * find_option(const char *name, bool create_placeholders, bool skip_errors, int elevel)
Definition: guc.c:1113
static void InitializeGUCOptionsFromEnvironment(void)
Definition: guc.c:1466
static void set_guc_source(struct config_generic *gconf, GucSource newsource)
Definition: guc.c:1986
static HTAB * guc_hashtab
Definition: guc.c:213
int set_config_option(const char *name, const char *value, GucContext context, GucSource source, GucAction action, bool changeVal, int elevel, bool is_reload)
Definition: guc.c:3216
#define PG_AUTOCONF_FILENAME
Definition: guc.h:37
@ GUC_ACTION_SET
Definition: guc.h:203
@ PGC_S_DEFAULT
Definition: guc.h:113
@ PGC_S_DYNAMIC_DEFAULT
Definition: guc.h:114
@ PGC_S_FILE
Definition: guc.h:116
@ PGC_POSTMASTER
Definition: guc.h:74
@ PGC_SIGHUP
Definition: guc.h:75
@ PGC_BACKEND
Definition: guc.h:77
char * ConfigFileName
Definition: guc_tables.c:556
#define GUC_IS_IN_FILE
Definition: guc_tables.h:294
#define GUC_PENDING_RESTART
Definition: guc_tables.h:299
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1268
char * pstrdup(const char *in)
Definition: mcxt.c:1759
void pfree(void *pointer)
Definition: mcxt.c:1594
static void bail_out(bool noatexit, const char *fmt,...) pg_attribute_printf(2
Definition: pg_regress.c:255
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
static void error(void)
Definition: sql-dyntest.c:147
struct ConfigVariable * next
Definition: guc.h:148
struct config_generic * gucvar
Definition: guc.c:210
GucContext context
Definition: guc_tables.h:253
const char * name
Definition: guc_tables.h:252
GucStack * stack
Definition: guc_tables.h:267
GucSource source
Definition: guc_tables.h:261
GucSource reset_source
Definition: guc_tables.h:262
struct guc_stack * prev
Definition: guc_tables.h:122
GucSource source
Definition: guc_tables.h:125

References bail_out(), CONF_FILE_START_DEPTH, ConfigFileName, config_generic::context, DataDir, ereport, errcode(), errmsg(), ERROR, error(), find_option(), GetConfigOption(), GetCurrentTimestamp(), GetDatabaseEncodingName(), GUC_ACTION_SET, guc_hashtab, GUC_IS_IN_FILE, GUC_PENDING_RESTART, GUCHashEntry::gucvar, hash_seq_init(), hash_seq_search(), InitializeGUCOptionsFromEnvironment(), IsUnderPostmaster, config_generic::name, ConfigVariable::next, ParseConfigFile(), pfree(), PG_AUTOCONF_FILENAME, pg_timezone_abbrev_initialize(), PGC_BACKEND, PGC_POSTMASTER, PGC_S_DEFAULT, PGC_S_DYNAMIC_DEFAULT, PGC_S_FILE, PGC_SIGHUP, PgReloadTime, guc_stack::prev, psprintf(), pstrdup(), record_config_file_error(), config_generic::reset_source, set_config_option(), set_config_sourcefile(), set_guc_source(), SetConfigOption(), guc_stack::source, config_generic::source, config_generic::stack, config_generic::status, and valid_custom_variable_name().

Referenced by ProcessConfigFile(), and show_all_file_settings().

◆ record_config_file_error()

void record_config_file_error ( const char *  errmsg,
const char *  config_file,
int  lineno,
ConfigVariable **  head_p,
ConfigVariable **  tail_p 
)

Definition at line 278 of file guc-file.l.

283{
284 ConfigVariable *item;
285
286 item = palloc(sizeof *item);
287 item->name = NULL;
288 item->value = NULL;
289 item->errmsg = pstrdup(errmsg);
290 item->filename = config_file ? pstrdup(config_file) : NULL;
291 item->sourceline = lineno;
292 item->ignore = true;
293 item->applied = false;
294 item->next = NULL;
295 if (*head_p == NULL)
296 *head_p = item;
297 else
298 (*tail_p)->next = item;
299 *tail_p = item;
300}
void * palloc(Size size)
Definition: mcxt.c:1365
static char * config_file
Definition: pg_rewind.c:71
char * name
Definition: guc.h:141
bool ignore
Definition: guc.h:146
bool applied
Definition: guc.h:147
char * filename
Definition: guc.h:144
int sourceline
Definition: guc.h:145
char * value
Definition: guc.h:142
char * errmsg
Definition: guc.h:143

References ConfigVariable::applied, config_file, errmsg(), ConfigVariable::errmsg, ConfigVariable::filename, ConfigVariable::ignore, ConfigVariable::name, ConfigVariable::next, palloc(), pstrdup(), ConfigVariable::sourceline, and ConfigVariable::value.

Referenced by ParseConfigDirectory(), ParseConfigFile(), ParseConfigFp(), and ProcessConfigFileInternal().