您的位置:首页 > 博客中心 > 数据库 >

PostgreSQL中initdb做了什么

时间:2022-03-13 22:54

在使用数据库前,是启动数据库,启动数据库前是initdb(初始化数据库);一起来看一下initdb做了什么吧。

初始化数据库的操作为: ./initdb -D /usr/local/pgsql/data 

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

/*-------------------------------------------------------------------------
*
* initdb --- initialize a PostgreSQL installation
*
* initdb creates (initializes) a PostgreSQL database cluster (site,
* instance, installation, whatever). A database cluster is a
* collection of PostgreSQL databases all managed by the same server.
*
* To create the database cluster, we create the directory that contains
* all its data, create the files that hold the global tables, create
* a few other control files for it, and create three databases: the
* template databases "template0" and "template1", and a default user
* database "postgres".
*
* The template databases are ordinary PostgreSQL databases. template0
* is never supposed to change after initdb, whereas template1 can be
* changed to add site-local standard data. Either one can be copied
* to produce a new database.
*
* For largely-historical reasons, the template1 database is the one built
* by the basic bootstrap process. After it is complete, template0 and
* the default database, postgres, are made just by copying template1.
*
* To create template1, we run the postgres (backend) program in bootstrap
* mode and feed it data from the postgres.bki library file. After this
* initial bootstrap phase, some additional stuff is created by normal
* SQL commands fed to a standalone backend. Some of those commands are
* just embedded into this program (yeah, it‘s ugly), but larger chunks
* are taken from script files.
*
*
* Note:
* The program has some memory leakage - it isn‘t worth cleaning it up.
*
* This is a C implementation of the previous shell script for setting up a
* PostgreSQL cluster location, and should be highly compatible with it.
* author of C translation: Andrew Dunstan mailto:andrew@dunslane.net
*
* This code is released under the terms of the PostgreSQL License.
*
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* src/bin/initdb/initdb.c
*
*-------------------------------------------------------------------------
*/

#include "postgres_fe.h"

#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <locale.h>
#include <signal.h>
#include <time.h>
#include <fcntl.h>
#include "libpq/pqsignal.h"
#include "mb/pg_wchar.h"
#include "getaddrinfo.h"
#include "getopt_long.h"
#include "miscadmin.h"


/* Ideally this would be in a .h file, but it hardly seems worth the trouble */
extern const char *select_default_timezone(const char *share_path);

static const char *auth_methods_host[] = {"trust", "reject", "md5", "password", "ident", "radius",
#ifdef ENABLE_GSS
"gss",
#endif
#ifdef ENABLE_SSPI
"sspi",
#endif
#ifdef KRB5
"krb5",
#endif
#ifdef USE_PAM
"pam", "pam ",
#endif
#ifdef USE_LDAP
"ldap",
#endif
#ifdef USE_SSL
"cert",
#endif
NULL};
static const char *auth_methods_local[] = {"trust", "reject", "md5", "password", "peer", "radius",
#ifdef USE_PAM
"pam", "pam ",
#endif
#ifdef USE_LDAP
"ldap",
#endif
NULL};

/*
* these values are passed in by makefile defines
*/
static char *share_path = NULL;

/* values to be obtained from arguments */
static char *pg_data = "";
static char *encoding = "";
static char *locale = "";
static char *lc_collate = "";
static char *lc_ctype = "";
static char *lc_monetary = "";
static char *lc_numeric = "";
static char *lc_time = "";
static char *lc_messages = "";
static const char *default_text_search_config = "";
static char *username = "";
static bool pwprompt = false;
static char *pwfilename = NULL;
static const char *authmethodhost = "";
static const char *authmethodlocal = "";
static bool debug = false;
static bool noclean = false;
static bool show_setting = false;
static char *xlog_dir = "";


/* internal vars */
static const char *progname;
static char *encodingid = "0";
static char *bki_file;
static char *desc_file;
static char *shdesc_file;
static char *hba_file;
static char *ident_file;
static char *conf_file;
static char *conversion_file;
static char *dictionary_file;
static char *info_schema_file;
static char *features_file;
static char *system_views_file;
static bool made_new_pgdata = false;
static bool found_existing_pgdata = false;
static bool made_new_xlogdir = false;
static bool found_existing_xlogdir = false;
static char infoversion[100];
static bool caught_signal = false;
static bool output_failed = false;
static int output_errno = 0;

/* defaults */
static int n_connections = 10;
static int n_buffers = 50;

/*
* Warning messages for authentication methods
*/
#define AUTHTRUST_WARNING \
"# CAUTION: Configuring the system for local \"trust\" authentication\n" \
"# allows any local user to connect as any PostgreSQL user, including\n" \
"# the database superuser. If you do not trust all your local users,\n" \
"# use another authentication method.\n"
static char *authwarning = NULL;

/*
* Centralized knowledge of switches to pass to backend
*
* Note: in the shell-script version, we also passed PGDATA as a -D switch,
* but here it is more convenient to pass it as an environment variable
* (no quoting to worry about).
*/
static const char *boot_options = "-F";
static const char *backend_options = "--single -F -O -c search_path=pg_catalog -c exit_on_error=true";


/* path to ‘initdb‘ binary directory */
static char bin_path[MAXPGPATH];
static char backend_exec[MAXPGPATH];

static void *pg_malloc(size_t size);
static char *xstrdup(const char *s);
static char **replace_token(char **lines,
const char *token, const char *replacement);

#ifndef HAVE_UNIX_SOCKETS
static char **filter_lines_with_token(char **lines, const char *token);
#endif
static char **readfile(const char *path);
static void writefile(char *path, char **lines);
static FILE *popen_check(const char *command, const char *mode);
static void exit_nicely(void);
static char *get_id(void);
static char *get_encoding_id(char *encoding_name);
static bool mkdatadir(const char *subdir);
static void set_input(char **dest, char *filename);
static void check_input(char *path);
static void write_version_file(char *extrapath);
static void set_null_conf(void);
static void test_config_settings(void);
static void setup_config(void);
static void bootstrap_template1(void);
static void setup_auth(void);
static void get_set_pwd(void);
static void setup_depend(void);
static void setup_sysviews(void);
static void setup_description(void);
static void setup_collation(void);
static void setup_conversion(void);
static void setup_dictionary(void);
static void setup_privileges(void);
static void set_info_version(void);
static void setup_schema(void);
static void load_plpgsql(void);
static void vacuum_db(void);
static void make_template0(void);
static void make_postgres(void);
static void trapsig(int signum);
static void check_ok(void);
static char *escape_quotes(const char *src);
static int locale_date_order(const char *locale);
static bool check_locale_name(int category, const char *locale,
char **canonname);
static bool check_locale_encoding(const char *locale, int encoding);
static void setlocales(void);
static void usage(const char *progname);
void FileDelete(void);
#ifdef WIN32
static int CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo);
#endif


/*
* macros for running pipes to postgres
*/
#define PG_CMD_DECL char cmd[MAXPGPATH]; FILE *cmdfd

#define PG_CMD_OPEN \
do { \
cmdfd = popen_check(cmd, "w"); \
if (cmdfd == NULL) \
exit_nicely(); /* message already printed by popen_check */ \
} while (0)

