From fa48a0a9824558d3bf8e5f5a8d13971189dd8a11 Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Mon, 16 Nov 2026 08:04:02 -0500 Subject: [PATCH v6 REL_18] Require child tables to use the parent's virtual generation expression MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Otherwise a NOT NULL or CHECK constraint can be wrong for rows read through the parent. Suggested-by: Álvaro Herrera Discussion: https://postgr.es/m/E1xFKs5-0000000116b-2Cge@gemulon.postgresql.org --- doc/src/sgml/ddl.sgml | 18 +- doc/src/sgml/ref/alter_table.sgml | 5 + src/backend/commands/tablecmds.c | 249 +++++++++++++++++- .../regress/expected/generated_virtual.out | 188 +++++++------ src/test/regress/sql/generated_virtual.sql | 73 ++++- 5 files changed, 425 insertions(+), 108 deletions(-) diff --git a/doc/src/sgml/ddl.sgml b/doc/src/sgml/ddl.sgml index ddd7096f6b2..ad416a2bdf7 100644 --- a/doc/src/sgml/ddl.sgml +++ b/doc/src/sgml/ddl.sgml @@ -451,8 +451,10 @@ CREATE TABLE people ( If a parent column is a generated column, its child column must also - be a generated column of the same kind (stored or virtual); however, - the child column can have a different generation expression. + be a generated column of the same kind (stored or virtual). + A virtual generated child column must have the same generation + expression as the parent column. A stored generated child column + can have a different generation expression. @@ -478,8 +480,9 @@ CREATE TABLE people ( ... INHERITS, then its GENERATED clause will automatically be copied from the parent. ALTER TABLE ... INHERIT will insist that parent and child columns - already match as to generation status, but it will not require their - generation expressions to match. + already match as to generation status, but it will only require + their generation expressions to match for virtual generated + columns. @@ -490,8 +493,8 @@ CREATE TABLE people ( its GENERATED clause will automatically be copied from the parent. ALTER TABLE ... ATTACH PARTITION will insist that parent and child columns already match as to - generation status, but it will not require their generation - expressions to match. + generation status, but it will only require their generation + expressions to match for virtual generated columns. @@ -499,7 +502,8 @@ CREATE TABLE people ( In case of multiple inheritance, if one parent column is a generated column, then all parent columns must be generated columns. If they do not all have the same generation expression, then the desired - expression for the child must be specified explicitly. + expression for the child must be specified explicitly. For virtual + generated columns, they must all have the same generation expression. diff --git a/doc/src/sgml/ref/alter_table.sgml b/doc/src/sgml/ref/alter_table.sgml index 31f2d91418b..01048b1c642 100644 --- a/doc/src/sgml/ref/alter_table.sgml +++ b/doc/src/sgml/ref/alter_table.sgml @@ -272,6 +272,11 @@ WITH ( MODULUS numeric_literal, REM This form replaces the expression of a generated column. Existing data in a stored generated column is rewritten and all the future changes will apply the new generation expression. + The expression of a virtual generated column cannot be changed for an + inherited column, or with ONLY for a table that has + child tables. It has to be changed for the whole inheritance tree, + from the top. If a child table inherits the column from more than one + parent, the expression cannot be changed while it does. diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5059909833e..c37705b1b6b 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -378,6 +378,7 @@ static List *MergeCheckConstraint(List *constraints, const char *name, Node *exp static void MergeChildAttribute(List *inh_columns, int exist_attno, int newcol_attno, const ColumnDef *newdef); static ColumnDef *MergeInheritedAttribute(List *inh_columns, int exist_attno, const ColumnDef *newdef); static void MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel, bool ispartition); +static void CheckVirtualGeneratedExprs(Relation childrel, Relation parentrel); static void MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel); static void StoreCatalogInheritance(Oid relationId, List *supers, bool child_is_partition); @@ -521,7 +522,11 @@ static ObjectAddress ATExecSetIdentity(Relation rel, const char *colName, static ObjectAddress ATExecDropIdentity(Relation rel, const char *colName, bool missing_ok, LOCKMODE lockmode, bool recurse, bool recursing); static ObjectAddress ATExecSetExpression(AlteredTableInfo *tab, Relation rel, const char *colName, - Node *newExpr, LOCKMODE lockmode); + Node *newExpr, bool check_tree, LOCKMODE lockmode); +static void ATPrepSetExpression(Relation rel, AlterTableCmd *cmd, bool recurse, + bool recursing, LOCKMODE lockmode); +static void CheckSetExpressionTree(Relation rel, const char *colName, + LOCKMODE lockmode); static void ATPrepDropExpression(Relation rel, AlterTableCmd *cmd, bool recurse, bool recursing, LOCKMODE lockmode); static ObjectAddress ATExecDropExpression(Relation rel, const char *colName, bool missing_ok, LOCKMODE lockmode); static ObjectAddress ATExecSetStatistics(Relation rel, const char *colName, int16 colNum, @@ -1118,6 +1123,15 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, */ CommandCounterIncrement(); + /* Virtual generated columns must use the parents' expressions */ + foreach_oid(parentId, inheritOids) + { + Relation parent = table_open(parentId, NoLock); + + CheckVirtualGeneratedExprs(rel, parent); + table_close(parent, NoLock); + } + /* Process and store partition bound, if any. */ if (stmt->partbound) { @@ -3058,8 +3072,8 @@ MergeAttributes(List *columns, const List *supers, char relpersistence, * Check for conflicts related to generated columns. * * Same rules as above: generated-ness has to match the - * parent, but the contents of the generation expression - * can be different. + * parent. For virtual generated columns, + * CheckVirtualGeneratedExprs checks the expression later. */ if (coldef->generated) { @@ -3138,7 +3152,8 @@ MergeAttributes(List *columns, const List *supers, char relpersistence, (errcode(ERRCODE_INVALID_COLUMN_DEFINITION), errmsg("column \"%s\" inherits conflicting generation expressions", def->colname), - errhint("To resolve the conflict, specify a generation expression explicitly."))); + def->generated == ATTRIBUTE_GENERATED_STORED ? + errhint("To resolve the conflict, specify a generation expression explicitly.") : 0)); else ereport(ERROR, (errcode(ERRCODE_INVALID_COLUMN_DEFINITION), @@ -3346,10 +3361,11 @@ MergeChildAttribute(List *inh_columns, int exist_attno, int newcol_attno, const * * If the parent column is generated, the child column will be made a * generated column if it isn't already. If it is a generated column, - * we'll take its generation expression in preference to the parent's. We - * must check that the child column doesn't specify a default value or - * identity, which matches the rules for a single column in - * parse_utilcmd.c. + * we'll take its generation expression in preference to the parent's, + * though for a virtual generated column CheckVirtualGeneratedExprs later + * requires the two to match. We must check that the child column doesn't + * specify a default value or identity, which matches the rules for a + * single column in parse_utilcmd.c. * * Conversely, if the parent column is not generated, the child column * can't be either. (We used to allow that, but it results in being able @@ -5036,6 +5052,7 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd, case AT_SetExpression: /* ALTER COLUMN SET EXPRESSION */ ATSimplePermissions(cmd->subtype, rel, ATT_TABLE | ATT_PARTITIONED_TABLE | ATT_FOREIGN_TABLE); + ATPrepSetExpression(rel, cmd, recurse, recursing, lockmode); ATSimpleRecursion(wqueue, rel, cmd, recurse, lockmode, context); pass = AT_PASS_SET_EXPRESSION; break; @@ -5437,7 +5454,9 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, cmd->recurse, false, lockmode); break; case AT_SetExpression: - address = ATExecSetExpression(tab, rel, cmd->name, cmd->def, lockmode); + /* ATPrepSetExpression sets recurse for the top-level table */ + address = ATExecSetExpression(tab, rel, cmd->name, cmd->def, + cmd->recurse, lockmode); break; case AT_DropExpression: address = ATExecDropExpression(rel, cmd->name, cmd->missing_ok, lockmode); @@ -7310,6 +7329,33 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel, get_collation_name(ccollid), get_collation_name(childatt->attcollation)))); + /* + * Child column must be virtual generated if and only if parent + * column is. Our caller checks the expression. + */ + if ((colDef->generated == ATTRIBUTE_GENERATED_VIRTUAL) != + (childatt->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)) + { + if (!childatt->attgenerated) + ereport(ERROR, + (errcode(ERRCODE_DATATYPE_MISMATCH), + errmsg("column \"%s\" in child table must be a generated column", + colDef->colname))); + else if (!colDef->generated) + ereport(ERROR, + (errcode(ERRCODE_DATATYPE_MISMATCH), + errmsg("column \"%s\" in child table must not be a generated column", + colDef->colname))); + else + ereport(ERROR, + (errcode(ERRCODE_DATATYPE_MISMATCH), + errmsg("column \"%s\" inherits from generated column of different kind", + colDef->colname), + errdetail("Parent column is %s, child column is %s.", + colDef->generated == ATTRIBUTE_GENERATED_STORED ? "STORED" : "VIRTUAL", + childatt->attgenerated == ATTRIBUTE_GENERATED_STORED ? "STORED" : "VIRTUAL"))); + } + /* Bump the existing child att's inhcount */ if (pg_add_s16_overflow(childatt->attinhcount, 1, &childatt->attinhcount)) @@ -7653,6 +7699,13 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel, &childcmd, recurse, true, lockmode, cur_pass, context); + /* The child may have had the column already */ + if (colDef->generated == ATTRIBUTE_GENERATED_VIRTUAL) + { + CommandCounterIncrement(); + CheckVirtualGeneratedExprs(childrel, rel); + } + table_close(childrel, NoLock); } @@ -8625,7 +8678,7 @@ ATExecDropIdentity(Relation rel, const char *colName, bool missing_ok, LOCKMODE */ static ObjectAddress ATExecSetExpression(AlteredTableInfo *tab, Relation rel, const char *colName, - Node *newExpr, LOCKMODE lockmode) + Node *newExpr, bool check_tree, LOCKMODE lockmode) { HeapTuple tuple; Form_pg_attribute attTup; @@ -8676,6 +8729,9 @@ ATExecSetExpression(AlteredTableInfo *tab, Relation rel, const char *colName, if (attgenerated == ATTRIBUTE_GENERATED_VIRTUAL && attTup->attnotnull) tab->verify_new_notnull = true; + if (attgenerated == ATTRIBUTE_GENERATED_VIRTUAL && check_tree) + CheckSetExpressionTree(rel, colName, lockmode); + /* * We need to prevent this because a change of expression could affect a * row filter and inject expressions that are not permitted in a row @@ -8774,6 +8830,111 @@ ATExecSetExpression(AlteredTableInfo *tab, Relation rel, const char *colName, return address; } +/* + * ALTER TABLE ALTER COLUMN SET EXPRESSION + * + * A virtual generated column must have the same expression in a table and in + * all its parents and children, see CheckVirtualGeneratedExprs. So the + * expression can only be changed for a whole inheritance tree, from the top. + */ +static void +ATPrepSetExpression(Relation rel, AlterTableCmd *cmd, bool recurse, + bool recursing, LOCKMODE lockmode) +{ + HeapTuple tuple; + Form_pg_attribute attTup; + char attgenerated; + int16 attinhcount; + + /* + * Check the whole tree in phase 2, once columns added by this command + * exist. See CheckSetExpressionTree. + */ + cmd->recurse = recurse && !recursing; + + if (recursing) + return; + + tuple = SearchSysCacheAttName(RelationGetRelid(rel), cmd->name); + if (!HeapTupleIsValid(tuple)) + return; /* error will be reported later */ + attTup = (Form_pg_attribute) GETSTRUCT(tuple); + attgenerated = attTup->attgenerated; + attinhcount = attTup->attinhcount; + ReleaseSysCache(tuple); + + if (attgenerated != ATTRIBUTE_GENERATED_VIRTUAL) + return; + + /* + * A partition has only one parent, so its column comes from a single + * top-level table. With plain inheritance it may come from several. + */ + if (attinhcount > 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_TABLE_DEFINITION), + errmsg("cannot alter inherited column \"%s\"", cmd->name), + rel->rd_rel->relispartition ? + errhint("Alter the generation expression of the top-level parent table instead.") : 0)); + + /* Partitions pending detach count here, as they still have the column */ + if (!recurse) + { + if (find_inheritance_children_extended(RelationGetRelid(rel), false, + NoLock, NULL, NULL) != NIL) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("ALTER TABLE / SET EXPRESSION must be applied to child tables too"))); + } +} + +/* + * Recursion skips partitions pending detach, so they would keep the old + * expression. Also, a child with another parent outside this tree would end + * up with a different expression than that parent. + */ +static void +CheckSetExpressionTree(Relation rel, const char *colName, LOCKMODE lockmode) +{ + HeapTuple tuple; + int16 attinhcount; + List *children; + List *numparents; + ListCell *lc; + ListCell *lc2; + + children = find_all_inheritors(RelationGetRelid(rel), lockmode, &numparents); + forboth(lc, children, lc2, numparents) + { + Oid childrelid = lfirst_oid(lc); + bool detached_exist = false; + + (void) find_inheritance_children_extended(childrelid, true, NoLock, + &detached_exist, NULL); + if (detached_exist) + ereport(ERROR, + (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), + errmsg("cannot alter generation expression of column \"%s\" while a partition of \"%s\" is pending detach", + colName, get_rel_name(childrelid)), + errhint("Use ALTER TABLE ... DETACH PARTITION ... FINALIZE to complete the pending detach operation."))); + + if (childrelid == RelationGetRelid(rel)) + continue; + + tuple = SearchSysCacheAttName(childrelid, colName); + if (!HeapTupleIsValid(tuple)) + continue; /* error will be reported later */ + attinhcount = ((Form_pg_attribute) GETSTRUCT(tuple))->attinhcount; + ReleaseSysCache(tuple); + + if (attinhcount > lfirst_int(lc2)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_TABLE_DEFINITION), + errmsg("cannot alter inherited column \"%s\" of relation \"%s\"", + colName, get_rel_name(childrelid)))); + } +} + /* * ALTER TABLE ALTER COLUMN DROP EXPRESSION */ @@ -17551,7 +17712,8 @@ constraints_equivalent(HeapTuple a, HeapTuple b, TupleDesc tupleDesc) * partitioned tables it's a foot-gun. * * The data type must match exactly. If the parent column is NOT NULL then - * the child must be as well. Defaults are not compared, however. + * the child must be as well. Defaults are not compared, however, and neither + * are generation expressions, except for virtual generated columns. */ static void MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel, bool ispartition) @@ -17672,6 +17834,71 @@ MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel, bool ispart } table_close(attrrel, RowExclusiveLock); + + CheckVirtualGeneratedExprs(child_rel, parent_rel); +} + +/* + * CheckVirtualGeneratedExprs + * Check that each virtual generated column of a child table has the same + * generation expression as in the parent. + * + * Reading through the parent computes the parent's expression, while + * constraints on the child are checked with its own, so they must be the + * same. As in constraints_equivalent, we compare the deparsed expressions. + * That is what pg_dump restores, and unlike equal() it also catches an + * implicit cast in one and an explicit cast in the other, which + * ALTER COLUMN TYPE treats differently. + */ +static void +CheckVirtualGeneratedExprs(Relation childrel, Relation parentrel) +{ + TupleDesc parent_desc = RelationGetDescr(parentrel); + TupleDesc child_desc = RelationGetDescr(childrel); + List *parent_context; + List *child_context; + + if (parent_desc->constr == NULL || + !parent_desc->constr->has_generated_virtual) + return; + + parent_context = deparse_context_for(RelationGetRelationName(parentrel), + RelationGetRelid(parentrel)); + child_context = deparse_context_for(RelationGetRelationName(childrel), + RelationGetRelid(childrel)); + + for (int i = 0; i < parent_desc->natts; i++) + { + Form_pg_attribute parent_att = TupleDescAttr(parent_desc, i); + AttrNumber child_attno; + Node *parent_expr; + Node *child_expr; + + if (parent_att->attisdropped || + parent_att->attgenerated != ATTRIBUTE_GENERATED_VIRTUAL) + continue; + + child_attno = get_attnum(RelationGetRelid(childrel), + NameStr(parent_att->attname)); + if (child_attno == InvalidAttrNumber) + elog(ERROR, "cache lookup failed for attribute \"%s\" of relation %u", + NameStr(parent_att->attname), RelationGetRelid(childrel)); + + parent_expr = TupleDescGetDefault(parent_desc, i + 1); + child_expr = TupleDescGetDefault(child_desc, child_attno); + if (parent_expr == NULL || child_expr == NULL) + elog(ERROR, "missing generation expression for column \"%s\"", + NameStr(parent_att->attname)); + + if (strcmp(deparse_expression(parent_expr, parent_context, false, false), + deparse_expression(child_expr, child_context, false, false)) != 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_COLUMN_DEFINITION), + errmsg("column \"%s\" of relation \"%s\" must have the same generation expression as in parent relation \"%s\"", + NameStr(parent_att->attname), + RelationGetRelationName(childrel), + RelationGetRelationName(parentrel)))); + } } /* diff --git a/src/test/regress/expected/generated_virtual.out b/src/test/regress/expected/generated_virtual.out index 03698488907..9fb79e9f5cd 100644 --- a/src/test/regress/expected/generated_virtual.out +++ b/src/test/regress/expected/generated_virtual.out @@ -356,15 +356,18 @@ CREATE TABLE gtestx (x int, b int GENERATED ALWAYS AS (a * 22) STORED) INHERITS NOTICE: merging column "b" with inherited definition ERROR: column "b" inherits from generated column of different kind DETAIL: Parent column is VIRTUAL, child column is STORED. -CREATE TABLE gtestx (x int, b int GENERATED ALWAYS AS (a * 22) VIRTUAL) INHERITS (gtest1); -- ok, overrides parent +CREATE TABLE gtestx (x int, b int GENERATED ALWAYS AS (a * 22) VIRTUAL) INHERITS (gtest1); -- error +NOTICE: merging column "b" with inherited definition +ERROR: column "b" of relation "gtestx" must have the same generation expression as in parent relation "gtest1" +CREATE TABLE gtestx (x int, b int GENERATED ALWAYS AS (a * 2) VIRTUAL) INHERITS (gtest1); -- ok NOTICE: merging column "b" with inherited definition \d+ gtestx Table "generated_virtual_tests.gtestx" - Column | Type | Collation | Nullable | Default | Storage | Stats target | Description ---------+---------+-----------+----------+------------------------------+---------+--------------+------------- - a | integer | | not null | | plain | | - b | integer | | | generated always as (a * 22) | plain | | - x | integer | | | | plain | | + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+-----------------------------+---------+--------------+------------- + a | integer | | not null | | plain | | + b | integer | | | generated always as (a * 2) | plain | | + x | integer | | | | plain | | Not-null constraints: "gtest1_a_not_null" NOT NULL "a" (inherited) Inherits: gtest1 @@ -379,9 +382,9 @@ SELECT * FROM gtest1; (3 rows) SELECT * FROM gtestx; - a | b | x -----+-----+---- - 11 | 242 | 22 + a | b | x +----+----+---- + 11 | 22 | 22 (1 row) CREATE TABLE gtestxx_1 (a int NOT NULL, b int); @@ -406,21 +409,49 @@ CREATE TABLE gtesty (x int, b int GENERATED ALWAYS AS (x * 22) VIRTUAL); CREATE TABLE gtest1_y () INHERITS (gtest1, gtesty); -- error NOTICE: merging multiple inherited definitions of column "b" ERROR: column "b" inherits conflicting generation expressions -HINT: To resolve the conflict, specify a generation expression explicitly. -CREATE TABLE gtest1_y (b int GENERATED ALWAYS AS (x + 1) VIRTUAL) INHERITS (gtest1, gtesty); -- ok +CREATE TABLE gtest1_y (b int GENERATED ALWAYS AS (x + 1) VIRTUAL) INHERITS (gtest1, gtesty); -- error NOTICE: merging multiple inherited definitions of column "b" NOTICE: moving and merging column "b" with inherited definition DETAIL: User-specified column moved to the position of the inherited column. -\d gtest1_y - Table "generated_virtual_tests.gtest1_y" - Column | Type | Collation | Nullable | Default ---------+---------+-----------+----------+----------------------------- - a | integer | | not null | - b | integer | | | generated always as (x + 1) - x | integer | | | -Inherits: gtest1, - gtesty - +ERROR: column "b" of relation "gtest1_y" must have the same generation expression as in parent relation "gtest1" +DROP TABLE gtesty; +-- adding a column merges it into an existing child column +CREATE TABLE gtesty (a int); +CREATE TABLE gtesty_1 (a int, b int GENERATED ALWAYS AS (a * 3) VIRTUAL) INHERITS (gtesty); +NOTICE: merging column "a" with inherited definition +CREATE TABLE gtesty_2 (a int, b int) INHERITS (gtesty); +NOTICE: merging column "a" with inherited definition +ALTER TABLE gtesty ADD COLUMN b int GENERATED ALWAYS AS (a * 2) VIRTUAL; -- error +NOTICE: merging definition of column "b" for child "gtesty_1" +ERROR: column "b" of relation "gtesty_1" must have the same generation expression as in parent relation "gtesty" +DROP TABLE gtesty_1; +ALTER TABLE gtesty ADD COLUMN b int GENERATED ALWAYS AS (a * 2) VIRTUAL; -- error +ERROR: column "b" in child table must be a generated column +DROP TABLE gtesty CASCADE; +NOTICE: drop cascades to table gtesty_2 +-- a child with two parents cannot have its expression changed through one +CREATE TABLE gtesty (a int, b int GENERATED ALWAYS AS (a * 2) VIRTUAL); +CREATE TABLE gtestz (a int, b int GENERATED ALWAYS AS (a * 2) VIRTUAL); +CREATE TABLE gtestyz () INHERITS (gtesty, gtestz); +NOTICE: merging multiple inherited definitions of column "a" +NOTICE: merging multiple inherited definitions of column "b" +ALTER TABLE gtesty ALTER COLUMN b SET EXPRESSION AS (a * 3); -- error +ERROR: cannot alter inherited column "b" of relation "gtestyz" +ALTER TABLE gtestyz ALTER COLUMN b SET EXPRESSION AS (a * 3); -- error, no hint +ERROR: cannot alter inherited column "b" +DROP TABLE gtesty, gtestz CASCADE; +NOTICE: drop cascades to table gtestyz +-- also when the column is added by the same command +CREATE TABLE gtestq (a int, b int GENERATED ALWAYS AS (a * 2) VIRTUAL); +CREATE TABLE gtestp (a int); +CREATE TABLE gtestpq () INHERITS (gtestp, gtestq); +NOTICE: merging multiple inherited definitions of column "a" +ALTER TABLE gtestp ADD COLUMN b int GENERATED ALWAYS AS (a * 2) VIRTUAL, + ALTER COLUMN b SET EXPRESSION AS (a * 3); -- error +NOTICE: merging definition of column "b" for child "gtestpq" +ERROR: cannot alter inherited column "b" of relation "gtestpq" +DROP TABLE gtestp, gtestq CASCADE; +NOTICE: drop cascades to table gtestpq -- test correct handling of GENERATED column that's only in child CREATE TABLE gtestp (f1 int); CREATE TABLE gtestc (f2 int GENERATED ALWAYS AS (f1+1) VIRTUAL) INHERITS(gtestp); @@ -882,7 +913,11 @@ CREATE TABLE gtest_parent (f1 date NOT NULL, f2 bigint, f3 bigint GENERATED ALWA CREATE TABLE gtest_child PARTITION OF gtest_parent FOR VALUES FROM ('2016-07-01') TO ('2016-08-01'); -- inherits gen expr CREATE TABLE gtest_child2 PARTITION OF gtest_parent ( - f3 WITH OPTIONS GENERATED ALWAYS AS (f2 * 22) VIRTUAL -- overrides gen expr + f3 WITH OPTIONS GENERATED ALWAYS AS (f2 * 22) VIRTUAL -- error, must match parent +) FOR VALUES FROM ('2016-08-01') TO ('2016-09-01'); +ERROR: column "f3" of relation "gtest_child2" must have the same generation expression as in parent relation "gtest_parent" +CREATE TABLE gtest_child2 PARTITION OF gtest_parent ( + f3 WITH OPTIONS GENERATED ALWAYS AS (f2 * 2) VIRTUAL -- same as parent, ok ) FOR VALUES FROM ('2016-08-01') TO ('2016-09-01'); CREATE TABLE gtest_child3 PARTITION OF gtest_parent ( f3 DEFAULT 42 -- error @@ -916,6 +951,17 @@ ERROR: column "f3" inherits from generated column of different kind DETAIL: Parent column is VIRTUAL, child column is STORED. DROP TABLE gtest_child3; CREATE TABLE gtest_child3 (f1 date NOT NULL, f2 bigint, f3 bigint GENERATED ALWAYS AS (f2 * 33) VIRTUAL); +ALTER TABLE gtest_parent ATTACH PARTITION gtest_child3 FOR VALUES FROM ('2016-09-01') TO ('2016-10-01'); -- error +ERROR: column "f3" of relation "gtest_child3" must have the same generation expression as in parent relation "gtest_parent" +DROP TABLE gtest_child3; +-- an explicit cast is not the same as an implicit one +CREATE TABLE gtest_cast (a int, b numeric(10,0) GENERATED ALWAYS AS (a) VIRTUAL) PARTITION BY RANGE (a); +CREATE TABLE gtest_cast_1 (a int, b numeric(10,0) GENERATED ALWAYS AS (a::numeric(10,0)) VIRTUAL); +ALTER TABLE gtest_cast ATTACH PARTITION gtest_cast_1 FOR VALUES FROM (0) TO (10); -- error +ERROR: column "b" of relation "gtest_cast_1" must have the same generation expression as in parent relation "gtest_cast" +DROP TABLE gtest_cast, gtest_cast_1; +-- different column order, so the parent's expression has to be mapped +CREATE TABLE gtest_child3 (f3 bigint GENERATED ALWAYS AS (f2 * 2) VIRTUAL, f2 bigint, f1 date NOT NULL); ALTER TABLE gtest_parent ATTACH PARTITION gtest_child3 FOR VALUES FROM ('2016-09-01') TO ('2016-10-01'); \d gtest_child Table "generated_virtual_tests.gtest_child" @@ -927,21 +973,21 @@ ALTER TABLE gtest_parent ATTACH PARTITION gtest_child3 FOR VALUES FROM ('2016-09 Partition of: gtest_parent FOR VALUES FROM ('07-01-2016') TO ('08-01-2016') \d gtest_child2 - Table "generated_virtual_tests.gtest_child2" - Column | Type | Collation | Nullable | Default ---------+--------+-----------+----------+------------------------------- + Table "generated_virtual_tests.gtest_child2" + Column | Type | Collation | Nullable | Default +--------+--------+-----------+----------+------------------------------ f1 | date | | not null | f2 | bigint | | | - f3 | bigint | | | generated always as (f2 * 22) + f3 | bigint | | | generated always as (f2 * 2) Partition of: gtest_parent FOR VALUES FROM ('08-01-2016') TO ('09-01-2016') \d gtest_child3 - Table "generated_virtual_tests.gtest_child3" - Column | Type | Collation | Nullable | Default ---------+--------+-----------+----------+------------------------------- - f1 | date | | not null | + Table "generated_virtual_tests.gtest_child3" + Column | Type | Collation | Nullable | Default +--------+--------+-----------+----------+------------------------------ + f3 | bigint | | | generated always as (f2 * 2) f2 | bigint | | | - f3 | bigint | | | generated always as (f2 * 33) + f1 | date | | not null | Partition of: gtest_parent FOR VALUES FROM ('09-01-2016') TO ('10-01-2016') INSERT INTO gtest_parent (f1, f2) VALUES ('2016-07-15', 1); @@ -962,14 +1008,14 @@ SELECT tableoid::regclass, * FROM gtest_child ORDER BY 1, 2, 3; gtest_child | 07-15-2016 | 2 | 4 (2 rows) -SELECT tableoid::regclass, * FROM gtest_child2 ORDER BY 1, 2, 3; -- uses child's generation expression, not parent's +SELECT tableoid::regclass, * FROM gtest_child2 ORDER BY 1, 2, 3; tableoid | f1 | f2 | f3 --------------+------------+----+---- - gtest_child2 | 08-15-2016 | 3 | 66 + gtest_child2 | 08-15-2016 | 3 | 6 (1 row) SELECT tableoid::regclass, * FROM gtest_child3 ORDER BY 1, 2, 3; - tableoid | f1 | f2 | f3 + tableoid | f3 | f2 | f1 ----------+----+----+---- (0 rows) @@ -982,46 +1028,14 @@ SELECT tableoid::regclass, * FROM gtest_parent ORDER BY 1, 2, 3; gtest_child3 | 09-13-2016 | 1 | 2 (3 rows) --- alter only parent's and one child's generation expression -ALTER TABLE ONLY gtest_parent ALTER COLUMN f3 SET EXPRESSION AS (f2 * 4); -ALTER TABLE gtest_child ALTER COLUMN f3 SET EXPRESSION AS (f2 * 10); -\d gtest_parent - Partitioned table "generated_virtual_tests.gtest_parent" - Column | Type | Collation | Nullable | Default ---------+--------+-----------+----------+------------------------------ - f1 | date | | not null | - f2 | bigint | | | - f3 | bigint | | | generated always as (f2 * 4) -Partition key: RANGE (f1) -Number of partitions: 3 (Use \d+ to list them.) - -\d gtest_child - Table "generated_virtual_tests.gtest_child" - Column | Type | Collation | Nullable | Default ---------+--------+-----------+----------+------------------------------- - f1 | date | | not null | - f2 | bigint | | | - f3 | bigint | | | generated always as (f2 * 10) -Partition of: gtest_parent FOR VALUES FROM ('07-01-2016') TO ('08-01-2016') - -\d gtest_child2 - Table "generated_virtual_tests.gtest_child2" - Column | Type | Collation | Nullable | Default ---------+--------+-----------+----------+------------------------------- - f1 | date | | not null | - f2 | bigint | | | - f3 | bigint | | | generated always as (f2 * 22) -Partition of: gtest_parent FOR VALUES FROM ('08-01-2016') TO ('09-01-2016') - -\d gtest_child3 - Table "generated_virtual_tests.gtest_child3" - Column | Type | Collation | Nullable | Default ---------+--------+-----------+----------+------------------------------- - f1 | date | | not null | - f2 | bigint | | | - f3 | bigint | | | generated always as (f2 * 33) -Partition of: gtest_parent FOR VALUES FROM ('09-01-2016') TO ('10-01-2016') - +-- cannot alter only parent's or one partition's generation expression +ALTER TABLE ONLY gtest_parent ALTER COLUMN f3 SET EXPRESSION AS (f2 * 4); -- error +ERROR: ALTER TABLE / SET EXPRESSION must be applied to child tables too +ALTER TABLE gtest_child ALTER COLUMN f3 SET EXPRESSION AS (f2 * 10); -- error +ERROR: cannot alter inherited column "f3" +HINT: Alter the generation expression of the top-level parent table instead. +-- alter generation expression of parent and all its children altogether +ALTER TABLE gtest_parent ALTER COLUMN f3 SET EXPRESSION AS (f2 * 4); SELECT tableoid::regclass, * FROM gtest_parent ORDER BY 1, 2, 3; tableoid | f1 | f2 | f3 --------------+------------+----+---- @@ -1030,7 +1044,10 @@ SELECT tableoid::regclass, * FROM gtest_parent ORDER BY 1, 2, 3; gtest_child3 | 09-13-2016 | 1 | 4 (3 rows) --- alter generation expression of parent and all its children altogether +ALTER TABLE gtest_parent ADD CONSTRAINT cc1 CHECK (f3 < 12); -- error +ERROR: check constraint "cc1" of relation "gtest_child2" is violated by some row +ALTER TABLE gtest_parent ADD CONSTRAINT cc CHECK (f3 < 13); -- ok +ALTER TABLE gtest_parent DROP CONSTRAINT cc; ALTER TABLE gtest_parent ALTER COLUMN f3 SET EXPRESSION AS (f2 * 2); \d gtest_parent Partitioned table "generated_virtual_tests.gtest_parent" @@ -1064,9 +1081,9 @@ Partition of: gtest_parent FOR VALUES FROM ('08-01-2016') TO ('09-01-2016') Table "generated_virtual_tests.gtest_child3" Column | Type | Collation | Nullable | Default --------+--------+-----------+----------+------------------------------ - f1 | date | | not null | - f2 | bigint | | | f3 | bigint | | | generated always as (f2 * 2) + f2 | bigint | | | + f1 | date | | not null | Partition of: gtest_parent FOR VALUES FROM ('09-01-2016') TO ('10-01-2016') SELECT tableoid::regclass, * FROM gtest_parent ORDER BY 1, 2, 3; @@ -1812,3 +1829,20 @@ select * from gtest_cursor order by id; commit; drop table gtest_cursor; +-- a partition cannot have a different generation expression, which would let +-- the partitioned table's NOT NULL virtual column read as NULL +CREATE TABLE gtestnn_parent2 (a int NOT NULL, b int GENERATED ALWAYS AS (nullif(a, 0)) VIRTUAL NOT NULL) PARTITION BY LIST (a); +CREATE TABLE gtestnn_child2 PARTITION OF gtestnn_parent2 (b GENERATED ALWAYS AS (a + 1) VIRTUAL) FOR VALUES IN (0); -- error +ERROR: column "b" of relation "gtestnn_child2" must have the same generation expression as in parent relation "gtestnn_parent2" +CREATE TABLE gtestnn_child2 PARTITION OF gtestnn_parent2 FOR VALUES IN (0, 1); +INSERT INTO gtestnn_parent2 VALUES (0); -- error +ERROR: null value in column "b" of relation "gtestnn_child2" violates not-null constraint +DETAIL: Failing row contains (0, virtual). +INSERT INTO gtestnn_parent2 VALUES (1); +SELECT a, b FROM gtestnn_parent2; + a | b +---+--- + 1 | 1 +(1 row) + +DROP TABLE gtestnn_parent2; diff --git a/src/test/regress/sql/generated_virtual.sql b/src/test/regress/sql/generated_virtual.sql index b1b92b2ced5..489f42ac255 100644 --- a/src/test/regress/sql/generated_virtual.sql +++ b/src/test/regress/sql/generated_virtual.sql @@ -153,7 +153,8 @@ DROP TABLE gtest_normal, gtest_normal_child; CREATE TABLE gtestx (x int, b int DEFAULT 10) INHERITS (gtest1); -- error CREATE TABLE gtestx (x int, b int GENERATED ALWAYS AS IDENTITY) INHERITS (gtest1); -- error CREATE TABLE gtestx (x int, b int GENERATED ALWAYS AS (a * 22) STORED) INHERITS (gtest1); -- error -CREATE TABLE gtestx (x int, b int GENERATED ALWAYS AS (a * 22) VIRTUAL) INHERITS (gtest1); -- ok, overrides parent +CREATE TABLE gtestx (x int, b int GENERATED ALWAYS AS (a * 22) VIRTUAL) INHERITS (gtest1); -- error +CREATE TABLE gtestx (x int, b int GENERATED ALWAYS AS (a * 2) VIRTUAL) INHERITS (gtest1); -- ok \d+ gtestx INSERT INTO gtestx (a, x) VALUES (11, 22); SELECT * FROM gtest1; @@ -177,8 +178,32 @@ DROP TABLE gtesty; CREATE TABLE gtesty (x int, b int GENERATED ALWAYS AS (x * 22) VIRTUAL); CREATE TABLE gtest1_y () INHERITS (gtest1, gtesty); -- error -CREATE TABLE gtest1_y (b int GENERATED ALWAYS AS (x + 1) VIRTUAL) INHERITS (gtest1, gtesty); -- ok -\d gtest1_y +CREATE TABLE gtest1_y (b int GENERATED ALWAYS AS (x + 1) VIRTUAL) INHERITS (gtest1, gtesty); -- error +DROP TABLE gtesty; + +-- adding a column merges it into an existing child column +CREATE TABLE gtesty (a int); +CREATE TABLE gtesty_1 (a int, b int GENERATED ALWAYS AS (a * 3) VIRTUAL) INHERITS (gtesty); +CREATE TABLE gtesty_2 (a int, b int) INHERITS (gtesty); +ALTER TABLE gtesty ADD COLUMN b int GENERATED ALWAYS AS (a * 2) VIRTUAL; -- error +DROP TABLE gtesty_1; +ALTER TABLE gtesty ADD COLUMN b int GENERATED ALWAYS AS (a * 2) VIRTUAL; -- error +DROP TABLE gtesty CASCADE; + +-- a child with two parents cannot have its expression changed through one +CREATE TABLE gtesty (a int, b int GENERATED ALWAYS AS (a * 2) VIRTUAL); +CREATE TABLE gtestz (a int, b int GENERATED ALWAYS AS (a * 2) VIRTUAL); +CREATE TABLE gtestyz () INHERITS (gtesty, gtestz); +ALTER TABLE gtesty ALTER COLUMN b SET EXPRESSION AS (a * 3); -- error +ALTER TABLE gtestyz ALTER COLUMN b SET EXPRESSION AS (a * 3); -- error, no hint +DROP TABLE gtesty, gtestz CASCADE; +-- also when the column is added by the same command +CREATE TABLE gtestq (a int, b int GENERATED ALWAYS AS (a * 2) VIRTUAL); +CREATE TABLE gtestp (a int); +CREATE TABLE gtestpq () INHERITS (gtestp, gtestq); +ALTER TABLE gtestp ADD COLUMN b int GENERATED ALWAYS AS (a * 2) VIRTUAL, + ALTER COLUMN b SET EXPRESSION AS (a * 3); -- error +DROP TABLE gtestp, gtestq CASCADE; -- test correct handling of GENERATED column that's only in child CREATE TABLE gtestp (f1 int); @@ -514,7 +539,10 @@ CREATE TABLE gtest_parent (f1 date NOT NULL, f2 bigint, f3 bigint GENERATED ALWA CREATE TABLE gtest_child PARTITION OF gtest_parent FOR VALUES FROM ('2016-07-01') TO ('2016-08-01'); -- inherits gen expr CREATE TABLE gtest_child2 PARTITION OF gtest_parent ( - f3 WITH OPTIONS GENERATED ALWAYS AS (f2 * 22) VIRTUAL -- overrides gen expr + f3 WITH OPTIONS GENERATED ALWAYS AS (f2 * 22) VIRTUAL -- error, must match parent +) FOR VALUES FROM ('2016-08-01') TO ('2016-09-01'); +CREATE TABLE gtest_child2 PARTITION OF gtest_parent ( + f3 WITH OPTIONS GENERATED ALWAYS AS (f2 * 2) VIRTUAL -- same as parent, ok ) FOR VALUES FROM ('2016-08-01') TO ('2016-09-01'); CREATE TABLE gtest_child3 PARTITION OF gtest_parent ( f3 DEFAULT 42 -- error @@ -538,6 +566,15 @@ CREATE TABLE gtest_child3 (f1 date NOT NULL, f2 bigint, f3 bigint GENERATED ALWA ALTER TABLE gtest_parent ATTACH PARTITION gtest_child3 FOR VALUES FROM ('2016-09-01') TO ('2016-10-01'); -- error DROP TABLE gtest_child3; CREATE TABLE gtest_child3 (f1 date NOT NULL, f2 bigint, f3 bigint GENERATED ALWAYS AS (f2 * 33) VIRTUAL); +ALTER TABLE gtest_parent ATTACH PARTITION gtest_child3 FOR VALUES FROM ('2016-09-01') TO ('2016-10-01'); -- error +DROP TABLE gtest_child3; +-- an explicit cast is not the same as an implicit one +CREATE TABLE gtest_cast (a int, b numeric(10,0) GENERATED ALWAYS AS (a) VIRTUAL) PARTITION BY RANGE (a); +CREATE TABLE gtest_cast_1 (a int, b numeric(10,0) GENERATED ALWAYS AS (a::numeric(10,0)) VIRTUAL); +ALTER TABLE gtest_cast ATTACH PARTITION gtest_cast_1 FOR VALUES FROM (0) TO (10); -- error +DROP TABLE gtest_cast, gtest_cast_1; +-- different column order, so the parent's expression has to be mapped +CREATE TABLE gtest_child3 (f3 bigint GENERATED ALWAYS AS (f2 * 2) VIRTUAL, f2 bigint, f1 date NOT NULL); ALTER TABLE gtest_parent ATTACH PARTITION gtest_child3 FOR VALUES FROM ('2016-09-01') TO ('2016-10-01'); \d gtest_child \d gtest_child2 @@ -547,21 +584,21 @@ INSERT INTO gtest_parent (f1, f2) VALUES ('2016-07-15', 2); INSERT INTO gtest_parent (f1, f2) VALUES ('2016-08-15', 3); SELECT tableoid::regclass, * FROM gtest_parent ORDER BY 1, 2, 3; SELECT tableoid::regclass, * FROM gtest_child ORDER BY 1, 2, 3; -SELECT tableoid::regclass, * FROM gtest_child2 ORDER BY 1, 2, 3; -- uses child's generation expression, not parent's +SELECT tableoid::regclass, * FROM gtest_child2 ORDER BY 1, 2, 3; SELECT tableoid::regclass, * FROM gtest_child3 ORDER BY 1, 2, 3; UPDATE gtest_parent SET f1 = f1 + 60 WHERE f2 = 1; SELECT tableoid::regclass, * FROM gtest_parent ORDER BY 1, 2, 3; --- alter only parent's and one child's generation expression -ALTER TABLE ONLY gtest_parent ALTER COLUMN f3 SET EXPRESSION AS (f2 * 4); -ALTER TABLE gtest_child ALTER COLUMN f3 SET EXPRESSION AS (f2 * 10); -\d gtest_parent -\d gtest_child -\d gtest_child2 -\d gtest_child3 -SELECT tableoid::regclass, * FROM gtest_parent ORDER BY 1, 2, 3; +-- cannot alter only parent's or one partition's generation expression +ALTER TABLE ONLY gtest_parent ALTER COLUMN f3 SET EXPRESSION AS (f2 * 4); -- error +ALTER TABLE gtest_child ALTER COLUMN f3 SET EXPRESSION AS (f2 * 10); -- error -- alter generation expression of parent and all its children altogether +ALTER TABLE gtest_parent ALTER COLUMN f3 SET EXPRESSION AS (f2 * 4); +SELECT tableoid::regclass, * FROM gtest_parent ORDER BY 1, 2, 3; +ALTER TABLE gtest_parent ADD CONSTRAINT cc1 CHECK (f3 < 12); -- error +ALTER TABLE gtest_parent ADD CONSTRAINT cc CHECK (f3 < 13); -- ok +ALTER TABLE gtest_parent DROP CONSTRAINT cc; ALTER TABLE gtest_parent ALTER COLUMN f3 SET EXPRESSION AS (f2 * 2); \d gtest_parent \d gtest_child @@ -969,3 +1006,13 @@ select * from gtest_cursor order by id; commit; drop table gtest_cursor; + +-- a partition cannot have a different generation expression, which would let +-- the partitioned table's NOT NULL virtual column read as NULL +CREATE TABLE gtestnn_parent2 (a int NOT NULL, b int GENERATED ALWAYS AS (nullif(a, 0)) VIRTUAL NOT NULL) PARTITION BY LIST (a); +CREATE TABLE gtestnn_child2 PARTITION OF gtestnn_parent2 (b GENERATED ALWAYS AS (a + 1) VIRTUAL) FOR VALUES IN (0); -- error +CREATE TABLE gtestnn_child2 PARTITION OF gtestnn_parent2 FOR VALUES IN (0, 1); +INSERT INTO gtestnn_parent2 VALUES (0); -- error +INSERT INTO gtestnn_parent2 VALUES (1); +SELECT a, b FROM gtestnn_parent2; +DROP TABLE gtestnn_parent2; -- 2.37.1 (Apple Git-137.1)