From ab4a8d4965431243f99cfb0f1e12e26096fca864 Mon Sep 17 00:00:00 2001 From: Sai Pranav Date: Sat, 21 Nov 2026 07:23:51 +0000 Subject: [PATCH v1 REL_18 v1] Fix "unprocessed inval messages" error after ALTER DOMAIN DROP CONSTRAINT AlterDomainDropConstraint() queued its manual invalidation for the domain's pg_type row after the constraint was deleted. Nothing processed it: the deletion's CommandCounterIncrement() had already run, and the one at the end of the command does nothing because no catalog change follows. The next SAVEPOINT or PL/pgSQL exception block that changed the catalogs then failed with "cannot start a subtransaction when there are unprocessed inval messages". The same happened with DROP CONSTRAINT IF EXISTS on a missing constraint. Queue the invalidation before the deletion, so that the deletion's CommandCounterIncrement() processes it, and only when a constraint is dropped. Reported-by: Alperen Keles Discussion: https://postgr.es/m/19790-fda013085342fe7f@postgresql.org Discussion: https://postgr.es/m/19919-c286b9e822bf23b9@postgresql.org --- src/backend/commands/typecmds.c | 18 +++++++++++------- src/test/regress/expected/domain.out | 13 +++++++++++++ src/test/regress/sql/domain.sql | 13 +++++++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/backend/commands/typecmds.c b/src/backend/commands/typecmds.c index 8797b1f9e6..035102331e 100644 --- a/src/backend/commands/typecmds.c +++ b/src/backend/commands/typecmds.c @@ -2935,6 +2935,17 @@ AlterDomainDropConstraint(List *names, const char *constrName, CatalogTupleUpdate(rel, &tup->t_self, tup); } + /* + * We must send out an sinval message for the domain, to ensure that + * any dependent plans get rebuilt. Since this command doesn't change + * the domain's pg_type row, that won't happen automatically; do it + * manually. Do it before the deletion, so that the deletion's + * CommandCounterIncrement() processes it: queued after the last + * catalog change, it would be left pending, and starting a + * subtransaction would then fail. + */ + CacheInvalidateHeapTuple(rel, tup, NULL); + conobj.classId = ConstraintRelationId; conobj.objectId = construct->oid; conobj.objectSubId = 0; @@ -2960,13 +2971,6 @@ AlterDomainDropConstraint(List *names, const char *constrName, constrName, TypeNameToString(typename)))); } - /* - * We must send out an sinval message for the domain, to ensure that any - * dependent plans get rebuilt. Since this command doesn't change the - * domain's pg_type row, that won't happen automatically; do it manually. - */ - CacheInvalidateHeapTuple(rel, tup, NULL); - ObjectAddressSet(address, TypeRelationId, domainoid); /* Clean up */ diff --git a/src/test/regress/expected/domain.out b/src/test/regress/expected/domain.out index b5ea707df3..3bbd8169bf 100644 --- a/src/test/regress/expected/domain.out +++ b/src/test/regress/expected/domain.out @@ -872,6 +872,19 @@ alter domain con drop constraint nonexistent; ERROR: constraint "nonexistent" of domain "con" does not exist alter domain con drop constraint if exists nonexistent; NOTICE: constraint "nonexistent" of domain "con" does not exist, skipping +-- a subtransaction after DROP CONSTRAINT must work +create domain dsubxact as int constraint dsubxact_pos check (value > 0); +begin; +alter domain dsubxact drop constraint dsubxact_pos; +savepoint s1; +drop domain dsubxact; +rollback; +begin; +alter domain dsubxact drop constraint if exists nonexistent; +NOTICE: constraint "nonexistent" of domain "dsubxact" does not exist, skipping +savepoint s1; +drop domain dsubxact; +commit; -- not-null constraints create domain connotnull integer; create table domconnotnulltest diff --git a/src/test/regress/sql/domain.sql b/src/test/regress/sql/domain.sql index b8f5a63971..6e79ff74b8 100644 --- a/src/test/regress/sql/domain.sql +++ b/src/test/regress/sql/domain.sql @@ -496,6 +496,19 @@ insert into domcontest values (42); alter domain con drop constraint nonexistent; alter domain con drop constraint if exists nonexistent; +-- a subtransaction after DROP CONSTRAINT must work +create domain dsubxact as int constraint dsubxact_pos check (value > 0); +begin; +alter domain dsubxact drop constraint dsubxact_pos; +savepoint s1; +drop domain dsubxact; +rollback; +begin; +alter domain dsubxact drop constraint if exists nonexistent; +savepoint s1; +drop domain dsubxact; +commit; + -- not-null constraints create domain connotnull integer; create table domconnotnulltest -- 2.50.1 (Apple Git-155)