PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
option.c File Reference
#include "postgres_fe.h"
#include "common/string.h"
#include "fe_utils/option_utils.h"
#include "getopt_long.h"
#include "pg_upgrade.h"
#include "utils/pidfile.h"
Include dependency graph for option.c:

Go to the source code of this file.

Macros

#define FIX_DEFAULT_READ_ONLY   "-c default_transaction_read_only=false"
 

Functions

static void usage (void)
 
static void check_required_directory (char **dirpath, const char *envVarName, bool useCwd, const char *cmdLineOption, const char *description, bool missingOk)
 
void parseCommandLine (int argc, char *argv[])
 
void adjust_data_dir (ClusterInfo *cluster)
 
void get_sock_dir (ClusterInfo *cluster)
 

Variables

UserOpts user_opts
 

Macro Definition Documentation

◆ FIX_DEFAULT_READ_ONLY

#define FIX_DEFAULT_READ_ONLY   "-c default_transaction_read_only=false"

Definition at line 27 of file option.c.

Function Documentation

◆ adjust_data_dir()

void adjust_data_dir ( ClusterInfo cluster)

Definition at line 423 of file option.c.

424{
425 char filename[MAXPGPATH];
426 char cmd[MAXPGPATH],
427 cmd_output[MAX_STRING];
428 FILE *fp,
429 *output;
430 int rc;
431
432 /* Initially assume config dir and data dir are the same */
433 cluster->pgconfig = pg_strdup(cluster->pgdata);
434
435 /* If there is no postgresql.conf, it can't be a config-only dir */
436 snprintf(filename, sizeof(filename), "%s/postgresql.conf", cluster->pgconfig);
437 if ((fp = fopen(filename, "r")) == NULL)
438 return;
439 fclose(fp);
440
441 /* If PG_VERSION exists, it can't be a config-only dir */
442 snprintf(filename, sizeof(filename), "%s/PG_VERSION", cluster->pgconfig);
443 if ((fp = fopen(filename, "r")) != NULL)
444 {
445 fclose(fp);
446 return;
447 }
448
449 /* Must be a configuration directory, so find the real data directory. */
450
451 if (cluster == &old_cluster)
452 prep_status("Finding the real data directory for the source cluster");
453 else
454 prep_status("Finding the real data directory for the target cluster");
455
456 /*
457 * We don't have a data directory yet, so we can't check the PG version,
458 * so this might fail --- only works for PG 9.2+. If this fails,
459 * pg_upgrade will fail anyway because the data files will not be found.
460 */
461 snprintf(cmd, sizeof(cmd), "\"%s/postgres\" -D \"%s\" -C data_directory",
462 cluster->bindir, cluster->pgconfig);
463 fflush(NULL);
464
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);
468
469 rc = pclose(output);
470 if (rc != 0)
471 pg_fatal("could not get data directory using %s: %s",
472 cmd, wait_result_to_str(rc));
473
474 /* strip trailing newline and carriage return */
475 (void) pg_strip_crlf(cmd_output);
476
477 cluster->pgdata = pg_strdup(cmd_output);
478
479 check_ok();
480}
void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:107
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
FILE * output
static void check_ok(void)
Definition: initdb.c:2121
#define pg_fatal(...)
#define MAXPGPATH
static char * filename
Definition: pg_dumpall.c:123
ClusterInfo old_cluster
Definition: pg_upgrade.c:68
#define MAX_STRING
Definition: pg_upgrade.h:22
void prep_status(const char *fmt,...) pg_attribute_printf(1
#define snprintf
Definition: port.h:239
int pg_strip_crlf(char *str)
Definition: string.c:154
char * wait_result_to_str(int exitstatus)
Definition: wait_error.c:33

References check_ok(), cluster(), filename, MAX_STRING, MAXPGPATH, old_cluster, output, pg_fatal, pg_strdup(), pg_strip_crlf(), prep_status(), snprintf, and wait_result_to_str().

◆ check_required_directory()

static void check_required_directory ( char **  dirpath,
const char *  envVarName,
bool  useCwd,
const char *  cmdLineOption,
const char *  description,
bool  missingOk 
)
static

Definition at line 378 of file option.c.

381{
382 if (*dirpath == NULL || strlen(*dirpath) == 0)
383 {
384 const char *envVar;
385
386 if ((envVar = getenv(envVarName)) && strlen(envVar))
387 *dirpath = pg_strdup(envVar);
388 else if (useCwd)
389 {
390 char cwd[MAXPGPATH];
391
392 if (!getcwd(cwd, MAXPGPATH))
393 pg_fatal("could not determine current directory");
394 *dirpath = pg_strdup(cwd);
395 }
396 else if (missingOk)
397 return;
398 else
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.",
401 description, cmdLineOption, envVarName);
402 }
403
404 /*
405 * Clean up the path, in particular trimming any trailing path separators,
406 * because we construct paths by appending to this path.
407 */
408 canonicalize_path(*dirpath);
409}
void canonicalize_path(char *path)
Definition: path.c:337
const char * description

