2010-05-12 04:19:11 +02:00
|
|
|
/*
|
|
|
|
* server.c
|
|
|
|
*
|
|
|
|
* database server functions
|
2010-07-03 16:23:14 +02:00
|
|
|
*
|
2019-01-02 18:44:25 +01:00
|
|
|
* Copyright (c) 2010-2019, PostgreSQL Global Development Group
|
2015-03-11 03:33:25 +01:00
|
|
|
* src/bin/pg_upgrade/server.c
|
2010-05-12 04:19:11 +02:00
|
|
|
*/
|
|
|
|
|
Create libpgcommon, and move pg_malloc et al to it
libpgcommon is a new static library to allow sharing code among the
various frontend programs and backend; this lets us eliminate duplicate
implementations of common routines. We avoid libpgport, because that's
intended as a place for porting issues; per discussion, it seems better
to keep them separate.
The first use case, and the only implemented by this patch, is pg_malloc
and friends, which many frontend programs were already using.
At the same time, we can use this to provide palloc emulation functions
for the frontend; this way, some palloc-using files in the backend can
also be used by the frontend cleanly. To do this, we change palloc() in
the backend to be a function instead of a macro on top of
MemoryContextAlloc(). This was previously believed to cause loss of
performance, but this implementation has been tweaked by Tom and Andres
so that on modern compilers it provides a slight improvement over the
previous one.
This lets us clean up some places that were already with
localized hacks.
Most of the pg_malloc/palloc changes in this patch were authored by
Andres Freund. Zoltán Böszörményi also independently provided a form of
that. libpgcommon infrastructure was authored by Álvaro.
2013-02-12 14:33:40 +01:00
|
|
|
#include "postgres_fe.h"
|
2011-08-27 03:16:24 +02:00
|
|
|
|
Empty search_path in Autovacuum and non-psql/pgbench clients.
This makes the client programs behave as documented regardless of the
connect-time search_path and regardless of user-created objects. Today,
a malicious user with CREATE permission on a search_path schema can take
control of certain of these clients' queries and invoke arbitrary SQL
functions under the client identity, often a superuser. This is
exploitable in the default configuration, where all users have CREATE
privilege on schema "public".
This changes behavior of user-defined code stored in the database, like
pg_index.indexprs and pg_extension_config_dump(). If they reach code
bearing unqualified names, "does not exist" or "no schema has been
selected to create in" errors might appear. Users may fix such errors
by schema-qualifying affected names. After upgrading, consider watching
server logs for these errors.
The --table arguments of src/bin/scripts clients have been lax; for
example, "vacuumdb -Zt pg_am\;CHECKPOINT" performed a checkpoint. That
now fails, but for now, "vacuumdb -Zt 'pg_am(amname);CHECKPOINT'" still
performs a checkpoint.
Back-patch to 9.3 (all supported versions).
Reviewed by Tom Lane, though this fix strategy was not his first choice.
Reported by Arseniy Sharoglazov.
Security: CVE-2018-1058
2018-02-26 16:39:44 +01:00
|
|
|
#include "fe_utils/connect.h"
|
2016-08-08 16:07:46 +02:00
|
|
|
#include "fe_utils/string_utils.h"
|
2010-05-12 04:19:11 +02:00
|
|
|
#include "pg_upgrade.h"
|
|
|
|
|
2011-04-26 16:15:18 +02:00
|
|
|
static PGconn *get_db_conn(ClusterInfo *cluster, const char *db_name);
|
2010-05-12 04:19:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* connectToServer()
|
|
|
|
*
|
|
|
|
* Connects to the desired database on the designated server.
|
|
|
|
* If the connection attempt fails, this function logs an error
|
2011-04-06 22:00:44 +02:00
|
|
|
* message and calls exit() to kill the program.
|
2010-05-12 04:19:11 +02:00
|
|
|
*/
|
|
|
|
PGconn *
|
2011-01-01 18:06:36 +01:00
|
|
|
connectToServer(ClusterInfo *cluster, const char *db_name)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
2011-04-26 16:15:18 +02:00
|
|
|
PGconn *conn = get_db_conn(cluster, db_name);
|
2010-05-12 04:19:11 +02:00
|
|
|
|
|
|
|
if (conn == NULL || PQstatus(conn) != CONNECTION_OK)
|
|
|
|
{
|
2017-08-23 02:32:17 +02:00
|
|
|
pg_log(PG_REPORT, "connection to database failed: %s",
|
2010-05-12 04:19:11 +02:00
|
|
|
PQerrorMessage(conn));
|
|
|
|
|
|
|
|
if (conn)
|
|
|
|
PQfinish(conn);
|
|
|
|
|
2016-10-14 18:00:00 +02:00
|
|
|
printf(_("Failure, exiting\n"));
|
2011-04-06 22:00:44 +02:00
|
|
|
exit(1);
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
|
Empty search_path in Autovacuum and non-psql/pgbench clients.
This makes the client programs behave as documented regardless of the
connect-time search_path and regardless of user-created objects. Today,
a malicious user with CREATE permission on a search_path schema can take
control of certain of these clients' queries and invoke arbitrary SQL
functions under the client identity, often a superuser. This is
exploitable in the default configuration, where all users have CREATE
privilege on schema "public".
This changes behavior of user-defined code stored in the database, like
pg_index.indexprs and pg_extension_config_dump(). If they reach code
bearing unqualified names, "does not exist" or "no schema has been
selected to create in" errors might appear. Users may fix such errors
by schema-qualifying affected names. After upgrading, consider watching
server logs for these errors.
The --table arguments of src/bin/scripts clients have been lax; for
example, "vacuumdb -Zt pg_am\;CHECKPOINT" performed a checkpoint. That
now fails, but for now, "vacuumdb -Zt 'pg_am(amname);CHECKPOINT'" still
performs a checkpoint.
Back-patch to 9.3 (all supported versions).
Reviewed by Tom Lane, though this fix strategy was not his first choice.
Reported by Arseniy Sharoglazov.
Security: CVE-2018-1058
2018-02-26 16:39:44 +01:00
|
|
|
PQclear(executeQueryOrDie(conn, ALWAYS_SECURE_SEARCH_PATH_SQL));
|
|
|
|
|
2010-05-12 04:19:11 +02:00
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-26 16:15:18 +02:00
|
|
|
/*
|
|
|
|
* get_db_conn()
|
|
|
|
*
|
2012-09-03 19:52:34 +02:00
|
|
|
* get database connection, using named database + standard params for cluster
|
2011-04-26 16:15:18 +02:00
|
|
|
*/
|
|
|
|
static PGconn *
|
|
|
|
get_db_conn(ClusterInfo *cluster, const char *db_name)
|
|
|
|
{
|
2016-08-08 16:07:46 +02:00
|
|
|
PQExpBufferData conn_opts;
|
|
|
|
PGconn *conn;
|
|
|
|
|
|
|
|
/* Build connection string with proper quoting */
|
|
|
|
initPQExpBuffer(&conn_opts);
|
|
|
|
appendPQExpBufferStr(&conn_opts, "dbname=");
|
|
|
|
appendConnStrVal(&conn_opts, db_name);
|
|
|
|
appendPQExpBufferStr(&conn_opts, " user=");
|
|
|
|
appendConnStrVal(&conn_opts, os_info.user);
|
|
|
|
appendPQExpBuffer(&conn_opts, " port=%d", cluster->port);
|
2012-09-03 19:52:34 +02:00
|
|
|
if (cluster->sockdir)
|
2016-08-08 16:07:46 +02:00
|
|
|
{
|
|
|
|
appendPQExpBufferStr(&conn_opts, " host=");
|
|
|
|
appendConnStrVal(&conn_opts, cluster->sockdir);
|
|
|
|
}
|
2011-04-26 16:15:18 +02:00
|
|
|
|
2016-08-08 16:07:46 +02:00
|
|
|
conn = PQconnectdb(conn_opts.data);
|
|
|
|
termPQExpBuffer(&conn_opts);
|
|
|
|
return conn;
|
2011-04-26 16:15:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-03 19:52:34 +02:00
|
|
|
/*
|
|
|
|
* cluster_conn_opts()
|
|
|
|
*
|
|
|
|
* Return standard command-line options for connecting to this cluster when
|
|
|
|
* using psql, pg_dump, etc. Ideally this would match what get_db_conn()
|
|
|
|
* sets, but the utilities we need aren't very consistent about the treatment
|
|
|
|
* of database name options, so we leave that out.
|
|
|
|
*
|
2016-08-08 16:07:46 +02:00
|
|
|
* Result is valid until the next call to this function.
|
2012-09-03 19:52:34 +02:00
|
|
|
*/
|
|
|
|
char *
|
|
|
|
cluster_conn_opts(ClusterInfo *cluster)
|
|
|
|
{
|
2016-08-08 16:07:46 +02:00
|
|
|
static PQExpBuffer buf;
|
2012-09-03 19:52:34 +02:00
|
|
|
|
2016-08-08 16:07:46 +02:00
|
|
|
if (buf == NULL)
|
|
|
|
buf = createPQExpBuffer();
|
2012-09-03 19:52:34 +02:00
|
|
|
else
|
2016-08-08 16:07:46 +02:00
|
|
|
resetPQExpBuffer(buf);
|
|
|
|
|
|
|
|
if (cluster->sockdir)
|
|
|
|
{
|
|
|
|
appendPQExpBufferStr(buf, "--host ");
|
|
|
|
appendShellString(buf, cluster->sockdir);
|
|
|
|
appendPQExpBufferChar(buf, ' ');
|
|
|
|
}
|
|
|
|
appendPQExpBuffer(buf, "--port %d --username ", cluster->port);
|
|
|
|
appendShellString(buf, os_info.user);
|
2012-09-03 19:52:34 +02:00
|
|
|
|
2016-08-08 16:07:46 +02:00
|
|
|
return buf->data;
|
2012-09-03 19:52:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-12 04:19:11 +02:00
|
|
|
/*
|
|
|
|
* executeQueryOrDie()
|
|
|
|
*
|
|
|
|
* Formats a query string from the given arguments and executes the
|
|
|
|
* resulting query. If the query fails, this function logs an error
|
2011-04-06 22:00:44 +02:00
|
|
|
* message and calls exit() to kill the program.
|
2010-05-12 04:19:11 +02:00
|
|
|
*/
|
|
|
|
PGresult *
|
2010-10-19 23:38:16 +02:00
|
|
|
executeQueryOrDie(PGconn *conn, const char *fmt,...)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
2014-08-20 19:03:58 +02:00
|
|
|
static char query[QUERY_ALLOC];
|
2010-05-12 04:19:11 +02:00
|
|
|
va_list args;
|
|
|
|
PGresult *result;
|
|
|
|
ExecStatusType status;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
2014-08-20 19:03:58 +02:00
|
|
|
vsnprintf(query, sizeof(query), fmt, args);
|
2010-05-12 04:19:11 +02:00
|
|
|
va_end(args);
|
|
|
|
|
2014-08-20 19:03:58 +02:00
|
|
|
pg_log(PG_VERBOSE, "executing: %s\n", query);
|
|
|
|
result = PQexec(conn, query);
|
2010-05-12 04:19:11 +02:00
|
|
|
status = PQresultStatus(result);
|
|
|
|
|
|
|
|
if ((status != PGRES_TUPLES_OK) && (status != PGRES_COMMAND_OK))
|
|
|
|
{
|
2017-08-23 02:32:17 +02:00
|
|
|
pg_log(PG_REPORT, "SQL command failed\n%s\n%s", query,
|
2010-05-12 04:19:11 +02:00
|
|
|
PQerrorMessage(conn));
|
|
|
|
PQclear(result);
|
|
|
|
PQfinish(conn);
|
2016-10-14 18:00:00 +02:00
|
|
|
printf(_("Failure, exiting\n"));
|
2011-04-06 22:00:44 +02:00
|
|
|
exit(1);
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_major_server_version()
|
|
|
|
*
|
2011-10-01 15:59:27 +02:00
|
|
|
* gets the version (in unsigned int form) for the given datadir. Assumes
|
2010-05-12 04:19:11 +02:00
|
|
|
* that datadir is an absolute path to a valid pgdata directory. The version
|
|
|
|
* is retrieved by reading the PG_VERSION file.
|
|
|
|
*/
|
|
|
|
uint32
|
2011-01-01 18:28:48 +01:00
|
|
|
get_major_server_version(ClusterInfo *cluster)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
|
|
|
FILE *version_fd;
|
2011-01-01 18:28:48 +01:00
|
|
|
char ver_filename[MAXPGPATH];
|
2017-11-07 01:46:52 +01:00
|
|
|
int v1 = 0,
|
|
|
|
v2 = 0;
|
2010-05-12 04:19:11 +02:00
|
|
|
|
2011-10-01 15:59:27 +02:00
|
|
|
snprintf(ver_filename, sizeof(ver_filename), "%s/PG_VERSION",
|
|
|
|
cluster->pgdata);
|
2011-01-01 18:28:48 +01:00
|
|
|
if ((version_fd = fopen(ver_filename, "r")) == NULL)
|
2013-10-02 03:24:56 +02:00
|
|
|
pg_fatal("could not open version file: %s\n", ver_filename);
|
2010-05-12 04:19:11 +02:00
|
|
|
|
2011-01-01 18:28:48 +01:00
|
|
|
if (fscanf(version_fd, "%63s", cluster->major_version_str) == 0 ||
|
2017-11-07 01:46:52 +01:00
|
|
|
sscanf(cluster->major_version_str, "%d.%d", &v1, &v2) < 1)
|
2017-09-09 23:32:10 +02:00
|
|
|
pg_fatal("could not parse PG_VERSION file from %s\n", cluster->pgdata);
|
2011-03-09 03:35:42 +01:00
|
|
|
|
|
|
|
fclose(version_fd);
|
2010-05-12 04:19:11 +02:00
|
|
|
|
2017-11-07 01:46:52 +01:00
|
|
|
if (v1 < 10)
|
|
|
|
{
|
|
|
|
/* old style, e.g. 9.6.1 */
|
|
|
|
return v1 * 10000 + v2 * 100;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* new style, e.g. 10.1 */
|
|
|
|
return v1 * 10000;
|
|
|
|
}
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-06 22:00:44 +02:00
|
|
|
static void
|
|
|
|
stop_postmaster_atexit(void)
|
|
|
|
{
|
2011-04-26 02:17:48 +02:00
|
|
|
stop_postmaster(true);
|
2011-04-06 22:00:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-24 21:20:11 +01:00
|
|
|
bool
|
2018-01-09 04:43:51 +01:00
|
|
|
start_postmaster(ClusterInfo *cluster, bool report_and_exit_on_error)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
2012-09-03 19:52:34 +02:00
|
|
|
char cmd[MAXPGPATH * 4 + 1000];
|
2011-04-26 16:15:18 +02:00
|
|
|
PGconn *conn;
|
2012-08-27 20:21:09 +02:00
|
|
|
bool pg_ctl_return = false;
|
2012-09-03 19:52:34 +02:00
|
|
|
char socket_string[MAXPGPATH + 200];
|
2011-06-09 20:32:50 +02:00
|
|
|
|
2016-07-28 17:39:10 +02:00
|
|
|
static bool exit_hook_registered = false;
|
|
|
|
|
2011-04-06 22:00:44 +02:00
|
|
|
if (!exit_hook_registered)
|
|
|
|
{
|
|
|
|
atexit(stop_postmaster_atexit);
|
|
|
|
exit_hook_registered = true;
|
|
|
|
}
|
|
|
|
|
2012-09-03 19:52:34 +02:00
|
|
|
socket_string[0] = '\0';
|
|
|
|
|
|
|
|
#ifdef HAVE_UNIX_SOCKETS
|
|
|
|
/* prevent TCP/IP connections, restrict socket access */
|
|
|
|
strcat(socket_string,
|
2013-07-02 16:29:27 +02:00
|
|
|
" -c listen_addresses='' -c unix_socket_permissions=0700");
|
2012-09-03 19:52:34 +02:00
|
|
|
|
2013-05-29 22:58:43 +02:00
|
|
|
/* Have a sockdir? Tell the postmaster. */
|
2012-09-03 19:52:34 +02:00
|
|
|
if (cluster->sockdir)
|
|
|
|
snprintf(socket_string + strlen(socket_string),
|
|
|
|
sizeof(socket_string) - strlen(socket_string),
|
|
|
|
" -c %s='%s'",
|
|
|
|
(GET_MAJOR_VERSION(cluster->major_version) < 903) ?
|
|
|
|
"unix_socket_directory" : "unix_socket_directories",
|
|
|
|
cluster->sockdir);
|
|
|
|
#endif
|
|
|
|
|
2010-06-22 18:45:10 +02:00
|
|
|
/*
|
2014-08-04 17:45:45 +02:00
|
|
|
* Since PG 9.1, we have used -b to disable autovacuum. For earlier
|
|
|
|
* releases, setting autovacuum=off disables cleanup vacuum and analyze,
|
2015-05-24 03:35:49 +02:00
|
|
|
* but freeze vacuums can still happen, so we set
|
|
|
|
* autovacuum_freeze_max_age to its maximum.
|
|
|
|
* (autovacuum_multixact_freeze_max_age was introduced after 9.1, so there
|
|
|
|
* is no need to set that.) We assume all datfrozenxid and relfrozenxid
|
|
|
|
* values are less than a gap of 2000000000 from the current xid counter,
|
|
|
|
* so autovacuum will not touch them.
|
2012-11-06 20:28:48 +01:00
|
|
|
*
|
2012-12-04 04:47:59 +01:00
|
|
|
* Turn off durability requirements to improve object creation speed, and
|
|
|
|
* we only modify the new cluster, so only use it there. If there is a
|
|
|
|
* crash, the new cluster has to be recreated anyway. fsync=off is a big
|
|
|
|
* win on ext4.
|
2011-04-10 17:42:00 +02:00
|
|
|
*/
|
2010-05-14 02:32:21 +02:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
"\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" -o \"-p %d%s%s %s%s\" start",
|
|
|
|
cluster->bindir, SERVER_LOG_FILE, cluster->pgconfig, cluster->port,
|
2011-04-25 18:00:21 +02:00
|
|
|
(cluster->controldata.cat_ver >=
|
2012-11-06 20:28:48 +01:00
|
|
|
BINARY_UPGRADE_SERVER_FLAG_CAT_VER) ? " -b" :
|
|
|
|
" -c autovacuum=off -c autovacuum_freeze_max_age=2000000000",
|
2012-12-04 04:47:59 +01:00
|
|
|
(cluster == &new_cluster) ?
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
" -c synchronous_commit=off -c fsync=off -c full_page_writes=off" : "",
|
2012-09-03 19:52:34 +02:00
|
|
|
cluster->pgopts ? cluster->pgopts : "", socket_string);
|
2011-04-25 18:00:21 +02:00
|
|
|
|
2011-05-16 16:46:52 +02:00
|
|
|
/*
|
2011-06-09 20:32:50 +02:00
|
|
|
* Don't throw an error right away, let connecting throw the error because
|
|
|
|
* it might supply a reason for the failure.
|
2011-05-16 16:46:52 +02:00
|
|
|
*/
|
2012-08-27 20:21:09 +02:00
|
|
|
pg_ctl_return = exec_prog(SERVER_START_LOG_FILE,
|
2013-05-29 22:58:43 +02:00
|
|
|
/* pass both file names if they differ */
|
2012-08-27 20:21:09 +02:00
|
|
|
(strcmp(SERVER_LOG_FILE,
|
|
|
|
SERVER_START_LOG_FILE) != 0) ?
|
2012-08-07 19:10:44 +02:00
|
|
|
SERVER_LOG_FILE : NULL,
|
2018-01-09 04:43:51 +01:00
|
|
|
report_and_exit_on_error, false,
|
2012-06-10 21:20:04 +02:00
|
|
|
"%s", cmd);
|
2010-05-12 04:19:11 +02:00
|
|
|
|
2013-08-16 17:09:09 +02:00
|
|
|
/* Did it fail and we are just testing if the server could be started? */
|
2018-01-09 04:43:51 +01:00
|
|
|
if (!pg_ctl_return && !report_and_exit_on_error)
|
2013-01-24 21:20:11 +01:00
|
|
|
return false;
|
2013-05-29 22:58:43 +02:00
|
|
|
|
2013-08-16 17:09:09 +02:00
|
|
|
/*
|
2014-05-06 18:12:18 +02:00
|
|
|
* We set this here to make sure atexit() shuts down the server, but only
|
|
|
|
* if we started the server successfully. We do it before checking for
|
|
|
|
* connectivity in case the server started but there is a connectivity
|
|
|
|
* failure. If pg_ctl did not return success, we will exit below.
|
2013-08-16 17:09:09 +02:00
|
|
|
*
|
|
|
|
* Pre-9.1 servers do not have PQping(), so we could be leaving the server
|
2014-05-06 18:12:18 +02:00
|
|
|
* running if authentication was misconfigured, so someday we might went
|
|
|
|
* to be more aggressive about doing server shutdowns even if pg_ctl
|
|
|
|
* fails, but now (2013-08-14) it seems prudent to be cautious. We don't
|
|
|
|
* want to shutdown a server that might have been accidentally started
|
|
|
|
* during the upgrade.
|
2013-08-16 17:09:09 +02:00
|
|
|
*/
|
|
|
|
if (pg_ctl_return)
|
|
|
|
os_info.running_cluster = cluster;
|
|
|
|
|
|
|
|
/*
|
2014-05-06 18:12:18 +02:00
|
|
|
* pg_ctl -w might have failed because the server couldn't be started, or
|
|
|
|
* there might have been a connection problem in _checking_ if the server
|
|
|
|
* has started. Therefore, even if pg_ctl failed, we continue and test
|
|
|
|
* for connectivity in case we get a connection reason for the failure.
|
2013-08-16 17:09:09 +02:00
|
|
|
*/
|
2011-04-26 16:15:18 +02:00
|
|
|
if ((conn = get_db_conn(cluster, "template1")) == NULL ||
|
|
|
|
PQstatus(conn) != CONNECTION_OK)
|
|
|
|
{
|
2017-08-23 02:32:17 +02:00
|
|
|
pg_log(PG_REPORT, "\nconnection to database failed: %s",
|
2011-05-07 14:55:13 +02:00
|
|
|
PQerrorMessage(conn));
|
2011-06-09 20:32:50 +02:00
|
|
|
if (conn)
|
2011-04-26 16:15:18 +02:00
|
|
|
PQfinish(conn);
|
2017-07-15 01:20:21 +02:00
|
|
|
if (cluster == &old_cluster)
|
|
|
|
pg_fatal("could not connect to source postmaster started with the command:\n"
|
|
|
|
"%s\n",
|
|
|
|
cmd);
|
|
|
|
else
|
|
|
|
pg_fatal("could not connect to target postmaster started with the command:\n"
|
|
|
|
"%s\n",
|
|
|
|
cmd);
|
2011-04-26 16:15:18 +02:00
|
|
|
}
|
|
|
|
PQfinish(conn);
|
2010-05-12 04:19:11 +02:00
|
|
|
|
2013-08-16 17:09:09 +02:00
|
|
|
/*
|
2018-01-09 04:43:51 +01:00
|
|
|
* If pg_ctl failed, and the connection didn't fail, and
|
2018-04-26 20:47:16 +02:00
|
|
|
* report_and_exit_on_error is enabled, fail now. This could happen if
|
|
|
|
* the server was already running.
|
2013-08-16 17:09:09 +02:00
|
|
|
*/
|
2012-08-27 20:21:09 +02:00
|
|
|
if (!pg_ctl_return)
|
2018-01-05 20:46:27 +01:00
|
|
|
{
|
|
|
|
if (cluster == &old_cluster)
|
|
|
|
pg_fatal("pg_ctl failed to start the source server, or connection failed\n");
|
|
|
|
else
|
|
|
|
pg_fatal("pg_ctl failed to start the target server, or connection failed\n");
|
|
|
|
}
|
2011-06-09 20:32:50 +02:00
|
|
|
|
2013-01-24 21:20:11 +01:00
|
|
|
return true;
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2018-01-09 04:43:51 +01:00
|
|
|
stop_postmaster(bool in_atexit)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
2011-10-10 13:43:28 +02:00
|
|
|
ClusterInfo *cluster;
|
2011-06-09 20:32:50 +02:00
|
|
|
|
2011-01-01 18:06:36 +01:00
|
|
|
if (os_info.running_cluster == &old_cluster)
|
2011-10-10 13:43:28 +02:00
|
|
|
cluster = &old_cluster;
|
2011-01-01 18:06:36 +01:00
|
|
|
else if (os_info.running_cluster == &new_cluster)
|
2011-10-10 13:43:28 +02:00
|
|
|
cluster = &new_cluster;
|
2010-05-12 04:19:11 +02:00
|
|
|
else
|
2012-06-10 21:20:04 +02:00
|
|
|
return; /* no cluster running */
|
2010-05-12 04:19:11 +02:00
|
|
|
|
2018-01-09 04:43:51 +01:00
|
|
|
exec_prog(SERVER_STOP_LOG_FILE, NULL, !in_atexit, !in_atexit,
|
2012-08-27 20:21:09 +02:00
|
|
|
"\"%s/pg_ctl\" -w -D \"%s\" -o \"%s\" %s stop",
|
|
|
|
cluster->bindir, cluster->pgconfig,
|
|
|
|
cluster->pgopts ? cluster->pgopts : "",
|
2018-01-09 04:43:51 +01:00
|
|
|
in_atexit ? "-m fast" : "-m smart");
|
2010-05-12 04:19:11 +02:00
|
|
|
|
2011-01-01 18:06:36 +01:00
|
|
|
os_info.running_cluster = NULL;
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2011-05-16 16:46:52 +02:00
|
|
|
* check_pghost_envvar()
|
2010-05-12 04:19:11 +02:00
|
|
|
*
|
2011-05-16 16:46:52 +02:00
|
|
|
* Tests that PGHOST does not point to a non-local server
|
2010-05-12 04:19:11 +02:00
|
|
|
*/
|
|
|
|
void
|
2011-05-16 16:46:52 +02:00
|
|
|
check_pghost_envvar(void)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
|
|
|
PQconninfoOption *option;
|
|
|
|
PQconninfoOption *start;
|
|
|
|
|
|
|
|
/* Get valid libpq env vars from the PQconndefaults function */
|
|
|
|
|
2011-04-07 18:22:34 +02:00
|
|
|
start = PQconndefaults();
|
2010-05-12 04:19:11 +02:00
|
|
|
|
2013-12-03 17:11:56 +01:00
|
|
|
if (!start)
|
|
|
|
pg_fatal("out of memory\n");
|
2013-12-04 04:57:08 +01:00
|
|
|
|
2011-04-07 18:22:34 +02:00
|
|
|
for (option = start; option->keyword != NULL; option++)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
2011-05-16 16:46:52 +02:00
|
|
|
if (option->envvar && (strcmp(option->envvar, "PGHOST") == 0 ||
|
2011-06-09 20:32:50 +02:00
|
|
|
strcmp(option->envvar, "PGHOSTADDR") == 0))
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
2011-05-16 16:46:52 +02:00
|
|
|
const char *value = getenv(option->envvar);
|
|
|
|
|
|
|
|
if (value && strlen(value) > 0 &&
|
2011-06-09 20:32:50 +02:00
|
|
|
/* check for 'local' host values */
|
2011-05-16 16:46:52 +02:00
|
|
|
(strcmp(value, "localhost") != 0 && strcmp(value, "127.0.0.1") != 0 &&
|
|
|
|
strcmp(value, "::1") != 0 && value[0] != '/'))
|
2013-10-02 03:24:56 +02:00
|
|
|
pg_fatal("libpq environment variable %s has a non-local server value: %s\n",
|
2014-05-06 18:12:18 +02:00
|
|
|
option->envvar, value);
|
2011-04-07 18:22:34 +02:00
|
|
|
}
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the memory that libpq allocated on our behalf */
|
|
|
|
PQconninfoFree(start);
|
|
|
|
}
|