1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-27 07:42:10 +03:00

Access pg_dump's options structs through Archive struct, not directly.

Rather than passing around DumpOptions and RestoreOptions as separate
arguments, add fields to struct Archive to carry pointers to these objects,
and access them through those fields when needed.  There already was a
RestoreOptions pointer in Archive, though for no obvious reason it was part
of the "private" struct rather than out where pg_dump.c could see it.

Doing this allows reversion of quite a lot of parameter-addition changes
made in commit 0eea8047bf, which is a good thing IMO because this will
reduce the code delta between 9.4 and 9.5, probably easing a few future
back-patch efforts.  Moreover, the previous commit only added a DumpOptions
argument to functions that had to have it at the time, which means we could
anticipate still more code churn (and more back-patch hazard) as the
requirement spread further.  I'd hit exactly that problem in my upcoming
patch to fix extension membership marking, which is what motivated me to
do this.
This commit is contained in:
Tom Lane
2016-01-13 17:48:33 -05:00
parent 5ef26b8de3
commit 6adba13e42
13 changed files with 486 additions and 405 deletions

View File

@@ -57,7 +57,7 @@ static ArchiveHandle *_allocAH(const char *FileSpec, const ArchiveFormat fmt,
const int compression, ArchiveMode mode, SetupWorkerPtr setupWorkerPtr);
static void _getObjectDescription(PQExpBuffer buf, TocEntry *te,
ArchiveHandle *AH);
static void _printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isData, bool acl_pass);
static void _printTocEntry(ArchiveHandle *AH, TocEntry *te, bool isData, bool acl_pass);
static char *replace_line_endings(const char *str);
static void _doSetFixedOutputState(ArchiveHandle *AH);
static void _doSetSessionAuth(ArchiveHandle *AH, const char *user);
@@ -71,8 +71,8 @@ static void processEncodingEntry(ArchiveHandle *AH, TocEntry *te);
static void processStdStringsEntry(ArchiveHandle *AH, TocEntry *te);
static teReqs _tocEntryRequired(TocEntry *te, teSection curSection, RestoreOptions *ropt);
static bool _tocEntryIsACL(TocEntry *te);
static void _disableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
static void _enableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
static void _disableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te);
static void _enableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te);
static void buildTocEntryArrays(ArchiveHandle *AH);
static void _moveBefore(ArchiveHandle *AH, TocEntry *pos, TocEntry *te);
static int _discoverArchiveFormat(ArchiveHandle *AH);
@@ -84,8 +84,7 @@ static void SetOutput(ArchiveHandle *AH, const char *filename, int compression);
static OutputContext SaveOutput(ArchiveHandle *AH);
static void RestoreOutput(ArchiveHandle *AH, OutputContext savedContext);
static int restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
RestoreOptions *ropt, bool is_parallel);
static int restore_toc_entry(ArchiveHandle *AH, TocEntry *te, bool is_parallel);
static void restore_toc_entries_prefork(ArchiveHandle *AH);
static void restore_toc_entries_parallel(ArchiveHandle *AH, ParallelState *pstate,
TocEntry *pending_list);
@@ -181,7 +180,7 @@ dumpOptionsFromRestoreOptions(RestoreOptions *ropt)
* setup doesn't need to know anything much, so it's defined here.
*/
static void
setupRestoreWorker(Archive *AHX, DumpOptions *dopt, RestoreOptions *ropt)
setupRestoreWorker(Archive *AHX)
{
ArchiveHandle *AH = (ArchiveHandle *) AHX;
@@ -213,12 +212,12 @@ OpenArchive(const char *FileSpec, const ArchiveFormat fmt)
/* Public */
void
CloseArchive(Archive *AHX, DumpOptions *dopt)
CloseArchive(Archive *AHX)
{
int res = 0;
ArchiveHandle *AH = (ArchiveHandle *) AHX;
(*AH->ClosePtr) (AH, dopt);
(*AH->ClosePtr) (AH);
/* Close the output */
if (AH->gzOut)
@@ -233,14 +232,25 @@ CloseArchive(Archive *AHX, DumpOptions *dopt)
/* Public */
void
SetArchiveRestoreOptions(Archive *AHX, RestoreOptions *ropt)
SetArchiveOptions(Archive *AH, DumpOptions *dopt, RestoreOptions *ropt)
{
ArchiveHandle *AH = (ArchiveHandle *) AHX;
TocEntry *te;
teSection curSection;
/* Caller can omit dump options, in which case we synthesize them */
if (dopt == NULL && ropt != NULL)
dopt = dumpOptionsFromRestoreOptions(ropt);
/* Save options for later access */
AH->dopt = dopt;
AH->ropt = ropt;
}
/* Public */
void
ProcessArchiveRestoreOptions(Archive *AHX)
{
ArchiveHandle *AH = (ArchiveHandle *) AHX;
RestoreOptions *ropt = AH->public.ropt;
TocEntry *te;
teSection curSection;
/* Decide which TOC entries will be dumped/restored, and mark them */
curSection = SECTION_PRE_DATA;
@@ -291,7 +301,7 @@ void
RestoreArchive(Archive *AHX)
{
ArchiveHandle *AH = (ArchiveHandle *) AHX;
RestoreOptions *ropt = AH->ropt;
RestoreOptions *ropt = AH->public.ropt;
bool parallel_mode;
TocEntry *te;
OutputContext sav;
@@ -598,7 +608,7 @@ RestoreArchive(Archive *AHX)
Assert(AH->connection == NULL);
/* ParallelBackupStart() will actually fork the processes */
pstate = ParallelBackupStart(AH, NULL, ropt);
pstate = ParallelBackupStart(AH);
restore_toc_entries_parallel(AH, pstate, &pending_list);
ParallelBackupEnd(AH, pstate);
@@ -609,7 +619,7 @@ RestoreArchive(Archive *AHX)
else
{
for (te = AH->toc->next; te != AH->toc; te = te->next)
(void) restore_toc_entry(AH, te, ropt, false);
(void) restore_toc_entry(AH, te, false);
}
/*
@@ -629,7 +639,7 @@ RestoreArchive(Archive *AHX)
else
ahlog(AH, 1, "setting owner and privileges for %s \"%s\"\n",
te->desc, te->tag);
_printTocEntry(AH, te, ropt, false, true);
_printTocEntry(AH, te, false, true);
}
}
@@ -666,9 +676,9 @@ RestoreArchive(Archive *AHX)
* the parallel parent has to make the corresponding status update.
*/
static int
restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
RestoreOptions *ropt, bool is_parallel)
restore_toc_entry(ArchiveHandle *AH, TocEntry *te, bool is_parallel)
{
RestoreOptions *ropt = AH->public.ropt;
int status = WORKER_OK;
teReqs reqs;
bool defnDumped;
@@ -710,7 +720,7 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
ahlog(AH, 1, "creating %s \"%s\"\n", te->desc, te->tag);
_printTocEntry(AH, te, ropt, false, false);
_printTocEntry(AH, te, false, false);
defnDumped = true;
if (strcmp(te->desc, "TABLE") == 0)
@@ -775,7 +785,7 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
*/
if (AH->PrintTocDataPtr !=NULL)
{
_printTocEntry(AH, te, ropt, true, false);
_printTocEntry(AH, te, true, false);
if (strcmp(te->desc, "BLOBS") == 0 ||
strcmp(te->desc, "BLOB COMMENTS") == 0)
@@ -788,13 +798,13 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
if (strcmp(te->desc, "BLOB COMMENTS") == 0)
AH->outputKind = OUTPUT_OTHERDATA;
(*AH->PrintTocDataPtr) (AH, te, ropt);
(*AH->PrintTocDataPtr) (AH, te);
AH->outputKind = OUTPUT_SQLCMDS;
}
else
{
_disableTriggersIfNecessary(AH, te, ropt);
_disableTriggersIfNecessary(AH, te);
/* Select owner and schema as necessary */
_becomeOwner(AH, te);
@@ -841,7 +851,7 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
else
AH->outputKind = OUTPUT_OTHERDATA;
(*AH->PrintTocDataPtr) (AH, te, ropt);
(*AH->PrintTocDataPtr) (AH, te);
/*
* Terminate COPY if needed.
@@ -855,7 +865,7 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
if (is_parallel && te->created)
CommitTransaction(&AH->public);
_enableTriggersIfNecessary(AH, te, ropt);
_enableTriggersIfNecessary(AH, te);
}
}
}
@@ -863,7 +873,7 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
{
/* If we haven't already dumped the defn part, do so now */
ahlog(AH, 1, "executing %s %s\n", te->desc, te->tag);
_printTocEntry(AH, te, ropt, false, false);
_printTocEntry(AH, te, false, false);
}
}
@@ -893,8 +903,10 @@ NewRestoreOptions(void)
}
static void
_disableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
_disableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te)
{
RestoreOptions *ropt = AH->public.ropt;
/* This hack is only needed in a data-only restore */
if (!ropt->dataOnly || !ropt->disable_triggers)
return;
@@ -919,8 +931,10 @@ _disableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te, RestoreOptions *rop
}
static void
_enableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
_enableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te)
{
RestoreOptions *ropt = AH->public.ropt;
/* This hack is only needed in a data-only restore */
if (!ropt->dataOnly || !ropt->disable_triggers)
return;
@@ -1033,9 +1047,10 @@ ArchiveEntry(Archive *AHX,
/* Public */
void
PrintTOCSummary(Archive *AHX, RestoreOptions *ropt)
PrintTOCSummary(Archive *AHX)
{
ArchiveHandle *AH = (ArchiveHandle *) AHX;
RestoreOptions *ropt = AH->public.ropt;
TocEntry *te;
teSection curSection;
OutputContext sav;
@@ -1148,7 +1163,9 @@ EndBlob(Archive *AHX, Oid oid)
void
StartRestoreBlobs(ArchiveHandle *AH)
{
if (!AH->ropt->single_txn)
RestoreOptions *ropt = AH->public.ropt;
if (!ropt->single_txn)
{
if (AH->connection)
StartTransaction(&AH->public);
@@ -1165,7 +1182,9 @@ StartRestoreBlobs(ArchiveHandle *AH)
void
EndRestoreBlobs(ArchiveHandle *AH)
{
if (!AH->ropt->single_txn)
RestoreOptions *ropt = AH->public.ropt;
if (!ropt->single_txn)
{
if (AH->connection)
CommitTransaction(&AH->public);
@@ -1254,9 +1273,10 @@ EndRestoreBlob(ArchiveHandle *AH, Oid oid)
***********/
void
SortTocFromFile(Archive *AHX, RestoreOptions *ropt)
SortTocFromFile(Archive *AHX)
{
ArchiveHandle *AH = (ArchiveHandle *) AHX;
RestoreOptions *ropt = AH->public.ropt;
FILE *fh;
char buf[100];
bool incomplete_line;
@@ -1539,7 +1559,9 @@ ahlog(ArchiveHandle *AH, int level, const char *fmt,...)
static int
RestoringToDB(ArchiveHandle *AH)
{
return (AH->ropt && AH->ropt->useDB && AH->connection);
RestoreOptions *ropt = AH->public.ropt;
return (ropt && ropt->useDB && AH->connection);
}
/*
@@ -2292,7 +2314,7 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt,
}
void
WriteDataChunks(ArchiveHandle *AH, DumpOptions *dopt, ParallelState *pstate)
WriteDataChunks(ArchiveHandle *AH, ParallelState *pstate)
{
TocEntry *te;
@@ -2315,13 +2337,13 @@ WriteDataChunks(ArchiveHandle *AH, DumpOptions *dopt, ParallelState *pstate)
DispatchJobForTocEntry(AH, pstate, te, ACT_DUMP);
}
else
WriteDataChunksForTocEntry(AH, dopt, te);
WriteDataChunksForTocEntry(AH, te);
}
EnsureWorkersFinished(AH, pstate);
}
void
WriteDataChunksForTocEntry(ArchiveHandle *AH, DumpOptions *dopt, TocEntry *te)
WriteDataChunksForTocEntry(ArchiveHandle *AH, TocEntry *te)
{
StartDataPtr startPtr;
EndDataPtr endPtr;
@@ -2345,7 +2367,7 @@ WriteDataChunksForTocEntry(ArchiveHandle *AH, DumpOptions *dopt, TocEntry *te)
/*
* The user-provided DataDumper routine needs to call AH->WriteData
*/
(*te->dataDumper) ((Archive *) AH, dopt, te->dataDumperArg);
(*te->dataDumper) ((Archive *) AH, te->dataDumperArg);
if (endPtr != NULL)
(*endPtr) (AH, te);
@@ -2767,6 +2789,8 @@ _tocEntryIsACL(TocEntry *te)
static void
_doSetFixedOutputState(ArchiveHandle *AH)
{
RestoreOptions *ropt = AH->public.ropt;
/* Disable statement_timeout since restore is probably slow */
ahprintf(AH, "SET statement_timeout = 0;\n");
@@ -2782,8 +2806,8 @@ _doSetFixedOutputState(ArchiveHandle *AH)
AH->public.std_strings ? "on" : "off");
/* Select the role to be used during restore */
if (AH->ropt && AH->ropt->use_role)
ahprintf(AH, "SET ROLE %s;\n", fmtId(AH->ropt->use_role));
if (ropt && ropt->use_role)
ahprintf(AH, "SET ROLE %s;\n", fmtId(ropt->use_role));
/* Make sure function checking is disabled */
ahprintf(AH, "SET check_function_bodies = false;\n");
@@ -2794,7 +2818,7 @@ _doSetFixedOutputState(ArchiveHandle *AH)
ahprintf(AH, "SET escape_string_warning = off;\n");
/* Adjust row-security state */
if (AH->ropt && AH->ropt->enable_row_security)
if (ropt && ropt->enable_row_security)
ahprintf(AH, "SET row_security = on;\n");
else
ahprintf(AH, "SET row_security = off;\n");
@@ -2952,7 +2976,9 @@ _becomeUser(ArchiveHandle *AH, const char *user)
static void
_becomeOwner(ArchiveHandle *AH, TocEntry *te)
{
if (AH->ropt && (AH->ropt->noOwner || !AH->ropt->use_setsessauth))
RestoreOptions *ropt = AH->public.ropt;
if (ropt && (ropt->noOwner || !ropt->use_setsessauth))
return;
_becomeUser(AH, te->owner);
@@ -3023,12 +3049,13 @@ _selectOutputSchema(ArchiveHandle *AH, const char *schemaName)
static void
_selectTablespace(ArchiveHandle *AH, const char *tablespace)
{
RestoreOptions *ropt = AH->public.ropt;
PQExpBuffer qry;
const char *want,
*have;
/* do nothing in --no-tablespaces mode */
if (AH->ropt->noTablespace)
if (ropt->noTablespace)
return;
have = AH->currTablespace;
@@ -3154,8 +3181,10 @@ _getObjectDescription(PQExpBuffer buf, TocEntry *te, ArchiveHandle *AH)
}
static void
_printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isData, bool acl_pass)
_printTocEntry(ArchiveHandle *AH, TocEntry *te, bool isData, bool acl_pass)
{
RestoreOptions *ropt = AH->public.ropt;
/* ACLs are dumped only during acl pass */
if (acl_pass)
{
@@ -3564,7 +3593,6 @@ dumpTimestamp(ArchiveHandle *AH, const char *msg, time_t tim)
static void
restore_toc_entries_prefork(ArchiveHandle *AH)
{
RestoreOptions *ropt = AH->ropt;
bool skipped_some;
TocEntry *next_work_item;
@@ -3616,7 +3644,7 @@ restore_toc_entries_prefork(ArchiveHandle *AH)
next_work_item->dumpId,
next_work_item->desc, next_work_item->tag);
(void) restore_toc_entry(AH, next_work_item, ropt, false);
(void) restore_toc_entry(AH, next_work_item, false);
/* there should be no touch of ready_list here, so pass NULL */
reduce_dependencies(AH, next_work_item, NULL);
@@ -3797,7 +3825,7 @@ restore_toc_entries_parallel(ArchiveHandle *AH, ParallelState *pstate,
static void
restore_toc_entries_postfork(ArchiveHandle *AH, TocEntry *pending_list)
{
RestoreOptions *ropt = AH->ropt;
RestoreOptions *ropt = AH->public.ropt;
TocEntry *te;
ahlog(AH, 2, "entering restore_toc_entries_postfork\n");
@@ -3820,7 +3848,7 @@ restore_toc_entries_postfork(ArchiveHandle *AH, TocEntry *pending_list)
{
ahlog(AH, 1, "processing missed item %d %s %s\n",
te->dumpId, te->desc, te->tag);
(void) restore_toc_entry(AH, te, ropt, false);
(void) restore_toc_entry(AH, te, false);
}
/* The ACLs will be handled back in RestoreArchive. */
@@ -3985,7 +4013,6 @@ parallel_restore(ParallelArgs *args)
{
ArchiveHandle *AH = args->AH;
TocEntry *te = args->te;
RestoreOptions *ropt = AH->ropt;
int status;
_doSetFixedOutputState(AH);
@@ -3995,7 +4022,7 @@ parallel_restore(ParallelArgs *args)
AH->public.n_errors = 0;
/* Restore the TOC item */
status = restore_toc_entry(AH, te, ropt, true);
status = restore_toc_entry(AH, te, true);
return status;
}
@@ -4357,7 +4384,7 @@ CloneArchive(ArchiveHandle *AH)
*/
if (AH->mode == archModeRead)
{
RestoreOptions *ropt = AH->ropt;
RestoreOptions *ropt = AH->public.ropt;
Assert(AH->connection == NULL);
/* this also sets clone->connection */