From d7c090f1c14a5d5ebec04b5a58d626cbfe057e2c Mon Sep 17 00:00:00 2001 From: amit Date: Thu, 21 Jun 2018 14:50:20 +0900 Subject: [PATCH v1] Convert indexParams to partition's attnos before recursing --- src/backend/commands/indexcmds.c | 18 +++++++++++++- src/test/regress/expected/indexing.out | 45 ++++++++++++++++++++++++++++++++++ src/test/regress/sql/indexing.sql | 26 ++++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 3a3223bffb..74ffd2c392 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -922,7 +922,6 @@ DefineIndex(Oid relationId, gettext_noop("could not convert row type")); maplen = parentDesc->natts; - foreach(cell, childidxs) { Oid cldidxid = lfirst_oid(cell); @@ -993,7 +992,24 @@ DefineIndex(Oid relationId, { IndexStmt *childStmt = copyObject(stmt); bool found_whole_row; + ListCell *lc; + /* Adjust Vars to match new table's column numbering */ + foreach(lc, childStmt->indexParams) + { + IndexElem *ielem = lfirst(lc); + + /* + * If the index parameter is an expression, we must + * translate it to contain child Vars. + */ + if (ielem->expr) + ielem->expr = + map_variable_attnos((Node *) ielem->expr, + 1, 0, attmap, maplen, + InvalidOid, + &found_whole_row); + } childStmt->whereClause = map_variable_attnos(stmt->whereClause, 1, 0, attmap, maplen, diff --git a/src/test/regress/expected/indexing.out b/src/test/regress/expected/indexing.out index 2c2bf44aa8..5a9c3ee943 100644 --- a/src/test/regress/expected/indexing.out +++ b/src/test/regress/expected/indexing.out @@ -1337,3 +1337,48 @@ insert into covidxpart values (4, 1); insert into covidxpart values (4, 1); ERROR: duplicate key value violates unique constraint "covidxpart4_a_b_idx" DETAIL: Key (a)=(4) already exists. +-- tests covering expression indexes in a partition tree with differing attributes +create table idx_part (a int) partition by hash (a); +create table idx_part1 partition of idx_part for values with (modulus 2, remainder 0); +create table idx_part2 partition of idx_part for values with (modulus 2, remainder 1); +alter table idx_part detach partition idx_part2; +alter table idx_part2 drop a, add a int; +alter table idx_part attach partition idx_part2 for values with (modulus 2, remainder 1); +create index idx_part_expr on idx_part ((a + 1)); +\d idx_part2 + Table "public.idx_part2" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + a | integer | | | +Partition of: idx_part FOR VALUES WITH (modulus 2, remainder 1) +Indexes: + "idx_part2_expr_idx" btree ((a + 1)) + +drop index idx_part_expr; +alter table idx_part detach partition idx_part2; +create index idx_part2_expr on idx_part2 ((a + 1)); +alter table idx_part attach partition idx_part2 for values with (modulus 2, remainder 1); +create index idx_part_expr on idx_part ((a + 1)); +\d idx_part2 + Table "public.idx_part2" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + a | integer | | | +Partition of: idx_part FOR VALUES WITH (modulus 2, remainder 1) +Indexes: + "idx_part2_expr" btree ((a + 1)) + +drop table idx_part2; +create table idx_part2 (b int, a int); +alter table idx_part2 drop b; +alter table idx_part attach partition idx_part2 for values with (modulus 2, remainder 1); +\d idx_part2; + Table "public.idx_part2" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + a | integer | | | +Partition of: idx_part FOR VALUES WITH (modulus 2, remainder 1) +Indexes: + "idx_part2_expr_idx" btree ((a + 1)) + +drop table idx_part; diff --git a/src/test/regress/sql/indexing.sql b/src/test/regress/sql/indexing.sql index 29333b31ef..b050781e00 100644 --- a/src/test/regress/sql/indexing.sql +++ b/src/test/regress/sql/indexing.sql @@ -720,3 +720,29 @@ create unique index on covidxpart4 (a); alter table covidxpart attach partition covidxpart4 for values in (4); insert into covidxpart values (4, 1); insert into covidxpart values (4, 1); + +-- tests covering expression indexes in a partition tree with differing attributes +create table idx_part (a int) partition by hash (a); +create table idx_part1 partition of idx_part for values with (modulus 2, remainder 0); +create table idx_part2 partition of idx_part for values with (modulus 2, remainder 1); + +alter table idx_part detach partition idx_part2; +alter table idx_part2 drop a, add a int; +alter table idx_part attach partition idx_part2 for values with (modulus 2, remainder 1); +create index idx_part_expr on idx_part ((a + 1)); +\d idx_part2 + +drop index idx_part_expr; +alter table idx_part detach partition idx_part2; +create index idx_part2_expr on idx_part2 ((a + 1)); +alter table idx_part attach partition idx_part2 for values with (modulus 2, remainder 1); +create index idx_part_expr on idx_part ((a + 1)); +\d idx_part2 + +drop table idx_part2; +create table idx_part2 (b int, a int); +alter table idx_part2 drop b; +alter table idx_part attach partition idx_part2 for values with (modulus 2, remainder 1); +\d idx_part2; + +drop table idx_part; -- 2.11.0