BUG #18754: Logical replication cannot restart if client sent a copyDone

Started by PG Bug reporting formalmost 2 years ago8 messagesbugs

Thread notes

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

@global #bug

#bug
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

Bug reference: 18754
Logged by: S.A.N
Email address: ua.san.alex@gmail.com
PostgreSQL version: 17.2
Operating system: MacOS
Description:

Logical replication cannot restart if the client sent a copyDone, because
the streamingDoneReceiving variable is not reassigned to false in function
StartLogicalReplication, this only happens when physical replication in
function StartReplication
https://github.com/postgres/postgres/blob/master/src/backend/replication/walsender.c#L906

I am writing a logical replication client, sometimes I need to exit copy
mode to send simple SQL queries.

I send copyDone, everything works correctly, I can perform SQL queries, but
then I need to return to replication mode, the START_REPLICATION command
will always completed immediately, because the streamingDoneReceiving
backend variable remains in true, I think it should be redefined in false
when starting logical replication.

Thanks.

#2vignesh C
vignesh21@gmail.com
In reply to: PG Bug reporting form (#1)
Re: BUG #18754: Logical replication cannot restart if client sent a copyDone

On Fri, 27 Dec 2024 at 20:44, PG Bug reporting form
<noreply@postgresql.org> wrote:

The following bug has been logged on the website:

Bug reference: 18754
Logged by: S.A.N
Email address: ua.san.alex@gmail.com
PostgreSQL version: 17.2
Operating system: MacOS
Description:

Logical replication cannot restart if the client sent a copyDone, because
the streamingDoneReceiving variable is not reassigned to false in function
StartLogicalReplication, this only happens when physical replication in
function StartReplication
https://github.com/postgres/postgres/blob/master/src/backend/replication/walsender.c#L906

I am writing a logical replication client, sometimes I need to exit copy
mode to send simple SQL queries.

I send copyDone, everything works correctly, I can perform SQL queries, but
then I need to return to replication mode, the START_REPLICATION command
will always completed immediately, because the streamingDoneReceiving
backend variable remains in true, I think it should be redefined in false
when starting logical replication.

In logical replication, however, the process exits after
streamingDoneReceiving is set to true (this happens when sending the
PqMsg_CopyDone message from libpqrcv_endstreaming), so the issue will
not happen in logical replication.
Could you share the client you're using(if it can be shared), along
with the steps you are following when encountering the issue? This
will help us better understand the scenario and potentially create a
test case to reproduce the issue, or at least validate if setting
'streamingDoneReceiving' to false is sufficient.

Regards,
Vignesh

#3Kacper Kuras
kacperkuras@hotmail.com
In reply to: vignesh C (#2)
Re: BUG #18754: Logical replication cannot restart if client sent a copyDone

Hi,

Following up on this one with steps to reproduce it and a patch with a test. It still happens on current master.

In logical replication, however, the process exits after
streamingDoneReceiving is set to true (this happens when sending the
PqMsg_CopyDone message from libpqrcv_endstreaming), so the issue will
not happen in logical replication.

That holds for the apply worker, which exits after ending the stream.
The walsender itself goes back to command mode once both sides have
sent CopyDone, as the protocol docs describe, and tablesync relies on
that: it ends streaming and then drops its slot over the same
connection. What no in-core client does is start a logical stream on
a connection that has already streamed, and that is what the
reporter's client does.

On a connection with replication=database:

1. START_REPLICATION SLOT s LOGICAL 0/0. The server sends
CopyBothResponse and the changes. The client sends CopyDone, and
the server replies with CopyDone, CommandComplete and
ReadyForQuery.
2. START_REPLICATION SLOT s LOGICAL 0/0 again. The server sends
CopyBothResponse, then CommandComplete and ReadyForQuery right
away, with no data and no CopyDone.

A physical stream on the same connection before the logical one has
the same effect. Physical after logical works, since
StartReplication() resets both flags.

... or at least validate if setting
'streamingDoneReceiving' to false is sufficient.

It isn't. With only streamingDoneReceiving reset, the second stream
delivers data, but when the client sends CopyDone the server doesn't
send its own, because ProcessRepliesIfAny() only does that while
streamingDoneSending is false. The attached patch resets both flags
in StartLogicalReplication(), like StartReplication() does.

The patch adds a TAP test. psql can't do COPY BOTH, so the test talks
to the walsender over a raw socket through raw_connect(), as
src/test/postmaster/t/004_negotiate.pl does. Without the fix, the two
logical streams that follow an earlier stream fail; with it, all four
subtests pass. I ran it on Windows, over TCP and with
PG_TEST_USE_UNIX_SOCKETS=1.

The fix applies cleanly to all supported branches.

To be transparent: I wrote the patch and the test with the help of an
AI assistant. I reproduced the problem and ran the test with and
without the fix on a local build.

Regards,
Kacper Kuras

Attachments:

t72903_3
v1-0001-Reset-streaming-state-at-the-start-of-logical-rep.patchapplication/octet-stream; name=v1-0001-Reset-streaming-state-at-the-start-of-logical-rep.patchDownload+181-1
#4Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Kacper Kuras (#3)
RE: BUG #18754: Logical replication cannot restart if client sent a copyDone

Hi,

That holds for the apply worker, which exits after ending the stream.
The walsender itself goes back to command mode once both sides have
sent CopyDone, as the protocol docs describe, and tablesync relies on
that: it ends streaming and then drops its slot over the same
connection.

To confirm, even the tablesync worker does not send the START_REPLICATION
replication message again, right?

It isn't. With only streamingDoneReceiving reset, the second stream
delivers data, but when the client sends CopyDone the server doesn't
send its own, because ProcessRepliesIfAny() only does that while
streamingDoneSending is false. The attached patch resets both flags
in StartLogicalReplication(), like StartReplication() does.

The fix looks enough, but the main concern is nobody in core can ensure
re-entering the streaming mode can work well.

Best regards,
Hayato Kuroda
FUJITSU LIMITED

#5Kacper Kuras
kacperkuras@hotmail.com
In reply to: Hayato Kuroda (Fujitsu) (#4)
Re: BUG #18754: Logical replication cannot restart if client sent a copyDone

Hi,

To confirm, even the tablesync worker does not send the START_REPLICATION
replication message again, right?

Right. Tablesync only relies on the walsender returning to command
mode: it starts streaming once, to catch up, and after the CopyDone
exchange it drops its slot on that connection and exits. In core,
streaming is started again on the same connection only for physical
replication: walreceiver and pg_receivewal do it when the server ends
the stream at a timeline switch, and StartReplication() resets the
flags for them.

The fix looks enough, but the main concern is nobody in core can ensure
re-entering the streaming mode can work well.

The test in the patch covers what is specific to a second stream: it
starts logical streaming again after a logical and after a physical
stream on the same connection, and checks that the new changes arrive
and that the server answers CopyDone. Beyond that, a second stream
runs the same code as the first one, which the existing tests cover,
so what could break it is state left over from the earlier stream.

I went through that state. Apart from these two flags, the logical
path sets it up again for each stream: the WAL reader, the decoding
context, sentPtr, the slot and the walsender state in
StartLogicalReplication(), the timeout and ping state in WalSndLoop(),
and the timeline variables on every page read in
logical_read_xlog_page(). What is not reset per stream is either
refreshed on every loop iteration, caches a flush position that can be
stale but never ahead, only matters at shutdown, or is used for the
lag columns of pg_stat_replication.

Last month f2dd2a3bd44 fixed another case of state outliving a
START_REPLICATION: the WAL reader and decoding context pointers were
left set after being freed. It clears them at the end of each
replication command and asserts that they are unset before the next
one.

Regards,
Kacper Kuras

#6Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Kacper Kuras (#5)
RE: BUG #18754: Logical replication cannot restart if client sent a copyDone

Hi,

Right. Tablesync only relies on the walsender returning to command
mode: it starts streaming once, to catch up, and after the CopyDone
exchange it drops its slot on that connection and exits.

It's same as my understanding.

In core,
streaming is started again on the same connection only for physical
replication: walreceiver and pg_receivewal do it when the server ends
the stream at a timeline switch, and StartReplication() resets the
flags for them.

Yeah. Per my check abfd192b1 allowed to resume the streaming.

The fix looks enough, but the main concern is nobody in core can ensure
re-entering the streaming mode can work well.

The test in the patch covers what is specific to a second stream: it
starts logical streaming again after a logical and after a physical
stream on the same connection, and checks that the new changes arrive
and that the server answers CopyDone. Beyond that, a second stream
runs the same code as the first one, which the existing tests cover,
so what could break it is state left over from the earlier stream.

Sorry for missing words. My point was that functionalities in core do not re-enter
the streaming mode. Your test code seems to do that, but it looks bit difficult
to put in core (raw_connect() has been used only for testing SSL negotiation so far).
It might be a good timing if build-in logical replication and/or pg_receivelogical
extends to support the usage.

Overall, I'm feeling this is a not a bug fix bug a feature proposal, and stronger
reasoning might be required.

How about others?

Best regards,
Hayato Kuroda
FUJITSU LIMITED

#7Michael Paquier
michael@paquier.xyz
In reply to: Hayato Kuroda (Fujitsu) (#6)
Re: BUG #18754: Logical replication cannot restart if client sent a copyDone

On Wed, Nov 25, 2026 at 04:38:42AM +0000, Hayato Kuroda (Fujitsu) wrote:

Overall, I'm feeling this is a not a bug fix bug a feature proposal, and stronger
reasoning might be required.

How about others?

My first and sole reaction is: why do we need to care about that?
--
Michael

#8Kacper Kuras
kacperkuras@hotmail.com
In reply to: Michael Paquier (#7)
Re: BUG #18754: Logical replication cannot restart if client sent a copyDone

Hi,

On Wed, Nov 25, 2026 at 04:38:42AM +0000, Hayato Kuroda (Fujitsu) wrote:

Overall, I'm feeling this is a not a bug fix bug a feature proposal, and stronger
reasoning might be required.

My first and sole reaction is: why do we need to care about that?

The reporter's client needs to stop a logical stream and continue it
on the same connection. If the client stops the stream with CopyDone,
the protocol's way to end it, the next START_REPLICATION ends right
away without sending the pending changes. If it stops it with a cancel
request instead, the next START_REPLICATION streams normally. So
ending the stream with an error leaves the walsender in a better state
than ending it cleanly. An open Npgsql PR [1]https://github.com/npgsql/npgsql/pull/6716 relies on exactly that:
it ends physical streams with CopyDone but cancels logical ones, since
a logical walsender can't stream again after CopyDone.

A cancel is not a full replacement, though. It ends the command with
an error, and the walsender's error cleanup drops the session's
temporary slots. A temporary slot can't be used from another
connection either, so with one there is no way to continue at all,
not even to start the stream again with different options such as
publication_names. I checked all of this on 18.4. With the patch, the
stream resumes after CopyDone, temporary slot included.

As for bug or feature, the docs promise two things that this breaks:

1. Once both sides have sent CopyDone, "the backend reverts to the
command-processing mode", where a logical walsender accepts
replication commands. A logical START_REPLICATION no longer works
there.
2. START_REPLICATION either fails or answers with CopyBothResponse and
starts streaming. The second logical one sends CopyBothResponse and
then ends without streaming.

Physical replication already starts again on the same connection; the
logical path just doesn't reset streamingDoneSending and
streamingDoneReceiving the way StartReplication() does. The fix is one
line.

About the test: raw_connect() is meant for low-level protocol tests,
as its description in Cluster.pm says, and 002_connection_limits and
003_start_stop use it too. psql doesn't support COPY BOTH, so a raw
socket was the only way I found to test this from TAP.

[1]: https://github.com/npgsql/npgsql/pull/6716

Regards,
Kacper Kuras