Skip to content

Commit b242e1d

Browse files
committed
Fix unexpected error messages for various flavors of ALTER TABLE
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
1 parent 645c5d1 commit b242e1d

File tree

5 files changed

+19
-0
lines changed

5 files changed

+19
-0
lines changed

src/backend/commands/tablecmds.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5372,6 +5372,9 @@ ATWrongRelkindError(Relation rel, int allowed_targets)
53725372
case ATT_TABLE | ATT_MATVIEW | ATT_INDEX:
53735373
msg = _("\"%s\" is not a table, materialized view, or index");
53745374
break;
5375+
case ATT_TABLE | ATT_MATVIEW | ATT_INDEX | ATT_PARTITIONED_INDEX:
5376+
msg = _("\"%s\" is not a table, materialized view, index, or partitioned index");
5377+
break;
53755378
case ATT_TABLE | ATT_MATVIEW | ATT_FOREIGN_TABLE:
53765379
msg = _("\"%s\" is not a table, materialized view, or foreign table");
53775380
break;
@@ -5384,6 +5387,9 @@ ATWrongRelkindError(Relation rel, int allowed_targets)
53845387
case ATT_TABLE | ATT_MATVIEW | ATT_INDEX | ATT_FOREIGN_TABLE:
53855388
msg = _("\"%s\" is not a table, materialized view, index, or foreign table");
53865389
break;
5390+
case ATT_TABLE | ATT_PARTITIONED_INDEX:
5391+
msg = _("\"%s\" is not a table or partitioned index");
5392+
break;
53875393
case ATT_VIEW:
53885394
msg = _("\"%s\" is not a view");
53895395
break;

src/test/regress/expected/alter_table.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3948,6 +3948,11 @@ ERROR: remainder for hash partition must be less than modulus
39483948
ALTER TABLE hash_parted ATTACH PARTITION fail_part FOR VALUES WITH (MODULUS 3, REMAINDER 2);
39493949
ERROR: every hash partition modulus must be a factor of the next larger modulus
39503950
DROP TABLE fail_part;
3951+
-- fails with incorrect object type
3952+
CREATE VIEW at_v1 AS SELECT 1 as a;
3953+
ALTER TABLE at_v1 ATTACH PARTITION dummy default;
3954+
ERROR: "at_v1" is not a partitioned table or index
3955+
DROP VIEW at_v1;
39513956
--
39523957
-- DETACH PARTITION
39533958
--

src/test/regress/expected/foreign_data.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,8 @@ ALTER FOREIGN TABLE ft1 DROP COLUMN c9;
880880
ALTER FOREIGN TABLE ft1 SET SCHEMA foreign_schema;
881881
ALTER FOREIGN TABLE ft1 SET TABLESPACE ts; -- ERROR
882882
ERROR: relation "ft1" does not exist
883+
ALTER FOREIGN TABLE foreign_schema.ft1 SET TABLESPACE ts; -- ERROR
884+
ERROR: "ft1" is not a table, materialized view, index, or partitioned index
883885
ALTER FOREIGN TABLE foreign_schema.ft1 RENAME c1 TO foreign_column_1;
884886
ALTER FOREIGN TABLE foreign_schema.ft1 RENAME TO foreign_table_1;
885887
\d foreign_schema.foreign_table_1

src/test/regress/sql/alter_table.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,6 +2593,11 @@ ALTER TABLE hash_parted ATTACH PARTITION fail_part FOR VALUES WITH (MODULUS 8, R
25932593
ALTER TABLE hash_parted ATTACH PARTITION fail_part FOR VALUES WITH (MODULUS 3, REMAINDER 2);
25942594
DROP TABLE fail_part;
25952595

2596+
-- fails with incorrect object type
2597+
CREATE VIEW at_v1 AS SELECT 1 as a;
2598+
ALTER TABLE at_v1 ATTACH PARTITION dummy default;
2599+
DROP VIEW at_v1;
2600+
25962601
--
25972602
-- DETACH PARTITION
25982603
--

src/test/regress/sql/foreign_data.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ ALTER FOREIGN TABLE ft1 DROP COLUMN IF EXISTS no_column;
408408
ALTER FOREIGN TABLE ft1 DROP COLUMN c9;
409409
ALTER FOREIGN TABLE ft1 SET SCHEMA foreign_schema;
410410
ALTER FOREIGN TABLE ft1 SET TABLESPACE ts; -- ERROR
411+
ALTER FOREIGN TABLE foreign_schema.ft1 SET TABLESPACE ts; -- ERROR
411412
ALTER FOREIGN TABLE foreign_schema.ft1 RENAME c1 TO foreign_column_1;
412413
ALTER FOREIGN TABLE foreign_schema.ft1 RENAME TO foreign_table_1;
413414
\d foreign_schema.foreign_table_1

0 commit comments

Comments
 (0)