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