PostgreSQL Source Code git master
pg_dumpall.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * pg_dumpall.c
4 *
5 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
7 *
8 * pg_dumpall forces all pg_dump output to be text, since it also outputs
9 * text into the same output stream.
10 *
11 * src/bin/pg_dump/pg_dumpall.c
12 *
13 *-------------------------------------------------------------------------
14 */
15
16#include "postgres_fe.h"
17
18#include <time.h>
19#include <unistd.h>
20
21#include "catalog/pg_authid_d.h"
22#include "common/connect.h"
23#include "common/file_perm.h"
24#include "common/file_utils.h"
26#include "common/logging.h"
27#include "common/string.h"
28#include "connectdb.h"
29#include "dumputils.h"
31#include "filter.h"
32#include "getopt_long.h"
33
34/* version string we expect back from pg_dump */
35#define PGDUMP_VERSIONSTR "pg_dump (PostgreSQL) " PG_VERSION "\n"
36
37typedef struct
38{
41 char *rolename;
43
44#define SH_PREFIX rolename
45#define SH_ELEMENT_TYPE RoleNameEntry
46#define SH_KEY_TYPE char *
47#define SH_KEY rolename
48#define SH_HASH_KEY(tb, key) hash_string(key)
49#define SH_EQUAL(tb, a, b) (strcmp(a, b) == 0)
50#define SH_STORE_HASH
51#define SH_GET_HASH(tb, a) (a)->hashval
52#define SH_SCOPE static inline
53#define SH_RAW_ALLOCATOR pg_malloc0
54#define SH_DECLARE
55#define SH_DEFINE
56#include "lib/simplehash.h"
57
58static void help(void);
59
60static void dropRoles(PGconn *conn);
61static void dumpRoles(PGconn *conn);
62static void dumpRoleMembership(PGconn *conn);
63static void dumpRoleGUCPrivs(PGconn *conn);
64static void dropTablespaces(PGconn *conn);
65static void dumpTablespaces(PGconn *conn);
66static void dropDBs(PGconn *conn);
67static void dumpUserConfig(PGconn *conn, const char *username);
68static void dumpDatabases(PGconn *conn, ArchiveFormat archDumpFormat);
69static void dumpTimestamp(const char *msg);
70static int runPgDump(const char *dbname, const char *create_opts,
71 char *dbfile, ArchiveFormat archDumpFormat);
72static void buildShSecLabels(PGconn *conn,
73 const char *catalog_name, Oid objectId,
74 const char *objtype, const char *objname,
75 PQExpBuffer buffer);
76static void executeCommand(PGconn *conn, const char *query);
78 SimpleStringList *names);
79static void read_dumpall_filters(const char *filename, SimpleStringList *pattern);
80static ArchiveFormat parseDumpFormat(const char *format);
81
84static const char *connstr = "";
85static bool output_clean = false;
86static bool skip_acls = false;
87static bool verbose = false;
88static bool dosync = true;
89
90static int binary_upgrade = 0;
91static int column_inserts = 0;
93static int disable_triggers = 0;
94static int if_exists = 0;
95static int inserts = 0;
97static int no_tablespaces = 0;
98static int use_setsessauth = 0;
99static int no_comments = 0;
100static int no_policies = 0;
101static int no_publications = 0;
102static int no_security_labels = 0;
103static int no_data = 0;
104static int no_schema = 0;
105static int no_statistics = 0;
106static int no_subscriptions = 0;
107static int no_toast_compression = 0;
109static int no_role_passwords = 0;
110static int with_data = 0;
111static int with_schema = 0;
112static int with_statistics = 0;
113static int server_version;
116static int statistics_only = 0;
117static int sequence_data = 0;
118
119static char role_catalog[10];
120#define PG_AUTHID "pg_authid"
121#define PG_ROLES "pg_roles "
122
123static FILE *OPF;
124static char *filename = NULL;
125
128
129int
130main(int argc, char *argv[])
131{
132 static struct option long_options[] = {
133 {"data-only", no_argument, NULL, 'a'},
134 {"clean", no_argument, NULL, 'c'},
135 {"encoding", required_argument, NULL, 'E'},
136 {"file", required_argument, NULL, 'f'},
137 {"globals-only", no_argument, NULL, 'g'},
138 {"host", required_argument, NULL, 'h'},
139 {"dbname", required_argument, NULL, 'd'},
140 {"database", required_argument, NULL, 'l'},
141 {"no-owner", no_argument, NULL, 'O'},
142 {"port", required_argument, NULL, 'p'},
143 {"roles-only", no_argument, NULL, 'r'},
144 {"schema-only", no_argument, NULL, 's'},
145 {"superuser", required_argument, NULL, 'S'},
146 {"tablespaces-only", no_argument, NULL, 't'},
147 {"username", required_argument, NULL, 'U'},
148 {"verbose", no_argument, NULL, 'v'},
149 {"no-password", no_argument, NULL, 'w'},
150 {"password", no_argument, NULL, 'W'},
151 {"no-privileges", no_argument, NULL, 'x'},
152 {"no-acl", no_argument, NULL, 'x'},
153 {"format", required_argument, NULL, 'F'},
154
155 /*
156 * the following options don't have an equivalent short option letter
157 */
158 {"attribute-inserts", no_argument, &column_inserts, 1},
159 {"binary-upgrade", no_argument, &binary_upgrade, 1},
160 {"column-inserts", no_argument, &column_inserts, 1},
161 {"disable-dollar-quoting", no_argument, &disable_dollar_quoting, 1},
162 {"disable-triggers", no_argument, &disable_triggers, 1},
163 {"exclude-database", required_argument, NULL, 6},
164 {"extra-float-digits", required_argument, NULL, 5},
165 {"if-exists", no_argument, &if_exists, 1},
166 {"inserts", no_argument, &inserts, 1},
167 {"lock-wait-timeout", required_argument, NULL, 2},
168 {"no-table-access-method", no_argument, &no_table_access_method, 1},
169 {"no-tablespaces", no_argument, &no_tablespaces, 1},
170 {"quote-all-identifiers", no_argument, &quote_all_identifiers, 1},
171 {"load-via-partition-root", no_argument, &load_via_partition_root, 1},
172 {"role", required_argument, NULL, 3},
173 {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
174 {"no-comments", no_argument, &no_comments, 1},
175 {"no-data", no_argument, &no_data, 1},
176 {"no-policies", no_argument, &no_policies, 1},
177 {"no-publications", no_argument, &no_publications, 1},
178 {"no-role-passwords", no_argument, &no_role_passwords, 1},
179 {"no-schema", no_argument, &no_schema, 1},
180 {"no-security-labels", no_argument, &no_security_labels, 1},
181 {"no-subscriptions", no_argument, &no_subscriptions, 1},
182 {"no-statistics", no_argument, &no_statistics, 1},
183 {"no-sync", no_argument, NULL, 4},
184 {"no-toast-compression", no_argument, &no_toast_compression, 1},
185 {"no-unlogged-table-data", no_argument, &no_unlogged_table_data, 1},
186 {"with-data", no_argument, &with_data, 1},
187 {"with-schema", no_argument, &with_schema, 1},
188 {"with-statistics", no_argument, &with_statistics, 1},
189 {"on-conflict-do-nothing", no_argument, &on_conflict_do_nothing, 1},
190 {"rows-per-insert", required_argument, NULL, 7},
191 {"statistics-only", no_argument, &statistics_only, 1},
192 {"filter", required_argument, NULL, 8},
193 {"sequence-data", no_argument, &sequence_data, 1},
194
195 {NULL, 0, NULL, 0}
196 };
197
198 char *pghost = NULL;
199 char *pgport = NULL;
200 char *pguser = NULL;
201 char *pgdb = NULL;
202 char *use_role = NULL;
203 const char *dumpencoding = NULL;
204 ArchiveFormat archDumpFormat = archNull;
205 const char *formatName = "p";
206 trivalue prompt_password = TRI_DEFAULT;
207 bool data_only = false;
208 bool globals_only = false;
209 bool roles_only = false;
210 bool tablespaces_only = false;
211 PGconn *conn;
212 int encoding;
213 const char *std_strings;
214 int c,
215 ret;
216 int optindex;
217
218 pg_logging_init(argv[0]);
220 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
221 progname = get_progname(argv[0]);
222
223 if (argc > 1)
224 {
225 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
226 {
227 help();
228 exit_nicely(0);
229 }
230 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
231 {
232 puts("pg_dumpall (PostgreSQL) " PG_VERSION);
233 exit_nicely(0);
234 }
235 }
236
237 if ((ret = find_other_exec(argv[0], "pg_dump", PGDUMP_VERSIONSTR,
238 pg_dump_bin)) < 0)
239 {
240 char full_path[MAXPGPATH];
241
242 if (find_my_exec(argv[0], full_path) < 0)
243 strlcpy(full_path, progname, sizeof(full_path));
244
245 if (ret == -1)
246 pg_fatal("program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
247 "pg_dump", progname, full_path);
248 else
249 pg_fatal("program \"%s\" was found by \"%s\" but was not the same version as %s",
250 "pg_dump", full_path, progname);
251 }
252
254
255 while ((c = getopt_long(argc, argv, "acd:E:f:F:gh:l:Op:rsS:tU:vwWx", long_options, &optindex)) != -1)
256 {
257 switch (c)
258 {
259 case 'a':
260 data_only = true;
262 break;
263
264 case 'c':
265 output_clean = true;
266 break;
267
268 case 'd':
270 break;
271
272 case 'E':
273 dumpencoding = pg_strdup(optarg);
276 break;
277
278 case 'f':
282 break;
283 case 'F':
284 formatName = pg_strdup(optarg);
285 break;
286 case 'g':
287 globals_only = true;
288 break;
289
290 case 'h':
292 break;
293
294 case 'l':
295 pgdb = pg_strdup(optarg);
296 break;
297
298 case 'O':
300 break;
301
302 case 'p':
304 break;
305
306 case 'r':
307 roles_only = true;
308 break;
309
310 case 's':
312 break;
313
314 case 'S':
317 break;
318
319 case 't':
320 tablespaces_only = true;
321 break;
322
323 case 'U':
324 pguser = pg_strdup(optarg);
325 break;
326
327 case 'v':
328 verbose = true;
331 break;
332
333 case 'w':
334 prompt_password = TRI_NO;
336 break;
337
338 case 'W':
339 prompt_password = TRI_YES;
341 break;
342
343 case 'x':
344 skip_acls = true;
346 break;
347
348 case 0:
349 break;
350
351 case 2:
352 appendPQExpBufferStr(pgdumpopts, " --lock-wait-timeout ");
354 break;
355
356 case 3:
357 use_role = pg_strdup(optarg);
358 appendPQExpBufferStr(pgdumpopts, " --role ");
359 appendShellString(pgdumpopts, use_role);
360 break;
361
362 case 4:
363 dosync = false;
364 appendPQExpBufferStr(pgdumpopts, " --no-sync");
365 break;
366
367 case 5:
368 appendPQExpBufferStr(pgdumpopts, " --extra-float-digits ");
370 break;
371
372 case 6:
374 break;
375
376 case 7:
377 appendPQExpBufferStr(pgdumpopts, " --rows-per-insert ");
379 break;
380
381 case 8:
383 break;
384
385 default:
386 /* getopt_long already emitted a complaint */
387 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
388 exit_nicely(1);
389 }
390 }
391
392 /* Complain if any arguments remain */
393 if (optind < argc)
394 {
395 pg_log_error("too many command-line arguments (first is \"%s\")",
396 argv[optind]);
397 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
398 exit_nicely(1);
399 }
400
401 if (database_exclude_patterns.head != NULL &&
402 (globals_only || roles_only || tablespaces_only))
403 {
404 pg_log_error("option --exclude-database cannot be used together with -g/--globals-only, -r/--roles-only, or -t/--tablespaces-only");
405 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
406 exit_nicely(1);
407 }
408
409 /* Make sure the user hasn't specified a mix of globals-only options */
410 if (globals_only && roles_only)
411 {
412 pg_log_error("options -g/--globals-only and -r/--roles-only cannot be used together");
413 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
414 exit_nicely(1);
415 }
416
417 if (globals_only && tablespaces_only)
418 {
419 pg_log_error("options -g/--globals-only and -t/--tablespaces-only cannot be used together");
420 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
421 exit_nicely(1);
422 }
423
424 if (if_exists && !output_clean)
425 pg_fatal("option --if-exists requires option -c/--clean");
426
427 if (roles_only && tablespaces_only)
428 {
429 pg_log_error("options -r/--roles-only and -t/--tablespaces-only cannot be used together");
430 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
431 exit_nicely(1);
432 }
433
434 /* Get format for dump. */
435 archDumpFormat = parseDumpFormat(formatName);
436
437 /*
438 * If a non-plain format is specified, a file name is also required as the
439 * path to the main directory.
440 */
441 if (archDumpFormat != archNull &&
442 (!filename || strcmp(filename, "") == 0))
443 {
444 pg_log_error("option -F/--format=d|c|t requires option -f/--file");
445 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
446 exit_nicely(1);
447 }
448
449 /*
450 * If password values are not required in the dump, switch to using
451 * pg_roles which is equally useful, just more likely to have unrestricted
452 * access than pg_authid.
453 */
456 else
458
459 /* Add long options to the pg_dump argument list */
460 if (binary_upgrade)
461 appendPQExpBufferStr(pgdumpopts, " --binary-upgrade");
462 if (column_inserts)
463 appendPQExpBufferStr(pgdumpopts, " --column-inserts");
465 appendPQExpBufferStr(pgdumpopts, " --disable-dollar-quoting");
467 appendPQExpBufferStr(pgdumpopts, " --disable-triggers");
468 if (inserts)
469 appendPQExpBufferStr(pgdumpopts, " --inserts");
471 appendPQExpBufferStr(pgdumpopts, " --no-table-access-method");
472 if (no_tablespaces)
473 appendPQExpBufferStr(pgdumpopts, " --no-tablespaces");
475 appendPQExpBufferStr(pgdumpopts, " --quote-all-identifiers");
477 appendPQExpBufferStr(pgdumpopts, " --load-via-partition-root");
478 if (use_setsessauth)
479 appendPQExpBufferStr(pgdumpopts, " --use-set-session-authorization");
480 if (no_comments)
481 appendPQExpBufferStr(pgdumpopts, " --no-comments");
482 if (no_data)
483 appendPQExpBufferStr(pgdumpopts, " --no-data");
484 if (no_policies)
485 appendPQExpBufferStr(pgdumpopts, " --no-policies");
486 if (no_publications)
487 appendPQExpBufferStr(pgdumpopts, " --no-publications");
489 appendPQExpBufferStr(pgdumpopts, " --no-security-labels");
490 if (no_schema)
491 appendPQExpBufferStr(pgdumpopts, " --no-schema");
492 if (no_statistics)
493 appendPQExpBufferStr(pgdumpopts, " --no-statistics");
495 appendPQExpBufferStr(pgdumpopts, " --no-subscriptions");
497 appendPQExpBufferStr(pgdumpopts, " --no-toast-compression");
499 appendPQExpBufferStr(pgdumpopts, " --no-unlogged-table-data");
500 if (with_data)
501 appendPQExpBufferStr(pgdumpopts, " --with-data");
502 if (with_schema)
503 appendPQExpBufferStr(pgdumpopts, " --with-schema");
504 if (with_statistics)
505 appendPQExpBufferStr(pgdumpopts, " --with-statistics");
507 appendPQExpBufferStr(pgdumpopts, " --on-conflict-do-nothing");
508 if (statistics_only)
509 appendPQExpBufferStr(pgdumpopts, " --statistics-only");
510 if (sequence_data)
511 appendPQExpBufferStr(pgdumpopts, " --sequence-data");
512
513 /*
514 * Open the output file if required, otherwise use stdout. If required,
515 * then create new directory and global.dat file.
516 */
517 if (archDumpFormat != archNull)
518 {
519 char global_path[MAXPGPATH];
520
521 /* Create new directory or accept the empty existing directory. */
523
524 snprintf(global_path, MAXPGPATH, "%s/global.dat", filename);
525
526 OPF = fopen(global_path, PG_BINARY_W);
527 if (!OPF)
528 pg_fatal("could not open \"%s\": %m", global_path);
529 }
530 else if (filename)
531 {
532 OPF = fopen(filename, PG_BINARY_W);
533 if (!OPF)
534 pg_fatal("could not open output file \"%s\": %m",
535 filename);
536 }
537 else
538 OPF = stdout;
539
540 /*
541 * If there was a database specified on the command line, use that,
542 * otherwise try to connect to database "postgres", and failing that
543 * "template1".
544 */
545 if (pgdb)
546 {
547 conn = ConnectDatabase(pgdb, connstr, pghost, pgport, pguser,
548 prompt_password, false,
549 progname, &connstr, &server_version, NULL, NULL);
550
551 if (!conn)
552 pg_fatal("could not connect to database \"%s\"", pgdb);
553 }
554 else
555 {
556 conn = ConnectDatabase("postgres", connstr, pghost, pgport, pguser,
557 prompt_password, false,
558 progname, &connstr, &server_version, NULL, NULL);
559 if (!conn)
560 conn = ConnectDatabase("template1", connstr, pghost, pgport, pguser,
561 prompt_password, true,
562 progname, &connstr, &server_version, NULL, NULL);
563
564 if (!conn)
565 {
566 pg_log_error("could not connect to databases \"postgres\" or \"template1\"\n"
567 "Please specify an alternative database.");
568 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
569 exit_nicely(1);
570 }
571 }
572
573 /*
574 * Get a list of database names that match the exclude patterns
575 */
578
579 /*
580 * Set the client encoding if requested.
581 */
582 if (dumpencoding)
583 {
584 if (PQsetClientEncoding(conn, dumpencoding) < 0)
585 pg_fatal("invalid client encoding \"%s\" specified",
586 dumpencoding);
587 }
588
589 /*
590 * Get the active encoding and the standard_conforming_strings setting, so
591 * we know how to escape strings.
592 */
595 std_strings = PQparameterStatus(conn, "standard_conforming_strings");
596 if (!std_strings)
597 std_strings = "off";
598
599 /* Set the role if requested */
600 if (use_role)
601 {
603
604 appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role));
605 executeCommand(conn, query->data);
606 destroyPQExpBuffer(query);
607 }
608
609 /* Force quoting of all identifiers if requested. */
611 executeCommand(conn, "SET quote_all_identifiers = true");
612
613 fprintf(OPF, "--\n-- PostgreSQL database cluster dump\n--\n\n");
614 if (verbose)
615 dumpTimestamp("Started on");
616
617 /*
618 * We used to emit \connect postgres here, but that served no purpose
619 * other than to break things for installations without a postgres
620 * database. Everything we're restoring here is a global, so whichever
621 * database we're connected to at the moment is fine.
622 */
623
624 /* Restore will need to write to the target cluster */
625 fprintf(OPF, "SET default_transaction_read_only = off;\n\n");
626
627 /* Replicate encoding and std_strings in output */
628 fprintf(OPF, "SET client_encoding = '%s';\n",
630 fprintf(OPF, "SET standard_conforming_strings = %s;\n", std_strings);
631 if (strcmp(std_strings, "off") == 0)
632 fprintf(OPF, "SET escape_string_warning = off;\n");
633 fprintf(OPF, "\n");
634
635 if (!data_only)
636 {
637 /*
638 * If asked to --clean, do that first. We can avoid detailed
639 * dependency analysis because databases never depend on each other,
640 * and tablespaces never depend on each other. Roles could have
641 * grants to each other, but DROP ROLE will clean those up silently.
642 */
643 if (output_clean)
644 {
645 if (!globals_only && !roles_only && !tablespaces_only)
646 dropDBs(conn);
647
648 if (!roles_only && !no_tablespaces)
650
651 if (!tablespaces_only)
653 }
654
655 /*
656 * Now create objects as requested. Be careful that option logic here
657 * is the same as for drops above.
658 */
659 if (!tablespaces_only)
660 {
661 /* Dump roles (users) */
663
664 /* Dump role memberships */
666
667 /* Dump role GUC privileges */
668 if (server_version >= 150000 && !skip_acls)
670 }
671
672 /* Dump tablespaces */
673 if (!roles_only && !no_tablespaces)
675 }
676
677 if (!globals_only && !roles_only && !tablespaces_only)
678 dumpDatabases(conn, archDumpFormat);
679
680 PQfinish(conn);
681
682 if (verbose)
683 dumpTimestamp("Completed on");
684 fprintf(OPF, "--\n-- PostgreSQL database cluster dump complete\n--\n\n");
685
686 if (filename)
687 {
688 fclose(OPF);
689
690 /* sync the resulting file, errors are not fatal */
691 if (dosync && (archDumpFormat == archNull))
692 (void) fsync_fname(filename, false);
693 }
694
695 exit_nicely(0);
696}
697
698
699static void
700help(void)
701{
702 printf(_("%s extracts a PostgreSQL database cluster based on specified dump format.\n\n"), progname);
703 printf(_("Usage:\n"));
704 printf(_(" %s [OPTION]...\n"), progname);
705
706 printf(_("\nGeneral options:\n"));
707 printf(_(" -f, --file=FILENAME output file name\n"));
708 printf(_(" -F, --format=c|d|t|p output file format (custom, directory, tar,\n"
709 " plain text (default))\n"));
710 printf(_(" -v, --verbose verbose mode\n"));
711 printf(_(" -V, --version output version information, then exit\n"));
712 printf(_(" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n"));
713 printf(_(" -?, --help show this help, then exit\n"));
714 printf(_("\nOptions controlling the output content:\n"));
715 printf(_(" -a, --data-only dump only the data, not the schema or statistics\n"));
716 printf(_(" -c, --clean clean (drop) databases before recreating\n"));
717 printf(_(" -E, --encoding=ENCODING dump the data in encoding ENCODING\n"));
718 printf(_(" -g, --globals-only dump only global objects, no databases\n"));
719 printf(_(" -O, --no-owner skip restoration of object ownership\n"));
720 printf(_(" -r, --roles-only dump only roles, no databases or tablespaces\n"));
721 printf(_(" -s, --schema-only dump only the schema, no data or statistics\n"));
722 printf(_(" -S, --superuser=NAME superuser user name to use in the dump\n"));
723 printf(_(" -t, --tablespaces-only dump only tablespaces, no databases or roles\n"));
724 printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
725 printf(_(" --binary-upgrade for use by upgrade utilities only\n"));
726 printf(_(" --column-inserts dump data as INSERT commands with column names\n"));
727 printf(_(" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n"));
728 printf(_(" --disable-triggers disable triggers during data-only restore\n"));
729 printf(_(" --exclude-database=PATTERN exclude databases whose name matches PATTERN\n"));
730 printf(_(" --extra-float-digits=NUM override default setting for extra_float_digits\n"));
731 printf(_(" --filter=FILENAME exclude databases based on expressions in FILENAME\n"));
732 printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
733 printf(_(" --inserts dump data as INSERT commands, rather than COPY\n"));
734 printf(_(" --load-via-partition-root load partitions via the root table\n"));
735 printf(_(" --no-comments do not dump comment commands\n"));
736 printf(_(" --no-data do not dump data\n"));
737 printf(_(" --no-policies do not dump row security policies\n"));
738 printf(_(" --no-publications do not dump publications\n"));
739 printf(_(" --no-role-passwords do not dump passwords for roles\n"));
740 printf(_(" --no-schema do not dump schema\n"));
741 printf(_(" --no-security-labels do not dump security label assignments\n"));
742 printf(_(" --no-statistics do not dump statistics\n"));
743 printf(_(" --no-subscriptions do not dump subscriptions\n"));
744 printf(_(" --no-sync do not wait for changes to be written safely to disk\n"));
745 printf(_(" --no-table-access-method do not dump table access methods\n"));
746 printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
747 printf(_(" --no-toast-compression do not dump TOAST compression methods\n"));
748 printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
749 printf(_(" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n"));
750 printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
751 printf(_(" --rows-per-insert=NROWS number of rows per INSERT; implies --inserts\n"));
752 printf(_(" --sequence-data include sequence data in dump\n"));
753 printf(_(" --statistics-only dump only the statistics, not schema or data\n"));
754 printf(_(" --use-set-session-authorization\n"
755 " use SET SESSION AUTHORIZATION commands instead of\n"
756 " ALTER OWNER commands to set ownership\n"));
757 printf(_(" --with-data dump the data\n"));
758 printf(_(" --with-schema dump the schema\n"));
759 printf(_(" --with-statistics dump the statistics\n"));
760
761 printf(_("\nConnection options:\n"));
762 printf(_(" -d, --dbname=CONNSTR connect using connection string\n"));
763 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
764 printf(_(" -l, --database=DBNAME alternative default database\n"));
765 printf(_(" -p, --port=PORT database server port number\n"));
766 printf(_(" -U, --username=NAME connect as specified database user\n"));
767 printf(_(" -w, --no-password never prompt for password\n"));
768 printf(_(" -W, --password force password prompt (should happen automatically)\n"));
769 printf(_(" --role=ROLENAME do SET ROLE before dump\n"));
770
771 printf(_("\nIf -f/--file is not used, then the SQL script will be written to the standard\n"
772 "output.\n\n"));
773 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
774 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
775}
776
777
778/*
779 * Drop roles
780 */
781static void
783{
785 PGresult *res;
786 int i_rolname;
787 int i;
788
789 if (server_version >= 90600)
791 "SELECT rolname "
792 "FROM %s "
793 "WHERE rolname !~ '^pg_' "
794 "ORDER BY 1", role_catalog);
795 else
797 "SELECT rolname "
798 "FROM %s "
799 "ORDER BY 1", role_catalog);
800
801 res = executeQuery(conn, buf->data);
802
803 i_rolname = PQfnumber(res, "rolname");
804
805 if (PQntuples(res) > 0)
806 fprintf(OPF, "--\n-- Drop roles\n--\n\n");
807
808 for (i = 0; i < PQntuples(res); i++)
809 {
810 const char *rolename;
811
812 rolename = PQgetvalue(res, i, i_rolname);
813
814 fprintf(OPF, "DROP ROLE %s%s;\n",
815 if_exists ? "IF EXISTS " : "",
816 fmtId(rolename));
817 }
818
819 PQclear(res);
821
822 fprintf(OPF, "\n\n");
823}
824
825/*
826 * Dump roles
827 */
828static void
830{
832 PGresult *res;
833 int i_oid,
834 i_rolname,
835 i_rolsuper,
836 i_rolinherit,
837 i_rolcreaterole,
838 i_rolcreatedb,
839 i_rolcanlogin,
840 i_rolconnlimit,
841 i_rolpassword,
842 i_rolvaliduntil,
843 i_rolreplication,
844 i_rolbypassrls,
845 i_rolcomment,
846 i_is_current_user;
847 int i;
848
849 /*
850 * Notes: rolconfig is dumped later, and pg_authid must be used for
851 * extracting rolcomment regardless of role_catalog.
852 */
853 if (server_version >= 90600)
855 "SELECT oid, rolname, rolsuper, rolinherit, "
856 "rolcreaterole, rolcreatedb, "
857 "rolcanlogin, rolconnlimit, rolpassword, "
858 "rolvaliduntil, rolreplication, rolbypassrls, "
859 "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, "
860 "rolname = current_user AS is_current_user "
861 "FROM %s "
862 "WHERE rolname !~ '^pg_' "
863 "ORDER BY 2", role_catalog);
864 else if (server_version >= 90500)
866 "SELECT oid, rolname, rolsuper, rolinherit, "
867 "rolcreaterole, rolcreatedb, "
868 "rolcanlogin, rolconnlimit, rolpassword, "
869 "rolvaliduntil, rolreplication, rolbypassrls, "
870 "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, "
871 "rolname = current_user AS is_current_user "
872 "FROM %s "
873 "ORDER BY 2", role_catalog);
874 else
876 "SELECT oid, rolname, rolsuper, rolinherit, "
877 "rolcreaterole, rolcreatedb, "
878 "rolcanlogin, rolconnlimit, rolpassword, "
879 "rolvaliduntil, rolreplication, "
880 "false as rolbypassrls, "
881 "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, "
882 "rolname = current_user AS is_current_user "
883 "FROM %s "
884 "ORDER BY 2", role_catalog);
885
886 res = executeQuery(conn, buf->data);
887
888 i_oid = PQfnumber(res, "oid");
889 i_rolname = PQfnumber(res, "rolname");
890 i_rolsuper = PQfnumber(res, "rolsuper");
891 i_rolinherit = PQfnumber(res, "rolinherit");
892 i_rolcreaterole = PQfnumber(res, "rolcreaterole");
893 i_rolcreatedb = PQfnumber(res, "rolcreatedb");
894 i_rolcanlogin = PQfnumber(res, "rolcanlogin");
895 i_rolconnlimit = PQfnumber(res, "rolconnlimit");
896 i_rolpassword = PQfnumber(res, "rolpassword");
897 i_rolvaliduntil = PQfnumber(res, "rolvaliduntil");
898 i_rolreplication = PQfnumber(res, "rolreplication");
899 i_rolbypassrls = PQfnumber(res, "rolbypassrls");
900 i_rolcomment = PQfnumber(res, "rolcomment");
901 i_is_current_user = PQfnumber(res, "is_current_user");
902
903 if (PQntuples(res) > 0)
904 fprintf(OPF, "--\n-- Roles\n--\n\n");
905
906 for (i = 0; i < PQntuples(res); i++)
907 {
908 const char *rolename;
909 Oid auth_oid;
910
911 auth_oid = atooid(PQgetvalue(res, i, i_oid));
912 rolename = PQgetvalue(res, i, i_rolname);
913
914 if (strncmp(rolename, "pg_", 3) == 0)
915 {
916 pg_log_warning("role name starting with \"pg_\" skipped (%s)",
917 rolename);
918 continue;
919 }
920
922
923 if (binary_upgrade)
924 {
925 appendPQExpBufferStr(buf, "\n-- For binary upgrade, must preserve pg_authid.oid\n");
927 "SELECT pg_catalog.binary_upgrade_set_next_pg_authid_oid('%u'::pg_catalog.oid);\n\n",
928 auth_oid);
929 }
930
931 /*
932 * We dump CREATE ROLE followed by ALTER ROLE to ensure that the role
933 * will acquire the right properties even if it already exists (ie, it
934 * won't hurt for the CREATE to fail). This is particularly important
935 * for the role we are connected as, since even with --clean we will
936 * have failed to drop it. binary_upgrade cannot generate any errors,
937 * so we assume the current role is already created.
938 */
939 if (!binary_upgrade ||
940 strcmp(PQgetvalue(res, i, i_is_current_user), "f") == 0)
941 appendPQExpBuffer(buf, "CREATE ROLE %s;\n", fmtId(rolename));
942 appendPQExpBuffer(buf, "ALTER ROLE %s WITH", fmtId(rolename));
943
944 if (strcmp(PQgetvalue(res, i, i_rolsuper), "t") == 0)
945 appendPQExpBufferStr(buf, " SUPERUSER");
946 else
947 appendPQExpBufferStr(buf, " NOSUPERUSER");
948
949 if (strcmp(PQgetvalue(res, i, i_rolinherit), "t") == 0)
950 appendPQExpBufferStr(buf, " INHERIT");
951 else
952 appendPQExpBufferStr(buf, " NOINHERIT");
953
954 if (strcmp(PQgetvalue(res, i, i_rolcreaterole), "t") == 0)
955 appendPQExpBufferStr(buf, " CREATEROLE");
956 else
957 appendPQExpBufferStr(buf, " NOCREATEROLE");
958
959 if (strcmp(PQgetvalue(res, i, i_rolcreatedb), "t") == 0)
960 appendPQExpBufferStr(buf, " CREATEDB");
961 else
962 appendPQExpBufferStr(buf, " NOCREATEDB");
963
964 if (strcmp(PQgetvalue(res, i, i_rolcanlogin), "t") == 0)
965 appendPQExpBufferStr(buf, " LOGIN");
966 else
967 appendPQExpBufferStr(buf, " NOLOGIN");
968
969 if (strcmp(PQgetvalue(res, i, i_rolreplication), "t") == 0)
970 appendPQExpBufferStr(buf, " REPLICATION");
971 else
972 appendPQExpBufferStr(buf, " NOREPLICATION");
973
974 if (strcmp(PQgetvalue(res, i, i_rolbypassrls), "t") == 0)
975 appendPQExpBufferStr(buf, " BYPASSRLS");
976 else
977 appendPQExpBufferStr(buf, " NOBYPASSRLS");
978
979 if (strcmp(PQgetvalue(res, i, i_rolconnlimit), "-1") != 0)
980 appendPQExpBuffer(buf, " CONNECTION LIMIT %s",
981 PQgetvalue(res, i, i_rolconnlimit));
982
983
984 if (!PQgetisnull(res, i, i_rolpassword) && !no_role_passwords)
985 {
986 appendPQExpBufferStr(buf, " PASSWORD ");
987 appendStringLiteralConn(buf, PQgetvalue(res, i, i_rolpassword), conn);
988 }
989
990 if (!PQgetisnull(res, i, i_rolvaliduntil))
991 appendPQExpBuffer(buf, " VALID UNTIL '%s'",
992 PQgetvalue(res, i, i_rolvaliduntil));
993
995
996 if (!no_comments && !PQgetisnull(res, i, i_rolcomment))
997 {
998 appendPQExpBuffer(buf, "COMMENT ON ROLE %s IS ", fmtId(rolename));
999 appendStringLiteralConn(buf, PQgetvalue(res, i, i_rolcomment), conn);
1000 appendPQExpBufferStr(buf, ";\n");
1001 }
1002
1003 if (!no_security_labels)
1004 buildShSecLabels(conn, "pg_authid", auth_oid,
1005 "ROLE", rolename,
1006 buf);
1007
1008 fprintf(OPF, "%s", buf->data);
1009 }
1010
1011 /*
1012 * Dump configuration settings for roles after all roles have been dumped.
1013 * We do it this way because config settings for roles could mention the
1014 * names of other roles.
1015 */
1016 for (i = 0; i < PQntuples(res); i++)
1017 dumpUserConfig(conn, PQgetvalue(res, i, i_rolname));
1018
1019 PQclear(res);
1020
1021 fprintf(OPF, "\n\n");
1022
1024}
1025
1026
1027/*
1028 * Dump role memberships.
1029 *
1030 * Note: we expect dumpRoles already created all the roles, but there is
1031 * no membership yet.
1032 */
1033static void
1035{
1037 PQExpBuffer optbuf = createPQExpBuffer();
1038 PGresult *res;
1039 int start = 0,
1040 end,
1041 total;
1042 bool dump_grantors;
1043 bool dump_grant_options;
1044 int i_role;
1045 int i_member;
1046 int i_grantor;
1047 int i_roleid;
1048 int i_memberid;
1049 int i_grantorid;
1050 int i_admin_option;
1051 int i_inherit_option;
1052 int i_set_option;
1053
1054 /*
1055 * Previous versions of PostgreSQL didn't used to track the grantor very
1056 * carefully in the backend, and the grantor could be any user even if
1057 * they didn't have ADMIN OPTION on the role, or a user that no longer
1058 * existed. To avoid dump and restore failures, don't dump the grantor
1059 * when talking to an old server version.
1060 *
1061 * Also, in older versions the roleid and/or member could be role OIDs
1062 * that no longer exist. If we find such cases, print a warning and skip
1063 * the entry.
1064 */
1065 dump_grantors = (PQserverVersion(conn) >= 160000);
1066
1067 /*
1068 * Previous versions of PostgreSQL also did not have grant-level options.
1069 */
1070 dump_grant_options = (server_version >= 160000);
1071
1072 /* Generate and execute query. */
1073 printfPQExpBuffer(buf, "SELECT ur.rolname AS role, "
1074 "um.rolname AS member, "
1075 "ug.rolname AS grantor, "
1076 "a.roleid AS roleid, "
1077 "a.member AS memberid, "
1078 "a.grantor AS grantorid, "
1079 "a.admin_option");
1080 if (dump_grant_options)
1081 appendPQExpBufferStr(buf, ", a.inherit_option, a.set_option");
1082 appendPQExpBuffer(buf, " FROM pg_auth_members a "
1083 "LEFT JOIN %s ur on ur.oid = a.roleid "
1084 "LEFT JOIN %s um on um.oid = a.member "
1085 "LEFT JOIN %s ug on ug.oid = a.grantor "
1086 "WHERE NOT (ur.rolname ~ '^pg_' AND um.rolname ~ '^pg_')"
1087 "ORDER BY 1,2,3", role_catalog, role_catalog, role_catalog);
1088 res = executeQuery(conn, buf->data);
1089 i_role = PQfnumber(res, "role");
1090 i_member = PQfnumber(res, "member");
1091 i_grantor = PQfnumber(res, "grantor");
1092 i_roleid = PQfnumber(res, "roleid");
1093 i_memberid = PQfnumber(res, "memberid");
1094 i_grantorid = PQfnumber(res, "grantorid");
1095 i_admin_option = PQfnumber(res, "admin_option");
1096 i_inherit_option = PQfnumber(res, "inherit_option");
1097 i_set_option = PQfnumber(res, "set_option");
1098
1099 if (PQntuples(res) > 0)
1100 fprintf(OPF, "--\n-- Role memberships\n--\n\n");
1101
1102 /*
1103 * We can't dump these GRANT commands in arbitrary order, because a role
1104 * that is named as a grantor must already have ADMIN OPTION on the role
1105 * for which it is granting permissions, except for the bootstrap
1106 * superuser, who can always be named as the grantor.
1107 *
1108 * We handle this by considering these grants role by role. For each role,
1109 * we initially consider the only allowable grantor to be the bootstrap
1110 * superuser. Every time we grant ADMIN OPTION on the role to some user,
1111 * that user also becomes an allowable grantor. We make repeated passes
1112 * over the grants for the role, each time dumping those whose grantors
1113 * are allowable and which we haven't done yet. Eventually this should let
1114 * us dump all the grants.
1115 */
1116 total = PQntuples(res);
1117 while (start < total)
1118 {
1119 char *role = PQgetvalue(res, start, i_role);
1120 int i;
1121 bool *done;
1122 int remaining;
1123 int prev_remaining = 0;
1124 rolename_hash *ht;
1125
1126 /* If we hit a null roleid, we're done (nulls sort to the end). */
1127 if (PQgetisnull(res, start, i_role))
1128 {
1129 /* translator: %s represents a numeric role OID */
1130 pg_log_warning("found orphaned pg_auth_members entry for role %s",
1131 PQgetvalue(res, start, i_roleid));
1132 break;
1133 }
1134
1135 /* All memberships for a single role should be adjacent. */
1136 for (end = start; end < total; ++end)
1137 {
1138 char *otherrole;
1139
1140 otherrole = PQgetvalue(res, end, i_role);
1141 if (strcmp(role, otherrole) != 0)
1142 break;
1143 }
1144
1145 remaining = end - start;
1146 done = pg_malloc0(remaining * sizeof(bool));
1147 ht = rolename_create(remaining, NULL);
1148
1149 /*
1150 * Make repeated passes over the grants for this role until all have
1151 * been dumped.
1152 */
1153 while (remaining > 0)
1154 {
1155 /*
1156 * We should make progress on every iteration, because a notional
1157 * graph whose vertices are grants and whose edges point from
1158 * grantors to members should be connected and acyclic. If we fail
1159 * to make progress, either we or the server have messed up.
1160 */
1161 if (remaining == prev_remaining)
1162 {
1163 pg_log_error("could not find a legal dump ordering for memberships in role \"%s\"",
1164 role);
1165 PQfinish(conn);
1166 exit_nicely(1);
1167 }
1168 prev_remaining = remaining;
1169
1170 /* Make one pass over the grants for this role. */
1171 for (i = start; i < end; ++i)
1172 {
1173 char *member;
1174 char *admin_option;
1175 char *grantorid;
1176 char *grantor;
1177 char *set_option = "true";
1178 bool found;
1179
1180 /* If we already did this grant, don't do it again. */
1181 if (done[i - start])
1182 continue;
1183
1184 /* Complain about, then ignore, entries with orphaned OIDs. */
1185 if (PQgetisnull(res, i, i_member))
1186 {
1187 /* translator: %s represents a numeric role OID */
1188 pg_log_warning("found orphaned pg_auth_members entry for role %s",
1189 PQgetvalue(res, i, i_memberid));
1190 done[i - start] = true;
1191 --remaining;
1192 continue;
1193 }
1194 if (PQgetisnull(res, i, i_grantor))
1195 {
1196 /* translator: %s represents a numeric role OID */
1197 pg_log_warning("found orphaned pg_auth_members entry for role %s",
1198 PQgetvalue(res, i, i_grantorid));
1199 done[i - start] = true;
1200 --remaining;
1201 continue;
1202 }
1203
1204 member = PQgetvalue(res, i, i_member);
1205 grantor = PQgetvalue(res, i, i_grantor);
1206 grantorid = PQgetvalue(res, i, i_grantorid);
1207 admin_option = PQgetvalue(res, i, i_admin_option);
1208 if (dump_grant_options)
1209 set_option = PQgetvalue(res, i, i_set_option);
1210
1211 /*
1212 * If we're not dumping grantors or if the grantor is the
1213 * bootstrap superuser, it's fine to dump this now. Otherwise,
1214 * it's got to be someone who has already been granted ADMIN
1215 * OPTION.
1216 */
1217 if (dump_grantors &&
1218 atooid(grantorid) != BOOTSTRAP_SUPERUSERID &&
1219 rolename_lookup(ht, grantor) == NULL)
1220 continue;
1221
1222 /* Remember that we did this so that we don't do it again. */
1223 done[i - start] = true;
1224 --remaining;
1225
1226 /*
1227 * If ADMIN OPTION is being granted, remember that grants
1228 * listing this member as the grantor can now be dumped.
1229 */
1230 if (*admin_option == 't')
1231 rolename_insert(ht, member, &found);
1232
1233 /* Generate the actual GRANT statement. */
1234 resetPQExpBuffer(optbuf);
1235 fprintf(OPF, "GRANT %s", fmtId(role));
1236 fprintf(OPF, " TO %s", fmtId(member));
1237 if (*admin_option == 't')
1238 appendPQExpBufferStr(optbuf, "ADMIN OPTION");
1239 if (dump_grant_options)
1240 {
1241 char *inherit_option;
1242
1243 if (optbuf->data[0] != '\0')
1244 appendPQExpBufferStr(optbuf, ", ");
1245 inherit_option = PQgetvalue(res, i, i_inherit_option);
1246 appendPQExpBuffer(optbuf, "INHERIT %s",
1247 *inherit_option == 't' ?
1248 "TRUE" : "FALSE");
1249 }
1250 if (*set_option != 't')
1251 {
1252 if (optbuf->data[0] != '\0')
1253 appendPQExpBufferStr(optbuf, ", ");
1254 appendPQExpBufferStr(optbuf, "SET FALSE");
1255 }
1256 if (optbuf->data[0] != '\0')
1257 fprintf(OPF, " WITH %s", optbuf->data);
1258 if (dump_grantors)
1259 fprintf(OPF, " GRANTED BY %s", fmtId(grantor));
1260 fprintf(OPF, ";\n");
1261 }
1262 }
1263
1264 rolename_destroy(ht);
1265 pg_free(done);
1266 start = end;
1267 }
1268
1269 PQclear(res);
1271
1272 fprintf(OPF, "\n\n");
1273}
1274
1275
1276/*
1277 * Dump role configuration parameter privileges. This code is used for 15.0
1278 * and later servers.
1279 *
1280 * Note: we expect dumpRoles already created all the roles, but there are
1281 * no per-role configuration parameter privileges yet.
1282 */
1283static void
1285{
1286 PGresult *res;
1287 int i;
1288
1289 /*
1290 * Get all parameters that have non-default acls defined.
1291 */
1292 res = executeQuery(conn, "SELECT parname, "
1293 "pg_catalog.pg_get_userbyid(" CppAsString2(BOOTSTRAP_SUPERUSERID) ") AS parowner, "
1294 "paracl, "
1295 "pg_catalog.acldefault('p', " CppAsString2(BOOTSTRAP_SUPERUSERID) ") AS acldefault "
1296 "FROM pg_catalog.pg_parameter_acl "
1297 "ORDER BY 1");
1298
1299 if (PQntuples(res) > 0)
1300 fprintf(OPF, "--\n-- Role privileges on configuration parameters\n--\n\n");
1301
1302 for (i = 0; i < PQntuples(res); i++)
1303 {
1305 char *parname = PQgetvalue(res, i, 0);
1306 char *parowner = PQgetvalue(res, i, 1);
1307 char *paracl = PQgetvalue(res, i, 2);
1308 char *acldefault = PQgetvalue(res, i, 3);
1309 char *fparname;
1310
1311 /* needed for buildACLCommands() */
1312 fparname = pg_strdup(fmtId(parname));
1313
1314 if (!buildACLCommands(fparname, NULL, NULL, "PARAMETER",
1315 paracl, acldefault,
1316 parowner, "", server_version, buf))
1317 {
1318 pg_log_error("could not parse ACL list (%s) for parameter \"%s\"",
1319 paracl, parname);
1320 PQfinish(conn);
1321 exit_nicely(1);
1322 }
1323
1324 fprintf(OPF, "%s", buf->data);
1325
1326 free(fparname);
1328 }
1329
1330 PQclear(res);
1331 fprintf(OPF, "\n\n");
1332}
1333
1334
1335/*
1336 * Drop tablespaces.
1337 */
1338static void
1340{
1341 PGresult *res;
1342 int i;
1343
1344 /*
1345 * Get all tablespaces except built-in ones (which we assume are named
1346 * pg_xxx)
1347 */
1348 res = executeQuery(conn, "SELECT spcname "
1349 "FROM pg_catalog.pg_tablespace "
1350 "WHERE spcname !~ '^pg_' "
1351 "ORDER BY 1");
1352
1353 if (PQntuples(res) > 0)
1354 fprintf(OPF, "--\n-- Drop tablespaces\n--\n\n");
1355
1356 for (i = 0; i < PQntuples(res); i++)
1357 {
1358 char *spcname = PQgetvalue(res, i, 0);
1359
1360 fprintf(OPF, "DROP TABLESPACE %s%s;\n",
1361 if_exists ? "IF EXISTS " : "",
1362 fmtId(spcname));
1363 }
1364
1365 PQclear(res);
1366
1367 fprintf(OPF, "\n\n");
1368}
1369
1370/*
1371 * Dump tablespaces.
1372 */
1373static void
1375{
1376 PGresult *res;
1377 int i;
1378
1379 /*
1380 * Get all tablespaces except built-in ones (which we assume are named
1381 * pg_xxx)
1382 */
1383 res = executeQuery(conn, "SELECT oid, spcname, "
1384 "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1385 "pg_catalog.pg_tablespace_location(oid), "
1386 "spcacl, acldefault('t', spcowner) AS acldefault, "
1387 "array_to_string(spcoptions, ', '),"
1388 "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1389 "FROM pg_catalog.pg_tablespace "
1390 "WHERE spcname !~ '^pg_' "
1391 "ORDER BY 1");
1392
1393 if (PQntuples(res) > 0)
1394 fprintf(OPF, "--\n-- Tablespaces\n--\n\n");
1395
1396 for (i = 0; i < PQntuples(res); i++)
1397 {
1399 Oid spcoid = atooid(PQgetvalue(res, i, 0));
1400 char *spcname = PQgetvalue(res, i, 1);
1401 char *spcowner = PQgetvalue(res, i, 2);
1402 char *spclocation = PQgetvalue(res, i, 3);
1403 char *spcacl = PQgetvalue(res, i, 4);
1404 char *acldefault = PQgetvalue(res, i, 5);
1405 char *spcoptions = PQgetvalue(res, i, 6);
1406 char *spccomment = PQgetvalue(res, i, 7);
1407 char *fspcname;
1408
1409 /* needed for buildACLCommands() */
1410 fspcname = pg_strdup(fmtId(spcname));
1411
1412 if (binary_upgrade)
1413 {
1414 appendPQExpBufferStr(buf, "\n-- For binary upgrade, must preserve pg_tablespace oid\n");
1415 appendPQExpBuffer(buf, "SELECT pg_catalog.binary_upgrade_set_next_pg_tablespace_oid('%u'::pg_catalog.oid);\n", spcoid);
1416 }
1417
1418 appendPQExpBuffer(buf, "CREATE TABLESPACE %s", fspcname);
1419 appendPQExpBuffer(buf, " OWNER %s", fmtId(spcowner));
1420
1421 appendPQExpBufferStr(buf, " LOCATION ");
1422
1423 /*
1424 * In-place tablespaces use a relative path, and need to be dumped
1425 * with an empty string as location.
1426 */
1427 if (is_absolute_path(spclocation))
1428 appendStringLiteralConn(buf, spclocation, conn);
1429 else
1431
1432 appendPQExpBufferStr(buf, ";\n");
1433
1434 if (spcoptions && spcoptions[0] != '\0')
1435 appendPQExpBuffer(buf, "ALTER TABLESPACE %s SET (%s);\n",
1436 fspcname, spcoptions);
1437
1438 /* tablespaces can't have initprivs */
1439
1440 if (!skip_acls &&
1441 !buildACLCommands(fspcname, NULL, NULL, "TABLESPACE",
1442 spcacl, acldefault,
1443 spcowner, "", server_version, buf))
1444 {
1445 pg_log_error("could not parse ACL list (%s) for tablespace \"%s\"",
1446 spcacl, spcname);
1447 PQfinish(conn);
1448 exit_nicely(1);
1449 }
1450
1451 if (!no_comments && spccomment && spccomment[0] != '\0')
1452 {
1453 appendPQExpBuffer(buf, "COMMENT ON TABLESPACE %s IS ", fspcname);
1454 appendStringLiteralConn(buf, spccomment, conn);
1455 appendPQExpBufferStr(buf, ";\n");
1456 }
1457
1458 if (!no_security_labels)
1459 buildShSecLabels(conn, "pg_tablespace", spcoid,
1460 "TABLESPACE", spcname,
1461 buf);
1462
1463 fprintf(OPF, "%s", buf->data);
1464
1465 free(fspcname);
1467 }
1468
1469 PQclear(res);
1470 fprintf(OPF, "\n\n");
1471}
1472
1473
1474/*
1475 * Dump commands to drop each database.
1476 */
1477static void
1479{
1480 PGresult *res;
1481 int i;
1482
1483 /*
1484 * Skip databases marked not datallowconn, since we'd be unable to connect
1485 * to them anyway. This must agree with dumpDatabases().
1486 */
1487 res = executeQuery(conn,
1488 "SELECT datname "
1489 "FROM pg_database d "
1490 "WHERE datallowconn AND datconnlimit != -2 "
1491 "ORDER BY datname");
1492
1493 if (PQntuples(res) > 0)
1494 fprintf(OPF, "--\n-- Drop databases (except postgres and template1)\n--\n\n");
1495
1496 for (i = 0; i < PQntuples(res); i++)
1497 {
1498 char *dbname = PQgetvalue(res, i, 0);
1499
1500 /*
1501 * Skip "postgres" and "template1"; dumpDatabases() will deal with
1502 * them specially. Also, be sure to skip "template0", even if for
1503 * some reason it's not marked !datallowconn.
1504 */
1505 if (strcmp(dbname, "template1") != 0 &&
1506 strcmp(dbname, "template0") != 0 &&
1507 strcmp(dbname, "postgres") != 0)
1508 {
1509 fprintf(OPF, "DROP DATABASE %s%s;\n",
1510 if_exists ? "IF EXISTS " : "",
1511 fmtId(dbname));
1512 }
1513 }
1514
1515 PQclear(res);
1516
1517 fprintf(OPF, "\n\n");
1518}
1519
1520
1521/*
1522 * Dump user-specific configuration
1523 */
1524static void
1526{
1528 PGresult *res;
1529 static bool header_done = false;
1530
1531 printfPQExpBuffer(buf, "SELECT unnest(setconfig) FROM pg_db_role_setting "
1532 "WHERE setdatabase = 0 AND setrole = "
1533 "(SELECT oid FROM %s WHERE rolname = ",
1534 role_catalog);
1537
1538 res = executeQuery(conn, buf->data);
1539
1540 if (PQntuples(res) > 0)
1541 {
1542 if (!header_done)
1543 fprintf(OPF, "\n--\n-- User Configurations\n--\n");
1544 header_done = true;
1545
1546 fprintf(OPF, "\n--\n-- User Config \"%s\"\n--\n\n", username);
1547 }
1548
1549 for (int i = 0; i < PQntuples(res); i++)
1550 {
1553 "ROLE", username, NULL, NULL,
1554 buf);
1555 fprintf(OPF, "%s", buf->data);
1556 }
1557
1558 PQclear(res);
1559
1561}
1562
1563/*
1564 * Find a list of database names that match the given patterns.
1565 * See also expand_table_name_patterns() in pg_dump.c
1566 */
1567static void
1569 SimpleStringList *patterns,
1570 SimpleStringList *names)
1571{
1572 PQExpBuffer query;
1573 PGresult *res;
1574
1575 if (patterns->head == NULL)
1576 return; /* nothing to do */
1577
1578 query = createPQExpBuffer();
1579
1580 /*
1581 * The loop below runs multiple SELECTs, which might sometimes result in
1582 * duplicate entries in the name list, but we don't care, since all we're
1583 * going to do is test membership of the list.
1584 */
1585
1586 for (SimpleStringListCell *cell = patterns->head; cell; cell = cell->next)
1587 {
1588 int dotcnt;
1589
1591 "SELECT datname FROM pg_catalog.pg_database n\n");
1592 processSQLNamePattern(conn, query, cell->val, false,
1593 false, NULL, "datname", NULL, NULL, NULL,
1594 &dotcnt);
1595
1596 if (dotcnt > 0)
1597 {
1598 pg_log_error("improper qualified name (too many dotted names): %s",
1599 cell->val);
1600 PQfinish(conn);
1601 exit_nicely(1);
1602 }
1603
1604 res = executeQuery(conn, query->data);
1605 for (int i = 0; i < PQntuples(res); i++)
1606 {
1607 simple_string_list_append(names, PQgetvalue(res, i, 0));
1608 }
1609
1610 PQclear(res);
1611 resetPQExpBuffer(query);
1612 }
1613
1614 destroyPQExpBuffer(query);
1615}
1616
1617/*
1618 * Dump contents of databases.
1619 */
1620static void
1622{
1623 PGresult *res;
1624 int i;
1625 char db_subdir[MAXPGPATH];
1626 char dbfilepath[MAXPGPATH];
1627 FILE *map_file = NULL;
1628
1629 /*
1630 * Skip databases marked not datallowconn, since we'd be unable to connect
1631 * to them anyway. This must agree with dropDBs().
1632 *
1633 * We arrange for template1 to be processed first, then we process other
1634 * DBs in alphabetical order. If we just did them all alphabetically, we
1635 * might find ourselves trying to drop the "postgres" database while still
1636 * connected to it. This makes trying to run the restore script while
1637 * connected to "template1" a bad idea, but there's no fixed order that
1638 * doesn't have some failure mode with --clean.
1639 */
1640 res = executeQuery(conn,
1641 "SELECT datname, oid "
1642 "FROM pg_database d "
1643 "WHERE datallowconn AND datconnlimit != -2 "
1644 "ORDER BY (datname <> 'template1'), datname");
1645
1646 if (archDumpFormat == archNull && PQntuples(res) > 0)
1647 fprintf(OPF, "--\n-- Databases\n--\n\n");
1648
1649 /*
1650 * If directory/tar/custom format is specified, create a subdirectory
1651 * under the main directory and each database dump file or subdirectory
1652 * will be created in that subdirectory by pg_dump.
1653 */
1654 if (archDumpFormat != archNull)
1655 {
1656 char map_file_path[MAXPGPATH];
1657
1658 snprintf(db_subdir, MAXPGPATH, "%s/databases", filename);
1659
1660 /* Create a subdirectory with 'databases' name under main directory. */
1661 if (mkdir(db_subdir, pg_dir_create_mode) != 0)
1662 pg_fatal("could not create subdirectory \"%s\": %m", db_subdir);
1663
1664 snprintf(map_file_path, MAXPGPATH, "%s/map.dat", filename);
1665
1666 /* Create a map file (to store dboid and dbname) */
1667 map_file = fopen(map_file_path, PG_BINARY_W);
1668 if (!map_file)
1669 pg_fatal("could not open map file: %s", strerror(errno));
1670 }
1671
1672 for (i = 0; i < PQntuples(res); i++)
1673 {
1674 char *dbname = PQgetvalue(res, i, 0);
1675 char *oid = PQgetvalue(res, i, 1);
1676 const char *create_opts = "";
1677 int ret;
1678
1679 /* Skip template0, even if it's not marked !datallowconn. */
1680 if (strcmp(dbname, "template0") == 0)
1681 continue;
1682
1683 /* Skip any explicitly excluded database */
1685 {
1686 pg_log_info("excluding database \"%s\"", dbname);
1687 continue;
1688 }
1689
1690 /*
1691 * If this is not a plain format dump, then append dboid and dbname to
1692 * the map.dat file.
1693 */
1694 if (archDumpFormat != archNull)
1695 {
1696 if (archDumpFormat == archCustom)
1697 snprintf(dbfilepath, MAXPGPATH, "\"%s\"/\"%s\".dmp", db_subdir, oid);
1698 else if (archDumpFormat == archTar)
1699 snprintf(dbfilepath, MAXPGPATH, "\"%s\"/\"%s\".tar", db_subdir, oid);
1700 else
1701 snprintf(dbfilepath, MAXPGPATH, "\"%s\"/\"%s\"", db_subdir, oid);
1702
1703 /* Put one line entry for dboid and dbname in map file. */
1704 fprintf(map_file, "%s %s\n", oid, dbname);
1705 }
1706
1707 pg_log_info("dumping database \"%s\"", dbname);
1708
1709 if (archDumpFormat == archNull)
1710 fprintf(OPF, "--\n-- Database \"%s\" dump\n--\n\n", dbname);
1711
1712 /*
1713 * We assume that "template1" and "postgres" already exist in the
1714 * target installation. dropDBs() won't have removed them, for fear
1715 * of removing the DB the restore script is initially connected to. If
1716 * --clean was specified, tell pg_dump to drop and recreate them;
1717 * otherwise we'll merely restore their contents. Other databases
1718 * should simply be created.
1719 */
1720 if (strcmp(dbname, "template1") == 0 || strcmp(dbname, "postgres") == 0)
1721 {
1722 if (output_clean)
1723 create_opts = "--clean --create";
1724 /* Since pg_dump won't emit a \connect command, we must */
1725 else if (archDumpFormat == archNull)
1726 fprintf(OPF, "\\connect %s\n\n", dbname);
1727 }
1728 else
1729 create_opts = "--create";
1730
1731 if (filename)
1732 fclose(OPF);
1733
1734 ret = runPgDump(dbname, create_opts, dbfilepath, archDumpFormat);
1735 if (ret != 0)
1736 pg_fatal("pg_dump failed on database \"%s\", exiting", dbname);
1737
1738 if (filename)
1739 {
1740 char global_path[MAXPGPATH];
1741
1742 if (archDumpFormat != archNull)
1743 snprintf(global_path, MAXPGPATH, "%s/global.dat", filename);
1744 else
1745 snprintf(global_path, MAXPGPATH, "%s", filename);
1746
1747 OPF = fopen(global_path, PG_BINARY_A);
1748 if (!OPF)
1749 pg_fatal("could not re-open the output file \"%s\": %m",
1750 global_path);
1751 }
1752 }
1753
1754 /* Close map file */
1755 if (archDumpFormat != archNull)
1756 fclose(map_file);
1757
1758 PQclear(res);
1759}
1760
1761
1762
1763/*
1764 * Run pg_dump on dbname, with specified options.
1765 */
1766static int
1767runPgDump(const char *dbname, const char *create_opts, char *dbfile,
1768 ArchiveFormat archDumpFormat)
1769{
1770 PQExpBufferData connstrbuf;
1771 PQExpBufferData cmd;
1772 int ret;
1773
1774 initPQExpBuffer(&connstrbuf);
1775 initPQExpBuffer(&cmd);
1776
1777 /*
1778 * If this is not a plain format dump, then append file name and dump
1779 * format to the pg_dump command to get archive dump.
1780 */
1781 if (archDumpFormat != archNull)
1782 {
1783 printfPQExpBuffer(&cmd, "\"%s\" -f %s %s", pg_dump_bin,
1784 dbfile, create_opts);
1785
1786 if (archDumpFormat == archDirectory)
1787 appendPQExpBufferStr(&cmd, " --format=directory ");
1788 else if (archDumpFormat == archCustom)
1789 appendPQExpBufferStr(&cmd, " --format=custom ");
1790 else if (archDumpFormat == archTar)
1791 appendPQExpBufferStr(&cmd, " --format=tar ");
1792 }
1793 else
1794 {
1795 printfPQExpBuffer(&cmd, "\"%s\" %s %s", pg_dump_bin,
1796 pgdumpopts->data, create_opts);
1797
1798 /*
1799 * If we have a filename, use the undocumented plain-append pg_dump
1800 * format.
1801 */
1802 if (filename)
1803 appendPQExpBufferStr(&cmd, " -Fa ");
1804 else
1805 appendPQExpBufferStr(&cmd, " -Fp ");
1806 }
1807
1808 /*
1809 * Append the database name to the already-constructed stem of connection
1810 * string.
1811 */
1812 appendPQExpBuffer(&connstrbuf, "%s dbname=", connstr);
1813 appendConnStrVal(&connstrbuf, dbname);
1814
1815 appendShellString(&cmd, connstrbuf.data);
1816
1817 pg_log_info("running \"%s\"", cmd.data);
1818
1819 fflush(NULL);
1820
1821 ret = system(cmd.data);
1822
1823 termPQExpBuffer(&cmd);
1824 termPQExpBuffer(&connstrbuf);
1825
1826 return ret;
1827}
1828
1829/*
1830 * buildShSecLabels
1831 *
1832 * Build SECURITY LABEL command(s) for a shared object
1833 *
1834 * The caller has to provide object type and identity in two separate formats:
1835 * catalog_name (e.g., "pg_database") and object OID, as well as
1836 * type name (e.g., "DATABASE") and object name (not pre-quoted).
1837 *
1838 * The command(s) are appended to "buffer".
1839 */
1840static void
1841buildShSecLabels(PGconn *conn, const char *catalog_name, Oid objectId,
1842 const char *objtype, const char *objname,
1843 PQExpBuffer buffer)
1844{
1846 PGresult *res;
1847
1848 buildShSecLabelQuery(catalog_name, objectId, sql);
1849 res = executeQuery(conn, sql->data);
1850 emitShSecLabels(conn, res, buffer, objtype, objname);
1851
1852 PQclear(res);
1853 destroyPQExpBuffer(sql);
1854}
1855
1856/*
1857 * As above for a SQL command (which returns nothing).
1858 */
1859static void
1860executeCommand(PGconn *conn, const char *query)
1861{
1862 PGresult *res;
1863
1864 pg_log_info("executing %s", query);
1865
1866 res = PQexec(conn, query);
1867 if (!res ||
1869 {
1870 pg_log_error("query failed: %s", PQerrorMessage(conn));
1871 pg_log_error_detail("Query was: %s", query);
1872 PQfinish(conn);
1873 exit_nicely(1);
1874 }
1875
1876 PQclear(res);
1877}
1878
1879
1880/*
1881 * dumpTimestamp
1882 */
1883static void
1884dumpTimestamp(const char *msg)
1885{
1886 char buf[64];
1887 time_t now = time(NULL);
1888
1889 if (strftime(buf, sizeof(buf), PGDUMP_STRFTIME_FMT, localtime(&now)) != 0)
1890 fprintf(OPF, "-- %s %s\n\n", msg, buf);
1891}
1892
1893/*
1894 * read_dumpall_filters - retrieve database identifier patterns from file
1895 *
1896 * Parse the specified filter file for include and exclude patterns, and add
1897 * them to the relevant lists. If the filename is "-" then filters will be
1898 * read from STDIN rather than a file.
1899 *
1900 * At the moment, the only allowed filter is for database exclusion.
1901 */
1902static void
1904{
1905 FilterStateData fstate;
1906 char *objname;
1907 FilterCommandType comtype;
1908 FilterObjectType objtype;
1909
1910 filter_init(&fstate, filename, exit);
1911
1912 while (filter_read_item(&fstate, &objname, &comtype, &objtype))
1913 {
1914 if (comtype == FILTER_COMMAND_TYPE_INCLUDE)
1915 {
1916 pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
1917 "include",
1918 filter_object_type_name(objtype));
1919 exit_nicely(1);
1920 }
1921
1922 switch (objtype)
1923 {
1925 break;
1936 pg_log_filter_error(&fstate, _("unsupported filter object"));
1937 exit_nicely(1);
1938 break;
1939
1941 simple_string_list_append(pattern, objname);
1942 break;
1943 }
1944
1945 if (objname)
1946 free(objname);
1947 }
1948
1949 filter_free(&fstate);
1950}
1951
1952/*
1953 * parseDumpFormat
1954 *
1955 * This will validate dump formats.
1956 */
1957static ArchiveFormat
1959{
1960 ArchiveFormat archDumpFormat;
1961
1962 if (pg_strcasecmp(format, "c") == 0)
1963 archDumpFormat = archCustom;
1964 else if (pg_strcasecmp(format, "custom") == 0)
1965 archDumpFormat = archCustom;
1966 else if (pg_strcasecmp(format, "d") == 0)
1967 archDumpFormat = archDirectory;
1968 else if (pg_strcasecmp(format, "directory") == 0)
1969 archDumpFormat = archDirectory;
1970 else if (pg_strcasecmp(format, "p") == 0)
1971 archDumpFormat = archNull;
1972 else if (pg_strcasecmp(format, "plain") == 0)
1973 archDumpFormat = archNull;
1974 else if (pg_strcasecmp(format, "t") == 0)
1975 archDumpFormat = archTar;
1976 else if (pg_strcasecmp(format, "tar") == 0)
1977 archDumpFormat = archTar;
1978 else
1979 pg_fatal("unrecognized archive format \"%s\"; please specify \"c\", \"d\", \"p\", or \"t\"",
1980 format);
1981
1982 return archDumpFormat;
1983}
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:787
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1609
#define PG_BINARY_A
Definition: c.h:1245
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1185
#define CppAsString2(x)
Definition: c.h:363
uint32_t uint32
Definition: c.h:502
#define PG_BINARY_W
Definition: c.h:1247
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:160
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:429
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
Definition: exec.c:310
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 fprintf(file, fmt, msg)
Definition: cubescan.l:21
bool buildACLCommands(const char *name, const char *subname, const char *nspname, const char *type, const char *acls, const char *baseacls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
Definition: dumputils.c:66
void buildShSecLabelQuery(const char *catalog_name, Oid objectId, PQExpBuffer sql)
Definition: dumputils.c:640
void makeAlterConfigCommand(PGconn *conn, const char *configitem, const char *type, const char *name, const char *type2, const char *name2, PQExpBuffer buf)
Definition: dumputils.c:826
void create_or_open_dir(const char *dirname)
Definition: dumputils.c:897
void emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer, const char *objtype, const char *objname)
Definition: dumputils.c:658
#define PGDUMP_STRFTIME_FMT
Definition: dumputils.h:33
#define _(x)
Definition: elog.c:91
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:756
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:7615
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7580
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7715
void PQfinish(PGconn *conn)
Definition: fe-connect.c:5296
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7625
int PQsetClientEncoding(PGconn *conn, const char *encoding)
Definition: fe-connect.c:7723
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
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
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3589
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3901
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_malloc0(size_t size)
Definition: fe_memutils.c:53
void pg_free(void *ptr)
Definition: fe_memutils.c:105
int pg_dir_create_mode
Definition: file_perm.c:18
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_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
return str start
#define free(a)
Definition: header.h:65
int remaining
Definition: informix.c:692
static char * username
Definition: initdb.c:153
int i
Definition: isn.c:77
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:125
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
#define pg_log_error_detail(...)
Definition: logging.h:109
const char * progname
Definition: main.c:44
bool set_option
bool inherit_option
bool admin_option
enum _archiveFormat ArchiveFormat
@ archTar
Definition: pg_backup.h:43
@ archCustom
Definition: pg_backup.h:42
@ archDirectory
Definition: pg_backup.h:45
@ archNull
Definition: pg_backup.h:44
void exit_nicely(int code)
#define pg_fatal(...)
static char format
#define MAXPGPATH
int32 encoding
Definition: pg_database.h:41
static void dumpTimestamp(const char *msg)
Definition: pg_dumpall.c:1884
static int if_exists
Definition: pg_dumpall.c:94
static int on_conflict_do_nothing
Definition: pg_dumpall.c:115
int main(int argc, char *argv[])
Definition: pg_dumpall.c:130
static void dropTablespaces(PGconn *conn)
Definition: pg_dumpall.c:1339
static void expand_dbname_patterns(PGconn *conn, SimpleStringList *patterns, SimpleStringList *names)
Definition: pg_dumpall.c:1568
static int no_role_passwords
Definition: pg_dumpall.c:109
static PQExpBuffer pgdumpopts
Definition: pg_dumpall.c:83
static int statistics_only
Definition: pg_dumpall.c:116
static int no_table_access_method
Definition: pg_dumpall.c:96
static int no_unlogged_table_data
Definition: pg_dumpall.c:108
#define PG_AUTHID
Definition: pg_dumpall.c:120
static int no_policies
Definition: pg_dumpall.c:100
static int binary_upgrade
Definition: pg_dumpall.c:90
static int disable_triggers
Definition: pg_dumpall.c:93
static void dumpUserConfig(PGconn *conn, const char *username)
Definition: pg_dumpall.c:1525
static bool dosync
Definition: pg_dumpall.c:88
static const char * connstr
Definition: pg_dumpall.c:84
static SimpleStringList database_exclude_patterns
Definition: pg_dumpall.c:126
static void dumpTablespaces(PGconn *conn)
Definition: pg_dumpall.c:1374
static void dumpRoleMembership(PGconn *conn)
Definition: pg_dumpall.c:1034
static int with_data
Definition: pg_dumpall.c:110
static char pg_dump_bin[MAXPGPATH]
Definition: pg_dumpall.c:82
static SimpleStringList database_exclude_names
Definition: pg_dumpall.c:127
static FILE * OPF
Definition: pg_dumpall.c:123
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 sequence_data
Definition: pg_dumpall.c:117
static int no_security_labels
Definition: pg_dumpall.c:102
static ArchiveFormat parseDumpFormat(const char *format)
Definition: pg_dumpall.c:1958
static void dumpDatabases(PGconn *conn, ArchiveFormat archDumpFormat)
Definition: pg_dumpall.c:1621
static int disable_dollar_quoting
Definition: pg_dumpall.c:92
static void buildShSecLabels(PGconn *conn, const char *catalog_name, Oid objectId, const char *objtype, const char *objname, PQExpBuffer buffer)
Definition: pg_dumpall.c:1841
static int no_tablespaces
Definition: pg_dumpall.c:97
static int no_toast_compression
Definition: pg_dumpall.c:107
#define PG_ROLES
Definition: pg_dumpall.c:121
static int inserts
Definition: pg_dumpall.c:95
static void executeCommand(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1860
static bool verbose
Definition: pg_dumpall.c:87
static void dumpRoleGUCPrivs(PGconn *conn)
Definition: pg_dumpall.c:1284
static int no_statistics
Definition: pg_dumpall.c:105
static void dumpRoles(PGconn *conn)
Definition: pg_dumpall.c:829
static void help(void)
Definition: pg_dumpall.c:700
static int use_setsessauth
Definition: pg_dumpall.c:98
static int no_data
Definition: pg_dumpall.c:103
static int load_via_partition_root
Definition: pg_dumpall.c:114
static int column_inserts
Definition: pg_dumpall.c:91
static void read_dumpall_filters(const char *filename, SimpleStringList *pattern)
Definition: pg_dumpall.c:1903
static void dropRoles(PGconn *conn)
Definition: pg_dumpall.c:782
static void dropDBs(PGconn *conn)
Definition: pg_dumpall.c:1478
static int server_version
Definition: pg_dumpall.c:113
static char role_catalog[10]
Definition: pg_dumpall.c:119
static bool output_clean
Definition: pg_dumpall.c:85
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:124
static int with_statistics
Definition: pg_dumpall.c:112
static int runPgDump(const char *dbname, const char *create_opts, char *dbfile, ArchiveFormat archDumpFormat)
Definition: pg_dumpall.c:1767
#define PGDUMP_VERSIONSTR
Definition: pg_dumpall.c:35
static bool skip_acls
Definition: pg_dumpall.c:86
PGDLLIMPORT int optind
Definition: getopt.c:51
PGDLLIMPORT char * optarg
Definition: getopt.c:53
static char * buf
Definition: pg_test_fsync.c:72
#define pg_encoding_to_char
Definition: pg_wchar.h:630
static const char * pghost
Definition: pgbench.c:295
static const char * pgport
Definition: pgbench.c:296
#define pg_log_warning(...)
Definition: pgfnames.c:24
#define is_absolute_path(filename)
Definition: port.h:104
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define sprintf
Definition: port.h:241
#define strerror
Definition: port.h:252
#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
unsigned int Oid
Definition: postgres_ext.h:30
#define atooid(x)
Definition: postgres_ext.h:41
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:235
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:146
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:114
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
char * c
bool quote_all_identifiers
Definition: ruleutils.c:339
bool simple_string_list_member(SimpleStringList *list, const char *val)
Definition: simple_list.c:87
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
char * dbname
Definition: streamutil.c:49
PGconn * conn
Definition: streamutil.c:52
const char * fmtId(const char *rawid)
Definition: string_utils.c:248
void setFmtEncoding(int encoding)
Definition: string_utils.c:69
void appendShellString(PQExpBuffer buf, const char *str)
Definition: string_utils.c:582
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 appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:698
uint32 hashval
Definition: pg_dumpall.c:40
uint32 status
Definition: pg_dumpall.c:39
char * rolename
Definition: pg_dumpall.c:41
struct SimpleStringListCell * next
Definition: simple_list.h:34
SimpleStringListCell * head
Definition: simple_list.h:42
trivalue
Definition: vacuumlo.c:35
@ TRI_YES
Definition: vacuumlo.c:38
@ TRI_DEFAULT
Definition: vacuumlo.c:36
@ TRI_NO
Definition: vacuumlo.c:37
#define mkdir(a, b)
Definition: win32_port.h:80