|
2 | 2 | # drop replication slot and restart.
|
3 | 3 | use strict;
|
4 | 4 | use warnings;
|
| 5 | +use File::Path qw(rmtree); |
5 | 6 | use PostgresNode;
|
6 | 7 | use TestLib;
|
7 |
| -use Test::More tests => 1; |
| 8 | +use Test::More tests => 2; |
8 | 9 |
|
9 | 10 | # Test set-up
|
10 | 11 | my $node = get_new_node('test');
|
11 | 12 | $node->init(allows_streaming => 'logical');
|
12 | 13 | $node->append_conf('postgresql.conf', 'synchronous_commit = on');
|
13 | 14 | $node->start;
|
14 | 15 |
|
| 16 | +# Check that replication slot stats are expected. |
| 17 | +sub test_slot_stats |
| 18 | +{ |
| 19 | + my ($node, $expected, $msg) = @_; |
| 20 | + |
| 21 | + my $result = $node->safe_psql( |
| 22 | + 'postgres', qq[ |
| 23 | + SELECT slot_name, total_txns > 0 AS total_txn, |
| 24 | + total_bytes > 0 AS total_bytes |
| 25 | + FROM pg_stat_replication_slots |
| 26 | + ORDER BY slot_name]); |
| 27 | + is($result, $expected, $msg); |
| 28 | +} |
| 29 | + |
15 | 30 | # Create table.
|
16 |
| -$node->safe_psql('postgres', |
17 |
| - "CREATE TABLE test_repl_stat(col1 int)"); |
| 31 | +$node->safe_psql('postgres', "CREATE TABLE test_repl_stat(col1 int)"); |
18 | 32 |
|
19 | 33 | # Create replication slots.
|
20 | 34 | $node->safe_psql(
|
|
26 | 40 | ]);
|
27 | 41 |
|
28 | 42 | # Insert some data.
|
29 |
| -$node->safe_psql('postgres', "INSERT INTO test_repl_stat values(generate_series(1, 5));"); |
| 43 | +$node->safe_psql('postgres', |
| 44 | + "INSERT INTO test_repl_stat values(generate_series(1, 5));"); |
30 | 45 |
|
31 | 46 | $node->safe_psql(
|
32 | 47 | 'postgres', qq[
|
|
50 | 65 |
|
51 | 66 | # Test to drop one of the replication slot and verify replication statistics data is
|
52 | 67 | # fine after restart.
|
53 |
| -$node->safe_psql('postgres', "SELECT pg_drop_replication_slot('regression_slot4')"); |
| 68 | +$node->safe_psql('postgres', |
| 69 | + "SELECT pg_drop_replication_slot('regression_slot4')"); |
54 | 70 |
|
55 | 71 | $node->stop;
|
56 | 72 | $node->start;
|
57 | 73 |
|
58 | 74 | # Verify statistics data present in pg_stat_replication_slots are sane after
|
59 | 75 | # restart.
|
60 |
| -my $result = $node->safe_psql('postgres', |
61 |
| - "SELECT slot_name, total_txns > 0 AS total_txn, |
62 |
| - total_bytes > 0 AS total_bytes FROM pg_stat_replication_slots |
63 |
| - ORDER BY slot_name" |
64 |
| -); |
65 |
| -is($result, qq(regression_slot1|t|t |
| 76 | +test_slot_stats( |
| 77 | + $node, |
| 78 | + qq(regression_slot1|t|t |
66 | 79 | regression_slot2|t|t
|
67 |
| -regression_slot3|t|t), 'check replication statistics are updated'); |
| 80 | +regression_slot3|t|t), |
| 81 | + 'check replication statistics are updated'); |
| 82 | + |
| 83 | +# Test to remove one of the replication slots and adjust |
| 84 | +# max_replication_slots accordingly to the number of slots. This leads |
| 85 | +# to a mismatch between the number of slots present in the stats file and the |
| 86 | +# number of stats present in the shared memory, simulating the scenario for |
| 87 | +# drop slot message lost by the statistics collector process. We verify |
| 88 | +# replication statistics data is fine after restart. |
| 89 | + |
| 90 | +$node->stop; |
| 91 | +my $datadir = $node->data_dir; |
| 92 | +my $slot3_replslotdir = "$datadir/pg_replslot/regression_slot3"; |
| 93 | + |
| 94 | +rmtree($slot3_replslotdir); |
| 95 | + |
| 96 | +$node->append_conf('postgresql.conf', 'max_replication_slots = 2'); |
| 97 | +$node->start; |
| 98 | + |
| 99 | +# Verify statistics data present in pg_stat_replication_slots are sane after |
| 100 | +# restart. |
| 101 | +test_slot_stats( |
| 102 | + $node, |
| 103 | + qq(regression_slot1|t|t |
| 104 | +regression_slot2|t|t), |
| 105 | + 'check replication statistics after removing the slot file'); |
68 | 106 |
|
69 | 107 | # cleanup
|
70 | 108 | $node->safe_psql('postgres', "DROP TABLE test_repl_stat");
|
71 |
| -$node->safe_psql('postgres', "SELECT pg_drop_replication_slot('regression_slot1')"); |
72 |
| -$node->safe_psql('postgres', "SELECT pg_drop_replication_slot('regression_slot2')"); |
73 |
| -$node->safe_psql('postgres', "SELECT pg_drop_replication_slot('regression_slot3')"); |
| 109 | +$node->safe_psql('postgres', |
| 110 | + "SELECT pg_drop_replication_slot('regression_slot1')"); |
| 111 | +$node->safe_psql('postgres', |
| 112 | + "SELECT pg_drop_replication_slot('regression_slot2')"); |
74 | 113 |
|
75 | 114 | # shutdown
|
76 | 115 | $node->stop;
|
0 commit comments