2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* server.c
|
|
|
|
*
|
|
|
|
* database server functions
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2011-01-02 02:18:15 +08:00
|
|
|
* Copyright (c) 2010-2011, 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
|
|
|
*/
|
|
|
|
|
2011-08-27 09:16:24 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
|
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()
|
|
|
|
*
|
|
|
|
* get database connection
|
|
|
|
*/
|
|
|
|
static PGconn *
|
|
|
|
get_db_conn(ClusterInfo *cluster, const char *db_name)
|
|
|
|
{
|
|
|
|
char conn_opts[MAXPGPATH];
|
|
|
|
|
|
|
|
snprintf(conn_opts, sizeof(conn_opts),
|
2011-06-10 02:32:50 +08:00
|
|
|
"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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_DEBUG, "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)
|
2010-05-12 10:19:11 +08:00
|
|
|
return 0;
|
|
|
|
|
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)
|
2011-10-01 21:59:27 +08:00
|
|
|
pg_log(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
|
|
|
|
#ifdef HAVE_ATEXIT
|
|
|
|
stop_postmaster_atexit(void)
|
|
|
|
#else
|
|
|
|
stop_postmaster_on_exit(int exitstatus, void *arg)
|
|
|
|
#endif
|
|
|
|
{
|
2011-04-26 08:17:48 +08:00
|
|
|
stop_postmaster(true);
|
2011-04-07 04:00:44 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
void
|
2011-04-26 08:17:48 +08:00
|
|
|
start_postmaster(ClusterInfo *cluster)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
char cmd[MAXPGPATH];
|
2011-04-26 22:15:18 +08:00
|
|
|
PGconn *conn;
|
2011-04-07 04:00:44 +08:00
|
|
|
bool exit_hook_registered = false;
|
2011-05-16 22:46:52 +08:00
|
|
|
int pg_ctl_return = 0;
|
2011-06-10 02:32:50 +08:00
|
|
|
|
2011-04-07 04:00:44 +08:00
|
|
|
if (!exit_hook_registered)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_ATEXIT
|
|
|
|
atexit(stop_postmaster_atexit);
|
|
|
|
#else
|
|
|
|
on_exit(stop_postmaster_on_exit);
|
|
|
|
#endif
|
|
|
|
exit_hook_registered = true;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2010-05-14 08:32:21 +08:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
2011-04-26 08:17:48 +08:00
|
|
|
SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" "
|
2011-10-10 19:43:28 +08:00
|
|
|
"-o \"-p %d %s %s\" start >> \"%s\" 2>&1" SYSTEMQUOTE,
|
2011-10-08 02:40:23 +08:00
|
|
|
cluster->bindir, log_opts.filename2, cluster->pgconfig, cluster->port,
|
2011-04-26 00:00:21 +08:00
|
|
|
(cluster->controldata.cat_ver >=
|
2011-06-10 02:32:50 +08:00
|
|
|
BINARY_UPGRADE_SERVER_FLAG_CAT_VER) ? "-b" :
|
|
|
|
"-c autovacuum=off -c autovacuum_freeze_max_age=2000000000",
|
2011-10-10 19:43:28 +08:00
|
|
|
cluster->pgopts ? cluster->pgopts : "", log_opts.filename2);
|
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
|
|
|
*/
|
|
|
|
pg_ctl_return = exec_prog(false, "%s", cmd);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-04-26 22:15:18 +08:00
|
|
|
/* Check to see if we can connect to the server; if not, report it. */
|
|
|
|
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);
|
2011-07-12 12:13:51 +08:00
|
|
|
pg_log(PG_FATAL, "could not connect to %s postmaster started with the command: %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
|
|
|
|
2011-05-16 22:46:52 +08:00
|
|
|
/* If the connection didn't fail, fail now */
|
|
|
|
if (pg_ctl_return != 0)
|
|
|
|
pg_log(PG_FATAL, "pg_ctl failed to start the %s server\n",
|
2011-06-10 02:32:50 +08:00
|
|
|
CLUSTER_NAME(cluster));
|
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
os_info.running_cluster = cluster;
|
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
|
|
|
{
|
2010-06-23 00:45:10 +08:00
|
|
|
char cmd[MAXPGPATH];
|
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
|
2011-10-10 19:43:28 +08:00
|
|
|
return; /* no cluster running */
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-06-23 00:45:10 +08:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
2011-10-10 19:43:28 +08:00
|
|
|
SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" -o \"%s\" "
|
|
|
|
"%s stop >> \"%s\" 2>&1" SYSTEMQUOTE,
|
|
|
|
cluster->bindir, log_opts.filename2, cluster->pgconfig,
|
|
|
|
cluster->pgopts ? cluster->pgopts : "",
|
|
|
|
fast ? "-m fast" : "", log_opts.filename2);
|
2011-04-26 22:15:18 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
exec_prog(fast ? false : true, "%s", cmd);
|
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
|
|
|
|
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] != '/'))
|
|
|
|
pg_log(PG_FATAL,
|
2011-06-10 02:32:50 +08:00
|
|
|
"libpq environment variable %s has a non-local server value: %s\n",
|
|
|
|
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);
|
|
|
|
}
|