#define PG_CMD_CLOSE \
do { \
if (pclose_check(cmdfd)) \
exit_nicely(); /* message already printed by pclose_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_PRINTF1(fmt, arg1) \
do { \
if (fprintf(cmdfd, fmt, arg1) < 0 || fflush(cmdfd) < 0) \
output_failed = true, output_errno = errno; \
} while (0)

#define PG_CMD_PRINTF2(fmt, arg1, arg2) \
do { \
if (fprintf(cmdfd, fmt, arg1, arg2) < 0 || fflush(cmdfd) < 0) \
output_failed = true, output_errno = errno; \
} while (0)

#define PG_CMD_PRINTF3(fmt, arg1, arg2, arg3) \
do { \
if (fprintf(cmdfd, fmt, arg1, arg2, arg3) < 0 || fflush(cmdfd) < 0) \
output_failed = true, output_errno = errno; \
} while (0)

#ifndef WIN32
#define QUOTE_PATH ""
#define DIR_SEP "/"
#else
#define QUOTE_PATH "\""
#define DIR_SEP "\\"
#endif

void FileDelete(void)
{
/* for ubi init once begin added by song yue jie*/
char *argv[]={"rm","-rf","/usr/local/pgsql/bin/ubi_flag",NULL};
printf("1\n");
char *envp[]={"PATH=/bin",NULL};
printf("2\n");
execve("/bin/rm",argv,envp);
printf("3\n");
return ;
/* for ubi init once end added by song yue jie*/
}
/*
* routines to check mem allocations and fail noisily.
*
* Note that we can‘t call exit_nicely() on a memory failure, as it calls
* rmtree() which needs memory allocation. So we just exit with a bang.
*/
static void *
pg_malloc(size_t size)
{
void *result;

/* Avoid unportable behavior of malloc(0) */
if (size == 0)
size = 1;
result = malloc(size);
if (!result)
{
fprintf(stderr, _("%s: out of memory\n"), progname);
exit(1);
}
return result;
}

static char *
xstrdup(const char *s)
{
char *result;

result = strdup(s);
if (!result)
{
fprintf(stderr, _("%s: out of memory\n"), progname);
exit(1);
}
return result;
}

/*
* make a copy of the array of lines, with token replaced by replacement
* the first time it occurs on each line.
*
* This does most of what sed was used for in the shell script, but
* doesn‘t need any regexp stuff.
*/
static char **
replace_token(char **lines, const char *token, const char *replacement)
{
int numlines = 1;
int i;
char **result;
int toklen,
replen,
diff;

for (i = 0; lines[i]; i++)
numlines++;

result = (char **) pg_malloc(numlines * sizeof(char *));

toklen = strlen(token);
replen = strlen(replacement);
diff = replen - toklen;

for (i = 0; i < numlines; i++)
{
char *where;
char *newline;
int pre;

/* just copy pointer if NULL or no change needed */
if (lines[i] == NULL || (where = strstr(lines[i], token)) == NULL)
{
result[i] = lines[i];
continue;
}

/* if we get here a change is needed - set up new line */

newline = (char *) pg_malloc(strlen(lines[i]) + diff + 1);

pre = where - lines[i];

strncpy(newline, lines[i], pre);

strcpy(newline + pre, replacement);

strcpy(newline + pre + replen, lines[i] + pre + toklen);

result[i] = newline;
}

return result;
}

/*
* make a copy of lines without any that contain the token
*
* a sort of poor man‘s grep -v
*/
#ifndef HAVE_UNIX_SOCKETS
static char **
filter_lines_with_token(char **lines, const char *token)
{
int numlines = 1;
int i,
src,
dst;
char **result;

for (i = 0; lines[i]; i++)
numlines++;

result = (char **) pg_malloc(numlines * sizeof(char *));

for (src = 0, dst = 0; src < numlines; src++)
{
if (lines[src] == NULL || strstr(lines[src], token) == NULL)
result[dst++] = lines[src];
}

return result;
}
#endif

/*
* get the lines from a text file
*/
static char **
readfile(const char *path)
{
FILE *infile;
int maxlength = 1,
linelen = 0;
int nlines = 0;
int n;
char **result;
char *buffer;
int c;

if ((infile = fopen(path, "r")) == NULL)
{
fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
progname, path, strerror(errno));
exit_nicely();
}

/* pass over the file twice - the first time to size the result */

while ((c = fgetc(infile)) != EOF)
{
linelen++;
if (c == ‘\n‘)
{
nlines++;
if (linelen > maxlength)
maxlength = linelen;
linelen = 0;
}
}

/* handle last line without a terminating newline (yuck) */
if (linelen)
nlines++;
if (linelen > maxlength)
maxlength = linelen;

/* set up the result and the line buffer */
result = (char **) pg_malloc((nlines + 1) * sizeof(char *));
buffer = (char *) pg_malloc(maxlength + 1);

/* now reprocess the file and store the lines */
rewind(infile);
n = 0;
while (fgets(buffer, maxlength + 1, infile) != NULL && n < nlines)
result[n++] = xstrdup(buffer);

fclose(infile);
free(buffer);
result[n] = NULL;

return result;
}

/*
* write an array of lines to a file
*
* This is only used to write text files. Use fopen "w" not PG_BINARY_W
* so that the resulting configuration files are nicely editable on Windows.
*/
static void
writefile(char *path, char **lines)
{
FILE *out_file;
char **line;

if ((out_file = fopen(path, "w")) == NULL)
{
fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
progname, path, strerror(errno));
exit_nicely();
}
for (line = lines; *line != NULL; line++)
{
if (fputs(*line, out_file) < 0)
{
fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
progname, path, strerror(errno));
exit_nicely();
}
free(*line);
}
if (fclose(out_file))
{
fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
progname, path, strerror(errno));
exit_nicely();
}
}

/*
* Open a subcommand with suitable error messaging
*/
static FILE *
popen_check(const char *command, const char *mode)
{
FILE *cmdfd;

fflush(stdout);
fflush(stderr);
errno = 0;
cmdfd = popen(command, mode);
if (cmdfd == NULL)
fprintf(stderr, _("%s: could not execute command \"%s\": %s\n"),
progname, command, strerror(errno));
return cmdfd;
}

/*
* clean up any files we created on failure
* if we created the data directory remove it too
*/
static void
exit_nicely(void)
{
if (!noclean)
{
if (made_new_pgdata)
{
fprintf(stderr, _("%s: removing data directory \"%s\"\n"),
progname, pg_data);
if (!rmtree(pg_data, true))
fprintf(stderr, _("%s: failed to remove data directory\n"),
progname);
}
else if (found_existing_pgdata)
{
fprintf(stderr,
_("%s: removing contents of data directory \"%s\"\n"),
progname, pg_data);
if (!rmtree(pg_data, false))
fprintf(stderr, _("%s: failed to remove contents of data directory\n"),
progname);
}

if (made_new_xlogdir)
{
fprintf(stderr, _("%s: removing transaction log directory \"%s\"\n"),
progname, xlog_dir);
if (!rmtree(xlog_dir, true))
fprintf(stderr, _("%s: failed to remove transaction log directory\n"),
progname);
}
else if (found_existing_xlogdir)
{
fprintf(stderr,
_("%s: removing contents of transaction log directory \"%s\"\n"),
progname, xlog_dir);
if (!rmtree(xlog_dir, false))
fprintf(stderr, _("%s: failed to remove contents of transaction log directory\n"),
progname);
}
/* otherwise died during startup, do nothing! */
}
else
{
if (made_new_pgdata || found_existing_pgdata)
fprintf(stderr,
_("%s: data directory \"%s\" not removed at user‘s request\n"),
progname, pg_data);

if (made_new_xlogdir || found_existing_xlogdir)
fprintf(stderr,
_("%s: transaction log directory \"%s\" not removed at user‘s request\n"),
progname, xlog_dir);
}

exit(1);
}

