以bootstrap模式运行BKI脚本创建template1

static void bootstrap_template1(void) {
PG_CMD_DECL; // #define PG_CMD_DECL char cmd[MAXPGPATH]; FILE *cmdfd
char **line; char **bki_lines;
char headerline[MAXPGPATH]; char buf[64];
printf(_("running bootstrap script ... ")); fflush(stdout);

bki_lines = readfile(bki_file); // 读取bki文件
/* Check that bki file appears to be of the right version */
snprintf(headerline, sizeof(headerline), "# PostgreSQL %s\n", PG_MAJORVERSION);
if (strcmp(headerline, *bki_lines) != 0) {
pg_log_error("input file \"%s\" does not belong to PostgreSQL %s", bki_file, PG_VERSION);
fprintf(stderr,_("Check your installation or specify the correct path using the option -L.\n")); exit(1);
}

/* Substitute for various symbols used in the BKI file */
sprintf(buf, "%d", NAMEDATALEN); // NAMEDATALEN为64
bki_lines = replace_token(bki_lines, "NAMEDATALEN", buf);
sprintf(buf, "%d", (int) sizeof(Pointer));
bki_lines = replace_token(bki_lines, "SIZEOF_POINTER", buf);
bki_lines = replace_token(bki_lines, "ALIGNOF_POINTER", (sizeof(Pointer) == 4) ? "i" : "d");
bki_lines = replace_token(bki_lines, "FLOAT4PASSBYVAL", FLOAT4PASSBYVAL ? "true" : "false");
bki_lines = replace_token(bki_lines, "FLOAT8PASSBYVAL", FLOAT8PASSBYVAL ? "true" : "false");
bki_lines = replace_token(bki_lines, "POSTGRES", escape_quotes_bki(username));
bki_lines = replace_token(bki_lines, "ENCODING", encodingid_to_string(encodingid));
bki_lines = replace_token(bki_lines, "LC_COLLATE", escape_quotes_bki(lc_collate));
bki_lines = replace_token(bki_lines, "LC_CTYPE", escape_quotes_bki(lc_ctype));

// 向bootstrap传递正确得LC_xxx
/* Pass correct LC_xxx environment to bootstrap.
* The shell script arranged to restore the LC settings afterwards, but
* there doesn't seem to be any compelling reason to do that. */
snprintf(cmd, sizeof(cmd), "LC_COLLATE=%s", lc_collate); putenv(pg_strdup(cmd));
snprintf(cmd, sizeof(cmd), "LC_CTYPE=%s", lc_ctype); putenv(pg_strdup(cmd));
unsetenv("LC_ALL");
/* Also ensure backend isn't confused by this environment var: */
unsetenv("PGCLIENTENCODING");

snprintf(cmd, sizeof(cmd),"\"%s\" --boot -x1 -X %u %s %s %s",backend_exec,wal_segment_size_mb * (1024 * 1024),data_checksums ? "-k" : "",boot_options,debug ? "-d 5" : "");

调用的命令:​​postgres --boot -x1 -X wal_segment_size_mb*(1024*1024) data_checksums ? "-k" : "" boot_options debug ? "-d 5" : ""​

;
for (line = bki_lines; *line != NULL; line++){
PG_CMD_PUTS(*line);
free(*line);
}
PG_CMD_CLOSE;
free(bki_lines);
check_ok();
}

#define PG_CMD_OPEN \
do {\
cmdfd = popen_check(cmd,"w");\
if (cmdfd == NULL)\
exit(1);/* message already printed by popen_check */ \
} while (0)
#define PG_CMD_PUTS(line)\
do {\
if (fputs(line, cmdfd) < 0 || fflush(cmdfd) < 0)\
output_failed = true, output_errno = errno;\
} while (0)
#define PG_CMD_CLOSE \
do {\
if (pclose_check(cmdfd))\
exit(1);/* message already printed by pclose_check */ \
} while (0)

PostgreSQL数据库集簇初始化——initdb初始化数据库(bootstrap_template1)_bootstrap