On Tue, Nov 17, 2026 at 1:55 PM Fujii Masao <[email protected]> wrote:

> On Tue, Nov 17, 2026 at 2:58 AM Stepan Neretin <[email protected]> wrote:
> > 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?
>
> Yes, I think.
>
> Thanks for the patch! I haven't read it yet, but when I ran CI with it,
> I observed that subscription/029_on_error failed with the following error:
>
> 436/436 postgresql:subscription / subscription/029_on_error
>                  ERROR           257.82s   exit status 29
> ――――――――――――――――――――――――――――――――――――― ✀
> ―――――――――――――――――――――――――――――――――――――
> stderr:
> # poll_query_until timed out executing this query:
> # SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN
> ('r', 's');
> # expecting this output:
> # t
> # last actual query output:
> # f
> # with stderr:
> # Last pg_subscription_rel contents:
> # 16392|16384|f|
> # die: timed out waiting for subscriber to synchronize data at
> /__w/postgresql/postgresql/src/test/subscription/t/029_on_error.pl
> line 130.
>
> (test program exited with status code 29)
>
> ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
>
> Regards,
>
> --
> Fujii Masao
>

Hi Fujii,

Thanks for checking.  The 029_on_error failure was real.

receive() is also used for tablesync COPY TO STDOUT, so CommandComplete
there set copyEnded, and the later catchup stream reused that connection
and skipped CopyDone.  v2 clears the flag in startstreaming().

029_on_error and 061_primary_shutdown_streaming both pass locally with
this.

Attached v2.

Regards,
Stepan Neretin
From 45676b8f73fb01504a248f5cdc0c8a8bdd17b02a Mon Sep 17 00:00:00 2001
From: Stepan Neretin <[email protected]>
Date: Tue, 17 Nov 2026 14:21:11 +0700
Subject: [PATCH v2 1/2] Fix FATAL when primary ends streaming with COPY 0

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

Remember that CommandComplete already ended the COPY and skip
PQputCopyEnd() in that case.  Clear the flag in
libpqrcv_startstreaming(), because receive() is also used for tablesync
COPY TO STDOUT and that must not cause a later replication stream on the
same connection to skip CopyDone.

Discussion: https://postgr.es/m/CA+Yyo5S_n3ZcZh-3LXNj2-rxqXnbGd8901Yres0LPUS=ttl...@mail.gmail.com
---
 .../libpqwalreceiver/libpqwalreceiver.c         | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index dcc738f4e38..bb02236c6c5 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;
+	/* True if CommandComplete already ended the COPY */
+	bool		copyEnded;
 };
 
 /* Prototypes for interface functions */
@@ -618,6 +620,9 @@ libpqrcv_startstreaming(WalReceiverConn *conn,
 	Assert(options->logical == conn->logical);
 	Assert(options->slotname || !options->logical);
 
+	/* receive() may have set this for a prior non-streaming COPY */
+	conn->copyEnded = false;
+
 	initStringInfo(&cmd);
 
 	/* Build the command. */
@@ -709,6 +714,17 @@ libpqrcv_endstreaming(WalReceiverConn *conn, TimeLineID *next_tli)
 {
 	PGresult   *res;
 
+	/*
+	 * If receive() already saw CommandComplete, the COPY is finished and
+	 * there is nothing 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 +903,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

From 6f5379490db530b62c904e0624a34c9deba2a996 Mon Sep 17 00:00:00 2001
From: Stepan Neretin <[email protected]>
Date: Tue, 17 Nov 2026 14:21:11 +0700
Subject: [PATCH v2 2/2] Add TAP test for primary shutdown during streaming

Verify that stopping the primary does not make the standby walreceiver
exit with "could not send end-of-streaming message".

Discussion: https://postgr.es/m/CA+Yyo5S_n3ZcZh-3LXNj2-rxqXnbGd8901Yres0LPUS=ttl...@mail.gmail.com
---
 .../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

Reply via email to