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

Поиск
Список
Период
Сортировка
Искать

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

От:
PG 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.

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

От:
vignesh C <vignesh21@gmail.com>
Дата:
On Fri, 27 Dec 2024 at 20:44, PG Bug reporting form
 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


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

От:
Kacper Kuras <kacperkuras@hotmail.com>
Дата:
Вложения:
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

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

От:
"Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
Дата:
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



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

От:
Kacper Kuras <kacperkuras@hotmail.com>
Дата:
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

FAQ