2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* server.c
|
|
|
|
*
|
|
|
|
* database server functions
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2013-01-02 06:15:01 +08:00
|
|
|
* Copyright (c) 2010-2013, PostgreSQL Global Development Group
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pg_upgrade/server.c
|
2010-05-12 10:19:11 +08: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 21:33:40 +08:00
|
|
|
#include "postgres_fe.h"
|
2011-08-27 09:16:24 +08:00
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
#include "pg_upgrade.h"
|
|
|
|
|
|
|
|
|
2011-04-26 22:15:18 +08:00
|
|
|
static PGconn *get_db_conn(ClusterInfo *cluster, const char *db_name);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* connectToServer()
|
|
|
|
*
|
|
|
|
* Connects to the desired database on the designated server.
|
|
|
|
* If the connection attempt fails, this function logs an error
|
2011-04-07 04:00:44 +08:00
|
|
|
* message and calls exit() to kill the program.
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
PGconn *
|
2011-01-02 01:06:36 +08:00
|
|
|
connectToServer(ClusterInfo *cluster, const char *db_name)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-04-26 22:15:18 +08:00
|
|
|
PGconn *conn = get_db_conn(cluster, db_name);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
if (conn == NULL || PQstatus(conn) != CONNECTION_OK)
|
|
|
|
{
|
2011-05-07 20:55:13 +08:00
|
|
|
pg_log(PG_REPORT, "connection to database failed: %s\n",
|
2010-05-12 10:19:11 +08:00
|
|
|
PQerrorMessage(conn));
|
|
|
|
|
|
|
|
if (conn)
|
|
|
|
PQfinish(conn);
|
|
|
|
|
2011-04-07 04:00:44 +08:00
|
|
|
printf("Failure, exiting\n");
|
|
|
|
exit(1);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-26 22:15:18 +08:00
|
|
|
/*
|
|
|
|
* get_db_conn()
|
|
|
|
*
|
2012-09-04 01:52:34 +08:00
|
|
|
* get database connection, using named database + standard params for cluster
|
2011-04-26 22:15:18 +08:00
|
|
|
*/
|
|
|
|
static PGconn *
|
|
|
|
get_db_conn(ClusterInfo *cluster, const char *db_name)
|
|
|
|
{
|
2012-09-04 01:52:34 +08:00
|
|
|
char conn_opts[2 * NAMEDATALEN + MAXPGPATH + 100];
|
2011-04-26 22:15:18 +08:00
|
|
|
|
2012-09-04 01:52:34 +08:00
|
|
|
if (cluster->sockdir)
|
|
|
|
snprintf(conn_opts, sizeof(conn_opts),
|
|
|
|
"dbname = '%s' user = '%s' host = '%s' port = %d",
|
|
|
|
db_name, os_info.user, cluster->sockdir, cluster->port);
|
|
|
|
else
|
|
|
|
snprintf(conn_opts, sizeof(conn_opts),
|
|
|
|
"dbname = '%s' user = '%s' port = %d",
|
|
|
|
db_name, os_info.user, cluster->port);
|
2011-04-26 22:15:18 +08:00
|
|
|
|
|
|
|
return PQconnectdb(conn_opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-04 01:52:34 +08: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.
|
|
|
|
*
|
|
|
|
* Note result is in static storage, so use it right away.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
cluster_conn_opts(ClusterInfo *cluster)
|
|
|
|
{
|
2013-05-30 04:58:43 +08:00
|
|
|
static char conn_opts[MAXPGPATH + NAMEDATALEN + 100];
|
2012-09-04 01:52:34 +08:00
|
|
|
|
|
|
|
if (cluster->sockdir)
|
|
|
|
snprintf(conn_opts, sizeof(conn_opts),
|
|
|
|
"--host \"%s\" --port %d --username \"%s\"",
|
|
|
|
cluster->sockdir, cluster->port, os_info.user);
|
|
|
|
else
|
|
|
|
snprintf(conn_opts, sizeof(conn_opts),
|
|
|
|
"--port %d --username \"%s\"",
|
|
|
|
cluster->port, os_info.user);
|
|
|
|
|
|
|
|
return conn_opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-12 10:19:11 +08: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-07 04:00:44 +08:00
|
|
|
* message and calls exit() to kill the program.
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
PGresult *
|
2010-10-20 05:38:16 +08:00
|
|
|
executeQueryOrDie(PGconn *conn, const char *fmt,...)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
static char command[8192];
|
|
|
|
va_list args;
|
|
|
|
PGresult *result;
|
|
|
|
ExecStatusType status;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(command, sizeof(command), fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2012-03-13 07:47:54 +08:00
|
|
|
pg_log(PG_VERBOSE, "executing: %s\n", command);
|
2010-05-12 10:19:11 +08:00
|
|
|
result = PQexec(conn, command);
|
|
|
|
status = PQresultStatus(result);
|
|
|
|
|
|
|
|
if ((status != PGRES_TUPLES_OK) && (status != PGRES_COMMAND_OK))
|
|
|
|
{
|
2011-07-12 12:13:51 +08:00
|
|
|
pg_log(PG_REPORT, "SQL command failed\n%s\n%s\n", command,
|
2010-05-12 10:19:11 +08:00
|
|
|
PQerrorMessage(conn));
|
|
|
|
PQclear(result);
|
|
|
|
PQfinish(conn);
|
2011-04-07 04:00:44 +08:00
|
|
|
printf("Failure, exiting\n");
|
|
|
|
exit(1);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_major_server_version()
|
|
|
|
*
|
2011-10-01 21:59:27 +08:00
|
|
|
* gets the version (in unsigned int form) for the given datadir. Assumes
|
2010-05-12 10:19:11 +08: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-02 01:28:48 +08:00
|
|
|
get_major_server_version(ClusterInfo *cluster)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
FILE *version_fd;
|
2011-01-02 01:28:48 +08:00
|
|
|
char ver_filename[MAXPGPATH];
|
2010-05-12 10:19:11 +08:00
|
|
|
int integer_version = 0;
|
|
|
|
int fractional_version = 0;
|
|
|
|
|
2011-10-01 21:59:27 +08:00
|
|
|
snprintf(ver_filename, sizeof(ver_filename), "%s/PG_VERSION",
|
|
|
|
cluster->pgdata);
|
2011-01-02 01:28:48 +08:00
|
|
|
if ((version_fd = fopen(ver_filename, "r")) == NULL)
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("could not open version file: %s\n", ver_filename);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-01-02 01:28:48 +08:00
|
|
|
if (fscanf(version_fd, "%63s", cluster->major_version_str) == 0 ||
|
|
|
|
sscanf(cluster->major_version_str, "%d.%d", &integer_version,
|
|
|
|
&fractional_version) != 2)
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("could not get version from %s\n", cluster->pgdata);
|
2011-03-09 10:35:42 +08:00
|
|
|
|
|
|
|
fclose(version_fd);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
return (100 * integer_version + fractional_version) * 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-07 04:00:44 +08:00
|
|
|
static void
|
|
|
|
stop_postmaster_atexit(void)
|
|
|
|
{
|
2011-04-26 08:17:48 +08:00
|
|
|
stop_postmaster(true);
|
2011-04-07 04:00:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-25 04:20:11 +08:00
|
|
|
bool
|
|
|
|
start_postmaster(ClusterInfo *cluster, bool throw_error)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2012-09-04 01:52:34 +08:00
|
|
|
char cmd[MAXPGPATH * 4 + 1000];
|
2011-04-26 22:15:18 +08:00
|
|
|
PGconn *conn;
|
2011-04-07 04:00:44 +08:00
|
|
|
bool exit_hook_registered = false;
|
2012-08-28 02:21:09 +08:00
|
|
|
bool pg_ctl_return = false;
|
2012-09-04 01:52:34 +08:00
|
|
|
char socket_string[MAXPGPATH + 200];
|
2011-06-10 02:32:50 +08:00
|
|
|
|
2011-04-07 04:00:44 +08:00
|
|
|
if (!exit_hook_registered)
|
|
|
|
{
|
|
|
|
atexit(stop_postmaster_atexit);
|
|
|
|
exit_hook_registered = true;
|
|
|
|
}
|
|
|
|
|
2012-09-04 01:52:34 +08:00
|
|
|
socket_string[0] = '\0';
|
|
|
|
|
|
|
|
#ifdef HAVE_UNIX_SOCKETS
|
|
|
|
/* prevent TCP/IP connections, restrict socket access */
|
|
|
|
strcat(socket_string,
|
2013-07-02 22:29:27 +08:00
|
|
|
" -c listen_addresses='' -c unix_socket_permissions=0700");
|
2012-09-04 01:52:34 +08:00
|
|
|
|
2013-05-30 04:58:43 +08:00
|
|
|
/* Have a sockdir? Tell the postmaster. */
|
2012-09-04 01:52:34 +08: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-23 00:45:10 +08:00
|
|
|
/*
|
2011-04-10 23:42:00 +08:00
|
|
|
* Using autovacuum=off disables cleanup vacuum and analyze, but freeze
|
|
|
|
* vacuums can still happen, so we set autovacuum_freeze_max_age to its
|
|
|
|
* maximum. We assume all datfrozenxid and relfrozen values are less than
|
|
|
|
* a gap of 2000000000 from the current xid counter, so autovacuum will
|
|
|
|
* not touch them.
|
2012-11-07 03:28:48 +08:00
|
|
|
*
|
2012-12-04 11:47:59 +08: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 23:42:00 +08:00
|
|
|
*/
|
2010-05-14 08:32:21 +08:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
2013-05-30 04:58:43 +08:00
|
|
|
"\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" -o \"-p %d%s%s %s%s\" start",
|
2012-06-11 03:20:04 +08:00
|
|
|
cluster->bindir, SERVER_LOG_FILE, cluster->pgconfig, cluster->port,
|
2011-04-26 00:00:21 +08:00
|
|
|
(cluster->controldata.cat_ver >=
|
2012-11-07 03:28:48 +08:00
|
|
|
BINARY_UPGRADE_SERVER_FLAG_CAT_VER) ? " -b" :
|
|
|
|
" -c autovacuum=off -c autovacuum_freeze_max_age=2000000000",
|
2012-12-04 11:47:59 +08:00
|
|
|
(cluster == &new_cluster) ?
|
2013-05-30 04:58:43 +08:00
|
|
|
" -c synchronous_commit=off -c fsync=off -c full_page_writes=off" : "",
|
2012-09-04 01:52:34 +08:00
|
|
|
cluster->pgopts ? cluster->pgopts : "", socket_string);
|
2011-04-26 00:00:21 +08:00
|
|
|
|
2011-05-16 22:46:52 +08:00
|
|
|
/*
|
2011-06-10 02:32:50 +08: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 22:46:52 +08:00
|
|
|
*/
|
2012-08-28 02:21:09 +08:00
|
|
|
pg_ctl_return = exec_prog(SERVER_START_LOG_FILE,
|
2013-05-30 04:58:43 +08:00
|
|
|
/* pass both file names if they differ */
|
2012-08-28 02:21:09 +08:00
|
|
|
(strcmp(SERVER_LOG_FILE,
|
|
|
|
SERVER_START_LOG_FILE) != 0) ?
|
2012-08-08 01:10:44 +08:00
|
|
|
SERVER_LOG_FILE : NULL,
|
2012-08-28 02:21:09 +08:00
|
|
|
false,
|
2012-06-11 03:20:04 +08:00
|
|
|
"%s", cmd);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2013-08-16 23:09:09 +08:00
|
|
|
/* Did it fail and we are just testing if the server could be started? */
|
2013-01-25 04:20:11 +08:00
|
|
|
if (!pg_ctl_return && !throw_error)
|
|
|
|
return false;
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2013-08-16 23:09:09 +08: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.
|
|
|
|
*
|
|
|
|
* Pre-9.1 servers do not have PQping(), so we could be leaving the server
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
if (pg_ctl_return)
|
|
|
|
os_info.running_cluster = cluster;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-04-26 22:15:18 +08:00
|
|
|
if ((conn = get_db_conn(cluster, "template1")) == NULL ||
|
|
|
|
PQstatus(conn) != CONNECTION_OK)
|
|
|
|
{
|
2011-05-07 20:55:13 +08:00
|
|
|
pg_log(PG_REPORT, "\nconnection to database failed: %s\n",
|
|
|
|
PQerrorMessage(conn));
|
2011-06-10 02:32:50 +08:00
|
|
|
if (conn)
|
2011-04-26 22:15:18 +08:00
|
|
|
PQfinish(conn);
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("could not connect to %s postmaster started with the command:\n"
|
2012-08-28 02:21:09 +08:00
|
|
|
"%s\n",
|
2011-01-02 01:06:36 +08:00
|
|
|
CLUSTER_NAME(cluster), cmd);
|
2011-04-26 22:15:18 +08:00
|
|
|
}
|
|
|
|
PQfinish(conn);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2013-08-16 23:09:09 +08:00
|
|
|
/*
|
|
|
|
* If pg_ctl failed, and the connection didn't fail, and throw_error is
|
|
|
|
* enabled, fail now. This could happen if the server was already running.
|
|
|
|
*/
|
2012-08-28 02:21:09 +08:00
|
|
|
if (!pg_ctl_return)
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("pg_ctl failed to start the %s server, or connection failed\n",
|
2011-06-10 02:32:50 +08:00
|
|
|
CLUSTER_NAME(cluster));
|
|
|
|
|
2013-01-25 04:20:11 +08:00
|
|
|
return true;
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2011-04-26 08:17:48 +08:00
|
|
|
stop_postmaster(bool fast)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-10-10 19:43:28 +08:00
|
|
|
ClusterInfo *cluster;
|
2011-06-10 02:32:50 +08:00
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
if (os_info.running_cluster == &old_cluster)
|
2011-10-10 19:43:28 +08:00
|
|
|
cluster = &old_cluster;
|
2011-01-02 01:06:36 +08:00
|
|
|
else if (os_info.running_cluster == &new_cluster)
|
2011-10-10 19:43:28 +08:00
|
|
|
cluster = &new_cluster;
|
2010-05-12 10:19:11 +08:00
|
|
|
else
|
2012-06-11 03:20:04 +08:00
|
|
|
return; /* no cluster running */
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-08-28 02:21:09 +08:00
|
|
|
exec_prog(SERVER_STOP_LOG_FILE, NULL, !fast,
|
|
|
|
"\"%s/pg_ctl\" -w -D \"%s\" -o \"%s\" %s stop",
|
|
|
|
cluster->bindir, cluster->pgconfig,
|
|
|
|
cluster->pgopts ? cluster->pgopts : "",
|
|
|
|
fast ? "-m fast" : "");
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
os_info.running_cluster = NULL;
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2011-05-16 22:46:52 +08:00
|
|
|
* check_pghost_envvar()
|
2010-05-12 10:19:11 +08:00
|
|
|
*
|
2011-05-16 22:46:52 +08:00
|
|
|
* Tests that PGHOST does not point to a non-local server
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
void
|
2011-05-16 22:46:52 +08:00
|
|
|
check_pghost_envvar(void)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
PQconninfoOption *option;
|
|
|
|
PQconninfoOption *start;
|
|
|
|
|
|
|
|
/* Get valid libpq env vars from the PQconndefaults function */
|
|
|
|
|
2011-04-08 00:22:34 +08:00
|
|
|
start = PQconndefaults();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2013-12-04 00:11:56 +08:00
|
|
|
if (!start)
|
|
|
|
pg_fatal("out of memory\n");
|
2013-12-04 11:57:08 +08:00
|
|
|
|
2011-04-08 00:22:34 +08:00
|
|
|
for (option = start; option->keyword != NULL; option++)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-05-16 22:46:52 +08:00
|
|
|
if (option->envvar && (strcmp(option->envvar, "PGHOST") == 0 ||
|
2011-06-10 02:32:50 +08:00
|
|
|
strcmp(option->envvar, "PGHOSTADDR") == 0))
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-05-16 22:46:52 +08:00
|
|
|
const char *value = getenv(option->envvar);
|
|
|
|
|
|
|
|
if (value && strlen(value) > 0 &&
|
2011-06-10 02:32:50 +08:00
|
|
|
/* check for 'local' host values */
|
2011-05-16 22:46:52 +08:00
|
|
|
(strcmp(value, "localhost") != 0 && strcmp(value, "127.0.0.1") != 0 &&
|
|
|
|
strcmp(value, "::1") != 0 && value[0] != '/'))
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("libpq environment variable %s has a non-local server value: %s\n",
|
2011-06-10 02:32:50 +08:00
|
|
|
option->envvar, value);
|
2011-04-08 00:22:34 +08:00
|
|
|
}
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the memory that libpq allocated on our behalf */
|
|
|
|
PQconninfoFree(start);
|
|
|
|
}
|