pgsql: Fix slot synchronization for two_phase enabled slots.

From: Amit Kapila <akapila(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix slot synchronization for two_phase enabled slots.
Date: 2025-04-03 06:57:34
Message-ID: E1u0EW6-002QNU-39@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix slot synchronization for two_phase enabled slots.

The issue is that the transactions prepared before two-phase decoding is
enabled can fail to replicate to the subscriber after being committed on a
promoted standby following a failover. This is because the two_phase_at
field of a slot, which tracks the LSN from which two-phase decoding
starts, is not synchronized to standby servers. Without two_phase_at, the
logical decoding might incorrectly identify prepared transaction as
already replicated to the subscriber after promotion of standby server,
causing them to be skipped.

To address the issue on HEAD, the two_phase_at field of the slot is
exposed by the pg_replication_slots view and allows the slot
synchronization to copy this value to the corresponding synced slot on the
standby server.

This bug is likely to occur if the user toggles the two_phase option to
true after initial slot creation. Given that altering the two_phase option
of a replication slot is not allowed in PostgreSQL 17, this bug is less
likely to occur. We can't change the view/function definition in
backbranch so we can't push the same fix but we are brainstorming an
appropriate solution for PG17.

Author: Zhijie Hou <houzj(dot)fnst(at)fujitsu(dot)com>
Reviewed-by: Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>
Reviewed-by: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
Discussion: https://postgr.es/m/TYAPR01MB5724CC7C288535BBCEEE65DA94A72@TYAPR01MB5724.jpnprd01.prod.outlook.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/4868c96bc8c60145958f1a83bbe9409718a696cb

Modified Files
--------------
doc/src/sgml/system-views.sgml | 11 +++
src/backend/catalog/system_views.sql | 1 +
src/backend/replication/logical/slotsync.c | 14 ++--
src/backend/replication/slotfuncs.c | 8 ++-
src/include/catalog/catversion.h | 2 +-
src/include/catalog/pg_proc.dat | 6 +-
.../recovery/t/040_standby_failover_slots_sync.pl | 81 ++++++++++++++++++++--
src/test/regress/expected/rules.out | 3 +-
8 files changed, 112 insertions(+), 14 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Peter Eisentraut 2025-04-03 07:28:28 pgsql: Update code comment
Previous Message Tom Lane 2025-04-02 20:18:39 pgsql: Remove unnecessary type violation in tsvectorrecv().