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.
20 *-------------------------------------------------------------------------
23 #include "pg_backup_db.h"
24 #include "dumputils.h"
28 #include <sys/types.h>
35 #include "libpq/libpq-fs.h"
38 * Special exit values from worker children. We reserve 0 for normal
39 * success; 1 and other small values should be interpreted as crashes.
41 #define WORKER_CREATE_DONE 10
42 #define WORKER_INHIBIT_DATA 11
43 #define WORKER_IGNORED_ERRORS 12
46 * Unix uses exit to return result from worker child, so function is void.
47 * Windows thread result comes via function return.
50 #define parallel_restore_result void
52 #define parallel_restore_result DWORD
55 /* IDs for worker children are either PIDs or thread handles */
59 #define thandle HANDLE
62 typedef struct _restore_args
68 typedef struct _parallel_slot
78 static const char *modulename
= gettext_noop("archiver");
81 static ArchiveHandle
*_allocAH(const char *FileSpec
, const ArchiveFormat fmt
,
82 const int compression
, ArchiveMode mode
);
83 static void _getObjectDescription(PQExpBuffer buf
, TocEntry
*te
,
85 static void _printTocEntry(ArchiveHandle
*AH
, TocEntry
*te
, RestoreOptions
*ropt
, bool isData
, bool acl_pass
);
88 static void _doSetFixedOutputState(ArchiveHandle
*AH
);
89 static void _doSetSessionAuth(ArchiveHandle
*AH
, const char *user
);
90 static void _doSetWithOids(ArchiveHandle
*AH
, const bool withOids
);
91 static void _reconnectToDB(ArchiveHandle
*AH
, const char *dbname
);
92 static void _becomeUser(ArchiveHandle
*AH
, const char *user
);
93 static void _becomeOwner(ArchiveHandle
*AH
, TocEntry
*te
);
94 static void _selectOutputSchema(ArchiveHandle
*AH
, const char *schemaName
);
95 static void _selectTablespace(ArchiveHandle
*AH
, const char *tablespace
);
96 static void processEncodingEntry(ArchiveHandle
*AH
, TocEntry
*te
);
97 static void processStdStringsEntry(ArchiveHandle
*AH
, TocEntry
*te
);
98 static teReqs
_tocEntryRequired(TocEntry
*te
, RestoreOptions
*ropt
, bool include_acls
);
99 static void _disableTriggersIfNecessary(ArchiveHandle
*AH
, TocEntry
*te
, RestoreOptions
*ropt
);
100 static void _enableTriggersIfNecessary(ArchiveHandle
*AH
, TocEntry
*te
, RestoreOptions
*ropt
);
101 static TocEntry
*getTocEntryByDumpId(ArchiveHandle
*AH
, DumpId id
);
102 static void _moveAfter(ArchiveHandle
*AH
, TocEntry
*pos
, TocEntry
*te
);
103 static int _discoverArchiveFormat(ArchiveHandle
*AH
);
105 static void dump_lo_buf(ArchiveHandle
*AH
);
106 static void _write_msg(const char *modulename
, const char *fmt
, va_list ap
);
107 static void _die_horribly(ArchiveHandle
*AH
, const char *modulename
, const char *fmt
, va_list ap
);
109 static void dumpTimestamp(ArchiveHandle
*AH
, const char *msg
, time_t tim
);
110 static OutputContext
SetOutput(ArchiveHandle
*AH
, char *filename
, int compression
);
111 static void ResetOutput(ArchiveHandle
*AH
, OutputContext savedContext
);
113 static int restore_toc_entry(ArchiveHandle
*AH
, TocEntry
*te
,
114 RestoreOptions
*ropt
, bool is_parallel
);
115 static void restore_toc_entries_parallel(ArchiveHandle
*AH
);
116 static thandle
spawn_restore(RestoreArgs
*args
);
117 static thandle
reap_child(ParallelSlot
*slots
, int n_slots
, int *work_status
);
118 static bool work_in_progress(ParallelSlot
*slots
, int n_slots
);
119 static int get_next_slot(ParallelSlot
*slots
, int n_slots
);
120 static TocEntry
*get_next_work_item(ArchiveHandle
*AH
,
121 TocEntry
**first_unprocessed
,
122 ParallelSlot
*slots
, int n_slots
);
123 static parallel_restore_result
parallel_restore(RestoreArgs
*args
);
124 static void mark_work_done(ArchiveHandle
*AH
, thandle worker
, int status
,
125 ParallelSlot
*slots
, int n_slots
);
126 static void fix_dependencies(ArchiveHandle
*AH
);
127 static bool has_lock_conflicts(TocEntry
*te1
, TocEntry
*te2
);
128 static void repoint_table_dependencies(ArchiveHandle
*AH
,
129 DumpId tableId
, DumpId tableDataId
);
130 static void identify_locking_dependencies(TocEntry
*te
,
131 TocEntry
**tocsByDumpId
);
132 static void reduce_dependencies(ArchiveHandle
*AH
, TocEntry
*te
);
133 static void mark_create_done(ArchiveHandle
*AH
, TocEntry
*te
);
134 static void inhibit_data_for_failed_table(ArchiveHandle
*AH
, TocEntry
*te
);
135 static ArchiveHandle
*CloneArchive(ArchiveHandle
*AH
);
136 static void DeCloneArchive(ArchiveHandle
*AH
);
142 * The objective it to make writing new formats and dumpers as simple
143 * as possible, if necessary at the expense of extra function calls etc.
148 /* Create a new archive */
151 CreateArchive(const char *FileSpec
, const ArchiveFormat fmt
,
152 const int compression
, ArchiveMode mode
)
155 ArchiveHandle
*AH
= _allocAH(FileSpec
, fmt
, compression
, mode
);
157 return (Archive
*) AH
;
160 /* Open an existing archive */
163 OpenArchive(const char *FileSpec
, const ArchiveFormat fmt
)
165 ArchiveHandle
*AH
= _allocAH(FileSpec
, fmt
, 0, archModeRead
);
167 return (Archive
*) AH
;
172 CloseArchive(Archive
*AHX
)
175 ArchiveHandle
*AH
= (ArchiveHandle
*) AHX
;
177 (*AH
->ClosePtr
) (AH
);
179 /* Close the output */
181 res
= GZCLOSE(AH
->OF
);
182 else if (AH
->OF
!= stdout
)
183 res
= fclose(AH
->OF
);
186 die_horribly(AH
, modulename
, "could not close output file: %s\n",
192 RestoreArchive(Archive
*AHX
, RestoreOptions
*ropt
)
194 ArchiveHandle
*AH
= (ArchiveHandle
*) AHX
;
200 AH
->stage
= STAGE_INITIALIZING
;
203 * Check for nonsensical option combinations.
205 * NB: create+dropSchema is useless because if you're creating the DB,
206 * there's no need to drop individual items in it. Moreover, if we tried
207 * to do that then we'd issue the drops in the database initially
208 * connected to, not the one we will create, which is very bad...
210 if (ropt
->create
&& ropt
->dropSchema
)
211 die_horribly(AH
, modulename
, "-C and -c are incompatible options\n");
214 * -1 is not compatible with -C, because we can't create a database inside
215 * a transaction block.
217 if (ropt
->create
&& ropt
->single_txn
)
218 die_horribly(AH
, modulename
, "-C and -1 are incompatible options\n");
221 * Make sure we won't need (de)compression we haven't got
224 if (AH
->compression
!= 0 && AH
->PrintTocDataPtr
!=NULL
)
226 for (te
= AH
->toc
->next
; te
!= AH
->toc
; te
= te
->next
)
228 reqs
= _tocEntryRequired(te
, ropt
, false);
229 if (te
->hadDumper
&& (reqs
& REQ_DATA
) != 0)
230 die_horribly(AH
, modulename
, "cannot restore from compressed archive (compression not supported in this installation)\n");
236 * If we're using a DB connection, then connect it.
240 ahlog(AH
, 1, "connecting to database for restore\n");
241 if (AH
->version
< K_VERS_1_3
)
242 die_horribly(AH
, modulename
, "direct database connections are not supported in pre-1.3 archives\n");
244 /* XXX Should get this from the archive */
245 AHX
->minRemoteVersion
= 070100;
246 AHX
->maxRemoteVersion
= 999999;
248 ConnectDatabase(AHX
, ropt
->dbname
,
249 ropt
->pghost
, ropt
->pgport
, ropt
->username
,
250 ropt
->promptPassword
);
253 * If we're talking to the DB directly, don't send comments since they
254 * obscure SQL when displaying errors
256 AH
->noTocComments
= 1;
260 * Work out if we have an implied data-only restore. This can happen if
261 * the dump was data only or if the user has used a toc list to exclude
262 * all of the schema data. All we do is look for schema entries - if none
263 * are found then we set the dataOnly flag.
265 * We could scan for wanted TABLE entries, but that is not the same as
266 * dataOnly. At this stage, it seems unnecessary (6-Mar-2001).
270 int impliedDataOnly
= 1;
272 for (te
= AH
->toc
->next
; te
!= AH
->toc
; te
= te
->next
)
274 reqs
= _tocEntryRequired(te
, ropt
, true);
275 if ((reqs
& REQ_SCHEMA
) != 0)
276 { /* It's schema, and it's wanted */
283 ropt
->dataOnly
= impliedDataOnly
;
284 ahlog(AH
, 1, "implied data-only restore\n");
289 * Setup the output file if necessary.
291 if (ropt
->filename
|| ropt
->compression
)
292 sav
= SetOutput(AH
, ropt
->filename
, ropt
->compression
);
294 ahprintf(AH
, "--\n-- PostgreSQL database dump\n--\n\n");
296 if (AH
->public.verbose
)
297 dumpTimestamp(AH
, "Started on", AH
->createDate
);
299 if (ropt
->single_txn
)
302 StartTransaction(AH
);
304 ahprintf(AH
, "BEGIN;\n\n");
308 * Establish important parameter values right away.
310 _doSetFixedOutputState(AH
);
312 AH
->stage
= STAGE_PROCESSING
;
315 * Drop the items at the start, in reverse order
317 if (ropt
->dropSchema
)
319 for (te
= AH
->toc
->prev
; te
!= AH
->toc
; te
= te
->prev
)
323 reqs
= _tocEntryRequired(te
, ropt
, false /* needn't drop ACLs */ );
324 if (((reqs
& REQ_SCHEMA
) != 0) && te
->dropStmt
)
326 /* We want the schema */
327 ahlog(AH
, 1, "dropping %s %s\n", te
->desc
, te
->tag
);
328 /* Select owner and schema as necessary */
329 _becomeOwner(AH
, te
);
330 _selectOutputSchema(AH
, te
->namespace);
332 ahprintf(AH
, "%s", te
->dropStmt
);
337 * _selectOutputSchema may have set currSchema to reflect the effect
338 * of a "SET search_path" command it emitted. However, by now we may
339 * have dropped that schema; or it might not have existed in the first
340 * place. In either case the effective value of search_path will not
341 * be what we think. Forcibly reset currSchema so that we will
342 * re-establish the search_path setting when needed (after creating
345 * If we treated users as pg_dump'able objects then we'd need to reset
349 free(AH
->currSchema
);
350 AH
->currSchema
= NULL
;
354 * In serial mode, we now process each non-ACL TOC entry.
356 * In parallel mode, turn control over to the parallel-restore logic.
358 if (ropt
->number_of_jobs
> 1 && ropt
->useDB
)
359 restore_toc_entries_parallel(AH
);
362 for (te
= AH
->toc
->next
; te
!= AH
->toc
; te
= te
->next
)
363 (void) restore_toc_entry(AH
, te
, ropt
, false);
367 * Scan TOC again to output ownership commands and ACLs
369 for (te
= AH
->toc
->next
; te
!= AH
->toc
; te
= te
->next
)
373 /* Work out what, if anything, we want from this entry */
374 reqs
= _tocEntryRequired(te
, ropt
, true);
376 if ((reqs
& REQ_SCHEMA
) != 0) /* We want the schema */
378 ahlog(AH
, 1, "setting owner and privileges for %s %s\n",
380 _printTocEntry(AH
, te
, ropt
, false, true);
384 if (ropt
->single_txn
)
387 CommitTransaction(AH
);
389 ahprintf(AH
, "COMMIT;\n\n");
392 if (AH
->public.verbose
)
393 dumpTimestamp(AH
, "Completed on", time(NULL
));
395 ahprintf(AH
, "--\n-- PostgreSQL database dump complete\n--\n\n");
398 * Clean up & we're done.
400 AH
->stage
= STAGE_FINALIZING
;
402 if (ropt
->filename
|| ropt
->compression
)
403 ResetOutput(AH
, sav
);
407 PQfinish(AH
->connection
);
408 AH
->connection
= NULL
;
413 * Restore a single TOC item. Used in both parallel and non-parallel restore;
414 * is_parallel is true if we are in a worker child process.
416 * Returns 0 normally, but WORKER_CREATE_DONE or WORKER_INHIBIT_DATA if
417 * the parallel parent has to make the corresponding status update.
420 restore_toc_entry(ArchiveHandle
*AH
, TocEntry
*te
,
421 RestoreOptions
*ropt
, bool is_parallel
)
429 /* Work out what, if anything, we want from this entry */
430 reqs
= _tocEntryRequired(te
, ropt
, false);
432 /* Dump any relevant dump warnings to stderr */
433 if (!ropt
->suppressDumpWarnings
&& strcmp(te
->desc
, "WARNING") == 0)
435 if (!ropt
->dataOnly
&& te
->defn
!= NULL
&& strlen(te
->defn
) != 0)
436 write_msg(modulename
, "warning from original dump file: %s\n", te
->defn
);
437 else if (te
->copyStmt
!= NULL
&& strlen(te
->copyStmt
) != 0)
438 write_msg(modulename
, "warning from original dump file: %s\n", te
->copyStmt
);
443 if ((reqs
& REQ_SCHEMA
) != 0) /* We want the schema */
445 ahlog(AH
, 1, "creating %s %s\n", te
->desc
, te
->tag
);
447 _printTocEntry(AH
, te
, ropt
, false, false);
450 if (strcmp(te
->desc
, "TABLE") == 0)
452 if (AH
->lastErrorTE
== te
)
455 * We failed to create the table. If
456 * --no-data-for-failed-tables was given, mark the
457 * corresponding TABLE DATA to be ignored.
459 * In the parallel case this must be done in the parent, so we
460 * just set the return value.
462 if (ropt
->noDataForFailedTables
)
465 retval
= WORKER_INHIBIT_DATA
;
467 inhibit_data_for_failed_table(AH
, te
);
473 * We created the table successfully. Mark the corresponding
474 * TABLE DATA for possible truncation.
476 * In the parallel case this must be done in the parent, so we
477 * just set the return value.
480 retval
= WORKER_CREATE_DONE
;
482 mark_create_done(AH
, te
);
486 /* If we created a DB, connect to it... */
487 if (strcmp(te
->desc
, "DATABASE") == 0)
489 ahlog(AH
, 1, "connecting to new database \"%s\"\n", te
->tag
);
490 _reconnectToDB(AH
, te
->tag
);
491 ropt
->dbname
= strdup(te
->tag
);
496 * If we have a data component, then process it
498 if ((reqs
& REQ_DATA
) != 0)
501 * hadDumper will be set if there is genuine data component for this
502 * node. Otherwise, we need to check the defn field for statements
503 * that need to be executed in data-only restores.
508 * If we can output the data, then restore it.
510 if (AH
->PrintTocDataPtr
!=NULL
&& (reqs
& REQ_DATA
) != 0)
512 _printTocEntry(AH
, te
, ropt
, true, false);
514 if (strcmp(te
->desc
, "BLOBS") == 0 ||
515 strcmp(te
->desc
, "BLOB COMMENTS") == 0)
517 ahlog(AH
, 1, "restoring %s\n", te
->desc
);
519 _selectOutputSchema(AH
, "pg_catalog");
521 (*AH
->PrintTocDataPtr
) (AH
, te
, ropt
);
525 _disableTriggersIfNecessary(AH
, te
, ropt
);
527 /* Select owner and schema as necessary */
528 _becomeOwner(AH
, te
);
529 _selectOutputSchema(AH
, te
->namespace);
531 ahlog(AH
, 1, "restoring data for table \"%s\"\n",
535 * In parallel restore, if we created the table earlier in
536 * the run then we wrap the COPY in a transaction and
537 * precede it with a TRUNCATE. If archiving is not on
538 * this prevents WAL-logging the COPY. This obtains a
539 * speedup similar to that from using single_txn mode in
540 * non-parallel restores.
542 if (is_parallel
&& te
->created
)
545 * Parallel restore is always talking directly to a
546 * server, so no need to see if we should issue BEGIN.
548 StartTransaction(AH
);
551 * If the server version is >= 8.4, make sure we issue
552 * TRUNCATE with ONLY so that child tables are not
555 ahprintf(AH
, "TRUNCATE TABLE %s%s;\n\n",
556 (PQserverVersion(AH
->connection
) >= 80400 ?
562 * If we have a copy statement, use it. As of V1.3, these
563 * are separate to allow easy import from withing a
564 * database connection. Pre 1.3 archives can not use DB
565 * connections and are sent to output only.
567 * For V1.3+, the table data MUST have a copy statement so
568 * that we can go into appropriate mode with libpq.
570 if (te
->copyStmt
&& strlen(te
->copyStmt
) > 0)
572 ahprintf(AH
, "%s", te
->copyStmt
);
573 AH
->writingCopyData
= true;
576 (*AH
->PrintTocDataPtr
) (AH
, te
, ropt
);
578 AH
->writingCopyData
= false;
580 /* close out the transaction started above */
581 if (is_parallel
&& te
->created
)
582 CommitTransaction(AH
);
584 _enableTriggersIfNecessary(AH
, te
, ropt
);
588 else if (!defnDumped
)
590 /* If we haven't already dumped the defn part, do so now */
591 ahlog(AH
, 1, "executing %s %s\n", te
->desc
, te
->tag
);
592 _printTocEntry(AH
, te
, ropt
, false, false);
600 * Allocate a new RestoreOptions block.
601 * This is mainly so we can initialize it, but also for future expansion,
604 NewRestoreOptions(void)
606 RestoreOptions
*opts
;
608 opts
= (RestoreOptions
*) calloc(1, sizeof(RestoreOptions
));
610 /* set any fields that shouldn't default to zeroes */
611 opts
->format
= archUnknown
;
612 opts
->promptPassword
= TRI_DEFAULT
;
618 _disableTriggersIfNecessary(ArchiveHandle
*AH
, TocEntry
*te
, RestoreOptions
*ropt
)
620 /* This hack is only needed in a data-only restore */
621 if (!ropt
->dataOnly
|| !ropt
->disable_triggers
)
624 ahlog(AH
, 1, "disabling triggers for %s\n", te
->tag
);
627 * Become superuser if possible, since they are the only ones who can
628 * disable constraint triggers. If -S was not given, assume the initial
629 * user identity is a superuser. (XXX would it be better to become the
632 _becomeUser(AH
, ropt
->superuser
);
637 _selectOutputSchema(AH
, te
->namespace);
639 ahprintf(AH
, "ALTER TABLE %s DISABLE TRIGGER ALL;\n\n",
644 _enableTriggersIfNecessary(ArchiveHandle
*AH
, TocEntry
*te
, RestoreOptions
*ropt
)
646 /* This hack is only needed in a data-only restore */
647 if (!ropt
->dataOnly
|| !ropt
->disable_triggers
)
650 ahlog(AH
, 1, "enabling triggers for %s\n", te
->tag
);
653 * Become superuser if possible, since they are the only ones who can
654 * disable constraint triggers. If -S was not given, assume the initial
655 * user identity is a superuser. (XXX would it be better to become the
658 _becomeUser(AH
, ropt
->superuser
);
663 _selectOutputSchema(AH
, te
->namespace);
665 ahprintf(AH
, "ALTER TABLE %s ENABLE TRIGGER ALL;\n\n",
670 * This is a routine that is part of the dumper interface, hence the 'Archive*' parameter.
675 WriteData(Archive
*AHX
, const void *data
, size_t dLen
)
677 ArchiveHandle
*AH
= (ArchiveHandle
*) AHX
;
680 die_horribly(AH
, modulename
, "internal error -- WriteData cannot be called outside the context of a DataDumper routine\n");
682 return (*AH
->WriteDataPtr
) (AH
, data
, dLen
);
686 * Create a new TOC entry. The TOC was designed as a TOC, but is now the
687 * repository for all metadata. But the name has stuck.
692 ArchiveEntry(Archive
*AHX
,
693 CatalogId catalogId
, DumpId dumpId
,
695 const char *namespace,
696 const char *tablespace
,
697 const char *owner
, bool withOids
,
698 const char *desc
, teSection section
,
700 const char *dropStmt
, const char *copyStmt
,
701 const DumpId
*deps
, int nDeps
,
702 DataDumperPtr dumpFn
, void *dumpArg
)
704 ArchiveHandle
*AH
= (ArchiveHandle
*) AHX
;
707 newToc
= (TocEntry
*) calloc(1, sizeof(TocEntry
));
709 die_horribly(AH
, modulename
, "out of memory\n");
712 if (dumpId
> AH
->maxDumpId
)
713 AH
->maxDumpId
= dumpId
;
715 newToc
->prev
= AH
->toc
->prev
;
716 newToc
->next
= AH
->toc
;
717 AH
->toc
->prev
->next
= newToc
;
718 AH
->toc
->prev
= newToc
;
720 newToc
->catalogId
= catalogId
;
721 newToc
->dumpId
= dumpId
;
722 newToc
->section
= section
;
724 newToc
->tag
= strdup(tag
);
725 newToc
->namespace = namespace ? strdup(namespace) : NULL
;
726 newToc
->tablespace
= tablespace
? strdup(tablespace
) : NULL
;
727 newToc
->owner
= strdup(owner
);
728 newToc
->withOids
= withOids
;
729 newToc
->desc
= strdup(desc
);
730 newToc
->defn
= strdup(defn
);
731 newToc
->dropStmt
= strdup(dropStmt
);
732 newToc
->copyStmt
= copyStmt
? strdup(copyStmt
) : NULL
;
736 newToc
->dependencies
= (DumpId
*) malloc(nDeps
* sizeof(DumpId
));
737 memcpy(newToc
->dependencies
, deps
, nDeps
* sizeof(DumpId
));
738 newToc
->nDeps
= nDeps
;
742 newToc
->dependencies
= NULL
;
746 newToc
->dataDumper
= dumpFn
;
747 newToc
->dataDumperArg
= dumpArg
;
748 newToc
->hadDumper
= dumpFn
? true : false;
750 newToc
->formatData
= NULL
;
752 if (AH
->ArchiveEntryPtr
!=NULL
)
753 (*AH
->ArchiveEntryPtr
) (AH
, newToc
);
758 PrintTOCSummary(Archive
*AHX
, RestoreOptions
*ropt
)
760 ArchiveHandle
*AH
= (ArchiveHandle
*) AHX
;
766 sav
= SetOutput(AH
, ropt
->filename
, 0 /* no compression */ );
768 ahprintf(AH
, ";\n; Archive created at %s", ctime(&AH
->createDate
));
769 ahprintf(AH
, "; dbname: %s\n; TOC Entries: %d\n; Compression: %d\n",
770 AH
->archdbname
, AH
->tocCount
, AH
->compression
);
787 ahprintf(AH
, "; Dump Version: %d.%d-%d\n", AH
->vmaj
, AH
->vmin
, AH
->vrev
);
788 ahprintf(AH
, "; Format: %s\n", fmtName
);
789 ahprintf(AH
, "; Integer: %d bytes\n", (int) AH
->intSize
);
790 ahprintf(AH
, "; Offset: %d bytes\n", (int) AH
->offSize
);
791 if (AH
->archiveRemoteVersion
)
792 ahprintf(AH
, "; Dumped from database version: %s\n",
793 AH
->archiveRemoteVersion
);
794 if (AH
->archiveDumpVersion
)
795 ahprintf(AH
, "; Dumped by pg_dump version: %s\n",
796 AH
->archiveDumpVersion
);
798 ahprintf(AH
, ";\n;\n; Selected TOC Entries:\n;\n");
800 for (te
= AH
->toc
->next
; te
!= AH
->toc
; te
= te
->next
)
802 if (ropt
->verbose
|| _tocEntryRequired(te
, ropt
, true) != 0)
803 ahprintf(AH
, "%d; %u %u %s %s %s %s\n", te
->dumpId
,
804 te
->catalogId
.tableoid
, te
->catalogId
.oid
,
805 te
->desc
, te
->namespace ? te
->namespace : "-",
807 if (ropt
->verbose
&& te
->nDeps
> 0)
811 ahprintf(AH
, ";\tdepends on:");
812 for (i
= 0; i
< te
->nDeps
; i
++)
813 ahprintf(AH
, " %d", te
->dependencies
[i
]);
819 ResetOutput(AH
, sav
);
826 /* Called by a dumper to signal start of a BLOB */
828 StartBlob(Archive
*AHX
, Oid oid
)
830 ArchiveHandle
*AH
= (ArchiveHandle
*) AHX
;
832 if (!AH
->StartBlobPtr
)
833 die_horribly(AH
, modulename
, "large-object output not supported in chosen format\n");
835 (*AH
->StartBlobPtr
) (AH
, AH
->currToc
, oid
);
840 /* Called by a dumper to signal end of a BLOB */
842 EndBlob(Archive
*AHX
, Oid oid
)
844 ArchiveHandle
*AH
= (ArchiveHandle
*) AHX
;
847 (*AH
->EndBlobPtr
) (AH
, AH
->currToc
, oid
);
857 * Called by a format handler before any blobs are restored
860 StartRestoreBlobs(ArchiveHandle
*AH
)
862 if (!AH
->ropt
->single_txn
)
865 StartTransaction(AH
);
867 ahprintf(AH
, "BEGIN;\n\n");
874 * Called by a format handler after all blobs are restored
877 EndRestoreBlobs(ArchiveHandle
*AH
)
879 if (!AH
->ropt
->single_txn
)
882 CommitTransaction(AH
);
884 ahprintf(AH
, "COMMIT;\n\n");
887 ahlog(AH
, 1, ngettext("restored %d large object\n",
888 "restored %d large objects\n",
895 * Called by a format handler to initiate restoration of a blob
898 StartRestoreBlob(ArchiveHandle
*AH
, Oid oid
)
904 /* Initialize the LO Buffer */
907 ahlog(AH
, 2, "restoring large object with OID %u\n", oid
);
911 loOid
= lo_create(AH
->connection
, oid
);
912 if (loOid
== 0 || loOid
!= oid
)
913 die_horribly(AH
, modulename
, "could not create large object %u\n",
916 AH
->loFd
= lo_open(AH
->connection
, oid
, INV_WRITE
);
918 die_horribly(AH
, modulename
, "could not open large object\n");
922 ahprintf(AH
, "SELECT lo_open(lo_create(%u), %d);\n", oid
, INV_WRITE
);
929 EndRestoreBlob(ArchiveHandle
*AH
, Oid oid
)
931 if (AH
->lo_buf_used
> 0)
933 /* Write remaining bytes from the LO buffer */
941 lo_close(AH
->connection
, AH
->loFd
);
946 ahprintf(AH
, "SELECT lo_close(0);\n\n");
951 * Sorting and Reordering
955 SortTocFromFile(Archive
*AHX
, RestoreOptions
*ropt
)
957 ArchiveHandle
*AH
= (ArchiveHandle
*) AHX
;
966 /* Allocate space for the 'wanted' array, and init it */
967 ropt
->idWanted
= (bool *) malloc(sizeof(bool) * AH
->maxDumpId
);
968 memset(ropt
->idWanted
, 0, sizeof(bool) * AH
->maxDumpId
);
970 /* Set prev entry as head of list */
974 fh
= fopen(ropt
->tocFile
, PG_BINARY_R
);
976 die_horribly(AH
, modulename
, "could not open TOC file \"%s\": %s\n",
977 ropt
->tocFile
, strerror(errno
));
979 while (fgets(buf
, sizeof(buf
), fh
) != NULL
)
981 /* Truncate line at comment, if any */
982 cmnt
= strchr(buf
, ';');
986 /* Ignore if all blank */
987 if (strspn(buf
, " \t\r") == strlen(buf
))
990 /* Get an ID, check it's valid and not already seen */
991 id
= strtol(buf
, &endptr
, 10);
992 if (endptr
== buf
|| id
<= 0 || id
> AH
->maxDumpId
||
993 ropt
->idWanted
[id
- 1])
995 write_msg(modulename
, "WARNING: line ignored: %s\n", buf
);
1000 te
= getTocEntryByDumpId(AH
, id
);
1002 die_horribly(AH
, modulename
, "could not find entry for ID %d\n",
1005 ropt
->idWanted
[id
- 1] = true;
1007 _moveAfter(AH
, tePrev
, te
);
1011 if (fclose(fh
) != 0)
1012 die_horribly(AH
, modulename
, "could not close TOC file: %s\n",
1017 * Set up a dummy ID filter that selects all dump IDs
1020 InitDummyWantedList(Archive
*AHX
, RestoreOptions
*ropt
)
1022 ArchiveHandle
*AH
= (ArchiveHandle
*) AHX
;
1024 /* Allocate space for the 'wanted' array, and init it to 1's */
1025 ropt
->idWanted
= (bool *) malloc(sizeof(bool) * AH
->maxDumpId
);
1026 memset(ropt
->idWanted
, 1, sizeof(bool) * AH
->maxDumpId
);
1029 /**********************
1030 * 'Convenience functions that look like standard IO functions
1031 * for writing data when in dump mode.
1032 **********************/
1036 archputs(const char *s
, Archive
*AH
)
1038 return WriteData(AH
, s
, strlen(s
));
1043 archprintf(Archive
*AH
, const char *fmt
,...)
1047 int bSize
= strlen(fmt
) + 256;
1051 * This is paranoid: deal with the possibility that vsnprintf is willing
1052 * to ignore trailing null or returns > 0 even if string does not fit. It
1053 * may be the case that it returns cnt = bufsize
1055 while (cnt
< 0 || cnt
>= (bSize
- 1))
1060 p
= (char *) malloc(bSize
);
1062 exit_horribly(AH
, modulename
, "out of memory\n");
1064 cnt
= vsnprintf(p
, bSize
, fmt
, ap
);
1067 WriteData(AH
, p
, cnt
);
1073 /*******************************
1074 * Stuff below here should be 'private' to the archiver routines
1075 *******************************/
1077 static OutputContext
1078 SetOutput(ArchiveHandle
*AH
, char *filename
, int compression
)
1083 /* Replace the AH output file handle */
1085 sav
.gzOut
= AH
->gzOut
;
1090 fn
= fileno(AH
->FH
);
1094 filename
= AH
->fSpec
;
1097 fn
= fileno(stdout
);
1099 /* If compression explicitly requested, use gzopen */
1101 if (compression
!= 0)
1105 /* Don't use PG_BINARY_x since this is zlib */
1106 sprintf(fmode
, "wb%d", compression
);
1108 AH
->OF
= gzdopen(dup(fn
), fmode
);
1110 AH
->OF
= gzopen(filename
, fmode
);
1116 if (AH
->mode
== archModeAppend
)
1119 AH
->OF
= fdopen(dup(fn
), PG_BINARY_A
);
1121 AH
->OF
= fopen(filename
, PG_BINARY_A
);
1126 AH
->OF
= fdopen(dup(fn
), PG_BINARY_W
);
1128 AH
->OF
= fopen(filename
, PG_BINARY_W
);
1136 die_horribly(AH
, modulename
, "could not open output file \"%s\": %s\n",
1137 filename
, strerror(errno
));
1139 die_horribly(AH
, modulename
, "could not open output file: %s\n",
1147 ResetOutput(ArchiveHandle
*AH
, OutputContext sav
)
1152 res
= GZCLOSE(AH
->OF
);
1154 res
= fclose(AH
->OF
);
1157 die_horribly(AH
, modulename
, "could not close output file: %s\n",
1160 AH
->gzOut
= sav
.gzOut
;
1167 * Print formatted text to the output file (usually stdout).
1170 ahprintf(ArchiveHandle
*AH
, const char *fmt
,...)
1174 int bSize
= strlen(fmt
) + 256; /* Should be enough */
1178 * This is paranoid: deal with the possibility that vsnprintf is willing
1179 * to ignore trailing null
1183 * or returns > 0 even if string does not fit. It may be the case that it
1184 * returns cnt = bufsize
1186 while (cnt
< 0 || cnt
>= (bSize
- 1))
1191 p
= (char *) malloc(bSize
);
1193 die_horribly(AH
, modulename
, "out of memory\n");
1195 cnt
= vsnprintf(p
, bSize
, fmt
, ap
);
1198 ahwrite(p
, 1, cnt
, AH
);
1204 ahlog(ArchiveHandle
*AH
, int level
, const char *fmt
,...)
1208 if (AH
->debugLevel
< level
&& (!AH
->public.verbose
|| level
> 1))
1212 _write_msg(NULL
, fmt
, ap
);
1217 * Single place for logic which says 'We are restoring to a direct DB connection'.
1220 RestoringToDB(ArchiveHandle
*AH
)
1222 return (AH
->ropt
&& AH
->ropt
->useDB
&& AH
->connection
);
1226 * Dump the current contents of the LO data buffer while writing a BLOB
1229 dump_lo_buf(ArchiveHandle
*AH
)
1235 res
= lo_write(AH
->connection
, AH
->loFd
, AH
->lo_buf
, AH
->lo_buf_used
);
1236 ahlog(AH
, 5, ngettext("wrote %lu byte of large object data (result = %lu)\n",
1237 "wrote %lu bytes of large object data (result = %lu)\n",
1239 (unsigned long) AH
->lo_buf_used
, (unsigned long) res
);
1240 if (res
!= AH
->lo_buf_used
)
1241 die_horribly(AH
, modulename
,
1242 "could not write to large object (result: %lu, expected: %lu)\n",
1243 (unsigned long) res
, (unsigned long) AH
->lo_buf_used
);
1250 str
= PQescapeBytea((const unsigned char *) AH
->lo_buf
,
1251 AH
->lo_buf_used
, &len
);
1253 die_horribly(AH
, modulename
, "out of memory\n");
1255 /* Hack: turn off writingBlob so ahwrite doesn't recurse to here */
1256 AH
->writingBlob
= 0;
1257 ahprintf(AH
, "SELECT lowrite(0, '%s');\n", str
);
1258 AH
->writingBlob
= 1;
1262 AH
->lo_buf_used
= 0;
1267 * Write buffer to the output file (usually stdout). This is user for
1268 * outputting 'restore' scripts etc. It is even possible for an archive
1269 * format to create a custom output routine to 'fake' a restore if it
1270 * wants to generate a script (see TAR output).
1273 ahwrite(const void *ptr
, size_t size
, size_t nmemb
, ArchiveHandle
*AH
)
1277 if (AH
->writingBlob
)
1279 size_t remaining
= size
* nmemb
;
1281 while (AH
->lo_buf_used
+ remaining
> AH
->lo_buf_size
)
1283 size_t avail
= AH
->lo_buf_size
- AH
->lo_buf_used
;
1285 memcpy((char *) AH
->lo_buf
+ AH
->lo_buf_used
, ptr
, avail
);
1286 ptr
= (const void *) ((const char *) ptr
+ avail
);
1288 AH
->lo_buf_used
+= avail
;
1292 memcpy((char *) AH
->lo_buf
+ AH
->lo_buf_used
, ptr
, remaining
);
1293 AH
->lo_buf_used
+= remaining
;
1295 return size
* nmemb
;
1299 res
= GZWRITE((void *) ptr
, size
, nmemb
, AH
->OF
);
1300 if (res
!= (nmemb
* size
))
1301 die_horribly(AH
, modulename
, "could not write to output file: %s\n", strerror(errno
));
1304 else if (AH
->CustomOutPtr
)
1306 res
= AH
->CustomOutPtr (AH
, ptr
, size
* nmemb
);
1308 if (res
!= (nmemb
* size
))
1309 die_horribly(AH
, modulename
, "could not write to custom output routine\n");
1315 * If we're doing a restore, and it's direct to DB, and we're
1316 * connected then send it to the DB.
1318 if (RestoringToDB(AH
))
1319 return ExecuteSqlCommandBuf(AH
, (void *) ptr
, size
* nmemb
); /* Always 1, currently */
1322 res
= fwrite((void *) ptr
, size
, nmemb
, AH
->OF
);
1324 die_horribly(AH
, modulename
, "could not write to output file: %s\n",
1331 /* Common exit code */
1333 _write_msg(const char *modulename
, const char *fmt
, va_list ap
)
1336 fprintf(stderr
, "%s: [%s] ", progname
, _(modulename
));
1338 fprintf(stderr
, "%s: ", progname
);
1339 vfprintf(stderr
, _(fmt
), ap
);
1343 write_msg(const char *modulename
, const char *fmt
,...)
1348 _write_msg(modulename
, fmt
, ap
);
1354 _die_horribly(ArchiveHandle
*AH
, const char *modulename
, const char *fmt
, va_list ap
)
1356 _write_msg(modulename
, fmt
, ap
);
1360 if (AH
->public.verbose
)
1361 write_msg(NULL
, "*** aborted because of error\n");
1363 PQfinish(AH
->connection
);
1371 exit_horribly(Archive
*AH
, const char *modulename
, const char *fmt
,...)
1376 _die_horribly((ArchiveHandle
*) AH
, modulename
, fmt
, ap
);
1380 /* Archiver use (just different arg declaration) */
1382 die_horribly(ArchiveHandle
*AH
, const char *modulename
, const char *fmt
,...)
1387 _die_horribly(AH
, modulename
, fmt
, ap
);
1391 /* on some error, we may decide to go on... */
1393 warn_or_die_horribly(ArchiveHandle
*AH
,
1394 const char *modulename
, const char *fmt
,...)
1402 /* Do nothing special */
1405 case STAGE_INITIALIZING
:
1406 if (AH
->stage
!= AH
->lastErrorStage
)
1407 write_msg(modulename
, "Error while INITIALIZING:\n");
1410 case STAGE_PROCESSING
:
1411 if (AH
->stage
!= AH
->lastErrorStage
)
1412 write_msg(modulename
, "Error while PROCESSING TOC:\n");
1415 case STAGE_FINALIZING
:
1416 if (AH
->stage
!= AH
->lastErrorStage
)
1417 write_msg(modulename
, "Error while FINALIZING:\n");
1420 if (AH
->currentTE
!= NULL
&& AH
->currentTE
!= AH
->lastErrorTE
)
1422 write_msg(modulename
, "Error from TOC entry %d; %u %u %s %s %s\n",
1423 AH
->currentTE
->dumpId
,
1424 AH
->currentTE
->catalogId
.tableoid
, AH
->currentTE
->catalogId
.oid
,
1425 AH
->currentTE
->desc
, AH
->currentTE
->tag
, AH
->currentTE
->owner
);
1427 AH
->lastErrorStage
= AH
->stage
;
1428 AH
->lastErrorTE
= AH
->currentTE
;
1431 if (AH
->public.exit_on_error
)
1432 _die_horribly(AH
, modulename
, fmt
, ap
);
1435 _write_msg(modulename
, fmt
, ap
);
1436 AH
->public.n_errors
++;
1442 _moveAfter(ArchiveHandle
*AH
, TocEntry
*pos
, TocEntry
*te
)
1444 te
->prev
->next
= te
->next
;
1445 te
->next
->prev
= te
->prev
;
1448 te
->next
= pos
->next
;
1450 pos
->next
->prev
= te
;
1457 _moveBefore(ArchiveHandle
*AH
, TocEntry
*pos
, TocEntry
*te
)
1459 te
->prev
->next
= te
->next
;
1460 te
->next
->prev
= te
->prev
;
1462 te
->prev
= pos
->prev
;
1464 pos
->prev
->next
= te
;
1470 getTocEntryByDumpId(ArchiveHandle
*AH
, DumpId id
)
1474 for (te
= AH
->toc
->next
; te
!= AH
->toc
; te
= te
->next
)
1476 if (te
->dumpId
== id
)
1483 TocIDRequired(ArchiveHandle
*AH
, DumpId id
, RestoreOptions
*ropt
)
1485 TocEntry
*te
= getTocEntryByDumpId(AH
, id
);
1490 return _tocEntryRequired(te
, ropt
, true);
1494 WriteOffset(ArchiveHandle
*AH
, pgoff_t o
, int wasSet
)
1499 (*AH
->WriteBytePtr
) (AH
, wasSet
);
1501 /* Write out pgoff_t smallest byte first, prevents endian mismatch */
1502 for (off
= 0; off
< sizeof(pgoff_t
); off
++)
1504 (*AH
->WriteBytePtr
) (AH
, o
& 0xFF);
1507 return sizeof(pgoff_t
) + 1;
1511 ReadOffset(ArchiveHandle
*AH
, pgoff_t
* o
)
1517 /* Initialize to zero */
1520 /* Check for old version */
1521 if (AH
->version
< K_VERS_1_7
)
1523 /* Prior versions wrote offsets using WriteInt */
1525 /* -1 means not set */
1527 return K_OFFSET_POS_NOT_SET
;
1529 return K_OFFSET_NO_DATA
;
1531 /* Cast to pgoff_t because it was written as an int. */
1533 return K_OFFSET_POS_SET
;
1537 * Read the flag indicating the state of the data pointer. Check if valid
1540 * This used to be handled by a negative or zero pointer, now we use an
1541 * extra byte specifically for the state.
1543 offsetFlg
= (*AH
->ReadBytePtr
) (AH
) & 0xFF;
1547 case K_OFFSET_POS_NOT_SET
:
1548 case K_OFFSET_NO_DATA
:
1549 case K_OFFSET_POS_SET
:
1554 die_horribly(AH
, modulename
, "unexpected data offset flag %d\n", offsetFlg
);
1560 for (off
= 0; off
< AH
->offSize
; off
++)
1562 if (off
< sizeof(pgoff_t
))
1563 *o
|= ((pgoff_t
) ((*AH
->ReadBytePtr
) (AH
))) << (off
* 8);
1566 if ((*AH
->ReadBytePtr
) (AH
) != 0)
1567 die_horribly(AH
, modulename
, "file offset in dump file is too large\n");
1575 WriteInt(ArchiveHandle
*AH
, int i
)
1580 * This is a bit yucky, but I don't want to make the binary format very
1581 * dependent on representation, and not knowing much about it, I write out
1582 * a sign byte. If you change this, don't forget to change the file
1583 * version #, and modify readInt to read the new format AS WELL AS the old
1590 (*AH
->WriteBytePtr
) (AH
, 1);
1594 (*AH
->WriteBytePtr
) (AH
, 0);
1596 for (b
= 0; b
< AH
->intSize
; b
++)
1598 (*AH
->WriteBytePtr
) (AH
, i
& 0xFF);
1602 return AH
->intSize
+ 1;
1606 ReadInt(ArchiveHandle
*AH
)
1611 int sign
= 0; /* Default positive */
1614 if (AH
->version
> K_VERS_1_0
)
1615 /* Read a sign byte */
1616 sign
= (*AH
->ReadBytePtr
) (AH
);
1618 for (b
= 0; b
< AH
->intSize
; b
++)
1620 bv
= (*AH
->ReadBytePtr
) (AH
) & 0xFF;
1622 res
= res
+ (bv
<< bitShift
);
1633 WriteStr(ArchiveHandle
*AH
, const char *c
)
1639 res
= WriteInt(AH
, strlen(c
));
1640 res
+= (*AH
->WriteBufPtr
) (AH
, c
, strlen(c
));
1643 res
= WriteInt(AH
, -1);
1649 ReadStr(ArchiveHandle
*AH
)
1659 buf
= (char *) malloc(l
+ 1);
1661 die_horribly(AH
, modulename
, "out of memory\n");
1663 if ((*AH
->ReadBufPtr
) (AH
, (void *) buf
, l
) != l
)
1664 die_horribly(AH
, modulename
, "unexpected end of file\n");
1673 _discoverArchiveFormat(ArchiveHandle
*AH
)
1676 char sig
[6]; /* More than enough */
1681 write_msg(modulename
, "attempting to ascertain archive format\n");
1685 free(AH
->lookahead
);
1687 AH
->lookaheadSize
= 512;
1688 AH
->lookahead
= calloc(1, 512);
1689 AH
->lookaheadLen
= 0;
1690 AH
->lookaheadPos
= 0;
1695 fh
= fopen(AH
->fSpec
, PG_BINARY_R
);
1697 die_horribly(AH
, modulename
, "could not open input file \"%s\": %s\n",
1698 AH
->fSpec
, strerror(errno
));
1704 die_horribly(AH
, modulename
, "could not open input file: %s\n",
1708 cnt
= fread(sig
, 1, 5, fh
);
1713 die_horribly(AH
, modulename
, "could not read input file: %s\n", strerror(errno
));
1715 die_horribly(AH
, modulename
, "input file is too short (read %lu, expected 5)\n",
1716 (unsigned long) cnt
);
1719 /* Save it, just in case we need it later */
1720 strncpy(&AH
->lookahead
[0], sig
, 5);
1721 AH
->lookaheadLen
= 5;
1723 if (strncmp(sig
, "PGDMP", 5) == 0)
1725 AH
->vmaj
= fgetc(fh
);
1726 AH
->vmin
= fgetc(fh
);
1728 /* Save these too... */
1729 AH
->lookahead
[AH
->lookaheadLen
++] = AH
->vmaj
;
1730 AH
->lookahead
[AH
->lookaheadLen
++] = AH
->vmin
;
1732 /* Check header version; varies from V1.0 */
1733 if (AH
->vmaj
> 1 || ((AH
->vmaj
== 1) && (AH
->vmin
> 0))) /* Version > 1.0 */
1735 AH
->vrev
= fgetc(fh
);
1736 AH
->lookahead
[AH
->lookaheadLen
++] = AH
->vrev
;
1741 /* Make a convenient integer <maj><min><rev>00 */
1742 AH
->version
= ((AH
->vmaj
* 256 + AH
->vmin
) * 256 + AH
->vrev
) * 256 + 0;
1744 AH
->intSize
= fgetc(fh
);
1745 AH
->lookahead
[AH
->lookaheadLen
++] = AH
->intSize
;
1747 if (AH
->version
>= K_VERS_1_7
)
1749 AH
->offSize
= fgetc(fh
);
1750 AH
->lookahead
[AH
->lookaheadLen
++] = AH
->offSize
;
1753 AH
->offSize
= AH
->intSize
;
1755 AH
->format
= fgetc(fh
);
1756 AH
->lookahead
[AH
->lookaheadLen
++] = AH
->format
;
1761 * *Maybe* we have a tar archive format file... So, read first 512
1764 cnt
= fread(&AH
->lookahead
[AH
->lookaheadLen
], 1, 512 - AH
->lookaheadLen
, fh
);
1765 AH
->lookaheadLen
+= cnt
;
1767 if (AH
->lookaheadLen
!= 512)
1768 die_horribly(AH
, modulename
, "input file does not appear to be a valid archive (too short?)\n");
1770 if (!isValidTarHeader(AH
->lookahead
))
1771 die_horribly(AH
, modulename
, "input file does not appear to be a valid archive\n");
1773 AH
->format
= archTar
;
1776 /* If we can't seek, then mark the header as read */
1777 if (fseeko(fh
, 0, SEEK_SET
) != 0)
1780 * NOTE: Formats that use the lookahead buffer can unset this in their
1786 AH
->lookaheadLen
= 0; /* Don't bother since we've reset the file */
1788 /* Close the file */
1790 if (fclose(fh
) != 0)
1791 die_horribly(AH
, modulename
, "could not close input file: %s\n",
1799 * Allocate an archive handle
1801 static ArchiveHandle
*
1802 _allocAH(const char *FileSpec
, const ArchiveFormat fmt
,
1803 const int compression
, ArchiveMode mode
)
1808 write_msg(modulename
, "allocating AH for %s, format %d\n", FileSpec
, fmt
);
1811 AH
= (ArchiveHandle
*) calloc(1, sizeof(ArchiveHandle
));
1813 die_horribly(AH
, modulename
, "out of memory\n");
1815 /* AH->debugLevel = 100; */
1817 AH
->vmaj
= K_VERS_MAJOR
;
1818 AH
->vmin
= K_VERS_MINOR
;
1819 AH
->vrev
= K_VERS_REV
;
1821 /* initialize for backwards compatible string processing */
1822 AH
->public.encoding
= 0; /* PG_SQL_ASCII */
1823 AH
->public.std_strings
= false;
1825 /* sql error handling */
1826 AH
->public.exit_on_error
= true;
1827 AH
->public.n_errors
= 0;
1829 AH
->createDate
= time(NULL
);
1831 AH
->intSize
= sizeof(int);
1832 AH
->offSize
= sizeof(pgoff_t
);
1835 AH
->fSpec
= strdup(FileSpec
);
1838 * Not used; maybe later....
1840 * AH->workDir = strdup(FileSpec); for(i=strlen(FileSpec) ; i > 0 ;
1841 * i--) if (AH->workDir[i-1] == '/')
1847 AH
->currUser
= NULL
; /* unknown */
1848 AH
->currSchema
= NULL
; /* ditto */
1849 AH
->currTablespace
= NULL
; /* ditto */
1850 AH
->currWithOids
= -1; /* force SET */
1852 AH
->toc
= (TocEntry
*) calloc(1, sizeof(TocEntry
));
1854 die_horribly(AH
, modulename
, "out of memory\n");
1856 AH
->toc
->next
= AH
->toc
;
1857 AH
->toc
->prev
= AH
->toc
;
1860 AH
->compression
= compression
;
1862 AH
->pgCopyBuf
= createPQExpBuffer();
1863 AH
->sqlBuf
= createPQExpBuffer();
1865 /* Open stdout with no compression for AH output handle */
1870 * On Windows, we need to use binary mode to read/write non-text archive
1871 * formats. Force stdin/stdout into binary mode if that is what we are
1875 if (fmt
!= archNull
&&
1876 (AH
->fSpec
== NULL
|| strcmp(AH
->fSpec
, "") == 0))
1878 if (mode
== archModeWrite
)
1879 setmode(fileno(stdout
), O_BINARY
);
1881 setmode(fileno(stdin
), O_BINARY
);
1885 if (fmt
== archUnknown
)
1886 AH
->format
= _discoverArchiveFormat(AH
);
1890 AH
->promptPassword
= TRI_DEFAULT
;
1895 InitArchiveFmt_Custom(AH
);
1899 InitArchiveFmt_Files(AH
);
1903 InitArchiveFmt_Null(AH
);
1907 InitArchiveFmt_Tar(AH
);
1911 die_horribly(AH
, modulename
, "unrecognized file format \"%d\"\n", fmt
);
1919 WriteDataChunks(ArchiveHandle
*AH
)
1922 StartDataPtr startPtr
;
1925 for (te
= AH
->toc
->next
; te
!= AH
->toc
; te
= te
->next
)
1927 if (te
->dataDumper
!= NULL
)
1930 /* printf("Writing data for %d (%x)\n", te->id, te); */
1932 if (strcmp(te
->desc
, "BLOBS") == 0)
1934 startPtr
= AH
->StartBlobsPtr
;
1935 endPtr
= AH
->EndBlobsPtr
;
1939 startPtr
= AH
->StartDataPtr
;
1940 endPtr
= AH
->EndDataPtr
;
1943 if (startPtr
!= NULL
)
1944 (*startPtr
) (AH
, te
);
1947 * printf("Dumper arg for %d is %x\n", te->id, te->dataDumperArg);
1951 * The user-provided DataDumper routine needs to call
1954 (*te
->dataDumper
) ((Archive
*) AH
, te
->dataDumperArg
);
1964 WriteToc(ArchiveHandle
*AH
)
1970 /* printf("%d TOC Entries to save\n", AH->tocCount); */
1972 WriteInt(AH
, AH
->tocCount
);
1974 for (te
= AH
->toc
->next
; te
!= AH
->toc
; te
= te
->next
)
1976 WriteInt(AH
, te
->dumpId
);
1977 WriteInt(AH
, te
->dataDumper
? 1 : 0);
1979 /* OID is recorded as a string for historical reasons */
1980 sprintf(workbuf
, "%u", te
->catalogId
.tableoid
);
1981 WriteStr(AH
, workbuf
);
1982 sprintf(workbuf
, "%u", te
->catalogId
.oid
);
1983 WriteStr(AH
, workbuf
);
1985 WriteStr(AH
, te
->tag
);
1986 WriteStr(AH
, te
->desc
);
1987 WriteInt(AH
, te
->section
);
1988 WriteStr(AH
, te
->defn
);
1989 WriteStr(AH
, te
->dropStmt
);
1990 WriteStr(AH
, te
->copyStmt
);
1991 WriteStr(AH
, te
->namespace);
1992 WriteStr(AH
, te
->tablespace
);
1993 WriteStr(AH
, te
->owner
);
1994 WriteStr(AH
, te
->withOids
? "true" : "false");
1996 /* Dump list of dependencies */
1997 for (i
= 0; i
< te
->nDeps
; i
++)
1999 sprintf(workbuf
, "%d", te
->dependencies
[i
]);
2000 WriteStr(AH
, workbuf
);
2002 WriteStr(AH
, NULL
); /* Terminate List */
2004 if (AH
->WriteExtraTocPtr
)
2005 (*AH
->WriteExtraTocPtr
) (AH
, te
);
2010 ReadToc(ArchiveHandle
*AH
)
2019 AH
->tocCount
= ReadInt(AH
);
2022 for (i
= 0; i
< AH
->tocCount
; i
++)
2024 te
= (TocEntry
*) calloc(1, sizeof(TocEntry
));
2025 te
->dumpId
= ReadInt(AH
);
2027 if (te
->dumpId
> AH
->maxDumpId
)
2028 AH
->maxDumpId
= te
->dumpId
;
2031 if (te
->dumpId
<= 0)
2032 die_horribly(AH
, modulename
,
2033 "entry ID %d out of range -- perhaps a corrupt TOC\n",
2036 te
->hadDumper
= ReadInt(AH
);
2038 if (AH
->version
>= K_VERS_1_8
)
2041 sscanf(tmp
, "%u", &te
->catalogId
.tableoid
);
2045 te
->catalogId
.tableoid
= InvalidOid
;
2047 sscanf(tmp
, "%u", &te
->catalogId
.oid
);
2050 te
->tag
= ReadStr(AH
);
2051 te
->desc
= ReadStr(AH
);
2053 if (AH
->version
>= K_VERS_1_11
)
2055 te
->section
= ReadInt(AH
);
2060 * rules for pre-8.4 archives wherein pg_dump hasn't classified
2061 * the entries into sections
2063 if (strcmp(te
->desc
, "COMMENT") == 0 ||
2064 strcmp(te
->desc
, "ACL") == 0)
2065 te
->section
= SECTION_NONE
;
2066 else if (strcmp(te
->desc
, "TABLE DATA") == 0 ||
2067 strcmp(te
->desc
, "BLOBS") == 0 ||
2068 strcmp(te
->desc
, "BLOB COMMENTS") == 0)
2069 te
->section
= SECTION_DATA
;
2070 else if (strcmp(te
->desc
, "CONSTRAINT") == 0 ||
2071 strcmp(te
->desc
, "CHECK CONSTRAINT") == 0 ||
2072 strcmp(te
->desc
, "FK CONSTRAINT") == 0 ||
2073 strcmp(te
->desc
, "INDEX") == 0 ||
2074 strcmp(te
->desc
, "RULE") == 0 ||
2075 strcmp(te
->desc
, "TRIGGER") == 0)
2076 te
->section
= SECTION_POST_DATA
;
2078 te
->section
= SECTION_PRE_DATA
;
2081 te
->defn
= ReadStr(AH
);
2082 te
->dropStmt
= ReadStr(AH
);
2084 if (AH
->version
>= K_VERS_1_3
)
2085 te
->copyStmt
= ReadStr(AH
);
2087 if (AH
->version
>= K_VERS_1_6
)
2088 te
->namespace = ReadStr(AH
);
2090 if (AH
->version
>= K_VERS_1_10
)
2091 te
->tablespace
= ReadStr(AH
);
2093 te
->owner
= ReadStr(AH
);
2094 if (AH
->version
>= K_VERS_1_9
)
2096 if (strcmp(ReadStr(AH
), "true") == 0)
2097 te
->withOids
= true;
2099 te
->withOids
= false;
2102 te
->withOids
= true;
2104 /* Read TOC entry dependencies */
2105 if (AH
->version
>= K_VERS_1_5
)
2108 deps
= (DumpId
*) malloc(sizeof(DumpId
) * depSize
);
2114 break; /* end of list */
2115 if (depIdx
>= depSize
)
2118 deps
= (DumpId
*) realloc(deps
, sizeof(DumpId
) * depSize
);
2120 sscanf(tmp
, "%d", &deps
[depIdx
]);
2125 if (depIdx
> 0) /* We have a non-null entry */
2127 deps
= (DumpId
*) realloc(deps
, sizeof(DumpId
) * depIdx
);
2128 te
->dependencies
= deps
;
2134 te
->dependencies
= NULL
;
2140 te
->dependencies
= NULL
;
2144 if (AH
->ReadExtraTocPtr
)
2145 (*AH
->ReadExtraTocPtr
) (AH
, te
);
2147 ahlog(AH
, 3, "read TOC entry %d (ID %d) for %s %s\n",
2148 i
, te
->dumpId
, te
->desc
, te
->tag
);
2150 /* link completed entry into TOC circular list */
2151 te
->prev
= AH
->toc
->prev
;
2152 AH
->toc
->prev
->next
= te
;
2156 /* special processing immediately upon read for some items */
2157 if (strcmp(te
->desc
, "ENCODING") == 0)
2158 processEncodingEntry(AH
, te
);
2159 else if (strcmp(te
->desc
, "STDSTRINGS") == 0)
2160 processStdStringsEntry(AH
, te
);
2165 processEncodingEntry(ArchiveHandle
*AH
, TocEntry
*te
)
2167 /* te->defn should have the form SET client_encoding = 'foo'; */
2168 char *defn
= strdup(te
->defn
);
2173 ptr1
= strchr(defn
, '\'');
2175 ptr2
= strchr(++ptr1
, '\'');
2179 encoding
= pg_char_to_encoding(ptr1
);
2181 die_horribly(AH
, modulename
, "unrecognized encoding \"%s\"\n",
2183 AH
->public.encoding
= encoding
;
2186 die_horribly(AH
, modulename
, "invalid ENCODING item: %s\n",
2193 processStdStringsEntry(ArchiveHandle
*AH
, TocEntry
*te
)
2195 /* te->defn should have the form SET standard_conforming_strings = 'x'; */
2198 ptr1
= strchr(te
->defn
, '\'');
2199 if (ptr1
&& strncmp(ptr1
, "'on'", 4) == 0)
2200 AH
->public.std_strings
= true;
2201 else if (ptr1
&& strncmp(ptr1
, "'off'", 5) == 0)
2202 AH
->public.std_strings
= false;
2204 die_horribly(AH
, modulename
, "invalid STDSTRINGS item: %s\n",
2209 _tocEntryRequired(TocEntry
*te
, RestoreOptions
*ropt
, bool include_acls
)
2211 teReqs res
= REQ_ALL
;
2213 /* ENCODING and STDSTRINGS items are dumped specially, so always reject */
2214 if (strcmp(te
->desc
, "ENCODING") == 0 ||
2215 strcmp(te
->desc
, "STDSTRINGS") == 0)
2218 /* If it's an ACL, maybe ignore it */
2219 if ((!include_acls
|| ropt
->aclsSkip
) && strcmp(te
->desc
, "ACL") == 0)
2222 if (!ropt
->create
&& strcmp(te
->desc
, "DATABASE") == 0)
2225 /* Check options for selective dump/restore */
2226 if (ropt
->schemaNames
)
2228 /* If no namespace is specified, it means all. */
2231 if (strcmp(ropt
->schemaNames
, te
->namespace) != 0)
2237 if (strcmp(te
->desc
, "TABLE") == 0 ||
2238 strcmp(te
->desc
, "TABLE DATA") == 0)
2240 if (!ropt
->selTable
)
2242 if (ropt
->tableNames
&& strcmp(ropt
->tableNames
, te
->tag
) != 0)
2245 else if (strcmp(te
->desc
, "INDEX") == 0)
2247 if (!ropt
->selIndex
)
2249 if (ropt
->indexNames
&& strcmp(ropt
->indexNames
, te
->tag
) != 0)
2252 else if (strcmp(te
->desc
, "FUNCTION") == 0)
2254 if (!ropt
->selFunction
)
2256 if (ropt
->functionNames
&& strcmp(ropt
->functionNames
, te
->tag
) != 0)
2259 else if (strcmp(te
->desc
, "TRIGGER") == 0)
2261 if (!ropt
->selTrigger
)
2263 if (ropt
->triggerNames
&& strcmp(ropt
->triggerNames
, te
->tag
) != 0)
2271 * Check if we had a dataDumper. Indicates if the entry is schema or data
2276 * Special Case: If 'SEQUENCE SET' then it is considered a data entry
2278 if (strcmp(te
->desc
, "SEQUENCE SET") == 0)
2279 res
= res
& REQ_DATA
;
2281 res
= res
& ~REQ_DATA
;
2285 * Special case: <Init> type with <Max OID> tag; this is obsolete and we
2288 if ((strcmp(te
->desc
, "<Init>") == 0) && (strcmp(te
->tag
, "Max OID") == 0))
2291 /* Mask it if we only want schema */
2292 if (ropt
->schemaOnly
)
2293 res
= res
& REQ_SCHEMA
;
2295 /* Mask it we only want data */
2297 res
= res
& REQ_DATA
;
2299 /* Mask it if we don't have a schema contribution */
2300 if (!te
->defn
|| strlen(te
->defn
) == 0)
2301 res
= res
& ~REQ_SCHEMA
;
2303 /* Finally, if there's a per-ID filter, limit based on that as well */
2304 if (ropt
->idWanted
&& !ropt
->idWanted
[te
->dumpId
- 1])
2311 * Issue SET commands for parameters that we want to have set the same way
2312 * at all times during execution of a restore script.
2315 _doSetFixedOutputState(ArchiveHandle
*AH
)
2317 /* Disable statement_timeout in archive for pg_restore/psql */
2318 ahprintf(AH
, "SET statement_timeout = 0;\n");
2320 /* Select the correct character set encoding */
2321 ahprintf(AH
, "SET client_encoding = '%s';\n",
2322 pg_encoding_to_char(AH
->public.encoding
));
2324 /* Select the correct string literal syntax */
2325 ahprintf(AH
, "SET standard_conforming_strings = %s;\n",
2326 AH
->public.std_strings
? "on" : "off");
2328 /* Select the role to be used during restore */
2329 if (AH
->ropt
&& AH
->ropt
->use_role
)
2330 ahprintf(AH
, "SET ROLE %s;\n", fmtId(AH
->ropt
->use_role
));
2332 /* Make sure function checking is disabled */
2333 ahprintf(AH
, "SET check_function_bodies = false;\n");
2335 /* Avoid annoying notices etc */
2336 ahprintf(AH
, "SET client_min_messages = warning;\n");
2337 if (!AH
->public.std_strings
)
2338 ahprintf(AH
, "SET escape_string_warning = off;\n");
2344 * Issue a SET SESSION AUTHORIZATION command. Caller is responsible
2345 * for updating state if appropriate. If user is NULL or an empty string,
2346 * the specification DEFAULT will be used.
2349 _doSetSessionAuth(ArchiveHandle
*AH
, const char *user
)
2351 PQExpBuffer cmd
= createPQExpBuffer();
2353 appendPQExpBuffer(cmd
, "SET SESSION AUTHORIZATION ");
2356 * SQL requires a string literal here. Might as well be correct.
2359 appendStringLiteralAHX(cmd
, user
, AH
);
2361 appendPQExpBuffer(cmd
, "DEFAULT");
2362 appendPQExpBuffer(cmd
, ";");
2364 if (RestoringToDB(AH
))
2368 res
= PQexec(AH
->connection
, cmd
->data
);
2370 if (!res
|| PQresultStatus(res
) != PGRES_COMMAND_OK
)
2371 /* NOT warn_or_die_horribly... use -O instead to skip this. */
2372 die_horribly(AH
, modulename
, "could not set session user to \"%s\": %s",
2373 user
, PQerrorMessage(AH
->connection
));
2378 ahprintf(AH
, "%s\n\n", cmd
->data
);
2380 destroyPQExpBuffer(cmd
);
2385 * Issue a SET default_with_oids command. Caller is responsible
2386 * for updating state if appropriate.
2389 _doSetWithOids(ArchiveHandle
*AH
, const bool withOids
)
2391 PQExpBuffer cmd
= createPQExpBuffer();
2393 appendPQExpBuffer(cmd
, "SET default_with_oids = %s;", withOids
?
2396 if (RestoringToDB(AH
))
2400 res
= PQexec(AH
->connection
, cmd
->data
);
2402 if (!res
|| PQresultStatus(res
) != PGRES_COMMAND_OK
)
2403 warn_or_die_horribly(AH
, modulename
,
2404 "could not set default_with_oids: %s",
2405 PQerrorMessage(AH
->connection
));
2410 ahprintf(AH
, "%s\n\n", cmd
->data
);
2412 destroyPQExpBuffer(cmd
);
2417 * Issue the commands to connect to the specified database.
2419 * If we're currently restoring right into a database, this will
2420 * actually establish a connection. Otherwise it puts a \connect into
2421 * the script output.
2423 * NULL dbname implies reconnecting to the current DB (pretty useless).
2426 _reconnectToDB(ArchiveHandle
*AH
, const char *dbname
)
2428 if (RestoringToDB(AH
))
2429 ReconnectToServer(AH
, dbname
, NULL
);
2432 PQExpBuffer qry
= createPQExpBuffer();
2434 appendPQExpBuffer(qry
, "\\connect %s\n\n",
2435 dbname
? fmtId(dbname
) : "-");
2436 ahprintf(AH
, "%s", qry
->data
);
2437 destroyPQExpBuffer(qry
);
2441 * NOTE: currUser keeps track of what the imaginary session user in our
2442 * script is. It's now effectively reset to the original userID.
2446 AH
->currUser
= NULL
;
2448 /* don't assume we still know the output schema, tablespace, etc either */
2450 free(AH
->currSchema
);
2451 AH
->currSchema
= NULL
;
2452 if (AH
->currTablespace
)
2453 free(AH
->currTablespace
);
2454 AH
->currTablespace
= NULL
;
2455 AH
->currWithOids
= -1;
2457 /* re-establish fixed state */
2458 _doSetFixedOutputState(AH
);
2462 * Become the specified user, and update state to avoid redundant commands
2464 * NULL or empty argument is taken to mean restoring the session default
2467 _becomeUser(ArchiveHandle
*AH
, const char *user
)
2470 user
= ""; /* avoid null pointers */
2472 if (AH
->currUser
&& strcmp(AH
->currUser
, user
) == 0)
2473 return; /* no need to do anything */
2475 _doSetSessionAuth(AH
, user
);
2478 * NOTE: currUser keeps track of what the imaginary session user in our
2483 AH
->currUser
= strdup(user
);
2487 * Become the owner of the the given TOC entry object. If
2488 * changes in ownership are not allowed, this doesn't do anything.
2491 _becomeOwner(ArchiveHandle
*AH
, TocEntry
*te
)
2493 if (AH
->ropt
&& (AH
->ropt
->noOwner
|| !AH
->ropt
->use_setsessauth
))
2496 _becomeUser(AH
, te
->owner
);
2501 * Set the proper default_with_oids value for the table.
2504 _setWithOids(ArchiveHandle
*AH
, TocEntry
*te
)
2506 if (AH
->currWithOids
!= te
->withOids
)
2508 _doSetWithOids(AH
, te
->withOids
);
2509 AH
->currWithOids
= te
->withOids
;
2515 * Issue the commands to select the specified schema as the current schema
2516 * in the target database.
2519 _selectOutputSchema(ArchiveHandle
*AH
, const char *schemaName
)
2523 if (!schemaName
|| *schemaName
== '\0' ||
2524 (AH
->currSchema
&& strcmp(AH
->currSchema
, schemaName
) == 0))
2525 return; /* no need to do anything */
2527 qry
= createPQExpBuffer();
2529 appendPQExpBuffer(qry
, "SET search_path = %s",
2531 if (strcmp(schemaName
, "pg_catalog") != 0)
2532 appendPQExpBuffer(qry
, ", pg_catalog");
2534 if (RestoringToDB(AH
))
2538 res
= PQexec(AH
->connection
, qry
->data
);
2540 if (!res
|| PQresultStatus(res
) != PGRES_COMMAND_OK
)
2541 warn_or_die_horribly(AH
, modulename
,
2542 "could not set search_path to \"%s\": %s",
2543 schemaName
, PQerrorMessage(AH
->connection
));
2548 ahprintf(AH
, "%s;\n\n", qry
->data
);
2551 free(AH
->currSchema
);
2552 AH
->currSchema
= strdup(schemaName
);
2554 destroyPQExpBuffer(qry
);
2558 * Issue the commands to select the specified tablespace as the current one
2559 * in the target database.
2562 _selectTablespace(ArchiveHandle
*AH
, const char *tablespace
)
2568 /* do nothing in --no-tablespaces mode */
2569 if (AH
->ropt
->noTablespace
)
2572 have
= AH
->currTablespace
;
2575 /* no need to do anything for non-tablespace object */
2579 if (have
&& strcmp(want
, have
) == 0)
2580 return; /* no need to do anything */
2582 qry
= createPQExpBuffer();
2584 if (strcmp(want
, "") == 0)
2586 /* We want the tablespace to be the database's default */
2587 appendPQExpBuffer(qry
, "SET default_tablespace = ''");
2591 /* We want an explicit tablespace */
2592 appendPQExpBuffer(qry
, "SET default_tablespace = %s", fmtId(want
));
2595 if (RestoringToDB(AH
))
2599 res
= PQexec(AH
->connection
, qry
->data
);
2601 if (!res
|| PQresultStatus(res
) != PGRES_COMMAND_OK
)
2602 warn_or_die_horribly(AH
, modulename
,
2603 "could not set default_tablespace to %s: %s",
2604 fmtId(want
), PQerrorMessage(AH
->connection
));
2609 ahprintf(AH
, "%s;\n\n", qry
->data
);
2611 if (AH
->currTablespace
)
2612 free(AH
->currTablespace
);
2613 AH
->currTablespace
= strdup(want
);
2615 destroyPQExpBuffer(qry
);
2619 * Extract an object description for a TOC entry, and append it to buf.
2621 * This is not quite as general as it may seem, since it really only
2622 * handles constructing the right thing to put into ALTER ... OWNER TO.
2624 * The whole thing is pretty grotty, but we are kind of stuck since the
2625 * information used is all that's available in older dump files.
2628 _getObjectDescription(PQExpBuffer buf
, TocEntry
*te
, ArchiveHandle
*AH
)
2630 const char *type
= te
->desc
;
2632 /* Use ALTER TABLE for views and sequences */
2633 if (strcmp(type
, "VIEW") == 0 || strcmp(type
, "SEQUENCE") == 0)
2636 /* objects named by a schema and name */
2637 if (strcmp(type
, "CONVERSION") == 0 ||
2638 strcmp(type
, "DOMAIN") == 0 ||
2639 strcmp(type
, "TABLE") == 0 ||
2640 strcmp(type
, "TYPE") == 0 ||
2641 strcmp(type
, "TEXT SEARCH DICTIONARY") == 0 ||
2642 strcmp(type
, "TEXT SEARCH CONFIGURATION") == 0)
2644 appendPQExpBuffer(buf
, "%s ", type
);
2645 if (te
->namespace && te
->namespace[0]) /* is null pre-7.3 */
2646 appendPQExpBuffer(buf
, "%s.", fmtId(te
->namespace));
2649 * Pre-7.3 pg_dump would sometimes (not always) put a fmtId'd name
2650 * into te->tag for an index. This check is heuristic, so make its
2651 * scope as narrow as possible.
2653 if (AH
->version
< K_VERS_1_7
&&
2654 te
->tag
[0] == '"' &&
2655 te
->tag
[strlen(te
->tag
) - 1] == '"' &&
2656 strcmp(type
, "INDEX") == 0)
2657 appendPQExpBuffer(buf
, "%s", te
->tag
);
2659 appendPQExpBuffer(buf
, "%s", fmtId(te
->tag
));
2663 /* objects named by just a name */
2664 if (strcmp(type
, "DATABASE") == 0 ||
2665 strcmp(type
, "PROCEDURAL LANGUAGE") == 0 ||
2666 strcmp(type
, "SCHEMA") == 0 ||
2667 strcmp(type
, "FOREIGN DATA WRAPPER") == 0 ||
2668 strcmp(type
, "SERVER") == 0 ||
2669 strcmp(type
, "USER MAPPING") == 0)
2671 appendPQExpBuffer(buf
, "%s %s", type
, fmtId(te
->tag
));
2676 * These object types require additional decoration. Fortunately, the
2677 * information needed is exactly what's in the DROP command.
2679 if (strcmp(type
, "AGGREGATE") == 0 ||
2680 strcmp(type
, "FUNCTION") == 0 ||
2681 strcmp(type
, "OPERATOR") == 0 ||
2682 strcmp(type
, "OPERATOR CLASS") == 0 ||
2683 strcmp(type
, "OPERATOR FAMILY") == 0)
2685 /* Chop "DROP " off the front and make a modifiable copy */
2686 char *first
= strdup(te
->dropStmt
+ 5);
2689 /* point to last character in string */
2690 last
= first
+ strlen(first
) - 1;
2692 /* Strip off any ';' or '\n' at the end */
2693 while (last
>= first
&& (*last
== '\n' || *last
== ';'))
2697 appendPQExpBufferStr(buf
, first
);
2703 write_msg(modulename
, "WARNING: don't know how to set owner for object type %s\n",
2708 _printTocEntry(ArchiveHandle
*AH
, TocEntry
*te
, RestoreOptions
*ropt
, bool isData
, bool acl_pass
)
2710 /* ACLs are dumped only during acl pass */
2713 if (strcmp(te
->desc
, "ACL") != 0)
2718 if (strcmp(te
->desc
, "ACL") == 0)
2723 * Avoid dumping the public schema, as it will already be created ...
2724 * unless we are using --clean mode, in which case it's been deleted and
2725 * we'd better recreate it. Likewise for its comment, if any.
2727 if (!ropt
->dropSchema
)
2729 if (strcmp(te
->desc
, "SCHEMA") == 0 &&
2730 strcmp(te
->tag
, "public") == 0)
2732 /* The comment restore would require super-user privs, so avoid it. */
2733 if (strcmp(te
->desc
, "COMMENT") == 0 &&
2734 strcmp(te
->tag
, "SCHEMA public") == 0)
2738 /* Select owner, schema, and tablespace as necessary */
2739 _becomeOwner(AH
, te
);
2740 _selectOutputSchema(AH
, te
->namespace);
2741 _selectTablespace(AH
, te
->tablespace
);
2743 /* Set up OID mode too */
2744 if (strcmp(te
->desc
, "TABLE") == 0)
2745 _setWithOids(AH
, te
);
2747 /* Emit header comment for item */
2748 if (!AH
->noTocComments
)
2757 ahprintf(AH
, "--\n");
2758 if (AH
->public.verbose
)
2760 ahprintf(AH
, "-- TOC entry %d (class %u OID %u)\n",
2761 te
->dumpId
, te
->catalogId
.tableoid
, te
->catalogId
.oid
);
2766 ahprintf(AH
, "-- Dependencies:");
2767 for (i
= 0; i
< te
->nDeps
; i
++)
2768 ahprintf(AH
, " %d", te
->dependencies
[i
]);
2772 ahprintf(AH
, "-- %sName: %s; Type: %s; Schema: %s; Owner: %s",
2773 pfx
, te
->tag
, te
->desc
,
2774 te
->namespace ? te
->namespace : "-",
2775 ropt
->noOwner
? "-" : te
->owner
);
2776 if (te
->tablespace
&& !ropt
->noTablespace
)
2777 ahprintf(AH
, "; Tablespace: %s", te
->tablespace
);
2780 if (AH
->PrintExtraTocPtr
!=NULL
)
2781 (*AH
->PrintExtraTocPtr
) (AH
, te
);
2782 ahprintf(AH
, "--\n\n");
2786 * Actually print the definition.
2788 * Really crude hack for suppressing AUTHORIZATION clause that old pg_dump
2789 * versions put into CREATE SCHEMA. We have to do this when --no-owner
2790 * mode is selected. This is ugly, but I see no other good way ...
2792 if (ropt
->noOwner
&& strcmp(te
->desc
, "SCHEMA") == 0)
2794 ahprintf(AH
, "CREATE SCHEMA %s;\n\n\n", fmtId(te
->tag
));
2798 if (strlen(te
->defn
) > 0)
2799 ahprintf(AH
, "%s\n\n", te
->defn
);
2803 * If we aren't using SET SESSION AUTH to determine ownership, we must
2804 * instead issue an ALTER OWNER command. We assume that anything without
2805 * a DROP command is not a separately ownable object. All the categories
2806 * with DROP commands must appear in one list or the other.
2808 if (!ropt
->noOwner
&& !ropt
->use_setsessauth
&&
2809 strlen(te
->owner
) > 0 && strlen(te
->dropStmt
) > 0)
2811 if (strcmp(te
->desc
, "AGGREGATE") == 0 ||
2812 strcmp(te
->desc
, "CONVERSION") == 0 ||
2813 strcmp(te
->desc
, "DATABASE") == 0 ||
2814 strcmp(te
->desc
, "DOMAIN") == 0 ||
2815 strcmp(te
->desc
, "FUNCTION") == 0 ||
2816 strcmp(te
->desc
, "OPERATOR") == 0 ||
2817 strcmp(te
->desc
, "OPERATOR CLASS") == 0 ||
2818 strcmp(te
->desc
, "OPERATOR FAMILY") == 0 ||
2819 strcmp(te
->desc
, "PROCEDURAL LANGUAGE") == 0 ||
2820 strcmp(te
->desc
, "SCHEMA") == 0 ||
2821 strcmp(te
->desc
, "TABLE") == 0 ||
2822 strcmp(te
->desc
, "TYPE") == 0 ||
2823 strcmp(te
->desc
, "VIEW") == 0 ||
2824 strcmp(te
->desc
, "SEQUENCE") == 0 ||
2825 strcmp(te
->desc
, "TEXT SEARCH DICTIONARY") == 0 ||
2826 strcmp(te
->desc
, "TEXT SEARCH CONFIGURATION") == 0 ||
2827 strcmp(te
->desc
, "FOREIGN DATA WRAPPER") == 0 ||
2828 strcmp(te
->desc
, "SERVER") == 0)
2830 PQExpBuffer temp
= createPQExpBuffer();
2832 appendPQExpBuffer(temp
, "ALTER ");
2833 _getObjectDescription(temp
, te
, AH
);
2834 appendPQExpBuffer(temp
, " OWNER TO %s;", fmtId(te
->owner
));
2835 ahprintf(AH
, "%s\n\n", temp
->data
);
2836 destroyPQExpBuffer(temp
);
2838 else if (strcmp(te
->desc
, "CAST") == 0 ||
2839 strcmp(te
->desc
, "CHECK CONSTRAINT") == 0 ||
2840 strcmp(te
->desc
, "CONSTRAINT") == 0 ||
2841 strcmp(te
->desc
, "DEFAULT") == 0 ||
2842 strcmp(te
->desc
, "FK CONSTRAINT") == 0 ||
2843 strcmp(te
->desc
, "INDEX") == 0 ||
2844 strcmp(te
->desc
, "RULE") == 0 ||
2845 strcmp(te
->desc
, "TRIGGER") == 0 ||
2846 strcmp(te
->desc
, "USER MAPPING") == 0)
2848 /* these object types don't have separate owners */
2852 write_msg(modulename
, "WARNING: don't know how to set owner for object type %s\n",
2858 * If it's an ACL entry, it might contain SET SESSION AUTHORIZATION
2859 * commands, so we can no longer assume we know the current auth setting.
2861 if (strncmp(te
->desc
, "ACL", 3) == 0)
2865 AH
->currUser
= NULL
;
2870 WriteHead(ArchiveHandle
*AH
)
2874 (*AH
->WriteBufPtr
) (AH
, "PGDMP", 5); /* Magic code */
2875 (*AH
->WriteBytePtr
) (AH
, AH
->vmaj
);
2876 (*AH
->WriteBytePtr
) (AH
, AH
->vmin
);
2877 (*AH
->WriteBytePtr
) (AH
, AH
->vrev
);
2878 (*AH
->WriteBytePtr
) (AH
, AH
->intSize
);
2879 (*AH
->WriteBytePtr
) (AH
, AH
->offSize
);
2880 (*AH
->WriteBytePtr
) (AH
, AH
->format
);
2883 if (AH
->compression
!= 0)
2884 write_msg(modulename
, "WARNING: requested compression not available in this "
2885 "installation -- archive will be uncompressed\n");
2887 AH
->compression
= 0;
2890 WriteInt(AH
, AH
->compression
);
2892 crtm
= *localtime(&AH
->createDate
);
2893 WriteInt(AH
, crtm
.tm_sec
);
2894 WriteInt(AH
, crtm
.tm_min
);
2895 WriteInt(AH
, crtm
.tm_hour
);
2896 WriteInt(AH
, crtm
.tm_mday
);
2897 WriteInt(AH
, crtm
.tm_mon
);
2898 WriteInt(AH
, crtm
.tm_year
);
2899 WriteInt(AH
, crtm
.tm_isdst
);
2900 WriteStr(AH
, PQdb(AH
->connection
));
2901 WriteStr(AH
, AH
->public.remoteVersionStr
);
2902 WriteStr(AH
, PG_VERSION
);
2906 ReadHead(ArchiveHandle
*AH
)
2912 /* If we haven't already read the header... */
2913 if (!AH
->readHeader
)
2915 if ((*AH
->ReadBufPtr
) (AH
, tmpMag
, 5) != 5)
2916 die_horribly(AH
, modulename
, "unexpected end of file\n");
2918 if (strncmp(tmpMag
, "PGDMP", 5) != 0)
2919 die_horribly(AH
, modulename
, "did not find magic string in file header\n");
2921 AH
->vmaj
= (*AH
->ReadBytePtr
) (AH
);
2922 AH
->vmin
= (*AH
->ReadBytePtr
) (AH
);
2924 if (AH
->vmaj
> 1 || ((AH
->vmaj
== 1) && (AH
->vmin
> 0))) /* Version > 1.0 */
2925 AH
->vrev
= (*AH
->ReadBytePtr
) (AH
);
2929 AH
->version
= ((AH
->vmaj
* 256 + AH
->vmin
) * 256 + AH
->vrev
) * 256 + 0;
2932 if (AH
->version
< K_VERS_1_0
|| AH
->version
> K_VERS_MAX
)
2933 die_horribly(AH
, modulename
, "unsupported version (%d.%d) in file header\n",
2934 AH
->vmaj
, AH
->vmin
);
2936 AH
->intSize
= (*AH
->ReadBytePtr
) (AH
);
2937 if (AH
->intSize
> 32)
2938 die_horribly(AH
, modulename
, "sanity check on integer size (%lu) failed\n",
2939 (unsigned long) AH
->intSize
);
2941 if (AH
->intSize
> sizeof(int))
2942 write_msg(modulename
, "WARNING: archive was made on a machine with larger integers, some operations might fail\n");
2944 if (AH
->version
>= K_VERS_1_7
)
2945 AH
->offSize
= (*AH
->ReadBytePtr
) (AH
);
2947 AH
->offSize
= AH
->intSize
;
2949 fmt
= (*AH
->ReadBytePtr
) (AH
);
2951 if (AH
->format
!= fmt
)
2952 die_horribly(AH
, modulename
, "expected format (%d) differs from format found in file (%d)\n",
2956 if (AH
->version
>= K_VERS_1_2
)
2958 if (AH
->version
< K_VERS_1_4
)
2959 AH
->compression
= (*AH
->ReadBytePtr
) (AH
);
2961 AH
->compression
= ReadInt(AH
);
2964 AH
->compression
= Z_DEFAULT_COMPRESSION
;
2967 if (AH
->compression
!= 0)
2968 write_msg(modulename
, "WARNING: archive is compressed, but this installation does not support compression -- no data will be available\n");
2971 if (AH
->version
>= K_VERS_1_4
)
2973 crtm
.tm_sec
= ReadInt(AH
);
2974 crtm
.tm_min
= ReadInt(AH
);
2975 crtm
.tm_hour
= ReadInt(AH
);
2976 crtm
.tm_mday
= ReadInt(AH
);
2977 crtm
.tm_mon
= ReadInt(AH
);
2978 crtm
.tm_year
= ReadInt(AH
);
2979 crtm
.tm_isdst
= ReadInt(AH
);
2981 AH
->archdbname
= ReadStr(AH
);
2983 AH
->createDate
= mktime(&crtm
);
2985 if (AH
->createDate
== (time_t) -1)
2986 write_msg(modulename
, "WARNING: invalid creation date in header\n");
2989 if (AH
->version
>= K_VERS_1_10
)
2991 AH
->archiveRemoteVersion
= ReadStr(AH
);
2992 AH
->archiveDumpVersion
= ReadStr(AH
);
3000 * check to see if fseek can be performed.
3005 if (fseeko(fp
, 0, SEEK_CUR
) != 0)
3007 else if (sizeof(pgoff_t
) > sizeof(long))
3010 * At this point, pgoff_t is too large for long, so we return based on
3011 * whether an pgoff_t version of fseek is available.
3028 dumpTimestamp(ArchiveHandle
*AH
, const char *msg
, time_t tim
)
3033 * We don't print the timezone on Win32, because the names are long and
3034 * localized, which means they may contain characters in various random
3035 * encodings; this has been seen to cause encoding errors when reading the
3038 if (strftime(buf
, sizeof(buf
),
3040 "%Y-%m-%d %H:%M:%S %Z",
3042 "%Y-%m-%d %H:%M:%S",
3044 localtime(&tim
)) != 0)
3045 ahprintf(AH
, "-- %s %s\n\n", msg
, buf
);
3050 * Main engine for parallel restore.
3052 * Work is done in three phases.
3053 * First we process tocEntries until we come to one that is marked
3054 * SECTION_DATA or SECTION_POST_DATA, in a single connection, just as for a
3055 * standard restore. Second we process the remaining non-ACL steps in
3056 * parallel worker children (threads on Windows, processes on Unix), each of
3057 * which connects separately to the database. Finally we process all the ACL
3058 * entries in a single connection (that happens back in RestoreArchive).
3061 restore_toc_entries_parallel(ArchiveHandle
*AH
)
3063 RestoreOptions
*ropt
= AH
->ropt
;
3064 int n_slots
= ropt
->number_of_jobs
;
3065 ParallelSlot
*slots
;
3068 TocEntry
*first_unprocessed
= AH
->toc
->next
;
3069 TocEntry
*next_work_item
;
3073 ahlog(AH
, 2, "entering restore_toc_entries_parallel\n");
3075 /* we haven't got round to making this work for all archive formats */
3076 if (AH
->ClonePtr
== NULL
|| AH
->ReopenPtr
== NULL
)
3077 die_horribly(AH
, modulename
, "parallel restore is not supported with this archive file format\n");
3079 slots
= (ParallelSlot
*) calloc(sizeof(ParallelSlot
), n_slots
);
3081 /* Adjust dependency information */
3082 fix_dependencies(AH
);
3085 * Do all the early stuff in a single connection in the parent. There's no
3086 * great point in running it in parallel, in fact it will actually run
3087 * faster in a single connection because we avoid all the connection and
3090 while ((next_work_item
= get_next_work_item(AH
, &first_unprocessed
,
3093 if (next_work_item
->section
== SECTION_DATA
||
3094 next_work_item
->section
== SECTION_POST_DATA
)
3097 ahlog(AH
, 1, "processing item %d %s %s\n",
3098 next_work_item
->dumpId
,
3099 next_work_item
->desc
, next_work_item
->tag
);
3101 (void) restore_toc_entry(AH
, next_work_item
, ropt
, false);
3103 next_work_item
->restored
= true;
3104 reduce_dependencies(AH
, next_work_item
);
3108 * Now close parent connection in prep for parallel steps. We do this
3109 * mainly to ensure that we don't exceed the specified number of parallel
3112 PQfinish(AH
->connection
);
3113 AH
->connection
= NULL
;
3115 /* blow away any transient state from the old connection */
3118 AH
->currUser
= NULL
;
3120 free(AH
->currSchema
);
3121 AH
->currSchema
= NULL
;
3122 if (AH
->currTablespace
)
3123 free(AH
->currTablespace
);
3124 AH
->currTablespace
= NULL
;
3125 AH
->currWithOids
= -1;
3130 * Keep going until there is no worker still running AND there is no work
3134 ahlog(AH
, 1, "entering main parallel loop\n");
3136 while ((next_work_item
= get_next_work_item(AH
, &first_unprocessed
,
3137 slots
, n_slots
)) != NULL
||
3138 work_in_progress(slots
, n_slots
))
3140 if (next_work_item
!= NULL
)
3144 /* If not to be dumped, don't waste time launching a worker */
3145 reqs
= _tocEntryRequired(next_work_item
, AH
->ropt
, false);
3146 if ((reqs
& (REQ_SCHEMA
| REQ_DATA
)) == 0)
3148 ahlog(AH
, 1, "skipping item %d %s %s\n",
3149 next_work_item
->dumpId
,
3150 next_work_item
->desc
, next_work_item
->tag
);
3152 next_work_item
->restored
= true;
3153 reduce_dependencies(AH
, next_work_item
);
3158 if ((next_slot
= get_next_slot(slots
, n_slots
)) != NO_SLOT
)
3160 /* There is work still to do and a worker slot available */
3164 ahlog(AH
, 1, "launching item %d %s %s\n",
3165 next_work_item
->dumpId
,
3166 next_work_item
->desc
, next_work_item
->tag
);
3168 next_work_item
->restored
= true;
3170 /* this memory is dealloced in mark_work_done() */
3171 args
= malloc(sizeof(RestoreArgs
));
3172 args
->AH
= CloneArchive(AH
);
3173 args
->te
= next_work_item
;
3175 /* run the step in a worker child */
3176 child
= spawn_restore(args
);
3178 slots
[next_slot
].child_id
= child
;
3179 slots
[next_slot
].args
= args
;
3186 * If we get here there must be work being done. Either there is no
3187 * work available to schedule (and work_in_progress returned true) or
3188 * there are no slots available. So we wait for a worker to finish,
3189 * and process the result.
3191 ret_child
= reap_child(slots
, n_slots
, &work_status
);
3193 if (WIFEXITED(work_status
))
3195 mark_work_done(AH
, ret_child
, WEXITSTATUS(work_status
),
3200 die_horribly(AH
, modulename
, "worker process crashed: status %d\n",
3205 ahlog(AH
, 1, "finished main parallel loop\n");
3208 * Now reconnect the single parent connection.
3210 ConnectDatabase((Archive
*) AH
, ropt
->dbname
,
3211 ropt
->pghost
, ropt
->pgport
, ropt
->username
,
3212 ropt
->promptPassword
);
3214 _doSetFixedOutputState(AH
);
3217 * Make sure there is no non-ACL work left due to, say, circular
3218 * dependencies, or some other pathological condition. If so, do it in the
3219 * single parent connection.
3221 for (te
= AH
->toc
->next
; te
!= AH
->toc
; te
= te
->next
)
3225 ahlog(AH
, 1, "processing missed item %d %s %s\n",
3226 te
->dumpId
, te
->desc
, te
->tag
);
3227 (void) restore_toc_entry(AH
, te
, ropt
, false);
3231 /* The ACLs will be handled back in RestoreArchive. */
3235 * create a worker child to perform a restore step in parallel
3238 spawn_restore(RestoreArgs
*args
)
3242 /* Ensure stdio state is quiesced before forking */
3249 /* in child process */
3250 parallel_restore(args
);
3251 die_horribly(args
->AH
, modulename
,
3252 "parallel_restore should not return\n");
3257 die_horribly(args
->AH
, modulename
,
3258 "could not create worker process: %s\n",
3262 child
= (HANDLE
) _beginthreadex(NULL
, 0, (void *) parallel_restore
,
3265 die_horribly(args
->AH
, modulename
,
3266 "could not create worker thread: %s\n",
3274 * collect status from a completed worker child
3277 reap_child(ParallelSlot
*slots
, int n_slots
, int *work_status
)
3280 /* Unix is so much easier ... */
3281 return wait(work_status
);
3283 static HANDLE
*handles
= NULL
;
3290 /* first time around only, make space for handles to listen on */
3291 if (handles
== NULL
)
3292 handles
= (HANDLE
*) calloc(sizeof(HANDLE
), n_slots
);
3294 /* set up list of handles to listen to */
3295 for (snum
= 0, tnum
= 0; snum
< n_slots
; snum
++)
3296 if (slots
[snum
].child_id
!= 0)
3297 handles
[tnum
++] = slots
[snum
].child_id
;
3299 /* wait for one to finish */
3300 hindex
= WaitForMultipleObjects(tnum
, handles
, false, INFINITE
);
3302 /* get handle of finished thread */
3303 ret_child
= handles
[hindex
- WAIT_OBJECT_0
];
3305 /* get the result */
3306 GetExitCodeThread(ret_child
, &res
);
3309 /* dispose of handle to stop leaks */
3310 CloseHandle(ret_child
);
3317 * are we doing anything now?
3320 work_in_progress(ParallelSlot
*slots
, int n_slots
)
3324 for (i
= 0; i
< n_slots
; i
++)
3326 if (slots
[i
].child_id
!= 0)
3333 * find the first free parallel slot (if any).
3336 get_next_slot(ParallelSlot
*slots
, int n_slots
)
3340 for (i
= 0; i
< n_slots
; i
++)
3342 if (slots
[i
].child_id
== 0)
3350 * Check if te1 has an exclusive lock requirement for an item that te2 also
3351 * requires, whether or not te2's requirement is for an exclusive lock.
3354 has_lock_conflicts(TocEntry
*te1
, TocEntry
*te2
)
3359 for (j
= 0; j
< te1
->nLockDeps
; j
++)
3361 for (k
= 0; k
< te2
->nDeps
; k
++)
3363 if (te1
->lockDeps
[j
] == te2
->dependencies
[k
])
3373 * Find the next work item (if any) that is capable of being run now.
3375 * To qualify, the item must have no remaining dependencies
3376 * and no requirement for locks that is incompatible with
3377 * items currently running.
3379 * first_unprocessed is state data that tracks the location of the first
3380 * TocEntry that's not marked 'restored'. This avoids O(N^2) search time
3381 * with long TOC lists. (Even though the constant is pretty small, it'd
3382 * get us eventually.)
3384 * pref_non_data is for an alternative selection algorithm that gives
3385 * preference to non-data items if there is already a data load running.
3386 * It is currently disabled.
3389 get_next_work_item(ArchiveHandle
*AH
, TocEntry
**first_unprocessed
,
3390 ParallelSlot
*slots
, int n_slots
)
3392 bool pref_non_data
= false; /* or get from AH->ropt */
3393 TocEntry
*data_te
= NULL
;
3399 * Bogus heuristics for pref_non_data
3405 for (k
= 0; k
< n_slots
; k
++)
3406 if (slots
[k
].args
->te
!= NULL
&&
3407 slots
[k
].args
->te
->section
== SECTION_DATA
)
3409 if (n_slots
== 0 || count
* 4 < n_slots
)
3410 pref_non_data
= false;
3414 * Advance first_unprocessed if possible.
3416 for (te
= *first_unprocessed
; te
!= AH
->toc
; te
= te
->next
)
3421 *first_unprocessed
= te
;
3424 * Search from first_unprocessed until we find an available item.
3426 for (; te
!= AH
->toc
; te
= te
->next
)
3428 bool conflicts
= false;
3430 /* Ignore if already done or still waiting on dependencies */
3431 if (te
->restored
|| te
->depCount
> 0)
3435 * Check to see if the item would need exclusive lock on something
3436 * that a currently running item also needs lock on, or vice versa. If
3437 * so, we don't want to schedule them together.
3439 for (i
= 0; i
< n_slots
&& !conflicts
; i
++)
3441 TocEntry
*running_te
;
3443 if (slots
[i
].args
== NULL
)
3445 running_te
= slots
[i
].args
->te
;
3447 if (has_lock_conflicts(te
, running_te
) ||
3448 has_lock_conflicts(running_te
, te
))
3458 if (pref_non_data
&& te
->section
== SECTION_DATA
)
3460 if (data_te
== NULL
)
3465 /* passed all tests, so this item can run */
3469 if (data_te
!= NULL
)
3472 ahlog(AH
, 2, "no item ready\n");
3478 * Restore a single TOC item in parallel with others
3480 * this is the procedure run as a thread (Windows) or a
3481 * separate process (everything else).
3483 static parallel_restore_result
3484 parallel_restore(RestoreArgs
*args
)
3486 ArchiveHandle
*AH
= args
->AH
;
3487 TocEntry
*te
= args
->te
;
3488 RestoreOptions
*ropt
= AH
->ropt
;
3492 * Close and reopen the input file so we have a private file pointer that
3493 * doesn't stomp on anyone else's file pointer, if we're actually going to
3494 * need to read from the file. Otherwise, just close it except on Windows,
3495 * where it will possibly be needed by other threads.
3497 * Note: on Windows, since we are using threads not processes, the reopen
3498 * call *doesn't* close the original file pointer but just open a new one.
3500 if (te
->section
== SECTION_DATA
)
3501 (AH
->ReopenPtr
) (AH
);
3504 (AH
->ClosePtr
) (AH
);
3508 * We need our own database connection, too
3510 ConnectDatabase((Archive
*) AH
, ropt
->dbname
,
3511 ropt
->pghost
, ropt
->pgport
, ropt
->username
,
3512 ropt
->promptPassword
);
3514 _doSetFixedOutputState(AH
);
3516 /* Restore the TOC item */
3517 retval
= restore_toc_entry(AH
, te
, ropt
, true);
3520 PQfinish(AH
->connection
);
3521 AH
->connection
= NULL
;
3523 /* If we reopened the file, we are done with it, so close it now */
3524 if (te
->section
== SECTION_DATA
)
3525 (AH
->ClosePtr
) (AH
);
3527 if (retval
== 0 && AH
->public.n_errors
)
3528 retval
= WORKER_IGNORED_ERRORS
;
3539 * Housekeeping to be done after a step has been parallel restored.
3541 * Clear the appropriate slot, free all the extra memory we allocated,
3542 * update status, and reduce the dependency count of any dependent items.
3545 mark_work_done(ArchiveHandle
*AH
, thandle worker
, int status
,
3546 ParallelSlot
*slots
, int n_slots
)
3548 TocEntry
*te
= NULL
;
3551 for (i
= 0; i
< n_slots
; i
++)
3553 if (slots
[i
].child_id
== worker
)
3555 slots
[i
].child_id
= 0;
3556 te
= slots
[i
].args
->te
;
3557 DeCloneArchive(slots
[i
].args
->AH
);
3558 free(slots
[i
].args
);
3559 slots
[i
].args
= NULL
;
3566 die_horribly(AH
, modulename
, "could not find slot of finished worker\n");
3568 ahlog(AH
, 1, "finished item %d %s %s\n",
3569 te
->dumpId
, te
->desc
, te
->tag
);
3571 if (status
== WORKER_CREATE_DONE
)
3572 mark_create_done(AH
, te
);
3573 else if (status
== WORKER_INHIBIT_DATA
)
3575 inhibit_data_for_failed_table(AH
, te
);
3576 AH
->public.n_errors
++;
3578 else if (status
== WORKER_IGNORED_ERRORS
)
3579 AH
->public.n_errors
++;
3580 else if (status
!= 0)
3581 die_horribly(AH
, modulename
, "worker process failed: exit code %d\n",
3584 reduce_dependencies(AH
, te
);
3589 * Process the dependency information into a form useful for parallel restore.
3591 * We set up depCount fields that are the number of as-yet-unprocessed
3592 * dependencies for each TOC entry.
3594 * We also identify locking dependencies so that we can avoid trying to
3595 * schedule conflicting items at the same time.
3598 fix_dependencies(ArchiveHandle
*AH
)
3600 TocEntry
**tocsByDumpId
;
3605 * For some of the steps here, it is convenient to have an array that
3606 * indexes the TOC entries by dump ID, rather than searching the TOC list
3607 * repeatedly. Entries for dump IDs not present in the TOC will be NULL.
3609 * Also, initialize the depCount fields.
3611 tocsByDumpId
= (TocEntry
**) calloc(AH
->maxDumpId
, sizeof(TocEntry
*));
3612 for (te
= AH
->toc
->next
; te
!= AH
->toc
; te
= te
->next
)
3614 tocsByDumpId
[te
->dumpId
- 1] = te
;
3615 te
->depCount
= te
->nDeps
;
3619 * POST_DATA items that are shown as depending on a table need to be
3620 * re-pointed to depend on that table's data, instead. This ensures they
3621 * won't get scheduled until the data has been loaded. We handle this by
3622 * first finding TABLE/TABLE DATA pairs and then scanning all the
3625 * Note: currently, a TABLE DATA should always have exactly one
3626 * dependency, on its TABLE item. So we don't bother to search, but look
3627 * just at the first dependency. We do trouble to make sure that it's a
3628 * TABLE, if possible. However, if the dependency isn't in the archive
3629 * then just assume it was a TABLE; this is to cover cases where the table
3630 * was suppressed but we have the data and some dependent post-data items.
3632 for (te
= AH
->toc
->next
; te
!= AH
->toc
; te
= te
->next
)
3634 if (strcmp(te
->desc
, "TABLE DATA") == 0 && te
->nDeps
> 0)
3636 DumpId tableId
= te
->dependencies
[0];
3638 if (tocsByDumpId
[tableId
- 1] == NULL
||
3639 strcmp(tocsByDumpId
[tableId
- 1]->desc
, "TABLE") == 0)
3641 repoint_table_dependencies(AH
, tableId
, te
->dumpId
);
3647 * Pre-8.4 versions of pg_dump neglected to set up a dependency from BLOB
3648 * COMMENTS to BLOBS. Cope. (We assume there's only one BLOBS and only
3649 * one BLOB COMMENTS in such files.)
3651 if (AH
->version
< K_VERS_1_11
)
3653 for (te
= AH
->toc
->next
; te
!= AH
->toc
; te
= te
->next
)
3655 if (strcmp(te
->desc
, "BLOB COMMENTS") == 0 && te
->nDeps
== 0)
3659 for (te2
= AH
->toc
->next
; te2
!= AH
->toc
; te2
= te2
->next
)
3661 if (strcmp(te2
->desc
, "BLOBS") == 0)
3663 te
->dependencies
= (DumpId
*) malloc(sizeof(DumpId
));
3664 te
->dependencies
[0] = te2
->dumpId
;
3676 * It is possible that the dependencies list items that are not in the
3677 * archive at all. Subtract such items from the depCounts.
3679 for (te
= AH
->toc
->next
; te
!= AH
->toc
; te
= te
->next
)
3681 for (i
= 0; i
< te
->nDeps
; i
++)
3683 if (tocsByDumpId
[te
->dependencies
[i
] - 1] == NULL
)
3689 * Lastly, work out the locking dependencies.
3691 for (te
= AH
->toc
->next
; te
!= AH
->toc
; te
= te
->next
)
3693 te
->lockDeps
= NULL
;
3695 identify_locking_dependencies(te
, tocsByDumpId
);
3702 * Change dependencies on tableId to depend on tableDataId instead,
3703 * but only in POST_DATA items.
3706 repoint_table_dependencies(ArchiveHandle
*AH
,
3707 DumpId tableId
, DumpId tableDataId
)
3712 for (te
= AH
->toc
->next
; te
!= AH
->toc
; te
= te
->next
)
3714 if (te
->section
!= SECTION_POST_DATA
)
3716 for (i
= 0; i
< te
->nDeps
; i
++)
3718 if (te
->dependencies
[i
] == tableId
)
3720 te
->dependencies
[i
] = tableDataId
;
3721 ahlog(AH
, 2, "transferring dependency %d -> %d to %d\n",
3722 te
->dumpId
, tableId
, tableDataId
);
3729 * Identify which objects we'll need exclusive lock on in order to restore
3730 * the given TOC entry (*other* than the one identified by the TOC entry
3731 * itself). Record their dump IDs in the entry's lockDeps[] array.
3732 * tocsByDumpId[] is a convenience array to avoid searching the TOC
3733 * for each dependency.
3736 identify_locking_dependencies(TocEntry
*te
, TocEntry
**tocsByDumpId
)
3742 /* Quick exit if no dependencies at all */
3746 /* Exit if this entry doesn't need exclusive lock on other objects */
3747 if (!(strcmp(te
->desc
, "CONSTRAINT") == 0 ||
3748 strcmp(te
->desc
, "CHECK CONSTRAINT") == 0 ||
3749 strcmp(te
->desc
, "FK CONSTRAINT") == 0 ||
3750 strcmp(te
->desc
, "RULE") == 0 ||
3751 strcmp(te
->desc
, "TRIGGER") == 0))
3755 * We assume the item requires exclusive lock on each TABLE DATA item
3756 * listed among its dependencies. (This was originally a dependency on
3757 * the TABLE, but fix_dependencies repointed it to the data item. Note
3758 * that all the entry types we are interested in here are POST_DATA, so
3759 * they will all have been changed this way.)
3761 lockids
= (DumpId
*) malloc(te
->nDeps
* sizeof(DumpId
));
3763 for (i
= 0; i
< te
->nDeps
; i
++)
3765 DumpId depid
= te
->dependencies
[i
];
3767 if (tocsByDumpId
[depid
- 1] &&
3768 strcmp(tocsByDumpId
[depid
- 1]->desc
, "TABLE DATA") == 0)
3769 lockids
[nlockids
++] = depid
;
3778 te
->lockDeps
= realloc(lockids
, nlockids
* sizeof(DumpId
));
3779 te
->nLockDeps
= nlockids
;
3783 * Remove the specified TOC entry from the depCounts of items that depend on
3784 * it, thereby possibly making them ready-to-run.
3787 reduce_dependencies(ArchiveHandle
*AH
, TocEntry
*te
)
3789 DumpId target
= te
->dumpId
;
3792 ahlog(AH
, 2, "reducing dependencies for %d\n", target
);
3795 * We must examine all entries, not only the ones after the target item,
3796 * because if the user used a -L switch then the original dependency-
3797 * respecting order has been destroyed by SortTocFromFile.
3799 for (te
= AH
->toc
->next
; te
!= AH
->toc
; te
= te
->next
)
3801 for (i
= 0; i
< te
->nDeps
; i
++)
3803 if (te
->dependencies
[i
] == target
)
3810 * Set the created flag on the DATA member corresponding to the given
3814 mark_create_done(ArchiveHandle
*AH
, TocEntry
*te
)
3818 for (tes
= AH
->toc
->next
; tes
!= AH
->toc
; tes
= tes
->next
)
3820 if (strcmp(tes
->desc
, "TABLE DATA") == 0 &&
3821 strcmp(tes
->tag
, te
->tag
) == 0 &&
3822 strcmp(tes
->namespace ? tes
->namespace : "",
3823 te
->namespace ? te
->namespace : "") == 0)
3825 tes
->created
= true;
3832 * Mark the DATA member corresponding to the given TABLE member
3836 inhibit_data_for_failed_table(ArchiveHandle
*AH
, TocEntry
*te
)
3838 RestoreOptions
*ropt
= AH
->ropt
;
3841 ahlog(AH
, 1, "table \"%s\" could not be created, will not restore its data\n",
3844 for (tes
= AH
->toc
->next
; tes
!= AH
->toc
; tes
= tes
->next
)
3846 if (strcmp(tes
->desc
, "TABLE DATA") == 0 &&
3847 strcmp(tes
->tag
, te
->tag
) == 0 &&
3848 strcmp(tes
->namespace ? tes
->namespace : "",
3849 te
->namespace ? te
->namespace : "") == 0)
3851 /* mark it unwanted; we assume idWanted array already exists */
3852 ropt
->idWanted
[tes
->dumpId
- 1] = false;
3860 * Clone and de-clone routines used in parallel restoration.
3862 * Enough of the structure is cloned to ensure that there is no
3863 * conflict between different threads each with their own clone.
3865 * These could be public, but no need at present.
3867 static ArchiveHandle
*
3868 CloneArchive(ArchiveHandle
*AH
)
3870 ArchiveHandle
*clone
;
3872 /* Make a "flat" copy */
3873 clone
= (ArchiveHandle
*) malloc(sizeof(ArchiveHandle
));
3875 die_horribly(AH
, modulename
, "out of memory\n");
3876 memcpy(clone
, AH
, sizeof(ArchiveHandle
));
3878 /* Handle format-independent fields */
3879 clone
->pgCopyBuf
= createPQExpBuffer();
3880 clone
->sqlBuf
= createPQExpBuffer();
3881 clone
->sqlparse
.tagBuf
= NULL
;
3883 /* The clone will have its own connection, so disregard connection state */
3884 clone
->connection
= NULL
;
3885 clone
->currUser
= NULL
;
3886 clone
->currSchema
= NULL
;
3887 clone
->currTablespace
= NULL
;
3888 clone
->currWithOids
= -1;
3890 /* savedPassword must be local in case we change it while connecting */
3891 if (clone
->savedPassword
)
3892 clone
->savedPassword
= strdup(clone
->savedPassword
);
3894 /* clone has its own error count, too */
3895 clone
->public.n_errors
= 0;
3897 /* Let the format-specific code have a chance too */
3898 (clone
->ClonePtr
) (clone
);
3904 * Release clone-local storage.
3906 * Note: we assume any clone-local connection was already closed.
3909 DeCloneArchive(ArchiveHandle
*AH
)
3911 /* Clear format-specific state */
3912 (AH
->DeClonePtr
) (AH
);
3914 /* Clear state allocated by CloneArchive */
3915 destroyPQExpBuffer(AH
->pgCopyBuf
);
3916 destroyPQExpBuffer(AH
->sqlBuf
);
3917 if (AH
->sqlparse
.tagBuf
)
3918 destroyPQExpBuffer(AH
->sqlparse
.tagBuf
);
3920 /* Clear any connection-local state */
3924 free(AH
->currSchema
);
3925 if (AH
->currTablespace
)
3926 free(AH
->currTablespace
);
3927 if (AH
->savedPassword
)
3928 free(AH
->savedPassword
);