References canonicalize_path(), description, MAXPGPATH, pg_fatal, and pg_strdup().

Referenced by parseCommandLine().

◆ get_sock_dir()

void get_sock_dir ( ClusterInfo cluster)

Definition at line 492 of file option.c.

493{
494#if !defined(WIN32)
496 cluster->sockdir = user_opts.socketdir;
497 else
498 {
499 /*
500 * If we are doing a live check, we will use the old cluster's Unix
501 * domain socket directory so we can connect to the live server.
502 */
503 unsigned short orig_port = cluster->port;
504 char filename[MAXPGPATH],
505 line[MAXPGPATH];
506 FILE *fp;
507 int lineno;
508
509 snprintf(filename, sizeof(filename), "%s/postmaster.pid",
510 cluster->pgdata);
511 if ((fp = fopen(filename, "r")) == NULL)
512 pg_fatal("could not open file \"%s\": %m", filename);
513
514 for (lineno = 1;
516 lineno++)
517 {
518 if (fgets(line, sizeof(line), fp) == NULL)
519 pg_fatal("could not read line %d from file \"%s\": %m",
520 lineno, filename);
521
522 /* potentially overwrite user-supplied value */
523 if (lineno == LOCK_FILE_LINE_PORT)
524 sscanf(line, "%hu", &old_cluster.port);
525 if (lineno == LOCK_FILE_LINE_SOCKET_DIR)
526 {
527 /* strip trailing newline and carriage return */
528 cluster->sockdir = pg_strdup(line);
529 (void) pg_strip_crlf(cluster->sockdir);
530 }
531 }
532 fclose(fp);
533
534 /* warn of port number correction */
535 if (orig_port != DEF_PGUPORT && old_cluster.port != orig_port)
536 pg_log(PG_WARNING, "user-supplied old port number %hu corrected to %hu",
537 orig_port, cluster->port);
538 }
539#else /* WIN32 */
540 cluster->sockdir = NULL;
541#endif
542}
#define Max(x, y)
Definition: c.h:969
ClusterInfo new_cluster
Definition: pg_upgrade.c:69
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
#define DEF_PGUPORT
Definition: pg_upgrade.h:20
@ PG_WARNING
Definition: pg_upgrade.h:276
#define LOCK_FILE_LINE_PORT
Definition: pidfile.h:40
#define LOCK_FILE_LINE_SOCKET_DIR
Definition: pidfile.h:41
UserOpts user_opts
Definition: option.c:30
unsigned short port
Definition: pg_upgrade.h:298
char * socketdir
Definition: pg_upgrade.h:334
bool live_check
Definition: pg_upgrade.h:330

References cluster(), DEF_PGUPORT, filename, UserOpts::live_check, LOCK_FILE_LINE_PORT, LOCK_FILE_LINE_SOCKET_DIR, Max, MAXPGPATH, new_cluster, old_cluster, pg_fatal, pg_log(), pg_strdup(), pg_strip_crlf(), PG_WARNING, ClusterInfo::port, snprintf, UserOpts::socketdir, and user_opts.

Referenced by main().

◆ parseCommandLine()

void parseCommandLine ( int  argc,
char *  argv[] 
)

Definition at line 39 of file option.c.

