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 1264 of file guc.c.

1265 {
1266  /*
1267  * The temptation to use strcasecmp() here must be resisted, because the
1268  * hash mapping has to remain stable across setlocale() calls. So, build
1269  * our own with a simple ASCII-only downcasing.
1270  */
1271  while (*namea && *nameb)
1272  {
1273  char cha = *namea++;
1274  char chb = *nameb++;
1275 
1276  if (cha >= 'A' && cha <= 'Z')
1277  cha += 'a' - 'A';
1278  if (chb >= 'A' && chb <= 'Z')
1279  chb += 'a' - 'A';
1280  if (cha != chb)
1281  return cha - chb;
1282  }
1283  if (*namea)
1284  return 1; /* a is longer */
1285  if (*nameb)
1286  return -1; /* b is longer */
1287  return 0;
1288 }

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

◆ ProcessConfigFileInternal()

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

Definition at line 274 of file guc.c.

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

References ConfigVariable::applied, CONF_FILE_START_DEPTH, ConfigFileName, config_generic::context, DataDir, ereport, errcode(), errmsg(), ConfigVariable::errmsg, ERROR, error(), ConfigVariable::filename, 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(), ConfigVariable::ignore, InitializeGUCOptionsFromEnvironment(), IsUnderPostmaster, ConfigVariable::name, 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, ConfigVariable::sourceline, config_generic::stack, config_generic::status, status(), valid_custom_variable_name(), and ConfigVariable::value.

Referenced by 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 
)