Line data Source code
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"
25 : #include "common/hashfn_unstable.h"
26 : #include "common/logging.h"
27 : #include "common/string.h"
28 : #include "connectdb.h"
29 : #include "dumputils.h"
30 : #include "fe_utils/string_utils.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 :
37 : typedef struct
38 : {
39 : uint32 status;
40 : uint32 hashval;
41 : char *rolename;
42 : } RoleNameEntry;
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 :
58 : static void help(void);
59 :
60 : static void dropRoles(PGconn *conn);
61 : static void dumpRoles(PGconn *conn);
62 : static void dumpRoleMembership(PGconn *conn);
63 : static void dumpRoleGUCPrivs(PGconn *conn);
64 : static void dropTablespaces(PGconn *conn);
65 : static void dumpTablespaces(PGconn *conn);
66 : static void dropDBs(PGconn *conn);
67 : static void dumpUserConfig(PGconn *conn, const char *username);
68 : static void dumpDatabases(PGconn *conn, ArchiveFormat archDumpFormat);
69 : static void dumpTimestamp(const char *msg);
70 : static int runPgDump(const char *dbname, const char *create_opts,
71 : char *dbfile, ArchiveFormat archDumpFormat);
72 : static void buildShSecLabels(PGconn *conn,
73 : const char *catalog_name, Oid objectId,
74 : const char *objtype, const char *objname,
75 : PQExpBuffer buffer);
76 : static void executeCommand(PGconn *conn, const char *query);
77 : static void expand_dbname_patterns(PGconn *conn, SimpleStringList *patterns,
78 : SimpleStringList *names);
79 : static void read_dumpall_filters(const char *filename, SimpleStringList *pattern);
80 : static ArchiveFormat parseDumpFormat(const char *format);
81 :
82 : static char pg_dump_bin[MAXPGPATH];
83 : static PQExpBuffer pgdumpopts;
84 : static const char *connstr = "";
85 : static bool output_clean = false;
86 : static bool skip_acls = false;
87 : static bool verbose = false;
88 : static bool dosync = true;
89 :
90 : static int binary_upgrade = 0;
91 : static int column_inserts = 0;
92 : static int disable_dollar_quoting = 0;
93 : static int disable_triggers = 0;
94 : static int if_exists = 0;
95 : static int inserts = 0;
96 : static int no_table_access_method = 0;
97 : static int no_tablespaces = 0;
98 : static int use_setsessauth = 0;
99 : static int no_comments = 0;
100 : static int no_policies = 0;
101 : static int no_publications = 0;
102 : static int no_security_labels = 0;
103 : static int no_data = 0;
104 : static int no_schema = 0;
105 : static int no_statistics = 0;
106 : static int no_subscriptions = 0;
107 : static int no_toast_compression = 0;
108 : static int no_unlogged_table_data = 0;
109 : static int no_role_passwords = 0;
110 : static int with_data = 0;
111 : static int with_schema = 0;
112 : static int with_statistics = 0;
113 : static int server_version;
114 : static int load_via_partition_root = 0;
115 : static int on_conflict_do_nothing = 0;
116 : static int statistics_only = 0;
117 : static int sequence_data = 0;
118 :
119 : static char role_catalog[10];
120 : #define PG_AUTHID "pg_authid"
121 : #define PG_ROLES "pg_roles "
122 :
123 : static FILE *OPF;
124 : static char *filename = NULL;
125 :
126 : static SimpleStringList database_exclude_patterns = {NULL, NULL};
127 : static SimpleStringList database_exclude_names = {NULL, NULL};
128 :
129 : int
130 140 : main(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, "e_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 140 : char *pghost = NULL;
199 140 : char *pgport = NULL;
200 140 : char *pguser = NULL;
201 140 : char *pgdb = NULL;
202 140 : char *use_role = NULL;
203 140 : const char *dumpencoding = NULL;
204 140 : ArchiveFormat archDumpFormat = archNull;
205 140 : const char *formatName = "p";
206 140 : trivalue prompt_password = TRI_DEFAULT;
207 140 : bool data_only = false;
208 140 : bool globals_only = false;
209 140 : bool roles_only = false;
210 140 : 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 140 : pg_logging_init(argv[0]);
219 140 : pg_logging_set_level(PG_LOG_WARNING);
220 140 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
221 140 : progname = get_progname(argv[0]);
222 :
223 140 : if (argc > 1)
224 : {
225 140 : if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
226 : {
227 2 : help();
228 2 : exit_nicely(0);
229 : }
230 138 : if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
231 : {
232 34 : puts("pg_dumpall (PostgreSQL) " PG_VERSION);
233 34 : exit_nicely(0);
234 : }
235 : }
236 :
237 104 : if ((ret = find_other_exec(argv[0], "pg_dump", PGDUMP_VERSIONSTR,
238 : pg_dump_bin)) < 0)
239 : {
240 : char full_path[MAXPGPATH];
241 :
242 0 : if (find_my_exec(argv[0], full_path) < 0)
243 0 : strlcpy(full_path, progname, sizeof(full_path));
244 :
245 0 : if (ret == -1)
246 0 : 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 0 : pg_fatal("program \"%s\" was found by \"%s\" but was not the same version as %s",
250 : "pg_dump", full_path, progname);
251 : }
252 :
253 104 : pgdumpopts = createPQExpBuffer();
254 :
255 492 : while ((c = getopt_long(argc, argv, "acd:E:f:F:gh:l:Op:rsS:tU:vwWx", long_options, &optindex)) != -1)
256 : {
257 396 : switch (c)
258 : {
259 0 : case 'a':
260 0 : data_only = true;
261 0 : appendPQExpBufferStr(pgdumpopts, " -a");
262 0 : break;
263 :
264 2 : case 'c':
265 2 : output_clean = true;
266 2 : break;
267 :
268 18 : case 'd':
269 18 : connstr = pg_strdup(optarg);
270 18 : break;
271 :
272 0 : case 'E':
273 0 : dumpencoding = pg_strdup(optarg);
274 0 : appendPQExpBufferStr(pgdumpopts, " -E ");
275 0 : appendShellString(pgdumpopts, optarg);
276 0 : break;
277 :
278 80 : case 'f':
279 80 : filename = pg_strdup(optarg);
280 80 : appendPQExpBufferStr(pgdumpopts, " -f ");
281 80 : appendShellString(pgdumpopts, filename);
282 80 : break;
283 18 : case 'F':
284 18 : formatName = pg_strdup(optarg);
285 18 : break;
286 36 : case 'g':
287 36 : globals_only = true;
288 36 : break;
289 :
290 20 : case 'h':
291 20 : pghost = pg_strdup(optarg);
292 20 : break;
293 :
294 0 : case 'l':
295 0 : pgdb = pg_strdup(optarg);
296 0 : break;
297 :
298 0 : case 'O':
299 0 : appendPQExpBufferStr(pgdumpopts, " -O");
300 0 : break;
301 :
302 36 : case 'p':
303 36 : pgport = pg_strdup(optarg);
304 36 : break;
305 :
306 14 : case 'r':
307 14 : roles_only = true;
308 14 : break;
309 :
310 0 : case 's':
311 0 : appendPQExpBufferStr(pgdumpopts, " -s");
312 0 : break;
313 :
314 0 : case 'S':
315 0 : appendPQExpBufferStr(pgdumpopts, " -S ");
316 0 : appendShellString(pgdumpopts, optarg);
317 0 : break;
318 :
319 4 : case 't':
320 4 : tablespaces_only = true;
321 4 : break;
322 :
323 34 : case 'U':
324 34 : pguser = pg_strdup(optarg);
325 34 : break;
326 :
327 4 : case 'v':
328 4 : verbose = true;
329 4 : pg_logging_increase_verbosity();
330 4 : appendPQExpBufferStr(pgdumpopts, " -v");
331 4 : break;
332 :
333 0 : case 'w':
334 0 : prompt_password = TRI_NO;
335 0 : appendPQExpBufferStr(pgdumpopts, " -w");
336 0 : break;
337 :
338 0 : case 'W':
339 0 : prompt_password = TRI_YES;
340 0 : appendPQExpBufferStr(pgdumpopts, " -W");
341 0 : break;
342 :
343 0 : case 'x':
344 0 : skip_acls = true;
345 0 : appendPQExpBufferStr(pgdumpopts, " -x");
346 0 : break;
347 :
348 52 : case 0:
349 52 : break;
350 :
351 0 : case 2:
352 0 : appendPQExpBufferStr(pgdumpopts, " --lock-wait-timeout ");
353 0 : appendShellString(pgdumpopts, optarg);
354 0 : break;
355 :
356 0 : case 3:
357 0 : use_role = pg_strdup(optarg);
358 0 : appendPQExpBufferStr(pgdumpopts, " --role ");
359 0 : appendShellString(pgdumpopts, use_role);
360 0 : break;
361 :
362 54 : case 4:
363 54 : dosync = false;
364 54 : appendPQExpBufferStr(pgdumpopts, " --no-sync");
365 54 : break;
366 :
367 0 : case 5:
368 0 : appendPQExpBufferStr(pgdumpopts, " --extra-float-digits ");
369 0 : appendShellString(pgdumpopts, optarg);
370 0 : break;
371 :
372 12 : case 6:
373 12 : simple_string_list_append(&database_exclude_patterns, optarg);
374 12 : break;
375 :
376 0 : case 7:
377 0 : appendPQExpBufferStr(pgdumpopts, " --rows-per-insert ");
378 0 : appendShellString(pgdumpopts, optarg);
379 0 : break;
380 :
381 10 : case 8:
382 10 : read_dumpall_filters(optarg, &database_exclude_patterns);
383 4 : break;
384 :
385 2 : default:
386 : /* getopt_long already emitted a complaint */
387 2 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
388 2 : exit_nicely(1);
389 : }
390 : }
391 :
392 : /* Complain if any arguments remain */
393 96 : if (optind < argc)
394 : {
395 2 : pg_log_error("too many command-line arguments (first is \"%s\")",
396 : argv[optind]);
397 2 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
398 2 : exit_nicely(1);
399 : }
400 :
401 94 : if (database_exclude_patterns.head != NULL &&
402 12 : (globals_only || roles_only || tablespaces_only))
403 : {
404 4 : pg_log_error("option --exclude-database cannot be used together with -g/--globals-only, -r/--roles-only, or -t/--tablespaces-only");
405 4 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
406 4 : exit_nicely(1);
407 : }
408 :
409 : /* Make sure the user hasn't specified a mix of globals-only options */
410 90 : if (globals_only && roles_only)
411 : {
412 2 : pg_log_error("options -g/--globals-only and -r/--roles-only cannot be used together");
413 2 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
414 2 : exit_nicely(1);
415 : }
416 :
417 88 : if (globals_only && tablespaces_only)
418 : {
419 2 : pg_log_error("options -g/--globals-only and -t/--tablespaces-only cannot be used together");
420 2 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
421 2 : exit_nicely(1);
422 : }
423 :
424 86 : if (if_exists && !output_clean)
425 2 : pg_fatal("option --if-exists requires option -c/--clean");
426 :
427 84 : if (roles_only && tablespaces_only)
428 : {
429 2 : pg_log_error("options -r/--roles-only and -t/--tablespaces-only cannot be used together");
430 2 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
431 2 : exit_nicely(1);
432 : }
433 :
434 : /* Get format for dump. */
435 82 : 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 80 : if (archDumpFormat != archNull &&
442 16 : (!filename || strcmp(filename, "") == 0))
443 : {
444 0 : pg_log_error("option -F/--format=d|c|t requires option -f/--file");
445 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
446 0 : 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 : */
454 80 : if (no_role_passwords)
455 0 : sprintf(role_catalog, "%s", PG_ROLES);
456 : else
457 80 : sprintf(role_catalog, "%s", PG_AUTHID);
458 :
459 : /* Add long options to the pg_dump argument list */
460 80 : if (binary_upgrade)
461 20 : appendPQExpBufferStr(pgdumpopts, " --binary-upgrade");
462 80 : if (column_inserts)
463 0 : appendPQExpBufferStr(pgdumpopts, " --column-inserts");
464 80 : if (disable_dollar_quoting)
465 0 : appendPQExpBufferStr(pgdumpopts, " --disable-dollar-quoting");
466 80 : if (disable_triggers)
467 0 : appendPQExpBufferStr(pgdumpopts, " --disable-triggers");
468 80 : if (inserts)
469 0 : appendPQExpBufferStr(pgdumpopts, " --inserts");
470 80 : if (no_table_access_method)
471 0 : appendPQExpBufferStr(pgdumpopts, " --no-table-access-method");
472 80 : if (no_tablespaces)
473 0 : appendPQExpBufferStr(pgdumpopts, " --no-tablespaces");
474 80 : if (quote_all_identifiers)
475 20 : appendPQExpBufferStr(pgdumpopts, " --quote-all-identifiers");
476 80 : if (load_via_partition_root)
477 0 : appendPQExpBufferStr(pgdumpopts, " --load-via-partition-root");
478 80 : if (use_setsessauth)
479 0 : appendPQExpBufferStr(pgdumpopts, " --use-set-session-authorization");
480 80 : if (no_comments)
481 0 : appendPQExpBufferStr(pgdumpopts, " --no-comments");
482 80 : if (no_data)
483 0 : appendPQExpBufferStr(pgdumpopts, " --no-data");
484 80 : if (no_policies)
485 0 : appendPQExpBufferStr(pgdumpopts, " --no-policies");
486 80 : if (no_publications)
487 0 : appendPQExpBufferStr(pgdumpopts, " --no-publications");
488 80 : if (no_security_labels)
489 0 : appendPQExpBufferStr(pgdumpopts, " --no-security-labels");
490 80 : if (no_schema)
491 0 : appendPQExpBufferStr(pgdumpopts, " --no-schema");
492 80 : if (no_statistics)
493 4 : appendPQExpBufferStr(pgdumpopts, " --no-statistics");
494 80 : if (no_subscriptions)
495 0 : appendPQExpBufferStr(pgdumpopts, " --no-subscriptions");
496 80 : if (no_toast_compression)
497 0 : appendPQExpBufferStr(pgdumpopts, " --no-toast-compression");
498 80 : if (no_unlogged_table_data)
499 6 : appendPQExpBufferStr(pgdumpopts, " --no-unlogged-table-data");
500 80 : if (with_data)
501 0 : appendPQExpBufferStr(pgdumpopts, " --with-data");
502 80 : if (with_schema)
503 0 : appendPQExpBufferStr(pgdumpopts, " --with-schema");
504 80 : if (with_statistics)
505 0 : appendPQExpBufferStr(pgdumpopts, " --with-statistics");
506 80 : if (on_conflict_do_nothing)
507 0 : appendPQExpBufferStr(pgdumpopts, " --on-conflict-do-nothing");
508 80 : if (statistics_only)
509 0 : appendPQExpBufferStr(pgdumpopts, " --statistics-only");
510 80 : if (sequence_data)
511 0 : 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 80 : if (archDumpFormat != archNull)
518 : {
519 : char global_path[MAXPGPATH];
520 :
521 : /* Create new directory or accept the empty existing directory. */
522 16 : create_or_open_dir(filename);
523 :
524 16 : snprintf(global_path, MAXPGPATH, "%s/global.dat", filename);
525 :
526 16 : OPF = fopen(global_path, PG_BINARY_W);
527 16 : if (!OPF)
528 0 : pg_fatal("could not open \"%s\": %m", global_path);
529 : }
530 64 : else if (filename)
531 : {
532 56 : OPF = fopen(filename, PG_BINARY_W);
533 56 : if (!OPF)
534 0 : pg_fatal("could not open output file \"%s\": %m",
535 : filename);
536 : }
537 : else
538 8 : 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 80 : if (pgdb)
546 : {
547 0 : conn = ConnectDatabase(pgdb, connstr, pghost, pgport, pguser,
548 : prompt_password, false,
549 : progname, &connstr, &server_version, NULL, NULL);
550 :
551 0 : if (!conn)
552 0 : pg_fatal("could not connect to database \"%s\"", pgdb);
553 : }
554 : else
555 : {
556 80 : conn = ConnectDatabase("postgres", connstr, pghost, pgport, pguser,
557 : prompt_password, false,
558 : progname, &connstr, &server_version, NULL, NULL);
559 80 : if (!conn)
560 0 : conn = ConnectDatabase("template1", connstr, pghost, pgport, pguser,
561 : prompt_password, true,
562 : progname, &connstr, &server_version, NULL, NULL);
563 :
564 80 : if (!conn)
565 : {
566 0 : pg_log_error("could not connect to databases \"postgres\" or \"template1\"\n"
567 : "Please specify an alternative database.");
568 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
569 0 : exit_nicely(1);
570 : }
571 : }
572 :
573 : /*
574 : * Get a list of database names that match the exclude patterns
575 : */
576 80 : expand_dbname_patterns(conn, &database_exclude_patterns,
577 : &database_exclude_names);
578 :
579 : /*
580 : * Set the client encoding if requested.
581 : */
582 76 : if (dumpencoding)
583 : {
584 0 : if (PQsetClientEncoding(conn, dumpencoding) < 0)
585 0 : 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 : */
593 76 : encoding = PQclientEncoding(conn);
594 76 : setFmtEncoding(encoding);
595 76 : std_strings = PQparameterStatus(conn, "standard_conforming_strings");
596 76 : if (!std_strings)
597 0 : std_strings = "off";
598 :
599 : /* Set the role if requested */
600 76 : if (use_role)
601 : {
602 0 : PQExpBuffer query = createPQExpBuffer();
603 :
604 0 : appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role));
605 0 : executeCommand(conn, query->data);
606 0 : destroyPQExpBuffer(query);
607 : }
608 :
609 : /* Force quoting of all identifiers if requested. */
610 76 : if (quote_all_identifiers)
611 20 : executeCommand(conn, "SET quote_all_identifiers = true");
612 :
613 76 : fprintf(OPF, "--\n-- PostgreSQL database cluster dump\n--\n\n");
614 76 : if (verbose)
615 4 : 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 76 : fprintf(OPF, "SET default_transaction_read_only = off;\n\n");
626 :
627 : /* Replicate encoding and std_strings in output */
628 76 : fprintf(OPF, "SET client_encoding = '%s';\n",
629 : pg_encoding_to_char(encoding));
630 76 : fprintf(OPF, "SET standard_conforming_strings = %s;\n", std_strings);
631 76 : if (strcmp(std_strings, "off") == 0)
632 0 : fprintf(OPF, "SET escape_string_warning = off;\n");
633 76 : fprintf(OPF, "\n");
634 :
635 76 : 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 76 : if (output_clean)
644 : {
645 2 : if (!globals_only && !roles_only && !tablespaces_only)
646 0 : dropDBs(conn);
647 :
648 2 : if (!roles_only && !no_tablespaces)
649 2 : dropTablespaces(conn);
650 :
651 2 : if (!tablespaces_only)
652 2 : dropRoles(conn);
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 76 : if (!tablespaces_only)
660 : {
661 : /* Dump roles (users) */
662 76 : dumpRoles(conn);
663 :
664 : /* Dump role memberships */
665 76 : dumpRoleMembership(conn);
666 :
667 : /* Dump role GUC privileges */
668 76 : if (server_version >= 150000 && !skip_acls)
669 76 : dumpRoleGUCPrivs(conn);
670 : }
671 :
672 : /* Dump tablespaces */
673 76 : if (!roles_only && !no_tablespaces)
674 66 : dumpTablespaces(conn);
675 : }
676 :
677 76 : if (!globals_only && !roles_only && !tablespaces_only)
678 38 : dumpDatabases(conn, archDumpFormat);
679 :
680 74 : PQfinish(conn);
681 :
682 74 : if (verbose)
683 4 : dumpTimestamp("Completed on");
684 74 : fprintf(OPF, "--\n-- PostgreSQL database cluster dump complete\n--\n\n");
685 :
686 74 : if (filename)
687 : {
688 70 : fclose(OPF);
689 :
690 : /* sync the resulting file, errors are not fatal */
691 70 : if (dosync && (archDumpFormat == archNull))
692 4 : (void) fsync_fname(filename, false);
693 : }
694 :
695 74 : exit_nicely(0);
696 : }
697 :
698 :
699 : static void
700 2 : help(void)
701 : {
702 2 : printf(_("%s extracts a PostgreSQL database cluster based on specified dump format.\n\n"), progname);
703 2 : printf(_("Usage:\n"));
704 2 : printf(_(" %s [OPTION]...\n"), progname);
705 :
706 2 : printf(_("\nGeneral options:\n"));
707 2 : printf(_(" -f, --file=FILENAME output file name\n"));
708 2 : printf(_(" -F, --format=c|d|t|p output file format (custom, directory, tar,\n"
709 : " plain text (default))\n"));
710 2 : printf(_(" -v, --verbose verbose mode\n"));
711 2 : printf(_(" -V, --version output version information, then exit\n"));
712 2 : printf(_(" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n"));
713 2 : printf(_(" -?, --help show this help, then exit\n"));
714 2 : printf(_("\nOptions controlling the output content:\n"));
715 2 : printf(_(" -a, --data-only dump only the data, not the schema or statistics\n"));
716 2 : printf(_(" -c, --clean clean (drop) databases before recreating\n"));
717 2 : printf(_(" -E, --encoding=ENCODING dump the data in encoding ENCODING\n"));
718 2 : printf(_(" -g, --globals-only dump only global objects, no databases\n"));
719 2 : printf(_(" -O, --no-owner skip restoration of object ownership\n"));
720 2 : printf(_(" -r, --roles-only dump only roles, no databases or tablespaces\n"));
721 2 : printf(_(" -s, --schema-only dump only the schema, no data or statistics\n"));
722 2 : printf(_(" -S, --superuser=NAME superuser user name to use in the dump\n"));
723 2 : printf(_(" -t, --tablespaces-only dump only tablespaces, no databases or roles\n"));
724 2 : printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
725 2 : printf(_(" --binary-upgrade for use by upgrade utilities only\n"));
726 2 : printf(_(" --column-inserts dump data as INSERT commands with column names\n"));
727 2 : printf(_(" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n"));
728 2 : printf(_(" --disable-triggers disable triggers during data-only restore\n"));
729 2 : printf(_(" --exclude-database=PATTERN exclude databases whose name matches PATTERN\n"));
730 2 : printf(_(" --extra-float-digits=NUM override default setting for extra_float_digits\n"));
731 2 : printf(_(" --filter=FILENAME exclude databases based on expressions in FILENAME\n"));
732 2 : printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
733 2 : printf(_(" --inserts dump data as INSERT commands, rather than COPY\n"));
734 2 : printf(_(" --load-via-partition-root load partitions via the root table\n"));
735 2 : printf(_(" --no-comments do not dump comment commands\n"));
736 2 : printf(_(" --no-data do not dump data\n"));
737 2 : printf(_(" --no-policies do not dump row security policies\n"));
738 2 : printf(_(" --no-publications do not dump publications\n"));
739 2 : printf(_(" --no-role-passwords do not dump passwords for roles\n"));
740 2 : printf(_(" --no-schema do not dump schema\n"));
741 2 : printf(_(" --no-security-labels do not dump security label assignments\n"));
742 2 : printf(_(" --no-statistics do not dump statistics\n"));
743 2 : printf(_(" --no-subscriptions do not dump subscriptions\n"));
744 2 : printf(_(" --no-sync do not wait for changes to be written safely to disk\n"));
745 2 : printf(_(" --no-table-access-method do not dump table access methods\n"));
746 2 : printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
747 2 : printf(_(" --no-toast-compression do not dump TOAST compression methods\n"));
748 2 : printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
749 2 : printf(_(" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n"));
750 2 : printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
751 2 : printf(_(" --rows-per-insert=NROWS number of rows per INSERT; implies --inserts\n"));
752 2 : printf(_(" --sequence-data include sequence data in dump\n"));
753 2 : printf(_(" --statistics-only dump only the statistics, not schema or data\n"));
754 2 : printf(_(" --use-set-session-authorization\n"
755 : " use SET SESSION AUTHORIZATION commands instead of\n"
756 : " ALTER OWNER commands to set ownership\n"));
757 2 : printf(_(" --with-data dump the data\n"));
758 2 : printf(_(" --with-schema dump the schema\n"));
759 2 : printf(_(" --with-statistics dump the statistics\n"));
760 :
761 2 : printf(_("\nConnection options:\n"));
762 2 : printf(_(" -d, --dbname=CONNSTR connect using connection string\n"));
763 2 : printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
764 2 : printf(_(" -l, --database=DBNAME alternative default database\n"));
765 2 : printf(_(" -p, --port=PORT database server port number\n"));
766 2 : printf(_(" -U, --username=NAME connect as specified database user\n"));
767 2 : printf(_(" -w, --no-password never prompt for password\n"));
768 2 : printf(_(" -W, --password force password prompt (should happen automatically)\n"));
769 2 : printf(_(" --role=ROLENAME do SET ROLE before dump\n"));
770 :
771 2 : printf(_("\nIf -f/--file is not used, then the SQL script will be written to the standard\n"
772 : "output.\n\n"));
773 2 : printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
774 2 : printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
775 2 : }
776 :
777 :
778 : /*
779 : * Drop roles
780 : */
781 : static void
782 2 : dropRoles(PGconn *conn)
783 : {
784 2 : PQExpBuffer buf = createPQExpBuffer();
785 : PGresult *res;
786 : int i_rolname;
787 : int i;
788 :
789 2 : if (server_version >= 90600)
790 2 : printfPQExpBuffer(buf,
791 : "SELECT rolname "
792 : "FROM %s "
793 : "WHERE rolname !~ '^pg_' "
794 : "ORDER BY 1", role_catalog);
795 : else
796 0 : printfPQExpBuffer(buf,
797 : "SELECT rolname "
798 : "FROM %s "
799 : "ORDER BY 1", role_catalog);
800 :
801 2 : res = executeQuery(conn, buf->data);
802 :
803 2 : i_rolname = PQfnumber(res, "rolname");
804 :
805 2 : if (PQntuples(res) > 0)
806 2 : fprintf(OPF, "--\n-- Drop roles\n--\n\n");
807 :
808 8 : for (i = 0; i < PQntuples(res); i++)
809 : {
810 : const char *rolename;
811 :
812 6 : rolename = PQgetvalue(res, i, i_rolname);
813 :
814 12 : fprintf(OPF, "DROP ROLE %s%s;\n",
815 6 : if_exists ? "IF EXISTS " : "",
816 : fmtId(rolename));
817 : }
818 :
819 2 : PQclear(res);
820 2 : destroyPQExpBuffer(buf);
821 :
822 2 : fprintf(OPF, "\n\n");
823 2 : }
824 :
825 : /*
826 : * Dump roles
827 : */
828 : static void
829 76 : dumpRoles(PGconn *conn)
830 : {
831 76 : PQExpBuffer buf = createPQExpBuffer();
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 76 : if (server_version >= 90600)
854 76 : printfPQExpBuffer(buf,
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 0 : else if (server_version >= 90500)
865 0 : printfPQExpBuffer(buf,
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
875 0 : printfPQExpBuffer(buf,
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 76 : res = executeQuery(conn, buf->data);
887 :
888 76 : i_oid = PQfnumber(res, "oid");
889 76 : i_rolname = PQfnumber(res, "rolname");
890 76 : i_rolsuper = PQfnumber(res, "rolsuper");
891 76 : i_rolinherit = PQfnumber(res, "rolinherit");
892 76 : i_rolcreaterole = PQfnumber(res, "rolcreaterole");
893 76 : i_rolcreatedb = PQfnumber(res, "rolcreatedb");
894 76 : i_rolcanlogin = PQfnumber(res, "rolcanlogin");
895 76 : i_rolconnlimit = PQfnumber(res, "rolconnlimit");
896 76 : i_rolpassword = PQfnumber(res, "rolpassword");
897 76 : i_rolvaliduntil = PQfnumber(res, "rolvaliduntil");
898 76 : i_rolreplication = PQfnumber(res, "rolreplication");
899 76 : i_rolbypassrls = PQfnumber(res, "rolbypassrls");
900 76 : i_rolcomment = PQfnumber(res, "rolcomment");
901 76 : i_is_current_user = PQfnumber(res, "is_current_user");
902 :
903 76 : if (PQntuples(res) > 0)
904 76 : fprintf(OPF, "--\n-- Roles\n--\n\n");
905 :
906 424 : for (i = 0; i < PQntuples(res); i++)
907 : {
908 : const char *rolename;
909 : Oid auth_oid;
910 :
911 348 : auth_oid = atooid(PQgetvalue(res, i, i_oid));
912 348 : rolename = PQgetvalue(res, i, i_rolname);
913 :
914 348 : if (strncmp(rolename, "pg_", 3) == 0)
915 : {
916 0 : pg_log_warning("role name starting with \"pg_\" skipped (%s)",
917 : rolename);
918 0 : continue;
919 : }
920 :
921 348 : resetPQExpBuffer(buf);
922 :
923 348 : if (binary_upgrade)
924 : {
925 20 : appendPQExpBufferStr(buf, "\n-- For binary upgrade, must preserve pg_authid.oid\n");
926 20 : appendPQExpBuffer(buf,
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 348 : if (!binary_upgrade ||
940 20 : strcmp(PQgetvalue(res, i, i_is_current_user), "f") == 0)
941 328 : appendPQExpBuffer(buf, "CREATE ROLE %s;\n", fmtId(rolename));
942 348 : appendPQExpBuffer(buf, "ALTER ROLE %s WITH", fmtId(rolename));
943 :
944 348 : if (strcmp(PQgetvalue(res, i, i_rolsuper), "t") == 0)
945 148 : appendPQExpBufferStr(buf, " SUPERUSER");
946 : else
947 200 : appendPQExpBufferStr(buf, " NOSUPERUSER");
948 :
949 348 : if (strcmp(PQgetvalue(res, i, i_rolinherit), "t") == 0)
950 348 : appendPQExpBufferStr(buf, " INHERIT");
951 : else
952 0 : appendPQExpBufferStr(buf, " NOINHERIT");
953 :
954 348 : if (strcmp(PQgetvalue(res, i, i_rolcreaterole), "t") == 0)
955 132 : appendPQExpBufferStr(buf, " CREATEROLE");
956 : else
957 216 : appendPQExpBufferStr(buf, " NOCREATEROLE");
958 :
959 348 : if (strcmp(PQgetvalue(res, i, i_rolcreatedb), "t") == 0)
960 132 : appendPQExpBufferStr(buf, " CREATEDB");
961 : else
962 216 : appendPQExpBufferStr(buf, " NOCREATEDB");
963 :
964 348 : if (strcmp(PQgetvalue(res, i, i_rolcanlogin), "t") == 0)
965 134 : appendPQExpBufferStr(buf, " LOGIN");
966 : else
967 214 : appendPQExpBufferStr(buf, " NOLOGIN");
968 :
969 348 : if (strcmp(PQgetvalue(res, i, i_rolreplication), "t") == 0)
970 92 : appendPQExpBufferStr(buf, " REPLICATION");
971 : else
972 256 : appendPQExpBufferStr(buf, " NOREPLICATION");
973 :
974 348 : if (strcmp(PQgetvalue(res, i, i_rolbypassrls), "t") == 0)
975 76 : appendPQExpBufferStr(buf, " BYPASSRLS");
976 : else
977 272 : appendPQExpBufferStr(buf, " NOBYPASSRLS");
978 :
979 348 : if (strcmp(PQgetvalue(res, i, i_rolconnlimit), "-1") != 0)
980 16 : appendPQExpBuffer(buf, " CONNECTION LIMIT %s",
981 : PQgetvalue(res, i, i_rolconnlimit));
982 :
983 :
984 348 : if (!PQgetisnull(res, i, i_rolpassword) && !no_role_passwords)
985 : {
986 16 : appendPQExpBufferStr(buf, " PASSWORD ");
987 16 : appendStringLiteralConn(buf, PQgetvalue(res, i, i_rolpassword), conn);
988 : }
989 :
990 348 : if (!PQgetisnull(res, i, i_rolvaliduntil))
991 0 : appendPQExpBuffer(buf, " VALID UNTIL '%s'",
992 : PQgetvalue(res, i, i_rolvaliduntil));
993 :
994 348 : appendPQExpBufferStr(buf, ";\n");
995 :
996 348 : if (!no_comments && !PQgetisnull(res, i, i_rolcomment))
997 : {
998 0 : appendPQExpBuffer(buf, "COMMENT ON ROLE %s IS ", fmtId(rolename));
999 0 : appendStringLiteralConn(buf, PQgetvalue(res, i, i_rolcomment), conn);
1000 0 : appendPQExpBufferStr(buf, ";\n");
1001 : }
1002 :
1003 348 : if (!no_security_labels)
1004 348 : buildShSecLabels(conn, "pg_authid", auth_oid,
1005 : "ROLE", rolename,
1006 : buf);
1007 :
1008 348 : 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 424 : for (i = 0; i < PQntuples(res); i++)
1017 348 : dumpUserConfig(conn, PQgetvalue(res, i, i_rolname));
1018 :
1019 76 : PQclear(res);
1020 :
1021 76 : fprintf(OPF, "\n\n");
1022 :
1023 76 : destroyPQExpBuffer(buf);
1024 76 : }
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 : */
1033 : static void
1034 76 : dumpRoleMembership(PGconn *conn)
1035 : {
1036 76 : PQExpBuffer buf = createPQExpBuffer();
1037 76 : PQExpBuffer optbuf = createPQExpBuffer();
1038 : PGresult *res;
1039 76 : 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 76 : dump_grantors = (PQserverVersion(conn) >= 160000);
1066 :
1067 : /*
1068 : * Previous versions of PostgreSQL also did not have grant-level options.
1069 : */
1070 76 : dump_grant_options = (server_version >= 160000);
1071 :
1072 : /* Generate and execute query. */
1073 76 : 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 76 : if (dump_grant_options)
1081 76 : appendPQExpBufferStr(buf, ", a.inherit_option, a.set_option");
1082 76 : 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 76 : res = executeQuery(conn, buf->data);
1089 76 : i_role = PQfnumber(res, "role");
1090 76 : i_member = PQfnumber(res, "member");
1091 76 : i_grantor = PQfnumber(res, "grantor");
1092 76 : i_roleid = PQfnumber(res, "roleid");
1093 76 : i_memberid = PQfnumber(res, "memberid");
1094 76 : i_grantorid = PQfnumber(res, "grantorid");
1095 76 : i_admin_option = PQfnumber(res, "admin_option");
1096 76 : i_inherit_option = PQfnumber(res, "inherit_option");
1097 76 : i_set_option = PQfnumber(res, "set_option");
1098 :
1099 76 : if (PQntuples(res) > 0)
1100 16 : 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 76 : total = PQntuples(res);
1117 92 : while (start < total)
1118 : {
1119 16 : char *role = PQgetvalue(res, start, i_role);
1120 : int i;
1121 : bool *done;
1122 : int remaining;
1123 16 : 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 16 : if (PQgetisnull(res, start, i_role))
1128 : {
1129 : /* translator: %s represents a numeric role OID */
1130 0 : pg_log_warning("found orphaned pg_auth_members entry for role %s",
1131 : PQgetvalue(res, start, i_roleid));
1132 0 : break;
1133 : }
1134 :
1135 : /* All memberships for a single role should be adjacent. */
1136 32 : for (end = start; end < total; ++end)
1137 : {
1138 : char *otherrole;
1139 :
1140 16 : otherrole = PQgetvalue(res, end, i_role);
1141 16 : if (strcmp(role, otherrole) != 0)
1142 0 : break;
1143 : }
1144 :
1145 16 : remaining = end - start;
1146 16 : done = pg_malloc0(remaining * sizeof(bool));
1147 16 : 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 32 : 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 16 : if (remaining == prev_remaining)
1162 : {
1163 0 : pg_log_error("could not find a legal dump ordering for memberships in role \"%s\"",
1164 : role);
1165 0 : PQfinish(conn);
1166 0 : exit_nicely(1);
1167 : }
1168 16 : prev_remaining = remaining;
1169 :
1170 : /* Make one pass over the grants for this role. */
1171 32 : for (i = start; i < end; ++i)
1172 : {
1173 : char *member;
1174 : char *admin_option;
1175 : char *grantorid;
1176 : char *grantor;
1177 16 : char *set_option = "true";
1178 : bool found;
1179 :
1180 : /* If we already did this grant, don't do it again. */
1181 16 : if (done[i - start])
1182 0 : continue;
1183 :
1184 : /* Complain about, then ignore, entries with orphaned OIDs. */
1185 16 : if (PQgetisnull(res, i, i_member))
1186 : {
1187 : /* translator: %s represents a numeric role OID */
1188 0 : pg_log_warning("found orphaned pg_auth_members entry for role %s",
1189 : PQgetvalue(res, i, i_memberid));
1190 0 : done[i - start] = true;
1191 0 : --remaining;
1192 0 : continue;
1193 : }
1194 16 : if (PQgetisnull(res, i, i_grantor))
1195 : {
1196 : /* translator: %s represents a numeric role OID */
1197 0 : pg_log_warning("found orphaned pg_auth_members entry for role %s",
1198 : PQgetvalue(res, i, i_grantorid));
1199 0 : done[i - start] = true;
1200 0 : --remaining;
1201 0 : continue;
1202 : }
1203 :
1204 16 : member = PQgetvalue(res, i, i_member);
1205 16 : grantor = PQgetvalue(res, i, i_grantor);
1206 16 : grantorid = PQgetvalue(res, i, i_grantorid);
1207 16 : admin_option = PQgetvalue(res, i, i_admin_option);
1208 16 : if (dump_grant_options)
1209 16 : 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 16 : if (dump_grantors &&
1218 16 : atooid(grantorid) != BOOTSTRAP_SUPERUSERID &&
1219 0 : rolename_lookup(ht, grantor) == NULL)
1220 0 : continue;
1221 :
1222 : /* Remember that we did this so that we don't do it again. */
1223 16 : done[i - start] = true;
1224 16 : --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 16 : if (*admin_option == 't')
1231 0 : rolename_insert(ht, member, &found);
1232 :
1233 : /* Generate the actual GRANT statement. */
1234 16 : resetPQExpBuffer(optbuf);
1235 16 : fprintf(OPF, "GRANT %s", fmtId(role));
1236 16 : fprintf(OPF, " TO %s", fmtId(member));
1237 16 : if (*admin_option == 't')
1238 0 : appendPQExpBufferStr(optbuf, "ADMIN OPTION");
1239 16 : if (dump_grant_options)
1240 : {
1241 : char *inherit_option;
1242 :
1243 16 : if (optbuf->data[0] != '\0')
1244 0 : appendPQExpBufferStr(optbuf, ", ");
1245 16 : inherit_option = PQgetvalue(res, i, i_inherit_option);
1246 16 : appendPQExpBuffer(optbuf, "INHERIT %s",
1247 16 : *inherit_option == 't' ?
1248 : "TRUE" : "FALSE");
1249 : }
1250 16 : if (*set_option != 't')
1251 : {
1252 0 : if (optbuf->data[0] != '\0')
1253 0 : appendPQExpBufferStr(optbuf, ", ");
1254 0 : appendPQExpBufferStr(optbuf, "SET FALSE");
1255 : }
1256 16 : if (optbuf->data[0] != '\0')
1257 16 : fprintf(OPF, " WITH %s", optbuf->data);
1258 16 : if (dump_grantors)
1259 16 : fprintf(OPF, " GRANTED BY %s", fmtId(grantor));
1260 16 : fprintf(OPF, ";\n");
1261 : }
1262 : }
1263 :
1264 16 : rolename_destroy(ht);
1265 16 : pg_free(done);
1266 16 : start = end;
1267 : }
1268 :
1269 76 : PQclear(res);
1270 76 : destroyPQExpBuffer(buf);
1271 :
1272 76 : fprintf(OPF, "\n\n");
1273 76 : }
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 : */
1283 : static void
1284 76 : dumpRoleGUCPrivs(PGconn *conn)
1285 : {
1286 : PGresult *res;
1287 : int i;
1288 :
1289 : /*
1290 : * Get all parameters that have non-default acls defined.
1291 : */
1292 76 : 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 76 : if (PQntuples(res) > 0)
1300 2 : fprintf(OPF, "--\n-- Role privileges on configuration parameters\n--\n\n");
1301 :
1302 82 : for (i = 0; i < PQntuples(res); i++)
1303 : {
1304 6 : PQExpBuffer buf = createPQExpBuffer();
1305 6 : char *parname = PQgetvalue(res, i, 0);
1306 6 : char *parowner = PQgetvalue(res, i, 1);
1307 6 : char *paracl = PQgetvalue(res, i, 2);
1308 6 : char *acldefault = PQgetvalue(res, i, 3);
1309 : char *fparname;
1310 :
1311 : /* needed for buildACLCommands() */
1312 6 : fparname = pg_strdup(fmtId(parname));
1313 :
1314 6 : if (!buildACLCommands(fparname, NULL, NULL, "PARAMETER",
1315 : paracl, acldefault,
1316 : parowner, "", server_version, buf))
1317 : {
1318 0 : pg_log_error("could not parse ACL list (%s) for parameter \"%s\"",
1319 : paracl, parname);
1320 0 : PQfinish(conn);
1321 0 : exit_nicely(1);
1322 : }
1323 :
1324 6 : fprintf(OPF, "%s", buf->data);
1325 :
1326 6 : free(fparname);
1327 6 : destroyPQExpBuffer(buf);
1328 : }
1329 :
1330 76 : PQclear(res);
1331 76 : fprintf(OPF, "\n\n");
1332 76 : }
1333 :
1334 :
1335 : /*
1336 : * Drop tablespaces.
1337 : */
1338 : static void
1339 2 : dropTablespaces(PGconn *conn)
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 2 : res = executeQuery(conn, "SELECT spcname "
1349 : "FROM pg_catalog.pg_tablespace "
1350 : "WHERE spcname !~ '^pg_' "
1351 : "ORDER BY 1");
1352 :
1353 2 : if (PQntuples(res) > 0)
1354 2 : fprintf(OPF, "--\n-- Drop tablespaces\n--\n\n");
1355 :
1356 4 : for (i = 0; i < PQntuples(res); i++)
1357 : {
1358 2 : char *spcname = PQgetvalue(res, i, 0);
1359 :
1360 4 : fprintf(OPF, "DROP TABLESPACE %s%s;\n",
1361 2 : if_exists ? "IF EXISTS " : "",
1362 : fmtId(spcname));
1363 : }
1364 :
1365 2 : PQclear(res);
1366 :
1367 2 : fprintf(OPF, "\n\n");
1368 2 : }
1369 :
1370 : /*
1371 : * Dump tablespaces.
1372 : */
1373 : static void
1374 66 : dumpTablespaces(PGconn *conn)
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 66 : 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 66 : if (PQntuples(res) > 0)
1394 30 : fprintf(OPF, "--\n-- Tablespaces\n--\n\n");
1395 :
1396 112 : for (i = 0; i < PQntuples(res); i++)
1397 : {
1398 46 : PQExpBuffer buf = createPQExpBuffer();
1399 46 : Oid spcoid = atooid(PQgetvalue(res, i, 0));
1400 46 : char *spcname = PQgetvalue(res, i, 1);
1401 46 : char *spcowner = PQgetvalue(res, i, 2);
1402 46 : char *spclocation = PQgetvalue(res, i, 3);
1403 46 : char *spcacl = PQgetvalue(res, i, 4);
1404 46 : char *acldefault = PQgetvalue(res, i, 5);
1405 46 : char *spcoptions = PQgetvalue(res, i, 6);
1406 46 : char *spccomment = PQgetvalue(res, i, 7);
1407 : char *fspcname;
1408 :
1409 : /* needed for buildACLCommands() */
1410 46 : fspcname = pg_strdup(fmtId(spcname));
1411 :
1412 46 : if (binary_upgrade)
1413 : {
1414 0 : appendPQExpBufferStr(buf, "\n-- For binary upgrade, must preserve pg_tablespace oid\n");
1415 0 : appendPQExpBuffer(buf, "SELECT pg_catalog.binary_upgrade_set_next_pg_tablespace_oid('%u'::pg_catalog.oid);\n", spcoid);
1416 : }
1417 :
1418 46 : appendPQExpBuffer(buf, "CREATE TABLESPACE %s", fspcname);
1419 46 : appendPQExpBuffer(buf, " OWNER %s", fmtId(spcowner));
1420 :
1421 46 : 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 46 : if (is_absolute_path(spclocation))
1428 36 : appendStringLiteralConn(buf, spclocation, conn);
1429 : else
1430 10 : appendStringLiteralConn(buf, "", conn);
1431 :
1432 46 : appendPQExpBufferStr(buf, ";\n");
1433 :
1434 46 : if (spcoptions && spcoptions[0] != '\0')
1435 16 : appendPQExpBuffer(buf, "ALTER TABLESPACE %s SET (%s);\n",
1436 : fspcname, spcoptions);
1437 :
1438 : /* tablespaces can't have initprivs */
1439 :
1440 46 : if (!skip_acls &&
1441 46 : !buildACLCommands(fspcname, NULL, NULL, "TABLESPACE",
1442 : spcacl, acldefault,
1443 : spcowner, "", server_version, buf))
1444 : {
1445 0 : pg_log_error("could not parse ACL list (%s) for tablespace \"%s\"",
1446 : spcacl, spcname);
1447 0 : PQfinish(conn);
1448 0 : exit_nicely(1);
1449 : }
1450 :
1451 46 : if (!no_comments && spccomment && spccomment[0] != '\0')
1452 : {
1453 0 : appendPQExpBuffer(buf, "COMMENT ON TABLESPACE %s IS ", fspcname);
1454 0 : appendStringLiteralConn(buf, spccomment, conn);
1455 0 : appendPQExpBufferStr(buf, ";\n");
1456 : }
1457 :
1458 46 : if (!no_security_labels)
1459 46 : buildShSecLabels(conn, "pg_tablespace", spcoid,
1460 : "TABLESPACE", spcname,
1461 : buf);
1462 :
1463 46 : fprintf(OPF, "%s", buf->data);
1464 :
1465 46 : free(fspcname);
1466 46 : destroyPQExpBuffer(buf);
1467 : }
1468 :
1469 66 : PQclear(res);
1470 66 : fprintf(OPF, "\n\n");
1471 66 : }
1472 :
1473 :
1474 : /*
1475 : * Dump commands to drop each database.
1476 : */
1477 : static void
1478 0 : dropDBs(PGconn *conn)
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 0 : res = executeQuery(conn,
1488 : "SELECT datname "
1489 : "FROM pg_database d "
1490 : "WHERE datallowconn AND datconnlimit != -2 "
1491 : "ORDER BY datname");
1492 :
1493 0 : if (PQntuples(res) > 0)
1494 0 : fprintf(OPF, "--\n-- Drop databases (except postgres and template1)\n--\n\n");
1495 :
1496 0 : for (i = 0; i < PQntuples(res); i++)
1497 : {
1498 0 : 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 0 : if (strcmp(dbname, "template1") != 0 &&
1506 0 : strcmp(dbname, "template0") != 0 &&
1507 0 : strcmp(dbname, "postgres") != 0)
1508 : {
1509 0 : fprintf(OPF, "DROP DATABASE %s%s;\n",
1510 0 : if_exists ? "IF EXISTS " : "",
1511 : fmtId(dbname));
1512 : }
1513 : }
1514 :
1515 0 : PQclear(res);
1516 :
1517 0 : fprintf(OPF, "\n\n");
1518 0 : }
1519 :
1520 :
1521 : /*
1522 : * Dump user-specific configuration
1523 : */
1524 : static void
1525 348 : dumpUserConfig(PGconn *conn, const char *username)
1526 : {
1527 348 : PQExpBuffer buf = createPQExpBuffer();
1528 : PGresult *res;
1529 : static bool header_done = false;
1530 :
1531 348 : 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);
1535 348 : appendStringLiteralConn(buf, username, conn);
1536 348 : appendPQExpBufferChar(buf, ')');
1537 :
1538 348 : res = executeQuery(conn, buf->data);
1539 :
1540 348 : if (PQntuples(res) > 0)
1541 : {
1542 0 : if (!header_done)
1543 0 : fprintf(OPF, "\n--\n-- User Configurations\n--\n");
1544 0 : header_done = true;
1545 :
1546 0 : fprintf(OPF, "\n--\n-- User Config \"%s\"\n--\n\n", username);
1547 : }
1548 :
1549 348 : for (int i = 0; i < PQntuples(res); i++)
1550 : {
1551 0 : resetPQExpBuffer(buf);
1552 0 : makeAlterConfigCommand(conn, PQgetvalue(res, i, 0),
1553 : "ROLE", username, NULL, NULL,
1554 : buf);
1555 0 : fprintf(OPF, "%s", buf->data);
1556 : }
1557 :
1558 348 : PQclear(res);
1559 :
1560 348 : destroyPQExpBuffer(buf);
1561 348 : }
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 : */
1567 : static void
1568 80 : expand_dbname_patterns(PGconn *conn,
1569 : SimpleStringList *patterns,
1570 : SimpleStringList *names)
1571 : {
1572 : PQExpBuffer query;
1573 : PGresult *res;
1574 :
1575 80 : if (patterns->head == NULL)
1576 68 : return; /* nothing to do */
1577 :
1578 12 : 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 20 : for (SimpleStringListCell *cell = patterns->head; cell; cell = cell->next)
1587 : {
1588 : int dotcnt;
1589 :
1590 12 : appendPQExpBufferStr(query,
1591 : "SELECT datname FROM pg_catalog.pg_database n\n");
1592 12 : processSQLNamePattern(conn, query, cell->val, false,
1593 : false, NULL, "datname", NULL, NULL, NULL,
1594 : &dotcnt);
1595 :
1596 12 : if (dotcnt > 0)
1597 : {
1598 4 : pg_log_error("improper qualified name (too many dotted names): %s",
1599 : cell->val);
1600 4 : PQfinish(conn);
1601 4 : exit_nicely(1);
1602 : }
1603 :
1604 8 : res = executeQuery(conn, query->data);
1605 20 : for (int i = 0; i < PQntuples(res); i++)
1606 : {
1607 12 : simple_string_list_append(names, PQgetvalue(res, i, 0));
1608 : }
1609 :
1610 8 : PQclear(res);
1611 8 : resetPQExpBuffer(query);
1612 : }
1613 :
1614 8 : destroyPQExpBuffer(query);
1615 : }
1616 :
1617 : /*
1618 : * Dump contents of databases.
1619 : */
1620 : static void
1621 38 : dumpDatabases(PGconn *conn, ArchiveFormat archDumpFormat)
1622 : {
1623 : PGresult *res;
1624 : int i;
1625 : char db_subdir[MAXPGPATH];
1626 : char dbfilepath[MAXPGPATH];
1627 38 : 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 38 : 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 38 : if (archDumpFormat == archNull && PQntuples(res) > 0)
1647 24 : 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 38 : if (archDumpFormat != archNull)
1655 : {
1656 : char map_file_path[MAXPGPATH];
1657 :
1658 14 : snprintf(db_subdir, MAXPGPATH, "%s/databases", filename);
1659 :
1660 : /* Create a subdirectory with 'databases' name under main directory. */
1661 14 : if (mkdir(db_subdir, pg_dir_create_mode) != 0)
1662 0 : pg_fatal("could not create subdirectory \"%s\": %m", db_subdir);
1663 :
1664 14 : snprintf(map_file_path, MAXPGPATH, "%s/map.dat", filename);
1665 :
1666 : /* Create a map file (to store dboid and dbname) */
1667 14 : map_file = fopen(map_file_path, PG_BINARY_W);
1668 14 : if (!map_file)
1669 0 : pg_fatal("could not open map file: %s", strerror(errno));
1670 : }
1671 :
1672 256 : for (i = 0; i < PQntuples(res); i++)
1673 : {
1674 220 : char *dbname = PQgetvalue(res, i, 0);
1675 220 : char *oid = PQgetvalue(res, i, 1);
1676 220 : const char *create_opts = "";
1677 : int ret;
1678 :
1679 : /* Skip template0, even if it's not marked !datallowconn. */
1680 220 : if (strcmp(dbname, "template0") == 0)
1681 0 : continue;
1682 :
1683 : /* Skip any explicitly excluded database */
1684 220 : if (simple_string_list_member(&database_exclude_names, dbname))
1685 : {
1686 12 : pg_log_info("excluding database \"%s\"", dbname);
1687 12 : 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 208 : if (archDumpFormat != archNull)
1695 : {
1696 108 : if (archDumpFormat == archCustom)
1697 16 : snprintf(dbfilepath, MAXPGPATH, "\"%s\"/\"%s\".dmp", db_subdir, oid);
1698 92 : else if (archDumpFormat == archTar)
1699 16 : snprintf(dbfilepath, MAXPGPATH, "\"%s\"/\"%s\".tar", db_subdir, oid);
1700 : else
1701 76 : snprintf(dbfilepath, MAXPGPATH, "\"%s\"/\"%s\"", db_subdir, oid);
1702 :
1703 : /* Put one line entry for dboid and dbname in map file. */
1704 108 : fprintf(map_file, "%s %s\n", oid, dbname);
1705 : }
1706 :
1707 208 : pg_log_info("dumping database \"%s\"", dbname);
1708 :
1709 208 : if (archDumpFormat == archNull)
1710 100 : 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 208 : if (strcmp(dbname, "template1") == 0 || strcmp(dbname, "postgres") == 0)
1721 : {
1722 72 : if (output_clean)
1723 0 : create_opts = "--clean --create";
1724 : /* Since pg_dump won't emit a \connect command, we must */
1725 72 : else if (archDumpFormat == archNull)
1726 44 : fprintf(OPF, "\\connect %s\n\n", dbname);
1727 : }
1728 : else
1729 136 : create_opts = "--create";
1730 :
1731 208 : if (filename)
1732 196 : fclose(OPF);
1733 :
1734 208 : ret = runPgDump(dbname, create_opts, dbfilepath, archDumpFormat);
1735 206 : if (ret != 0)
1736 0 : pg_fatal("pg_dump failed on database \"%s\", exiting", dbname);
1737 :
1738 206 : if (filename)
1739 : {
1740 : char global_path[MAXPGPATH];
1741 :
1742 194 : if (archDumpFormat != archNull)
1743 108 : snprintf(global_path, MAXPGPATH, "%s/global.dat", filename);
1744 : else
1745 86 : snprintf(global_path, MAXPGPATH, "%s", filename);
1746 :
1747 194 : OPF = fopen(global_path, PG_BINARY_A);
1748 194 : if (!OPF)
1749 0 : pg_fatal("could not re-open the output file \"%s\": %m",
1750 : global_path);
1751 : }
1752 : }
1753 :
1754 : /* Close map file */
1755 36 : if (archDumpFormat != archNull)
1756 14 : fclose(map_file);
1757 :
1758 36 : PQclear(res);
1759 36 : }
1760 :
1761 :
1762 :
1763 : /*
1764 : * Run pg_dump on dbname, with specified options.
1765 : */
1766 : static int
1767 208 : runPgDump(const char *dbname, const char *create_opts, char *dbfile,
1768 : ArchiveFormat archDumpFormat)
1769 : {
1770 : PQExpBufferData connstrbuf;
1771 : PQExpBufferData cmd;
1772 : int ret;
1773 :
1774 208 : initPQExpBuffer(&connstrbuf);
1775 208 : 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 208 : if (archDumpFormat != archNull)
1782 : {
1783 108 : printfPQExpBuffer(&cmd, "\"%s\" -f %s %s", pg_dump_bin,
1784 : dbfile, create_opts);
1785 :
1786 108 : if (archDumpFormat == archDirectory)
1787 76 : appendPQExpBufferStr(&cmd, " --format=directory ");
1788 32 : else if (archDumpFormat == archCustom)
1789 16 : appendPQExpBufferStr(&cmd, " --format=custom ");
1790 16 : else if (archDumpFormat == archTar)
1791 16 : appendPQExpBufferStr(&cmd, " --format=tar ");
1792 : }
1793 : else
1794 : {
1795 100 : printfPQExpBuffer(&cmd, "\"%s\" %s %s", pg_dump_bin,
1796 100 : pgdumpopts->data, create_opts);
1797 :
1798 : /*
1799 : * If we have a filename, use the undocumented plain-append pg_dump
1800 : * format.
1801 : */
1802 100 : if (filename)
1803 88 : appendPQExpBufferStr(&cmd, " -Fa ");
1804 : else
1805 12 : appendPQExpBufferStr(&cmd, " -Fp ");
1806 : }
1807 :
1808 : /*
1809 : * Append the database name to the already-constructed stem of connection
1810 : * string.
1811 : */
1812 208 : appendPQExpBuffer(&connstrbuf, "%s dbname=", connstr);
1813 208 : appendConnStrVal(&connstrbuf, dbname);
1814 :
1815 208 : appendShellString(&cmd, connstrbuf.data);
1816 :
1817 206 : pg_log_info("running \"%s\"", cmd.data);
1818 :
1819 206 : fflush(NULL);
1820 :
1821 206 : ret = system(cmd.data);
1822 :
1823 206 : termPQExpBuffer(&cmd);
1824 206 : termPQExpBuffer(&connstrbuf);
1825 :
1826 206 : 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 : */
1840 : static void
1841 394 : buildShSecLabels(PGconn *conn, const char *catalog_name, Oid objectId,
1842 : const char *objtype, const char *objname,
1843 : PQExpBuffer buffer)
1844 : {
1845 394 : PQExpBuffer sql = createPQExpBuffer();
1846 : PGresult *res;
1847 :
1848 394 : buildShSecLabelQuery(catalog_name, objectId, sql);
1849 394 : res = executeQuery(conn, sql->data);
1850 394 : emitShSecLabels(conn, res, buffer, objtype, objname);
1851 :
1852 394 : PQclear(res);
1853 394 : destroyPQExpBuffer(sql);
1854 394 : }
1855 :
1856 : /*
1857 : * As above for a SQL command (which returns nothing).
1858 : */
1859 : static void
1860 20 : executeCommand(PGconn *conn, const char *query)
1861 : {
1862 : PGresult *res;
1863 :
1864 20 : pg_log_info("executing %s", query);
1865 :
1866 20 : res = PQexec(conn, query);
1867 40 : if (!res ||
1868 20 : PQresultStatus(res) != PGRES_COMMAND_OK)
1869 : {
1870 0 : pg_log_error("query failed: %s", PQerrorMessage(conn));
1871 0 : pg_log_error_detail("Query was: %s", query);
1872 0 : PQfinish(conn);
1873 0 : exit_nicely(1);
1874 : }
1875 :
1876 20 : PQclear(res);
1877 20 : }
1878 :
1879 :
1880 : /*
1881 : * dumpTimestamp
1882 : */
1883 : static void
1884 8 : dumpTimestamp(const char *msg)
1885 : {
1886 : char buf[64];
1887 8 : time_t now = time(NULL);
1888 :
1889 8 : if (strftime(buf, sizeof(buf), PGDUMP_STRFTIME_FMT, localtime(&now)) != 0)
1890 8 : fprintf(OPF, "-- %s %s\n\n", msg, buf);
1891 8 : }
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 : */
1902 : static void
1903 10 : read_dumpall_filters(const char *filename, SimpleStringList *pattern)
1904 : {
1905 : FilterStateData fstate;
1906 : char *objname;
1907 : FilterCommandType comtype;
1908 : FilterObjectType objtype;
1909 :
1910 10 : filter_init(&fstate, filename, exit);
1911 :
1912 14 : while (filter_read_item(&fstate, &objname, &comtype, &objtype))
1913 : {
1914 6 : if (comtype == FILTER_COMMAND_TYPE_INCLUDE)
1915 : {
1916 0 : pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
1917 : "include",
1918 : filter_object_type_name(objtype));
1919 0 : exit_nicely(1);
1920 : }
1921 :
1922 6 : switch (objtype)
1923 : {
1924 0 : case FILTER_OBJECT_TYPE_NONE:
1925 0 : break;
1926 2 : case FILTER_OBJECT_TYPE_FUNCTION:
1927 : case FILTER_OBJECT_TYPE_INDEX:
1928 : case FILTER_OBJECT_TYPE_TABLE_DATA:
1929 : case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN:
1930 : case FILTER_OBJECT_TYPE_TRIGGER:
1931 : case FILTER_OBJECT_TYPE_EXTENSION:
1932 : case FILTER_OBJECT_TYPE_FOREIGN_DATA:
1933 : case FILTER_OBJECT_TYPE_SCHEMA:
1934 : case FILTER_OBJECT_TYPE_TABLE:
1935 : case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN:
1936 2 : pg_log_filter_error(&fstate, _("unsupported filter object"));
1937 2 : exit_nicely(1);
1938 : break;
1939 :
1940 4 : case FILTER_OBJECT_TYPE_DATABASE:
1941 4 : simple_string_list_append(pattern, objname);
1942 4 : break;
1943 : }
1944 :
1945 4 : if (objname)
1946 4 : free(objname);
1947 : }
1948 :
1949 4 : filter_free(&fstate);
1950 4 : }
1951 :
1952 : /*
1953 : * parseDumpFormat
1954 : *
1955 : * This will validate dump formats.
1956 : */
1957 : static ArchiveFormat
1958 82 : parseDumpFormat(const char *format)
1959 : {
1960 : ArchiveFormat archDumpFormat;
1961 :
1962 82 : if (pg_strcasecmp(format, "c") == 0)
1963 0 : archDumpFormat = archCustom;
1964 82 : else if (pg_strcasecmp(format, "custom") == 0)
1965 2 : archDumpFormat = archCustom;
1966 80 : else if (pg_strcasecmp(format, "d") == 0)
1967 0 : archDumpFormat = archDirectory;
1968 80 : else if (pg_strcasecmp(format, "directory") == 0)
1969 12 : archDumpFormat = archDirectory;
1970 68 : else if (pg_strcasecmp(format, "p") == 0)
1971 64 : archDumpFormat = archNull;
1972 4 : else if (pg_strcasecmp(format, "plain") == 0)
1973 0 : archDumpFormat = archNull;
1974 4 : else if (pg_strcasecmp(format, "t") == 0)
1975 0 : archDumpFormat = archTar;
1976 4 : else if (pg_strcasecmp(format, "tar") == 0)
1977 2 : archDumpFormat = archTar;
1978 : else
1979 2 : pg_fatal("unrecognized archive format \"%s\"; please specify \"c\", \"d\", \"p\", or \"t\"",
1980 : format);
1981 :
1982 80 : return archDumpFormat;
1983 : }
|