4 * database server functions
6 * Copyright (c) 2010-2025, PostgreSQL Global Development Group
7 * src/bin/pg_upgrade/server.c
23 * Connects to the desired database on the designated server.
24 * If the connection attempt fails, this function logs an error
25 * message and calls exit() to kill the program.
39 printf(
_(
"Failure, exiting\n"));
52 * get database connection, using named database + standard params for cluster
54 * Caller must check for connection failure!
62 /* Build connection string with proper quoting */
84 * Return standard command-line options for connecting to this cluster when
85 * using psql, pg_dump, etc. Ideally this would match what get_db_conn()
86 * sets, but the utilities we need aren't very consistent about the treatment
87 * of database name options, so we leave that out.
89 * Result is valid until the next call to this function.
115 * executeQueryOrDie()
117 * Formats a query string from the given arguments and executes the
118 * resulting query. If the query fails, this function logs an error
119 * message and calls exit() to kill the program.
143 printf(
_(
"Failure, exiting\n"));
152 * get_major_server_version()
154 * gets the version (in unsigned int form) for the given datadir. Assumes
155 * that datadir is an absolute path to a valid pgdata directory. The version
156 * is retrieved by reading the PG_VERSION file.
166 snprintf(ver_filename,
sizeof(ver_filename),
"%s/PG_VERSION",
168 if ((version_fd = fopen(ver_filename,
"r")) == NULL)
169 pg_fatal(
"could not open version file \"%s\": %m", ver_filename);
171 if (fscanf(version_fd,
"%63s",
cluster->major_version_str) == 0 ||
172 sscanf(
cluster->major_version_str,
"%d.%d", &v1, &v2) < 1)
173 pg_fatal(
"could not parse version file \"%s\"", ver_filename);
179 /* old style, e.g. 9.6.1 */
180 return v1 * 10000 + v2 * 100;
184 /* new style, e.g. 10.1 */
202 bool pg_ctl_return =
false;
206 static bool exit_hook_registered =
false;
208 if (!exit_hook_registered)
211 exit_hook_registered =
true;
214 socket_string[0] =
'0円';
217 /* prevent TCP/IP connections, restrict socket access */
218 strcat(socket_string,
219 " -c listen_addresses='' -c unix_socket_permissions=0700");
221 /* Have a sockdir? Tell the postmaster. */
223 snprintf(socket_string + strlen(socket_string),
224 sizeof(socket_string) - strlen(socket_string),
227 "unix_socket_directory" :
"unix_socket_directories",
234 * Construct a parameter string which is passed to the server process.
236 * Turn off durability requirements to improve object creation speed, and
237 * we only modify the new cluster, so only use it there. If there is a
238 * crash, the new cluster has to be recreated anyway. fsync=off is a big
242 appendPQExpBufferStr(&pgoptions,
" -c synchronous_commit=off -c fsync=off -c full_page_writes=off");
245 * Use -b to disable autovacuum and logical replication launcher
246 * (effective in PG17 or later for the latter).
249 "\"%s/pg_ctl\" -w -l \"%s/%s\" -D \"%s\" -o \"-p %d -b%s %s%s\" start",
259 * Don't throw an error right away, let connecting throw the error because
260 * it might supply a reason for the failure.
263 /* pass both file names if they differ */
267 report_and_exit_on_error,
false,
270 /* Did it fail and we are just testing if the server could be started? */
271 if (!pg_ctl_return && !report_and_exit_on_error)
275 * We set this here to make sure atexit() shuts down the server, but only
276 * if we started the server successfully. We do it before checking for
277 * connectivity in case the server started but there is a connectivity
278 * failure. If pg_ctl did not return success, we will exit below.
280 * Pre-9.1 servers do not have PQping(), so we could be leaving the server
281 * running if authentication was misconfigured, so someday we might went
282 * to be more aggressive about doing server shutdowns even if pg_ctl
283 * fails, but now (2013年08月14日) it seems prudent to be cautious. We don't
284 * want to shutdown a server that might have been accidentally started
285 * during the upgrade.
291 * pg_ctl -w might have failed because the server couldn't be started, or
292 * there might have been a connection problem in _checking_ if the server
293 * has started. Therefore, even if pg_ctl failed, we continue and test
294 * for connectivity in case we get a connection reason for the failure.
303 pg_fatal(
"could not connect to source postmaster started with the command:\n"
307 pg_fatal(
"could not connect to target postmaster started with the command:\n"
314 * If pg_ctl failed, and the connection didn't fail, and
315 * report_and_exit_on_error is enabled, fail now. This could happen if
316 * the server was already running.
321 pg_fatal(
"pg_ctl failed to start the source server, or connection failed");
323 pg_fatal(
"pg_ctl failed to start the target server, or connection failed");
340 return;
/* no cluster running */
343 "\"%s/pg_ctl\" -w -D \"%s\" -o \"%s\" %s stop",
346 in_atexit ?
"-m fast" :
"-m smart");
353 * check_pghost_envvar()
355 * Tests that PGHOST does not point to a non-local server
363 /* Get valid libpq env vars from the PQconndefaults function */
372 if (
option->envvar && (strcmp(
option->envvar,
"PGHOST") == 0 ||
373 strcmp(
option->envvar,
"PGHOSTADDR") == 0))
378 /* check for 'local' host values */
379 (strcmp(
value,
"localhost") != 0 && strcmp(
value,
"127.0.0.1") != 0 &&
381 pg_fatal(
"libpq environment variable %s has a non-local server value: %s",
386 /* Free the memory that libpq allocated on our behalf */
bool exec_prog(const char *log_filename, const char *opt_log_file, bool report_error, bool exit_on_error, const char *fmt,...)
void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
#define ALWAYS_SECURE_SEARCH_PATH_SQL
PGconn * PQconnectdb(const char *conninfo)
void PQconninfoFree(PQconninfoOption *connOptions)
ConnStatusType PQstatus(const PGconn *conn)
void PQfinish(PGconn *conn)
PQconninfoOption * PQconndefaults(void)
char * PQerrorMessage(const PGconn *conn)
PGresult * PQexec(PGconn *conn, const char *query)
#define SERVER_START_LOG_FILE
#define SERVER_STOP_LOG_FILE
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
#define GET_MAJOR_VERSION(v)
static bool is_unixsock_path(const char *path)
PQExpBuffer createPQExpBuffer(void)
void initPQExpBuffer(PQExpBuffer str)
void resetPQExpBuffer(PQExpBuffer str)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
void termPQExpBuffer(PQExpBuffer str)
static PGconn * get_db_conn(ClusterInfo *cluster, const char *db_name)
void check_pghost_envvar(void)
bool start_postmaster(ClusterInfo *cluster, bool report_and_exit_on_error)
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...)
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
void stop_postmaster(bool in_atexit)
uint32 get_major_server_version(ClusterInfo *cluster)
char * cluster_conn_opts(ClusterInfo *cluster)
static void stop_postmaster_atexit(void)
void appendShellString(PQExpBuffer buf, const char *str)
void appendConnStrVal(PQExpBuffer buf, const char *str)
ClusterInfo * running_cluster