mirror of
https://github.com/postgres/postgres.git
synced 2025-06-20 15:22:23 +03:00
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 <ewaldo@healthetechs.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
This commit is contained in:
@ -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.
|
||||
|
Reference in New Issue
Block a user