From 886497e3628f6042e30d5f099bc80947a08fc5df Mon Sep 17 00:00:00 2001 From: Mehmet Yilmaz Date: Fri, 27 Dec 2024 18:37:31 +0000 Subject: [PATCH] add tests for reindex event CONCURRENTLY update --- src/test/regress/expected/pg17.out | 75 ++++++++++++++++++++++++++++++ src/test/regress/sql/pg17.sql | 61 ++++++++++++++++++++++++ 2 files changed, 136 insertions(+) diff --git a/src/test/regress/expected/pg17.out b/src/test/regress/expected/pg17.out index ff1e57d7418..3d90b9ccad8 100644 --- a/src/test/regress/expected/pg17.out +++ b/src/test/regress/expected/pg17.out @@ -1453,6 +1453,81 @@ ROLLBACK; NOTICE: issuing ROLLBACK DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx -- End of Testing AT LOCAL option +-- Test for REINDEX support in event triggers for Citus-related objects +-- Create a test table with a distributed setup +CREATE TABLE reindex_test (id SERIAL PRIMARY KEY, data TEXT); +SELECT create_distributed_table('reindex_test', 'id'); + create_distributed_table +--------------------------------------------------------------------- + +(1 row) + +-- Create an index to test REINDEX functionality +CREATE INDEX reindex_test_data_idx ON reindex_test (data); +-- Create event triggers to capture REINDEX events (start and end) +CREATE OR REPLACE FUNCTION log_reindex_events() RETURNS event_trigger LANGUAGE plpgsql AS $$ +DECLARE + command_tag TEXT; + command_object JSONB; +BEGIN + command_tag := tg_tag; + command_object := jsonb_build_object( + 'object_type', tg_event, + 'command_tag', command_tag, + 'query', current_query() + ); + RAISE NOTICE 'Event Trigger Log: %', command_object::TEXT; +END; +$$; +CREATE EVENT TRIGGER reindex_event_trigger + ON ddl_command_start + WHEN TAG IN ('REINDEX') +EXECUTE FUNCTION log_reindex_events(); +CREATE EVENT TRIGGER reindex_event_trigger_end + ON ddl_command_end + WHEN TAG IN ('REINDEX') +EXECUTE FUNCTION log_reindex_events(); +-- Insert some data to create index bloat +INSERT INTO reindex_test (data) +SELECT 'value_' || g.i +FROM generate_series(1, 10000) g(i); +-- Perform REINDEX TABLE ... CONCURRENTLY and verify event trigger logs +REINDEX TABLE CONCURRENTLY reindex_test; +NOTICE: Event Trigger Log: {"query": "REINDEX TABLE CONCURRENTLY reindex_test;", "command_tag": "REINDEX", "object_type": "ddl_command_start"} +CONTEXT: PL/pgSQL function log_reindex_events() line XX at RAISE +NOTICE: Event Trigger Log: {"query": "REINDEX TABLE CONCURRENTLY reindex_test;", "command_tag": "REINDEX", "object_type": "ddl_command_end"} +CONTEXT: PL/pgSQL function log_reindex_events() line XX at RAISE +-- Perform REINDEX INDEX ... CONCURRENTLY and verify event trigger logs +REINDEX INDEX CONCURRENTLY reindex_test_data_idx; +NOTICE: Event Trigger Log: {"query": "REINDEX INDEX CONCURRENTLY reindex_test_data_idx;", "command_tag": "REINDEX", "object_type": "ddl_command_start"} +CONTEXT: PL/pgSQL function log_reindex_events() line XX at RAISE +NOTICE: Event Trigger Log: {"query": "REINDEX INDEX CONCURRENTLY reindex_test_data_idx;", "command_tag": "REINDEX", "object_type": "ddl_command_end"} +CONTEXT: PL/pgSQL function log_reindex_events() line XX at RAISE +-- Verify shard-level impact of REINDEX on the distributed table +SELECT + s.shardid, + p.nodename, + p.nodeport +FROM pg_dist_shard s +JOIN pg_dist_shard_placement p ON s.shardid = p.shardid +WHERE s.logicalrelid = 'reindex_test'::regclass; + shardid | nodename | nodeport +--------------------------------------------------------------------- + 25122059 | localhost | 57637 + 25122059 | localhost | 57638 + 25122060 | localhost | 57637 + 25122060 | localhost | 57638 + 25122061 | localhost | 57637 + 25122061 | localhost | 57638 + 25122062 | localhost | 57637 + 25122062 | localhost | 57638 +(8 rows) + +-- Cleanup +DROP EVENT TRIGGER reindex_event_trigger; +DROP EVENT TRIGGER reindex_event_trigger_end; +DROP TABLE reindex_test CASCADE; +-- End of test for REINDEX support in event triggers for Citus-related objects \set VERBOSITY terse SET client_min_messages TO WARNING; DROP SCHEMA pg17 CASCADE; diff --git a/src/test/regress/sql/pg17.sql b/src/test/regress/sql/pg17.sql index fd3a6ddfdf6..96a1df67583 100644 --- a/src/test/regress/sql/pg17.sql +++ b/src/test/regress/sql/pg17.sql @@ -797,6 +797,67 @@ ROLLBACK; -- End of Testing AT LOCAL option +-- Test for REINDEX support in event triggers for Citus-related objects +-- Create a test table with a distributed setup +CREATE TABLE reindex_test (id SERIAL PRIMARY KEY, data TEXT); +SELECT create_distributed_table('reindex_test', 'id'); + +-- Create an index to test REINDEX functionality +CREATE INDEX reindex_test_data_idx ON reindex_test (data); + +-- Create event triggers to capture REINDEX events (start and end) +CREATE OR REPLACE FUNCTION log_reindex_events() RETURNS event_trigger LANGUAGE plpgsql AS $$ +DECLARE + command_tag TEXT; + command_object JSONB; +BEGIN + command_tag := tg_tag; + command_object := jsonb_build_object( + 'object_type', tg_event, + 'command_tag', command_tag, + 'query', current_query() + ); + RAISE NOTICE 'Event Trigger Log: %', command_object::TEXT; +END; +$$; + +CREATE EVENT TRIGGER reindex_event_trigger + ON ddl_command_start + WHEN TAG IN ('REINDEX') +EXECUTE FUNCTION log_reindex_events(); + +CREATE EVENT TRIGGER reindex_event_trigger_end + ON ddl_command_end + WHEN TAG IN ('REINDEX') +EXECUTE FUNCTION log_reindex_events(); + +-- Insert some data to create index bloat +INSERT INTO reindex_test (data) +SELECT 'value_' || g.i +FROM generate_series(1, 10000) g(i); + +-- Perform REINDEX TABLE ... CONCURRENTLY and verify event trigger logs +REINDEX TABLE CONCURRENTLY reindex_test; + +-- Perform REINDEX INDEX ... CONCURRENTLY and verify event trigger logs +REINDEX INDEX CONCURRENTLY reindex_test_data_idx; + +-- Verify shard-level impact of REINDEX on the distributed table +SELECT + s.shardid, + p.nodename, + p.nodeport +FROM pg_dist_shard s +JOIN pg_dist_shard_placement p ON s.shardid = p.shardid +WHERE s.logicalrelid = 'reindex_test'::regclass; + +-- Cleanup +DROP EVENT TRIGGER reindex_event_trigger; +DROP EVENT TRIGGER reindex_event_trigger_end; +DROP TABLE reindex_test CASCADE; + +-- End of test for REINDEX support in event triggers for Citus-related objects + \set VERBOSITY terse SET client_min_messages TO WARNING; DROP SCHEMA pg17 CASCADE;