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

前端之家收集整理的这篇文章主要介绍了PostgreSQL启动过程中的那些事三:加载GUC参数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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


@H_404_3@

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


@H_404_3@

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

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

@H_404_3@

五个结构定义:@H_404_3@

struct config_bool@H_404_3@

{@H_404_3@

struct config_generic gen;@H_404_3@

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

/* (all but reset_val are constants) */@H_404_3@

bool *variable;@H_404_3@

bool boot_val;@H_404_3@

GucIntCheckHookcheck_hook;@H_404_3@

GucBoolAssignHookassign_hook;@H_404_3@

GucShowHookshow_hook;@H_404_3@

/* variable fields,initialized at runtime: */@H_404_3@

bool reset_val;@H_404_3@

void * reset_extra@H_404_3@

};@H_404_3@

@H_404_3@

struct config_int@H_404_3@

{@H_404_3@

struct config_generic gen;@H_404_3@

/*constant fields,must be set correctly in initial value: */@H_404_3@

int *variable;@H_404_3@

int boot_val;@H_404_3@

int min;@H_404_3@

int max;@H_404_3@

GucIntCheckHookcheck_hook;@H_404_3@

GucIntAssignHookassign_hook;@H_404_3@

GucShowHookshow_hook;@H_404_3@

/* variable fields,initialized at runtime: */@H_404_3@

int reset_val;@H_404_3@

void * reset_extra@H_404_3@

};@H_404_3@

@H_404_3@

struct config_real@H_404_3@

{@H_404_3@

struct config_generic gen;@H_404_3@

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

double *variable;@H_404_3@

double boot_val;@H_404_3@

double min;@H_404_3@

double max;@H_404_3@

GucIntCheckHookcheck_hook;@H_404_3@

GucRealAssignHookassign_hook;@H_404_3@

GucShowHookshow_hook;@H_404_3@

/* variable fields,initialized at runtime: */@H_404_3@

double reset_val;@H_404_3@

void * reset_extra@H_404_3@

};@H_404_3@

@H_404_3@

struct config_string@H_404_3@

{@H_404_3@

struct config_generic gen;@H_404_3@

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

char **variable;@H_404_3@

const char *boot_val;@H_404_3@

GucIntCheckHookcheck_hook;@H_404_3@

GucStringAssignHookassign_hook;@H_404_3@

GucShowHookshow_hook;@H_404_3@

/* variable fields,initialized at runtime: */@H_404_3@

char *reset_val;@H_404_3@

void * reset_extra@H_404_3@

};@H_404_3@

struct config_enum@H_404_3@

{@H_404_3@

struct config_generic gen;@H_404_3@

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

int *variable;@H_404_3@

int boot_val;@H_404_3@

GucIntCheckHookcheck_hook;@H_404_3@

GucStringAssignHookassign_hook;@H_404_3@

GucShowHookshow_hook;@H_404_3@

/* variable fields,initialized at runtime: */@H_404_3@

int reset_val;@H_404_3@

void * reset_extra@H_404_3@

};@H_404_3@

@H_404_3@

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

static struct config_boolConfigureNamesBool[] =@H_404_3@

{@H_404_3@

{@H_404_3@

{"enable_seqscan",PGC_USERSET,QUERY_TUNING_METHOD,@H_404_3@

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

NULL@H_404_3@

},@H_404_3@

&enable_seqscan,@H_404_3@

true,@H_404_3@

NULL,NULL,NULL@H_404_3@

},@H_404_3@

……@H_404_3@

/*End-of-list marker */@H_404_3@

{@H_404_3@

{NULL,NULL},false,NULL@H_404_3@

}@H_404_3@

};@H_404_3@

@H_404_3@

static struct config_int ConfigureNamesInt[]=@H_404_3@

{@H_404_3@

{@H_404_3@

{"archive_timeout",PGC_SIGHUP,WAL_ARCHIVING,@H_404_3@

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

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

GUC_UNIT_S@H_404_3@

},@H_404_3@

&XLogArchiveTimeout,@H_404_3@

0,INT_MAX,NULL@H_404_3@

}@H_404_3@

};@H_404_3@