40{
41 static struct option long_options[] = {
42 {"old-datadir", required_argument, NULL, 'd'},
43 {"new-datadir", required_argument, NULL, 'D'},
44 {"old-bindir", required_argument, NULL, 'b'},
45 {"new-bindir", required_argument, NULL, 'B'},
46 {"no-sync", no_argument, NULL, 'N'},
47 {"old-options", required_argument, NULL, 'o'},
48 {"new-options", required_argument, NULL, 'O'},
49 {"old-port", required_argument, NULL, 'p'},
50 {"new-port", required_argument, NULL, 'P'},
51
52 {"username", required_argument, NULL, 'U'},
53 {"check", no_argument, NULL, 'c'},
54 {"link", no_argument, NULL, 'k'},
55 {"retain", no_argument, NULL, 'r'},
56 {"jobs", required_argument, NULL, 'j'},
57 {"socketdir", required_argument, NULL, 's'},
58 {"verbose", no_argument, NULL, 'v'},
59 {"clone", no_argument, NULL, 1},
60 {"copy", no_argument, NULL, 2},
61 {"copy-file-range", no_argument, NULL, 3},
62 {"sync-method", required_argument, NULL, 4},
63 {"no-statistics", no_argument, NULL, 5},
64 {"set-char-signedness", required_argument, NULL, 6},
65
66 {NULL, 0, NULL, 0}
67 };
68 int option; /* Command line option */
69 int optindex = 0; /* used by getopt_long */
70 int os_user_effective_id;
71 DataDirSyncMethod unused;
72
73 user_opts.do_sync = true;
77
78 os_info.progname = get_progname(argv[0]);
79
80 /* Process libpq env. variables; load values here for usage() output */
81 old_cluster.port = getenv("PGPORTOLD") ? atoi(getenv("PGPORTOLD")) : DEF_PGUPORT;
82 new_cluster.port = getenv("PGPORTNEW") ? atoi(getenv("PGPORTNEW")) : DEF_PGUPORT;
83
84 os_user_effective_id = get_user_info(&os_info.user);
85 /* we override just the database user name; we got the OS id above */
86 if (getenv("PGUSER"))
87 {
89 /* must save value, getenv()'s pointer is not stable */
90 os_info.user = pg_strdup(getenv("PGUSER"));
91 }
92
93 if (argc > 1)
94 {
95 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
96 {
97 usage();
98 exit(0);
99 }
100 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
101 {
102 puts("pg_upgrade (PostgreSQL) " PG_VERSION);
103 exit(0);
104 }
105 }
106
107 /* Allow help and version to be run as root, so do the test here. */
108 if (os_user_effective_id == 0)
109 pg_fatal("%s: cannot be run as root", os_info.progname);
110
111 while ((option = getopt_long(argc, argv, "b:B:cd:D:j:kNo:O:p:P:rs:U:v",
112 long_options, &optindex)) != -1)
113 {
114 switch (option)
115 {
116 case 'b':
118 break;
119
120 case 'B':
122 break;
123
124 case 'c':
125 user_opts.check = true;
126 break;
127
128 case 'd':
130 break;
131
132 case 'D':
134 break;
135
136 case 'j':
137 user_opts.jobs = atoi(optarg);
138 break;
139
140 case 'k':
142 break;
143
144 case 'N':
145 user_opts.do_sync = false;
146 break;
147
148 case 'o':
149 /* append option? */
150 if (!old_cluster.pgopts)
152 else
153 {
154 char *old_pgopts = old_cluster.pgopts;
155
156 old_cluster.pgopts = psprintf("%s %s", old_pgopts, optarg);
157 free(old_pgopts);
158 }
159 break;
160
161 case 'O':
162 /* append option? */
163 if (!new_cluster.pgopts)
165 else
166 {
167 char *new_pgopts = new_cluster.pgopts;
168
169 new_cluster.pgopts = psprintf("%s %s", new_pgopts, optarg);
170 free(new_pgopts);
171 }
172 break;
173
174 case 'p':
175 if ((old_cluster.port = atoi(optarg)) <= 0)
176 pg_fatal("invalid old port number");
177 break;
178
179 case 'P':
180 if ((new_cluster.port = atoi(optarg)) <= 0)
181 pg_fatal("invalid new port number");
182 break;
183
184 case 'r':
185 log_opts.retain = true;
186 break;
187
188 case 's':
190 break;
191
192 case 'U':
195 os_info.user_specified = true;
196 break;
197
198 case 'v':
199 log_opts.verbose = true;
200 break;
201
202 case 1:
204 break;
205
206 case 2:
208 break;
209
210 case 3:
212 break;
213 case 4:
214 if (!parse_sync_method(optarg, &unused))
215 exit(1);
217 break;
218
219 case 5:
220 user_opts.do_statistics = false;
221 break;
222
223 case 6:
224 if (pg_strcasecmp(optarg, "signed") == 0)
226 else if (pg_strcasecmp(optarg, "unsigned") == 0)
228 else
229 pg_fatal("invalid argument for option %s", "--set-char-signedness");
230 break;
231 default:
232 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
234 exit(1);
235 }
236 }
237
238 if (optind < argc)
239 pg_fatal("too many command-line arguments (first is \"%s\")", argv[optind]);
240
243
244 if (log_opts.verbose)
245 pg_log(PG_REPORT, "Running in verbose mode");
246
247 log_opts.isatty = isatty(fileno(stdout));
248
249 /* Turn off read-only mode; add prefix to PGOPTIONS? */
250 if (getenv("PGOPTIONS"))
251 {
252 char *pgoptions = psprintf("%s %s", FIX_DEFAULT_READ_ONLY,
253 getenv("PGOPTIONS"));
254
255 setenv("PGOPTIONS", pgoptions, 1);
256 pfree(pgoptions);
257 }
258 else
259 setenv("PGOPTIONS", FIX_DEFAULT_READ_ONLY, 1);
260
261 /* Get values from env if not already set */
262 check_required_directory(&old_cluster.bindir, "PGBINOLD", false,
263 "-b", _("old cluster binaries reside"), false);
264 check_required_directory(&new_cluster.bindir, "PGBINNEW", false,
265 "-B", _("new cluster binaries reside"), true);
266 check_required_directory(&old_cluster.pgdata, "PGDATAOLD", false,
267 "-d", _("old cluster data resides"), false);
268 check_required_directory(&new_cluster.pgdata, "PGDATANEW", false,
269 "-D", _("new cluster data resides"), false);
270 check_required_directory(&user_opts.socketdir, "PGSOCKETDIR", true,
271 "-s", _("sockets will be created"), false);
272
273#ifdef WIN32
274
275 /*
276 * On Windows, initdb --sync-only will fail with a "Permission denied"
277 * error on file pg_upgrade_utility.log if pg_upgrade is run inside the
278 * new cluster directory, so we do a check here.
279 */
280 {
281 char cwd[MAXPGPATH],
282 new_cluster_pgdata[MAXPGPATH];
283
284 strlcpy(new_cluster_pgdata, new_cluster.pgdata, MAXPGPATH);
285 canonicalize_path(new_cluster_pgdata);
286
287 if (!getcwd(cwd, MAXPGPATH))
288 pg_fatal("could not determine current directory");
290 if (path_is_prefix_of_path(new_cluster_pgdata, cwd))
291 pg_fatal("cannot run pg_upgrade from inside the new cluster data directory on Windows");
292 }
293#endif
294}
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
#define _(x)
Definition: elog.c:90
void pg_free(void *ptr)
Definition: fe_memutils.c:105
DataDirSyncMethod
Definition: file_utils.h:28
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:60
#define no_argument
Definition: getopt_long.h:25
#define required_argument
Definition: getopt_long.h:26
#define free(a)
Definition: header.h:65
void pfree(void *pointer)
Definition: mcxt.c:1524
bool parse_sync_method(const char *optarg, DataDirSyncMethod *sync_method)
Definition: option_utils.c:90
PGDLLIMPORT int optind
Definition: getopt.c:51
PGDLLIMPORT char * optarg
Definition: getopt.c:53
OSInfo os_info
Definition: pg_upgrade.c:70
int get_user_info(char **user_name_p)
Definition: util.c:323
@ TRANSFER_MODE_COPY
Definition: pg_upgrade.h:262
@ TRANSFER_MODE_LINK
Definition: pg_upgrade.h:264
@ TRANSFER_MODE_CLONE
Definition: pg_upgrade.h:261
@ TRANSFER_MODE_COPY_FILE_RANGE
Definition: pg_upgrade.h:263
LogOpts log_opts
Definition: util.c:17
@ PG_REPORT
Definition: pg_upgrade.h:275
bool path_is_prefix_of_path(const char *path1, const char *path2)
Definition: path.c:637
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
const char * get_progname(const char *argv0)
Definition: path.c:652
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
#define FIX_DEFAULT_READ_ONLY
Definition: option.c:27
static void usage(void)
Definition: option.c:298
static void check_required_directory(char **dirpath, const char *envVarName, bool useCwd, const char *cmdLineOption, const char *description, bool missingOk)
Definition: option.c:378
char * pgdata
Definition: pg_upgrade.h:291
char * bindir
Definition: pg_upgrade.h:294
char * pgopts
Definition: pg_upgrade.h:295
bool retain
Definition: pg_upgrade.h:314
bool isatty
Definition: pg_upgrade.h:320
bool verbose
Definition: pg_upgrade.h:313
char * user
Definition: pg_upgrade.h:354
const char * progname
Definition: pg_upgrade.h:353
bool user_specified
Definition: pg_upgrade.h:355
char * sync_method
Definition: pg_upgrade.h:335
bool do_statistics
Definition: pg_upgrade.h:336
bool do_sync
Definition: pg_upgrade.h:331
int char_signedness
Definition: pg_upgrade.h:337
transferMode transfer_mode
Definition: pg_upgrade.h:332
bool check
Definition: pg_upgrade.h:329
int jobs
Definition: pg_upgrade.h:333
#define setenv(x, y, z)
Definition: win32_port.h:545

