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