@H_404_3@

@H_404_3@

static struct config_realConfigureNamesReal[] =@H_404_3@

{@H_404_3@

{@H_404_3@

{"seq_page_cost",QUERY_TUNING_COST,@H_404_3@

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

"sequentially fetched disk page."),@H_404_3@

&seq_page_cost,@H_404_3@

DEFAULT_SEQ_PAGE_COST,DBL_MAX,@H_404_3@

/*End-of-list marker */@H_404_3@

{@H_404_3@

{NULL,0.0,NULL@H_404_3@

}@H_404_3@

};@H_404_3@

@H_404_3@

@H_404_3@

static struct config_stringConfigureNamesString[] =@H_404_3@

{@H_404_3@

{@H_404_3@

{"archive_command",@H_404_3@

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

&XLogArchiveCommand,@H_404_3@

"",show_archive_command@H_404_3@

},NULL@H_404_3@

}@H_404_3@

};@H_404_3@

@H_404_3@

@H_404_3@

static struct config_enumConfigureNamesEnum[] =@H_404_3@

{@H_404_3@

{@H_404_3@

{"backslash_quote",COMPAT_OPTIONS_PREVIoUS,@H_404_3@

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

&backslash_quote,@H_404_3@

BACKSLASH_QUOTE_SAFE_ENCODING,backslash_quote_options,NULL@H_404_3@

}@H_404_3@

};@H_404_3@

@H_404_3@

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

struct config_generic@H_404_3@

{@H_404_3@

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

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

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

enumconfig_group group; /* to help organize variables by function */@H_404_3@

const char *short_desc; /* short desc. of this variable's purpose */@H_404_3@

const char *long_desc; /* long desc. of this variable's purpose */@H_404_3@

int flags; /* flag bits,see below*/@H_404_3@

/* variablefields,initialized at runtime: */@H_404_3@

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

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

GucSource reset_source; /* source of thereset_value */@H_404_3@

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

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

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

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

* file) */@H_404_3@

int sourceline; /* line in source file */@H_404_3@

};@H_404_3@

@H_404_3@

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

static struct config_generic **guc_variables;@H_404_3@

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

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


@H_404_3@


@H_404_3@


@H_404_3@

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

从main->PostmasterMain->SelectConfigFiles->ProcessConfigFile开始处理参数配置文件postgresql.conf,读取postgresql.conf配置文件调用过程是ProcessConfigFile -> ParseConfigFile -> AllocateFile->fopen,最后用fopen打开文件,其中ProcessConfigFile、ParseConfigFile在文件src\backend\utils\misc\guc-file.l中,AllocateFile在文件src\backend\storage\file\fd.c中。@H_404_3@

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

@H_404_3@

这里以"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。@H_404_3@

@H_404_3@

typedef enum@H_404_3@

{@H_404_3@

PGC_INTERNAL,@H_404_3@

PGC_POSTMASTER,@H_404_3@

PGC_SIGHUP,@H_404_3@

PGC_BACKEND,@H_404_3@

PGC_SUSET,@H_404_3@

PGC_USERSET@H_404_3@

} GucContext;@H_404_3@

@H_404_3@

typedef enum@H_404_3@

{@H_404_3@

PGC_S_DEFAULT,/*wired-in default */@H_404_3@

PGC_S_ENV_VAR,/*postmaster environment variable */@H_404_3@

PGC_S_FILE,/*postgresql.conf */@H_404_3@

PGC_S_ARGV,/*postmaster command line */@H_404_3@

PGC_S_DATABASE,/*per-database setting */@H_404_3@

PGC_S_USER,/*per-user setting */@H_404_3@

PGC_S_CLIENT,/* fromclient connection request */@H_404_3@

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

PGC_S_INTERACTIVE,/* dividingline for error reporting */@H_404_3@

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

PGC_S_SESSION /* SETcommand */@H_404_3@

} GucSource@H_404_3@

猜你在找的Postgre SQL相关文章