| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test FSM-driven INSERT just after truncation clears FSM slots indicating |
| 5 |
# free space in removed blocks. |
| 6 |
# The FSM mustn't return a page that doesn't exist (anymore). |
| 7 |
use strict; |
| 8 |
use warnings FATAL => 'all'; |
| 9 |
|
| 10 |
use PostgreSQL::Test::Cluster; |
| 11 |
use PostgreSQL::Test::Utils; |
| 12 |
use Test::More; |
| 13 |
|
| 14 |
my $node_primary = PostgreSQL::Test::Cluster->new('primary'); |
| 15 |
$node_primary->init(allows_streaming => 1); |
| 16 |
|
| 17 |
$node_primary->append_conf( |
| 18 |
'postgresql.conf', qq{ |
| 19 |
wal_log_hints = on |
| 20 |
max_prepared_transactions = 5 |
| 21 |
autovacuum = off |
| 22 |
}); |
| 23 |
|
| 24 |
# Create a primary node and its standby, initializing both with some data |
| 25 |
# at the same time. |
| 26 |
$node_primary->start; |
| 27 |
|
| 28 |
$node_primary->backup('primary_backup'); |
| 29 |
my $node_standby = PostgreSQL::Test::Cluster->new('standby'); |
| 30 |
$node_standby->init_from_backup($node_primary, 'primary_backup', |
| 31 |
has_streaming => 1); |
| 32 |
$node_standby->start; |
| 33 |
|
| 34 |
$node_primary->psql( |
| 35 |
'postgres', qq{ |
| 36 |
create table testtab (a int, b char(100)); |
| 37 |
insert into testtab select generate_series(1,1000), 'foo'; |
| 38 |
insert into testtab select generate_series(1,1000), 'foo'; |
| 39 |
delete from testtab where ctid > '(8,0)'; |
| 40 |
}); |
| 41 |
|
| 42 |
# Take a lock on the table to prevent following vacuum from truncating it |
| 43 |
$node_primary->psql( |
| 44 |
'postgres', qq{ |
| 45 |
begin; |
| 46 |
lock table testtab in row share mode; |
| 47 |
prepare transaction 'p1'; |
| 48 |
}); |
| 49 |
|
| 50 |
# Vacuum, update FSM without truncation |
| 51 |
$node_primary->psql('postgres', 'vacuum verbose testtab'); |
| 52 |
|
| 53 |
# Force a checkpoint |
| 54 |
$node_primary->psql('postgres', 'checkpoint'); |
| 55 |
|
| 56 |
# Now do some more insert/deletes, another vacuum to ensure full-page writes |
| 57 |
# are done |
| 58 |
$node_primary->psql( |
| 59 |
'postgres', qq{ |
| 60 |
insert into testtab select generate_series(1,1000), 'foo'; |
| 61 |
delete from testtab where ctid > '(8,0)'; |
| 62 |
vacuum verbose testtab; |
| 63 |
}); |
| 64 |
|
| 65 |
# Ensure all buffers are now clean on the standby |
| 66 |
$node_standby->psql('postgres', 'checkpoint'); |
| 67 |
|
| 68 |
# Release the lock, vacuum again which should lead to truncation |
| 69 |
$node_primary->psql( |
| 70 |
'postgres', qq{ |
| 71 |
rollback prepared 'p1'; |
| 72 |
vacuum verbose testtab; |
| 73 |
}); |
| 74 |
|
| 75 |
$node_primary->psql('postgres', 'checkpoint'); |
| 76 |
my $until_lsn = |
| 77 |
$node_primary->safe_psql('postgres', "SELECT pg_current_wal_lsn();"); |
| 78 |
|
| 79 |
# Wait long enough for standby to receive and apply all WAL |
| 80 |
my $caughtup_query = |
| 81 |
"SELECT '$until_lsn'::pg_lsn <= pg_last_wal_replay_lsn()"; |
| 82 |
$node_standby->poll_query_until('postgres', $caughtup_query) |
| 83 |
or die "Timed out while waiting for standby to catch up"; |
| 84 |
|
| 85 |
# Promote the standby |
| 86 |
$node_standby->promote; |
| 87 |
$node_standby->psql('postgres', 'checkpoint'); |
| 88 |
|
| 89 |
# Restart to discard in-memory copy of FSM |
| 90 |
$node_standby->restart; |
| 91 |
|
| 92 |
# Insert should work on standby |
| 93 |
is( $node_standby->psql( |
| 94 |
'postgres', |
| 95 |
qq{insert into testtab select generate_series(1,1000), 'foo';}), |
| 96 |
0, |
| 97 |
'INSERT succeeds with truncated relation FSM'); |
| 98 |
|
| 99 |
done_testing(); |