Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5264add

Browse files
committed
pgstat: add/extend tests for resetting various kinds of stats.
- subscriber stats reset path was untested - slot stat sreset path for all slots was untested - pg_stat_database.sessions etc was untested - pg_stat_reset_shared() was untested, for any kind of shared stats - pg_stat_reset() was untested Author: Melanie Plageman <melanieplageman@gmail.com> Author: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/20220303021600.hs34ghqcw6zcokdh@alap3.anarazel.de
1 parent 10a8d13 commit 5264add

File tree

7 files changed

+664
-111
lines changed

7 files changed

+664
-111
lines changed
+87-30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
-- predictability
22
SET synchronous_commit = on;
3-
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_stats', 'test_decoding');
3+
SELECT 'init' FROM
4+
pg_create_logical_replication_slot('regression_slot_stats1', 'test_decoding') s1,
5+
pg_create_logical_replication_slot('regression_slot_stats2', 'test_decoding') s2,
6+
pg_create_logical_replication_slot('regression_slot_stats3', 'test_decoding') s3;
47
?column?
58
----------
69
init
@@ -10,7 +13,19 @@ CREATE TABLE stats_test(data text);
1013
-- non-spilled xact
1114
SET logical_decoding_work_mem to '64MB';
1215
INSERT INTO stats_test values(1);
13-
SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats', NULL, NULL, 'skip-empty-xacts', '1');
16+
SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats1', NULL, NULL, 'skip-empty-xacts', '1');
17+
count
18+
-------
19+
3
20+
(1 row)
21+
22+
SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats2', NULL, NULL, 'skip-empty-xacts', '1');
23+
count
24+
-------
25+
3
26+
(1 row)
27+
28+
SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats3', NULL, NULL, 'skip-empty-xacts', '1');
1429
count
1530
-------
1631
3
@@ -22,31 +37,65 @@ SELECT pg_stat_force_next_flush();
2237

2338
(1 row)
2439

25-
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;
26-
slot_name | spill_txns | spill_count | total_txns | total_bytes
27-
-----------------------+------------+-------------+------------+-------------
28-
regression_slot_stats | t | t | t | t
29-
(1 row)
40+
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;
41+
slot_name | spill_txns | spill_count | total_txns | total_bytes
42+
------------------------+------------+-------------+------------+-------------
43+
regression_slot_stats1 | t | t | t | t
44+
regression_slot_stats2 | t | t | t | t
45+
regression_slot_stats3 | t | t | t | t
46+
(3 rows)
3047

3148
RESET logical_decoding_work_mem;
32-
-- reset the slot stats
33-
SELECT pg_stat_reset_replication_slot('regression_slot_stats');
49+
-- reset stats for one slot, others should be unaffected
50+
SELECT pg_stat_reset_replication_slot('regression_slot_stats1');
3451
pg_stat_reset_replication_slot
3552
--------------------------------
3653

3754
(1 row)
3855

39-
SELECT slot_name, spill_txns, spill_count, total_txns, total_bytes FROM pg_stat_replication_slots;
40-
slot_name | spill_txns | spill_count | total_txns | total_bytes
41-
-----------------------+------------+-------------+------------+-------------
42-
regression_slot_stats | 0 | 0 | 0 | 0
56+
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;
57+
slot_name | spill_txns | spill_count | total_txns | total_bytes
58+
------------------------+------------+-------------+------------+-------------
59+
regression_slot_stats1 | t | t | f | f
60+
regression_slot_stats2 | t | t | t | t
61+
regression_slot_stats3 | t | t | t | t
62+
(3 rows)
63+
64+
-- reset stats for all slots
65+
SELECT pg_stat_reset_replication_slot(NULL);
66+
pg_stat_reset_replication_slot
67+
--------------------------------
68+
69+
(1 row)
70+
71+
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;
72+
slot_name | spill_txns | spill_count | total_txns | total_bytes
73+
------------------------+------------+-------------+------------+-------------
74+
regression_slot_stats1 | t | t | f | f
75+
regression_slot_stats2 | t | t | f | f
76+
regression_slot_stats3 | t | t | f | f
77+
(3 rows)
78+
79+
-- verify accessing/resetting stats for non-existent slot does something reasonable
80+
SELECT * FROM pg_stat_get_replication_slot('do-not-exist');
81+
slot_name | spill_txns | spill_count | spill_bytes | stream_txns | stream_count | stream_bytes | total_txns | total_bytes | stats_reset
82+
--------------+------------+-------------+-------------+-------------+--------------+--------------+------------+-------------+-------------
83+
do-not-exist | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
84+
(1 row)
85+
86+
SELECT pg_stat_reset_replication_slot('do-not-exist');
87+
ERROR: replication slot "do-not-exist" does not exist
88+
SELECT * FROM pg_stat_get_replication_slot('do-not-exist');
89+
slot_name | spill_txns | spill_count | spill_bytes | stream_txns | stream_count | stream_bytes | total_txns | total_bytes | stats_reset
90+
--------------+------------+-------------+-------------+-------------+--------------+--------------+------------+-------------+-------------
91+
do-not-exist | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4392
(1 row)
4493

