加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PostgreSQL启动过程中的那些事三:加载GUC参数

发布时间:2020-12-13 17:49:14 所属栏目:百科 来源:网络整理
导读:1先上个图,看一下函数调用过程梗概,中间细节有略 GUC参数初始化分两步,第一步先读取buildin/compiled-in的GUC参数默认值,这里包括全部的GUC参数,建立GUC参数相关结构变量,第二步读取postgresql.conf配置文件中的参数设置之。从上图中能看出来,这个读

1先上个图,看一下函数调用过程梗概,中间细节有略


GUC参数初始化分两步,第一步先读取buildin/compiled-in的GUC参数默认值,这里包括全部的GUC参数,建立GUC参数相关结构变量,第二步读取postgresql.conf配置文件中的参数设置之。从上图中能看出来,这个读取并设置postgresql.conf中参数的过程还是挺复杂的。


2初始化GUC相关数据结构并取hardcode/buildin的参数值。

pg里的GUC参数按设置的值分五种类型,分别是bool、int、real、string、enum,根据这五种类型,定义了五种结构类型,再根据这五种结构,每个类型建一个对应的静态数组,用于存储这些相应类型的GUC参数。这五种类型是config_bool、config_int、config_real、config_string、config_enum,对应的静态数组是ConfigureNamesBool、ConfigureNamesInt、ConfigureNamesReal、ConfigureNamesString、ConfigureNamesEnum。具体结构和数组定义见下面。

五个结构定义:

struct config_bool

{

struct config_generic gen;

/* these fields must be set correctly in initial value: */

/* (all but reset_val are constants) */

bool *variable;

bool boot_val;

GucIntCheckHookcheck_hook;

GucBoolAssignHookassign_hook;

GucShowHookshow_hook;

/* variable fields,initialized at runtime: */

bool reset_val;

void * reset_extra

};

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;

GucIntCheckHookcheck_hook;

GucIntAssignHookassign_hook;

GucShowHookshow_hook;

/* variable fields,initialized at runtime: */

int reset_val;

void * reset_extra

};

struct config_real

{

struct config_generic gen;

/* constantfields,must be set correctly in initial value: */

double *variable;

double boot_val;

double min;

double max;

GucIntCheckHookcheck_hook;

GucRealAssignHookassign_hook;

GucShowHookshow_hook;

/* variable fields,initialized at runtime: */

double reset_val;

void * reset_extra

};

struct config_string

{

struct config_generic gen;

/* constant fields,must beset correctly in initial value: */

char **variable;

const char *boot_val;

GucIntCheckHookcheck_hook;

GucStringAssignHookassign_hook;

GucShowHookshow_hook;

/* variable fields,initialized at runtime: */

char *reset_val;

void * reset_extra

};

struct config_enum

{

struct config_generic gen;

/* constant fields,must beset correctly in initial value: */

int *variable;

int boot_val;

GucIntCheckHookcheck_hook;

GucStringAssignHookassign_hook;

GucShowHookshow_hook;

/* variable fields,initialized at runtime: */

int reset_val;

void * reset_extra

};

和结构类型对应的五个静态数组:

static struct config_boolConfigureNamesBool[] =

{

{

{"enable_seqscan",PGC_USERSET,QUERY_TUNING_METHOD,

gettext_noop("Enablesthe planner's use of sequential-scan plans."),

NULL

},

&enable_seqscan,

true,

NULL,NULL,NULL

},

……

/*End-of-list marker */

{

{NULL,NULL},false,NULL

}

};

static struct config_int ConfigureNamesInt[]=

{

{

{"archive_timeout",PGC_SIGHUP,WAL_ARCHIVING,

gettext_noop("Forcesa switch to the next xlog file if a "

"new file has not been started within Nseconds."),

GUC_UNIT_S

},

&XLogArchiveTimeout,

0,INT_MAX,NULL

}

};

static struct config_realConfigureNamesReal[] =

