v7-0001-Virtual-generated-columns-no-table_rewrite.no-cfbot

application/octet-stream

Filename: v7-0001-Virtual-generated-columns-no-table_rewrite.no-cfbot
Type: application/octet-stream
Part: 0
Message: Re: Virtual generated columns
From 7a79374caa8196d6bba908c7a83ebe4ef7e66f53 Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Mon, 16 Sep 2024 16:58:55 +0800
Subject: [PATCH v7 1/1] Virtual generated columns no table_rewrite

minor change RelationBuildTupleDesc.
if constr->has_generated_virtual is true
assign constr to relation->rd_att->constr.

make virtual generated columns no table_rewrite.
add tests on src/test/regress/sql/fast_default.sql
---
 src/backend/commands/tablecmds.c           |  2 +-
 src/backend/utils/cache/relcache.c         |  1 +
 src/test/regress/expected/fast_default.out | 12 ++++++++++++
 src/test/regress/sql/fast_default.sql      | 10 ++++++++++
 4 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 0914e0566b..b01e431355 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -7181,7 +7181,7 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
 		 * DEFAULT value outside of the heap.  This may be disabled inside
 		 * AddRelationNewConstraints if the optimization cannot be applied.
 		 */
-		rawEnt->missingMode = (!colDef->generated);
+		rawEnt->missingMode = (colDef->generated != ATTRIBUTE_GENERATED_STORED);
 
 		rawEnt->generated = colDef->generated;
 
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index dca8a5a4a2..c8a711cb7e 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -687,6 +687,7 @@ RelationBuildTupleDesc(Relation relation)
 	 */
 	if (constr->has_not_null ||
 		constr->has_generated_stored ||
+		constr->has_generated_virtual ||
 		ndef > 0 ||
 		attrmiss ||
 		relation->rd_rel->relchecks > 0)
diff --git a/src/test/regress/expected/fast_default.out b/src/test/regress/expected/fast_default.out
index 59365dad96..0c77fdada8 100644
--- a/src/test/regress/expected/fast_default.out
+++ b/src/test/regress/expected/fast_default.out
@@ -58,6 +58,18 @@ ALTER TABLE has_volatile ADD col2 int DEFAULT 1;
 ALTER TABLE has_volatile ADD col3 timestamptz DEFAULT current_timestamp;
 ALTER TABLE has_volatile ADD col4 int DEFAULT (random() * 10000)::int;
 NOTICE:  rewriting table has_volatile for reason 2
+--virtual generated column don't need rewrite.
+ALTER TABLE has_volatile ADD col5 int GENERATED ALWAYS AS (tableoid::int + col2) VIRTUAL;
+ALTER TABLE has_volatile ALTER COLUMN col5 TYPE float8;
+ALTER TABLE has_volatile ALTER COLUMN col5 TYPE numeric;
+ALTER TABLE has_volatile ALTER COLUMN col5 TYPE numeric;
+--here, we do need rewrite
+ALTER TABLE has_volatile ALTER COLUMN col1 SET DATA TYPE float8,
+  ADD COLUMN col6 float8 GENERATED ALWAYS AS (col1 * 4) VIRTUAL;
+NOTICE:  rewriting table has_volatile for reason 4
+--stored generated column need rewrite.
+ALTER TABLE has_volatile ADD col7 int GENERATED ALWAYS AS (55) stored;
+NOTICE:  rewriting table has_volatile for reason 2
 -- Test a large sample of different datatypes
 CREATE TABLE T(pk INT NOT NULL PRIMARY KEY, c_int INT DEFAULT 1);
 SELECT set('t');
diff --git a/src/test/regress/sql/fast_default.sql b/src/test/regress/sql/fast_default.sql
index dc9df78a35..ed860c1e45 100644
--- a/src/test/regress/sql/fast_default.sql
+++ b/src/test/regress/sql/fast_default.sql
@@ -65,8 +65,18 @@ ALTER TABLE has_volatile ADD col1 int;
 ALTER TABLE has_volatile ADD col2 int DEFAULT 1;
 ALTER TABLE has_volatile ADD col3 timestamptz DEFAULT current_timestamp;
 ALTER TABLE has_volatile ADD col4 int DEFAULT (random() * 10000)::int;
+--virtual generated column don't need rewrite.
+ALTER TABLE has_volatile ADD col5 int GENERATED ALWAYS AS (tableoid::int + col2) VIRTUAL;
+ALTER TABLE has_volatile ALTER COLUMN col5 TYPE float8;
+ALTER TABLE has_volatile ALTER COLUMN col5 TYPE numeric;
+ALTER TABLE has_volatile ALTER COLUMN col5 TYPE numeric;
 
+--here, we do need rewrite
+ALTER TABLE has_volatile ALTER COLUMN col1 SET DATA TYPE float8,
+  ADD COLUMN col6 float8 GENERATED ALWAYS AS (col1 * 4) VIRTUAL;
 
+--stored generated column need rewrite.
+ALTER TABLE has_volatile ADD col7 int GENERATED ALWAYS AS (55) stored;
 
 -- Test a large sample of different datatypes
 CREATE TABLE T(pk INT NOT NULL PRIMARY KEY, c_int INT DEFAULT 1);

base-commit: 4632e5cf4bc5c496f41dfc6a89533e7afa7262dd
prerequisite-patch-id: 3d7cd74ab7d10140274b25350d8e27e73db99210
-- 
2.34.1