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