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