diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 860a60d109..ad819177d7 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -342,6 +342,7 @@ DefineIndex(Oid relationId, Oid tablespaceId; Oid createdConstraintId = InvalidOid; List *indexColNames; + List *allIndexParams; Relation rel; Relation indexRelation; HeapTuple tuple; @@ -378,16 +379,16 @@ DefineIndex(Oid relationId, numberOfKeyAttributes = list_length(stmt->indexParams); /* - * We append any INCLUDE columns onto the indexParams list so that we have - * one list with all columns. Later we can determine which of these are - * key columns, and which are just part of the INCLUDE list by checking - * the list position. A list item in a position less than - * ii_NumIndexKeyAttrs is part of the key columns, and anything equal to - * and over is part of the INCLUDE columns. + * Calculate the new list of index columns including both key columns and + * INCLUDE columns. Later we can determine which of these are key columns, + * and which are just part of the INCLUDE list by checking the list + * position. A list item in a position less than ii_NumIndexKeyAttrs is + * part of the key columns, and anything equal to and over is part of the + * INCLUDE columns. */ - stmt->indexParams = list_concat(stmt->indexParams, - stmt->indexIncludingParams); - numberOfAttributes = list_length(stmt->indexParams); + allIndexParams = list_concat(list_copy(stmt->indexParams), + list_copy(stmt->indexIncludingParams)); + numberOfAttributes = list_length(allIndexParams); if (numberOfAttributes <= 0) ereport(ERROR, @@ -544,7 +545,7 @@ DefineIndex(Oid relationId, /* * Choose the index column names. */ - indexColNames = ChooseIndexColumnNames(stmt->indexParams); + indexColNames = ChooseIndexColumnNames(allIndexParams); /* * Select name for index if caller didn't specify @@ -658,7 +659,7 @@ DefineIndex(Oid relationId, coloptions = (int16 *) palloc(numberOfAttributes * sizeof(int16)); ComputeIndexAttrs(indexInfo, typeObjectId, collationObjectId, classObjectId, - coloptions, stmt->indexParams, + coloptions, allIndexParams, stmt->excludeOpNames, relationId, accessMethodName, accessMethodId, amcanorder, stmt->isconstraint); @@ -886,8 +887,8 @@ DefineIndex(Oid relationId, memcpy(part_oids, partdesc->oids, sizeof(Oid) * nparts); parentDesc = CreateTupleDescCopy(RelationGetDescr(rel)); - opfamOids = palloc(sizeof(Oid) * numberOfAttributes); - for (i = 0; i < numberOfAttributes; i++) + opfamOids = palloc(sizeof(Oid) * numberOfKeyAttributes); + for (i = 0; i < numberOfKeyAttributes; i++) opfamOids[i] = get_opclass_family(classObjectId[i]); heap_close(rel, NoLock); diff --git a/src/test/regress/expected/indexing.out b/src/test/regress/expected/indexing.out index 33f68aab71..1ff57c70c6 100644 --- a/src/test/regress/expected/indexing.out +++ b/src/test/regress/expected/indexing.out @@ -1038,19 +1038,19 @@ create table idxpart1 partition of idxpart for values from (0) to (100000); create table idxpart2 (c int, like idxpart); insert into idxpart2 (c, a, b) values (42, 572814, 'inserted first'); alter table idxpart2 drop column c; -create unique index on idxpart (a); +create unique index on idxpart (a) include (b); alter table idxpart attach partition idxpart2 for values from (100000) to (1000000); insert into idxpart values (0, 'zero'), (42, 'life'), (2^16, 'sixteen'); insert into idxpart select 2^g, format('two to power of %s', g) from generate_series(15, 17) g; -ERROR: duplicate key value violates unique constraint "idxpart1_a_idx" +ERROR: duplicate key value violates unique constraint "idxpart1_a_b_idx" DETAIL: Key (a)=(65536) already exists. insert into idxpart values (16, 'sixteen'); insert into idxpart (b, a) values ('one', 142857), ('two', 285714); insert into idxpart select a * 2, b || b from idxpart where a between 2^16 and 2^19; -ERROR: duplicate key value violates unique constraint "idxpart2_a_idx" +ERROR: duplicate key value violates unique constraint "idxpart2_a_b_idx" DETAIL: Key (a)=(285714) already exists. insert into idxpart values (572814, 'five'); -ERROR: duplicate key value violates unique constraint "idxpart2_a_idx" +ERROR: duplicate key value violates unique constraint "idxpart2_a_b_idx" DETAIL: Key (a)=(572814) already exists. insert into idxpart values (857142, 'six'); select tableoid::regclass, * from idxpart order by a; diff --git a/src/test/regress/sql/indexing.sql b/src/test/regress/sql/indexing.sql index ab7c2d1475..9aa2784e9c 100644 --- a/src/test/regress/sql/indexing.sql +++ b/src/test/regress/sql/indexing.sql @@ -562,7 +562,7 @@ create table idxpart1 partition of idxpart for values from (0) to (100000); create table idxpart2 (c int, like idxpart); insert into idxpart2 (c, a, b) values (42, 572814, 'inserted first'); alter table idxpart2 drop column c; -create unique index on idxpart (a); +create unique index on idxpart (a) include (b); alter table idxpart attach partition idxpart2 for values from (100000) to (1000000); insert into idxpart values (0, 'zero'), (42, 'life'), (2^16, 'sixteen'); insert into idxpart select 2^g, format('two to power of %s', g) from generate_series(15, 17) g;