Re: Virtual generated columns - Mailing list pgsql-hackers

From Peter Eisentraut
Subject Re: Virtual generated columns
Date
Msg-id 721f6d40-ac9b-4103-aec8-a664f9e9dc1c@eisentraut.org
Whole thread Raw
In response to Re: Virtual generated columns  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
On 08.01.25 17:38, Tom Lane wrote:
> Peter Eisentraut <peter@eisentraut.org> writes:
>> On 03.12.24 15:15, jian he wrote:
>>> SELECT attrelid, attname, attgenerated FROM pg_attribute WHERE
>>> attgenerated IN ('v') and (attnotnull or not atthasdef);
> 
>> I don't understand what the purpose of testing attnotnull is.  That is
>> independent of attgenerated, I think.
> 
> Does it make any sense to set NOT NULL on a generated column (virtual
> or otherwise, but especially virtual)?  What is the system supposed
> to do if the expression evaluates to null?  That concern generalizes
> to any constraint really.  Even if we checked it at row storage time,
> there's no real guarantee that the expression is immutable enough
> to pass the constraint later.

The generation expression is required to be immutable.  So a table 
definition like

    a int,
    b int generated always as (a * 2) virtual,
    check (b > 0)

is not very different from

    a int,
    check (a * 2 > 0)

in terms of the constraint execution.

The current patch does not support not-null constraints, but that's 
mostly because it's not implemented yet.  Maybe that's what Jian was 
thinking about.





pgsql-hackers by date:

Previous
From: Vik Fearing
Date:
Subject: Re: Virtual generated columns
Next
From: Marcos Pegoraro
Date:
Subject: Re: Virtual generated columns