diff --git a/src/test/regress/enterprise_split_schedule b/src/test/regress/enterprise_split_schedule index 6f216ea44..fd9788a42 100644 --- a/src/test/regress/enterprise_split_schedule +++ b/src/test/regress/enterprise_split_schedule @@ -7,3 +7,4 @@ test: tablespace # Split tests go here. test: citus_split_shard_by_split_points_negative test: citus_split_shard_by_split_points +test: citus_split_shard_by_split_points_deferred_drop diff --git a/src/test/regress/expected/citus_split_shard_by_split_points_deferred_drop.out b/src/test/regress/expected/citus_split_shard_by_split_points_deferred_drop.out new file mode 100644 index 000000000..f0cd227b9 --- /dev/null +++ b/src/test/regress/expected/citus_split_shard_by_split_points_deferred_drop.out @@ -0,0 +1,132 @@ +CREATE SCHEMA "citus_split_shard_by_split_points_deferred_schema"; +CREATE ROLE test_split_role WITH LOGIN; +GRANT USAGE, CREATE ON SCHEMA "citus_split_shard_by_split_points_deferred_schema" TO test_split_role; +SET ROLE test_split_role; +SET search_path TO "citus_split_shard_by_split_points_deferred_schema"; +-- Valide user cannot insert directly to pg_dist_cleanup table but can select from it. +CREATE TABLE temp_table (id INT); +INSERT INTO pg_catalog.pg_dist_cleanup (operation_id, object_type, object_name, node_group_id, policy_type) + VALUES (3134, 1, 'citus_split_shard_by_split_points_deferred_schema.temp_table', 1, 1); +ERROR: permission denied for table pg_dist_cleanup +SELECT * from pg_dist_cleanup; + record_id | operation_id | object_type | object_name | node_group_id | policy_type +--------------------------------------------------------------------- +(0 rows) + +-- Set a very long(10mins) time interval to stop auto cleanup in case of deferred drop. +\c - postgres - :master_port +ALTER SYSTEM SET citus.defer_shard_delete_interval TO 600000; +SELECT pg_reload_conf(); + pg_reload_conf +--------------------------------------------------------------------- + t +(1 row) + +-- Perform a split and validate shard is marked for deferred drop. +SET citus.next_shard_id TO 8981000; +SET citus.next_placement_id TO 8610000; +SET citus.shard_count TO 2; +SET citus.shard_replication_factor TO 1; +SET citus.next_operation_id TO 777; +SET citus.next_cleanup_record_id TO 11; +SET ROLE test_split_role; +CREATE TABLE table_to_split(id int PRIMARY KEY, int_data int, data text); +SELECT create_distributed_table('table_to_split', 'id'); + create_distributed_table +--------------------------------------------------------------------- + +(1 row) + +SELECT nodeid AS worker_1_node FROM pg_dist_node WHERE nodeport=:worker_1_port \gset +SELECT nodeid AS worker_2_node FROM pg_dist_node WHERE nodeport=:worker_2_port \gset +SET citus.next_shard_id TO 9999000; +SET citus.next_placement_id TO 5555000; +SELECT pg_catalog.citus_split_shard_by_split_points( + 8981000, + ARRAY['-100000'], + ARRAY[:worker_1_node, :worker_2_node], + 'block_writes'); + citus_split_shard_by_split_points +--------------------------------------------------------------------- + +(1 row) + +SELECT pg_catalog.citus_split_shard_by_split_points( + 8981001, + ARRAY['100000'], + ARRAY[:worker_1_node, :worker_2_node], + 'force_logical'); + citus_split_shard_by_split_points +--------------------------------------------------------------------- + +(1 row) + +-- The original shards are marked for deferred drop with policy_type = 2. +SELECT * from pg_dist_cleanup; + record_id | operation_id | object_type | object_name | node_group_id | policy_type +--------------------------------------------------------------------- + 11 | 777 | 1 | public.table_to_split_8981000 | 14 | 2 + 12 | 778 | 1 | public.table_to_split_8981001 | 16 | 2 +(2 rows) + +-- The physical shards should not be deleted. +\c - - - :worker_1_port +SELECT relname FROM pg_class where relname LIKE '%table_to_split_%' AND relkind = 'r'; + relname +--------------------------------------------------------------------- + table_to_split_8981000 + table_to_split_9999000 + table_to_split_9999002 +(3 rows) + +\c - - - :worker_2_port +SELECT relname FROM pg_class where relname LIKE '%table_to_split_%' AND relkind = 'r'; + relname +--------------------------------------------------------------------- + table_to_split_8981001 + table_to_split_9999001 + table_to_split_9999003 +(3 rows) + +-- Set a very short(1ms) time interval to force deferred drop cleanup. +\c - postgres - :master_port +ALTER SYSTEM SET citus.defer_shard_delete_interval TO 1; +SELECT pg_reload_conf(); + pg_reload_conf +--------------------------------------------------------------------- + t +(1 row) + +-- Give enough time for the deferred drop cleanup to run. +SELECT pg_sleep(2); + pg_sleep +--------------------------------------------------------------------- + +(1 row) + +-- Clenaup has been done. +SELECT * from pg_dist_cleanup; + record_id | operation_id | object_type | object_name | node_group_id | policy_type +--------------------------------------------------------------------- +(0 rows) + +\c - - - :worker_1_port +SELECT relname FROM pg_class where relname LIKE '%table_to_split_%' AND relkind = 'r'; + relname +--------------------------------------------------------------------- + table_to_split_9999000 + table_to_split_9999002 +(2 rows) + +\c - - - :worker_2_port +SELECT relname FROM pg_class where relname LIKE '%table_to_split_%' AND relkind = 'r'; + relname +--------------------------------------------------------------------- + table_to_split_9999001 + table_to_split_9999003 +(2 rows) + +-- Test Cleanup +\c - postgres - :master_port +DROP SCHEMA "citus_split_shard_by_split_points_deferred_schema" CASCADE; +NOTICE: drop cascades to table citus_split_shard_by_split_points_deferred_schema.temp_table diff --git a/src/test/regress/sql/citus_split_shard_by_split_points_deferred_drop.sql b/src/test/regress/sql/citus_split_shard_by_split_points_deferred_drop.sql new file mode 100644 index 000000000..84e8940a5 --- /dev/null +++ b/src/test/regress/sql/citus_split_shard_by_split_points_deferred_drop.sql @@ -0,0 +1,80 @@ +CREATE SCHEMA "citus_split_shard_by_split_points_deferred_schema"; + +CREATE ROLE test_split_role WITH LOGIN; +GRANT USAGE, CREATE ON SCHEMA "citus_split_shard_by_split_points_deferred_schema" TO test_split_role; +SET ROLE test_split_role; + +SET search_path TO "citus_split_shard_by_split_points_deferred_schema"; + +-- Valide user cannot insert directly to pg_dist_cleanup table but can select from it. +CREATE TABLE temp_table (id INT); +INSERT INTO pg_catalog.pg_dist_cleanup (operation_id, object_type, object_name, node_group_id, policy_type) + VALUES (3134, 1, 'citus_split_shard_by_split_points_deferred_schema.temp_table', 1, 1); + +SELECT * from pg_dist_cleanup; + +-- Set a very long(10mins) time interval to stop auto cleanup in case of deferred drop. +\c - postgres - :master_port +ALTER SYSTEM SET citus.defer_shard_delete_interval TO 600000; +SELECT pg_reload_conf(); + +-- Perform a split and validate shard is marked for deferred drop. +SET citus.next_shard_id TO 8981000; +SET citus.next_placement_id TO 8610000; +SET citus.shard_count TO 2; +SET citus.shard_replication_factor TO 1; +SET citus.next_operation_id TO 777; +SET citus.next_cleanup_record_id TO 11; +SET ROLE test_split_role; + +CREATE TABLE table_to_split(id int PRIMARY KEY, int_data int, data text); +SELECT create_distributed_table('table_to_split', 'id'); + +SELECT nodeid AS worker_1_node FROM pg_dist_node WHERE nodeport=:worker_1_port \gset +SELECT nodeid AS worker_2_node FROM pg_dist_node WHERE nodeport=:worker_2_port \gset + +SET citus.next_shard_id TO 9999000; +SET citus.next_placement_id TO 5555000; + +SELECT pg_catalog.citus_split_shard_by_split_points( + 8981000, + ARRAY['-100000'], + ARRAY[:worker_1_node, :worker_2_node], + 'block_writes'); + +SELECT pg_catalog.citus_split_shard_by_split_points( + 8981001, + ARRAY['100000'], + ARRAY[:worker_1_node, :worker_2_node], + 'force_logical'); + +-- The original shards are marked for deferred drop with policy_type = 2. +SELECT * from pg_dist_cleanup; + +-- The physical shards should not be deleted. +\c - - - :worker_1_port +SELECT relname FROM pg_class where relname LIKE '%table_to_split_%' AND relkind = 'r'; + +\c - - - :worker_2_port +SELECT relname FROM pg_class where relname LIKE '%table_to_split_%' AND relkind = 'r'; + +-- Set a very short(1ms) time interval to force deferred drop cleanup. +\c - postgres - :master_port +ALTER SYSTEM SET citus.defer_shard_delete_interval TO 1; +SELECT pg_reload_conf(); + +-- Give enough time for the deferred drop cleanup to run. +SELECT pg_sleep(2); + +-- Clenaup has been done. +SELECT * from pg_dist_cleanup; + +\c - - - :worker_1_port +SELECT relname FROM pg_class where relname LIKE '%table_to_split_%' AND relkind = 'r'; + +\c - - - :worker_2_port +SELECT relname FROM pg_class where relname LIKE '%table_to_split_%' AND relkind = 'r'; + +-- Test Cleanup +\c - postgres - :master_port +DROP SCHEMA "citus_split_shard_by_split_points_deferred_schema" CASCADE;