Re: [PATCH v1] Fix for Bug#19724 - ALTER TYPE ... ALTER ATTRIBUTE triggers internal error for base type of domain with check

From: "Matheus Alcantara" <matheusssilv97(at)gmail(dot)com>
To: "Zsolt Parragi" <zsolt(dot)parragi(at)percona(dot)com>
Cc: <pgsql-hackers(at)lists(dot)postgresql(dot)org>, "Nitin Motiani" <nitinmotiani(at)google(dot)com>, "Ayush Tiwari" <ayushtiwari(dot)slg01(at)gmail(dot)com>, "Heikki Linnakangas" <hlinnaka(at)iki(dot)fi>
Subject: Re: [PATCH v1] Fix for Bug#19724 - ALTER TYPE ... ALTER ATTRIBUTE triggers internal error for base type of domain with check
Date: 2026-11-24 15:49:55
Message-ID: DN3K6DEXRDV4.PXVHFKAOUI79@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Thanks for testing the patch!

On Mon Nov 23, 2026 at 6:59 PM -03, Zsolt Parragi wrote:
> The following shape still doesn't work properly with v5:
>
> create type ct as (i int);
> create function ct_mk(int) returns ct language sql as 'select row($1)::ct';
> create table t2 (x int check ((ct_mk(x)).i > 0));
> insert into t2 values (1);
> create domain d as int constraint d_check check ((ct_mk(value)).i > 0);
> comment on constraint d_check on domain d is 'bob comment';
>
> s1: begin; select count(*) from t2;
> s2: alter type ct alter attribute i type bigint; (waits for t2)
> s3: alter domain d rename to d_old; create domain d as int;
> s1: commit;
>
> -- master: s2 fails
> ERROR: could not identify relation associated with constraint 16408
>
> -- v5: s2 succeeds, and
> select t.typname, c.conname, obj_description(c.oid,'pg_constraint')
> from pg_constraint c join pg_type t on t.oid = c.contypid;
> d | d_check | bob comment
> select (-1)::d_old;
> d_old
> -------
> -1
>
>> We may try to capture the domain oid above AlterDomainAddConstraint,
>> while the old constraint still exists and re-add the constraint to that
>> OID instead of resolving the name again.
>
> That doesn't seem that complicated to me, and it would fix the above issue?
>

Yes, you're right, it wasn't that complicated, and I think any fix for
your case that doesn't capture the OID would be more complicated. v6
attached does this.

The re-add now takes the domain's OID from the old constraint in
ATPostAlterTypeParse(), while it still exists, and re-adds the
constraint to that OID instead of resolving the name from the deparsed
definition again.

Changes in v6, all in 0003:

- AT_ReAddDomainConstraint now carries a new ad-hoc node,
ATReAddDomainConstraint, with the domain OID, the constraint and its
comment, instead of the AlterDomainStmt.

- The domain is locked with AccessShareLock when its OID is captured, so
a concurrent DROP DOMAIN after that point waits for the ALTER instead
of removing the domain before the constraint is re-added.

- Since the OID now comes from the old constraint, I think that the "is
not a domain" check can't be reached from SQL anymore, so it's an
elog() now.

- The comment on a domain constraint is restored right after the re-add,
using the new constraint's OID, so it can't end up on another domain
either. RebuildConstraintComment() now handles only table constraints.

- Added your scenario as an isolation test. With v5 it shows d_check and
its comment on the new d; with v6 they stay on d_old and (-1)::d_old
fails again.

Regarding back-patching, I think that we will need a different 0003
patch for older versions because of the new node type. Another option is
to keep the OIDs in a list in AlteredTableInfo instead of adding a new
node, which I think could then be back-patched as is. But I'm not sure
this issue is common enough to justify that approach (I'm not a big fan
of it, but I'm OK with it if we decide that we need to back-patch).
Thoughts?

--
Matheus Alcantara
EDB: https://www.enterprisedb.com

Attachment Content-Type Size
v6-0001-Fix-ALTER-.-TYPE-failure-with-dependent-domain-co.patch text/plain 10.3 KB
v6-0002-Validate-re-added-domain-constraints-after-ALTER-.patch text/plain 15.3 KB
v6-0003-Don-t-require-ownership-when-rebuilding-constrain.patch text/plain 23.9 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Melanie Plageman 2026-11-24 16:10:52 Re: Stop masking visibility information during WAL consistency checking
Previous Message Henson Choi 2026-11-24 15:23:36 Re: [PATCH] Consider window functions when reordering GROUP BY items