43 #define TEXT_DUMP_HEADER "--\n-- PostgreSQL database dump\n--\n\n" 44 #define TEXT_DUMPALL_HEADER "--\n-- PostgreSQL database cluster dump\n--\n\n" 130 void *callback_data);
134 void *callback_data);
246 mode, setupDumpWorker);
274 res = fclose(AH->
OF);
277 fatal(
"could not close output file: %m");
285 if (dopt == NULL && ropt != NULL)
331 fatal(
"unexpected section code %d",
368 fatal(
"parallel restore is not supported with this archive file format");
372 fatal(
"parallel restore is not supported with archives made by pre-8.0 pg_dump");
390 fatal(
"cannot restore from compressed archive (compression not supported in this installation)");
409 fatal(
"direct database connections are not supported in pre-1.3 archives");
439 int impliedDataOnly = 1;
463 ahprintf(AH,
"--\n-- PostgreSQL database dump\n--\n\n");
466 ahprintf(AH,
"-- Dumped from database version %s\n",
469 ahprintf(AH,
"-- Dumped by pg_dump version %s\n",
510 if (strcmp(te->
desc,
"DATABASE") != 0 &&
511 strcmp(te->
desc,
"DATABASE PROPERTIES") != 0)
544 if (strncmp(te->
desc,
"BLOB", 4) == 0)
551 char *dropStmtOrig = dropStmt;
558 if (strncmp(dropStmt,
"ALTER TABLE", 11) == 0)
561 "ALTER TABLE IF EXISTS");
562 dropStmt = dropStmt + 11;
584 if (strcmp(te->
desc,
"DEFAULT") == 0 ||
585 strcmp(te->
desc,
"DATABASE PROPERTIES") == 0 ||
586 strncmp(dropStmt,
"CREATE OR REPLACE VIEW", 22) == 0)
593 if (strcmp(te->
desc,
"CONSTRAINT") == 0 ||
594 strcmp(te->
desc,
"CHECK CONSTRAINT") == 0 ||
595 strcmp(te->
desc,
"FK CONSTRAINT") == 0)
596 strcpy(buffer,
"DROP CONSTRAINT");
598 snprintf(buffer,
sizeof(buffer),
"DROP %s",
601 mark = strstr(dropStmt, buffer);
608 mark + strlen(buffer));
613 pg_log_warning(
"could not find where to insert IF EXISTS in statement \"%s\"",
680 bool haveACL =
false;
681 bool havePostACL =
false;
696 case RESTORE_PASS_POST_ACL:
734 ahprintf(AH,
"--\n-- PostgreSQL database dump complete\n--\n\n");
787 te->
desc, te->namespace, te->
tag);
795 if (strcmp(te->
desc,
"TABLE") == 0)
836 if (strcmp(te->
desc,
"DATABASE") == 0 ||
837 strcmp(te->
desc,
"DATABASE PROPERTIES") == 0)
863 if (strcmp(te->
desc,
"BLOBS") == 0 ||
864 strcmp(te->
desc,
"BLOB COMMENTS") == 0)
871 if (strcmp(te->
desc,
"BLOB COMMENTS") == 0)
887 te->namespace, te->
tag);
897 if (is_parallel && te->
created)
910 ahprintf(AH,
"TRUNCATE TABLE %s%s;\n\n",
938 if (is_parallel && te->
created)
945 else if (!defnDumped)
1000 ahprintf(AH,
"ALTER TABLE %s DISABLE TRIGGER ALL;\n\n",
1026 ahprintf(AH,
"ALTER TABLE %s ENABLE TRIGGER ALL;\n\n",
1041 fatal(
"internal error -- WriteData cannot be called outside the context of a DataDumper routine");
1076 newToc->
section = opts->section;
1079 newToc->namespace = opts->namespace ?
pg_strdup(opts->namespace) : NULL;
1084 newToc->
defn = opts->createStmt ?
pg_strdup(opts->createStmt) : NULL;
1088 if (opts->nDeps > 0)
1092 newToc->
nDeps = opts->nDeps;
1122 const char *fmtName;
1131 strcpy(stamp_str,
"[unknown]");
1133 ahprintf(AH,
";\n; Archive created at %s\n", stamp_str);
1134 ahprintf(AH,
"; dbname: %s\n; TOC Entries: %d\n; Compression: %d\n",
1144 fmtName =
"DIRECTORY";
1150 fmtName =
"UNKNOWN";
1153 ahprintf(AH,
"; Dump Version: %d.%d-%d\n",
1155 ahprintf(AH,
"; Format: %s\n", fmtName);
1159 ahprintf(AH,
"; Dumped from database version: %s\n",
1162 ahprintf(AH,
"; Dumped by pg_dump version: %s\n",
1165 ahprintf(AH,
";\n;\n; Selected TOC Entries:\n;\n");
1175 char *sanitized_name;
1176 char *sanitized_schema;
1177 char *sanitized_owner;
1187 te->
desc, sanitized_schema, sanitized_name,
1190 free(sanitized_name);
1191 free(sanitized_schema);
1192 free(sanitized_owner);
1199 for (i = 0; i < te->
nDeps; i++)
1224 fatal(
"large-object output not supported in chosen format");
1283 "restored %d large objects",
1303 pg_log_info(
"restoring large object with OID %u", oid);
1306 if (old_blob_style && drop)
1314 if (loOid == 0 || loOid != oid)
1315 fatal(
"could not create large object %u: %s",
1320 fatal(
"could not open large object %u: %s",
1326 ahprintf(AH,
"SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n",
1329 ahprintf(AH,
"SELECT pg_catalog.lo_open('%u', %d);\n",
1354 ahprintf(AH,
"SELECT pg_catalog.lo_close(0);\n\n");
1376 fatal(
"could not open TOC file \"%s\": %m", ropt->
tocFile);
1388 cmnt = strchr(linebuf.
data,
';');
1392 linebuf.
len = cmnt - linebuf.
data;
1396 if (strspn(linebuf.
data,
" \t\r\n") == linebuf.
len)
1400 id = strtol(linebuf.
data, &endptr, 10);
1401 if (endptr == linebuf.
data || id <= 0 || id > AH->
maxDumpId ||
1411 fatal(
"could not find entry for ID %d",
1433 if (fclose(fh) != 0)
1434 fatal(
"could not close TOC file: %m");
1453 int save_errno = errno;
1467 va_start(args, fmt);
1496 if (strcmp(filename,
"-") == 0)
1502 fn = fileno(AH->
FH);
1506 filename = AH->
fSpec;
1513 if (compression != 0)
1518 sprintf(fmode,
"wb%d", compression);
1520 AH->
OF = gzdopen(dup(fn), fmode);
1522 AH->
OF = gzopen(filename, fmode);
1548 fatal(
"could not open output file \"%s\": %m", filename);
1550 fatal(
"could not open output file: %m");
1573 res = fclose(AH->
OF);
1576 fatal(
"could not close output file: %m");
1579 AH->
OF = savedContext.
OF;
1590 int save_errno = errno;
1604 va_start(args, fmt);
1644 "wrote %zu bytes of large object data (result = %d)",
1657 (
const unsigned char *) AH->
lo_buf,
1663 ahprintf(AH,
"SELECT pg_catalog.lowrite(0, %s);\n", buf->
data);
1681 int bytes_written = 0;
1692 ptr = (
const void *) ((
const char *) ptr + avail);
1701 bytes_written = size * nmemb;
1704 bytes_written =
GZWRITE(ptr, size, nmemb, AH->
OF);
1706 bytes_written = AH->
CustomOutPtr(AH, ptr, size * nmemb);
1717 bytes_written = fwrite(ptr, size, nmemb, AH->
OF) * size;
1720 if (bytes_written != size * nmemb)
1830 fatal(
"bad dumpId");
1841 if (strcmp(te->
desc,
"TABLE DATA") == 0 && te->
nDeps > 0)
1850 if (tableId <= 0 || tableId > maxDumpId)
1851 fatal(
"bad table dumpId for TABLE DATA item");
1865 if (
id > 0 && id <= AH->maxDumpId)
1891 for (off = 0; off <
sizeof(
pgoff_t); off++)
1943 fatal(
"unexpected data offset flag %d", offsetFlg);
1949 for (off = 0; off < AH->
offSize; off++)
1956 fatal(
"file offset in dump file is too large");
1985 for (b = 0; b < AH->
intSize; b++)
2007 for (b = 0; b < AH->
intSize; b++)
2011 res = res + (bv << bitShift);
2028 int len = strlen(c);
2068 pg_log_debug(
"attempting to ascertain archive format");
2093 fatal(
"directory name too long: \"%s\"",
2103 fatal(
"directory name too long: \"%s\"",
2111 fatal(
"directory \"%s\" does not appear to be a valid archive (\"toc.dat\" does not exist)",
2119 fatal(
"could not open input file \"%s\": %m", AH->
fSpec);
2126 fatal(
"could not open input file: %m");
2129 if ((cnt = fread(sig, 1, 5, fh)) != 5)
2132 fatal(
"could not read input file: %m");
2134 fatal(
"input file is too short (read %lu, expected 5)",
2135 (
unsigned long) cnt);
2142 if (strncmp(sig,
"PGDMP", 5) == 0)
2154 if ((byteread = fgetc(fh)) == EOF)
2159 if ((byteread = fgetc(fh)) == EOF)
2169 if (vmaj > 1 || (vmaj == 1 && vmin > 0))
2171 if ((byteread = fgetc(fh)) == EOF)
2182 if ((AH->
intSize = fgetc(fh)) == EOF)
2188 if ((AH->
offSize = fgetc(fh)) == EOF)
2195 if ((byteread = fgetc(fh)) == EOF)
2219 fatal(
"input file appears to be a text format dump. Please use psql.");
2225 fatal(
"input file does not appear to be a valid archive (too short?)");
2231 fatal(
"input file does not appear to be a valid archive");
2237 if (
fseeko(fh, 0, SEEK_SET) != 0)
2250 if (fclose(fh) != 0)
2251 fatal(
"could not close input file: %m");
2268 FileSpec ? FileSpec :
"(stdio)", fmt);
2328 if ((fmt !=
archNull || compression != 0) &&
2329 (AH->
fSpec == NULL || strcmp(AH->
fSpec,
"") == 0))
2332 _setmode(fileno(
stdout), O_BINARY);
2334 _setmode(fileno(stdin), O_BINARY);
2364 fatal(
"unrecognized file format \"%d\"", fmt);
2408 for (
int i = 0;
i < ntes;
i++)
2444 void *callback_data)
2450 fatal(
"worker process failed: exit code %d",
2463 if (strcmp(te->
desc,
"BLOBS") == 0)
2474 if (startPtr != NULL)
2475 (*startPtr) (AH, te);
2535 for (i = 0; i < te->
nDeps; i++)
2570 fatal(
"entry ID %d out of range -- perhaps a corrupt TOC",
2601 if (strcmp(te->
desc,
"COMMENT") == 0 ||
2602 strcmp(te->
desc,
"ACL") == 0 ||
2603 strcmp(te->
desc,
"ACL LANGUAGE") == 0)
2605 else if (strcmp(te->
desc,
"TABLE DATA") == 0 ||
2606 strcmp(te->
desc,
"BLOBS") == 0 ||
2607 strcmp(te->
desc,
"BLOB COMMENTS") == 0)
2609 else if (strcmp(te->
desc,
"CONSTRAINT") == 0 ||
2610 strcmp(te->
desc,
"CHECK CONSTRAINT") == 0 ||
2611 strcmp(te->
desc,
"FK CONSTRAINT") == 0 ||
2612 strcmp(te->
desc,
"INDEX") == 0 ||
2613 strcmp(te->
desc,
"RULE") == 0 ||
2614 strcmp(te->
desc,
"TRIGGER") == 0)
2637 pg_log_warning(
"restoring tables WITH OIDS is not supported anymore");
2650 if (depIdx >= depSize)
2655 sscanf(tmp,
"%d", &deps[depIdx]);
2693 if (strcmp(te->
desc,
"ENCODING") == 0)
2695 else if (strcmp(te->
desc,
"STDSTRINGS") == 0)
2697 else if (strcmp(te->
desc,
"SEARCHPATH") == 0)
2711 ptr1 = strchr(defn,
'\'');
2713 ptr2 = strchr(++ptr1,
'\'');
2719 fatal(
"unrecognized encoding \"%s\"",
2724 fatal(
"invalid ENCODING item: %s",
2736 ptr1 = strchr(te->
defn,
'\'');
2737 if (ptr1 && strncmp(ptr1,
"'on'", 4) == 0)
2739 else if (ptr1 && strncmp(ptr1,
"'off'", 5) == 0)
2742 fatal(
"invalid STDSTRINGS item: %s",
2759 const char *missing_name;
2766 if (missing_name != NULL)
2767 fatal(
"schema \"%s\" not found", missing_name);
2773 if (missing_name != NULL)
2774 fatal(
"table \"%s\" not found", missing_name);
2780 if (missing_name != NULL)
2781 fatal(
"index \"%s\" not found", missing_name);
2787 if (missing_name != NULL)
2788 fatal(
"function \"%s\" not found", missing_name);
2794 if (missing_name != NULL)
2795 fatal(
"trigger \"%s\" not found", missing_name);
2813 if (strcmp(te->
desc,
"ENCODING") == 0 ||
2814 strcmp(te->
desc,
"STDSTRINGS") == 0 ||
2815 strcmp(te->
desc,
"SEARCHPATH") == 0)
2823 if (strcmp(te->
desc,
"DATABASE") == 0 ||
2824 strcmp(te->
desc,
"DATABASE PROPERTIES") == 0)
2849 (strcmp(te->
desc,
"PUBLICATION") == 0 ||
2850 strcmp(te->
desc,
"PUBLICATION TABLE") == 0))
2888 if (strcmp(te->
desc,
"ACL") == 0 ||
2889 strcmp(te->
desc,
"COMMENT") == 0 ||
2890 strcmp(te->
desc,
"SECURITY LABEL") == 0)
2893 if (strncmp(te->
tag,
"DATABASE ", 9) == 0)
2918 if (te->
nDeps != 1 ||
2942 if (strcmp(te->
desc,
"TABLE") == 0 ||
2943 strcmp(te->
desc,
"TABLE DATA") == 0 ||
2944 strcmp(te->
desc,
"VIEW") == 0 ||
2945 strcmp(te->
desc,
"FOREIGN TABLE") == 0 ||
2946 strcmp(te->
desc,
"MATERIALIZED VIEW") == 0 ||
2947 strcmp(te->
desc,
"MATERIALIZED VIEW DATA") == 0 ||
2948 strcmp(te->
desc,
"SEQUENCE") == 0 ||
2949 strcmp(te->
desc,
"SEQUENCE SET") == 0)
2957 else if (strcmp(te->
desc,
"INDEX") == 0)
2965 else if (strcmp(te->
desc,
"FUNCTION") == 0 ||
2966 strcmp(te->
desc,
"AGGREGATE") == 0 ||
2967 strcmp(te->
desc,
"PROCEDURE") == 0)
2975 else if (strcmp(te->
desc,
"TRIGGER") == 0)
3003 if (strcmp(te->
desc,
"SEQUENCE SET") == 0 ||
3004 strcmp(te->
desc,
"BLOB") == 0 ||
3005 (strcmp(te->
desc,
"ACL") == 0 &&
3006 strncmp(te->
tag,
"LARGE OBJECT ", 13) == 0) ||
3007 (strcmp(te->
desc,
"COMMENT") == 0 &&
3008 strncmp(te->
tag,
"LARGE OBJECT ", 13) == 0) ||
3009 (strcmp(te->
desc,
"SECURITY LABEL") == 0 &&
3010 strncmp(te->
tag,
"LARGE OBJECT ", 13) == 0))
3013 res = res & ~REQ_DATA;
3024 if ((strcmp(te->
desc,
"<Init>") == 0) && (strcmp(te->
tag,
"Max OID") == 0))
3040 (strcmp(te->
desc,
"BLOB") == 0 ||
3041 (strcmp(te->
desc,
"ACL") == 0 &&
3042 strncmp(te->
tag,
"LARGE OBJECT ", 13) == 0) ||
3043 (strcmp(te->
desc,
"COMMENT") == 0 &&
3044 strncmp(te->
tag,
"LARGE OBJECT ", 13) == 0) ||
3045 (strcmp(te->
desc,
"SECURITY LABEL") == 0 &&
3046 strncmp(te->
tag,
"LARGE OBJECT ", 13) == 0))))
3066 if (strcmp(te->
desc,
"ACL") == 0 ||
3067 strcmp(te->
desc,
"ACL LANGUAGE") == 0 ||
3068 strcmp(te->
desc,
"DEFAULT ACL") == 0)
3070 if (strcmp(te->
desc,
"EVENT TRIGGER") == 0 ||
3071 strcmp(te->
desc,
"MATERIALIZED VIEW DATA") == 0)
3072 return RESTORE_PASS_POST_ACL;
3080 if (strcmp(te->
desc,
"COMMENT") == 0 &&
3081 strncmp(te->
tag,
"EVENT TRIGGER ", 14) == 0)
3082 return RESTORE_PASS_POST_ACL;
3099 if (strcmp(te->
desc,
"ACL") == 0 ||
3100 strcmp(te->
desc,
"ACL LANGUAGE") == 0 ||
3101 strcmp(te->
desc,
"DEFAULT ACL") == 0)
3118 ahprintf(AH,
"SET statement_timeout = 0;\n");
3119 ahprintf(AH,
"SET lock_timeout = 0;\n");
3120 ahprintf(AH,
"SET idle_in_transaction_session_timeout = 0;\n");
3123 ahprintf(AH,
"SET client_encoding = '%s';\n",
3127 ahprintf(AH,
"SET standard_conforming_strings = %s;\n",
3139 ahprintf(AH,
"SET check_function_bodies = false;\n");
3142 ahprintf(AH,
"SET xmloption = content;\n");
3145 ahprintf(AH,
"SET client_min_messages = warning;\n");
3147 ahprintf(AH,
"SET escape_string_warning = off;\n");
3151 ahprintf(AH,
"SET row_security = on;\n");
3153 ahprintf(AH,
"SET row_security = off;\n");
3187 fatal(
"could not set session user to \"%s\": %s",
3300 if (!schemaName || *schemaName ==
'\0' ||
3308 if (strcmp(schemaName,
"pg_catalog") != 0)
3319 "could not set search_path to \"%s\": %s",
3357 if (have && strcmp(want, have) == 0)
3362 if (strcmp(want,
"") == 0)
3381 "could not set default_tablespace to %s: %s",
3412 if (have && strcmp(want, have) == 0)
3426 "could not set default_table_access_method: %s",
3450 if (strcmp(type,
"VIEW") == 0 || strcmp(type,
"SEQUENCE") == 0 ||
3451 strcmp(type,
"MATERIALIZED VIEW") == 0)
3455 if (strcmp(type,
"COLLATION") == 0 ||
3456 strcmp(type,
"CONVERSION") == 0 ||
3457 strcmp(type,
"DOMAIN") == 0 ||
3458 strcmp(type,
"TABLE") == 0 ||
3459 strcmp(type,
"TYPE") == 0 ||
3460 strcmp(type,
"FOREIGN TABLE") == 0 ||
3461 strcmp(type,
"TEXT SEARCH DICTIONARY") == 0 ||
3462 strcmp(type,
"TEXT SEARCH CONFIGURATION") == 0 ||
3463 strcmp(type,
"STATISTICS") == 0 ||
3465 strcmp(type,
"DATABASE") == 0 ||
3466 strcmp(type,
"PROCEDURAL LANGUAGE") == 0 ||
3467 strcmp(type,
"SCHEMA") == 0 ||
3468 strcmp(type,
"EVENT TRIGGER") == 0 ||
3469 strcmp(type,
"FOREIGN DATA WRAPPER") == 0 ||
3470 strcmp(type,
"SERVER") == 0 ||
3471 strcmp(type,
"PUBLICATION") == 0 ||
3472 strcmp(type,
"SUBSCRIPTION") == 0 ||
3473 strcmp(type,
"USER MAPPING") == 0)
3476 if (te->namespace && *te->namespace)
3483 if (strcmp(type,
"BLOB") == 0)
3493 if (strcmp(type,
"AGGREGATE") == 0 ||
3494 strcmp(type,
"FUNCTION") == 0 ||
3495 strcmp(type,
"OPERATOR") == 0 ||
3496 strcmp(type,
"OPERATOR CLASS") == 0 ||
3497 strcmp(type,
"OPERATOR FAMILY") == 0 ||
3498 strcmp(type,
"PROCEDURE") == 0)
3505 last = first + strlen(first) - 1;
3508 while (last >= first && (*last ==
'\n' || *last ==
';'))
3518 pg_log_warning(
"don't know how to set owner for object type \"%s\"",
3544 char *sanitized_name;
3545 char *sanitized_schema;
3546 char *sanitized_owner;
3556 ahprintf(AH,
"-- TOC entry %d (class %u OID %u)\n",
3563 for (i = 0; i < te->
nDeps; i++)
3573 ahprintf(AH,
"-- %sName: %s; Type: %s; Schema: %s; Owner: %s",
3574 pfx, sanitized_name, te->
desc, sanitized_schema,
3577 free(sanitized_name);
3578 free(sanitized_schema);
3579 free(sanitized_owner);
3583 char *sanitized_tablespace;
3586 ahprintf(AH,
"; Tablespace: %s", sanitized_tablespace);
3587 free(sanitized_tablespace);
3603 if (ropt->
noOwner && strcmp(te->
desc,
"SCHEMA") == 0)
3609 if (te->
defn && strlen(te->
defn) > 0)
3623 if (strcmp(te->
desc,
"AGGREGATE") == 0 ||
3624 strcmp(te->
desc,
"BLOB") == 0 ||
3625 strcmp(te->
desc,
"COLLATION") == 0 ||
3626 strcmp(te->
desc,
"CONVERSION") == 0 ||
3627 strcmp(te->
desc,
"DATABASE") == 0 ||
3628 strcmp(te->
desc,
"DOMAIN") == 0 ||
3629 strcmp(te->
desc,
"FUNCTION") == 0 ||
3630 strcmp(te->
desc,
"OPERATOR") == 0 ||
3631 strcmp(te->
desc,
"OPERATOR CLASS") == 0 ||
3632 strcmp(te->
desc,
"OPERATOR FAMILY") == 0 ||
3633 strcmp(te->
desc,
"PROCEDURE") == 0 ||
3634 strcmp(te->
desc,
"PROCEDURAL LANGUAGE") == 0 ||
3635 strcmp(te->
desc,
"SCHEMA") == 0 ||
3636 strcmp(te->
desc,
"EVENT TRIGGER") == 0 ||
3637 strcmp(te->
desc,
"TABLE") == 0 ||
3638 strcmp(te->
desc,
"TYPE") == 0 ||
3639 strcmp(te->
desc,
"VIEW") == 0 ||
3640 strcmp(te->
desc,
"MATERIALIZED VIEW") == 0 ||
3641 strcmp(te->
desc,
"SEQUENCE") == 0 ||
3642 strcmp(te->
desc,
"FOREIGN TABLE") == 0 ||
3643 strcmp(te->
desc,
"TEXT SEARCH DICTIONARY") == 0 ||
3644 strcmp(te->
desc,
"TEXT SEARCH CONFIGURATION") == 0 ||
3645 strcmp(te->
desc,
"FOREIGN DATA WRAPPER") == 0 ||
3646 strcmp(te->
desc,
"SERVER") == 0 ||
3647 strcmp(te->
desc,
"STATISTICS") == 0 ||
3648 strcmp(te->
desc,
"PUBLICATION") == 0 ||
3649 strcmp(te->
desc,
"SUBSCRIPTION") == 0)
3659 else if (strcmp(te->
desc,
"CAST") == 0 ||
3660 strcmp(te->
desc,
"CHECK CONSTRAINT") == 0 ||
3661 strcmp(te->
desc,
"CONSTRAINT") == 0 ||
3662 strcmp(te->
desc,
"DATABASE PROPERTIES") == 0 ||
3663 strcmp(te->
desc,
"DEFAULT") == 0 ||
3664 strcmp(te->
desc,
"FK CONSTRAINT") == 0 ||
3665 strcmp(te->
desc,
"INDEX") == 0 ||
3666 strcmp(te->
desc,
"RULE") == 0 ||
3667 strcmp(te->
desc,
"TRIGGER") == 0 ||
3668 strcmp(te->
desc,
"ROW SECURITY") == 0 ||
3669 strcmp(te->
desc,
"POLICY") == 0 ||
3670 strcmp(te->
desc,
"USER MAPPING") == 0)
3676 pg_log_warning(
"don't know how to set owner for object type \"%s\"",
3716 return pg_strdup(want_hyphen ?
"-" :
"");
3720 for (s = result; *s !=
'\0'; s++)
3722 if (*s ==
'\n' || *s ==
'\r')
3779 if (strncmp(tmpMag,
"PGDMP", 5) != 0)
3780 fatal(
"did not find magic string in file header");
3785 if (vmaj > 1 || (vmaj == 1 && vmin > 0))
3793 fatal(
"unsupported version (%d.%d) in file header",
3798 fatal(
"sanity check on integer size (%lu) failed",
3801 if (AH->
intSize >
sizeof(
int))
3802 pg_log_warning(
"archive was made on a machine with larger integers, some operations might fail");
3812 fatal(
"expected format (%d) differs from format found in file (%d)",
3828 pg_log_warning(
"archive is compressed, but this installation does not support compression -- no data will be available");
3876 if (
fseeko(fp, tpos, SEEK_SET) != 0)
3892 ahprintf(AH,
"-- %s %s\n\n", msg, buf);
3936 skipped_some =
false;
3937 for (next_work_item = AH->
toc->
next; next_work_item != AH->
toc; next_work_item = next_work_item->
next)
3948 skipped_some =
true;
3976 next_work_item->
desc, next_work_item->
tag);
4059 if (next_work_item != NULL)
4066 next_work_item->
desc, next_work_item->
tag);
4075 next_work_item->
desc, next_work_item->
tag);
4181 for (k = 0; k < te2->
nDeps; k++)
4235 ready_list->
sorted =
false;
4253 ready_list->
sorted =
false;
4262 Assert(i >= f && i <= ready_list->last_te);
4275 memmove(first_te_ptr + 1, first_te_ptr, (i - f) *
sizeof(
TocEntry *));
4292 ready_list->
sorted =
true;
4334 for (te = pending_list->
pending_next; te != pending_list; te = next_te)
4373 for (
int i = ready_list->
first_te; i <= ready_list->last_te;
i++)
4376 bool conflicts =
false;
4387 if (running_te == NULL)
4445 void *callback_data)
4461 else if (status != 0)
4462 fatal(
"worker process failed: exit code %d",
4517 if (strcmp(te->
desc,
"BLOB COMMENTS") == 0 && te->
nDeps == 0)
4523 if (strcmp(te2->
desc,
"BLOBS") == 0)
4550 for (i = 0; i < te->
nDeps; i++)
4554 if (depid <= AH->maxDumpId && AH->
tocsByDumpId[depid] != NULL)
4578 for (i = 0; i < te->
nDeps; i++)
4582 if (depid <= AH->maxDumpId && AH->
tocsByDumpId[depid] != NULL)
4623 for (i = 0; i < te->
nDeps; i++)
4626 if (olddep <= AH->maxDumpId &&
4635 te->
dumpId, olddep, tabledataid);
4670 if (strcmp(te->
desc,
"INDEX") == 0)
4685 for (i = 0; i < te->
nDeps; i++)
4689 if (depid <= AH->maxDumpId && AH->
tocsByDumpId[depid] != NULL &&
4692 lockids[nlockids++] = depid;
4768 pg_log_info(
"table \"%s\" could not be created, will not restore its data",
4823 clone->ClonePtr(clone);
4825 Assert(clone->connection != NULL);
static int TocEntrySizeCompare(const void *p1, const void *p2)
static void processEncodingEntry(ArchiveHandle *AH, TocEntry *te)
bool pg_get_line_buf(FILE *stream, StringInfo buf)
ReadBufPtrType ReadBufPtr
static int RestoringToDB(ArchiveHandle *AH)
void ReadToc(ArchiveHandle *AH)
#define appendByteaLiteralAHX(buf, str, len, AH)
static PgChecksumMode mode
int parallel_restore(ArchiveHandle *AH, TocEntry *te)
void EndDBCopyMode(Archive *AHX, const char *tocEntryTag)
void ConnectDatabase(Archive *AHX, const ConnParams *cparams, bool isReconnect)
void ReadHead(ArchiveHandle *AH)
char * PQerrorMessage(const PGconn *conn)
int noDataForFailedTables
#define Z_DEFAULT_COMPRESSION
static void _printTocEntry(ArchiveHandle *AH, TocEntry *te, bool isData)
DeClonePtrType DeClonePtr
struct _tocEntry * currentTE
static void dumpTimestamp(ArchiveHandle *AH, const char *msg, time_t tim)
static void mark_restore_job_done(ArchiveHandle *AH, TocEntry *te, int status, void *callback_data)
int pg_char_to_encoding(const char *name)
void DispatchJobForTocEntry(ArchiveHandle *AH, ParallelState *pstate, TocEntry *te, T_Action act, ParallelCompletionPtr callback, void *callback_data)
int disable_dollar_quoting
bool simple_string_list_member(SimpleStringList *list, const char *val)
int archprintf(Archive *AH, const char *fmt,...)
int ExecuteSqlCommandBuf(Archive *AHX, const char *buf, size_t bufLen)
int EndBlob(Archive *AHX, Oid oid)
SimpleStringList triggerNames
void DropBlobIfExists(ArchiveHandle *AH, Oid oid)
static ArchiveHandle * _allocAH(const char *FileSpec, const ArchiveFormat fmt, const int compression, bool dosync, ArchiveMode mode, SetupWorkerPtrType setupWorkerPtr)
static void _getObjectDescription(PQExpBuffer buf, TocEntry *te)
void PrintTOCSummary(Archive *AHX)
bool IsEveryWorkerIdle(ParallelState *pstate)
static void setupRestoreWorker(Archive *AHX)
void * pg_malloc(size_t size)
WriteBufPtrType WriteBufPtr
static void _becomeUser(ArchiveHandle *AH, const char *user)
const char * fmtId(const char *rawid)
static void restore_toc_entries_postfork(ArchiveHandle *AH, TocEntry *pending_list)
void termPQExpBuffer(PQExpBuffer str)
const char * simple_string_list_not_touched(SimpleStringList *list)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
static TocEntry * pop_next_work_item(ParallelReadyList *ready_list, ParallelState *pstate)
static void SetOutput(ArchiveHandle *AH, const char *filename, int compression)
void WriteDataChunks(ArchiveHandle *AH, ParallelState *pstate)
void ProcessArchiveRestoreOptions(Archive *AHX)
void warn_or_exit_horribly(ArchiveHandle *AH, const char *fmt,...)
StartBlobPtrType StartBlobPtr
static void ready_list_init(ParallelReadyList *ready_list, int tocCount)
static void ready_list_remove(ParallelReadyList *ready_list, int i)
struct _tocEntry * pending_prev
static void restore_toc_entries_parallel(ArchiveHandle *AH, ParallelState *pstate, TocEntry *pending_list)
EndDataPtrType EndDataPtr
char * archiveDumpVersion
#define WORKER_CREATE_DONE
void WriteToc(ArchiveHandle *AH)
void InitArchiveFmt_Tar(ArchiveHandle *AH)
DumpOptions * NewDumpOptions(void)
int ReadInt(ArchiveHandle *AH)
SimpleStringList schemaNames
StartBlobsPtrType StartBlobsPtr
PGcancel *volatile connCancel
int PQserverVersion(const PGconn *conn)
bool isValidTarHeader(char *header)
static void processStdStringsEntry(ArchiveHandle *AH, TocEntry *te)
SetupWorkerPtrType SetupWorkerPtr
struct _parallelReadyList ParallelReadyList
void CloseArchive(Archive *AHX)
void StartRestoreBlobs(ArchiveHandle *AH)
ExecStatusType PQresultStatus(const PGresult *res)
struct _tocEntry * currToc
ReadBytePtrType ReadBytePtr
#define WORKER_IGNORED_ERRORS
static bool _tocEntryIsACL(TocEntry *te)
void appendPsqlMetaConnect(PQExpBuffer buf, const char *dbname)
struct _tocEntry * pending_next
void DeCloneArchive(ArchiveHandle *AH)
const char * lockWaitTimeout
EndBlobPtrType EndBlobPtr
static void ready_list_sort(ParallelReadyList *ready_list)
static void _moveBefore(TocEntry *pos, TocEntry *te)
void destroyPQExpBuffer(PQExpBuffer str)
int lo_close(PGconn *conn, int fd)
int disable_dollar_quoting
void struct _archiveOpts ArchiveOpts
size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args)
static int restore_toc_entry(ArchiveHandle *AH, TocEntry *te, bool is_parallel)
RestoreOptions * NewRestoreOptions(void)
void * pg_malloc0(size_t size)
#define pg_log_debug(...)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
#define MAKE_ARCHIVE_VERSION(major, minor, rev)
void StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop)
static void move_to_ready_list(TocEntry *pending_list, ParallelReadyList *ready_list, RestorePass pass)
const char * lockWaitTimeout
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
static void reduce_dependencies(ArchiveHandle *AH, TocEntry *te, ParallelReadyList *ready_list)
SimpleStringList tableNames
TocEntry * getTocEntryByDumpId(ArchiveHandle *AH, DumpId id)
static void _selectTableAccessMethod(ArchiveHandle *AH, const char *tableam)
StartDataPtrType StartDataPtr
static void processSearchPathEntry(ArchiveHandle *AH, TocEntry *te)
ArchiveHandle * CloneArchive(ArchiveHandle *AH)
static RestorePass _tocEntryRestorePass(TocEntry *te)
char * archiveRemoteVersion
static void dump_lo_buf(ArchiveHandle *AH)
SimpleStringList functionNames
#define ARCHIVE_REV(version)
char * pg_strdup(const char *in)
void ReconnectToServer(ArchiveHandle *AH, const char *dbname)
static void _doSetFixedOutputState(ArchiveHandle *AH)
void SetArchiveOptions(Archive *AH, DumpOptions *dopt, RestoreOptions *ropt)
Archive * CreateArchive(const char *FileSpec, const ArchiveFormat fmt, const int compression, bool dosync, ArchiveMode mode, SetupWorkerPtrType setupDumpWorker)
int StartBlob(Archive *AHX, Oid oid)
DumpOptions * dumpOptionsFromRestoreOptions(RestoreOptions *ropt)
Archive * OpenArchive(const char *FileSpec, const ArchiveFormat fmt)
static void buildTocEntryArrays(ArchiveHandle *AH)
void * pg_realloc(void *ptr, size_t size)
static void _becomeOwner(ArchiveHandle *AH, TocEntry *te)
WriteBytePtrType WriteBytePtr
void WriteData(Archive *AHX, const void *data, size_t dLen)
static void inhibit_data_for_failed_table(ArchiveHandle *AH, TocEntry *te)
int lo_open(PGconn *conn, Oid lobjId, int mode)
void archputs(const char *s, Archive *AH)
int lo_write(int fd, const char *buf, int len)
void DisconnectDatabase(Archive *AHX)
static void pending_list_header_init(TocEntry *l)
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
ParallelState * ParallelBackupStart(ArchiveHandle *AH)
size_t WriteOffset(ArchiveHandle *AH, pgoff_t o, int wasSet)
#define WORKER_INHIBIT_DATA
#define ARCHIVE_MINOR(version)
void initStringInfo(StringInfo str)
void SortTocFromFile(Archive *AHX)
static void mark_create_done(ArchiveHandle *AH, TocEntry *te)
#define GZWRITE(p, s, n, fh)
void ParallelBackupEnd(ArchiveHandle *AH, ParallelState *pstate)
void EndRestoreBlobs(ArchiveHandle *AH)
static void _reconnectToDB(ArchiveHandle *AH, const char *dbname)
#define ngettext(s, p, n)
void(* StartDataPtrType)(ArchiveHandle *AH, TocEntry *te)
#define exit_nicely(code)
static int _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH)
int ReadOffset(ArchiveHandle *AH, pgoff_t *o)
#define TEXT_DUMPALL_HEADER
static void fix_dependencies(ArchiveHandle *AH)
PQExpBuffer createPQExpBuffer(void)
static void StrictNamesCheck(RestoreOptions *ropt)
static void _doSetSessionAuth(ArchiveHandle *AH, const char *user)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
static int _discoverArchiveFormat(ArchiveHandle *AH)
#define ARCHIVE_MAJOR(version)
PrintTocDataPtrType PrintTocDataPtr
static OutputContext SaveOutput(ArchiveHandle *AH)
void WaitForWorkers(ArchiveHandle *AH, ParallelState *pstate, WFW_WaitOption mode)
#define fseeko(stream, offset, origin)
WriteDataPtrType WriteDataPtr
void InitArchiveFmt_Directory(ArchiveHandle *AH)
enum _archiveFormat ArchiveFormat
void PQclear(PGresult *res)
void pg_log_generic(enum pg_log_level level, const char *pg_restrict fmt,...)
void InitArchiveFmt_Null(ArchiveHandle *AH)
static void * fn(void *arg)
static void StartTransaction(void)
const char * pg_encoding_to_char(int encoding)
enum _teSection teSection
Oid lo_create(PGconn *conn, Oid lobjId)
char * PQdb(const PGconn *conn)
void InitArchiveFmt_Custom(ArchiveHandle *AH)
CustomOutPtrType CustomOutPtr
#define Assert(condition)
char * ReadStr(ArchiveHandle *AH)
void(* EndDataPtrType)(ArchiveHandle *AH, TocEntry *te)
PrepParallelRestorePtrType PrepParallelRestorePtr
EndBlobsPtrType EndBlobsPtr
SimpleStringListCell * head
void pg_log_generic_v(enum pg_log_level level, const char *pg_restrict fmt, va_list ap)
size_t WriteInt(ArchiveHandle *AH, int i)
Definition: