Re: support virtual generated column not null constraint

From: jian he <jian(dot)universality(at)gmail(dot)com>
To: Xuneng Zhou <xunengzhou(at)gmail(dot)com>
Cc: Navneet Kumar <thanit3111(at)gmail(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: support virtual generated column not null constraint
Date: 2025-03-13 12:22:11
Message-ID: CACJufxHT4R1oABzeQuvjb6DHrig7k-QSr6LEe53Q_nLi9pfanA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

hi.

new patch attached.

0001 for virtual generated columns not null.
minor change to fix the compiler warning.
0002-0004 is for domain over virtual generated columns.

0002: we need to compute the generation expression for the domain with
constraints,
thus rename ExecComputeStoredGenerated to ExecComputeGenerated.

0003. preparatory patch for 0004.
soft error variant of ExecPrepareExpr, ExecInitExpr.
for soft error processing of CoerceToDomain. see below description.

0004. no table rewrite for adding virtual generation column over
domain with constraints.
(syntax: ALTER TABLE xx ADD COLUMN X domain_type GENERATED ALWAYS AS
(expression) VIRTUAL
in phase3, ATRewriteTable: we already initialized AlteredTableInfo->newvals via
``ex->exprstate = ExecInitExpr((Expr *) ex->expr, NULL);``
we can easily evaluate it via ExecCheck. if fail, then error out.

--------------
reason for the 0003 patch:

ALTER DOMAIN ADD CONSTRAINT.
since the virtual generated column has no actual storage.
so, in validateDomainCheckConstraint we cannot use
```
d = slot_getattr(slot, attnum, &isNull);
econtext->domainValue_datum = d;
econtext->domainValue_isNull = isNull;
conResult = ExecEvalExprSwitchContext(exprstate,
econtext,
&isNull);

```
to check whether existing generation expression satisfy the newly
added domain constraint or not.

we also need to evaluate error softly,
because
ALTER DOMAIN ADD CONSTRAINT need check exists table domain value satisfy
the newly constraint or not.
if we not do soft error evaluation, the error message would be like:
``value for domain gtestdomain1 violates check constraint "gtestdomain1_check"``
but we want error message like:
ERROR: column "b" of table "gtest24" contains values that violate the
new constraint
--------------

Attachment Content-Type Size
v4-0002-rename-ExecComputeStoredGenerated-to-ExecComputeG.patch text/x-patch 5.2 KB
v4-0004-domain-over-virtual-generated-column.patch text/x-patch 31.0 KB
v4-0003-soft-error-variant-of-ExecPrepareExpr-ExecInitExp.patch text/x-patch 3.9 KB
v4-0001-not-null-for-virtual-generated-column.patch text/x-patch 31.1 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Gustafsson 2025-03-13 12:32:20 Re: Changing the state of data checksums in a running cluster
Previous Message Andrew Dunstan 2025-03-13 12:19:31 Re: Random pg_upgrade 004_subscription test failure on drongo