Logical slot creation/synchronization on a standby may deadlock with recovery conflict resolution
Logical slot creation/synchronization on a standby may deadlock with recovery conflict resolution
While working on eliminating potential deadlock risks of WAIT command, an agent noticed a similar risk for creating logical replication slots on hot standby. Here's how it goes:
A statement such as:
SELECT * FROM pg_create_logical_replication_slot('s', 'pgoutput');
retains its executor-registered statement snapshot while DecodingContextFindStartpoint() searches for a consistent decoding point. Its WAL reader, read_local_xlog_page_guts(), limits reads to GetXLogReplayRecPtr().
There seems to be a problematic ordering:
1) The slot-creation statement holds a snapshot and starts waiting for an additional WAL record to be replayed.
2) The operation in primary generates a cleanup record conflicting with that snapshot, for example through UPDATE followed by VACUUM while hot_standby_feedback is off.
- 3) A running-transactions record needed to complete slot initialization, which follows the cleanup record.
4) Startup process waits for the slot creator's snapshot before replaying cleanup, while the creator waits for the replay to advance.
This seems serious if max_standby_streaming_delay = -1, since nothing left to break it automatically. I haven't found a report in the mailing lists since the debut of this feature. The reason for that could be an encounterance of it requires a fair coincidence of several factors and the symptom of being affected is manifested an interval of delay, which could be masked by the creation of the slot with finite delay setting.
I don't see a clear solution to this potential issue, because the interface is a function, which means that the held snapshots cannot be popped cleanly since they belong to the surrounding executor.
Slot synchronization also has a replay-wait path that may participate in these cycles. I haven't looked into it yet.
Feedbacks are appreciated.
Xuneng Zhou
HighGo Software Co., Ltd.
Re: Logical slot creation/synchronization on a standby may deadlock with recovery conflict resolution
Hi hackers,
While working on eliminating potential deadlock risks of WAIT command, an agent noticed a similar risk for creating logical replication slots on hot standby. Here's how it goes:A statement such as:
SELECT * FROM pg_create_logical_replication_slot('s', 'pgoutput');retains its executor-registered statement snapshot while DecodingContextFindStartpoint() searches for a consistent decoding point. Its WAL reader, read_local_xlog_page_guts(), limits reads to GetXLogReplayRecPtr().
There seems to be a problematic ordering:
1) The slot-creation statement holds a snapshot and starts waiting for an additional WAL record to be replayed.
2) The operation in primary generates a cleanup record conflicting with that snapshot, for example through UPDATE followed by VACUUM while hot_standby_feedback is off
3) A running-transactions record needed to complete slot initialization, which follows the cleanup record
4) Startup process waits for the slot creator's snapshot before replaying cleanup, while the creator waits for the replay to advance
This seems serious if max_standby_streaming_delay = -1, since nothing left to break it automatically. I haven't found a report in the mailing lists since the debut of this feature. The reason for that could be an encounterance of it requires a fair coincidence of several factors and the symptom of being affected is manifested an interval of delay, which could be masked by the creation of the slot with finite delay setting
I don't see a clear solution to this potential issue, because the interface is a function, which means that the held snapshots cannot be popped cleanly since they belong to the surrounding executor.
Slot synchronization also has a replay-wait path that may participate in these cycles. I haven't looked into it yet.
Feedbacks are appreciated.
Xuneng Zhou
HighGo Software Co., Ltd.
Re: Logical slot creation/synchronization on a standby may deadlock with recovery conflict resolution
Hi, On Thu, Sep 24, 2026 at 03:49:35PM +0800, Xuneng Zhou wrote: > Hi hackers, > > I don't see a clear solution to this potential issue, because the interface > is a function, which means that the held snapshots cannot be popped cleanly > since they belong to the surrounding executor. Thanks for the report and reproducers! Thinking out loud, I wonder if we could add a transient PGPROC state while a backend depends on recovery replay. After deadlock_timeout, ResolveRecoveryConflictWithVirtualXIDs() could check whether a VXID in its waitlist has that state set and, if so, use the existing recovery conflict cancellation path. Does that make sense to you and others? If so, I can have a look at preparing a patch. Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com
Re: Logical slot creation/synchronization on a standby may deadlock with recovery conflict resolution
On Thu, Sep 24, 2026 at 5:28 PM Bertrand Drouvot wrote: > > Hi, > > On Thu, Sep 24, 2026 at 03:49:35PM +0800, Xuneng Zhou wrote: > > Hi hackers, > > > > I don't see a clear solution to this potential issue, because the interface > > is a function, which means that the held snapshots cannot be popped cleanly > > since they belong to the surrounding executor. > > Thanks for the report and reproducers! > > Thinking out loud, I wonder if we could add a transient PGPROC state while a > backend depends on recovery replay. After deadlock_timeout, ResolveRecoveryConflictWithVirtualXIDs() > could check whether a VXID in its waitlist has that state set and, if so, use the > existing recovery conflict cancellation path. > > Does that make sense to you and others? If so, I can have a look at preparing a > patch. The overall direction looks promising to me, and I haven't come up with a simpler fix. As a side benefit, it could also break the potential deadlock where 'WAIT' command waits for replay while recovery waits for the same backend's VXID. It would be good to hear more echo before heading to implementation. -- Regards, Xuneng Zhou HighGo Software Co., Ltd.
Re: Logical slot creation/synchronization on a standby may deadlock with recovery conflict resolution
On Thu, Sep 24, 2026 at 7:00 PM Xuneng Zhou wrote: > > On Thu, Sep 24, 2026 at 5:28 PM Bertrand Drouvot > wrote: > > > > Hi, > > > > On Thu, Sep 24, 2026 at 03:49:35PM +0800, Xuneng Zhou wrote: > > > Hi hackers, > > > > > > I don't see a clear solution to this potential issue, because the interface > > > is a function, which means that the held snapshots cannot be popped cleanly > > > since they belong to the surrounding executor. > > > > Thanks for the report and reproducers! > > > > Thinking out loud, I wonder if we could add a transient PGPROC state while a > > backend depends on recovery replay. After deadlock_timeout, ResolveRecoveryConflictWithVirtualXIDs() > > could check whether a VXID in its waitlist has that state set and, if so, use the > > existing recovery conflict cancellation path. > > > > Does that make sense to you and others? If so, I can have a look at preparing a > > patch. > > The overall direction looks promising to me, and I haven't come up > with a simpler fix. As a side benefit, it could also break the > potential deadlock where 'WAIT' command waits for replay while > recovery waits for the same backend's VXID. It would be good to hear > more echo before heading to implementation. Here's the reproducer for the mentioned VXID issue. I think we need to test the fix for it as well, since the underlying issue remains the same. The reproducers could fit in existing test files like 031, but for clarity, they are in standalone files. Also CCed Alexander for this. -- Regards, Xuneng Zhou HighGo Software Co., Ltd.
Re: Logical slot creation/synchronization on a standby may deadlock with recovery conflict resolution
On Thu, Sep 24, 2026 at 7:34 PM Xuneng Zhou wrote: > > On Thu, Sep 24, 2026 at 7:00 PM Xuneng Zhou wrote: > > > > On Thu, Sep 24, 2026 at 5:28 PM Bertrand Drouvot > > wrote: > > > > > > Hi, > > > > > > On Thu, Sep 24, 2026 at 03:49:35PM +0800, Xuneng Zhou wrote: > > > > Hi hackers, > > > > > > > > I don't see a clear solution to this potential issue, because the interface > > > > is a function, which means that the held snapshots cannot be popped cleanly > > > > since they belong to the surrounding executor. > > > > > > Thanks for the report and reproducers! > > > > > > Thinking out loud, I wonder if we could add a transient PGPROC state while a > > > backend depends on recovery replay. After deadlock_timeout, ResolveRecoveryConflictWithVirtualXIDs() > > > could check whether a VXID in its waitlist has that state set and, if so, use the > > > existing recovery conflict cancellation path. > > > > > > Does that make sense to you and others? If so, I can have a look at preparing a > > > patch. > > > > The overall direction looks promising to me, and I haven't come up > > with a simpler fix. As a side benefit, it could also break the > > potential deadlock where 'WAIT' command waits for replay while > > recovery waits for the same backend's VXID. It would be good to hear > > more echo before heading to implementation. > > Here's the reproducer for the mentioned VXID issue. I think we need to > test the fix for it as well, since the underlying issue remains the > same. The reproducers could fit in existing test files like 031, but > for clarity, they are in standalone files. Also CCed Alexander for > this. After more investigation, both slot functions seem also vulnerable to VXID deadlock issue like the WAIT command. My original thought for the fix of the issue is to let ResolveRecoveryConflictWithSnapshot make the blocking decision based on the actual snapshot conflict rather than simply checking whether the VXID has gone away. However, that is more complex and needs more consideration than what you proposed. It could be a follow-up optimization, not necessarily the bug fix. Another problem that both functions suffered is the heavyweight deadlock issue[1]. I also asked Astra to do a broader inspection for the same categorical issue in the tree, and it did find more, which I'll share later. [1] https://www.postgresql.org/message-id/CABPTF7U0gW5%2B-4oL7-qdML-yerZxUb7ku4QXp7JxCYo0qyJ_Tw%40mail.gmail.com -- Regards, Xuneng Zhou HighGo Software Co., Ltd.
Re: Logical slot creation/synchronization on a standby may deadlock with recovery conflict resolution
On Sat, Sep 26, 2026 at 10:55 AM Xuneng Zhou wrote: > > On Thu, Sep 24, 2026 at 7:34 PM Xuneng Zhou wrote: > > > > On Thu, Sep 24, 2026 at 7:00 PM Xuneng Zhou wrote: > > > > > > On Thu, Sep 24, 2026 at 5:28 PM Bertrand Drouvot > > > wrote: > > > > > > > > Hi, > > > > > > > > On Thu, Sep 24, 2026 at 03:49:35PM +0800, Xuneng Zhou wrote: > > > > > Hi hackers, > > > > > > > > > > I don't see a clear solution to this potential issue, because the interface > > > > > is a function, which means that the held snapshots cannot be popped cleanly > > > > > since they belong to the surrounding executor. > > > > > > > > Thanks for the report and reproducers! > > > > > > > > Thinking out loud, I wonder if we could add a transient PGPROC state while a > > > > backend depends on recovery replay. After deadlock_timeout, ResolveRecoveryConflictWithVirtualXIDs() > > > > could check whether a VXID in its waitlist has that state set and, if so, use the > > > > existing recovery conflict cancellation path. > > > > > > > > Does that make sense to you and others? If so, I can have a look at preparing a > > > > patch. > > > > > > The overall direction looks promising to me, and I haven't come up > > > with a simpler fix. As a side benefit, it could also break the > > > potential deadlock where 'WAIT' command waits for replay while > > > recovery waits for the same backend's VXID. It would be good to hear > > > more echo before heading to implementation. > > > > Here's the reproducer for the mentioned VXID issue. I think we need to > > test the fix for it as well, since the underlying issue remains the > > same. The reproducers could fit in existing test files like 031, but > > for clarity, they are in standalone files. Also CCed Alexander for > > this. > > After more investigation, both slot functions seem also vulnerable to > VXID deadlock issue like the WAIT command. My original thought for the > fix of the issue is to let ResolveRecoveryConflictWithSnapshot make > the blocking decision based on the actual snapshot conflict rather > than simply checking whether the VXID has gone away. However, that is > more complex and needs more consideration than what you proposed. It > could be a follow-up optimization, not necessarily the bug fix. > Another problem that both functions suffered is the heavyweight > deadlock issue[1]. > > I also asked Astra to do a broader inspection for the same categorical > issue in the tree, and it did find more, which I'll share later. > > [1] https://www.postgresql.org/message-id/CABPTF7U0gW5%2B-4oL7-qdML-yerZxUb7ku4QXp7JxCYo0qyJ_Tw%40mail.gmail.com Hold on a bit. The deeper I dig, the more interesting it gets. I'll share something very different soon.. -- Regards, Xuneng Zhou HighGo Software Co., Ltd.