4594
-- spilling the xact
4695
BEGIN;
4796
INSERT INTO stats_test SELECT 'serialize-topbig--1:'||g.i FROM generate_series(1, 5000) g(i);
4897
COMMIT;
49-
SELECT count(*) FROM pg_logical_slot_peek_changes('regression_slot_stats', NULL, NULL, 'skip-empty-xacts', '1');
98+
SELECT count(*) FROM pg_logical_slot_peek_changes('regression_slot_stats1', NULL, NULL, 'skip-empty-xacts', '1');
5099
count
51100
-------
52101
5002
@@ -62,31 +111,39 @@ SELECT pg_stat_force_next_flush();
62111
(1 row)
63112

64113
SELECT slot_name, spill_txns > 0 AS spill_txns, spill_count > 0 AS spill_count FROM pg_stat_replication_slots;
65-
slot_name | spill_txns | spill_count
66-
-----------------------+------------+-------------
67-
regression_slot_stats | t | t
68-
(1 row)
114+
slot_name | spill_txns | spill_count
115+
------------------------+------------+-------------
116+
regression_slot_stats1 | t | t
117+
regression_slot_stats2 | f | f
118+
regression_slot_stats3 | f | f
119+
(3 rows)
69120

70121
-- Ensure stats can be repeatedly accessed using the same stats snapshot. See
71122
-- https://postgr.es/m/20210317230447.c7uc4g3vbs4wi32i%40alap3.anarazel.de
72123
BEGIN;
73124
SELECT slot_name FROM pg_stat_replication_slots;
74-
slot_name
75-
-----------------------
76-
regression_slot_stats
77-
(1 row)
125+
slot_name
126+
------------------------
127+
regression_slot_stats1
128+
regression_slot_stats2
129+
regression_slot_stats3
130+
(3 rows)
78131

79132
SELECT slot_name FROM pg_stat_replication_slots;
80-
slot_name
81-
-----------------------
82-
regression_slot_stats
83-
(1 row)
133+
slot_name
134+
------------------------
135+
regression_slot_stats1
136+
regression_slot_stats2
137+
regression_slot_stats3
138+
(3 rows)
84139

85140
COMMIT;
86141
DROP TABLE stats_test;
87-
SELECT pg_drop_replication_slot('regression_slot_stats');
88-
pg_drop_replication_slot
89-
--------------------------
90-
142+
SELECT pg_drop_replication_slot('regression_slot_stats1'),
143+
pg_drop_replication_slot('regression_slot_stats2'),
144+
pg_drop_replication_slot('regression_slot_stats3');
145+
pg_drop_replication_slot | pg_drop_replication_slot | pg_drop_replication_slot
146+
--------------------------+--------------------------+--------------------------
147+
| |
91148
(1 row)
92149

contrib/test_decoding/sql/stats.sql

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
11
-- predictability
22
SET synchronous_commit = on;
33

4-
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_stats', 'test_decoding');
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;
58

69
CREATE TABLE stats_test(data text);
710

811
-- non-spilled xact
912
SET logical_decoding_work_mem to '64MB';
1013
INSERT INTO stats_test values(1);
11-
SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot_stats', NULL, NULL, 'skip-empty-xacts', '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');
1217
SELECT pg_stat_force_next_flush();
13-
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;
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;
1419
RESET logical_decoding_work_mem;
1520

16-
-- reset the slot stats
17-
SELECT pg_stat_reset_replication_slot('regression_slot_stats');
18-
SELECT slot_name, spill_txns, spill_count, total_txns, total_bytes FROM pg_stat_replication_slots;
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');
1933

2034
-- spilling the xact
2135
BEGIN;
2236
INSERT INTO stats_test SELECT 'serialize-topbig--1:'||g.i FROM generate_series(1, 5000) g(i);
2337
COMMIT;
24-
SELECT count(*) FROM pg_logical_slot_peek_changes('regression_slot_stats', NULL, NULL, 'skip-empty-xacts', '1');
38+
SELECT count(*) FROM pg_logical_slot_peek_changes('regression_slot_stats1', NULL, NULL, 'skip-empty-xacts', '1');
2539

