From 319254f19f9ddf1b7c93a58d9b0bab40b0a00d85 Mon Sep 17 00:00:00 2001 From: Anthonin Bonnefoy Date: Tue, 24 Feb 2026 09:24:48 +0100 Subject: Fix stuck shutdown due to unflushed records Shutdown sequence may be stuck indefinitely under the following circumstances: - Data checksums is enabled - A logical replication walsender is running - A select in an explicit transaction tries to prune a full heap page, wrote a FPI_FOR_HINT record which crosses the page boundary - The select is rollbacked (or killed) - 'pg_ctl stop' is sent The FPI_FOR_HINT record is likely going to be a contrecord and starts a new page. However, as the select is rollbacked, XLogSetAsyncXactLSN isn't called to advance the LSN to include this record. When the checkpointer starts ShutdownXLOG(), all walsenders will be notified to stop. However, the logical replication walsender will be stuck in the following infinite loop: - Tries to read the last FPI_FOR_HINT record - The page with the record header is read - tot_len > len, the record needs to be reassembled - Tries to read the next page containing the rest of the record. It fails since this page was never written. - xlog reader state is reset with XLogReaderInvalReadState - It goes back to the start of WalSndLoop's loop There are some attempts done by the walsender to flush the WAL using XLogBackgroundFlush. However, XLogBackgroundFlush only writes completed blocks, or up to the latest known async lsn. Since the select was rollbacked, XLogBackgroundFlush doesn't flush the next partial page. This patch fixes the issue by advancing the async LSN, even when the transaction doesn't have an assigned xid. This allows XLogBackgroundFlush to write the necessary partial page when called by the walsender. --- src/backend/access/transam/xact.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c index eba4f063168..15729d9e2da 100644 --- a/src/backend/access/transam/xact.c +++ b/src/backend/access/transam/xact.c @@ -1787,7 +1787,19 @@ RecordTransactionAbort(bool isSubXact) { /* Reset XactLastRecEnd until the next transaction writes something */ if (!isSubXact) + { + /* + * Even if no xid was assigned, some records may have been written + * in the WAL. Report the latest async LSN, so that the WAL writer + * knows to flush those records. This is important when shutting + * down, walsender may use XLogBackgroundFlush to trigger pending + * WAL to be written out. If they're not tracked by async xact + * lsn, they won't be written by XLogBackgroundFlush. + */ + if (XactLastRecEnd != 0) + XLogSetAsyncXactLSN(XactLastRecEnd); XactLastRecEnd = 0; + } return InvalidTransactionId; } -- 2.52.0