Skip to content

Commit bbba59e

Browse files
committed
Remove ATT_TABLE for ALTER TABLE ... ATTACH/DETACH
Attempting these commands for a non-partitioned table would result in a failure when creating the relation in transformPartitionCmd(). This gives the possibility to throw an error earlier with a much better error message, thanks to d69a3f4. The extra test cases are from me. Note that FINALIZE uses a different subcommand and it had no coverage for its failure path with non-partitioned tables. Author: Álvaro Herrera, Michael Paquier Reviewed-by: Nathan Bossart Discussion: https://postgr.es/m/202409190803.tnis52adt2n5@alvherre.pgsql
1 parent 75240f6 commit bbba59e

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/backend/commands/tablecmds.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5107,19 +5107,17 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
51075107
break;
51085108
case AT_AttachPartition:
51095109
ATSimplePermissions(cmd->subtype, rel,
5110-
ATT_TABLE | ATT_PARTITIONED_TABLE | ATT_PARTITIONED_INDEX);
5110+
ATT_PARTITIONED_TABLE | ATT_PARTITIONED_INDEX);
51115111
/* No command-specific prep needed */
51125112
pass = AT_PASS_MISC;
51135113
break;
51145114
case AT_DetachPartition:
5115-
ATSimplePermissions(cmd->subtype, rel,
5116-
ATT_TABLE | ATT_PARTITIONED_TABLE);
5115+
ATSimplePermissions(cmd->subtype, rel, ATT_PARTITIONED_TABLE);
51175116
/* No command-specific prep needed */
51185117
pass = AT_PASS_MISC;
51195118
break;
51205119
case AT_DetachPartitionFinalize:
5121-
ATSimplePermissions(cmd->subtype, rel,
5122-
ATT_TABLE | ATT_PARTITIONED_TABLE);
5120+
ATSimplePermissions(cmd->subtype, rel, ATT_PARTITIONED_TABLE);
51235121
/* No command-specific prep needed */
51245122
pass = AT_PASS_MISC;
51255123
break;

src/test/regress/expected/alter_table.out

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3911,7 +3911,8 @@ CREATE TABLE unparted (
39113911
);
39123912
CREATE TABLE fail_part (like unparted);
39133913
ALTER TABLE unparted ATTACH PARTITION fail_part FOR VALUES IN ('a');
3914-
ERROR: table "unparted" is not partitioned
3914+
ERROR: ALTER action ATTACH PARTITION cannot be performed on relation "unparted"
3915+
DETAIL: This operation is not supported for tables.
39153916
DROP TABLE unparted, fail_part;
39163917
-- check that partition bound is compatible
39173918
CREATE TABLE list_parted (
@@ -4281,7 +4282,14 @@ DROP TABLE fail_part;
42814282
-- check that the table is partitioned at all
42824283
CREATE TABLE regular_table (a int);
42834284
ALTER TABLE regular_table DETACH PARTITION any_name;
4284-
ERROR: table "regular_table" is not partitioned
4285+
ERROR: ALTER action DETACH PARTITION cannot be performed on relation "regular_table"
4286+
DETAIL: This operation is not supported for tables.
4287+
ALTER TABLE regular_table DETACH PARTITION any_name CONCURRENTLY;
4288+
ERROR: ALTER action DETACH PARTITION cannot be performed on relation "regular_table"
4289+
DETAIL: This operation is not supported for tables.
4290+
ALTER TABLE regular_table DETACH PARTITION any_name FINALIZE;
4291+
ERROR: ALTER action DETACH PARTITION ... FINALIZE cannot be performed on relation "regular_table"
4292+
DETAIL: This operation is not supported for tables.
42854293
DROP TABLE regular_table;
42864294
-- check that the partition being detached exists at all
42874295
ALTER TABLE list_parted2 DETACH PARTITION part_4;

src/test/regress/sql/alter_table.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2742,6 +2742,8 @@ DROP TABLE fail_part;
27422742
-- check that the table is partitioned at all
27432743
CREATE TABLE regular_table (a int);
27442744
ALTER TABLE regular_table DETACH PARTITION any_name;
2745+
ALTER TABLE regular_table DETACH PARTITION any_name CONCURRENTLY;
2746+
ALTER TABLE regular_table DETACH PARTITION any_name FINALIZE;
27452747
DROP TABLE regular_table;
27462748

27472749
-- check that the partition being detached exists at all

0 commit comments

Comments
 (0)