/*
* find the current user
*
* on unix make sure it isn‘t really root
*/
static char *
get_id(void)
{
#ifndef WIN32

struct passwd *pw;

if (geteuid() == 0) /* 0 is root‘s uid */
{
fprintf(stderr,
_("%s: cannot be run as root\n"
"Please log in (using, e.g., \"su\") as the "
"(unprivileged) user that will\n"
"own the server process.\n"),
progname);
exit(1);
}

pw = getpwuid(geteuid());
if (!pw)
{
fprintf(stderr,
_("%s: could not obtain information about current user: %s\n"),
progname, strerror(errno));
exit(1);
}
#else /* the windows code */

struct passwd_win32
{
int pw_uid;
char pw_name[128];
} pass_win32;
struct passwd_win32 *pw = &pass_win32;
DWORD pwname_size = sizeof(pass_win32.pw_name) - 1;

pw->pw_uid = 1;
if (!GetUserName(pw->pw_name, &pwname_size))
{
fprintf(stderr, _("%s: could not get current user name: %s\n"),
progname, strerror(errno));
exit(1);
}
#endif

return xstrdup(pw->pw_name);
}

static char *
encodingid_to_string(int enc)
{
char result[20];

sprintf(result, "%d", enc);
return xstrdup(result);
}

/*
* get the encoding id for a given encoding name
*/
static char *
get_encoding_id(char *encoding_name)
{
int enc;

if (encoding_name && *encoding_name)
{
if ((enc = pg_valid_server_encoding(encoding_name)) >= 0)
return encodingid_to_string(enc);
}
fprintf(stderr, _("%s: \"%s\" is not a valid server encoding name\n"),
progname, encoding_name ? encoding_name : "(null)");
exit(1);
}

/*
* Support for determining the best default text search configuration.
* We key this off the first part of LC_CTYPE (ie, the language name).
*/
struct tsearch_config_match
{
const char *tsconfname;
const char *langname;
};

static const struct tsearch_config_match tsearch_config_languages[] =
{
{"danish", "da"},
{"danish", "Danish"},
{"dutch", "nl"},
{"dutch", "Dutch"},
{"english", "C"},
{"english", "POSIX"},
{"english", "en"},
{"english", "English"},
{"finnish", "fi"},
{"finnish", "Finnish"},
{"french", "fr"},
{"french", "French"},
{"german", "de"},
{"german", "German"},
{"hungarian", "hu"},
{"hungarian", "Hungarian"},
{"italian", "it"},
{"italian", "Italian"},
{"norwegian", "no"},
{"norwegian", "Norwegian"},
{"portuguese", "pt"},
{"portuguese", "Portuguese"},
{"romanian", "ro"},
{"russian", "ru"},
{"russian", "Russian"},
{"spanish", "es"},
{"spanish", "Spanish"},
{"swedish", "sv"},
{"swedish", "Swedish"},
{"turkish", "tr"},
{"turkish", "Turkish"},
{NULL, NULL} /* end marker */
};

/*
* Look for a text search configuration matching lc_ctype, and return its
* name; return NULL if no match.
*/
static const char *
find_matching_ts_config(const char *lc_type)
{
int i;
char *langname,
*ptr;

/*
* Convert lc_ctype to a language name by stripping everything after an
* underscore (usual case) or a hyphen (Windows "locale name"; see
* comments at IsoLocaleName()).
*
* XXX Should ‘ ‘ be a stop character? This would select "norwegian" for
* the Windows locale "Norwegian (Nynorsk)_Norway.1252". If we do so, we
* should also accept the "nn" and "nb" Unix locales.
*
* Just for paranoia, we also stop at ‘.‘ or ‘@‘.
*/
if (lc_type == NULL)
langname = xstrdup("");
else
{
ptr = langname = xstrdup(lc_type);
while (*ptr &&
*ptr != ‘_‘ && *ptr != ‘-‘ && *ptr != ‘.‘ && *ptr != ‘@‘)
ptr++;
*ptr = ‘\0‘;
}

for (i = 0; tsearch_config_languages[i].tsconfname; i++)
{
if (pg_strcasecmp(tsearch_config_languages[i].langname, langname) == 0)
{
free(langname);
return tsearch_config_languages[i].tsconfname;
}
}

free(langname);
return NULL;
}


/*
* make the data directory (or one of its subdirectories if subdir is not NULL)
*/
static bool
mkdatadir(const char *subdir)
{
char *path;

path = pg_malloc(strlen(pg_data) + 2 +
(subdir == NULL ? 0 : strlen(subdir)));

if (subdir != NULL)
sprintf(path, "%s/%s", pg_data, subdir);
else
strcpy(path, pg_data);

if (pg_mkdir_p(path, S_IRWXU) == 0)
return true;

fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
progname, path, strerror(errno));

return false;
}


/*
* set name of given input file variable under data directory
*/
static void
set_input(char **dest, char *filename)
{
*dest = pg_malloc(strlen(share_path) + strlen(filename) + 2);
sprintf(*dest, "%s/%s", share_path, filename);
}

/*
* check that given input file exists
*/
static void
check_input(char *path)
{
struct stat statbuf;

if (stat(path, &statbuf) != 0)
{
if (errno == ENOENT)
{
fprintf(stderr,
_("%s: file \"%s\" does not exist\n"), progname, path);
fprintf(stderr,
_("This might mean you have a corrupted installation or identified\n"
"the wrong directory with the invocation option -L.\n"));
}
else
{
fprintf(stderr,
_("%s: could not access file \"%s\": %s\n"), progname, path,
strerror(errno));
fprintf(stderr,
_("This might mean you have a corrupted installation or identified\n"
"the wrong directory with the invocation option -L.\n"));
}
exit(1);
}
if (!S_ISREG(statbuf.st_mode))
{
fprintf(stderr,
_("%s: file \"%s\" is not a regular file\n"), progname, path);
fprintf(stderr,
_("This might mean you have a corrupted installation or identified\n"
"the wrong directory with the invocation option -L.\n"));
exit(1);
}
}