2640
-- Check stats. We can't test the exact stats count as that can vary if any
2741
-- background transaction (say by autovacuum) happens in parallel to the main
@@ -37,4 +51,6 @@ SELECT slot_name FROM pg_stat_replication_slots;
3751
COMMIT;
3852

3953
DROP TABLE stats_test;
40-
SELECT pg_drop_replication_slot('regression_slot_stats');
54+
SELECT pg_drop_replication_slot('regression_slot_stats1'),
55+
pg_drop_replication_slot('regression_slot_stats2'),
56+
pg_drop_replication_slot('regression_slot_stats3');

src/test/recovery/t/006_logical_decoding.pl

+63
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,69 @@
200200
ok(($logical_restart_lsn_pre cmp $logical_restart_lsn_post) == 0,
201201
"logical slot advance persists across restarts");
202202

203+
my $stats_test_slot1 = 'test_slot';
204+
my $stats_test_slot2 = 'logical_slot';
205+
206+
# Test that reset works for pg_stat_replication_slots
207+
208+
# Stats exist for stats test slot 1
209+
is($node_primary->safe_psql(
210+
'postgres',
211+
qq(SELECT total_bytes > 0, stats_reset IS NULL FROM pg_stat_replication_slots WHERE slot_name = '$stats_test_slot1')
212+
), qq(t|t), qq(Total bytes is > 0 and stats_reset is NULL for slot '$stats_test_slot1'.));
213+
214+
# Do reset of stats for stats test slot 1
215+
$node_primary->safe_psql(
216+
'postgres',
217+
qq(SELECT pg_stat_reset_replication_slot('$stats_test_slot1'))
218+
);
219+
220+
# Get reset value after reset
221+
my $reset1 = $node_primary->safe_psql(
222+
'postgres',
223+
qq(SELECT stats_reset FROM pg_stat_replication_slots WHERE slot_name = '$stats_test_slot1')
224+
);
225+
226+
# Do reset again
227+
$node_primary->safe_psql(
228+
'postgres',
229+
qq(SELECT pg_stat_reset_replication_slot('$stats_test_slot1'))
230+
);
231+
232+
is($node_primary->safe_psql(
233+
'postgres',
234+
qq(SELECT stats_reset > '$reset1'::timestamptz, total_bytes = 0 FROM pg_stat_replication_slots WHERE slot_name = '$stats_test_slot1')
235+
), qq(t|t), qq(Check that reset timestamp is later after the second reset of stats for slot '$stats_test_slot1' and confirm total_bytes was set to 0.));
236+
237+
# Check that test slot 2 has NULL in reset timestamp
238+
is($node_primary->safe_psql(
239+
'postgres',
240+
qq(SELECT stats_reset IS NULL FROM pg_stat_replication_slots WHERE slot_name = '$stats_test_slot2')
241+
), qq(t), qq(Stats_reset is NULL for slot '$stats_test_slot2' before reset.));
242+
243+
# Get reset value again for test slot 1
244+
$reset1 = $node_primary->safe_psql(
245+
'postgres',
246+
qq(SELECT stats_reset FROM pg_stat_replication_slots WHERE slot_name = '$stats_test_slot1')
247+
);
248+
249+
# Reset stats for all replication slots
250+
$node_primary->safe_psql(
251+
'postgres',
252+
qq(SELECT pg_stat_reset_replication_slot(NULL))
253+
);
254+
255+
# Check that test slot 2 reset timestamp is no longer NULL after reset
256+
is($node_primary->safe_psql(
257+
'postgres',
258+
qq(SELECT stats_reset IS NOT NULL FROM pg_stat_replication_slots WHERE slot_name = '$stats_test_slot2')
259+
), qq(t), qq(Stats_reset is not NULL for slot '$stats_test_slot2' after reset all.));
260+
261+
is($node_primary->safe_psql(
262+
'postgres',
263+
qq(SELECT stats_reset > '$reset1'::timestamptz FROM pg_stat_replication_slots WHERE slot_name = '$stats_test_slot1')
264+
), qq(t), qq(Check that reset timestamp is later after resetting stats for slot '$stats_test_slot1' again.));
265+
203266
# done with the node
204267
$node_primary->stop;
205268

0 commit comments

Comments
 (0)