PostgreSQL数据库集簇初始化——initdb初始化数据库(命令行参数处理)

  template1和template0数据库用于创建数据库。PG中采用从模板数据库复制的方法来创建新的数据库,在创建数据库的命令中可以用-T选项来指定以哪个数据库为模板来创建新数据库。template1数据库是创建数据库命令默认的模板,也就是说通过不带-T选项的命令创建的用户数据库和template1一模一样。template1是可以修改的,如果对tempalte1进行了修改,那么在修改之后创建的用户数据库中也能体现出这些修改的结果。template1的存在允许用户可以制作一个自定义的模板数据库,在其中用户可以创建一些应用需要的表、数据、索引等,在日后需要多次创建相同内容的数据库时,都可以用template1作为模板生成。

src\bin\initdb

  initdb是PG中一个独立的程序,它的主要工作就是对数据集簇进行初始化,创建模板数据库和系统表,并向系统表中插入初始元组。在这以后,用户创建各种数据库、表、视图、索引等数据库对象和进行其他操作时,都是在模板数据库和系统表的基础上进行的。执行initdb程序时,将从发initdb.c文件中的main函数开始执行,initdb执行时将按照顺序执行下列工作:

1. 根据用户输入的命令行参数获取输入的命令名。

initdb把用户指定的选项转换成对应的参数,通过外部程序调用的方式执行postgres程序。postgres程序在这种方式下将进入bootstrap模式创建数据集簇,并读取后端接口postgres.bki文件来创建模板数据库。

initdb [option...] --pgdata|-D directory 常用option选项如下:

-A METHOD | --auth = METHOD 指定本地连接的默认用户认证方式

-D DATADIR | --pgdata = DATADIR 数据目录的路径,必须是对当前用户可读写的空目录,也可以使用环境变量PGDATA来指定而省略本选项

-E ENCODING | --encoding = ENCODING 指定默认数据库编码方式

-U NAME | --username = NAME 指定数据库超级用户名

-W | --pwprompt 提示超级用户设置口令

-d | --debug 以调试模式运行,可以打印出很多调试消息

-L directory 指定输入文件(比如postgres.bki)位置

 1     static struct option long_options[] = {
 2         {"pgdata", required_argument, NULL, 'D'},
 3         {"encoding", required_argument, NULL, 'E'},
 4         {"locale", required_argument, NULL, 1},
 5         {"lc-collate", required_argument, NULL, 2},
 6         {"lc-ctype", required_argument, NULL, 3},
 7         {"lc-monetary", required_argument, NULL, 4},
 8         {"lc-numeric", required_argument, NULL, 5},
 9         {"lc-time", required_argument, NULL, 6},
10         {"lc-messages", required_argument, NULL, 7},
11         {"no-locale", no_argument, NULL, 8},
12         {"text-search-config", required_argument, NULL, 'T'},
13         {"auth", required_argument, NULL, 'A'},
14         {"pwprompt", no_argument, NULL, 'W'},
15         {"pwfile", required_argument, NULL, 9},
16         {"username", required_argument, NULL, 'U'},
17         {"help", no_argument, NULL, '?'},
18         {"version", no_argument, NULL, 'V'},
19         {"debug", no_argument, NULL, 'd'},
20         {"show", no_argument, NULL, 's'},
21         {"noclean", no_argument, NULL, 'n'},
22         {"xlogdir", required_argument, NULL, 'X'},
23         {NULL, 0, NULL, 0}
24     };

 getopt_long函数用于处理命令行参数,代码处于src/port/getopt_long.c。

