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

Commit 5940730

Browse files
committed
Avoid crash in ALTER TABLE not_partitioned DETACH PARTITION.
Amit Langote, reviewed and slightly changed by me.
1 parent 93e6e40 commit 5940730

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

src/backend/parser/parse_utilcmd.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static void transformConstraintAttrs(CreateStmtContext *cxt,
133133
List *constraintList);
134134
static void transformColumnType(CreateStmtContext *cxt, ColumnDef *column);
135135
static void setSchemaName(char *context_schema, char **stmt_schema_name);
136-
static void transformAttachPartition(CreateStmtContext *cxt, PartitionCmd *cmd);
136+
static void transformPartitionCmd(CreateStmtContext *cxt, PartitionCmd *cmd);
137137

138138

139139
/*
@@ -2654,12 +2654,12 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
26542654
}
26552655

26562656
case AT_AttachPartition:
2657+
case AT_DetachPartition:
26572658
{
26582659
PartitionCmd *partcmd = (PartitionCmd *) cmd->def;
26592660

2660-
transformAttachPartition(&cxt, partcmd);
2661-
2662-
/* assign transformed values */
2661+
transformPartitionCmd(&cxt, partcmd);
2662+
/* assign transformed value of the partition bound */
26632663
partcmd->bound = cxt.partbound;
26642664
}
26652665

@@ -3032,28 +3032,29 @@ setSchemaName(char *context_schema, char **stmt_schema_name)
30323032
}
30333033

30343034
/*
3035-
* transformAttachPartition
3036-
* Analyze ATTACH PARTITION ... FOR VALUES ...
3035+
* transformPartitionCmd
3036+
* Analyze the ATTACH/DETACH PARTITION command
3037+
*
3038+
* In case of the ATTACH PARTITION command, cxt->partbound is set to the
3039+
* transformed value of cmd->bound.
30373040
*/
30383041
static void
3039-
transformAttachPartition(CreateStmtContext *cxt, PartitionCmd *cmd)
3042+
transformPartitionCmd(CreateStmtContext *cxt, PartitionCmd *cmd)
30403043
{
30413044
Relation parentRel = cxt->rel;
30423045

3043-
/*
3044-
* We are going to try to validate the partition bound specification
3045-
* against the partition key of rel, so it better have one.
3046-
*/
3046+
/* the table must be partitioned */
30473047
if (parentRel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
30483048
ereport(ERROR,
30493049
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
30503050
errmsg("\"%s\" is not partitioned",
30513051
RelationGetRelationName(parentRel))));
30523052

3053-
/* transform the values */
3053+
/* transform the partition bound, if any */
30543054
Assert(RelationGetPartitionKey(parentRel) != NULL);
3055-
cxt->partbound = transformPartitionBound(cxt->pstate, parentRel,
3056-
cmd->bound);
3055+
if (cmd->bound != NULL)
3056+
cxt->partbound = transformPartitionBound(cxt->pstate, parentRel,
3057+
cmd->bound);
30573058
}
30583059

30593060
/*

src/test/regress/expected/alter_table.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3259,6 +3259,11 @@ DETAIL: "list_parted2" is already a child of "list_parted2".
32593259
--
32603260
-- DETACH PARTITION
32613261
--
3262+
-- check that the table is partitioned at all
3263+
CREATE TABLE regular_table (a int);
3264+
ALTER TABLE regular_table DETACH PARTITION any_name;
3265+
ERROR: "regular_table" is not partitioned
3266+
DROP TABLE regular_table;
32623267
-- check that the partition being detached exists at all
32633268
ALTER TABLE list_parted2 DETACH PARTITION part_4;
32643269
ERROR: relation "part_4" does not exist

src/test/regress/sql/alter_table.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,6 +2139,11 @@ ALTER TABLE list_parted2 ATTACH PARTITION list_parted2 FOR VALUES IN (0);
21392139
-- DETACH PARTITION
21402140
--
21412141

2142+
-- check that the table is partitioned at all
2143+
CREATE TABLE regular_table (a int);
2144+
ALTER TABLE regular_table DETACH PARTITION any_name;
2145+
DROP TABLE regular_table;
2146+
21422147
-- check that the partition being detached exists at all
21432148
ALTER TABLE list_parted2 DETACH PARTITION part_4;
21442149

0 commit comments

Comments
 (0)