diff options
author | Amit Kapila <akapila@postgresql.org> | 2023-10-26 06:54:16 +0530 |
---|---|---|
committer | Amit Kapila <akapila@postgresql.org> | 2023-10-26 07:06:55 +0530 |
commit | 29d0a77fa6606f9c01ba17311fc452dabd3f793d (patch) | |
tree | c1d63845bf43db3ab71cb16a43ee53867b6119dc /src/backend/utils/adt/pg_upgrade_support.c | |
parent | bddc2f7480374023218427a0185145a127207c28 (diff) | |
download | postgresql-29d0a77fa6606f9c01ba17311fc452dabd3f793d.tar.gz postgresql-29d0a77fa6606f9c01ba17311fc452dabd3f793d.zip |
Migrate logical slots to the new node during an upgrade.
While reading information from the old cluster, a list of logical
slots is fetched. At the later part of upgrading, pg_upgrade revisits the
list and restores slots by executing pg_create_logical_replication_slot()
on the new cluster. Migration of logical replication slots is only
supported when the old cluster is version 17.0 or later.
If the old node has invalid slots or slots with unconsumed WAL records,
the pg_upgrade fails. These checks are needed to prevent data loss.
The significant advantage of this commit is that it makes it easy to
continue logical replication even after upgrading the publisher node.
Previously, pg_upgrade allowed copying publications to a new node. With
this patch, adjusting the connection string to the new publisher will
cause the apply worker on the subscriber to connect to the new publisher
automatically. This enables seamless continuation of logical replication,
even after an upgrade.
Author: Hayato Kuroda, Hou Zhijie
Reviewed-by: Peter Smith, Bharath Rupireddy, Dilip Kumar, Vignesh C, Shlok Kyal
Discussion: http://postgr.es/m/TYAPR01MB58664C81887B3AF2EB6B16E3F5939@TYAPR01MB5866.jpnprd01.prod.outlook.com
Discussion: http://postgr.es/m/CAA4eK1+t7xYcfa0rEQw839=b2MzsfvYDPz3xbD+ZqOdP3zpKYg@mail.gmail.com
Diffstat (limited to 'src/backend/utils/adt/pg_upgrade_support.c')
-rw-r--r-- | src/backend/utils/adt/pg_upgrade_support.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/backend/utils/adt/pg_upgrade_support.c b/src/backend/utils/adt/pg_upgrade_support.c index 0186636d9f8..2f6fc86c3df 100644 --- a/src/backend/utils/adt/pg_upgrade_support.c +++ b/src/backend/utils/adt/pg_upgrade_support.c @@ -17,6 +17,7 @@ #include "catalog/pg_type.h" #include "commands/extension.h" #include "miscadmin.h" +#include "replication/logical.h" #include "utils/array.h" #include "utils/builtins.h" @@ -261,3 +262,46 @@ binary_upgrade_set_missing_value(PG_FUNCTION_ARGS) PG_RETURN_VOID(); } + +/* + * Verify the given slot has already consumed all the WAL changes. + * + * Returns true if there are no decodable WAL records after the + * confirmed_flush_lsn. Otherwise false. + * + * This is a special purpose function to ensure that the given slot can be + * upgraded without data loss. + */ +Datum +binary_upgrade_logical_slot_has_caught_up(PG_FUNCTION_ARGS) +{ + Name slot_name; + XLogRecPtr end_of_wal; + bool found_pending_wal; + + CHECK_IS_BINARY_UPGRADE; + + /* We must check before dereferencing the argument */ + if (PG_ARGISNULL(0)) + elog(ERROR, "null argument to binary_upgrade_validate_wal_records is not allowed"); + + CheckSlotPermissions(); + + slot_name = PG_GETARG_NAME(0); + + /* Acquire the given slot */ + ReplicationSlotAcquire(NameStr(*slot_name), true); + + Assert(SlotIsLogical(MyReplicationSlot)); + + /* Slots must be valid as otherwise we won't be able to scan the WAL */ + Assert(MyReplicationSlot->data.invalidated == RS_INVAL_NONE); + + end_of_wal = GetFlushRecPtr(NULL); + found_pending_wal = LogicalReplicationSlotHasPendingWal(end_of_wal); + + /* Clean up */ + ReplicationSlotRelease(); + + PG_RETURN_BOOL(!found_pending_wal); +} |