Re: bug in stored generated column over domain with constraints. - Mailing list pgsql-hackers

From jian he
Subject Re: bug in stored generated column over domain with constraints.
Date
Msg-id CACJufxF0xJ1D6ZBvqopMA=vKRKbFrtUbaMJ8bm0b8d9KLDC4Ag@mail.gmail.com
Whole thread Raw
In response to Re: bug in stored generated column over domain with constraints.  (jian he <jian.universality@gmail.com>)
Responses Re: bug in stored generated column over domain with constraints.
List pgsql-hackers
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.
--------------------------------

Attachment

pgsql-hackers by date:

Previous
From: Tender Wang
Date:
Subject: Re: Consistently use macro HeapTupleIsValid to check the validity of tuples in tablecmds.c
Next
From: jian he
Date:
Subject: wrong comments in rewriteTargetListIU