Fix unexpected error messages for various flavors of ALTER TABLE
authorMichael Paquier
Wed, 14 Jul 2021 08:15:18 +0000 (17:15 +0900)
committerMichael Paquier
Wed, 14 Jul 2021 08:15:18 +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[email protected]
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 135fa4698172241b53d4b254b95f1fa91a894386..1bf4ca68843c163919cd67f4d8849d36541413ab 100644 (file)
@@ -5649,6 +5649,12 @@ 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_INDEX | ATT_PARTITIONED_INDEX | ATT_FOREIGN_TABLE:
+           msg = _("\"%s\" is not a table, materialized view, index, partitioned index, or foreign table");
+           break;
        case ATT_TABLE | ATT_MATVIEW | ATT_FOREIGN_TABLE:
            msg = _("\"%s\" is not a table, materialized view, or foreign table");
            break;
@@ -5661,6 +5667,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 f56615393ec32780bb5c2b458a097b98fd246ecf..2e3f20756d265329503a7265b753d2c1dbf480b2 100644 (file)
@@ -120,6 +120,11 @@ ALTER INDEX attmp_idx ALTER COLUMN 4 SET STATISTICS 1000;
 ERROR:  column number 4 of relation "attmp_idx" does not exist
 ALTER INDEX attmp_idx ALTER COLUMN 2 SET STATISTICS -1;
 DROP TABLE attmp;
+-- fails with incorrect object type
+CREATE VIEW at_v1 AS SELECT 1 as a;
+ALTER TABLE at_v1 ALTER COLUMN a SET STATISTICS 0;
+ERROR:  "at_v1" is not a table, materialized view, index, partitioned index, or foreign table
+DROP VIEW at_v1;
 --
 -- rename - check on both non-temp and temp tables
 --
@@ -4100,6 +4105,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 table or partitioned index
+DROP VIEW at_v1;
 --
 -- DETACH PARTITION
 --
index b9e25820bc02f27566b59dece4f11a25e3e6904f..1376bfefa265d05e99fb49db38ad3bdc1712fe89 100644 (file)
@@ -880,6 +880,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 4cc55d852513e698ecb51586efad2c0e44f72e83..5a69b785de49a9c3bdcdb82c4bc31a62e037b20e 100644 (file)
@@ -158,6 +158,10 @@ ALTER INDEX attmp_idx ALTER COLUMN 2 SET STATISTICS -1;
 
 DROP TABLE attmp;
 
+-- fails with incorrect object type
+CREATE VIEW at_v1 AS SELECT 1 as a;
+ALTER TABLE at_v1 ALTER COLUMN a SET STATISTICS 0;
+DROP VIEW at_v1;
 
 --
 -- rename - check on both non-temp and temp tables
@@ -2640,6 +2644,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 73f9f621d8ff9d06dad556142ba86b2591848c5f..279786f4bfe7ed9f4ff56c2d4ee421e878465a81 100644 (file)
@@ -408,6 +408,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