| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test for promotion handling with WAL records generated post-promotion |
| 5 |
# before the first checkpoint is generated. This test case checks for |
| 6 |
# invalid page references at replay based on the minimum consistent |
| 7 |
# recovery point defined. |
| 8 |
use strict; |
| 9 |
use warnings FATAL => 'all'; |
| 10 |
use PostgreSQL::Test::Cluster; |
| 11 |
use PostgreSQL::Test::Utils; |
| 12 |
use Test::More; |
| 13 |
|
| 14 |
# Initialize primary node |
| 15 |
my $alpha = PostgreSQL::Test::Cluster->new('alpha'); |
| 16 |
$alpha->init(allows_streaming => 1); |
| 17 |
# Setting wal_log_hints to off is important to get invalid page |
| 18 |
# references. |
| 19 |
$alpha->append_conf("postgresql.conf", <<EOF); |
| 20 |
wal_log_hints = off |
| 21 |
EOF |
| 22 |
|
| 23 |
# Start the primary |
| 24 |
$alpha->start; |
| 25 |
|
| 26 |
# setup/start a standby |
| 27 |
$alpha->backup('bkp'); |
| 28 |
my $bravo = PostgreSQL::Test::Cluster->new('bravo'); |
| 29 |
$bravo->init_from_backup($alpha, 'bkp', has_streaming => 1); |
| 30 |
$bravo->append_conf('postgresql.conf', <<EOF); |
| 31 |
checkpoint_timeout=1h |
| 32 |
EOF |
| 33 |
$bravo->start; |
| 34 |
|
| 35 |
# Dummy table for the upcoming tests. |
| 36 |
$alpha->safe_psql('postgres', 'create table test1 (a int)'); |
| 37 |
$alpha->safe_psql('postgres', |
| 38 |
'insert into test1 select generate_series(1, 10000)'); |
| 39 |
|
| 40 |
# take a checkpoint |
| 41 |
$alpha->safe_psql('postgres', 'checkpoint'); |
| 42 |
|
| 43 |
# The following vacuum will set visibility map bits and create |
| 44 |
# problematic WAL records. |
| 45 |
$alpha->safe_psql('postgres', 'vacuum verbose test1'); |
| 46 |
# Wait for last record to have been replayed on the standby. |
| 47 |
$alpha->wait_for_catchup($bravo); |
| 48 |
|
| 49 |
# Now force a checkpoint on the standby. This seems unnecessary but for "some" |
| 50 |
# reason, the previous checkpoint on the primary does not reflect on the standby |
| 51 |
# and without an explicit checkpoint, it may start redo recovery from a much |
| 52 |
# older point, which includes even create table and initial page additions. |
| 53 |
$bravo->safe_psql('postgres', 'checkpoint'); |
| 54 |
|
| 55 |
# Now just use a dummy table and run some operations to move minRecoveryPoint |
| 56 |
# beyond the previous vacuum. |
| 57 |
$alpha->safe_psql('postgres', 'create table test2 (a int, b bytea)'); |
| 58 |
$alpha->safe_psql('postgres', |
| 59 |
q{insert into test2 select generate_series(1,10000), sha256(random()::text::bytea)} |
| 60 |
); |
| 61 |
$alpha->safe_psql('postgres', 'truncate test2'); |
| 62 |
|
| 63 |
# Wait again for all records to be replayed. |
| 64 |
$alpha->wait_for_catchup($bravo); |
| 65 |
|
| 66 |
# Do the promotion, which reinitializes minRecoveryPoint in the control |
| 67 |
# file so as WAL is replayed up to the end. |
| 68 |
$bravo->promote; |
| 69 |
|
| 70 |
# Truncate the table on the promoted standby, vacuum and extend it |
| 71 |
# again to create new page references. The first post-recovery checkpoint |
| 72 |
# has not happened yet. |
| 73 |
$bravo->safe_psql('postgres', 'truncate test1'); |
| 74 |
$bravo->safe_psql('postgres', 'vacuum verbose test1'); |
| 75 |
$bravo->safe_psql('postgres', |
| 76 |
'insert into test1 select generate_series(1,1000)'); |
| 77 |
|
| 78 |
# Now crash-stop the promoted standby and restart. This makes sure that |
| 79 |
# replay does not see invalid page references because of an invalid |
| 80 |
# minimum consistent recovery point. |
| 81 |
$bravo->stop('immediate'); |
| 82 |
$bravo->start; |
| 83 |
|
| 84 |
# Check state of the table after full crash recovery. All its data should |
| 85 |
# be here. |
| 86 |
my $psql_out; |
| 87 |
$bravo->psql('postgres', "SELECT count(*) FROM test1", stdout => \$psql_out); |
| 88 |
is($psql_out, '1000', "Check that table state is correct"); |
| 89 |
|
| 90 |
done_testing(); |