Skip to content

Commit fd02931

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 35419ae commit fd02931

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
@@ -440,60 +440,75 @@ RestoreArchive(Archive *AHX)
440440
}
441441
else
442442
{
443-
char buffer[40];
444-
char *mark;
445-
char *dropStmt = pg_strdup(te->dropStmt);
446-
char *dropStmtPtr = dropStmt;
447-
PQExpBuffer ftStmt = createPQExpBuffer();
448-
449443
/*
450-
* Need to inject IF EXISTS clause after ALTER TABLE
451-
* part in ALTER TABLE .. DROP statement
444+
* Inject an appropriate spelling of "if exists". For
445+
* large objects, we have a separate routine that
446+
* knows how to do it, without depending on
447+
* te->dropStmt; use that. For other objects we need
448+
* to parse the command.
449+
*
452450
*/
453-
if (strncmp(dropStmt, "ALTER TABLE", 11) == 0)
451+
if (strncmp(te->desc, "BLOB", 4) == 0)
454452
{
455-
appendPQExpBuffer(ftStmt,
456-
"ALTER TABLE IF EXISTS");
457-
dropStmt = dropStmt + 11;
453+
DropBlobIfExists(AH, te->catalogId.oid);
458454
}
459-
460-
/*
461-
* ALTER TABLE..ALTER COLUMN..DROP DEFAULT does not
462-
* support the IF EXISTS clause, and therefore we
463-
* simply emit the original command for such objects.
464-
* For other objects, we need to extract the first
465-
* part of the DROP which includes the object type.
466-
* Most of the time this matches te->desc, so search
467-
* for that; however for the different kinds of
468-
* CONSTRAINTs, we know to search for hardcoded "DROP
469-
* CONSTRAINT" instead.
470-
*/
471-
if (strcmp(te->desc, "DEFAULT") == 0)
472-
appendPQExpBuffer(ftStmt, "%s", dropStmt);
473455
else
474456
{
475-
if (strcmp(te->desc, "CONSTRAINT") == 0 ||
476-
strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
477-
strcmp(te->desc, "FK CONSTRAINT") == 0)
478-
strcpy(buffer, "DROP CONSTRAINT");
457+
char buffer[40];
458+
char *mark;
459+
char *dropStmt = pg_strdup(te->dropStmt);
460+
char *dropStmtPtr = dropStmt;
461+
PQExpBuffer ftStmt = createPQExpBuffer();
462+
463+
/*
464+
* Need to inject IF EXISTS clause after ALTER
465+
* TABLE part in ALTER TABLE .. DROP statement
466+
*/
467+
if (strncmp(dropStmt, "ALTER TABLE", 11) == 0)
468+
{
469+
appendPQExpBuffer(ftStmt,
470+
"ALTER TABLE IF EXISTS");
471+
dropStmt = dropStmt + 11;
472+
}
473+
474+
/*
475+
* ALTER TABLE..ALTER COLUMN..DROP DEFAULT does
476+
* not support the IF EXISTS clause, and therefore
477+
* we simply emit the original command for such
478+
* objects. For other objects, we need to extract
479+
* the first part of the DROP which includes the
480+
* object type. Most of the time this matches
481+
* te->desc, so search for that; however for the
482+
* different kinds of CONSTRAINTs, we know to
483+
* search for hardcoded "DROP CONSTRAINT" instead.
484+
*/
485+
if (strcmp(te->desc, "DEFAULT") == 0)
486+
appendPQExpBuffer(ftStmt, "%s", dropStmt);
479487
else
480-
snprintf(buffer, sizeof(buffer), "DROP %s",
481-
te->desc);
488+
{
489+
if (strcmp(te->desc, "CONSTRAINT") == 0 ||
490+
strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
491+
strcmp(te->desc, "FK CONSTRAINT") == 0)
492+
strcpy(buffer, "DROP CONSTRAINT");
493+
else
494+
snprintf(buffer, sizeof(buffer), "DROP %s",
495+
te->desc);
482496

483-
mark = strstr(dropStmt, buffer);
484-
Assert(mark != NULL);
497+
mark = strstr(dropStmt, buffer);
498+
Assert(mark != NULL);
485499

486-
*mark = '\0';
487-
appendPQExpBuffer(ftStmt, "%s%s IF EXISTS%s",
488-
dropStmt, buffer,
489-
mark + strlen(buffer));
490-
}
500+
*mark = '\0';
501+
appendPQExpBuffer(ftStmt, "%s%s IF EXISTS%s",
502+
dropStmt, buffer,
503+
mark + strlen(buffer));
504+
}
491505

492-
ahprintf(AH, "%s", ftStmt->data);
506+
ahprintf(AH, "%s", ftStmt->data);
493507

494-
destroyPQExpBuffer(ftStmt);
508+
destroyPQExpBuffer(ftStmt);
495509

496-
pg_free(dropStmtPtr);
510+
pg_free(dropStmtPtr);
511+
}
497512
}
498513
}
499514
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy