postgresql/contrib/test_decoding/expected/stats.out
Amit Kapila c64dcc7fee Fix test case added by commit f5fc2f5b23.
In the new test after resetting the stats, we were not waiting for the
stats message to be delivered. Also, we need to decode the results for
the new test, otherwise, it will show the old stats.

In passing,
a. Change docs added by commit f5fc2f5b23 as per suggestion by
Justin Pryzby.
b. Bump the PGSTAT_FILE_FORMAT_ID as commit f5fc2f5b23 changes the file
format of stats.

Reported-by: Tom Lane based on buildfarm reports
Author: Vignesh C, Justin Pryzby
Reviewed-by: Amit Kapila
Discussion: https://postgr.es/m/20210319185247.ldebgpdaxsowiflw@alap3.anarazel.de
2021-04-19 09:02:47 +05:30

180 lines
5.5 KiB
Plaintext

-- 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, check_spill_txns 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
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
FROM pg_stat_replication_slots WHERE slot_name='regression_slot';
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
FROM pg_stat_replication_slots WHERE slot_name='regression_slot';
END IF;
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;
-- non-spilled xact
INSERT INTO stats_test values(1);
SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'skip-empty-xacts', '1');
count
-------
3
(1 row)
SELECT wait_for_decode_stats(false, false);
wait_for_decode_stats
-----------------------
(1 row)
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;
slot_name | spill_txns | spill_count | total_txns | total_bytes
-----------------+------------+-------------+------------+-------------
regression_slot | t | t | t | t
(1 row)
-- reset the slot stats, and wait for stats collector's total txn to reset
SELECT pg_stat_reset_replication_slot('regression_slot');
pg_stat_reset_replication_slot
--------------------------------
(1 row)
SELECT wait_for_decode_stats(true, false);
wait_for_decode_stats
-----------------------
(1 row)
SELECT slot_name, spill_txns, spill_count, total_txns, total_bytes FROM pg_stat_replication_slots;
slot_name | spill_txns | spill_count | total_txns | total_bytes
-----------------+------------+-------------+------------+-------------
regression_slot | 0 | 0 | 0 | 0
(1 row)
-- spilling the xact
BEGIN;
INSERT INTO stats_test SELECT 'serialize-topbig--1:'||g.i FROM generate_series(1, 5000) g(i);
COMMIT;
SELECT count(*) FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'skip-empty-xacts', '1');
count
-------
5002
(1 row)
-- 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.
SELECT wait_for_decode_stats(false, true);
wait_for_decode_stats
-----------------------
(1 row)
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;
slot_name | spill_txns | spill_count | total_txns | total_bytes
-----------------+------------+-------------+------------+-------------
regression_slot | t | t | t | t
(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, true);
wait_for_decode_stats
-----------------------
(1 row)
SELECT slot_name, spill_txns, spill_count, total_txns, total_bytes FROM pg_stat_replication_slots;
slot_name | spill_txns | spill_count | total_txns | total_bytes
-----------------+------------+-------------+------------+-------------
regression_slot | 0 | 0 | 0 | 0
(1 row)
-- decode and check stats again.
SELECT count(*) FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'skip-empty-xacts', '1');
count
-------
5002
(1 row)
SELECT wait_for_decode_stats(false, true);
wait_for_decode_stats
-----------------------
(1 row)
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;
slot_name | spill_txns | spill_count | total_txns | total_bytes
-----------------+------------+-------------+------------+-------------
regression_slot | t | t | t | t
(1 row)
-- 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;
slot_name
-----------------
regression_slot
(1 row)
SELECT slot_name FROM pg_stat_replication_slots;
slot_name
-----------------
regression_slot
(1 row)
COMMIT;
DROP FUNCTION wait_for_decode_stats(bool, bool);
DROP TABLE stats_test;
SELECT pg_drop_replication_slot('regression_slot');
pg_drop_replication_slot
--------------------------
(1 row)