|
| 1 | +# Replication checks |
| 2 | +use Data::Dumper; |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | + |
| 6 | +use PostgresNode; |
| 7 | +use TestLib; |
| 8 | +use Test::More; |
| 9 | + |
| 10 | +# Wait until replay to complete |
| 11 | +sub replay_wait( $$ ) { |
| 12 | + my $node_master = shift; |
| 13 | + my $node_standby = shift; |
| 14 | + my $until_lsn = |
| 15 | + $node_master->safe_psql('postgres', "SELECT pg_current_wal_lsn()"); |
| 16 | + |
| 17 | + my $last_wal_replay_lsn = |
| 18 | + $node_standby->safe_psql('postgres', "SELECT pg_last_wal_replay_lsn()"); |
| 19 | + |
| 20 | + $node_standby->poll_query_until('postgres', |
| 21 | + "SELECT (pg_last_wal_replay_lsn() - '$until_lsn'::pg_lsn) >= 0") |
| 22 | + or die "standby never caught up"; |
| 23 | + |
| 24 | + print Dumper( $until_lsn ); |
| 25 | + print Dumper( $last_wal_replay_lsn ); |
| 26 | +} |
| 27 | + |
| 28 | +my ( $ret, $stdout, $stderr ); |
| 29 | +my ( $ret_standby, $stdout_standby, $stderr_standby ); |
| 30 | + |
| 31 | +# Initialize master node |
| 32 | +my $node_master = get_new_node('master'); |
| 33 | +$node_master->init(allows_streaming => 1); |
| 34 | +$node_master->start; |
| 35 | + |
| 36 | +# Make first snapshot |
| 37 | +$node_master->safe_psql('postgres', |
| 38 | + "select pg_make_snapshot();"); |
| 39 | + |
| 40 | +# Create table |
| 41 | +$node_master->safe_psql( 'postgres', |
| 42 | + "CREATE TABLE t1 AS SELECT generate_series as id, trim( to_char( generate_series, '0000' ) ) as \"name\" from generate_series(1, 1000)" ); |
| 43 | +$node_master->safe_psql( 'postgres', |
| 44 | + "alter table t1 add constraint t1_pk PRIMARY KEY ( id )" ); |
| 45 | + |
| 46 | +# Take backup |
| 47 | +my $backup_name = 'my_backup'; |
| 48 | +$node_master->backup($backup_name); |
| 49 | + |
| 50 | +# Create streaming standby from backup |
| 51 | +my $node_standby = get_new_node('standby'); |
| 52 | + |
| 53 | +$node_standby->init_from_backup($node_master, $backup_name, |
| 54 | + has_streaming => 1); |
| 55 | + |
| 56 | +$node_standby->start; |
| 57 | + |
| 58 | +# Make second snapshot |
| 59 | +$node_master->safe_psql( 'postgres', |
| 60 | + "select pg_make_snapshot();" ); |
| 61 | + |
| 62 | +$node_master->safe_psql( 'postgres', |
| 63 | + "insert into t1 SELECT generate_series as id, trim( to_char( generate_series, '0000' ) ) as \"name\" from generate_series(1001, 2000)" ); |
| 64 | + |
| 65 | +$node_master->safe_psql( 'postgres', |
| 66 | + "update t1 set name = name || '_updated' where id <= 1000" ); |
| 67 | + |
| 68 | +# Make third snapshot |
| 69 | +$node_master->safe_psql( 'postgres', |
| 70 | + "select pg_make_snapshot();" ); |
| 71 | + |
| 72 | +$node_master->safe_psql( 'postgres', |
| 73 | + "delete from t1 where id > 1000;" ); |
| 74 | + |
| 75 | +replay_wait( $node_master, $node_standby ); |
| 76 | + |
| 77 | +$ret = $node_master->safe_psql( 'postgres', |
| 78 | + "select count(*) as cnt, avg(id), ( select name from t1 where id = 566 ) from t1;" ); |
| 79 | +$ret_standby = $node_standby->safe_psql( 'postgres', |
| 80 | + "select count(*) as cnt, avg(id), ( select name from t1 where id = 566 ) from t1;" ); |
| 81 | + |
| 82 | +ok( $ret eq $ret_standby, 'initial check' ); |
| 83 | + |
| 84 | +# switch to snapshot check |
| 85 | +$node_master->safe_psql( 'postgres', |
| 86 | + "select pg_switch_to_snapshot( ( select recent_snapshot - 2 from pg_control_snapshot() ) );" ); |
| 87 | + |
| 88 | +( $ret, $stdout, $stderr ) = $node_master->psql( 'postgres', |
| 89 | + "select count(*) as cnt, avg(id), ( select name from t1 where id = 566 ) from t1;" ); |
| 90 | +like( $stderr, '/ERROR: relation "t1" does not exist/', 'switch to snapshot 1 master check' ); |
| 91 | + |
| 92 | +( $ret_standby, $stdout_standby, $stderr_standby ) = $node_standby->psql( 'postgres', |
| 93 | + "select count(*) as cnt, avg(id), ( select name from t1 where id = 566 ) from t1;" ); |
| 94 | +ok( $stdout_standby eq '1000|500.5000000000000000|0566_updated', 'switch to snapshot 1 standby check' ); |
| 95 | + |
| 96 | +#teardown |
| 97 | +$node_master->safe_psql( 'postgres', |
| 98 | + "select pg_switch_to_snapshot( 0 );" ); |
| 99 | + |
| 100 | +# recover to snapshot checks |
| 101 | +$node_master->safe_psql( 'postgres', |
| 102 | + "select pg_recover_to_snapshot( ( select recent_snapshot from pg_control_snapshot() ) );" ); |
| 103 | +replay_wait( $node_master, $node_standby ); |
| 104 | +( $ret, $stdout, $stderr ) = $node_master->psql( 'postgres', |
| 105 | + "select count(*) as cnt, avg(id), ( select name from t1 where id = 566 ) from t1;" ); |
| 106 | +ok( $stdout eq '2000|1000.5000000000000000|0566_updated', 'recover to snapshot 1 master check' ); |
| 107 | +( $ret_standby, $stdout_standby, $stderr_standby ) = $node_standby->psql( 'postgres', |
| 108 | + "select count(*) as cnt, avg(id), ( select name from t1 where id = 566 ) from t1;" ); |
| 109 | +ok( $stdout_standby eq '2000|1000.5000000000000000|0566_updated', 'recover to snapshot 1 standby check' ); |
| 110 | + |
| 111 | +$node_master->safe_psql( 'postgres', |
| 112 | + "select pg_recover_to_snapshot( ( select recent_snapshot from pg_control_snapshot() ) );" ); |
| 113 | +replay_wait( $node_master, $node_standby ); |
| 114 | +( $ret, $stdout, $stderr ) = $node_master->psql( 'postgres', |
| 115 | + "select count(*) as cnt, avg(id), ( select name from t1 where id = 566 ) from t1;" ); |
| 116 | +ok( $stdout eq '1000|500.5000000000000000|0566', 'recover to snapshot 2 master check' ); |
| 117 | +( $ret_standby, $stdout_standby, $stderr_standby ) = $node_standby->psql( 'postgres', |
| 118 | + "select count(*) as cnt, avg(id), ( select name from t1 where id = 566 ) from t1;" ); |
| 119 | +ok( $stdout_standby eq '1000|500.5000000000000000|0566', 'recover to snapshot 2 standby check' ); |
| 120 | + |
| 121 | +$node_master->safe_psql( 'postgres', |
| 122 | + "select pg_recover_to_snapshot( ( select recent_snapshot from pg_control_snapshot() ) );" ); |
| 123 | +replay_wait( $node_master, $node_standby ); |
| 124 | + |
| 125 | +( $ret, $stdout, $stderr ) = $node_master->psql( 'postgres', |
| 126 | + "select count(*) as cnt, avg(id), ( select name from t1 where id = 566 ) from t1;" ); |
| 127 | +like( $stderr, '/ERROR: relation "t1" does not exist/', 'recover to snapshot 3 master check' ); |
| 128 | + |
| 129 | +#$node_master->safe_psql( 'postgres', |
| 130 | +# "select pg_sleep( 10 );" ); |
| 131 | +#replay_wait( $node_master, $node_standby ); |
| 132 | + |
| 133 | +( $ret_standby, $stdout_standby, $stderr_standby ) = $node_standby->psql( 'postgres', |
| 134 | + "select count(*) as cnt, avg(id), ( select name from t1 where id = 566 ) from t1;" ); |
| 135 | +like( $stderr_standby, '/ERROR: relation "t1" does not exist/', 'recover to snapshot 3 standby check' ); |
| 136 | + |
| 137 | +print Dumper( $ret ); |
| 138 | +print Dumper( $stdout ); |
| 139 | +print Dumper( $stderr ); |
| 140 | +print Dumper( $ret_standby ); |
| 141 | +print Dumper( $stdout_standby ); |
| 142 | +print Dumper( $stderr_standby ); |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +#( $ret, $stdout, $stderr ) = $node_master->psql( 'postgres', "select pg_switch_to_snapshot( 0 );" ); |
| 147 | +#$ret = $node_master->safe_psql( 'postgres', "select pg_recover_to_snapshot( 2 );" ); |
| 148 | +#ok( $ret eq '', 'pg_recover_to_snapshot() on master' ); |
| 149 | + |
| 150 | +#print Dumper( $ret ); |
| 151 | +#print Dumper( $stdout ); |
| 152 | +#print Dumper( $stderr ); |
| 153 | + |
| 154 | + |
| 155 | +done_testing(); |
0 commit comments