1# Checks for recovery_min_apply_delay
2use strict;
3use warnings;
4
5use PostgresNode;
6use TestLib;
7use Test::More tests => 1;
8
9# Initialize master node
10my $node_master = get_new_node('master');
11$node_master->init(allows_streaming => 1);
12$node_master->start;
13
14# And some content
15$node_master->safe_psql('postgres',
16	"CREATE TABLE tab_int AS SELECT generate_series(1, 10) AS a");
17
18# Take backup
19my $backup_name = 'my_backup';
20$node_master->backup($backup_name);
21
22# Create streaming standby from backup
23my $node_standby = get_new_node('standby');
24my $delay        = 3;
25$node_standby->init_from_backup($node_master, $backup_name,
26	has_streaming => 1);
27$node_standby->append_conf(
28	'recovery.conf', qq(
29recovery_min_apply_delay = '${delay}s'
30));
31$node_standby->start;
32
33# Make new content on master and check its presence in standby depending
34# on the delay applied above. Before doing the insertion, get the
35# current timestamp that will be used as a comparison base. Even on slow
36# machines, this allows to have a predictable behavior when comparing the
37# delay between data insertion moment on master and replay time on standby.
38my $master_insert_time = time();
39$node_master->safe_psql('postgres',
40	"INSERT INTO tab_int VALUES (generate_series(11, 20))");
41
42# Now wait for replay to complete on standby. We're done waiting when the
43# standby has replayed up to the previously saved master LSN.
44my $until_lsn =
45  $node_master->safe_psql('postgres', "SELECT pg_current_wal_lsn()");
46
47$node_standby->poll_query_until('postgres',
48	"SELECT (pg_last_wal_replay_lsn() - '$until_lsn'::pg_lsn) >= 0")
49  or die "standby never caught up";
50
51# This test is successful if and only if the LSN has been applied with at least
52# the configured apply delay.
53ok(time() - $master_insert_time >= $delay,
54	"standby applies WAL only after replication delay");
55