From 87b43d1b02051ae3a93ea47d27ace459f9442f4f Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Thu, 19 Nov 2026 22:45:11 -0500 Subject: [PATCH 2/2] Don't fail ALTER DOMAIN DROP CONSTRAINT IF EXISTS on a missing domain. pg_dump --clean --if-exists relies on this for NOT VALID domain constraints. b152c6cd0de did the same for DROP TRIGGER. Reported-by: Atish Prakash <125atishprak@gmail.com> Discussion: https://postgr.es/m/19858-5a734f08714a5c73@postgresql.org --- doc/src/sgml/ref/alter_domain.sgml | 3 ++- src/backend/commands/typecmds.c | 17 +++++++++++++++-- src/bin/pg_dump/t/011_restore.pl | 10 ++++++++-- src/test/regress/expected/drop_if_exists.out | 6 ++++++ src/test/regress/sql/drop_if_exists.sql | 4 ++++ 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/doc/src/sgml/ref/alter_domain.sgml b/doc/src/sgml/ref/alter_domain.sgml index 74855172222..89a0a11a761 100644 --- a/doc/src/sgml/ref/alter_domain.sgml +++ b/doc/src/sgml/ref/alter_domain.sgml @@ -103,7 +103,8 @@ ALTER DOMAIN name This form drops constraints on a domain. If IF EXISTS is specified and the constraint - does not exist, no error is thrown. In this case a notice is issued instead. + or the domain does not exist, no error is thrown. In this case a notice + is issued instead. diff --git a/src/backend/commands/typecmds.c b/src/backend/commands/typecmds.c index 9f57728129f..a8f0dbe42c7 100644 --- a/src/backend/commands/typecmds.c +++ b/src/backend/commands/typecmds.c @@ -2896,7 +2896,8 @@ AlterDomainNotNull(List *names, bool notNull) * * Implements the ALTER DOMAIN DROP CONSTRAINT statement * - * Returns ObjectAddress of the modified domain. + * Returns ObjectAddress of the modified domain. If missing_ok is true and + * the domain does not exist, InvalidObjectAddress is returned. */ ObjectAddress AlterDomainDropConstraint(List *names, const char *constrName, @@ -2915,7 +2916,19 @@ AlterDomainDropConstraint(List *names, const char *constrName, /* Make a TypeName so we can use standard type lookup machinery */ typename = makeTypeNameFromNameList(names); - domainoid = typenameTypeId(NULL, typename); + + /* + * With IF EXISTS, a missing domain is not an error either. pg_dump + * --clean --if-exists relies on that. + */ + domainoid = LookupTypeNameOid(NULL, typename, missing_ok); + if (!OidIsValid(domainoid)) + { + ereport(NOTICE, + (errmsg("type \"%s\" does not exist, skipping", + TypeNameToString(typename)))); + return InvalidObjectAddress; + } /* Look up the domain in the type table */ rel = table_open(TypeRelationId, RowExclusiveLock); diff --git a/src/bin/pg_dump/t/011_restore.pl b/src/bin/pg_dump/t/011_restore.pl index a0ff9fb0bc0..cbebf0a2abd 100644 --- a/src/bin/pg_dump/t/011_restore.pl +++ b/src/bin/pg_dump/t/011_restore.pl @@ -268,6 +268,8 @@ foreach my $c (@dumpall_cases) # 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. +# Likewise for a domain, with ALTER DOMAIN. The domain's check calls a +# function, so the constraint has to be dropped before the function is. $node->safe_psql('postgres', 'CREATE DATABASE clean_src'); $node->safe_psql('postgres', 'CREATE DATABASE clean_dst'); $node->safe_psql( @@ -276,6 +278,10 @@ $node->safe_psql( 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; + CREATE FUNCTION is_pos(int) RETURNS bool + LANGUAGE sql IMMUTABLE AS 'SELECT $1 > 0'; + CREATE DOMAIN d AS int; + ALTER DOMAIN d ADD CONSTRAINT d_check CHECK (is_pos(VALUE)) NOT VALID; }); my $clean_dump = "$backupdir/clean-if-exists.sql"; $node->command_ok( @@ -298,9 +304,9 @@ foreach my $target ('empty', 'existing') } is( $node->safe_psql( 'clean_dst', - "SELECT count(*) FROM pg_constraint WHERE NOT convalidated AND conname = 'ft_check'" + "SELECT count(*) FROM pg_constraint WHERE NOT convalidated AND conname IN ('ft_check', 'd_check')" ), - '1', + '2', 'NOT VALID constraints restored'); done_testing(); diff --git a/src/test/regress/expected/drop_if_exists.out b/src/test/regress/expected/drop_if_exists.out index 5e44c2c3cea..557fe4d3e71 100644 --- a/src/test/regress/expected/drop_if_exists.out +++ b/src/test/regress/expected/drop_if_exists.out @@ -57,6 +57,12 @@ DROP DOMAIN test_domain_exists; ERROR: type "test_domain_exists" does not exist DROP DOMAIN IF EXISTS test_domain_exists; NOTICE: type "test_domain_exists" does not exist, skipping +ALTER DOMAIN test_domain_exists DROP CONSTRAINT test_constraint_exists; +ERROR: type "test_domain_exists" does not exist +ALTER DOMAIN test_domain_exists DROP CONSTRAINT IF EXISTS test_constraint_exists; +NOTICE: type "test_domain_exists" does not exist, skipping +ALTER DOMAIN no_such_schema.test_domain_exists DROP CONSTRAINT IF EXISTS test_constraint_exists; +NOTICE: type "no_such_schema.test_domain_exists" does not exist, skipping CREATE domain test_domain_exists as int not null check (value > 0); DROP DOMAIN IF EXISTS test_domain_exists; DROP DOMAIN test_domain_exists; diff --git a/src/test/regress/sql/drop_if_exists.sql b/src/test/regress/sql/drop_if_exists.sql index ac6168b91f8..24e3a97ab99 100644 --- a/src/test/regress/sql/drop_if_exists.sql +++ b/src/test/regress/sql/drop_if_exists.sql @@ -76,6 +76,10 @@ DROP DOMAIN test_domain_exists; DROP DOMAIN IF EXISTS test_domain_exists; +ALTER DOMAIN test_domain_exists DROP CONSTRAINT test_constraint_exists; +ALTER DOMAIN test_domain_exists DROP CONSTRAINT IF EXISTS test_constraint_exists; +ALTER DOMAIN no_such_schema.test_domain_exists DROP CONSTRAINT IF EXISTS test_constraint_exists; + CREATE domain test_domain_exists as int not null check (value > 0); DROP DOMAIN IF EXISTS test_domain_exists; -- 2.37.1 (Apple Git-137.1)