From b81a9c2fc52509025c635fa08ecaec1bad21441b Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Wed, 25 Sep 2019 17:30:42 -0400 Subject: Fix handling of GENERATED columns in CREATE TABLE LIKE INCLUDING DEFAULTS. LIKE INCLUDING DEFAULTS tried to copy the attrdef expression without copying the state of the attgenerated column. This is in fact wrong, because GENERATED and DEFAULT expressions are not the same kind of animal; one can contain Vars and the other not. We *must* copy attgenerated when we're copying the attrdef expression. Rearrange the if-tests so that the expression is copied only when the correct one of INCLUDING DEFAULTS and INCLUDING GENERATED has been specified. Per private report from Manuel Rigger. Tom Lane and Peter Eisentraut --- src/backend/parser/parse_utilcmd.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/backend/parser/parse_utilcmd.c') diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index 6e5768c66cf..ee475476248 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -1023,11 +1023,13 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla attmap[parent_attno - 1] = list_length(cxt->columns); /* - * Copy default, if present and the default has been requested + * Copy default, if present and it should be copied. We have separate + * options for plain default expressions and GENERATED defaults. */ if (attribute->atthasdef && - (table_like_clause->options & CREATE_TABLE_LIKE_DEFAULTS || - table_like_clause->options & CREATE_TABLE_LIKE_GENERATED)) + (attribute->attgenerated ? + (table_like_clause->options & CREATE_TABLE_LIKE_GENERATED) : + (table_like_clause->options & CREATE_TABLE_LIKE_DEFAULTS))) { Node *this_default = NULL; AttrDefault *attrdef; @@ -1065,9 +1067,7 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla attributeName, RelationGetRelationName(relation)))); - if (attribute->attgenerated && - (table_like_clause->options & CREATE_TABLE_LIKE_GENERATED)) - def->generated = attribute->attgenerated; + def->generated = attribute->attgenerated; } /* -- cgit v1.2.3