Skip to content

Commit 54152d0

Browse files
committed
Fix bugs in "restore.sql" script emitted in pg_dump tar output.
The tar output module did some very ugly and ultimately incorrect hacking on COPY commands to try to get them to work in the context of restoring a deconstructed tar archive. In particular, it would fail altogether for table names containing any upper-case characters, since it smashed the command string to lower-case before modifying it (and, just to add insult to injury, did that in a way that would fail in multibyte encodings). I don't see any particular value in being flexible about the case of the command keywords, since the string will just have been created by dumpTableData, so let's get rid of the whole case-folding thing. Also, it doesn't seem to meet the POLA for the script to restore data only in COPY mode, so add \i commands to make it have comparable behavior in --inserts mode. Noted while looking at the tar-output code in connection with Brian Weaver's patch.
1 parent 158cbed commit 54152d0

File tree

1 file changed

+35
-47
lines changed

1 file changed

+35
-47
lines changed

src/bin/pg_dump/pg_backup_tar.c

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -644,56 +644,46 @@ _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
644644
{
645645
lclContext *ctx = (lclContext *) AH->formatData;
646646
lclTocEntry *tctx = (lclTocEntry *) te->formatData;
647-
char *tmpCopy;
648-
size_t i,
649-
pos1,
650-
pos2;
647+
int pos1;
651648

652649
if (!tctx->filename)
653650
return;
654651

652+
/*
653+
* If we're writing the special restore.sql script, emit a suitable
654+
* command to include each table's data from the corresponding file.
655+
*
656+
* In the COPY case this is a bit klugy because the regular COPY command
657+
* was already printed before we get control.
658+
*/
655659
if (ctx->isSpecialScript)
656660
{
657-
if (!te->copyStmt)
658-
return;
659-
660-
/* Abort the default COPY */
661-
ahprintf(AH, "\\.\n");
662-
663-
/* Get a copy of the COPY statement and clean it up */
664-
tmpCopy = strdup(te->copyStmt);
665-
for (i = 0; i < strlen(tmpCopy); i++)
666-
tmpCopy[i] = pg_tolower((unsigned char) tmpCopy[i]);
667-
668-
/*
669-
* This is very nasty; we don't know if the archive used WITH OIDS, so
670-
* we search the string for it in a paranoid sort of way.
671-
*/
672-
if (strncmp(tmpCopy, "copy ", 5) != 0)
673-
die_horribly(AH, modulename,
674-
"invalid COPY statement -- could not find \"copy\" in string \"%s\"\n", tmpCopy);
675-
676-
pos1 = 5;
677-
for (pos1 = 5; pos1 < strlen(tmpCopy); pos1++)
678-
if (tmpCopy[pos1] != ' ')
679-
break;
680-
681-
if (tmpCopy[pos1] == '"')
682-
pos1 += 2;
683-
684-
pos1 += strlen(te->tag);
685-
686-
for (pos2 = pos1; pos2 < strlen(tmpCopy); pos2++)
687-
if (strncmp(&tmpCopy[pos2], "from stdin", 10) == 0)
688-
break;
661+
if (te->copyStmt)
662+
{
663+
/* Abort the COPY FROM stdin */
664+
ahprintf(AH, "\\.\n");
689665

690-
if (pos2 >= strlen(tmpCopy))
691-
die_horribly(AH, modulename,
692-
"invalid COPY statement -- could not find \"from stdin\" in string \"%s\" starting at position %lu\n",
693-
tmpCopy, (unsigned long) pos1);
666+
/*
667+
* The COPY statement should look like "COPY ... FROM stdin;\n",
668+
* see dumpTableData().
669+
*/
670+
pos1 = (int) strlen(te->copyStmt) - 13;
671+
if (pos1 < 6 || strncmp(te->copyStmt, "COPY ", 5) != 0 ||
672+
strcmp(te->copyStmt + pos1, " FROM stdin;\n") != 0)
673+
die_horribly(AH, modulename,
674+
"unexpected COPY statement syntax: \"%s\"\n",
675+
te->copyStmt);
694676

695-
ahwrite(tmpCopy, 1, pos2, AH); /* 'copy "table" [with oids]' */
696-
ahprintf(AH, " from '$$PATH$$/%s' %s", tctx->filename, &tmpCopy[pos2 + 10]);
677+
/* Emit all but the FROM part ... */
678+
ahwrite(te->copyStmt, 1, pos1, AH);
679+
/* ... and insert modified FROM */
680+
ahprintf(AH, " FROM '$$PATH$$/%s';\n\n", tctx->filename);
681+
}
682+
else
683+
{
684+
/* --inserts mode, no worries, just include the data file */
685+
ahprintf(AH, "\\i $$PATH$$/%s\n\n", tctx->filename);
686+
}
697687

698688
return;
699689
}
@@ -839,18 +829,14 @@ _CloseArchive(ArchiveHandle *AH)
839829
* if the files have been extracted.
840830
*/
841831
th = tarOpen(AH, "restore.sql", 'w');
842-
tarPrintf(AH, th, "create temporary table pgdump_restore_path(p text);\n");
832+
843833
tarPrintf(AH, th, "--\n"
844834
"-- NOTE:\n"
845835
"--\n"
846836
"-- File paths need to be edited. Search for $$PATH$$ and\n"
847837
"-- replace it with the path to the directory containing\n"
848838
"-- the extracted data files.\n"
849-
"--\n"
850-
"-- Edit the following to match the path where the\n"
851-
"-- tar archive has been extracted.\n"
852839
"--\n");
853-
tarPrintf(AH, th, "insert into pgdump_restore_path values('/tmp');\n\n");
854840

855841
AH->CustomOutPtr = _scriptOut;
856842

@@ -872,6 +858,8 @@ _CloseArchive(ArchiveHandle *AH)
872858

873859
tarClose(AH, th);
874860

861+
ctx->isSpecialScript = 0;
862+
875863
/*
876864
* EOF marker for tar files is two blocks of NULLs.
877865
*/

0 commit comments

Comments
 (0)