Skip to content

Commit

Permalink
add tests for reindex event
Browse files Browse the repository at this point in the history
CONCURRENTLY update

update
  • Loading branch information
m3hm3t committed Dec 31, 2024
1 parent 103125f commit f810a17
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/test/regress/expected/pg17.out
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,61 @@ LEFT JOIN ref_table ON TRUE;
(1 row)

-- End of random(min, max) testing with Citus
-- 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
-- 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;
Expand Down
52 changes: 52 additions & 0 deletions src/test/regress/sql/pg17.sql
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,58 @@ LEFT JOIN ref_table ON TRUE;

-- End of random(min, max) testing with Citus

-- 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;

-- 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;
Expand Down

0 comments on commit f810a17

Please sign in to comment.