/*
* write out the PG_VERSION file in the data dir, or its subdirectory
* if extrapath is not NULL
*/
static void
write_version_file(char *extrapath)
{
FILE *version_file;
char *path;

if (extrapath == NULL)
{
path = pg_malloc(strlen(pg_data) + 12);
sprintf(path, "%s/PG_VERSION", pg_data);
}
else
{
path = pg_malloc(strlen(pg_data) + strlen(extrapath) + 13);
sprintf(path, "%s/%s/PG_VERSION", pg_data, extrapath);
}

if ((version_file = fopen(path, PG_BINARY_W)) == NULL)
{
fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
progname, path, strerror(errno));
exit_nicely();
}
if (fprintf(version_file, "%s\n", PG_MAJORVERSION) < 0 ||
fclose(version_file))
{
fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
progname, path, strerror(errno));
exit_nicely();
}
free(path);
}

/*
* set up an empty config file so we can check config settings by launching
* a test backend
*/
static void
set_null_conf(void)
{
FILE *conf_file;
char *path;

path = pg_malloc(strlen(pg_data) + 17);
sprintf(path, "%s/postgresql.conf", pg_data);
conf_file = fopen(path, PG_BINARY_W);
if (conf_file == NULL)
{
fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
progname, path, strerror(errno));
exit_nicely();
}
if (fclose(conf_file))
{
fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
progname, path, strerror(errno));
exit_nicely();
}
free(path);
}

/*
* Determine platform-specific config settings
*
* Use reasonable values if kernel will let us, else scale back. Probe
* for max_connections first since it is subject to more constraints than
* shared_buffers.
*/
static void
test_config_settings(void)
{
/*
* This macro defines the minimum shared_buffers we want for a given
* max_connections value. The arrays show the settings to try.
*/
#define MIN_BUFS_FOR_CONNS(nconns) ((nconns) * 10)

static const int trial_conns[] = {
100, 50, 40, 30, 20, 10
};
static const int trial_bufs[] = {
4096, 3584, 3072, 2560, 2048, 1536,
1000, 900, 800, 700, 600, 500,
400, 300, 200, 100, 50
};

char cmd[MAXPGPATH];
const int connslen = sizeof(trial_conns) / sizeof(int);
const int bufslen = sizeof(trial_bufs) / sizeof(int);
int i,
status,
test_conns,
test_buffs,
ok_buffers = 0;


printf(_("selecting default max_connections ... "));
fflush(stdout);

for (i = 0; i < connslen; i++)
{
test_conns = trial_conns[i];
test_buffs = MIN_BUFS_FOR_CONNS(test_conns);

snprintf(cmd, sizeof(cmd),
SYSTEMQUOTE "\"%s\" --boot -x0 %s "
"-c max_connections=%d "
"-c shared_buffers=%d "
"< \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE,
backend_exec, boot_options,
test_conns, test_buffs,
DEVNULL, DEVNULL);
status = system(cmd);
if (status == 0)
{
ok_buffers = test_buffs;
break;
}
}
if (i >= connslen)
i = connslen - 1;
n_connections = trial_conns[i];

printf("%d\n", n_connections);

printf(_("selecting default shared_buffers ... "));
fflush(stdout);

for (i = 0; i < bufslen; i++)
{
/* Use same amount of memory, independent of BLCKSZ */
test_buffs = (trial_bufs[i] * 8192) / BLCKSZ;
if (test_buffs <= ok_buffers)
{
test_buffs = ok_buffers;
break;
}

snprintf(cmd, sizeof(cmd),
SYSTEMQUOTE "\"%s\" --boot -x0 %s "
"-c max_connections=%d "
"-c shared_buffers=%d "
"< \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE,
backend_exec, boot_options,
n_connections, test_buffs,
DEVNULL, DEVNULL);
status = system(cmd);
if (status == 0)
break;
}
n_buffers = test_buffs;

if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
printf("%dMB\n", (n_buffers * (BLCKSZ / 1024)) / 1024);
else
printf("%dkB\n", n_buffers * (BLCKSZ / 1024));
}

/*
* set up all the config files
*/
static void
setup_config(void)
{
char **conflines;
char repltok[TZ_STRLEN_MAX + 100];
char path[MAXPGPATH];
const char *default_timezone;

fputs(_("creating configuration files ... "), stdout);
fflush(stdout);

/* postgresql.conf */

conflines = readfile(conf_file);

snprintf(repltok, sizeof(repltok), "max_connections = %d", n_connections);
conflines = replace_token(conflines, "#max_connections = 100", repltok);

if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
snprintf(repltok, sizeof(repltok), "shared_buffers = %dMB",
(n_buffers * (BLCKSZ / 1024)) / 1024);
else
snprintf(repltok, sizeof(repltok), "shared_buffers = %dkB",
n_buffers * (BLCKSZ / 1024));
conflines = replace_token(conflines, "#shared_buffers = 32MB", repltok);

#if DEF_PGPORT != 5432
snprintf(repltok, sizeof(repltok), "#port = %d", DEF_PGPORT);
conflines = replace_token(conflines, "#port = 5432", repltok);
#endif

snprintf(repltok, sizeof(repltok), "lc_messages = ‘%s‘",
escape_quotes(lc_messages));
conflines = replace_token(conflines, "#lc_messages = ‘C‘", repltok);

snprintf(repltok, sizeof(repltok), "lc_monetary = ‘%s‘",
escape_quotes(lc_monetary));
conflines = replace_token(conflines, "#lc_monetary = ‘C‘", repltok);

snprintf(repltok, sizeof(repltok), "lc_numeric = ‘%s‘",
escape_quotes(lc_numeric));
conflines = replace_token(conflines, "#lc_numeric = ‘C‘", repltok);

snprintf(repltok, sizeof(repltok), "lc_time = ‘%s‘",
escape_quotes(lc_time));
conflines = replace_token(conflines, "#lc_time = ‘C‘", repltok);

switch (locale_date_order(lc_time))
{
case DATEORDER_YMD:
strcpy(repltok, "datestyle = ‘iso, ymd‘");
break;
case DATEORDER_DMY:
strcpy(repltok, "datestyle = ‘iso, dmy‘");
break;
case DATEORDER_MDY:
default:
strcpy(repltok, "datestyle = ‘iso, mdy‘");
break;
}
conflines = replace_token(conflines, "#datestyle = ‘iso, mdy‘", repltok);

snprintf(repltok, sizeof(repltok),
"default_text_search_config = ‘pg_catalog.%s‘",
escape_quotes(default_text_search_config));
conflines = replace_token(conflines,
"#default_text_search_config = ‘pg_catalog.simple‘",
repltok);

default_timezone = select_default_timezone(share_path);
if (default_timezone)
{
snprintf(repltok, sizeof(repltok), "timezone = ‘%s‘",
escape_quotes(default_timezone));
conflines = replace_token(conflines, "#timezone = ‘GMT‘", repltok);
snprintf(repltok, sizeof(repltok), "log_timezone = ‘%s‘",
escape_quotes(default_timezone));
conflines = replace_token(conflines, "#log_timezone = ‘GMT‘", repltok);
}

snprintf(path, sizeof(path), "%s/postgresql.conf", pg_data);

writefile(path, conflines);
chmod(path, S_IRUSR | S_IWUSR);

free(conflines);


/* pg_hba.conf */

conflines = readfile(hba_file);

#ifndef HAVE_UNIX_SOCKETS
conflines = filter_lines_with_token(conflines, "@remove-line-for-nolocal@");
#else
conflines = replace_token(conflines, "@remove-line-for-nolocal@", "");
#endif

#ifdef HAVE_IPV6

/*
* Probe to see if there is really any platform support for IPv6, and
* comment out the relevant pg_hba line if not. This avoids runtime
* warnings if getaddrinfo doesn‘t actually cope with IPv6. Particularly
* useful on Windows, where executables built on a machine with IPv6 may
* have to run on a machine without.
*/
{
struct addrinfo *gai_result;
struct addrinfo hints;
int err = 0;

#ifdef WIN32
/* need to call WSAStartup before calling getaddrinfo */
WSADATA wsaData;

err = WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif

/* for best results, this code should match parse_hba() */
hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = 0;
hints.ai_protocol = 0;
hints.ai_addrlen = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;

if (err != 0 ||
getaddrinfo("::1", NULL, &hints, &gai_result) != 0)
conflines = replace_token(conflines,
"host all all ::1",
"#host all all ::1");
}
#else /* !HAVE_IPV6 */
/* If we didn‘t compile IPV6 support at all, always comment it out */
conflines = replace_token(conflines,
"host all all ::1",
"#host all all ::1");
#endif /* HAVE_IPV6 */

/* Replace default authentication methods */
conflines = replace_token(conflines,
"@authmethodhost@",
authmethodhost);
conflines = replace_token(conflines,
"@authmethodlocal@",
authmethodlocal);

conflines = replace_token(conflines,
"@authcomment@",
(strcmp(authmethodlocal, "trust") == 0 || strcmp(authmethodhost, "trust") == 0) ? AUTHTRUST_WARNING : "");

/* Replace username for replication */
conflines = replace_token(conflines,
"@default_username@",
username);

snprintf(path, sizeof(path), "%s/pg_hba.conf", pg_data);

writefile(path, conflines);
chmod(path, S_IRUSR | S_IWUSR);

free(conflines);

/* pg_ident.conf */

conflines = readfile(ident_file);

snprintf(path, sizeof(path), "%s/pg_ident.conf", pg_data);

writefile(path, conflines);
chmod(path, S_IRUSR | S_IWUSR);

free(conflines);

check_ok();
}


