From 015c34f97411cefea9efd1162b60bcae7c005f73 Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Mon, 16 Nov 2026 21:03:25 -0500 Subject: [PATCH 1/2] Fix pg_dump --clean --if-exists for NOT VALID domain constraints. ALTER DOMAIN has no IF EXISTS, so skip the constraint drop when DROP DOMAIN will remove it anyway. Reported-by: Atish Prakash <125atishprak@gmail.com> Discussion: https://postgr.es/m/19858-5a734f08714a5c73@postgresql.org Backpatch-through: 14 --- src/bin/pg_dump/pg_backup_archiver.c | 54 ++++++++++++++++++++++++++++ src/bin/pg_dump/t/002_pg_dump.pl | 13 +++++++ src/bin/pg_dump/t/011_restore.pl | 53 +++++++++++++++++++++++++++ 3 files changed, 120 insertions(+) diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index d3e0006fa07..25c1c5fb96b 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -65,6 +65,7 @@ static void _doSetSessionAuth(ArchiveHandle *AH, const char *user); static void _reconnectToDB(ArchiveHandle *AH, const char *dbname); static void _becomeUser(ArchiveHandle *AH, const char *user); static void _becomeOwner(ArchiveHandle *AH, TocEntry *te); +static bool droppedWithDomain(ArchiveHandle *AH, TocEntry *te); static void _selectOutputSchema(ArchiveHandle *AH, const char *schemaName); static void _selectTablespace(ArchiveHandle *AH, const char *tablespace); static void _selectTableAccessMethod(ArchiveHandle *AH, const char *tableam); @@ -583,6 +584,15 @@ RestoreArchive(Archive *AHX) */ ahprintf(AH, "%s", te->dropStmt); } + else if (droppedWithDomain(AH, te)) + { + /* + * ALTER DOMAIN has no IF EXISTS option, so we can't + * make this command safe if the domain is missing. + * But the domain is being dropped too, which will + * take the constraint with it, so just skip this. + */ + } else { /* @@ -2044,6 +2054,50 @@ getTocEntryByDumpId(ArchiveHandle *AH, DumpId id) return NULL; } +/* + * Is te a separately-dumped domain constraint whose domain is also + * going to be dropped during this restore? + * + * We can't rely on te's dependencies, since pg_dump doesn't fill those in + * for plain-text output. Instead, match the domain name in the two drop + * commands, "ALTER DOMAIN name DROP CONSTRAINT ..." and "DROP DOMAIN name;". + */ +static bool +droppedWithDomain(ArchiveHandle *AH, TocEntry *te) +{ + const char *rest; + + if (strcmp(te->desc, "CHECK CONSTRAINT") != 0 && + strcmp(te->desc, "CONSTRAINT") != 0) + return false; + if (strncmp(te->dropStmt, "ALTER DOMAIN ", 13) != 0) + return false; + rest = te->dropStmt + 13; + + for (TocEntry *dte = AH->toc->next; dte != AH->toc; dte = dte->next) + { + const char *domname; + size_t len; + + if (strcmp(dte->desc, "DOMAIN") != 0 || + (dte->reqs & REQ_SCHEMA) == 0 || + dte->dropStmt == NULL || + strncmp(dte->dropStmt, "DROP DOMAIN ", 12) != 0) + continue; + /* the name is whatever is between "DROP DOMAIN " and ";\n" */ + domname = dte->dropStmt + 12; + len = strlen(domname); + if (len < 2 || strcmp(domname + len - 2, ";\n") != 0) + continue; + len -= 2; + if (strncmp(rest, domname, len) == 0 && + strncmp(rest + len, " DROP CONSTRAINT ", 17) == 0) + return true; + } + + return false; +} + int TocIDRequired(ArchiveHandle *AH, DumpId id) { diff --git a/src/bin/pg_dump/t/002_pg_dump.pl b/src/bin/pg_dump/t/002_pg_dump.pl index ec73c4d5be9..f6d83eaee91 100644 --- a/src/bin/pg_dump/t/002_pg_dump.pl +++ b/src/bin/pg_dump/t/002_pg_dump.pl @@ -2355,6 +2355,19 @@ my %tests = ( }, }, + 'ALTER DOMAIN dump_test.d_notvalid DROP CONSTRAINT' => { + create_order => 30, + create_sql => 'CREATE DOMAIN dump_test.d_notvalid AS int; + ALTER DOMAIN dump_test.d_notvalid + ADD CONSTRAINT d_notvalid_check CHECK (VALUE > 0) NOT VALID;', + regexp => qr/^ + \QALTER DOMAIN dump_test.d_notvalid DROP CONSTRAINT\E + /xm, + + # with --if-exists, the domain's own drop takes care of this + like => { clean => 1, }, + }, + 'COMMENT ON CONSTRAINT ON DOMAIN (1)' => { regexp => qr/^ \QCOMMENT ON CONSTRAINT nn ON DOMAIN dump_test.us_postal_code IS 'not null';\E diff --git a/src/bin/pg_dump/t/011_restore.pl b/src/bin/pg_dump/t/011_restore.pl index 9ce275ee8da..e4a55df5b91 100644 --- a/src/bin/pg_dump/t/011_restore.pl +++ b/src/bin/pg_dump/t/011_restore.pl @@ -265,5 +265,58 @@ foreach my $c (@dumpall_cases) "restore from pg_dumpall --clean ($c->{name})"); } +# A NOT VALID domain constraint is dumped separately, and its drop command +# is ALTER DOMAIN ... DROP CONSTRAINT, which can't be made conditional on the +# domain existing. Check that --clean --if-exists works into an empty +# database, and still drops the constraint when it is restored alone. +$node->safe_psql('postgres', 'CREATE DATABASE notvalid_src'); +$node->safe_psql('postgres', 'CREATE DATABASE notvalid_dst'); +$node->safe_psql( + 'notvalid_src', q{ + CREATE DOMAIN d AS int; + ALTER DOMAIN d ADD CONSTRAINT k CHECK (VALUE > 0) NOT VALID; + CREATE DOMAIN "d x" AS int; + ALTER DOMAIN "d x" ADD CONSTRAINT k CHECK (VALUE > 0) NOT VALID; +}); +my $notvalid_dump = "$backupdir/notvalid-dump"; +$node->command_ok( + [ + 'pg_dump', + '--format' => 'custom', + '--no-sync', + '--file' => $notvalid_dump, + $node->connstr('notvalid_src'), + ], + 'dump NOT VALID constraints'); +$node->command_ok( + [ + 'pg_restore', '--clean', '--if-exists', '--exit-on-error', + '--dbname' => $node->connstr('notvalid_dst'), + $notvalid_dump, + ], + 'pg_restore --clean --if-exists of NOT VALID constraints into empty database' +); + +my ($toc, $toc_err); +run_log [ 'pg_restore', '--list', $notvalid_dump ], '>' => \$toc, + '2>' => \$toc_err; +my $toc_file = "$backupdir/notvalid-toc"; +open(my $fh, '>', $toc_file) or die "could not open $toc_file: $!"; +print $fh grep { /CHECK CONSTRAINT/ } split(/^/, $toc); +close($fh); +$node->command_ok( + [ + 'pg_restore', '--clean', '--if-exists', '--exit-on-error', + '--use-list' => $toc_file, + '--dbname' => $node->connstr('notvalid_dst'), + $notvalid_dump, + ], + 'pg_restore --clean --if-exists of only NOT VALID constraints'); +is( $node->safe_psql( + 'notvalid_dst', + "SELECT count(*) FROM pg_constraint WHERE NOT convalidated AND conname = 'k'" + ), + '2', + 'NOT VALID constraints restored'); done_testing(); -- 2.37.1 (Apple Git-137.1)