Re: Virtual generated columns

Peter Eisentraut <peter@eisentraut.org>

From: Peter Eisentraut <peter@eisentraut.org>
To: Corey Huinker <corey.huinker@gmail.com>
Cc: pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2024-05-22T17:25:59Z
Lists: pgsql-hackers

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Expand virtual generated columns for ALTER COLUMN TYPE

  2. Eliminate code duplication in replace_rte_variables callbacks

  3. Expand virtual generated columns in the planner

  4. Virtual generated columns

  5. Additional tests for stored generated columns

  6. Improve generated_stored test

  7. Fix handling of CREATE DOMAIN with GENERATED constraint syntax

  8. Add pg_constraint rows for not-null constraints

  9. Put generated_stored test objects in a schema

  10. Rename regress test generated to generated_stored

  11. Small code simplification

  12. Remove useless code

  13. Remove useless initializations

  14. doc: Clarify that pg_attrdef also stores generation expressions

  15. Clean out column-level pg_init_privs entries when dropping tables.

  16. Re-implement the ereport() macro using __VA_ARGS__.

On 29.04.24 20:54, Corey Huinker wrote:
>       -- generation expression must be immutable
>     -CREATE TABLE gtest_err_4 (a int PRIMARY KEY, b double precision
>     GENERATED ALWAYS AS (random()) STORED);
>     +CREATE TABLE gtest_err_4 (a int PRIMARY KEY, b double precision
>     GENERATED ALWAYS AS (random()) VIRTUAL);
> 
> Does a VIRTUAL generated column have to be immutable? I can see where 
> the STORED one has to be, but consider the following:
> 
>     CREATE TABLE foo (
>     created_at timestamptz DEFAULT CURRENT_TIMESTAMP,
>     row_age interval GENERATED ALWAYS AS CURRENT_TIMESTAMP - created_at
>     );

I have been hesitant about this, but I'm now leaning toward that we 
could allow this.

>       -- can't have generated column that is a child of normal column
>       CREATE TABLE gtest_normal (a int, b int);
>     -CREATE TABLE gtest_normal_child (a int, b int GENERATED ALWAYS AS
>     (a * 2) STORED) INHERITS (gtest_normal);  -- error
>     +CREATE TABLE gtest_normal_child (a int, b int GENERATED ALWAYS AS
>     (a * 2) VIRTUAL) INHERITS (gtest_normal);  -- error
> 
> This is the barrier to the partitioning reorganization scheme I 
> described above. Is there any hard rule why a child table couldn't have 
> a generated column matching the parent's regular column? I can see where 
> it might prevent indexing that column on the parent table, but is there 
> some other dealbreaker or is this just a "it doesn't work yet" situation?

We had a quite a difficult time getting the inheritance business of 
stored generated columns working correctly.  I'm sticking to the 
well-trodden path here.  We can possibly expand this if someone wants to 
work out the details.

> One last thing to keep in mind is that there are two special case 
> expressions in the spec:
> 
>     GENERATED ALWAYS AS ROW START
>     GENERATED ALWAYS AS ROW END
> 
> and we'll need to be able to fit those into the catalog. I'll start 
> another thread for that unless you prefer I keep it here.

I think this is a separate feature.