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

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

From
Peter Eisentraut
Date:
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.universality@gmail.com>
Reviewed-by: Srinath Reddy <srinath2133@gmail.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(-)