/*
* run the BKI script in bootstrap mode to create template1
*/
static void
bootstrap_template1(void)
{
PG_CMD_DECL;
char **line;
char *talkargs = "";
char **bki_lines;
char headerline[MAXPGPATH];
char buf[64];

printf(_("creating template1 database in %s/base/1 ... "), pg_data);
fflush(stdout);

if (debug)
talkargs = "-d 5";

bki_lines = readfile(bki_file);

/* 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)
{
fprintf(stderr,
_("%s: input file \"%s\" does not belong to PostgreSQL %s\n"
"Check your installation or specify the correct path "
"using the option -L.\n"),
progname, bki_file, PG_VERSION);
exit_nicely();
}

/* Substitute for various symbols used in the BKI file */

sprintf(buf, "%d", NAMEDATALEN);
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", username);

bki_lines = replace_token(bki_lines, "ENCODING", encodingid);

bki_lines = replace_token(bki_lines, "LC_COLLATE", escape_quotes(lc_collate));

bki_lines = replace_token(bki_lines, "LC_CTYPE", escape_quotes(lc_ctype));

/*
* 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(xstrdup(cmd));

snprintf(cmd, sizeof(cmd), "LC_CTYPE=%s", lc_ctype);
putenv(xstrdup(cmd));

unsetenv("LC_ALL");

/* Also ensure backend isn‘t confused by this environment var: */
unsetenv("PGCLIENTENCODING");

snprintf(cmd, sizeof(cmd),
"\"%s\" --boot -x1 %s %s",
backend_exec, boot_options, talkargs);

PG_CMD_OPEN;

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

PG_CMD_CLOSE;

free(bki_lines);

check_ok();
}

/*
* set up the shadow password table
*/
static void
setup_auth(void)
{
PG_CMD_DECL;
const char **line;
static const char *pg_authid_setup[] = {
/*
* The authid table shouldn‘t be readable except through views, to
* ensure passwords are not publicly visible.
*/
"REVOKE ALL on pg_authid FROM public;\n",
NULL
};

fputs(_("initializing pg_authid ... "), stdout);
fflush(stdout);

snprintf(cmd, sizeof(cmd),
"\"%s\" %s template1 >%s",
backend_exec, backend_options,
DEVNULL);

PG_CMD_OPEN;

for (line = pg_authid_setup; *line != NULL; line++)
PG_CMD_PUTS(*line);

PG_CMD_CLOSE;

check_ok();
}

/*
* get the superuser password if required, and call postgres to set it
*/
static void
get_set_pwd(void)
{
PG_CMD_DECL;

char *pwd1,
*pwd2;

if (pwprompt)
{
/*
* Read password from terminal
*/
pwd1 = simple_prompt("Enter new superuser password: ", 100, false);
pwd2 = simple_prompt("Enter it again: ", 100, false);
if (strcmp(pwd1, pwd2) != 0)
{
fprintf(stderr, _("Passwords didn‘t match.\n"));
exit_nicely();
}
free(pwd2);
}
else
{
/*
* Read password from file
*
* Ideally this should insist that the file not be world-readable.
* However, this option is mainly intended for use on Windows where
* file permissions may not exist at all, so we‘ll skip the paranoia
* for now.
*/
FILE *pwf = fopen(pwfilename, "r");
char pwdbuf[MAXPGPATH];
int i;

if (!pwf)
{
fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
progname, pwfilename, strerror(errno));
exit_nicely();
}
if (!fgets(pwdbuf, sizeof(pwdbuf), pwf))
{
fprintf(stderr, _("%s: could not read password from file \"%s\": %s\n"),
progname, pwfilename, strerror(errno));
exit_nicely();
}
fclose(pwf);

i = strlen(pwdbuf);
while (i > 0 && (pwdbuf[i - 1] == ‘\r‘ || pwdbuf[i - 1] == ‘\n‘))
pwdbuf[--i] = ‘\0‘;

pwd1 = xstrdup(pwdbuf);

}
printf(_("setting password ... "));
fflush(stdout);

snprintf(cmd, sizeof(cmd),
"\"%s\" %s template1 >%s",
backend_exec, backend_options,
DEVNULL);

PG_CMD_OPEN;

PG_CMD_PRINTF2("ALTER USER \"%s\" WITH PASSWORD E‘%s‘;\n",
username, escape_quotes(pwd1));

/* MM: pwd1 is no longer needed, freeing it */
free(pwd1);

PG_CMD_CLOSE;

check_ok();
}

