pgsql: Fix ALTER TABLE ADD VIRTUAL GENERATED COLUMN when table rewrite

From: Peter Eisentraut <peter(at)eisentraut(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix ALTER TABLE ADD VIRTUAL GENERATED COLUMN when table rewrite
Date: 2025-03-04 08:27:45
Message-ID: E1tpNcv-000iOA-1M@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix ALTER TABLE ADD VIRTUAL GENERATED COLUMN when table rewrite

demo:
CREATE TABLE gtest20a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) VIRTUAL);
ALTER TABLE gtest20a ADD COLUMN c float8 DEFAULT RANDOM() CHECK (b < 60);
ERROR: no generation expression found for column number 2 of table "pg_temp_17306"

In ATRewriteTable, the variable OIDNewHeap (if valid) corresponding
pg_attrdef default expression entry was not populated. So OIDNewHeap
cannot be used to call expand_generated_columns_in_expr or
build_generation_expression. Therefore in ATRewriteTable, we can only
use the existing relation to expand the generated expression.

Author: jian he <jian(dot)universality(at)gmail(dot)com>
Reviewed-by: Srinath Reddy <srinath2133(at)gmail(dot)com>
Discussion: https://www.postgresql.org/message-id/flat/CACJufxEJ%3DFoajabWXjszo_yrQeKSxdZ87KJqBW373rSbajKGAA%40mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/f011acdd61fc296ec8822ca4edc18e32b6bd2b56

Modified Files
--------------
src/backend/commands/tablecmds.c | 2 +-
src/test/regress/expected/generated_stored.out | 4 ++++
src/test/regress/expected/generated_virtual.out | 4 ++++
src/test/regress/sql/generated_stored.sql | 3 +++
src/test/regress/sql/generated_virtual.sql | 3 +++
5 files changed, 15 insertions(+), 1 deletion(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Peter Eisentraut 2025-03-04 08:50:31 pgsql: Fix accidental use of = instead of ==
Previous Message Richard Guo 2025-03-04 07:18:09 pgsql: Avoid NullTest deduction for clone clauses