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