Fix pg_restore to handle the 'set max oid' entry correctly in archives

dumped by pg_dump -o.  Per bug report posted by Bruce; fix is from
Philip Warner, reviewed by Tom Lane.
This commit is contained in:
Tom Lane 2002-01-18 17:13:51 +00:00
parent 3dfe80243c
commit bb698c25c7
2 changed files with 233 additions and 188 deletions

View File

@ -15,7 +15,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.38 2001/11/08 04:05:12 tgl Exp $
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.39 2002/01/18 17:13:50 tgl Exp $
*
* Modifications - 28-Jun-2000 - pjw@rhyme.com.au
*
@ -62,6 +62,11 @@
* backup file; prior version was restoring schema in data-only
* restores. Added enum to make code easier to understand.
*
* Modifications - 18-Jan-2002 - pjw@rhyme.com.au
* - Modified _tocEntryRequired to handle '<Init>/Max OID' as a special
* case (ie. as a DATA item) as per bugs reported by Bruce Momjian
* around 17-Jan-2002.
*
*-------------------------------------------------------------------------
*/
@ -1917,6 +1922,13 @@ _tocEntryRequired(TocEntry *te, RestoreOptions *ropt)
res = res & ~REQ_DATA;
}
/* Special case: <Init> type with <Max OID> name; this is part of
* a DATA restore even though it has SQL.
*/
if ( ( strcmp(te->desc, "<Init>") == 0 ) && ( strcmp(te->name, "Max OID") == 0) ) {
res = REQ_DATA;
}
/* Mask it if we only want schema */
if (ropt->schemaOnly)
res = res & REQ_SCHEMA;

View File