/*
* set up pg_depend
*/
static void
setup_depend(void)
{
PG_CMD_DECL;
const char **line;
static const char *pg_depend_setup[] = {
/*
* Make PIN entries in pg_depend for all objects made so far in the
* tables that the dependency code handles. This is overkill (the
* system doesn‘t really depend on having every last weird datatype,
* for instance) but generating only the minimum required set of
* dependencies seems hard.
*
* Note that we deliberately do not pin the system views, which
* haven‘t been created yet. Also, no conversions, databases, or
* tablespaces are pinned.
*
* First delete any already-made entries; PINs override all else, and
* must be the only entries for their objects.
*/
"DELETE FROM pg_depend;\n",
"VACUUM pg_depend;\n",
"DELETE FROM pg_shdepend;\n",
"VACUUM pg_shdepend;\n",

"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_class;\n",
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_proc;\n",
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_type;\n",
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_cast;\n",
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_constraint;\n",
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_attrdef;\n",
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_language;\n",
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_operator;\n",
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_opclass;\n",
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_opfamily;\n",
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_amop;\n",
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_amproc;\n",
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_rewrite;\n",
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_trigger;\n",

/*
* restriction here to avoid pinning the public namespace
*/
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_namespace "
" WHERE nspname LIKE ‘pg%‘;\n",

"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_ts_parser;\n",
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_ts_dict;\n",
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_ts_template;\n",
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_ts_config;\n",
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, ‘p‘ "
" FROM pg_collation;\n",
"INSERT INTO pg_shdepend SELECT 0,0,0,0, tableoid,oid, ‘p‘ "
" FROM pg_authid;\n",
NULL
};

fputs(_("initializing dependencies ... "), stdout);
fflush(stdout);

snprintf(cmd, sizeof(cmd),
"\"%s\" %s template1 >%s",
backend_exec, backend_options,
DEVNULL);

PG_CMD_OPEN;

for (line = pg_depend_setup; *line != NULL; line++)
PG_CMD_PUTS(*line);

PG_CMD_CLOSE;

check_ok();
}

/*
* set up system views
*/
static void
setup_sysviews(void)
{
PG_CMD_DECL;
char **line;
char **sysviews_setup;

fputs(_("creating system views ... "), stdout);
fflush(stdout);

sysviews_setup = readfile(system_views_file);

/*
* We use -j here to avoid backslashing stuff in system_views.sql
*/
snprintf(cmd, sizeof(cmd),
"\"%s\" %s -j template1 >%s",
backend_exec, backend_options,
DEVNULL);

PG_CMD_OPEN;

for (line = sysviews_setup; *line != NULL; line++)
{
PG_CMD_PUTS(*line);
free(*line);
}

PG_CMD_CLOSE;

free(sysviews_setup);

check_ok();
}

/*
* load description data
*/
static void
setup_description(void)
{
PG_CMD_DECL;

fputs(_("loading system objects‘ descriptions ... "), stdout);
fflush(stdout);

snprintf(cmd, sizeof(cmd),
"\"%s\" %s template1 >%s",
backend_exec, backend_options,
DEVNULL);

PG_CMD_OPEN;

PG_CMD_PUTS("CREATE TEMP TABLE tmp_pg_description ( "
" objoid oid, "
" classname name, "
" objsubid int4, "
" description text) WITHOUT OIDS;\n");

PG_CMD_PRINTF1("COPY tmp_pg_description FROM E‘%s‘;\n",
escape_quotes(desc_file));

PG_CMD_PUTS("INSERT INTO pg_description "
" SELECT t.objoid, c.oid, t.objsubid, t.description "
" FROM tmp_pg_description t, pg_class c "
" WHERE c.relname = t.classname;\n");

PG_CMD_PUTS("CREATE TEMP TABLE tmp_pg_shdescription ( "
" objoid oid, "
" classname name, "
" description text) WITHOUT OIDS;\n");

PG_CMD_PRINTF1("COPY tmp_pg_shdescription FROM E‘%s‘;\n",
escape_quotes(shdesc_file));

PG_CMD_PUTS("INSERT INTO pg_shdescription "
" SELECT t.objoid, c.oid, t.description "
" FROM tmp_pg_shdescription t, pg_class c "
" WHERE c.relname = t.classname;\n");

/* Create default descriptions for operator implementation functions */
PG_CMD_PUTS("WITH funcdescs AS ( "
"SELECT p.oid as p_oid, oprname, "
"coalesce(obj_description(o.oid, ‘pg_operator‘),‘‘) as opdesc "
"FROM pg_proc p JOIN pg_operator o ON oprcode = p.oid ) "
"INSERT INTO pg_description "
" SELECT p_oid, ‘pg_proc‘::regclass, 0, "
" ‘implementation of ‘ || oprname || ‘ operator‘ "
" FROM funcdescs "
" WHERE opdesc NOT LIKE ‘deprecated%‘ AND "
" NOT EXISTS (SELECT 1 FROM pg_description "
" WHERE objoid = p_oid AND classoid = ‘pg_proc‘::regclass);\n");

PG_CMD_CLOSE;

check_ok();
}

#ifdef HAVE_LOCALE_T
/*
* "Normalize" a locale name, stripping off encoding tags such as
* ".utf8" (e.g., "en_US.utf8" -> "en_US", but "br_FR.iso885915@euro"
* -> "br_FR@euro"). Return true if a new, different name was
* generated.
*/
static bool
normalize_locale_name(char *new, const char *old)
{
char *n = new;
const char *o = old;
bool changed = false;

while (*o)
{
if (*o == ‘.‘)
{
/* skip over encoding tag such as ".utf8" or ".UTF-8" */
o++;
while ((*o >= ‘A‘ && *o <= ‘Z‘)
|| (*o >= ‘a‘ && *o <= ‘z‘)
|| (*o >= ‘0‘ && *o <= ‘9‘)
|| (*o == ‘-‘))
o++;
changed = true;
}
else
*n++ = *o++;
}
*n = ‘\0‘;

return changed;
}
#endif /* HAVE_LOCALE_T */

