Re: Slot's restart_lsn may point to removed WAL segment after hard restart unexpectedly
Vitaly Davydov <v.davydov@postgrespro.ru>
From: "Vitaly Davydov" <v.davydov@postgrespro.ru>
To: "Amit Kapila" <amit.kapila16@gmail.com>
Cc: "Alexander Korotkov" <aekorotkov@gmail.com>,
"pgsql-hackers" <pgsql-hackers@postgresql.org>,
"Masahiko Sawada" <sawada.mshk@gmail.com>, tomas@vondra.me
Date: 2025-05-26T10:22:51Z
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
Attachments
- v7-0003-Remove-redundant-ReplicationSlotsComputeRequiredLSN-.patch (text/x-patch) patch v7-0003
- v7-0002-Add-TAP-tests-to-check-replication-slot-advance-duri.patch (text/x-patch) patch v7-0002
- v7-0001-Keep-WAL-segments-by-the-flushed-value-of-the-slot-s.patch (text/x-patch) patch v7-0001
Dear Alexander, Amit, All > Amit wrote: > > Is my understanding correct that we need 0001 because > > PhysicalConfirmReceivedLocation() doesn't save the slot to disk after > > changing the slot's restart_lsn? > > Yes. Also, even if it would save slot to the disk, there is still > race condition that concurrent checkpoint could use updated value from > the shared memory to clean old WAL segments, and then crash happens > before we managed to write the slot to the disk. > > How can that happen, if we first write the updated value to disk and > then update the shared memory as we do in > LogicalConfirmReceivedLocation? I guess, that the problem with logical slots still exist. Please, see the tap test: src/test/recovery/t/046_logical_slot.pl from the v6 version of the patch. A race condition may happen when logical slot's restart_lsn was changed but not yet written to the disk. Imagine, there is another physical slot which is advanced at this moment. It recomputes oldest min LSN and takes into account changed but not saved to disk restart_lsn of the logical slot. We come to the situation when the WAL segment for the logical slot's restart_lsn may be truncated after immediate restart. I'm not sure what may happen with two checkpoints which execute in parallel, but I would say that the patch 0001 guarantees that every checkpoint run will trim the WAL segments based on the already saved on disk restart LSNs of slots. The rule to trim the WAL by saved slot's restart_lsn will not be violated. > Amit wrote: > As per my understanding, neither the xmin nor the LSN problem exists > for logical slots. I am pointing this out to indicate we may need to > think of a different solution for physical slots, if these are > problems only for physical slots. As I've already told, it indirectly affects the logical slots as well. > Alexander wrote: > I spend more time on this. The next revision is attached. It > contains revised comments and other cosmetic changes. I'm going to > backpatch 0001 to all supported branches, and 0002 to 17 where > injection points were introduced. Alexander, thank you for polishing the patch. Just my opinion, I would prefer to put tests before the fix due to reason that you can reproduce the problem when simply checkout the commit with tests. Once, the tests are after the fix you are not able to do this way. Anyway, I'm ok with your changes. Thank you! I did some changes in the patch (v7 is attached): * Removed modules/test_replslot_required_lsn directory. It is not needed anymore, once you've moved test files to another directory. * Renamed tests to 046_checkpoint_logical_slot.pl, 047_checkpoint_physical_slot.pl. I believe, such names are more descriptive. Please, consider these changes. With best regards, Vitaly