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
From bf2b3abcaefc5ff28e2830ea08b17d3233f7d494 Mon Sep 17 00:00:00 2001
From: Stepan Neretin <[email protected]>
Date: Tue, 17 Nov 2026 00:52:37 +0700
Subject: [PATCH 2/2] Add TAP test for primary shutdown end of streaming FATAL

Verify that stopping the primary does not make the standby walreceiver
FATAL with could not send end of streaming message.
---
 .../t/061_primary_shutdown_streaming.pl       | 32 +++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 src/test/recovery/t/061_primary_shutdown_streaming.pl

diff --git a/src/test/recovery/t/061_primary_shutdown_streaming.pl b/src/test/recovery/t/061_primary_shutdown_streaming.pl
new file mode 100644
index 00000000000..ec8748a4f30
--- /dev/null
+++ b/src/test/recovery/t/061_primary_shutdown_streaming.pl
@@ -0,0 +1,32 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+# After primary shutdown, walreceiver must not FATAL with
+# "could not send end-of-streaming message".
+
+use strict;
+use warnings FATAL => 'all';
+
+use PostgreSQL::Test::Cluster;
+use Test::More;
+
+my $node_primary = PostgreSQL::Test::Cluster->new('primary');
+$node_primary->init(allows_streaming => 1);
+$node_primary->start;
+$node_primary->backup('backup');
+
+my $node_standby = PostgreSQL::Test::Cluster->new('standby');
+$node_standby->init_from_backup($node_primary, 'backup', has_streaming => 1);
+$node_standby->start;
+
+$node_primary->wait_for_catchup($node_standby);
+
+my $log_offset = -s $node_standby->logfile;
+$node_primary->stop;
+
+ok(!$node_standby->log_contains(
+	'could not send end-of-streaming message', $log_offset),
+  'walreceiver survives primary shutdown');
+
+$node_standby->stop;
+
+done_testing();
-- 
2.56.0

From 8e5aa285217fa5932b6b5e8102e604bafea5e70d Mon Sep 17 00:00:00 2001
From: Stepan Neretin <[email protected]>
Date: Tue, 17 Nov 2026 00:52:37 +0700
Subject: [PATCH 1/2] Avoid FATAL when primary ends streaming with COPY 0

If the primary shuts down cleanly, physical walsender ends the COPY with
CommandComplete and exits without waiting for CopyDone.  The standby
walreceiver already consumes that result in libpqrcv_receive(), then
libpqrcv_endstreaming() still calls PQputCopyEnd() and fails with no COPY
in progress, which turns into a FATAL.

Remember that CommandComplete already ended the COPY, and skip sending
CopyDone in that case.
---
 .../libpqwalreceiver/libpqwalreceiver.c           | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index dcc738f4e38..5be0d7e337e 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -52,6 +52,8 @@ struct WalReceiverConn
 	bool		logical;
 	/* Buffer for currently read records */
 	char	   *recvBuf;
+	/* Set by receive() when CommandComplete ended the COPY */
+	bool		copyEnded;
 };
 
 /* Prototypes for interface functions */
@@ -709,6 +711,18 @@ libpqrcv_endstreaming(WalReceiverConn *conn, TimeLineID *next_tli)
 {
 	PGresult   *res;
 
+	/*
+	 * Physical walsender can finish with CommandComplete ("COPY 0") and exit
+	 * without waiting for CopyDone.  receive() already consumed that result,
+	 * so there is no COPY left to end.
+	 */
+	if (conn->copyEnded)
+	{
+		conn->copyEnded = false;
+		*next_tli = 0;
+		return;
+	}
+
 	/*
 	 * Send copy-end message.  As in libpqsrv_exec, this could theoretically
 	 * block, but the risk seems small.
@@ -887,6 +901,7 @@ libpqrcv_receive(WalReceiverConn *conn, char **buffer,
 		if (PQresultStatus(res) == PGRES_COMMAND_OK)
 		{
 			PQclear(res);
+			conn->copyEnded = true;
 
 			/* Verify that there are no more results. */
 			res = libpqsrv_get_result(conn->streamConn,
-- 
2.56.0

Reply via email to