Debian Codegraph @

src/test/recovery/t/005_replay_delay.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Checks for recovery_min_apply_delay and recovery pause
5 use strict;
6 use warnings FATAL => 'all';
7
8 use PostgreSQL::Test::Cluster;
9 use PostgreSQL::Test::Utils;
10 use Test::More;
11
12 # Initialize primary node
13 my $node_primary = PostgreSQL::Test::Cluster->new('primary');
14 $node_primary->init(allows_streaming => 1);
15 $node_primary->start;
16
17 # And some content
18 $node_primary->safe_psql('postgres',
19 "CREATE TABLE tab_int AS SELECT generate_series(1, 10) AS a");
20
21 # Take backup
22 my $backup_name = 'my_backup';
23 $node_primary->backup($backup_name);
24
25 # Create streaming standby from backup
26 my $node_standby = PostgreSQL::Test::Cluster->new('standby');
27 my $delay = 3;
28 $node_standby->init_from_backup($node_primary, $backup_name,
29 has_streaming => 1);
30 $node_standby->append_conf(
31 'postgresql.conf', qq(
32 recovery_min_apply_delay = '${delay}s'
33 ));
34 $node_standby->start;
35
36 # Make new content on primary and check its presence in standby depending
37 # on the delay applied above. Before doing the insertion, get the
38 # current timestamp that will be used as a comparison base. Even on slow
39 # machines, this allows to have a predictable behavior when comparing the
40 # delay between data insertion moment on primary and replay time on standby.
41 my $primary_insert_time = time();
42 $node_primary->safe_psql('postgres',
43 "INSERT INTO tab_int VALUES (generate_series(11, 20))");
44
45 # Now wait for replay to complete on standby. We're done waiting when the
46 # standby has replayed up to the previously saved primary LSN.
47 my $until_lsn =
48 $node_primary->safe_psql('postgres', "SELECT pg_current_wal_lsn()");
49
50 $node_standby->poll_query_until('postgres',
51 "SELECT (pg_last_wal_replay_lsn() - '$until_lsn'::pg_lsn) >= 0")
52 or die "standby never caught up";
53
54 # This test is successful if and only if the LSN has been applied with at least
55 # the configured apply delay.
56 ok(time() - $primary_insert_time >= $delay,
57 "standby applies WAL only after replication delay");
58
59
60 # Check that recovery can be paused or resumed expectedly.
61 my $node_standby2 = PostgreSQL::Test::Cluster->new('standby2');
62 $node_standby2->init_from_backup($node_primary, $backup_name,
63 has_streaming => 1);
64 $node_standby2->start;
65
66 # Recovery is not yet paused.
67 is( $node_standby2->safe_psql(
68 'postgres', "SELECT pg_get_wal_replay_pause_state()"),
69 'not paused',
70 'pg_get_wal_replay_pause_state() reports not paused');
71
72 # Request to pause recovery and wait until it's actually paused.
73 $node_standby2->safe_psql('postgres', "SELECT pg_wal_replay_pause()");
74 $node_primary->safe_psql('postgres',
75 "INSERT INTO tab_int VALUES (generate_series(21,30))");
76 $node_standby2->poll_query_until('postgres',
77 "SELECT pg_get_wal_replay_pause_state() = 'paused'")
78 or die "Timed out while waiting for recovery to be paused";
79
80 # Even if new WAL records are streamed from the primary,
81 # recovery in the paused state doesn't replay them.
82 my $receive_lsn =
83 $node_standby2->safe_psql('postgres', "SELECT pg_last_wal_receive_lsn()");
84 my $replay_lsn =
85 $node_standby2->safe_psql('postgres', "SELECT pg_last_wal_replay_lsn()");
86 $node_primary->safe_psql('postgres',
87 "INSERT INTO tab_int VALUES (generate_series(31,40))");
88 $node_standby2->poll_query_until('postgres',
89 "SELECT '$receive_lsn'::pg_lsn < pg_last_wal_receive_lsn()")
90 or die "Timed out while waiting for new WAL to be streamed";
91 is( $node_standby2->safe_psql('postgres', "SELECT pg_last_wal_replay_lsn()"),
92 qq($replay_lsn),
93 'no WAL is replayed in the paused state');
94
95 # Request to resume recovery and wait until it's actually resumed.
96 $node_standby2->safe_psql('postgres', "SELECT pg_wal_replay_resume()");
97 $node_standby2->poll_query_until('postgres',
98 "SELECT pg_get_wal_replay_pause_state() = 'not paused' AND pg_last_wal_replay_lsn() > '$replay_lsn'::pg_lsn"
99 ) or die "Timed out while waiting for recovery to be resumed";
100
101 # Check that the paused state ends and promotion continues if a promotion
102 # is triggered while recovery is paused.
103 $node_standby2->safe_psql('postgres', "SELECT pg_wal_replay_pause()");
104 $node_primary->safe_psql('postgres',
105 "INSERT INTO tab_int VALUES (generate_series(41,50))");
106 $node_standby2->poll_query_until('postgres',
107 "SELECT pg_get_wal_replay_pause_state() = 'paused'")
108 or die "Timed out while waiting for recovery to be paused";
109
110 $node_standby2->promote;
111 $node_standby2->poll_query_until('postgres', "SELECT NOT pg_is_in_recovery()")
112 or die "Timed out while waiting for promotion to finish";
113
114 done_testing();