2020-10-13 05:00:35 +02:00
-- predictability
SET synchronous_commit = on;
2022-04-08 00:17:07 +02:00
SELECT 'init' FROM
pg_create_logical_replication_slot('regression_slot_stats1', 'test_decoding') s1,
pg_create_logical_replication_slot('regression_slot_stats2', 'test_decoding') s2,
pg_create_logical_replication_slot('regression_slot_stats3', 'test_decoding') s3;
2020-10-13 05:00:35 +02:00
?column?
----------
init
(1 row)
CREATE TABLE stats_test(data text);
2021-04-19 05:32:47 +02:00
-- non-spilled xact
2021-04-30 04:25:42 +02:00
SET logical_decoding_work_mem to '64MB';
2021-04-19 05:32:47 +02:00
INSERT INTO stats_test values(1);
2022-04-08 00:17:07 +02:00
SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats1', NULL, NULL, 'skip-empty-xacts', '1');
count
-------
3
(1 row)
SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats2', NULL, NULL, 'skip-empty-xacts', '1');
count
-------
3
(1 row)
SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'skip-empty-xacts', '1');
2020-10-13 05:00:35 +02:00
count
-------
2021-04-19 05:32:47 +02:00
3
2020-10-13 05:00:35 +02:00
(1 row)
2022-04-07 08:35:56 +02:00
SELECT pg_stat_force_next_flush();
pg_stat_force_next_flush
--------------------------
2020-10-13 05:00:35 +02:00
(1 row)
2022-04-08 00:17:07 +02:00
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 ORDER BY slot_name;
slot_name | spill_txns | spill_count | total_txns | total_bytes
------------------------+------------+-------------+------------+-------------
regression_slot_stats1 | t | t | t | t
regression_slot_stats2 | t | t | t | t
regression_slot_stats3 | t | t | t | t
(3 rows)
2020-10-13 05:00:35 +02:00
2021-04-30 04:25:42 +02:00
RESET logical_decoding_work_mem;
2022-04-08 00:17:07 +02:00
-- reset stats for one slot, others should be unaffected
SELECT pg_stat_reset_replication_slot('regression_slot_stats1');
2020-10-13 05:00:35 +02:00
pg_stat_reset_replication_slot
--------------------------------
(1 row)
2022-04-08 00:17:07 +02:00
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 ORDER BY slot_name;
slot_name | spill_txns | spill_count | total_txns | total_bytes
------------------------+------------+-------------+------------+-------------
regression_slot_stats1 | t | t | f | f
regression_slot_stats2 | t | t | t | t
regression_slot_stats3 | t | t | t | t
(3 rows)
-- reset stats for all slots
SELECT pg_stat_reset_replication_slot(NULL);
pg_stat_reset_replication_slot
--------------------------------
(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 ORDER BY slot_name;
slot_name | spill_txns | spill_count | total_txns | total_bytes
------------------------+------------+-------------+------------+-------------
regression_slot_stats1 | t | t | f | f
regression_slot_stats2 | t | t | f | f
regression_slot_stats3 | t | t | f | f
(3 rows)
-- verify accessing/resetting stats for non-existent slot does something reasonable
SELECT * FROM pg_stat_get_replication_slot('do-not-exist');
slot_name | spill_txns | spill_count | spill_bytes | stream_txns | stream_count | stream_bytes | total_txns | total_bytes | stats_reset
--------------+------------+-------------+-------------+-------------+--------------+--------------+------------+-------------+-------------
do-not-exist | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
(1 row)
SELECT pg_stat_reset_replication_slot('do-not-exist');
ERROR: replication slot "do-not-exist" does not exist
SELECT * FROM pg_stat_get_replication_slot('do-not-exist');
slot_name | spill_txns | spill_count | spill_bytes | stream_txns | stream_count | stream_bytes | total_txns | total_bytes | stats_reset
--------------+------------+-------------+-------------+-------------+--------------+--------------+------------+-------------+-------------
do-not-exist | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
2020-10-13 05:00:35 +02:00
(1 row)
2021-04-19 05:32:47 +02:00
-- spilling the xact
BEGIN;
INSERT INTO stats_test SELECT 'serialize-topbig--1:'||g.i FROM generate_series(1, 5000) g(i);
COMMIT;
2022-04-08 00:17:07 +02:00
SELECT count(*) FROM pg_logical_slot_peek_changes('regression_slot_stats1', 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)
2022-04-07 08:35:56 +02:00
-- Check stats. 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 pg_stat_force_next_flush();
pg_stat_force_next_flush
--------------------------
2021-04-16 04:04:43 +02:00
(1 row)
2021-05-13 06:44:07 +02:00
SELECT slot_name, spill_txns > 0 AS spill_txns, spill_count > 0 AS spill_count FROM pg_stat_replication_slots;
2022-04-08 00:17:07 +02:00
slot_name | spill_txns | spill_count
------------------------+------------+-------------
regression_slot_stats1 | t | t
regression_slot_stats2 | f | f
regression_slot_stats3 | f | f
(3 rows)
2020-10-13 05:00:35 +02:00
2021-03-18 00:18:37 +01: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;
2022-04-08 00:17:07 +02:00
slot_name
------------------------
regression_slot_stats1
regression_slot_stats2
regression_slot_stats3
(3 rows)
2021-03-18 00:18:37 +01:00
SELECT slot_name FROM pg_stat_replication_slots;
2022-04-08 00:17:07 +02:00
slot_name
------------------------
regression_slot_stats1
regression_slot_stats2
regression_slot_stats3
(3 rows)
2021-03-18 00:18:37 +01:00
COMMIT;
2020-10-13 05:00:35 +02:00
DROP TABLE stats_test;
2022-04-08 00:17:07 +02:00
SELECT pg_drop_replication_slot('regression_slot_stats1'),
pg_drop_replication_slot('regression_slot_stats2'),
pg_drop_replication_slot('regression_slot_stats3');
pg_drop_replication_slot | pg_drop_replication_slot | pg_drop_replication_slot
--------------------------+--------------------------+--------------------------
| |
2020-10-13 05:00:35 +02:00
(1 row)