2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* opt.c
|
|
|
|
*
|
|
|
|
* options functions
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2014-01-08 05:05:30 +08:00
|
|
|
* Copyright (c) 2010-2014, PostgreSQL Global Development Group
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pg_upgrade/option.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
|
|
|
|
2012-09-04 01:52:34 +08:00
|
|
|
#include "miscadmin.h"
|
2014-02-16 03:31:30 +08:00
|
|
|
#include "getopt_long.h"
|
2012-09-04 01:52:34 +08:00
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
#include "pg_upgrade.h"
|
|
|
|
|
2012-03-13 07:47:54 +08:00
|
|
|
#include <time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2010-05-12 10:19:11 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
static void usage(void);
|
2012-07-06 11:36:30 +08:00
|
|
|
static void check_required_directory(char **dirpath, char **configpath,
|
2010-05-12 10:19:11 +08:00
|
|
|
char *envVarName, char *cmdLineOption, char *description);
|
2013-12-01 05:50:33 +08:00
|
|
|
#define FIX_DEFAULT_READ_ONLY "-c default_transaction_read_only=false"
|
2010-10-20 05:38:16 +08:00
|
|
|
|
|
|
|
|
2010-10-20 06:37:04 +08:00
|
|
|
UserOpts user_opts;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parseCommandLine()
|
|
|
|
*
|
2010-12-15 20:11:31 +08:00
|
|
|
* Parses the command line (argc, argv[]) and loads structures
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
void
|
2010-10-20 05:38:16 +08:00
|
|
|
parseCommandLine(int argc, char *argv[])
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"old-datadir", required_argument, NULL, 'd'},
|
|
|
|
{"new-datadir", required_argument, NULL, 'D'},
|
|
|
|
{"old-bindir", required_argument, NULL, 'b'},
|
|
|
|
{"new-bindir", required_argument, NULL, 'B'},
|
2011-10-10 19:43:28 +08:00
|
|
|
{"old-options", required_argument, NULL, 'o'},
|
|
|
|
{"new-options", required_argument, NULL, 'O'},
|
2010-05-12 10:19:11 +08:00
|
|
|
{"old-port", required_argument, NULL, 'p'},
|
|
|
|
{"new-port", required_argument, NULL, 'P'},
|
|
|
|
|
2013-06-29 05:27:02 +08:00
|
|
|
{"username", required_argument, NULL, 'U'},
|
2010-05-12 10:19:11 +08:00
|
|
|
{"check", no_argument, NULL, 'c'},
|
|
|
|
{"link", no_argument, NULL, 'k'},
|
2012-03-13 07:47:54 +08:00
|
|
|
{"retain", no_argument, NULL, 'r'},
|
2012-12-27 08:26:30 +08:00
|
|
|
{"jobs", required_argument, NULL, 'j'},
|
2010-05-12 10:19:11 +08:00
|
|
|
{"verbose", no_argument, NULL, 'v'},
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
2010-06-16 07:25:01 +08:00
|
|
|
int option; /* Command line option */
|
2010-05-12 10:19:11 +08:00
|
|
|
int optindex = 0; /* used by getopt_long */
|
2011-05-16 22:46:52 +08:00
|
|
|
int os_user_effective_id;
|
2012-06-11 03:20:04 +08:00
|
|
|
FILE *fp;
|
|
|
|
char **filename;
|
2012-03-13 07:47:54 +08:00
|
|
|
time_t run_time = time(NULL);
|
2012-06-11 03:20:04 +08:00
|
|
|
|
2011-05-16 22:46:52 +08:00
|
|
|
user_opts.transfer_mode = TRANSFER_MODE_COPY;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
os_info.progname = get_progname(argv[0]);
|
2011-05-16 22:46:52 +08:00
|
|
|
|
|
|
|
/* Process libpq env. variables; load values here for usage() output */
|
2011-07-02 06:17:12 +08:00
|
|
|
old_cluster.port = getenv("PGPORTOLD") ? atoi(getenv("PGPORTOLD")) : DEF_PGUPORT;
|
|
|
|
new_cluster.port = getenv("PGPORTNEW") ? atoi(getenv("PGPORTNEW")) : DEF_PGUPORT;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-05-16 22:46:52 +08:00
|
|
|
os_user_effective_id = get_user_info(&os_info.user);
|
|
|
|
/* we override just the database user name; we got the OS id above */
|
|
|
|
if (getenv("PGUSER"))
|
|
|
|
{
|
|
|
|
pg_free(os_info.user);
|
|
|
|
/* must save value, getenv()'s pointer is not stable */
|
|
|
|
os_info.user = pg_strdup(getenv("PGUSER"));
|
|
|
|
}
|
2010-07-07 03:19:02 +08:00
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
if (argc > 1)
|
|
|
|
{
|
2013-06-29 06:10:13 +08:00
|
|
|
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
usage();
|
2011-04-07 04:00:44 +08:00
|
|
|
exit(0);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
|
|
|
|
{
|
2011-05-11 02:40:29 +08:00
|
|
|
puts("pg_upgrade (PostgreSQL) " PG_VERSION);
|
2011-04-07 04:00:44 +08:00
|
|
|
exit(0);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-16 22:46:52 +08:00
|
|
|
/* Allow help and version to be run as root, so do the test here. */
|
|
|
|
if (os_user_effective_id == 0)
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("%s: cannot be run as root\n", os_info.progname);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-03-13 07:47:54 +08:00
|
|
|
if ((log_opts.internal = fopen_priv(INTERNAL_LOG_FILE, "a")) == NULL)
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("cannot write to log file %s\n", INTERNAL_LOG_FILE);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2013-06-29 05:27:02 +08:00
|
|
|
while ((option = getopt_long(argc, argv, "d:D:b:B:cj:ko:O:p:P:rU:v",
|
2010-05-12 10:19:11 +08:00
|
|
|
long_options, &optindex)) != -1)
|
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'b':
|
2010-10-20 05:38:16 +08:00
|
|
|
old_cluster.bindir = pg_strdup(optarg);
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B':
|
2010-10-20 05:38:16 +08:00
|
|
|
new_cluster.bindir = pg_strdup(optarg);
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
2010-10-20 05:38:16 +08:00
|
|
|
user_opts.check = true;
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
|
2011-05-16 22:46:52 +08:00
|
|
|
case 'd':
|
|
|
|
old_cluster.pgdata = pg_strdup(optarg);
|
2011-10-08 02:40:23 +08:00
|
|
|
old_cluster.pgconfig = pg_strdup(optarg);
|
2011-05-16 22:46:52 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D':
|
|
|
|
new_cluster.pgdata = pg_strdup(optarg);
|
2011-10-08 02:40:23 +08:00
|
|
|
new_cluster.pgconfig = pg_strdup(optarg);
|
2011-05-16 22:46:52 +08:00
|
|
|
break;
|
|
|
|
|
2012-12-27 08:26:30 +08:00
|
|
|
case 'j':
|
|
|
|
user_opts.jobs = atoi(optarg);
|
|
|
|
break;
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
case 'k':
|
2010-10-20 05:38:16 +08:00
|
|
|
user_opts.transfer_mode = TRANSFER_MODE_LINK;
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
|
2011-10-10 19:43:28 +08:00
|
|
|
case 'o':
|
|
|
|
old_cluster.pgopts = pg_strdup(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'O':
|
|
|
|
new_cluster.pgopts = pg_strdup(optarg);
|
|
|
|
break;
|
|
|
|
|
2012-06-11 03:20:04 +08:00
|
|
|
/*
|
|
|
|
* Someday, the port number option could be removed and passed
|
|
|
|
* using -o/-O, but that requires postmaster -C to be
|
|
|
|
* supported on all old/new versions.
|
|
|
|
*/
|
2010-05-12 10:19:11 +08:00
|
|
|
case 'p':
|
2010-10-20 05:38:16 +08:00
|
|
|
if ((old_cluster.port = atoi(optarg)) <= 0)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("invalid old port number\n");
|
2011-04-07 04:00:44 +08:00
|
|
|
exit(1);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'P':
|
2010-10-20 05:38:16 +08:00
|
|
|
if ((new_cluster.port = atoi(optarg)) <= 0)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("invalid new port number\n");
|
2011-04-07 04:00:44 +08:00
|
|
|
exit(1);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-03-13 07:47:54 +08:00
|
|
|
case 'r':
|
|
|
|
log_opts.retain = true;
|
|
|
|
break;
|
|
|
|
|
2013-06-29 05:27:02 +08:00
|
|
|
case 'U':
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_free(os_info.user);
|
|
|
|
os_info.user = pg_strdup(optarg);
|
2013-06-29 07:11:51 +08:00
|
|
|
os_info.user_specified = true;
|
2011-06-10 02:32:50 +08:00
|
|
|
|
2011-05-16 22:46:52 +08:00
|
|
|
/*
|
|
|
|
* Push the user name into the environment so pre-9.1
|
|
|
|
* pg_ctl/libpq uses it.
|
|
|
|
*/
|
|
|
|
pg_putenv("PGUSER", os_info.user);
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_REPORT, "Running in verbose mode\n");
|
2010-10-20 10:31:17 +08:00
|
|
|
log_opts.verbose = true;
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("Try \"%s --help\" for more information.\n",
|
2010-10-20 05:38:16 +08:00
|
|
|
os_info.progname);
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-13 07:47:54 +08:00
|
|
|
/* label start of upgrade in logfiles */
|
2012-05-24 08:19:21 +08:00
|
|
|
for (filename = output_files; *filename != NULL; filename++)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2012-05-24 08:19:21 +08:00
|
|
|
if ((fp = fopen_priv(*filename, "a")) == NULL)
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("cannot write to log file %s\n", *filename);
|
2012-05-24 08:19:21 +08:00
|
|
|
|
|
|
|
/* Start with newline because we might be appending to a file. */
|
2012-03-13 07:47:54 +08:00
|
|
|
fprintf(fp, "\n"
|
|
|
|
"-----------------------------------------------------------------\n"
|
2012-06-11 03:20:04 +08:00
|
|
|
" pg_upgrade run on %s"
|
|
|
|
"-----------------------------------------------------------------\n\n",
|
|
|
|
ctime(&run_time));
|
2012-03-13 07:47:54 +08:00
|
|
|
fclose(fp);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
2013-12-01 05:50:33 +08:00
|
|
|
/* Turn off read-only mode; add prefix to PGOPTIONS? */
|
|
|
|
if (getenv("PGOPTIONS"))
|
|
|
|
{
|
|
|
|
char *pgoptions = psprintf("%s %s", FIX_DEFAULT_READ_ONLY,
|
|
|
|
getenv("PGOPTIONS"));
|
|
|
|
pg_putenv("PGOPTIONS", pgoptions);
|
|
|
|
pfree(pgoptions);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pg_putenv("PGOPTIONS", FIX_DEFAULT_READ_ONLY);
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
/* Get values from env if not already set */
|
2012-07-06 11:36:30 +08:00
|
|
|
check_required_directory(&old_cluster.bindir, NULL, "PGBINOLD", "-b",
|
2012-06-11 03:20:04 +08:00
|
|
|
"old cluster binaries reside");
|
2012-07-06 11:36:30 +08:00
|
|
|
check_required_directory(&new_cluster.bindir, NULL, "PGBINNEW", "-B",
|
2012-06-11 03:20:04 +08:00
|
|
|
"new cluster binaries reside");
|
2012-07-06 11:36:30 +08:00
|
|
|
check_required_directory(&old_cluster.pgdata, &old_cluster.pgconfig,
|
|
|
|
"PGDATAOLD", "-d", "old cluster data resides");
|
|
|
|
check_required_directory(&new_cluster.pgdata, &new_cluster.pgconfig,
|
|
|
|
"PGDATANEW", "-D", "new cluster data resides");
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2010-10-20 05:38:16 +08:00
|
|
|
usage(void)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-05-06 03:56:06 +08:00
|
|
|
printf(_("pg_upgrade upgrades a PostgreSQL cluster to a different major version.\n\
|
|
|
|
\nUsage:\n\
|
2012-05-09 02:55:05 +08:00
|
|
|
pg_upgrade [OPTION]...\n\
|
2010-05-12 10:19:11 +08:00
|
|
|
\n\
|
|
|
|
Options:\n\
|
2013-06-29 06:01:46 +08:00
|
|
|
-b, --old-bindir=BINDIR old cluster executable directory\n\
|
|
|
|
-B, --new-bindir=BINDIR new cluster executable directory\n\
|
2011-05-06 03:56:06 +08:00
|
|
|
-c, --check check clusters only, don't change any data\n\
|
2013-06-29 06:01:46 +08:00
|
|
|
-d, --old-datadir=DATADIR old cluster data directory\n\
|
|
|
|
-D, --new-datadir=DATADIR new cluster data directory\n\
|
2012-12-27 08:26:30 +08:00
|
|
|
-j, --jobs number of simultaneous processes or threads to use\n\
|
2011-05-06 03:56:06 +08:00
|
|
|
-k, --link link instead of copying files to new cluster\n\
|
2011-10-10 19:43:28 +08:00
|
|
|
-o, --old-options=OPTIONS old cluster options to pass to the server\n\
|
|
|
|
-O, --new-options=OPTIONS new cluster options to pass to the server\n\
|
2013-06-29 06:01:46 +08:00
|
|
|
-p, --old-port=PORT old cluster port number (default %d)\n\
|
|
|
|
-P, --new-port=PORT new cluster port number (default %d)\n\
|
2012-03-13 07:47:54 +08:00
|
|
|
-r, --retain retain SQL and log files after success\n\
|
2013-06-29 05:27:02 +08:00
|
|
|
-U, --username=NAME cluster superuser (default \"%s\")\n\
|
2012-03-13 07:47:54 +08:00
|
|
|
-v, --verbose enable verbose internal logging\n\
|
2011-05-06 03:56:06 +08:00
|
|
|
-V, --version display version information, then exit\n\
|
2013-06-29 06:10:13 +08:00
|
|
|
-?, --help show this help, then exit\n\
|
2010-05-12 10:19:11 +08:00
|
|
|
\n\
|
|
|
|
Before running pg_upgrade you must:\n\
|
|
|
|
create a new database cluster (using the new version of initdb)\n\
|
|
|
|
shutdown the postmaster servicing the old cluster\n\
|
|
|
|
shutdown the postmaster servicing the new cluster\n\
|
|
|
|
\n\
|
|
|
|
When you run pg_upgrade, you must provide the following information:\n\
|
2013-06-29 06:01:46 +08:00
|
|
|
the data directory for the old cluster (-d DATADIR)\n\
|
|
|
|
the data directory for the new cluster (-D DATADIR)\n\
|
|
|
|
the \"bin\" directory for the old version (-b BINDIR)\n\
|
|
|
|
the \"bin\" directory for the new version (-B BINDIR)\n\
|
2010-05-12 10:19:11 +08:00
|
|
|
\n\
|
|
|
|
For example:\n\
|
|
|
|
pg_upgrade -d oldCluster/data -D newCluster/data -b oldCluster/bin -B newCluster/bin\n\
|
2010-10-20 05:38:16 +08:00
|
|
|
or\n"), old_cluster.port, new_cluster.port, os_info.user);
|
2010-05-12 10:19:11 +08:00
|
|
|
#ifndef WIN32
|
|
|
|
printf(_("\
|
2011-07-02 06:17:12 +08:00
|
|
|
$ export PGDATAOLD=oldCluster/data\n\
|
|
|
|
$ export PGDATANEW=newCluster/data\n\
|
|
|
|
$ export PGBINOLD=oldCluster/bin\n\
|
|
|
|
$ export PGBINNEW=newCluster/bin\n\
|
2010-05-12 10:19:11 +08:00
|
|
|
$ pg_upgrade\n"));
|
|
|
|
#else
|
|
|
|
printf(_("\
|
2011-07-02 06:17:12 +08:00
|
|
|
C:\\> set PGDATAOLD=oldCluster/data\n\
|
|
|
|
C:\\> set PGDATANEW=newCluster/data\n\
|
|
|
|
C:\\> set PGBINOLD=oldCluster/bin\n\
|
|
|
|
C:\\> set PGBINNEW=newCluster/bin\n\
|
2010-05-12 10:19:11 +08:00
|
|
|
C:\\> pg_upgrade\n"));
|
|
|
|
#endif
|
2011-05-11 02:37:55 +08:00
|
|
|
printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2011-06-23 05:50:40 +08:00
|
|
|
* check_required_directory()
|
2010-05-12 10:19:11 +08:00
|
|
|
*
|
2011-06-23 05:50:40 +08:00
|
|
|
* Checks a directory option.
|
2010-05-12 10:19:11 +08:00
|
|
|
* dirpath - the directory name supplied on the command line
|
2012-07-06 21:39:22 +08:00
|
|
|
* configpath - optional configuration directory
|
2010-05-12 10:19:11 +08:00
|
|
|
* envVarName - the name of an environment variable to get if dirpath is NULL
|
|
|
|
* cmdLineOption - the command line option corresponds to this directory (-o, -O, -n, -N)
|
|
|
|
* description - a description of this directory option
|
|
|
|
*
|
|
|
|
* We use the last two arguments to construct a meaningful error message if the
|
|
|
|
* user hasn't provided the required directory name.
|
|
|
|
*/
|
|
|
|
static void
|
2012-07-06 11:36:30 +08:00
|
|
|
check_required_directory(char **dirpath, char **configpath,
|
|
|
|
char *envVarName, char *cmdLineOption,
|
|
|
|
char *description)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-06-16 05:23:02 +08:00
|
|
|
if (*dirpath == NULL || strlen(*dirpath) == 0)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
const char *envVar;
|
|
|
|
|
|
|
|
if ((envVar = getenv(envVarName)) && strlen(envVar))
|
2012-07-06 11:36:30 +08:00
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
*dirpath = pg_strdup(envVar);
|
2012-07-06 11:36:30 +08:00
|
|
|
if (configpath)
|
|
|
|
*configpath = pg_strdup(envVar);
|
|
|
|
}
|
2010-05-12 10:19:11 +08:00
|
|
|
else
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("You must identify the directory where the %s.\n"
|
2011-07-12 12:13:51 +08:00
|
|
|
"Please use the %s command-line option or the %s environment variable.\n",
|
2010-05-12 10:19:11 +08:00
|
|
|
description, cmdLineOption, envVarName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-05-30 04:58:43 +08:00
|
|
|
* Trim off any trailing path separators because we construct paths by
|
|
|
|
* appending to this path.
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
2010-05-14 08:13:38 +08:00
|
|
|
#ifndef WIN32
|
|
|
|
if ((*dirpath)[strlen(*dirpath) - 1] == '/')
|
|
|
|
#else
|
|
|
|
if ((*dirpath)[strlen(*dirpath) - 1] == '/' ||
|
2010-07-07 03:19:02 +08:00
|
|
|
(*dirpath)[strlen(*dirpath) - 1] == '\\')
|
2010-05-14 08:13:38 +08:00
|
|
|
#endif
|
2010-05-12 10:19:11 +08:00
|
|
|
(*dirpath)[strlen(*dirpath) - 1] = 0;
|
|
|
|
}
|
2011-10-08 02:40:23 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* adjust_data_dir
|
|
|
|
*
|
|
|
|
* If a configuration-only directory was specified, find the real data dir
|
|
|
|
* by quering the running server. This has limited checking because we
|
|
|
|
* can't check for a running server because we can't find postmaster.pid.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
adjust_data_dir(ClusterInfo *cluster)
|
|
|
|
{
|
|
|
|
char filename[MAXPGPATH];
|
2012-06-11 03:20:04 +08:00
|
|
|
char cmd[MAXPGPATH],
|
|
|
|
cmd_output[MAX_STRING];
|
|
|
|
FILE *fp,
|
|
|
|
*output;
|
2011-10-08 02:40:23 +08:00
|
|
|
|
|
|
|
/* If there is no postgresql.conf, it can't be a config-only dir */
|
|
|
|
snprintf(filename, sizeof(filename), "%s/postgresql.conf", cluster->pgconfig);
|
2012-03-13 07:47:54 +08:00
|
|
|
if ((fp = fopen(filename, "r")) == NULL)
|
2011-10-08 02:40:23 +08:00
|
|
|
return;
|
2012-03-13 07:47:54 +08:00
|
|
|
fclose(fp);
|
2011-10-08 02:40:23 +08:00
|
|
|
|
|
|
|
/* If PG_VERSION exists, it can't be a config-only dir */
|
|
|
|
snprintf(filename, sizeof(filename), "%s/PG_VERSION", cluster->pgconfig);
|
2012-03-13 07:47:54 +08:00
|
|
|
if ((fp = fopen(filename, "r")) != NULL)
|
2011-10-08 02:40:23 +08:00
|
|
|
{
|
2012-03-13 07:47:54 +08:00
|
|
|
fclose(fp);
|
2011-10-08 02:40:23 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Must be a configuration directory, so find the real data directory. */
|
|
|
|
|
|
|
|
prep_status("Finding the real data directory for the %s cluster",
|
|
|
|
CLUSTER_NAME(cluster));
|
|
|
|
|
|
|
|
/*
|
2012-06-11 03:20:04 +08:00
|
|
|
* We don't have a data directory yet, so we can't check the PG version,
|
|
|
|
* so this might fail --- only works for PG 9.2+. If this fails,
|
|
|
|
* pg_upgrade will fail anyway because the data files will not be found.
|
2011-10-08 02:40:23 +08:00
|
|
|
*/
|
|
|
|
snprintf(cmd, sizeof(cmd), "\"%s/postmaster\" -D \"%s\" -C data_directory",
|
|
|
|
cluster->bindir, cluster->pgconfig);
|
|
|
|
|
|
|
|
if ((output = popen(cmd, "r")) == NULL ||
|
|
|
|
fgets(cmd_output, sizeof(cmd_output), output) == NULL)
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("Could not get data directory using %s: %s\n",
|
2012-06-11 03:20:04 +08:00
|
|
|
cmd, getErrorText(errno));
|
2011-10-08 02:40:23 +08:00
|
|
|
|
|
|
|
pclose(output);
|
|
|
|
|
|
|
|
/* Remove trailing newline */
|
|
|
|
if (strchr(cmd_output, '\n') != NULL)
|
|
|
|
*strchr(cmd_output, '\n') = '\0';
|
|
|
|
|
|
|
|
cluster->pgdata = pg_strdup(cmd_output);
|
|
|
|
|
|
|
|
check_ok();
|
|
|
|
}
|
2012-09-04 01:52:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2012-09-04 10:15:09 +08:00
|
|
|
/*
|
2013-05-30 04:58:43 +08:00
|
|
|
* sockdir and port were added to postmaster.pid in PG 9.1. Pre-9.1 cannot
|
|
|
|
* process pg_ctl -w for sockets in non-default locations.
|
2012-09-04 10:15:09 +08:00
|
|
|
*/
|
|
|
|
if (GET_MAJOR_VERSION(cluster->major_version) >= 901)
|
2012-09-04 01:52:34 +08:00
|
|
|
{
|
2012-09-04 10:15:09 +08:00
|
|
|
if (!live_check)
|
2012-09-04 01:52:34 +08:00
|
|
|
{
|
2012-09-04 10:15:09 +08:00
|
|
|
/* Use the current directory for the socket */
|
|
|
|
cluster->sockdir = pg_malloc(MAXPGPATH);
|
|
|
|
if (!getcwd(cluster->sockdir, MAXPGPATH))
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("cannot find current directory\n");
|
2012-09-04 10:15:09 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2013-05-30 04:58:43 +08:00
|
|
|
* 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.
|
2012-09-04 10:15:09 +08:00
|
|
|
*/
|
|
|
|
unsigned short orig_port = cluster->port;
|
2013-05-30 04:58:43 +08:00
|
|
|
char filename[MAXPGPATH],
|
|
|
|
line[MAXPGPATH];
|
|
|
|
FILE *fp;
|
2012-09-04 10:15:09 +08:00
|
|
|
int lineno;
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2012-09-04 01:52:34 +08:00
|
|
|
snprintf(filename, sizeof(filename), "%s/postmaster.pid",
|
|
|
|
cluster->pgdata);
|
|
|
|
if ((fp = fopen(filename, "r")) == NULL)
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("Cannot open file %s: %m\n", filename);
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2012-09-04 10:15:09 +08:00
|
|
|
for (lineno = 1;
|
2013-05-30 04:58:43 +08:00
|
|
|
lineno <= Max(LOCK_FILE_LINE_PORT, LOCK_FILE_LINE_SOCKET_DIR);
|
2012-09-04 10:15:09 +08:00
|
|
|
lineno++)
|
|
|
|
{
|
|
|
|
if (fgets(line, sizeof(line), fp) == NULL)
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("Cannot read line %d from %s: %m\n", lineno, filename);
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2012-09-04 10:15:09 +08:00
|
|
|
/* potentially overwrite user-supplied value */
|
|
|
|
if (lineno == LOCK_FILE_LINE_PORT)
|
|
|
|
sscanf(line, "%hu", &old_cluster.port);
|
|
|
|
if (lineno == LOCK_FILE_LINE_SOCKET_DIR)
|
|
|
|
{
|
2014-02-16 00:50:56 +08:00
|
|
|
cluster->sockdir = pg_strdup(line);
|
2012-09-04 10:15:09 +08:00
|
|
|
/* strip off newline */
|
2014-02-16 00:50:56 +08:00
|
|
|
if (strchr(cluster->sockdir, '\n') != NULL)
|
|
|
|
*strchr(cluster->sockdir, '\n') = '\0';
|
2012-09-04 10:15:09 +08:00
|
|
|
}
|
|
|
|
}
|
2012-09-04 01:52:34 +08:00
|
|
|
fclose(fp);
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2012-09-04 10:15:09 +08:00
|
|
|
/* warn of port number correction */
|
|
|
|
if (orig_port != DEF_PGUPORT && old_cluster.port != orig_port)
|
|
|
|
pg_log(PG_WARNING, "User-supplied old port number %hu corrected to %hu\n",
|
2013-05-30 04:58:43 +08:00
|
|
|
orig_port, cluster->port);
|
2012-09-04 01:52:34 +08:00
|
|
|
}
|
|
|
|
}
|
2012-09-04 10:15:09 +08:00
|
|
|
else
|
|
|
|
|
2013-05-30 04:58:43 +08:00
|
|
|
/*
|
|
|
|
* Can't get sockdir and pg_ctl -w can't use a non-default, use
|
|
|
|
* default
|
|
|
|
*/
|
|
|
|
cluster->sockdir = NULL;
|
|
|
|
#else /* !HAVE_UNIX_SOCKETS */
|
2012-09-04 01:52:34 +08:00
|
|
|
cluster->sockdir = NULL;
|
|
|
|
#endif
|
|
|
|
}
|