Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 49fe1c8

Browse files
committed
Fix virtual generated column type checking for ALTER TABLE
Virtual generated columns have some special checks in CheckAttributeType(), mainly to check that domains are not used. But this check was only applied during CREATE TABLE, not during ALTER TABLE. This fixes that. Reported-by: jian he <[email protected]> Discussion: https://www.postgresql.org/message-id/CACJufxE0KHR__-h=zHXbhSNZXMMs4LYo4-dbj8H3YoStYBok1Q@mail.gmail.com
1 parent 0cb5145 commit 49fe1c8

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/backend/commands/tablecmds.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7374,7 +7374,7 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
73747374
/* make sure datatype is legal for a column */
73757375
CheckAttributeType(NameStr(attribute->attname), attribute->atttypid, attribute->attcollation,
73767376
list_make1_oid(rel->rd_rel->reltype),
7377-
0);
7377+
(attribute->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL ? CHKATYPE_IS_VIRTUAL : 0));
73787378

73797379
InsertPgAttributeTuples(attrdesc, tupdesc, myrelid, NULL, NULL);
73807380

@@ -14426,7 +14426,7 @@ ATPrepAlterColumnType(List **wqueue,
1442614426
/* make sure datatype is legal for a column */
1442714427
CheckAttributeType(colName, targettype, targetcollid,
1442814428
list_make1_oid(rel->rd_rel->reltype),
14429-
0);
14429+
(attTup->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL ? CHKATYPE_IS_VIRTUAL : 0));
1443014430

1443114431
if (attTup->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
1443214432
{

src/test/regress/expected/generated_virtual.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,12 @@ CREATE TABLE gtest24r (a int PRIMARY KEY, b gtestdomain1range GENERATED ALWAYS A
800800
ERROR: virtual generated column "b" cannot have a domain type
801801
--INSERT INTO gtest24r (a) VALUES (4); -- ok
802802
--INSERT INTO gtest24r (a) VALUES (6); -- error
803+
CREATE TABLE gtest24at (a int PRIMARY KEY);
804+
ALTER TABLE gtest24at ADD COLUMN b gtestdomain1 GENERATED ALWAYS AS (a * 2) VIRTUAL; -- error
805+
ERROR: virtual generated column "b" cannot have a domain type
806+
CREATE TABLE gtest24ata (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) VIRTUAL);
807+
ALTER TABLE gtest24ata ALTER COLUMN b TYPE gtestdomain1; -- error
808+
ERROR: virtual generated column "b" cannot have a domain type
803809
CREATE DOMAIN gtestdomainnn AS int CHECK (VALUE IS NOT NULL);
804810
CREATE TABLE gtest24nn (a int, b gtestdomainnn GENERATED ALWAYS AS (a * 2) VIRTUAL);
805811
ERROR: virtual generated column "b" cannot have a domain type

src/test/regress/sql/generated_virtual.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,11 @@ CREATE TABLE gtest24r (a int PRIMARY KEY, b gtestdomain1range GENERATED ALWAYS A
453453
--INSERT INTO gtest24r (a) VALUES (4); -- ok
454454
--INSERT INTO gtest24r (a) VALUES (6); -- error
455455

456+
CREATE TABLE gtest24at (a int PRIMARY KEY);
457+
ALTER TABLE gtest24at ADD COLUMN b gtestdomain1 GENERATED ALWAYS AS (a * 2) VIRTUAL; -- error
458+
CREATE TABLE gtest24ata (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) VIRTUAL);
459+
ALTER TABLE gtest24ata ALTER COLUMN b TYPE gtestdomain1; -- error
460+
456461
CREATE DOMAIN gtestdomainnn AS int CHECK (VALUE IS NOT NULL);
457462
CREATE TABLE gtest24nn (a int, b gtestdomainnn GENERATED ALWAYS AS (a * 2) VIRTUAL);
458463
--INSERT INTO gtest24nn (a) VALUES (4); -- ok

0 commit comments

Comments
 (0)