v1-0001-Fix-pg_stat_wal_receiver-to-show-CONNECTING-statu.patch

application/octet-stream

Filename: v1-0001-Fix-pg_stat_wal_receiver-to-show-CONNECTING-statu.patch
Type: application/octet-stream
Part: 0
Message: Fix pg_stat_wal_receiver to show CONNECTING status
From 817d3c881dd6d3912635c89d7aa24407576a85d8 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Tue, 19 May 2026 13:32:15 +0800
Subject: [PATCH v1] Fix pg_stat_wal_receiver to show CONNECTING status

Commit a36164e7465 added a CONNECTING status for the WAL receiver, but
pg_stat_wal_receiver still returned no row while ready_to_display was
false.  That made the new status invisible during the connection setup
phase.

Allow pg_stat_wal_receiver to show the WAL receiver PID and status once
they have been advertised, even before connection details are ready to
display.  Keep the remaining fields NULL until conninfo has been
obfuscated.

Author: Chao Li <lic@highgo.com>
---
 src/backend/replication/walreceiver.c | 36 +++++++++++++++++----------
 1 file changed, 23 insertions(+), 13 deletions(-)

diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c
index 07eac07b9ce..78d948adc49 100644
--- a/src/backend/replication/walreceiver.c
+++ b/src/backend/replication/walreceiver.c
@@ -1474,21 +1474,10 @@ pg_stat_get_wal_receiver(PG_FUNCTION_ARGS)
 	strlcpy(conninfo, WalRcv->conninfo, sizeof(conninfo));
 	SpinLockRelease(&WalRcv->mutex);
 
-	/*
-	 * No WAL receiver (or not ready yet), just return a tuple with NULL
-	 * values
-	 */
-	if (pid == 0 || !ready_to_display)
+	/* No WAL receiver, just return a tuple with NULL values */
+	if (pid == 0)
 		PG_RETURN_NULL();
 
-	/*
-	 * Read "writtenUpto" without holding a spinlock.  Note that it may not be
-	 * consistent with the other shared variables of the WAL receiver
-	 * protected by a spinlock, but this should not be used for data integrity
-	 * checks.
-	 */
-	written_lsn = pg_atomic_read_u64(&WalRcv->writtenUpto);
-
 	/* determine result type */
 	if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
 		elog(ERROR, "return type must be a row type");
@@ -1512,6 +1501,27 @@ pg_stat_get_wal_receiver(PG_FUNCTION_ARGS)
 	{
 		values[1] = CStringGetTextDatum(WalRcvGetStateString(state));
 
+		/*
+		 * The WAL receiver advertises its PID and state before connection
+		 * details are safe to display.  Show the state, but keep all other
+		 * details hidden until conninfo has been obfuscated.
+		 */
+		if (!ready_to_display)
+		{
+			memset(&nulls[2], true, sizeof(bool) * (tupdesc->natts - 2));
+
+			PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values,
+															  nulls)));
+		}
+
+		/*
+		 * Read "writtenUpto" without holding a spinlock.  Note that it may
+		 * not be consistent with the other shared variables of the WAL
+		 * receiver protected by a spinlock, but this should not be used for
+		 * data integrity checks.
+		 */
+		written_lsn = pg_atomic_read_u64(&WalRcv->writtenUpto);
+
 		if (!XLogRecPtrIsValid(receive_start_lsn))
 			nulls[2] = true;
 		else
-- 
2.50.1 (Apple Git-155)