Re: [PATCH] no table rewrite when set column type to constrained domain

From: Viktor Holmberg <v(at)viktorh(dot)net>
To: jian he <jian(dot)universality(at)gmail(dot)com>
Cc: Aditya Gollamudi <adigollamudi(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [PATCH] no table rewrite when set column type to constrained domain
Date: 2026-03-25 13:48:54
Message-ID: 643f46b5-f0b6-4f93-aef2-a54e721fdbb9@Spark
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 24 Mar 2026 at 02:17 +0100, jian he <jian(dot)universality(at)gmail(dot)com>, wrote:
> On Tue, Mar 24, 2026 at 12:18 AM Viktor Holmberg <v(at)viktorh(dot)net> wrote:
> >
> > This appears to address some of my comments but not this one?
> >
> > CREATE DOMAIN domain1 AS INT CHECK(VALUE > 1) NOT NULL;
> > CREATE TABLE t_const_using(a INT);
> > INSERT INTO t_const_using VALUES(-2);
> > ALTER TABLE t_const_using ALTER COLUMN a SET DATA TYPE domain1 USING 5;
> > SELECT a FROM t_const_using; -- should be 5 after rewrite, not -2
> > a
> > ----
> > -2
>
> Sure, these tests are added to v6.
>
> --
> jian
> https://www.enterprisedb.com/
@@ -6077,7 +6084,7 @@ ATRewriteTables(AlterTableStmt *parsetree, List **wqueue, LOCKMODE lockmode,
 * rebuild data.
 */
 if (tab->constraints != NIL || tab->verify_new_notnull ||
- tab->partition_constraint != NULL)
+ tab->partition_constraint != NULL || tab->newvals)
ATRewriteTable(tab, InvalidOid);

I’m not 100% sure, but I think when you add an unconstraint domain:
CREATE DOMAIN mydom AS int; -- no CHECK, no NOT NULL
 CREATE TABLE t (a int);
 ALTER TABLE t ALTER COLUMN a TYPE mydom;

The tab->newvals check makes it so that ATRewriteTable is run, even though it’s not needed.

---
/*
 * ExecEvalExprNoReturn cannot be used here because
 * the expression was compiled via ExecInitExpr.
 */
 (void) ExecEvalExpr(ex->exprstate, econtext, &isnull);

I still don’t understand this comment at all. Not saying it’s wrong, but not sure if it’s right or not. Perhaps its clear for those that are more in the know so maybe if you can explain in an email it’s be enough.

---
Nit:
-- Test chaning  column data type to constrained domain

 "chaning" → "changing", and there's a double space.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Carlos Alves 2026-03-25 13:59:21 Re: COMMENTS are not being copied in CREATE TABLE LIKE
Previous Message jian he 2026-03-25 13:13:49 Re: CAST(... ON DEFAULT) - WIP build on top of Error-Safe User Functions