Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0eaa511

Browse files
committed
Improve pg_dump/pg_restore --create --if-exists logic.
Teach it not to complain if the dropStmt attached to an archive entry is actually spelled CREATE OR REPLACE VIEW, since that will happen due to an upcoming bug fix. Also, if it doesn't recognize a dropStmt, have it print a WARNING and then emit the dropStmt unmodified. That seems like a much saner behavior than Assert'ing or dumping core due to a null-pointer dereference, which is what would happen before :-(. Back-patch to 9.4 where this option was introduced. Discussion: <19092.1479325184@sss.pgh.pa.us>
1 parent f5d8944 commit 0eaa511

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

src/bin/pg_dump/pg_backup_archiver.c

+32-16
Original file line numberDiff line numberDiff line change
@@ -515,18 +515,15 @@ RestoreArchive(Archive *AHX)
515515
* knows how to do it, without depending on
516516
* te->dropStmt; use that. For other objects we need
517517
* to parse the command.
518-
*
519518
*/
520519
if (strncmp(te->desc, "BLOB", 4) == 0)
521520
{
522521
DropBlobIfExists(AH, te->catalogId.oid);
523522
}
524523
else
525524
{
526-
char buffer[40];
527-
char *mark;
528525
char *dropStmt = pg_strdup(te->dropStmt);
529-
char *dropStmtPtr = dropStmt;
526+
char *dropStmtOrig = dropStmt;
530527
PQExpBuffer ftStmt = createPQExpBuffer();
531528

532529
/*
@@ -543,18 +540,28 @@ RestoreArchive(Archive *AHX)
543540
/*
544541
* ALTER TABLE..ALTER COLUMN..DROP DEFAULT does
545542
* not support the IF EXISTS clause, and therefore
546-
* we simply emit the original command for such
547-
* objects. For other objects, we need to extract
548-
* the first part of the DROP which includes the
549-
* object type. Most of the time this matches
543+
* we simply emit the original command for DEFAULT
544+
* objects (modulo the adjustment made above).
545+
*
546+
* If we used CREATE OR REPLACE VIEW as a means of
547+
* quasi-dropping an ON SELECT rule, that should
548+
* be emitted unchanged as well.
549+
*
550+
* For other object types, we need to extract the
551+
* first part of the DROP which includes the
552+
* object type. Most of the time this matches
550553
* te->desc, so search for that; however for the
551554
* different kinds of CONSTRAINTs, we know to
552555
* search for hardcoded "DROP CONSTRAINT" instead.
553556
*/
554-
if (strcmp(te->desc, "DEFAULT") == 0)
557+
if (strcmp(te->desc, "DEFAULT") == 0 ||
558+
strncmp(dropStmt, "CREATE OR REPLACE VIEW", 22) == 0)
555559
appendPQExpBufferStr(ftStmt, dropStmt);
556560
else
557561
{
562+
char buffer[40];
563+
char *mark;
564+
558565
if (strcmp(te->desc, "CONSTRAINT") == 0 ||
559566
strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
560567
strcmp(te->desc, "FK CONSTRAINT") == 0)
@@ -564,19 +571,28 @@ RestoreArchive(Archive *AHX)
564571
te->desc);
565572

566573
mark = strstr(dropStmt, buffer);
567-
Assert(mark != NULL);
568574

569-
*mark = '\0';
570-
appendPQExpBuffer(ftStmt, "%s%s IF EXISTS%s",
571-
dropStmt, buffer,
572-
mark + strlen(buffer));
575+
if (mark)
576+
{
577+
*mark = '\0';
578+
appendPQExpBuffer(ftStmt, "%s%s IF EXISTS%s",
579+
dropStmt, buffer,
580+
mark + strlen(buffer));
581+
}
582+
else
583+
{
584+
/* complain and emit unmodified command */
585+
write_msg(modulename,
586+
"WARNING: could not find where to insert IF EXISTS in statement \"%s\"\n",
587+
dropStmtOrig);
588+
appendPQExpBufferStr(ftStmt, dropStmt);
589+
}
573590
}
574591

575592
ahprintf(AH, "%s", ftStmt->data);
576593

577594
destroyPQExpBuffer(ftStmt);
578-
579-
pg_free(dropStmtPtr);
595+
pg_free(dropStmtOrig);
580596
}
581597
}
582598
}

0 commit comments

Comments
 (0)