Skip to content

Commit d617b28

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 dfa6eda commit d617b28

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
@@ -648,56 +648,46 @@ _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
648648
{
649649
lclContext *ctx = (lclContext *) AH->formatData;
650650
lclTocEntry *tctx = (lclTocEntry *) te->formatData;
651-
char *tmpCopy;
652-
size_t i,
653-
pos1,
654-
pos2;
651+
int pos1;
655652

656653
if (!tctx->filename)
657654
return;
658655

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

694-
if (pos2 >= strlen(tmpCopy))
695-
die_horribly(AH, modulename,
696-
"invalid COPY statement -- could not find \"from stdin\" in string \"%s\" starting at position %lu\n",
697-
tmpCopy, (unsigned long) pos1);
670+
/*
671+
* The COPY statement should look like "COPY ... FROM stdin;\n",
672+
* see dumpTableData().
673+
*/
674+
pos1 = (int) strlen(te->copyStmt) - 13;
675+
if (pos1 < 6 || strncmp(te->copyStmt, "COPY ", 5) != 0 ||
676+
strcmp(te->copyStmt + pos1, " FROM stdin;\n") != 0)
677+
die_horribly(AH, modulename,
678+
"unexpected COPY statement syntax: \"%s\"\n",
679+
te->copyStmt);
698680

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

702692
return;
703693
}
@@ -843,18 +833,14 @@ _CloseArchive(ArchiveHandle *AH)
843833
* if the files have been extracted.
844834
*/
845835
th = tarOpen(AH, "restore.sql", 'w');
846-
tarPrintf(AH, th, "create temporary table pgdump_restore_path(p text);\n");
836+
847837
tarPrintf(AH, th, "--\n"
848838
"-- NOTE:\n"
849839
"--\n"
850840
"-- File paths need to be edited. Search for $$PATH$$ and\n"
851841
"-- replace it with the path to the directory containing\n"
852842
"-- the extracted data files.\n"
853-
"--\n"
854-
"-- Edit the following to match the path where the\n"
855-
"-- tar archive has been extracted.\n"
856843
"--\n");
857-
tarPrintf(AH, th, "insert into pgdump_restore_path values('/tmp');\n\n");
858844

859845
AH->CustomOutPtr = _scriptOut;
860846

@@ -876,6 +862,8 @@ _CloseArchive(ArchiveHandle *AH)
876862

877863
tarClose(AH, th);
878864

865+
ctx->isSpecialScript = 0;
866+
879867
/*
880868
* EOF marker for tar files is two blocks of NULLs.
881869
*/

0 commit comments

Comments
 (0)