]> granicus.if.org Git - postgresql/blob - src/test/recovery/t/004_timeline_switch.pl
Allow PostgresNode.pm tests to wait for catchup
[postgresql] / src / test / recovery / t / 004_timeline_switch.pl
1 # Test for timeline switch
2 # Ensure that a cascading standby is able to follow a newly-promoted standby
3 # on a new timeline.
4 use strict;
5 use warnings;
6 use File::Path qw(rmtree);
7 use PostgresNode;
8 use TestLib;
9 use Test::More tests => 1;
10
11 $ENV{PGDATABASE} = 'postgres';
12
13 # Initialize master node
14 my $node_master = get_new_node('master');
15 $node_master->init(allows_streaming => 1);
16 $node_master->start;
17
18 # Take backup
19 my $backup_name = 'my_backup';
20 $node_master->backup($backup_name);
21
22 # Create two standbys linking to it
23 my $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;
27 my $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', $node_master->lsn('write'));
38
39 # Stop and remove master, and promote standby 1, switching it to a new timeline
40 $node_master->teardown_node;
41 $node_standby_1->promote;
42
43 # Switch standby 2 to replay from standby 1
44 rmtree($node_standby_2->data_dir . '/recovery.conf');
45 my $connstr_1 = $node_standby_1->connstr;
46 $node_standby_2->append_conf(
47         'recovery.conf', qq(
48 primary_conninfo='$connstr_1 application_name=@{[$node_standby_2->name]}'
49 standby_mode=on
50 recovery_target_timeline='latest'
51 ));
52 $node_standby_2->restart;
53
54 # Insert some data in standby 1 and check its presence in standby 2
55 # to ensure that the timeline switch has been done.
56 $node_standby_1->safe_psql('postgres',
57         "INSERT INTO tab_int VALUES (generate_series(1001,2000))");
58 $node_standby_1->wait_for_catchup($node_standby_2, 'replay', $node_standby_1->lsn('write'));
59
60 my $result =
61   $node_standby_2->safe_psql('postgres', "SELECT count(*) FROM tab_int");
62 is($result, qq(2000), 'check content of standby 2');