22static void usage(
void);
24 const char *envVarName,
bool useCwd,
27#define FIX_DEFAULT_READ_ONLY "-c default_transaction_read_only=false"
41 static struct option long_options[] = {
70 int os_user_effective_id;
95 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
100 if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
102 puts(
"pg_upgrade (PostgreSQL) " PG_VERSION);
108 if (os_user_effective_id == 0)
112 long_options, &optindex)) != -1)
176 pg_fatal(
"invalid old port number");
181 pg_fatal(
"invalid new port number");
229 pg_fatal(
"invalid argument for option %s",
"--set-char-signedness");
232 fprintf(stderr,
_(
"Try \"%s --help\" for more information.\n"),
239 pg_fatal(
"too many command-line arguments (first is \"%s\")", argv[
optind]);
250 if (getenv(
"PGOPTIONS"))
253 getenv(
"PGOPTIONS"));
255 setenv(
"PGOPTIONS", pgoptions, 1);
263 "-b",
_(
"old cluster binaries reside"),
false);
265 "-B",
_(
"new cluster binaries reside"),
true);
267 "-d",
_(
"old cluster data resides"),
false);
269 "-D",
_(
"new cluster data resides"),
false);
271 "-s",
_(
"sockets will be created"),
false);
288 pg_fatal(
"could not determine current directory");
291 pg_fatal(
"cannot run pg_upgrade from inside the new cluster data directory on Windows");
300 printf(
_(
"pg_upgrade upgrades a PostgreSQL cluster to a different major version.\n\n"));
302 printf(
_(
" pg_upgrade [OPTION]...\n\n"));
304 printf(
_(
" -b, --old-bindir=BINDIR old cluster executable directory\n"));
305 printf(
_(
" -B, --new-bindir=BINDIR new cluster executable directory (default\n"
306 " same directory as pg_upgrade)\n"));
307 printf(
_(
" -c, --check check clusters only, don't change any data\n"));
308 printf(
_(
" -d, --old-datadir=DATADIR old cluster data directory\n"));
309 printf(
_(
" -D, --new-datadir=DATADIR new cluster data directory\n"));
310 printf(
_(
" -j, --jobs=NUM number of simultaneous processes or threads to use\n"));
311 printf(
_(
" -k, --link link instead of copying files to new cluster\n"));
312 printf(
_(
" -N, --no-sync do not wait for changes to be written safely to disk\n"));
313 printf(
_(
" -o, --old-options=OPTIONS old cluster options to pass to the server\n"));
314 printf(
_(
" -O, --new-options=OPTIONS new cluster options to pass to the server\n"));
317 printf(
_(
" -r, --retain retain SQL and log files after success\n"));
318 printf(
_(
" -s, --socketdir=DIR socket directory to use (default current dir.)\n"));
320 printf(
_(
" -v, --verbose enable verbose internal logging\n"));
321 printf(
_(
" -V, --version display version information, then exit\n"));
322 printf(
_(
" --clone clone instead of copying files to new cluster\n"));
323 printf(
_(
" --copy copy files to new cluster (default)\n"));
324 printf(
_(
" --copy-file-range copy files to new cluster with copy_file_range\n"));
325 printf(
_(
" --no-statistics do not import statistics from old cluster\n"));
326 printf(
_(
" --set-char-signedness=OPTION set new cluster char signedness to \"signed\" or\n"
328 printf(
_(
" --sync-method=METHOD set method for syncing files to disk\n"));
329 printf(
_(
" -?, --help show this help, then exit\n"));
331 "Before running pg_upgrade you must:\n"
332 " create a new database cluster (using the new version of initdb)\n"
333 " shutdown the postmaster servicing the old cluster\n"
334 " shutdown the postmaster servicing the new cluster\n"));
336 "When you run pg_upgrade, you must provide the following information:\n"
337 " the data directory for the old cluster (-d DATADIR)\n"
338 " the data directory for the new cluster (-D DATADIR)\n"
339 " the \"bin\" directory for the old version (-b BINDIR)\n"
340 " the \"bin\" directory for the new version (-B BINDIR)\n"));
343 " pg_upgrade -d oldCluster/data -D newCluster/data -b oldCluster/bin -B newCluster/bin\n"
346 printf(
_(
" $ export PGDATAOLD=oldCluster/data\n"
347 " $ export PGDATANEW=newCluster/data\n"
348 " $ export PGBINOLD=oldCluster/bin\n"
349 " $ export PGBINNEW=newCluster/bin\n"
352 printf(
_(
" C:\\> set PGDATAOLD=oldCluster/data\n"
353 " C:\\> set PGDATANEW=newCluster/data\n"
354 " C:\\> set PGBINOLD=oldCluster/bin\n"
355 " C:\\> set PGBINNEW=newCluster/bin\n"
356 " C:\\> pg_upgrade\n"));
358 printf(
_(
"\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
359 printf(
_(
"%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
379 const char *cmdLineOption,
const char *
description,
382 if (*dirpath == NULL || strlen(*dirpath) == 0)
386 if ((envVar = getenv(envVarName)) && strlen(envVar))
393 pg_fatal(
"could not determine current directory");
399 pg_fatal(
"You must identify the directory where the %s.\n"
400 "Please use the %s command-line option or the %s environment variable.",
437 if ((fp = fopen(
filename,
"r")) == NULL)
443 if ((fp = fopen(
filename,
"r")) != NULL)
452 prep_status(
"Finding the real data directory for the source cluster");
454 prep_status(
"Finding the real data directory for the target cluster");
461 snprintf(cmd,
sizeof(cmd),
"\"%s/postgres\" -D \"%s\" -C data_directory",
465 if ((
output = popen(cmd,
"r")) == NULL ||
466 fgets(cmd_output,
sizeof(cmd_output),
output) == NULL)
467 pg_fatal(
"could not get data directory using %s: %m", cmd);
471 pg_fatal(
"could not get data directory using %s: %s",
503 unsigned short orig_port =
cluster->port;
511 if ((fp = fopen(
filename,
"r")) == NULL)
518 if (fgets(line,
sizeof(line), fp) == NULL)
519 pg_fatal(
"could not read line %d from file \"%s\": %m",
void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
#define fprintf(file, fmt, msg)
char * pg_strdup(const char *in)
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
static void check_ok(void)
void pfree(void *pointer)
bool parse_sync_method(const char *optarg, DataDirSyncMethod *sync_method)
PGDLLIMPORT char * optarg
int get_user_info(char **user_name_p)
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
@ TRANSFER_MODE_COPY_FILE_RANGE
void prep_status(const char *fmt,...) pg_attribute_printf(1
#define LOCK_FILE_LINE_PORT
#define LOCK_FILE_LINE_SOCKET_DIR
bool path_is_prefix_of_path(const char *path1, const char *path2)
int pg_strcasecmp(const char *s1, const char *s2)
void canonicalize_path(char *path)
const char * get_progname(const char *argv0)
size_t strlcpy(char *dst, const char *src, size_t siz)
char * psprintf(const char *fmt,...)
#define FIX_DEFAULT_READ_ONLY
void adjust_data_dir(ClusterInfo *cluster)
void parseCommandLine(int argc, char *argv[])
void get_sock_dir(ClusterInfo *cluster)
static void check_required_directory(char **dirpath, const char *envVarName, bool useCwd, const char *cmdLineOption, const char *description, bool missingOk)
int pg_strip_crlf(char *str)
transferMode transfer_mode
char * wait_result_to_str(int exitstatus)