Re: Virtual generated columns

From: Peter Eisentraut <peter(at)eisentraut(dot)org>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: jian he <jian(dot)universality(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
Subject: Re: Virtual generated columns
Date: 2025-01-08 19:28:33
Message-ID: 721f6d40-ac9b-4103-aec8-a664f9e9dc1c@eisentraut.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 08.01.25 17:38, Tom Lane wrote:
> Peter Eisentraut <peter(at)eisentraut(dot)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.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Marcos Pegoraro 2025-01-08 19:29:51 Re: Virtual generated columns
Previous Message Andres Freund 2025-01-08 19:26:15 Re: Reorder shutdown sequence, to flush pgstats later