1 while ((c = getopt_long(argc, argv, "dD:E:L:nU:WA:sT:X:", long_options, &option_index)) != -1){
2     switch (c){
3         case 'A':
4             authmethod = xstrdup(optarg);
5             break;
6 ...

 

2. 设置系统编码为LC_ALL,查找执行命令的绝对路径并设置该路径。

set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("initdb"));

设置指定应用的locale和service目录,函数获取argv[0]的值而不是全路径

 1 void set_pglocale_pgservice(const char *argv0, const char *app){
 2     char        path[MAXPGPATH];
 3     char        my_exec_path[MAXPGPATH];
 4     char        env_path[MAXPGPATH + sizeof("PGSYSCONFDIR=")];    /* longer than PGLOCALEDIR */
 5     /* don't set LC_ALL in the backend */
 6     if (strcmp(app, PG_TEXTDOMAIN("postgres")) != 0)
 7         setlocale(LC_ALL, "");
 8     if (find_my_exec(argv0, my_exec_path) < 0)
 9         return;
10 #ifdef ENABLE_NLS
11     get_locale_path(my_exec_path, path);
12     bindtextdomain(app, path);
13     textdomain(app);
14     if (getenv("PGLOCALEDIR") == NULL){
15         /* set for libpq to use */
16         snprintf(env_path, sizeof(env_path), "PGLOCALEDIR=%s", path);
17         canonicalize_path(env_path + 12);
18         putenv(strdup(env_path));
19     }
20 #endif
21     if (getenv("PGSYSCONFDIR") == NULL){
22         get_etc_path(my_exec_path, path);
23         /* set for libpq to use */
24         snprintf(env_path, sizeof(env_path), "PGSYSCONFDIR=%s", path);
25         canonicalize_path(env_path + 13);
26         putenv(strdup(env_path));
27     }
28 }

 

3. 设置环境变量(pg_data等),获取系统配置文件的源文件路径(postgres.bki、postgresql.conf. sample等文件),并检查该路径下各文件的可用性。

4. 设置中断信号处理函数,对终端命令行SIGHUP、程序中断SIGINT、程序退出SIGQUIT、软件中断SIGTERM和管道中断SIGPIPE等信号进行屏蔽,保证初始化工作顺利进行。

5. 创建数据目录,以及该目录下一些必要的子目录,如base、global、base/1等。

6. 测试当前服务器系统性能,由测试结果创建配置文件postgresql.conf、pg_hba.conf、pg_ident.conf,并对其中定义的参数做一些设置。

7. 在bootstrap模式下创建数据库template1,存储在数据目录的子目录base/1/中

3141行

1     /* Bootstrap template1 */
2     bootstrap_template1(short_version);
3 
4     /*
5      * Make the per-database PG_VERSION for template1 only after init'ing it
6      */
7     set_short_version(short_version, "base/1");

8. 创建系统视图、系统表TOAST表等,复制template1来创建template0和postgres,这些操作都用普通的SQL命令来完成。

    setup_auth();
    if (pwprompt || pwfilename)
        get_set_pwd();
    setup_depend();
    setup_sysviews();
    setup_description();
    setup_conversion();
    setup_dictionary();
    setup_privileges();
    setup_schema();
    vacuum_db();
    make_template0();
    make_postgres();

9. 打印操作成功等相关信息,退出。

    if (authwarning != NULL)
        fprintf(stderr, "%s", authwarning);
    /* Get directory specification used to start this executable */
    strcpy(bin_dir, argv[0]);
    get_parent_directory(bin_dir);
    printf(_("\nSuccess. You can now start the database server using:\n\n"
             "    %s%s%spostgres%s -D %s%s%s\n"
             "or\n"
             "    %s%s%spg_ctl%s -D %s%s%s -l logfile start\n\n"),
       QUOTE_PATH, bin_dir, (strlen(bin_dir) > 0) ? DIR_SEP : "", QUOTE_PATH,
           QUOTE_PATH, pg_data_native, QUOTE_PATH,
       QUOTE_PATH, bin_dir, (strlen(bin_dir) > 0) ? DIR_SEP : "", QUOTE_PATH,
           QUOTE_PATH, pg_data_native, QUOTE_PATH);

 

posted @ 2020-12-22 22:11  肥叔菌  阅读(2602)  评论(0编辑  收藏  举报