LCOV - code coverage report
Current view: top level - src/bin/pg_dump - pg_dumpall.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 594 822 72.3 %
Date: 2024-04-26 03:10:58 Functions: 19 20 95.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14