From 3f091e3493f949c818556da30d8708d3279e6796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrkan=20=C4=B0ndibay?= Date: Mon, 12 Dec 2022 23:56:22 +0300 Subject: [PATCH] Give nicer error message when using alter_table_set_access_method on a view (#6553) DESCRIPTION: Fixes alter_table_set_access_method error for views. Fixes #6001 --- src/backend/distributed/commands/alter_table.c | 5 +++++ .../expected/alter_table_set_access_method.out | 17 +++++++++++++++-- .../sql/alter_table_set_access_method.sql | 13 +++++++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/backend/distributed/commands/alter_table.c b/src/backend/distributed/commands/alter_table.c index b30dcccf7..48f7977ba 100644 --- a/src/backend/distributed/commands/alter_table.c +++ b/src/backend/distributed/commands/alter_table.c @@ -477,6 +477,11 @@ AlterTableSetAccessMethod(TableConversionParameters *params) ereport(ERROR, (errmsg("you cannot alter access method of a partitioned table"))); } + if (get_rel_relkind(params->relationId) == RELKIND_VIEW) + { + ereport(ERROR, (errmsg("you cannot alter access method of a view"))); + } + if (PartitionTable(params->relationId) && IsCitusTableType(params->relationId, DISTRIBUTED_TABLE)) { diff --git a/src/test/regress/expected/alter_table_set_access_method.out b/src/test/regress/expected/alter_table_set_access_method.out index 5ff345c66..87a423e78 100644 --- a/src/test/regress/expected/alter_table_set_access_method.out +++ b/src/test/regress/expected/alter_table_set_access_method.out @@ -299,7 +299,7 @@ CREATE INDEX idx1 ON index_table (a); -- also create an index with statistics CREATE INDEX idx2 ON index_table ((a+1)); ALTER INDEX idx2 ALTER COLUMN 1 SET STATISTICS 300; -SELECT indexname FROM pg_indexes WHERE schemaname = 'alter_table_set_access_method' AND tablename = 'index_table'; +SELECT indexname FROM pg_indexes WHERE schemaname = 'alter_table_set_access_method' AND tablename = 'index_table' order by indexname; indexname --------------------------------------------------------------------- idx1 @@ -322,7 +322,7 @@ NOTICE: renaming the new table to alter_table_set_access_method.index_table (1 row) -SELECT indexname FROM pg_indexes WHERE schemaname = 'alter_table_set_access_method' AND tablename = 'index_table'; +SELECT indexname FROM pg_indexes WHERE schemaname = 'alter_table_set_access_method' AND tablename = 'index_table' order by indexname; indexname --------------------------------------------------------------------- idx1 @@ -792,6 +792,19 @@ NOTICE: renaming the new table to alter_table_set_access_method.events (1 row) COMMIT; +--create the view to test alter table set access method on it +CREATE TABLE view_test_table (id int, val int, flag bool, kind int); +SELECT create_distributed_table('view_test_table','id'); + create_distributed_table +--------------------------------------------------------------------- + +(1 row) + +INSERT INTO view_test_table VALUES (1, 1, true, 99), (2, 2, false, 99), (2, 3, true, 88); +CREATE VIEW view_test_view AS SELECT * FROM view_test_table; +-- error out when attempting to set access method of a view. +select alter_table_set_access_method('view_test_view','columnar'); +ERROR: you cannot alter access method of a view SET client_min_messages TO WARNING; DROP SCHEMA alter_table_set_access_method CASCADE; SELECT 1 FROM master_remove_node('localhost', :master_port); diff --git a/src/test/regress/sql/alter_table_set_access_method.sql b/src/test/regress/sql/alter_table_set_access_method.sql index eb260210c..68e8237c5 100644 --- a/src/test/regress/sql/alter_table_set_access_method.sql +++ b/src/test/regress/sql/alter_table_set_access_method.sql @@ -99,10 +99,10 @@ CREATE INDEX idx1 ON index_table (a); -- also create an index with statistics CREATE INDEX idx2 ON index_table ((a+1)); ALTER INDEX idx2 ALTER COLUMN 1 SET STATISTICS 300; -SELECT indexname FROM pg_indexes WHERE schemaname = 'alter_table_set_access_method' AND tablename = 'index_table'; +SELECT indexname FROM pg_indexes WHERE schemaname = 'alter_table_set_access_method' AND tablename = 'index_table' order by indexname; SELECT a.amname FROM pg_class c, pg_am a where c.relname = 'index_table' AND c.relnamespace = 'alter_table_set_access_method'::regnamespace AND c.relam = a.oid; SELECT alter_table_set_access_method('index_table', 'columnar'); -SELECT indexname FROM pg_indexes WHERE schemaname = 'alter_table_set_access_method' AND tablename = 'index_table'; +SELECT indexname FROM pg_indexes WHERE schemaname = 'alter_table_set_access_method' AND tablename = 'index_table' order by indexname; SELECT a.amname FROM pg_class c, pg_am a where c.relname = 'index_table' AND c.relnamespace = 'alter_table_set_access_method'::regnamespace AND c.relam = a.oid; CREATE TABLE "heap_\'tbl" ( @@ -271,6 +271,15 @@ BEGIN; select alter_table_set_access_method('events', 'columnar'); COMMIT; +--create the view to test alter table set access method on it +CREATE TABLE view_test_table (id int, val int, flag bool, kind int); +SELECT create_distributed_table('view_test_table','id'); +INSERT INTO view_test_table VALUES (1, 1, true, 99), (2, 2, false, 99), (2, 3, true, 88); +CREATE VIEW view_test_view AS SELECT * FROM view_test_table; + +-- error out when attempting to set access method of a view. +select alter_table_set_access_method('view_test_view','columnar'); + SET client_min_messages TO WARNING; DROP SCHEMA alter_table_set_access_method CASCADE; SELECT 1 FROM master_remove_node('localhost', :master_port);