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

1266 {
1267  /*
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  */
1272  while (*namea && *nameb)
1273  {
1274  char cha = *namea++;
1275  char chb = *nameb++;
1276 
1277  if (cha >= 'A' && cha <= 'Z')
1278  cha += 'a' - 'A';
1279  if (chb >= 'A' && chb <= 'Z')
1280  chb += 'a' - 'A';
1281  if (cha != chb)
1282  return cha - chb;
1283  }
1284  if (*namea)
1285  return 1; /* a is longer */
1286  if (*nameb)
1287  return -1; /* b is longer */
1288  return 0;
1289 }

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

276 {
277  bool error = false;
278  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  ConfFileWithError = ConfigFileName;
288  head = tail = NULL;
289 
290  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 */
295  error = true;
296  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  */
305  if (DataDir)
306  {
308  NULL, 0, CONF_FILE_START_DEPTH, elevel,
309  &head, &tail))
310  {
311  /* Syntax error(s) detected in the file, so bail out */
312  error = true;
313  ConfFileWithError = PG_AUTOCONF_FILENAME;
314  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  */
327  ConfigVariable *newlist = NULL;
328 
329  /*
330  * Prune all items except the last "data_directory" from the list.
331  */
332  for (item = head; item; item = item->next)
333  {
334  if (!item->ignore &&
335  strcmp(item->name, "data_directory") == 0)
336  newlist = item;
337  }
338 
339  if (newlist)
340  newlist->next = NULL;
341  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  if (head == NULL)
351  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  */
359  hash_seq_init(&status, guc_hashtab);
360  while ((hentry = (GUCHashEntry *) hash_seq_search(&status)) != NULL)
361  {
362  struct config_generic *gconf = hentry->gucvar;
363 
364  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  for (item = head; item; item = item->next)
381  {
382  struct config_generic *record;
383 
384  /* Ignore anything already marked as ignorable */
385  if (item->ignore)
386  continue;
387 
388  /*
389  * Try to find the variable; but do not create a custom placeholder if
390  * it's not there already.
391  */
392  record = find_option(item->name, false, true, elevel);
393 
394  if (record)
395  {
396  /* If it's already marked, then this is a duplicate entry */
397  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  for (pitem = head; pitem != item; pitem = pitem->next)
407  {
408  if (!pitem->ignore &&
409  strcmp(pitem->name, item->name) == 0)
410  pitem->ignore = true;
411  }
412  }
413  /* Now mark it as present in file */
414  record->status |= GUC_IS_IN_FILE;
415  }
416  else if (!valid_custom_variable_name(item->name))
417  {
418  /* Invalid non-custom variable, so complain */
419  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  item->errmsg = pstrdup("unrecognized configuration parameter");
425  error = true;
426  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  if (error)
435  goto bail_out;
436 
437  /* Otherwise, set flag that we're beginning to apply changes */
438  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  */
447  while ((hentry = (GUCHashEntry *) hash_seq_search(&status)) != NULL)
448  {
449  struct config_generic *gconf = hentry->gucvar;
450  GucStack *stack;
451 
452  if (gconf->reset_source != PGC_S_FILE ||
453  (gconf->status & GUC_IS_IN_FILE))
454  continue;
455  if (gconf->context < PGC_SIGHUP)
456  {
457  /* The removal can't be effective without a restart */
458  gconf->status |= GUC_PENDING_RESTART;
459  ereport(elevel,
460  (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
461  errmsg("parameter \"%s\" cannot be changed without restarting the server",
462  gconf->name)));
463  record_config_file_error(psprintf("parameter \"%s\" cannot be changed without restarting the server",
464  gconf->name),
465  NULL, 0,
466  &head, &tail);
467  error = true;
468  continue;
469  }
470 
471  /* No more to do if we're just doing show_all_file_settings() */
472  if (!applySettings)
473  continue;
474 
475  /*
476  * Reset any "file" sources to "default", else set_config_option will
477  * not override those settings.
478  */
479  if (gconf->reset_source == PGC_S_FILE)
480  gconf->reset_source = PGC_S_DEFAULT;
481  if (gconf->source == PGC_S_FILE)
483  for (stack = gconf->stack; stack; stack = stack->prev)
484  {
485  if (stack->source == PGC_S_FILE)
487  }
488 
489  /* Now we can re-apply the wired-in default (i.e., the boot_val) */
490  if (set_config_option(gconf->name, NULL,
492  GUC_ACTION_SET, true, 0, false) > 0)
493  {
494  /* Log the change if appropriate */
495  if (context == PGC_SIGHUP)
496  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  if (context == PGC_SIGHUP && applySettings)
516  {
519  /* this selects SQL_ASCII in processes not connected to a database */
520  SetConfigOption("client_encoding", GetDatabaseEncodingName(),
522  }
523 
524  /*
525  * Now apply the values from the config file.
526  */
527  for (item = head; item; item = item->next)
528  {
529  char *pre_value = NULL;
530  int scres;
531 
532  /* Ignore anything marked as ignorable */
533  if (item->ignore)
534  continue;
535 
536  /* In SIGHUP cases in the postmaster, we want to report changes */
537  if (context == PGC_SIGHUP && applySettings && !IsUnderPostmaster)
538  {
539  const char *preval = GetConfigOption(item->name, true, false);
540 
541  /* If option doesn't exist yet or is NULL, treat as empty string */
542  if (!preval)
543  preval = "";
544  /* must dup, else might have dangling pointer below */
545  pre_value = pstrdup(preval);
546  }
547 
548  scres = set_config_option(item->name, item->value,
550  GUC_ACTION_SET, applySettings, 0, false);
551  if (scres > 0)
552  {
553  /* variable was updated, so log the change if appropriate */
554  if (pre_value)
555  {
556  const char *post_value = GetConfigOption(item->name, true, false);
557 
558  if (!post_value)
559  post_value = "";
560  if (strcmp(pre_value, post_value) != 0)
561  ereport(elevel,
562  (errmsg("parameter \"%s\" changed to \"%s\"",
563  item->name, item->value)));
564  }
565  item->applied = true;
566  }
567  else if (scres == 0)
568  {
569  error = true;
570  item->errmsg = pstrdup("setting could not be applied");
571  ConfFileWithError = item->filename;
572  }
573  else
574  {
575  /* no error, but variable's active value was not changed */
576  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  if (scres != 0 && applySettings)
586  set_config_sourcefile(item->name, item->filename,
587  item->sourceline);
588 
589  if (pre_value)
590  pfree(pre_value);
591  }
592 
593  /* Remember when we last successfully loaded the config file. */
594  if (applySettings)
596 
597 bail_out:
598  if (error && applySettings)
599  {
600  /* During postmaster startup, any error is fatal */
601  if (context == PGC_POSTMASTER)
602  ereport(ERROR,
603  (errcode(ERRCODE_CONFIG_FILE_ERROR),
604  errmsg("configuration file \"%s\" contains errors",
605  ConfFileWithError)));
606  else if (applying)
607  ereport(elevel,
608  (errcode(ERRCODE_CONFIG_FILE_ERROR),
609  errmsg("configuration file \"%s\" contains errors; unaffected changes were applied",
610  ConfFileWithError)));
611  else
612  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 */
619  return head;
620 }
TimestampTz PgReloadTime
Definition: timestamp.c:56
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1583
#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:1163
static void set_config_sourcefile(const char *name, char *sourcefile, int sourceline)
Definition: guc.c:4143
static bool valid_custom_variable_name(const char *name)
Definition: guc.c:1069
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4176
static void pg_timezone_abbrev_initialize(void)
Definition: guc.c:1958
const char * GetConfigOption(const char *name, bool missing_ok, bool restrict_privileged)
Definition: guc.c:4200
static void InitializeGUCOptionsFromEnvironment(void)
Definition: guc.c:1554
static void set_guc_source(struct config_generic *gconf, GucSource newsource)
Definition: guc.c:2077
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:3284
#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:528
#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:1644
void pfree(void *pointer)
Definition: mcxt.c:1456
static void bail_out(bool noatexit, const char *fmt,...) pg_attribute_printf(2
Definition: pg_regress.c:246
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, bail_out(), 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, 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 
)