|
| 1 | + |
| 2 | +# Copyright (c) 2023, 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; |
| 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(allows_streaming => 'logical'); |
| 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 | +# Insert some data |
| 64 | +$node_publisher->safe_psql('postgres', |
| 65 | + "INSERT INTO test_tbl VALUES (generate_series(1, 5));"); |
| 66 | + |
| 67 | +# Setup logical replication |
| 68 | +my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 69 | +$node_publisher->safe_psql('postgres', |
| 70 | + "CREATE PUBLICATION pub FOR ALL TABLES"); |
| 71 | +$node_subscriber->safe_psql('postgres', |
| 72 | + "CREATE SUBSCRIPTION sub CONNECTION '$publisher_connstr' PUBLICATION pub" |
| 73 | +); |
| 74 | + |
| 75 | +$node_subscriber->wait_for_subscription_sync($node_publisher, 'sub'); |
| 76 | + |
| 77 | +my $result = |
| 78 | + $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM test_tbl"); |
| 79 | + |
| 80 | +is($result, qq(5), "check initial copy was done"); |
| 81 | + |
| 82 | +my $offset = -s $node_publisher->logfile; |
| 83 | + |
| 84 | +# Restart the publisher to ensure that the slot will be flushed if required |
| 85 | +$node_publisher->restart(); |
| 86 | + |
| 87 | +# Wait until the walsender creates decoding context |
| 88 | +$node_publisher->wait_for_log( |
| 89 | + qr/Streaming transactions committing after ([A-F0-9]+\/[A-F0-9]+), reading WAL from ([A-F0-9]+\/[A-F0-9]+)./, |
| 90 | + $offset); |
| 91 | + |
| 92 | +# Extract confirmed_flush from the logfile |
| 93 | +my $log_contents = slurp_file($node_publisher->logfile, $offset); |
| 94 | +$log_contents =~ |
| 95 | + qr/Streaming transactions committing after ([A-F0-9]+\/[A-F0-9]+), reading WAL from ([A-F0-9]+\/[A-F0-9]+)./ |
| 96 | + or die "could not get confirmed_flush_lsn"; |
| 97 | + |
| 98 | +# Ensure that the slot's confirmed_flush LSN is the same as the |
| 99 | +# latest_checkpoint location. |
| 100 | +compare_confirmed_flush($node_publisher, $1); |
| 101 | + |
| 102 | +done_testing(); |
0 commit comments