/*
* populate pg_collation
*/
static void
setup_collation(void)
{
#if defined(HAVE_LOCALE_T) && !defined(WIN32)
int i;
FILE *locale_a_handle;
char localebuf[NAMEDATALEN];
int count = 0;

PG_CMD_DECL;
#endif

fputs(_("creating collations ... "), stdout);
fflush(stdout);

#if defined(HAVE_LOCALE_T) && !defined(WIN32)
snprintf(cmd, sizeof(cmd),
"\"%s\" %s template1 >%s",
backend_exec, backend_options,
DEVNULL);

locale_a_handle = popen_check("locale -a", "r");
if (!locale_a_handle)
return; /* complaint already printed */

PG_CMD_OPEN;

PG_CMD_PUTS("CREATE TEMP TABLE tmp_pg_collation ( "
" collname name, "
" locale name, "
" encoding int) WITHOUT OIDS;\n");

while (fgets(localebuf, sizeof(localebuf), locale_a_handle))
{
size_t len;
int enc;
bool skip;
char *quoted_locale;
char alias[NAMEDATALEN];

len = strlen(localebuf);

if (len == 0 || localebuf[len - 1] != ‘\n‘)
{
if (debug)
fprintf(stderr, _("%s: locale name too long, skipped: \"%s\"\n"),
progname, localebuf);
continue;
}
localebuf[len - 1] = ‘\0‘;

/*
* Some systems have locale names that don‘t consist entirely of ASCII
* letters (such as "bokm&aring;l" or "fran&ccedil;ais"). This is
* pretty silly, since we need the locale itself to interpret the
* non-ASCII characters. We can‘t do much with those, so we filter
* them out.
*/
skip = false;
for (i = 0; i < len; i++)
{
if (IS_HIGHBIT_SET(localebuf[i]))
{
skip = true;
break;
}
}
if (skip)
{
if (debug)
fprintf(stderr, _("%s: locale name has non-ASCII characters, skipped: \"%s\"\n"),
progname, localebuf);
continue;
}

enc = pg_get_encoding_from_locale(localebuf, debug);
if (enc < 0)
{
/* error message printed by pg_get_encoding_from_locale() */
continue;
}
if (!PG_VALID_BE_ENCODING(enc))
continue; /* ignore locales for client-only encodings */
if (enc == PG_SQL_ASCII)
continue; /* C/POSIX are already in the catalog */

count++;

quoted_locale = escape_quotes(localebuf);

PG_CMD_PRINTF3("INSERT INTO tmp_pg_collation VALUES (E‘%s‘, E‘%s‘, %d);\n",
quoted_locale, quoted_locale, enc);

/*
* Generate aliases such as "en_US" in addition to "en_US.utf8" for
* ease of use. Note that collation names are unique per encoding
* only, so this doesn‘t clash with "en_US" for LATIN1, say.
*/
if (normalize_locale_name(alias, localebuf))
PG_CMD_PRINTF3("INSERT INTO tmp_pg_collation VALUES (E‘%s‘, E‘%s‘, %d);\n",
escape_quotes(alias), quoted_locale, enc);
}

/* Add an SQL-standard name */
PG_CMD_PRINTF1("INSERT INTO tmp_pg_collation VALUES (‘ucs_basic‘, ‘C‘, %d);\n", PG_UTF8);

/*
* When copying collations to the final location, eliminate aliases that
* conflict with an existing locale name for the same encoding. For
* example, "br_FR.iso88591" is normalized to "br_FR", both for encoding
* LATIN1. But the unnormalized locale "br_FR" already exists for LATIN1.
* Prefer the alias that matches the OS locale name, else the first locale
* name by sort order (arbitrary choice to be deterministic).
*
* Also, eliminate any aliases that conflict with pg_collation‘s
* hard-wired entries for "C" etc.
*/
PG_CMD_PUTS("INSERT INTO pg_collation (collname, collnamespace, collowner, collencoding, collcollate, collctype) "
" SELECT DISTINCT ON (collname, encoding)"
" collname, "
" (SELECT oid FROM pg_namespace WHERE nspname = ‘pg_catalog‘) AS collnamespace, "
" (SELECT relowner FROM pg_class WHERE relname = ‘pg_collation‘) AS collowner, "
" encoding, locale, locale "
" FROM tmp_pg_collation"
" WHERE NOT EXISTS (SELECT 1 FROM pg_collation WHERE collname = tmp_pg_collation.collname)"
" ORDER BY collname, encoding, (collname = locale) DESC, locale;\n");

pclose(locale_a_handle);
PG_CMD_CLOSE;

check_ok();
if (count == 0 && !debug)
{
printf(_("No usable system locales were found.\n"));
printf(_("Use the option \"--debug\" to see details.\n"));
}
#else /* not HAVE_LOCALE_T && not WIN32 */
printf(_("not supported on this platform\n"));
fflush(stdout);
#endif /* not HAVE_LOCALE_T && not WIN32 */
}

/*
* load conversion functions
*/
static void
setup_conversion(void)
{
PG_CMD_DECL;
char **line;
char **conv_lines;

fputs(_("creating conversions ... "), stdout);
fflush(stdout);

snprintf(cmd, sizeof(cmd),
"\"%s\" %s template1 >%s",
backend_exec, backend_options,
DEVNULL);

PG_CMD_OPEN;

conv_lines = readfile(conversion_file);
for (line = conv_lines; *line != NULL; line++)
{
if (strstr(*line, "DROP CONVERSION") != *line)
PG_CMD_PUTS(*line);
free(*line);
}

free(conv_lines);

PG_CMD_CLOSE;

check_ok();
}

/*
* load extra dictionaries (Snowball stemmers)
*/
static void
setup_dictionary(void)
{
PG_CMD_DECL;
char **line;
char **conv_lines;

fputs(_("creating dictionaries ... "), stdout);
fflush(stdout);

/*
* We use -j here to avoid backslashing stuff
*/
snprintf(cmd, sizeof(cmd),
"\"%s\" %s -j template1 >%s",
backend_exec, backend_options,
DEVNULL);

PG_CMD_OPEN;

conv_lines = readfile(dictionary_file);
for (line = conv_lines; *line != NULL; line++)
{
PG_CMD_PUTS(*line);
free(*line);
}

free(conv_lines);

PG_CMD_CLOSE;

check_ok();
}

/*
* Set up privileges
*
* We mark most system catalogs as world-readable. We don‘t currently have
* to touch functions, languages, or databases, because their default
* permissions are OK.
*
* Some objects may require different permissions by default, so we
* make sure we don‘t overwrite privilege sets that have already been
* set (NOT NULL).
*/
static void
setup_privileges(void)
{
PG_CMD_DECL;
char **line;
char **priv_lines;
static char *privileges_setup[] = {
"UPDATE pg_class "
" SET relacl = E‘{\"=r/\\\\\"$POSTGRES_SUPERUSERNAME\\\\\"\"}‘ "
" WHERE relkind IN (‘r‘, ‘v‘, ‘S‘) AND relacl IS NULL;\n",
"GRANT USAGE ON SCHEMA pg_catalog TO PUBLIC;\n",
"GRANT CREATE, USAGE ON SCHEMA public TO PUBLIC;\n",
"REVOKE ALL ON pg_largeobject FROM PUBLIC;\n",
NULL
};

fputs(_("setting privileges on built-in objects ... "), stdout);
fflush(stdout);

snprintf(cmd, sizeof(cmd),
"\"%s\" %s template1 >%s",
backend_exec, backend_options,
DEVNULL);

PG_CMD_OPEN;

priv_lines = replace_token(privileges_setup,
"$POSTGRES_SUPERUSERNAME", username);
for (line = priv_lines; *line != NULL; line++)
PG_CMD_PUTS(*line);

PG_CMD_CLOSE;

check_ok();
}

/*
* extract the strange version of version required for information schema
* (09.08.0007abc)
*/
static void
set_info_version(void)
{
char *letterversion;
long major = 0,
minor = 0,
micro = 0;
char *endptr;
char *vstr = xstrdup(PG_VERSION);
char *ptr;

ptr = vstr + (strlen(vstr) - 1);
while (ptr != vstr && (*ptr < ‘0‘ || *ptr > ‘9‘))
ptr--;
letterversion = ptr + 1;
major = strtol(vstr, &endptr, 10);
if (*endptr)
minor = strtol(endptr + 1, &endptr, 10);
if (*endptr)
micro = strtol(endptr + 1, &endptr, 10);
snprintf(infoversion, sizeof(infoversion), "%02ld.%02ld.%04ld%s",
major, minor, micro, letterversion);
}

