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