数据库

 首页 > 数据库 > postgresql > PostgreSQL启动过程之加载GUC参数

PostgreSQL启动过程之加载GUC参数

分享到:
【字体:
导读:
         摘要:1先上个图,看一下函数调用过程梗概,中间细节有略GUC参数初始化分两步,第一步先读取buildin/compiled-in的GUC参数默认值,这里包括全部的GUC参数,建立GUC参数相关结构变量,第二步读取postgresql.conf配置文件中的参数设置之。从上图中能看出来,这个读...

PostgreSQL启动过程之加载GUC参数

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,0, 0, NULL, NULL}, NULL, false, NULL, NULL, 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."),

NULL,

GUC_UNIT_S

},

&XLogArchiveTimeout,

0,0, INT_MAX,

NULL,NULL, NULL

       },

……

       /*End-of-list marker */

       {

{NULL,0, 0, NULL, NULL}, NULL, 0, 0, 0, NULL, NULL, NULL

       }

};

 

 

static struct config_realConfigureNamesReal[] =

{

       {

{"seq_page_cost",PGC_USERSET, QUERY_TUNING_COST,

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

 "sequentially fetched disk page."),

NULL

},

&seq_page_cost,

DEFAULT_SEQ_PAGE_COST,0, DBL_MAX,

NULL,NULL, NULL

       },

       /*End-of-list marker */

       {

{NULL,0, 0, NULL, NULL}, NULL, 0.0, 0.0, 0.0, NULL, NULL, NULL

       }

};

 

 

static struct config_stringConfigureNamesString[] =

{

       {

{"archive_command",PGC_SIGHUP, WAL_ARCHIVING,

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

NULL

},

&XLogArchiveCommand,

"",

NULL,NULL, show_archive_command

       },

……

       /*End-of-list marker */

       {

{NULL,0, 0, NULL, NULL}, NULL, NULL, NULL, NULL, NULL

       }

};

 

 

static struct config_enumConfigureNamesEnum[] =

{

       {

{"backslash_quote",PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,

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

NULL

},

&backslash_quote,

BACKSLASH_QUOTE_SAFE_ENCODING,backslash_quote_options,

NULL,NULL, NULL

       },

……

       /*End-of-list marker */

       {

{NULL,0, 0, NULL, NULL}, NULL, 0, NULL, NULL, NULL, 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

PostgreSQL启动过程之加载GUC参数
分享到:
PostgreSQL数据库新版本中增加的特性
PostgreSQL数据库新版本中增加的特性 今天初略的看了一下POSTGRESQL9开发版的RELEASE NOTES,较以前的版本从功能上和性能上都有较大的提升,最明显的感觉是为DBA们增加的特性相当的多。 大概的改进如下: 1.继承表的权限问题一直是比较困扰的,增加一个分区难道还要重新赋权吗,改善后DBA非常开心。 2.字符处理将...
CentOS5系统编译安装Nginx/PHP/MySQL/pos...
CentOS5系统编译安装Nginx/PHP/MySQL/postgresql环境详解 虽然查看有关“网站”的文章">网站已经有很多编译安装nginx/php/mysql的教程,但是都是囫囵吞枣,只告诉人操作,不告诉别人为什么这么操作,让很多人看了一愣一愣的,只知道模仿。为让大家深入学习,才有了这篇《centos5上编译安装nginx/php/mysql详细教程》 ...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……