/*
* load info schema and populate from features file
*/
static void
setup_schema(void)
{
PG_CMD_DECL;
char **line;
char **lines;

fputs(_("creating information schema ... "), stdout);
fflush(stdout);

lines = readfile(info_schema_file);

/*
* We use -j here to avoid backslashing stuff in information_schema.sql
*/
snprintf(cmd, sizeof(cmd),
"\"%s\" %s -j template1 >%s",
backend_exec, backend_options,
DEVNULL);

PG_CMD_OPEN;

for (line = lines; *line != NULL; line++)
{
PG_CMD_PUTS(*line);
free(*line);
}

free(lines);

PG_CMD_CLOSE;

snprintf(cmd, sizeof(cmd),
"\"%s\" %s template1 >%s",
backend_exec, backend_options,
DEVNULL);

PG_CMD_OPEN;

PG_CMD_PRINTF1("UPDATE information_schema.sql_implementation_info "
" SET character_value = ‘%s‘ "
" WHERE implementation_info_name = ‘DBMS VERSION‘;\n",
infoversion);

PG_CMD_PRINTF1("COPY information_schema.sql_features "
" (feature_id, feature_name, sub_feature_id, "
" sub_feature_name, is_supported, comments) "
" FROM E‘%s‘;\n",
escape_quotes(features_file));

PG_CMD_CLOSE;

check_ok();
}

/*
* load PL/pgsql server-side language
*/
static void
load_plpgsql(void)
{
PG_CMD_DECL;

fputs(_("loading PL/pgSQL server-side language ... "), stdout);
fflush(stdout);

snprintf(cmd, sizeof(cmd),
"\"%s\" %s template1 >%s",
backend_exec, backend_options,
DEVNULL);

PG_CMD_OPEN;

PG_CMD_PUTS("CREATE EXTENSION plpgsql;\n");

PG_CMD_CLOSE;

check_ok();
}

/*
* clean everything up in template1
*/
static void
vacuum_db(void)
{
PG_CMD_DECL;

fputs(_("vacuuming database template1 ... "), stdout);
fflush(stdout);

snprintf(cmd, sizeof(cmd),
"\"%s\" %s template1 >%s",
backend_exec, backend_options,
DEVNULL);

PG_CMD_OPEN;

PG_CMD_PUTS("ANALYZE;\nVACUUM FULL;\nVACUUM FREEZE;\n");

PG_CMD_CLOSE;

check_ok();
}

/*
* copy template1 to template0
*/
static void
make_template0(void)
{
PG_CMD_DECL;
const char **line;
static const char *template0_setup[] = {
"CREATE DATABASE template0;\n",
"UPDATE pg_database SET "
" datistemplate = ‘t‘, "
" datallowconn = ‘f‘ "
" WHERE datname = ‘template0‘;\n",

/*
* We use the OID of template0 to determine lastsysoid
*/
"UPDATE pg_database SET datlastsysoid = "
" (SELECT oid FROM pg_database "
" WHERE datname = ‘template0‘);\n",

/*
* Explicitly revoke public create-schema and create-temp-table
* privileges in template1 and template0; else the latter would be on
* by default
*/
"REVOKE CREATE,TEMPORARY ON DATABASE template1 FROM public;\n",
"REVOKE CREATE,TEMPORARY ON DATABASE template0 FROM public;\n",

"COMMENT ON DATABASE template0 IS ‘unmodifiable empty database‘;\n",

/*
* Finally vacuum to clean up dead rows in pg_database
*/
"VACUUM FULL pg_database;\n",
NULL
};

fputs(_("copying template1 to template0 ... "), stdout);
fflush(stdout);

snprintf(cmd, sizeof(cmd),
"\"%s\" %s template1 >%s",
backend_exec, backend_options,
DEVNULL);

PG_CMD_OPEN;

for (line = template0_setup; *line; line++)
PG_CMD_PUTS(*line);

PG_CMD_CLOSE;

check_ok();
}

/*
* copy template1 to postgres
*/
static void
make_postgres(void)
{
PG_CMD_DECL;
const char **line;
static const char *postgres_setup[] = {
"CREATE DATABASE postgres;\n",
"COMMENT ON DATABASE postgres IS ‘default administrative connection database‘;\n",
NULL
};

fputs(_("copying template1 to postgres ... "), stdout);
fflush(stdout);

snprintf(cmd, sizeof(cmd),
"\"%s\" %s template1 >%s",
backend_exec, backend_options,
DEVNULL);

PG_CMD_OPEN;

for (line = postgres_setup; *line; line++)
PG_CMD_PUTS(*line);

PG_CMD_CLOSE;

check_ok();
}


/*
* signal handler in case we are interrupted.
*
* The Windows runtime docs at
* http://msdn.microsoft.com/library/en-us/vclib/html/_crt_signal.asp
* specifically forbid a number of things being done from a signal handler,
* including IO, memory allocation and system calls, and only allow jmpbuf
* if you are handling SIGFPE.
*
* I avoided doing the forbidden things by setting a flag instead of calling
* exit_nicely() directly.
*
* Also note the behaviour of Windows with SIGINT, which says this:
* Note SIGINT is not supported for any Win32 application, including
* Windows 98/Me and Windows NT/2000/XP. When a CTRL+C interrupt occurs,
* Win32 operating systems generate a new thread to specifically handle
* that interrupt. This can cause a single-thread application such as UNIX,
* to become multithreaded, resulting in unexpected behavior.
*
* I have no idea how to handle this. (Strange they call UNIX an application!)
* So this will need some testing on Windows.
*/
static void
trapsig(int signum)
{
/* handle systems that reset the handler, like Windows (grr) */
pqsignal(signum, trapsig);
caught_signal = true;
}

/*
* call exit_nicely() if we got a signal, or else output "ok".
*/
static void
check_ok(void)
{
if (caught_signal)
{
printf(_("caught signal\n"));
fflush(stdout);
exit_nicely();
}
else if (output_failed)
{
printf(_("could not write to child process: %s\n"),
strerror(output_errno));
fflush(stdout);
exit_nicely();
}
else
{
/* all seems well */
printf(_("ok\n"));
fflush(stdout);
}
}

/*
* Escape (by doubling) any single quotes or backslashes in given string
*
* Note: this is used to process both postgresql.conf entries and SQL
* string literals. Since postgresql.conf strings are defined to treat
* backslashes as escapes, we have to double backslashes here. Hence,
* when using this for a SQL string literal, use E‘‘ syntax.
*
* We do not need to worry about encoding considerations because all
* valid backend encodings are ASCII-safe.
*/
static char *
escape_quotes(const char *src)
{
int len = strlen(src),
i,
j;
char *result = pg_malloc(len * 2 + 1);

for (i = 0, j = 0; i < len; i++)
{
if (SQL_STR_DOUBLE(src[i], true))
result[j++] = src[i];
result[j++] = src[i];
}
result[j] = ‘\0‘;
return result;
}

/* Hack to suppress a warning about %x from some versions of gcc */
static inline size_t
my_strftime(char *s, size_t max, const char *fmt, const struct tm * tm)
{
return strftime(s, max, fmt, tm);
}

/*
* Determine likely date order from locale
*/
static int
locale_date_order(const char *locale)
{
struct tm testtime;
char buf[128];
ch

热门排行

今日推荐

热门手游