1 /*-------------------------------------------------------------------------
5 * Private implementation of the archiver routines.
7 * See the headers to pg_restore for more details.
9 * Copyright (c) 2000, Philip Warner
10 * Rights are granted to use this software in any way so long
11 * as this notice is not removed.
13 * The author is not responsible for loss or damages that may
14 * result from its use.
18 * src/bin/pg_dump/pg_backup_archiver.c
20 *-------------------------------------------------------------------------
23 #include "pg_backup_db.h"
24 #include "dumputils.h"
28 #include <sys/types.h>
35 #include "libpq/libpq-fs.h"
38 * Special exit values from worker children. We reserve 0 for normal
39 * success; 1 and other small values should be interpreted as crashes.
41 #define WORKER_CREATE_DONE 10
42 #define WORKER_INHIBIT_DATA 11
43 #define WORKER_IGNORED_ERRORS 12
46 * Unix uses exit to return result from worker child, so function is void.
47 * Windows thread result comes via function return.
50 #define parallel_restore_result void
52 #define parallel_restore_result DWORD
55 /* IDs for worker children are either PIDs or thread handles */
59 #define thandle HANDLE
62 /* Arguments needed for a worker child */
63 typedef struct _restore_args
69 /* State for each parallel activity slot */
70 typedef struct _parallel_slot
78 /* state needed to save/restore an archive's output target */
79 typedef struct _outputContext
87 static const char *modulename = gettext_noop("archiver");
89 /* index array created by fix_dependencies -- only used in parallel restore */
90 static TocEntry **tocsByDumpId; /* index by dumpId - 1 */
91 static DumpId maxDumpId; /* length of above array */
94 static ArchiveHandle *_allocAH(const char *FileSpec, const ArchiveFormat fmt,
95 const int compression, ArchiveMode mode);
96 static void _getObjectDescription(PQExpBuffer buf, TocEntry *te,
98 static void _printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isData, bool acl_pass);
101 static void _doSetFixedOutputState(ArchiveHandle *AH);
102 static void _doSetSessionAuth(ArchiveHandle *AH, const char *user);
103 static void _doSetWithOids(ArchiveHandle *AH, const bool withOids);
104 static void _reconnectToDB(ArchiveHandle *AH, const char *dbname);
105 static void _becomeUser(ArchiveHandle *AH, const char *user);
106 static void _becomeOwner(ArchiveHandle *AH, TocEntry *te);
107 static void _selectOutputSchema(ArchiveHandle *AH, const char *schemaName);
108 static void _selectTablespace(ArchiveHandle *AH, const char *tablespace);
109 static void processEncodingEntry(ArchiveHandle *AH, TocEntry *te);
110 static void processStdStringsEntry(ArchiveHandle *AH, TocEntry *te);
111 static teReqs _tocEntryRequired(TocEntry *te, RestoreOptions *ropt, bool include_acls);
112 static bool _tocEntryIsACL(TocEntry *te);
113 static void _disableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
114 static void _enableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
115 static TocEntry *getTocEntryByDumpId(ArchiveHandle *AH, DumpId id);
116 static void _moveBefore(ArchiveHandle *AH, TocEntry *pos, TocEntry *te);
117 static int _discoverArchiveFormat(ArchiveHandle *AH);
119 static void dump_lo_buf(ArchiveHandle *AH);
120 static void _write_msg(const char *modulename, const char *fmt, va_list ap);
121 static void _die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt, va_list ap);
123 static void dumpTimestamp(ArchiveHandle *AH, const char *msg, time_t tim);
124 static void SetOutput(ArchiveHandle *AH, char *filename, int compression);
125 static OutputContext SaveOutput(ArchiveHandle *AH);
126 static void RestoreOutput(ArchiveHandle *AH, OutputContext savedContext);
128 static int restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
129 RestoreOptions *ropt, bool is_parallel);
130 static void restore_toc_entries_parallel(ArchiveHandle *AH);
131 static thandle spawn_restore(RestoreArgs *args);
132 static thandle reap_child(ParallelSlot *slots, int n_slots, int *work_status);
133 static bool work_in_progress(ParallelSlot *slots, int n_slots);
134 static int get_next_slot(ParallelSlot *slots, int n_slots);
135 static void par_list_header_init(TocEntry *l);
136 static void par_list_append(TocEntry *l, TocEntry *te);
137 static void par_list_remove(TocEntry *te);
138 static TocEntry *get_next_work_item(ArchiveHandle *AH,
139 TocEntry *ready_list,
140 ParallelSlot *slots, int n_slots);
141 static parallel_restore_result parallel_restore(RestoreArgs *args);
142 static void mark_work_done(ArchiveHandle *AH, TocEntry *ready_list,
143 thandle worker, int status,
144 ParallelSlot *slots, int n_slots);
145 static void fix_dependencies(ArchiveHandle *AH);
146 static bool has_lock_conflicts(TocEntry *te1, TocEntry *te2);
147 static void repoint_table_dependencies(ArchiveHandle *AH,
148 DumpId tableId, DumpId tableDataId);
149 static void identify_locking_dependencies(TocEntry *te);
150 static void reduce_dependencies(ArchiveHandle *AH, TocEntry *te,
151 TocEntry *ready_list);
152 static void mark_create_done(ArchiveHandle *AH, TocEntry *te);
153 static void inhibit_data_for_failed_table(ArchiveHandle *AH, TocEntry *te);
154 static ArchiveHandle *CloneArchive(ArchiveHandle *AH);
155 static void DeCloneArchive(ArchiveHandle *AH);
161 * The objective it to make writing new formats and dumpers as simple
162 * as possible, if necessary at the expense of extra function calls etc.
167 /* Create a new archive */
170 CreateArchive(const char *FileSpec, const ArchiveFormat fmt,
171 const int compression, ArchiveMode mode)
174 ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression, mode);
176 return (Archive *) AH;
179 /* Open an existing archive */
182 OpenArchive(const char *FileSpec, const ArchiveFormat fmt)
184 ArchiveHandle *AH = _allocAH(FileSpec, fmt, 0, archModeRead);
186 return (Archive *) AH;
191 CloseArchive(Archive *AHX)
194 ArchiveHandle *AH = (ArchiveHandle *) AHX;
196 (*AH->ClosePtr) (AH);
198 /* Close the output */
200 res = GZCLOSE(AH->OF);
201 else if (AH->OF != stdout)
202 res = fclose(AH->OF);
205 die_horribly(AH, modulename, "could not close output file: %s\n",
211 RestoreArchive(Archive *AHX, RestoreOptions *ropt)
213 ArchiveHandle *AH = (ArchiveHandle *) AHX;
219 AH->stage = STAGE_INITIALIZING;
222 * Check for nonsensical option combinations.
224 * NB: createDB+dropSchema is useless because if you're creating the DB,
225 * there's no need to drop individual items in it. Moreover, if we tried
226 * to do that then we'd issue the drops in the database initially
227 * connected to, not the one we will create, which is very bad...
229 if (ropt->createDB && ropt->dropSchema)
230 die_horribly(AH, modulename, "-C and -c are incompatible options\n");
233 * -C is not compatible with -1, because we can't create a database inside
234 * a transaction block.
236 if (ropt->createDB && ropt->single_txn)
237 die_horribly(AH, modulename, "-C and -1 are incompatible options\n");
240 * Make sure we won't need (de)compression we haven't got
243 if (AH->compression != 0 && AH->PrintTocDataPtr !=NULL)
245 for (te = AH->toc->next; te != AH->toc; te = te->next)
247 reqs = _tocEntryRequired(te, ropt, false);
248 if (te->hadDumper && (reqs & REQ_DATA) != 0)
249 die_horribly(AH, modulename, "cannot restore from compressed archive (compression not supported in this installation)\n");
255 * If we're using a DB connection, then connect it.
259 ahlog(AH, 1, "connecting to database for restore\n");
260 if (AH->version < K_VERS_1_3)
261 die_horribly(AH, modulename, "direct database connections are not supported in pre-1.3 archives\n");
263 /* XXX Should get this from the archive */
264 AHX->minRemoteVersion = 070100;
265 AHX->maxRemoteVersion = 999999;
267 ConnectDatabase(AHX, ropt->dbname,
268 ropt->pghost, ropt->pgport, ropt->username,
269 ropt->promptPassword);
272 * If we're talking to the DB directly, don't send comments since they
273 * obscure SQL when displaying errors
275 AH->noTocComments = 1;
279 * Work out if we have an implied data-only restore. This can happen if
280 * the dump was data only or if the user has used a toc list to exclude
281 * all of the schema data. All we do is look for schema entries - if none
282 * are found then we set the dataOnly flag.
284 * We could scan for wanted TABLE entries, but that is not the same as
285 * dataOnly. At this stage, it seems unnecessary (6-Mar-2001).
289 int impliedDataOnly = 1;
291 for (te = AH->toc->next; te != AH->toc; te = te->next)
293 reqs = _tocEntryRequired(te, ropt, true);
294 if ((reqs & REQ_SCHEMA) != 0)
295 { /* It's schema, and it's wanted */
302 ropt->dataOnly = impliedDataOnly;
303 ahlog(AH, 1, "implied data-only restore\n");
308 * Setup the output file if necessary.
310 sav = SaveOutput(AH);
311 if (ropt->filename || ropt->compression)
312 SetOutput(AH, ropt->filename, ropt->compression);
314 ahprintf(AH, "--\n-- PostgreSQL database dump\n--\n\n");
316 if (AH->public.verbose)
318 if (AH->archiveRemoteVersion)
319 ahprintf(AH, "-- Dumped from database version %s\n",
320 AH->archiveRemoteVersion);
321 if (AH->archiveDumpVersion)
322 ahprintf(AH, "-- Dumped by pg_dump version %s\n",
323 AH->archiveDumpVersion);
324 dumpTimestamp(AH, "Started on", AH->createDate);
327 if (ropt->single_txn)
330 StartTransaction(AH);
332 ahprintf(AH, "BEGIN;\n\n");
336 * Establish important parameter values right away.
338 _doSetFixedOutputState(AH);
340 AH->stage = STAGE_PROCESSING;
343 * Drop the items at the start, in reverse order
345 if (ropt->dropSchema)
347 for (te = AH->toc->prev; te != AH->toc; te = te->prev)
351 reqs = _tocEntryRequired(te, ropt, false /* needn't drop ACLs */ );
352 /* We want anything that's selected and has a dropStmt */
353 if (((reqs & (REQ_SCHEMA | REQ_DATA)) != 0) && te->dropStmt)
355 ahlog(AH, 1, "dropping %s %s\n", te->desc, te->tag);
356 /* Select owner and schema as necessary */
357 _becomeOwner(AH, te);
358 _selectOutputSchema(AH, te->namespace);
360 ahprintf(AH, "%s", te->dropStmt);
365 * _selectOutputSchema may have set currSchema to reflect the effect
366 * of a "SET search_path" command it emitted. However, by now we may
367 * have dropped that schema; or it might not have existed in the first
368 * place. In either case the effective value of search_path will not
369 * be what we think. Forcibly reset currSchema so that we will
370 * re-establish the search_path setting when needed (after creating
373 * If we treated users as pg_dump'able objects then we'd need to reset
377 free(AH->currSchema);
378 AH->currSchema = NULL;
382 * In serial mode, we now process each non-ACL TOC entry.
384 * In parallel mode, turn control over to the parallel-restore logic.
386 if (ropt->number_of_jobs > 1 && ropt->useDB)
387 restore_toc_entries_parallel(AH);
390 for (te = AH->toc->next; te != AH->toc; te = te->next)
391 (void) restore_toc_entry(AH, te, ropt, false);
395 * Scan TOC again to output ownership commands and ACLs
397 for (te = AH->toc->next; te != AH->toc; te = te->next)
401 /* Work out what, if anything, we want from this entry */
402 reqs = _tocEntryRequired(te, ropt, true);
404 /* Both schema and data objects might now have ownership/ACLs */
405 if ((reqs & (REQ_SCHEMA | REQ_DATA)) != 0)
407 ahlog(AH, 1, "setting owner and privileges for %s %s\n",
409 _printTocEntry(AH, te, ropt, false, true);
413 if (ropt->single_txn)
416 CommitTransaction(AH);
418 ahprintf(AH, "COMMIT;\n\n");
421 if (AH->public.verbose)
422 dumpTimestamp(AH, "Completed on", time(NULL));
424 ahprintf(AH, "--\n-- PostgreSQL database dump complete\n--\n\n");
427 * Clean up & we're done.
429 AH->stage = STAGE_FINALIZING;
431 if (ropt->filename || ropt->compression)
432 RestoreOutput(AH, sav);
436 PQfinish(AH->connection);
437 AH->connection = NULL;
442 * Restore a single TOC item. Used in both parallel and non-parallel restore;
443 * is_parallel is true if we are in a worker child process.
445 * Returns 0 normally, but WORKER_CREATE_DONE or WORKER_INHIBIT_DATA if
446 * the parallel parent has to make the corresponding status update.
449 restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
450 RestoreOptions *ropt, bool is_parallel)
458 /* Work out what, if anything, we want from this entry */
459 reqs = _tocEntryRequired(te, ropt, false);
461 /* Dump any relevant dump warnings to stderr */
462 if (!ropt->suppressDumpWarnings && strcmp(te->desc, "WARNING") == 0)
464 if (!ropt->dataOnly && te->defn != NULL && strlen(te->defn) != 0)
465 write_msg(modulename, "warning from original dump file: %s\n", te->defn);
466 else if (te->copyStmt != NULL && strlen(te->copyStmt) != 0)
467 write_msg(modulename, "warning from original dump file: %s\n", te->copyStmt);
472 if ((reqs & REQ_SCHEMA) != 0) /* We want the schema */
474 ahlog(AH, 1, "creating %s %s\n", te->desc, te->tag);
476 _printTocEntry(AH, te, ropt, false, false);
479 if (strcmp(te->desc, "TABLE") == 0)
481 if (AH->lastErrorTE == te)
484 * We failed to create the table. If
485 * --no-data-for-failed-tables was given, mark the
486 * corresponding TABLE DATA to be ignored.
488 * In the parallel case this must be done in the parent, so we
489 * just set the return value.
491 if (ropt->noDataForFailedTables)
494 retval = WORKER_INHIBIT_DATA;
496 inhibit_data_for_failed_table(AH, te);
502 * We created the table successfully. Mark the corresponding
503 * TABLE DATA for possible truncation.
505 * In the parallel case this must be done in the parent, so we
506 * just set the return value.
509 retval = WORKER_CREATE_DONE;
511 mark_create_done(AH, te);
515 /* If we created a DB, connect to it... */
516 if (strcmp(te->desc, "DATABASE") == 0)
518 ahlog(AH, 1, "connecting to new database \"%s\"\n", te->tag);
519 _reconnectToDB(AH, te->tag);
520 ropt->dbname = strdup(te->tag);
525 * If we have a data component, then process it
527 if ((reqs & REQ_DATA) != 0)
530 * hadDumper will be set if there is genuine data component for this
531 * node. Otherwise, we need to check the defn field for statements
532 * that need to be executed in data-only restores.
537 * If we can output the data, then restore it.
539 if (AH->PrintTocDataPtr !=NULL && (reqs & REQ_DATA) != 0)
541 _printTocEntry(AH, te, ropt, true, false);
543 if (strcmp(te->desc, "BLOBS") == 0 ||
544 strcmp(te->desc, "BLOB COMMENTS") == 0)
546 ahlog(AH, 1, "restoring %s\n", te->desc);
548 _selectOutputSchema(AH, "pg_catalog");
550 (*AH->PrintTocDataPtr) (AH, te, ropt);
554 _disableTriggersIfNecessary(AH, te, ropt);
556 /* Select owner and schema as necessary */
557 _becomeOwner(AH, te);
558 _selectOutputSchema(AH, te->namespace);
560 ahlog(AH, 1, "restoring data for table \"%s\"\n",
564 * In parallel restore, if we created the table earlier in
565 * the run then we wrap the COPY in a transaction and
566 * precede it with a TRUNCATE. If archiving is not on
567 * this prevents WAL-logging the COPY. This obtains a
568 * speedup similar to that from using single_txn mode in
569 * non-parallel restores.
571 if (is_parallel && te->created)
574 * Parallel restore is always talking directly to a
575 * server, so no need to see if we should issue BEGIN.
577 StartTransaction(AH);
580 * If the server version is >= 8.4, make sure we issue
581 * TRUNCATE with ONLY so that child tables are not
584 ahprintf(AH, "TRUNCATE TABLE %s%s;\n\n",
585 (PQserverVersion(AH->connection) >= 80400 ?
591 * If we have a copy statement, use it. As of V1.3, these
592 * are separate to allow easy import from withing a
593 * database connection. Pre 1.3 archives can not use DB
594 * connections and are sent to output only.
596 * For V1.3+, the table data MUST have a copy statement so
597 * that we can go into appropriate mode with libpq.
599 if (te->copyStmt && strlen(te->copyStmt) > 0)
601 ahprintf(AH, "%s", te->copyStmt);
602 AH->writingCopyData = true;
605 (*AH->PrintTocDataPtr) (AH, te, ropt);
607 AH->writingCopyData = false;
609 /* close out the transaction started above */
610 if (is_parallel && te->created)
611 CommitTransaction(AH);
613 _enableTriggersIfNecessary(AH, te, ropt);
617 else if (!defnDumped)
619 /* If we haven't already dumped the defn part, do so now */
620 ahlog(AH, 1, "executing %s %s\n", te->desc, te->tag);
621 _printTocEntry(AH, te, ropt, false, false);
629 * Allocate a new RestoreOptions block.
630 * This is mainly so we can initialize it, but also for future expansion,
633 NewRestoreOptions(void)
635 RestoreOptions *opts;
637 opts = (RestoreOptions *) calloc(1, sizeof(RestoreOptions));
639 /* set any fields that shouldn't default to zeroes */
640 opts->format = archUnknown;
641 opts->promptPassword = TRI_DEFAULT;
647 _disableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
649 /* This hack is only needed in a data-only restore */
650 if (!ropt->dataOnly || !ropt->disable_triggers)
653 ahlog(AH, 1, "disabling triggers for %s\n", te->tag);
656 * Become superuser if possible, since they are the only ones who can
657 * disable constraint triggers. If -S was not given, assume the initial
658 * user identity is a superuser. (XXX would it be better to become the
661 _becomeUser(AH, ropt->superuser);
666 _selectOutputSchema(AH, te->namespace);
668 ahprintf(AH, "ALTER TABLE %s DISABLE TRIGGER ALL;\n\n",
673 _enableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
675 /* This hack is only needed in a data-only restore */
676 if (!ropt->dataOnly || !ropt->disable_triggers)
679 ahlog(AH, 1, "enabling triggers for %s\n", te->tag);
682 * Become superuser if possible, since they are the only ones who can
683 * disable constraint triggers. If -S was not given, assume the initial
684 * user identity is a superuser. (XXX would it be better to become the
687 _becomeUser(AH, ropt->superuser);
692 _selectOutputSchema(AH, te->namespace);
694 ahprintf(AH, "ALTER TABLE %s ENABLE TRIGGER ALL;\n\n",
699 * This is a routine that is part of the dumper interface, hence the 'Archive*' parameter.
704 WriteData(Archive *AHX, const void *data, size_t dLen)
706 ArchiveHandle *AH = (ArchiveHandle *) AHX;
709 die_horribly(AH, modulename, "internal error -- WriteData cannot be called outside the context of a DataDumper routine\n");
711 return (*AH->WriteDataPtr) (AH, data, dLen);
715 * Create a new TOC entry. The TOC was designed as a TOC, but is now the
716 * repository for all metadata. But the name has stuck.
721 ArchiveEntry(Archive *AHX,
722 CatalogId catalogId, DumpId dumpId,
724 const char *namespace,
725 const char *tablespace,
726 const char *owner, bool withOids,
727 const char *desc, teSection section,
729 const char *dropStmt, const char *copyStmt,
730 const DumpId *deps, int nDeps,
731 DataDumperPtr dumpFn, void *dumpArg)
733 ArchiveHandle *AH = (ArchiveHandle *) AHX;
736 newToc = (TocEntry *) calloc(1, sizeof(TocEntry));
738 die_horribly(AH, modulename, "out of memory\n");
741 if (dumpId > AH->maxDumpId)
742 AH->maxDumpId = dumpId;
744 newToc->prev = AH->toc->prev;
745 newToc->next = AH->toc;
746 AH->toc->prev->next = newToc;
747 AH->toc->prev = newToc;
749 newToc->catalogId = catalogId;
750 newToc->dumpId = dumpId;
751 newToc->section = section;
753 newToc->tag = strdup(tag);
754 newToc->namespace = namespace ? strdup(namespace) : NULL;
755 newToc->tablespace = tablespace ? strdup(tablespace) : NULL;
756 newToc->owner = strdup(owner);
757 newToc->withOids = withOids;
758 newToc->desc = strdup(desc);
759 newToc->defn = strdup(defn);
760 newToc->dropStmt = strdup(dropStmt);
761 newToc->copyStmt = copyStmt ? strdup(copyStmt) : NULL;
765 newToc->dependencies = (DumpId *) malloc(nDeps * sizeof(DumpId));
766 memcpy(newToc->dependencies, deps, nDeps * sizeof(DumpId));
767 newToc->nDeps = nDeps;
771 newToc->dependencies = NULL;
775 newToc->dataDumper = dumpFn;
776 newToc->dataDumperArg = dumpArg;
777 newToc->hadDumper = dumpFn ? true : false;
779 newToc->formatData = NULL;
781 if (AH->ArchiveEntryPtr !=NULL)
782 (*AH->ArchiveEntryPtr) (AH, newToc);
787 PrintTOCSummary(Archive *AHX, RestoreOptions *ropt)
789 ArchiveHandle *AH = (ArchiveHandle *) AHX;
794 sav = SaveOutput(AH);
796 SetOutput(AH, ropt->filename, 0 /* no compression */ );
798 ahprintf(AH, ";\n; Archive created at %s", ctime(&AH->createDate));
799 ahprintf(AH, "; dbname: %s\n; TOC Entries: %d\n; Compression: %d\n",
800 AH->archdbname, AH->tocCount, AH->compression);
817 ahprintf(AH, "; Dump Version: %d.%d-%d\n", AH->vmaj, AH->vmin, AH->vrev);
818 ahprintf(AH, "; Format: %s\n", fmtName);
819 ahprintf(AH, "; Integer: %d bytes\n", (int) AH->intSize);
820 ahprintf(AH, "; Offset: %d bytes\n", (int) AH->offSize);
821 if (AH->archiveRemoteVersion)
822 ahprintf(AH, "; Dumped from database version: %s\n",
823 AH->archiveRemoteVersion);
824 if (AH->archiveDumpVersion)
825 ahprintf(AH, "; Dumped by pg_dump version: %s\n",
826 AH->archiveDumpVersion);
828 ahprintf(AH, ";\n;\n; Selected TOC Entries:\n;\n");
830 /* We should print DATABASE entries whether or not -C was specified */
833 for (te = AH->toc->next; te != AH->toc; te = te->next)
835 if (ropt->verbose || _tocEntryRequired(te, ropt, true) != 0)
836 ahprintf(AH, "%d; %u %u %s %s %s %s\n", te->dumpId,
837 te->catalogId.tableoid, te->catalogId.oid,
838 te->desc, te->namespace ? te->namespace : "-",
840 if (ropt->verbose && te->nDeps > 0)
844 ahprintf(AH, ";\tdepends on:");
845 for (i = 0; i < te->nDeps; i++)
846 ahprintf(AH, " %d", te->dependencies[i]);
852 RestoreOutput(AH, sav);
859 /* Called by a dumper to signal start of a BLOB */
861 StartBlob(Archive *AHX, Oid oid)
863 ArchiveHandle *AH = (ArchiveHandle *) AHX;
865 if (!AH->StartBlobPtr)
866 die_horribly(AH, modulename, "large-object output not supported in chosen format\n");
868 (*AH->StartBlobPtr) (AH, AH->currToc, oid);
873 /* Called by a dumper to signal end of a BLOB */
875 EndBlob(Archive *AHX, Oid oid)
877 ArchiveHandle *AH = (ArchiveHandle *) AHX;
880 (*AH->EndBlobPtr) (AH, AH->currToc, oid);
890 * Called by a format handler before any blobs are restored
893 StartRestoreBlobs(ArchiveHandle *AH)
895 if (!AH->ropt->single_txn)
898 StartTransaction(AH);
900 ahprintf(AH, "BEGIN;\n\n");
907 * Called by a format handler after all blobs are restored
910 EndRestoreBlobs(ArchiveHandle *AH)
912 if (!AH->ropt->single_txn)
915 CommitTransaction(AH);
917 ahprintf(AH, "COMMIT;\n\n");
920 ahlog(AH, 1, ngettext("restored %d large object\n",
921 "restored %d large objects\n",
928 * Called by a format handler to initiate restoration of a blob
931 StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop)
933 bool old_blob_style = (AH->version < K_VERS_1_12);
938 /* Initialize the LO Buffer */
941 ahlog(AH, 2, "restoring large object with OID %u\n", oid);
943 /* With an old archive we must do drop and create logic here */
944 if (old_blob_style && drop)
945 DropBlobIfExists(AH, oid);
951 loOid = lo_create(AH->connection, oid);
952 if (loOid == 0 || loOid != oid)
953 die_horribly(AH, modulename, "could not create large object %u: %s",
954 oid, PQerrorMessage(AH->connection));
956 AH->loFd = lo_open(AH->connection, oid, INV_WRITE);
958 die_horribly(AH, modulename, "could not open large object %u: %s",
959 oid, PQerrorMessage(AH->connection));
964 ahprintf(AH, "SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n",
967 ahprintf(AH, "SELECT pg_catalog.lo_open('%u', %d);\n",
975 EndRestoreBlob(ArchiveHandle *AH, Oid oid)
977 if (AH->lo_buf_used > 0)
979 /* Write remaining bytes from the LO buffer */
987 lo_close(AH->connection, AH->loFd);
992 ahprintf(AH, "SELECT pg_catalog.lo_close(0);\n\n");
997 * Sorting and Reordering
1001 SortTocFromFile(Archive *AHX, RestoreOptions *ropt)
1003 ArchiveHandle *AH = (ArchiveHandle *) AHX;
1011 /* Allocate space for the 'wanted' array, and init it */
1012 ropt->idWanted = (bool *) malloc(sizeof(bool) * AH->maxDumpId);
1013 memset(ropt->idWanted, 0, sizeof(bool) * AH->maxDumpId);
1015 /* Setup the file */
1016 fh = fopen(ropt->tocFile, PG_BINARY_R);
1018 die_horribly(AH, modulename, "could not open TOC file \"%s\": %s\n",
1019 ropt->tocFile, strerror(errno));
1021 while (fgets(buf, sizeof(buf), fh) != NULL)
1023 /* Truncate line at comment, if any */
1024 cmnt = strchr(buf, ';');
1028 /* Ignore if all blank */
1029 if (strspn(buf, " \t\r\n") == strlen(buf))
1032 /* Get an ID, check it's valid and not already seen */
1033 id = strtol(buf, &endptr, 10);
1034 if (endptr == buf || id <= 0 || id > AH->maxDumpId ||
1035 ropt->idWanted[id - 1])
1037 write_msg(modulename, "WARNING: line ignored: %s\n", buf);
1041 /* Find TOC entry */
1042 te = getTocEntryByDumpId(AH, id);
1044 die_horribly(AH, modulename, "could not find entry for ID %d\n",
1047 /* Mark it wanted */
1048 ropt->idWanted[id - 1] = true;
1051 * Move each item to the end of the list as it is selected, so that
1052 * they are placed in the desired order. Any unwanted items will end
1053 * up at the front of the list, which may seem unintuitive but it's
1054 * what we need. In an ordinary serial restore that makes no
1055 * difference, but in a parallel restore we need to mark unrestored
1056 * items' dependencies as satisfied before we start examining
1057 * restorable items. Otherwise they could have surprising
1058 * side-effects on the order in which restorable items actually get
1061 _moveBefore(AH, AH->toc, te);
1064 if (fclose(fh) != 0)
1065 die_horribly(AH, modulename, "could not close TOC file: %s\n",
1070 * Set up a dummy ID filter that selects all dump IDs
1073 InitDummyWantedList(Archive *AHX, RestoreOptions *ropt)
1075 ArchiveHandle *AH = (ArchiveHandle *) AHX;
1077 /* Allocate space for the 'wanted' array, and init it to 1's */
1078 ropt->idWanted = (bool *) malloc(sizeof(bool) * AH->maxDumpId);
1079 memset(ropt->idWanted, 1, sizeof(bool) * AH->maxDumpId);
1082 /**********************
1083 * 'Convenience functions that look like standard IO functions
1084 * for writing data when in dump mode.
1085 **********************/
1089 archputs(const char *s, Archive *AH)
1091 return WriteData(AH, s, strlen(s));
1096 archprintf(Archive *AH, const char *fmt,...)
1100 int bSize = strlen(fmt) + 256;
1104 * This is paranoid: deal with the possibility that vsnprintf is willing
1105 * to ignore trailing null or returns > 0 even if string does not fit. It
1106 * may be the case that it returns cnt = bufsize
1108 while (cnt < 0 || cnt >= (bSize - 1))
1113 p = (char *) malloc(bSize);
1115 exit_horribly(AH, modulename, "out of memory\n");
1117 cnt = vsnprintf(p, bSize, fmt, ap);
1120 WriteData(AH, p, cnt);
1126 /*******************************
1127 * Stuff below here should be 'private' to the archiver routines
1128 *******************************/
1131 SetOutput(ArchiveHandle *AH, char *filename, int compression)
1138 fn = fileno(AH->FH);
1142 filename = AH->fSpec;
1145 fn = fileno(stdout);
1147 /* If compression explicitly requested, use gzopen */
1149 if (compression != 0)
1153 /* Don't use PG_BINARY_x since this is zlib */
1154 sprintf(fmode, "wb%d", compression);
1156 AH->OF = gzdopen(dup(fn), fmode);
1158 AH->OF = gzopen(filename, fmode);
1164 if (AH->mode == archModeAppend)
1167 AH->OF = fdopen(dup(fn), PG_BINARY_A);
1169 AH->OF = fopen(filename, PG_BINARY_A);
1174 AH->OF = fdopen(dup(fn), PG_BINARY_W);
1176 AH->OF = fopen(filename, PG_BINARY_W);
1184 die_horribly(AH, modulename, "could not open output file \"%s\": %s\n",
1185 filename, strerror(errno));
1187 die_horribly(AH, modulename, "could not open output file: %s\n",
1192 static OutputContext
1193 SaveOutput(ArchiveHandle *AH)
1198 sav.gzOut = AH->gzOut;
1204 RestoreOutput(ArchiveHandle *AH, OutputContext savedContext)
1209 res = GZCLOSE(AH->OF);
1211 res = fclose(AH->OF);
1214 die_horribly(AH, modulename, "could not close output file: %s\n",
1217 AH->gzOut = savedContext.gzOut;
1218 AH->OF = savedContext.OF;
1224 * Print formatted text to the output file (usually stdout).
1227 ahprintf(ArchiveHandle *AH, const char *fmt,...)
1231 int bSize = strlen(fmt) + 256; /* Should be enough */
1235 * This is paranoid: deal with the possibility that vsnprintf is willing
1236 * to ignore trailing null
1240 * or returns > 0 even if string does not fit. It may be the case that it
1241 * returns cnt = bufsize
1243 while (cnt < 0 || cnt >= (bSize - 1))
1248 p = (char *) malloc(bSize);
1250 die_horribly(AH, modulename, "out of memory\n");
1252 cnt = vsnprintf(p, bSize, fmt, ap);
1255 ahwrite(p, 1, cnt, AH);
1261 ahlog(ArchiveHandle *AH, int level, const char *fmt,...)
1265 if (AH->debugLevel < level && (!AH->public.verbose || level > 1))
1269 _write_msg(NULL, fmt, ap);
1274 * Single place for logic which says 'We are restoring to a direct DB connection'.
1277 RestoringToDB(ArchiveHandle *AH)
1279 return (AH->ropt && AH->ropt->useDB && AH->connection);
1283 * Dump the current contents of the LO data buffer while writing a BLOB
1286 dump_lo_buf(ArchiveHandle *AH)
1292 res = lo_write(AH->connection, AH->loFd, AH->lo_buf, AH->lo_buf_used);
1293 ahlog(AH, 5, ngettext("wrote %lu byte of large object data (result = %lu)\n",
1294 "wrote %lu bytes of large object data (result = %lu)\n",
1296 (unsigned long) AH->lo_buf_used, (unsigned long) res);
1297 if (res != AH->lo_buf_used)
1298 die_horribly(AH, modulename,
1299 "could not write to large object (result: %lu, expected: %lu)\n",
1300 (unsigned long) res, (unsigned long) AH->lo_buf_used);
1304 PQExpBuffer buf = createPQExpBuffer();
1306 appendByteaLiteralAHX(buf,
1307 (const unsigned char *) AH->lo_buf,
1311 /* Hack: turn off writingBlob so ahwrite doesn't recurse to here */
1312 AH->writingBlob = 0;
1313 ahprintf(AH, "SELECT pg_catalog.lowrite(0, %s);\n", buf->data);
1314 AH->writingBlob = 1;
1316 destroyPQExpBuffer(buf);
1318 AH->lo_buf_used = 0;
1323 * Write buffer to the output file (usually stdout). This is user for
1324 * outputting 'restore' scripts etc. It is even possible for an archive
1325 * format to create a custom output routine to 'fake' a restore if it
1326 * wants to generate a script (see TAR output).
1329 ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
1333 if (AH->writingBlob)
1335 size_t remaining = size * nmemb;
1337 while (AH->lo_buf_used + remaining > AH->lo_buf_size)
1339 size_t avail = AH->lo_buf_size - AH->lo_buf_used;
1341 memcpy((char *) AH->lo_buf + AH->lo_buf_used, ptr, avail);
1342 ptr = (const void *) ((const char *) ptr + avail);
1344 AH->lo_buf_used += avail;
1348 memcpy((char *) AH->lo_buf + AH->lo_buf_used, ptr, remaining);
1349 AH->lo_buf_used += remaining;
1351 return size * nmemb;
1355 res = GZWRITE((void *) ptr, size, nmemb, AH->OF);
1356 if (res != (nmemb * size))
1357 die_horribly(AH, modulename, "could not write to output file: %s\n", strerror(errno));
1360 else if (AH->CustomOutPtr)
1362 res = AH->CustomOutPtr (AH, ptr, size * nmemb);
1364 if (res != (nmemb * size))
1365 die_horribly(AH, modulename, "could not write to custom output routine\n");
1371 * If we're doing a restore, and it's direct to DB, and we're
1372 * connected then send it to the DB.
1374 if (RestoringToDB(AH))
1375 return ExecuteSqlCommandBuf(AH, (void *) ptr, size * nmemb); /* Always 1, currently */
1378 res = fwrite((void *) ptr, size, nmemb, AH->OF);
1380 die_horribly(AH, modulename, "could not write to output file: %s\n",
1387 /* Common exit code */
1389 _write_msg(const char *modulename, const char *fmt, va_list ap)
1392 fprintf(stderr, "%s: [%s] ", progname, _(modulename));
1394 fprintf(stderr, "%s: ", progname);
1395 vfprintf(stderr, _(fmt), ap);
1399 write_msg(const char *modulename, const char *fmt,...)
1404 _write_msg(modulename, fmt, ap);
1410 _die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt, va_list ap)
1412 _write_msg(modulename, fmt, ap);
1416 if (AH->public.verbose)
1417 write_msg(NULL, "*** aborted because of error\n");
1419 PQfinish(AH->connection);
1427 exit_horribly(Archive *AH, const char *modulename, const char *fmt,...)
1432 _die_horribly((ArchiveHandle *) AH, modulename, fmt, ap);
1436 /* Archiver use (just different arg declaration) */
1438 die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...)
1443 _die_horribly(AH, modulename, fmt, ap);
1447 /* on some error, we may decide to go on... */
1449 warn_or_die_horribly(ArchiveHandle *AH,
1450 const char *modulename, const char *fmt,...)
1458 /* Do nothing special */
1461 case STAGE_INITIALIZING:
1462 if (AH->stage != AH->lastErrorStage)
1463 write_msg(modulename, "Error while INITIALIZING:\n");
1466 case STAGE_PROCESSING:
1467 if (AH->stage != AH->lastErrorStage)
1468 write_msg(modulename, "Error while PROCESSING TOC:\n");
1471 case STAGE_FINALIZING:
1472 if (AH->stage != AH->lastErrorStage)
1473 write_msg(modulename, "Error while FINALIZING:\n");
1476 if (AH->currentTE != NULL && AH->currentTE != AH->lastErrorTE)
1478 write_msg(modulename, "Error from TOC entry %d; %u %u %s %s %s\n",
1479 AH->currentTE->dumpId,
1480 AH->currentTE->catalogId.tableoid, AH->currentTE->catalogId.oid,
1481 AH->currentTE->desc, AH->currentTE->tag, AH->currentTE->owner);
1483 AH->lastErrorStage = AH->stage;
1484 AH->lastErrorTE = AH->currentTE;
1487 if (AH->public.exit_on_error)
1488 _die_horribly(AH, modulename, fmt, ap);
1491 _write_msg(modulename, fmt, ap);
1492 AH->public.n_errors++;
1500 _moveAfter(ArchiveHandle *AH, TocEntry *pos, TocEntry *te)
1502 /* Unlink te from list */
1503 te->prev->next = te->next;
1504 te->next->prev = te->prev;
1506 /* and insert it after "pos" */
1508 te->next = pos->next;
1509 pos->next->prev = te;
1516 _moveBefore(ArchiveHandle *AH, TocEntry *pos, TocEntry *te)
1518 /* Unlink te from list */
1519 te->prev->next = te->next;
1520 te->next->prev = te->prev;
1522 /* and insert it before "pos" */
1523 te->prev = pos->prev;
1525 pos->prev->next = te;
1530 getTocEntryByDumpId(ArchiveHandle *AH, DumpId id)
1534 for (te = AH->toc->next; te != AH->toc; te = te->next)
1536 if (te->dumpId == id)
1543 TocIDRequired(ArchiveHandle *AH, DumpId id, RestoreOptions *ropt)
1545 TocEntry *te = getTocEntryByDumpId(AH, id);
1550 return _tocEntryRequired(te, ropt, true);
1554 WriteOffset(ArchiveHandle *AH, pgoff_t o, int wasSet)
1559 (*AH->WriteBytePtr) (AH, wasSet);
1561 /* Write out pgoff_t smallest byte first, prevents endian mismatch */
1562 for (off = 0; off < sizeof(pgoff_t); off++)
1564 (*AH->WriteBytePtr) (AH, o & 0xFF);
1567 return sizeof(pgoff_t) + 1;
1571 ReadOffset(ArchiveHandle *AH, pgoff_t * o)
1577 /* Initialize to zero */
1580 /* Check for old version */
1581 if (AH->version < K_VERS_1_7)
1583 /* Prior versions wrote offsets using WriteInt */
1585 /* -1 means not set */
1587 return K_OFFSET_POS_NOT_SET;
1589 return K_OFFSET_NO_DATA;
1591 /* Cast to pgoff_t because it was written as an int. */
1593 return K_OFFSET_POS_SET;
1597 * Read the flag indicating the state of the data pointer. Check if valid
1600 * This used to be handled by a negative or zero pointer, now we use an
1601 * extra byte specifically for the state.
1603 offsetFlg = (*AH->ReadBytePtr) (AH) & 0xFF;
1607 case K_OFFSET_POS_NOT_SET:
1608 case K_OFFSET_NO_DATA:
1609 case K_OFFSET_POS_SET:
1614 die_horribly(AH, modulename, "unexpected data offset flag %d\n", offsetFlg);
1620 for (off = 0; off < AH->offSize; off++)
1622 if (off < sizeof(pgoff_t))
1623 *o |= ((pgoff_t) ((*AH->ReadBytePtr) (AH))) << (off * 8);
1626 if ((*AH->ReadBytePtr) (AH) != 0)
1627 die_horribly(AH, modulename, "file offset in dump file is too large\n");
1635 WriteInt(ArchiveHandle *AH, int i)
1640 * This is a bit yucky, but I don't want to make the binary format very
1641 * dependent on representation, and not knowing much about it, I write out
1642 * a sign byte. If you change this, don't forget to change the file
1643 * version #, and modify readInt to read the new format AS WELL AS the old
1650 (*AH->WriteBytePtr) (AH, 1);
1654 (*AH->WriteBytePtr) (AH, 0);
1656 for (b = 0; b < AH->intSize; b++)
1658 (*AH->WriteBytePtr) (AH, i & 0xFF);
1662 return AH->intSize + 1;
1666 ReadInt(ArchiveHandle *AH)
1671 int sign = 0; /* Default positive */
1674 if (AH->version > K_VERS_1_0)
1675 /* Read a sign byte */
1676 sign = (*AH->ReadBytePtr) (AH);
1678 for (b = 0; b < AH->intSize; b++)
1680 bv = (*AH->ReadBytePtr) (AH) & 0xFF;
1682 res = res + (bv << bitShift);
1693 WriteStr(ArchiveHandle *AH, const char *c)
1699 res = WriteInt(AH, strlen(c));
1700 res += (*AH->WriteBufPtr) (AH, c, strlen(c));
1703 res = WriteInt(AH, -1);
1709 ReadStr(ArchiveHandle *AH)
1719 buf = (char *) malloc(l + 1);
1721 die_horribly(AH, modulename, "out of memory\n");
1723 if ((*AH->ReadBufPtr) (AH, (void *) buf, l) != l)
1724 die_horribly(AH, modulename, "unexpected end of file\n");
1733 _discoverArchiveFormat(ArchiveHandle *AH)
1736 char sig[6]; /* More than enough */
1741 write_msg(modulename, "attempting to ascertain archive format\n");
1745 free(AH->lookahead);
1747 AH->lookaheadSize = 512;
1748 AH->lookahead = calloc(1, 512);
1749 AH->lookaheadLen = 0;
1750 AH->lookaheadPos = 0;
1755 fh = fopen(AH->fSpec, PG_BINARY_R);
1757 die_horribly(AH, modulename, "could not open input file \"%s\": %s\n",
1758 AH->fSpec, strerror(errno));
1764 die_horribly(AH, modulename, "could not open input file: %s\n",
1768 cnt = fread(sig, 1, 5, fh);
1773 die_horribly(AH, modulename, "could not read input file: %s\n", strerror(errno));
1775 die_horribly(AH, modulename, "input file is too short (read %lu, expected 5)\n",
1776 (unsigned long) cnt);
1779 /* Save it, just in case we need it later */
1780 strncpy(&AH->lookahead[0], sig, 5);
1781 AH->lookaheadLen = 5;
1783 if (strncmp(sig, "PGDMP", 5) == 0)
1786 * Finish reading (most of) a custom-format header.
1788 * NB: this code must agree with ReadHead().
1790 AH->vmaj = fgetc(fh);
1791 AH->vmin = fgetc(fh);
1793 /* Save these too... */
1794 AH->lookahead[AH->lookaheadLen++] = AH->vmaj;
1795 AH->lookahead[AH->lookaheadLen++] = AH->vmin;
1797 /* Check header version; varies from V1.0 */
1798 if (AH->vmaj > 1 || ((AH->vmaj == 1) && (AH->vmin > 0))) /* Version > 1.0 */
1800 AH->vrev = fgetc(fh);
1801 AH->lookahead[AH->lookaheadLen++] = AH->vrev;
1806 /* Make a convenient integer <maj><min><rev>00 */
1807 AH->version = ((AH->vmaj * 256 + AH->vmin) * 256 + AH->vrev) * 256 + 0;
1809 AH->intSize = fgetc(fh);
1810 AH->lookahead[AH->lookaheadLen++] = AH->intSize;
1812 if (AH->version >= K_VERS_1_7)
1814 AH->offSize = fgetc(fh);
1815 AH->lookahead[AH->lookaheadLen++] = AH->offSize;
1818 AH->offSize = AH->intSize;
1820 AH->format = fgetc(fh);
1821 AH->lookahead[AH->lookaheadLen++] = AH->format;
1826 * *Maybe* we have a tar archive format file... So, read first 512
1829 cnt = fread(&AH->lookahead[AH->lookaheadLen], 1, 512 - AH->lookaheadLen, fh);
1830 AH->lookaheadLen += cnt;
1832 if (AH->lookaheadLen != 512)
1833 die_horribly(AH, modulename, "input file does not appear to be a valid archive (too short?)\n");
1835 if (!isValidTarHeader(AH->lookahead))
1836 die_horribly(AH, modulename, "input file does not appear to be a valid archive\n");
1838 AH->format = archTar;
1841 /* If we can't seek, then mark the header as read */
1842 if (fseeko(fh, 0, SEEK_SET) != 0)
1845 * NOTE: Formats that use the lookahead buffer can unset this in their
1851 AH->lookaheadLen = 0; /* Don't bother since we've reset the file */
1853 /* Close the file */
1855 if (fclose(fh) != 0)
1856 die_horribly(AH, modulename, "could not close input file: %s\n",
1864 * Allocate an archive handle
1866 static ArchiveHandle *
1867 _allocAH(const char *FileSpec, const ArchiveFormat fmt,
1868 const int compression, ArchiveMode mode)
1873 write_msg(modulename, "allocating AH for %s, format %d\n", FileSpec, fmt);
1876 AH = (ArchiveHandle *) calloc(1, sizeof(ArchiveHandle));
1878 die_horribly(AH, modulename, "out of memory\n");
1880 /* AH->debugLevel = 100; */
1882 AH->vmaj = K_VERS_MAJOR;
1883 AH->vmin = K_VERS_MINOR;
1884 AH->vrev = K_VERS_REV;
1886 /* Make a convenient integer <maj><min><rev>00 */
1887 AH->version = ((AH->vmaj * 256 + AH->vmin) * 256 + AH->vrev) * 256 + 0;
1889 /* initialize for backwards compatible string processing */
1890 AH->public.encoding = 0; /* PG_SQL_ASCII */
1891 AH->public.std_strings = false;
1893 /* sql error handling */
1894 AH->public.exit_on_error = true;
1895 AH->public.n_errors = 0;
1897 AH->archiveDumpVersion = PG_VERSION;
1899 AH->createDate = time(NULL);
1901 AH->intSize = sizeof(int);
1902 AH->offSize = sizeof(pgoff_t);
1905 AH->fSpec = strdup(FileSpec);
1908 * Not used; maybe later....
1910 * AH->workDir = strdup(FileSpec); for(i=strlen(FileSpec) ; i > 0 ;
1911 * i--) if (AH->workDir[i-1] == '/')
1917 AH->currUser = NULL; /* unknown */
1918 AH->currSchema = NULL; /* ditto */
1919 AH->currTablespace = NULL; /* ditto */
1920 AH->currWithOids = -1; /* force SET */
1922 AH->toc = (TocEntry *) calloc(1, sizeof(TocEntry));
1924 die_horribly(AH, modulename, "out of memory\n");
1926 AH->toc->next = AH->toc;
1927 AH->toc->prev = AH->toc;
1930 AH->compression = compression;
1932 AH->pgCopyBuf = createPQExpBuffer();
1933 AH->sqlBuf = createPQExpBuffer();
1935 /* Open stdout with no compression for AH output handle */
1940 * On Windows, we need to use binary mode to read/write non-text archive
1941 * formats. Force stdin/stdout into binary mode if that is what we are
1945 if (fmt != archNull &&
1946 (AH->fSpec == NULL || strcmp(AH->fSpec, "") == 0))
1948 if (mode == archModeWrite)
1949 setmode(fileno(stdout), O_BINARY);
1951 setmode(fileno(stdin), O_BINARY);
1955 if (fmt == archUnknown)
1956 AH->format = _discoverArchiveFormat(AH);
1960 AH->promptPassword = TRI_DEFAULT;
1965 InitArchiveFmt_Custom(AH);
1969 InitArchiveFmt_Files(AH);
1973 InitArchiveFmt_Null(AH);
1977 InitArchiveFmt_Tar(AH);
1981 die_horribly(AH, modulename, "unrecognized file format \"%d\"\n", fmt);
1989 WriteDataChunks(ArchiveHandle *AH)
1992 StartDataPtr startPtr;
1995 for (te = AH->toc->next; te != AH->toc; te = te->next)
1997 if (te->dataDumper != NULL)
2000 /* printf("Writing data for %d (%x)\n", te->id, te); */
2002 if (strcmp(te->desc, "BLOBS") == 0)
2004 startPtr = AH->StartBlobsPtr;
2005 endPtr = AH->EndBlobsPtr;
2009 startPtr = AH->StartDataPtr;
2010 endPtr = AH->EndDataPtr;
2013 if (startPtr != NULL)
2014 (*startPtr) (AH, te);
2017 * printf("Dumper arg for %d is %x\n", te->id, te->dataDumperArg);
2021 * The user-provided DataDumper routine needs to call
2024 (*te->dataDumper) ((Archive *) AH, te->dataDumperArg);
2034 WriteToc(ArchiveHandle *AH)
2040 /* printf("%d TOC Entries to save\n", AH->tocCount); */
2042 WriteInt(AH, AH->tocCount);
2044 for (te = AH->toc->next; te != AH->toc; te = te->next)
2046 WriteInt(AH, te->dumpId);
2047 WriteInt(AH, te->dataDumper ? 1 : 0);
2049 /* OID is recorded as a string for historical reasons */
2050 sprintf(workbuf, "%u", te->catalogId.tableoid);
2051 WriteStr(AH, workbuf);
2052 sprintf(workbuf, "%u", te->catalogId.oid);
2053 WriteStr(AH, workbuf);
2055 WriteStr(AH, te->tag);
2056 WriteStr(AH, te->desc);
2057 WriteInt(AH, te->section);
2058 WriteStr(AH, te->defn);
2059 WriteStr(AH, te->dropStmt);
2060 WriteStr(AH, te->copyStmt);
2061 WriteStr(AH, te->namespace);
2062 WriteStr(AH, te->tablespace);
2063 WriteStr(AH, te->owner);
2064 WriteStr(AH, te->withOids ? "true" : "false");
2066 /* Dump list of dependencies */
2067 for (i = 0; i < te->nDeps; i++)
2069 sprintf(workbuf, "%d", te->dependencies[i]);
2070 WriteStr(AH, workbuf);
2072 WriteStr(AH, NULL); /* Terminate List */
2074 if (AH->WriteExtraTocPtr)
2075 (*AH->WriteExtraTocPtr) (AH, te);
2080 ReadToc(ArchiveHandle *AH)
2089 AH->tocCount = ReadInt(AH);
2092 for (i = 0; i < AH->tocCount; i++)
2094 te = (TocEntry *) calloc(1, sizeof(TocEntry));
2095 te->dumpId = ReadInt(AH);
2097 if (te->dumpId > AH->maxDumpId)
2098 AH->maxDumpId = te->dumpId;
2101 if (te->dumpId <= 0)
2102 die_horribly(AH, modulename,
2103 "entry ID %d out of range -- perhaps a corrupt TOC\n",
2106 te->hadDumper = ReadInt(AH);
2108 if (AH->version >= K_VERS_1_8)
2111 sscanf(tmp, "%u", &te->catalogId.tableoid);
2115 te->catalogId.tableoid = InvalidOid;
2117 sscanf(tmp, "%u", &te->catalogId.oid);
2120 te->tag = ReadStr(AH);
2121 te->desc = ReadStr(AH);
2123 if (AH->version >= K_VERS_1_11)
2125 te->section = ReadInt(AH);
2130 * Rules for pre-8.4 archives wherein pg_dump hasn't classified
2131 * the entries into sections. This list need not cover entry
2132 * types added later than 8.4.
2134 if (strcmp(te->desc, "COMMENT") == 0 ||
2135 strcmp(te->desc, "ACL") == 0 ||
2136 strcmp(te->desc, "ACL LANGUAGE") == 0)
2137 te->section = SECTION_NONE;
2138 else if (strcmp(te->desc, "TABLE DATA") == 0 ||
2139 strcmp(te->desc, "BLOBS") == 0 ||
2140 strcmp(te->desc, "BLOB COMMENTS") == 0)
2141 te->section = SECTION_DATA;
2142 else if (strcmp(te->desc, "CONSTRAINT") == 0 ||
2143 strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
2144 strcmp(te->desc, "FK CONSTRAINT") == 0 ||
2145 strcmp(te->desc, "INDEX") == 0 ||
2146 strcmp(te->desc, "RULE") == 0 ||
2147 strcmp(te->desc, "TRIGGER") == 0)
2148 te->section = SECTION_POST_DATA;
2150 te->section = SECTION_PRE_DATA;
2153 te->defn = ReadStr(AH);
2154 te->dropStmt = ReadStr(AH);
2156 if (AH->version >= K_VERS_1_3)
2157 te->copyStmt = ReadStr(AH);
2159 if (AH->version >= K_VERS_1_6)
2160 te->namespace = ReadStr(AH);
2162 if (AH->version >= K_VERS_1_10)
2163 te->tablespace = ReadStr(AH);
2165 te->owner = ReadStr(AH);
2166 if (AH->version >= K_VERS_1_9)
2168 if (strcmp(ReadStr(AH), "true") == 0)
2169 te->withOids = true;
2171 te->withOids = false;
2174 te->withOids = true;
2176 /* Read TOC entry dependencies */
2177 if (AH->version >= K_VERS_1_5)
2180 deps = (DumpId *) malloc(sizeof(DumpId) * depSize);
2186 break; /* end of list */
2187 if (depIdx >= depSize)
2190 deps = (DumpId *) realloc(deps, sizeof(DumpId) * depSize);
2192 sscanf(tmp, "%d", &deps[depIdx]);
2197 if (depIdx > 0) /* We have a non-null entry */
2199 deps = (DumpId *) realloc(deps, sizeof(DumpId) * depIdx);
2200 te->dependencies = deps;
2206 te->dependencies = NULL;
2212 te->dependencies = NULL;
2216 if (AH->ReadExtraTocPtr)
2217 (*AH->ReadExtraTocPtr) (AH, te);
2219 ahlog(AH, 3, "read TOC entry %d (ID %d) for %s %s\n",
2220 i, te->dumpId, te->desc, te->tag);
2222 /* link completed entry into TOC circular list */
2223 te->prev = AH->toc->prev;
2224 AH->toc->prev->next = te;
2228 /* special processing immediately upon read for some items */
2229 if (strcmp(te->desc, "ENCODING") == 0)
2230 processEncodingEntry(AH, te);
2231 else if (strcmp(te->desc, "STDSTRINGS") == 0)
2232 processStdStringsEntry(AH, te);
2237 processEncodingEntry(ArchiveHandle *AH, TocEntry *te)
2239 /* te->defn should have the form SET client_encoding = 'foo'; */
2240 char *defn = strdup(te->defn);
2245 ptr1 = strchr(defn, '\'');
2247 ptr2 = strchr(++ptr1, '\'');
2251 encoding = pg_char_to_encoding(ptr1);
2253 die_horribly(AH, modulename, "unrecognized encoding \"%s\"\n",
2255 AH->public.encoding = encoding;
2258 die_horribly(AH, modulename, "invalid ENCODING item: %s\n",
2265 processStdStringsEntry(ArchiveHandle *AH, TocEntry *te)
2267 /* te->defn should have the form SET standard_conforming_strings = 'x'; */
2270 ptr1 = strchr(te->defn, '\'');
2271 if (ptr1 && strncmp(ptr1, "'on'", 4) == 0)
2272 AH->public.std_strings = true;
2273 else if (ptr1 && strncmp(ptr1, "'off'", 5) == 0)
2274 AH->public.std_strings = false;
2276 die_horribly(AH, modulename, "invalid STDSTRINGS item: %s\n",
2281 _tocEntryRequired(TocEntry *te, RestoreOptions *ropt, bool include_acls)
2283 teReqs res = REQ_ALL;
2285 /* ENCODING and STDSTRINGS items are dumped specially, so always reject */
2286 if (strcmp(te->desc, "ENCODING") == 0 ||
2287 strcmp(te->desc, "STDSTRINGS") == 0)
2290 /* If it's an ACL, maybe ignore it */
2291 if ((!include_acls || ropt->aclsSkip) && _tocEntryIsACL(te))
2294 /* If it's security labels, maybe ignore it */
2295 if (ropt->skip_seclabel && strcmp(te->desc, "SECURITY LABEL") == 0)
2298 /* Ignore DATABASE entry unless we should create it */
2299 if (!ropt->createDB && strcmp(te->desc, "DATABASE") == 0)
2302 /* Check options for selective dump/restore */
2303 if (ropt->schemaNames)
2305 /* If no namespace is specified, it means all. */
2308 if (strcmp(ropt->schemaNames, te->namespace) != 0)
2314 if (strcmp(te->desc, "TABLE") == 0 ||
2315 strcmp(te->desc, "TABLE DATA") == 0)
2317 if (!ropt->selTable)
2319 if (ropt->tableNames && strcmp(ropt->tableNames, te->tag) != 0)
2322 else if (strcmp(te->desc, "INDEX") == 0)
2324 if (!ropt->selIndex)
2326 if (ropt->indexNames && strcmp(ropt->indexNames, te->tag) != 0)
2329 else if (strcmp(te->desc, "FUNCTION") == 0)
2331 if (!ropt->selFunction)
2333 if (ropt->functionNames && strcmp(ropt->functionNames, te->tag) != 0)
2336 else if (strcmp(te->desc, "TRIGGER") == 0)
2338 if (!ropt->selTrigger)
2340 if (ropt->triggerNames && strcmp(ropt->triggerNames, te->tag) != 0)
2348 * Check if we had a dataDumper. Indicates if the entry is schema or data
2353 * Special Case: If 'SEQUENCE SET' or anything to do with BLOBs, then
2354 * it is considered a data entry. We don't need to check for the
2355 * BLOBS entry or old-style BLOB COMMENTS, because they will have
2356 * hadDumper = true ... but we do need to check new-style BLOB
2359 if (strcmp(te->desc, "SEQUENCE SET") == 0 ||
2360 strcmp(te->desc, "BLOB") == 0 ||
2361 (strcmp(te->desc, "ACL") == 0 &&
2362 strncmp(te->tag, "LARGE OBJECT ", 13) == 0) ||
2363 (strcmp(te->desc, "COMMENT") == 0 &&
2364 strncmp(te->tag, "LARGE OBJECT ", 13) == 0) ||
2365 (strcmp(te->desc, "SECURITY LABEL") == 0 &&
2366 strncmp(te->tag, "LARGE OBJECT ", 13) == 0))
2367 res = res & REQ_DATA;
2369 res = res & ~REQ_DATA;
2373 * Special case: <Init> type with <Max OID> tag; this is obsolete and we
2376 if ((strcmp(te->desc, "<Init>") == 0) && (strcmp(te->tag, "Max OID") == 0))
2379 /* Mask it if we only want schema */
2380 if (ropt->schemaOnly)
2381 res = res & REQ_SCHEMA;
2383 /* Mask it we only want data */
2385 res = res & REQ_DATA;
2387 /* Mask it if we don't have a schema contribution */
2388 if (!te->defn || strlen(te->defn) == 0)
2389 res = res & ~REQ_SCHEMA;
2391 /* Finally, if there's a per-ID filter, limit based on that as well */
2392 if (ropt->idWanted && !ropt->idWanted[te->dumpId - 1])
2399 * Identify TOC entries that are ACLs.
2402 _tocEntryIsACL(TocEntry *te)
2404 /* "ACL LANGUAGE" was a crock emitted only in PG 7.4 */
2405 if (strcmp(te->desc, "ACL") == 0 ||
2406 strcmp(te->desc, "ACL LANGUAGE") == 0 ||
2407 strcmp(te->desc, "DEFAULT ACL") == 0)
2413 * Issue SET commands for parameters that we want to have set the same way
2414 * at all times during execution of a restore script.
2417 _doSetFixedOutputState(ArchiveHandle *AH)
2419 /* Disable statement_timeout in archive for pg_restore/psql */
2420 ahprintf(AH, "SET statement_timeout = 0;\n");
2422 /* Select the correct character set encoding */
2423 ahprintf(AH, "SET client_encoding = '%s';\n",
2424 pg_encoding_to_char(AH->public.encoding));
2426 /* Select the correct string literal syntax */
2427 ahprintf(AH, "SET standard_conforming_strings = %s;\n",
2428 AH->public.std_strings ? "on" : "off");
2430 /* Select the role to be used during restore */
2431 if (AH->ropt && AH->ropt->use_role)
2432 ahprintf(AH, "SET ROLE %s;\n", fmtId(AH->ropt->use_role));
2434 /* Make sure function checking is disabled */
2435 ahprintf(AH, "SET check_function_bodies = false;\n");
2437 /* Avoid annoying notices etc */
2438 ahprintf(AH, "SET client_min_messages = warning;\n");
2439 if (!AH->public.std_strings)
2440 ahprintf(AH, "SET escape_string_warning = off;\n");
2446 * Issue a SET SESSION AUTHORIZATION command. Caller is responsible
2447 * for updating state if appropriate. If user is NULL or an empty string,
2448 * the specification DEFAULT will be used.
2451 _doSetSessionAuth(ArchiveHandle *AH, const char *user)
2453 PQExpBuffer cmd = createPQExpBuffer();
2455 appendPQExpBuffer(cmd, "SET SESSION AUTHORIZATION ");
2458 * SQL requires a string literal here. Might as well be correct.
2461 appendStringLiteralAHX(cmd, user, AH);
2463 appendPQExpBuffer(cmd, "DEFAULT");
2464 appendPQExpBuffer(cmd, ";");
2466 if (RestoringToDB(AH))
2470 res = PQexec(AH->connection, cmd->data);
2472 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
2473 /* NOT warn_or_die_horribly... use -O instead to skip this. */
2474 die_horribly(AH, modulename, "could not set session user to \"%s\": %s",
2475 user, PQerrorMessage(AH->connection));
2480 ahprintf(AH, "%s\n\n", cmd->data);
2482 destroyPQExpBuffer(cmd);
2487 * Issue a SET default_with_oids command. Caller is responsible
2488 * for updating state if appropriate.
2491 _doSetWithOids(ArchiveHandle *AH, const bool withOids)
2493 PQExpBuffer cmd = createPQExpBuffer();
2495 appendPQExpBuffer(cmd, "SET default_with_oids = %s;", withOids ?
2498 if (RestoringToDB(AH))
2502 res = PQexec(AH->connection, cmd->data);
2504 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
2505 warn_or_die_horribly(AH, modulename,
2506 "could not set default_with_oids: %s",
2507 PQerrorMessage(AH->connection));
2512 ahprintf(AH, "%s\n\n", cmd->data);
2514 destroyPQExpBuffer(cmd);
2519 * Issue the commands to connect to the specified database.
2521 * If we're currently restoring right into a database, this will
2522 * actually establish a connection. Otherwise it puts a \connect into
2523 * the script output.
2525 * NULL dbname implies reconnecting to the current DB (pretty useless).
2528 _reconnectToDB(ArchiveHandle *AH, const char *dbname)
2530 if (RestoringToDB(AH))
2531 ReconnectToServer(AH, dbname, NULL);
2534 PQExpBuffer qry = createPQExpBuffer();
2536 appendPQExpBuffer(qry, "\\connect %s\n\n",
2537 dbname ? fmtId(dbname) : "-");
2538 ahprintf(AH, "%s", qry->data);
2539 destroyPQExpBuffer(qry);
2543 * NOTE: currUser keeps track of what the imaginary session user in our
2544 * script is. It's now effectively reset to the original userID.
2548 AH->currUser = NULL;
2550 /* don't assume we still know the output schema, tablespace, etc either */
2552 free(AH->currSchema);
2553 AH->currSchema = NULL;
2554 if (AH->currTablespace)
2555 free(AH->currTablespace);
2556 AH->currTablespace = NULL;
2557 AH->currWithOids = -1;
2559 /* re-establish fixed state */
2560 _doSetFixedOutputState(AH);
2564 * Become the specified user, and update state to avoid redundant commands
2566 * NULL or empty argument is taken to mean restoring the session default
2569 _becomeUser(ArchiveHandle *AH, const char *user)
2572 user = ""; /* avoid null pointers */
2574 if (AH->currUser && strcmp(AH->currUser, user) == 0)
2575 return; /* no need to do anything */
2577 _doSetSessionAuth(AH, user);
2580 * NOTE: currUser keeps track of what the imaginary session user in our
2585 AH->currUser = strdup(user);
2589 * Become the owner of the given TOC entry object. If
2590 * changes in ownership are not allowed, this doesn't do anything.
2593 _becomeOwner(ArchiveHandle *AH, TocEntry *te)
2595 if (AH->ropt && (AH->ropt->noOwner || !AH->ropt->use_setsessauth))
2598 _becomeUser(AH, te->owner);
2603 * Set the proper default_with_oids value for the table.
2606 _setWithOids(ArchiveHandle *AH, TocEntry *te)
2608 if (AH->currWithOids != te->withOids)
2610 _doSetWithOids(AH, te->withOids);
2611 AH->currWithOids = te->withOids;
2617 * Issue the commands to select the specified schema as the current schema
2618 * in the target database.
2621 _selectOutputSchema(ArchiveHandle *AH, const char *schemaName)
2625 if (!schemaName || *schemaName == '\0' ||
2626 (AH->currSchema && strcmp(AH->currSchema, schemaName) == 0))
2627 return; /* no need to do anything */
2629 qry = createPQExpBuffer();
2631 appendPQExpBuffer(qry, "SET search_path = %s",
2633 if (strcmp(schemaName, "pg_catalog") != 0)
2634 appendPQExpBuffer(qry, ", pg_catalog");
2636 if (RestoringToDB(AH))
2640 res = PQexec(AH->connection, qry->data);
2642 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
2643 warn_or_die_horribly(AH, modulename,
2644 "could not set search_path to \"%s\": %s",
2645 schemaName, PQerrorMessage(AH->connection));
2650 ahprintf(AH, "%s;\n\n", qry->data);
2653 free(AH->currSchema);
2654 AH->currSchema = strdup(schemaName);
2656 destroyPQExpBuffer(qry);
2660 * Issue the commands to select the specified tablespace as the current one
2661 * in the target database.
2664 _selectTablespace(ArchiveHandle *AH, const char *tablespace)
2670 /* do nothing in --no-tablespaces mode */
2671 if (AH->ropt->noTablespace)
2674 have = AH->currTablespace;
2677 /* no need to do anything for non-tablespace object */
2681 if (have && strcmp(want, have) == 0)
2682 return; /* no need to do anything */
2684 qry = createPQExpBuffer();
2686 if (strcmp(want, "") == 0)
2688 /* We want the tablespace to be the database's default */
2689 appendPQExpBuffer(qry, "SET default_tablespace = ''");
2693 /* We want an explicit tablespace */
2694 appendPQExpBuffer(qry, "SET default_tablespace = %s", fmtId(want));
2697 if (RestoringToDB(AH))
2701 res = PQexec(AH->connection, qry->data);
2703 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
2704 warn_or_die_horribly(AH, modulename,
2705 "could not set default_tablespace to %s: %s",
2706 fmtId(want), PQerrorMessage(AH->connection));
2711 ahprintf(AH, "%s;\n\n", qry->data);
2713 if (AH->currTablespace)
2714 free(AH->currTablespace);
2715 AH->currTablespace = strdup(want);
2717 destroyPQExpBuffer(qry);
2721 * Extract an object description for a TOC entry, and append it to buf.
2723 * This is not quite as general as it may seem, since it really only
2724 * handles constructing the right thing to put into ALTER ... OWNER TO.
2726 * The whole thing is pretty grotty, but we are kind of stuck since the
2727 * information used is all that's available in older dump files.
2730 _getObjectDescription(PQExpBuffer buf, TocEntry *te, ArchiveHandle *AH)
2732 const char *type = te->desc;
2734 /* Use ALTER TABLE for views and sequences */
2735 if (strcmp(type, "VIEW") == 0 || strcmp(type, "SEQUENCE") == 0)
2738 /* objects named by a schema and name */
2739 if (strcmp(type, "CONVERSION") == 0 ||
2740 strcmp(type, "DOMAIN") == 0 ||
2741 strcmp(type, "TABLE") == 0 ||
2742 strcmp(type, "TYPE") == 0 ||
2743 strcmp(type, "FOREIGN TABLE") == 0 ||
2744 strcmp(type, "TEXT SEARCH DICTIONARY") == 0 ||
2745 strcmp(type, "TEXT SEARCH CONFIGURATION") == 0)
2747 appendPQExpBuffer(buf, "%s ", type);
2748 if (te->namespace && te->namespace[0]) /* is null pre-7.3 */
2749 appendPQExpBuffer(buf, "%s.", fmtId(te->namespace));
2752 * Pre-7.3 pg_dump would sometimes (not always) put a fmtId'd name
2753 * into te->tag for an index. This check is heuristic, so make its
2754 * scope as narrow as possible.
2756 if (AH->version < K_VERS_1_7 &&
2757 te->tag[0] == '"' &&
2758 te->tag[strlen(te->tag) - 1] == '"' &&
2759 strcmp(type, "INDEX") == 0)
2760 appendPQExpBuffer(buf, "%s", te->tag);
2762 appendPQExpBuffer(buf, "%s", fmtId(te->tag));
2766 /* objects named by just a name */
2767 if (strcmp(type, "DATABASE") == 0 ||
2768 strcmp(type, "PROCEDURAL LANGUAGE") == 0 ||
2769 strcmp(type, "SCHEMA") == 0 ||
2770 strcmp(type, "FOREIGN DATA WRAPPER") == 0 ||
2771 strcmp(type, "SERVER") == 0 ||
2772 strcmp(type, "USER MAPPING") == 0)
2774 appendPQExpBuffer(buf, "%s %s", type, fmtId(te->tag));
2778 /* BLOBs just have a name, but it's numeric so must not use fmtId */
2779 if (strcmp(type, "BLOB") == 0)
2781 appendPQExpBuffer(buf, "LARGE OBJECT %s", te->tag);
2786 * These object types require additional decoration. Fortunately, the
2787 * information needed is exactly what's in the DROP command.
2789 if (strcmp(type, "AGGREGATE") == 0 ||
2790 strcmp(type, "FUNCTION") == 0 ||
2791 strcmp(type, "OPERATOR") == 0 ||
2792 strcmp(type, "OPERATOR CLASS") == 0 ||
2793 strcmp(type, "OPERATOR FAMILY") == 0)
2795 /* Chop "DROP " off the front and make a modifiable copy */
2796 char *first = strdup(te->dropStmt + 5);
2799 /* point to last character in string */
2800 last = first + strlen(first) - 1;
2802 /* Strip off any ';' or '\n' at the end */
2803 while (last >= first && (*last == '\n' || *last == ';'))
2807 appendPQExpBufferStr(buf, first);
2813 write_msg(modulename, "WARNING: don't know how to set owner for object type %s\n",
2818 _printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isData, bool acl_pass)
2820 /* ACLs are dumped only during acl pass */
2823 if (!_tocEntryIsACL(te))
2828 if (_tocEntryIsACL(te))
2833 * Avoid dumping the public schema, as it will already be created ...
2834 * unless we are using --clean mode, in which case it's been deleted and
2835 * we'd better recreate it. Likewise for its comment, if any.
2837 if (!ropt->dropSchema)
2839 if (strcmp(te->desc, "SCHEMA") == 0 &&
2840 strcmp(te->tag, "public") == 0)
2842 /* The comment restore would require super-user privs, so avoid it. */
2843 if (strcmp(te->desc, "COMMENT") == 0 &&
2844 strcmp(te->tag, "SCHEMA public") == 0)
2848 /* Select owner, schema, and tablespace as necessary */
2849 _becomeOwner(AH, te);
2850 _selectOutputSchema(AH, te->namespace);
2851 _selectTablespace(AH, te->tablespace);
2853 /* Set up OID mode too */
2854 if (strcmp(te->desc, "TABLE") == 0)
2855 _setWithOids(AH, te);
2857 /* Emit header comment for item */
2858 if (!AH->noTocComments)
2867 ahprintf(AH, "--\n");
2868 if (AH->public.verbose)
2870 ahprintf(AH, "-- TOC entry %d (class %u OID %u)\n",
2871 te->dumpId, te->catalogId.tableoid, te->catalogId.oid);
2876 ahprintf(AH, "-- Dependencies:");
2877 for (i = 0; i < te->nDeps; i++)
2878 ahprintf(AH, " %d", te->dependencies[i]);
2882 ahprintf(AH, "-- %sName: %s; Type: %s; Schema: %s; Owner: %s",
2883 pfx, te->tag, te->desc,
2884 te->namespace ? te->namespace : "-",
2885 ropt->noOwner ? "-" : te->owner);
2886 if (te->tablespace && !ropt->noTablespace)
2887 ahprintf(AH, "; Tablespace: %s", te->tablespace);
2890 if (AH->PrintExtraTocPtr !=NULL)
2891 (*AH->PrintExtraTocPtr) (AH, te);
2892 ahprintf(AH, "--\n\n");
2896 * Actually print the definition.
2898 * Really crude hack for suppressing AUTHORIZATION clause that old pg_dump
2899 * versions put into CREATE SCHEMA. We have to do this when --no-owner
2900 * mode is selected. This is ugly, but I see no other good way ...
2902 if (ropt->noOwner && strcmp(te->desc, "SCHEMA") == 0)
2904 ahprintf(AH, "CREATE SCHEMA %s;\n\n\n", fmtId(te->tag));
2908 if (strlen(te->defn) > 0)
2909 ahprintf(AH, "%s\n\n", te->defn);
2913 * If we aren't using SET SESSION AUTH to determine ownership, we must
2914 * instead issue an ALTER OWNER command. We assume that anything without
2915 * a DROP command is not a separately ownable object. All the categories
2916 * with DROP commands must appear in one list or the other.
2918 if (!ropt->noOwner && !ropt->use_setsessauth &&
2919 strlen(te->owner) > 0 && strlen(te->dropStmt) > 0)
2921 if (strcmp(te->desc, "AGGREGATE") == 0 ||
2922 strcmp(te->desc, "BLOB") == 0 ||
2923 strcmp(te->desc, "CONVERSION") == 0 ||
2924 strcmp(te->desc, "DATABASE") == 0 ||
2925 strcmp(te->desc, "DOMAIN") == 0 ||
2926 strcmp(te->desc, "FUNCTION") == 0 ||
2927 strcmp(te->desc, "OPERATOR") == 0 ||
2928 strcmp(te->desc, "OPERATOR CLASS") == 0 ||
2929 strcmp(te->desc, "OPERATOR FAMILY") == 0 ||
2930 strcmp(te->desc, "PROCEDURAL LANGUAGE") == 0 ||
2931 strcmp(te->desc, "SCHEMA") == 0 ||
2932 strcmp(te->desc, "TABLE") == 0 ||
2933 strcmp(te->desc, "TYPE") == 0 ||
2934 strcmp(te->desc, "VIEW") == 0 ||
2935 strcmp(te->desc, "SEQUENCE") == 0 ||
2936 strcmp(te->desc, "FOREIGN TABLE") == 0 ||
2937 strcmp(te->desc, "TEXT SEARCH DICTIONARY") == 0 ||
2938 strcmp(te->desc, "TEXT SEARCH CONFIGURATION") == 0 ||
2939 strcmp(te->desc, "FOREIGN DATA WRAPPER") == 0 ||
2940 strcmp(te->desc, "SERVER") == 0)
2942 PQExpBuffer temp = createPQExpBuffer();
2944 appendPQExpBuffer(temp, "ALTER ");
2945 _getObjectDescription(temp, te, AH);
2946 appendPQExpBuffer(temp, " OWNER TO %s;", fmtId(te->owner));
2947 ahprintf(AH, "%s\n\n", temp->data);
2948 destroyPQExpBuffer(temp);
2950 else if (strcmp(te->desc, "CAST") == 0 ||
2951 strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
2952 strcmp(te->desc, "CONSTRAINT") == 0 ||
2953 strcmp(te->desc, "DEFAULT") == 0 ||
2954 strcmp(te->desc, "FK CONSTRAINT") == 0 ||
2955 strcmp(te->desc, "INDEX") == 0 ||
2956 strcmp(te->desc, "RULE") == 0 ||
2957 strcmp(te->desc, "TRIGGER") == 0 ||
2958 strcmp(te->desc, "USER MAPPING") == 0)
2960 /* these object types don't have separate owners */
2964 write_msg(modulename, "WARNING: don't know how to set owner for object type %s\n",
2970 * If it's an ACL entry, it might contain SET SESSION AUTHORIZATION
2971 * commands, so we can no longer assume we know the current auth setting.
2977 AH->currUser = NULL;
2982 WriteHead(ArchiveHandle *AH)
2986 (*AH->WriteBufPtr) (AH, "PGDMP", 5); /* Magic code */
2987 (*AH->WriteBytePtr) (AH, AH->vmaj);
2988 (*AH->WriteBytePtr) (AH, AH->vmin);
2989 (*AH->WriteBytePtr) (AH, AH->vrev);
2990 (*AH->WriteBytePtr) (AH, AH->intSize);
2991 (*AH->WriteBytePtr) (AH, AH->offSize);
2992 (*AH->WriteBytePtr) (AH, AH->format);
2995 if (AH->compression != 0)
2996 write_msg(modulename, "WARNING: requested compression not available in this "
2997 "installation -- archive will be uncompressed\n");
2999 AH->compression = 0;
3002 WriteInt(AH, AH->compression);
3004 crtm = *localtime(&AH->createDate);
3005 WriteInt(AH, crtm.tm_sec);
3006 WriteInt(AH, crtm.tm_min);
3007 WriteInt(AH, crtm.tm_hour);
3008 WriteInt(AH, crtm.tm_mday);
3009 WriteInt(AH, crtm.tm_mon);
3010 WriteInt(AH, crtm.tm_year);
3011 WriteInt(AH, crtm.tm_isdst);
3012 WriteStr(AH, PQdb(AH->connection));
3013 WriteStr(AH, AH->public.remoteVersionStr);
3014 WriteStr(AH, PG_VERSION);
3018 ReadHead(ArchiveHandle *AH)
3025 * If we haven't already read the header, do so.
3027 * NB: this code must agree with _discoverArchiveFormat(). Maybe find a
3028 * way to unify the cases?
3030 if (!AH->readHeader)
3032 if ((*AH->ReadBufPtr) (AH, tmpMag, 5) != 5)
3033 die_horribly(AH, modulename, "unexpected end of file\n");
3035 if (strncmp(tmpMag, "PGDMP", 5) != 0)
3036 die_horribly(AH, modulename, "did not find magic string in file header\n");
3038 AH->vmaj = (*AH->ReadBytePtr) (AH);
3039 AH->vmin = (*AH->ReadBytePtr) (AH);
3041 if (AH->vmaj > 1 || ((AH->vmaj == 1) && (AH->vmin > 0))) /* Version > 1.0 */
3042 AH->vrev = (*AH->ReadBytePtr) (AH);
3046 AH->version = ((AH->vmaj * 256 + AH->vmin) * 256 + AH->vrev) * 256 + 0;
3048 if (AH->version < K_VERS_1_0 || AH->version > K_VERS_MAX)
3049 die_horribly(AH, modulename, "unsupported version (%d.%d) in file header\n",
3050 AH->vmaj, AH->vmin);
3052 AH->intSize = (*AH->ReadBytePtr) (AH);
3053 if (AH->intSize > 32)
3054 die_horribly(AH, modulename, "sanity check on integer size (%lu) failed\n",
3055 (unsigned long) AH->intSize);
3057 if (AH->intSize > sizeof(int))
3058 write_msg(modulename, "WARNING: archive was made on a machine with larger integers, some operations might fail\n");
3060 if (AH->version >= K_VERS_1_7)
3061 AH->offSize = (*AH->ReadBytePtr) (AH);
3063 AH->offSize = AH->intSize;
3065 fmt = (*AH->ReadBytePtr) (AH);
3067 if (AH->format != fmt)
3068 die_horribly(AH, modulename, "expected format (%d) differs from format found in file (%d)\n",
3072 if (AH->version >= K_VERS_1_2)
3074 if (AH->version < K_VERS_1_4)
3075 AH->compression = (*AH->ReadBytePtr) (AH);
3077 AH->compression = ReadInt(AH);
3080 AH->compression = Z_DEFAULT_COMPRESSION;
3083 if (AH->compression != 0)
3084 write_msg(modulename, "WARNING: archive is compressed, but this installation does not support compression -- no data will be available\n");
3087 if (AH->version >= K_VERS_1_4)
3089 crtm.tm_sec = ReadInt(AH);
3090 crtm.tm_min = ReadInt(AH);
3091 crtm.tm_hour = ReadInt(AH);
3092 crtm.tm_mday = ReadInt(AH);
3093 crtm.tm_mon = ReadInt(AH);
3094 crtm.tm_year = ReadInt(AH);
3095 crtm.tm_isdst = ReadInt(AH);
3097 AH->archdbname = ReadStr(AH);
3099 AH->createDate = mktime(&crtm);
3101 if (AH->createDate == (time_t) -1)
3102 write_msg(modulename, "WARNING: invalid creation date in header\n");
3105 if (AH->version >= K_VERS_1_10)
3107 AH->archiveRemoteVersion = ReadStr(AH);
3108 AH->archiveDumpVersion = ReadStr(AH);
3115 * check to see if ftell/fseek can be performed.
3123 * If pgoff_t is wider than long, we must have "real" fseeko and not an
3124 * emulation using fseek. Otherwise report no seek capability.
3127 if (sizeof(pgoff_t) > sizeof(long))
3131 /* Check that ftello works on this file */
3138 * Check that fseeko(SEEK_SET) works, too. NB: we used to try to test
3139 * this with fseeko(fp, 0, SEEK_CUR). But some platforms treat that as a
3140 * successful no-op even on files that are otherwise unseekable.
3142 if (fseeko(fp, tpos, SEEK_SET) != 0)
3153 dumpTimestamp(ArchiveHandle *AH, const char *msg, time_t tim)
3158 * We don't print the timezone on Win32, because the names are long and
3159 * localized, which means they may contain characters in various random
3160 * encodings; this has been seen to cause encoding errors when reading the
3163 if (strftime(buf, sizeof(buf),
3165 "%Y-%m-%d %H:%M:%S %Z",
3167 "%Y-%m-%d %H:%M:%S",
3169 localtime(&tim)) != 0)
3170 ahprintf(AH, "-- %s %s\n\n", msg, buf);
3175 * Main engine for parallel restore.
3177 * Work is done in three phases.
3178 * First we process tocEntries until we come to one that is marked
3179 * SECTION_DATA or SECTION_POST_DATA, in a single connection, just as for a
3180 * standard restore. Second we process the remaining non-ACL steps in
3181 * parallel worker children (threads on Windows, processes on Unix), each of
3182 * which connects separately to the database. Finally we process all the ACL
3183 * entries in a single connection (that happens back in RestoreArchive).
3186 restore_toc_entries_parallel(ArchiveHandle *AH)
3188 RestoreOptions *ropt = AH->ropt;
3189 int n_slots = ropt->number_of_jobs;
3190 ParallelSlot *slots;
3193 TocEntry pending_list;
3194 TocEntry ready_list;
3195 TocEntry *next_work_item;
3199 ahlog(AH, 2, "entering restore_toc_entries_parallel\n");
3201 /* we haven't got round to making this work for all archive formats */
3202 if (AH->ClonePtr == NULL || AH->ReopenPtr == NULL)
3203 die_horribly(AH, modulename, "parallel restore is not supported with this archive file format\n");
3205 /* doesn't work if the archive represents dependencies as OIDs, either */
3206 if (AH->version < K_VERS_1_8)
3207 die_horribly(AH, modulename, "parallel restore is not supported with archives made by pre-8.0 pg_dump\n");
3209 slots = (ParallelSlot *) calloc(sizeof(ParallelSlot), n_slots);
3211 /* Adjust dependency information */
3212 fix_dependencies(AH);
3215 * Do all the early stuff in a single connection in the parent. There's no
3216 * great point in running it in parallel, in fact it will actually run
3217 * faster in a single connection because we avoid all the connection and
3218 * setup overhead. Also, pg_dump is not currently very good about
3219 * showing all the dependencies of SECTION_PRE_DATA items, so we do not
3220 * risk trying to process them out-of-order.
3222 for (next_work_item = AH->toc->next; next_work_item != AH->toc; next_work_item = next_work_item->next)
3224 /* Non-PRE_DATA items are just ignored for now */
3225 if (next_work_item->section == SECTION_DATA ||
3226 next_work_item->section == SECTION_POST_DATA)
3229 ahlog(AH, 1, "processing item %d %s %s\n",
3230 next_work_item->dumpId,
3231 next_work_item->desc, next_work_item->tag);
3233 (void) restore_toc_entry(AH, next_work_item, ropt, false);
3235 /* there should be no touch of ready_list here, so pass NULL */
3236 reduce_dependencies(AH, next_work_item, NULL);
3240 * Now close parent connection in prep for parallel steps. We do this
3241 * mainly to ensure that we don't exceed the specified number of parallel
3244 PQfinish(AH->connection);
3245 AH->connection = NULL;
3247 /* blow away any transient state from the old connection */
3250 AH->currUser = NULL;
3252 free(AH->currSchema);
3253 AH->currSchema = NULL;
3254 if (AH->currTablespace)
3255 free(AH->currTablespace);
3256 AH->currTablespace = NULL;
3257 AH->currWithOids = -1;
3260 * Initialize the lists of pending and ready items. After this setup, the
3261 * pending list is everything that needs to be done but is blocked by one
3262 * or more dependencies, while the ready list contains items that have no
3263 * remaining dependencies. Note: we don't yet filter out entries that
3264 * aren't going to be restored. They might participate in dependency
3265 * chains connecting entries that should be restored, so we treat them as
3266 * live until we actually process them.
3268 par_list_header_init(&pending_list);
3269 par_list_header_init(&ready_list);
3270 for (next_work_item = AH->toc->next; next_work_item != AH->toc; next_work_item = next_work_item->next)
3272 /* All PRE_DATA items were dealt with above */
3273 if (next_work_item->section == SECTION_DATA ||
3274 next_work_item->section == SECTION_POST_DATA)
3276 if (next_work_item->depCount > 0)
3277 par_list_append(&pending_list, next_work_item);
3279 par_list_append(&ready_list, next_work_item);
3286 * Keep going until there is no worker still running AND there is no work
3290 ahlog(AH, 1, "entering main parallel loop\n");
3292 while ((next_work_item = get_next_work_item(AH, &ready_list,
3293 slots, n_slots)) != NULL ||
3294 work_in_progress(slots, n_slots))
3296 if (next_work_item != NULL)
3300 /* If not to be dumped, don't waste time launching a worker */
3301 reqs = _tocEntryRequired(next_work_item, AH->ropt, false);
3302 if ((reqs & (REQ_SCHEMA | REQ_DATA)) == 0)
3304 ahlog(AH, 1, "skipping item %d %s %s\n",
3305 next_work_item->dumpId,
3306 next_work_item->desc, next_work_item->tag);
3308 par_list_remove(next_work_item);
3309 reduce_dependencies(AH, next_work_item, &ready_list);
3314 if ((next_slot = get_next_slot(slots, n_slots)) != NO_SLOT)
3316 /* There is work still to do and a worker slot available */
3320 ahlog(AH, 1, "launching item %d %s %s\n",
3321 next_work_item->dumpId,
3322 next_work_item->desc, next_work_item->tag);
3324 par_list_remove(next_work_item);
3326 /* this memory is dealloced in mark_work_done() */
3327 args = malloc(sizeof(RestoreArgs));
3328 args->AH = CloneArchive(AH);
3329 args->te = next_work_item;
3331 /* run the step in a worker child */
3332 child = spawn_restore(args);
3334 slots[next_slot].child_id = child;
3335 slots[next_slot].args = args;
3342 * If we get here there must be work being done. Either there is no
3343 * work available to schedule (and work_in_progress returned true) or
3344 * there are no slots available. So we wait for a worker to finish,
3345 * and process the result.
3347 ret_child = reap_child(slots, n_slots, &work_status);
3349 if (WIFEXITED(work_status))
3351 mark_work_done(AH, &ready_list,
3352 ret_child, WEXITSTATUS(work_status),
3357 die_horribly(AH, modulename, "worker process crashed: status %d\n",
3362 ahlog(AH, 1, "finished main parallel loop\n");
3365 * Now reconnect the single parent connection.
3367 ConnectDatabase((Archive *) AH, ropt->dbname,
3368 ropt->pghost, ropt->pgport, ropt->username,
3369 ropt->promptPassword);
3371 _doSetFixedOutputState(AH);
3374 * Make sure there is no non-ACL work left due to, say, circular
3375 * dependencies, or some other pathological condition. If so, do it in the
3376 * single parent connection.
3378 for (te = pending_list.par_next; te != &pending_list; te = te->par_next)
3380 ahlog(AH, 1, "processing missed item %d %s %s\n",
3381 te->dumpId, te->desc, te->tag);
3382 (void) restore_toc_entry(AH, te, ropt, false);
3385 /* The ACLs will be handled back in RestoreArchive. */
3389 * create a worker child to perform a restore step in parallel
3392 spawn_restore(RestoreArgs *args)
3396 /* Ensure stdio state is quiesced before forking */
3403 /* in child process */
3404 parallel_restore(args);
3405 die_horribly(args->AH, modulename,
3406 "parallel_restore should not return\n");
3411 die_horribly(args->AH, modulename,
3412 "could not create worker process: %s\n",
3416 child = (HANDLE) _beginthreadex(NULL, 0, (void *) parallel_restore,
3419 die_horribly(args->AH, modulename,
3420 "could not create worker thread: %s\n",
3428 * collect status from a completed worker child
3431 reap_child(ParallelSlot *slots, int n_slots, int *work_status)
3434 /* Unix is so much easier ... */
3435 return wait(work_status);
3437 static HANDLE *handles = NULL;
3444 /* first time around only, make space for handles to listen on */
3445 if (handles == NULL)
3446 handles = (HANDLE *) calloc(sizeof(HANDLE), n_slots);
3448 /* set up list of handles to listen to */
3449 for (snum = 0, tnum = 0; snum < n_slots; snum++)
3450 if (slots[snum].child_id != 0)
3451 handles[tnum++] = slots[snum].child_id;
3453 /* wait for one to finish */
3454 hindex = WaitForMultipleObjects(tnum, handles, false, INFINITE);
3456 /* get handle of finished thread */
3457 ret_child = handles[hindex - WAIT_OBJECT_0];
3459 /* get the result */
3460 GetExitCodeThread(ret_child, &res);
3463 /* dispose of handle to stop leaks */
3464 CloseHandle(ret_child);
3471 * are we doing anything now?
3474 work_in_progress(ParallelSlot *slots, int n_slots)
3478 for (i = 0; i < n_slots; i++)
3480 if (slots[i].child_id != 0)
3487 * find the first free parallel slot (if any).
3490 get_next_slot(ParallelSlot *slots, int n_slots)
3494 for (i = 0; i < n_slots; i++)
3496 if (slots[i].child_id == 0)
3504 * Check if te1 has an exclusive lock requirement for an item that te2 also
3505 * requires, whether or not te2's requirement is for an exclusive lock.
3508 has_lock_conflicts(TocEntry *te1, TocEntry *te2)
3513 for (j = 0; j < te1->nLockDeps; j++)
3515 for (k = 0; k < te2->nDeps; k++)
3517 if (te1->lockDeps[j] == te2->dependencies[k])
3526 * Initialize the header of a parallel-processing list.
3528 * These are circular lists with a dummy TocEntry as header, just like the
3529 * main TOC list; but we use separate list links so that an entry can be in
3530 * the main TOC list as well as in a parallel-processing list.
3533 par_list_header_init(TocEntry *l)
3535 l->par_prev = l->par_next = l;
3538 /* Append te to the end of the parallel-processing list headed by l */
3540 par_list_append(TocEntry *l, TocEntry *te)
3542 te->par_prev = l->par_prev;
3543 l->par_prev->par_next = te;
3548 /* Remove te from whatever parallel-processing list it's in */
3550 par_list_remove(TocEntry *te)
3552 te->par_prev->par_next = te->par_next;
3553 te->par_next->par_prev = te->par_prev;
3554 te->par_prev = NULL;
3555 te->par_next = NULL;
3560 * Find the next work item (if any) that is capable of being run now.
3562 * To qualify, the item must have no remaining dependencies
3563 * and no requirements for locks that are incompatible with
3564 * items currently running. Items in the ready_list are known to have
3565 * no remaining dependencies, but we have to check for lock conflicts.
3567 * Note that the returned item has *not* been removed from ready_list.
3568 * The caller must do that after successfully dispatching the item.
3570 * pref_non_data is for an alternative selection algorithm that gives
3571 * preference to non-data items if there is already a data load running.
3572 * It is currently disabled.
3575 get_next_work_item(ArchiveHandle *AH, TocEntry *ready_list,
3576 ParallelSlot *slots, int n_slots)
3578 bool pref_non_data = false; /* or get from AH->ropt */
3579 TocEntry *data_te = NULL;
3585 * Bogus heuristics for pref_non_data
3591 for (k = 0; k < n_slots; k++)
3592 if (slots[k].args->te != NULL &&
3593 slots[k].args->te->section == SECTION_DATA)
3595 if (n_slots == 0 || count * 4 < n_slots)
3596 pref_non_data = false;
3600 * Search the ready_list until we find a suitable item.
3602 for (te = ready_list->par_next; te != ready_list; te = te->par_next)
3604 bool conflicts = false;
3607 * Check to see if the item would need exclusive lock on something
3608 * that a currently running item also needs lock on, or vice versa. If
3609 * so, we don't want to schedule them together.
3611 for (i = 0; i < n_slots && !conflicts; i++)
3613 TocEntry *running_te;
3615 if (slots[i].args == NULL)
3617 running_te = slots[i].args->te;
3619 if (has_lock_conflicts(te, running_te) ||
3620 has_lock_conflicts(running_te, te))
3630 if (pref_non_data && te->section == SECTION_DATA)
3632 if (data_te == NULL)
3637 /* passed all tests, so this item can run */
3641 if (data_te != NULL)
3644 ahlog(AH, 2, "no item ready\n");
3650 * Restore a single TOC item in parallel with others
3652 * this is the procedure run as a thread (Windows) or a
3653 * separate process (everything else).
3655 static parallel_restore_result
3656 parallel_restore(RestoreArgs *args)
3658 ArchiveHandle *AH = args->AH;
3659 TocEntry *te = args->te;
3660 RestoreOptions *ropt = AH->ropt;
3664 * Close and reopen the input file so we have a private file pointer that
3665 * doesn't stomp on anyone else's file pointer, if we're actually going to
3666 * need to read from the file. Otherwise, just close it except on Windows,
3667 * where it will possibly be needed by other threads.
3669 * Note: on Windows, since we are using threads not processes, the reopen
3670 * call *doesn't* close the original file pointer but just open a new one.
3672 if (te->section == SECTION_DATA)
3673 (AH->ReopenPtr) (AH);
3676 (AH->ClosePtr) (AH);
3680 * We need our own database connection, too
3682 ConnectDatabase((Archive *) AH, ropt->dbname,
3683 ropt->pghost, ropt->pgport, ropt->username,
3684 ropt->promptPassword);
3686 _doSetFixedOutputState(AH);
3688 /* Restore the TOC item */
3689 retval = restore_toc_entry(AH, te, ropt, true);
3692 PQfinish(AH->connection);
3693 AH->connection = NULL;
3695 /* If we reopened the file, we are done with it, so close it now */
3696 if (te->section == SECTION_DATA)
3697 (AH->ClosePtr) (AH);
3699 if (retval == 0 && AH->public.n_errors)
3700 retval = WORKER_IGNORED_ERRORS;
3711 * Housekeeping to be done after a step has been parallel restored.
3713 * Clear the appropriate slot, free all the extra memory we allocated,
3714 * update status, and reduce the dependency count of any dependent items.
3717 mark_work_done(ArchiveHandle *AH, TocEntry *ready_list,
3718 thandle worker, int status,
3719 ParallelSlot *slots, int n_slots)
3721 TocEntry *te = NULL;
3724 for (i = 0; i < n_slots; i++)
3726 if (slots[i].child_id == worker)
3728 slots[i].child_id = 0;
3729 te = slots[i].args->te;
3730 DeCloneArchive(slots[i].args->AH);
3731 free(slots[i].args);
3732 slots[i].args = NULL;
3739 die_horribly(AH, modulename, "could not find slot of finished worker\n");
3741 ahlog(AH, 1, "finished item %d %s %s\n",
3742 te->dumpId, te->desc, te->tag);
3744 if (status == WORKER_CREATE_DONE)
3745 mark_create_done(AH, te);
3746 else if (status == WORKER_INHIBIT_DATA)
3748 inhibit_data_for_failed_table(AH, te);
3749 AH->public.n_errors++;
3751 else if (status == WORKER_IGNORED_ERRORS)
3752 AH->public.n_errors++;
3753 else if (status != 0)
3754 die_horribly(AH, modulename, "worker process failed: exit code %d\n",
3757 reduce_dependencies(AH, te, ready_list);
3762 * Process the dependency information into a form useful for parallel restore.
3764 * This function takes care of fixing up some missing or badly designed
3765 * dependencies, and then prepares subsidiary data structures that will be
3766 * used in the main parallel-restore logic, including:
3767 * 1. We build the tocsByDumpId[] index array.
3768 * 2. We build the revDeps[] arrays of incoming dependency dumpIds.
3769 * 3. We set up depCount fields that are the number of as-yet-unprocessed
3770 * dependencies for each TOC entry.
3772 * We also identify locking dependencies so that we can avoid trying to
3773 * schedule conflicting items at the same time.
3776 fix_dependencies(ArchiveHandle *AH)
3782 * It is convenient to have an array that indexes the TOC entries by dump
3783 * ID, rather than searching the TOC list repeatedly. Entries for dump
3784 * IDs not present in the TOC will be NULL.
3786 * NOTE: because maxDumpId is just the highest dump ID defined in the
3787 * archive, there might be dependencies for IDs > maxDumpId. All uses of
3788 * this array must guard against out-of-range dependency numbers.
3790 * Also, initialize the depCount/revDeps/nRevDeps fields, and make sure
3791 * the TOC items are marked as not being in any parallel-processing list.
3793 maxDumpId = AH->maxDumpId;
3794 tocsByDumpId = (TocEntry **) calloc(maxDumpId, sizeof(TocEntry *));
3795 for (te = AH->toc->next; te != AH->toc; te = te->next)
3797 tocsByDumpId[te->dumpId - 1] = te;
3798 te->depCount = te->nDeps;
3801 te->par_prev = NULL;
3802 te->par_next = NULL;
3806 * POST_DATA items that are shown as depending on a table need to be
3807 * re-pointed to depend on that table's data, instead. This ensures they
3808 * won't get scheduled until the data has been loaded. We handle this by
3809 * first finding TABLE/TABLE DATA pairs and then scanning all the
3812 * Note: currently, a TABLE DATA should always have exactly one
3813 * dependency, on its TABLE item. So we don't bother to search, but look
3814 * just at the first dependency. We do trouble to make sure that it's a
3815 * TABLE, if possible. However, if the dependency isn't in the archive
3816 * then just assume it was a TABLE; this is to cover cases where the table
3817 * was suppressed but we have the data and some dependent post-data items.
3819 * XXX this is O(N^2) if there are a lot of tables. We ought to fix
3820 * pg_dump to produce correctly-linked dependencies in the first place.
3822 for (te = AH->toc->next; te != AH->toc; te = te->next)
3824 if (strcmp(te->desc, "TABLE DATA") == 0 && te->nDeps > 0)
3826 DumpId tableId = te->dependencies[0];
3828 if (tableId > maxDumpId ||
3829 tocsByDumpId[tableId - 1] == NULL ||
3830 strcmp(tocsByDumpId[tableId - 1]->desc, "TABLE") == 0)
3832 repoint_table_dependencies(AH, tableId, te->dumpId);
3838 * Pre-8.4 versions of pg_dump neglected to set up a dependency from BLOB
3839 * COMMENTS to BLOBS. Cope. (We assume there's only one BLOBS and only
3840 * one BLOB COMMENTS in such files.)
3842 if (AH->version < K_VERS_1_11)
3844 for (te = AH->toc->next; te != AH->toc; te = te->next)
3846 if (strcmp(te->desc, "BLOB COMMENTS") == 0 && te->nDeps == 0)
3850 for (te2 = AH->toc->next; te2 != AH->toc; te2 = te2->next)
3852 if (strcmp(te2->desc, "BLOBS") == 0)
3854 te->dependencies = (DumpId *) malloc(sizeof(DumpId));
3855 te->dependencies[0] = te2->dumpId;
3867 * At this point we start to build the revDeps reverse-dependency arrays,
3868 * so all changes of dependencies must be complete.
3872 * Count the incoming dependencies for each item. Also, it is possible
3873 * that the dependencies list items that are not in the archive at
3874 * all. Subtract such items from the depCounts.
3876 for (te = AH->toc->next; te != AH->toc; te = te->next)
3878 for (i = 0; i < te->nDeps; i++)
3880 DumpId depid = te->dependencies[i];
3882 if (depid <= maxDumpId && tocsByDumpId[depid - 1] != NULL)
3883 tocsByDumpId[depid - 1]->nRevDeps++;
3890 * Allocate space for revDeps[] arrays, and reset nRevDeps so we can
3891 * use it as a counter below.
3893 for (te = AH->toc->next; te != AH->toc; te = te->next)
3895 if (te->nRevDeps > 0)
3896 te->revDeps = (DumpId *) malloc(te->nRevDeps * sizeof(DumpId));
3901 * Build the revDeps[] arrays of incoming-dependency dumpIds. This
3902 * had better agree with the loops above.
3904 for (te = AH->toc->next; te != AH->toc; te = te->next)
3906 for (i = 0; i < te->nDeps; i++)
3908 DumpId depid = te->dependencies[i];
3910 if (depid <= maxDumpId && tocsByDumpId[depid - 1] != NULL)
3912 TocEntry *otherte = tocsByDumpId[depid - 1];
3914 otherte->revDeps[otherte->nRevDeps++] = te->dumpId;
3920 * Lastly, work out the locking dependencies.
3922 for (te = AH->toc->next; te != AH->toc; te = te->next)
3924 te->lockDeps = NULL;
3926 identify_locking_dependencies(te);
3931 * Change dependencies on tableId to depend on tableDataId instead,
3932 * but only in POST_DATA items.
3935 repoint_table_dependencies(ArchiveHandle *AH,
3936 DumpId tableId, DumpId tableDataId)
3941 for (te = AH->toc->next; te != AH->toc; te = te->next)
3943 if (te->section != SECTION_POST_DATA)
3945 for (i = 0; i < te->nDeps; i++)
3947 if (te->dependencies[i] == tableId)
3949 te->dependencies[i] = tableDataId;
3950 ahlog(AH, 2, "transferring dependency %d -> %d to %d\n",
3951 te->dumpId, tableId, tableDataId);
3958 * Identify which objects we'll need exclusive lock on in order to restore
3959 * the given TOC entry (*other* than the one identified by the TOC entry
3960 * itself). Record their dump IDs in the entry's lockDeps[] array.
3963 identify_locking_dependencies(TocEntry *te)
3969 /* Quick exit if no dependencies at all */
3973 /* Exit if this entry doesn't need exclusive lock on other objects */
3974 if (!(strcmp(te->desc, "CONSTRAINT") == 0 ||
3975 strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
3976 strcmp(te->desc, "FK CONSTRAINT") == 0 ||
3977 strcmp(te->desc, "RULE") == 0 ||
3978 strcmp(te->desc, "TRIGGER") == 0))
3982 * We assume the item requires exclusive lock on each TABLE DATA item
3983 * listed among its dependencies. (This was originally a dependency on
3984 * the TABLE, but fix_dependencies repointed it to the data item. Note
3985 * that all the entry types we are interested in here are POST_DATA, so
3986 * they will all have been changed this way.)
3988 lockids = (DumpId *) malloc(te->nDeps * sizeof(DumpId));
3990 for (i = 0; i < te->nDeps; i++)
3992 DumpId depid = te->dependencies[i];
3994 if (depid <= maxDumpId && tocsByDumpId[depid - 1] &&
3995 strcmp(tocsByDumpId[depid - 1]->desc, "TABLE DATA") == 0)
3996 lockids[nlockids++] = depid;
4005 te->lockDeps = realloc(lockids, nlockids * sizeof(DumpId));
4006 te->nLockDeps = nlockids;
4010 * Remove the specified TOC entry from the depCounts of items that depend on
4011 * it, thereby possibly making them ready-to-run. Any pending item that
4012 * becomes ready should be moved to the ready list.
4015 reduce_dependencies(ArchiveHandle *AH, TocEntry *te, TocEntry *ready_list)
4019 ahlog(AH, 2, "reducing dependencies for %d\n", te->dumpId);
4021 for (i = 0; i < te->nRevDeps; i++)
4023 TocEntry *otherte = tocsByDumpId[te->revDeps[i] - 1];
4025 otherte->depCount--;
4026 if (otherte->depCount == 0 && otherte->par_prev != NULL)
4028 /* It must be in the pending list, so remove it ... */
4029 par_list_remove(otherte);
4030 /* ... and add to ready_list */
4031 par_list_append(ready_list, otherte);
4037 * Set the created flag on the DATA member corresponding to the given
4041 mark_create_done(ArchiveHandle *AH, TocEntry *te)
4045 for (tes = AH->toc->next; tes != AH->toc; tes = tes->next)
4047 if (strcmp(tes->desc, "TABLE DATA") == 0 &&
4048 strcmp(tes->tag, te->tag) == 0 &&
4049 strcmp(tes->namespace ? tes->namespace : "",
4050 te->namespace ? te->namespace : "") == 0)
4052 tes->created = true;
4059 * Mark the DATA member corresponding to the given TABLE member
4063 inhibit_data_for_failed_table(ArchiveHandle *AH, TocEntry *te)
4065 RestoreOptions *ropt = AH->ropt;
4068 ahlog(AH, 1, "table \"%s\" could not be created, will not restore its data\n",
4071 for (tes = AH->toc->next; tes != AH->toc; tes = tes->next)
4073 if (strcmp(tes->desc, "TABLE DATA") == 0 &&
4074 strcmp(tes->tag, te->tag) == 0 &&
4075 strcmp(tes->namespace ? tes->namespace : "",
4076 te->namespace ? te->namespace : "") == 0)
4078 /* mark it unwanted; we assume idWanted array already exists */
4079 ropt->idWanted[tes->dumpId - 1] = false;
4087 * Clone and de-clone routines used in parallel restoration.
4089 * Enough of the structure is cloned to ensure that there is no
4090 * conflict between different threads each with their own clone.
4092 * These could be public, but no need at present.
4094 static ArchiveHandle *
4095 CloneArchive(ArchiveHandle *AH)
4097 ArchiveHandle *clone;
4099 /* Make a "flat" copy */
4100 clone = (ArchiveHandle *) malloc(sizeof(ArchiveHandle));
4102 die_horribly(AH, modulename, "out of memory\n");
4103 memcpy(clone, AH, sizeof(ArchiveHandle));
4105 /* Handle format-independent fields */
4106 clone->pgCopyBuf = createPQExpBuffer();
4107 clone->sqlBuf = createPQExpBuffer();
4108 clone->sqlparse.tagBuf = NULL;
4110 /* The clone will have its own connection, so disregard connection state */
4111 clone->connection = NULL;
4112 clone->currUser = NULL;
4113 clone->currSchema = NULL;
4114 clone->currTablespace = NULL;
4115 clone->currWithOids = -1;
4117 /* savedPassword must be local in case we change it while connecting */
4118 if (clone->savedPassword)
4119 clone->savedPassword = strdup(clone->savedPassword);
4121 /* clone has its own error count, too */
4122 clone->public.n_errors = 0;
4124 /* Let the format-specific code have a chance too */
4125 (clone->ClonePtr) (clone);
4131 * Release clone-local storage.
4133 * Note: we assume any clone-local connection was already closed.
4136 DeCloneArchive(ArchiveHandle *AH)
4138 /* Clear format-specific state */
4139 (AH->DeClonePtr) (AH);
4141 /* Clear state allocated by CloneArchive */
4142 destroyPQExpBuffer(AH->pgCopyBuf);
4143 destroyPQExpBuffer(AH->sqlBuf);
4144 if (AH->sqlparse.tagBuf)
4145 destroyPQExpBuffer(AH->sqlparse.tagBuf);
4147 /* Clear any connection-local state */
4151 free(AH->currSchema);
4152 if (AH->currTablespace)
4153 free(AH->currTablespace);
4154 if (AH->savedPassword)
4155 free(AH->savedPassword);