@ -5,7 +5,7 @@
* Implements the basic DB functions used by the archiver.
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.29 2001/10/25 05:49:52 momjian Exp $
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.30 2002/01/18 17:13:51 tgl Exp $
*
* NOTES
*
@ -17,6 +17,14 @@
*
* - Avoid forcing table name to lower case in FixupBlobXrefs!
*
*
* Modifications - 18-Jan-2002 - pjw@rhyme.com.au
*
* - Split ExecuteSqlCommandBuf into 3 routines for (slightly) improved
* clarity. Modify loop to cater for COPY commands buried in the SQL
* command buffer (prev version assumed COPY command was executed
* in prior call). This was to fix the buf in the 'set max oid' code.
*
*-------------------------------------------------------------------------
*/
@ -43,6 +51,8 @@ static void _check_database_version(ArchiveHandle *AH, bool ignoreVersion);
static PGconn *_connectDB(ArchiveHandle *AH, const char *newdbname, const char *newUser);
static int _executeSqlCommand(ArchiveHandle *AH, PGconn *conn, PQExpBuffer qry, char *desc);
static void notice_processor(void *arg, const char *message);
static char* _sendSQLLine( ArchiveHandle *AH, char *qry, char *eos);
static char* _sendCopyLine( ArchiveHandle *AH, char *qry, char *eos);
/*
@ -534,38 +544,27 @@ _executeSqlCommand(ArchiveHandle *AH, PGconn *conn, PQExpBuffer qry, char *desc)
return strlen(qry->data);
}
/* Convenience function to send one or more queries. Monitors result to handle COPY statements */
int
ExecuteSqlCommandBuf(ArchiveHandle *AH, void *qryv, int bufLen)
{
int loc;
int pos = 0;
int sPos = 0;
char *qry = (char *) qryv;
int isEnd = 0;
char *eos = qry + bufLen;
/*
* fprintf(stderr, "\n\n*****\n
* Buffer:\n\n%s\n*******************\n\n", qry);
/*
* Used by ExecuteSqlCommandBuf to send one buffered line when running a COPY command.
*/
static char*
_sendCopyLine( ArchiveHandle *AH, char *qry, char *eos)
{
int loc; /* Location of next newline */
int pos = 0; /* Current position */
int sPos = 0; /* Last pos of a slash char */
int isEnd = 0;
/* If we're in COPY IN mode, then just break it into lines and send... */
if (AH->pgCopyIn)
{
for (;;)
{
/* Find a lf */
/* loop to find unquoted newline ending the line of COPY data */
for (;;) {
loc = strcspn(&qry[pos], "\n") + pos;
pos = 0;
/* If no match, then wait */
if (loc >= (eos - qry)) /* None found */
{
appendBinaryPQExpBuffer(AH->pgCopyBuf, qry, (eos - qry));
break;
};
return eos;
}
/*
* fprintf(stderr, "Found cr at %d, prev char was %c, next was
@ -581,7 +580,7 @@ ExecuteSqlCommandBuf(ArchiveHandle *AH, void *qryv, int bufLen)
/*
* If an odd number of preceding slashes, then \n was escaped
* so set the next search pos, and restart (if any left).
* so set the next search pos, and loop (if any left).
*/
if ((sPos & 1) == 1)
{
@ -590,15 +589,16 @@ ExecuteSqlCommandBuf(ArchiveHandle *AH, void *qryv, int bufLen)
if (pos >= (eos - qry))
{
appendBinaryPQExpBuffer(AH->pgCopyBuf, qry, (eos - qry));
return eos;
}
} else {
break;
}
}
else
{
/* We got a good cr */
/* We found an unquoted newline */
qry[loc] = '\0';
appendPQExpBuffer(AH->pgCopyBuf, "%s\n", qry);
qry += loc + 1;
isEnd = (strcmp(AH->pgCopyBuf->data, "\\.\n") == 0);
/*---------
@ -623,23 +623,21 @@ ExecuteSqlCommandBuf(ArchiveHandle *AH, void *qryv, int bufLen)
die_horribly(AH, modulename, "error returned by PQendcopy\n");
AH->pgCopyIn = 0;
break;
}
}
return qry + loc + 1;
}
/* Make sure we're not past the original buffer end */
if (qry >= eos)
break;
/*
* Used by ExecuteSqlCommandBuf to send one buffered line of SQL (not data for the copy command).
*/
static char*
_sendSQLLine( ArchiveHandle *AH, char *qry, char *eos)
{
int pos = 0; /* Current position */
}
}
/* We may have finished Copy In, and have a non-empty buffer */
if (!AH->pgCopyIn)
{
/*
* The following is a mini state machine to assess then of of an
* The following is a mini state machine to assess the end of an
* SQL statement. It really only needs to parse good SQL, or at
* least that's the theory... End-of-statement is assumed to be an
* unquoted, un commented semi-colon.
@ -671,6 +669,15 @@ ExecuteSqlCommandBuf(ArchiveHandle *AH, void *qryv, int bufLen)
ExecuteSqlCommand(AH, AH->sqlBuf, "could not execute query", false);
resetPQExpBuffer(AH->sqlBuf);
AH->sqlparse.lastChar = '\0';
/* Remove any following newlines - so that embedded COPY commands don't get a
* starting newline.
*/
pos++;
for ( ; pos < (eos - qry) && qry[pos] == '\n' ; pos++ ) ;
/* We've got our line, so exit */
return qry + pos;
}
else
{
@ -734,6 +741,32 @@ ExecuteSqlCommandBuf(ArchiveHandle *AH, void *qryv, int bufLen)
/* fprintf(stderr, "\n"); */
}
/* If we get here, we've processed entire string with no complete SQL stmt */
return eos;
}
/* Convenience function to send one or more queries. Monitors result to handle COPY statements */
int
ExecuteSqlCommandBuf(ArchiveHandle *AH, void *qryv, int bufLen)
{
char *qry = (char *) qryv;
char *eos = qry + bufLen;
/*
* fprintf(stderr, "\n\n*****\n
* Buffer:\n\n%s\n*******************\n\n", qry);
*/
/* Could switch between command and COPY IN mode at each line */
while (qry < eos)
{
if (AH->pgCopyIn) {
qry = _sendCopyLine(AH, qry, eos);
} else {
qry = _sendSQLLine(AH, qry, eos);
}
}
return 1;