From ddd3739eff35141bf00e64a9c5e5500f3c97383b Mon Sep 17 00:00:00 2001 From: Hayato Kuroda Date: Fri, 14 Apr 2023 08:59:03 +0000 Subject: [PATCH v7 3/3] pg_upgrade: Add check function for --include-logical-replication-slots option --- src/bin/pg_upgrade/check.c | 76 +++++++++++++++++++ .../t/003_logical_replication_slots.pl | 58 +++++++++++--- 2 files changed, 123 insertions(+), 11 deletions(-) diff --git a/src/bin/pg_upgrade/check.c b/src/bin/pg_upgrade/check.c index fea159689e..8dda227bf2 100644 --- a/src/bin/pg_upgrade/check.c +++ b/src/bin/pg_upgrade/check.c @@ -9,7 +9,10 @@ #include "postgres_fe.h" +#include "access/xlogrecord.h" +#include "access/xlog_internal.h" #include "catalog/pg_authid_d.h" +#include "catalog/pg_control.h" #include "catalog/pg_collation.h" #include "fe_utils/string_utils.h" #include "mb/pg_wchar.h" @@ -30,6 +33,7 @@ static void check_for_jsonb_9_4_usage(ClusterInfo *cluster); static void check_for_pg_role_prefix(ClusterInfo *cluster); static void check_for_new_tablespace_dir(ClusterInfo *new_cluster); static void check_for_user_defined_encoding_conversions(ClusterInfo *cluster); +static void check_for_logical_replication_slots(ClusterInfo *cluster); /* @@ -103,6 +107,8 @@ check_and_dump_old_cluster(bool live_check) check_for_composite_data_type_usage(&old_cluster); check_for_reg_data_type_usage(&old_cluster); check_for_isn_and_int8_passing_mismatch(&old_cluster); + if (user_opts.include_logical_slots) + check_for_logical_replication_slots(&old_cluster); /* * PG 16 increased the size of the 'aclitem' type, which breaks the on-disk @@ -1402,3 +1408,73 @@ check_for_user_defined_encoding_conversions(ClusterInfo *cluster) else check_ok(); } + +/* + * Verify that all logical replication slots consumed all WALs, except a + * CHECKPOINT_SHUTDOWN record. + */ +static void +check_for_logical_replication_slots(ClusterInfo *cluster) +{ + int i, + ntups, + i_slotname; + bool is_error = false; + PGresult *res; + DbInfo *active_db = &cluster->dbarr.dbs[0]; + PGconn *conn = connectToServer(cluster, active_db->db_name); + + Assert(user_opts.include_logical_slots); + + /* --include-logical-replication-slots can be used since PG16. */ + if (GET_MAJOR_VERSION(cluster->major_version < 1600)) + return; + + prep_status("Checking for logical replication slots"); + + + /* + * Check that all logical replication slots have reached the current WAL + * position, except for the CHECKPOINT_SHUTDOWN record. Even if all WALs + * are consumed before shutting down the node, the checkpointer generates + * a CHECKPOINT_SHUTDOWN record at shutdown, which cannot be consumed by + * any slots. Therefore, we must allow for a difference between + * pg_current_wal_insert_lsn() and confirmed_flush_lsn. + */ +#define SHUTDOWN_RECORD_SIZE (SizeOfXLogRecord + \ + SizeOfXLogRecordDataHeaderShort + \ + sizeof(CheckPoint)) + + res = executeQueryOrDie(conn, + "SELECT slot_name FROM pg_catalog.pg_replication_slots " + "WHERE (pg_catalog.pg_current_wal_insert_lsn() - confirmed_flush_lsn) > %d " + "AND temporary = false AND wal_status IN ('reserved', 'extended');", + (int) (SizeOfXLogShortPHD + SHUTDOWN_RECORD_SIZE)); + +#undef SHUTDOWN_RECORD_SIZE + + ntups = PQntuples(res); + i_slotname = PQfnumber(res, "slot_name"); + + for (i = 0; i < ntups; i++) + { + char *slotname; + + is_error = true; + + slotname = PQgetvalue(res, i, i_slotname); + + pg_log(PG_WARNING, + "\nWARNING: logical replication slot \"%s\" has not consumed WALs yet.", + slotname); + } + + PQclear(res); + PQfinish(conn); + + if (is_error) + pg_fatal("--include-logical-replication-slot requires that all " + "logical replication slots consumed all the WALs"); + + check_ok(); +} diff --git a/src/bin/pg_upgrade/t/003_logical_replication_slots.pl b/src/bin/pg_upgrade/t/003_logical_replication_slots.pl index 81917f3b14..527d448a00 100644 --- a/src/bin/pg_upgrade/t/003_logical_replication_slots.pl +++ b/src/bin/pg_upgrade/t/003_logical_replication_slots.pl @@ -4,6 +4,9 @@ use strict; use warnings; + +use File::Path qw(rmtree); + use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; @@ -11,7 +14,7 @@ use Test::More; # Can be changed to test the other modes. my $mode = $ENV{PG_TEST_PG_UPGRADE_MODE} || '--copy'; -# Initialize publisher node +# Initialize old publisher node my $old_publisher = PostgreSQL::Test::Cluster->new('old_publisher'); $old_publisher->init(allows_streaming => 'logical'); $old_publisher->start; @@ -21,10 +24,48 @@ my $subscriber = PostgreSQL::Test::Cluster->new('subscriber'); $subscriber->init(allows_streaming => 'logical'); $subscriber->start; +# Initialize new publisher node +my $new_publisher = PostgreSQL::Test::Cluster->new('new_publisher'); +$new_publisher->init(allows_streaming => 'logical'); +my $bindir = $new_publisher->config_data('--bindir'); + +# Schema setup $old_publisher->safe_psql('postgres', "CREATE TABLE tbl AS SELECT generate_series(1,10) AS a"); $subscriber->safe_psql('postgres', "CREATE TABLE tbl (a int)"); +# Create a dummy slot on old publisher to fail the test +$old_publisher->safe_psql('postgres', + "SELECT pg_create_logical_replication_slot('dropme_slot', 'pgoutput')"); +$old_publisher->safe_psql('postgres', + "INSERT INTO tbl VALUES (generate_series(11, 20))"); +$old_publisher->stop; + +# Cause a failure at the start of pg_upgrade because dropme_slot is not +# advanced till the INSERT statement +command_fails( + [ + 'pg_upgrade', '--no-sync', + '-d', $old_publisher->data_dir, + '-D', $new_publisher->data_dir, + '-b', $bindir, + '-B', $bindir, + '-s', $new_publisher->host, + '-p', $old_publisher->port, + '-P', $new_publisher->port, + $mode, '--include-logical-replication-slots', + '--check', + ], + 'run of pg_upgrade --check for new instance with inactive slot'); +ok( -d $new_publisher->data_dir . "/pg_upgrade_output.d", + "pg_upgrade_output.d/ not removed after pg_upgrade failure"); + +# Clean up +rmtree($new_publisher->data_dir . "/pg_upgrade_output.d"); +$old_publisher->start; +$old_publisher->safe_psql('postgres', + "SELECT pg_drop_replication_slot('dropme_slot')"); + # Setup logical replication my $old_connstr = $old_publisher->connstr . ' dbname=postgres'; $old_publisher->safe_psql('postgres', @@ -36,18 +77,13 @@ $subscriber->safe_psql('postgres', $subscriber->wait_for_subscription_sync($old_publisher, 'sub'); my $result = $subscriber->safe_psql('postgres', "SELECT count(*) FROM tbl"); -is($result, qq(10), 'check initial rows on subscriber'); +is($result, qq(20), 'check initial rows on subscriber'); # Preparations for upgrading publisher $old_publisher->stop; $subscriber->safe_psql('postgres', "ALTER SUBSCRIPTION sub DISABLE"); -my $new_publisher = PostgreSQL::Test::Cluster->new('new_publisher'); -$new_publisher->init(allows_streaming => 'logical'); - -my $bindir = $new_publisher->config_data('--bindir'); - -# Run pg_upgrade. pg_upgrade_output.d is removed at the end +# Run pg_upgrade again. pg_upgrade_output.d is removed at the end command_ok( [ 'pg_upgrade', '--no-sync', @@ -64,7 +100,7 @@ command_ok( ok( !-d $new_publisher->data_dir . "/pg_upgrade_output.d", "pg_upgrade_output.d/ removed after pg_upgrade success"); -# Check whether the replication slot is copied +# Check whether the replication slot is copied to new publisher $new_publisher->start; $result = $new_publisher->safe_psql('postgres', @@ -80,11 +116,11 @@ $subscriber->safe_psql('postgres', "ALTER SUBSCRIPTION sub ENABLE"); # Check whether changes on the new publisher get replicated to the subscriber $new_publisher->safe_psql('postgres', - "INSERT INTO tbl VALUES (generate_series(11, 20))"); + "INSERT INTO tbl VALUES (generate_series(21, 30))"); $new_publisher->wait_for_catchup('sub'); $result = $subscriber->safe_psql('postgres', "SELECT count(*) FROM tbl"); -is($result, qq(20), 'check changes are shipped to subscriber'); +is($result, qq(30), 'check changes are shipped to subscriber'); done_testing(); -- 2.27.0