logical apply worker's lock waits in subscriber can stall checkpointer in publisher

Started by Fujii Masao10 months ago8 messageshackers

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
1 day ago

@global #bug

#bug
Jump to latest
#1Fujii Masao
masao.fujii@gmail.com

Hi,

While reviewing the patch at [1]/messages/by-id/TYAPR01MB586668E50FC2447AD7F92491F5E89@TYAPR01MB5866.jpnprd01.prod.outlook.com, I noticed a case where lock waits on
a logical apply worker in the subscriber can cause the checkpointer on
the publisher to stall. This seems like unexpected behavior and may
need to be addressed.

The issue can occur as follows:

1. A logical apply worker on the subscriber blocks waiting for a lock.
2. Because the apply worker cannot receive further messages, the walsender's
send buffer on the publisher becomes full.
3. If the walsender then encounters a max_slot_wal_keep_size error,
it attempts to send an error message to the subscriber before exiting.
However, with a full send buffer, the walsender blocks while trying to
send this message.
4. The checkpointer on the publisher calls InvalidateObsoleteReplicationSlots()
and waits for the slot to be released. Since the walsender is stuck and
the slot is not released, the checkpointer also becomes stuck.

This behavior seems problematic, isn't it?

One possible approach to address this issue would be to make the walsender
send the error message in non-blocking mode. Even if the send buffer is full,
the walsender could then exit, allowing the slot to be released and
the checkpointer to proceed. This would mean that, in some cases,
the final error message might not reach the subscriber, which seems
acceptable to me, though others may disagree.

This approach would also help when users want to terminate a walsender
via pg_terminate_backend() but the send buffer is full. In this case, today,
the walsender can similarly block while trying to send the error message.

Another idea would be to change the checkpointer so that
InvalidateObsoleteReplicationSlots() operates in a non-blocking manner.
I'm not sure whether that is feasible, but if immediate invalidation is not
strictly required, the checkpointer could give up and retry later.

Thoughts?

Regards,

[1]: /messages/by-id/TYAPR01MB586668E50FC2447AD7F92491F5E89@TYAPR01MB5866.jpnprd01.prod.outlook.com
/messages/by-id/TYAPR01MB586668E50FC2447AD7F92491F5E89@TYAPR01MB5866.jpnprd01.prod.outlook.com

--
Fujii Masao

#2Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Fujii Masao (#1)
RE: logical apply worker's lock waits in subscriber can stall checkpointer in publisher

Dear Fujii-san,

While reviewing the patch at [1], I noticed a case where lock waits on
a logical apply worker in the subscriber can cause the checkpointer on
the publisher to stall. This seems like unexpected behavior and may
need to be addressed.

The issue can occur as follows:

1. A logical apply worker on the subscriber blocks waiting for a lock.
2. Because the apply worker cannot receive further messages, the walsender's
send buffer on the publisher becomes full.
3. If the walsender then encounters a max_slot_wal_keep_size error,
it attempts to send an error message to the subscriber before exiting.
However, with a full send buffer, the walsender blocks while trying to
send this message.
4. The checkpointer on the publisher calls InvalidateObsoleteReplicationSlots()
and waits for the slot to be released. Since the walsender is stuck and
the slot is not released, the checkpointer also becomes stuck.

I confirmed this could happen if the max_slot_wal_keep_size is enabled
(in other words, the value is not -1).
Per my test, wal_sender_timeout cannot work well because the process is stuck at
the lower layer, but tcp_user_timeout can terminate the process. Can we mention
the workaround in the doc instead of fixing the code?

It won't work for a Unix domain socket connection, but it's not realistic for the
production stage.

Best regards,
Hayato Kuroda
FUJITSU LIMITED