References _, ClusterInfo::bindir, canonicalize_path(), UserOpts::char_signedness, UserOpts::check, check_required_directory(), DEF_PGUPORT, UserOpts::do_statistics, UserOpts::do_sync, FIX_DEFAULT_READ_ONLY, fprintf, free, get_progname(), get_user_info(), getopt_long(), LogOpts::isatty, UserOpts::jobs, log_opts, MAXPGPATH, new_cluster, no_argument, old_cluster, optarg, optind, os_info, parse_sync_method(), path_is_prefix_of_path(), pfree(), pg_fatal, pg_free(), pg_log(), PG_REPORT, pg_strcasecmp(), pg_strdup(), ClusterInfo::pgdata, ClusterInfo::pgopts, ClusterInfo::port, OSInfo::progname, psprintf(), required_argument, LogOpts::retain, setenv, UserOpts::socketdir, generate_unaccent_rules::stdout, strlcpy(), UserOpts::sync_method, UserOpts::transfer_mode, TRANSFER_MODE_CLONE, TRANSFER_MODE_COPY, TRANSFER_MODE_COPY_FILE_RANGE, TRANSFER_MODE_LINK, usage(), OSInfo::user, user_opts, OSInfo::user_specified, and LogOpts::verbose.

Referenced by main().

◆ usage()

