summaryrefslogtreecommitdiffstats
path: root/src/test/recovery/t/005_replay_delay.pl
blob: 370fc9eace5fb6bfaac0a8000343bffd81fc0d80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Copyright (c) 2021-2022, PostgreSQL Global Development Group

# Checks for recovery_min_apply_delay and recovery pause
use strict;
use warnings;

use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;

# Initialize primary node
my $node_primary = PostgreSQL::Test::Cluster->new('primary');
$node_primary->init(allows_streaming => 1);
$node_primary->start;

# And some content
$node_primary->safe_psql('postgres',
	"CREATE TABLE tab_int AS SELECT generate_series(1, 10) AS a");

# Take backup
my $backup_name = 'my_backup';
$node_primary->backup($backup_name);

# Create streaming standby from backup
my $node_standby = PostgreSQL::Test::Cluster->new('standby');
my $delay        = 3;
$node_standby->init_from_backup($node_primary, $backup_name,
	has_streaming => 1);
$node_standby->append_conf(
	'postgresql.conf', qq(
recovery_min_apply_delay = '${delay}s'
));
$node_standby->start;

# Make new content on primary and check its presence in standby depending
# on the delay applied above. Before doing the insertion, get the
# current timestamp that will be used as a comparison base. Even on slow
# machines, this allows to have a predictable behavior when comparing the
# delay between data insertion moment on primary and replay time on standby.
my $primary_insert_time = time();
$node_primary->safe_psql('postgres',
	"INSERT INTO tab_int VALUES (generate_series(11, 20))");

# Now wait for replay to complete on standby. We're done waiting when the
# standby has replayed up to the previously saved primary LSN.
my $until_lsn =
  $node_primary->safe_psql('postgres', "SELECT pg_current_wal_lsn()");

$node_standby->poll_query_until('postgres',
	"SELECT (pg_last_wal_replay_lsn() - '$until_lsn'::pg_lsn) >= 0")
  or die "standby never caught up";

# This test is successful if and only if the LSN has been applied with at least
# the configured apply delay.
ok(time() - $primary_insert_time >= $delay,
	"standby applies WAL only after replication delay");


# Check that recovery can be paused or resumed expectedly.
my $node_standby2 = PostgreSQL::Test::Cluster->new('standby2');
$node_standby2->init_from_backup($node_primary, $backup_name,
	has_streaming => 1);
$node_standby2->start;

# Recovery is not yet paused.
is( $node_standby2->safe_psql(
		'postgres', "SELECT pg_get_wal_replay_pause_state()"),
	'not paused',
	'pg_get_wal_replay_pause_state() reports not paused');

# Request to pause recovery and wait until it's actually paused.
$node_standby2->safe_psql('postgres', "SELECT pg_wal_replay_pause()");
$node_primary->safe_psql('postgres',
	"INSERT INTO tab_int VALUES (generate_series(21,30))");
$node_standby2->poll_query_until('postgres',
	"SELECT pg_get_wal_replay_pause_state() = 'paused'")
  or die "Timed out while waiting for recovery to be paused";

# Even if new WAL records are streamed from the primary,
# recovery in the paused state doesn't replay them.
my $receive_lsn =
  $node_standby2->safe_psql('postgres', "SELECT pg_last_wal_receive_lsn()");
my $replay_lsn =
  $node_standby2->safe_psql('postgres', "SELECT pg_last_wal_replay_lsn()");
$node_primary->safe_psql('postgres',
	"INSERT INTO tab_int VALUES (generate_series(31,40))");
$node_standby2->poll_query_until('postgres',
	"SELECT '$receive_lsn'::pg_lsn < pg_last_wal_receive_lsn()")
  or die "Timed out while waiting for new WAL to be streamed";
is( $node_standby2->safe_psql('postgres', "SELECT pg_last_wal_replay_lsn()"),
	qq($replay_lsn),
	'no WAL is replayed in the paused state');

# Request to resume recovery and wait until it's actually resumed.
$node_standby2->safe_psql('postgres', "SELECT pg_wal_replay_resume()");
$node_standby2->poll_query_until('postgres',
	"SELECT pg_get_wal_replay_pause_state() = 'not paused' AND pg_last_wal_replay_lsn() > '$replay_lsn'::pg_lsn"
) or die "Timed out while waiting for recovery to be resumed";

# Check that the paused state ends and promotion continues if a promotion
# is triggered while recovery is paused.
$node_standby2->safe_psql('postgres', "SELECT pg_wal_replay_pause()");
$node_primary->safe_psql('postgres',
	"INSERT INTO tab_int VALUES (generate_series(41,50))");
$node_standby2->poll_query_until('postgres',
	"SELECT pg_get_wal_replay_pause_state() = 'paused'")
  or die "Timed out while waiting for recovery to be paused";

$node_standby2->promote;
$node_standby2->poll_query_until('postgres', "SELECT NOT pg_is_in_recovery()")
  or die "Timed out while waiting for promotion to finish";

done_testing();