PostgreSQL Source Code: src/bin/pg_upgrade/option.c Source File (original) (raw)
1
2
3
4
5
6
7
8
9
11
12#ifdef WIN32
13#include <io.h>
14#endif
15
21
22static void usage(void);
24 const char *envVarName, bool useCwd,
25 const char *cmdLineOption, const char *description,
26 bool missingOk);
27#define FIX_DEFAULT_READ_ONLY "-c default_transaction_read_only=false"
28
29
31
32
33
34
35
36
37
38void
40{
41 static struct option long_options[] = {
51
61 {"copy-file-range", no_argument, NULL, 3},
66
67 {NULL, 0, NULL, 0}
68 };
69 int option;
70 int optindex = 0;
71 int os_user_effective_id;
73
78
80
81
84
86
87 if (getenv("PGUSER"))
88 {
90
92 }
93
94 if (argc > 1)
95 {
96 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
97 {
99 exit(0);
100 }
101 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
102 {
103 puts("pg_upgrade (PostgreSQL) " PG_VERSION);
104 exit(0);
105 }
106 }
107
108
109 if (os_user_effective_id == 0)
111
112 while ((option = getopt_long(argc, argv, "b:B:cd:D:j:kNo:O:p:P:rs:U:v",
113 long_options, &optindex)) != -1)
114 {
116 {
117 case 'b':
119 break;
120
121 case 'B':
123 break;
124
125 case 'c':
127 break;
128
129 case 'd':
131 break;
132
133 case 'D':
135 break;
136
137 case 'j':
139 break;
140
141 case 'k':
143 break;
144
145 case 'N':
147 break;
148
149 case 'o':
150
153 else
154 {
156
158 free(old_pgopts);
159 }
160 break;
161
162 case 'O':
163
166 else
167 {
169
171 free(new_pgopts);
172 }
173 break;
174
175 case 'p':
177 pg_fatal("invalid old port number");
178 break;
179
180 case 'P':
182 pg_fatal("invalid new port number");
183 break;
184
185 case 'r':
187 break;
188
189 case 's':
191 break;
192
193 case 'U':
197 break;
198
199 case 'v':
201 break;
202
203 case 1:
205 break;
206
207 case 2:
209 break;
210
211 case 3:
213 break;
214 case 4:
216 exit(1);
218 break;
219
220 case 5:
222 break;
223
224 case 6:
229 else
230 pg_fatal("invalid argument for option %s", "--set-char-signedness");
231 break;
232
233 case 7:
235 break;
236
237 default:
238 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
240 exit(1);
241 }
242 }
243
245 pg_fatal("too many command-line arguments (first is \"%s\")", argv[optind]);
246
249
252
254
255
256 if (getenv("PGOPTIONS"))
257 {
259 getenv("PGOPTIONS"));
260
261 setenv("PGOPTIONS", pgoptions, 1);
262 pfree(pgoptions);
263 }
264 else
266
267
269 "-b", _("old cluster binaries reside"), false);
271 "-B", _("new cluster binaries reside"), true);
273 "-d", _("old cluster data resides"), false);
275 "-D", _("new cluster data resides"), false);
277 "-s", _("sockets will be created"), false);
278
279#ifdef WIN32
280
281
282
283
284
285
286 {
289
292
294 pg_fatal("could not determine current directory");
297 pg_fatal("cannot run pg_upgrade from inside the new cluster data directory on Windows");
298 }
299#endif
300}
301
302
303static void
305{
306 printf(_("pg_upgrade upgrades a PostgreSQL cluster to a different major version.\n\n"));
308 printf(_(" pg_upgrade [OPTION]...\n\n"));
310 printf(_(" -b, --old-bindir=BINDIR old cluster executable directory\n"));
311 printf(_(" -B, --new-bindir=BINDIR new cluster executable directory (default\n"
312 " same directory as pg_upgrade)\n"));
313 printf(_(" -c, --check check clusters only, don't change any data\n"));
314 printf(_(" -d, --old-datadir=DATADIR old cluster data directory\n"));
315 printf(_(" -D, --new-datadir=DATADIR new cluster data directory\n"));
316 printf(_(" -j, --jobs=NUM number of simultaneous processes or threads to use\n"));
317 printf(_(" -k, --link link instead of copying files to new cluster\n"));
318 printf(_(" -N, --no-sync do not wait for changes to be written safely to disk\n"));
319 printf(_(" -o, --old-options=OPTIONS old cluster options to pass to the server\n"));
320 printf(_(" -O, --new-options=OPTIONS new cluster options to pass to the server\n"));
321 printf(_(" -p, --old-port=PORT old cluster port number (default %d)\n"), old_cluster.port);
322 printf(_(" -P, --new-port=PORT new cluster port number (default %d)\n"), new_cluster.port);
323 printf(_(" -r, --retain retain SQL and log files after success\n"));
324 printf(_(" -s, --socketdir=DIR socket directory to use (default current dir.)\n"));
325 printf(_(" -U, --username=NAME cluster superuser (default \"%s\")\n"), os_info.user);
326 printf(_(" -v, --verbose enable verbose internal logging\n"));
327 printf(_(" -V, --version display version information, then exit\n"));
328 printf(_(" --clone clone instead of copying files to new cluster\n"));
329 printf(_(" --copy copy files to new cluster (default)\n"));
330 printf(_(" --copy-file-range copy files to new cluster with copy_file_range\n"));
331 printf(_(" --no-statistics do not import statistics from old cluster\n"));
332 printf(_(" --set-char-signedness=OPTION set new cluster char signedness to \"signed\" or\n"
333 " \"unsigned\"\n"));
334 printf(_(" --swap move data directories to new cluster\n"));
335 printf(_(" --sync-method=METHOD set method for syncing files to disk\n"));
336 printf(_(" -?, --help show this help, then exit\n"));
338 "Before running pg_upgrade you must:\n"
339 " create a new database cluster (using the new version of initdb)\n"
340 " shutdown the postmaster servicing the old cluster\n"
341 " shutdown the postmaster servicing the new cluster\n"));
343 "When you run pg_upgrade, you must provide the following information:\n"
344 " the data directory for the old cluster (-d DATADIR)\n"
345 " the data directory for the new cluster (-D DATADIR)\n"
346 " the \"bin\" directory for the old version (-b BINDIR)\n"
347 " the \"bin\" directory for the new version (-B BINDIR)\n"));
349 "For example:\n"
350 " pg_upgrade -d oldCluster/data -D newCluster/data -b oldCluster/bin -B newCluster/bin\n"
351 "or\n"));
352#ifndef WIN32
353 printf(_(" $ export PGDATAOLD=oldCluster/data\n"
354 " $ export PGDATANEW=newCluster/data\n"
355 " $ export PGBINOLD=oldCluster/bin\n"
356 " $ export PGBINNEW=newCluster/bin\n"
357 " $ pg_upgrade\n"));
358#else
359 printf(_(" C:\\> set PGDATAOLD=oldCluster/data\n"
360 " C:\\> set PGDATANEW=newCluster/data\n"
361 " C:\\> set PGBINOLD=oldCluster/bin\n"
362 " C:\\> set PGBINNEW=newCluster/bin\n"
363 " C:\\> pg_upgrade\n"));
364#endif
365 printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
366 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
367}
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384static void
386 const char *cmdLineOption, const char *description,
387 bool missingOk)
388{
389 if (*dirpath == NULL || strlen(*dirpath) == 0)
390 {
391 const char *envVar;
392
393 if ((envVar = getenv(envVarName)) && strlen(envVar))
395 else if (useCwd)
396 {
398
400 pg_fatal("could not determine current directory");
402 }
403 else if (missingOk)
404 return;
405 else
406 pg_fatal("You must identify the directory where the %s.\n"
407 "Please use the %s command-line option or the %s environment variable.",
409 }
410
411
412
413
414
416}
417
418
419
420
421
422
423
424
425
426
427
428
429void
431{
435 FILE *fp,
437 int rc;
438
439
441
442
444 if ((fp = fopen(filename, "r")) == NULL)
445 return;
446 fclose(fp);
447
448
450 if ((fp = fopen(filename, "r")) != NULL)
451 {
452 fclose(fp);
453 return;
454 }
455
456
457
459 prep_status("Finding the real data directory for the source cluster");
460 else
461 prep_status("Finding the real data directory for the target cluster");
462
463
464
465
466
467
468 snprintf(cmd, sizeof(cmd), "\"%s/postgres\" -D \"%s\" -C data_directory",
470 fflush(NULL);
471
472 if ((output = popen(cmd, "r")) == NULL ||
473 fgets(cmd_output, sizeof(cmd_output), output) == NULL)
474 pg_fatal("could not get data directory using %s: %m", cmd);
475
477 if (rc != 0)
478 pg_fatal("could not get data directory using %s: %s",
480
481
483
485
487}
488
489
490
491
492
493
494
495
496
497
498void
500{
501#if !defined(WIN32)
504 else
505 {
506
507
508
509
510 unsigned short orig_port = cluster->port;
513 FILE *fp;
514 int lineno;
515
518 if ((fp = fopen(filename, "r")) == NULL)
520
521 for (lineno = 1;
523 lineno++)
524 {
525 if (fgets(line, sizeof(line), fp) == NULL)
526 pg_fatal("could not read line %d from file \"%s\": %m",
528
529
533 {
534
537 }
538 }
539 fclose(fp);
540
541
543 pg_log(PG_WARNING, "user-supplied old port number %hu corrected to %hu",
544 orig_port, cluster->port);
545 }
546#else
548#endif
549}
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)