Skip to content

Commit 5f4f66f

Browse files
committed
Fix pg_dump's --if-exists for large objects
This was born broken in 9067310. Per trouble report from Joachim Wieland. Pavel Stěhule and Álvaro Herrera
1 parent 6f13100 commit 5f4f66f

File tree

1 file changed

+57
-42
lines changed

1 file changed

+57
-42
lines changed

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -428,60 +428,75 @@ RestoreArchive(Archive *AHX)
428428
}
429429
else
430430
{
431-
char buffer[40];
432-
char *mark;
433-
char *dropStmt = pg_strdup(te->dropStmt);
434-
char *dropStmtPtr = dropStmt;
435-
PQExpBuffer ftStmt = createPQExpBuffer();
436-
437431
/*
438-
* Need to inject IF EXISTS clause after ALTER TABLE
439-
* part in ALTER TABLE .. DROP statement
432+
* Inject an appropriate spelling of "if exists". For
433+
* large objects, we have a separate routine that
434+
* knows how to do it, without depending on
435+
* te->dropStmt; use that. For other objects we need
436+
* to parse the command.
437+
*
440438
*/
441-
if (strncmp(dropStmt, "ALTER TABLE", 11) == 0)
439+
if (strncmp(te->desc, "BLOB", 4) == 0)
442440
{
443-
appendPQExpBuffer(ftStmt,
444-
"ALTER TABLE IF EXISTS");
445-
dropStmt = dropStmt + 11;
441+
DropBlobIfExists(AH, te->catalogId.oid);
446442
}
447-
448-
/*
449-
* ALTER TABLE..ALTER COLUMN..DROP DEFAULT does not
450-
* support the IF EXISTS clause, and therefore we
451-
* simply emit the original command for such objects.
452-
* For other objects, we need to extract the first
453-
* part of the DROP which includes the object type.
454-
* Most of the time this matches te->desc, so search
455-
* for that; however for the different kinds of
456-
* CONSTRAINTs, we know to search for hardcoded "DROP
457-
* CONSTRAINT" instead.
458-
*/
459-
if (strcmp(te->desc, "DEFAULT") == 0)
460-
appendPQExpBuffer(ftStmt, "%s", dropStmt);
461443
else
462444
{
463-
if (strcmp(te->desc, "CONSTRAINT") == 0 ||
464-
strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
465-
strcmp(te->desc, "FK CONSTRAINT") == 0)
466-
strcpy(buffer, "DROP CONSTRAINT");
445+
char buffer[40];
446+
char *mark;
447+
char *dropStmt = pg_strdup(te->dropStmt);
448+
char *dropStmtPtr = dropStmt;
449+
PQExpBuffer ftStmt = createPQExpBuffer();
450+
451+
/*
452+
* Need to inject IF EXISTS clause after ALTER
453+
* TABLE part in ALTER TABLE .. DROP statement
454+
*/
455+
if (strncmp(dropStmt, "ALTER TABLE", 11) == 0)
456+
{
457+
appendPQExpBuffer(ftStmt,
458+
"ALTER TABLE IF EXISTS");
459+
dropStmt = dropStmt + 11;
460+
}
461+
462+
/*
463+
* ALTER TABLE..ALTER COLUMN..DROP DEFAULT does
464+
* not support the IF EXISTS clause, and therefore
465+
* we simply emit the original command for such
466+
* objects. For other objects, we need to extract
467+
* the first part of the DROP which includes the
468+
* object type. Most of the time this matches
469+
* te->desc, so search for that; however for the
470+
* different kinds of CONSTRAINTs, we know to
471+
* search for hardcoded "DROP CONSTRAINT" instead.
472+
*/
473+
if (strcmp(te->desc, "DEFAULT") == 0)
474+
appendPQExpBuffer(ftStmt, "%s", dropStmt);
467475
else
468-
snprintf(buffer, sizeof(buffer), "DROP %s",
469-
te->desc);
476+
{
477+
if (strcmp(te->desc, "CONSTRAINT") == 0 ||
478+
strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
479+
strcmp(te->desc, "FK CONSTRAINT") == 0)
480+
strcpy(buffer, "DROP CONSTRAINT");
481+
else
482+
snprintf(buffer, sizeof(buffer), "DROP %s",
483+
te->desc);
470484

471-
mark = strstr(dropStmt, buffer);
472-
Assert(mark != NULL);
485+
mark = strstr(dropStmt, buffer);
486+
Assert(mark != NULL);
473487

474-
*mark = '\0';
475-
appendPQExpBuffer(ftStmt, "%s%s IF EXISTS%s",
476-
dropStmt, buffer,
477-
mark + strlen(buffer));
478-
}
488+
*mark = '\0';
489+
appendPQExpBuffer(ftStmt, "%s%s IF EXISTS%s",
490+
dropStmt, buffer,
491+
mark + strlen(buffer));
492+
}
479493

480-
ahprintf(AH, "%s", ftStmt->data);
494+
ahprintf(AH, "%s", ftStmt->data);
481495

482-
destroyPQExpBuffer(ftStmt);
496+
destroyPQExpBuffer(ftStmt);
483497

484-
pg_free(dropStmtPtr);
498+
pg_free(dropStmtPtr);
499+
}
485500
}
486501
}
487502
}

0 commit comments

Comments
 (0)