Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Fix unexpected error messages for various flavors of ALTER TABLE
authorMichael Paquier <michael@paquier.xyz>
Wed, 14 Jul 2021 08:15:31 +0000 (17:15 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 14 Jul 2021 08:15:31 +0000 (17:15 +0900)
Some commands of ALTER TABLE could fail with the following error:
ERROR:  "tab" is of the wrong type

This error is unexpected, as all the code paths leading to
ATWrongRelkindError() should use a supported set of relkinds to generate
correct error messages.  This commit closes the gap with such mistakes,
by adding all the missing relkind combinations.  Tests are added to
check all the problems found.  Note that some combinations are not used,
but these are left around as it could have an impact on applications
relying on this code.

2ed532e has done a much larger refactoring on HEAD to make such error
messages easier to manage in the long-term, so nothing is needed there.

Author: Kyotaro Horiguchi
Reviewed-by: Peter Eisentraut, Ahsan Hadi, Michael Paquier
Discussion: https://postgr.es/m/20210216.181415.368926598204753659.horikyota.ntt@gmail.com
Backpatch-through: 11

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

index 8ef23f6442262c959e460efb810f5e470d2c9918..73d7935d5b079794b91d52714d4e3113e58d0774 100644 (file)
@@ -5082,6 +5082,9 @@ ATWrongRelkindError(Relation rel, int allowed_targets)
        case ATT_TABLE | ATT_MATVIEW | ATT_INDEX:
            msg = _("\"%s\" is not a table, materialized view, or index");
            break;
+       case ATT_TABLE | ATT_MATVIEW | ATT_INDEX | ATT_PARTITIONED_INDEX:
+           msg = _("\"%s\" is not a table, materialized view, index, or partitioned index");
+           break;
        case ATT_TABLE | ATT_MATVIEW | ATT_FOREIGN_TABLE:
            msg = _("\"%s\" is not a table, materialized view, or foreign table");
            break;
@@ -5094,6 +5097,9 @@ ATWrongRelkindError(Relation rel, int allowed_targets)
        case ATT_TABLE | ATT_MATVIEW | ATT_INDEX | ATT_FOREIGN_TABLE:
            msg = _("\"%s\" is not a table, materialized view, index, or foreign table");
            break;
+       case ATT_TABLE | ATT_PARTITIONED_INDEX:
+           msg = _("\"%s\" is not a table or partitioned index");
+           break;
        case ATT_VIEW:
            msg = _("\"%s\" is not a view");
            break;
index f925eafebf3faabfb7e547886eebdc15ba1eec73..9709202ec735b4e601984f2455d579a8b148f68a 100644 (file)
@@ -3974,6 +3974,11 @@ ERROR:  remainder for hash partition must be less than modulus
 ALTER TABLE hash_parted ATTACH PARTITION fail_part FOR VALUES WITH (MODULUS 3, REMAINDER 2);
 ERROR:  every hash partition modulus must be a factor of the next larger modulus
 DROP TABLE fail_part;
+-- fails with incorrect object type
+CREATE VIEW at_v1 AS SELECT 1 as a;
+ALTER TABLE at_v1 ATTACH PARTITION dummy default;
+ERROR:  "at_v1" is not a partitioned table or index
+DROP VIEW at_v1;
 --
 -- DETACH PARTITION
 --
index d613d8e09167378b3928020db03713301ac589e1..b391c63801d7c50bef282eb3bf6c2766aac19ae3 100644 (file)
@@ -885,6 +885,8 @@ ALTER FOREIGN TABLE ft1 DROP COLUMN c9;
 ALTER FOREIGN TABLE ft1 SET SCHEMA foreign_schema;
 ALTER FOREIGN TABLE ft1 SET TABLESPACE ts;                      -- ERROR
 ERROR:  relation "ft1" does not exist
+ALTER FOREIGN TABLE foreign_schema.ft1 SET TABLESPACE ts;       -- ERROR
+ERROR:  "ft1" is not a table, materialized view, index, or partitioned index
 ALTER FOREIGN TABLE foreign_schema.ft1 RENAME c1 TO foreign_column_1;
 ALTER FOREIGN TABLE foreign_schema.ft1 RENAME TO foreign_table_1;
 \d foreign_schema.foreign_table_1
index 1978248dc5db007f96ce14498859883b13f971ec..8e30947666c9fef7bd2ff5dbfe1ffe0323fac3f9 100644 (file)
@@ -2581,6 +2581,11 @@ ALTER TABLE hash_parted ATTACH PARTITION fail_part FOR VALUES WITH (MODULUS 8, R
 ALTER TABLE hash_parted ATTACH PARTITION fail_part FOR VALUES WITH (MODULUS 3, REMAINDER 2);
 DROP TABLE fail_part;
 
+-- fails with incorrect object type
+CREATE VIEW at_v1 AS SELECT 1 as a;
+ALTER TABLE at_v1 ATTACH PARTITION dummy default;
+DROP VIEW at_v1;
+
 --
 -- DETACH PARTITION
 --
index 0c36208e4cdc754a8b8f0877223f35076484d5e0..3ac53fba06db2e2d6dde8ea5c0aebe2bb059e1bb 100644 (file)
@@ -410,6 +410,7 @@ ALTER FOREIGN TABLE ft1 DROP COLUMN IF EXISTS no_column;
 ALTER FOREIGN TABLE ft1 DROP COLUMN c9;
 ALTER FOREIGN TABLE ft1 SET SCHEMA foreign_schema;
 ALTER FOREIGN TABLE ft1 SET TABLESPACE ts;                      -- ERROR
+ALTER FOREIGN TABLE foreign_schema.ft1 SET TABLESPACE ts;       -- ERROR
 ALTER FOREIGN TABLE foreign_schema.ft1 RENAME c1 TO foreign_column_1;
 ALTER FOREIGN TABLE foreign_schema.ft1 RENAME TO foreign_table_1;
 \d foreign_schema.foreign_table_1