#3Fujii Masao
masao.fujii@gmail.com
In reply to: Hayato Kuroda (Fujitsu) (#2)
Re: logical apply worker's lock waits in subscriber can stall checkpointer in publisher

On Thu, Jan 29, 2026 at 4:03 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:

Dear Fujii-san,

While reviewing the patch at [1], I noticed a case where lock waits on
a logical apply worker in the subscriber can cause the checkpointer on
the publisher to stall. This seems like unexpected behavior and may
need to be addressed.

The issue can occur as follows:

1. A logical apply worker on the subscriber blocks waiting for a lock.
2. Because the apply worker cannot receive further messages, the walsender's
send buffer on the publisher becomes full.
3. If the walsender then encounters a max_slot_wal_keep_size error,
it attempts to send an error message to the subscriber before exiting.
However, with a full send buffer, the walsender blocks while trying to
send this message.
4. The checkpointer on the publisher calls InvalidateObsoleteReplicationSlots()
and waits for the slot to be released. Since the walsender is stuck and
the slot is not released, the checkpointer also becomes stuck.

I confirmed this could happen if the max_slot_wal_keep_size is enabled
(in other words, the value is not -1).
Per my test, wal_sender_timeout cannot work well because the process is stuck at
the lower layer, but tcp_user_timeout can terminate the process. Can we mention
the workaround in the doc instead of fixing the code?

It won't work for a Unix domain socket connection, but it's not realistic for the
production stage.

This approach doesn't seem helpful on platforms that don't support
TCP_USER_TIMEOUT, i.e., tcp_user_timeout is not available. Right?
If I remember correctly, Windows is one of those platforms.

Regards,

--
Fujii Masao

#4Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Fujii Masao (#3)
RE: logical apply worker's lock waits in subscriber can stall checkpointer in publisher

Dear Fujii-san,

This approach doesn't seem helpful on platforms that don't support
TCP_USER_TIMEOUT, i.e., tcp_user_timeout is not available. Right?
If I remember correctly, Windows is one of those platforms.

Good point, documentation said it's not usable for Windows.
The easiest fix I can come up with is to determine a timeout for checkpoint wait;
ConditionVariableTimedSleep() can be used in InvalidatePossiblyObsoleteSlot(),
we can put some LOG and skip invalidating for a while. Not sure how long we
should wait but at least we can use the a fixed value. This might be similar
with your second option.
Regarding the first option, it can solve the root cause, but I'm afraid we may
have to modify very common code.

Best regards,
Hayato Kuroda
FUJITSU LIMITED

#5Fujii Masao
masao.fujii@gmail.com
In reply to: Hayato Kuroda (Fujitsu) (#4)
Re: logical apply worker's lock waits in subscriber can stall checkpointer in publisher

On Fri, Jan 30, 2026 at 1:19 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:

Dear Fujii-san,

This approach doesn't seem helpful on platforms that don't support
TCP_USER_TIMEOUT, i.e., tcp_user_timeout is not available. Right?
If I remember correctly, Windows is one of those platforms.

Good point, documentation said it's not usable for Windows.
The easiest fix I can come up with is to determine a timeout for checkpoint wait;
ConditionVariableTimedSleep() can be used in InvalidatePossiblyObsoleteSlot(),
we can put some LOG and skip invalidating for a while. Not sure how long we
should wait but at least we can use the a fixed value. This might be similar
with your second option.
Regarding the first option, it can solve the root cause, but I'm afraid we may
have to modify very common code.

Yeah, but I'd like to try the first option. Attached is a very WIP patch that
attempts to implement it.

With this patch, when a walsender exits with >= FATAL,
send_message_to_frontend() attempts to send the error message to the standby
in non-blocking mode. If that fails, the walsender gives up on sending
the message and exits immediately.

I'm not yet sure whether treating walsender exit as a special case is
acceptable, but I wanted to share this WIP patch to get feedback.

Regards,

--
Fujii Masao

Attachments:

t53189_5
v1-0001-Allow-walsender-to-send-error-message-to-client-i.patchapplication/octet-stream; name=v1-0001-Allow-walsender-to-send-error-message-to-client-i.patchDownload+51-3
#6Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Fujii Masao (#5)
RE: logical apply worker's lock waits in subscriber can stall checkpointer in publisher

Dear Fujii-san,

Yeah, but I'd like to try the first option. Attached is a very WIP patch that
attempts to implement it.

With this patch, when a walsender exits with >= FATAL,
send_message_to_frontend() attempts to send the error message to the standby
in non-blocking mode. If that fails, the walsender gives up on sending
the message and exits immediately.

I'm still unclear it is OK to modify the fundamental code, but confirmed your
patch can solve the issue.

One concern for me is that the WALs might be more likely to be missed for
streaming replication case. What if the case walreceiver is bit busy thus send
buffer becomes full for a while?
Are there no issues because switchover after the walsender exits with FATAL is
not recommended?

Best regards,
Hayato Kuroda
FUJITSU LIMITED

#7Fujii Masao
masao.fujii@gmail.com
In reply to: Hayato Kuroda (Fujitsu) (#6)
Re: logical apply worker's lock waits in subscriber can stall checkpointer in publisher

On Mon, Feb 2, 2026 at 1:50 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:

Dear Fujii-san,

Yeah, but I'd like to try the first option. Attached is a very WIP patch that
attempts to implement it.

With this patch, when a walsender exits with >= FATAL,
send_message_to_frontend() attempts to send the error message to the standby
in non-blocking mode. If that fails, the walsender gives up on sending
the message and exits immediately.

I'm still unclear it is OK to modify the fundamental code, but confirmed your
patch can solve the issue.

One concern for me is that the WALs might be more likely to be missed for
streaming replication case. What if the case walreceiver is bit busy thus send
buffer becomes full for a while?
Are there no issues because switchover after the walsender exits with FATAL is
not recommended?

I don't think this is problematic, since PostgreSQL has never guaranteed that
WAL data already in the send buffer will actually be delivered to the client
at walsender FATAL exit case. But do you see this differently?

Regards,

--
Fujii Masao

#8Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Fujii Masao (#7)
RE: logical apply worker's lock waits in subscriber can stall checkpointer in publisher

Dear Fujii-san,

One concern for me is that the WALs might be more likely to be missed for
streaming replication case. What if the case walreceiver is bit busy thus send
buffer becomes full for a while?
Are there no issues because switchover after the walsender exits with FATAL is
not recommended?

I don't think this is problematic, since PostgreSQL has never guaranteed that
WAL data already in the send buffer will actually be delivered to the client
at walsender FATAL exit case. But do you see this differently?

Then it might be OK. I did not recognize the policy that it's not guaranteed at FATAL.

Best regards,
Hayato Kuroda
FUJITSU LIMITED