Debian Codegraph @

contrib/test_decoding/t/001_repl_stats.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Test replication statistics data in pg_stat_replication_slots is sane after
5 # drop replication slot and restart.
6 use strict;
7 use warnings FATAL => 'all';
8 use File::Path qw(rmtree);
9 use PostgreSQL::Test::Cluster;
10 use PostgreSQL::Test::Utils;
11 use Test::More;
12
13 # Test set-up
14 my $node = PostgreSQL::Test::Cluster->new('test');
15 $node->init(allows_streaming => 'logical');
16 $node->append_conf('postgresql.conf', 'synchronous_commit = on');
17 $node->start;
18
19 # Check that replication slot stats are expected.
20 sub test_slot_stats
21 {
22 local $Test::Builder::Level = $Test::Builder::Level + 1;
23
24 my ($node, $expected, $msg) = @_;
25
26 my $result = $node->safe_psql(
27 'postgres', qq[
28 SELECT slot_name, total_txns > 0 AS total_txn,
29 total_bytes > 0 AS total_bytes
30 FROM pg_stat_replication_slots
31 ORDER BY slot_name]);
32 is($result, $expected, $msg);
33 }
34
35 # Create table.
36 $node->safe_psql('postgres', "CREATE TABLE test_repl_stat(col1 int)");
37
38 # Create replication slots.
39 $node->safe_psql(
40 'postgres', qq[
41 SELECT pg_create_logical_replication_slot('regression_slot1', 'test_decoding');
42 SELECT pg_create_logical_replication_slot('regression_slot2', 'test_decoding');
43 SELECT pg_create_logical_replication_slot('regression_slot3', 'test_decoding');
44 SELECT pg_create_logical_replication_slot('regression_slot4', 'test_decoding');
45 ]);
46
47 # Insert some data.
48 $node->safe_psql('postgres',
49 "INSERT INTO test_repl_stat values(generate_series(1, 5));");
50
51 $node->safe_psql(
52 'postgres', qq[
53 SELECT data FROM pg_logical_slot_get_changes('regression_slot1', NULL,
54 NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
55 SELECT data FROM pg_logical_slot_get_changes('regression_slot2', NULL,
56 NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
57 SELECT data FROM pg_logical_slot_get_changes('regression_slot3', NULL,
58 NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
59 SELECT data FROM pg_logical_slot_get_changes('regression_slot4', NULL,
60 NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
61 ]);
62
63 # Wait for the statistics to be updated.
64 $node->poll_query_until(
65 'postgres', qq[
66 SELECT count(slot_name) >= 4 FROM pg_stat_replication_slots
67 WHERE slot_name ~ 'regression_slot'
68 AND total_txns > 0 AND total_bytes > 0;
69 ]) or die "Timed out while waiting for statistics to be updated";
70
71 # Test to drop one of the replication slot and verify replication statistics data is
72 # fine after restart.
73 $node->safe_psql('postgres',
74 "SELECT pg_drop_replication_slot('regression_slot4')");
75
76 $node->stop;
77 $node->start;
78
79 # Verify statistics data present in pg_stat_replication_slots are sane after
80 # restart.
81 test_slot_stats(
82 $node,
83 qq(regression_slot1|t|t
84 regression_slot2|t|t
85 regression_slot3|t|t),
86 'check replication statistics are updated');
87
88 # Test to remove one of the replication slots and adjust
89 # max_replication_slots accordingly to the number of slots. This leads
90 # to a mismatch between the number of slots present in the stats file and the
91 # number of stats present in shared memory. We verify
92 # replication statistics data is fine after restart.
93
94 $node->stop;
95 my $datadir = $node->data_dir;
96 my $slot3_replslotdir = "$datadir/pg_replslot/regression_slot3";
97
98 rmtree($slot3_replslotdir);
99
100 $node->append_conf('postgresql.conf', 'max_replication_slots = 2');
101 $node->start;
102
103 # Verify statistics data present in pg_stat_replication_slots are sane after
104 # restart.
105 test_slot_stats(
106 $node,
107 qq(regression_slot1|t|t
108 regression_slot2|t|t),
109 'check replication statistics after removing the slot file');
110
111 # cleanup
112 $node->safe_psql('postgres', "DROP TABLE test_repl_stat");
113 $node->safe_psql('postgres',
114 "SELECT pg_drop_replication_slot('regression_slot1')");
115 $node->safe_psql('postgres',
116 "SELECT pg_drop_replication_slot('regression_slot2')");
117
118 # shutdown
119 $node->stop;
120
121 # Test replication slot stats persistence in a single session. The slot
122 # is dropped and created concurrently of a session peeking at its data
123 # repeatedly, hence holding in its local cache a reference to the stats.
124 $node->start;
125
126 my $slot_name_restart = 'regression_slot5';
127 $node->safe_psql('postgres',
128 "SELECT pg_create_logical_replication_slot('$slot_name_restart', 'test_decoding');"
129 );
130
131 # Look at slot data, with a persistent connection.
132 my $bpgsql = $node->background_psql('postgres', on_error_stop => 1);
133
134 # Launch query and look at slot data, incrementing the refcount of the
135 # stats entry.
136 $bpgsql->query_safe(
137 "SELECT pg_logical_slot_peek_binary_changes('$slot_name_restart', NULL, NULL)"
138 );
139
140 # Drop the slot entry. The stats entry is not dropped yet as the previous
141 # session still holds a reference to it.
142 $node->safe_psql('postgres',
143 "SELECT pg_drop_replication_slot('$slot_name_restart')");
144
145 # Create again the same slot. The stats entry is reinitialized, not marked
146 # as dropped anymore.
147 $node->safe_psql('postgres',
148 "SELECT pg_create_logical_replication_slot('$slot_name_restart', 'test_decoding');"
149 );
150
151 # Look again at the slot data. The local stats reference should be refreshed
152 # to the reinitialized entry.
153 $bpgsql->query_safe(
154 "SELECT pg_logical_slot_peek_binary_changes('$slot_name_restart', NULL, NULL)"
155 );
156 # Drop again the slot, the entry is not dropped yet as the previous session
157 # still has a refcount on it.
158 $node->safe_psql('postgres',
159 "SELECT pg_drop_replication_slot('$slot_name_restart')");
160
161 # Shutdown the node, which should happen cleanly with the stats file written
162 # to disk. Note that the background session created previously needs to be
163 # hold *while* the node is shutting down to check that it drops the stats
164 # entry of the slot before writing the stats file.
165 $node->stop;
166
167 # Make sure that the node is correctly shut down. Checking the control file
168 # is not enough, as the node may detect that something is incorrect after the
169 # control file has been updated and the shutdown checkpoint is finished, so
170 # also check that the stats file has been written out.
171 command_like(
172 [ 'pg_controldata', $node->data_dir ],
173 qr/Database cluster state:\s+shut down\n/,
174 'node shut down ok');
175
176 my $stats_file = "$datadir/pg_stat/pgstat.stat";
177 ok(-f "$stats_file", "stats file must exist after shutdown");
178
179 $bpgsql->quit;
180
181 done_testing();