Skip to content

Commit

Permalink
add tests for reindex event
Browse files Browse the repository at this point in the history
  • Loading branch information
m3hm3t committed Dec 27, 2024
1 parent 1a33162 commit b714959
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/test/regress/expected/pg17.out
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,81 @@ DROP TABLE test_local_table_expr CASCADE;
DROP TABLE test_distributed_table_expr CASCADE;
DROP TABLE test_partitioned_expr CASCADE;
-- End of Test for ALTER TABLE ... ALTER COLUMN ... SET EXPRESSION
-- 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 on the distributed table and verify event trigger logs
REINDEX TABLE reindex_test;
NOTICE: Event Trigger Log: {"query": "REINDEX TABLE 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 reindex_test;", "command_tag": "REINDEX", "object_type": "ddl_command_end"}
CONTEXT: PL/pgSQL function log_reindex_events() line XX at RAISE
-- Perform REINDEX on the distributed index
REINDEX INDEX reindex_test_data_idx;
NOTICE: Event Trigger Log: {"query": "REINDEX INDEX 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 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;
Expand Down
61 changes: 61 additions & 0 deletions src/test/regress/sql/pg17.sql
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,67 @@ DROP TABLE test_distributed_table_expr CASCADE;
DROP TABLE test_partitioned_expr CASCADE;
-- End of Test for ALTER TABLE ... ALTER COLUMN ... SET EXPRESSION

-- 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 on the distributed table and verify event trigger logs
REINDEX TABLE reindex_test;

-- Perform REINDEX on the distributed index
REINDEX INDEX 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;
Expand Down

0 comments on commit b714959

Please sign in to comment.