1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Fix pg_upgrade to cope with non-default unix_socket_directory scenarios.

When starting either an old or new postmaster, force it to place its Unix
socket in the current directory.  This makes it even harder for accidental
connections to occur during pg_upgrade, and also works around some
scenarios where the default socket location isn't usable.  (For example,
if the default location is something other than "/tmp", it might not exist
during "make check".)

When checking an already-running old postmaster, find out its actual socket
directory location from postmaster.pid, if possible.  This dodges problems
with an old postmaster having a configured location different from the
default built into pg_upgrade's libpq.  We can't find that out if the old
postmaster is pre-9.1, so also document how to cope with such scenarios
manually.

In support of this, centralize handling of the connection-related command
line options passed to pg_upgrade's subsidiary programs, such as pg_dump.
This should make future changes easier.

Bruce Momjian and Tom Lane
This commit is contained in:
Tom Lane
2012-09-03 13:52:34 -04:00
parent c1f3c045cd
commit f763b77193
7 changed files with 149 additions and 20 deletions

View File

@ -9,6 +9,8 @@
#include "postgres.h"
#include "miscadmin.h"
#include "pg_upgrade.h"
#include <getopt_long.h>
@ -376,3 +378,64 @@ adjust_data_dir(ClusterInfo *cluster)
check_ok();
}
/*
* get_sock_dir
*
* Identify the socket directory to use for this cluster. If we're doing
* a live check (old cluster only), we need to find out where the postmaster
* is listening. Otherwise, we're going to put the socket into the current
* directory.
*/
void
get_sock_dir(ClusterInfo *cluster, bool live_check)
{
#ifdef HAVE_UNIX_SOCKETS
if (!live_check)
{
/* Use the current directory for the socket */
cluster->sockdir = pg_malloc(MAXPGPATH);
if (!getcwd(cluster->sockdir, MAXPGPATH))
pg_log(PG_FATAL, "cannot find current directory\n");
}
else
{
/*
* If we are doing a live check, we will use the old cluster's Unix
* domain socket directory so we can connect to the live server.
*/
/* sockdir was added to postmaster.pid in PG 9.1 */
if (GET_MAJOR_VERSION(cluster->major_version) >= 901)
{
char filename[MAXPGPATH];
FILE *fp;
int i;
snprintf(filename, sizeof(filename), "%s/postmaster.pid",
cluster->pgdata);
if ((fp = fopen(filename, "r")) == NULL)
pg_log(PG_FATAL, "Could not get socket directory of the old server\n");
cluster->sockdir = pg_malloc(MAXPGPATH);
for (i = 0; i < LOCK_FILE_LINE_SOCKET_DIR; i++)
if (fgets(cluster->sockdir, MAXPGPATH, fp) == NULL)
pg_log(PG_FATAL, "Could not get socket directory of the old server\n");
fclose(fp);
/* Remove trailing newline */
if (strchr(cluster->sockdir, '\n') != NULL)
*strchr(cluster->sockdir, '\n') = '\0';
}
else
{
/* Can't get live sockdir, so assume the default is OK. */
cluster->sockdir = NULL;
}
}
#else /* !HAVE_UNIX_SOCKETS */
cluster->sockdir = NULL;
#endif
}