Commit 6545a901 authored by Tom Lane's avatar Tom Lane

Fix pg_restore's direct-to-database mode for standard_conforming_strings.

pg_backup_db.c contained a mini SQL lexer with which it tried to identify
boundaries between SQL commands, but that code was not designed to cope
with standard_conforming_strings, and would get the wrong answer if a
backslash immediately precedes a closing single quote in such a string,
as per report from Julian Mehnle.  The bug only affects direct-to-database
restores from archive files made with standard_conforming_strings = on.

Rather than complicating the code some more to try to fix that, let's just
rip it all out.  The only reason it was needed was to cope with COPY data
embedded into ordinary archive entries, which was a layout that was used
only for about the first three weeks of the archive format's existence,
and never in any production release of pg_dump.  Instead, just rely on the
archive file layout to tell us whether we're printing COPY data or not.

This bug represents a data corruption hazard in all releases in which
standard_conforming_strings can be turned on, ie 8.2 and later, so
back-patch to all supported branches.
parent 0fe81508
...@@ -117,6 +117,7 @@ static TocEntry *getTocEntryByDumpId(ArchiveHandle *AH, DumpId id); ...@@ -117,6 +117,7 @@ static TocEntry *getTocEntryByDumpId(ArchiveHandle *AH, DumpId id);
static void _moveBefore(ArchiveHandle *AH, TocEntry *pos, TocEntry *te); static void _moveBefore(ArchiveHandle *AH, TocEntry *pos, TocEntry *te);
static int _discoverArchiveFormat(ArchiveHandle *AH); static int _discoverArchiveFormat(ArchiveHandle *AH);
static int RestoringToDB(ArchiveHandle *AH);
static void dump_lo_buf(ArchiveHandle *AH); static void dump_lo_buf(ArchiveHandle *AH);
static void _write_msg(const char *modulename, const char *fmt, va_list ap); static void _write_msg(const char *modulename, const char *fmt, va_list ap);
static void _die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt, va_list ap); static void _die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt, va_list ap);
...@@ -589,13 +590,7 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te, ...@@ -589,13 +590,7 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
} }
/* /*
* If we have a copy statement, use it. As of V1.3, these * If we have a copy statement, use it.
* are separate to allow easy import from withing a
* database connection. Pre 1.3 archives can not use DB
* connections and are sent to output only.
*
* For V1.3+, the table data MUST have a copy statement so
* that we can go into appropriate mode with libpq.
*/ */
if (te->copyStmt && strlen(te->copyStmt) > 0) if (te->copyStmt && strlen(te->copyStmt) > 0)
{ {
...@@ -605,7 +600,15 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te, ...@@ -605,7 +600,15 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
(*AH->PrintTocDataPtr) (AH, te, ropt); (*AH->PrintTocDataPtr) (AH, te, ropt);
/*
* Terminate COPY if needed.
*/
if (AH->writingCopyData)
{
if (RestoringToDB(AH))
EndDBCopyMode(AH, te);
AH->writingCopyData = false; AH->writingCopyData = false;
}
/* close out the transaction started above */ /* close out the transaction started above */
if (is_parallel && te->created) if (is_parallel && te->created)
...@@ -1248,17 +1251,13 @@ ahprintf(ArchiveHandle *AH, const char *fmt,...) ...@@ -1248,17 +1251,13 @@ ahprintf(ArchiveHandle *AH, const char *fmt,...)
{ {
char *p = NULL; char *p = NULL;
va_list ap; va_list ap;
int bSize = strlen(fmt) + 256; /* Should be enough */ int bSize = strlen(fmt) + 256; /* Usually enough */
int cnt = -1; int cnt = -1;
/* /*
* This is paranoid: deal with the possibility that vsnprintf is willing * This is paranoid: deal with the possibility that vsnprintf is willing
* to ignore trailing null * to ignore trailing null or returns > 0 even if string does not fit.
*/ * It may be the case that it returns cnt = bufsize.
/*
* or returns > 0 even if string does not fit. It may be the case that it
* returns cnt = bufsize
*/ */
while (cnt < 0 || cnt >= (bSize - 1)) while (cnt < 0 || cnt >= (bSize - 1))
{ {
...@@ -1340,7 +1339,7 @@ dump_lo_buf(ArchiveHandle *AH) ...@@ -1340,7 +1339,7 @@ dump_lo_buf(ArchiveHandle *AH)
/* /*
* Write buffer to the output file (usually stdout). This is user for * Write buffer to the output file (usually stdout). This is used for
* outputting 'restore' scripts etc. It is even possible for an archive * outputting 'restore' scripts etc. It is even possible for an archive
* format to create a custom output routine to 'fake' a restore if it * format to create a custom output routine to 'fake' a restore if it
* wants to generate a script (see TAR output). * wants to generate a script (see TAR output).
...@@ -1392,7 +1391,7 @@ ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH) ...@@ -1392,7 +1391,7 @@ ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
* connected then send it to the DB. * connected then send it to the DB.
*/ */
if (RestoringToDB(AH)) if (RestoringToDB(AH))
return ExecuteSqlCommandBuf(AH, (void *) ptr, size * nmemb); /* Always 1, currently */ return ExecuteSqlCommandBuf(AH, (const char *) ptr, size * nmemb);
else else
{ {
res = fwrite((void *) ptr, size, nmemb, AH->OF); res = fwrite((void *) ptr, size, nmemb, AH->OF);
...@@ -1985,9 +1984,6 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt, ...@@ -1985,9 +1984,6 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt,
AH->mode = mode; AH->mode = mode;
AH->compression = compression; AH->compression = compression;
AH->pgCopyBuf = createPQExpBuffer();
AH->sqlBuf = createPQExpBuffer();
/* Open stdout with no compression for AH output handle */ /* Open stdout with no compression for AH output handle */
AH->gzOut = 0; AH->gzOut = 0;
AH->OF = stdout; AH->OF = stdout;
...@@ -4199,10 +4195,7 @@ CloneArchive(ArchiveHandle *AH) ...@@ -4199,10 +4195,7 @@ CloneArchive(ArchiveHandle *AH)
die_horribly(AH, modulename, "out of memory\n"); die_horribly(AH, modulename, "out of memory\n");
memcpy(clone, AH, sizeof(ArchiveHandle)); memcpy(clone, AH, sizeof(ArchiveHandle));
/* Handle format-independent fields */ /* Handle format-independent fields ... none at the moment */
clone->pgCopyBuf = createPQExpBuffer();
clone->sqlBuf = createPQExpBuffer();
clone->sqlparse.tagBuf = NULL;
/* The clone will have its own connection, so disregard connection state */ /* The clone will have its own connection, so disregard connection state */
clone->connection = NULL; clone->connection = NULL;
...@@ -4235,11 +4228,7 @@ DeCloneArchive(ArchiveHandle *AH) ...@@ -4235,11 +4228,7 @@ DeCloneArchive(ArchiveHandle *AH)
/* Clear format-specific state */ /* Clear format-specific state */
(AH->DeClonePtr) (AH); (AH->DeClonePtr) (AH);
/* Clear state allocated by CloneArchive */ /* Clear state allocated by CloneArchive ... none at the moment */
destroyPQExpBuffer(AH->pgCopyBuf);
destroyPQExpBuffer(AH->sqlBuf);
if (AH->sqlparse.tagBuf)
destroyPQExpBuffer(AH->sqlparse.tagBuf);
/* Clear any connection-local state */ /* Clear any connection-local state */
if (AH->currUser) if (AH->currUser)
......
...@@ -132,28 +132,6 @@ typedef void (*DeClonePtr) (struct _archiveHandle * AH); ...@@ -132,28 +132,6 @@ 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 enum
{
SQL_SCAN = 0, /* normal */
SQL_IN_SQL_COMMENT, /* -- comment */
SQL_IN_EXT_COMMENT, /* slash-star comment */
SQL_IN_SINGLE_QUOTE, /* '...' literal */
SQL_IN_E_QUOTE, /* E'...' literal */
SQL_IN_DOUBLE_QUOTE, /* "..." identifier */
SQL_IN_DOLLAR_TAG, /* possible dollar-quote starting tag */
SQL_IN_DOLLAR_QUOTE /* body of dollar quote */
} sqlparseState;
typedef struct
{
sqlparseState state; /* see above */
char lastChar; /* preceding char, or '\0' initially */
bool backSlash; /* next char is backslash quoted? */
int braceDepth; /* parenthesis nesting depth */
PQExpBuffer tagBuf; /* dollar quote tag (NULL if not created) */
int minTagEndPos; /* first possible end position of $-quote */
} sqlparseInfo;
typedef enum typedef enum
{ {
STAGE_NONE = 0, STAGE_NONE = 0,
...@@ -189,9 +167,6 @@ typedef struct _archiveHandle ...@@ -189,9 +167,6 @@ typedef struct _archiveHandle
* Added V1.7 */ * Added V1.7 */
ArchiveFormat format; /* Archive format */ ArchiveFormat format; /* Archive format */
sqlparseInfo sqlparse;
PQExpBuffer sqlBuf;
time_t createDate; /* Date archive created */ time_t createDate; /* Date archive created */
/* /*
...@@ -244,8 +219,6 @@ typedef struct _archiveHandle ...@@ -244,8 +219,6 @@ typedef struct _archiveHandle
* required */ * required */
bool writingCopyData; /* True when we are sending COPY data */ bool writingCopyData; /* True when we are sending COPY data */
bool pgCopyIn; /* Currently in libpq 'COPY IN' mode. */ bool pgCopyIn; /* Currently in libpq 'COPY IN' mode. */
PQExpBuffer pgCopyBuf; /* Left-over data from incomplete lines in
* COPY IN */
int loFd; /* BLOB fd */ int loFd; /* BLOB fd */
int writingBlob; /* Flag */ int writingBlob; /* Flag */
......
This diff is collapsed.
...@@ -10,7 +10,9 @@ ...@@ -10,7 +10,9 @@
#include "pg_backup_archiver.h" #include "pg_backup_archiver.h"
extern int ExecuteSqlCommandBuf(ArchiveHandle *AH, void *qry, size_t bufLen); extern int ExecuteSqlCommandBuf(ArchiveHandle *AH, const char *buf, size_t bufLen);
extern void EndDBCopyMode(ArchiveHandle *AH, struct _tocEntry * te);
extern void StartTransaction(ArchiveHandle *AH); extern void StartTransaction(ArchiveHandle *AH);
extern void CommitTransaction(ArchiveHandle *AH); extern void CommitTransaction(ArchiveHandle *AH);
......
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