initdb 命令用于创建一个新的PostgreSQL数据库集簇
使用的代码版本为:9.6.10
先看一下命令的用法
initdb [option…] [–pgdata | -D] directory
通过-D参数可以指定数据库集簇的目录,其他参数包括字符集,区域等可以参考官方手册
http://www.postgres.cn/docs/9.6/app-initdb.html
initdb大概可以分为以下几个阶段:
1.初期准备
2. 参数解析
3. 环境设定
4. 初期化集簇
先看初期准备:
1.初期准备主要是相关列表的初期化,比如:
pgdata下的目录列表
static const char *const subdirs[] = {
"global",
"pg_xlog/archive_status",
"pg_clog",
"pg_commit_ts",
"pg_dynshmem",
"pg_notify",
"pg_serial",
"pg_snapshots",
"pg_subtrans",
"pg_twophase",
"pg_multixact",
"pg_multixact/members",
"pg_multixact/offsets",
"base",
"base/1",
"pg_replslot",
"pg_tblspc",
"pg_stat",
"pg_stat_tmp",
"pg_logical",
"pg_logical/snapshots",
"pg_logical/mappings"
};
参数列表
static struct option long_options[] = {
{"pgdata", required_argument, NULL, 'D'},
{"encoding", required_argument, NULL, 'E'},
{"locale", required_argument, NULL, 1},
{"lc-collate", required_argument, NULL, 2},
{"lc-ctype", required_argument, NULL, 3},
{"lc-monetary", required_argument, NULL, 4},
{"lc-numeric", required_argument, NULL, 5},
{"lc-time", required_argument, NULL, 6},
{"lc-messages", required_argument, NULL, 7},
{"no-locale", no_argument, NULL, 8},
{"text-search-config", required_argument, NULL, 'T'},
{"auth", required_argument, NULL, 'A'},
{"auth-local", required_argument, NULL, 10},
{"auth-host", required_argument, NULL, 11},
{"pwprompt", no_argument, NULL, 'W'},
{"pwfile", required_argument, NULL, 9},
{"username", required_argument, NULL, 'U'},
{"help", no_argument, NULL, '?'},
{"version", no_argument, NULL, 'V'},
{"debug", no_argument, NULL, 'd'},
{"show", no_argument, NULL, 's'},
{"noclean", no_argument, NULL, 'n'},
{"nosync", no_argument, NULL, 'N'},
{"sync-only", no_argument, NULL, 'S'},
{"xlogdir", required_argument, NULL, 'X'},
{"data-checksums", no_argument, NULL, 'k'},
{NULL, 0, NULL, 0}
};
还有一些别的这里就不一一列出了
2.确保stdout和stderr的缓冲行为与交互使用(至少在大多数平台上)中的缓冲行为匹配。
setvbuf(stdout, NULL, PG_IOLBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
setvbuf函数用来定义流 stream 应如何缓冲
这里将stdout定义为_IONBF,即:不使用缓冲。每个 I/O 操作都被即时写入。buffer 和 size 参数被忽略。
stdout定义为PG_IOLBF,PG_IOLBF和_IONBF一样,是无缓冲。
到这里初期准备阶段差不多就结束了。