| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test simple scenario involving a standby |
| 5 |
|
| 6 |
use strict; |
| 7 |
use warnings FATAL => 'all'; |
| 8 |
|
| 9 |
use PostgreSQL::Test::Utils; |
| 10 |
use Test::More; |
| 11 |
use PostgreSQL::Test::Cluster; |
| 12 |
|
| 13 |
my $bkplabel = 'backup'; |
| 14 |
my $primary = PostgreSQL::Test::Cluster->new('primary'); |
| 15 |
$primary->init(allows_streaming => 1); |
| 16 |
|
| 17 |
$primary->append_conf( |
| 18 |
'postgresql.conf', qq{ |
| 19 |
track_commit_timestamp = on |
| 20 |
max_wal_senders = 5 |
| 21 |
}); |
| 22 |
$primary->start; |
| 23 |
$primary->backup($bkplabel); |
| 24 |
|
| 25 |
my $standby = PostgreSQL::Test::Cluster->new('standby'); |
| 26 |
$standby->init_from_backup($primary, $bkplabel, has_streaming => 1); |
| 27 |
$standby->start; |
| 28 |
|
| 29 |
for my $i (1 .. 10) |
| 30 |
{ |
| 31 |
$primary->safe_psql('postgres', "create table t$i()"); |
| 32 |
} |
| 33 |
my $primary_ts = $primary->safe_psql('postgres', |
| 34 |
qq{SELECT ts.* FROM pg_class, pg_xact_commit_timestamp(xmin) AS ts WHERE relname = 't10'} |
| 35 |
); |
| 36 |
my $primary_lsn = |
| 37 |
$primary->safe_psql('postgres', 'select pg_current_wal_lsn()'); |
| 38 |
$standby->poll_query_until('postgres', |
| 39 |
qq{SELECT '$primary_lsn'::pg_lsn <= pg_last_wal_replay_lsn()}) |
| 40 |
or die "standby never caught up"; |
| 41 |
|
| 42 |
my $standby_ts = $standby->safe_psql('postgres', |
| 43 |
qq{select ts.* from pg_class, pg_xact_commit_timestamp(xmin) ts where relname = 't10'} |
| 44 |
); |
| 45 |
is($primary_ts, $standby_ts, "standby gives same value as primary"); |
| 46 |
|
| 47 |
$primary->append_conf('postgresql.conf', 'track_commit_timestamp = off'); |
| 48 |
$primary->restart; |
| 49 |
$primary->safe_psql('postgres', 'checkpoint'); |
| 50 |
$primary_lsn = $primary->safe_psql('postgres', 'select pg_current_wal_lsn()'); |
| 51 |
$standby->poll_query_until('postgres', |
| 52 |
qq{SELECT '$primary_lsn'::pg_lsn <= pg_last_wal_replay_lsn()}) |
| 53 |
or die "standby never caught up"; |
| 54 |
$standby->safe_psql('postgres', 'checkpoint'); |
| 55 |
|
| 56 |
# This one should raise an error now |
| 57 |
my ($ret, $standby_ts_stdout, $standby_ts_stderr) = $standby->psql('postgres', |
| 58 |
'select ts.* from pg_class, pg_xact_commit_timestamp(xmin) ts where relname = \'t10\'' |
| 59 |
); |
| 60 |
is($ret, 3, 'standby errors when primary turned feature off'); |
| 61 |
is($standby_ts_stdout, '', |
| 62 |
"standby gives no value when primary turned feature off"); |
| 63 |
like( |
| 64 |
$standby_ts_stderr, |
| 65 |
qr/could not get commit timestamp data/, |
| 66 |
'expected error when primary turned feature off'); |
| 67 |
|
| 68 |
done_testing(); |