Re: Set 1s WaitLatch timeout if standby limit has expired in ResolveRecoveryConflictWithBufferPin
Re: Set 1s WaitLatch timeout if standby limit has expired in ResolveRecoveryConflictWithBufferPin
От:
Álvaro Herrera <alvherre@kurilemu.de>
Дата:
On 2025-Jul-06, Anthony Hsu wrote:
> Hi,
>
> I think there is a race scenario where a backend holding a conflicting
> buffer pin isn't promptly canceled even when the standby limit has expired:
This patch seems to hav efallen through the cracks. I created a
commitfest entry for it,
https://commitfest.postgresql.org/patch/6445/
--
Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/
"No tengo por qué estar de acuerdo con lo que pienso"
(Carlos Caszeli)
Re: Set 1s WaitLatch timeout if standby limit has expired in ResolveRecoveryConflictWithBufferPin
Thanks Álvaro for creating a commitfest entry. I've rebased my patch on master to fix the build issues. Also made some minor code comment changes and added a detailed commit message. Please let me know if you have any feedback or questions.
-Anthony
On Fri, Jan 30, 2026 at 9:42 AM Álvaro Herrera <alvherre@kurilemu.de> wrote:
On 2025-Jul-06, Anthony Hsu wrote:
> Hi,
>
> I think there is a race scenario where a backend holding a conflicting
> buffer pin isn't promptly canceled even when the standby limit has expired:
This patch seems to hav efallen through the cracks. I created a
commitfest entry for it,
https://commitfest.postgresql.org/patch/6445/
--
Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/
"No tengo por qué estar de acuerdo con lo que pienso"
(Carlos Caszeli)
Re: Set 1s WaitLatch timeout if standby limit has expired in ResolveRecoveryConflictWithBufferPin
Hi,
I reviewed v2. The bug is real on master and v2 fixes it. With
Dmytro's steps, master never cancels the cursor session. With v2 it
gets FATAL about one second after it takes the pin.
I had a few small comments, so I just made the changes. v3 is
attached.
- It calls WaitLatch directly in standby.c, so proc.c and proc.h are
not touched. That should be easier to backpatch.
- The comment no longer names PROCSIG_RECOVERY_CONFLICT_BUFFERPIN,
which is gone since 17f51ea8187.
- The 1s is a define now, and the commit message is shorter.
The patch in [1] rewrites the same wait as a loop, so the two do not
apply together. Its expired path still sleeps with no timeout, so
this fix is still needed after it.
I reviewed v2. The bug is real on master and v2 fixes it. With
Dmytro's steps, master never cancels the cursor session. With v2 it
gets FATAL about one second after it takes the pin.
I had a few small comments, so I just made the changes. v3 is
attached.
- It calls WaitLatch directly in standby.c, so proc.c and proc.h are
not touched. That should be easier to backpatch.
- The comment no longer names PROCSIG_RECOVERY_CONFLICT_BUFFERPIN,
which is gone since 17f51ea8187.
- The 1s is a define now, and the commit message is shorter.
The patch in [1] rewrites the same wait as a loop, so the two do not
apply together. Its expired path still sleeps with no timeout, so
this fix is still needed after it.
Re: Set 1s WaitLatch timeout if standby limit has expired in ResolveRecoveryConflictWithBufferPin
От:
Andrey Borodin <x4mmm@yandex-team.ru>
Дата:
On 2 Oct 2026, Shihao Zhong wrote: > It calls WaitLatch directly in standby.c, so proc.c and proc.h are > not touched. That should be easier to backpatch. We hit what looks like this bug today on a production standby running our PostgreSQL 17.11 build. Both max_standby_*_delay settings were 30s, but replay lag had already reached 5h41m. During investigation, startup was waiting in BufferPin at an unchanged LSN. Following Dmytro's tip, pg_log_backend_memory_contexts() woke it up. A buffer-pin recovery conflict was logged, and the standby caught up without a restart. I tested v3 on REL_17_STABLE, adapting the signal and wait-event enum names. I used two cursors and SIGSTOP/SIGCONT on the first reader so that the second pin was acquired after the cancellation broadcast. Without the fix, the second reader survived cancellation of the first, and replay needed the diagnostic wakeup. With v3, the second reader was canceled in about a second, without an external wakeup. I'll reopen the CF entry [0]. Please let me know if that's unnecessary because this bug is already tracked elsewhere. Thank you! Best regards, Andrey Borodin. [0] https://commitfest.postgresql.org/patch/6445/
Re: Set 1s WaitLatch timeout if standby limit has expired in ResolveRecoveryConflictWithBufferPin
От:
shihao zhong <zhong950419@gmail.com>
Дата:
Hi Andrey,
Sorry I missed your email. I closed 6445 because it is
duplicate with https://commitfest.postgresql.org/patch/6444/
But I think this is an important issue and we need to fix it.
Thanks,
Shihao
Re: Set 1s WaitLatch timeout if standby limit has expired in ResolveRecoveryConflictWithBufferPin
От:
João Rito <joaovictor.rito@gmail.com>
Дата:
Hi Anthony, Shihao,
Thanks for the patch. I reviewed v3 (CF entry 6445) and tested it locally.
What I tested
- v3 applies cleanly with "git apply" on master at 9f191dce61e (no
rebase needed). Build with --enable-cassert --enable-debug has no
warnings, before or after the patch.
- Reproducer: primary plus streaming standby (pg_basebackup -R),
max_standby_streaming_delay = 2s, log_recovery_conflict_waits = on. I
followed Dmytro's steps, with one change: session A is "COPY (SELECT
t.id, repeat('y', 1000000) FROM bufferpin_test t) TO STDOUT | sleep",
so it sits in ClientWrite holding the pin. After the VACUUM on the
primary I waited 5s so the limit had expired and the startup process
was in BufferCleanup, then opened session B (BEGIN; DECLARE c CURSOR
...; FETCH 5 FROM c), and then killed the "sleep" so session A goes
away.
- Without the patch (master 9f191dce61e): session B is never
cancelled. 30s after session A was gone the startup process was still
in BufferCleanup, B was still idle in transaction, and replay was
behind by 34640 bytes. The log has only "recovery still waiting after
1000.189 ms: recovery conflict on buffer pin" and no "finished
waiting" line.
- With v3: in both runs session B got "FATAL: terminating connection
due to conflict with recovery" while session A was still stuck in
ClientWrite, i.e. before I released A and within my 2s check after B
took its pin. The startup process resumed once A was released
("recovery finished waiting after 7059.663 ms: recovery conflict on
buffer pin" in the first run, 7043.339 ms in the second), and the
replay lag went back to 0.
- make -C src/test/recovery check
PROVE_TESTS=t/031_recovery_conflict.pl passes with the patch (18
tests).
Comments
1. The fix looks correct to me. ProcWaitForSignal() is just
WaitLatch(MyLatch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, 0, ...)
followed by ResetLatch() and CHECK_FOR_INTERRUPTS(), so the new branch
is the same call with WL_TIMEOUT and 1000 ms. After the timeout,
LockBufferForCleanup() rechecks the pin count and calls
ResolveRecoveryConflictWithBufferPin() again, where the limit_expired
branch resends the signal. Other cases are unchanged: when the limit
has not expired the STANDBY_TIMEOUT and STANDBY_DEADLOCK_TIMEOUT path
is untouched, and with max_standby_streaming_delay = -1 (ltime == 0)
the wait stays unbounded, which is intended.
2. The #define is placed between the large header comment and the
function, so the comment block no longer sits directly above
ResolveRecoveryConflictWithBufferPin(). It would read better at the
top of the file, next to the other static state in standby.c.
3. While the limit stays expired, the startup process now sends the
conflict signal to all backends once per second for as long as a pin
is held. This is cheap, and a backend that cannot process the signal
(like session A in ClientWrite) just gets it again, but it may be
worth saying in the commit message or the comment that this is
intended. It also means the deadlock-check part of the existing XXX
comment is not affected, since the expired path never armed
STANDBY_DEADLOCK_TIMEOUT.
4. Tests: the patch has none. A TAP test seems possible with the
pattern above (two sessions with BackgroundPsql, the second one
pinning after the limit has expired and the first one still holding),
but the first backend has to be blocked somewhere it cannot process
the signal, which is why I used ClientWrite. If you think it is too
timing sensitive for the tree, a short comment saying how it was
verified would still help. I can share my script if useful.
5. Commit message: it could add "Reported-by"/"Reviewed-by" trailers
(Dmytro, Andrey, Shihao) and a "Backpatch-through" line. Since v3 does
not touch proc.c or proc.h it should be easy to backpatch, as Shihao
says, and Andrey and Dmytro saw it on 17 and 18.
6. Process: CF entries 6444 and 6445 both point to this same thread,
and both are in "Needs review" for PG20-3. It might be good to close
one of them so reviews are not split.
Best regards,
João Rito
Em ter., 10 de nov. de 2026 às 20:46, shihao zhong
escreveu:
>
> Hi Andrey,
>
> Sorry I missed your email. I closed 6445 because it is
> duplicate with https://commitfest.postgresql.org/patch/6444/
>
> But I think this is an important issue and we need to fix it.
>
> Thanks,
> Shihao
Re: Set 1s WaitLatch timeout if standby limit has expired in ResolveRecoveryConflictWithBufferPin
От:
Xuneng Zhou <xunengzhou@gmail.com>
Дата:
Hi shihao, On Fri, Oct 2, 2026 at 1:38 PM shihao zhong wrote: > > Hi, > > I reviewed v2. The bug is real on master and v2 fixes it. With > Dmytro's steps, master never cancels the cursor session. With v2 it > gets FATAL about one second after it takes the pin. > > I had a few small comments, so I just made the changes. v3 is > attached. > > - It calls WaitLatch directly in standby.c, so proc.c and proc.h are > not touched. That should be easier to backpatch. > - The comment no longer names PROCSIG_RECOVERY_CONFLICT_BUFFERPIN, > which is gone since 17f51ea8187. > - The 1s is a define now, and the commit message is shorter. > > The patch in [1] rewrites the same wait as a loop, so the two do not > apply together. Its expired path still sleeps with no timeout, so > this fix is still needed after it. > > [1] https://www.postgresql.org/message-id/flat/44c24dcf-5710-410f-b1b6-d10b315f3d51@postgrespro.ru Thanks for pointing me here. I've been hovering for this for a while. The diganose of the problem seems good to me. The fix is small and helps to mitigate the stall. What I'm a bit conservative of is the polling and its repeated broadcasts. A more ideal fix seems to be more event-driven -- let the late pinners cancel themselves. I'ven't thought about it carefully. That's certainly more complex and might involve more trade-offs making the idea not pursuable. That being said, the polling itself might not be concerning because we wait for 1s to do a deadlock check before max_standby_*_delay is expired if it's set. However, it seems to violate the promise of max_standby_*_delay to some extent -- the startup could be stalled further even if it has already expired. -- Regards, Xuneng Zhou HighGo Software Co., Ltd.
Re: Set 1s WaitLatch timeout if standby limit has expired in ResolveRecoveryConflictWithBufferPin
От:
shihao zhong <zhong950419@gmail.com>
Дата:
Hi Xuneng,
Thanks for reviewing this patch.
> The diganose of the problem seems good to me. The fix is small and
> helps to mitigate the stall.
> helps to mitigate the stall.
Yes, so I think we can start from the existing patch and get it backpatched.
> A more ideal fix seems to be more
> event-driven -- let the late pinners cancel themselves.If I understand correctly, you are referring to changing the PINBuffer logic
where the backend sees BM_PIN_COUNT_WAITER and cancels itself?
I am not sure if that is the best approach as PinBuffer is in the hot path.
I am not sure if that is the best approach as PinBuffer is in the hot path.
Thanks,
Shihao
Re: Set 1s WaitLatch timeout if standby limit has expired in ResolveRecoveryConflictWithBufferPin
От:
Xuneng Zhou <xunengzhou@gmail.com>
Дата:
On Wed, Nov 11, 2026 at 10:53 AM Xuneng Zhou wrote:
> it's set. However, it seems to violate the promise of
> max_standby_*_delay to some extent -- the startup could be stalled
> further even if it has already expired.
After reading the doc of this parameter closely, this seems a bit
overstated -- it has no promise of freeing replay from stall...
When hot standby is active, this parameter determines how long the
standby server should wait before canceling standby queries that
conflict with about-to-be-applied WAL entries, as described in
.
it does extend the "wait" to exceed the setting value if late pinners
keep coming. Nevertheless, this is just an imagination of the worst
case.
--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.
Re: Set 1s WaitLatch timeout if standby limit has expired in ResolveRecoveryConflictWithBufferPin
От:
Xuneng Zhou <xunengzhou@gmail.com>
Дата:
On Wed, Nov 11, 2026 at 11:25 AM shihao zhong wrote: > > Hi Xuneng, > > Thanks for reviewing this patch. > > > The diganose of the problem seems good to me. The fix is small and > > helps to mitigate the stall. > > Yes, so I think we can start from the existing patch and get it backpatched. Make sense. I'll look into this one. > > A more ideal fix seems to be more > > event-driven -- let the late pinners cancel themselves. > > If I understand correctly, you are referring to changing the PINBuffer logic > where the backend sees BM_PIN_COUNT_WAITER and cancels itself? > > I am not sure if that is the best approach as PinBuffer is in the hot path. Honestly, I don't have a concrete idea of how to do this for now. Just a natural thought of keeping the stall causers away instead of asking them to leave repeatedly. -- Regards, Xuneng Zhou HighGo Software Co., Ltd.
Re: Set 1s WaitLatch timeout if standby limit has expired in ResolveRecoveryConflictWithBufferPin
Hi João,
> is held. ... it may be worth saying in the commit message or the comment that this is intended
Thanks for the review and comments. I have addressed your comments:
> 1. The fix looks correct to me. ...
> 2. The #define ... would read better at the top of the file, next to the other static state in standby.c.
Moved #define to top of the file.
> 3. While the limit stays expired, the startup process now sends the
> conflict signal to all backends once per second for as long as a pin> is held. ... it may be worth saying in the commit message or the comment that this is intended
Updated the commit message to mention this.
> 4. Tests
Added a test to 031_recovery_conflict.pl based on Dmytro's repro.
> 5. Commit message: it could add "Reported-by"/"Reviewed-by" trailers ...
> and a "Backpatch-through" line.
Added these lines. I put "Backpatch-through: 15" since I think this issue could occur in older PG versions too, and PG15 is the oldest still-supported version.
> 6. Process: CF entries 6444 and 6445 both point to this same thread,
> ... It might be good to close one of them so reviews are not split.
I just closed 6444 - we can use 6445 going forward.
Best,
Anthony
On Tue, Nov 10, 2026 at 3:48 PM João Rito <joaovictor.rito@gmail.com> wrote:
Hi Anthony, Shihao,
Thanks for the patch. I reviewed v3 (CF entry 6445) and tested it locally.
What I tested
- v3 applies cleanly with "git apply" on master at 9f191dce61e (no
rebase needed). Build with --enable-cassert --enable-debug has no
warnings, before or after the patch.
- Reproducer: primary plus streaming standby (pg_basebackup -R),
max_standby_streaming_delay = 2s, log_recovery_conflict_waits = on. I
followed Dmytro's steps, with one change: session A is "COPY (SELECT
t.id, repeat('y', 1000000) FROM bufferpin_test t) TO STDOUT | sleep",
so it sits in ClientWrite holding the pin. After the VACUUM on the
primary I waited 5s so the limit had expired and the startup process
was in BufferCleanup, then opened session B (BEGIN; DECLARE c CURSOR
...; FETCH 5 FROM c), and then killed the "sleep" so session A goes
away.
- Without the patch (master 9f191dce61e): session B is never
cancelled. 30s after session A was gone the startup process was still
in BufferCleanup, B was still idle in transaction, and replay was
behind by 34640 bytes. The log has only "recovery still waiting after
1000.189 ms: recovery conflict on buffer pin" and no "finished
waiting" line.
- With v3: in both runs session B got "FATAL: terminating connection
due to conflict with recovery" while session A was still stuck in
ClientWrite, i.e. before I released A and within my 2s check after B
took its pin. The startup process resumed once A was released
("recovery finished waiting after 7059.663 ms: recovery conflict on
buffer pin" in the first run, 7043.339 ms in the second), and the
replay lag went back to 0.
- make -C src/test/recovery check
PROVE_TESTS=t/031_recovery_conflict.pl passes with the patch (18
tests).
Comments
1. The fix looks correct to me. ProcWaitForSignal() is just
WaitLatch(MyLatch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, 0, ...)
followed by ResetLatch() and CHECK_FOR_INTERRUPTS(), so the new branch
is the same call with WL_TIMEOUT and 1000 ms. After the timeout,
LockBufferForCleanup() rechecks the pin count and calls
ResolveRecoveryConflictWithBufferPin() again, where the limit_expired
branch resends the signal. Other cases are unchanged: when the limit
has not expired the STANDBY_TIMEOUT and STANDBY_DEADLOCK_TIMEOUT path
is untouched, and with max_standby_streaming_delay = -1 (ltime == 0)
the wait stays unbounded, which is intended.
2. The #define is placed between the large header comment and the
function, so the comment block no longer sits directly above
ResolveRecoveryConflictWithBufferPin(). It would read better at the
top of the file, next to the other static state in standby.c.
3. While the limit stays expired, the startup process now sends the
conflict signal to all backends once per second for as long as a pin
is held. This is cheap, and a backend that cannot process the signal
(like session A in ClientWrite) just gets it again, but it may be
worth saying in the commit message or the comment that this is
intended. It also means the deadlock-check part of the existing XXX
comment is not affected, since the expired path never armed
STANDBY_DEADLOCK_TIMEOUT.
4. Tests: the patch has none. A TAP test seems possible with the
pattern above (two sessions with BackgroundPsql, the second one
pinning after the limit has expired and the first one still holding),
but the first backend has to be blocked somewhere it cannot process
the signal, which is why I used ClientWrite. If you think it is too
timing sensitive for the tree, a short comment saying how it was
verified would still help. I can share my script if useful.
5. Commit message: it could add "Reported-by"/"Reviewed-by" trailers
(Dmytro, Andrey, Shihao) and a "Backpatch-through" line. Since v3 does
not touch proc.c or proc.h it should be easy to backpatch, as Shihao
says, and Andrey and Dmytro saw it on 17 and 18.
6. Process: CF entries 6444 and 6445 both point to this same thread,
and both are in "Needs review" for PG20-3. It might be good to close
one of them so reviews are not split.
Best regards,
João Rito
Em ter., 10 de nov. de 2026 às 20:46, shihao zhong
<zhong950419@gmail.com> escreveu:
>
> Hi Andrey,
>
> Sorry I missed your email. I closed 6445 because it is
> duplicate with https://commitfest.postgresql.org/patch/6444/
>
> But I think this is an important issue and we need to fix it.
>
> Thanks,
> Shihao