static void usage ( void  )
static

Definition at line 298 of file option.c.

299{
300 printf(_("pg_upgrade upgrades a PostgreSQL cluster to a different major version.\n\n"));
301 printf(_("Usage:\n"));
302 printf(_(" pg_upgrade [OPTION]...\n\n"));
303 printf(_("Options:\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"));
315 printf(_(" -p, --old-port=PORT old cluster port number (default %d)\n"), old_cluster.port);
316 printf(_(" -P, --new-port=PORT new cluster port number (default %d)\n"), new_cluster.port);
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"));
319 printf(_(" -U, --username=NAME cluster superuser (default \"%s\")\n"), os_info.user);
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"
327 " \"unsigned\"\n"));
328 printf(_(" --sync-method=METHOD set method for syncing files to disk\n"));
329 printf(_(" -?, --help show this help, then exit\n"));
330 printf(_("\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"));
335 printf(_("\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"));
341 printf(_("\n"
342 "For example:\n"
343 " pg_upgrade -d oldCluster/data -D newCluster/data -b oldCluster/bin -B newCluster/bin\n"
344 "or\n"));
345#ifndef WIN32
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"
350 " $ pg_upgrade\n"));
351#else
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"));
357#endif
358 printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
359 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
360}
#define printf(...)
Definition: port.h:245

References _, new_cluster, old_cluster, os_info, ClusterInfo::port, printf, and OSInfo::user.

Referenced by parseCommandLine().

Variable Documentation

◆ user_opts