| 1 |
# Copyright (c) 2025, PostgreSQL Global Development Group |
| 2 |
# |
| 3 |
# This test verifies the case when the physical slot is advanced during |
| 4 |
# checkpoint. The test checks that the physical slot's restart_lsn still refers |
| 5 |
# to an existed WAL segment after immediate restart. |
| 6 |
# |
| 7 |
use strict; |
| 8 |
use warnings FATAL => 'all'; |
| 9 |
|
| 10 |
use PostgreSQL::Test::Cluster; |
| 11 |
use PostgreSQL::Test::Utils; |
| 12 |
|
| 13 |
use Test::More; |
| 14 |
|
| 15 |
if ($ENV{enable_injection_points} ne 'yes') |
| 16 |
{ |
| 17 |
plan skip_all => 'Injection points not supported by this build'; |
| 18 |
} |
| 19 |
|
| 20 |
my ($node, $result); |
| 21 |
|
| 22 |
$node = PostgreSQL::Test::Cluster->new('mike'); |
| 23 |
$node->init; |
| 24 |
$node->append_conf('postgresql.conf', "wal_level = 'replica'"); |
| 25 |
$node->start; |
| 26 |
|
| 27 |
# Check if the extension injection_points is available, as it may be |
| 28 |
# possible that this script is run with installcheck, where the module |
| 29 |
# would not be installed by default. |
| 30 |
if (!$node->check_extension('injection_points')) |
| 31 |
{ |
| 32 |
plan skip_all => 'Extension injection_points not installed'; |
| 33 |
} |
| 34 |
|
| 35 |
$node->safe_psql('postgres', q(CREATE EXTENSION injection_points)); |
| 36 |
|
| 37 |
# Create a physical replication slot. |
| 38 |
$node->safe_psql('postgres', |
| 39 |
q{select pg_create_physical_replication_slot('slot_physical', true)}); |
| 40 |
|
| 41 |
# Advance slot to the current position, just to have everything "valid". |
| 42 |
$node->safe_psql('postgres', |
| 43 |
q{select pg_replication_slot_advance('slot_physical', pg_current_wal_lsn())} |
| 44 |
); |
| 45 |
|
| 46 |
# Run checkpoint to flush current state to disk and set a baseline. |
| 47 |
$node->safe_psql('postgres', q{checkpoint}); |
| 48 |
|
| 49 |
# Insert 2M rows; that's about 260MB (~20 segments) worth of WAL. |
| 50 |
$node->advance_wal(20); |
| 51 |
|
| 52 |
# Advance slot to the current position, just to have everything "valid". |
| 53 |
$node->safe_psql('postgres', |
| 54 |
q{select pg_replication_slot_advance('slot_physical', pg_current_wal_lsn())} |
| 55 |
); |
| 56 |
|
| 57 |
# Run another checkpoint to set a new restore LSN. |
| 58 |
$node->safe_psql('postgres', q{checkpoint}); |
| 59 |
|
| 60 |
# Another 2M rows; that's about 260MB (~20 segments) worth of WAL. |
| 61 |
$node->advance_wal(20); |
| 62 |
|
| 63 |
my $restart_lsn_init = $node->safe_psql('postgres', |
| 64 |
q{select restart_lsn from pg_replication_slots where slot_name = 'slot_physical'} |
| 65 |
); |
| 66 |
chomp($restart_lsn_init); |
| 67 |
note("restart lsn before checkpoint: $restart_lsn_init"); |
| 68 |
|
| 69 |
# Run another checkpoint, this time in the background, and make it wait |
| 70 |
# on the injection point) so that the checkpoint stops right before |
| 71 |
# removing old WAL segments. |
| 72 |
note('starting checkpoint'); |
| 73 |
|
| 74 |
my $checkpoint = $node->background_psql('postgres'); |
| 75 |
$checkpoint->query_safe( |
| 76 |
q{select injection_points_attach('checkpoint-before-old-wal-removal','wait')} |
| 77 |
); |
| 78 |
$checkpoint->query_until( |
| 79 |
qr/starting_checkpoint/, |
| 80 |
q(\echo starting_checkpoint |
| 81 |
checkpoint; |
| 82 |
\q |
| 83 |
)); |
| 84 |
|
| 85 |
# Wait until the checkpoint stops right before removing WAL segments. |
| 86 |
note('waiting for injection_point'); |
| 87 |
$node->wait_for_event('checkpointer', 'checkpoint-before-old-wal-removal'); |
| 88 |
note('injection_point is reached'); |
| 89 |
|
| 90 |
# OK, we're in the right situation: time to advance the physical slot, which |
| 91 |
# recalculates the required LSN and then unblock the checkpoint, which |
| 92 |
# removes the WAL still needed by the physical slot. |
| 93 |
$node->safe_psql('postgres', |
| 94 |
q{select pg_replication_slot_advance('slot_physical', pg_current_wal_lsn())} |
| 95 |
); |
| 96 |
|
| 97 |
# Continue the checkpoint and wait for its completion. |
| 98 |
my $log_offset = -s $node->logfile; |
| 99 |
$node->safe_psql('postgres', |
| 100 |
q{select injection_points_wakeup('checkpoint-before-old-wal-removal')}); |
| 101 |
$node->wait_for_log(qr/checkpoint complete/, $log_offset); |
| 102 |
|
| 103 |
my $restart_lsn_old = $node->safe_psql('postgres', |
| 104 |
q{select restart_lsn from pg_replication_slots where slot_name = 'slot_physical'} |
| 105 |
); |
| 106 |
chomp($restart_lsn_old); |
| 107 |
note("restart lsn before stop: $restart_lsn_old"); |
| 108 |
|
| 109 |
# Abruptly stop the server. |
| 110 |
$node->stop('immediate'); |
| 111 |
|
| 112 |
$node->start; |
| 113 |
|
| 114 |
# Get the restart_lsn of the slot right after restarting. |
| 115 |
my $restart_lsn = $node->safe_psql('postgres', |
| 116 |
q{select restart_lsn from pg_replication_slots where slot_name = 'slot_physical'} |
| 117 |
); |
| 118 |
chomp($restart_lsn); |
| 119 |
note("restart lsn: $restart_lsn"); |
| 120 |
|
| 121 |
# Get the WAL segment name for the slot's restart_lsn. |
| 122 |
my $restart_lsn_segment = $node->safe_psql('postgres', |
| 123 |
"SELECT pg_walfile_name('$restart_lsn'::pg_lsn)"); |
| 124 |
chomp($restart_lsn_segment); |
| 125 |
|
| 126 |
# Check if the required wal segment exists. |
| 127 |
note("required by slot segment name: $restart_lsn_segment"); |
| 128 |
my $datadir = $node->data_dir; |
| 129 |
ok( -f "$datadir/pg_wal/$restart_lsn_segment", |
| 130 |
"WAL segment $restart_lsn_segment for physical slot's restart_lsn $restart_lsn exists" |
| 131 |
); |
| 132 |
|
| 133 |
done_testing(); |