Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5940730

Browse files
committed
Avoid crash in ALTER TABLE not_partitioned DETACH PARTITION.
Amit Langote, reviewed and slightly changed by me.
1 parent93e6e40 commit5940730

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
staticvoidtransformColumnType(CreateStmtContext*cxt,ColumnDef*column);
135135
staticvoidsetSchemaName(char*context_schema,char**stmt_schema_name);
136-
staticvoidtransformAttachPartition(CreateStmtContext*cxt,PartitionCmd*cmd);
136+
staticvoidtransformPartitionCmd(CreateStmtContext*cxt,PartitionCmd*cmd);
137137

138138

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

26562656
caseAT_AttachPartition:
2657+
caseAT_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
staticvoid
3039-
transformAttachPartition(CreateStmtContext*cxt,PartitionCmd*cmd)
3042+
transformPartitionCmd(CreateStmtContext*cxt,PartitionCmd*cmd)
30403043
{
30413044
RelationparentRel=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 thevalues */
3053+
/* transform thepartition 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+
CREATETABLEregular_table (aint);
2144+
ALTERTABLE regular_table DETACH PARTITION any_name;
2145+
DROPTABLE regular_table;
2146+
21422147
-- check that the partition being detached exists at all
21432148
ALTERTABLE list_parted2 DETACH PARTITION part_4;
21442149

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp