Checkpoint replication slots later
Hi,
Right now replication slots are synced close to the beginning of
CheckpointGuts(). Importantly, before CheckPointBuffers() which for spread
checkpoints might take most of checkpoint_timeout to complete. This is a
problem because this function calculates how much WAL to keep around in
ReplicationSlotsComputeRequiredLSN(). By the time RemoveOldXlogFiles() gets
called this information might be quite stale and we hold onto many WAL
files unnecessarily until the next checkpoint cycle.
As far as I could tell there is no reason for this to happen early, so in
the attached patched I just moved it down closer to the end.
Regards,
Ants Aasma
Hi Ant,
Good idea. Moving CheckPointReplicationSlots() to the end of CheckPointGuts() means we can see more fresher restart_lsn. It's especially valuable for spread checkpoints as you described.
While reading the code for this, I noticed that the ReplicationSlot.last_saved_restart_lsn is completely invisible to operators today.
pg_replication_slots.restart_lsn exposes slot->data.restart_lsn, the current in-memory value, but WAL removal for persistent slots is gated on last_saved_restart_lsn (the value last
durably written to disk at checkpoint), not restart_lsn. See ReplicationSlotsComputeRequiredLSN() at slot.c:1346–1352:
Your patch makes the it more current at cleanup time, which helps. But between checkpoints the gap can still be wide. Should we also expose last_saved_restart_lsn since it's already available and can be exposed. ? Otherwise, the current approach LGTM.
On Sun, May 17, 2026 at 1:38 AM Hüseyin Demir <huseyin.d3r@gmail.com> wrote:
Hi Ant,
Good idea.
+1.
CheckPointSnapBuild() and CheckPointLogicalRewriteHeap() call
ReplicationSlotsComputeLogicalRestartLSN(), which seems to use
last_saved_restart_lsn for persistent logical slots. Since
last_saved_restart_lsn is updated by CheckPointReplicationSlots(),
if those functions run before it, their cleanup decisions are still
based on the restart_lsn saved at the previous checkpoint.
So, isn't it better to move them after CheckPointReplicationSlots()
as well, so that logical snapshot and rewrite mapping cleanup can use
the newly saved restart_lsn?
Of course, we'd first need to confirm that doing so is safe, though.
Regards,
--
Fujii Masao
Hi,
Hi Ant,
Good idea.
+1.
CheckPointSnapBuild() and CheckPointLogicalRewriteHeap() call
ReplicationSlotsComputeLogicalRestartLSN(), which seems to use
last_saved_restart_lsn for persistent logical slots. Since
last_saved_restart_lsn is updated by CheckPointReplicationSlots(),
if those functions run before it, their cleanup decisions are still
based on the restart_lsn saved at the previous checkpoint.So, isn't it better to move them after CheckPointReplicationSlots()
as well, so that logical snapshot and rewrite mapping cleanup can use
the newly saved restart_lsn?Of course, we'd first need to confirm that doing so is safe, though.
I revisited the CheckPointSnapBuild() and
CheckPointLogicalRewriteHeap(). Both functions work idempotently and
are safe to move after CheckPointReplicationSlots(). They benefit from
the updated last_saved_restart_lsn computed in the current checkpoint
cycle, enabling more accurate cleanup decisions.
I ran regression testing comparing the baseline (master) version with
the patched version:
- Checkpoint duration: No change.
- WAL retention: More accurate, because it uses the current
replication slot state.
- Regressions: None detected.
- Dependencies: No new dependencies introduced.
Please see the v2 as attached but we can also commit the v1 since the
only difference is the order of CheckPointSnapBuild and
CheckPointLogicalRewriteHeap working idempotently.
Regards,
Demir.
Attachments:
v2-0001-checkpoint-replication-slots-late.patchapplication/octet-stream; name=v2-0001-checkpoint-replication-slots-late.patchDownload+10-4
I revisited the CheckPointSnapBuild() and
CheckPointLogicalRewriteHeap(). Both functions work idempotently and
are safe to move after CheckPointReplicationSlots(). They benefit from
the updated last_saved_restart_lsn computed in the current checkpoint
cycle, enabling more accurate cleanup decisions.I ran regression testing comparing the baseline (master) version with
the patched version:- Checkpoint duration: No change.
- WAL retention: More accurate, because it uses the current
replication slot state.
- Regressions: None detected.
- Dependencies: No new dependencies introduced.Please see the v2 as attached but we can also commit the v1 since the
only difference is the order of CheckPointSnapBuild and
CheckPointLogicalRewriteHeap working idempotently.
Rebased it against the head.
Regards,
Demir.
Hi,
v3 has been showing "needs rebase" on cfbot for 3 weeks. Cfbot just
can't read v3 as a patch because it's missing some standard
formatting.
Attached is v4: same code as v3, no changes, just regenerated so cfbot
can apply it.
Regards,
Demir.
Show quoted text
I revisited the CheckPointSnapBuild() and
CheckPointLogicalRewriteHeap(). Both functions work idempotently and
are safe to move after CheckPointReplicationSlots(). They benefit from
the updated last_saved_restart_lsn computed in the current checkpoint
cycle, enabling more accurate cleanup decisions.I ran regression testing comparing the baseline (master) version with
the patched version:- Checkpoint duration: No change.
- WAL retention: More accurate, because it uses the current
replication slot state.
- Regressions: None detected.
- Dependencies: No new dependencies introduced.Please see the v2 as attached but we can also commit the v1 since the
only difference is the order of CheckPointSnapBuild and
CheckPointLogicalRewriteHeap working idempotently.Rebased it against the head.
On Sun, Aug 2, 2026 at 7:15 PM Hüseyin Demir <huseyin.d3r@gmail.com> wrote:
Attached is v4: same code as v3, no changes, just regenerated so cfbot
can apply it.
Thanks for updating the patch! It basically looks good to me.
Attached is an updated version. I only revised the code comment and
the commit message; the code itself is unchanged.
Barring any objections, I'll commit it.
Regards,
--
Fujii Masao
Thanks for updating the patch! It basically looks good to me.
Attached is an updated version. I only revised the code comment and
the commit message; the code itself is unchanged.Barring any objections, I'll commit it.
No objections from my side, thanks in advance!
Regards,
Demir.
On Tue, 4 Aug 2026 at 08:19, Hüseyin Demir <huseyin.d3r@gmail.com> wrote:
Thanks for updating the patch! It basically looks good to me.
Attached is an updated version. I only revised the code comment and
the commit message; the code itself is unchanged.Barring any objections, I'll commit it.
No objections from my side, thanks in advance!
Looks good to me too. Big thank you for keeping it up to date.
Ants
On Tue, Aug 4, 2026 at 3:50 PM Ants Aasma <ants.aasma@cybertec.at> wrote:
On Tue, 4 Aug 2026 at 08:19, Hüseyin Demir <huseyin.d3r@gmail.com> wrote:
No objections from my side, thanks in advance!
Looks good to me too. Big thank you for keeping it up to date.
I've pushed the patch. Thanks!
Regards,
--
Fujii Masao