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

From: Kacper Kuras <kacperkuras(at)hotmail(dot)com>
To: Michael Paquier <michael(at)paquier(dot)xyz>
Cc: "ua(dot)san(dot)alex(at)gmail(dot)com" <ua(dot)san(dot)alex(at)gmail(dot)com>, "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>, vignesh C <vignesh21(at)gmail(dot)com>, "kuroda(dot)hayato(at)fujitsu(dot)com" <kuroda(dot)hayato(at)fujitsu(dot)com>
Subject: Re: BUG #18754: Logical replication cannot restart if client sent a copyDone
Date: 2026-11-25 17:27:21
Message-ID: VI0P193MB31117074CABE29E81F066EB3BF432@VI0P193MB3111.EURP193.PROD.OUTLOOK.COM
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

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] 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

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2026-11-25 17:30:01 BUG #19949: pg_partition_tree() returns dropped partition from cached OID list
Previous Message PG Bug reporting form 2026-11-25 17:26:07 BUG #19948: Unqualified prefix <= operator is rejected despite being first in search_path