Fix assertion failure with ALTER TABLE ATTACH PARTITION and indexes
authorMichael Paquier <michael@paquier.xyz>
Tue, 3 Mar 2020 04:56:34 +0000 (13:56 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 3 Mar 2020 04:56:34 +0000 (13:56 +0900)
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

src/backend/parser/parse_utilcmd.c
src/test/regress/expected/indexing.out
src/test/regress/sql/indexing.sql

index 5d2329ddc26c8f61114d203398c5f0fea592a4be..46d2803aa8493db11dc9fba9e236e10ebaf4aab9 100644 (file)
@@ -3563,7 +3563,6 @@ transformPartitionCmd(CreateStmtContext *cxt, PartitionCmd *cmd)
            break;
        case RELKIND_PARTITIONED_INDEX:
            /* nothing to check */
-           Assert(cmd->bound == NULL);
            break;
        case RELKIND_RELATION:
            /* the table must be partitioned */
index 6a21c0c2e1949787830cdd1632ddbfadcb59eceb..c7822a4c5852fab7189148ee86c61b35f9f4681d 100644 (file)
@@ -94,6 +94,22 @@ Partition of: idxparti2
 No partition constraint
 btree, for table "public.idxpart1"
 
+-- ALTER TABLE when attaching or detaching an index to a partition.
+create index idxpart_c on only idxpart (c);
+create index idxpart1_c on idxpart1 (c);
+alter table idxpart_c attach partition idxpart1_c for values from (10) to (20);
+alter index idxpart_c attach partition idxpart1_c;
+select relname, relpartbound from pg_class
+  where relname in ('idxpart_c', 'idxpart1_c')
+  order by relname;
+  relname   | relpartbound 
+------------+--------------
+ idxpart1_c | 
+ idxpart_c  | 
+(2 rows)
+
+alter table idxpart_c detach partition idxpart1_c;
+ERROR:  "idxpart_c" is not a table
 drop table idxpart;
 -- If a partition already has an index, don't create a duplicative one
 create table idxpart (a int, b int) partition by range (a, b);
index d14d9f85287d4b9ed9dc0a14f439c075b6afbeb7..d87031b3984b2be660d9d45d787f83a23a22e2de 100644 (file)
@@ -46,6 +46,16 @@ alter table idxpart attach partition idxpart1 for values from (0) to (10);
 \d idxpart1
 \d+ idxpart1_a_idx
 \d+ idxpart1_b_c_idx
+
+-- ALTER TABLE when attaching or detaching an index to a partition.
+create index idxpart_c on only idxpart (c);
+create index idxpart1_c on idxpart1 (c);
+alter table idxpart_c attach partition idxpart1_c for values from (10) to (20);
+alter index idxpart_c attach partition idxpart1_c;
+select relname, relpartbound from pg_class
+  where relname in ('idxpart_c', 'idxpart1_c')
+  order by relname;
+alter table idxpart_c detach partition idxpart1_c;
 drop table idxpart;
 
 -- If a partition already has an index, don't create a duplicative one