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