From bfd25c13e74272a1b7fc2707413ba553ec6bce27 Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Mon, 16 Nov 2026 20:21:38 -0500 Subject: [PATCH 1/2] Check LIKE defaults and generation expressions against inherited columns CREATE TABLE ... (LIKE ...) INHERITS adds copied defaults and generation expressions after creation, skipping the checks the explicit syntax gets. --- src/backend/commands/tablecmds.c | 26 ++++++++++++++--- src/backend/parser/parse_utilcmd.c | 45 +++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index b6700929459..216bf0f9a86 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -8882,10 +8882,28 @@ ATExecCookedColumnDefault(Relation rel, AttrNumber attnum, CheckUsageOnTypesInSingleRelExpr(newDefault, RelationGetRelid(rel), GetUserId()); /* - * Remove any old default for the column. We use RESTRICT here for - * safety, but at present we do not expect anything to depend on the - * default. (In ordinary cases, there could not be a default in place - * anyway, but it's possible when combining LIKE with inheritance.) + * Remove any old default for the column. In ordinary cases, there could + * not be a default in place anyway, but it's possible when combining LIKE + * with inheritance. The old default can even be an inherited generation + * expression, which the column depends on internally. As in + * ATExecSetExpression, drop that dependency first. + */ + if (TupleDescAttr(RelationGetDescr(rel), attnum - 1)->attgenerated) + { + Oid attrdefoid; + + attrdefoid = GetAttrDefaultOid(RelationGetRelid(rel), attnum); + if (OidIsValid(attrdefoid)) + { + (void) deleteDependencyRecordsFor(AttrDefaultRelationId, + attrdefoid, false); + CommandCounterIncrement(); + } + } + + /* + * We use RESTRICT here for safety, but at present we do not expect + * anything to depend on the default. */ RemoveAttrDefault(RelationGetRelid(rel), attnum, DROP_RESTRICT, false, true); diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index 583346bf531..46e6e433c1d 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -1422,15 +1422,29 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause) Node *this_default; AlterTableCmd *atsubcmd; bool found_whole_row; + TupleDesc childdesc = RelationGetDescr(childrel); + AttrNumber child_attno = attmap->attnums[parent_attno - 1]; this_default = TupleDescGetDefault(tupleDesc, parent_attno); if (this_default == NULL) elog(ERROR, "default expression not found for attribute %d of relation \"%s\"", parent_attno, RelationGetRelationName(relation)); + /* + * If the column was merged with an inherited generated + * column, it can't take a plain default, just as if the + * column had been written out (see MergeChildAttribute). + */ + if (!attribute->attgenerated && + TupleDescAttr(childdesc, child_attno - 1)->attgenerated) + ereport(ERROR, + (errcode(ERRCODE_INVALID_COLUMN_DEFINITION), + errmsg("column \"%s\" inherits from generated column but specifies default", + NameStr(attribute->attname)))); + atsubcmd = makeNode(AlterTableCmd); atsubcmd->subtype = AT_CookedColumnDefault; - atsubcmd->num = attmap->attnums[parent_attno - 1]; + atsubcmd->num = child_attno; atsubcmd->def = map_variable_attnos(this_default, 1, 0, attmap, @@ -1450,6 +1464,35 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause) NameStr(attribute->attname), RelationGetRelationName(relation)))); + /* + * A column that the generation expression refers to may have + * been merged with an inherited generated column. The + * expression was cooked against the LIKE table, so redo the + * check of cookDefault against the new table. + */ + if (attribute->attgenerated) + { + Bitmapset *attnos = NULL; + int k = -1; + + pull_varattnos(atsubcmd->def, 1, &attnos); + while ((k = bms_next_member(attnos, k)) >= 0) + { + AttrNumber attno = k + FirstLowInvalidHeapAttributeNumber; + Form_pg_attribute refatt; + + if (attno <= 0) + continue; + refatt = TupleDescAttr(childdesc, attno - 1); + if (refatt->attgenerated) + ereport(ERROR, + (errcode(ERRCODE_INVALID_OBJECT_DEFINITION), + errmsg("cannot use generated column \"%s\" in column generation expression", + NameStr(refatt->attname)), + errdetail("A generated column cannot reference another generated column."))); + } + } + atsubcmds = lappend(atsubcmds, atsubcmd); } } -- 2.37.1 (Apple Git-137.1)