2020-10-13 05:00:35 +02:00
|
|
|
-- predictability
|
|
|
|
SET synchronous_commit = on;
|
|
|
|
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding');
|
|
|
|
?column?
|
|
|
|
----------
|
|
|
|
init
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
CREATE TABLE stats_test(data text);
|
|
|
|
-- function to wait for counters to advance
|
|
|
|
CREATE FUNCTION wait_for_decode_stats(check_reset bool) RETURNS void AS $$
|
|
|
|
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
|
|
|
|
|
|
|
|
-- 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
|
|
|
|
FROM pg_stat_replication_slots WHERE name='regression_slot';
|
|
|
|
|
|
|
|
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;
|
|
|
|
-- spilling the xact
|
|
|
|
BEGIN;
|
|
|
|
INSERT INTO stats_test SELECT 'serialize-topbig--1:'||g.i FROM generate_series(1, 5000) g(i);
|
|
|
|
COMMIT;
|
2020-10-13 09:16:38 +02:00
|
|
|
SELECT count(*) FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'skip-empty-xacts', '1');
|
2020-10-13 05:00:35 +02:00
|
|
|
count
|
|
|
|
-------
|
2020-10-13 09:16:38 +02:00
|
|
|
5002
|
2020-10-13 05:00:35 +02:00
|
|
|
(1 row)
|
|
|
|
|
2020-10-13 09:16:38 +02:00
|
|
|
-- 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.
|
2020-10-13 05:00:35 +02:00
|
|
|
SELECT wait_for_decode_stats(false);
|
|
|
|
wait_for_decode_stats
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
(1 row)
|
|
|
|
|
2020-10-13 09:16:38 +02:00
|
|
|
SELECT name, spill_txns > 0 AS spill_txns, spill_count > 0 AS spill_count FROM pg_stat_replication_slots;
|
2020-10-13 05:00:35 +02:00
|
|
|
name | spill_txns | spill_count
|
|
|
|
-----------------+------------+-------------
|
2020-10-13 09:16:38 +02:00
|
|
|
regression_slot | t | t
|
2020-10-13 05:00:35 +02:00
|
|
|
(1 row)
|
|
|
|
|
|
|
|
-- reset the slot stats, and wait for stats collector to reset
|
|
|
|
SELECT pg_stat_reset_replication_slot('regression_slot');
|
|
|
|
pg_stat_reset_replication_slot
|
|
|
|
--------------------------------
|
|
|
|
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT wait_for_decode_stats(true);
|
|
|
|
wait_for_decode_stats
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT name, spill_txns, spill_count FROM pg_stat_replication_slots;
|
|
|
|
name | spill_txns | spill_count
|
|
|
|
-----------------+------------+-------------
|
|
|
|
regression_slot | 0 | 0
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
-- decode and check stats again.
|
2020-10-13 09:16:38 +02:00
|
|
|
SELECT count(*) FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'skip-empty-xacts', '1');
|
2020-10-13 05:00:35 +02:00
|
|
|
count
|
|
|
|
-------
|
2020-10-13 09:16:38 +02:00
|
|
|
5002
|
2020-10-13 05:00:35 +02:00
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT wait_for_decode_stats(false);
|
|
|
|
wait_for_decode_stats
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
(1 row)
|
|
|
|
|
2020-10-13 09:16:38 +02:00
|
|
|
SELECT name, spill_txns > 0 AS spill_txns, spill_count > 0 AS spill_count FROM pg_stat_replication_slots;
|
2020-10-13 05:00:35 +02:00
|
|
|
name | spill_txns | spill_count
|
|
|
|
-----------------+------------+-------------
|
2020-10-13 09:16:38 +02:00
|
|
|
regression_slot | t | t
|
2020-10-13 05:00:35 +02:00
|
|
|
(1 row)
|
|
|
|
|
|
|
|
DROP FUNCTION wait_for_decode_stats(bool);
|
|
|
|
DROP TABLE stats_test;
|
|
|
|
SELECT pg_drop_replication_slot('regression_slot');
|
|
|
|
pg_drop_replication_slot
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
(1 row)
|
|
|
|
|