Skip to content

Commit dc83648

Browse files
committed
Fix assertion failure with ALTER TABLE ATTACH PARTITION and indexes
Using ALTER TABLE ATTACH PARTITION causes an assertion failure when attempting to work on a partitioned index, because partitioned indexes cannot have partition bounds. The grammar of ALTER TABLE ATTACH PARTITION requires partition bounds, but not ALTER INDEX, so mixing ALTER TABLE with partitioned indexes is confusing. Hence, on HEAD, prevent ALTER TABLE to attach a partition if the relation involved is a partitioned index. On back-branches, as applications may rely on the existing behavior, just remove the culprit assertion. Reported-by: Alexander Lakhin Author: Amit Langote, Michael Paquier Discussion: https://postgr.es/m/16276-5cd1dcc8fb8be7b5@postgresql.org Backpatch-through: 11
1 parent f22feed commit dc83648

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/backend/parser/parse_utilcmd.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3685,7 +3685,6 @@ transformPartitionCmd(CreateStmtContext *cxt, PartitionCmd *cmd)
36853685
break;
36863686
case RELKIND_PARTITIONED_INDEX:
36873687
/* nothing to check */
3688-
Assert(cmd->bound == NULL);
36893688
break;
36903689
case RELKIND_RELATION:
36913690
/* the table must be partitioned */

src/test/regress/expected/indexing.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,22 @@ Partition of: idxparti2
121121
No partition constraint
122122
btree, for table "public.idxpart1"
123123

124+
-- ALTER TABLE when attaching or detaching an index to a partition.
125+
create index idxpart_c on only idxpart (c);
126+
create index idxpart1_c on idxpart1 (c);
127+
alter table idxpart_c attach partition idxpart1_c for values from (10) to (20);
128+
alter index idxpart_c attach partition idxpart1_c;
129+
select relname, relpartbound from pg_class
130+
where relname in ('idxpart_c', 'idxpart1_c')
131+
order by relname;
132+
relname | relpartbound
133+
------------+--------------
134+
idxpart1_c |
135+
idxpart_c |
136+
(2 rows)
137+
138+
alter table idxpart_c detach partition idxpart1_c;
139+
ERROR: "idxpart_c" is not a table
124140
drop table idxpart;
125141
-- If a partition already has an index, don't create a duplicative one
126142
create table idxpart (a int, b int) partition by range (a, b);

src/test/regress/sql/indexing.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ alter table idxpart attach partition idxpart1 for values from (0) to (10);
6363
\d idxpart1
6464
\d+ idxpart1_a_idx
6565
\d+ idxpart1_b_c_idx
66+
67+
-- ALTER TABLE when attaching or detaching an index to a partition.
68+
create index idxpart_c on only idxpart (c);
69+
create index idxpart1_c on idxpart1 (c);
70+
alter table idxpart_c attach partition idxpart1_c for values from (10) to (20);
71+
alter index idxpart_c attach partition idxpart1_c;
72+
select relname, relpartbound from pg_class
73+
where relname in ('idxpart_c', 'idxpart1_c')
74+
order by relname;
75+
alter table idxpart_c detach partition idxpart1_c;
6676
drop table idxpart;
6777

6878
-- If a partition already has an index, don't create a duplicative one

0 commit comments

Comments
 (0)