Postgresql Guc

GUC: Grand Unified Configuration 指的是postgreSQL数据库的一种对数据库变量进行设置对数据库进行控制的机制。通常理解是对postgresql.conf文件中变量进行修改,或通过set命令对参数进行设置。但实际上GUC变量的种类,设置方法要更加复杂多样。在guc.h和guc.c中可以看到GUC变量的详细实现。

Guc相关结构

  1. bool

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    struct config_bool
    {
    struct config_generic gen;
    /* constant fields, must be set correctly in initial value: */
    bool *variable;
    bool boot_val;
    GucBoolCheckHook check_hook;
    GucBoolAssignHook assign_hook;
    GucShowHook show_hook;
    /* variable fields, initialized at runtime: */
    bool reset_val;
    void *reset_extra;
    };
  1. int

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    struct config_int
    {
    struct config_generic gen;
    /* constant fields, must be set correctly in initial value: */
    int *variable;
    int boot_val;
    int min;
    int max;
    GucIntCheckHook check_hook;
    GucIntAssignHook assign_hook;
    GucShowHook show_hook;
    /* variable fields, initialized at runtime: */
    int reset_val;
    void *reset_extra;
    };
  2. real

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    struct config_real
    {
    struct config_generic gen;
    /* constant fields, must be set correctly in initial value: */
    double *variable;
    double boot_val;
    double min;
    double max;
    GucRealCheckHook check_hook;
    GucRealAssignHook assign_hook;
    GucShowHook show_hook;
    /* variable fields, initialized at runtime: */
    double reset_val;
    void *reset_extra;
    };
  3. string

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    struct config_string
    {
    struct config_generic gen;
    /* constant fields, must be set correctly in initial value: */
    char **variable;
    const char *boot_val;
    GucStringCheckHook check_hook;
    GucStringAssignHook assign_hook;
    GucShowHook show_hook;
    /* variable fields, initialized at runtime: */
    char *reset_val;
    void *reset_extra;
    };
  4. enum

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    struct config_enum
    {
    struct config_generic gen;
    /* constant fields, must be set correctly in initial value: */
    int *variable;
    int boot_val;
    const struct config_enum_entry *options;
    GucEnumCheckHook check_hook;
    GucEnumAssignHook assign_hook;
    GucShowHook show_hook;
    /* variable fields, initialized at runtime: */
    int reset_val;
    void *reset_extra;
    };

每一个类型建立一个对应的静态数组. 用于存储相应的Guc类型 src/backend/utils/misc/guc.c

1565589409242

Guc作用上下文:

1
2
3
4
5
6
7
8
9
10
typedef enum
{
PGC_INTERNAL,
PGC_POSTMASTER,
PGC_SIGHUP,
PGC_SU_BACKEND,
PGC_BACKEND,
PGC_SUSET,
PGC_USERSET
} GucContext;
  1. Internal 无法被用户修改, 只能被内部进程设置, show 命令能查看此类变量, 通常在编译时设置与改变.
  2. Postmaster在postmaster进程启动时通过读取configure文件或命令行来设置。 这类变量的改变在postgresql重启时生效.
  3. Sighup在postmaster进程启动或向postmaster 或backend进程发sighup信号来读取configure文件时设置.
  4. Backend在新的backend进程启动时读取configure文件生效.
  5. Suset 指超级用户修改生效, 不需要重新读取configure文件.
  6. User 指普通用户修改生效, 在当前会话下有效, 无需读取configure文件.

Guc 分组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
enum config_group
{
UNGROUPED,
FILE_LOCATIONS,
CONN_AUTH,
CONN_AUTH_SETTINGS,
CONN_AUTH_SECURITY,
RESOURCES,
RESOURCES_MEM,
RESOURCES_DISK,
RESOURCES_KERNEL,
RESOURCES_VACUUM_DELAY,
RESOURCES_BGWRITER,
RESOURCES_ASYNCHRONOUS,
WAL,
WAL_SETTINGS,
WAL_CHECKPOINTS,
WAL_ARCHIVING,
REPLICATION,
REPLICATION_SENDING,
REPLICATION_MASTER,
REPLICATION_STANDBY,
REPLICATION_SUBSCRIBERS,
QUERY_TUNING,
QUERY_TUNING_METHOD,
QUERY_TUNING_COST,
QUERY_TUNING_GEQO,
QUERY_TUNING_OTHER,
LOGGING,
LOGGING_WHERE,
LOGGING_WHEN,
LOGGING_WHAT,
PROCESS_TITLE,
STATS,
STATS_MONITORING,
STATS_COLLECTOR,
AUTOVACUUM,
CLIENT_CONN,
CLIENT_CONN_STATEMENT,
CLIENT_CONN_LOCALE,
CLIENT_CONN_PRELOAD,
CLIENT_CONN_OTHER,
LOCK_MANAGEMENT,
COMPAT_OPTIONS,
COMPAT_OPTIONS_PREVIOUS,
COMPAT_OPTIONS_CLIENT,
ERROR_HANDLING_OPTIONS,
PRESET_OPTIONS,
CUSTOM_OPTIONS,
DEVELOPER_OPTIONS
};

保存命令

1
alter system set military_sensitive_data = '0';

将会把参数刷出到磁盘文件postgresql.auto.conf

1
2
3
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.
military_sensitive_data = '0'

Guc变量分析

1
2
3
4
5
6
7
8
9
{
{"lc_numeric", PGC_USERSET, CLIENT_CONN_LOCALE,
gettext_noop("Sets the locale for formatting numbers."),
NULL
},
&locale_numeric,
"C",
check_locale_numeric, assign_locale_numeric, NULL
},
1
2
3
4
5
bool
check_locale_numeric(char **newval, void **extra, GucSource source)
{
return check_locale(LC_NUMERIC, *newval, NULL);
}
欣赏此文? 求鼓励,求支持!