| 1 |
|
| 2 |
# Copyright (c) 2023-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test logical replication slots are always flushed to disk during a shutdown |
| 5 |
# checkpoint. |
| 6 |
|
| 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 |
sub compare_confirmed_flush |
| 15 |
{ |
| 16 |
my ($node, $confirmed_flush_from_log) = @_; |
| 17 |
|
| 18 |
# Fetch Latest checkpoint location from the control file |
| 19 |
my ($stdout, $stderr) = |
| 20 |
run_command([ 'pg_controldata', $node->data_dir ]); |
| 21 |
my @control_data = split("\n", $stdout); |
| 22 |
my $latest_checkpoint = undef; |
| 23 |
foreach (@control_data) |
| 24 |
{ |
| 25 |
if ($_ =~ /^Latest checkpoint location:\s*(.*)$/mg) |
| 26 |
{ |
| 27 |
$latest_checkpoint = $1; |
| 28 |
last; |
| 29 |
} |
| 30 |
} |
| 31 |
die "Latest checkpoint location not found in control file\n" |
| 32 |
unless defined($latest_checkpoint); |
| 33 |
|
| 34 |
# Is it same as the value read from log? |
| 35 |
ok( $latest_checkpoint eq $confirmed_flush_from_log, |
| 36 |
"Check that the slot's confirmed_flush LSN is the same as the latest_checkpoint location" |
| 37 |
); |
| 38 |
|
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
# Initialize publisher node |
| 43 |
my $node_publisher = PostgreSQL::Test::Cluster->new('pub'); |
| 44 |
$node_publisher->init(allows_streaming => 'logical'); |
| 45 |
# Avoid checkpoint during the test, otherwise, the latest checkpoint location |
| 46 |
# will change. |
| 47 |
$node_publisher->append_conf( |
| 48 |
'postgresql.conf', q{ |
| 49 |
checkpoint_timeout = 1h |
| 50 |
autovacuum = off |
| 51 |
}); |
| 52 |
$node_publisher->start; |
| 53 |
|
| 54 |
# Create subscriber node |
| 55 |
my $node_subscriber = PostgreSQL::Test::Cluster->new('sub'); |
| 56 |
$node_subscriber->init; |
| 57 |
$node_subscriber->start; |
| 58 |
|
| 59 |
# Create tables |
| 60 |
$node_publisher->safe_psql('postgres', "CREATE TABLE test_tbl (id int)"); |
| 61 |
$node_subscriber->safe_psql('postgres', "CREATE TABLE test_tbl (id int)"); |
| 62 |
|
| 63 |
# To avoid a shutdown checkpoint WAL record (that gets generated as part of |
| 64 |
# the publisher restart below) falling into a new page, advance the WAL |
| 65 |
# segment. Otherwise, the confirmed_flush_lsn and shutdown_checkpoint |
| 66 |
# location won't match. |
| 67 |
$node_publisher->advance_wal(1); |
| 68 |
|
| 69 |
# Insert some data |
| 70 |
$node_publisher->safe_psql('postgres', |
| 71 |
"INSERT INTO test_tbl VALUES (generate_series(1, 5));"); |
| 72 |
|
| 73 |
# Setup logical replication |
| 74 |
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 75 |
$node_publisher->safe_psql('postgres', |
| 76 |
"CREATE PUBLICATION pub FOR ALL TABLES"); |
| 77 |
$node_subscriber->safe_psql('postgres', |
| 78 |
"CREATE SUBSCRIPTION sub CONNECTION '$publisher_connstr' PUBLICATION pub" |
| 79 |
); |
| 80 |
|
| 81 |
$node_subscriber->wait_for_subscription_sync($node_publisher, 'sub'); |
| 82 |
|
| 83 |
my $result = |
| 84 |
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM test_tbl"); |
| 85 |
|
| 86 |
is($result, qq(5), "check initial copy was done"); |
| 87 |
|
| 88 |
my $offset = -s $node_publisher->logfile; |
| 89 |
|
| 90 |
# Note: Don't insert any data on the publisher that may cause the shutdown |
| 91 |
# checkpoint to fall into a new WAL file. See the comments atop advance_wal() |
| 92 |
# above. |
| 93 |
|
| 94 |
# Restart the publisher to ensure that the slot will be flushed if required |
| 95 |
$node_publisher->restart(); |
| 96 |
|
| 97 |
# Wait until the walsender creates decoding context |
| 98 |
$node_publisher->wait_for_log( |
| 99 |
qr/Streaming transactions committing after ([A-F0-9]+\/[A-F0-9]+), reading WAL from ([A-F0-9]+\/[A-F0-9]+)./, |
| 100 |
$offset); |
| 101 |
|
| 102 |
# Extract confirmed_flush from the logfile |
| 103 |
my $log_contents = slurp_file($node_publisher->logfile, $offset); |
| 104 |
$log_contents =~ |
| 105 |
qr/Streaming transactions committing after ([A-F0-9]+\/[A-F0-9]+), reading WAL from ([A-F0-9]+\/[A-F0-9]+)./ |
| 106 |
or die "could not get confirmed_flush_lsn"; |
| 107 |
|
| 108 |
# Ensure that the slot's confirmed_flush LSN is the same as the |
| 109 |
# latest_checkpoint location. |
| 110 |
compare_confirmed_flush($node_publisher, $1); |
| 111 |
|
| 112 |
done_testing(); |