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