Subject: pgsql: Add a slot synchronization function.

From: Amit Kapila <[email-masked]>
Date: Wed, 14 Feb 2024 04:43:18 +0000

Add a slot synchronization function.

This commit introduces a new SQL function pg_sync_replication_slots()
which is used to synchronize the logical replication slots from the
primary server to the physical standby so that logical replication can be
resumed after a failover or planned switchover.

A new 'synced' flag is introduced in pg_replication_slots view, indicating
whether the slot has been synchronized from the primary server. On a
standby, synced slots cannot be dropped or consumed, and any attempt to
perform logical decoding on them will result in an error.

The logical replication slots on the primary can be synchronized to the
hot standby by using the 'failover' parameter of
pg-create-logical-replication-slot(), or by using the 'failover' option of
CREATE SUBSCRIPTION during slot creation, and then calling
pg_sync_replication_slots() on standby. For the synchronization to work,
it is mandatory to have a physical replication slot between the primary
and the standby aka 'primary_slot_name' should be configured on the
standby, and 'hot_standby_feedback' must be enabled on the standby. It is
also necessary to specify a valid 'dbname' in the 'primary_conninfo'.

If a logical slot is invalidated on the primary, then that slot on the
standby is also invalidated.

If a logical slot on the primary is valid but is invalidated on the
standby, then that slot is dropped but will be recreated on the standby in
the next pg_sync_replication_slots() call provided the slot still exists
on the primary server. It is okay to recreate such slots as long as these
are not consumable on standby (which is the case currently). This
situation may occur due to the following reasons:
- The 'max_slot_wal_keep_size' on the standby is insufficient to retain
WAL records from the restart_lsn of the slot.
- 'primary_slot_name' is temporarily reset to null and the physical slot
is removed.

The slot synchronization status on the standby can be monitored using the
'synced' column of pg_replication_slots view.

A functionality to automatically synchronize slots by a background worker
and allow logical walsenders to wait for the physical will be done in
subsequent commits.

Author: Hou Zhijie, Shveta Malik, Ajin Cherian based on an earlier version by Peter Eisentraut
Reviewed-by: Masahiko Sawada, Bertrand Drouvot, Peter Smith, Dilip Kumar, Nisha Moond, Kuroda Hayato, Amit Kapila
Discussion: https://postgr.es/m/[email-masked]

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/ddd5f4f54a026db6a6692876d0d44aef902ab686

Modified Files
--------------
contrib/test_decoding/expected/permissions.out     |   3 +
contrib/test_decoding/expected/slot.out            |   2 +
contrib/test_decoding/sql/permissions.sql          |   1 +
contrib/test_decoding/sql/slot.sql                 |   1 +
doc/src/sgml/config.sgml                           |   9 +-
doc/src/sgml/func.sgml                             |  35 +-
doc/src/sgml/logicaldecoding.sgml                  |  56 ++
doc/src/sgml/protocol.sgml                         |   6 +-
doc/src/sgml/system-views.sgml                     |  20 +-
src/backend/catalog/system_views.sql               |   3 +-
src/backend/replication/logical/Makefile           |   1 +
src/backend/replication/logical/logical.c          |  12 +
src/backend/replication/logical/meson.build        |   1 +
src/backend/replication/logical/slotsync.c         | 906 +++++++++++++++++++++
src/backend/replication/slot.c                     | 106 ++-
src/backend/replication/slotfuncs.c                |  74 +-
src/backend/replication/walsender.c                |  19 +-
src/backend/storage/ipc/ipci.c                     |   3 +
src/include/catalog/catversion.h                   |   2 +-
src/include/catalog/pg_proc.dat                    |  10 +-
src/include/replication/slot.h                     |  19 +-
src/include/replication/slotsync.h                 |  23 +
src/include/replication/walsender.h                |   3 +
.../recovery/t/040_standby_failover_slots_sync.pl  | 237 ++++++
src/test/regress/expected/rules.out                |   5 +-
src/tools/pgindent/typedefs.list                   |   2 +
26 files changed, 1522 insertions(+), 37 deletions(-)

---

From: Amit Kapila <[email-masked]>
Date: Wed, 14 Feb 2024 10:34:46 +0530

> Add a slot synchronization function.

There are a few BF failures and it seems related to this commit. I am
looking into those.

-- 
With Regards,
Amit Kapila.