FATAL: could not send end-of-streaming message to primary: no COPY in progress

From: Stepan Neretin <slpmcf(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: FATAL: could not send end-of-streaming message to primary: no COPY in progress
Date: 2026-11-16 17:58:12
Message-ID: CA+Yyo5S_n3ZcZh-3LXNj2-rxqXnbGd8901Yres0LPUS=tTLjaQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

If you shut down a primary that has a streaming standby, the standby still
ends up with this:

LOG: replication terminated by primary server
DETAIL: End of WAL reached on timeline 1 at ...
FATAL: could not send end-of-streaming message to primary: no COPY in
progress

Repro is just primary plus streaming standby, then stop the primary. The
attached TAP test does that.

Thomas reported this in 2016 [1]. Fujii sketched two ways out [2]: either
have walsender wait for CopyDone after "COPY 0", or stop walreceiver from
sending end-of-copy once it already saw that. Kyotaro rejected the first
[3]. The second was mentioned, but as far as I can tell never really
pursued. Andres later called the FATAL itself a bug [4]. Commit
5fd56b9f5b4
improved the related disconnect messaging, but this path is still there.

ISTM Fujii's second option is still the right fix. On shutdown, physical
walsender already ends the COPY with CommandComplete and exits. receive()
consumes that, then endstreaming unconditionally calls PQputCopyEnd(), and
libpq quite reasonably complains that there is no COPY in progress.
Timeline switch is different: there the server waits for the client's
CopyDone, so the existing path remains correct.

The attached patch just remembers that CommandComplete already ended the
COPY, and skips PQputCopyEnd() in that case. No walsender change.

Does this still seem worth fixing?

[1]
https://www.postgresql.org/message-id/CAEepm%3D1fEZ7Dt2r-HEp71BL8Bf%2BHzLmU6gN-yTgH4u7zhxEcXQ%40mail.gmail.com
[2]
https://www.postgresql.org/message-id/CAHGQGwHvzV2J0QodA8x1xCx3CbaBmJTveQeoLFzX8hq5G25jEA%40mail.gmail.com
[3]
https://www.postgresql.org/message-id/20160420.171830.207678798.horiguchi.kyotaro%40lab.ntt.co.jp
[4]
https://www.postgresql.org/message-id/20200323194914.novyhczbkqvk4j5x%40alap3.anarazel.de

Regards,
Stepan Neretin

Attachment Content-Type Size
0002-Add-TAP-test-for-primary-shutdown-end-of-streaming-F.patch text/x-patch 1.7 KB
0001-Avoid-FATAL-when-primary-ends-streaming-with-COPY-0.patch text/x-patch 2.1 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tomas Vondra 2026-11-16 18:01:26 Re: contain_nonstrict_functions_walker() fails to consider JsonExpr and CoerceToDomain
Previous Message Tristan Partin 2026-11-16 17:49:07 Re: compiler attribute alloc_size