domain over virtual generated column

Started by jian heover 1 year ago2 messageshackers

Thread notes

1 note#feature#moderate#enhancement
hackorum-bot#feature#moderate#enhancement
hackorum-bot
1 day ago

@global #feature #moderate #enhancement

#feature#moderate#enhancement
Jump to latest
#1jian he
jian.universality@gmail.com

hi.

I posted $subject at [1]/messages/by-id/CACJufxHArQysbDkWFmvK+D1TPHQWWTxWN15cMuUaTYX3xhQXgg@mail.gmail.com, but that thread is already committed,
now post it in a separate thread, so CI will test it.

While at it, I found some problem when the virtual generated column domain
is with a NOT VALID check constraint.
------------------
CREATE DOMAIN d1 AS int;
CREATE TABLE t (a int, b d1 GENERATED ALWAYS AS (a * 2) VIRTUAL);
INSERT INTO t (a, b) VALUES (4, default), (3,default);
ALTER DOMAIN d1 ADD constraint cc CHECK (VALUE < 7) NOT VALID;

SELECT * FROM t ORDER BY a, b; --error
ERROR: value for domain d1 violates check constraint "cc"
ALTER DOMAIN d1 ADD CHECK (VALUE < 9); --error
ERROR: column "b" of table "t" contains values that violate the new constraint

When a domain constraint is invalidated:
Any evaluation of a virtual generated column will expand and evaluate the
generated expression, which is wrapped in a CoerceToDomain node. CoerceToDomain
itself will compile and check all invalidated domain constraints (refer to
lookup_type_cache). As a result, any SELECT statement would trigger validation
of invalid domain constraints.
Thus, NOT VALID domain constraints on virtual generated columns do not behave as
expected, so I have disallowed them.

[1]: /messages/by-id/CACJufxHArQysbDkWFmvK+D1TPHQWWTxWN15cMuUaTYX3xhQXgg@mail.gmail.com

summary of attached patch:
v1-0001
we need to compute the generation expression for the domain with constraints,
thus rename ExecComputeStoredGenerated to ExecComputeGenerated.

v1-0002
soft error variant of ExecPrepareExpr, ExecInitExpr. for soft error processing
of CoerceToDomain. we don't want error messages like "value for domain d2
violates check constraint "d2_check"" while validating existing domain data, we
want something like:
ERROR: column "b" of table "gtest24" contains values that violate the
new constraint

v1-0003 virtual generation columns over domain.

Attachments:

v1-0001-rename-ExecComputeStoredGenerated-to-ExecComputeGenerated.patchtext/x-patch; charset=US-ASCII; name=v1-0001-rename-ExecComputeStoredGenerated-to-ExecComputeGenerated.patchDownload+18-20
v1-0003-domain-over-virtual-generated-column.patchtext/x-patch; charset=US-ASCII; name=v1-0003-domain-over-virtual-generated-column.patchDownload+459-80
v1-0002-soft-error-variant-of-ExecPrepareExpr-ExecInitExpr.patchtext/x-patch; charset=US-ASCII; name=v1-0002-soft-error-variant-of-ExecPrepareExpr-ExecInitExpr.patchDownload+65-1
#2jian he
jian.universality@gmail.com
In reply to: jian he (#1)
Re: domain over virtual generated column

On Mon, Apr 28, 2025 at 10:45 AM jian he <jian.universality@gmail.com> wrote:

summary of attached patch:
v1-0001
we need to compute the generation expression for the domain with constraints,
thus rename ExecComputeStoredGenerated to ExecComputeGenerated.

v1-0002
soft error variant of ExecPrepareExpr, ExecInitExpr. for soft error processing
of CoerceToDomain. we don't want error messages like "value for domain d2
violates check constraint "d2_check"" while validating existing domain data, we
want something like:
ERROR: column "b" of table "gtest24" contains values that violate the
new constraint

v1-0003 virtual generation columns over domain.

hi.

new patches attached.
mainly code refactoring, also try to reduce some unnecessary regress tests.

Attachments:

t51503_2
v2-0001-rename-ExecComputeStoredGenerated-to-ExecComputeGenerated.patchtext/x-patch; charset=US-ASCII; name=v2-0001-rename-ExecComputeStoredGenerated-to-ExecComputeGenerated.patchDownload+18-20
v2-0003-domain-over-virtual-generated-column.patchtext/x-patch; charset=US-ASCII; name=v2-0003-domain-over-virtual-generated-column.patchDownload+459-67
v2-0002-soft-error-variant-of-ExecPrepareExpr-ExecInitExpr.patchtext/x-patch; charset=US-ASCII; name=v2-0002-soft-error-variant-of-ExecPrepareExpr-ExecInitExpr.patchDownload+65-1