pgsql: Fix standby logical slot creation while enabling logical decodin

From: Masahiko Sawada <msawada(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix standby logical slot creation while enabling logical decodin
Date: 2026-11-24 22:23:53
Message-ID: E1xVyvZ-00000000BlX-0Te7@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix standby logical slot creation while enabling logical decoding.

When creating a logical slot during recovery, we reserve WAL from the
last replayed position. However, logical decoding can become enabled
before the replay position moves past the
XLOG_LOGICAL_DECODING_STATUS_CHANGE record that enables it. During
replay, the status is changed before the replay position advances to
the end of the record. At the end of recovery, the status is changed
before the record is even written, and the replay position never moves
past it. A logical slot created concurrently in either window could
get a restart_lsn preceding the record, and then fail with "unexpected
logical decoding status change 1" when decoding it.

Fix by making the restart_lsn of a logical slot created during
recovery at least the end LSN of the last record that enabled logical
decoding. For this, we now also remember the end LSN of the record
written at the end of recovery, and write it while holding
LogicalDecodingControlLock so that concurrent slot creation sees it.

Oversight in commit 67c20979ce7.

Author: Zhijie Hou <houzhijie22(at)gmail(dot)com>
Reviewed-by: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
Discussion: https://postgr.es/m/11c3987c-0be9-4212-8347-9aba81f36580@gmail.com
Backpatch-through: 19

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/4931dab89d35c13a5c2e3e1fe8b31b338a965c73

Modified Files
--------------
src/backend/replication/logical/logicalctl.c | 65 +++++++++++++++++++-------
src/backend/replication/slot.c | 13 +++++-
src/include/replication/logicalctl.h | 1 +
src/test/recovery/t/051_effective_wal_level.pl | 59 +++++++++++++++++++++++
4 files changed, 121 insertions(+), 17 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Masahiko Sawada 2026-11-24 22:23:57 pgsql: Fix standby logical slot creation while enabling logical decodin
Previous Message Álvaro Herrera 2026-11-24 20:38:33 pgsql: Avoid unnecessary allocation for tablesync.c COPY buffer