Skip to content

Commit 7963c4c

Browse files
committed
Fix failure when creating cloned indexes for a partition
When using CREATE TABLE for a new partition, the partitioned indexes of the parent are created automatically in a fashion similar to LIKE INDEXES. The new partition and its parent use a mapping for attribute numbers for this operation, and while the mapping was correctly built, its length was defined as the number of attributes of the newly-created child, and not the parent. If the parent includes dropped columns, this could cause failures. This is wrong since 8b08f7d which has introduced the concept of partitioned indexes, so backpatch down to 11. Reported-by: Wyatt Alt Author: Michael Paquier Reviewed-by: Amit Langote Discussion: https://postgr.es/m/CAGem3qCcRmhbs4jYMkenYNfP2kEusDXvTfw-q+eOhM0zTceG-g@mail.gmail.com Backpatch-through: 11
1 parent 7b8c2de commit 7963c4c

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

src/backend/commands/tablecmds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
10921092
gettext_noop("could not convert row type"));
10931093
idxstmt =
10941094
generateClonedIndexStmt(NULL, idxRel,
1095-
attmap, RelationGetDescr(rel)->natts,
1095+
attmap, RelationGetDescr(parent)->natts,
10961096
&constraintOid);
10971097
DefineIndex(RelationGetRelid(rel),
10981098
idxstmt,

src/test/regress/expected/create_table.out

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,3 +1168,52 @@ insert into defcheck_def values (0, 0);
11681168
create table defcheck_0 partition of defcheck for values in (0);
11691169
ERROR: updated partition constraint for default partition "defcheck_def" would be violated by some row
11701170
drop table defcheck;
1171+
-- tests of column drop with partition tables and indexes using
1172+
-- predicates and expressions.
1173+
create table part_column_drop (
1174+
useless_1 int,
1175+
id int,
1176+
useless_2 int,
1177+
d int,
1178+
b int,
1179+
useless_3 int
1180+
) partition by range (id);
1181+
alter table part_column_drop drop column useless_1;
1182+
alter table part_column_drop drop column useless_2;
1183+
alter table part_column_drop drop column useless_3;
1184+
create index part_column_drop_b_pred on part_column_drop(b) where b = 1;
1185+
create index part_column_drop_b_expr on part_column_drop((b = 1));
1186+
create index part_column_drop_d_pred on part_column_drop(d) where d = 2;
1187+
create index part_column_drop_d_expr on part_column_drop((d = 2));
1188+
create table part_column_drop_1_10 partition of
1189+
part_column_drop for values from (1) to (10);
1190+
\d part_column_drop
1191+
Partitioned table "public.part_column_drop"
1192+
Column | Type | Collation | Nullable | Default
1193+
--------+---------+-----------+----------+---------
1194+
id | integer | | |
1195+
d | integer | | |
1196+
b | integer | | |
1197+
Partition key: RANGE (id)
1198+
Indexes:
1199+
"part_column_drop_b_expr" btree ((b = 1))
1200+
"part_column_drop_b_pred" btree (b) WHERE b = 1
1201+
"part_column_drop_d_expr" btree ((d = 2))
1202+
"part_column_drop_d_pred" btree (d) WHERE d = 2
1203+
Number of partitions: 1 (Use \d+ to list them.)
1204+
1205+
\d part_column_drop_1_10
1206+
Table "public.part_column_drop_1_10"
1207+
Column | Type | Collation | Nullable | Default
1208+
--------+---------+-----------+----------+---------
1209+
id | integer | | |
1210+
d | integer | | |
1211+
b | integer | | |
1212+
Partition of: part_column_drop FOR VALUES FROM (1) TO (10)
1213+
Indexes:
1214+
"part_column_drop_1_10_b_idx" btree (b) WHERE b = 1
1215+
"part_column_drop_1_10_d_idx" btree (d) WHERE d = 2
1216+
"part_column_drop_1_10_expr_idx" btree ((b = 1))
1217+
"part_column_drop_1_10_expr_idx1" btree ((d = 2))
1218+
1219+
drop table part_column_drop;

src/test/regress/sql/create_table.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,3 +903,26 @@ create table defcheck_1 partition of defcheck for values in (1, null);
903903
insert into defcheck_def values (0, 0);
904904
create table defcheck_0 partition of defcheck for values in (0);
905905
drop table defcheck;
906+
907+
-- tests of column drop with partition tables and indexes using
908+
-- predicates and expressions.
909+
create table part_column_drop (
910+
useless_1 int,
911+
id int,
912+
useless_2 int,
913+
d int,
914+
b int,
915+
useless_3 int
916+
) partition by range (id);
917+
alter table part_column_drop drop column useless_1;
918+
alter table part_column_drop drop column useless_2;
919+
alter table part_column_drop drop column useless_3;
920+
create index part_column_drop_b_pred on part_column_drop(b) where b = 1;
921+
create index part_column_drop_b_expr on part_column_drop((b = 1));
922+
create index part_column_drop_d_pred on part_column_drop(d) where d = 2;
923+
create index part_column_drop_d_expr on part_column_drop((d = 2));
924+
create table part_column_drop_1_10 partition of
925+
part_column_drop for values from (1) to (10);
926+
\d part_column_drop
927+
\d part_column_drop_1_10
928+
drop table part_column_drop;

0 commit comments

Comments
 (0)