2020-10-13 08:30:35 +05:30
-- predictability
SET synchronous_commit = on;
2021-04-30 07:55:42 +05:30
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_stats', 'test_decoding');
2020-10-13 08:30:35 +05:30
?column?
----------
init
(1 row)
CREATE TABLE stats_test(data text);
-- function to wait for counters to advance
2021-04-16 07:34:43 +05:30
CREATE FUNCTION wait_for_decode_stats(check_reset bool, check_spill_txns bool) RETURNS void AS $$
2020-10-13 08:30:35 +05:30
DECLARE
start_time timestamptz := clock_timestamp();
updated bool;
BEGIN
-- we don't want to wait forever; loop will exit after 30 seconds
FOR i IN 1 .. 300 LOOP
2021-04-16 07:34:43 +05:30
IF check_spill_txns THEN
-- check to see if all updates have been reset/updated
SELECT CASE WHEN check_reset THEN (spill_txns = 0)
ELSE (spill_txns > 0)
END
INTO updated
2021-04-30 07:55:42 +05:30
FROM pg_stat_replication_slots WHERE slot_name='regression_slot_stats';
2021-04-16 07:34:43 +05:30
ELSE
-- check to see if all updates have been reset/updated
SELECT CASE WHEN check_reset THEN (total_txns = 0)
ELSE (total_txns > 0)
END
INTO updated
2021-04-30 07:55:42 +05:30
FROM pg_stat_replication_slots WHERE slot_name='regression_slot_stats';
2021-04-16 07:34:43 +05:30
END IF;
2020-10-13 08:30:35 +05:30
exit WHEN updated;
-- wait a little
perform pg_sleep_for('100 milliseconds');
-- reset stats snapshot so we can test again
perform pg_stat_clear_snapshot();
END LOOP;
-- report time waited in postmaster log (where it won't change test output)
RAISE LOG 'wait_for_decode_stats delayed % seconds',
extract(epoch from clock_timestamp() - start_time);
END
$$ LANGUAGE plpgsql;
2021-04-19 09:02:47 +05:30
-- non-spilled xact
2021-04-30 07:55:42 +05:30
SET logical_decoding_work_mem to '64MB';
2021-04-19 09:02:47 +05:30
INSERT INTO stats_test values(1);
2021-04-30 07:55:42 +05:30
SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats', NULL, NULL, 'skip-empty-xacts', '1');
2020-10-13 08:30:35 +05:30
count
-------
2021-04-19 09:02:47 +05:30
3
2020-10-13 08:30:35 +05:30
(1 row)
2021-04-19 09:02:47 +05:30
SELECT wait_for_decode_stats(false, false);
2020-10-13 08:30:35 +05:30
wait_for_decode_stats
-----------------------
(1 row)
2021-04-19 09:02:47 +05:30
SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count, total_txns > 0 AS total_txns, total_bytes > 0 AS total_bytes FROM pg_stat_replication_slots;
2021-04-30 07:55:42 +05:30
slot_name | spill_txns | spill_count | total_txns | total_bytes
-----------------------+------------+-------------+------------+-------------
regression_slot_stats | t | t | t | t
2020-10-13 08:30:35 +05:30
(1 row)
2021-04-30 07:55:42 +05:30
RESET logical_decoding_work_mem;
2021-04-19 09:02:47 +05:30
-- reset the slot stats, and wait for stats collector's total txn to reset
2021-04-30 07:55:42 +05:30
SELECT pg_stat_reset_replication_slot('regression_slot_stats');
2020-10-13 08:30:35 +05:30
pg_stat_reset_replication_slot
--------------------------------
(1 row)
2021-04-19 09:02:47 +05:30
SELECT wait_for_decode_stats(true, false);
2020-10-13 08:30:35 +05:30
wait_for_decode_stats
-----------------------
(1 row)
2021-04-16 07:34:43 +05:30
SELECT slot_name, spill_txns, spill_count, total_txns, total_bytes FROM pg_stat_replication_slots;
2021-04-30 07:55:42 +05:30
slot_name | spill_txns | spill_count | total_txns | total_bytes
-----------------------+------------+-------------+------------+-------------
regression_slot_stats | 0 | 0 | 0 | 0
2020-10-13 08:30:35 +05:30
(1 row)
2021-04-19 09:02:47 +05:30
-- spilling the xact
BEGIN;
INSERT INTO stats_test SELECT 'serialize-topbig--1:'||g.i FROM generate_series(1, 5000) g(i);
COMMIT;
2021-04-30 07:55:42 +05:30
SELECT count(*) FROM pg_logical_slot_peek_changes('regression_slot_stats', NULL, NULL, 'skip-empty-xacts', '1');
2020-10-13 08:30:35 +05:30
count
-------
2020-10-13 12:46:38 +05:30
5002
2020-10-13 08:30:35 +05:30
(1 row)
2021-04-19 09:02:47 +05:30
-- Check stats, wait for the stats collector to update. We can't test the
-- exact stats count as that can vary if any background transaction (say by
-- autovacuum) happens in parallel to the main transaction.
2021-04-16 07:34:43 +05:30
SELECT wait_for_decode_stats(false, true);
wait_for_decode_stats
-----------------------
(1 row)
2021-05-13 10:14:07 +05:30
SELECT slot_name, spill_txns > 0 AS spill_txns, spill_count > 0 AS spill_count FROM pg_stat_replication_slots;
slot_name | spill_txns | spill_count
-----------------------+------------+-------------
regression_slot_stats | t | t
2020-10-13 08:30:35 +05:30
(1 row)
2021-03-17 16:18:37 -07:00
-- Ensure stats can be repeatedly accessed using the same stats snapshot. See
-- https://postgr.es/m/20210317230447.c7uc4g3vbs4wi32i%40alap3.anarazel.de
BEGIN;
SELECT slot_name FROM pg_stat_replication_slots;
2021-04-30 07:55:42 +05:30
slot_name
-----------------------
regression_slot_stats
2021-03-17 16:18:37 -07:00
(1 row)
SELECT slot_name FROM pg_stat_replication_slots;
2021-04-30 07:55:42 +05:30
slot_name
-----------------------
regression_slot_stats
2021-03-17 16:18:37 -07:00
(1 row)
COMMIT;
2021-04-16 07:34:43 +05:30
DROP FUNCTION wait_for_decode_stats(bool, bool);
2020-10-13 08:30:35 +05:30
DROP TABLE stats_test;
2021-04-30 07:55:42 +05:30
SELECT pg_drop_replication_slot('regression_slot_stats');
2020-10-13 08:30:35 +05:30
pg_drop_replication_slot
--------------------------
(1 row)