Debian Codegraph @

contrib/test_decoding/sql/stats.sql

raw
1 -- predictability
2 SET synchronous_commit = on;
3
4 SELECT 'init' FROM
5 pg_create_logical_replication_slot('regression_slot_stats1', 'test_decoding') s1,
6 pg_create_logical_replication_slot('regression_slot_stats2', 'test_decoding') s2,
7 pg_create_logical_replication_slot('regression_slot_stats3', 'test_decoding') s3;
8
9 CREATE TABLE stats_test(data text);
10
11 -- non-spilled xact
12 SET logical_decoding_work_mem to '64MB';
13 INSERT INTO stats_test values(1);
14 SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats1', NULL, NULL, 'skip-empty-xacts', '1');
15 SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats2', NULL, NULL, 'skip-empty-xacts', '1');
16 SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'skip-empty-xacts', '1');
17 SELECT pg_stat_force_next_flush();
18 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;
19 RESET logical_decoding_work_mem;
20
21 -- reset stats for one slot, others should be unaffected
22 SELECT pg_stat_reset_replication_slot('regression_slot_stats1');
23 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;
24
25 -- reset stats for all slots
26 SELECT pg_stat_reset_replication_slot(NULL);
27 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;
28
29 -- verify accessing/resetting stats for non-existent slot does something reasonable
30 SELECT * FROM pg_stat_get_replication_slot('do-not-exist');
31 SELECT pg_stat_reset_replication_slot('do-not-exist');
32 SELECT * FROM pg_stat_get_replication_slot('do-not-exist');
33
34 -- spilling the xact
35 BEGIN;
36 INSERT INTO stats_test SELECT 'serialize-topbig--1:'||g.i FROM generate_series(1, 5000) g(i);
37 COMMIT;
38 SELECT count(*) FROM pg_logical_slot_peek_changes('regression_slot_stats1', NULL, NULL, 'skip-empty-xacts', '1');
39
40 -- Check stats. We can't test the exact stats count as that can vary if any
41 -- background transaction (say by autovacuum) happens in parallel to the main
42 -- transaction.
43 SELECT pg_stat_force_next_flush();
44 SELECT slot_name, spill_txns > 0 AS spill_txns, spill_count > 0 AS spill_count FROM pg_stat_replication_slots;
45
46 -- Ensure stats can be repeatedly accessed using the same stats snapshot. See
47 -- https://postgr.es/m/20210317230447.c7uc4g3vbs4wi32i%40alap3.anarazel.de
48 BEGIN;
49 SELECT slot_name FROM pg_stat_replication_slots;
50 SELECT slot_name FROM pg_stat_replication_slots;
51 COMMIT;
52
53
54 SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_stats4_twophase', 'test_decoding', false, true) s4;
55
56 -- The INSERT changes are large enough to be spilled but will not be, because
57 -- the transaction is aborted. The logical decoding skips collecting further
58 -- changes too. The transaction is prepared to make sure the decoding processes
59 -- the aborted transaction.
60 BEGIN;
61 INSERT INTO stats_test SELECT 'serialize-toobig--1:'||g.i FROM generate_series(1, 5000) g(i);
62 PREPARE TRANSACTION 'test1_abort';
63 ROLLBACK PREPARED 'test1_abort';
64 SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats4_twophase', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
65
66 -- Verify that the decoding doesn't spill already-aborted transaction's changes.
67 SELECT pg_stat_force_next_flush();
68 SELECT slot_name, spill_txns, spill_count FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot_stats4_twophase';
69
70 DROP TABLE stats_test;
71 SELECT pg_drop_replication_slot('regression_slot_stats1'),
72 pg_drop_replication_slot('regression_slot_stats2'),
73 pg_drop_replication_slot('regression_slot_stats3'),
74 pg_drop_replication_slot('regression_slot_stats4_twophase');