citus/src/test/regress/expected/alter_table_set_access_meth...

462 lines
20 KiB
Plaintext

SHOW server_version \gset
SELECT substring(:'server_version', '\d+')::int > 11 AS server_version_above_eleven
\gset
\if :server_version_above_eleven
\else
\q
\endif
CREATE SCHEMA alter_table_set_access_method;
SET search_path TO alter_table_set_access_method;
SET citus.shard_count TO 4;
SET citus.shard_replication_factor TO 1;
SELECT public.run_command_on_coordinator_and_workers($Q$
CREATE FUNCTION fake_am_handler(internal)
RETURNS table_am_handler
AS 'citus'
LANGUAGE C;
CREATE ACCESS METHOD fake_am TYPE TABLE HANDLER fake_am_handler;
$Q$);
run_command_on_coordinator_and_workers
---------------------------------------------------------------------
(1 row)
CREATE TABLE dist_table (a INT, b INT);
SELECT create_distributed_table ('dist_table', 'a');
create_distributed_table
---------------------------------------------------------------------
(1 row)
INSERT INTO dist_table VALUES (1, 1), (2, 2), (3, 3);
SELECT "Name", "Access Method" FROM public.citus_tables WHERE "Name"::text = 'dist_table' ORDER BY 1;
Name | Access Method
---------------------------------------------------------------------
dist_table | heap
(1 row)
SELECT alter_table_set_access_method('dist_table', 'columnar');
NOTICE: any index will be dropped, because columnar tables cannot have indexes
NOTICE: creating a new table for alter_table_set_access_method.dist_table
NOTICE: Moving the data of alter_table_set_access_method.dist_table
NOTICE: Dropping the old alter_table_set_access_method.dist_table
NOTICE: Renaming the new table to alter_table_set_access_method.dist_table
alter_table_set_access_method
---------------------------------------------------------------------
(1 row)
SELECT "Name", "Access Method" FROM public.citus_tables WHERE "Name"::text = 'dist_table' ORDER BY 1;
Name | Access Method
---------------------------------------------------------------------
dist_table | columnar
(1 row)
-- test partitions
CREATE TABLE partitioned_table (id INT, a INT) PARTITION BY RANGE (id);
CREATE TABLE partitioned_table_1_5 PARTITION OF partitioned_table FOR VALUES FROM (1) TO (5);
CREATE TABLE partitioned_table_6_10 PARTITION OF partitioned_table FOR VALUES FROM (6) TO (10);
SELECT create_distributed_table('partitioned_table', 'id');
create_distributed_table
---------------------------------------------------------------------
(1 row)
INSERT INTO partitioned_table VALUES (2, 12), (7, 2);
SELECT logicalrelid::text FROM pg_dist_partition WHERE logicalrelid::regclass::text LIKE 'partitioned\_table%' ORDER BY 1;
logicalrelid
---------------------------------------------------------------------
partitioned_table
partitioned_table_1_5
partitioned_table_6_10
(3 rows)
SELECT run_command_on_workers($$SELECT COUNT(*) FROM pg_catalog.pg_class WHERE relname LIKE 'partitioned\_table%'$$);
run_command_on_workers
---------------------------------------------------------------------
(localhost,57637,t,6)
(localhost,57638,t,6)
(2 rows)
SELECT inhrelid::regclass::text FROM pg_catalog.pg_inherits WHERE inhparent = 'partitioned_table'::regclass ORDER BY 1;
inhrelid
---------------------------------------------------------------------
partitioned_table_1_5
partitioned_table_6_10
(2 rows)
SELECT "Name"::text, "Access Method" FROM public.citus_tables WHERE "Name"::text LIKE 'partitioned\_table%' ORDER BY 1;
Name | Access Method
---------------------------------------------------------------------
partitioned_table |
partitioned_table_1_5 | heap
partitioned_table_6_10 | heap
(3 rows)
SELECT * FROM partitioned_table ORDER BY 1, 2;
id | a
---------------------------------------------------------------------
2 | 12
7 | 2
(2 rows)
SELECT * FROM partitioned_table_1_5 ORDER BY 1, 2;
id | a
---------------------------------------------------------------------
2 | 12
(1 row)
SELECT * FROM partitioned_table_6_10 ORDER BY 1, 2;
id | a
---------------------------------------------------------------------
7 | 2
(1 row)
-- altering partitioned tables' access methods is not supported
SELECT alter_table_set_access_method('partitioned_table', 'columnar');
ERROR: you cannot alter access method of a partitioned table
-- test altering the partition's access method
SELECT alter_table_set_access_method('partitioned_table_1_5', 'columnar');
NOTICE: any index will be dropped, because columnar tables cannot have indexes
NOTICE: creating a new table for alter_table_set_access_method.partitioned_table_1_5
NOTICE: Moving the data of alter_table_set_access_method.partitioned_table_1_5
NOTICE: Dropping the old alter_table_set_access_method.partitioned_table_1_5
NOTICE: Renaming the new table to alter_table_set_access_method.partitioned_table_1_5
alter_table_set_access_method
---------------------------------------------------------------------
(1 row)
SELECT logicalrelid::text FROM pg_dist_partition WHERE logicalrelid::regclass::text LIKE 'partitioned\_table%' ORDER BY 1;
logicalrelid
---------------------------------------------------------------------
partitioned_table
partitioned_table_1_5
partitioned_table_6_10
(3 rows)
SELECT run_command_on_workers($$SELECT COUNT(*) FROM pg_catalog.pg_class WHERE relname LIKE 'partitioned\_table%'$$);
run_command_on_workers
---------------------------------------------------------------------
(localhost,57637,t,6)
(localhost,57638,t,6)
(2 rows)
SELECT inhrelid::regclass::text FROM pg_catalog.pg_inherits WHERE inhparent = 'partitioned_table'::regclass ORDER BY 1;
inhrelid
---------------------------------------------------------------------
partitioned_table_1_5
partitioned_table_6_10
(2 rows)
SELECT "Name"::text, "Access Method" FROM public.citus_tables WHERE "Name"::text LIKE 'partitioned\_table%' ORDER BY 1;
Name | Access Method
---------------------------------------------------------------------
partitioned_table |
partitioned_table_1_5 | columnar
partitioned_table_6_10 | heap
(3 rows)
SELECT * FROM partitioned_table ORDER BY 1, 2;
id | a
---------------------------------------------------------------------
2 | 12
7 | 2
(2 rows)
SELECT * FROM partitioned_table_1_5 ORDER BY 1, 2;
id | a
---------------------------------------------------------------------
2 | 12
(1 row)
SELECT * FROM partitioned_table_6_10 ORDER BY 1, 2;
id | a
---------------------------------------------------------------------
7 | 2
(1 row)
-- try to compress partitions with an integer partition column
CALL alter_old_partitions_set_access_method('partitioned_table', '2021-01-01', 'columnar');
ERROR: partition column of partitioned_table cannot be cast to a timestamptz
CONTEXT: PL/pgSQL function alter_old_partitions_set_access_method(regclass,timestamp with time zone,name) line 13 at RAISE
CREATE TABLE time_partitioned (event_time timestamp, event int) partition by range (event_time);
SELECT create_distributed_table('time_partitioned', 'event_time');
create_distributed_table
---------------------------------------------------------------------
(1 row)
CREATE TABLE time_partitioned_d00 PARTITION OF time_partitioned FOR VALUES FROM ('2000-01-01') TO ('2009-12-31');
CREATE TABLE time_partitioned_d10 PARTITION OF time_partitioned FOR VALUES FROM ('2010-01-01') TO ('2019-12-31');
CREATE TABLE time_partitioned_d20 PARTITION OF time_partitioned FOR VALUES FROM ('2020-01-01') TO ('2029-12-31');
INSERT INTO time_partitioned VALUES ('2005-01-01', 1);
INSERT INTO time_partitioned VALUES ('2015-01-01', 2);
INSERT INTO time_partitioned VALUES ('2025-01-01', 3);
\set VERBOSITY terse
-- compress no partitions
CALL alter_old_partitions_set_access_method('time_partitioned', '1999-01-01', 'columnar');
SELECT partition, access_method FROM time_partitions WHERE parent_table = 'time_partitioned'::regclass ORDER BY partition::text;
partition | access_method
---------------------------------------------------------------------
time_partitioned_d00 | heap
time_partitioned_d10 | heap
time_partitioned_d20 | heap
(3 rows)
SELECT event FROM time_partitioned ORDER BY 1;
event
---------------------------------------------------------------------
1
2
3
(3 rows)
-- compress 2 old partitions
CALL alter_old_partitions_set_access_method('time_partitioned', '2021-01-01', 'columnar');
NOTICE: converting time_partitioned_d00 with start time Sat Jan 01 00:00:00 2000 and end time Thu Dec 31 00:00:00 2009
NOTICE: any index will be dropped, because columnar tables cannot have indexes
NOTICE: creating a new table for alter_table_set_access_method.time_partitioned_d00
NOTICE: Moving the data of alter_table_set_access_method.time_partitioned_d00
NOTICE: Dropping the old alter_table_set_access_method.time_partitioned_d00
NOTICE: Renaming the new table to alter_table_set_access_method.time_partitioned_d00
NOTICE: converting time_partitioned_d10 with start time Fri Jan 01 00:00:00 2010 and end time Tue Dec 31 00:00:00 2019
NOTICE: any index will be dropped, because columnar tables cannot have indexes
NOTICE: creating a new table for alter_table_set_access_method.time_partitioned_d10
NOTICE: Moving the data of alter_table_set_access_method.time_partitioned_d10
NOTICE: Dropping the old alter_table_set_access_method.time_partitioned_d10
NOTICE: Renaming the new table to alter_table_set_access_method.time_partitioned_d10
SELECT partition, access_method FROM time_partitions WHERE parent_table = 'time_partitioned'::regclass ORDER BY partition::text;
partition | access_method
---------------------------------------------------------------------
time_partitioned_d00 | columnar
time_partitioned_d10 | columnar
time_partitioned_d20 | heap
(3 rows)
SELECT event FROM time_partitioned ORDER BY 1;
event
---------------------------------------------------------------------
1
2
3
(3 rows)
-- will not be compressed again
CALL alter_old_partitions_set_access_method('time_partitioned', '2021-01-01', 'columnar');
SELECT partition, access_method FROM time_partitions WHERE parent_table = 'time_partitioned'::regclass ORDER BY partition::text;
partition | access_method
---------------------------------------------------------------------
time_partitioned_d00 | columnar
time_partitioned_d10 | columnar
time_partitioned_d20 | heap
(3 rows)
SELECT event FROM time_partitioned ORDER BY 1;
event
---------------------------------------------------------------------
1
2
3
(3 rows)
-- back to heap
CALL alter_old_partitions_set_access_method('time_partitioned', '2021-01-01', 'heap');
NOTICE: converting time_partitioned_d00 with start time Sat Jan 01 00:00:00 2000 and end time Thu Dec 31 00:00:00 2009
NOTICE: creating a new table for alter_table_set_access_method.time_partitioned_d00
NOTICE: Moving the data of alter_table_set_access_method.time_partitioned_d00
NOTICE: Dropping the old alter_table_set_access_method.time_partitioned_d00
NOTICE: Renaming the new table to alter_table_set_access_method.time_partitioned_d00
NOTICE: converting time_partitioned_d10 with start time Fri Jan 01 00:00:00 2010 and end time Tue Dec 31 00:00:00 2019
NOTICE: creating a new table for alter_table_set_access_method.time_partitioned_d10
NOTICE: Moving the data of alter_table_set_access_method.time_partitioned_d10
NOTICE: Dropping the old alter_table_set_access_method.time_partitioned_d10
NOTICE: Renaming the new table to alter_table_set_access_method.time_partitioned_d10
SELECT partition, access_method FROM time_partitions WHERE parent_table = 'time_partitioned'::regclass ORDER BY partition::text;
partition | access_method
---------------------------------------------------------------------
time_partitioned_d00 | heap
time_partitioned_d10 | heap
time_partitioned_d20 | heap
(3 rows)
SELECT event FROM time_partitioned ORDER BY 1;
event
---------------------------------------------------------------------
1
2
3
(3 rows)
\set VERBOSITY default
DROP TABLE time_partitioned;
-- test altering a table with index to columnar
-- the index will be dropped
CREATE TABLE index_table (a INT) ;
CREATE INDEX idx1 ON index_table (a);
SELECT indexname FROM pg_indexes WHERE schemaname = 'alter_table_set_access_method' AND tablename = 'index_table';
indexname
---------------------------------------------------------------------
idx1
(1 row)
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;
amname
---------------------------------------------------------------------
heap
(1 row)
SELECT alter_table_set_access_method('index_table', 'columnar');
NOTICE: any index will be dropped, because columnar tables cannot have indexes
NOTICE: creating a new table for alter_table_set_access_method.index_table
NOTICE: Moving the data of alter_table_set_access_method.index_table
NOTICE: Dropping the old alter_table_set_access_method.index_table
NOTICE: Renaming the new table to alter_table_set_access_method.index_table
alter_table_set_access_method
---------------------------------------------------------------------
(1 row)
SELECT indexname FROM pg_indexes WHERE schemaname = 'alter_table_set_access_method' AND tablename = 'index_table';
indexname
---------------------------------------------------------------------
(0 rows)
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;
amname
---------------------------------------------------------------------
columnar
(1 row)
-- test different table types
SET client_min_messages to WARNING;
SELECT 1 FROM master_add_node('localhost', :master_port, groupId := 0);
?column?
---------------------------------------------------------------------
1
(1 row)
SET client_min_messages to DEFAULT;
CREATE TABLE table_type_dist (a INT);
SELECT create_distributed_table('table_type_dist', 'a');
create_distributed_table
---------------------------------------------------------------------
(1 row)
CREATE TABLE table_type_ref (a INT);
SELECT create_reference_table('table_type_ref');
create_reference_table
---------------------------------------------------------------------
(1 row)
CREATE TABLE table_type_citus_local(a INT);
SELECT create_citus_local_table('table_type_citus_local');
create_citus_local_table
---------------------------------------------------------------------
(1 row)
CREATE TABLE table_type_pg_local (a INT);
SELECT "Name", "Citus Table Type", "Distribution Column", "Shard Count", "Access Method" FROM public.citus_tables WHERE "Name"::text LIKE 'table\_type%' ORDER BY 1;
Name | Citus Table Type | Distribution Column | Shard Count | Access Method
---------------------------------------------------------------------
table_type_dist | distributed | a | 4 | heap
table_type_ref | reference | <none> | 1 | heap
(2 rows)
SELECT c.relname, a.amname FROM pg_class c, pg_am a where c.relname SIMILAR TO 'table_type\D*' AND c.relnamespace = 'alter_table_set_access_method'::regnamespace AND c.relam = a.oid;
relname | amname
---------------------------------------------------------------------
table_type_citus_local | heap
table_type_dist | heap
table_type_pg_local | heap
table_type_ref | heap
(4 rows)
SELECT alter_table_set_access_method('table_type_dist', 'fake_am');
NOTICE: creating a new table for alter_table_set_access_method.table_type_dist
WARNING: fake_scan_getnextslot
CONTEXT: SQL statement "SELECT EXISTS (SELECT 1 FROM alter_table_set_access_method.table_type_dist_1533505599)"
WARNING: fake_scan_getnextslot
NOTICE: Moving the data of alter_table_set_access_method.table_type_dist
NOTICE: Dropping the old alter_table_set_access_method.table_type_dist
NOTICE: Renaming the new table to alter_table_set_access_method.table_type_dist
alter_table_set_access_method
---------------------------------------------------------------------
(1 row)
SELECT alter_table_set_access_method('table_type_ref', 'fake_am');
NOTICE: creating a new table for alter_table_set_access_method.table_type_ref
WARNING: fake_scan_getnextslot
CONTEXT: SQL statement "SELECT EXISTS (SELECT 1 FROM alter_table_set_access_method.table_type_ref_1037855087)"
WARNING: fake_scan_getnextslot
NOTICE: Moving the data of alter_table_set_access_method.table_type_ref
NOTICE: Dropping the old alter_table_set_access_method.table_type_ref
NOTICE: Renaming the new table to alter_table_set_access_method.table_type_ref
alter_table_set_access_method
---------------------------------------------------------------------
(1 row)
SELECT alter_table_set_access_method('table_type_pg_local', 'fake_am');
NOTICE: creating a new table for alter_table_set_access_method.table_type_pg_local
NOTICE: Moving the data of alter_table_set_access_method.table_type_pg_local
NOTICE: Dropping the old alter_table_set_access_method.table_type_pg_local
NOTICE: Renaming the new table to alter_table_set_access_method.table_type_pg_local
alter_table_set_access_method
---------------------------------------------------------------------
(1 row)
SELECT alter_table_set_access_method('table_type_citus_local', 'fake_am');
NOTICE: creating a new table for alter_table_set_access_method.table_type_citus_local
NOTICE: Moving the data of alter_table_set_access_method.table_type_citus_local
NOTICE: Dropping the old alter_table_set_access_method.table_type_citus_local
NOTICE: Renaming the new table to alter_table_set_access_method.table_type_citus_local
alter_table_set_access_method
---------------------------------------------------------------------
(1 row)
SELECT "Name", "Citus Table Type", "Distribution Column", "Shard Count", "Access Method" FROM public.citus_tables WHERE "Name"::text LIKE 'table\_type%' ORDER BY 1;
Name | Citus Table Type | Distribution Column | Shard Count | Access Method
---------------------------------------------------------------------
table_type_dist | distributed | a | 4 | fake_am
table_type_ref | reference | <none> | 1 | fake_am
(2 rows)
SELECT c.relname, a.amname FROM pg_class c, pg_am a where c.relname SIMILAR TO 'table_type\D*' AND c.relnamespace = 'alter_table_set_access_method'::regnamespace AND c.relam = a.oid;
relname | amname
---------------------------------------------------------------------
table_type_citus_local | fake_am
table_type_dist | fake_am
table_type_pg_local | fake_am
table_type_ref | fake_am
(4 rows)
-- test when the parent of a partition has foreign key to a reference table
create table test_pk(n int primary key);
create table test_fk_p(i int references test_pk(n)) partition by range(i);
create table test_fk_p0 partition of test_fk_p for values from (0) to (10);
create table test_fk_p1 partition of test_fk_p for values from (10) to (20);
select alter_table_set_access_method('test_fk_p1', 'columnar');
NOTICE: any index will be dropped, because columnar tables cannot have indexes
NOTICE: creating a new table for alter_table_set_access_method.test_fk_p1
NOTICE: Moving the data of alter_table_set_access_method.test_fk_p1
NOTICE: Dropping the old alter_table_set_access_method.test_fk_p1
NOTICE: Renaming the new table to alter_table_set_access_method.test_fk_p1
ERROR: Foreign keys and AFTER ROW triggers are not supported for columnar tables
HINT: Consider an AFTER STATEMENT trigger instead.
CONTEXT: SQL statement "ALTER TABLE alter_table_set_access_method.test_fk_p ATTACH PARTITION alter_table_set_access_method.test_fk_p1 FOR VALUES FROM (10) TO (20);"
SET client_min_messages TO WARNING;
DROP SCHEMA alter_table_set_access_method CASCADE;
SELECT 1 FROM master_remove_node('localhost', :master_port);
?column?
---------------------------------------------------------------------
1
(1 row)