1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +03:00

Back-patch recent pg_upgrade fixes into 9.2.

This syncs contrib/pg_upgrade in the 9.2 branch with HEAD, except for the
HEAD changes related to converting XLogRecPtr to 64-bit int.  It includes
back-patching these commits:

666d494d19
pg_upgrade: abstract out copying of files from old cluster to new
7afa8bed65
pg_upgrade: Run the created scripts in the test suite
ab577e63fa
Remove analyze_new_cluster.sh on make clean, too
34c02044ed
Fix thinko in comment
088c065ce8
pg_upgrade: Fix exec_prog API to be less flaky
f763b77193
Fix pg_upgrade to cope with non-default unix_socket_directory scenarios.
This commit is contained in:
Tom Lane
2012-09-03 15:03:08 -04:00
parent b681a874d9
commit 5c7e91e9c3
11 changed files with 274 additions and 146 deletions

View File

@ -46,21 +46,54 @@ connectToServer(ClusterInfo *cluster, const char *db_name)
/*
* get_db_conn()
*
* get database connection
* get database connection, using named database + standard params for cluster
*/
static PGconn *
get_db_conn(ClusterInfo *cluster, const char *db_name)
{
char conn_opts[MAXPGPATH];
char conn_opts[2 * NAMEDATALEN + MAXPGPATH + 100];
snprintf(conn_opts, sizeof(conn_opts),
"dbname = '%s' user = '%s' port = %d", db_name, os_info.user,
cluster->port);
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);
return PQconnectdb(conn_opts);
}
/*
* 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)
{
static char conn_opts[MAXPGPATH + NAMEDATALEN + 100];
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;
}
/*
* executeQueryOrDie()
*
@ -140,10 +173,11 @@ stop_postmaster_atexit(void)
void
start_postmaster(ClusterInfo *cluster)
{
char cmd[MAXPGPATH];
char cmd[MAXPGPATH * 4 + 1000];
PGconn *conn;
bool exit_hook_registered = false;
int pg_ctl_return = 0;
bool pg_ctl_return = false;
char socket_string[MAXPGPATH + 200];
if (!exit_hook_registered)
{
@ -151,6 +185,23 @@ start_postmaster(ClusterInfo *cluster)
exit_hook_registered = true;
}
socket_string[0] = '\0';
#ifdef HAVE_UNIX_SOCKETS
/* prevent TCP/IP connections, restrict socket access */
strcat(socket_string,
" -c listen_addresses='' -c unix_socket_permissions=0700");
/* Have a sockdir? Tell the postmaster. */
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
/*
* Using autovacuum=off disables cleanup vacuum and analyze, but freeze
* vacuums can still happen, so we set autovacuum_freeze_max_age to its
@ -159,22 +210,23 @@ start_postmaster(ClusterInfo *cluster)
* not touch them.
*/
snprintf(cmd, sizeof(cmd),
SYSTEMQUOTE "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" "
"-o \"-p %d %s %s\" start >> \"%s\" 2>&1" SYSTEMQUOTE,
"\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" -o \"-p %d %s %s%s\" start",
cluster->bindir, SERVER_LOG_FILE, cluster->pgconfig, cluster->port,
(cluster->controldata.cat_ver >=
BINARY_UPGRADE_SERVER_FLAG_CAT_VER) ? "-b" :
"-c autovacuum=off -c autovacuum_freeze_max_age=2000000000",
cluster->pgopts ? cluster->pgopts : "", SERVER_START_LOG_FILE);
cluster->pgopts ? cluster->pgopts : "", socket_string);
/*
* Don't throw an error right away, let connecting throw the error because
* it might supply a reason for the failure.
*/
pg_ctl_return = exec_prog(false, true, SERVER_START_LOG_FILE,
/* pass both file names if the differ */
(strcmp(SERVER_LOG_FILE, SERVER_START_LOG_FILE) != 0) ?
pg_ctl_return = exec_prog(SERVER_START_LOG_FILE,
/* pass both file names if they differ */
(strcmp(SERVER_LOG_FILE,
SERVER_START_LOG_FILE) != 0) ?
SERVER_LOG_FILE : NULL,
false,
"%s", cmd);
/* Check to see if we can connect to the server; if not, report it. */
@ -185,13 +237,14 @@ start_postmaster(ClusterInfo *cluster)
PQerrorMessage(conn));
if (conn)
PQfinish(conn);
pg_log(PG_FATAL, "could not connect to %s postmaster started with the command: %s\n",
pg_log(PG_FATAL, "could not connect to %s postmaster started with the command:\n"
"%s\n",
CLUSTER_NAME(cluster), cmd);
}
PQfinish(conn);
/* If the connection didn't fail, fail now */
if (pg_ctl_return != 0)
if (!pg_ctl_return)
pg_log(PG_FATAL, "pg_ctl failed to start the %s server, or connection failed\n",
CLUSTER_NAME(cluster));
@ -202,7 +255,6 @@ start_postmaster(ClusterInfo *cluster)
void
stop_postmaster(bool fast)
{
char cmd[MAXPGPATH];
ClusterInfo *cluster;
if (os_info.running_cluster == &old_cluster)
@ -212,14 +264,11 @@ stop_postmaster(bool fast)
else
return; /* no cluster running */
snprintf(cmd, sizeof(cmd),
SYSTEMQUOTE "\"%s/pg_ctl\" -w -D \"%s\" -o \"%s\" "
"%s stop >> \"%s\" 2>&1" SYSTEMQUOTE,
cluster->bindir, cluster->pgconfig,
cluster->pgopts ? cluster->pgopts : "",
fast ? "-m fast" : "", SERVER_STOP_LOG_FILE);
exec_prog(fast ? false : true, true, SERVER_STOP_LOG_FILE, NULL, "%s", cmd);
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" : "");
os_info.running_cluster = NULL;
}