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"
25 #include "dumputils.h"
30 #include <sys/types.h>
37 #include "libpq/libpq-fs.h"
40 * Special exit values from worker children. We reserve 0 for normal
41 * success; 1 and other small values should be interpreted as crashes.
43 #define WORKER_CREATE_DONE 10
44 #define WORKER_INHIBIT_DATA 11
45 #define WORKER_IGNORED_ERRORS 12
48 * Unix uses exit to return result from worker child, so function is void.
49 * Windows thread result comes via function return.
52 #define parallel_restore_result void
54 #define parallel_restore_result DWORD
57 /* IDs for worker children are either PIDs or thread handles */
61 #define thandle HANDLE
64 /* Arguments needed for a worker child */
65 typedef struct _restore_args
71 /* State for each parallel activity slot */
72 typedef struct _parallel_slot
80 #define TEXT_DUMP_HEADER "--\n-- PostgreSQL database dump\n--\n\n"
81 #define TEXT_DUMPALL_HEADER "--\n-- PostgreSQL database cluster dump\n--\n\n"
83 /* state needed to save/restore an archive's output target */
84 typedef struct _outputContext
90 static const char *modulename = gettext_noop("archiver");
92 /* index array created by fix_dependencies -- only used in parallel restore */
93 static TocEntry **tocsByDumpId; /* index by dumpId - 1 */
94 static DumpId maxDumpId; /* length of above array */
97 static ArchiveHandle *_allocAH(const char *FileSpec, const ArchiveFormat fmt,
98 const int compression, ArchiveMode mode);
99 static void _getObjectDescription(PQExpBuffer buf, TocEntry *te,
101 static void _printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isData, bool acl_pass);
104 static void _doSetFixedOutputState(ArchiveHandle *AH);
105 static void _doSetSessionAuth(ArchiveHandle *AH, const char *user);
106 static void _doSetWithOids(ArchiveHandle *AH, const bool withOids);
107 static void _reconnectToDB(ArchiveHandle *AH, const char *dbname);
108 static void _becomeUser(ArchiveHandle *AH, const char *user);
109 static void _becomeOwner(ArchiveHandle *AH, TocEntry *te);
110 static void _selectOutputSchema(ArchiveHandle *AH, const char *schemaName);
111 static void _selectTablespace(ArchiveHandle *AH, const char *tablespace);
112 static void processEncodingEntry(ArchiveHandle *AH, TocEntry *te);
113 static void processStdStringsEntry(ArchiveHandle *AH, TocEntry *te);
114 static teReqs _tocEntryRequired(TocEntry *te, RestoreOptions *ropt, bool include_acls);
115 static bool _tocEntryIsACL(TocEntry *te);
116 static void _disableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
117 static void _enableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
118 static TocEntry *getTocEntryByDumpId(ArchiveHandle *AH, DumpId id);
119 static void _moveBefore(ArchiveHandle *AH, TocEntry *pos, TocEntry *te);
120 static int _discoverArchiveFormat(ArchiveHandle *AH);
122 static int RestoringToDB(ArchiveHandle *AH);
123 static void dump_lo_buf(ArchiveHandle *AH);
124 static void vdie_horribly(ArchiveHandle *AH, const char *modulename,
125 const char *fmt, va_list ap)
126 __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 0), noreturn));
128 static void dumpTimestamp(ArchiveHandle *AH, const char *msg, time_t tim);
129 static void SetOutput(ArchiveHandle *AH, const char *filename, int compression);
130 static OutputContext SaveOutput(ArchiveHandle *AH);
131 static void RestoreOutput(ArchiveHandle *AH, OutputContext savedContext);
133 static int restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
134 RestoreOptions *ropt, bool is_parallel);
135 static void restore_toc_entries_parallel(ArchiveHandle *AH);
136 static thandle spawn_restore(RestoreArgs *args);
137 static thandle reap_child(ParallelSlot *slots, int n_slots, int *work_status);
138 static bool work_in_progress(ParallelSlot *slots, int n_slots);
139 static int get_next_slot(ParallelSlot *slots, int n_slots);
140 static void par_list_header_init(TocEntry *l);
141 static void par_list_append(TocEntry *l, TocEntry *te);
142 static void par_list_remove(TocEntry *te);
143 static TocEntry *get_next_work_item(ArchiveHandle *AH,
144 TocEntry *ready_list,
145 ParallelSlot *slots, int n_slots);
146 static parallel_restore_result parallel_restore(RestoreArgs *args);
147 static void mark_work_done(ArchiveHandle *AH, TocEntry *ready_list,
148 thandle worker, int status,
149 ParallelSlot *slots, int n_slots);
150 static void fix_dependencies(ArchiveHandle *AH);
151 static bool has_lock_conflicts(TocEntry *te1, TocEntry *te2);
152 static void repoint_table_dependencies(ArchiveHandle *AH,
153 DumpId tableId, DumpId tableDataId);
154 static void identify_locking_dependencies(TocEntry *te);
155 static void reduce_dependencies(ArchiveHandle *AH, TocEntry *te,
156 TocEntry *ready_list);
157 static void mark_create_done(ArchiveHandle *AH, TocEntry *te);
158 static void inhibit_data_for_failed_table(ArchiveHandle *AH, TocEntry *te);
159 static ArchiveHandle *CloneArchive(ArchiveHandle *AH);
160 static void DeCloneArchive(ArchiveHandle *AH);
166 * The objective it to make writing new formats and dumpers as simple
167 * as possible, if necessary at the expense of extra function calls etc.
172 /* Create a new archive */
175 CreateArchive(const char *FileSpec, const ArchiveFormat fmt,
176 const int compression, ArchiveMode mode)
179 ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression, mode);
181 return (Archive *) AH;
184 /* Open an existing archive */
187 OpenArchive(const char *FileSpec, const ArchiveFormat fmt)
189 ArchiveHandle *AH = _allocAH(FileSpec, fmt, 0, archModeRead);
191 return (Archive *) AH;
196 CloseArchive(Archive *AHX)
199 ArchiveHandle *AH = (ArchiveHandle *) AHX;
201 (*AH->ClosePtr) (AH);
203 /* Close the output */
205 res = GZCLOSE(AH->OF);
206 else if (AH->OF != stdout)
207 res = fclose(AH->OF);
210 die_horribly(AH, modulename, "could not close output file: %s\n",
216 RestoreArchive(Archive *AHX, RestoreOptions *ropt)
218 ArchiveHandle *AH = (ArchiveHandle *) AHX;
225 AH->stage = STAGE_INITIALIZING;
228 * Check for nonsensical option combinations.
230 * NB: createDB+dropSchema is useless because if you're creating the DB,
231 * there's no need to drop individual items in it. Moreover, if we tried
232 * to do that then we'd issue the drops in the database initially
233 * connected to, not the one we will create, which is very bad...
235 if (ropt->createDB && ropt->dropSchema)
236 die_horribly(AH, modulename, "-C and -c are incompatible options\n");
239 * -C is not compatible with -1, because we can't create a database inside
240 * a transaction block.
242 if (ropt->createDB && ropt->single_txn)
243 die_horribly(AH, modulename, "-C and -1 are incompatible options\n");
246 * If we're going to do parallel restore, there are some restrictions.
248 parallel_mode = (ropt->number_of_jobs > 1 && ropt->useDB);
251 /* We haven't got round to making this work for all archive formats */
252 if (AH->ClonePtr == NULL || AH->ReopenPtr == NULL)
253 die_horribly(AH, modulename, "parallel restore is not supported with this archive file format\n");
255 /* Doesn't work if the archive represents dependencies as OIDs */
256 if (AH->version < K_VERS_1_8)
257 die_horribly(AH, modulename, "parallel restore is not supported with archives made by pre-8.0 pg_dump\n");
260 * It's also not gonna work if we can't reopen the input file, so
261 * let's try that immediately.
263 (AH->ReopenPtr) (AH);
267 * Make sure we won't need (de)compression we haven't got
270 if (AH->compression != 0 && AH->PrintTocDataPtr !=NULL)
272 for (te = AH->toc->next; te != AH->toc; te = te->next)
274 reqs = _tocEntryRequired(te, ropt, false);
275 if (te->hadDumper && (reqs & REQ_DATA) != 0)
276 die_horribly(AH, modulename, "cannot restore from compressed archive (compression not supported in this installation)\n");
282 * If we're using a DB connection, then connect it.
286 ahlog(AH, 1, "connecting to database for restore\n");
287 if (AH->version < K_VERS_1_3)
288 die_horribly(AH, modulename, "direct database connections are not supported in pre-1.3 archives\n");
290 /* XXX Should get this from the archive */
291 AHX->minRemoteVersion = 070100;
292 AHX->maxRemoteVersion = 999999;
294 ConnectDatabase(AHX, ropt->dbname,
295 ropt->pghost, ropt->pgport, ropt->username,
296 ropt->promptPassword);
299 * If we're talking to the DB directly, don't send comments since they
300 * obscure SQL when displaying errors
302 AH->noTocComments = 1;
306 * Work out if we have an implied data-only restore. This can happen if
307 * the dump was data only or if the user has used a toc list to exclude
308 * all of the schema data. All we do is look for schema entries - if none
309 * are found then we set the dataOnly flag.
311 * We could scan for wanted TABLE entries, but that is not the same as
312 * dataOnly. At this stage, it seems unnecessary (6-Mar-2001).
316 int impliedDataOnly = 1;
318 for (te = AH->toc->next; te != AH->toc; te = te->next)
320 reqs = _tocEntryRequired(te, ropt, true);
321 if ((reqs & REQ_SCHEMA) != 0)
322 { /* It's schema, and it's wanted */
329 ropt->dataOnly = impliedDataOnly;
330 ahlog(AH, 1, "implied data-only restore\n");
335 * Setup the output file if necessary.
337 sav = SaveOutput(AH);
338 if (ropt->filename || ropt->compression)
339 SetOutput(AH, ropt->filename, ropt->compression);
341 ahprintf(AH, "--\n-- PostgreSQL database dump\n--\n\n");
343 if (AH->public.verbose)
345 if (AH->archiveRemoteVersion)
346 ahprintf(AH, "-- Dumped from database version %s\n",
347 AH->archiveRemoteVersion);
348 if (AH->archiveDumpVersion)
349 ahprintf(AH, "-- Dumped by pg_dump version %s\n",
350 AH->archiveDumpVersion);
351 dumpTimestamp(AH, "Started on", AH->createDate);
354 if (ropt->single_txn)
357 StartTransaction(AH);
359 ahprintf(AH, "BEGIN;\n\n");
363 * Establish important parameter values right away.
365 _doSetFixedOutputState(AH);
367 AH->stage = STAGE_PROCESSING;
370 * Drop the items at the start, in reverse order
372 if (ropt->dropSchema)
374 for (te = AH->toc->prev; te != AH->toc; te = te->prev)
378 reqs = _tocEntryRequired(te, ropt, false /* needn't drop ACLs */ );
379 /* We want anything that's selected and has a dropStmt */
380 if (((reqs & (REQ_SCHEMA | REQ_DATA)) != 0) && te->dropStmt)
382 ahlog(AH, 1, "dropping %s %s\n", te->desc, te->tag);
383 /* Select owner and schema as necessary */
384 _becomeOwner(AH, te);
385 _selectOutputSchema(AH, te->namespace);
387 ahprintf(AH, "%s", te->dropStmt);
392 * _selectOutputSchema may have set currSchema to reflect the effect
393 * of a "SET search_path" command it emitted. However, by now we may
394 * have dropped that schema; or it might not have existed in the first
395 * place. In either case the effective value of search_path will not
396 * be what we think. Forcibly reset currSchema so that we will
397 * re-establish the search_path setting when needed (after creating
400 * If we treated users as pg_dump'able objects then we'd need to reset
404 free(AH->currSchema);
405 AH->currSchema = NULL;
409 * In serial mode, we now process each non-ACL TOC entry.
411 * In parallel mode, turn control over to the parallel-restore logic.
414 restore_toc_entries_parallel(AH);
417 for (te = AH->toc->next; te != AH->toc; te = te->next)
418 (void) restore_toc_entry(AH, te, ropt, false);
422 * Scan TOC again to output ownership commands and ACLs
424 for (te = AH->toc->next; te != AH->toc; te = te->next)
428 /* Work out what, if anything, we want from this entry */
429 reqs = _tocEntryRequired(te, ropt, true);
431 /* Both schema and data objects might now have ownership/ACLs */
432 if ((reqs & (REQ_SCHEMA | REQ_DATA)) != 0)
434 ahlog(AH, 1, "setting owner and privileges for %s %s\n",
436 _printTocEntry(AH, te, ropt, false, true);
440 if (ropt->single_txn)
443 CommitTransaction(AH);
445 ahprintf(AH, "COMMIT;\n\n");
448 if (AH->public.verbose)
449 dumpTimestamp(AH, "Completed on", time(NULL));
451 ahprintf(AH, "--\n-- PostgreSQL database dump complete\n--\n\n");
454 * Clean up & we're done.
456 AH->stage = STAGE_FINALIZING;
458 if (ropt->filename || ropt->compression)
459 RestoreOutput(AH, sav);
463 PQfinish(AH->connection);
464 AH->connection = NULL;
469 * Restore a single TOC item. Used in both parallel and non-parallel restore;
470 * is_parallel is true if we are in a worker child process.
472 * Returns 0 normally, but WORKER_CREATE_DONE or WORKER_INHIBIT_DATA if
473 * the parallel parent has to make the corresponding status update.
476 restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
477 RestoreOptions *ropt, bool is_parallel)
485 /* Work out what, if anything, we want from this entry */
486 reqs = _tocEntryRequired(te, ropt, false);
488 /* Dump any relevant dump warnings to stderr */
489 if (!ropt->suppressDumpWarnings && strcmp(te->desc, "WARNING") == 0)
491 if (!ropt->dataOnly && te->defn != NULL && strlen(te->defn) != 0)
492 write_msg(modulename, "warning from original dump file: %s\n", te->defn);
493 else if (te->copyStmt != NULL && strlen(te->copyStmt) != 0)
494 write_msg(modulename, "warning from original dump file: %s\n", te->copyStmt);
499 if ((reqs & REQ_SCHEMA) != 0) /* We want the schema */
501 ahlog(AH, 1, "creating %s %s\n", te->desc, te->tag);
503 _printTocEntry(AH, te, ropt, false, false);
506 if (strcmp(te->desc, "TABLE") == 0)
508 if (AH->lastErrorTE == te)
511 * We failed to create the table. If
512 * --no-data-for-failed-tables was given, mark the
513 * corresponding TABLE DATA to be ignored.
515 * In the parallel case this must be done in the parent, so we
516 * just set the return value.
518 if (ropt->noDataForFailedTables)
521 retval = WORKER_INHIBIT_DATA;
523 inhibit_data_for_failed_table(AH, te);
529 * We created the table successfully. Mark the corresponding
530 * TABLE DATA for possible truncation.
532 * In the parallel case this must be done in the parent, so we
533 * just set the return value.
536 retval = WORKER_CREATE_DONE;
538 mark_create_done(AH, te);
542 /* If we created a DB, connect to it... */
543 if (strcmp(te->desc, "DATABASE") == 0)
545 ahlog(AH, 1, "connecting to new database \"%s\"\n", te->tag);
546 _reconnectToDB(AH, te->tag);
547 ropt->dbname = pg_strdup(te->tag);
552 * If we have a data component, then process it
554 if ((reqs & REQ_DATA) != 0)
557 * hadDumper will be set if there is genuine data component for this
558 * node. Otherwise, we need to check the defn field for statements
559 * that need to be executed in data-only restores.
564 * If we can output the data, then restore it.
566 if (AH->PrintTocDataPtr !=NULL && (reqs & REQ_DATA) != 0)
568 _printTocEntry(AH, te, ropt, true, false);
570 if (strcmp(te->desc, "BLOBS") == 0 ||
571 strcmp(te->desc, "BLOB COMMENTS") == 0)
573 ahlog(AH, 1, "restoring %s\n", te->desc);
575 _selectOutputSchema(AH, "pg_catalog");
577 (*AH->PrintTocDataPtr) (AH, te, ropt);
581 _disableTriggersIfNecessary(AH, te, ropt);
583 /* Select owner and schema as necessary */
584 _becomeOwner(AH, te);
585 _selectOutputSchema(AH, te->namespace);
587 ahlog(AH, 1, "restoring data for table \"%s\"\n",
591 * In parallel restore, if we created the table earlier in
592 * the run then we wrap the COPY in a transaction and
593 * precede it with a TRUNCATE. If archiving is not on
594 * this prevents WAL-logging the COPY. This obtains a
595 * speedup similar to that from using single_txn mode in
596 * non-parallel restores.
598 if (is_parallel && te->created)
601 * Parallel restore is always talking directly to a
602 * server, so no need to see if we should issue BEGIN.
604 StartTransaction(AH);
607 * If the server version is >= 8.4, make sure we issue
608 * TRUNCATE with ONLY so that child tables are not
611 ahprintf(AH, "TRUNCATE TABLE %s%s;\n\n",
612 (PQserverVersion(AH->connection) >= 80400 ?
618 * If we have a copy statement, use it.
620 if (te->copyStmt && strlen(te->copyStmt) > 0)
622 ahprintf(AH, "%s", te->copyStmt);
623 AH->outputKind = OUTPUT_COPYDATA;
626 AH->outputKind = OUTPUT_OTHERDATA;
628 (*AH->PrintTocDataPtr) (AH, te, ropt);
631 * Terminate COPY if needed.
633 if (AH->outputKind == OUTPUT_COPYDATA &&
635 EndDBCopyMode(AH, te);
636 AH->outputKind = OUTPUT_SQLCMDS;
638 /* close out the transaction started above */
639 if (is_parallel && te->created)
640 CommitTransaction(AH);
642 _enableTriggersIfNecessary(AH, te, ropt);
646 else if (!defnDumped)
648 /* If we haven't already dumped the defn part, do so now */
649 ahlog(AH, 1, "executing %s %s\n", te->desc, te->tag);
650 _printTocEntry(AH, te, ropt, false, false);
658 * Allocate a new RestoreOptions block.
659 * This is mainly so we can initialize it, but also for future expansion,
662 NewRestoreOptions(void)
664 RestoreOptions *opts;
666 opts = (RestoreOptions *) pg_calloc(1, sizeof(RestoreOptions));
668 /* set any fields that shouldn't default to zeroes */
669 opts->format = archUnknown;
670 opts->promptPassword = TRI_DEFAULT;
671 opts->dumpSections = DUMP_UNSECTIONED;
677 _disableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
679 /* This hack is only needed in a data-only restore */
680 if (!ropt->dataOnly || !ropt->disable_triggers)
683 ahlog(AH, 1, "disabling triggers for %s\n", te->tag);
686 * Become superuser if possible, since they are the only ones who can
687 * disable constraint triggers. If -S was not given, assume the initial
688 * user identity is a superuser. (XXX would it be better to become the
691 _becomeUser(AH, ropt->superuser);
696 _selectOutputSchema(AH, te->namespace);
698 ahprintf(AH, "ALTER TABLE %s DISABLE TRIGGER ALL;\n\n",
703 _enableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
705 /* This hack is only needed in a data-only restore */
706 if (!ropt->dataOnly || !ropt->disable_triggers)
709 ahlog(AH, 1, "enabling triggers for %s\n", te->tag);
712 * Become superuser if possible, since they are the only ones who can
713 * disable constraint triggers. If -S was not given, assume the initial
714 * user identity is a superuser. (XXX would it be better to become the
717 _becomeUser(AH, ropt->superuser);
722 _selectOutputSchema(AH, te->namespace);
724 ahprintf(AH, "ALTER TABLE %s ENABLE TRIGGER ALL;\n\n",
729 * This is a routine that is part of the dumper interface, hence the 'Archive*' parameter.
734 WriteData(Archive *AHX, const void *data, size_t dLen)
736 ArchiveHandle *AH = (ArchiveHandle *) AHX;
739 die_horribly(AH, modulename, "internal error -- WriteData cannot be called outside the context of a DataDumper routine\n");
741 return (*AH->WriteDataPtr) (AH, data, dLen);
745 * Create a new TOC entry. The TOC was designed as a TOC, but is now the
746 * repository for all metadata. But the name has stuck.
751 ArchiveEntry(Archive *AHX,
752 CatalogId catalogId, DumpId dumpId,
754 const char *namespace,
755 const char *tablespace,
756 const char *owner, bool withOids,
757 const char *desc, teSection section,
759 const char *dropStmt, const char *copyStmt,
760 const DumpId *deps, int nDeps,
761 DataDumperPtr dumpFn, void *dumpArg)
763 ArchiveHandle *AH = (ArchiveHandle *) AHX;
766 newToc = (TocEntry *) pg_calloc(1, sizeof(TocEntry));
769 if (dumpId > AH->maxDumpId)
770 AH->maxDumpId = dumpId;
772 newToc->prev = AH->toc->prev;
773 newToc->next = AH->toc;
774 AH->toc->prev->next = newToc;
775 AH->toc->prev = newToc;
777 newToc->catalogId = catalogId;
778 newToc->dumpId = dumpId;
779 newToc->section = section;
781 newToc->tag = pg_strdup(tag);
782 newToc->namespace = namespace ? pg_strdup(namespace) : NULL;
783 newToc->tablespace = tablespace ? pg_strdup(tablespace) : NULL;
784 newToc->owner = pg_strdup(owner);
785 newToc->withOids = withOids;
786 newToc->desc = pg_strdup(desc);
787 newToc->defn = pg_strdup(defn);
788 newToc->dropStmt = pg_strdup(dropStmt);
789 newToc->copyStmt = copyStmt ? pg_strdup(copyStmt) : NULL;
793 newToc->dependencies = (DumpId *) pg_malloc(nDeps * sizeof(DumpId));
794 memcpy(newToc->dependencies, deps, nDeps * sizeof(DumpId));
795 newToc->nDeps = nDeps;
799 newToc->dependencies = NULL;
803 newToc->dataDumper = dumpFn;
804 newToc->dataDumperArg = dumpArg;
805 newToc->hadDumper = dumpFn ? true : false;
807 newToc->formatData = NULL;
809 if (AH->ArchiveEntryPtr !=NULL)
810 (*AH->ArchiveEntryPtr) (AH, newToc);
815 PrintTOCSummary(Archive *AHX, RestoreOptions *ropt)
817 ArchiveHandle *AH = (ArchiveHandle *) AHX;
822 sav = SaveOutput(AH);
824 SetOutput(AH, ropt->filename, 0 /* no compression */ );
826 ahprintf(AH, ";\n; Archive created at %s", ctime(&AH->createDate));
827 ahprintf(AH, "; dbname: %s\n; TOC Entries: %d\n; Compression: %d\n",
828 AH->archdbname, AH->tocCount, AH->compression);
845 ahprintf(AH, "; Dump Version: %d.%d-%d\n", AH->vmaj, AH->vmin, AH->vrev);
846 ahprintf(AH, "; Format: %s\n", fmtName);
847 ahprintf(AH, "; Integer: %d bytes\n", (int) AH->intSize);
848 ahprintf(AH, "; Offset: %d bytes\n", (int) AH->offSize);
849 if (AH->archiveRemoteVersion)
850 ahprintf(AH, "; Dumped from database version: %s\n",
851 AH->archiveRemoteVersion);
852 if (AH->archiveDumpVersion)
853 ahprintf(AH, "; Dumped by pg_dump version: %s\n",
854 AH->archiveDumpVersion);
856 ahprintf(AH, ";\n;\n; Selected TOC Entries:\n;\n");
858 /* We should print DATABASE entries whether or not -C was specified */
861 for (te = AH->toc->next; te != AH->toc; te = te->next)
863 if (ropt->verbose || _tocEntryRequired(te, ropt, true) != 0)
864 ahprintf(AH, "%d; %u %u %s %s %s %s\n", te->dumpId,
865 te->catalogId.tableoid, te->catalogId.oid,
866 te->desc, te->namespace ? te->namespace : "-",
868 if (ropt->verbose && te->nDeps > 0)
872 ahprintf(AH, ";\tdepends on:");
873 for (i = 0; i < te->nDeps; i++)
874 ahprintf(AH, " %d", te->dependencies[i]);
880 RestoreOutput(AH, sav);
887 /* Called by a dumper to signal start of a BLOB */
889 StartBlob(Archive *AHX, Oid oid)
891 ArchiveHandle *AH = (ArchiveHandle *) AHX;
893 if (!AH->StartBlobPtr)
894 die_horribly(AH, modulename, "large-object output not supported in chosen format\n");
896 (*AH->StartBlobPtr) (AH, AH->currToc, oid);
901 /* Called by a dumper to signal end of a BLOB */
903 EndBlob(Archive *AHX, Oid oid)
905 ArchiveHandle *AH = (ArchiveHandle *) AHX;
908 (*AH->EndBlobPtr) (AH, AH->currToc, oid);
918 * Called by a format handler before any blobs are restored
921 StartRestoreBlobs(ArchiveHandle *AH)
923 if (!AH->ropt->single_txn)
926 StartTransaction(AH);
928 ahprintf(AH, "BEGIN;\n\n");
935 * Called by a format handler after all blobs are restored
938 EndRestoreBlobs(ArchiveHandle *AH)
940 if (!AH->ropt->single_txn)
943 CommitTransaction(AH);
945 ahprintf(AH, "COMMIT;\n\n");
948 ahlog(AH, 1, ngettext("restored %d large object\n",
949 "restored %d large objects\n",
956 * Called by a format handler to initiate restoration of a blob
959 StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop)
961 bool old_blob_style = (AH->version < K_VERS_1_12);
966 /* Initialize the LO Buffer */
969 ahlog(AH, 2, "restoring large object with OID %u\n", oid);
971 /* With an old archive we must do drop and create logic here */
972 if (old_blob_style && drop)
973 DropBlobIfExists(AH, oid);
979 loOid = lo_create(AH->connection, oid);
980 if (loOid == 0 || loOid != oid)
981 die_horribly(AH, modulename, "could not create large object %u: %s",
982 oid, PQerrorMessage(AH->connection));
984 AH->loFd = lo_open(AH->connection, oid, INV_WRITE);
986 die_horribly(AH, modulename, "could not open large object %u: %s",
987 oid, PQerrorMessage(AH->connection));
992 ahprintf(AH, "SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n",
995 ahprintf(AH, "SELECT pg_catalog.lo_open('%u', %d);\n",
1003 EndRestoreBlob(ArchiveHandle *AH, Oid oid)
1005 if (AH->lo_buf_used > 0)
1007 /* Write remaining bytes from the LO buffer */
1011 AH->writingBlob = 0;
1015 lo_close(AH->connection, AH->loFd);
1020 ahprintf(AH, "SELECT pg_catalog.lo_close(0);\n\n");
1025 * Sorting and Reordering
1029 SortTocFromFile(Archive *AHX, RestoreOptions *ropt)
1031 ArchiveHandle *AH = (ArchiveHandle *) AHX;
1034 bool incomplete_line;
1036 /* Allocate space for the 'wanted' array, and init it */
1037 ropt->idWanted = (bool *) pg_malloc(sizeof(bool) * AH->maxDumpId);
1038 memset(ropt->idWanted, 0, sizeof(bool) * AH->maxDumpId);
1040 /* Setup the file */
1041 fh = fopen(ropt->tocFile, PG_BINARY_R);
1043 die_horribly(AH, modulename, "could not open TOC file \"%s\": %s\n",
1044 ropt->tocFile, strerror(errno));
1046 incomplete_line = false;
1047 while (fgets(buf, sizeof(buf), fh) != NULL)
1049 bool prev_incomplete_line = incomplete_line;
1057 * Some lines in the file might be longer than sizeof(buf). This is
1058 * no problem, since we only care about the leading numeric ID which
1059 * can be at most a few characters; but we have to skip continuation
1060 * bufferloads when processing a long line.
1062 buflen = strlen(buf);
1063 if (buflen > 0 && buf[buflen - 1] == '\n')
1064 incomplete_line = false;
1066 incomplete_line = true;
1067 if (prev_incomplete_line)
1070 /* Truncate line at comment, if any */
1071 cmnt = strchr(buf, ';');
1075 /* Ignore if all blank */
1076 if (strspn(buf, " \t\r\n") == strlen(buf))
1079 /* Get an ID, check it's valid and not already seen */
1080 id = strtol(buf, &endptr, 10);
1081 if (endptr == buf || id <= 0 || id > AH->maxDumpId ||
1082 ropt->idWanted[id - 1])
1084 write_msg(modulename, "WARNING: line ignored: %s\n", buf);
1088 /* Find TOC entry */
1089 te = getTocEntryByDumpId(AH, id);
1091 die_horribly(AH, modulename, "could not find entry for ID %d\n",
1094 /* Mark it wanted */
1095 ropt->idWanted[id - 1] = true;
1098 * Move each item to the end of the list as it is selected, so that
1099 * they are placed in the desired order. Any unwanted items will end
1100 * up at the front of the list, which may seem unintuitive but it's
1101 * what we need. In an ordinary serial restore that makes no
1102 * difference, but in a parallel restore we need to mark unrestored
1103 * items' dependencies as satisfied before we start examining
1104 * restorable items. Otherwise they could have surprising
1105 * side-effects on the order in which restorable items actually get
1108 _moveBefore(AH, AH->toc, te);
1111 if (fclose(fh) != 0)
1112 die_horribly(AH, modulename, "could not close TOC file: %s\n",
1117 * Set up a dummy ID filter that selects all dump IDs
1120 InitDummyWantedList(Archive *AHX, RestoreOptions *ropt)
1122 ArchiveHandle *AH = (ArchiveHandle *) AHX;
1124 /* Allocate space for the 'wanted' array, and init it to 1's */
1125 ropt->idWanted = (bool *) pg_malloc(sizeof(bool) * AH->maxDumpId);
1126 memset(ropt->idWanted, 1, sizeof(bool) * AH->maxDumpId);
1129 /**********************
1130 * 'Convenience functions that look like standard IO functions
1131 * for writing data when in dump mode.
1132 **********************/
1136 archputs(const char *s, Archive *AH)
1138 return WriteData(AH, s, strlen(s));
1143 archprintf(Archive *AH, const char *fmt,...)
1147 int bSize = strlen(fmt) + 256;
1151 * This is paranoid: deal with the possibility that vsnprintf is willing
1152 * to ignore trailing null or returns > 0 even if string does not fit. It
1153 * may be the case that it returns cnt = bufsize
1155 while (cnt < 0 || cnt >= (bSize - 1))
1160 p = (char *) pg_malloc(bSize);
1162 cnt = vsnprintf(p, bSize, fmt, ap);
1165 WriteData(AH, p, cnt);
1171 /*******************************
1172 * Stuff below here should be 'private' to the archiver routines
1173 *******************************/
1176 SetOutput(ArchiveHandle *AH, const char *filename, int compression)
1183 fn = fileno(AH->FH);
1187 filename = AH->fSpec;
1190 fn = fileno(stdout);
1192 /* If compression explicitly requested, use gzopen */
1194 if (compression != 0)
1198 /* Don't use PG_BINARY_x since this is zlib */
1199 sprintf(fmode, "wb%d", compression);
1201 AH->OF = gzdopen(dup(fn), fmode);
1203 AH->OF = gzopen(filename, fmode);
1209 if (AH->mode == archModeAppend)
1212 AH->OF = fdopen(dup(fn), PG_BINARY_A);
1214 AH->OF = fopen(filename, PG_BINARY_A);
1219 AH->OF = fdopen(dup(fn), PG_BINARY_W);
1221 AH->OF = fopen(filename, PG_BINARY_W);
1229 die_horribly(AH, modulename, "could not open output file \"%s\": %s\n",
1230 filename, strerror(errno));
1232 die_horribly(AH, modulename, "could not open output file: %s\n",
1237 static OutputContext
1238 SaveOutput(ArchiveHandle *AH)
1243 sav.gzOut = AH->gzOut;
1249 RestoreOutput(ArchiveHandle *AH, OutputContext savedContext)
1254 res = GZCLOSE(AH->OF);
1256 res = fclose(AH->OF);
1259 die_horribly(AH, modulename, "could not close output file: %s\n",
1262 AH->gzOut = savedContext.gzOut;
1263 AH->OF = savedContext.OF;
1269 * Print formatted text to the output file (usually stdout).
1272 ahprintf(ArchiveHandle *AH, const char *fmt,...)
1276 int bSize = strlen(fmt) + 256; /* Usually enough */
1280 * This is paranoid: deal with the possibility that vsnprintf is willing
1281 * to ignore trailing null or returns > 0 even if string does not fit.
1282 * It may be the case that it returns cnt = bufsize.
1284 while (cnt < 0 || cnt >= (bSize - 1))
1289 p = (char *) pg_malloc(bSize);
1291 cnt = vsnprintf(p, bSize, fmt, ap);
1294 ahwrite(p, 1, cnt, AH);
1300 ahlog(ArchiveHandle *AH, int level, const char *fmt,...)
1304 if (AH->debugLevel < level && (!AH->public.verbose || level > 1))
1308 vwrite_msg(NULL, fmt, ap);
1313 * Single place for logic which says 'We are restoring to a direct DB connection'.
1316 RestoringToDB(ArchiveHandle *AH)
1318 return (AH->ropt && AH->ropt->useDB && AH->connection);
1322 * Dump the current contents of the LO data buffer while writing a BLOB
1325 dump_lo_buf(ArchiveHandle *AH)
1331 res = lo_write(AH->connection, AH->loFd, AH->lo_buf, AH->lo_buf_used);
1332 ahlog(AH, 5, ngettext("wrote %lu byte of large object data (result = %lu)\n",
1333 "wrote %lu bytes of large object data (result = %lu)\n",
1335 (unsigned long) AH->lo_buf_used, (unsigned long) res);
1336 if (res != AH->lo_buf_used)
1337 die_horribly(AH, modulename,
1338 "could not write to large object (result: %lu, expected: %lu)\n",
1339 (unsigned long) res, (unsigned long) AH->lo_buf_used);
1343 PQExpBuffer buf = createPQExpBuffer();
1345 appendByteaLiteralAHX(buf,
1346 (const unsigned char *) AH->lo_buf,
1350 /* Hack: turn off writingBlob so ahwrite doesn't recurse to here */
1351 AH->writingBlob = 0;
1352 ahprintf(AH, "SELECT pg_catalog.lowrite(0, %s);\n", buf->data);
1353 AH->writingBlob = 1;
1355 destroyPQExpBuffer(buf);
1357 AH->lo_buf_used = 0;
1362 * Write buffer to the output file (usually stdout). This is used for
1363 * outputting 'restore' scripts etc. It is even possible for an archive
1364 * format to create a custom output routine to 'fake' a restore if it
1365 * wants to generate a script (see TAR output).
1368 ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
1372 if (AH->writingBlob)
1374 size_t remaining = size * nmemb;
1376 while (AH->lo_buf_used + remaining > AH->lo_buf_size)
1378 size_t avail = AH->lo_buf_size - AH->lo_buf_used;
1380 memcpy((char *) AH->lo_buf + AH->lo_buf_used, ptr, avail);
1381 ptr = (const void *) ((const char *) ptr + avail);
1383 AH->lo_buf_used += avail;
1387 memcpy((char *) AH->lo_buf + AH->lo_buf_used, ptr, remaining);
1388 AH->lo_buf_used += remaining;
1390 return size * nmemb;
1394 res = GZWRITE(ptr, size, nmemb, AH->OF);
1395 if (res != (nmemb * size))
1396 die_horribly(AH, modulename, "could not write to output file: %s\n", strerror(errno));
1399 else if (AH->CustomOutPtr)
1401 res = AH->CustomOutPtr (AH, ptr, size * nmemb);
1403 if (res != (nmemb * size))
1404 die_horribly(AH, modulename, "could not write to custom output routine\n");
1410 * If we're doing a restore, and it's direct to DB, and we're
1411 * connected then send it to the DB.
1413 if (RestoringToDB(AH))
1414 return ExecuteSqlCommandBuf(AH, (const char *) ptr, size * nmemb);
1417 res = fwrite(ptr, size, nmemb, AH->OF);
1419 die_horribly(AH, modulename, "could not write to output file: %s\n",
1427 /* Report a fatal error and exit(1) */
1429 vdie_horribly(ArchiveHandle *AH, const char *modulename,
1430 const char *fmt, va_list ap)
1432 vwrite_msg(modulename, fmt, ap);
1436 if (AH->public.verbose)
1437 write_msg(NULL, "*** aborted because of error\n");
1439 PQfinish(AH->connection);
1445 /* As above, but with variable arg list */
1447 die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...)
1452 vdie_horribly(AH, modulename, fmt, ap);
1456 /* As above, but with a complaint about a particular query. */
1458 die_on_query_failure(ArchiveHandle *AH, const char *modulename,
1461 write_msg(modulename, "query failed: %s",
1462 PQerrorMessage(AH->connection));
1463 die_horribly(AH, modulename, "query was: %s\n", query);
1466 /* on some error, we may decide to go on... */
1468 warn_or_die_horribly(ArchiveHandle *AH,
1469 const char *modulename, const char *fmt,...)
1477 /* Do nothing special */
1480 case STAGE_INITIALIZING:
1481 if (AH->stage != AH->lastErrorStage)
1482 write_msg(modulename, "Error while INITIALIZING:\n");
1485 case STAGE_PROCESSING:
1486 if (AH->stage != AH->lastErrorStage)
1487 write_msg(modulename, "Error while PROCESSING TOC:\n");
1490 case STAGE_FINALIZING:
1491 if (AH->stage != AH->lastErrorStage)
1492 write_msg(modulename, "Error while FINALIZING:\n");
1495 if (AH->currentTE != NULL && AH->currentTE != AH->lastErrorTE)
1497 write_msg(modulename, "Error from TOC entry %d; %u %u %s %s %s\n",
1498 AH->currentTE->dumpId,
1499 AH->currentTE->catalogId.tableoid, AH->currentTE->catalogId.oid,
1500 AH->currentTE->desc, AH->currentTE->tag, AH->currentTE->owner);
1502 AH->lastErrorStage = AH->stage;
1503 AH->lastErrorTE = AH->currentTE;
1506 if (AH->public.exit_on_error)
1507 vdie_horribly(AH, modulename, fmt, ap);
1510 vwrite_msg(modulename, fmt, ap);
1511 AH->public.n_errors++;
1519 _moveAfter(ArchiveHandle *AH, TocEntry *pos, TocEntry *te)
1521 /* Unlink te from list */
1522 te->prev->next = te->next;
1523 te->next->prev = te->prev;
1525 /* and insert it after "pos" */
1527 te->next = pos->next;
1528 pos->next->prev = te;
1534 _moveBefore(ArchiveHandle *AH, TocEntry *pos, TocEntry *te)
1536 /* Unlink te from list */
1537 te->prev->next = te->next;
1538 te->next->prev = te->prev;
1540 /* and insert it before "pos" */
1541 te->prev = pos->prev;
1543 pos->prev->next = te;
1548 getTocEntryByDumpId(ArchiveHandle *AH, DumpId id)
1552 for (te = AH->toc->next; te != AH->toc; te = te->next)
1554 if (te->dumpId == id)
1561 TocIDRequired(ArchiveHandle *AH, DumpId id, RestoreOptions *ropt)
1563 TocEntry *te = getTocEntryByDumpId(AH, id);
1568 return _tocEntryRequired(te, ropt, true);
1572 WriteOffset(ArchiveHandle *AH, pgoff_t o, int wasSet)
1577 (*AH->WriteBytePtr) (AH, wasSet);
1579 /* Write out pgoff_t smallest byte first, prevents endian mismatch */
1580 for (off = 0; off < sizeof(pgoff_t); off++)
1582 (*AH->WriteBytePtr) (AH, o & 0xFF);
1585 return sizeof(pgoff_t) + 1;
1589 ReadOffset(ArchiveHandle *AH, pgoff_t * o)
1595 /* Initialize to zero */
1598 /* Check for old version */
1599 if (AH->version < K_VERS_1_7)
1601 /* Prior versions wrote offsets using WriteInt */
1603 /* -1 means not set */
1605 return K_OFFSET_POS_NOT_SET;
1607 return K_OFFSET_NO_DATA;
1609 /* Cast to pgoff_t because it was written as an int. */
1611 return K_OFFSET_POS_SET;
1615 * Read the flag indicating the state of the data pointer. Check if valid
1618 * This used to be handled by a negative or zero pointer, now we use an
1619 * extra byte specifically for the state.
1621 offsetFlg = (*AH->ReadBytePtr) (AH) & 0xFF;
1625 case K_OFFSET_POS_NOT_SET:
1626 case K_OFFSET_NO_DATA:
1627 case K_OFFSET_POS_SET:
1632 die_horribly(AH, modulename, "unexpected data offset flag %d\n", offsetFlg);
1638 for (off = 0; off < AH->offSize; off++)
1640 if (off < sizeof(pgoff_t))
1641 *o |= ((pgoff_t) ((*AH->ReadBytePtr) (AH))) << (off * 8);
1644 if ((*AH->ReadBytePtr) (AH) != 0)
1645 die_horribly(AH, modulename, "file offset in dump file is too large\n");
1653 WriteInt(ArchiveHandle *AH, int i)
1658 * This is a bit yucky, but I don't want to make the binary format very
1659 * dependent on representation, and not knowing much about it, I write out
1660 * a sign byte. If you change this, don't forget to change the file
1661 * version #, and modify readInt to read the new format AS WELL AS the old
1668 (*AH->WriteBytePtr) (AH, 1);
1672 (*AH->WriteBytePtr) (AH, 0);
1674 for (b = 0; b < AH->intSize; b++)
1676 (*AH->WriteBytePtr) (AH, i & 0xFF);
1680 return AH->intSize + 1;
1684 ReadInt(ArchiveHandle *AH)
1689 int sign = 0; /* Default positive */
1692 if (AH->version > K_VERS_1_0)
1693 /* Read a sign byte */
1694 sign = (*AH->ReadBytePtr) (AH);
1696 for (b = 0; b < AH->intSize; b++)
1698 bv = (*AH->ReadBytePtr) (AH) & 0xFF;
1700 res = res + (bv << bitShift);
1711 WriteStr(ArchiveHandle *AH, const char *c)
1717 res = WriteInt(AH, strlen(c));
1718 res += (*AH->WriteBufPtr) (AH, c, strlen(c));
1721 res = WriteInt(AH, -1);
1727 ReadStr(ArchiveHandle *AH)
1737 buf = (char *) pg_malloc(l + 1);
1738 if ((*AH->ReadBufPtr) (AH, (void *) buf, l) != l)
1739 die_horribly(AH, modulename, "unexpected end of file\n");
1748 _discoverArchiveFormat(ArchiveHandle *AH)
1751 char sig[6]; /* More than enough */
1756 write_msg(modulename, "attempting to ascertain archive format\n");
1760 free(AH->lookahead);
1762 AH->lookaheadSize = 512;
1763 AH->lookahead = pg_calloc(1, 512);
1764 AH->lookaheadLen = 0;
1765 AH->lookaheadPos = 0;
1774 * Check if the specified archive is a directory. If so, check if
1775 * there's a "toc.dat" (or "toc.dat.gz") file in it.
1777 if (stat(AH->fSpec, &st) == 0 && S_ISDIR(st.st_mode))
1779 char buf[MAXPGPATH];
1781 if (snprintf(buf, MAXPGPATH, "%s/toc.dat", AH->fSpec) >= MAXPGPATH)
1782 die_horribly(AH, modulename, "directory name too long: \"%s\"\n",
1784 if (stat(buf, &st) == 0 && S_ISREG(st.st_mode))
1786 AH->format = archDirectory;
1791 if (snprintf(buf, MAXPGPATH, "%s/toc.dat.gz", AH->fSpec) >= MAXPGPATH)
1792 die_horribly(AH, modulename, "directory name too long: \"%s\"\n",
1794 if (stat(buf, &st) == 0 && S_ISREG(st.st_mode))
1796 AH->format = archDirectory;
1800 die_horribly(AH, modulename, "directory \"%s\" does not appear to be a valid archive (\"toc.dat\" does not exist)\n",
1802 fh = NULL; /* keep compiler quiet */
1806 fh = fopen(AH->fSpec, PG_BINARY_R);
1808 die_horribly(AH, modulename, "could not open input file \"%s\": %s\n",
1809 AH->fSpec, strerror(errno));
1816 die_horribly(AH, modulename, "could not open input file: %s\n",
1820 cnt = fread(sig, 1, 5, fh);
1825 die_horribly(AH, modulename, "could not read input file: %s\n", strerror(errno));
1827 die_horribly(AH, modulename, "input file is too short (read %lu, expected 5)\n",
1828 (unsigned long) cnt);
1831 /* Save it, just in case we need it later */
1832 strncpy(&AH->lookahead[0], sig, 5);
1833 AH->lookaheadLen = 5;
1835 if (strncmp(sig, "PGDMP", 5) == 0)
1838 * Finish reading (most of) a custom-format header.
1840 * NB: this code must agree with ReadHead().
1842 AH->vmaj = fgetc(fh);
1843 AH->vmin = fgetc(fh);
1845 /* Save these too... */
1846 AH->lookahead[AH->lookaheadLen++] = AH->vmaj;
1847 AH->lookahead[AH->lookaheadLen++] = AH->vmin;
1849 /* Check header version; varies from V1.0 */
1850 if (AH->vmaj > 1 || ((AH->vmaj == 1) && (AH->vmin > 0))) /* Version > 1.0 */
1852 AH->vrev = fgetc(fh);
1853 AH->lookahead[AH->lookaheadLen++] = AH->vrev;
1858 /* Make a convenient integer <maj><min><rev>00 */
1859 AH->version = ((AH->vmaj * 256 + AH->vmin) * 256 + AH->vrev) * 256 + 0;
1861 AH->intSize = fgetc(fh);
1862 AH->lookahead[AH->lookaheadLen++] = AH->intSize;
1864 if (AH->version >= K_VERS_1_7)
1866 AH->offSize = fgetc(fh);
1867 AH->lookahead[AH->lookaheadLen++] = AH->offSize;
1870 AH->offSize = AH->intSize;
1872 AH->format = fgetc(fh);
1873 AH->lookahead[AH->lookaheadLen++] = AH->format;
1878 * *Maybe* we have a tar archive format file or a text dump ...
1879 * So, read first 512 byte header...
1881 cnt = fread(&AH->lookahead[AH->lookaheadLen], 1, 512 - AH->lookaheadLen, fh);
1882 AH->lookaheadLen += cnt;
1884 if (AH->lookaheadLen >= strlen(TEXT_DUMPALL_HEADER) &&
1885 (strncmp(AH->lookahead, TEXT_DUMP_HEADER, strlen(TEXT_DUMP_HEADER)) == 0 ||
1886 strncmp(AH->lookahead, TEXT_DUMPALL_HEADER, strlen(TEXT_DUMPALL_HEADER)) == 0))
1888 /* looks like it's probably a text format dump. so suggest they try psql */
1889 die_horribly(AH, modulename, "input file appears to be a text format dump. Please use psql.\n");
1892 if (AH->lookaheadLen != 512)
1893 die_horribly(AH, modulename, "input file does not appear to be a valid archive (too short?)\n");
1895 if (!isValidTarHeader(AH->lookahead))
1896 die_horribly(AH, modulename, "input file does not appear to be a valid archive\n");
1898 AH->format = archTar;
1901 /* If we can't seek, then mark the header as read */
1902 if (fseeko(fh, 0, SEEK_SET) != 0)
1905 * NOTE: Formats that use the lookahead buffer can unset this in their
1911 AH->lookaheadLen = 0; /* Don't bother since we've reset the file */
1913 /* Close the file */
1915 if (fclose(fh) != 0)
1916 die_horribly(AH, modulename, "could not close input file: %s\n",
1924 * Allocate an archive handle
1926 static ArchiveHandle *
1927 _allocAH(const char *FileSpec, const ArchiveFormat fmt,
1928 const int compression, ArchiveMode mode)
1933 write_msg(modulename, "allocating AH for %s, format %d\n", FileSpec, fmt);
1936 AH = (ArchiveHandle *) pg_calloc(1, sizeof(ArchiveHandle));
1938 /* AH->debugLevel = 100; */
1940 AH->vmaj = K_VERS_MAJOR;
1941 AH->vmin = K_VERS_MINOR;
1942 AH->vrev = K_VERS_REV;
1944 /* Make a convenient integer <maj><min><rev>00 */
1945 AH->version = ((AH->vmaj * 256 + AH->vmin) * 256 + AH->vrev) * 256 + 0;
1947 /* initialize for backwards compatible string processing */
1948 AH->public.encoding = 0; /* PG_SQL_ASCII */
1949 AH->public.std_strings = false;
1951 /* sql error handling */
1952 AH->public.exit_on_error = true;
1953 AH->public.n_errors = 0;
1955 AH->archiveDumpVersion = PG_VERSION;
1957 AH->createDate = time(NULL);
1959 AH->intSize = sizeof(int);
1960 AH->offSize = sizeof(pgoff_t);
1963 AH->fSpec = pg_strdup(FileSpec);
1966 * Not used; maybe later....
1968 * AH->workDir = pg_strdup(FileSpec); for(i=strlen(FileSpec) ; i > 0 ;
1969 * i--) if (AH->workDir[i-1] == '/')
1975 AH->currUser = NULL; /* unknown */
1976 AH->currSchema = NULL; /* ditto */
1977 AH->currTablespace = NULL; /* ditto */
1978 AH->currWithOids = -1; /* force SET */
1980 AH->toc = (TocEntry *) pg_calloc(1, sizeof(TocEntry));
1982 AH->toc->next = AH->toc;
1983 AH->toc->prev = AH->toc;
1986 AH->compression = compression;
1988 memset(&(AH->sqlparse), 0, sizeof(AH->sqlparse));
1990 /* Open stdout with no compression for AH output handle */
1995 * On Windows, we need to use binary mode to read/write non-text archive
1996 * formats. Force stdin/stdout into binary mode if that is what we are
2000 if (fmt != archNull &&
2001 (AH->fSpec == NULL || strcmp(AH->fSpec, "") == 0))
2003 if (mode == archModeWrite)
2004 setmode(fileno(stdout), O_BINARY);
2006 setmode(fileno(stdin), O_BINARY);
2010 if (fmt == archUnknown)
2011 AH->format = _discoverArchiveFormat(AH);
2015 AH->promptPassword = TRI_DEFAULT;
2020 InitArchiveFmt_Custom(AH);
2024 InitArchiveFmt_Files(AH);
2028 InitArchiveFmt_Null(AH);
2032 InitArchiveFmt_Directory(AH);
2036 InitArchiveFmt_Tar(AH);
2040 die_horribly(AH, modulename, "unrecognized file format \"%d\"\n", fmt);
2048 WriteDataChunks(ArchiveHandle *AH)
2051 StartDataPtr startPtr;
2054 for (te = AH->toc->next; te != AH->toc; te = te->next)
2056 if (te->dataDumper != NULL)
2059 /* printf("Writing data for %d (%x)\n", te->id, te); */
2061 if (strcmp(te->desc, "BLOBS") == 0)
2063 startPtr = AH->StartBlobsPtr;
2064 endPtr = AH->EndBlobsPtr;
2068 startPtr = AH->StartDataPtr;
2069 endPtr = AH->EndDataPtr;
2072 if (startPtr != NULL)
2073 (*startPtr) (AH, te);
2076 * printf("Dumper arg for %d is %x\n", te->id, te->dataDumperArg);
2080 * The user-provided DataDumper routine needs to call
2083 (*te->dataDumper) ((Archive *) AH, te->dataDumperArg);
2093 WriteToc(ArchiveHandle *AH)
2099 /* printf("%d TOC Entries to save\n", AH->tocCount); */
2101 WriteInt(AH, AH->tocCount);
2103 for (te = AH->toc->next; te != AH->toc; te = te->next)
2105 WriteInt(AH, te->dumpId);
2106 WriteInt(AH, te->dataDumper ? 1 : 0);
2108 /* OID is recorded as a string for historical reasons */
2109 sprintf(workbuf, "%u", te->catalogId.tableoid);
2110 WriteStr(AH, workbuf);
2111 sprintf(workbuf, "%u", te->catalogId.oid);
2112 WriteStr(AH, workbuf);
2114 WriteStr(AH, te->tag);
2115 WriteStr(AH, te->desc);
2116 WriteInt(AH, te->section);
2117 WriteStr(AH, te->defn);
2118 WriteStr(AH, te->dropStmt);
2119 WriteStr(AH, te->copyStmt);
2120 WriteStr(AH, te->namespace);
2121 WriteStr(AH, te->tablespace);
2122 WriteStr(AH, te->owner);
2123 WriteStr(AH, te->withOids ? "true" : "false");
2125 /* Dump list of dependencies */
2126 for (i = 0; i < te->nDeps; i++)
2128 sprintf(workbuf, "%d", te->dependencies[i]);
2129 WriteStr(AH, workbuf);
2131 WriteStr(AH, NULL); /* Terminate List */
2133 if (AH->WriteExtraTocPtr)
2134 (*AH->WriteExtraTocPtr) (AH, te);
2139 ReadToc(ArchiveHandle *AH)
2147 bool in_post_data = false;
2149 AH->tocCount = ReadInt(AH);
2152 for (i = 0; i < AH->tocCount; i++)
2154 te = (TocEntry *) pg_calloc(1, sizeof(TocEntry));
2155 te->dumpId = ReadInt(AH);
2157 if (te->dumpId > AH->maxDumpId)
2158 AH->maxDumpId = te->dumpId;
2161 if (te->dumpId <= 0)
2162 die_horribly(AH, modulename,
2163 "entry ID %d out of range -- perhaps a corrupt TOC\n",
2166 te->hadDumper = ReadInt(AH);
2168 if (AH->version >= K_VERS_1_8)
2171 sscanf(tmp, "%u", &te->catalogId.tableoid);
2175 te->catalogId.tableoid = InvalidOid;
2177 sscanf(tmp, "%u", &te->catalogId.oid);
2180 te->tag = ReadStr(AH);
2181 te->desc = ReadStr(AH);
2183 if (AH->version >= K_VERS_1_11)
2185 te->section = ReadInt(AH);
2190 * Rules for pre-8.4 archives wherein pg_dump hasn't classified
2191 * the entries into sections. This list need not cover entry
2192 * types added later than 8.4.
2194 if (strcmp(te->desc, "COMMENT") == 0 ||
2195 strcmp(te->desc, "ACL") == 0 ||
2196 strcmp(te->desc, "ACL LANGUAGE") == 0)
2197 te->section = SECTION_NONE;
2198 else if (strcmp(te->desc, "TABLE DATA") == 0 ||
2199 strcmp(te->desc, "BLOBS") == 0 ||
2200 strcmp(te->desc, "BLOB COMMENTS") == 0)
2201 te->section = SECTION_DATA;
2202 else if (strcmp(te->desc, "CONSTRAINT") == 0 ||
2203 strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
2204 strcmp(te->desc, "FK CONSTRAINT") == 0 ||
2205 strcmp(te->desc, "INDEX") == 0 ||
2206 strcmp(te->desc, "RULE") == 0 ||
2207 strcmp(te->desc, "TRIGGER") == 0)
2208 te->section = SECTION_POST_DATA;
2210 te->section = SECTION_PRE_DATA;
2213 /* will stay true even for SECTION_NONE items */
2214 if (te->section == SECTION_POST_DATA)
2215 in_post_data = true;
2217 te->inPostData = in_post_data;
2219 te->defn = ReadStr(AH);
2220 te->dropStmt = ReadStr(AH);
2222 if (AH->version >= K_VERS_1_3)
2223 te->copyStmt = ReadStr(AH);
2225 if (AH->version >= K_VERS_1_6)
2226 te->namespace = ReadStr(AH);
2228 if (AH->version >= K_VERS_1_10)
2229 te->tablespace = ReadStr(AH);
2231 te->owner = ReadStr(AH);
2232 if (AH->version >= K_VERS_1_9)
2234 if (strcmp(ReadStr(AH), "true") == 0)
2235 te->withOids = true;
2237 te->withOids = false;
2240 te->withOids = true;
2242 /* Read TOC entry dependencies */
2243 if (AH->version >= K_VERS_1_5)
2246 deps = (DumpId *) pg_malloc(sizeof(DumpId) * depSize);
2252 break; /* end of list */
2253 if (depIdx >= depSize)
2256 deps = (DumpId *) pg_realloc(deps, sizeof(DumpId) * depSize);
2258 sscanf(tmp, "%d", &deps[depIdx]);
2263 if (depIdx > 0) /* We have a non-null entry */
2265 deps = (DumpId *) pg_realloc(deps, sizeof(DumpId) * depIdx);
2266 te->dependencies = deps;
2272 te->dependencies = NULL;
2278 te->dependencies = NULL;
2282 if (AH->ReadExtraTocPtr)
2283 (*AH->ReadExtraTocPtr) (AH, te);
2285 ahlog(AH, 3, "read TOC entry %d (ID %d) for %s %s\n",
2286 i, te->dumpId, te->desc, te->tag);
2288 /* link completed entry into TOC circular list */
2289 te->prev = AH->toc->prev;
2290 AH->toc->prev->next = te;
2294 /* special processing immediately upon read for some items */
2295 if (strcmp(te->desc, "ENCODING") == 0)
2296 processEncodingEntry(AH, te);
2297 else if (strcmp(te->desc, "STDSTRINGS") == 0)
2298 processStdStringsEntry(AH, te);
2303 processEncodingEntry(ArchiveHandle *AH, TocEntry *te)
2305 /* te->defn should have the form SET client_encoding = 'foo'; */
2306 char *defn = pg_strdup(te->defn);
2311 ptr1 = strchr(defn, '\'');
2313 ptr2 = strchr(++ptr1, '\'');
2317 encoding = pg_char_to_encoding(ptr1);
2319 die_horribly(AH, modulename, "unrecognized encoding \"%s\"\n",
2321 AH->public.encoding = encoding;
2324 die_horribly(AH, modulename, "invalid ENCODING item: %s\n",
2331 processStdStringsEntry(ArchiveHandle *AH, TocEntry *te)
2333 /* te->defn should have the form SET standard_conforming_strings = 'x'; */
2336 ptr1 = strchr(te->defn, '\'');
2337 if (ptr1 && strncmp(ptr1, "'on'", 4) == 0)
2338 AH->public.std_strings = true;
2339 else if (ptr1 && strncmp(ptr1, "'off'", 5) == 0)
2340 AH->public.std_strings = false;
2342 die_horribly(AH, modulename, "invalid STDSTRINGS item: %s\n",
2347 _tocEntryRequired(TocEntry *te, RestoreOptions *ropt, bool include_acls)
2349 teReqs res = REQ_ALL;
2351 /* ENCODING and STDSTRINGS items are dumped specially, so always reject */
2352 if (strcmp(te->desc, "ENCODING") == 0 ||
2353 strcmp(te->desc, "STDSTRINGS") == 0)
2356 /* If it's an ACL, maybe ignore it */
2357 if ((!include_acls || ropt->aclsSkip) && _tocEntryIsACL(te))
2360 /* If it's security labels, maybe ignore it */
2361 if (ropt->no_security_labels && strcmp(te->desc, "SECURITY LABEL") == 0)
2364 /* Ignore DATABASE entry unless we should create it */
2365 if (!ropt->createDB && strcmp(te->desc, "DATABASE") == 0)
2368 /* skip (all but) post data section as required */
2369 /* table data is filtered if necessary lower down */
2370 if (ropt->dumpSections != DUMP_UNSECTIONED)
2372 if (!(ropt->dumpSections & DUMP_POST_DATA) && te->inPostData)
2374 if (!(ropt->dumpSections & DUMP_PRE_DATA) && ! te->inPostData && strcmp(te->desc, "TABLE DATA") != 0)
2379 /* Check options for selective dump/restore */
2380 if (ropt->schemaNames)
2382 /* If no namespace is specified, it means all. */
2385 if (strcmp(ropt->schemaNames, te->namespace) != 0)
2391 if (strcmp(te->desc, "TABLE") == 0 ||
2392 strcmp(te->desc, "TABLE DATA") == 0)
2394 if (!ropt->selTable)
2396 if (ropt->tableNames && strcmp(ropt->tableNames, te->tag) != 0)
2399 else if (strcmp(te->desc, "INDEX") == 0)
2401 if (!ropt->selIndex)
2403 if (ropt->indexNames && strcmp(ropt->indexNames, te->tag) != 0)
2406 else if (strcmp(te->desc, "FUNCTION") == 0)
2408 if (!ropt->selFunction)
2410 if (ropt->functionNames && strcmp(ropt->functionNames, te->tag) != 0)
2413 else if (strcmp(te->desc, "TRIGGER") == 0)
2415 if (!ropt->selTrigger)
2417 if (ropt->triggerNames && strcmp(ropt->triggerNames, te->tag) != 0)
2425 * Check if we had a dataDumper. Indicates if the entry is schema or data
2430 * Special Case: If 'SEQUENCE SET' or anything to do with BLOBs, then
2431 * it is considered a data entry. We don't need to check for the
2432 * BLOBS entry or old-style BLOB COMMENTS, because they will have
2433 * hadDumper = true ... but we do need to check new-style BLOB
2436 if (strcmp(te->desc, "SEQUENCE SET") == 0 ||
2437 strcmp(te->desc, "BLOB") == 0 ||
2438 (strcmp(te->desc, "ACL") == 0 &&
2439 strncmp(te->tag, "LARGE OBJECT ", 13) == 0) ||
2440 (strcmp(te->desc, "COMMENT") == 0 &&
2441 strncmp(te->tag, "LARGE OBJECT ", 13) == 0) ||
2442 (strcmp(te->desc, "SECURITY LABEL") == 0 &&
2443 strncmp(te->tag, "LARGE OBJECT ", 13) == 0))
2444 res = res & REQ_DATA;
2446 res = res & ~REQ_DATA;
2450 * Special case: <Init> type with <Max OID> tag; this is obsolete and we
2453 if ((strcmp(te->desc, "<Init>") == 0) && (strcmp(te->tag, "Max OID") == 0))
2456 /* Mask it if we only want schema */
2457 if (ropt->schemaOnly)
2458 res = res & REQ_SCHEMA;
2460 /* Mask it we only want data */
2462 res = res & REQ_DATA;
2464 /* Mask it if we don't have a schema contribution */
2465 if (!te->defn || strlen(te->defn) == 0)
2466 res = res & ~REQ_SCHEMA;
2468 /* Finally, if there's a per-ID filter, limit based on that as well */
2469 if (ropt->idWanted && !ropt->idWanted[te->dumpId - 1])
2476 * Identify TOC entries that are ACLs.
2479 _tocEntryIsACL(TocEntry *te)
2481 /* "ACL LANGUAGE" was a crock emitted only in PG 7.4 */
2482 if (strcmp(te->desc, "ACL") == 0 ||
2483 strcmp(te->desc, "ACL LANGUAGE") == 0 ||
2484 strcmp(te->desc, "DEFAULT ACL") == 0)
2490 * Issue SET commands for parameters that we want to have set the same way
2491 * at all times during execution of a restore script.
2494 _doSetFixedOutputState(ArchiveHandle *AH)
2496 /* Disable statement_timeout in archive for pg_restore/psql */
2497 ahprintf(AH, "SET statement_timeout = 0;\n");
2499 /* Select the correct character set encoding */
2500 ahprintf(AH, "SET client_encoding = '%s';\n",
2501 pg_encoding_to_char(AH->public.encoding));
2503 /* Select the correct string literal syntax */
2504 ahprintf(AH, "SET standard_conforming_strings = %s;\n",
2505 AH->public.std_strings ? "on" : "off");
2507 /* Select the role to be used during restore */
2508 if (AH->ropt && AH->ropt->use_role)
2509 ahprintf(AH, "SET ROLE %s;\n", fmtId(AH->ropt->use_role));
2511 /* Make sure function checking is disabled */
2512 ahprintf(AH, "SET check_function_bodies = false;\n");
2514 /* Avoid annoying notices etc */
2515 ahprintf(AH, "SET client_min_messages = warning;\n");
2516 if (!AH->public.std_strings)
2517 ahprintf(AH, "SET escape_string_warning = off;\n");
2523 * Issue a SET SESSION AUTHORIZATION command. Caller is responsible
2524 * for updating state if appropriate. If user is NULL or an empty string,
2525 * the specification DEFAULT will be used.
2528 _doSetSessionAuth(ArchiveHandle *AH, const char *user)
2530 PQExpBuffer cmd = createPQExpBuffer();
2532 appendPQExpBuffer(cmd, "SET SESSION AUTHORIZATION ");
2535 * SQL requires a string literal here. Might as well be correct.
2538 appendStringLiteralAHX(cmd, user, AH);
2540 appendPQExpBuffer(cmd, "DEFAULT");
2541 appendPQExpBuffer(cmd, ";");
2543 if (RestoringToDB(AH))
2547 res = PQexec(AH->connection, cmd->data);
2549 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
2550 /* NOT warn_or_die_horribly... use -O instead to skip this. */
2551 die_horribly(AH, modulename, "could not set session user to \"%s\": %s",
2552 user, PQerrorMessage(AH->connection));
2557 ahprintf(AH, "%s\n\n", cmd->data);
2559 destroyPQExpBuffer(cmd);
2564 * Issue a SET default_with_oids command. Caller is responsible
2565 * for updating state if appropriate.
2568 _doSetWithOids(ArchiveHandle *AH, const bool withOids)
2570 PQExpBuffer cmd = createPQExpBuffer();
2572 appendPQExpBuffer(cmd, "SET default_with_oids = %s;", withOids ?
2575 if (RestoringToDB(AH))
2579 res = PQexec(AH->connection, cmd->data);
2581 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
2582 warn_or_die_horribly(AH, modulename,
2583 "could not set default_with_oids: %s",
2584 PQerrorMessage(AH->connection));
2589 ahprintf(AH, "%s\n\n", cmd->data);
2591 destroyPQExpBuffer(cmd);
2596 * Issue the commands to connect to the specified database.
2598 * If we're currently restoring right into a database, this will
2599 * actually establish a connection. Otherwise it puts a \connect into
2600 * the script output.
2602 * NULL dbname implies reconnecting to the current DB (pretty useless).
2605 _reconnectToDB(ArchiveHandle *AH, const char *dbname)
2607 if (RestoringToDB(AH))
2608 ReconnectToServer(AH, dbname, NULL);
2611 PQExpBuffer qry = createPQExpBuffer();
2613 appendPQExpBuffer(qry, "\\connect %s\n\n",
2614 dbname ? fmtId(dbname) : "-");
2615 ahprintf(AH, "%s", qry->data);
2616 destroyPQExpBuffer(qry);
2620 * NOTE: currUser keeps track of what the imaginary session user in our
2621 * script is. It's now effectively reset to the original userID.
2625 AH->currUser = NULL;
2627 /* don't assume we still know the output schema, tablespace, etc either */
2629 free(AH->currSchema);
2630 AH->currSchema = NULL;
2631 if (AH->currTablespace)
2632 free(AH->currTablespace);
2633 AH->currTablespace = NULL;
2634 AH->currWithOids = -1;
2636 /* re-establish fixed state */
2637 _doSetFixedOutputState(AH);
2641 * Become the specified user, and update state to avoid redundant commands
2643 * NULL or empty argument is taken to mean restoring the session default
2646 _becomeUser(ArchiveHandle *AH, const char *user)
2649 user = ""; /* avoid null pointers */
2651 if (AH->currUser && strcmp(AH->currUser, user) == 0)
2652 return; /* no need to do anything */
2654 _doSetSessionAuth(AH, user);
2657 * NOTE: currUser keeps track of what the imaginary session user in our
2662 AH->currUser = pg_strdup(user);
2666 * Become the owner of the given TOC entry object. If
2667 * changes in ownership are not allowed, this doesn't do anything.
2670 _becomeOwner(ArchiveHandle *AH, TocEntry *te)
2672 if (AH->ropt && (AH->ropt->noOwner || !AH->ropt->use_setsessauth))
2675 _becomeUser(AH, te->owner);
2680 * Set the proper default_with_oids value for the table.
2683 _setWithOids(ArchiveHandle *AH, TocEntry *te)
2685 if (AH->currWithOids != te->withOids)
2687 _doSetWithOids(AH, te->withOids);
2688 AH->currWithOids = te->withOids;
2694 * Issue the commands to select the specified schema as the current schema
2695 * in the target database.
2698 _selectOutputSchema(ArchiveHandle *AH, const char *schemaName)
2702 if (!schemaName || *schemaName == '\0' ||
2703 (AH->currSchema && strcmp(AH->currSchema, schemaName) == 0))
2704 return; /* no need to do anything */
2706 qry = createPQExpBuffer();
2708 appendPQExpBuffer(qry, "SET search_path = %s",
2710 if (strcmp(schemaName, "pg_catalog") != 0)
2711 appendPQExpBuffer(qry, ", pg_catalog");
2713 if (RestoringToDB(AH))
2717 res = PQexec(AH->connection, qry->data);
2719 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
2720 warn_or_die_horribly(AH, modulename,
2721 "could not set search_path to \"%s\": %s",
2722 schemaName, PQerrorMessage(AH->connection));
2727 ahprintf(AH, "%s;\n\n", qry->data);
2730 free(AH->currSchema);
2731 AH->currSchema = pg_strdup(schemaName);
2733 destroyPQExpBuffer(qry);
2737 * Issue the commands to select the specified tablespace as the current one
2738 * in the target database.
2741 _selectTablespace(ArchiveHandle *AH, const char *tablespace)
2747 /* do nothing in --no-tablespaces mode */
2748 if (AH->ropt->noTablespace)
2751 have = AH->currTablespace;
2754 /* no need to do anything for non-tablespace object */
2758 if (have && strcmp(want, have) == 0)
2759 return; /* no need to do anything */
2761 qry = createPQExpBuffer();
2763 if (strcmp(want, "") == 0)
2765 /* We want the tablespace to be the database's default */
2766 appendPQExpBuffer(qry, "SET default_tablespace = ''");
2770 /* We want an explicit tablespace */
2771 appendPQExpBuffer(qry, "SET default_tablespace = %s", fmtId(want));
2774 if (RestoringToDB(AH))
2778 res = PQexec(AH->connection, qry->data);
2780 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
2781 warn_or_die_horribly(AH, modulename,
2782 "could not set default_tablespace to %s: %s",
2783 fmtId(want), PQerrorMessage(AH->connection));
2788 ahprintf(AH, "%s;\n\n", qry->data);
2790 if (AH->currTablespace)
2791 free(AH->currTablespace);
2792 AH->currTablespace = pg_strdup(want);
2794 destroyPQExpBuffer(qry);
2798 * Extract an object description for a TOC entry, and append it to buf.
2800 * This is not quite as general as it may seem, since it really only
2801 * handles constructing the right thing to put into ALTER ... OWNER TO.
2803 * The whole thing is pretty grotty, but we are kind of stuck since the
2804 * information used is all that's available in older dump files.
2807 _getObjectDescription(PQExpBuffer buf, TocEntry *te, ArchiveHandle *AH)
2809 const char *type = te->desc;
2811 /* Use ALTER TABLE for views and sequences */
2812 if (strcmp(type, "VIEW") == 0 || strcmp(type, "SEQUENCE") == 0)
2815 /* objects named by a schema and name */
2816 if (strcmp(type, "COLLATION") == 0 ||
2817 strcmp(type, "CONVERSION") == 0 ||
2818 strcmp(type, "DOMAIN") == 0 ||
2819 strcmp(type, "TABLE") == 0 ||
2820 strcmp(type, "TYPE") == 0 ||
2821 strcmp(type, "FOREIGN TABLE") == 0 ||
2822 strcmp(type, "TEXT SEARCH DICTIONARY") == 0 ||
2823 strcmp(type, "TEXT SEARCH CONFIGURATION") == 0)
2825 appendPQExpBuffer(buf, "%s ", type);
2826 if (te->namespace && te->namespace[0]) /* is null pre-7.3 */
2827 appendPQExpBuffer(buf, "%s.", fmtId(te->namespace));
2830 * Pre-7.3 pg_dump would sometimes (not always) put a fmtId'd name
2831 * into te->tag for an index. This check is heuristic, so make its
2832 * scope as narrow as possible.
2834 if (AH->version < K_VERS_1_7 &&
2835 te->tag[0] == '"' &&
2836 te->tag[strlen(te->tag) - 1] == '"' &&
2837 strcmp(type, "INDEX") == 0)
2838 appendPQExpBuffer(buf, "%s", te->tag);
2840 appendPQExpBuffer(buf, "%s", fmtId(te->tag));
2844 /* objects named by just a name */
2845 if (strcmp(type, "DATABASE") == 0 ||
2846 strcmp(type, "PROCEDURAL LANGUAGE") == 0 ||
2847 strcmp(type, "SCHEMA") == 0 ||
2848 strcmp(type, "FOREIGN DATA WRAPPER") == 0 ||
2849 strcmp(type, "SERVER") == 0 ||
2850 strcmp(type, "USER MAPPING") == 0)
2852 appendPQExpBuffer(buf, "%s %s", type, fmtId(te->tag));
2856 /* BLOBs just have a name, but it's numeric so must not use fmtId */
2857 if (strcmp(type, "BLOB") == 0)
2859 appendPQExpBuffer(buf, "LARGE OBJECT %s", te->tag);
2864 * These object types require additional decoration. Fortunately, the
2865 * information needed is exactly what's in the DROP command.
2867 if (strcmp(type, "AGGREGATE") == 0 ||
2868 strcmp(type, "FUNCTION") == 0 ||
2869 strcmp(type, "OPERATOR") == 0 ||
2870 strcmp(type, "OPERATOR CLASS") == 0 ||
2871 strcmp(type, "OPERATOR FAMILY") == 0)
2873 /* Chop "DROP " off the front and make a modifiable copy */
2874 char *first = pg_strdup(te->dropStmt + 5);
2877 /* point to last character in string */
2878 last = first + strlen(first) - 1;
2880 /* Strip off any ';' or '\n' at the end */
2881 while (last >= first && (*last == '\n' || *last == ';'))
2885 appendPQExpBufferStr(buf, first);
2891 write_msg(modulename, "WARNING: don't know how to set owner for object type %s\n",
2896 _printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isData, bool acl_pass)
2898 /* ACLs are dumped only during acl pass */
2901 if (!_tocEntryIsACL(te))
2906 if (_tocEntryIsACL(te))
2911 * Avoid dumping the public schema, as it will already be created ...
2912 * unless we are using --clean mode, in which case it's been deleted and
2913 * we'd better recreate it. Likewise for its comment, if any.
2915 if (!ropt->dropSchema)
2917 if (strcmp(te->desc, "SCHEMA") == 0 &&
2918 strcmp(te->tag, "public") == 0)
2920 /* The comment restore would require super-user privs, so avoid it. */
2921 if (strcmp(te->desc, "COMMENT") == 0 &&
2922 strcmp(te->tag, "SCHEMA public") == 0)
2926 /* Select owner, schema, and tablespace as necessary */
2927 _becomeOwner(AH, te);
2928 _selectOutputSchema(AH, te->namespace);
2929 _selectTablespace(AH, te->tablespace);
2931 /* Set up OID mode too */
2932 if (strcmp(te->desc, "TABLE") == 0)
2933 _setWithOids(AH, te);
2935 /* Emit header comment for item */
2936 if (!AH->noTocComments)
2945 ahprintf(AH, "--\n");
2946 if (AH->public.verbose)
2948 ahprintf(AH, "-- TOC entry %d (class %u OID %u)\n",
2949 te->dumpId, te->catalogId.tableoid, te->catalogId.oid);
2954 ahprintf(AH, "-- Dependencies:");
2955 for (i = 0; i < te->nDeps; i++)
2956 ahprintf(AH, " %d", te->dependencies[i]);
2960 ahprintf(AH, "-- %sName: %s; Type: %s; Schema: %s; Owner: %s",
2961 pfx, te->tag, te->desc,
2962 te->namespace ? te->namespace : "-",
2963 ropt->noOwner ? "-" : te->owner);
2964 if (te->tablespace && !ropt->noTablespace)
2965 ahprintf(AH, "; Tablespace: %s", te->tablespace);
2968 if (AH->PrintExtraTocPtr !=NULL)
2969 (*AH->PrintExtraTocPtr) (AH, te);
2970 ahprintf(AH, "--\n\n");
2974 * Actually print the definition.
2976 * Really crude hack for suppressing AUTHORIZATION clause that old pg_dump
2977 * versions put into CREATE SCHEMA. We have to do this when --no-owner
2978 * mode is selected. This is ugly, but I see no other good way ...
2980 if (ropt->noOwner && strcmp(te->desc, "SCHEMA") == 0)
2982 ahprintf(AH, "CREATE SCHEMA %s;\n\n\n", fmtId(te->tag));
2986 if (strlen(te->defn) > 0)
2987 ahprintf(AH, "%s\n\n", te->defn);
2991 * If we aren't using SET SESSION AUTH to determine ownership, we must
2992 * instead issue an ALTER OWNER command. We assume that anything without
2993 * a DROP command is not a separately ownable object. All the categories
2994 * with DROP commands must appear in one list or the other.
2996 if (!ropt->noOwner && !ropt->use_setsessauth &&
2997 strlen(te->owner) > 0 && strlen(te->dropStmt) > 0)
2999 if (strcmp(te->desc, "AGGREGATE") == 0 ||
3000 strcmp(te->desc, "BLOB") == 0 ||
3001 strcmp(te->desc, "COLLATION") == 0 ||
3002 strcmp(te->desc, "CONVERSION") == 0 ||
3003 strcmp(te->desc, "DATABASE") == 0 ||
3004 strcmp(te->desc, "DOMAIN") == 0 ||
3005 strcmp(te->desc, "FUNCTION") == 0 ||
3006 strcmp(te->desc, "OPERATOR") == 0 ||
3007 strcmp(te->desc, "OPERATOR CLASS") == 0 ||
3008 strcmp(te->desc, "OPERATOR FAMILY") == 0 ||
3009 strcmp(te->desc, "PROCEDURAL LANGUAGE") == 0 ||
3010 strcmp(te->desc, "SCHEMA") == 0 ||
3011 strcmp(te->desc, "TABLE") == 0 ||
3012 strcmp(te->desc, "TYPE") == 0 ||
3013 strcmp(te->desc, "VIEW") == 0 ||
3014 strcmp(te->desc, "SEQUENCE") == 0 ||
3015 strcmp(te->desc, "FOREIGN TABLE") == 0 ||
3016 strcmp(te->desc, "TEXT SEARCH DICTIONARY") == 0 ||
3017 strcmp(te->desc, "TEXT SEARCH CONFIGURATION") == 0 ||
3018 strcmp(te->desc, "FOREIGN DATA WRAPPER") == 0 ||
3019 strcmp(te->desc, "SERVER") == 0)
3021 PQExpBuffer temp = createPQExpBuffer();
3023 appendPQExpBuffer(temp, "ALTER ");
3024 _getObjectDescription(temp, te, AH);
3025 appendPQExpBuffer(temp, " OWNER TO %s;", fmtId(te->owner));
3026 ahprintf(AH, "%s\n\n", temp->data);
3027 destroyPQExpBuffer(temp);
3029 else if (strcmp(te->desc, "CAST") == 0 ||
3030 strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
3031 strcmp(te->desc, "CONSTRAINT") == 0 ||
3032 strcmp(te->desc, "DEFAULT") == 0 ||
3033 strcmp(te->desc, "FK CONSTRAINT") == 0 ||
3034 strcmp(te->desc, "INDEX") == 0 ||
3035 strcmp(te->desc, "RULE") == 0 ||
3036 strcmp(te->desc, "TRIGGER") == 0 ||
3037 strcmp(te->desc, "USER MAPPING") == 0)
3039 /* these object types don't have separate owners */
3043 write_msg(modulename, "WARNING: don't know how to set owner for object type %s\n",
3049 * If it's an ACL entry, it might contain SET SESSION AUTHORIZATION
3050 * commands, so we can no longer assume we know the current auth setting.
3056 AH->currUser = NULL;
3061 WriteHead(ArchiveHandle *AH)
3065 (*AH->WriteBufPtr) (AH, "PGDMP", 5); /* Magic code */
3066 (*AH->WriteBytePtr) (AH, AH->vmaj);
3067 (*AH->WriteBytePtr) (AH, AH->vmin);
3068 (*AH->WriteBytePtr) (AH, AH->vrev);
3069 (*AH->WriteBytePtr) (AH, AH->intSize);
3070 (*AH->WriteBytePtr) (AH, AH->offSize);
3071 (*AH->WriteBytePtr) (AH, AH->format);
3074 if (AH->compression != 0)
3075 write_msg(modulename, "WARNING: requested compression not available in this "
3076 "installation -- archive will be uncompressed\n");
3078 AH->compression = 0;
3081 WriteInt(AH, AH->compression);
3083 crtm = *localtime(&AH->createDate);
3084 WriteInt(AH, crtm.tm_sec);
3085 WriteInt(AH, crtm.tm_min);
3086 WriteInt(AH, crtm.tm_hour);
3087 WriteInt(AH, crtm.tm_mday);
3088 WriteInt(AH, crtm.tm_mon);
3089 WriteInt(AH, crtm.tm_year);
3090 WriteInt(AH, crtm.tm_isdst);
3091 WriteStr(AH, PQdb(AH->connection));
3092 WriteStr(AH, AH->public.remoteVersionStr);
3093 WriteStr(AH, PG_VERSION);
3097 ReadHead(ArchiveHandle *AH)
3104 * If we haven't already read the header, do so.
3106 * NB: this code must agree with _discoverArchiveFormat(). Maybe find a
3107 * way to unify the cases?
3109 if (!AH->readHeader)
3111 if ((*AH->ReadBufPtr) (AH, tmpMag, 5) != 5)
3112 die_horribly(AH, modulename, "unexpected end of file\n");
3114 if (strncmp(tmpMag, "PGDMP", 5) != 0)
3115 die_horribly(AH, modulename, "did not find magic string in file header\n");
3117 AH->vmaj = (*AH->ReadBytePtr) (AH);
3118 AH->vmin = (*AH->ReadBytePtr) (AH);
3120 if (AH->vmaj > 1 || ((AH->vmaj == 1) && (AH->vmin > 0))) /* Version > 1.0 */
3121 AH->vrev = (*AH->ReadBytePtr) (AH);
3125 AH->version = ((AH->vmaj * 256 + AH->vmin) * 256 + AH->vrev) * 256 + 0;
3127 if (AH->version < K_VERS_1_0 || AH->version > K_VERS_MAX)
3128 die_horribly(AH, modulename, "unsupported version (%d.%d) in file header\n",
3129 AH->vmaj, AH->vmin);
3131 AH->intSize = (*AH->ReadBytePtr) (AH);
3132 if (AH->intSize > 32)
3133 die_horribly(AH, modulename, "sanity check on integer size (%lu) failed\n",
3134 (unsigned long) AH->intSize);
3136 if (AH->intSize > sizeof(int))
3137 write_msg(modulename, "WARNING: archive was made on a machine with larger integers, some operations might fail\n");
3139 if (AH->version >= K_VERS_1_7)
3140 AH->offSize = (*AH->ReadBytePtr) (AH);
3142 AH->offSize = AH->intSize;
3144 fmt = (*AH->ReadBytePtr) (AH);
3146 if (AH->format != fmt)
3147 die_horribly(AH, modulename, "expected format (%d) differs from format found in file (%d)\n",
3151 if (AH->version >= K_VERS_1_2)
3153 if (AH->version < K_VERS_1_4)
3154 AH->compression = (*AH->ReadBytePtr) (AH);
3156 AH->compression = ReadInt(AH);
3159 AH->compression = Z_DEFAULT_COMPRESSION;
3162 if (AH->compression != 0)
3163 write_msg(modulename, "WARNING: archive is compressed, but this installation does not support compression -- no data will be available\n");
3166 if (AH->version >= K_VERS_1_4)
3168 crtm.tm_sec = ReadInt(AH);
3169 crtm.tm_min = ReadInt(AH);
3170 crtm.tm_hour = ReadInt(AH);
3171 crtm.tm_mday = ReadInt(AH);
3172 crtm.tm_mon = ReadInt(AH);
3173 crtm.tm_year = ReadInt(AH);
3174 crtm.tm_isdst = ReadInt(AH);
3176 AH->archdbname = ReadStr(AH);
3178 AH->createDate = mktime(&crtm);
3180 if (AH->createDate == (time_t) -1)
3181 write_msg(modulename, "WARNING: invalid creation date in header\n");
3184 if (AH->version >= K_VERS_1_10)
3186 AH->archiveRemoteVersion = ReadStr(AH);
3187 AH->archiveDumpVersion = ReadStr(AH);
3194 * check to see if ftell/fseek can be performed.
3202 * If pgoff_t is wider than long, we must have "real" fseeko and not an
3203 * emulation using fseek. Otherwise report no seek capability.
3206 if (sizeof(pgoff_t) > sizeof(long))
3210 /* Check that ftello works on this file */
3217 * Check that fseeko(SEEK_SET) works, too. NB: we used to try to test
3218 * this with fseeko(fp, 0, SEEK_CUR). But some platforms treat that as a
3219 * successful no-op even on files that are otherwise unseekable.
3221 if (fseeko(fp, tpos, SEEK_SET) != 0)
3232 dumpTimestamp(ArchiveHandle *AH, const char *msg, time_t tim)
3237 * We don't print the timezone on Win32, because the names are long and
3238 * localized, which means they may contain characters in various random
3239 * encodings; this has been seen to cause encoding errors when reading the
3242 if (strftime(buf, sizeof(buf),
3244 "%Y-%m-%d %H:%M:%S %Z",
3246 "%Y-%m-%d %H:%M:%S",
3248 localtime(&tim)) != 0)
3249 ahprintf(AH, "-- %s %s\n\n", msg, buf);
3254 * Main engine for parallel restore.
3256 * Work is done in three phases.
3257 * First we process all SECTION_PRE_DATA tocEntries, in a single connection,
3258 * just as for a standard restore. Second we process the remaining non-ACL
3259 * steps in parallel worker children (threads on Windows, processes on Unix),
3260 * each of which connects separately to the database. Finally we process all
3261 * the ACL entries in a single connection (that happens back in
3265 restore_toc_entries_parallel(ArchiveHandle *AH)
3267 RestoreOptions *ropt = AH->ropt;
3268 int n_slots = ropt->number_of_jobs;
3269 ParallelSlot *slots;
3273 TocEntry pending_list;
3274 TocEntry ready_list;
3275 TocEntry *next_work_item;
3279 ahlog(AH, 2, "entering restore_toc_entries_parallel\n");
3281 slots = (ParallelSlot *) pg_calloc(sizeof(ParallelSlot), n_slots);
3283 /* Adjust dependency information */
3284 fix_dependencies(AH);
3287 * Do all the early stuff in a single connection in the parent. There's no
3288 * great point in running it in parallel, in fact it will actually run
3289 * faster in a single connection because we avoid all the connection and
3290 * setup overhead. Also, pg_dump is not currently very good about showing
3291 * all the dependencies of SECTION_PRE_DATA items, so we do not risk
3292 * trying to process them out-of-order.
3294 skipped_some = false;
3295 for (next_work_item = AH->toc->next; next_work_item != AH->toc; next_work_item = next_work_item->next)
3297 /* NB: process-or-continue logic must be the inverse of loop below */
3298 if (next_work_item->section != SECTION_PRE_DATA)
3300 /* DATA and POST_DATA items are just ignored for now */
3301 if (next_work_item->section == SECTION_DATA ||
3302 next_work_item->section == SECTION_POST_DATA)
3304 skipped_some = true;
3310 * SECTION_NONE items, such as comments, can be processed now
3311 * if we are still in the PRE_DATA part of the archive. Once
3312 * we've skipped any items, we have to consider whether the
3313 * comment's dependencies are satisfied, so skip it for now.
3320 ahlog(AH, 1, "processing item %d %s %s\n",
3321 next_work_item->dumpId,
3322 next_work_item->desc, next_work_item->tag);
3324 (void) restore_toc_entry(AH, next_work_item, ropt, false);
3326 /* there should be no touch of ready_list here, so pass NULL */
3327 reduce_dependencies(AH, next_work_item, NULL);
3331 * Now close parent connection in prep for parallel steps. We do this
3332 * mainly to ensure that we don't exceed the specified number of parallel
3335 PQfinish(AH->connection);
3336 AH->connection = NULL;
3338 /* blow away any transient state from the old connection */
3341 AH->currUser = NULL;
3343 free(AH->currSchema);
3344 AH->currSchema = NULL;
3345 if (AH->currTablespace)
3346 free(AH->currTablespace);
3347 AH->currTablespace = NULL;
3348 AH->currWithOids = -1;
3351 * Initialize the lists of pending and ready items. After this setup, the
3352 * pending list is everything that needs to be done but is blocked by one
3353 * or more dependencies, while the ready list contains items that have no
3354 * remaining dependencies. Note: we don't yet filter out entries that
3355 * aren't going to be restored. They might participate in dependency
3356 * chains connecting entries that should be restored, so we treat them as
3357 * live until we actually process them.
3359 par_list_header_init(&pending_list);
3360 par_list_header_init(&ready_list);
3361 skipped_some = false;
3362 for (next_work_item = AH->toc->next; next_work_item != AH->toc; next_work_item = next_work_item->next)
3364 /* NB: process-or-continue logic must be the inverse of loop above */
3365 if (next_work_item->section == SECTION_PRE_DATA)
3367 /* All PRE_DATA items were dealt with above */
3370 if (next_work_item->section == SECTION_DATA ||
3371 next_work_item->section == SECTION_POST_DATA)
3373 /* set this flag at same point that previous loop did */
3374 skipped_some = true;
3378 /* SECTION_NONE items must be processed if previous loop didn't */
3383 if (next_work_item->depCount > 0)
3384 par_list_append(&pending_list, next_work_item);
3386 par_list_append(&ready_list, next_work_item);
3392 * Keep going until there is no worker still running AND there is no work
3396 ahlog(AH, 1, "entering main parallel loop\n");
3398 while ((next_work_item = get_next_work_item(AH, &ready_list,
3399 slots, n_slots)) != NULL ||
3400 work_in_progress(slots, n_slots))
3402 if (next_work_item != NULL)
3406 /* If not to be dumped, don't waste time launching a worker */
3407 reqs = _tocEntryRequired(next_work_item, AH->ropt, false);
3408 if ((reqs & (REQ_SCHEMA | REQ_DATA)) == 0)
3410 ahlog(AH, 1, "skipping item %d %s %s\n",
3411 next_work_item->dumpId,
3412 next_work_item->desc, next_work_item->tag);
3414 par_list_remove(next_work_item);
3415 reduce_dependencies(AH, next_work_item, &ready_list);
3420 if ((next_slot = get_next_slot(slots, n_slots)) != NO_SLOT)
3422 /* There is work still to do and a worker slot available */
3426 ahlog(AH, 1, "launching item %d %s %s\n",
3427 next_work_item->dumpId,
3428 next_work_item->desc, next_work_item->tag);
3430 par_list_remove(next_work_item);
3432 /* this memory is dealloced in mark_work_done() */
3433 args = pg_malloc(sizeof(RestoreArgs));
3434 args->AH = CloneArchive(AH);
3435 args->te = next_work_item;
3437 /* run the step in a worker child */
3438 child = spawn_restore(args);
3440 slots[next_slot].child_id = child;
3441 slots[next_slot].args = args;
3448 * If we get here there must be work being done. Either there is no
3449 * work available to schedule (and work_in_progress returned true) or
3450 * there are no slots available. So we wait for a worker to finish,
3451 * and process the result.
3453 ret_child = reap_child(slots, n_slots, &work_status);
3455 if (WIFEXITED(work_status))
3457 mark_work_done(AH, &ready_list,
3458 ret_child, WEXITSTATUS(work_status),
3463 die_horribly(AH, modulename, "worker process crashed: status %d\n",
3468 ahlog(AH, 1, "finished main parallel loop\n");
3471 * Now reconnect the single parent connection.
3473 ConnectDatabase((Archive *) AH, ropt->dbname,
3474 ropt->pghost, ropt->pgport, ropt->username,
3475 ropt->promptPassword);
3477 _doSetFixedOutputState(AH);
3480 * Make sure there is no non-ACL work left due to, say, circular
3481 * dependencies, or some other pathological condition. If so, do it in the
3482 * single parent connection.
3484 for (te = pending_list.par_next; te != &pending_list; te = te->par_next)
3486 ahlog(AH, 1, "processing missed item %d %s %s\n",
3487 te->dumpId, te->desc, te->tag);
3488 (void) restore_toc_entry(AH, te, ropt, false);
3491 /* The ACLs will be handled back in RestoreArchive. */
3495 * create a worker child to perform a restore step in parallel
3498 spawn_restore(RestoreArgs *args)
3502 /* Ensure stdio state is quiesced before forking */
3509 /* in child process */
3510 parallel_restore(args);
3511 die_horribly(args->AH, modulename,
3512 "parallel_restore should not return\n");
3517 die_horribly(args->AH, modulename,
3518 "could not create worker process: %s\n",
3522 child = (HANDLE) _beginthreadex(NULL, 0, (void *) parallel_restore,
3525 die_horribly(args->AH, modulename,
3526 "could not create worker thread: %s\n",
3534 * collect status from a completed worker child
3537 reap_child(ParallelSlot *slots, int n_slots, int *work_status)
3540 /* Unix is so much easier ... */
3541 return wait(work_status);
3543 static HANDLE *handles = NULL;
3550 /* first time around only, make space for handles to listen on */
3551 if (handles == NULL)
3552 handles = (HANDLE *) pg_calloc(sizeof(HANDLE), n_slots);
3554 /* set up list of handles to listen to */
3555 for (snum = 0, tnum = 0; snum < n_slots; snum++)
3556 if (slots[snum].child_id != 0)
3557 handles[tnum++] = slots[snum].child_id;
3559 /* wait for one to finish */
3560 hindex = WaitForMultipleObjects(tnum, handles, false, INFINITE);
3562 /* get handle of finished thread */
3563 ret_child = handles[hindex - WAIT_OBJECT_0];
3565 /* get the result */
3566 GetExitCodeThread(ret_child, &res);
3569 /* dispose of handle to stop leaks */
3570 CloseHandle(ret_child);
3577 * are we doing anything now?
3580 work_in_progress(ParallelSlot *slots, int n_slots)
3584 for (i = 0; i < n_slots; i++)
3586 if (slots[i].child_id != 0)
3593 * find the first free parallel slot (if any).
3596 get_next_slot(ParallelSlot *slots, int n_slots)
3600 for (i = 0; i < n_slots; i++)
3602 if (slots[i].child_id == 0)
3610 * Check if te1 has an exclusive lock requirement for an item that te2 also
3611 * requires, whether or not te2's requirement is for an exclusive lock.
3614 has_lock_conflicts(TocEntry *te1, TocEntry *te2)
3619 for (j = 0; j < te1->nLockDeps; j++)
3621 for (k = 0; k < te2->nDeps; k++)
3623 if (te1->lockDeps[j] == te2->dependencies[k])
3632 * Initialize the header of a parallel-processing list.
3634 * These are circular lists with a dummy TocEntry as header, just like the
3635 * main TOC list; but we use separate list links so that an entry can be in
3636 * the main TOC list as well as in a parallel-processing list.
3639 par_list_header_init(TocEntry *l)
3641 l->par_prev = l->par_next = l;
3644 /* Append te to the end of the parallel-processing list headed by l */
3646 par_list_append(TocEntry *l, TocEntry *te)
3648 te->par_prev = l->par_prev;
3649 l->par_prev->par_next = te;
3654 /* Remove te from whatever parallel-processing list it's in */
3656 par_list_remove(TocEntry *te)
3658 te->par_prev->par_next = te->par_next;
3659 te->par_next->par_prev = te->par_prev;
3660 te->par_prev = NULL;
3661 te->par_next = NULL;
3666 * Find the next work item (if any) that is capable of being run now.
3668 * To qualify, the item must have no remaining dependencies
3669 * and no requirements for locks that are incompatible with
3670 * items currently running. Items in the ready_list are known to have
3671 * no remaining dependencies, but we have to check for lock conflicts.
3673 * Note that the returned item has *not* been removed from ready_list.
3674 * The caller must do that after successfully dispatching the item.
3676 * pref_non_data is for an alternative selection algorithm that gives
3677 * preference to non-data items if there is already a data load running.
3678 * It is currently disabled.
3681 get_next_work_item(ArchiveHandle *AH, TocEntry *ready_list,
3682 ParallelSlot *slots, int n_slots)
3684 bool pref_non_data = false; /* or get from AH->ropt */
3685 TocEntry *data_te = NULL;
3691 * Bogus heuristics for pref_non_data
3697 for (k = 0; k < n_slots; k++)
3698 if (slots[k].args->te != NULL &&
3699 slots[k].args->te->section == SECTION_DATA)
3701 if (n_slots == 0 || count * 4 < n_slots)
3702 pref_non_data = false;
3706 * Search the ready_list until we find a suitable item.
3708 for (te = ready_list->par_next; te != ready_list; te = te->par_next)
3710 bool conflicts = false;
3713 * Check to see if the item would need exclusive lock on something
3714 * that a currently running item also needs lock on, or vice versa. If
3715 * so, we don't want to schedule them together.
3717 for (i = 0; i < n_slots && !conflicts; i++)
3719 TocEntry *running_te;
3721 if (slots[i].args == NULL)
3723 running_te = slots[i].args->te;
3725 if (has_lock_conflicts(te, running_te) ||
3726 has_lock_conflicts(running_te, te))
3736 if (pref_non_data && te->section == SECTION_DATA)
3738 if (data_te == NULL)
3743 /* passed all tests, so this item can run */
3747 if (data_te != NULL)
3750 ahlog(AH, 2, "no item ready\n");
3756 * Restore a single TOC item in parallel with others
3758 * this is the procedure run as a thread (Windows) or a
3759 * separate process (everything else).
3761 static parallel_restore_result
3762 parallel_restore(RestoreArgs *args)
3764 ArchiveHandle *AH = args->AH;
3765 TocEntry *te = args->te;
3766 RestoreOptions *ropt = AH->ropt;
3770 * Close and reopen the input file so we have a private file pointer that
3771 * doesn't stomp on anyone else's file pointer, if we're actually going to
3772 * need to read from the file. Otherwise, just close it except on Windows,
3773 * where it will possibly be needed by other threads.
3775 * Note: on Windows, since we are using threads not processes, the reopen
3776 * call *doesn't* close the original file pointer but just open a new one.
3778 if (te->section == SECTION_DATA)
3779 (AH->ReopenPtr) (AH);
3782 (AH->ClosePtr) (AH);
3786 * We need our own database connection, too
3788 ConnectDatabase((Archive *) AH, ropt->dbname,
3789 ropt->pghost, ropt->pgport, ropt->username,
3790 ropt->promptPassword);
3792 _doSetFixedOutputState(AH);
3794 /* Restore the TOC item */
3795 retval = restore_toc_entry(AH, te, ropt, true);
3798 PQfinish(AH->connection);
3799 AH->connection = NULL;
3801 /* If we reopened the file, we are done with it, so close it now */
3802 if (te->section == SECTION_DATA)
3803 (AH->ClosePtr) (AH);
3805 if (retval == 0 && AH->public.n_errors)
3806 retval = WORKER_IGNORED_ERRORS;
3817 * Housekeeping to be done after a step has been parallel restored.
3819 * Clear the appropriate slot, free all the extra memory we allocated,
3820 * update status, and reduce the dependency count of any dependent items.
3823 mark_work_done(ArchiveHandle *AH, TocEntry *ready_list,
3824 thandle worker, int status,
3825 ParallelSlot *slots, int n_slots)
3827 TocEntry *te = NULL;
3830 for (i = 0; i < n_slots; i++)
3832 if (slots[i].child_id == worker)
3834 slots[i].child_id = 0;
3835 te = slots[i].args->te;
3836 DeCloneArchive(slots[i].args->AH);
3837 free(slots[i].args);
3838 slots[i].args = NULL;
3845 die_horribly(AH, modulename, "could not find slot of finished worker\n");
3847 ahlog(AH, 1, "finished item %d %s %s\n",
3848 te->dumpId, te->desc, te->tag);
3850 if (status == WORKER_CREATE_DONE)
3851 mark_create_done(AH, te);
3852 else if (status == WORKER_INHIBIT_DATA)
3854 inhibit_data_for_failed_table(AH, te);
3855 AH->public.n_errors++;
3857 else if (status == WORKER_IGNORED_ERRORS)
3858 AH->public.n_errors++;
3859 else if (status != 0)
3860 die_horribly(AH, modulename, "worker process failed: exit code %d\n",
3863 reduce_dependencies(AH, te, ready_list);
3868 * Process the dependency information into a form useful for parallel restore.
3870 * This function takes care of fixing up some missing or badly designed
3871 * dependencies, and then prepares subsidiary data structures that will be
3872 * used in the main parallel-restore logic, including:
3873 * 1. We build the tocsByDumpId[] index array.
3874 * 2. We build the revDeps[] arrays of incoming dependency dumpIds.
3875 * 3. We set up depCount fields that are the number of as-yet-unprocessed
3876 * dependencies for each TOC entry.
3878 * We also identify locking dependencies so that we can avoid trying to
3879 * schedule conflicting items at the same time.
3882 fix_dependencies(ArchiveHandle *AH)
3888 * It is convenient to have an array that indexes the TOC entries by dump
3889 * ID, rather than searching the TOC list repeatedly. Entries for dump
3890 * IDs not present in the TOC will be NULL.
3892 * NOTE: because maxDumpId is just the highest dump ID defined in the
3893 * archive, there might be dependencies for IDs > maxDumpId. All uses of
3894 * this array must guard against out-of-range dependency numbers.
3896 * Also, initialize the depCount/revDeps/nRevDeps fields, and make sure
3897 * the TOC items are marked as not being in any parallel-processing list.
3899 maxDumpId = AH->maxDumpId;
3900 tocsByDumpId = (TocEntry **) pg_calloc(maxDumpId, sizeof(TocEntry *));
3901 for (te = AH->toc->next; te != AH->toc; te = te->next)
3903 tocsByDumpId[te->dumpId - 1] = te;
3904 te->depCount = te->nDeps;
3907 te->par_prev = NULL;
3908 te->par_next = NULL;
3912 * POST_DATA items that are shown as depending on a table need to be
3913 * re-pointed to depend on that table's data, instead. This ensures they
3914 * won't get scheduled until the data has been loaded. We handle this by
3915 * first finding TABLE/TABLE DATA pairs and then scanning all the
3918 * Note: currently, a TABLE DATA should always have exactly one
3919 * dependency, on its TABLE item. So we don't bother to search, but look
3920 * just at the first dependency. We do trouble to make sure that it's a
3921 * TABLE, if possible. However, if the dependency isn't in the archive
3922 * then just assume it was a TABLE; this is to cover cases where the table
3923 * was suppressed but we have the data and some dependent post-data items.
3925 * XXX this is O(N^2) if there are a lot of tables. We ought to fix
3926 * pg_dump to produce correctly-linked dependencies in the first place.
3928 for (te = AH->toc->next; te != AH->toc; te = te->next)
3930 if (strcmp(te->desc, "TABLE DATA") == 0 && te->nDeps > 0)
3932 DumpId tableId = te->dependencies[0];
3934 if (tableId > maxDumpId ||
3935 tocsByDumpId[tableId - 1] == NULL ||
3936 strcmp(tocsByDumpId[tableId - 1]->desc, "TABLE") == 0)
3938 repoint_table_dependencies(AH, tableId, te->dumpId);
3944 * Pre-8.4 versions of pg_dump neglected to set up a dependency from BLOB
3945 * COMMENTS to BLOBS. Cope. (We assume there's only one BLOBS and only
3946 * one BLOB COMMENTS in such files.)
3948 if (AH->version < K_VERS_1_11)
3950 for (te = AH->toc->next; te != AH->toc; te = te->next)
3952 if (strcmp(te->desc, "BLOB COMMENTS") == 0 && te->nDeps == 0)
3956 for (te2 = AH->toc->next; te2 != AH->toc; te2 = te2->next)
3958 if (strcmp(te2->desc, "BLOBS") == 0)
3960 te->dependencies = (DumpId *) pg_malloc(sizeof(DumpId));
3961 te->dependencies[0] = te2->dumpId;
3973 * At this point we start to build the revDeps reverse-dependency arrays,
3974 * so all changes of dependencies must be complete.
3978 * Count the incoming dependencies for each item. Also, it is possible
3979 * that the dependencies list items that are not in the archive at all.
3980 * Subtract such items from the depCounts.
3982 for (te = AH->toc->next; te != AH->toc; te = te->next)
3984 for (i = 0; i < te->nDeps; i++)
3986 DumpId depid = te->dependencies[i];
3988 if (depid <= maxDumpId && tocsByDumpId[depid - 1] != NULL)
3989 tocsByDumpId[depid - 1]->nRevDeps++;
3996 * Allocate space for revDeps[] arrays, and reset nRevDeps so we can use
3997 * it as a counter below.
3999 for (te = AH->toc->next; te != AH->toc; te = te->next)
4001 if (te->nRevDeps > 0)
4002 te->revDeps = (DumpId *) pg_malloc(te->nRevDeps * sizeof(DumpId));
4007 * Build the revDeps[] arrays of incoming-dependency dumpIds. This had
4008 * better agree with the loops above.
4010 for (te = AH->toc->next; te != AH->toc; te = te->next)
4012 for (i = 0; i < te->nDeps; i++)
4014 DumpId depid = te->dependencies[i];
4016 if (depid <= maxDumpId && tocsByDumpId[depid - 1] != NULL)
4018 TocEntry *otherte = tocsByDumpId[depid - 1];
4020 otherte->revDeps[otherte->nRevDeps++] = te->dumpId;
4026 * Lastly, work out the locking dependencies.
4028 for (te = AH->toc->next; te != AH->toc; te = te->next)
4030 te->lockDeps = NULL;
4032 identify_locking_dependencies(te);
4037 * Change dependencies on tableId to depend on tableDataId instead,
4038 * but only in POST_DATA items.
4041 repoint_table_dependencies(ArchiveHandle *AH,
4042 DumpId tableId, DumpId tableDataId)
4047 for (te = AH->toc->next; te != AH->toc; te = te->next)
4049 if (te->section != SECTION_POST_DATA)
4051 for (i = 0; i < te->nDeps; i++)
4053 if (te->dependencies[i] == tableId)
4055 te->dependencies[i] = tableDataId;
4056 ahlog(AH, 2, "transferring dependency %d -> %d to %d\n",
4057 te->dumpId, tableId, tableDataId);
4064 * Identify which objects we'll need exclusive lock on in order to restore
4065 * the given TOC entry (*other* than the one identified by the TOC entry
4066 * itself). Record their dump IDs in the entry's lockDeps[] array.
4069 identify_locking_dependencies(TocEntry *te)
4075 /* Quick exit if no dependencies at all */
4079 /* Exit if this entry doesn't need exclusive lock on other objects */
4080 if (!(strcmp(te->desc, "CONSTRAINT") == 0 ||
4081 strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
4082 strcmp(te->desc, "FK CONSTRAINT") == 0 ||
4083 strcmp(te->desc, "RULE") == 0 ||
4084 strcmp(te->desc, "TRIGGER") == 0))
4088 * We assume the item requires exclusive lock on each TABLE DATA item
4089 * listed among its dependencies. (This was originally a dependency on
4090 * the TABLE, but fix_dependencies repointed it to the data item. Note
4091 * that all the entry types we are interested in here are POST_DATA, so
4092 * they will all have been changed this way.)
4094 lockids = (DumpId *) pg_malloc(te->nDeps * sizeof(DumpId));
4096 for (i = 0; i < te->nDeps; i++)
4098 DumpId depid = te->dependencies[i];
4100 if (depid <= maxDumpId && tocsByDumpId[depid - 1] &&
4101 strcmp(tocsByDumpId[depid - 1]->desc, "TABLE DATA") == 0)
4102 lockids[nlockids++] = depid;
4111 te->lockDeps = pg_realloc(lockids, nlockids * sizeof(DumpId));
4112 te->nLockDeps = nlockids;
4116 * Remove the specified TOC entry from the depCounts of items that depend on
4117 * it, thereby possibly making them ready-to-run. Any pending item that
4118 * becomes ready should be moved to the ready list.
4121 reduce_dependencies(ArchiveHandle *AH, TocEntry *te, TocEntry *ready_list)
4125 ahlog(AH, 2, "reducing dependencies for %d\n", te->dumpId);
4127 for (i = 0; i < te->nRevDeps; i++)
4129 TocEntry *otherte = tocsByDumpId[te->revDeps[i] - 1];
4131 otherte->depCount--;
4132 if (otherte->depCount == 0 && otherte->par_prev != NULL)
4134 /* It must be in the pending list, so remove it ... */
4135 par_list_remove(otherte);
4136 /* ... and add to ready_list */
4137 par_list_append(ready_list, otherte);
4143 * Set the created flag on the DATA member corresponding to the given
4147 mark_create_done(ArchiveHandle *AH, TocEntry *te)
4151 for (tes = AH->toc->next; tes != AH->toc; tes = tes->next)
4153 if (strcmp(tes->desc, "TABLE DATA") == 0 &&
4154 strcmp(tes->tag, te->tag) == 0 &&
4155 strcmp(tes->namespace ? tes->namespace : "",
4156 te->namespace ? te->namespace : "") == 0)
4158 tes->created = true;
4165 * Mark the DATA member corresponding to the given TABLE member
4169 inhibit_data_for_failed_table(ArchiveHandle *AH, TocEntry *te)
4171 RestoreOptions *ropt = AH->ropt;
4174 ahlog(AH, 1, "table \"%s\" could not be created, will not restore its data\n",
4177 for (tes = AH->toc->next; tes != AH->toc; tes = tes->next)
4179 if (strcmp(tes->desc, "TABLE DATA") == 0 &&
4180 strcmp(tes->tag, te->tag) == 0 &&
4181 strcmp(tes->namespace ? tes->namespace : "",
4182 te->namespace ? te->namespace : "") == 0)
4184 /* mark it unwanted; we assume idWanted array already exists */
4185 ropt->idWanted[tes->dumpId - 1] = false;
4193 * Clone and de-clone routines used in parallel restoration.
4195 * Enough of the structure is cloned to ensure that there is no
4196 * conflict between different threads each with their own clone.
4198 * These could be public, but no need at present.
4200 static ArchiveHandle *
4201 CloneArchive(ArchiveHandle *AH)
4203 ArchiveHandle *clone;
4205 /* Make a "flat" copy */
4206 clone = (ArchiveHandle *) pg_malloc(sizeof(ArchiveHandle));
4207 memcpy(clone, AH, sizeof(ArchiveHandle));
4209 /* Handle format-independent fields */
4210 memset(&(clone->sqlparse), 0, sizeof(clone->sqlparse));
4212 /* The clone will have its own connection, so disregard connection state */
4213 clone->connection = NULL;
4214 clone->currUser = NULL;
4215 clone->currSchema = NULL;
4216 clone->currTablespace = NULL;
4217 clone->currWithOids = -1;
4219 /* savedPassword must be local in case we change it while connecting */
4220 if (clone->savedPassword)
4221 clone->savedPassword = pg_strdup(clone->savedPassword);
4223 /* clone has its own error count, too */
4224 clone->public.n_errors = 0;
4226 /* Let the format-specific code have a chance too */
4227 (clone->ClonePtr) (clone);
4233 * Release clone-local storage.
4235 * Note: we assume any clone-local connection was already closed.
4238 DeCloneArchive(ArchiveHandle *AH)
4240 /* Clear format-specific state */
4241 (AH->DeClonePtr) (AH);
4243 /* Clear state allocated by CloneArchive */
4244 if (AH->sqlparse.curCmd)
4245 destroyPQExpBuffer(AH->sqlparse.curCmd);
4247 /* Clear any connection-local state */
4251 free(AH->currSchema);
4252 if (AH->currTablespace)
4253 free(AH->currTablespace);
4254 if (AH->savedPassword)
4255 free(AH->savedPassword);