From af40571acda21cffd16bd00e08d89869fd782691 Mon Sep 17 00:00:00 2001 From: Nitin Motiani Date: Mon, 28 Sep 2026 12:49:33 +0000 Subject: [PATCH v5 1/3] Fix ALTER ... TYPE failure with dependent domain constraints. When ALTER TYPE ... ALTER ATTRIBUTE ... TYPE or ALTER TABLE ... ALTER COLUMN ... TYPE changes a column that a domain's CHECK constraint depends on, ATPostAlterTypeCleanup() arranges for the constraint to be dropped and re-created. Since commit af20e2d72, it chose the relation to attach the re-creation step to by calling get_typ_typrelid(getBaseType(con->contypid)), which only works if the domain's base type is composite. For a domain whose base type isn't composite (for example int, float8 or int[], possibly via another domain) but whose CHECK expression references a composite type or a table's row type, that returned InvalidOid, and the command failed with "could not identify relation associated with constraint". For a domain constraint, that relation OID only decides whether another relation gets locked and which work queue entry receives the AT_ReAddDomainConstraint command; the command itself identifies the domain by name. So just use the relation being altered (tab->relid), which is already locked. A side effect is that for a domain over a composite type other than the one being altered, we no longer lock that other type's relation or add a work queue entry for it, neither of which was needed. Since commit ac460a144ac, find_composite_type_dependencies() rejects the ALTER up front when the CHECK expression references the composite type directly, e.g. via ROW(value)::rt, so the originally reported case now fails with a proper "cannot alter type" error. The problem is still reachable when the expression only depends on a column of the type, for example by applying a field selection to a function returning it, so the tests use that form. Bug: #19724 Reported-by: Alexander Lakhin Author: Nitin Motiani Reviewed-by: Ayush Tiwari Reviewed-by: Rahul Yadav Reviewed-by: Matheus Alcantara --- src/backend/commands/tablecmds.c | 12 ++-- src/test/regress/expected/domain.out | 95 ++++++++++++++++++++++++++++ src/test/regress/sql/domain.sql | 68 ++++++++++++++++++++ 3 files changed, 171 insertions(+), 4 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 9786a5af58a..591a0c9528c 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -16833,10 +16833,14 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode) relid = con->conrelid; else { - /* must be a domain constraint */ - relid = get_typ_typrelid(getBaseType(con->contypid)); - if (!OidIsValid(relid)) - elog(ERROR, "could not identify relation associated with constraint %u", oldId); + /* + * Must be a domain constraint. The domain's base type need not + * be composite, so there may be no relation associated with it. + * Since the relid is only used to determine which work queue + * entry the command is attached to, we can use the relation which + * is being altered and which we already hold a lock on. + */ + relid = tab->relid; } confrelid = con->confrelid; conislocal = con->conislocal; diff --git a/src/test/regress/expected/domain.out b/src/test/regress/expected/domain.out index 62a48a523a2..1caa96cdee2 100644 --- a/src/test/regress/expected/domain.out +++ b/src/test/regress/expected/domain.out @@ -432,6 +432,101 @@ select conname, obj_description(oid, 'pg_constraint') from pg_constraint drop type comptype cascade; NOTICE: drop cascades to type dcomptype +-- check altering columns used by constraints of domains whose base type +-- isn't composite (bug #19724). A direct reference to the composite type +-- in the CHECK expression makes find_composite_type_dependencies() reject +-- the ALTER, so reach it through a function returning the type instead; +-- that leaves only a dependency on the referenced column. +create type rt as (i int); +create domain dt as int check ((row(value)::rt).i > 0); +alter type rt alter attribute i type bigint; -- fail +ERROR: cannot alter type "rt" because constraint dt_check uses it +drop domain dt; +create function rt_mk(int) returns rt language sql as 'select row($1)::rt'; +create domain dt as int check ((rt_mk(value)).i > 0); +alter type rt alter attribute i type text; -- fail +ERROR: operator does not exist: text > integer +DETAIL: No operator of that name accepts the given argument types. +HINT: You might need to add explicit type casts. +alter type rt alter attribute i type bigint; +select 1::dt; + dt +---- + 1 +(1 row) + +select (-1)::dt; -- fail +ERROR: value for domain dt violates check constraint "dt_check" +drop domain dt; +drop function rt_mk(int); +drop type rt; +-- same for a domain over float8 that uses one field of a two-field type +create type comptype as (r float8, i float8); +create function comptype_mk(float8) returns comptype language sql + as 'select row($1, 0)::comptype'; +create domain silly as float8 check ((comptype_mk(value)).r > 0); +alter type comptype alter attribute r type bigint; +select 1.0::silly; + silly +------- + 1 +(1 row) + +select (-1.0)::silly; -- fail +ERROR: value for domain silly violates check constraint "silly_check" +drop domain silly; +drop function comptype_mk(float8); +drop type comptype; +-- rebuilding a constraint for one type it depends on must preserve its +-- dependency on another +create type r1 as (a int); +create type r2 as (b int); +create function r1_mk(int) returns r1 language sql as 'select row($1)::r1'; +create function r2_mk(int) returns r2 language sql as 'select row($1)::r2'; +create domain dt_multi as int + check ((r1_mk(value)).a > 0 and (r2_mk(value)).b > 0); +alter type r1 alter attribute a type bigint; +alter type r2 alter attribute b type text; -- fail +ERROR: operator does not exist: text > integer +DETAIL: No operator of that name accepts the given argument types. +HINT: You might need to add explicit type casts. +alter type r2 alter attribute b type bigint; +select 1::dt_multi; + dt_multi +---------- + 1 +(1 row) + +select (-1)::dt_multi; -- fail +ERROR: value for domain dt_multi violates check constraint "dt_multi_check" +drop domain dt_multi; +drop function r1_mk(int); +drop function r2_mk(int); +drop type r1; +drop type r2; +-- same via ALTER TABLE, with the constraint depending on both the parent's +-- and the inheritance child's row types +create table dp (a int); +create table dc (b int) inherits (dp); +create function dp_mk(int) returns dp language sql as 'select row($1)::dp'; +create function dc_mk(int) returns dc language sql + as 'select row($1, $1)::dc'; +create domain dt_inh as int + check ((dp_mk(value)).a > 0 and (dc_mk(value)).a > 0); +alter table dp alter column a type bigint; +select 1::dt_inh; + dt_inh +-------- + 1 +(1 row) + +select (-1)::dt_inh; -- fail +ERROR: value for domain dt_inh violates check constraint "dt_inh_check" +drop domain dt_inh; +drop function dp_mk(int); +drop function dc_mk(int); +drop table dc; +drop table dp; -- Test domains over arrays of composite create type comptype as (r float8, i float8); create domain dcomptypea as comptype[]; diff --git a/src/test/regress/sql/domain.sql b/src/test/regress/sql/domain.sql index b8f5a639712..0f874a365e3 100644 --- a/src/test/regress/sql/domain.sql +++ b/src/test/regress/sql/domain.sql @@ -219,6 +219,74 @@ select conname, obj_description(oid, 'pg_constraint') from pg_constraint drop type comptype cascade; +-- check altering columns used by constraints of domains whose base type +-- isn't composite (bug #19724). A direct reference to the composite type +-- in the CHECK expression makes find_composite_type_dependencies() reject +-- the ALTER, so reach it through a function returning the type instead; +-- that leaves only a dependency on the referenced column. +create type rt as (i int); +create domain dt as int check ((row(value)::rt).i > 0); +alter type rt alter attribute i type bigint; -- fail +drop domain dt; +create function rt_mk(int) returns rt language sql as 'select row($1)::rt'; +create domain dt as int check ((rt_mk(value)).i > 0); +alter type rt alter attribute i type text; -- fail +alter type rt alter attribute i type bigint; +select 1::dt; +select (-1)::dt; -- fail +drop domain dt; +drop function rt_mk(int); +drop type rt; + +-- same for a domain over float8 that uses one field of a two-field type +create type comptype as (r float8, i float8); +create function comptype_mk(float8) returns comptype language sql + as 'select row($1, 0)::comptype'; +create domain silly as float8 check ((comptype_mk(value)).r > 0); +alter type comptype alter attribute r type bigint; +select 1.0::silly; +select (-1.0)::silly; -- fail +drop domain silly; +drop function comptype_mk(float8); +drop type comptype; + +-- rebuilding a constraint for one type it depends on must preserve its +-- dependency on another +create type r1 as (a int); +create type r2 as (b int); +create function r1_mk(int) returns r1 language sql as 'select row($1)::r1'; +create function r2_mk(int) returns r2 language sql as 'select row($1)::r2'; +create domain dt_multi as int + check ((r1_mk(value)).a > 0 and (r2_mk(value)).b > 0); +alter type r1 alter attribute a type bigint; +alter type r2 alter attribute b type text; -- fail +alter type r2 alter attribute b type bigint; +select 1::dt_multi; +select (-1)::dt_multi; -- fail +drop domain dt_multi; +drop function r1_mk(int); +drop function r2_mk(int); +drop type r1; +drop type r2; + +-- same via ALTER TABLE, with the constraint depending on both the parent's +-- and the inheritance child's row types +create table dp (a int); +create table dc (b int) inherits (dp); +create function dp_mk(int) returns dp language sql as 'select row($1)::dp'; +create function dc_mk(int) returns dc language sql + as 'select row($1, $1)::dc'; +create domain dt_inh as int + check ((dp_mk(value)).a > 0 and (dc_mk(value)).a > 0); +alter table dp alter column a type bigint; +select 1::dt_inh; +select (-1)::dt_inh; -- fail +drop domain dt_inh; +drop function dp_mk(int); +drop function dc_mk(int); +drop table dc; +drop table dp; + -- Test domains over arrays of composite -- 2.50.1 (Apple Git-155)