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: "pgsql-hackers" <pgsql-hackers@postgresql.org>
Cc: "Alexander Korotkov" <aekorotkov@gmail.com>,
"Masahiko Sawada" <sawada.mshk@gmail.com>, tomas@vondra.me
Date: 2025-05-02T17:47:48Z
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
- 0003-Add-TAP-test-to-check-physical-repl-slot-advance-dur.v2.patch (text/x-patch) patch v2-0003
- 0004-Keep-WAL-segments-by-slot-s-flushed-restart-LSN.v2.patch (text/x-patch) patch v2-0004
- 0001-Add-injection-points-to-test-replication-slot-advanc.v2.patch (text/x-patch) patch v2-0001
- 0005-Remove-redundant-ReplicationSlotsComputeRequiredLSN-.v2.patch (text/x-patch) patch v2-0005
- 0002-Add-TAP-test-to-check-logical-repl-slot-advance-duri.v2.patch (text/x-patch) patch v2-0002
Dear All, Thank you for the attention to the patch. I updated a patch with a better solution for the master branch which can be easily backported to the other branches as we agree on the final solution. Two tests are introduced which are based on Tomas Vondra's test for logical slots with injection points from the discussion (patches: [1], [2], [3]). Tests are implemented as module tests in src/test/modules/test_replslot_required_lsn directory. I slightly modified the original test for logical slots and created a new simpler test for physical slots without any additional injection points. I prepared a new solution (patch [4]) which is also based on Tomas Vondra's proposal. With a fresh eye, I realized that it can fix the issue as well. It is easy and less invasive to implement. The new solution differs from my original solution: it is backward compatible (doesn't require any changes in ReplicationSlot structure). My original solution can be backward compatible as well if to allocate flushed_restart_lsn in a separate array in shmem, not in the ReplicationSlot structure, but I believe the new solution is the better one. If you still think that my previous solution is the better (I don't think so), I will prepare a backward compatible patch with my previous solution. I also proposed one more commit (patch [5]) which removes unnecessary calls of ReplicationSlotsComputeRequiredLSN function which seems to be redundant. This function updates the oldest required LSN for slots and it is called every time when slots' restart_lsn is changed. Once, we use the oldest required LSN in CreateCheckPoint/CreateRestartPoint to remove old WAL segments, I believe, there is no need to calculate the oldest value immediately when the slot is advancing and in other cases when restart_lsn is changed. It may affect on GetWALAvailability function because the oldest required LSN will be not up to date, but this function seems to be used in the system view pg_get_replication_slots and doesn't affect the logic of old WAL segments removal. I also have some doubts concerning advancing of logical replication slots: the call of ReplicationSlotsComputeRequiredLSN was removed. Not sure, how it can affect on ReplicationSlotsComputeRequiredXmin. This commit is not necessary for the fix but I think it is worth to consider. It may be dropped or applied only to the master branch. This patch can be easily backported to the major release branches. I will quickly prepare the patches for the major releases as we agree on the final solution. I apologize for such too late change in patch when it is already on commitfest. I'm not well experienced yet in the internals of PostgreSQL at the moment, sometimes the better solution needs some time to grow. In doing we learn :) [1] 0001-Add-injection-points-to-test-replication-slot-advanc.v2.patch [2] 0002-Add-TAP-test-to-check-logical-repl-slot-advance-duri.v2.patch [3] 0003-Add-TAP-test-to-check-physical-repl-slot-advance-dur.v2.patch [4] 0004-Keep-WAL-segments-by-slot-s-flushed-restart-LSN.v2.patch [5] 0005-Remove-redundant-ReplicationSlotsComputeRequiredLSN-.v2.patch With best regards, Vitaly