文章目录
- 加载参数整体流程
- 参数结构
- 举例:ConfigureNamesBool
- 初始化参数 InitializeGUCOptions
- build_guc_variables
- InitializeOneGUCOption
- InitializeGUCOptionsFromEnvironment
- 命令行添加
- SelectConfigFiles配置
加载参数整体流程
我们先看下guc参数是如何管理的。
首先初始化GUC选项,将其设置为默认值;然后读取命令行配置,最后读取配置文件postgresql.conf中的配置项。
参数结构
有5类参数:ConfigureNamesBool、ConfigureNamesInt、ConfigureNamesReal、ConfigureNamesReal、ConfigureNamesEnum。
在guc_tables中定义了不同类型的参数清单:
struct config_bool ConfigureNamesBool[] (guc_tables.c:771)
struct config_int ConfigureNamesInt[] (guc_tables.c:2056)
struct config_real ConfigureNamesReal[] (guc_tables.c:3678)
struct config_string ConfigureNamesString[] (guc_tables.c:3959)
struct config_enum ConfigureNamesEnum[] (guc_tables.c:4736)
不同的参数类型结构体定义在guc_tables.h
struct config_generic (guc_tables.h:153) //被用于下面几种类型参数的成员。
struct config_bool (guc_tables.h:198)
struct config_int (guc_tables.h:212)
struct config_real (guc_tables.h:228)
struct config_string (guc_tables.h:254)
struct config_enum (guc_tables.h:268)
在guc_tables.h中定义了参数类型如下:guc_tables.h:21
enum config_type
{PGC_BOOL,PGC_INT,PGC_REAL,PGC_STRING,PGC_ENUM,
};
在guc_tables.c中定义了参数类型对应的值如下:guc_tables.c:731
const char *const config_type_names[] =
{[PGC_BOOL] = "bool",[PGC_INT] = "integer",[PGC_REAL] = "real",[PGC_STRING] = "string",[PGC_ENUM] = "enum",
};
举例:ConfigureNamesBool
ConfigureNamesBool
参数组,每一个成员都是一个结构体config_bool
struct config_bool ConfigureNamesBool[] =
{{{"backtrace_on_internal_error", PGC_SUSET, DEVELOPER_OPTIONS,gettext_noop("Log backtrace for any error with error code XX000 (internal error)."),NULL,GUC_NOT_IN_SAMPLE},&backtrace_on_internal_error,false,NULL, NULL, NULL},
结构体config_bool
的定义如下:
struct config_bool
{struct config_generic gen; //包含的config_generic结构体/* 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;
};
结构体config_generic
的定义如下:
struct config_generic
{/* constant fields, must be set correctly in initial value: */const char *name; /* name of variable - MUST BE FIRST */GucContext context; /* context required to set the variable */enum config_group group; /* to help organize variables by function */const char *short_desc; /* short desc. of this variable's purpose */const char *long_desc; /* long desc. of this variable's purpose */int flags; /* flag bits, see guc.h *//* variable fields, initialized at runtime: */enum config_type vartype; /* type of variable (set only at startup) */int status; /* status bits, see below */GucSource source; /* source of the current actual value */GucSource reset_source; /* source of the reset_value */GucContext scontext; /* context that set the current value */GucContext reset_scontext; /* context that set the reset value */Oid srole; /* role that set the current value */Oid reset_srole; /* role that set the reset value */GucStack *stack; /* stacked prior values */void *extra; /* "extra" pointer for current actual value */dlist_node nondef_link; /* list link for variables that have source* different from PGC_S_DEFAULT */slist_node stack_link; /* list link for variables that have non-NULL* stack */slist_node report_link; /* list link for variables that have the* GUC_NEEDS_REPORT bit set in status */char *last_reported; /* if variable is GUC_REPORT, value last sent* to client (NULL if not yet sent) */char *sourcefile; /* file current setting is from (NULL if not* set in config file) */int sourceline; /* line in source file */
};
初始化参数 InitializeGUCOptions
InitializeGUCOptionsbuild_guc_variables();InitializeOneGUCOption(guc_variables[i]);InitializeGUCOptionsFromEnvironment
build_guc_variables
完成空间申请与初始化,
- 会通过设定的参数组ConfigureNamesxxx中的vartype值;
- 将所有的参数保存在guc_hashtab全局hash桶内
- 初始化所有的参数组的vartype为对应的config_type,
struct config_bool *conf = &ConfigureNamesBool[i];/* Rather than requiring vartype to be filled in by hand, do this: */conf->gen.vartype = PGC_BOOL;num_vars++;
- 创建hash结构
// (guc.c:976)
guc_hashtab = hash_create("GUC hash table",size_vars,&hash_ctl,HASH_ELEM | HASH_FUNCTION | HASH_COMPARE | HASH_CONTEXT);
其中的guc_hashtab为static的hash结构(guc.c:241):
static HTAB *guc_hashtab; /* entries are GUCHashEntrys */...// HTAB定义如下:
/** Top control structure for a hashtable --- in a shared table, each backend* has its own copy (OK since no fields change at runtime)*/
struct HTAB
{HASHHDR *hctl; /* => shared control information */HASHSEGMENT *dir; /* directory of segment starts */HashValueFunc hash; /* hash function */HashCompareFunc match; /* key comparison function */HashCopyFunc keycopy; /* key copying function */HashAllocFunc alloc; /* memory allocator */MemoryContext hcxt; /* memory context if default allocator used */char *tabname; /* table name (for error messages) */bool isshared; /* true if table is in shared memory */bool isfixed; /* if true, don't enlarge *//* freezing a shared table isn't allowed, so we can keep state here */bool frozen; /* true = no more inserts allowed *//* We keep local copies of these fixed values to reduce contention */Size keysize; /* hash key length in bytes */long ssize; /* segment size --- must be power of 2 */int sshift; /* segment shift = log2(ssize) */
};
- 将参数加入guc_hashtable
struct config_generic *gucvar = &ConfigureNamesBool[i].gen;//这一步会从guc_hashtab(静态结构的hash表),中查找参数名(gucvar->name),没有的话就会新建一个返回给hentryhentry = (GUCHashEntry *) hash_search(guc_hashtab,&gucvar->name,HASH_ENTER,&found);Assert(!found);//初始化添加时一定找不到参数名的,那么就会新建一个桶,同时把桶赋值gucvar即 &ConfigureNamesBool[i].gen;hentry->gucvar = gucvar;
其中,
结构体GUCHashEntry
的定义链如下:
typedef struct
{const char *gucname; /* hash key */struct config_generic *gucvar; /* -> GUC's defining structure */
} GUCHashEntry;
hash_search(dynahash.c:955)函数定义如下,其直接套用下层hash_search_with_hash_value函数
void *
hash_search(HTAB *hashp,const void *keyPtr,HASHACTION action,bool *foundPtr)
{return hash_search_with_hash_value(hashp,keyPtr,hashp->hash(keyPtr, hashp->keysize),action,foundPtr);
}
hash_search_with_hash_value(dynahash.c:968)函数大致作用是:
在Bucket中一个个找,用currBucket判断是否为null来确认是非存在对应的桶
// 在Bucket中一个个找,用currBucket判断是否为null来确认是非存在对应的桶while (currBucket != NULL){if (currBucket->hashvalue == hashvalue &&match(ELEMENTKEY(currBucket), keyPtr, keysize) == 0)break;prevBucketPtr = &(currBucket->link);currBucket = *prevBucketPtr;
#ifdef HASH_STATISTICShash_collisions++;hctl->collisions++;
#endif}
针对不同的hashaction有不同的动作
// hashaction的定义如下(hsearch.h:110)
/* hash_search operations */
typedef enum
{HASH_FIND,HASH_ENTER,HASH_REMOVE,HASH_ENTER_NULL,
} HASHACTION;* action is one of:* HASH_FIND: look up key in table* HASH_ENTER: look up key in table, creating entry if not present* HASH_ENTER_NULL: same, but return NULL if out of memory* HASH_REMOVE: look up key in table, remove entry if present... ...
// (dynahash.c:1037)switch (action){case HASH_FIND:... case HASH_ENTER:case HASH_ENTER_NULL:/* Return existing element if found, else create one */if (currBucket != NULL)return (void *) ELEMENTKEY(currBucket);/* disallow inserts if frozen */if (hashp->frozen)elog(ERROR, "cannot insert into frozen hashtable \"%s\"",hashp->tabname);//重新申请一个hash bucketcurrBucket = get_hash_entry(hashp, freelist_idx);if (currBucket == NULL){/* out of memory */if (action == HASH_ENTER_NULL)return NULL;/* report a generic message */if (hashp->isshared)ereport(ERROR,(errcode(ERRCODE_OUT_OF_MEMORY),errmsg("out of shared memory")));elseereport(ERROR,(errcode(ERRCODE_OUT_OF_MEMORY),errmsg("out of memory")));}/* link into hashbucket chain */*prevBucketPtr = currBucket;currBucket->link = NULL;/* copy key into record */currBucket->hashvalue = hashvalue;hashp->keycopy(ELEMENTKEY(currBucket), keyPtr, keysize);/** Caller is expected to fill the data field on return. DO NOT* insert any code that could possibly throw error here, as doing* so would leave the table entry incomplete and hence corrupt the* caller's data structure.*/return (void *) ELEMENTKEY(currBucket);}
在初始化参数是的堆栈如下:其中action是“HASH_ENTER”。
#0 hash_search_with_hash_value (hashp=0x1023f80, keyPtr=0xfb4c60 <ConfigureNamesBool>, hashvalue=1010527281, action=HASH_ENTER, foundPtr=0x7fffffffe05f) at dynahash.c:975
#1 0x0000000000bc1901 in hash_search (hashp=0x1023f80, keyPtr=0xfb4c60 <ConfigureNamesBool>, action=HASH_ENTER, foundPtr=0x7fffffffe05f) at dynahash.c:960
#2 0x0000000000bcd413 in build_guc_variables () at guc.c:985
#3 0x0000000000bce451 in InitializeGUCOptions () at guc.c:1546
#4 0x0000000000911a54 in PostmasterMain (argc=1, argv=0x1000ef0) at postmaster.c:585
#5 0x00000000007d3f97 in main (argc=1, argv=0x1000ef0) at main.c:197
如上所示,会使用get_hash_entry(dynahash.c:1256)
重新创建一个bucket存储某一个参数。存储类型为GUCHashEntry。
InitializeOneGUCOption
代码栈如下:
Breakpoint 1, InitializeOneGUCOption (gconf=0xfc39c8 <ConfigureNamesString+3400>) at guc.c:1648
1648 gconf->status = 0;
(gdb) bt
#0 InitializeOneGUCOption (gconf=0xfc39c8 <ConfigureNamesString+3400>) at guc.c:1648
#1 0x0000000000bce4a4 in InitializeGUCOptions () at guc.c:1558
#2 0x0000000000911a54 in PostmasterMain (argc=3, argv=0x1000ef0) at postmaster.c:585
#3 0x00000000007d3f97 in main (argc=3, argv=0x1000ef0) at main.c:197
其中
InitializeOneGUCOption函数的入参gconf来源如下,为GUCHashEntry->config_generic:
hash_seq_init(&status, guc_hashtab);while ((hentry = (GUCHashEntry *) hash_seq_search(&status)) != NULL){/* Check mapping between initial and default value */Assert(check_GUC_init(hentry->gucvar));InitializeOneGUCOption(hentry->gucvar);}
即此时通过全局静态变量guc_hashtab来一个个配置参数。配置过程调用InitializeOneGUCOption函数,具体步骤如下:
- 第一步:填充统一的默认值:
gconf->status = 0;gconf->source = PGC_S_DEFAULT;gconf->reset_source = PGC_S_DEFAULT;gconf->scontext = PGC_INTERNAL;gconf->reset_scontext = PGC_INTERNAL;gconf->srole = BOOTSTRAP_SUPERUSERID;gconf->reset_srole = BOOTSTRAP_SUPERUSERID;gconf->stack = NULL;gconf->extra = NULL;gconf->last_reported = NULL;gconf->sourcefile = NULL;gconf->sourceline = 0;
- 第二部,初始化参数值
switch (gconf->vartype){case PGC_BOOL:{struct config_bool *conf = (struct config_bool *) gconf;bool newval = conf->boot_val;void *extra = NULL;if (!call_bool_check_hook(conf, &newval, &extra,PGC_S_DEFAULT, LOG))elog(FATAL, "failed to initialize %s to %d",conf->gen.name, (int) newval);if (conf->assign_hook)conf->assign_hook(newval, extra);*conf->variable = conf->reset_val = newval;conf->gen.extra = conf->reset_extra = extra;break;}case PGC_INT:{......
此时newval值的来源在哪里?
查看此时的conf变量,发现它的初始值中就conf->boot_val就是1!!
(gdb) p *conf
$15 = {gen = {name = 0xe2dc71 "extra_float_digits", context = PGC_USERSET, group = CLIENT_CONN_LOCALE, short_desc = 0xe2dc88 "Sets the number of digits displayed for floating-point values.", long_desc = 0xe2dcc8 "This affects real, double precision, and geometric data types. A zero or negative parameter value is added to the standard number of digits (FLT_DIG or DBL_DIG as appropriate). Any value greater than "..., flags = 0, vartype = PGC_INT, status = 0, source = PGC_S_DEFAULT, reset_source = PGC_S_DEFAULT, scontext = PGC_INTERNAL, reset_scontext = PGC_INTERNAL, srole = 10, reset_srole = 10, stack = 0x0, extra = 0x0, nondef_link = {prev = 0x0, next = 0x0}, stack_link = {next = 0x0}, report_link = {next = 0x0}, last_reported = 0x0, sourcefile = 0x0, sourceline = 0}, variable = 0xfb49d8 <extra_float_digits>, boot_val = 1, min = -15, max = 3, check_hook = 0x0, assign_hook = 0x0, show_hook = 0x0, reset_val = 0, reset_extra = 0x0}
然而此时的conf 来源与gconf,而gconf来源于静态变量guc_hashtab。其中保存的是struct config_int ConfigureNamesInt[]的地址。
查看config_int ConfigureNamesInt[];发现:
{{"extra_float_digits", PGC_USERSET, CLIENT_CONN_LOCALE,gettext_noop("Sets the number of digits displayed for floating-point values."),gettext_noop("This affects real, double precision, and geometric data types. ""A zero or negative parameter value is added to the standard ""number of digits (FLT_DIG or DBL_DIG as appropriate). ""Any value greater than zero selects precise output mode.")},&extra_float_digits,1, -15, 3,NULL, NULL, NULL},
已经默认配置了初始值。见上面的“1,-15,3”分别对应指针conf的“boot_val,min,max”;
同时观察整个ConfigureNamesInt数组,发现初始值也有一些宏,如下
{/* Can't be set in postgresql.conf */{"server_version_num", PGC_INTERNAL, PRESET_OPTIONS,gettext_noop("Shows the server version as an integer."),NULL,GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE},&server_version_num,PG_VERSION_NUM, PG_VERSION_NUM, PG_VERSION_NUM,NULL, NULL, NULL},
即通过静态参数组中的boot_val属性为每个参数赋值。
InitializeGUCOptionsFromEnvironment
堆栈如下:
Breakpoint 1, InitializeGUCOptionsFromEnvironment () at guc.c:1596
warning: Source file is more recent than executable.
1596 env = getenv("PGPORT");
(gdb) bt
#0 InitializeGUCOptionsFromEnvironment () at guc.c:1596
#1 0x0000000000bce512 in InitializeGUCOptions () at guc.c:1578
#2 0x0000000000911a54 in PostmasterMain (argc=3, argv=0x1000ef0) at postmaster.c:585
#3 0x00000000007d3f97 in main (argc=3, argv=0x1000ef0) at main.c:197
其中主要调用SetConfigOption(guc.c:4275)来配置PGPORT、PGDATESTYLE、PGCLIENTENCODING这些linuc的宏(环境变量),确认postgresql中的port、datestyle、client_encoding等参数配置,如下图:
env = getenv("PGPORT");if (env != NULL)SetConfigOption("port", env, PGC_POSTMASTER, PGC_S_ENV_VAR);env = getenv("PGDATESTYLE");if (env != NULL)SetConfigOption("datestyle", env, PGC_POSTMASTER, PGC_S_ENV_VAR);env = getenv("PGCLIENTENCODING");if (env != NULL)SetConfigOption("client_encoding", env, PGC_POSTMASTER, PGC_S_ENV_VAR);
其中SetConfigOption(guc.c:4275)
void
SetConfigOption(const char *name, const char *value,GucContext context, GucSource source)
{(void) set_config_option(name, value, context, source,GUC_ACTION_SET, true, 0, false);
}
SetConfigOption嵌套set_config_option(guc.c:3333)函数如下:
int
set_config_option(const char *name, const char *value,GucContext context, GucSource source,GucAction action, bool changeVal, int elevel,bool is_reload)
{// bt如下
#0 set_config_with_handle (name=0xe2460e "transaction_isolation", handle=0x0, value=0xe245ff "read committed", context=PGC_POSTMASTER, source=PGC_S_OVERRIDE, srole=10, action=GUC_ACTION_SET, changeVal=true, elevel=0, is_reload=false) at guc.c:3401
#1 0x0000000000bd16fe in set_config_option (name=0xe2460e "transaction_isolation", value=0xe245ff "read committed", context=PGC_POSTMASTER, source=PGC_S_OVERRIDE, action=GUC_ACTION_SET, changeVal=true, elevel=0, is_reload=false) at guc.c:3351
#2 0x0000000000bd33a0 in SetConfigOption (name=0xe2460e "transaction_isolation", value=0xe245ff "read committed", context=PGC_POSTMASTER, source=PGC_S_OVERRIDE) at guc.c:4278
#3 0x0000000000bce4db in InitializeGUCOptions () at guc.c:1567
#4 0x0000000000911a54 in PostmasterMain (argc=3, argv=0x1000ef0) at postmaster.c:585
#5 0x00000000007d3f97 in main (argc=3, argv=0x1000ef0) at main.c:197
其也是通过将guc_hashtab全局hash表内的值拿出来,经过一系列对比之后进行赋值。
命令行添加
// postmaster.c:594while ((opt = getopt(argc, argv, "B:bC:c:D:d:EeFf:h:ijk:lN:OPp:r:S:sTt:W:-:")) != -1){switch (opt)case 'B':SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);break;... ...
这里面会对不同的参数做设置:
同时也是调用的SetConfigOption(guc.c:4275)完成重写
SelectConfigFiles配置
调用堆栈如下:
(gdb) b SelectConfigFiles
Breakpoint 2 at 0xbcedd2: file guc.c, line 1795.
(gdb) r
Starting program: /home/db_postg/build/bin/postgres -D /home/db_postg/data
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".Breakpoint 2, SelectConfigFiles (userDoption=0x102bff0 "/home/db_postg/data", progname=0x10002a0 "postgres") at guc.c:1795
1795 if (userDoption)
(gdb) b read
Breakpoint 3 at 0x7ffff7d5bc10 (2 locations)
(gdb) b open
Breakpoint 4 at 0x7ffff7d5b920 (2 locations)
(gdb) c
Continuing.
Breakpoint 4, 0x00007ffff7d5b920 in open64 () from /lib64/libc.so.6
(gdb) bt
#0 0x00007ffff7d5b920 in open64 () from /lib64/libc.so.6
#1 0x00007ffff7ceb956 in _IO_file_open () from /lib64/libc.so.6
#2 0x00007ffff7cebb2a in __GI__IO_file_fopen () from /lib64/libc.so.6
#3 0x00007ffff7cdf41d in __fopen_internal () from /lib64/libc.so.6
#4 0x00000000009a72b3 in AllocateFile (name=0x102c130 "/home/db_postg/data/postgresql.conf", mode=0xe264d2 "r") at fd.c:2601
#5 0x0000000000bd947e in ParseConfigFile (config_file=0x10228b8 "/home/db_postg/data/postgresql.conf", strict=true, calling_file=0x0, calling_lineno=0, depth=0, elevel=15, head_p=0x7fffffffdfb8, tail_p=0x7fffffffdfb0)at guc-file.l:238
#6 0x0000000000bcc202 in ProcessConfigFileInternal (context=PGC_POSTMASTER, applySettings=true, elevel=15) at guc.c:299
#7 0x0000000000bd926d in ProcessConfigFile (context=PGC_POSTMASTER) at guc-file.l:152
#8 0x0000000000bcef97 in SelectConfigFiles (userDoption=0x102bff0 "/home/db_postg/data", progname=0x10002a0 "postgres") at guc.c:1864
#9 0x0000000000911f78 in PostmasterMain (argc=3, argv=0x1000ef0) at postmaster.c:768
#10 0x00000000007d3f97 in main (argc=3, argv=0x1000ef0) at main.c:197
(gdb) c
Continuing.Breakpoint 3, 0x00007ffff7d5bc10 in read () from /lib64/libc.so.6
(gdb) bt
#0 0x00007ffff7d5bc10 in read () from /lib64/libc.so.6
#1 0x00007ffff7ceb118 in __GI__IO_file_xsgetn () from /lib64/libc.so.6
#2 0x00007ffff7cdf983 in fread () from /lib64/libc.so.6
#3 0x0000000000bd8258 in yy_get_next_buffer () at guc-file.c:1369
#4 0x0000000000bd7d38 in GUC_yylex () at guc-file.c:1209
#5 0x0000000000bd9d22 in ParseConfigFp (fp=0x102e040, config_file=0x102c130 "/home/db_postg/data/postgresql.conf", depth=0, elevel=15, head_p=0x7fffffffdfb8, tail_p=0x7fffffffdfb0) at guc-file.l:388
#6 0x0000000000bd956e in ParseConfigFile (config_file=0x10228b8 "/home/db_postg/data/postgresql.conf", strict=true, calling_file=0x0, calling_lineno=0, depth=0, elevel=15, head_p=0x7fffffffdfb8, tail_p=0x7fffffffdfb0)at guc-file.l:262
#7 0x0000000000bcc202 in ProcessConfigFileInternal (context=PGC_POSTMASTER, applySettings=true, elevel=15) at guc.c:299
#8 0x0000000000bd926d in ProcessConfigFile (context=PGC_POSTMASTER) at guc-file.l:152
#9 0x0000000000bcef97 in SelectConfigFiles (userDoption=0x102bff0 "/home/db_postg/data", progname=0x10002a0 "postgres") at guc.c:1864
#10 0x0000000000911f78 in PostmasterMain (argc=3, argv=0x1000ef0) at postmaster.c:768
#11 0x00000000007d3f97 in main (argc=3, argv=0x1000ef0) at main.c:197
其中的主要动作有:
1、生成配置文件路径并打开:
// SelectConfigFiles at guc.c:1826...if (userDoption)configdir = make_absolute_path(userDoption);elseconfigdir = make_absolute_path(getenv("PGDATA"));/*(gdb) p configdir$5 = 0x102c010 "/home/db_postg/data"*/... else if (configdir){fname = guc_malloc(FATAL,strlen(configdir) + strlen(CONFIG_FILENAME) + 2);sprintf(fname, "%s/%s", configdir, CONFIG_FILENAME);fname_is_malloced = false;}/*(gdb) p fname$4 = 0x1022868 "/home/db_postg/data/postgresql.conf"*/
2、配置数据目录参数和配置文件参数
SetConfigOption("config_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);... ...//第一次读取配置文件,只是为了设置data目录。ProcessConfigFile(PGC_POSTMASTER);...data_directory_rec = (struct config_string *)find_option("data_directory", false, false, PANIC);if (*data_directory_rec->variable)SetDataDir(*data_directory_rec->variable);else if (configdir)SetDataDir(configdir);... ...SetConfigOption("data_directory", DataDir, PGC_POSTMASTER, PGC_S_OVERRIDE);
3、配置其余参数
ProcessConfigFile(PGC_POSTMASTER);
...
SetConfigOption("hba_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
...
SetConfigOption("ident_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);