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