alter-tab-idx-part-attach.patch

application/octet-stream

Filename: alter-tab-idx-part-attach.patch
Type: application/octet-stream
Part: 0
Message: Re: BUG #16276: Server crash on an invalid attempt to attach a partition to an index

Patch

Format: unified
File+
src/backend/parser/parse_utilcmd.c 11 2
src/test/regress/expected/indexing.out 6 0
src/test/regress/sql/indexing.sql 5 0
diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c
index ee2d2b54a1..50e9312a68 100644
--- a/src/backend/parser/parse_utilcmd.c
+++ b/src/backend/parser/parse_utilcmd.c
@@ -3698,8 +3698,17 @@ transformPartitionCmd(CreateStmtContext *cxt, PartitionCmd *cmd)
 														 cmd->bound);
 			break;
 		case RELKIND_PARTITIONED_INDEX:
-			/* nothing to check */
-			Assert(cmd->bound == NULL);
+			/*
+			 * ALTER INDEX, which does not allow partition bound to be
+			 * specified, must be used when trying to attach partition of an
+			 * index.
+			 */
+			if (cmd->bound != NULL)
+				ereport(ERROR,
+						(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
+						 errmsg("\"%s\" is not a partitioned table",
+								RelationGetRelationName(parentRel)),
+						 errhint("Use ALTER INDEX to attach index partition.")));
 			break;
 		case RELKIND_RELATION:
 			/* the table must be partitioned */
diff --git a/src/test/regress/expected/indexing.out b/src/test/regress/expected/indexing.out
index ec1d4eaef4..5420875d5f 100644
--- a/src/test/regress/expected/indexing.out
+++ b/src/test/regress/expected/indexing.out
@@ -121,6 +121,12 @@ Partition of: idxparti2
 No partition constraint
 btree, for table "public.idxpart1"
 
+-- Don't allow using ALTER TABLE to attach index partition
+create index idxpart_c on only idxpart (c);
+create index idxpart1_c on idxpart1 (c);
+alter table idxpart_c attach partition idxpart1_c default;
+ERROR:  "idxpart_c" is not a partitioned table
+HINT:  Use ALTER INDEX to attach index partition.
 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);
diff --git a/src/test/regress/sql/indexing.sql b/src/test/regress/sql/indexing.sql
index f6a3767918..313871bf4e 100644
--- a/src/test/regress/sql/indexing.sql
+++ b/src/test/regress/sql/indexing.sql
@@ -63,6 +63,11 @@ alter table idxpart attach partition idxpart1 for values from (0) to (10);
 \d idxpart1
 \d+ idxpart1_a_idx
 \d+ idxpart1_b_c_idx
+
+-- Don't allow using ALTER TABLE to attach index partition
+create index idxpart_c on only idxpart (c);
+create index idxpart1_c on idxpart1 (c);
+alter table idxpart_c attach partition idxpart1_c default;
 drop table idxpart;
 
 -- If a partition already has an index, don't create a duplicative one