Re: Slot's restart_lsn may point to removed WAL segment after hard restart unexpectedly
Tomas Vondra <tomas@vondra.me>
From: Tomas Vondra <tomas@vondra.me>
To: pgsql-hackers@lists.postgresql.org
Date: 2024-11-20T22:19:52Z
Lists: pgsql-hackers
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Update comment for ReplicationSlot.last_saved_restart_lsn
- 0810fbb02dbe 19 (unreleased) landed
-
Fix CheckPointReplicationSlots() with max_replication_slots == 0
- 7195c804bd12 18.0 landed
-
Remove excess assert from InvalidatePossiblyObsoleteSlot()
- 70d8a91f82f1 18.0 landed
-
Improve runtime and output of tests for replication slots checkpointing.
- 5ed50f9386f0 17.6 landed
- 4464fddf7b50 18.0 landed
-
Keep WAL segments by slot's last saved restart LSN
- ca307d5cec90 18.0 landed
-
Fix an assert in CheckPointReplicationSlots().
- d1ffcc7fa3c5 17.0 cited
On 11/20/24 18:24, Tomas Vondra wrote: > > ... > > What confuses me a bit is that we update the restart_lsn (and call > ReplicationSlotsComputeRequiredLSN() to recalculate the global value) > all the time. Walsender does that in PhysicalConfirmReceivedLocation for > example. So we actually see the required LSN to move during checkpoint > very often. So how come we don't see the issues much more often? Surely > I miss something important. > This question "How come we don't see this more often?" kept bugging me, and the answer is actually pretty simple. The restart_lsn can move backwards after a hard restart (for the reasons explained), but physical replication does not actually rely on that. The replica keeps track of the LSN it received (well, it uses the same LSN), and on reconnect it sends the startpoint to the primary. And the primary just proceeds use that instead of the (stale) restart LSN for the slot. And the startpoint is guaranteed (I think) to be at least restart_lsn. AFAICS this would work for pg_replication_slot_advance() too, that is if you remember the last LSN the slot advanced to, it should be possible to advance to it just fine. Of course, it requires a way to remember that LSN, which for a replica is not an issue. But this just highlights we can't rely on restart_lsn for this purpose. (Apologies if this repeats something obvious, or something you already said, Vitaly.) regards -- Tomas Vondra