Remove ATT_TABLE for ALTER TABLE ... ATTACH/DETACH
authorMichael Paquier <[email protected]>
Mon, 23 Sep 2024 23:59:08 +0000 (08:59 +0900)
committerMichael Paquier <[email protected]>
Mon, 23 Sep 2024 23:59:08 +0000 (08:59 +0900)
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 d69a3f4d70b7.

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[email protected]

src/backend/commands/tablecmds.c
src/test/regress/expected/alter_table.out
src/test/regress/sql/alter_table.sql

index 2d703aa22e2685c03fb6a5ef43c0daf82524d3f3..d27e6cf3451a49bd907a42976310fbf1439140d8 100644 (file)
@@ -5107,19 +5107,17 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
            break;
        case AT_AttachPartition:
            ATSimplePermissions(cmd->subtype, rel,
-                               ATT_TABLE | ATT_PARTITIONED_TABLE | ATT_PARTITIONED_INDEX);
+                               ATT_PARTITIONED_TABLE | ATT_PARTITIONED_INDEX);
            /* No command-specific prep needed */
            pass = AT_PASS_MISC;
            break;
        case AT_DetachPartition:
-           ATSimplePermissions(cmd->subtype, rel,
-                               ATT_TABLE | ATT_PARTITIONED_TABLE);
+           ATSimplePermissions(cmd->subtype, rel, ATT_PARTITIONED_TABLE);
            /* No command-specific prep needed */
            pass = AT_PASS_MISC;
            break;
        case AT_DetachPartitionFinalize:
-           ATSimplePermissions(cmd->subtype, rel,
-                               ATT_TABLE | ATT_PARTITIONED_TABLE);
+           ATSimplePermissions(cmd->subtype, rel, ATT_PARTITIONED_TABLE);
            /* No command-specific prep needed */
            pass = AT_PASS_MISC;
            break;
index 79cf82b5aed1287f6511ab30df07522082186e9e..3b3b0738d79341aa6aaa984de31b224b39b06b4c 100644 (file)
@@ -3911,7 +3911,8 @@ CREATE TABLE unparted (
 );
 CREATE TABLE fail_part (like unparted);
 ALTER TABLE unparted ATTACH PARTITION fail_part FOR VALUES IN ('a');
-ERROR:  table "unparted" is not partitioned
+ERROR:  ALTER action ATTACH PARTITION cannot be performed on relation "unparted"
+DETAIL:  This operation is not supported for tables.
 DROP TABLE unparted, fail_part;
 -- check that partition bound is compatible
 CREATE TABLE list_parted (
@@ -4281,7 +4282,14 @@ DROP TABLE fail_part;
 -- check that the table is partitioned at all
 CREATE TABLE regular_table (a int);
 ALTER TABLE regular_table DETACH PARTITION any_name;
-ERROR:  table "regular_table" is not partitioned
+ERROR:  ALTER action DETACH PARTITION cannot be performed on relation "regular_table"
+DETAIL:  This operation is not supported for tables.
+ALTER TABLE regular_table DETACH PARTITION any_name CONCURRENTLY;
+ERROR:  ALTER action DETACH PARTITION cannot be performed on relation "regular_table"
+DETAIL:  This operation is not supported for tables.
+ALTER TABLE regular_table DETACH PARTITION any_name FINALIZE;
+ERROR:  ALTER action DETACH PARTITION ... FINALIZE cannot be performed on relation "regular_table"
+DETAIL:  This operation is not supported for tables.
 DROP TABLE regular_table;
 -- check that the partition being detached exists at all
 ALTER TABLE list_parted2 DETACH PARTITION part_4;
index 28cabc49e9fe02400dc65a6466b4cf8abb9a9dd3..453799abed48e0726ac83c6e06779edaf41cf442 100644 (file)
@@ -2742,6 +2742,8 @@ DROP TABLE fail_part;
 -- check that the table is partitioned at all
 CREATE TABLE regular_table (a int);
 ALTER TABLE regular_table DETACH PARTITION any_name;
+ALTER TABLE regular_table DETACH PARTITION any_name CONCURRENTLY;
+ALTER TABLE regular_table DETACH PARTITION any_name FINALIZE;
 DROP TABLE regular_table;
 
 -- check that the partition being detached exists at all