]> granicus.if.org Git - postgresql/commitdiff
Avoid crash in ALTER TABLE not_partitioned DETACH PARTITION.
authorRobert Haas <rhaas@postgresql.org>
Thu, 16 Feb 2017 13:37:37 +0000 (08:37 -0500)
committerRobert Haas <rhaas@postgresql.org>
Thu, 16 Feb 2017 13:40:58 +0000 (08:40 -0500)
Amit Langote, reviewed and slightly changed by me.

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

index 0f78abaae206803fdd7a7c560e10c0a37d388a86..46e5ae59de73fb7f427713180b0d528c460e4759 100644 (file)
@@ -133,7 +133,7 @@ static void transformConstraintAttrs(CreateStmtContext *cxt,
                                                 List *constraintList);
 static void transformColumnType(CreateStmtContext *cxt, ColumnDef *column);
 static void setSchemaName(char *context_schema, char **stmt_schema_name);
-static void transformAttachPartition(CreateStmtContext *cxt, PartitionCmd *cmd);
+static void transformPartitionCmd(CreateStmtContext *cxt, PartitionCmd *cmd);
 
 
 /*
@@ -2654,12 +2654,12 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
                                }
 
                        case AT_AttachPartition:
+                       case AT_DetachPartition:
                                {
                                        PartitionCmd *partcmd = (PartitionCmd *) cmd->def;
 
-                                       transformAttachPartition(&cxt, partcmd);
-
-                                       /* assign transformed values */
+                                       transformPartitionCmd(&cxt, partcmd);
+                                       /* assign transformed value of the partition bound */
                                        partcmd->bound = cxt.partbound;
                                }
 
@@ -3032,28 +3032,29 @@ setSchemaName(char *context_schema, char **stmt_schema_name)
 }
 
 /*
- * transformAttachPartition
- *             Analyze ATTACH PARTITION ... FOR VALUES ...
+ * transformPartitionCmd
+ *             Analyze the ATTACH/DETACH PARTITION command
+ *
+ * In case of the ATTACH PARTITION command, cxt->partbound is set to the
+ * transformed value of cmd->bound.
  */
 static void
-transformAttachPartition(CreateStmtContext *cxt, PartitionCmd *cmd)
+transformPartitionCmd(CreateStmtContext *cxt, PartitionCmd *cmd)
 {
        Relation        parentRel = cxt->rel;
 
-       /*
-        * We are going to try to validate the partition bound specification
-        * against the partition key of rel, so it better have one.
-        */
+       /* the table must be partitioned */
        if (parentRel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
                ereport(ERROR,
                                (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                                 errmsg("\"%s\" is not partitioned",
                                                RelationGetRelationName(parentRel))));
 
-       /* transform the values */
+       /* transform the partition bound, if any */
        Assert(RelationGetPartitionKey(parentRel) != NULL);
-       cxt->partbound = transformPartitionBound(cxt->pstate, parentRel,
-                                                                                        cmd->bound);
+       if (cmd->bound != NULL)
+               cxt->partbound = transformPartitionBound(cxt->pstate, parentRel,
+                                                                                                cmd->bound);
 }
 
 /*
index b0e80a7788fb7f49c25755b85e2db8387d0f8f3d..e84af67fb225cbcd0b55d9774ca724c1a86bd3c1 100644 (file)
@@ -3259,6 +3259,11 @@ DETAIL:  "list_parted2" is already a child of "list_parted2".
 --
 -- DETACH PARTITION
 --
+-- check that the table is partitioned at all
+CREATE TABLE regular_table (a int);
+ALTER TABLE regular_table DETACH PARTITION any_name;
+ERROR:  "regular_table" is not partitioned
+DROP TABLE regular_table;
 -- check that the partition being detached exists at all
 ALTER TABLE list_parted2 DETACH PARTITION part_4;
 ERROR:  relation "part_4" does not exist
index 7513769359756c15f6db04c7c199feff8891c8d6..a403fd8cb4de5dde34182792384614de3c5a4294 100644 (file)
@@ -2139,6 +2139,11 @@ ALTER TABLE list_parted2 ATTACH PARTITION list_parted2 FOR VALUES IN (0);
 -- DETACH PARTITION
 --
 
+-- check that the table is partitioned at all
+CREATE TABLE regular_table (a int);
+ALTER TABLE regular_table DETACH PARTITION any_name;
+DROP TABLE regular_table;
+
 -- check that the partition being detached exists at all
 ALTER TABLE list_parted2 DETACH PARTITION part_4;