Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * pg_restore.c
4 : : * pg_restore is an utility extracting postgres database definitions
5 : : * from a backup archive created by pg_dump using the archiver
6 : : * interface.
7 : : *
8 : : * pg_restore will read the backup archive and
9 : : * dump out a script that reproduces
10 : : * the schema of the database in terms of
11 : : * user-defined types
12 : : * user-defined functions
13 : : * tables
14 : : * indexes
15 : : * aggregates
16 : : * operators
17 : : * ACL - grant/revoke
18 : : *
19 : : * the output script is SQL that is understood by PostgreSQL
20 : : *
21 : : * Basic process in a restore operation is:
22 : : *
23 : : * Open the Archive and read the TOC.
24 : : * Set flags in TOC entries, and *maybe* reorder them.
25 : : * Generate script to stdout
26 : : * Exit
27 : : *
28 : : * Copyright (c) 2000, Philip Warner
29 : : * Rights are granted to use this software in any way so long
30 : : * as this notice is not removed.
31 : : *
32 : : * The author is not responsible for loss or damages that may
33 : : * result from its use.
34 : : *
35 : : *
36 : : * IDENTIFICATION
37 : : * src/bin/pg_dump/pg_restore.c
38 : : *
39 : : *-------------------------------------------------------------------------
40 : : */
41 : : #include "postgres_fe.h"
42 : :
43 : : #include <ctype.h>
44 : : #ifdef HAVE_TERMIOS_H
45 : : #include <termios.h>
46 : : #endif
47 : :
48 : : #include "dumputils.h"
49 : : #include "fe_utils/option_utils.h"
50 : : #include "filter.h"
51 : : #include "getopt_long.h"
52 : : #include "parallel.h"
53 : : #include "pg_backup_utils.h"
54 : :
55 : : static void usage(const char *progname);
56 : : static void read_restore_filters(const char *filename, RestoreOptions *opts);
57 : :
58 : : int
8934 bruce@momjian.us 59 :CBC 93 : main(int argc, char **argv)
60 : : {
61 : : RestoreOptions *opts;
62 : : int c;
63 : : int exit_code;
4549 andrew@dunslane.net 64 : 93 : int numWorkers = 1;
65 : : Archive *AH;
66 : : char *inputFileSpec;
198 jdavis@postgresql.or 67 : 93 : bool data_only = false;
68 : 93 : bool schema_only = false;
69 : : static int disable_triggers = 0;
70 : : static int enable_row_security = 0;
71 : : static int if_exists = 0;
72 : : static int no_data_for_failed_tables = 0;
73 : : static int outputNoTableAm = 0;
74 : : static int outputNoTablespaces = 0;
75 : : static int use_setsessauth = 0;
76 : : static int no_comments = 0;
77 : : static int no_data = 0;
78 : : static int no_policies = 0;
79 : : static int no_publications = 0;
80 : : static int no_schema = 0;
81 : : static int no_security_labels = 0;
82 : : static int no_statistics = 0;
83 : : static int no_subscriptions = 0;
84 : : static int strict_names = 0;
85 : : static int statistics_only = 0;
86 : : static int with_statistics = 0;
87 : :
8781 peter_e@gmx.net 88 : 93 : struct option cmdopts[] = {
89 : : {"clean", 0, NULL, 'c'},
90 : : {"create", 0, NULL, 'C'},
91 : : {"data-only", 0, NULL, 'a'},
92 : : {"dbname", 1, NULL, 'd'},
93 : : {"exit-on-error", 0, NULL, 'e'},
94 : : {"exclude-schema", 1, NULL, 'N'},
95 : : {"file", 1, NULL, 'f'},
96 : : {"format", 1, NULL, 'F'},
97 : : {"function", 1, NULL, 'P'},
98 : : {"host", 1, NULL, 'h'},
99 : : {"index", 1, NULL, 'I'},
100 : : {"jobs", 1, NULL, 'j'},
101 : : {"list", 0, NULL, 'l'},
102 : : {"no-privileges", 0, NULL, 'x'},
103 : : {"no-acl", 0, NULL, 'x'},
104 : : {"no-owner", 0, NULL, 'O'},
105 : : {"no-reconnect", 0, NULL, 'R'},
106 : : {"port", 1, NULL, 'p'},
107 : : {"no-password", 0, NULL, 'w'},
108 : : {"password", 0, NULL, 'W'},
109 : : {"schema", 1, NULL, 'n'},
110 : : {"schema-only", 0, NULL, 's'},
111 : : {"superuser", 1, NULL, 'S'},
112 : : {"table", 1, NULL, 't'},
113 : : {"trigger", 1, NULL, 'T'},
114 : : {"use-list", 1, NULL, 'L'},
115 : : {"username", 1, NULL, 'U'},
116 : : {"verbose", 0, NULL, 'v'},
117 : : {"single-transaction", 0, NULL, '1'},
118 : :
119 : : /*
120 : : * the following options don't have an equivalent short option letter
121 : : */
122 : : {"disable-triggers", no_argument, &disable_triggers, 1},
123 : : {"enable-row-security", no_argument, &enable_row_security, 1},
124 : : {"if-exists", no_argument, &if_exists, 1},
125 : : {"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
126 : : {"no-table-access-method", no_argument, &outputNoTableAm, 1},
127 : : {"no-tablespaces", no_argument, &outputNoTablespaces, 1},
128 : : {"role", required_argument, NULL, 2},
129 : : {"section", required_argument, NULL, 3},
130 : : {"strict-names", no_argument, &strict_names, 1},
131 : : {"transaction-size", required_argument, NULL, 5},
132 : : {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
133 : : {"no-comments", no_argument, &no_comments, 1},
134 : : {"no-data", no_argument, &no_data, 1},
135 : : {"no-policies", no_argument, &no_policies, 1},
136 : : {"no-publications", no_argument, &no_publications, 1},
137 : : {"no-schema", no_argument, &no_schema, 1},
138 : : {"no-security-labels", no_argument, &no_security_labels, 1},
139 : : {"no-subscriptions", no_argument, &no_subscriptions, 1},
140 : : {"no-statistics", no_argument, &no_statistics, 1},
141 : : {"statistics", no_argument, &with_statistics, 1},
142 : : {"statistics-only", no_argument, &statistics_only, 1},
143 : : {"filter", required_argument, NULL, 4},
144 : : {"restrict-key", required_argument, NULL, 6},
145 : :
146 : : {NULL, 0, NULL, 0}
147 : : };
148 : :
2350 peter@eisentraut.org 149 : 93 : pg_logging_init(argv[0]);
2341 150 : 93 : pg_logging_set_level(PG_LOG_WARNING);
6113 peter_e@gmx.net 151 : 93 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
152 : :
6023 andrew@dunslane.net 153 : 93 : init_parallel_dump_utils();
154 : :
9178 pjw@rhyme.com.au 155 : 93 : opts = NewRestoreOptions();
156 : :
8191 bruce@momjian.us 157 : 93 : progname = get_progname(argv[0]);
158 : :
9009 peter_e@gmx.net 159 [ + + ]: 93 : if (argc > 1)
160 : : {
8934 bruce@momjian.us 161 [ + + - + ]: 92 : if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
162 : : {
9009 peter_e@gmx.net 163 : 1 : usage(progname);
4951 rhaas@postgresql.org 164 : 1 : exit_nicely(0);
165 : : }
8934 bruce@momjian.us 166 [ + + + + ]: 91 : if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
167 : : {
9009 peter_e@gmx.net 168 : 18 : puts("pg_restore (PostgreSQL) " PG_VERSION);
4951 rhaas@postgresql.org 169 : 18 : exit_nicely(0);
170 : : }
171 : : }
172 : :
38 andrew@dunslane.net 173 : 420 : while ((c = getopt_long(argc, argv, "acCd:ef:F:h:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1",
8279 peter_e@gmx.net 174 [ + + ]: 420 : cmdopts, NULL)) != -1)
175 : : {
9178 pjw@rhyme.com.au 176 [ + + + + : 352 : switch (c)
+ + + + +
+ - - - -
+ - - - -
+ - - + +
- - - + +
- - + + -
+ ]
177 : : {
178 : 2 : case 'a': /* Dump data only */
285 nathan@postgresql.or 179 : 2 : data_only = true;
9178 pjw@rhyme.com.au 180 : 2 : break;
7266 bruce@momjian.us 181 : 17 : case 'c': /* clean (i.e., drop) schema prior to create */
9178 pjw@rhyme.com.au 182 : 17 : opts->dropSchema = 1;
183 : 17 : break;
9167 184 : 30 : case 'C':
5593 tgl@sss.pgh.pa.us 185 : 30 : opts->createDB = 1;
9167 pjw@rhyme.com.au 186 : 30 : break;
9178 187 : 33 : case 'd':
1808 tgl@sss.pgh.pa.us 188 : 33 : opts->cparams.dbname = pg_strdup(optarg);
9178 pjw@rhyme.com.au 189 : 33 : break;
7687 bruce@momjian.us 190 : 28 : case 'e':
191 : 28 : opts->exit_on_error = true;
192 : 28 : break;
9178 pjw@rhyme.com.au 193 : 33 : case 'f': /* output file name */
5034 bruce@momjian.us 194 : 33 : opts->filename = pg_strdup(optarg);
9178 pjw@rhyme.com.au 195 : 33 : break;
196 : 11 : case 'F':
8934 bruce@momjian.us 197 [ + - ]: 11 : if (strlen(optarg) != 0)
5034 198 : 11 : opts->formatName = pg_strdup(optarg);
9178 pjw@rhyme.com.au 199 : 11 : break;
200 : 28 : case 'h':
201 [ + - ]: 28 : if (strlen(optarg) != 0)
1808 tgl@sss.pgh.pa.us 202 : 28 : opts->cparams.pghost = pg_strdup(optarg);
9178 pjw@rhyme.com.au 203 : 28 : break;
204 : :
6014 peter_e@gmx.net 205 : 9 : case 'j': /* number of restore jobs */
1505 michael@paquier.xyz 206 [ + + ]: 9 : if (!option_parse_int(optarg, "-j/--jobs", 1,
207 : : PG_MAX_JOBS,
208 : : &numWorkers))
209 : 1 : exit(1);
6014 peter_e@gmx.net 210 : 8 : break;
211 : :
8950 pjw@rhyme.com.au 212 : 5 : case 'l': /* Dump the TOC summary */
213 : 5 : opts->tocSummary = 1;
214 : 5 : break;
215 : :
8950 pjw@rhyme.com.au 216 :UBC 0 : case 'L': /* input TOC summary file name */
5034 bruce@momjian.us 217 : 0 : opts->tocFile = pg_strdup(optarg);
8950 pjw@rhyme.com.au 218 : 0 : break;
219 : :
7146 bruce@momjian.us 220 : 0 : case 'n': /* Dump data for this schema only */
4392 heikki.linnakangas@i 221 : 0 : simple_string_list_append(&opts->schemaNames, optarg);
7146 bruce@momjian.us 222 : 0 : break;
223 : :
3273 peter_e@gmx.net 224 : 0 : case 'N': /* Do not dump data for this schema */
225 : 0 : simple_string_list_append(&opts->schemaExcludeNames, optarg);
226 : 0 : break;
227 : :
9178 pjw@rhyme.com.au 228 : 0 : case 'O':
9167 229 : 0 : opts->noOwner = 1;
9178 230 : 0 : break;
231 : :
9178 pjw@rhyme.com.au 232 :CBC 38 : case 'p':
233 [ + - ]: 38 : if (strlen(optarg) != 0)
1808 tgl@sss.pgh.pa.us 234 : 38 : opts->cparams.pgport = pg_strdup(optarg);
9178 pjw@rhyme.com.au 235 : 38 : break;
9167 pjw@rhyme.com.au 236 :UBC 0 : case 'R':
237 : : /* no-op, still accepted for backwards compatibility */
238 : 0 : break;
8934 bruce@momjian.us 239 : 0 : case 'P': /* Function */
9178 pjw@rhyme.com.au 240 : 0 : opts->selTypes = 1;
241 : 0 : opts->selFunction = 1;
4392 heikki.linnakangas@i 242 : 0 : simple_string_list_append(&opts->functionNames, optarg);
9178 pjw@rhyme.com.au 243 : 0 : break;
8934 bruce@momjian.us 244 : 0 : case 'I': /* Index */
9178 pjw@rhyme.com.au 245 : 0 : opts->selTypes = 1;
246 : 0 : opts->selIndex = 1;
4392 heikki.linnakangas@i 247 : 0 : simple_string_list_append(&opts->indexNames, optarg);
9178 pjw@rhyme.com.au 248 : 0 : break;
8934 bruce@momjian.us 249 : 0 : case 'T': /* Trigger */
9178 pjw@rhyme.com.au 250 : 0 : opts->selTypes = 1;
251 : 0 : opts->selTrigger = 1;
4392 heikki.linnakangas@i 252 : 0 : simple_string_list_append(&opts->triggerNames, optarg);
9178 pjw@rhyme.com.au 253 : 0 : break;
9178 pjw@rhyme.com.au 254 :CBC 1 : case 's': /* dump schema only */
285 nathan@postgresql.or 255 : 1 : schema_only = true;
9178 pjw@rhyme.com.au 256 : 1 : break;
9167 pjw@rhyme.com.au 257 :UBC 0 : case 'S': /* Superuser username */
258 [ # # ]: 0 : if (strlen(optarg) != 0)
5034 bruce@momjian.us 259 : 0 : opts->superuser = pg_strdup(optarg);
9167 pjw@rhyme.com.au 260 : 0 : break;
3719 tgl@sss.pgh.pa.us 261 : 0 : case 't': /* Dump specified table(s) only */
9178 pjw@rhyme.com.au 262 : 0 : opts->selTypes = 1;
263 : 0 : opts->selTable = 1;
4615 magnus@hagander.net 264 : 0 : simple_string_list_append(&opts->tableNames, optarg);
9178 pjw@rhyme.com.au 265 : 0 : break;
266 : :
8878 peter_e@gmx.net 267 :CBC 30 : case 'U':
1808 tgl@sss.pgh.pa.us 268 : 30 : opts->cparams.username = pg_strdup(optarg);
9178 pjw@rhyme.com.au 269 : 30 : break;
270 : :
271 : 33 : case 'v': /* verbose */
272 : 33 : opts->verbose = 1;
1815 tgl@sss.pgh.pa.us 273 : 33 : pg_logging_increase_verbosity();
9178 pjw@rhyme.com.au 274 : 33 : break;
275 : :
6036 peter_e@gmx.net 276 :UBC 0 : case 'w':
1808 tgl@sss.pgh.pa.us 277 : 0 : opts->cparams.promptPassword = TRI_NO;
6036 peter_e@gmx.net 278 : 0 : break;
279 : :
8878 280 : 0 : case 'W':
1808 tgl@sss.pgh.pa.us 281 : 0 : opts->cparams.promptPassword = TRI_YES;
8878 peter_e@gmx.net 282 : 0 : break;
283 : :
9178 pjw@rhyme.com.au 284 : 0 : case 'x': /* skip ACL dump */
285 : 0 : opts->aclsSkip = 1;
286 : 0 : break;
287 : :
6088 tgl@sss.pgh.pa.us 288 :CBC 2 : case '1': /* Restore data in a single transaction */
289 : 2 : opts->single_txn = true;
290 : 2 : opts->exit_on_error = true;
291 : 2 : break;
292 : :
8781 peter_e@gmx.net 293 : 13 : case 0:
294 : :
295 : : /*
296 : : * This covers the long options without a short equivalent.
297 : : */
298 : 13 : break;
299 : :
6088 tgl@sss.pgh.pa.us 300 :UBC 0 : case 2: /* SET ROLE */
4712 bruce@momjian.us 301 : 0 : opts->use_role = pg_strdup(optarg);
7146 302 : 0 : break;
303 : :
5013 andrew@dunslane.net 304 : 0 : case 3: /* section */
4848 tgl@sss.pgh.pa.us 305 : 0 : set_dump_section(optarg, &(opts->dumpSections));
5013 andrew@dunslane.net 306 : 0 : break;
307 : :
523 tgl@sss.pgh.pa.us 308 :CBC 10 : case 4: /* filter */
647 dgustafsson@postgres 309 : 10 : read_restore_filters(optarg, opts);
310 : 6 : break;
311 : :
523 tgl@sss.pgh.pa.us 312 : 28 : case 5: /* transaction-size */
313 [ - + ]: 28 : if (!option_parse_int(optarg, "--transaction-size",
314 : : 1, INT_MAX,
315 : : &opts->txn_size))
523 tgl@sss.pgh.pa.us 316 :UBC 0 : exit(1);
523 tgl@sss.pgh.pa.us 317 :CBC 28 : opts->exit_on_error = true;
318 : 28 : break;
319 : :
26 nathan@postgresql.or 320 :UBC 0 : case 6:
321 : 0 : opts->restrict_key = pg_strdup(optarg);
322 : 0 : break;
323 : :
9178 pjw@rhyme.com.au 324 :CBC 1 : default:
325 : : /* getopt_long already emitted a complaint */
1247 tgl@sss.pgh.pa.us 326 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
4951 rhaas@postgresql.org 327 : 1 : exit_nicely(1);
328 : : }
329 : : }
330 : :
331 : : /* Get file name from command line */
8934 bruce@momjian.us 332 [ + + ]: 68 : if (optind < argc)
5503 tgl@sss.pgh.pa.us 333 : 60 : inputFileSpec = argv[optind++];
334 : : else
7992 335 : 8 : inputFileSpec = NULL;
336 : :
337 : : /* Complain if any arguments remain */
5503 338 [ + + ]: 68 : if (optind < argc)
339 : : {
2350 peter@eisentraut.org 340 : 1 : pg_log_error("too many command-line arguments (first is \"%s\")",
341 : : argv[optind]);
1247 tgl@sss.pgh.pa.us 342 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
4951 rhaas@postgresql.org 343 : 1 : exit_nicely(1);
344 : : }
345 : :
346 : : /* Complain if neither -f nor -d was specified (except if dumping TOC) */
1808 tgl@sss.pgh.pa.us 347 [ + + + + : 67 : if (!opts->cparams.dbname && !opts->filename && !opts->tocSummary)
+ + ]
1247 348 : 1 : pg_fatal("one of -d/--dbname and -f/--file must be specified");
349 : :
350 : : /* Should get at most one of -d and -f, else user is confused */
1808 351 [ + + ]: 66 : if (opts->cparams.dbname)
352 : : {
7992 353 [ + + ]: 33 : if (opts->filename)
354 : : {
2350 peter@eisentraut.org 355 : 1 : pg_log_error("options -d/--dbname and -f/--file cannot be used together");
1247 tgl@sss.pgh.pa.us 356 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
4951 rhaas@postgresql.org 357 : 1 : exit_nicely(1);
358 : : }
359 : :
26 nathan@postgresql.or 360 [ - + ]: 32 : if (opts->restrict_key)
26 nathan@postgresql.or 361 :UBC 0 : pg_fatal("options -d/--dbname and --restrict-key cannot be used together");
362 : :
7992 tgl@sss.pgh.pa.us 363 :CBC 32 : opts->useDB = 1;
364 : : }
365 : : else
366 : : {
367 : : /*
368 : : * If you don't provide a restrict key, one will be appointed for you.
369 : : */
26 nathan@postgresql.or 370 [ + - ]: 33 : if (!opts->restrict_key)
371 : 33 : opts->restrict_key = generate_restrict_key();
372 [ - + ]: 33 : if (!opts->restrict_key)
26 nathan@postgresql.or 373 :UBC 0 : pg_fatal("could not generate restrict key");
26 nathan@postgresql.or 374 [ - + ]:CBC 33 : if (!valid_restrict_key(opts->restrict_key))
26 nathan@postgresql.or 375 :UBC 0 : pg_fatal("invalid restrict key");
376 : : }
377 : :
378 : : /* reject conflicting "-only" options */
285 nathan@postgresql.or 379 [ + + + + ]:CBC 65 : if (data_only && schema_only)
1247 tgl@sss.pgh.pa.us 380 : 1 : pg_fatal("options -s/--schema-only and -a/--data-only cannot be used together");
198 jdavis@postgresql.or 381 [ - + - - ]: 64 : if (schema_only && statistics_only)
198 jdavis@postgresql.or 382 :UBC 0 : pg_fatal("options -s/--schema-only and --statistics-only cannot be used together");
165 jdavis@postgresql.or 383 [ + + - + ]:CBC 64 : if (data_only && statistics_only)
165 jdavis@postgresql.or 384 :UBC 0 : pg_fatal("options -a/--data-only and --statistics-only cannot be used together");
385 : :
386 : : /* reject conflicting "-only" and "no-" options */
165 jdavis@postgresql.or 387 [ + + - + ]:CBC 64 : if (data_only && no_data)
165 jdavis@postgresql.or 388 :UBC 0 : pg_fatal("options -a/--data-only and --no-data cannot be used together");
165 jdavis@postgresql.or 389 [ - + - - ]:CBC 64 : if (schema_only && no_schema)
165 jdavis@postgresql.or 390 :UBC 0 : pg_fatal("options -s/--schema-only and --no-schema cannot be used together");
165 jdavis@postgresql.or 391 [ - + - - ]:CBC 64 : if (statistics_only && no_statistics)
165 jdavis@postgresql.or 392 :UBC 0 : pg_fatal("options --statistics-only and --no-statistics cannot be used together");
393 : :
394 : : /* reject conflicting "no-" options */
165 jdavis@postgresql.or 395 [ + + - + ]:CBC 64 : if (with_statistics && no_statistics)
35 jdavis@postgresql.or 396 :UBC 0 : pg_fatal("options --statistics and --no-statistics cannot be used together");
397 : :
398 : : /* reject conflicting "only-" options */
35 jdavis@postgresql.or 399 [ + + - + ]:CBC 64 : if (data_only && with_statistics)
36 jdavis@postgresql.or 400 :UBC 0 : pg_fatal("options %s and %s cannot be used together",
401 : : "-a/--data-only", "--statistics");
35 jdavis@postgresql.or 402 [ - + - - ]:CBC 64 : if (schema_only && with_statistics)
36 jdavis@postgresql.or 403 :UBC 0 : pg_fatal("options %s and %s cannot be used together",
404 : : "-s/--schema-only", "--statistics");
405 : :
285 nathan@postgresql.or 406 [ + + + - ]:CBC 64 : if (data_only && opts->dropSchema)
1247 tgl@sss.pgh.pa.us 407 : 1 : pg_fatal("options -c/--clean and -a/--data-only cannot be used together");
408 : :
523 409 [ + + - + ]: 63 : if (opts->single_txn && opts->txn_size > 0)
523 tgl@sss.pgh.pa.us 410 :UBC 0 : pg_fatal("options -1/--single-transaction and --transaction-size cannot be used together");
411 : :
412 : : /*
413 : : * -C is not compatible with -1, because we can't create a database inside
414 : : * a transaction block.
415 : : */
2503 michael@paquier.xyz 416 [ + + + + ]:CBC 63 : if (opts->createDB && opts->single_txn)
1247 tgl@sss.pgh.pa.us 417 : 1 : pg_fatal("options -C/--create and -1/--single-transaction cannot be used together");
418 : :
419 : : /* Can't do single-txn mode with multiple connections */
4549 andrew@dunslane.net 420 [ + + + - ]: 62 : if (opts->single_txn && numWorkers > 1)
1247 tgl@sss.pgh.pa.us 421 : 1 : pg_fatal("cannot specify both --single-transaction and multiple jobs");
422 : :
423 : : /*
424 : : * Set derivative flags. Ambiguous or nonsensical combinations, e.g.
425 : : * "--schema-only --no-schema", will have already caused an error in one
426 : : * of the checks above.
427 : : */
165 jdavis@postgresql.or 428 [ + - - + : 61 : opts->dumpData = ((opts->dumpData && !schema_only && !statistics_only) ||
- - ]
35 429 [ + - + - ]: 122 : data_only) && !no_data;
165 430 [ + - - + : 61 : opts->dumpSchema = ((opts->dumpSchema && !data_only && !statistics_only) ||
- - ]
35 431 [ + - + - ]: 122 : schema_only) && !no_schema;
165 432 [ + - - + ]: 61 : opts->dumpStatistics = ((opts->dumpStatistics && !schema_only && !data_only) ||
433 [ + - - - : 122 : (statistics_only || with_statistics)) && !no_statistics;
- - + - ]
434 : :
8520 tgl@sss.pgh.pa.us 435 : 61 : opts->disable_triggers = disable_triggers;
4005 sfrost@snowman.net 436 : 61 : opts->enable_row_security = enable_row_security;
6909 peter_e@gmx.net 437 : 61 : opts->noDataForFailedTables = no_data_for_failed_tables;
1328 michael@paquier.xyz 438 : 61 : opts->noTableAm = outputNoTableAm;
6379 tgl@sss.pgh.pa.us 439 : 61 : opts->noTablespace = outputNoTablespaces;
440 : 61 : opts->use_setsessauth = use_setsessauth;
2781 441 : 61 : opts->no_comments = no_comments;
174 442 : 61 : opts->no_policies = no_policies;
3039 peter_e@gmx.net 443 : 61 : opts->no_publications = no_publications;
5224 444 : 61 : opts->no_security_labels = no_security_labels;
3042 445 : 61 : opts->no_subscriptions = no_subscriptions;
446 : :
4205 alvherre@alvh.no-ip. 447 [ + + + - ]: 61 : if (if_exists && !opts->dropSchema)
1247 tgl@sss.pgh.pa.us 448 : 1 : pg_fatal("option --if-exists requires option -c/--clean");
4205 alvherre@alvh.no-ip. 449 : 60 : opts->if_exists = if_exists;
3645 teodor@sigaev.ru 450 : 60 : opts->strict_names = strict_names;
451 : :
8934 bruce@momjian.us 452 [ + + ]: 60 : if (opts->formatName)
453 : : {
224 tgl@sss.pgh.pa.us 454 [ + - + + ]: 22 : if (pg_strcasecmp(opts->formatName, "c") == 0 ||
455 : 11 : pg_strcasecmp(opts->formatName, "custom") == 0)
456 : 8 : opts->format = archCustom;
457 [ + - + + ]: 6 : else if (pg_strcasecmp(opts->formatName, "d") == 0 ||
458 : 3 : pg_strcasecmp(opts->formatName, "directory") == 0)
459 : 1 : opts->format = archDirectory;
460 [ + - + + ]: 4 : else if (pg_strcasecmp(opts->formatName, "t") == 0 ||
461 : 2 : pg_strcasecmp(opts->formatName, "tar") == 0)
462 : 1 : opts->format = archTar;
463 [ + - - + ]: 2 : else if (pg_strcasecmp(opts->formatName, "p") == 0 ||
464 : 1 : pg_strcasecmp(opts->formatName, "plain") == 0)
465 : : {
466 : : /* recognize this for consistency with pg_dump */
224 tgl@sss.pgh.pa.us 467 :UBC 0 : pg_fatal("archive format \"%s\" is not supported; please use psql",
468 : : opts->formatName);
469 : : }
470 : : else
224 tgl@sss.pgh.pa.us 471 :CBC 1 : pg_fatal("unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"",
472 : : opts->formatName);
473 : : }
474 : :
7992 475 : 59 : AH = OpenArchive(inputFileSpec, opts->format);
476 : :
3524 477 : 59 : SetArchiveOptions(AH, NULL, opts);
478 : :
479 : : /*
480 : : * We don't have a connection yet but that doesn't matter. The connection
481 : : * is initialized to NULL and if we terminate through exit_nicely() while
482 : : * it's still NULL, the cleanup function will just be a no-op.
483 : : */
38 andrew@dunslane.net 484 : 59 : on_exit_close_archive(AH);
485 : :
486 : : /* Let the archiver know how noisy to be */
9178 pjw@rhyme.com.au 487 : 59 : AH->verbose = opts->verbose;
488 : :
489 : : /*
490 : : * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
491 : : */
7687 bruce@momjian.us 492 : 59 : AH->exit_on_error = opts->exit_on_error;
493 : :
8934 494 [ - + ]: 59 : if (opts->tocFile)
3524 tgl@sss.pgh.pa.us 495 :UBC 0 : SortTocFromFile(AH);
496 : :
4549 andrew@dunslane.net 497 :CBC 59 : AH->numWorkers = numWorkers;
498 : :
8934 bruce@momjian.us 499 [ + + ]: 59 : if (opts->tocSummary)
3524 tgl@sss.pgh.pa.us 500 : 5 : PrintTOCSummary(AH);
501 : : else
502 : : {
503 : 54 : ProcessArchiveRestoreOptions(AH);
38 andrew@dunslane.net 504 : 54 : RestoreArchive(AH);
505 : : }
506 : :
507 : : /* done, print a summary of ignored errors */
508 [ - + ]: 59 : if (AH->n_errors)
38 andrew@dunslane.net 509 :UBC 0 : pg_log_warning("errors ignored on restore: %d", AH->n_errors);
510 : :
511 : : /* AH may be freed in CloseArchive? */
38 andrew@dunslane.net 512 :CBC 59 : exit_code = AH->n_errors ? 1 : 0;
513 : :
3524 tgl@sss.pgh.pa.us 514 : 59 : CloseArchive(AH);
515 : :
38 andrew@dunslane.net 516 : 59 : return exit_code;
517 : : }
518 : :
519 : : static void
8934 bruce@momjian.us 520 : 1 : usage(const char *progname)
521 : : {
38 andrew@dunslane.net 522 : 1 : printf(_("%s restores a PostgreSQL database from an archive created by pg_dump.\n\n"), progname);
8410 peter_e@gmx.net 523 : 1 : printf(_("Usage:\n"));
8359 524 : 1 : printf(_(" %s [OPTION]... [FILE]\n"), progname);
525 : :
526 : 1 : printf(_("\nGeneral options:\n"));
7604 bruce@momjian.us 527 : 1 : printf(_(" -d, --dbname=NAME connect to database name\n"));
2347 alvherre@alvh.no-ip. 528 : 1 : printf(_(" -f, --file=FILENAME output file name (- for stdout)\n"));
5340 heikki.linnakangas@i 529 : 1 : printf(_(" -F, --format=c|d|t backup file format (should be automatic)\n"));
8403 bruce@momjian.us 530 : 1 : printf(_(" -l, --list print summarized TOC of the archive\n"));
8359 peter_e@gmx.net 531 : 1 : printf(_(" -v, --verbose verbose mode\n"));
4828 532 : 1 : printf(_(" -V, --version output version information, then exit\n"));
533 : 1 : printf(_(" -?, --help show this help, then exit\n"));
534 : :
7633 bruce@momjian.us 535 : 1 : printf(_("\nOptions controlling the restore:\n"));
4859 peter_e@gmx.net 536 : 1 : printf(_(" -a, --data-only restore only the data, no schema\n"));
537 : 1 : printf(_(" -c, --clean clean (drop) database objects before recreating\n"));
538 : 1 : printf(_(" -C, --create create the target database\n"));
539 : 1 : printf(_(" -e, --exit-on-error exit on error, default is to continue\n"));
4032 540 : 1 : printf(_(" -I, --index=NAME restore named index\n"));
4859 541 : 1 : printf(_(" -j, --jobs=NUM use this many parallel jobs to restore\n"));
542 : 1 : printf(_(" -L, --use-list=FILENAME use table of contents from this file for\n"
543 : : " selecting/ordering output\n"));
4032 544 : 1 : printf(_(" -n, --schema=NAME restore only objects in this schema\n"));
3273 545 : 1 : printf(_(" -N, --exclude-schema=NAME do not restore objects in this schema\n"));
4859 546 : 1 : printf(_(" -O, --no-owner skip restoration of object ownership\n"));
4032 547 : 1 : printf(_(" -P, --function=NAME(args) restore named function\n"));
4859 548 : 1 : printf(_(" -s, --schema-only restore only the schema, no data\n"));
549 : 1 : printf(_(" -S, --superuser=NAME superuser user name to use for disabling triggers\n"));
3421 550 : 1 : printf(_(" -t, --table=NAME restore named relation (table, view, etc.)\n"));
4032 551 : 1 : printf(_(" -T, --trigger=NAME restore named trigger\n"));
4859 552 : 1 : printf(_(" -x, --no-privileges skip restoration of access privileges (grant/revoke)\n"));
553 : 1 : printf(_(" -1, --single-transaction restore as a single transaction\n"));
554 : 1 : printf(_(" --disable-triggers disable triggers during data-only restore\n"));
3643 555 : 1 : printf(_(" --enable-row-security enable row security\n"));
647 dgustafsson@postgres 556 : 1 : printf(_(" --filter=FILENAME restore or skip objects based on expressions\n"
557 : : " in FILENAME\n"));
4205 alvherre@alvh.no-ip. 558 : 1 : printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
290 bruce@momjian.us 559 : 1 : printf(_(" --no-comments do not restore comment commands\n"));
198 jdavis@postgresql.or 560 : 1 : printf(_(" --no-data do not restore data\n"));
4859 peter_e@gmx.net 561 : 1 : printf(_(" --no-data-for-failed-tables do not restore data of tables that could not be\n"
562 : : " created\n"));
173 tgl@sss.pgh.pa.us 563 : 1 : printf(_(" --no-policies do not restore row security policies\n"));
3039 peter_e@gmx.net 564 : 1 : printf(_(" --no-publications do not restore publications\n"));
198 jdavis@postgresql.or 565 : 1 : printf(_(" --no-schema do not restore schema\n"));
4859 peter_e@gmx.net 566 : 1 : printf(_(" --no-security-labels do not restore security labels\n"));
198 jdavis@postgresql.or 567 : 1 : printf(_(" --no-statistics do not restore statistics\n"));
3042 peter_e@gmx.net 568 : 1 : printf(_(" --no-subscriptions do not restore subscriptions\n"));
1328 michael@paquier.xyz 569 : 1 : printf(_(" --no-table-access-method do not restore table access methods\n"));
4859 peter_e@gmx.net 570 : 1 : printf(_(" --no-tablespaces do not restore tablespace assignments\n"));
26 nathan@postgresql.or 571 : 1 : printf(_(" --restrict-key=RESTRICT_KEY use provided string as psql \\restrict key\n"));
4032 peter_e@gmx.net 572 : 1 : printf(_(" --section=SECTION restore named section (pre-data, data, or post-data)\n"));
35 jdavis@postgresql.or 573 : 1 : printf(_(" --statistics restore the statistics\n"));
198 574 : 1 : printf(_(" --statistics-only restore only the statistics, not schema or data\n"));
3645 teodor@sigaev.ru 575 : 1 : printf(_(" --strict-names require table and/or schema include patterns to\n"
576 : : " match at least one entity each\n"));
523 tgl@sss.pgh.pa.us 577 : 1 : printf(_(" --transaction-size=N commit after every N objects\n"));
6379 578 : 1 : printf(_(" --use-set-session-authorization\n"
579 : : " use SET SESSION AUTHORIZATION commands instead of\n"
580 : : " ALTER OWNER commands to set ownership\n"));
581 : :
8359 peter_e@gmx.net 582 : 1 : printf(_("\nConnection options:\n"));
8123 bruce@momjian.us 583 : 1 : printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
8359 peter_e@gmx.net 584 : 1 : printf(_(" -p, --port=PORT database server port number\n"));
585 : 1 : printf(_(" -U, --username=NAME connect as specified database user\n"));
6036 586 : 1 : printf(_(" -w, --no-password never prompt for password\n"));
8359 587 : 1 : printf(_(" -W, --password force password prompt (should happen automatically)\n"));
5218 588 : 1 : printf(_(" --role=ROLENAME do SET ROLE before restore\n"));
589 : :
4032 590 : 1 : printf(_("\n"
591 : : "The options -I, -n, -N, -P, -t, -T, and --section can be combined and specified\n"
592 : : "multiple times to select multiple objects.\n"));
8410 593 : 1 : printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
2017 peter@eisentraut.org 594 : 1 : printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
595 : 1 : printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
9178 pjw@rhyme.com.au 596 : 1 : }
597 : :
598 : : /*
599 : : * read_restore_filters - retrieve object identifier patterns from file
600 : : *
601 : : * Parse the specified filter file for include and exclude patterns, and add
602 : : * them to the relevant lists. If the filename is "-" then filters will be
603 : : * read from STDIN rather than a file.
604 : : */
605 : : static void
647 dgustafsson@postgres 606 : 10 : read_restore_filters(const char *filename, RestoreOptions *opts)
607 : : {
608 : : FilterStateData fstate;
609 : : char *objname;
610 : : FilterCommandType comtype;
611 : : FilterObjectType objtype;
612 : :
613 : 10 : filter_init(&fstate, filename, exit_nicely);
614 : :
615 [ + + ]: 27 : while (filter_read_item(&fstate, &objname, &comtype, &objtype))
616 : : {
617 [ + + ]: 11 : if (comtype == FILTER_COMMAND_TYPE_INCLUDE)
618 : : {
619 [ - + + + : 8 : switch (objtype)
+ + + - ]
620 : : {
647 dgustafsson@postgres 621 :UBC 0 : case FILTER_OBJECT_TYPE_NONE:
622 : 0 : break;
647 dgustafsson@postgres 623 :CBC 2 : case FILTER_OBJECT_TYPE_TABLE_DATA:
624 : : case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN:
625 : : case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN:
626 : : case FILTER_OBJECT_TYPE_DATABASE:
627 : : case FILTER_OBJECT_TYPE_EXTENSION:
628 : : case FILTER_OBJECT_TYPE_FOREIGN_DATA:
646 629 : 2 : pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
630 : : "include",
631 : : filter_object_type_name(objtype));
647 632 : 2 : exit_nicely(1);
633 : :
634 : 2 : case FILTER_OBJECT_TYPE_FUNCTION:
635 : 2 : opts->selTypes = 1;
636 : 2 : opts->selFunction = 1;
637 : 2 : simple_string_list_append(&opts->functionNames, objname);
638 : 2 : break;
639 : 1 : case FILTER_OBJECT_TYPE_INDEX:
640 : 1 : opts->selTypes = 1;
641 : 1 : opts->selIndex = 1;
642 : 1 : simple_string_list_append(&opts->indexNames, objname);
643 : 1 : break;
644 : 1 : case FILTER_OBJECT_TYPE_SCHEMA:
645 : 1 : simple_string_list_append(&opts->schemaNames, objname);
646 : 1 : break;
647 : 1 : case FILTER_OBJECT_TYPE_TABLE:
648 : 1 : opts->selTypes = 1;
649 : 1 : opts->selTable = 1;
650 : 1 : simple_string_list_append(&opts->tableNames, objname);
651 : 1 : break;
652 : 1 : case FILTER_OBJECT_TYPE_TRIGGER:
653 : 1 : opts->selTypes = 1;
654 : 1 : opts->selTrigger = 1;
655 : 1 : simple_string_list_append(&opts->triggerNames, objname);
656 : 1 : break;
657 : : }
658 : : }
659 [ + - ]: 3 : else if (comtype == FILTER_COMMAND_TYPE_EXCLUDE)
660 : : {
661 [ - + + - ]: 3 : switch (objtype)
662 : : {
647 dgustafsson@postgres 663 :UBC 0 : case FILTER_OBJECT_TYPE_NONE:
664 : 0 : break;
647 dgustafsson@postgres 665 :CBC 2 : case FILTER_OBJECT_TYPE_TABLE_DATA:
666 : : case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN:
667 : : case FILTER_OBJECT_TYPE_DATABASE:
668 : : case FILTER_OBJECT_TYPE_EXTENSION:
669 : : case FILTER_OBJECT_TYPE_FOREIGN_DATA:
670 : : case FILTER_OBJECT_TYPE_FUNCTION:
671 : : case FILTER_OBJECT_TYPE_INDEX:
672 : : case FILTER_OBJECT_TYPE_TABLE:
673 : : case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN:
674 : : case FILTER_OBJECT_TYPE_TRIGGER:
646 675 : 2 : pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
676 : : "exclude",
677 : : filter_object_type_name(objtype));
647 678 : 2 : exit_nicely(1);
679 : :
680 : 1 : case FILTER_OBJECT_TYPE_SCHEMA:
681 : 1 : simple_string_list_append(&opts->schemaExcludeNames, objname);
682 : 1 : break;
683 : : }
684 : : }
685 : : else
686 : : {
647 dgustafsson@postgres 687 [ # # ]:UBC 0 : Assert(comtype == FILTER_COMMAND_TYPE_NONE);
688 [ # # ]: 0 : Assert(objtype == FILTER_OBJECT_TYPE_NONE);
689 : : }
690 : :
647 dgustafsson@postgres 691 [ - + ]:CBC 7 : if (objname)
692 : 7 : free(objname);
693 : : }
694 : :
695 : 6 : filter_free(&fstate);
696 : 6 : }
|