Commit 775f1b37 authored by Andrew Dunstan's avatar Andrew Dunstan

Provide for parallel restoration from a custom format archive. Each data and

post-data step is run in a separate worker child (a thread on Windows, a child
process elsewhere) up to the concurrent number specified by the new pg_restore
command-line --multi-thread | -m switch.

Andrew Dunstan, with some editing by Tom Lane.
parent 3a5b7737
<!-- $PostgreSQL: pgsql/doc/src/sgml/ref/pg_restore.sgml,v 1.77 2009/01/05 16:54:36 tgl Exp $ --> <!-- $PostgreSQL: pgsql/doc/src/sgml/ref/pg_restore.sgml,v 1.78 2009/02/02 20:07:36 adunstan Exp $ -->
<refentry id="APP-PGRESTORE"> <refentry id="APP-PGRESTORE">
<refmeta> <refmeta>
...@@ -241,6 +241,28 @@ ...@@ -241,6 +241,28 @@
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><option>-m <replaceable class="parameter">number-of-threads</replaceable></option></term>
<term><option>--multi-thread=<replaceable class="parameter">number-of-threads</replaceable></option></term>
<listitem>
<para>
Run the most time-consuming parts of <application>pg_restore</>
&mdash; those which load data, create indexes, or create
constraints &mdash; using multiple concurrent connections to the
database. This option can dramatically reduce the time to restore a
large database to a server running on a multi-processor machine.
</para>
<para>
This option is ignored when emitting a script rather than connecting
directly to a database server. Multiple threads cannot be used
together with <option>--single-transaction</option>. Also, the input
must be a plain file (not, for example, a pipe), and at present only
the custom archive format is supported.
</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><option>-n <replaceable class="parameter">namespace</replaceable></option></term> <term><option>-n <replaceable class="parameter">namespace</replaceable></option></term>
<term><option>--schema=<replaceable class="parameter">schema</replaceable></option></term> <term><option>--schema=<replaceable class="parameter">schema</replaceable></option></term>
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup.h,v 1.48 2009/01/05 16:54:36 tgl Exp $ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup.h,v 1.49 2009/02/02 20:07:36 adunstan Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -53,6 +53,14 @@ typedef enum _archiveMode ...@@ -53,6 +53,14 @@ typedef enum _archiveMode
archModeRead archModeRead
} ArchiveMode; } ArchiveMode;
typedef enum _teSection
{
SECTION_NONE = 1, /* COMMENTs, ACLs, etc; can be anywhere */
SECTION_PRE_DATA, /* stuff to be processed before data */
SECTION_DATA, /* TABLE DATA, BLOBS, BLOB COMMENTS */
SECTION_POST_DATA /* stuff to be processed after data */
} teSection;
/* /*
* We may want to have some more user-readable data, but in the mean * We may want to have some more user-readable data, but in the mean
* time this gives us some abstraction and type checking. * time this gives us some abstraction and type checking.
...@@ -124,6 +132,7 @@ typedef struct _restoreOptions ...@@ -124,6 +132,7 @@ typedef struct _restoreOptions
int suppressDumpWarnings; /* Suppress output of WARNING entries int suppressDumpWarnings; /* Suppress output of WARNING entries
* to stderr */ * to stderr */
bool single_txn; bool single_txn;
int number_of_threads;
bool *idWanted; /* array showing which dump IDs to emit */ bool *idWanted; /* array showing which dump IDs to emit */
} RestoreOptions; } RestoreOptions;
...@@ -152,7 +161,8 @@ extern void ArchiveEntry(Archive *AHX, ...@@ -152,7 +161,8 @@ extern void ArchiveEntry(Archive *AHX,
const char *tag, const char *tag,
const char *namespace, const char *tablespace, const char *namespace, const char *tablespace,
const char *owner, bool withOids, const char *owner, bool withOids,
const char *desc, const char *defn, const char *desc, teSection section,
const char *defn,
const char *dropStmt, const char *copyStmt, const char *dropStmt, const char *copyStmt,
const DumpId *deps, int nDeps, const DumpId *deps, int nDeps,
DataDumperPtr dumpFn, void *dumpArg); DataDumperPtr dumpFn, void *dumpArg);
......
This diff is collapsed.
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.h,v 1.76 2007/11/07 12:24:24 petere Exp $ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.h,v 1.77 2009/02/02 20:07:37 adunstan Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -62,7 +62,7 @@ typedef z_stream *z_streamp; ...@@ -62,7 +62,7 @@ typedef z_stream *z_streamp;
#endif #endif
#define K_VERS_MAJOR 1 #define K_VERS_MAJOR 1
#define K_VERS_MINOR 10 #define K_VERS_MINOR 11
#define K_VERS_REV 0 #define K_VERS_REV 0
/* Data block types */ /* Data block types */
...@@ -85,8 +85,9 @@ typedef z_stream *z_streamp; ...@@ -85,8 +85,9 @@ typedef z_stream *z_streamp;
#define K_VERS_1_9 (( (1 * 256 + 9) * 256 + 0) * 256 + 0) /* add default_with_oids #define K_VERS_1_9 (( (1 * 256 + 9) * 256 + 0) * 256 + 0) /* add default_with_oids
* tracking */ * tracking */
#define K_VERS_1_10 (( (1 * 256 + 10) * 256 + 0) * 256 + 0) /* add tablespace */ #define K_VERS_1_10 (( (1 * 256 + 10) * 256 + 0) * 256 + 0) /* add tablespace */
#define K_VERS_1_11 (( (1 * 256 + 11) * 256 + 0) * 256 + 0) /* add toc section indicator */
#define K_VERS_MAX (( (1 * 256 + 10) * 256 + 255) * 256 + 0) #define K_VERS_MAX (( (1 * 256 + 11) * 256 + 255) * 256 + 0)
/* Flags to indicate disposition of offsets stored in files */ /* Flags to indicate disposition of offsets stored in files */
...@@ -99,6 +100,7 @@ struct _tocEntry; ...@@ -99,6 +100,7 @@ struct _tocEntry;
struct _restoreList; struct _restoreList;
typedef void (*ClosePtr) (struct _archiveHandle * AH); typedef void (*ClosePtr) (struct _archiveHandle * AH);
typedef void (*ReopenPtr) (struct _archiveHandle * AH);
typedef void (*ArchiveEntryPtr) (struct _archiveHandle * AH, struct _tocEntry * te); typedef void (*ArchiveEntryPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
typedef void (*StartDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te); typedef void (*StartDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
...@@ -120,6 +122,9 @@ typedef void (*ReadExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * ...@@ -120,6 +122,9 @@ typedef void (*ReadExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry *
typedef void (*PrintExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te); typedef void (*PrintExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
typedef void (*PrintTocDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te, RestoreOptions *ropt); typedef void (*PrintTocDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te, RestoreOptions *ropt);
typedef void (*ClonePtr) (struct _archiveHandle * AH);
typedef void (*DeClonePtr) (struct _archiveHandle * AH);
typedef size_t (*CustomOutPtr) (struct _archiveHandle * AH, const void *buf, size_t len); typedef size_t (*CustomOutPtr) (struct _archiveHandle * AH, const void *buf, size_t len);
typedef struct _outputContext typedef struct _outputContext
...@@ -212,6 +217,7 @@ typedef struct _archiveHandle ...@@ -212,6 +217,7 @@ typedef struct _archiveHandle
WriteBufPtr WriteBufPtr; /* Write a buffer of output to the archive */ WriteBufPtr WriteBufPtr; /* Write a buffer of output to the archive */
ReadBufPtr ReadBufPtr; /* Read a buffer of input from the archive */ ReadBufPtr ReadBufPtr; /* Read a buffer of input from the archive */
ClosePtr ClosePtr; /* Close the archive */ ClosePtr ClosePtr; /* Close the archive */
ReopenPtr ReopenPtr; /* Reopen the archive */
WriteExtraTocPtr WriteExtraTocPtr; /* Write extra TOC entry data WriteExtraTocPtr WriteExtraTocPtr; /* Write extra TOC entry data
* associated with the current archive * associated with the current archive
* format */ * format */
...@@ -225,11 +231,15 @@ typedef struct _archiveHandle ...@@ -225,11 +231,15 @@ typedef struct _archiveHandle
StartBlobPtr StartBlobPtr; StartBlobPtr StartBlobPtr;
EndBlobPtr EndBlobPtr; EndBlobPtr EndBlobPtr;
ClonePtr ClonePtr; /* Clone format-specific fields */
DeClonePtr DeClonePtr; /* Clean up cloned fields */
CustomOutPtr CustomOutPtr; /* Alternative script output routine */ CustomOutPtr CustomOutPtr; /* Alternative script output routine */
/* Stuff for direct DB connection */ /* Stuff for direct DB connection */
char *archdbname; /* DB name *read* from archive */ char *archdbname; /* DB name *read* from archive */
bool requirePassword; bool requirePassword;
char *savedPassword; /* password for ropt->username, if known */
PGconn *connection; PGconn *connection;
int connectToDB; /* Flag to indicate if direct DB connection is int connectToDB; /* Flag to indicate if direct DB connection is
* required */ * required */
...@@ -260,9 +270,9 @@ typedef struct _archiveHandle ...@@ -260,9 +270,9 @@ typedef struct _archiveHandle
* etc */ * etc */
/* these vars track state to avoid sending redundant SET commands */ /* these vars track state to avoid sending redundant SET commands */
char *currUser; /* current username */ char *currUser; /* current username, or NULL if unknown */
char *currSchema; /* current schema */ char *currSchema; /* current schema, or NULL */
char *currTablespace; /* current tablespace */ char *currTablespace; /* current tablespace, or NULL */
bool currWithOids; /* current default_with_oids setting */ bool currWithOids; /* current default_with_oids setting */
void *lo_buf; void *lo_buf;
...@@ -282,6 +292,7 @@ typedef struct _tocEntry ...@@ -282,6 +292,7 @@ typedef struct _tocEntry
struct _tocEntry *next; struct _tocEntry *next;
CatalogId catalogId; CatalogId catalogId;
DumpId dumpId; DumpId dumpId;
teSection section;
bool hadDumper; /* Archiver was passed a dumper routine (used bool hadDumper; /* Archiver was passed a dumper routine (used
* in restore) */ * in restore) */
char *tag; /* index tag */ char *tag; /* index tag */
...@@ -300,6 +311,13 @@ typedef struct _tocEntry ...@@ -300,6 +311,13 @@ typedef struct _tocEntry
DataDumperPtr dataDumper; /* Routine to dump data for object */ DataDumperPtr dataDumper; /* Routine to dump data for object */
void *dataDumperArg; /* Arg for above routine */ void *dataDumperArg; /* Arg for above routine */
void *formatData; /* TOC Entry data specific to file format */ void *formatData; /* TOC Entry data specific to file format */
/* working state (needed only for parallel restore) */
bool restored; /* item is in progress or done */
bool created; /* set for DATA member if TABLE was created */
int depCount; /* number of dependencies not yet restored */
DumpId *lockDeps; /* dumpIds of objects this one needs lock on */
int nLockDeps; /* number of such dependencies */
} TocEntry; } TocEntry;
/* Used everywhere */ /* Used everywhere */
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.40 2007/10/28 21:55:52 tgl Exp $ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_custom.c,v 1.41 2009/02/02 20:07:37 adunstan Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -40,6 +40,7 @@ static int _ReadByte(ArchiveHandle *); ...@@ -40,6 +40,7 @@ static int _ReadByte(ArchiveHandle *);
static size_t _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len); static size_t _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len);
static size_t _ReadBuf(ArchiveHandle *AH, void *buf, size_t len); static size_t _ReadBuf(ArchiveHandle *AH, void *buf, size_t len);
static void _CloseArchive(ArchiveHandle *AH); static void _CloseArchive(ArchiveHandle *AH);
static void _ReopenArchive(ArchiveHandle *AH);
static void _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt); static void _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te); static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te);
static void _ReadExtraToc(ArchiveHandle *AH, TocEntry *te); static void _ReadExtraToc(ArchiveHandle *AH, TocEntry *te);
...@@ -54,6 +55,8 @@ static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid); ...@@ -54,6 +55,8 @@ static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid);
static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid); static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid);
static void _EndBlobs(ArchiveHandle *AH, TocEntry *te); static void _EndBlobs(ArchiveHandle *AH, TocEntry *te);
static void _LoadBlobs(ArchiveHandle *AH); static void _LoadBlobs(ArchiveHandle *AH);
static void _Clone(ArchiveHandle *AH);
static void _DeClone(ArchiveHandle *AH);
/*------------ /*------------
* Buffers used in zlib compression and extra data stored in archive and * Buffers used in zlib compression and extra data stored in archive and
...@@ -120,6 +123,7 @@ InitArchiveFmt_Custom(ArchiveHandle *AH) ...@@ -120,6 +123,7 @@ InitArchiveFmt_Custom(ArchiveHandle *AH)
AH->WriteBufPtr = _WriteBuf; AH->WriteBufPtr = _WriteBuf;
AH->ReadBufPtr = _ReadBuf; AH->ReadBufPtr = _ReadBuf;
AH->ClosePtr = _CloseArchive; AH->ClosePtr = _CloseArchive;
AH->ReopenPtr = _ReopenArchive;
AH->PrintTocDataPtr = _PrintTocData; AH->PrintTocDataPtr = _PrintTocData;
AH->ReadExtraTocPtr = _ReadExtraToc; AH->ReadExtraTocPtr = _ReadExtraToc;
AH->WriteExtraTocPtr = _WriteExtraToc; AH->WriteExtraTocPtr = _WriteExtraToc;
...@@ -129,6 +133,8 @@ InitArchiveFmt_Custom(ArchiveHandle *AH) ...@@ -129,6 +133,8 @@ InitArchiveFmt_Custom(ArchiveHandle *AH)
AH->StartBlobPtr = _StartBlob; AH->StartBlobPtr = _StartBlob;
AH->EndBlobPtr = _EndBlob; AH->EndBlobPtr = _EndBlob;
AH->EndBlobsPtr = _EndBlobs; AH->EndBlobsPtr = _EndBlobs;
AH->ClonePtr = _Clone;
AH->DeClonePtr = _DeClone;
/* /*
* Set up some special context used in compressing data. * Set up some special context used in compressing data.
...@@ -569,7 +575,6 @@ _PrintData(ArchiveHandle *AH) ...@@ -569,7 +575,6 @@ _PrintData(ArchiveHandle *AH)
zp->avail_in = blkLen; zp->avail_in = blkLen;
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
if (AH->compression != 0) if (AH->compression != 0)
{ {
while (zp->avail_in != 0) while (zp->avail_in != 0)
...@@ -585,15 +590,12 @@ _PrintData(ArchiveHandle *AH) ...@@ -585,15 +590,12 @@ _PrintData(ArchiveHandle *AH)
} }
} }
else else
{
#endif #endif
{
in[zp->avail_in] = '\0'; in[zp->avail_in] = '\0';
ahwrite(in, 1, zp->avail_in, AH); ahwrite(in, 1, zp->avail_in, AH);
zp->avail_in = 0; zp->avail_in = 0;
#ifdef HAVE_LIBZ
} }
#endif
blkLen = ReadInt(AH); blkLen = ReadInt(AH);
} }
...@@ -822,12 +824,10 @@ _CloseArchive(ArchiveHandle *AH) ...@@ -822,12 +824,10 @@ _CloseArchive(ArchiveHandle *AH)
* expect to be doing seeks to read the data back - it may be ok to * expect to be doing seeks to read the data back - it may be ok to
* just use the existing self-consistent block formatting. * just use the existing self-consistent block formatting.
*/ */
if (ctx->hasSeek) if (ctx->hasSeek &&
{ fseeko(AH->FH, tpos, SEEK_SET) == 0)
fseeko(AH->FH, tpos, SEEK_SET);
WriteToc(AH); WriteToc(AH);
} }
}
if (fclose(AH->FH) != 0) if (fclose(AH->FH) != 0)
die_horribly(AH, modulename, "could not close archive file: %s\n", strerror(errno)); die_horribly(AH, modulename, "could not close archive file: %s\n", strerror(errno));
...@@ -835,6 +835,48 @@ _CloseArchive(ArchiveHandle *AH) ...@@ -835,6 +835,48 @@ _CloseArchive(ArchiveHandle *AH)
AH->FH = NULL; AH->FH = NULL;
} }
/*
* Reopen the archive's file handle.
*
* We close the original file handle, except on Windows. (The difference
* is because on Windows, this is used within a multithreading context,
* and we don't want a thread closing the parent file handle.)
*/
static void
_ReopenArchive(ArchiveHandle *AH)
{
lclContext *ctx = (lclContext *) AH->formatData;
pgoff_t tpos;
if (AH->mode == archModeWrite)
die_horribly(AH,modulename,"can only reopen input archives\n");
if (AH->fSpec == NULL || strcmp(AH->fSpec, "") == 0)
die_horribly(AH,modulename,"cannot reopen stdin\n");
if (!ctx->hasSeek)
die_horribly(AH,modulename,"cannot reopen non-seekable file\n");
errno = 0;
tpos = ftello(AH->FH);
if (errno)
die_horribly(AH, modulename, "could not determine seek position in archive file: %s\n",
strerror(errno));
#ifndef WIN32
if (fclose(AH->FH) != 0)
die_horribly(AH, modulename, "could not close archive file: %s\n",
strerror(errno));
#endif
AH->FH = fopen(AH->fSpec, PG_BINARY_R);
if (!AH->FH)
die_horribly(AH, modulename, "could not open input file \"%s\": %s\n",
AH->fSpec, strerror(errno));
if (fseeko(AH->FH, tpos, SEEK_SET) != 0)
die_horribly(AH, modulename, "could not set seek position in archive file: %s\n",
strerror(errno));
}
/*-------------------------------------------------- /*--------------------------------------------------
* END OF FORMAT CALLBACKS * END OF FORMAT CALLBACKS
*-------------------------------------------------- *--------------------------------------------------
...@@ -990,7 +1032,6 @@ _DoDeflate(ArchiveHandle *AH, lclContext *ctx, int flush) ...@@ -990,7 +1032,6 @@ _DoDeflate(ArchiveHandle *AH, lclContext *ctx, int flush)
/* /*
* Terminate zlib context and flush it's buffers. If no zlib * Terminate zlib context and flush it's buffers. If no zlib
* then just return. * then just return.
*
*/ */
static void static void
_EndDataCompressor(ArchiveHandle *AH, TocEntry *te) _EndDataCompressor(ArchiveHandle *AH, TocEntry *te)
...@@ -1020,3 +1061,44 @@ _EndDataCompressor(ArchiveHandle *AH, TocEntry *te) ...@@ -1020,3 +1061,44 @@ _EndDataCompressor(ArchiveHandle *AH, TocEntry *te)
/* Send the end marker */ /* Send the end marker */
WriteInt(AH, 0); WriteInt(AH, 0);
} }
/*
* Clone format-specific fields during parallel restoration.
*/
static void
_Clone(ArchiveHandle *AH)
{
lclContext *ctx = (lclContext *) AH->formatData;
AH->formatData = (lclContext *) malloc(sizeof(lclContext));
if (AH->formatData == NULL)
die_horribly(AH, modulename, "out of memory\n");
memcpy(AH->formatData, ctx, sizeof(lclContext));
ctx = (lclContext *) AH->formatData;
ctx->zp = (z_streamp) malloc(sizeof(z_stream));
ctx->zlibOut = (char *) malloc(zlibOutSize + 1);
ctx->zlibIn = (char *) malloc(ctx->inSize);
if (ctx->zp == NULL || ctx->zlibOut == NULL || ctx->zlibIn == NULL)
die_horribly(AH, modulename, "out of memory\n");
/*
* Note: we do not make a local lo_buf because we expect at most one
* BLOBS entry per archive, so no parallelism is possible. Likewise,
* TOC-entry-local state isn't an issue because any one TOC entry is
* touched by just one worker child.
*/
}
static void
_DeClone(ArchiveHandle *AH)
{
lclContext *ctx = (lclContext *) AH->formatData;
free(ctx->zlibOut);
free(ctx->zlibIn);
free(ctx->zp);
free(ctx);
}
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
* Implements the basic DB functions used by the archiver. * Implements the basic DB functions used by the archiver.
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.80 2008/08/16 02:25:06 tgl Exp $ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.81 2009/02/02 20:07:37 adunstan Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -116,29 +116,36 @@ ReconnectToServer(ArchiveHandle *AH, const char *dbname, const char *username) ...@@ -116,29 +116,36 @@ ReconnectToServer(ArchiveHandle *AH, const char *dbname, const char *username)
/* /*
* Connect to the db again. * Connect to the db again.
*
* Note: it's not really all that sensible to use a single-entry password
* cache if the username keeps changing. In current usage, however, the
* username never does change, so one savedPassword is sufficient. We do
* update the cache on the off chance that the password has changed since the
* start of the run.
*/ */
static PGconn * static PGconn *
_connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser) _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser)
{ {
PGconn *newConn; PGconn *newConn;
char *newdb; const char *newdb;
char *newuser; const char *newuser;
char *password = NULL; char *password = AH->savedPassword;
bool new_pass; bool new_pass;
if (!reqdb) if (!reqdb)
newdb = PQdb(AH->connection); newdb = PQdb(AH->connection);
else else
newdb = (char *) reqdb; newdb = reqdb;
if (!requser || (strlen(requser) == 0)) if (!requser || strlen(requser) == 0)
newuser = PQuser(AH->connection); newuser = PQuser(AH->connection);
else else
newuser = (char *) requser; newuser = requser;
ahlog(AH, 1, "connecting to database \"%s\" as user \"%s\"\n", newdb, newuser); ahlog(AH, 1, "connecting to database \"%s\" as user \"%s\"\n",
newdb, newuser);
if (AH->requirePassword) if (AH->requirePassword && password == NULL)
{ {
password = simple_prompt("Password: ", 100, false); password = simple_prompt("Password: ", 100, false);
if (password == NULL) if (password == NULL)
...@@ -170,12 +177,13 @@ _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser) ...@@ -170,12 +177,13 @@ _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser)
if (password) if (password)
free(password); free(password);
password = simple_prompt("Password: ", 100, false); password = simple_prompt("Password: ", 100, false);
if (password == NULL)
die_horribly(AH, modulename, "out of memory\n");
new_pass = true; new_pass = true;
} }
} while (new_pass); } while (new_pass);
if (password) AH->savedPassword = password;
free(password);
/* check for version mismatch */ /* check for version mismatch */
_check_database_version(AH); _check_database_version(AH);
...@@ -190,6 +198,10 @@ _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser) ...@@ -190,6 +198,10 @@ _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser)
* Make a database connection with the given parameters. The * Make a database connection with the given parameters. The
* connection handle is returned, the parameters are stored in AHX. * connection handle is returned, the parameters are stored in AHX.
* An interactive password prompt is automatically issued if required. * An interactive password prompt is automatically issued if required.
*
* Note: it's not really all that sensible to use a single-entry password
* cache if the username keeps changing. In current usage, however, the
* username never does change, so one savedPassword is sufficient.
*/ */
PGconn * PGconn *
ConnectDatabase(Archive *AHX, ConnectDatabase(Archive *AHX,
...@@ -200,21 +212,19 @@ ConnectDatabase(Archive *AHX, ...@@ -200,21 +212,19 @@ ConnectDatabase(Archive *AHX,
int reqPwd) int reqPwd)
{ {
ArchiveHandle *AH = (ArchiveHandle *) AHX; ArchiveHandle *AH = (ArchiveHandle *) AHX;
char *password = NULL; char *password = AH->savedPassword;
bool new_pass; bool new_pass;
if (AH->connection) if (AH->connection)
die_horribly(AH, modulename, "already connected to a database\n"); die_horribly(AH, modulename, "already connected to a database\n");
if (reqPwd) if (reqPwd && password == NULL)
{ {
password = simple_prompt("Password: ", 100, false); password = simple_prompt("Password: ", 100, false);
if (password == NULL) if (password == NULL)
die_horribly(AH, modulename, "out of memory\n"); die_horribly(AH, modulename, "out of memory\n");
AH->requirePassword = true;
} }
else AH->requirePassword = reqPwd;
AH->requirePassword = false;
/* /*
* Start the connection. Loop until we have a password if requested by * Start the connection. Loop until we have a password if requested by
...@@ -236,12 +246,13 @@ ConnectDatabase(Archive *AHX, ...@@ -236,12 +246,13 @@ ConnectDatabase(Archive *AHX,
{ {
PQfinish(AH->connection); PQfinish(AH->connection);
password = simple_prompt("Password: ", 100, false); password = simple_prompt("Password: ", 100, false);
if (password == NULL)
die_horribly(AH, modulename, "out of memory\n");
new_pass = true; new_pass = true;
} }
} while (new_pass); } while (new_pass);
if (password) AH->savedPassword = password;
free(password);
/* check to see that the backend connection was successfully made */ /* check to see that the backend connection was successfully made */
if (PQstatus(AH->connection) == CONNECTION_BAD) if (PQstatus(AH->connection) == CONNECTION_BAD)
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.34 2007/10/28 21:55:52 tgl Exp $ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.35 2009/02/02 20:07:37 adunstan Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -87,6 +87,7 @@ InitArchiveFmt_Files(ArchiveHandle *AH) ...@@ -87,6 +87,7 @@ InitArchiveFmt_Files(ArchiveHandle *AH)
AH->WriteBufPtr = _WriteBuf; AH->WriteBufPtr = _WriteBuf;
AH->ReadBufPtr = _ReadBuf; AH->ReadBufPtr = _ReadBuf;
AH->ClosePtr = _CloseArchive; AH->ClosePtr = _CloseArchive;
AH->ReopenPtr = NULL;
AH->PrintTocDataPtr = _PrintTocData; AH->PrintTocDataPtr = _PrintTocData;
AH->ReadExtraTocPtr = _ReadExtraToc; AH->ReadExtraTocPtr = _ReadExtraToc;
AH->WriteExtraTocPtr = _WriteExtraToc; AH->WriteExtraTocPtr = _WriteExtraToc;
...@@ -96,6 +97,8 @@ InitArchiveFmt_Files(ArchiveHandle *AH) ...@@ -96,6 +97,8 @@ InitArchiveFmt_Files(ArchiveHandle *AH)
AH->StartBlobPtr = _StartBlob; AH->StartBlobPtr = _StartBlob;
AH->EndBlobPtr = _EndBlob; AH->EndBlobPtr = _EndBlob;
AH->EndBlobsPtr = _EndBlobs; AH->EndBlobsPtr = _EndBlobs;
AH->ClonePtr = NULL;
AH->DeClonePtr = NULL;
/* /*
* Set up some special context used in compressing data. * Set up some special context used in compressing data.
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_null.c,v 1.19 2006/07/14 14:52:26 momjian Exp $ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_null.c,v 1.20 2009/02/02 20:07:37 adunstan Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -54,12 +54,15 @@ InitArchiveFmt_Null(ArchiveHandle *AH) ...@@ -54,12 +54,15 @@ InitArchiveFmt_Null(ArchiveHandle *AH)
AH->WriteBytePtr = _WriteByte; AH->WriteBytePtr = _WriteByte;
AH->WriteBufPtr = _WriteBuf; AH->WriteBufPtr = _WriteBuf;
AH->ClosePtr = _CloseArchive; AH->ClosePtr = _CloseArchive;
AH->ReopenPtr = NULL;
AH->PrintTocDataPtr = _PrintTocData; AH->PrintTocDataPtr = _PrintTocData;
AH->StartBlobsPtr = _StartBlobs; AH->StartBlobsPtr = _StartBlobs;
AH->StartBlobPtr = _StartBlob; AH->StartBlobPtr = _StartBlob;
AH->EndBlobPtr = _EndBlob; AH->EndBlobPtr = _EndBlob;
AH->EndBlobsPtr = _EndBlobs; AH->EndBlobsPtr = _EndBlobs;
AH->ClonePtr = NULL;
AH->DeClonePtr = NULL;
/* Initialize LO buffering */ /* Initialize LO buffering */
AH->lo_buf_size = LOBBUFSIZE; AH->lo_buf_size = LOBBUFSIZE;
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_tar.c,v 1.62 2007/11/15 21:14:41 momjian Exp $ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_tar.c,v 1.63 2009/02/02 20:07:37 adunstan Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -143,6 +143,7 @@ InitArchiveFmt_Tar(ArchiveHandle *AH) ...@@ -143,6 +143,7 @@ InitArchiveFmt_Tar(ArchiveHandle *AH)
AH->WriteBufPtr = _WriteBuf; AH->WriteBufPtr = _WriteBuf;
AH->ReadBufPtr = _ReadBuf; AH->ReadBufPtr = _ReadBuf;
AH->ClosePtr = _CloseArchive; AH->ClosePtr = _CloseArchive;
AH->ReopenPtr = NULL;
AH->PrintTocDataPtr = _PrintTocData; AH->PrintTocDataPtr = _PrintTocData;
AH->ReadExtraTocPtr = _ReadExtraToc; AH->ReadExtraTocPtr = _ReadExtraToc;
AH->WriteExtraTocPtr = _WriteExtraToc; AH->WriteExtraTocPtr = _WriteExtraToc;
...@@ -152,6 +153,8 @@ InitArchiveFmt_Tar(ArchiveHandle *AH) ...@@ -152,6 +153,8 @@ InitArchiveFmt_Tar(ArchiveHandle *AH)
AH->StartBlobPtr = _StartBlob; AH->StartBlobPtr = _StartBlob;
AH->EndBlobPtr = _EndBlob; AH->EndBlobPtr = _EndBlob;
AH->EndBlobsPtr = _EndBlobs; AH->EndBlobsPtr = _EndBlobs;
AH->ClonePtr = NULL;
AH->DeClonePtr = NULL;
/* /*
* Set up some special context used in compressing data. * Set up some special context used in compressing data.
...@@ -1383,5 +1386,4 @@ _tarWriteHeader(TAR_MEMBER *th) ...@@ -1383,5 +1386,4 @@ _tarWriteHeader(TAR_MEMBER *th)
if (fwrite(h, 1, 512, th->tarFH) != 512) if (fwrite(h, 1, 512, th->tarFH) != 512)
die_horribly(th->AH, modulename, "could not write to output file: %s\n", strerror(errno)); die_horribly(th->AH, modulename, "could not write to output file: %s\n", strerror(errno));
} }
This diff is collapsed.
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.91 2009/01/06 17:18:11 momjian Exp $ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.92 2009/02/02 20:07:37 adunstan Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -93,6 +93,7 @@ main(int argc, char **argv) ...@@ -93,6 +93,7 @@ main(int argc, char **argv)
{"ignore-version", 0, NULL, 'i'}, {"ignore-version", 0, NULL, 'i'},
{"index", 1, NULL, 'I'}, {"index", 1, NULL, 'I'},
{"list", 0, NULL, 'l'}, {"list", 0, NULL, 'l'},
{"multi-thread", 1, NULL, 'm'},
{"no-privileges", 0, NULL, 'x'}, {"no-privileges", 0, NULL, 'x'},
{"no-acl", 0, NULL, 'x'}, {"no-acl", 0, NULL, 'x'},
{"no-owner", 0, NULL, 'O'}, {"no-owner", 0, NULL, 'O'},
...@@ -141,7 +142,7 @@ main(int argc, char **argv) ...@@ -141,7 +142,7 @@ main(int argc, char **argv)
} }
} }
while ((c = getopt_long(argc, argv, "acCd:ef:F:h:iI:lL:n:Op:P:RsS:t:T:U:vWxX:1", while ((c = getopt_long(argc, argv, "acCd:ef:F:h:iI:lL:m:n:Op:P:RsS:t:T:U:vWxX:1",
cmdopts, NULL)) != -1) cmdopts, NULL)) != -1)
{ {
switch (c) switch (c)
...@@ -184,6 +185,10 @@ main(int argc, char **argv) ...@@ -184,6 +185,10 @@ main(int argc, char **argv)
opts->tocFile = strdup(optarg); opts->tocFile = strdup(optarg);
break; break;
case 'm': /* number of restore threads */
opts->number_of_threads = atoi(optarg);
break;
case 'n': /* Dump data for this schema only */ case 'n': /* Dump data for this schema only */
opts->schemaNames = strdup(optarg); opts->schemaNames = strdup(optarg);
break; break;
...@@ -269,7 +274,10 @@ main(int argc, char **argv) ...@@ -269,7 +274,10 @@ main(int argc, char **argv)
break; break;
case 0: case 0:
/* This covers the long options equivalent to -X xxx. */ /*
* This covers the long options without a short equivalent,
* including those equivalent to -X xxx.
*/
break; break;
case 2: /* SET ROLE */ case 2: /* SET ROLE */
...@@ -301,6 +309,14 @@ main(int argc, char **argv) ...@@ -301,6 +309,14 @@ main(int argc, char **argv)
opts->useDB = 1; opts->useDB = 1;
} }
/* Can't do single-txn mode with multiple connections */
if (opts->single_txn && opts->number_of_threads > 1)
{
fprintf(stderr, _("%s: cannot specify both --single-transaction and multiple threads\n"),
progname);
exit(1);
}
opts->disable_triggers = disable_triggers; opts->disable_triggers = disable_triggers;
opts->noDataForFailedTables = no_data_for_failed_tables; opts->noDataForFailedTables = no_data_for_failed_tables;
opts->noTablespace = outputNoTablespaces; opts->noTablespace = outputNoTablespaces;
...@@ -308,10 +324,8 @@ main(int argc, char **argv) ...@@ -308,10 +324,8 @@ main(int argc, char **argv)
if (opts->formatName) if (opts->formatName)
{ {
switch (opts->formatName[0]) switch (opts->formatName[0])
{ {
case 'c': case 'c':
case 'C': case 'C':
opts->format = archCustom; opts->format = archCustom;
...@@ -396,6 +410,7 @@ usage(const char *progname) ...@@ -396,6 +410,7 @@ usage(const char *progname)
printf(_(" -I, --index=NAME restore named index\n")); printf(_(" -I, --index=NAME restore named index\n"));
printf(_(" -L, --use-list=FILENAME use specified table of contents for ordering\n" printf(_(" -L, --use-list=FILENAME use specified table of contents for ordering\n"
" output from this file\n")); " output from this file\n"));
printf(_(" -m, --multi-thread=NUM use this many parallel connections to restore\n"));
printf(_(" -n, --schema=NAME restore only objects in this schema\n")); printf(_(" -n, --schema=NAME restore only objects in this schema\n"));
printf(_(" -O, --no-owner skip restoration of object ownership\n")); printf(_(" -O, --no-owner skip restoration of object ownership\n"));
printf(_(" -P, --function=NAME(args)\n" printf(_(" -P, --function=NAME(args)\n"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment