From e92e4a2b68fe877677278c1300db1780956c984e Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Thu, 9 Apr 2020 16:17:55 +0200 Subject: Fix CREATE TABLE LIKE INCLUDING GENERATED column order issue CREATE TABLE LIKE INCLUDING GENERATED would fail if a generated column referred to a column with a higher attribute number. This is because the column mapping mechanism created the mapping incrementally as columns are added. This was sufficient for previous uses of that mechanism (omitting dropped columns), and it also happened to work if generated columns only referred to columns with lower attribute numbers, but here it failed. This fix is to build the attribute mapping in a separate loop before processing the columns in detail. Bug: #16342 Reported-by: Ethan Waldo Reviewed-by: Tom Lane --- src/backend/parser/parse_utilcmd.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 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 350959a5e0b..75c122fe348 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -924,6 +924,7 @@ static void transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_clause) { AttrNumber parent_attno; + AttrNumber new_attno; Relation relation; TupleDesc tupleDesc; TupleConstr *constr; @@ -986,6 +987,26 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla */ attmap = make_attrmap(tupleDesc->natts); + /* + * We must fill the attmap now so that it can be used to process generated + * column default expressions in the per-column loop below. + */ + new_attno = 1; + for (parent_attno = 1; parent_attno <= tupleDesc->natts; + parent_attno++) + { + Form_pg_attribute attribute = TupleDescAttr(tupleDesc, + parent_attno - 1); + + /* + * Ignore dropped columns in the parent. attmap entry is left zero. + */ + if (attribute->attisdropped) + continue; + + attmap->attnums[parent_attno - 1] = list_length(cxt->columns) + (new_attno++); + } + /* * Insert the copied attributes into the cxt for the new table definition. */ @@ -998,7 +1019,7 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla ColumnDef *def; /* - * Ignore dropped columns in the parent. attmap entry is left zero. + * Ignore dropped columns in the parent. */ if (attribute->attisdropped) continue; @@ -1030,8 +1051,6 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla */ cxt->columns = lappend(cxt->columns, def); - attmap->attnums[parent_attno - 1] = list_length(cxt->columns); - /* * Copy default, if present and it should be copied. We have separate * options for plain default expressions and GENERATED defaults. -- cgit v1.2.3