From 080745adce718f1ebd1065bc953d51954327fc94 Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Mon, 16 Nov 2026 21:03:25 -0500 Subject: [PATCH 2/2] Fix pg_dump --clean --if-exists for foreign table constraints. Add IF EXISTS after ALTER FOREIGN TABLE too. This covers CHECK constraints and column defaults. Discussion: https://postgr.es/m/19858-5a734f08714a5c73@postgresql.org Backpatch-through: 14 --- src/bin/pg_dump/pg_backup_archiver.c | 9 ++++++++- src/bin/pg_dump/t/002_pg_dump.pl | 11 +++++++++++ src/bin/pg_dump/t/011_restore.pl | 9 +++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index 25c1c5fb96b..0d6f981874c 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -614,7 +614,8 @@ RestoreArchive(Archive *AHX) /* * Need to inject IF EXISTS clause after ALTER - * TABLE part in ALTER TABLE .. DROP statement + * TABLE part in ALTER TABLE .. DROP statement, + * and likewise for ALTER FOREIGN TABLE */ if (strncmp(dropStmt, "ALTER TABLE", 11) == 0) { @@ -622,6 +623,12 @@ RestoreArchive(Archive *AHX) "ALTER TABLE IF EXISTS"); dropStmt = dropStmt + 11; } + else if (strncmp(dropStmt, "ALTER FOREIGN TABLE", 19) == 0) + { + appendPQExpBufferStr(ftStmt, + "ALTER FOREIGN TABLE IF EXISTS"); + dropStmt = dropStmt + 19; + } /* * ALTER TABLE..ALTER COLUMN..DROP DEFAULT does diff --git a/src/bin/pg_dump/t/002_pg_dump.pl b/src/bin/pg_dump/t/002_pg_dump.pl index f6d83eaee91..5bbc75772d9 100644 --- a/src/bin/pg_dump/t/002_pg_dump.pl +++ b/src/bin/pg_dump/t/002_pg_dump.pl @@ -3041,6 +3041,17 @@ my %tests = ( }, }, + 'ALTER FOREIGN TABLE IF EXISTS foreign_table DROP CONSTRAINT' => { + create_order => 89, + create_sql => 'ALTER FOREIGN TABLE dump_test.foreign_table + ADD CONSTRAINT foreign_table_check CHECK (c1 > 0) NOT VALID;', + regexp => qr/^ + \QALTER FOREIGN TABLE IF EXISTS dump_test.foreign_table \E + \QDROP CONSTRAINT IF EXISTS foreign_table_check;\E + /xm, + like => { clean_if_exists => 1, }, + }, + 'CREATE USER MAPPING FOR regress_dump_test_role SERVER s1' => { create_order => 86, create_sql => diff --git a/src/bin/pg_dump/t/011_restore.pl b/src/bin/pg_dump/t/011_restore.pl index e4a55df5b91..5b58914cce4 100644 --- a/src/bin/pg_dump/t/011_restore.pl +++ b/src/bin/pg_dump/t/011_restore.pl @@ -277,6 +277,11 @@ $node->safe_psql( 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; + -- likewise for a foreign table, whose drop is ALTER FOREIGN TABLE + CREATE FOREIGN DATA WRAPPER w; + CREATE SERVER s FOREIGN DATA WRAPPER w; + CREATE FOREIGN TABLE ft (a int) SERVER s; + ALTER FOREIGN TABLE ft ADD CONSTRAINT fk CHECK (a > 0) NOT VALID; }); my $notvalid_dump = "$backupdir/notvalid-dump"; $node->command_ok( @@ -314,9 +319,9 @@ $node->command_ok( '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'" + "SELECT count(*) FROM pg_constraint WHERE NOT convalidated AND conname IN ('k', 'fk')" ), - '2', + '3', 'NOT VALID constraints restored'); done_testing(); -- 2.37.1 (Apple Git-137.1)