Re: bug in stored generated column over domain with constraints.
jian he <jian.universality@gmail.com>
From: jian he <jian.universality@gmail.com>
To: PostgreSQL-development <pgsql-hackers@postgresql.org>
Date: 2025-04-10T08:53:12Z
Lists: pgsql-hackers
Attachments
- v3-0001-fix-INSERT-generated-column-over-domain-with-cons.patch (text/x-patch) patch v3-0001
hi.
new patch attached.
rewriteTargetListIU, expand_insert_targetlist these two places can
make a null Const TargetEntry for the generated column in an INSERT
operation.
but since this problem only occurs in INSERT, so i placed the logic
within expand_insert_targetlist would be appropriate?
The following are excerpts of the commit message.
--------------------------------
create domain d3 as int check (value is not null);
create table t0(b int, a d3 GENERATED ALWAYS as (b + 11) stored);
insert into t0 values (1, default);
ERROR: value for domain d3 violates check constraint "d3_check"
explain(costs off, verbose) insert into t0 values (1, default);
QUERY PLAN
---------------------------------------
Insert on public.t0
-> Result
Output: 1, NULL::integer
For INSERT operation, for Query->targetList, we should not make a
generated column
over domain with constraint to a CoerceToDomain node, instead, we make it as a
simple null Const over domain's base type.
When a column is a generated column in an INSERT, expand_insert_targetlist
should unconditionally generate a null Const to be inserted. If we are not
doing this way, we might end up wrapping the null Const in a CoerceToDomain
node, which may trigger runtime error earlier if the domain has a NOT NULL
constraint. That's not fine, as generated columns are already handled in
ExecComputeStoredGenerated.
--------------------------------
Commits
-
Fix failure for generated column with a not-null domain constraint.
- f04e0faa3702 16.9 landed
- 97d671672393 15.13 landed
- 7c8728494077 18.0 landed
- 4604928edd72 14.18 landed
- 3c39c000c859 17.5 landed
-
Handle default NULL insertion a little better.
- 0da39aa7667b 18.0 cited