{

{

{"seq_page_cost",QUERY_TUNING_COST,

gettext_noop("Setsthe planner's estimate of the cost of a "

"sequentially fetched disk page."),

&seq_page_cost,

DEFAULT_SEQ_PAGE_COST,DBL_MAX,

/*End-of-list marker */

{

{NULL,0.0,NULL

}

};

static struct config_stringConfigureNamesString[] =

{

{

{"archive_command",

gettext_noop("Setsthe shell command that will be called to archive a WAL file."),

&XLogArchiveCommand,

"",show_archive_command

},NULL

}

};

static struct config_enumConfigureNamesEnum[] =

{

{

{"backslash_quote",COMPAT_OPTIONS_PREVIOUS,

gettext_noop("Setswhether "'" is allowed in string literals."),

&backslash_quote,

BACKSLASH_QUOTE_SAFE_ENCODING,backslash_quote_options,NULL

}

};

上面五个结构定义中,每个结构的第一个成员变量都是一个config_generic结构的gen成员,下面是config_generic的结构定义:

struct config_generic

{

/* constantfields,must be set correctly in initial value: */

const char *name; /* name of variable - MUST BE FIRST */

GucContext context; /* context required to set the variable */

enumconfig_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 below*/

/* variablefields,initialized at runtime: */

enumconfig_type vartype; /* type of variable (set only at startup) */

int status; /* status bits,see below*/

GucSource reset_source; /* source of thereset_value */

GucSource source; /*source of the current actual value */

GucStack*stack; /* stacked outside-of-transaction states */

void *extra; /*"extra" pointer for current actual value */

char *sourcefile; /* filecurrent setting is from (NULL if not

* file) */

int sourceline; /* line in source file */

};

然后,定义一个config_generic **类型的静态变量数组guc_variables,再计算参数总数,所有参数以config_generic*类型计算所需内存空间,冗余25%内存后malloc分配内存空间。把guc_variables每一个元素指向ConfigureNamesBool、ConfigureNamesInt、ConfigureNamesReal、ConfigureNamesString、ConfigureNamesEnum这五个数组的config_generic类型成员gen的地址,然后按照参数名称把所有元素做了快速排序。这个过程中还设置了一些GUC参数的默认值。

static struct config_generic **guc_variables;

后面查询GUC参数都是在guc_variables这个已排序的数组里找。这样GUC参数的数据结构就搭建完成了,下面看看GUC参数相关的数据结构图吧。

先把涉及到的结构的图分别列出,再画个这些结构的组织关系示意图。




3加载postgresql.conf参数配置文件里的参数设置

从main->PostmasterMain->SelectConfigFiles->ProcessConfigFile开始处理参数配置文件postgresql.conf,读取postgresql.conf配置文件的调用过程是ProcessConfigFile -> ParseConfigFile -> AllocateFile->fopen,最后用fopen打开文件,其中ProcessConfigFile、ParseConfigFile在文件srcbackendutilsmiscguc-file.l中,AllocateFile在文件srcbackendstoragefilefd.c中。

pg使用 flex 去处理 conf 文件。在ParseConfigFile中把配置文件中的配置项组织成一个链表,调用set_config_option检查这些值是否有效,若可以设置就调用set_config_option设置这些值。

这里以"max_connections"做例子,从配置文件读取"max_connections",然后从guc_variables数组中找元素" max_connections ",比较参数结构的GucContext (枚举类参数能被设置的时机。定义见下面)枚举类型成员context和当前时间,看是否可以此刻修改。接着比较参数结构的GucSource(枚举了当前GUC参数设置的来源。除非参数新值的来源等级不小于原参数的来源等级时,新设置才能生效。例如,修改配置文件不能覆盖postmaster command line的设置。定义见下面)枚举类型成员source和新参数值的来源,看是否可以修改。如果可以,把config_generic结构类型的元素" max_connections "类型转换为config_int类型,修改variable成员为新值,修改该参数的来源source为当前来源PGC_S_FILE,如果元素" max_connections "的reset_source <= source,修改reset_val成员为新值,修改该参数的reset_source为当前来源PGC_S_FILE。

typedef enum

{

PGC_INTERNAL,

PGC_POSTMASTER,

PGC_SIGHUP,

PGC_BACKEND,

PGC_SUSET,

PGC_USERSET

} GucContext;

typedef enum

{

PGC_S_DEFAULT,/*wired-in default */

PGC_S_ENV_VAR,/*postmaster environment variable */

PGC_S_FILE,/*postgresql.conf */

PGC_S_ARGV,/*postmaster command line */

PGC_S_DATABASE,/*per-database setting */

PGC_S_USER,/*per-user setting */

PGC_S_CLIENT,/* fromclient connection request */

PGC_S_OVERRIDE,/*special case to forcibly set default */

PGC_S_INTERACTIVE,/* dividingline for error reporting */

PGC_S_TEST,/*test per-database or per-user setting */

PGC_S_SESSION /* SETcommand */

} GucSource

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读