2010-05-12 04:19:11 +02:00
|
|
|
/*
|
2019-07-27 07:46:02 +02:00
|
|
|
* option.c
|
2010-05-12 04:19:11 +02:00
|
|
|
*
|
|
|
|
* options functions
|
2010-07-03 16:23:14 +02:00
|
|
|
*
|
2020-01-01 18:21:45 +01:00
|
|
|
* Copyright (c) 2010-2020, PostgreSQL Global Development Group
|
2015-03-11 03:33:25 +01:00
|
|
|
* src/bin/pg_upgrade/option.c
|
2010-05-12 04:19:11 +02: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 14:33:40 +01:00
|
|
|
#include "postgres_fe.h"
|
2011-08-27 03:16:24 +02:00
|
|
|
|
2012-03-13 00:47:54 +01:00
|
|
|
#include <time.h>
|
2010-05-12 04:19:11 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2019-08-09 04:05:14 +02:00
|
|
|
#include "common/string.h"
|
2019-10-23 06:08:53 +02:00
|
|
|
#include "getopt_long.h"
|
2017-02-25 22:12:24 +01:00
|
|
|
#include "pg_upgrade.h"
|
2019-10-23 06:08:53 +02:00
|
|
|
#include "utils/pidfile.h"
|
2010-05-12 04:19:11 +02:00
|
|
|
|
2010-10-19 23:38:16 +02:00
|
|
|
static void usage(void);
|
2018-12-01 21:45:11 +01:00
|
|
|
static void check_required_directory(char **dirpath,
|
2019-05-22 19:04:48 +02:00
|
|
|
const char *envVarName, bool useCwd,
|
2019-07-27 07:56:20 +02:00
|
|
|
const char *cmdLineOption, const char *description,
|
|
|
|
bool missingOk);
|
2013-11-30 22:50:33 +01:00
|
|
|
#define FIX_DEFAULT_READ_ONLY "-c default_transaction_read_only=false"
|
2010-10-19 23:38:16 +02:00
|
|
|
|
|
|
|
|
2010-10-20 00:37:04 +02:00
|
|
|
UserOpts user_opts;
|
2010-05-12 04:19:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parseCommandLine()
|
|
|
|
*
|
2010-12-15 13:11:31 +01:00
|
|
|
* Parses the command line (argc, argv[]) and loads structures
|
2010-05-12 04:19:11 +02:00
|
|
|
*/
|
|
|
|
void
|
2010-10-19 23:38:16 +02:00
|
|
|
parseCommandLine(int argc, char *argv[])
|
2010-05-12 04:19:11 +02: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 13:43:28 +02:00
|
|
|
{"old-options", required_argument, NULL, 'o'},
|
|
|
|
{"new-options", required_argument, NULL, 'O'},
|
2010-05-12 04:19:11 +02:00
|
|
|
{"old-port", required_argument, NULL, 'p'},
|
|
|
|
{"new-port", required_argument, NULL, 'P'},
|
|
|
|
|
2013-06-28 23:27:02 +02:00
|
|
|
{"username", required_argument, NULL, 'U'},
|
2010-05-12 04:19:11 +02:00
|
|
|
{"check", no_argument, NULL, 'c'},
|
|
|
|
{"link", no_argument, NULL, 'k'},
|
2012-03-13 00:47:54 +01:00
|
|
|
{"retain", no_argument, NULL, 'r'},
|
2012-12-27 01:26:30 +01:00
|
|
|
{"jobs", required_argument, NULL, 'j'},
|
2018-12-01 21:45:11 +01:00
|
|
|
{"socketdir", required_argument, NULL, 's'},
|
2010-05-12 04:19:11 +02:00
|
|
|
{"verbose", no_argument, NULL, 'v'},
|
2018-11-07 18:05:54 +01:00
|
|
|
{"clone", no_argument, NULL, 1},
|
|
|
|
|
2010-05-12 04:19:11 +02:00
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
2010-06-16 01:25:01 +02:00
|
|
|
int option; /* Command line option */
|
2010-05-12 04:19:11 +02:00
|
|
|
int optindex = 0; /* used by getopt_long */
|
2011-05-16 16:46:52 +02:00
|
|
|
int os_user_effective_id;
|
2012-06-10 21:20:04 +02:00
|
|
|
FILE *fp;
|
|
|
|
char **filename;
|
2012-03-13 00:47:54 +01:00
|
|
|
time_t run_time = time(NULL);
|
2012-06-10 21:20:04 +02:00
|
|
|
|
2011-05-16 16:46:52 +02:00
|
|
|
user_opts.transfer_mode = TRANSFER_MODE_COPY;
|
2010-05-12 04:19:11 +02:00
|
|
|
|
2010-10-19 23:38:16 +02:00
|
|
|
os_info.progname = get_progname(argv[0]);
|
2011-05-16 16:46:52 +02:00
|
|
|
|
|
|
|
/* Process libpq env. variables; load values here for usage() output */
|
2011-07-02 00:17:12 +02:00
|
|
|
old_cluster.port = getenv("PGPORTOLD") ? atoi(getenv("PGPORTOLD")) : DEF_PGUPORT;
|
|
|
|
new_cluster.port = getenv("PGPORTNEW") ? atoi(getenv("PGPORTNEW")) : DEF_PGUPORT;
|
2010-05-12 04:19:11 +02:00
|
|
|
|
2011-05-16 16:46:52 +02: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-06 21:19:02 +02:00
|
|
|
|
2010-05-12 04:19:11 +02:00
|
|
|
if (argc > 1)
|
|
|
|
{
|
2013-06-29 00:10:13 +02:00
|
|
|
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
2010-10-19 23:38:16 +02:00
|
|
|
usage();
|
2011-04-06 22:00:44 +02:00
|
|
|
exit(0);
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
|
|
|
|
{
|
2011-05-10 20:40:29 +02:00
|
|
|
puts("pg_upgrade (PostgreSQL) " PG_VERSION);
|
2011-04-06 22:00:44 +02:00
|
|
|
exit(0);
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-16 16:46:52 +02:00
|
|
|
/* Allow help and version to be run as root, so do the test here. */
|
|
|
|
if (os_user_effective_id == 0)
|
2013-10-02 03:24:56 +02:00
|
|
|
pg_fatal("%s: cannot be run as root\n", os_info.progname);
|
2010-05-12 04:19:11 +02:00
|
|
|
|
2018-12-01 21:45:11 +01:00
|
|
|
while ((option = getopt_long(argc, argv, "d:D:b:B:cj:ko:O:p:P:rs:U:v",
|
2010-05-12 04:19:11 +02:00
|
|
|
long_options, &optindex)) != -1)
|
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'b':
|
2010-10-19 23:38:16 +02:00
|
|
|
old_cluster.bindir = pg_strdup(optarg);
|
2010-05-12 04:19:11 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B':
|
2010-10-19 23:38:16 +02:00
|
|
|
new_cluster.bindir = pg_strdup(optarg);
|
2010-05-12 04:19:11 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
2010-10-19 23:38:16 +02:00
|
|
|
user_opts.check = true;
|
2010-05-12 04:19:11 +02:00
|
|
|
break;
|
|
|
|
|
2011-05-16 16:46:52 +02:00
|
|
|
case 'd':
|
|
|
|
old_cluster.pgdata = pg_strdup(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D':
|
|
|
|
new_cluster.pgdata = pg_strdup(optarg);
|
|
|
|
break;
|
|
|
|
|
2012-12-27 01:26:30 +01:00
|
|
|
case 'j':
|
|
|
|
user_opts.jobs = atoi(optarg);
|
|
|
|
break;
|
|
|
|
|
2010-05-12 04:19:11 +02:00
|
|
|
case 'k':
|
2010-10-19 23:38:16 +02:00
|
|
|
user_opts.transfer_mode = TRANSFER_MODE_LINK;
|
2010-05-12 04:19:11 +02:00
|
|
|
break;
|
|
|
|
|
2011-10-10 13:43:28 +02:00
|
|
|
case 'o':
|
2014-08-25 22:30:26 +02:00
|
|
|
/* append option? */
|
|
|
|
if (!old_cluster.pgopts)
|
|
|
|
old_cluster.pgopts = pg_strdup(optarg);
|
|
|
|
else
|
|
|
|
{
|
2015-05-24 03:35:49 +02:00
|
|
|
char *old_pgopts = old_cluster.pgopts;
|
2014-08-25 22:30:26 +02:00
|
|
|
|
|
|
|
old_cluster.pgopts = psprintf("%s %s", old_pgopts, optarg);
|
|
|
|
free(old_pgopts);
|
|
|
|
}
|
2011-10-10 13:43:28 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'O':
|
2014-08-25 22:30:26 +02:00
|
|
|
/* append option? */
|
|
|
|
if (!new_cluster.pgopts)
|
|
|
|
new_cluster.pgopts = pg_strdup(optarg);
|
|
|
|
else
|
|
|
|
{
|
2015-05-24 03:35:49 +02:00
|
|
|
char *new_pgopts = new_cluster.pgopts;
|
2014-08-25 22:30:26 +02:00
|
|
|
|
|
|
|
new_cluster.pgopts = psprintf("%s %s", new_pgopts, optarg);
|
|
|
|
free(new_pgopts);
|
|
|
|
}
|
2011-10-10 13:43:28 +02:00
|
|
|
break;
|
|
|
|
|
2012-06-10 21:20:04 +02:00
|
|
|
/*
|
|
|
|
* Someday, the port number option could be removed and passed
|
|
|
|
* using -o/-O, but that requires postmaster -C to be
|
2014-08-25 22:30:26 +02:00
|
|
|
* supported on all old/new versions (added in PG 9.2).
|
2012-06-10 21:20:04 +02:00
|
|
|
*/
|
2010-05-12 04:19:11 +02:00
|
|
|
case 'p':
|
2010-10-19 23:38:16 +02:00
|
|
|
if ((old_cluster.port = atoi(optarg)) <= 0)
|
2013-10-02 03:24:56 +02:00
|
|
|
pg_fatal("invalid old port number\n");
|
2010-05-12 04:19:11 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'P':
|
2010-10-19 23:38:16 +02:00
|
|
|
if ((new_cluster.port = atoi(optarg)) <= 0)
|
2013-10-02 03:24:56 +02:00
|
|
|
pg_fatal("invalid new port number\n");
|
2010-05-12 04:19:11 +02:00
|
|
|
break;
|
|
|
|
|
2012-03-13 00:47:54 +01:00
|
|
|
case 'r':
|
|
|
|
log_opts.retain = true;
|
|
|
|
break;
|
|
|
|
|
2018-12-01 21:45:11 +01:00
|
|
|
case 's':
|
|
|
|
user_opts.socketdir = pg_strdup(optarg);
|
|
|
|
break;
|
|
|
|
|
2013-06-28 23:27:02 +02:00
|
|
|
case 'U':
|
2010-10-19 23:38:16 +02:00
|
|
|
pg_free(os_info.user);
|
|
|
|
os_info.user = pg_strdup(optarg);
|
2013-06-29 01:11:51 +02:00
|
|
|
os_info.user_specified = true;
|
2011-06-09 20:32:50 +02:00
|
|
|
|
2011-05-16 16:46:52 +02: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 04:19:11 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
2010-10-20 04:31:17 +02:00
|
|
|
log_opts.verbose = true;
|
2010-05-12 04:19:11 +02:00
|
|
|
break;
|
|
|
|
|
2018-11-07 18:05:54 +01:00
|
|
|
case 1:
|
|
|
|
user_opts.transfer_mode = TRANSFER_MODE_CLONE;
|
|
|
|
break;
|
|
|
|
|
2010-05-12 04:19:11 +02:00
|
|
|
default:
|
2019-06-19 21:43:31 +02:00
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
|
|
|
os_info.progname);
|
|
|
|
exit(1);
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 16:19:35 +02:00
|
|
|
if (optind < argc)
|
|
|
|
pg_fatal("too many command-line arguments (first is \"%s\")\n", argv[optind]);
|
|
|
|
|
2019-06-19 21:43:31 +02:00
|
|
|
if ((log_opts.internal = fopen_priv(INTERNAL_LOG_FILE, "a")) == NULL)
|
2019-06-19 21:49:03 +02:00
|
|
|
pg_fatal("could not open log file \"%s\": %m\n", INTERNAL_LOG_FILE);
|
2019-06-19 21:43:31 +02:00
|
|
|
|
|
|
|
if (log_opts.verbose)
|
|
|
|
pg_log(PG_REPORT, "Running in verbose mode\n");
|
|
|
|
|
2012-03-13 00:47:54 +01:00
|
|
|
/* label start of upgrade in logfiles */
|
2012-05-24 02:19:21 +02:00
|
|
|
for (filename = output_files; *filename != NULL; filename++)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
2012-05-24 02:19:21 +02:00
|
|
|
if ((fp = fopen_priv(*filename, "a")) == NULL)
|
2019-06-19 21:49:03 +02:00
|
|
|
pg_fatal("could not write to log file \"%s\": %m\n", *filename);
|
2012-05-24 02:19:21 +02:00
|
|
|
|
|
|
|
/* Start with newline because we might be appending to a file. */
|
2012-03-13 00:47:54 +01:00
|
|
|
fprintf(fp, "\n"
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
"-----------------------------------------------------------------\n"
|
2012-06-10 21:20:04 +02:00
|
|
|
" pg_upgrade run on %s"
|
|
|
|
"-----------------------------------------------------------------\n\n",
|
|
|
|
ctime(&run_time));
|
2012-03-13 00:47:54 +01:00
|
|
|
fclose(fp);
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
|
2013-11-30 22:50:33 +01:00
|
|
|
/* Turn off read-only mode; add prefix to PGOPTIONS? */
|
|
|
|
if (getenv("PGOPTIONS"))
|
|
|
|
{
|
2014-05-06 18:12:18 +02:00
|
|
|
char *pgoptions = psprintf("%s %s", FIX_DEFAULT_READ_ONLY,
|
|
|
|
getenv("PGOPTIONS"));
|
|
|
|
|
2013-11-30 22:50:33 +01:00
|
|
|
pg_putenv("PGOPTIONS", pgoptions);
|
|
|
|
pfree(pgoptions);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pg_putenv("PGOPTIONS", FIX_DEFAULT_READ_ONLY);
|
|
|
|
|
2010-05-12 04:19:11 +02:00
|
|
|
/* Get values from env if not already set */
|
2018-12-01 21:45:11 +01:00
|
|
|
check_required_directory(&old_cluster.bindir, "PGBINOLD", false,
|
2019-07-27 07:56:20 +02:00
|
|
|
"-b", _("old cluster binaries reside"), false);
|
2018-12-01 21:45:11 +01:00
|
|
|
check_required_directory(&new_cluster.bindir, "PGBINNEW", false,
|
2019-07-27 07:56:20 +02:00
|
|
|
"-B", _("new cluster binaries reside"), true);
|
2018-12-01 21:45:11 +01:00
|
|
|
check_required_directory(&old_cluster.pgdata, "PGDATAOLD", false,
|
2019-07-27 07:56:20 +02:00
|
|
|
"-d", _("old cluster data resides"), false);
|
2018-12-01 21:45:11 +01:00
|
|
|
check_required_directory(&new_cluster.pgdata, "PGDATANEW", false,
|
2019-07-27 07:56:20 +02:00
|
|
|
"-D", _("new cluster data resides"), false);
|
2018-12-01 21:45:11 +01:00
|
|
|
check_required_directory(&user_opts.socketdir, "PGSOCKETDIR", true,
|
2019-07-27 07:56:20 +02:00
|
|
|
"-s", _("sockets will be created"), false);
|
2014-08-15 18:03:49 +02:00
|
|
|
|
2014-08-15 18:28:18 +02:00
|
|
|
#ifdef WIN32
|
2015-05-24 03:35:49 +02:00
|
|
|
|
2014-08-15 18:03:49 +02:00
|
|
|
/*
|
|
|
|
* On Windows, initdb --sync-only will fail with a "Permission denied"
|
2015-05-24 03:35:49 +02:00
|
|
|
* error on file pg_upgrade_utility.log if pg_upgrade is run inside the
|
|
|
|
* new cluster directory, so we do a check here.
|
2014-08-15 18:03:49 +02:00
|
|
|
*/
|
|
|
|
{
|
2015-05-24 03:35:49 +02:00
|
|
|
char cwd[MAXPGPATH],
|
|
|
|
new_cluster_pgdata[MAXPGPATH];
|
2014-08-15 18:03:49 +02:00
|
|
|
|
|
|
|
strlcpy(new_cluster_pgdata, new_cluster.pgdata, MAXPGPATH);
|
|
|
|
canonicalize_path(new_cluster_pgdata);
|
2014-08-15 20:52:06 +02:00
|
|
|
|
2014-08-15 18:03:49 +02:00
|
|
|
if (!getcwd(cwd, MAXPGPATH))
|
2017-09-09 23:32:10 +02:00
|
|
|
pg_fatal("could not determine current directory\n");
|
2014-08-15 18:03:49 +02:00
|
|
|
canonicalize_path(cwd);
|
|
|
|
if (path_is_prefix_of_path(new_cluster_pgdata, cwd))
|
|
|
|
pg_fatal("cannot run pg_upgrade from inside the new cluster data directory on Windows\n");
|
|
|
|
}
|
|
|
|
#endif
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2010-10-19 23:38:16 +02:00
|
|
|
usage(void)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
2016-10-14 18:00:00 +02:00
|
|
|
printf(_("pg_upgrade upgrades a PostgreSQL cluster to a different major version.\n\n"));
|
|
|
|
printf(_("Usage:\n"));
|
|
|
|
printf(_(" pg_upgrade [OPTION]...\n\n"));
|
|
|
|
printf(_("Options:\n"));
|
|
|
|
printf(_(" -b, --old-bindir=BINDIR old cluster executable directory\n"));
|
2019-07-27 07:56:20 +02:00
|
|
|
printf(_(" -B, --new-bindir=BINDIR new cluster executable directory (default\n"
|
2019-08-27 21:14:55 +02:00
|
|
|
" same directory as pg_upgrade)\n"));
|
2016-10-14 18:00:00 +02:00
|
|
|
printf(_(" -c, --check check clusters only, don't change any data\n"));
|
|
|
|
printf(_(" -d, --old-datadir=DATADIR old cluster data directory\n"));
|
|
|
|
printf(_(" -D, --new-datadir=DATADIR new cluster data directory\n"));
|
2020-02-11 23:47:36 +01:00
|
|
|
printf(_(" -j, --jobs=NUM number of simultaneous processes or threads to use\n"));
|
2016-10-14 18:00:00 +02:00
|
|
|
printf(_(" -k, --link link instead of copying files to new cluster\n"));
|
|
|
|
printf(_(" -o, --old-options=OPTIONS old cluster options to pass to the server\n"));
|
|
|
|
printf(_(" -O, --new-options=OPTIONS new cluster options to pass to the server\n"));
|
|
|
|
printf(_(" -p, --old-port=PORT old cluster port number (default %d)\n"), old_cluster.port);
|
|
|
|
printf(_(" -P, --new-port=PORT new cluster port number (default %d)\n"), new_cluster.port);
|
|
|
|
printf(_(" -r, --retain retain SQL and log files after success\n"));
|
2019-07-09 15:47:09 +02:00
|
|
|
printf(_(" -s, --socketdir=DIR socket directory to use (default current dir.)\n"));
|
2016-10-14 18:00:00 +02:00
|
|
|
printf(_(" -U, --username=NAME cluster superuser (default \"%s\")\n"), os_info.user);
|
|
|
|
printf(_(" -v, --verbose enable verbose internal logging\n"));
|
|
|
|
printf(_(" -V, --version display version information, then exit\n"));
|
2018-11-07 18:05:54 +01:00
|
|
|
printf(_(" --clone clone instead of copying files to new cluster\n"));
|
2016-10-14 18:00:00 +02:00
|
|
|
printf(_(" -?, --help show this help, then exit\n"));
|
|
|
|
printf(_("\n"
|
|
|
|
"Before running pg_upgrade you must:\n"
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
" create a new database cluster (using the new version of initdb)\n"
|
2016-10-14 18:00:00 +02:00
|
|
|
" shutdown the postmaster servicing the old cluster\n"
|
|
|
|
" shutdown the postmaster servicing the new cluster\n"));
|
|
|
|
printf(_("\n"
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
"When you run pg_upgrade, you must provide the following information:\n"
|
2016-10-14 18:00:00 +02: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"));
|
|
|
|
printf(_("\n"
|
|
|
|
"For example:\n"
|
|
|
|
" pg_upgrade -d oldCluster/data -D newCluster/data -b oldCluster/bin -B newCluster/bin\n"
|
|
|
|
"or\n"));
|
2010-05-12 04:19:11 +02:00
|
|
|
#ifndef WIN32
|
2016-10-14 18:00:00 +02:00
|
|
|
printf(_(" $ export PGDATAOLD=oldCluster/data\n"
|
|
|
|
" $ export PGDATANEW=newCluster/data\n"
|
|
|
|
" $ export PGBINOLD=oldCluster/bin\n"
|
|
|
|
" $ export PGBINNEW=newCluster/bin\n"
|
|
|
|
" $ pg_upgrade\n"));
|
2010-05-12 04:19:11 +02:00
|
|
|
#else
|
2016-10-14 18:00:00 +02:00
|
|
|
printf(_(" C:\\> set PGDATAOLD=oldCluster/data\n"
|
|
|
|
" C:\\> set PGDATANEW=newCluster/data\n"
|
|
|
|
" C:\\> set PGBINOLD=oldCluster/bin\n"
|
|
|
|
" C:\\> set PGBINNEW=newCluster/bin\n"
|
|
|
|
" C:\\> pg_upgrade\n"));
|
2010-05-12 04:19:11 +02:00
|
|
|
#endif
|
2020-02-28 08:54:49 +01:00
|
|
|
printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
|
2020-02-28 08:54:49 +01:00
|
|
|
printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2011-06-22 23:50:40 +02:00
|
|
|
* check_required_directory()
|
2010-05-12 04:19:11 +02:00
|
|
|
*
|
2011-06-22 23:50:40 +02:00
|
|
|
* Checks a directory option.
|
2018-12-01 21:45:11 +01:00
|
|
|
* dirpath - the directory name supplied on the command line, or NULL
|
2010-05-12 04:19:11 +02:00
|
|
|
* envVarName - the name of an environment variable to get if dirpath is NULL
|
2018-12-01 21:45:11 +01:00
|
|
|
* useCwd - true if OK to default to CWD
|
|
|
|
* cmdLineOption - the command line option for this directory
|
2010-05-12 04:19:11 +02:00
|
|
|
* description - a description of this directory option
|
2019-07-27 07:56:20 +02:00
|
|
|
* missingOk - true if OK that both dirpath and envVarName are not existing
|
2010-05-12 04:19:11 +02:00
|
|
|
*
|
|
|
|
* We use the last two arguments to construct a meaningful error message if the
|
|
|
|
* user hasn't provided the required directory name.
|
|
|
|
*/
|
|
|
|
static void
|
2018-12-01 21:45:11 +01:00
|
|
|
check_required_directory(char **dirpath, const char *envVarName, bool useCwd,
|
2019-07-27 07:56:20 +02:00
|
|
|
const char *cmdLineOption, const char *description,
|
|
|
|
bool missingOk)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
2011-06-15 23:23:02 +02:00
|
|
|
if (*dirpath == NULL || strlen(*dirpath) == 0)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
|
|
|
const char *envVar;
|
|
|
|
|
|
|
|
if ((envVar = getenv(envVarName)) && strlen(envVar))
|
2010-10-19 23:38:16 +02:00
|
|
|
*dirpath = pg_strdup(envVar);
|
2018-12-01 21:45:11 +01:00
|
|
|
else if (useCwd)
|
|
|
|
{
|
|
|
|
char cwd[MAXPGPATH];
|
|
|
|
|
|
|
|
if (!getcwd(cwd, MAXPGPATH))
|
|
|
|
pg_fatal("could not determine current directory\n");
|
|
|
|
*dirpath = pg_strdup(cwd);
|
2012-07-06 05:36:30 +02:00
|
|
|
}
|
2019-07-27 07:56:20 +02:00
|
|
|
else if (missingOk)
|
|
|
|
return;
|
2010-05-12 04:19:11 +02:00
|
|
|
else
|
2013-10-02 03:24:56 +02:00
|
|
|
pg_fatal("You must identify the directory where the %s.\n"
|
2014-05-06 18:12:18 +02:00
|
|
|
"Please use the %s command-line option or the %s environment variable.\n",
|
|
|
|
description, cmdLineOption, envVarName);
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-12-01 21:45:11 +01:00
|
|
|
* Clean up the path, in particular trimming any trailing path separators,
|
|
|
|
* because we construct paths by appending to this path.
|
2010-05-12 04:19:11 +02:00
|
|
|
*/
|
2018-12-01 21:45:11 +01:00
|
|
|
canonicalize_path(*dirpath);
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
2011-10-07 20:40:23 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* adjust_data_dir
|
|
|
|
*
|
|
|
|
* If a configuration-only directory was specified, find the real data dir
|
2016-03-15 23:06:11 +01:00
|
|
|
* by querying the running server. This has limited checking because we
|
2011-10-07 20:40:23 +02:00
|
|
|
* can't check for a running server because we can't find postmaster.pid.
|
2018-12-01 21:45:11 +01:00
|
|
|
*
|
|
|
|
* On entry, cluster->pgdata has been set from command line or env variable,
|
|
|
|
* but cluster->pgconfig isn't set. We fill both variables with corrected
|
|
|
|
* values.
|
2011-10-07 20:40:23 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
adjust_data_dir(ClusterInfo *cluster)
|
|
|
|
{
|
|
|
|
char filename[MAXPGPATH];
|
2012-06-10 21:20:04 +02:00
|
|
|
char cmd[MAXPGPATH],
|
|
|
|
cmd_output[MAX_STRING];
|
|
|
|
FILE *fp,
|
|
|
|
*output;
|
2011-10-07 20:40:23 +02:00
|
|
|
|
2018-12-01 21:45:11 +01:00
|
|
|
/* Initially assume config dir and data dir are the same */
|
|
|
|
cluster->pgconfig = pg_strdup(cluster->pgdata);
|
|
|
|
|
2011-10-07 20:40:23 +02: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 00:47:54 +01:00
|
|
|
if ((fp = fopen(filename, "r")) == NULL)
|
2011-10-07 20:40:23 +02:00
|
|
|
return;
|
2012-03-13 00:47:54 +01:00
|
|
|
fclose(fp);
|
2011-10-07 20:40:23 +02: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 00:47:54 +01:00
|
|
|
if ((fp = fopen(filename, "r")) != NULL)
|
2011-10-07 20:40:23 +02:00
|
|
|
{
|
2012-03-13 00:47:54 +01:00
|
|
|
fclose(fp);
|
2011-10-07 20:40:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Must be a configuration directory, so find the real data directory. */
|
|
|
|
|
2017-07-15 01:20:21 +02:00
|
|
|
if (cluster == &old_cluster)
|
|
|
|
prep_status("Finding the real data directory for the source cluster");
|
|
|
|
else
|
|
|
|
prep_status("Finding the real data directory for the target cluster");
|
2011-10-07 20:40:23 +02:00
|
|
|
|
|
|
|
/*
|
2012-06-10 21:20:04 +02:00
|
|
|
* We don't have a data directory yet, so we can't check the PG version,
|
2014-05-06 18:12:18 +02:00
|
|
|
* so this might fail --- only works for PG 9.2+. If this fails,
|
2012-06-10 21:20:04 +02:00
|
|
|
* pg_upgrade will fail anyway because the data files will not be found.
|
2011-10-07 20:40:23 +02:00
|
|
|
*/
|
2015-04-02 00:25:45 +02:00
|
|
|
snprintf(cmd, sizeof(cmd), "\"%s/postgres\" -D \"%s\" -C data_directory",
|
2011-10-07 20:40:23 +02:00
|
|
|
cluster->bindir, cluster->pgconfig);
|
|
|
|
|
|
|
|
if ((output = popen(cmd, "r")) == NULL ||
|
|
|
|
fgets(cmd_output, sizeof(cmd_output), output) == NULL)
|
Improve error reporting in pg_upgrade's file copying/linking/rewriting.
The previous design for this had copyFile(), linkFile(), and
rewriteVisibilityMap() returning strerror strings, with the caller
producing one-size-fits-all error messages based on that. This made it
impossible to produce messages that described the failures with any degree
of precision, especially not short-read problems since those don't set
errno at all.
Since pg_upgrade has no intention of continuing after any error in this
area, let's fix this by just letting these functions call pg_fatal() for
themselves, making it easy for each point of failure to have a suitable
error message. Taking this approach also allows dropping cleanup code
that was unnecessary and was often rather sloppy about preserving errno.
To not lose relevant info that was reported before, pass in the schema name
and table name of the current table so that they can be included in the
error reports.
An additional problem was the use of getErrorText(), which was flat out
wrong for all but a couple of call sites, because it unconditionally did
"_dosmaperr(GetLastError())" on Windows. That's only appropriate when
reporting an error from a Windows-native API, which only a couple of
the callers were actually doing. Thus, even the reported strerror string
would be unrelated to the actual failure in many cases on Windows.
To fix, get rid of getErrorText() altogether, and just have call sites
do strerror(errno) instead, since that's the way all the rest of our
frontend programs do it. Add back the _dosmaperr() calls in the two
places where that's actually appropriate.
In passing, make assorted messages hew more closely to project style
guidelines, notably by removing initial capitals in not-complete-sentence
primary error messages. (I didn't make any effort to clean up places
I didn't have another reason to touch, though.)
Per discussion of a report from Thomas Kellerer. Back-patch to 9.6,
but no further; given the relative infrequency of reports of problems
here, it's not clear it's worth adapting the patch to older branches.
Patch by me, but with credit to Alvaro Herrera for spotting the issue
with getErrorText's misuse of _dosmaperr().
Discussion: <nsjrbh$8li$1@blaine.gmane.org>
2016-10-01 02:40:27 +02:00
|
|
|
pg_fatal("could not get data directory using %s: %s\n",
|
|
|
|
cmd, strerror(errno));
|
2011-10-07 20:40:23 +02:00
|
|
|
|
|
|
|
pclose(output);
|
|
|
|
|
2019-08-09 04:05:14 +02:00
|
|
|
/* strip trailing newline and carriage return */
|
|
|
|
(void) pg_strip_crlf(cmd_output);
|
2011-10-07 20:40:23 +02:00
|
|
|
|
|
|
|
cluster->pgdata = pg_strdup(cmd_output);
|
|
|
|
|
|
|
|
check_ok();
|
|
|
|
}
|
2012-09-03 19:52:34 +02: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-29 22:58:43 +02:00
|
|
|
|
2012-09-04 04:15:09 +02:00
|
|
|
/*
|
2013-05-29 22:58:43 +02: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 04:15:09 +02:00
|
|
|
*/
|
|
|
|
if (GET_MAJOR_VERSION(cluster->major_version) >= 901)
|
2012-09-03 19:52:34 +02:00
|
|
|
{
|
2012-09-04 04:15:09 +02:00
|
|
|
if (!live_check)
|
2018-12-01 21:45:11 +01:00
|
|
|
cluster->sockdir = user_opts.socketdir;
|
2012-09-04 04:15:09 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2013-05-29 22:58:43 +02: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 04:15:09 +02:00
|
|
|
*/
|
|
|
|
unsigned short orig_port = cluster->port;
|
2013-05-29 22:58:43 +02:00
|
|
|
char filename[MAXPGPATH],
|
|
|
|
line[MAXPGPATH];
|
|
|
|
FILE *fp;
|
2012-09-04 04:15:09 +02:00
|
|
|
int lineno;
|
2013-05-29 22:58:43 +02:00
|
|
|
|
2012-09-03 19:52:34 +02:00
|
|
|
snprintf(filename, sizeof(filename), "%s/postmaster.pid",
|
|
|
|
cluster->pgdata);
|
|
|
|
if ((fp = fopen(filename, "r")) == NULL)
|
2017-09-09 23:32:10 +02:00
|
|
|
pg_fatal("could not open file \"%s\": %s\n",
|
|
|
|
filename, strerror(errno));
|
2013-05-29 22:58:43 +02:00
|
|
|
|
2012-09-04 04:15:09 +02:00
|
|
|
for (lineno = 1;
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
lineno <= Max(LOCK_FILE_LINE_PORT, LOCK_FILE_LINE_SOCKET_DIR);
|
2012-09-04 04:15:09 +02:00
|
|
|
lineno++)
|
|
|
|
{
|
|
|
|
if (fgets(line, sizeof(line), fp) == NULL)
|
2017-09-09 23:32:10 +02:00
|
|
|
pg_fatal("could not read line %d from file \"%s\": %s\n",
|
|
|
|
lineno, filename, strerror(errno));
|
2013-05-29 22:58:43 +02:00
|
|
|
|
2012-09-04 04:15:09 +02: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)
|
|
|
|
{
|
2019-08-09 04:05:14 +02:00
|
|
|
/* strip trailing newline and carriage return */
|
2014-02-15 17:50:56 +01:00
|
|
|
cluster->sockdir = pg_strdup(line);
|
2019-08-09 04:05:14 +02:00
|
|
|
(void) pg_strip_crlf(cluster->sockdir);
|
2012-09-04 04:15:09 +02:00
|
|
|
}
|
|
|
|
}
|
2012-09-03 19:52:34 +02:00
|
|
|
fclose(fp);
|
2013-05-29 22:58:43 +02:00
|
|
|
|
2012-09-04 04:15:09 +02:00
|
|
|
/* warn of port number correction */
|
|
|
|
if (orig_port != DEF_PGUPORT && old_cluster.port != orig_port)
|
2017-09-09 23:32:10 +02:00
|
|
|
pg_log(PG_WARNING, "user-supplied old port number %hu corrected to %hu\n",
|
2013-05-29 22:58:43 +02:00
|
|
|
orig_port, cluster->port);
|
2012-09-03 19:52:34 +02:00
|
|
|
}
|
|
|
|
}
|
2012-09-04 04:15:09 +02:00
|
|
|
else
|
|
|
|
|
2013-05-29 22:58:43 +02: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-03 19:52:34 +02:00
|
|
|
cluster->sockdir = NULL;
|
|
|
|
#endif
|
|
|
|
}
|