PostgreSQL Source Code: src/bin/pg_dump/pg_dumpall.c Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
17
20
21#include "catalog/pg_authid_d.h"
33
34
35#define PGDUMP_VERSIONSTR "pg_dump (PostgreSQL) " PG_VERSION "\n"
36
37typedef struct
38{
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
57
58static void help(void);
59
70static int runPgDump(const char *dbname, const char *create_opts,
73 const char *catalog_name, Oid objectId,
74 const char *objtype, const char *objname,
81
89
118
120#define PG_AUTHID "pg_authid"
121#define PG_ROLES "pg_roles "
122
125
128
129int
130main(int argc, char *argv[])
131{
132 static struct option long_options[] = {
137 {"globals-only", no_argument, NULL, 'g'},
146 {"tablespaces-only", no_argument, NULL, 't'},
151 {"no-privileges", no_argument, NULL, 'x'},
154
155
156
157
194
195 {NULL, 0, NULL, 0}
196 };
197
200 char *pguser = NULL;
201 char *pgdb = NULL;
202 char *use_role = NULL;
203 const char *dumpencoding = NULL;
205 const char *formatName = "p";
207 bool data_only = false;
208 bool globals_only = false;
209 bool roles_only = false;
210 bool tablespaces_only = false;
213 const char *std_strings;
214 int c,
215 ret;
216 int optindex;
217
222
223 if (argc > 1)
224 {
225 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
226 {
229 }
230 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
231 {
232 puts("pg_dumpall (PostgreSQL) " PG_VERSION);
234 }
235 }
236
239 {
241
244
245 if (ret == -1)
246 pg_fatal("program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
247 "pg_dump", progname, full_path);
248 else
249 pg_fatal("program \"%s\" was found by \"%s\" but was not the same version as %s",
250 "pg_dump", full_path, progname);
251 }
252
254
255 while ((c = getopt_long(argc, argv, "acd:E:f:F:gh:l:Op:rsS:tU:vwWx", long_options, &optindex)) != -1)
256 {
257 switch (c)
258 {
259 case 'a':
260 data_only = true;
262 break;
263
264 case 'c':
266 break;
267
268 case 'd':
270 break;
271
272 case 'E':
276 break;
277
278 case 'f':
282 break;
283 case 'F':
285 break;
286 case 'g':
287 globals_only = true;
288 break;
289
290 case 'h':
292 break;
293
294 case 'l':
296 break;
297
298 case 'O':
300 break;
301
302 case 'p':
304 break;
305
306 case 'r':
307 roles_only = true;
308 break;
309
310 case 's':
312 break;
313
314 case 'S':
317 break;
318
319 case 't':
320 tablespaces_only = true;
321 break;
322
323 case 'U':
325 break;
326
327 case 'v':
331 break;
332
333 case 'w':
334 prompt_password = TRI_NO;
336 break;
337
338 case 'W':
339 prompt_password = TRI_YES;
341 break;
342
343 case 'x':
346 break;
347
348 case 0:
349 break;
350
351 case 2:
354 break;
355
356 case 3:
360 break;
361
362 case 4:
365 break;
366
367 case 5:
370 break;
371
372 case 6:
374 break;
375
376 case 7:
379 break;
380
381 case 8:
383 break;
384
385 default:
386
389 }
390 }
391
392
394 {
395 pg_log_error("too many command-line arguments (first is \"%s\")",
399 }
400
402 (globals_only || roles_only || tablespaces_only))
403 {
404 pg_log_error("option --exclude-database cannot be used together with -g/--globals-only, -r/--roles-only, or -t/--tablespaces-only");
407 }
408
409
410 if (globals_only && roles_only)
411 {
412 pg_log_error("options -g/--globals-only and -r/--roles-only cannot be used together");
415 }
416
417 if (globals_only && tablespaces_only)
418 {
419 pg_log_error("options -g/--globals-only and -t/--tablespaces-only cannot be used together");
422 }
423
425 pg_fatal("option --if-exists requires option -c/--clean");
426
427 if (roles_only && tablespaces_only)
428 {
429 pg_log_error("options -r/--roles-only and -t/--tablespaces-only cannot be used together");
432 }
433
434
436
437
438
439
440
441 if (archDumpFormat != archNull &&
443 {
444 pg_log_error("option -F/--format=d|c|t requires option -f/--file");
447 }
448
449
450
451
452
453
456 else
458
459
512
513
514
515
516
517 if (archDumpFormat != archNull)
518 {
520
521
523
525
527 if ()
528 pg_fatal("could not open \"%s\": %m", global_path);
529 }
531 {
533 if ()
534 pg_fatal("could not open output file \"%s\": %m",
536 }
537 else
539
540
541
542
543
544
545 if (pgdb)
546 {
548 prompt_password, false,
550
552 pg_fatal("could not connect to database \"%s\"", pgdb);
553 }
554 else
555 {
557 prompt_password, false,
561 prompt_password, true,
563
565 {
566 pg_log_error("could not connect to databases \"postgres\" or \"template1\"\n"
567 "Please specify an alternative database.");
570 }
571 }
572
573
574
575
578
579
580
581
582 if (dumpencoding)
583 {
585 pg_fatal("invalid client encoding \"%s\" specified",
586 dumpencoding);
587 }
588
589
590
591
592
596 if (!std_strings)
597 std_strings = "off";
598
599
600 if (use_role)
601 {
603
607 }
608
609
612
613 fprintf(OPF, "--\n-- PostgreSQL database cluster dump\n--\n\n");
616
617
618
619
620
621
622
623
624
625 fprintf(OPF, "SET default_transaction_read_only = off;\n\n");
626
627
628 fprintf(OPF, "SET client_encoding = '%s';\n",
630 fprintf(OPF, "SET standard_conforming_strings = %s;\n", std_strings);
631 if (strcmp(std_strings, "off") == 0)
632 fprintf(OPF, "SET escape_string_warning = off;\n");
634
635 if (!data_only)
636 {
637
638
639
640
641
642
644 {
645 if (!globals_only && !roles_only && !tablespaces_only)
647
650
651 if (!tablespaces_only)
653 }
654
655
656
657
658
659 if (!tablespaces_only)
660 {
661
663
664
666
667
670 }
671
672
675 }
676
677 if (!globals_only && !roles_only && !tablespaces_only)
679
681
684 fprintf(OPF, "--\n-- PostgreSQL database cluster dump complete\n--\n\n");
685
687 {
688 fclose(OPF);
689
690
693 }
694
696}
697
698
699static void
701{
702 printf(_("%s extracts a PostgreSQL database cluster based on specified dump format.\n\n"), progname);
705
706 printf(_("\nGeneral options:\n"));
707 printf(_(" -f, --file=FILENAME output file name\n"));
708 printf(_(" -F, --format=c|d|t|p output file format (custom, directory, tar,\n"
709 " plain text (default))\n"));
710 printf(_(" -v, --verbose verbose mode\n"));
711 printf(_(" -V, --version output version information, then exit\n"));
712 printf(_(" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n"));
713 printf(_(" -?, --help show this help, then exit\n"));
714 printf(_("\nOptions controlling the output content:\n"));
715 printf(_(" -a, --data-only dump only the data, not the schema or statistics\n"));
716 printf(_(" -c, --clean clean (drop) databases before recreating\n"));
717 printf(_(" -E, --encoding=ENCODING dump the data in encoding ENCODING\n"));
718 printf(_(" -g, --globals-only dump only global objects, no databases\n"));
719 printf(_(" -O, --no-owner skip restoration of object ownership\n"));
720 printf(_(" -r, --roles-only dump only roles, no databases or tablespaces\n"));
721 printf(_(" -s, --schema-only dump only the schema, no data or statistics\n"));
722 printf(_(" -S, --superuser=NAME superuser user name to use in the dump\n"));
723 printf(_(" -t, --tablespaces-only dump only tablespaces, no databases or roles\n"));
724 printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
725 printf(_(" --binary-upgrade for use by upgrade utilities only\n"));
726 printf(_(" --column-inserts dump data as INSERT commands with column names\n"));
727 printf(_(" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n"));
728 printf(_(" --disable-triggers disable triggers during data-only restore\n"));
729 printf(_(" --exclude-database=PATTERN exclude databases whose name matches PATTERN\n"));
730 printf(_(" --extra-float-digits=NUM override default setting for extra_float_digits\n"));
731 printf(_(" --filter=FILENAME exclude databases based on expressions in FILENAME\n"));
732 printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
733 printf(_(" --inserts dump data as INSERT commands, rather than COPY\n"));
734 printf(_(" --load-via-partition-root load partitions via the root table\n"));
735 printf(_(" --no-comments do not dump comment commands\n"));
736 printf(_(" --no-data do not dump data\n"));
737 printf(_(" --no-policies do not dump row security policies\n"));
738 printf(_(" --no-publications do not dump publications\n"));
739 printf(_(" --no-role-passwords do not dump passwords for roles\n"));
740 printf(_(" --no-schema do not dump schema\n"));
741 printf(_(" --no-security-labels do not dump security label assignments\n"));
742 printf(_(" --no-statistics do not dump statistics\n"));
743 printf(_(" --no-subscriptions do not dump subscriptions\n"));
744 printf(_(" --no-sync do not wait for changes to be written safely to disk\n"));
745 printf(_(" --no-table-access-method do not dump table access methods\n"));
746 printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
747 printf(_(" --no-toast-compression do not dump TOAST compression methods\n"));
748 printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
749 printf(_(" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n"));
750 printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
751 printf(_(" --rows-per-insert=NROWS number of rows per INSERT; implies --inserts\n"));
752 printf(_(" --sequence-data include sequence data in dump\n"));
753 printf(_(" --statistics-only dump only the statistics, not schema or data\n"));
754 printf(_(" --use-set-session-authorization\n"
755 " use SET SESSION AUTHORIZATION commands instead of\n"
756 " ALTER OWNER commands to set ownership\n"));
757 printf(_(" --with-data dump the data\n"));
758 printf(_(" --with-schema dump the schema\n"));
759 printf(_(" --with-statistics dump the statistics\n"));
760
761 printf(_("\nConnection options:\n"));
762 printf(_(" -d, --dbname=CONNSTR connect using connection string\n"));
763 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
764 printf(_(" -l, --database=DBNAME alternative default database\n"));
765 printf(_(" -p, --port=PORT database server port number\n"));
766 printf(_(" -U, --username=NAME connect as specified database user\n"));
767 printf(_(" -w, --no-password never prompt for password\n"));
768 printf(_(" -W, --password force password prompt (should happen automatically)\n"));
769 printf(_(" --role=ROLENAME do SET ROLE before dump\n"));
770
771 printf(_("\nIf -f/--file is not used, then the SQL script will be written to the standard\n"
772 "output.\n\n"));
773 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
774 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
775}
776
777
778
779
780
781static void
783{
786 int i_rolname;
787 int i;
788
791 "SELECT rolname "
792 "FROM %s "
793 "WHERE rolname !~ '^pg_' "
795 else
797 "SELECT rolname "
798 "FROM %s "
800
802
803 i_rolname = PQfnumber(res, "rolname");
804
806 fprintf(OPF, "--\n-- Drop roles\n--\n\n");
807
809 {
810 const char *rolename;
811
812 rolename = PQgetvalue(res, i, i_rolname);
813
816 fmtId(rolename));
817 }
818
821
823}
824
825
826
827
828static void
830{
833 int i_oid,
834 i_rolname,
835 i_rolsuper,
836 i_rolinherit,
837 i_rolcreaterole,
838 i_rolcreatedb,
839 i_rolcanlogin,
840 i_rolconnlimit,
841 i_rolpassword,
842 i_rolvaliduntil,
843 i_rolreplication,
844 i_rolbypassrls,
845 i_rolcomment,
846 i_is_current_user;
847 int i;
848
849
850
851
852
855 "SELECT oid, rolname, rolsuper, rolinherit, "
856 "rolcreaterole, rolcreatedb, "
857 "rolcanlogin, rolconnlimit, rolpassword, "
858 "rolvaliduntil, rolreplication, rolbypassrls, "
859 "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, "
860 "rolname = current_user AS is_current_user "
861 "FROM %s "
862 "WHERE rolname !~ '^pg_' "
866 "SELECT oid, rolname, rolsuper, rolinherit, "
867 "rolcreaterole, rolcreatedb, "
868 "rolcanlogin, rolconnlimit, rolpassword, "
869 "rolvaliduntil, rolreplication, rolbypassrls, "
870 "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, "
871 "rolname = current_user AS is_current_user "
872 "FROM %s "
874 else
876 "SELECT oid, rolname, rolsuper, rolinherit, "
877 "rolcreaterole, rolcreatedb, "
878 "rolcanlogin, rolconnlimit, rolpassword, "
879 "rolvaliduntil, rolreplication, "
880 "false as rolbypassrls, "
881 "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, "
882 "rolname = current_user AS is_current_user "
883 "FROM %s "
885
887
889 i_rolname = PQfnumber(res, "rolname");
890 i_rolsuper = PQfnumber(res, "rolsuper");
891 i_rolinherit = PQfnumber(res, "rolinherit");
892 i_rolcreaterole = PQfnumber(res, "rolcreaterole");
893 i_rolcreatedb = PQfnumber(res, "rolcreatedb");
894 i_rolcanlogin = PQfnumber(res, "rolcanlogin");
895 i_rolconnlimit = PQfnumber(res, "rolconnlimit");
896 i_rolpassword = PQfnumber(res, "rolpassword");
897 i_rolvaliduntil = PQfnumber(res, "rolvaliduntil");
898 i_rolreplication = PQfnumber(res, "rolreplication");
899 i_rolbypassrls = PQfnumber(res, "rolbypassrls");
900 i_rolcomment = PQfnumber(res, "rolcomment");
901 i_is_current_user = PQfnumber(res, "is_current_user");
902
904 fprintf(OPF, "--\n-- Roles\n--\n\n");
905
907 {
908 const char *rolename;
909 Oid auth_oid;
910
912 rolename = PQgetvalue(res, i, i_rolname);
913
914 if (strncmp(rolename, "pg_", 3) == 0)
915 {
916 pg_log_warning("role name starting with \"pg_\" skipped (%s)",
917 rolename);
918 continue;
919 }
920
922
924 {
927 "SELECT pg_catalog.binary_upgrade_set_next_pg_authid_oid('%u'::pg_catalog.oid);\n\n",
928 auth_oid);
929 }
930
931
932
933
934
935
936
937
938
940 strcmp(PQgetvalue(res, i, i_is_current_user), "f") == 0)
943
944 if (strcmp(PQgetvalue(res, i, i_rolsuper), "t") == 0)
946 else
948
949 if (strcmp(PQgetvalue(res, i, i_rolinherit), "t") == 0)
951 else
953
954 if (strcmp(PQgetvalue(res, i, i_rolcreaterole), "t") == 0)
956 else
958
959 if (strcmp(PQgetvalue(res, i, i_rolcreatedb), "t") == 0)
961 else
963
964 if (strcmp(PQgetvalue(res, i, i_rolcanlogin), "t") == 0)
966 else
968
969 if (strcmp(PQgetvalue(res, i, i_rolreplication), "t") == 0)
971 else
973
974 if (strcmp(PQgetvalue(res, i, i_rolbypassrls), "t") == 0)
976 else
978
979 if (strcmp(PQgetvalue(res, i, i_rolconnlimit), "-1") != 0)
982
983
985 {
988 }
989
993
995
997 {
1001 }
1002
1005 "ROLE", rolename,
1007
1009 }
1010
1011
1012
1013
1014
1015
1018
1020
1022
1024}
1025
1026
1027
1028
1029
1030
1031
1032
1033static void
1035{
1040 end,
1041 total;
1042 bool dump_grantors;
1043 bool dump_grant_options;
1044 int i_role;
1045 int i_member;
1046 int i_grantor;
1047 int i_roleid;
1048 int i_memberid;
1049 int i_grantorid;
1050 int i_admin_option;
1051 int i_inherit_option;
1052 int i_set_option;
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1066
1067
1068
1069
1071
1072
1074 "um.rolname AS member, "
1075 "ug.rolname AS grantor, "
1076 "a.roleid AS roleid, "
1077 "a.member AS memberid, "
1078 "a.grantor AS grantorid, "
1079 "a.admin_option");
1080 if (dump_grant_options)
1083 "LEFT JOIN %s ur on ur.oid = a.roleid "
1084 "LEFT JOIN %s um on um.oid = a.member "
1085 "LEFT JOIN %s ug on ug.oid = a.grantor "
1086 "WHERE NOT (ur.rolname ~ '^pg_' AND um.rolname ~ '^pg_')"
1089 i_role = PQfnumber(res, "role");
1090 i_member = PQfnumber(res, "member");
1091 i_grantor = PQfnumber(res, "grantor");
1092 i_roleid = PQfnumber(res, "roleid");
1093 i_memberid = PQfnumber(res, "memberid");
1094 i_grantorid = PQfnumber(res, "grantorid");
1095 i_admin_option = PQfnumber(res, "admin_option");
1096 i_inherit_option = PQfnumber(res, "inherit_option");
1097 i_set_option = PQfnumber(res, "set_option");
1098
1100 fprintf(OPF, "--\n-- Role memberships\n--\n\n");
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1117 while (start < total)
1118 {
1120 int i;
1121 bool *done;
1123 int prev_remaining = 0;
1124 rolename_hash *ht;
1125
1126
1128 {
1129
1130 pg_log_warning("found orphaned pg_auth_members entry for role %s",
1132 break;
1133 }
1134
1135
1136 for (end = start; end < total; ++end)
1137 {
1138 char *otherrole;
1139
1140 otherrole = PQgetvalue(res, end, i_role);
1141 if (strcmp(role, otherrole) != 0)
1142 break;
1143 }
1144
1147 ht = rolename_create(remaining, NULL);
1148
1149
1150
1151
1152
1154 {
1155
1156
1157
1158
1159
1160
1161 if (remaining == prev_remaining)
1162 {
1163 pg_log_error("could not find a legal dump ordering for memberships in role \"%s\"",
1164 role);
1167 }
1169
1170
1171 for (i = start; i < end; ++i)
1172 {
1173 char *member;
1175 char *grantorid;
1176 char *grantor;
1178 bool found;
1179
1180
1182 continue;
1183
1184
1186 {
1187
1188 pg_log_warning("found orphaned pg_auth_members entry for role %s",
1192 continue;
1193 }
1195 {
1196
1197 pg_log_warning("found orphaned pg_auth_members entry for role %s",
1201 continue;
1202 }
1203
1205 grantor = PQgetvalue(res, i, i_grantor);
1206 grantorid = PQgetvalue(res, i, i_grantorid);
1208 if (dump_grant_options)
1210
1211
1212
1213
1214
1215
1216
1217 if (dump_grantors &&
1218 atooid(grantorid) != BOOTSTRAP_SUPERUSERID &&
1219 rolename_lookup(ht, grantor) == NULL)
1220 continue;
1221
1222
1225
1226
1227
1228
1229
1231 rolename_insert(ht, member, &found);
1232
1233
1239 if (dump_grant_options)
1240 {
1242
1243 if (optbuf->data[0] != '\0')
1248 "TRUE" : "FALSE");
1249 }
1251 {
1252 if (optbuf->data[0] != '\0')
1255 }
1256 if (optbuf->data[0] != '\0')
1258 if (dump_grantors)
1261 }
1262 }
1263
1264 rolename_destroy(ht);
1267 }
1268
1271
1273}
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283static void
1285{
1287 int i;
1288
1289
1290
1291
1293 "pg_catalog.pg_get_userbyid(" CppAsString2(BOOTSTRAP_SUPERUSERID) ") AS parowner, "
1294 "paracl, "
1295 "pg_catalog.acldefault('p', " CppAsString2(BOOTSTRAP_SUPERUSERID) ") AS acldefault "
1296 "FROM pg_catalog.pg_parameter_acl "
1297 "ORDER BY 1");
1298
1300 fprintf(OPF, "--\n-- Role privileges on configuration parameters\n--\n\n");
1301
1303 {
1309 char *fparname;
1310
1311
1313
1317 {
1318 pg_log_error("could not parse ACL list (%s) for parameter \"%s\"",
1319 paracl, parname);
1322 }
1323
1325
1326 free(fparname);
1328 }
1329
1332}
1333
1334
1335
1336
1337
1338static void
1340{
1342 int i;
1343
1344
1345
1346
1347
1349 "FROM pg_catalog.pg_tablespace "
1350 "WHERE spcname !~ '^pg_' "
1351 "ORDER BY 1");
1352
1354 fprintf(OPF, "--\n-- Drop tablespaces\n--\n\n");
1355
1357 {
1359
1360 fprintf(OPF, "DROP TABLESPACE %s%s;\n",
1362 fmtId(spcname));
1363 }
1364
1366
1368}
1369
1370
1371
1372
1373static void
1375{
1377 int i;
1378
1379
1380
1381
1382
1384 "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1385 "pg_catalog.pg_tablespace_location(oid), "
1386 "spcacl, acldefault('t', spcowner) AS acldefault, "
1387 "array_to_string(spcoptions, ', '),"
1388 "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1389 "FROM pg_catalog.pg_tablespace "
1390 "WHERE spcname !~ '^pg_' "
1391 "ORDER BY 1");
1392
1394 fprintf(OPF, "--\n-- Tablespaces\n--\n\n");
1395
1397 {
1402 char *spclocation = PQgetvalue(res, i, 3);
1405 char *spcoptions = PQgetvalue(res, i, 6);
1406 char *spccomment = PQgetvalue(res, i, 7);
1407 char *fspcname;
1408
1409
1411
1413 {
1414 appendPQExpBufferStr(buf, "\n-- For binary upgrade, must preserve pg_tablespace oid\n");
1415 appendPQExpBuffer(buf, "SELECT pg_catalog.binary_upgrade_set_next_pg_tablespace_oid('%u'::pg_catalog.oid);\n", spcoid);
1416 }
1417
1420
1422
1423
1424
1425
1426
1429 else
1431
1433
1434 if (spcoptions && spcoptions[0] != '\0')
1436 fspcname, spcoptions);
1437
1438
1439
1444 {
1445 pg_log_error("could not parse ACL list (%s) for tablespace \"%s\"",
1446 spcacl, spcname);
1449 }
1450
1451 if ( && spccomment && spccomment[0] != '\0')
1452 {
1456 }
1457
1460 "TABLESPACE", spcname,
1462
1464
1465 free(fspcname);
1467 }
1468
1471}
1472
1473
1474
1475
1476
1477static void
1479{
1481 int i;
1482
1483
1484
1485
1486
1488 "SELECT datname "
1489 "FROM pg_database d "
1490 "WHERE datallowconn AND datconnlimit != -2 "
1491 "ORDER BY datname");
1492
1494 fprintf(OPF, "--\n-- Drop databases (except postgres and template1)\n--\n\n");
1495
1497 {
1499
1500
1501
1502
1503
1504
1505 if (strcmp(dbname, "template1") != 0 &&
1506 strcmp(dbname, "template0") != 0 &&
1507 strcmp(dbname, "postgres") != 0)
1508 {
1509 fprintf(OPF, "DROP DATABASE %s%s;\n",
1512 }
1513 }
1514
1516
1518}
1519
1520
1521
1522
1523
1524static void
1526{
1529 static bool header_done = false;
1530
1532 "WHERE setdatabase = 0 AND setrole = "
1533 "(SELECT oid FROM %s WHERE rolname = ",
1537
1539
1541 {
1542 if (!header_done)
1543 fprintf(OPF, "\n--\n-- User Configurations\n--\n");
1544 header_done = true;
1545
1547 }
1548
1550 {
1553 "ROLE", username, NULL, NULL,
1556 }
1557
1559
1561}
1562
1563
1564
1565
1566
1567static void
1571{
1574
1575 if (patterns->head == NULL)
1576 return;
1577
1579
1580
1581
1582
1583
1584
1585
1587 {
1588 int dotcnt;
1589
1591 "SELECT datname FROM pg_catalog.pg_database n\n");
1593 false, NULL, "datname", NULL, NULL, NULL,
1594 &dotcnt);
1595
1596 if (dotcnt > 0)
1597 {
1598 pg_log_error("improper qualified name (too many dotted names): %s",
1599 cell->val);
1602 }
1603
1606 {
1608 }
1609
1612 }
1613
1615}
1616
1617
1618
1619
1620static void
1622{
1624 int i;
1627 FILE *map_file = NULL;
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1641 "SELECT datname, oid "
1642 "FROM pg_database d "
1643 "WHERE datallowconn AND datconnlimit != -2 "
1644 "ORDER BY (datname <> 'template1'), datname");
1645
1647 fprintf(OPF, "--\n-- Databases\n--\n\n");
1648
1649
1650
1651
1652
1653
1654 if (archDumpFormat != archNull)
1655 {
1657
1659
1660
1662 pg_fatal("could not create subdirectory \"%s\": %m", db_subdir);
1663
1665
1666
1667 map_file = fopen(map_file_path, PG_BINARY_W);
1668 if (!map_file)
1670 }
1671
1673 {
1676 const char *create_opts = "";
1677 int ret;
1678
1679
1680 if (strcmp(dbname, "template0") == 0)
1681 continue;
1682
1683
1685 {
1687 continue;
1688 }
1689
1690
1691
1692
1693
1694 if (archDumpFormat != archNull)
1695 {
1697 snprintf(dbfilepath, MAXPGPATH, "\"%s\"/\"%s\".dmp", db_subdir, oid);
1698 else if (archDumpFormat == archTar)
1699 snprintf(dbfilepath, MAXPGPATH, "\"%s\"/\"%s\".tar", db_subdir, oid);
1700 else
1701 snprintf(dbfilepath, MAXPGPATH, "\"%s\"/\"%s\"", db_subdir, oid);
1702
1703
1705 }
1706
1708
1709 if (archDumpFormat == archNull)
1710 fprintf(OPF, "--\n-- Database \"%s\" dump\n--\n\n", dbname);
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720 if (strcmp(dbname, "template1") == 0 || strcmp(dbname, "postgres") == 0)
1721 {
1723 create_opts = "--clean --create";
1724
1725 else if (archDumpFormat == archNull)
1727 }
1728 else
1729 create_opts = "--create";
1730
1732 fclose(OPF);
1733
1734 ret = runPgDump(dbname, create_opts, dbfilepath, archDumpFormat);
1735 if (ret != 0)
1736 pg_fatal("pg_dump failed on database \"%s\", exiting", dbname);
1737
1739 {
1741
1742 if (archDumpFormat != archNull)
1744 else
1746
1748 if ()
1749 pg_fatal("could not re-open the output file \"%s\": %m",
1750 global_path);
1751 }
1752 }
1753
1754
1755 if (archDumpFormat != archNull)
1756 fclose(map_file);
1757
1759}
1760
1761
1762
1763
1764
1765
1766static int
1769{
1772 int ret;
1773
1776
1777
1778
1779
1780
1781 if (archDumpFormat != archNull)
1782 {
1784 dbfile, create_opts);
1785
1788 else if (archDumpFormat == archCustom)
1790 else if (archDumpFormat == archTar)
1792 }
1793 else
1794 {
1797
1798
1799
1800
1801
1804 else
1806 }
1807
1808
1809
1810
1811
1814
1816
1818
1819 fflush(NULL);
1820
1821 ret = system(cmd.data);
1822
1825
1826 return ret;
1827}
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840static void
1842 const char *objtype, const char *objname,
1844{
1847
1851
1854}
1855
1856
1857
1858
1859static void
1861{
1863
1865
1867 if (!res ||
1869 {
1874 }
1875
1877}
1878
1879
1880
1881
1882
1883static void
1885{
1886 char buf[64];
1887 time_t now = time(NULL);
1888
1891}
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902static void
1904{
1906 char *objname;
1909
1911
1912 while (filter_read_item(&fstate, &objname, &comtype, &objtype))
1913 {
1915 {
1917 "include",
1920 }
1921
1922 switch (objtype)
1923 {
1925 break;
1938 break;
1939
1942 break;
1943 }
1944
1945 if (objname)
1946 free(objname);
1947 }
1948
1950}
1951
1952
1953
1954
1955
1956
1959{
1961
1975 archDumpFormat = archTar;
1977 archDumpFormat = archTar;
1978 else
1979 pg_fatal("unrecognized archive format \"%s\"; please specify \"c\", \"d\", \"p\", or \"t\"",
1981
1982 return archDumpFormat;
1983}
Acl * acldefault(ObjectType objtype, Oid ownerId)
Datum now(PG_FUNCTION_ARGS)
#define PG_TEXTDOMAIN(domain)
int find_my_exec(const char *argv0, char *retpath)
void set_pglocale_pgservice(const char *argv0, const char *app)
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
PGresult * executeQuery(PGconn *conn, const char *query)
PGconn * ConnectDatabase(const char *dbname, const char *connection_string, const char *pghost, const char *pgport, const char *pguser, trivalue prompt_password, bool fail_on_error, const char *progname, const char **connstr, int *server_version, char *password, char *override_dbname)
#define fprintf(file, fmt, msg)
bool buildACLCommands(const char *name, const char *subname, const char *nspname, const char *type, const char *acls, const char *baseacls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
void buildShSecLabelQuery(const char *catalog_name, Oid objectId, PQExpBuffer sql)
void makeAlterConfigCommand(PGconn *conn, const char *configitem, const char *type, const char *name, const char *type2, const char *name2, PQExpBuffer buf)
void create_or_open_dir(const char *dirname)
void emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer, const char *objtype, const char *objname)
#define PGDUMP_STRFTIME_FMT
void fsync_fname(const char *fname, bool isdir)
int PQserverVersion(const PGconn *conn)
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
int PQclientEncoding(const PGconn *conn)
void PQfinish(PGconn *conn)
char * PQerrorMessage(const PGconn *conn)
int PQsetClientEncoding(PGconn *conn, const char *encoding)
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
ExecStatusType PQresultStatus(const PGresult *res)
void PQclear(PGresult *res)
int PQntuples(const PGresult *res)
int PQfnumber(const PGresult *res, const char *field_name)
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
PGresult * PQexec(PGconn *conn, const char *query)
char * pg_strdup(const char *in)
void * pg_malloc0(size_t size)
void filter_init(FilterStateData *fstate, const char *filename, exit_function f_exit)
void filter_free(FilterStateData *fstate)
const char * filter_object_type_name(FilterObjectType fot)
bool filter_read_item(FilterStateData *fstate, char **objname, FilterCommandType *comtype, FilterObjectType *objtype)
void pg_log_filter_error(FilterStateData *fstate, const char *fmt,...)
@ FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN
@ FILTER_OBJECT_TYPE_SCHEMA
@ FILTER_OBJECT_TYPE_INDEX
@ FILTER_OBJECT_TYPE_TRIGGER
@ FILTER_OBJECT_TYPE_FOREIGN_DATA
@ FILTER_OBJECT_TYPE_DATABASE
@ FILTER_OBJECT_TYPE_FUNCTION
@ FILTER_OBJECT_TYPE_TABLE_DATA
@ FILTER_OBJECT_TYPE_NONE
@ FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN
@ FILTER_OBJECT_TYPE_EXTENSION
@ FILTER_OBJECT_TYPE_TABLE
@ FILTER_COMMAND_TYPE_INCLUDE
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
void pg_logging_increase_verbosity(void)
void pg_logging_init(const char *argv0)
void pg_logging_set_level(enum pg_log_level new_level)
#define pg_log_error(...)
#define pg_log_error_hint(...)
#define pg_log_error_detail(...)
enum _archiveFormat ArchiveFormat
void exit_nicely(int code)
static void dumpTimestamp(const char *msg)
static int on_conflict_do_nothing
int main(int argc, char *argv[])
static void dropTablespaces(PGconn *conn)
static void expand_dbname_patterns(PGconn *conn, SimpleStringList *patterns, SimpleStringList *names)
static int no_role_passwords
static PQExpBuffer pgdumpopts
static int statistics_only
static int no_table_access_method
static int no_unlogged_table_data
static int binary_upgrade
static int disable_triggers
static void dumpUserConfig(PGconn *conn, const char *username)
static const char * connstr
static SimpleStringList database_exclude_patterns
static void dumpTablespaces(PGconn *conn)
static void dumpRoleMembership(PGconn *conn)
static char pg_dump_bin[MAXPGPATH]
static SimpleStringList database_exclude_names
static int no_publications
static int no_security_labels
static ArchiveFormat parseDumpFormat(const char *format)
static void dumpDatabases(PGconn *conn, ArchiveFormat archDumpFormat)
static int disable_dollar_quoting
static void buildShSecLabels(PGconn *conn, const char *catalog_name, Oid objectId, const char *objtype, const char *objname, PQExpBuffer buffer)
static int no_tablespaces
static int no_toast_compression
static void executeCommand(PGconn *conn, const char *query)
static void dumpRoleGUCPrivs(PGconn *conn)
static void dumpRoles(PGconn *conn)
static int use_setsessauth
static int load_via_partition_root
static int column_inserts
static void read_dumpall_filters(const char *filename, SimpleStringList *pattern)
static void dropRoles(PGconn *conn)
static void dropDBs(PGconn *conn)
static int server_version
static char role_catalog[10]
static int no_subscriptions
static int with_statistics
static int runPgDump(const char *dbname, const char *create_opts, char *dbfile, ArchiveFormat archDumpFormat)
#define PGDUMP_VERSIONSTR
PGDLLIMPORT char * optarg
#define pg_encoding_to_char
static const char * pghost
static const char * pgport
#define pg_log_warning(...)
#define is_absolute_path(filename)
int pg_strcasecmp(const char *s1, const char *s2)
const char * get_progname(const char *argv0)
size_t strlcpy(char *dst, const char *src, size_t siz)
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
PQExpBuffer createPQExpBuffer(void)
void initPQExpBuffer(PQExpBuffer str)
void resetPQExpBuffer(PQExpBuffer str)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void destroyPQExpBuffer(PQExpBuffer str)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
void termPQExpBuffer(PQExpBuffer str)
bool quote_all_identifiers
bool simple_string_list_member(SimpleStringList *list, const char *val)
void simple_string_list_append(SimpleStringList *list, const char *val)
const char * fmtId(const char *rawid)
void setFmtEncoding(int encoding)
void appendShellString(PQExpBuffer buf, const char *str)
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
bool processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern, bool have_where, bool force_escape, const char *schemavar, const char *namevar, const char *altnamevar, const char *visibilityrule, PQExpBuffer dbnamebuf, int *dotcnt)
void appendConnStrVal(PQExpBuffer buf, const char *str)
struct SimpleStringListCell * next
SimpleStringListCell * head