Some records of GUC in Postgres

What is GUC

Various parameters required to run in the database -> GUC: Grand Unified Configuration. When we need to write related plug-ins in postgres, we cannot do without GUC. This article makes some simple records of GUC.

Different GUCs in Postgres

These parameters can come from various sources, and postgres handles each source differently. These sources include pg default, system environment, user, session, and so on. . . Some pg-based distributed databases also add additional GUC sources.

postgres 9.4 guc file src/backend/utils/misc/guc.c

 const char *const GucSource_Names[] = { /* PGC_S_DEFAULT */ "default", /* PGC_S_DYNAMIC_DEFAULT */ "default", /* PGC_S_ENV_VAR */ "environment variable", /* PGC_S_FILE */ "configuration file", /* PGC_S_ARGV */ "command line", /* PGC_S_GLOBAL */ "global", /* PGC_S_DATABASE */ "database", /* PGC_S_USER */ "user", /* PGC_S_DATABASE_USER */ "database user", /* PGC_S_CLIENT */ "client", /* PGC_S_OVERRIDE */ "override", /* PGC_S_INTERACTIVE */ "interactive", /* PGC_S_TEST */ "test", /* PGC_S_SESSION */ "session" };

We can look up these related GUCs and sources with the command

 select name, setting, source from pg_settings; 

image

Different GUCs will also have different permissions.

 /* * Displayable names for context types (enum GucContext) * * Note: these strings are deliberately not localized. */ const char *const GucContext_Names[] = { /* PGC_INTERNAL */ "internal", /* PGC_POSTMASTER */ "postmaster", /* PGC_SIGHUP */ "sighup", /* PGC_BACKEND */ "backend", /* PGC_SUSET */ "superuser", /* PGC_USERSET */ "user" };

Types of GUCs

 const char *const config_type_names[] = { /* PGC_BOOL */ "bool", /* PGC_INT */ "integer", /* PGC_REAL */ "real", /* PGC_STRING */ "string", /* PGC_ENUM */ "enum" };

Different types of GUC have corresponding relative GUC functions, and use DefineCustomXXXVariable to name the srting type as an example:

 void DefineCustomStringVariable(const char *name, const char *short_desc, const char *long_desc, char **valueAddr, const char *bootValue, GucContext context, int flags, GucStringCheckHook check_hook, GucStringAssignHook assign_hook, GucShowHook show_hook) { struct config_string *var; var = (struct config_string *) init_custom_variable(name, short_desc, long_desc, context, flags, PGC_STRING, sizeof(struct config_string)); var->variable = valueAddr; var->boot_val = bootValue; var->check_hook = check_hook; var->assign_hook = assign_hook; var->show_hook = show_hook; define_custom_variable(&var->gen); }

hooks in GUC

As can be seen from DefineCustomStringVariable above, 3 different hooks can be added to each type of GUC. These 3 hooks are triggered when set, respectively

  • bool check_hook: verify the validity of the data, return false or true, if true, the setting is successful, this function can provide additional information, such as pop-up error messages
  • void assign_hook: Triggered when the value is set
  • const char *show_hook: After setting guc, it will be displayed when the show command is displayed.

If you want to write a pg plugin, how to add a GUC?

  1. Confirm GUC type, permission, trigger condition. Set new guc with DefineCustomXXXVariable in guc.
  2. Add the new GUC plugin to _PG_init
  3. Set up the hook function
  4. test

Interesting details in GUC

  1. 3 hooks are also called on the first initialization
  2. guc-file.c is flex generated by guc-file.l. If you are interested in interesting details, you can refer to this article
  3. In a distributed database based on pg, such as gpdb, the master-slave synchronization problem of guc also needs to be considered, and there are additional parameters to control

This article is reprinted from: https://github.com/yihong0618/gitblog/issues/233
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment