Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0b48f13

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 54a4f52 commit 0b48f13

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/backend/parser/parse_utilcmd.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -3698,8 +3698,16 @@ transformPartitionCmd(CreateStmtContext *cxt, PartitionCmd *cmd)
36983698
cmd->bound);
36993699
break;
37003700
case RELKIND_PARTITIONED_INDEX:
3701-
/* nothing to check */
3702-
Assert(cmd->bound == NULL);
3701+
3702+
/*
3703+
* A partitioned index cannot have a partition bound set. ALTER
3704+
* INDEX prevents that with its grammar, but not ALTER TABLE.
3705+
*/
3706+
if (cmd->bound != NULL)
3707+
ereport(ERROR,
3708+
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3709+
errmsg("\"%s\" is not a partitioned table",
3710+
RelationGetRelationName(parentRel))));
37033711
break;
37043712
case RELKIND_RELATION:
37053713
/* the table must be partitioned */

src/test/regress/expected/indexing.out

+17
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,23 @@ Partition of: idxparti2
121121
No partition constraint
122122
btree, for table "public.idxpart1"
123123

124+
-- Forbid 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+
ERROR: "idxpart_c" is not a partitioned table
129+
alter index idxpart_c attach partition idxpart1_c;
130+
select relname, relpartbound from pg_class
131+
where relname in ('idxpart_c', 'idxpart1_c')
132+
order by relname;
133+
relname | relpartbound
134+
------------+--------------
135+
idxpart1_c |
136+
idxpart_c |
137+
(2 rows)
138+
139+
alter table idxpart_c detach partition idxpart1_c;
140+
ERROR: "idxpart_c" is not a table
124141
drop table idxpart;
125142
-- If a partition already has an index, don't create a duplicative one
126143
create table idxpart (a int, b int) partition by range (a, b);

src/test/regress/sql/indexing.sql

+10
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+
-- Forbid 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)