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

Commit ac88898

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 fcf70e0 commit ac88898

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
@@ -521,18 +521,15 @@ RestoreArchive(Archive *AHX)
521521
* knows how to do it, without depending on
522522
* te->dropStmt; use that. For other objects we need
523523
* to parse the command.
524-
*
525524
*/
526525
if (strncmp(te->desc, "BLOB", 4) == 0)
527526
{
528527
DropBlobIfExists(AH, te->catalogId.oid);
529528
}
530529
else
531530
{
532-
char buffer[40];
533-
char *mark;
534531
char *dropStmt = pg_strdup(te->dropStmt);
535-
char *dropStmtPtr = dropStmt;
532+
char *dropStmtOrig = dropStmt;
536533
PQExpBuffer ftStmt = createPQExpBuffer();
537534

538535
/*
@@ -549,18 +546,28 @@ RestoreArchive(Archive *AHX)
549546
/*
550547
* ALTER TABLE..ALTER COLUMN..DROP DEFAULT does
551548
* not support the IF EXISTS clause, and therefore
552-
* we simply emit the original command for such
553-
* objects. For other objects, we need to extract
554-
* the first part of the DROP which includes the
555-
* object type. Most of the time this matches
549+
* we simply emit the original command for DEFAULT
550+
* objects (modulo the adjustment made above).
551+
*
552+
* If we used CREATE OR REPLACE VIEW as a means of
553+
* quasi-dropping an ON SELECT rule, that should
554+
* be emitted unchanged as well.
555+
*
556+
* For other object types, we need to extract the
557+
* first part of the DROP which includes the
558+
* object type. Most of the time this matches
556559
* te->desc, so search for that; however for the
557560
* different kinds of CONSTRAINTs, we know to
558561
* search for hardcoded "DROP CONSTRAINT" instead.
559562
*/
560-
if (strcmp(te->desc, "DEFAULT") == 0)
563+
if (strcmp(te->desc, "DEFAULT") == 0 ||
564+
strncmp(dropStmt, "CREATE OR REPLACE VIEW", 22) == 0)
561565
appendPQExpBufferStr(ftStmt, dropStmt);
562566
else
563567
{
568+
char buffer[40];
569+
char *mark;
570+
564571
if (strcmp(te->desc, "CONSTRAINT") == 0 ||
565572
strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
566573
strcmp(te->desc, "FK CONSTRAINT") == 0)
@@ -570,19 +577,28 @@ RestoreArchive(Archive *AHX)
570577
te->desc);
571578

572579
mark = strstr(dropStmt, buffer);
573-
Assert(mark != NULL);
574580

575-
*mark = '\0';
576-
appendPQExpBuffer(ftStmt, "%s%s IF EXISTS%s",
577-
dropStmt, buffer,
578-
mark + strlen(buffer));
581+
if (mark)
582+
{
583+
*mark = '\0';
584+
appendPQExpBuffer(ftStmt, "%s%s IF EXISTS%s",
585+
dropStmt, buffer,
586+
mark + strlen(buffer));
587+
}
588+
else
589+
{
590+
/* complain and emit unmodified command */
591+
write_msg(modulename,
592+
"WARNING: could not find where to insert IF EXISTS in statement \"%s\"\n",
593+
dropStmtOrig);
594+
appendPQExpBufferStr(ftStmt, dropStmt);
595+
}
579596
}
580597

581598
ahprintf(AH, "%s", ftStmt->data);
582599

583600
destroyPQExpBuffer(ftStmt);
584-
585-
pg_free(dropStmtPtr);
601+
pg_free(dropStmtOrig);
586602
}
587603
}
588604
}

0 commit comments

Comments
 (0)