Logical slot creation/synchronization on a standby may deadlock with recovery conflict resolution

Started by Xuneng Zhou2 months ago7 messageshackers

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1Xuneng Zhou
xunengzhou@gmail.com

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.

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.

Attachments:

057_standby_slot_creation_conflict.pltext/x-perl-script; charset=US-ASCII; name=057_standby_slot_creation_conflict.plDownload
058_standby_slot_sync_conflict.pltext/x-perl-script; charset=US-ASCII; name=058_standby_slot_sync_conflict.plDownload
#2Xuneng Zhou
xunengzhou@gmail.com
In reply to: Xuneng Zhou (#1)
Re: Logical slot creation/synchronization on a standby may deadlock with recovery conflict resolution

On Thu, Sep 24, 2026 at 3:49 PM Xuneng Zhou <xunengzhou@gmail.com> wrote:

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.

Sorry for the indentation issue..

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.

#3Bertrand Drouvot
bertranddrouvot.pg@gmail.com
In reply to: Xuneng Zhou (#1)
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

#4Xuneng Zhou
xunengzhou@gmail.com
In reply to: Bertrand Drouvot (#3)
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
<bertranddrouvot.pg@gmail.com> 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.

#5Xuneng Zhou
xunengzhou@gmail.com
In reply to: Xuneng Zhou (#4)
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 <xunengzhou@gmail.com> wrote:

On Thu, Sep 24, 2026 at 5:28 PM Bertrand Drouvot
<bertranddrouvot.pg@gmail.com> 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.

Attachments:

059_standby_wait_for_vxid_conflict.pltext/x-perl-script; charset=US-ASCII; name=059_standby_wait_for_vxid_conflict.plDownload
#6Xuneng Zhou
xunengzhou@gmail.com
In reply to: Xuneng Zhou (#5)
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 <xunengzhou@gmail.com> wrote:

On Thu, Sep 24, 2026 at 7:00 PM Xuneng Zhou <xunengzhou@gmail.com> wrote:

On Thu, Sep 24, 2026 at 5:28 PM Bertrand Drouvot
<bertranddrouvot.pg@gmail.com> 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]/messages/by-id/CABPTF7U0gW5+-4oL7-qdML-yerZxUb7ku4QXp7JxCYo0qyJ_Tw@mail.gmail.com.

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]: /messages/by-id/CABPTF7U0gW5+-4oL7-qdML-yerZxUb7ku4QXp7JxCYo0qyJ_Tw@mail.gmail.com

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.

Attachments:

059_standby_wait_for_vxid_conflict.pltext/x-perl-script; charset=US-ASCII; name=059_standby_wait_for_vxid_conflict.plDownload
063_standby_temp_synced_slot_dbdrop_fatal.pltext/x-perl-script; charset=US-ASCII; name=063_standby_temp_synced_slot_dbdrop_fatal.plDownload
060_standby_slot_creation_lock_conflict.pltext/x-perl-script; charset=US-ASCII; name=060_standby_slot_creation_lock_conflict.plDownload
061_standby_slot_sync_lock_conflict.pltext/x-perl-script; charset=US-ASCII; name=061_standby_slot_sync_lock_conflict.plDownload
062_standby_slot_sync_movedb_finite_delay.pltext/x-perl-script; charset=US-ASCII; name=062_standby_slot_sync_movedb_finite_delay.plDownload
058_standby_slot_sync_conflict.pltext/x-perl-script; charset=US-ASCII; name=058_standby_slot_sync_conflict.plDownload
057_standby_slot_creation_conflict.pltext/x-perl-script; charset=US-ASCII; name=057_standby_slot_creation_conflict.plDownload
#7Xuneng Zhou
xunengzhou@gmail.com
In reply to: Xuneng Zhou (#6)
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 <xunengzhou@gmail.com> wrote:

On Thu, Sep 24, 2026 at 7:34 PM Xuneng Zhou <xunengzhou@gmail.com> wrote:

On Thu, Sep 24, 2026 at 7:00 PM Xuneng Zhou <xunengzhou@gmail.com> wrote:

On Thu, Sep 24, 2026 at 5:28 PM Bertrand Drouvot
<bertranddrouvot.pg@gmail.com> 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] /messages/by-id/CABPTF7U0gW5+-4oL7-qdML-yerZxUb7ku4QXp7JxCYo0qyJ_Tw@mail.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.