]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Checkpoint replication slots later in the checkpoint cycle
authorFujii Masao <fujii@postgresql.org>
Tue, 4 Aug 2026 08:06:38 +0000 (17:06 +0900)
committerFujii Masao <fujii@postgresql.org>
Tue, 4 Aug 2026 08:06:38 +0000 (17:06 +0900)
Previously, CheckPointReplicationSlots() ran at the start of
CheckPointGuts(), while WAL cleanup occurred much later in
CreateCheckPoint() and CreateRestartPoint(), after the buffer write
and ProcessSyncRequests() phases. During a spread checkpoint, this gap
could be several minutes.

During that time, active replication slots could advance their
restart_lsn. However, replicationSlotMinLSN had already been
computed from the older saved values. As a result, KeepLogSeg() could
retain WAL segments that were no longer needed, causing unnecessary
pg_wal growth until the next checkpoint or restartpoint.

Fix this by moving CheckPointReplicationSlots(),
CheckPointSnapBuild(), and CheckPointLogicalRewriteHeap() to just
before CheckPointTwoPhase(), after the buffer write and
ProcessSyncRequests() phases. This makes WAL retention decisions use
the latest replication slot state. The logical snapshot and rewrite heap
cleanup decisions also benefit from the updated saved restart_lsn.

Author: Ants Aasma <ants@cybertec.at>
Author: Hüseyin Demir <huseyin.d3r@gmail.com>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/CANwKhkPCBcTQ_pk06MD5W5YYNnuYHp8dLNuOUz8-5pMBMPY1Bw%40mail.gmail.com

src/backend/access/transam/xlog.c

index f8b939853e945d6156db45198994bc3b8d987b66..b23d8bbbdad60ab608fcb9c3ec5e1db31a9310ab 100644 (file)
@@ -8049,9 +8049,6 @@ static void
 CheckPointGuts(XLogRecPtr checkPointRedo, int flags)
 {
        CheckPointRelationMap();
-       CheckPointReplicationSlots(flags & CHECKPOINT_IS_SHUTDOWN);
-       CheckPointSnapBuild();
-       CheckPointLogicalRewriteHeap();
        CheckPointReplicationOrigin();
 
        /* Write out all dirty data in SLRUs and the main buffer pool */
@@ -8071,7 +8068,17 @@ CheckPointGuts(XLogRecPtr checkPointRedo, int flags)
        CheckpointStats.ckpt_sync_end_t = GetCurrentTimestamp();
        TRACE_POSTGRESQL_BUFFER_CHECKPOINT_DONE();
 
-       /* We deliberately delay 2PC checkpointing as long as possible */
+       /*
+        * Run replication slot checkpointing after buffer writes and
+        * ProcessSyncRequests(), so WAL removal uses a fresher slot retention
+        * horizon and avoids retaining WAL segments that slots no longer need.
+        * Then clean up logical snapshots and rewrite mappings based on the
+        * updated saved restart LSNs.  Also delay 2PC checkpointing as long as
+        * possible.
+        */
+       CheckPointReplicationSlots(flags & CHECKPOINT_IS_SHUTDOWN);
+       CheckPointSnapBuild();
+       CheckPointLogicalRewriteHeap();
        CheckPointTwoPhase(checkPointRedo);
 }