1# Test for timeline switch
2# Ensure that a cascading standby is able to follow a newly-promoted standby
3# on a new timeline.
4use strict;
5use warnings;
6use File::Path qw(rmtree);
7use PostgresNode;
8use TestLib;
9use Test::More tests => 1;
10
11$ENV{PGDATABASE} = 'postgres';
12
13# Initialize master node
14my $node_master = get_new_node('master');
15$node_master->init(allows_streaming => 1);
16$node_master->start;
17
18# Take backup
19my $backup_name = 'my_backup';
20$node_master->backup($backup_name);
21
22# Create two standbys linking to it
23my $node_standby_1 = get_new_node('standby_1');
24$node_standby_1->init_from_backup($node_master, $backup_name,
25	has_streaming => 1);
26$node_standby_1->start;
27my $node_standby_2 = get_new_node('standby_2');
28$node_standby_2->init_from_backup($node_master, $backup_name,
29	has_streaming => 1);
30$node_standby_2->start;
31
32# Create some content on master
33$node_master->safe_psql('postgres',
34	"CREATE TABLE tab_int AS SELECT generate_series(1,1000) AS a");
35
36# Wait until standby has replayed enough data on standby 1
37$node_master->wait_for_catchup($node_standby_1, 'replay',
38	$node_master->lsn('write'));
39
40# Stop and remove master, and promote standby 1, switching it to a new timeline
41$node_master->teardown_node;
42$node_standby_1->promote;
43
44# Switch standby 2 to replay from standby 1
45rmtree($node_standby_2->data_dir . '/recovery.conf');
46my $connstr_1 = $node_standby_1->connstr;
47$node_standby_2->append_conf(
48	'recovery.conf', qq(
49primary_conninfo='$connstr_1 application_name=@{[$node_standby_2->name]}'
50standby_mode=on
51recovery_target_timeline='latest'
52));
53$node_standby_2->restart;
54
55# Insert some data in standby 1 and check its presence in standby 2
56# to ensure that the timeline switch has been done.
57$node_standby_1->safe_psql('postgres',
58	"INSERT INTO tab_int VALUES (generate_series(1001,2000))");
59$node_standby_1->wait_for_catchup($node_standby_2, 'replay',
60	$node_standby_1->lsn('write'));
61
62my $result =
63  $node_standby_2->safe_psql('postgres', "SELECT count(*) FROM tab_int");
64is($result, qq(2000), 'check content of standby 2');
65