Re: Virtual generated columns

jian he <jian.universality@gmail.com>

From: jian he <jian.universality@gmail.com>
To: Peter Eisentraut <peter@eisentraut.org>
Cc: pgsql-hackers <pgsql-hackers@postgresql.org>, Dean Rasheed <dean.a.rasheed@gmail.com>
Date: 2024-12-11T06:49:52Z
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__.

Attachments

On Fri, Nov 29, 2024 at 6:01 PM Peter Eisentraut <peter@eisentraut.org> wrote:
> The purpose of check_modified_virtual_generated() for trigger functions
> written in C.  The prevent someone from inserting real values into the
> trigger tuples, because they would then be processed by the rest of the
> system, which would be incorrect.
>
> Higher-level languages such as plpgsql should handle that themselves, by
> preventing setting generated columns in trigger functions.  The presence
> of check_modified_virtual_generated() is still a backstop for those, but
> shouldn't really be necessary.

please check the attached patch.
* remove check_modified_virtual_generated.
* using heap_modify_tuple_by_cols in ExecBRInsertTriggers, ExecBRUpdateTriggers
to overwrite virtual generated columns value to null.

and it's not complicated.
so that trigger behavior for stored and virtual will be more aligned

-------------------------------
I think contrib module: spi-autoin can be used to test triggers (c
language) before rows behavior
for the generated columns (stored, virtual).

for example (copied from contrib/spi/autoinc.example)

DROP SEQUENCE next_id;
CREATE SEQUENCE next_id START -2 MINVALUE -2;
CREATE TABLE id_gen_stored (id int4 GENERATED ALWAYS AS (2) stored,idesc text);

CREATE TRIGGER ids_nextids
BEFORE INSERT OR UPDATE ON id_stored
FOR EACH ROW
EXECUTE PROCEDURE autoinc (id, next_id);

INSERT INTO id_gen_stored VALUES (default, 'hello');
select * from id_gen_stored;
UPDATE id_gen_stored SET id = default, idesc = 'world' ;
select * from id_gen_stored;

then we can validate this sentence in trigger.sgml:
"""
Changes to the value of a generated column in a
<literal>BEFORE</literal> trigger are ignored and will be overwritten.
"""
for c language triggers.