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

From: jian he <jian(dot)universality(at)gmail(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [PATCH] no table rewrite when set column type to constrained domain
Date: 2025-12-15 07:06:40
Message-ID: CACJufxFEGpLYVBzt=cTp2xFZpm02CfyGkMz6bj3Xqx9x3=bNxQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Aug 26, 2025 at 11:26 AM jian he <jian(dot)universality(at)gmail(dot)com> wrote:
>
> typedef struct NewColumnValue
> {
> AttrNumber attnum; /* which column */
> Expr *expr; /* expression to compute */
> ExprState *exprstate; /* execution state */
> bool is_generated; /* is it a GENERATED expression? */
> bool scan_only; /* table scan only */
> } NewColumnValue;

I changed scan_only to need_compute.

+ *
+ * If need_compute is true, we will evaluate the new column value in Phase 3.
+ * Currently, this is only used in ALTER COLUMN SET DATA TYPE
command, where the
+ * column’s data type is being changed to a constrained domain, and all the
+ * domain's constraints are non-volatile. In case table rewrite, we also set it
+ * to true.
*/
typedef struct NewColumnValue
{
@@ -238,6 +244,7 @@ typedef struct NewColumnValue
Expr *expr; /* expression to compute */
ExprState *exprstate; /* execution state */
bool is_generated; /* is it a GENERATED expression? */
+ bool need_compute; /* compute this new expression in Phase 3 */
} NewColumnValue;

I use domain over domain for regress tests.
I also constrained the no–table-rewrite behavior to cases where the coercion is
to a domain type and all constraints of the new domain are non-volatile.

Demo:

CREATE DOMAIN domain1 AS INT CHECK(VALUE > 1) NOT NULL;
CREATE DOMAIN domain11 AS domain1 CHECK(VALUE > 1) NOT NULL;
CREATE DOMAIN domain21 AS domain1 CHECK(VALUE > random(min=>10,
max=>10)) NOT NULL;
CREATE DOMAIN domain3 AS INT8;
CREATE TABLE t22(a INT, b INT);
INSERT INTO t22 VALUES(-2, -1);

-- no table rewrite, but fail at domain constraint check
ALTER TABLE t22 ALTER COLUMN a SET DATA TYPE domain11 USING a::domain11;
-- no table rewrite, but fail at domain constraint check
ALTER TABLE t22 ALTER COLUMN b SET DATA TYPE domain11 USING b::domain11;

-- table rewrite
ALTER TABLE t22 ALTER COLUMN a SET DATA TYPE domain21;
ALTER TABLE t22 ALTER COLUMN b SET DATA TYPE domain3;
ALTER TABLE t22 ALTER COLUMN a SET DATA TYPE domain1 USING (a+0)::domain1;

--
jian
https://www.enterprisedb.com

Attachment Content-Type Size
v3-0002-skip-table-rewrite-when-set-column-type-to-constrained-domain.patch text/x-patch 11.3 KB
v3-0001-add-function-DomainHaveVolatileConstraints.patch text/x-patch 2.7 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Chao Li 2025-12-15 07:13:52 Re: Proposal: Cascade REPLICA IDENTITY changes to leaf partitions
Previous Message Tom Lane 2025-12-15 07:01:52 Re: PRI?64 vs Visual Studio (2022)