# Minimal test testing streaming replication use strict; use warnings; use PostgresNode; use TestLib; use Test::More tests => 2; my $primary = get_new_node('primary'); $primary->init(allows_streaming => 1); $primary->start; my $backup_name = 'my_backup'; $primary->backup($backup_name); my $node_2 = get_new_node('node_2'); $node_2->init_from_backup($primary, $backup_name, has_streaming => 1, allows_streaming => 1); $node_2->append_conf('postgresql.conf', "archive_mode = always"); my $archdir = $node_2->data_dir . "/../archive"; $node_2->append_conf('postgresql.conf', "archive_command = 'cp %p $archdir/%f'"); mkdir($archdir); $node_2->start; # Create streaming standby linking to primary my $node_3 = get_new_node('node_3'); $node_3->init_from_backup($primary, $backup_name, has_streaming => 1); $node_3->append_conf('postgresql.conf', "restore_command = 'cp $archdir/%f %p'"); $node_3->start; $primary->psql('postgres', 'SELECT pg_switch_wal(); CREATE TABLE t(); DROP TABLE t; CHECKPOINT;'); $primary->wait_for_catchup($node_2, 'write', $primary->lsn('insert')); $primary->wait_for_catchup($node_3, 'write', $primary->lsn('insert')); $node_3->stop; # put node3 a bit behind to cause streaming on the old timeline $primary->psql('postgres', 'CREATE TABLE t(); DROP TABLE t;'); $primary->wait_for_catchup($node_2, 'write', $primary->lsn('insert')); $primary->stop; # promote node2 $node_2->psql('postgres', 'SELECT pg_promote()'); # optional: archive segment 3 of TLI=2 on node2 and advance one more segment if (0) { my $lastwal = $node_2->safe_psql('postgres', 'select last_archived_wal from pg_stat_archiver'); $node_2->psql('postgres', 'SELECT pg_switch_wal();'); $node_2->psql('postgres', 'CREATE TABLE t(); DROP TABLE t;'); $node_2->poll_query_until('postgres', "SELECT '$lastwal' <> last_archived_wal from pg_stat_archiver"); $lastwal = $node_2->safe_psql('postgres', 'select last_archived_wal from pg_stat_archiver'); $node_2->psql('postgres', 'SELECT pg_switch_wal();'); $node_2->psql('postgres', 'CREATE TABLE t(); DROP TABLE t;'); $node_2->poll_query_until('postgres', "SELECT '$lastwal' <> last_archived_wal from pg_stat_archiver"); } # attach node3 as a standby of node2 $node_3->enable_streaming($node_2); $node_3->append_conf('postgresql.conf', "recovery_target_timeline = 2"); # *restart# node3, not just reloading to trigger archive recovery $node_3->start; $node_2->psql('postgres', 'CREATE TABLE t(); DROP TABLE t;'); $node_2->psql('postgres', 'SELECT pg_switch_wal();'); # XXX: another defect comes out without this X( $node_2->psql('postgres', 'CREATE TABLE t(); DROP TABLE t;'); $node_2->wait_for_catchup($node_3, 'write', $node_2->lsn('insert')); my $result = $node_2->safe_psql('postgres', 'SELECT pg_current_wal_insert_lsn() = write_lsn FROM pg_stat_replication'); ok($result eq 't', 'check'); # set 0 to leave data directories after a successful run ok(1, 'break');