From b4af83c969adcbf23ffbab0262aef38697047adb Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Thu, 19 Nov 2026 22:45:11 -0500 Subject: [PATCH] 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: 15 --- 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 | 37 ++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index 3a811fdf56f..6a1270f9828 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -572,7 +572,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) { @@ -580,6 +581,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 cbb2e47722e..9f394dcd768 100644 --- a/src/bin/pg_dump/t/002_pg_dump.pl +++ b/src/bin/pg_dump/t/002_pg_dump.pl @@ -2358,6 +2358,17 @@ my %tests = ( unlike => { exclude_dump_test_schema => 1, }, }, + '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 51062392740..d018f6e0ee1 100644 --- a/src/bin/pg_dump/t/011_restore.pl +++ b/src/bin/pg_dump/t/011_restore.pl @@ -265,5 +265,42 @@ foreach my $c (@dumpall_cases) "restore from pg_dumpall --clean ($c->{name})"); } +# A "pg_dump --clean --if-exists" script must run without errors both in an +# empty database and over an existing copy. A NOT VALID constraint on a +# foreign table is dumped separately and dropped with ALTER FOREIGN TABLE. +$node->safe_psql('postgres', 'CREATE DATABASE clean_src'); +$node->safe_psql('postgres', 'CREATE DATABASE clean_dst'); +$node->safe_psql( + 'clean_src', q{ + 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 ft_check CHECK (a > 0) NOT VALID; +}); +my $clean_dump = "$backupdir/clean-if-exists.sql"; +$node->command_ok( + [ + 'pg_dump', '--clean', '--if-exists', '--no-sync', + '--file' => $clean_dump, + $node->connstr('clean_src'), + ], + 'dump for: restore from pg_dump --clean --if-exists'); +foreach my $target ('empty', 'existing') +{ + $node->command_ok( + [ + 'psql', '--no-psqlrc', + '--set' => 'ON_ERROR_STOP=1', + '--file' => $clean_dump, + '--dbname' => $node->connstr('clean_dst'), + ], + "restore from pg_dump --clean --if-exists into $target database"); +} +is( $node->safe_psql( + 'clean_dst', + "SELECT count(*) FROM pg_constraint WHERE NOT convalidated AND conname = 'ft_check'" + ), + '1', + 'NOT VALID constraints restored'); done_testing(); -- 2.37.1 (Apple Git-137.1)