PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pg_restore.c
Go to the documentation of this file.
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/pg_dumpall 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#include <sys/stat.h>
45#ifdef HAVE_TERMIOS_H
46#include <termios.h>
47#endif
48
49#include "common/string.h"
50#include "connectdb.h"
53#include "filter.h"
54#include "getopt_long.h"
55#include "parallel.h"
56#include "pg_backup_utils.h"
57
58static void usage(const char *progname);
59static void read_restore_filters(const char *filename, RestoreOptions *opts);
60static bool file_exists_in_directory(const char *dir, const char *filename);
61static int restore_one_database(const char *inputFileSpec, RestoreOptions *opts,
62 int numWorkers, bool append_data, int num);
63static int read_one_statement(StringInfo inBuf, FILE *pfile);
64static int restore_all_databases(PGconn *conn, const char *dumpdirpath,
65 SimpleStringList db_exclude_patterns, RestoreOptions *opts, int numWorkers);
66static int process_global_sql_commands(PGconn *conn, const char *dumpdirpath,
67 const char *outfile);
68static void copy_or_print_global_file(const char *outfile, FILE *pfile);
70 SimpleOidStringList *dbname_oid_list,
71 SimpleStringList db_exclude_patterns);
72static int get_dbname_oid_list_from_mfile(const char *dumpdirpath,
73 SimpleOidStringList *dbname_oid_list);
74
75int
76main(int argc, char **argv)
77{
79 int c;
80 int numWorkers = 1;
81 char *inputFileSpec;
82 bool data_only = false;
83 bool schema_only = false;
84 int n_errors = 0;
85 bool globals_only = false;
86 SimpleStringList db_exclude_patterns = {NULL, NULL};
87 static int disable_triggers = 0;
88 static int enable_row_security = 0;
89 static int if_exists = 0;
90 static int no_data_for_failed_tables = 0;
91 static int outputNoTableAm = 0;
92 static int outputNoTablespaces = 0;
93 static int use_setsessauth = 0;
94 static int no_comments = 0;
95 static int no_data = 0;
96 static int no_policies = 0;
97 static int no_publications = 0;
98 static int no_schema = 0;
99 static int no_security_labels = 0;
100 static int no_statistics = 0;
101 static int no_subscriptions = 0;
102 static int strict_names = 0;
103 static int statistics_only = 0;
104 static int with_data = 0;
105 static int with_schema = 0;
106 static int with_statistics = 0;
107
108 struct option cmdopts[] = {
109 {"clean", 0, NULL, 'c'},
110 {"create", 0, NULL, 'C'},
111 {"data-only", 0, NULL, 'a'},
112 {"globals-only", 0, NULL, 'g'},
113 {"dbname", 1, NULL, 'd'},
114 {"exit-on-error", 0, NULL, 'e'},
115 {"exclude-schema", 1, NULL, 'N'},
116 {"file", 1, NULL, 'f'},
117 {"format", 1, NULL, 'F'},
118 {"function", 1, NULL, 'P'},
119 {"host", 1, NULL, 'h'},
120 {"index", 1, NULL, 'I'},
121 {"jobs", 1, NULL, 'j'},
122 {"list", 0, NULL, 'l'},
123 {"no-privileges", 0, NULL, 'x'},
124 {"no-acl", 0, NULL, 'x'},
125 {"no-owner", 0, NULL, 'O'},
126 {"no-reconnect", 0, NULL, 'R'},
127 {"port", 1, NULL, 'p'},
128 {"no-password", 0, NULL, 'w'},
129 {"password", 0, NULL, 'W'},
130 {"schema", 1, NULL, 'n'},
131 {"schema-only", 0, NULL, 's'},
132 {"superuser", 1, NULL, 'S'},
133 {"table", 1, NULL, 't'},
134 {"trigger", 1, NULL, 'T'},
135 {"use-list", 1, NULL, 'L'},
136 {"username", 1, NULL, 'U'},
137 {"verbose", 0, NULL, 'v'},
138 {"single-transaction", 0, NULL, '1'},
139
140 /*
141 * the following options don't have an equivalent short option letter
142 */
143 {"disable-triggers", no_argument, &disable_triggers, 1},
144 {"enable-row-security", no_argument, &enable_row_security, 1},
145 {"if-exists", no_argument, &if_exists, 1},
146 {"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
147 {"no-table-access-method", no_argument, &outputNoTableAm, 1},
148 {"no-tablespaces", no_argument, &outputNoTablespaces, 1},
149 {"role", required_argument, NULL, 2},
150 {"section", required_argument, NULL, 3},
151 {"strict-names", no_argument, &strict_names, 1},
152 {"transaction-size", required_argument, NULL, 5},
153 {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
154 {"no-comments", no_argument, &no_comments, 1},
155 {"no-data", no_argument, &no_data, 1},
156 {"no-policies", no_argument, &no_policies, 1},
157 {"no-publications", no_argument, &no_publications, 1},
158 {"no-schema", no_argument, &no_schema, 1},
159 {"no-security-labels", no_argument, &no_security_labels, 1},
160 {"no-subscriptions", no_argument, &no_subscriptions, 1},
161 {"no-statistics", no_argument, &no_statistics, 1},
162 {"with-data", no_argument, &with_data, 1},
163 {"with-schema", no_argument, &with_schema, 1},
164 {"with-statistics", no_argument, &with_statistics, 1},
165 {"statistics-only", no_argument, &statistics_only, 1},
166 {"filter", required_argument, NULL, 4},
167 {"exclude-database", required_argument, NULL, 6},
168
169 {NULL, 0, NULL, 0}
170 };
171
172 pg_logging_init(argv[0]);
174 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
175
177
179
180 progname = get_progname(argv[0]);
181
182 if (argc > 1)
183 {
184 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
185 {
187 exit_nicely(0);
188 }
189 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
190 {
191 puts("pg_restore (PostgreSQL) " PG_VERSION);
192 exit_nicely(0);
193 }
194 }
195
196 while ((c = getopt_long(argc, argv, "acCd:ef:F:gh:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1",
197 cmdopts, NULL)) != -1)
198 {
199 switch (c)
200 {
201 case 'a': /* Dump data only */
202 data_only = true;
203 break;
204 case 'c': /* clean (i.e., drop) schema prior to create */
205 opts->dropSchema = 1;
206 break;
207 case 'C':
208 opts->createDB = 1;
209 break;
210 case 'd':
211 opts->cparams.dbname = pg_strdup(optarg);
212 break;
213 case 'e':
214 opts->exit_on_error = true;
215 break;
216 case 'f': /* output file name */
217 opts->filename = pg_strdup(optarg);
218 break;
219 case 'F':
220 if (strlen(optarg) != 0)
221 opts->formatName = pg_strdup(optarg);
222 break;
223 case 'g':
224 /* restore only global.dat file from directory */
225 globals_only = true;
226 break;
227 case 'h':
228 if (strlen(optarg) != 0)
229 opts->cparams.pghost = pg_strdup(optarg);
230 break;
231 case 'j': /* number of restore jobs */
232 if (!option_parse_int(optarg, "-j/--jobs", 1,
234 &numWorkers))
235 exit(1);
236 break;
237
238 case 'l': /* Dump the TOC summary */
239 opts->tocSummary = 1;
240 break;
241
242 case 'L': /* input TOC summary file name */
243 opts->tocFile = pg_strdup(optarg);
244 break;
245
246 case 'n': /* Dump data for this schema only */
247 simple_string_list_append(&opts->schemaNames, optarg);
248 break;
249
250 case 'N': /* Do not dump data for this schema */
251 simple_string_list_append(&opts->schemaExcludeNames, optarg);
252 break;
253
254 case 'O':
255 opts->noOwner = 1;
256 break;
257
258 case 'p':
259 if (strlen(optarg) != 0)
260 opts->cparams.pgport = pg_strdup(optarg);
261 break;
262 case 'R':
263 /* no-op, still accepted for backwards compatibility */
264 break;
265 case 'P': /* Function */
266 opts->selTypes = 1;
267 opts->selFunction = 1;
268 simple_string_list_append(&opts->functionNames, optarg);
269 break;
270 case 'I': /* Index */
271 opts->selTypes = 1;
272 opts->selIndex = 1;
274 break;
275 case 'T': /* Trigger */
276 opts->selTypes = 1;
277 opts->selTrigger = 1;
278 simple_string_list_append(&opts->triggerNames, optarg);
279 break;
280 case 's': /* dump schema only */
281 schema_only = true;
282 break;
283 case 'S': /* Superuser username */
284 if (strlen(optarg) != 0)
285 opts->superuser = pg_strdup(optarg);
286 break;
287 case 't': /* Dump specified table(s) only */
288 opts->selTypes = 1;
289 opts->selTable = 1;
291 break;
292
293 case 'U':
294 opts->cparams.username = pg_strdup(optarg);
295 break;
296
297 case 'v': /* verbose */
298 opts->verbose = 1;
300 break;
301
302 case 'w':
303 opts->cparams.promptPassword = TRI_NO;
304 break;
305
306 case 'W':
307 opts->cparams.promptPassword = TRI_YES;
308 break;
309
310 case 'x': /* skip ACL dump */
311 opts->aclsSkip = 1;
312 break;
313
314 case '1': /* Restore data in a single transaction */
315 opts->single_txn = true;
316 opts->exit_on_error = true;
317 break;
318
319 case 0:
320
321 /*
322 * This covers the long options without a short equivalent.
323 */
324 break;
325
326 case 2: /* SET ROLE */
327 opts->use_role = pg_strdup(optarg);
328 break;
329
330 case 3: /* section */
331 set_dump_section(optarg, &(opts->dumpSections));
332 break;
333
334 case 4: /* filter */
336 break;
337
338 case 5: /* transaction-size */
339 if (!option_parse_int(optarg, "--transaction-size",
340 1, INT_MAX,
341 &opts->txn_size))
342 exit(1);
343 opts->exit_on_error = true;
344 break;
345 case 6: /* database patterns to skip */
346 simple_string_list_append(&db_exclude_patterns, optarg);
347 break;
348
349 default:
350 /* getopt_long already emitted a complaint */
351 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
352 exit_nicely(1);
353 }
354 }
355
356 /* Get file name from command line */
357 if (optind < argc)
358 inputFileSpec = argv[optind++];
359 else
360 inputFileSpec = NULL;
361
362 /* Complain if any arguments remain */
363 if (optind < argc)
364 {
365 pg_log_error("too many command-line arguments (first is \"%s\")",
366 argv[optind]);
367 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
368 exit_nicely(1);
369 }
370
371 /* Complain if neither -f nor -d was specified (except if dumping TOC) */
372 if (!opts->cparams.dbname && !opts->filename && !opts->tocSummary)
373 pg_fatal("one of -d/--dbname and -f/--file must be specified");
374
375 if (db_exclude_patterns.head != NULL && globals_only)
376 {
377 pg_log_error("option --exclude-database cannot be used together with -g/--globals-only");
378 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
379 exit_nicely(1);
380 }
381
382 /* Should get at most one of -d and -f, else user is confused */
383 if (opts->cparams.dbname)
384 {
385 if (opts->filename)
386 {
387 pg_log_error("options -d/--dbname and -f/--file cannot be used together");
388 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
389 exit_nicely(1);
390 }
391 opts->useDB = 1;
392 }
393
394 /* reject conflicting "-only" options */
395 if (data_only && schema_only)
396 pg_fatal("options -s/--schema-only and -a/--data-only cannot be used together");
397 if (schema_only && statistics_only)
398 pg_fatal("options -s/--schema-only and --statistics-only cannot be used together");
399 if (data_only && statistics_only)
400 pg_fatal("options -a/--data-only and --statistics-only cannot be used together");
401
402 /* reject conflicting "-only" and "no-" options */
403 if (data_only && no_data)
404 pg_fatal("options -a/--data-only and --no-data cannot be used together");
405 if (schema_only && no_schema)
406 pg_fatal("options -s/--schema-only and --no-schema cannot be used together");
408 pg_fatal("options --statistics-only and --no-statistics cannot be used together");
409
410 /* reject conflicting "with-" and "no-" options */
411 if (with_data && no_data)
412 pg_fatal("options --with-data and --no-data cannot be used together");
413 if (with_schema && no_schema)
414 pg_fatal("options --with-schema and --no-schema cannot be used together");
416 pg_fatal("options --with-statistics and --no-statistics cannot be used together");
417
418 if (data_only && opts->dropSchema)
419 pg_fatal("options -c/--clean and -a/--data-only cannot be used together");
420
421 if (opts->single_txn && opts->txn_size > 0)
422 pg_fatal("options -1/--single-transaction and --transaction-size cannot be used together");
423
424 /*
425 * -C is not compatible with -1, because we can't create a database inside
426 * a transaction block.
427 */
428 if (opts->createDB && opts->single_txn)
429 pg_fatal("options -C/--create and -1/--single-transaction cannot be used together");
430
431 /* Can't do single-txn mode with multiple connections */
432 if (opts->single_txn && numWorkers > 1)
433 pg_fatal("cannot specify both --single-transaction and multiple jobs");
434
435 /*
436 * Set derivative flags. An "-only" option may be overridden by an
437 * explicit "with-" option; e.g. "--schema-only --with-statistics" will
438 * include schema and statistics. Other ambiguous or nonsensical
439 * combinations, e.g. "--schema-only --no-schema", will have already
440 * caused an error in one of the checks above.
441 */
442 opts->dumpData = ((opts->dumpData && !schema_only && !statistics_only) ||
443 (data_only || with_data)) && !no_data;
444 opts->dumpSchema = ((opts->dumpSchema && !data_only && !statistics_only) ||
445 (schema_only || with_schema)) && !no_schema;
446 opts->dumpStatistics = ((opts->dumpStatistics && !schema_only && !data_only) ||
448
449 opts->disable_triggers = disable_triggers;
450 opts->enable_row_security = enable_row_security;
451 opts->noDataForFailedTables = no_data_for_failed_tables;
452 opts->noTableAm = outputNoTableAm;
453 opts->noTablespace = outputNoTablespaces;
454 opts->use_setsessauth = use_setsessauth;
455 opts->no_comments = no_comments;
456 opts->no_policies = no_policies;
457 opts->no_publications = no_publications;
458 opts->no_security_labels = no_security_labels;
459 opts->no_subscriptions = no_subscriptions;
460
461 if (if_exists && !opts->dropSchema)
462 pg_fatal("option --if-exists requires option -c/--clean");
463 opts->if_exists = if_exists;
465
466 if (opts->formatName)
467 {
468 if (pg_strcasecmp(opts->formatName, "c") == 0 ||
469 pg_strcasecmp(opts->formatName, "custom") == 0)
470 opts->format = archCustom;
471 else if (pg_strcasecmp(opts->formatName, "d") == 0 ||
472 pg_strcasecmp(opts->formatName, "directory") == 0)
473 opts->format = archDirectory;
474 else if (pg_strcasecmp(opts->formatName, "t") == 0 ||
475 pg_strcasecmp(opts->formatName, "tar") == 0)
476 opts->format = archTar;
477 else if (pg_strcasecmp(opts->formatName, "p") == 0 ||
478 pg_strcasecmp(opts->formatName, "plain") == 0)
479 {
480 /* recognize this for consistency with pg_dump */
481 pg_fatal("archive format \"%s\" is not supported; please use psql",
482 opts->formatName);
483 }
484 else
485 pg_fatal("unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"",
486 opts->formatName);
487 }
488
489 /*
490 * If toc.dat file is not present in the current path, then check for
491 * global.dat. If global.dat file is present, then restore all the
492 * databases from map.dat (if it exists), but skip restoring those
493 * matching --exclude-database patterns.
494 */
495 if (inputFileSpec != NULL && !file_exists_in_directory(inputFileSpec, "toc.dat") &&
496 file_exists_in_directory(inputFileSpec, "global.dat"))
497 {
498 PGconn *conn = NULL; /* Connection to restore global sql
499 * commands. */
500
501 /*
502 * Can only use --list or --use-list options with a single database
503 * dump.
504 */
505 if (opts->tocSummary)
506 pg_fatal("option -l/--list cannot be used when restoring an archive created by pg_dumpall");
507 else if (opts->tocFile)
508 pg_fatal("option -L/--use-list cannot be used when restoring an archive created by pg_dumpall");
509
510 /*
511 * To restore from a pg_dumpall archive, -C (create database) option
512 * must be specified unless we are only restoring globals.
513 */
514 if (!globals_only && opts->createDB != 1)
515 {
516 pg_log_error("-C/--create option should be specified when restoring an archive created by pg_dumpall");
517 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
518 pg_log_error_hint("Individual databases can be restored using their specific archives.");
519 exit_nicely(1);
520 }
521
522 /*
523 * Connect to the database to execute global sql commands from
524 * global.dat file.
525 */
526 if (opts->cparams.dbname)
527 {
528 conn = ConnectDatabase(opts->cparams.dbname, NULL, opts->cparams.pghost,
529 opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
530 false, progname, NULL, NULL, NULL, NULL);
531
532
533 if (!conn)
534 pg_fatal("could not connect to database \"%s\"", opts->cparams.dbname);
535 }
536
537 /* If globals-only, then return from here. */
538 if (globals_only)
539 {
540 /*
541 * Open global.dat file and execute/append all the global sql
542 * commands.
543 */
544 n_errors = process_global_sql_commands(conn, inputFileSpec,
545 opts->filename);
546
547 if (conn)
548 PQfinish(conn);
549
550 pg_log_info("database restoring skipped as -g/--globals-only option was specified");
551 }
552 else
553 {
554 /* Now restore all the databases from map.dat */
555 n_errors = restore_all_databases(conn, inputFileSpec, db_exclude_patterns,
556 opts, numWorkers);
557 }
558
559 /* Free db pattern list. */
560 simple_string_list_destroy(&db_exclude_patterns);
561 }
562 else /* process if global.dat file does not exist. */
563 {
564 if (db_exclude_patterns.head != NULL)
565 pg_fatal("option --exclude-database can be used only when restoring an archive created by pg_dumpall");
566
567 if (globals_only)
568 pg_fatal("option -g/--globals-only can be used only when restoring an archive created by pg_dumpall");
569
570 n_errors = restore_one_database(inputFileSpec, opts, numWorkers, false, 0);
571 }
572
573 /* Done, print a summary of ignored errors during restore. */
574 if (n_errors)
575 {
576 pg_log_warning("errors ignored on restore: %d", n_errors);
577 return 1;
578 }
579
580 return 0;
581}
582
583/*
584 * restore_one_database
585 *
586 * This will restore one database using toc.dat file.
587 *
588 * returns the number of errors while doing restore.
589 */
590static int
591restore_one_database(const char *inputFileSpec, RestoreOptions *opts,
592 int numWorkers, bool append_data, int num)
593{
594 Archive *AH;
595 int n_errors;
596
597 AH = OpenArchive(inputFileSpec, opts->format);
598
599 SetArchiveOptions(AH, NULL, opts);
600
601 /*
602 * We don't have a connection yet but that doesn't matter. The connection
603 * is initialized to NULL and if we terminate through exit_nicely() while
604 * it's still NULL, the cleanup function will just be a no-op. If we are
605 * restoring multiple databases, then only update AX handle for cleanup as
606 * the previous entry was already in the array and we had closed previous
607 * connection, so we can use the same array slot.
608 */
609 if (!append_data || num == 0)
611 else
613
614 /* Let the archiver know how noisy to be */
615 AH->verbose = opts->verbose;
616
617 /*
618 * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
619 */
620 AH->exit_on_error = opts->exit_on_error;
621
622 if (opts->tocFile)
623 SortTocFromFile(AH);
624
625 AH->numWorkers = numWorkers;
626
627 if (opts->tocSummary)
628 PrintTOCSummary(AH);
629 else
630 {
633 }
634
635 n_errors = AH->n_errors;
636
637 /* AH may be freed in CloseArchive? */
638 CloseArchive(AH);
639
640 return n_errors;
641}
642
643static void
644usage(const char *progname)
645{
646 printf(_("%s restores a PostgreSQL database from an archive created by pg_dump or pg_dumpall.\n"
647 "If the archive is created by pg_dumpall, then restores multiple databases also.\n\n"), progname);
648 printf(_("Usage:\n"));
649 printf(_(" %s [OPTION]... [FILE]\n"), progname);
650
651 printf(_("\nGeneral options:\n"));
652 printf(_(" -d, --dbname=NAME connect to database name\n"));
653 printf(_(" -f, --file=FILENAME output file name (- for stdout)\n"));
654 printf(_(" -F, --format=c|d|t backup file format (should be automatic)\n"));
655 printf(_(" -l, --list print summarized TOC of the archive\n"));
656 printf(_(" -v, --verbose verbose mode\n"));
657 printf(_(" -V, --version output version information, then exit\n"));
658 printf(_(" -?, --help show this help, then exit\n"));
659
660 printf(_("\nOptions controlling the restore:\n"));
661 printf(_(" -a, --data-only restore only the data, no schema\n"));
662 printf(_(" -c, --clean clean (drop) database objects before recreating\n"));
663 printf(_(" -C, --create create the target database\n"));
664 printf(_(" -e, --exit-on-error exit on error, default is to continue\n"));
665 printf(_(" -g, --globals-only restore only global objects, no databases\n"));
666 printf(_(" -I, --index=NAME restore named index\n"));
667 printf(_(" -j, --jobs=NUM use this many parallel jobs to restore\n"));
668 printf(_(" -L, --use-list=FILENAME use table of contents from this file for\n"
669 " selecting/ordering output\n"));
670 printf(_(" -n, --schema=NAME restore only objects in this schema\n"));
671 printf(_(" -N, --exclude-schema=NAME do not restore objects in this schema\n"));
672 printf(_(" -O, --no-owner skip restoration of object ownership\n"));
673 printf(_(" -P, --function=NAME(args) restore named function\n"));
674 printf(_(" -s, --schema-only restore only the schema, no data\n"));
675 printf(_(" -S, --superuser=NAME superuser user name to use for disabling triggers\n"));
676 printf(_(" -t, --table=NAME restore named relation (table, view, etc.)\n"));
677 printf(_(" -T, --trigger=NAME restore named trigger\n"));
678 printf(_(" --exclude-database=PATTERN exclude databases whose name matches with pattern\n"));
679 printf(_(" -x, --no-privileges skip restoration of access privileges (grant/revoke)\n"));
680 printf(_(" -1, --single-transaction restore as a single transaction\n"));
681 printf(_(" --disable-triggers disable triggers during data-only restore\n"));
682 printf(_(" --enable-row-security enable row security\n"));
683 printf(_(" --filter=FILENAME restore or skip objects based on expressions\n"
684 " in FILENAME\n"));
685 printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
686 printf(_(" --no-comments do not restore comment commands\n"));
687 printf(_(" --no-data do not restore data\n"));
688 printf(_(" --no-data-for-failed-tables do not restore data of tables that could not be\n"
689 " created\n"));
690 printf(_(" --no-policies do not restore row security policies\n"));
691 printf(_(" --no-publications do not restore publications\n"));
692 printf(_(" --no-schema do not restore schema\n"));
693 printf(_(" --no-security-labels do not restore security labels\n"));
694 printf(_(" --no-statistics do not restore statistics\n"));
695 printf(_(" --no-subscriptions do not restore subscriptions\n"));
696 printf(_(" --no-table-access-method do not restore table access methods\n"));
697 printf(_(" --no-tablespaces do not restore tablespace assignments\n"));
698 printf(_(" --section=SECTION restore named section (pre-data, data, or post-data)\n"));
699 printf(_(" --statistics-only restore only the statistics, not schema or data\n"));
700 printf(_(" --strict-names require table and/or schema include patterns to\n"
701 " match at least one entity each\n"));
702 printf(_(" --transaction-size=N commit after every N objects\n"));
703 printf(_(" --use-set-session-authorization\n"
704 " use SET SESSION AUTHORIZATION commands instead of\n"
705 " ALTER OWNER commands to set ownership\n"));
706 printf(_(" --with-data dump the data\n"));
707 printf(_(" --with-schema dump the schema\n"));
708 printf(_(" --with-statistics dump the statistics\n"));
709
710 printf(_("\nConnection options:\n"));
711 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
712 printf(_(" -p, --port=PORT database server port number\n"));
713 printf(_(" -U, --username=NAME connect as specified database user\n"));
714 printf(_(" -w, --no-password never prompt for password\n"));
715 printf(_(" -W, --password force password prompt (should happen automatically)\n"));
716 printf(_(" --role=ROLENAME do SET ROLE before restore\n"));
717
718 printf(_("\n"
719 "The options -I, -n, -N, -P, -t, -T, --section, and --exclude-database can be combined\n"
720 "and specified multiple times to select multiple objects.\n"));
721 printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
722 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
723 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
724}
725
726/*
727 * read_restore_filters - retrieve object identifier patterns from file
728 *
729 * Parse the specified filter file for include and exclude patterns, and add
730 * them to the relevant lists. If the filename is "-" then filters will be
731 * read from STDIN rather than a file.
732 */
733static void
735{
736 FilterStateData fstate;
737 char *objname;
738 FilterCommandType comtype;
739 FilterObjectType objtype;
740
742
743 while (filter_read_item(&fstate, &objname, &comtype, &objtype))
744 {
745 if (comtype == FILTER_COMMAND_TYPE_INCLUDE)
746 {
747 switch (objtype)
748 {
750 break;
757 pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
758 "include",
759 filter_object_type_name(objtype));
760 exit_nicely(1);
761
763 opts->selTypes = 1;
764 opts->selFunction = 1;
765 simple_string_list_append(&opts->functionNames, objname);
766 break;
768 opts->selTypes = 1;
769 opts->selIndex = 1;
770 simple_string_list_append(&opts->indexNames, objname);
771 break;
773 simple_string_list_append(&opts->schemaNames, objname);
774 break;
776 opts->selTypes = 1;
777 opts->selTable = 1;
778 simple_string_list_append(&opts->tableNames, objname);
779 break;
781 opts->selTypes = 1;
782 opts->selTrigger = 1;
783 simple_string_list_append(&opts->triggerNames, objname);
784 break;
785 }
786 }
787 else if (comtype == FILTER_COMMAND_TYPE_EXCLUDE)
788 {
789 switch (objtype)
790 {
792 break;
803 pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
804 "exclude",
805 filter_object_type_name(objtype));
806 exit_nicely(1);
807
809 simple_string_list_append(&opts->schemaExcludeNames, objname);
810 break;
811 }
812 }
813 else
814 {
817 }
818
819 if (objname)
820 free(objname);
821 }
822
823 filter_free(&fstate);
824}
825
826/*
827 * file_exists_in_directory
828 *
829 * Returns true if the file exists in the given directory.
830 */
831static bool
832file_exists_in_directory(const char *dir, const char *filename)
833{
834 struct stat st;
835 char buf[MAXPGPATH];
836
837 if (snprintf(buf, MAXPGPATH, "%s/%s", dir, filename) >= MAXPGPATH)
838 pg_fatal("directory name too long: \"%s\"", dir);
839
840 return (stat(buf, &st) == 0 && S_ISREG(st.st_mode));
841}
842
843/*
844 * read_one_statement
845 *
846 * This will start reading from passed file pointer using fgetc and read till
847 * semicolon(sql statement terminator for global.dat file)
848 *
849 * EOF is returned if end-of-file input is seen; time to shut down.
850 */
851
852static int
854{
855 int c; /* character read from getc() */
856 int m;
857
859
860 initStringInfo(&q);
861
862 resetStringInfo(inBuf);
863
864 /*
865 * Read characters until EOF or the appropriate delimiter is seen.
866 */
867 while ((c = fgetc(pfile)) != EOF)
868 {
869 if (c != '\'' && c != '"' && c != '\n' && c != ';')
870 {
871 appendStringInfoChar(inBuf, (char) c);
872 while ((c = fgetc(pfile)) != EOF)
873 {
874 if (c != '\'' && c != '"' && c != ';' && c != '\n')
875 appendStringInfoChar(inBuf, (char) c);
876 else
877 break;
878 }
879 }
880
881 if (c == '\'' || c == '"')
882 {
883 appendStringInfoChar(&q, (char) c);
884 m = c;
885
886 while ((c = fgetc(pfile)) != EOF)
887 {
888 appendStringInfoChar(&q, (char) c);
889
890 if (c == m)
891 {
893 resetStringInfo(&q);
894 break;
895 }
896 }
897 }
898
899 if (c == ';')
900 {
901 appendStringInfoChar(inBuf, (char) ';');
902 break;
903 }
904
905 if (c == '\n')
906 appendStringInfoChar(inBuf, (char) '\n');
907 }
908
909 pg_free(q.data);
910
911 /* No input before EOF signal means time to quit. */
912 if (c == EOF && inBuf->len == 0)
913 return EOF;
914
915 /* return something that's not EOF */
916 return 'Q';
917}
918
919/*
920 * get_dbnames_list_to_restore
921 *
922 * This will mark for skipping any entries from dbname_oid_list that pattern match an
923 * entry in the db_exclude_patterns list.
924 *
925 * Returns the number of database to be restored.
926 *
927 */
928static int
930 SimpleOidStringList *dbname_oid_list,
931 SimpleStringList db_exclude_patterns)
932{
933 int count_db = 0;
934 PQExpBuffer query;
935 PGresult *res;
936
937 query = createPQExpBuffer();
938
939 if (!conn)
940 pg_log_info("considering PATTERN as NAME for --exclude-database option as no db connection while doing pg_restore.");
941
942 /*
943 * Process one by one all dbnames and if specified to skip restoring, then
944 * remove dbname from list.
945 */
946 for (SimpleOidStringListCell * db_cell = dbname_oid_list->head;
947 db_cell; db_cell = db_cell->next)
948 {
949 bool skip_db_restore = false;
951
952 appendStringLiteralConn(db_lit, db_cell->str, conn);
953
954 for (SimpleStringListCell *pat_cell = db_exclude_patterns.head; pat_cell; pat_cell = pat_cell->next)
955 {
956 /*
957 * If there is an exact match then we don't need to try a pattern
958 * match
959 */
960 if (pg_strcasecmp(db_cell->str, pat_cell->val) == 0)
961 skip_db_restore = true;
962 /* Otherwise, try a pattern match if there is a connection */
963 else if (conn)
964 {
965 int dotcnt;
966
967 appendPQExpBufferStr(query, "SELECT 1 ");
968 processSQLNamePattern(conn, query, pat_cell->val, false,
969 false, NULL, db_lit->data,
970 NULL, NULL, NULL, &dotcnt);
971
972 if (dotcnt > 0)
973 {
974 pg_log_error("improper qualified name (too many dotted names): %s",
975 db_cell->str);
976 PQfinish(conn);
977 exit_nicely(1);
978 }
979
980 res = executeQuery(conn, query->data);
981
982 if ((PQresultStatus(res) == PGRES_TUPLES_OK) && PQntuples(res))
983 {
984 skip_db_restore = true;
985 pg_log_info("database \"%s\" matches exclude pattern: \"%s\"", db_cell->str, pat_cell->val);
986 }
987
988 PQclear(res);
989 resetPQExpBuffer(query);
990 }
991
992 if (skip_db_restore)
993 break;
994 }
995
996 destroyPQExpBuffer(db_lit);
997
998 /*
999 * Mark db to be skipped or increment the counter of dbs to be
1000 * restored
1001 */
1002 if (skip_db_restore)
1003 {
1004 pg_log_info("excluding database \"%s\"", db_cell->str);
1005 db_cell->oid = InvalidOid;
1006 }
1007 else
1008 {
1009 count_db++;
1010 }
1011 }
1012
1013 destroyPQExpBuffer(query);
1014
1015 return count_db;
1016}
1017
1018/*
1019 * get_dbname_oid_list_from_mfile
1020 *
1021 * Open map.dat file and read line by line and then prepare a list of database
1022 * names and corresponding db_oid.
1023 *
1024 * Returns, total number of database names in map.dat file.
1025 */
1026static int
1027get_dbname_oid_list_from_mfile(const char *dumpdirpath, SimpleOidStringList *dbname_oid_list)
1028{
1029 FILE *pfile;
1030 char map_file_path[MAXPGPATH];
1031 char line[MAXPGPATH];
1032 int count = 0;
1033
1034 /*
1035 * If there is only global.dat file in dump, then return from here as
1036 * there is no database to restore.
1037 */
1038 if (!file_exists_in_directory(dumpdirpath, "map.dat"))
1039 {
1040 pg_log_info("database restoring is skipped as \"map.dat\" is not present in \"%s\"", dumpdirpath);
1041 return 0;
1042 }
1043
1044 snprintf(map_file_path, MAXPGPATH, "%s/map.dat", dumpdirpath);
1045
1046 /* Open map.dat file. */
1047 pfile = fopen(map_file_path, PG_BINARY_R);
1048
1049 if (pfile == NULL)
1050 pg_fatal("could not open \"%s\": %m", map_file_path);
1051
1052 /* Append all the dbname/db_oid combinations to the list. */
1053 while ((fgets(line, MAXPGPATH, pfile)) != NULL)
1054 {
1055 Oid db_oid = InvalidOid;
1056 char db_oid_str[MAXPGPATH + 1] = "";
1057 char *dbname;
1058
1059 /* Extract dboid. */
1060 sscanf(line, "%u", &db_oid);
1061 sscanf(line, "%20s", db_oid_str);
1062
1063 /* dbname is the rest of the line */
1064 dbname = line + strlen(db_oid_str) + 1;
1065
1066 /* Remove \n from dbname. */
1067 dbname[strlen(dbname) - 1] = '\0';
1068
1069 pg_log_info("found database \"%s\" (OID: %u) in \"%s\"",
1070 dbname, db_oid, map_file_path);
1071
1072 /* Report error and exit if the file has any corrupted data. */
1073 if (!OidIsValid(db_oid) || strlen(dbname) == 0)
1074 pg_fatal("invalid entry in \"%s\" at line : %d", map_file_path,
1075 count + 1);
1076
1077 simple_oid_string_list_append(dbname_oid_list, db_oid, dbname);
1078 count++;
1079 }
1080
1081 /* Close map.dat file. */
1082 fclose(pfile);
1083
1084 return count;
1085}
1086
1087/*
1088 * restore_all_databases
1089 *
1090 * This will restore databases those dumps are present in
1091 * directory based on map.dat file mapping.
1092 *
1093 * This will skip restoring for databases that are specified with
1094 * exclude-database option.
1095 *
1096 * returns, number of errors while doing restore.
1097 */
1098static int
1099restore_all_databases(PGconn *conn, const char *dumpdirpath,
1100 SimpleStringList db_exclude_patterns, RestoreOptions *opts,
1101 int numWorkers)
1102{
1103 SimpleOidStringList dbname_oid_list = {NULL, NULL};
1104 int num_db_restore = 0;
1105 int num_total_db;
1106 int n_errors_total;
1107 int count = 0;
1108 char *connected_db = NULL;
1109 bool dumpData = opts->dumpData;
1110 bool dumpSchema = opts->dumpSchema;
1111 bool dumpStatistics = opts->dumpSchema;
1112
1113 /* Save db name to reuse it for all the database. */
1114 if (opts->cparams.dbname)
1115 connected_db = opts->cparams.dbname;
1116
1117 num_total_db = get_dbname_oid_list_from_mfile(dumpdirpath, &dbname_oid_list);
1118
1119 /* If map.dat has no entry, return after processing global.dat */
1120 if (dbname_oid_list.head == NULL)
1121 return process_global_sql_commands(conn, dumpdirpath, opts->filename);
1122
1123 pg_log_info("found %d database names in \"map.dat\"", num_total_db);
1124
1125 if (!conn)
1126 {
1127 pg_log_info("trying to connect database \"postgres\"");
1128
1129 conn = ConnectDatabase("postgres", NULL, opts->cparams.pghost,
1130 opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
1131 false, progname, NULL, NULL, NULL, NULL);
1132
1133 /* Try with template1. */
1134 if (!conn)
1135 {
1136 pg_log_info("trying to connect database \"template1\"");
1137
1138 conn = ConnectDatabase("template1", NULL, opts->cparams.pghost,
1139 opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
1140 false, progname, NULL, NULL, NULL, NULL);
1141 }
1142 }
1143
1144 /*
1145 * filter the db list according to the exclude patterns
1146 */
1147 num_db_restore = get_dbnames_list_to_restore(conn, &dbname_oid_list,
1148 db_exclude_patterns);
1149
1150 /* Open global.dat file and execute/append all the global sql commands. */
1151 n_errors_total = process_global_sql_commands(conn, dumpdirpath, opts->filename);
1152
1153 /* Close the db connection as we are done with globals and patterns. */
1154 if (conn)
1155 PQfinish(conn);
1156
1157 /* Exit if no db needs to be restored. */
1158 if (dbname_oid_list.head == NULL || num_db_restore == 0)
1159 {
1160 pg_log_info("no database needs to restore out of %d databases", num_total_db);
1161 return n_errors_total;
1162 }
1163
1164 pg_log_info("need to restore %d databases out of %d databases", num_db_restore, num_total_db);
1165
1166 /*
1167 * Till now, we made a list of databases, those needs to be restored after
1168 * skipping names of exclude-database. Now we can launch parallel workers
1169 * to restore these databases.
1170 */
1171 for (SimpleOidStringListCell * db_cell = dbname_oid_list.head;
1172 db_cell; db_cell = db_cell->next)
1173 {
1174 char subdirpath[MAXPGPATH];
1175 char subdirdbpath[MAXPGPATH];
1176 char dbfilename[MAXPGPATH];
1177 int n_errors;
1178
1179 /* ignore dbs marked for skipping */
1180 if (db_cell->oid == InvalidOid)
1181 continue;
1182
1183 /*
1184 * We need to reset override_dbname so that objects can be restored
1185 * into an already created database. (used with -d/--dbname option)
1186 */
1187 if (opts->cparams.override_dbname)
1188 {
1189 pfree(opts->cparams.override_dbname);
1190 opts->cparams.override_dbname = NULL;
1191 }
1192
1193 snprintf(subdirdbpath, MAXPGPATH, "%s/databases", dumpdirpath);
1194
1195 /*
1196 * Look for the database dump file/dir. If there is an {oid}.tar or
1197 * {oid}.dmp file, use it. Otherwise try to use a directory called
1198 * {oid}
1199 */
1200 snprintf(dbfilename, MAXPGPATH, "%u.tar", db_cell->oid);
1201 if (file_exists_in_directory(subdirdbpath, dbfilename))
1202 snprintf(subdirpath, MAXPGPATH, "%s/databases/%u.tar", dumpdirpath, db_cell->oid);
1203 else
1204 {
1205 snprintf(dbfilename, MAXPGPATH, "%u.dmp", db_cell->oid);
1206
1207 if (file_exists_in_directory(subdirdbpath, dbfilename))
1208 snprintf(subdirpath, MAXPGPATH, "%s/databases/%u.dmp", dumpdirpath, db_cell->oid);
1209 else
1210 snprintf(subdirpath, MAXPGPATH, "%s/databases/%u", dumpdirpath, db_cell->oid);
1211 }
1212
1213 pg_log_info("restoring database \"%s\"", db_cell->str);
1214
1215 /* If database is already created, then don't set createDB flag. */
1216 if (opts->cparams.dbname)
1217 {
1218 PGconn *test_conn;
1219
1220 test_conn = ConnectDatabase(db_cell->str, NULL, opts->cparams.pghost,
1221 opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
1222 false, progname, NULL, NULL, NULL, NULL);
1223 if (test_conn)
1224 {
1225 PQfinish(test_conn);
1226
1227 /* Use already created database for connection. */
1228 opts->createDB = 0;
1229 opts->cparams.dbname = db_cell->str;
1230 }
1231 else
1232 {
1233 /* we'll have to create it */
1234 opts->createDB = 1;
1235 opts->cparams.dbname = connected_db;
1236 }
1237 }
1238
1239 /*
1240 * Reset flags - might have been reset in pg_backup_archiver.c by the
1241 * previous restore.
1242 */
1243 opts->dumpData = dumpData;
1244 opts->dumpSchema = dumpSchema;
1245 opts->dumpStatistics = dumpStatistics;
1246
1247 /* Restore the single database. */
1248 n_errors = restore_one_database(subdirpath, opts, numWorkers, true, count);
1249
1250 /* Print a summary of ignored errors during single database restore. */
1251 if (n_errors)
1252 {
1253 n_errors_total += n_errors;
1254 pg_log_warning("errors ignored on database \"%s\" restore: %d", db_cell->str, n_errors);
1255 }
1256
1257 count++;
1258 }
1259
1260 /* Log number of processed databases. */
1261 pg_log_info("number of restored databases is %d", num_db_restore);
1262
1263 /* Free dbname and dboid list. */
1264 simple_oid_string_list_destroy(&dbname_oid_list);
1265
1266 return n_errors_total;
1267}
1268
1269/*
1270 * process_global_sql_commands
1271 *
1272 * Open global.dat and execute or copy the sql commands one by one.
1273 *
1274 * If outfile is not NULL, copy all sql commands into outfile rather than
1275 * executing them.
1276 *
1277 * Returns the number of errors while processing global.dat
1278 */
1279static int
1280process_global_sql_commands(PGconn *conn, const char *dumpdirpath, const char *outfile)
1281{
1282 char global_file_path[MAXPGPATH];
1283 PGresult *result;
1284 StringInfoData sqlstatement,
1285 user_create;
1286 FILE *pfile;
1287 int n_errors = 0;
1288
1289 snprintf(global_file_path, MAXPGPATH, "%s/global.dat", dumpdirpath);
1290
1291 /* Open global.dat file. */
1292 pfile = fopen(global_file_path, PG_BINARY_R);
1293
1294 if (pfile == NULL)
1295 pg_fatal("could not open \"%s\": %m", global_file_path);
1296
1297 /*
1298 * If outfile is given, then just copy all global.dat file data into
1299 * outfile.
1300 */
1301 if (outfile)
1302 {
1304 return 0;
1305 }
1306
1307 /* Init sqlstatement to append commands. */
1308 initStringInfo(&sqlstatement);
1309
1310 /* creation statement for our current role */
1311 initStringInfo(&user_create);
1312 appendStringInfoString(&user_create, "CREATE ROLE ");
1313 /* should use fmtId here, but we don't know the encoding */
1314 appendStringInfoString(&user_create, PQuser(conn));
1315 appendStringInfoChar(&user_create, ';');
1316
1317 /* Process file till EOF and execute sql statements. */
1318 while (read_one_statement(&sqlstatement, pfile) != EOF)
1319 {
1320 /* don't try to create the role we are connected as */
1321 if (strstr(sqlstatement.data, user_create.data))
1322 continue;
1323
1324 pg_log_info("executing query: %s", sqlstatement.data);
1325 result = PQexec(conn, sqlstatement.data);
1326
1327 switch (PQresultStatus(result))
1328 {
1329 case PGRES_COMMAND_OK:
1330 case PGRES_TUPLES_OK:
1331 case PGRES_EMPTY_QUERY:
1332 break;
1333 default:
1334 n_errors++;
1335 pg_log_error("could not execute query: \"%s\" \nCommand was: \"%s\"", PQerrorMessage(conn), sqlstatement.data);
1336 }
1337 PQclear(result);
1338 }
1339
1340 /* Print a summary of ignored errors during global.dat. */
1341 if (n_errors)
1342 pg_log_warning("ignored %d errors in \"%s\"", n_errors, global_file_path);
1343
1344 fclose(pfile);
1345
1346 return n_errors;
1347}
1348
1349/*
1350 * copy_or_print_global_file
1351 *
1352 * Copy global.dat into the output file. If "-" is used as outfile,
1353 * then print commands to stdout.
1354 */
1355static void
1356copy_or_print_global_file(const char *outfile, FILE *pfile)
1357{
1358 char out_file_path[MAXPGPATH];
1359 FILE *OPF;
1360 int c;
1361
1362 /* "-" is used for stdout. */
1363 if (strcmp(outfile, "-") == 0)
1364 OPF = stdout;
1365 else
1366 {
1367 snprintf(out_file_path, MAXPGPATH, "%s", outfile);
1368 OPF = fopen(out_file_path, PG_BINARY_W);
1369
1370 if (OPF == NULL)
1371 {
1372 fclose(pfile);
1373 pg_fatal("could not open file: \"%s\"", outfile);
1374 }
1375 }
1376
1377 /* Append global.dat into output file or print to stdout. */
1378 while ((c = fgetc(pfile)) != EOF)
1379 fputc(c, OPF);
1380
1381 fclose(pfile);
1382
1383 /* Close output file. */
1384 if (strcmp(outfile, "-") != 0)
1385 fclose(OPF);
1386}
void replace_on_exit_close_archive(Archive *AHX)
Definition: parallel.c:341
void on_exit_close_archive(Archive *AHX)
Definition: parallel.c:330
void init_parallel_dump_utils(void)
Definition: parallel.c:238
#define PG_MAX_JOBS
Definition: parallel.h:48
#define PG_BINARY_R
Definition: c.h:1246
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1185
#define PG_BINARY_W
Definition: c.h:1247
#define OidIsValid(objectId)
Definition: c.h:746
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:429
PGresult * executeQuery(PGconn *conn, const char *query)
Definition: connectdb.c:277
PGconn * ConnectDatabase(const char *dbname, const char *connection_string, const char *pghost, const char *pgport, const char *pguser, trivalue prompt_password, bool fail_on_error, const char *progname, const char **connstr, int *server_version, char *password, char *override_dbname)
Definition: connectdb.c:40
#define _(x)
Definition: elog.c:91
static size_t append_data(char *buf, size_t size, size_t nmemb, void *userdata)
void PQfinish(PGconn *conn)
Definition: fe-connect.c:5290
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7463
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7619
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
void PQclear(PGresult *res)
Definition: fe-exec.c:721
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2262
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void pg_free(void *ptr)
Definition: fe_memutils.c:105
void filter_init(FilterStateData *fstate, const char *filename, exit_function f_exit)
Definition: filter.c:36
void filter_free(FilterStateData *fstate)
Definition: filter.c:60
const char * filter_object_type_name(FilterObjectType fot)
Definition: filter.c:82
bool filter_read_item(FilterStateData *fstate, char **objname, FilterCommandType *comtype, FilterObjectType *objtype)
Definition: filter.c:389
void pg_log_filter_error(FilterStateData *fstate, const char *fmt,...)
Definition: filter.c:154
FilterObjectType
Definition: filter.h:48
@ FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN
Definition: filter.h:51
@ FILTER_OBJECT_TYPE_SCHEMA
Definition: filter.h:57
@ FILTER_OBJECT_TYPE_INDEX
Definition: filter.h:56
@ FILTER_OBJECT_TYPE_TRIGGER
Definition: filter.h:60
@ FILTER_OBJECT_TYPE_FOREIGN_DATA
Definition: filter.h:54
@ FILTER_OBJECT_TYPE_DATABASE
Definition: filter.h:52
@ FILTER_OBJECT_TYPE_FUNCTION
Definition: filter.h:55
@ FILTER_OBJECT_TYPE_TABLE_DATA
Definition: filter.h:50
@ FILTER_OBJECT_TYPE_NONE
Definition: filter.h:49
@ FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN
Definition: filter.h:59
@ FILTER_OBJECT_TYPE_EXTENSION
Definition: filter.h:53
@ FILTER_OBJECT_TYPE_TABLE
Definition: filter.h:58
FilterCommandType
Definition: filter.h:38
@ FILTER_COMMAND_TYPE_NONE
Definition: filter.h:39
@ FILTER_COMMAND_TYPE_EXCLUDE
Definition: filter.h:41
@ FILTER_COMMAND_TYPE_INCLUDE
Definition: filter.h:40
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
Assert(PointerIsAligned(start, uint64))
#define free(a)
Definition: header.h:65
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:125
@ PGRES_EMPTY_QUERY
Definition: libpq-fe.h:124
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:128
void pg_logging_increase_verbosity(void)
Definition: logging.c:185
void pg_logging_init(const char *argv0)
Definition: logging.c:83
void pg_logging_set_level(enum pg_log_level new_level)
Definition: logging.c:176
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_error_hint(...)
Definition: logging.h:112
#define pg_log_info(...)
Definition: logging.h:124
@ PG_LOG_WARNING
Definition: logging.h:38
const char * progname
Definition: main.c:44
void pfree(void *pointer)
Definition: mcxt.c:2147
bool option_parse_int(const char *optarg, const char *optname, int min_range, int max_range, int *result)
Definition: option_utils.c:50
static AmcheckOptions opts
Definition: pg_amcheck.c:112
void ProcessArchiveRestoreOptions(Archive *AHX)
RestoreOptions * NewRestoreOptions(void)
Archive * OpenArchive(const char *FileSpec, const ArchiveFormat fmt)
void RestoreArchive(Archive *AHX, bool append_data)
void CloseArchive(Archive *AHX)
void SortTocFromFile(Archive *AHX)
void PrintTOCSummary(Archive *AHX)
void SetArchiveOptions(Archive *AH, DumpOptions *dopt, RestoreOptions *ropt)
@ archTar
Definition: pg_backup.h:43
@ archCustom
Definition: pg_backup.h:42
@ archDirectory
Definition: pg_backup.h:45
void exit_nicely(int code)
void set_dump_section(const char *arg, int *dumpSections)
#define pg_fatal(...)
#define MAXPGPATH
static int strict_names
Definition: pg_dump.c:152
static int if_exists
Definition: pg_dumpall.c:94
static int statistics_only
Definition: pg_dumpall.c:116
static int no_policies
Definition: pg_dumpall.c:100
static int disable_triggers
Definition: pg_dumpall.c:93
static int with_data
Definition: pg_dumpall.c:110
static FILE * OPF
Definition: pg_dumpall.c:122
static int no_comments
Definition: pg_dumpall.c:99
static int no_publications
Definition: pg_dumpall.c:101
static int with_schema
Definition: pg_dumpall.c:111
static int no_security_labels
Definition: pg_dumpall.c:102
static int no_statistics
Definition: pg_dumpall.c:105
static int use_setsessauth
Definition: pg_dumpall.c:98
static int no_data
Definition: pg_dumpall.c:103
static int no_schema
Definition: pg_dumpall.c:104
static int no_subscriptions
Definition: pg_dumpall.c:106
static char * filename
Definition: pg_dumpall.c:123
static int with_statistics
Definition: pg_dumpall.c:112
PGDLLIMPORT int optind
Definition: getopt.c:51
PGDLLIMPORT char * optarg
Definition: getopt.c:53
static char * outfile
static void usage(const char *progname)
Definition: pg_restore.c:644
static bool file_exists_in_directory(const char *dir, const char *filename)
Definition: pg_restore.c:832
static int get_dbnames_list_to_restore(PGconn *conn, SimpleOidStringList *dbname_oid_list, SimpleStringList db_exclude_patterns)
Definition: pg_restore.c:929
int main(int argc, char **argv)
Definition: pg_restore.c:76
static int restore_all_databases(PGconn *conn, const char *dumpdirpath, SimpleStringList db_exclude_patterns, RestoreOptions *opts, int numWorkers)
Definition: pg_restore.c:1099
static void copy_or_print_global_file(const char *outfile, FILE *pfile)
Definition: pg_restore.c:1356
static int get_dbname_oid_list_from_mfile(const char *dumpdirpath, SimpleOidStringList *dbname_oid_list)
Definition: pg_restore.c:1027
static void read_restore_filters(const char *filename, RestoreOptions *opts)
Definition: pg_restore.c:734
static int process_global_sql_commands(PGconn *conn, const char *dumpdirpath, const char *outfile)
Definition: pg_restore.c:1280
static int read_one_statement(StringInfo inBuf, FILE *pfile)
Definition: pg_restore.c:853
static int restore_one_database(const char *inputFileSpec, RestoreOptions *opts, int numWorkers, bool append_data, int num)
Definition: pg_restore.c:591
static char * buf
Definition: pg_test_fsync.c:72
#define pg_log_warning(...)
Definition: pgfnames.c:24
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define snprintf
Definition: port.h:239
const char * get_progname(const char *argv0)
Definition: path.c:652
#define printf(...)
Definition: port.h:245
#define InvalidOid
Definition: postgres_ext.h:35
unsigned int Oid
Definition: postgres_ext.h:30
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:146
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:114
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
char * c
void simple_oid_string_list_append(SimpleOidStringList *list, Oid oid, const char *str)
Definition: simple_list.c:200
void simple_oid_string_list_destroy(SimpleOidStringList *list)
Definition: simple_list.c:222
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
void simple_string_list_destroy(SimpleStringList *list)
Definition: simple_list.c:125
char * dbname
Definition: streamutil.c:49
PGconn * conn
Definition: streamutil.c:52
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:446
bool processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern, bool have_where, bool force_escape, const char *schemavar, const char *namevar, const char *altnamevar, const char *visibilityrule, PQExpBuffer dbnamebuf, int *dotcnt)
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:126
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:230
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:242
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
bool strict_names
Definition: pg_amcheck.c:61
bool exit_on_error
Definition: pg_backup.h:247
int n_errors
Definition: pg_backup.h:248
int numWorkers
Definition: pg_backup.h:235
int verbose
Definition: pg_backup.h:227
struct SimpleOidStringListCell * next
Definition: simple_list.h:60
SimpleOidStringListCell * head
Definition: simple_list.h:67
struct SimpleStringListCell * next
Definition: simple_list.h:34
SimpleStringListCell * head
Definition: simple_list.h:42
unsigned short st_mode
Definition: win32_port.h:258
@ TRI_YES
Definition: vacuumlo.c:38
@ TRI_DEFAULT
Definition: vacuumlo.c:36
@ TRI_NO
Definition: vacuumlo.c:37
#define stat
Definition: win32_port.h:274
#define S_ISREG(m)
Definition: win32_port.h:318