From 3c381a90b1270fdd3f1b01e8eefb85f1ac4af3d8 Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Tue, 19 May 2026 22:52:38 +0900 Subject: [PATCH v2] Improve pg_stat_wal_receiver for CONNECTING status Commit a36164e7465 added a CONNECTING status for the WAL receiver, but pg_stat_wal_receiver returned no information while the connection to the primary was attempted, limiting the usability of the feature in high-latency environments where the connection attempt to the primary could take time. This commit improves the report of the status by splitting the way the shared memory state of the WAL receiver is filled before and after the connection to the primary is attempted: - Before the attempt, reset all the connection fields, switch ready_to_display to true. - After the attempt, fill in the connection fields. This change means two spinlock acquisitions instead of one, but at least monitoring tools can know about the connection attempt before its completion, enlarging the usability of the feature. Reported-by: Chao Li Author: Michael Paquier Discussion: https://postgr.es/m/XXX --- src/backend/replication/walreceiver.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c index 07eac07b9ce4..d19317703c1f 100644 --- a/src/backend/replication/walreceiver.c +++ b/src/backend/replication/walreceiver.c @@ -267,6 +267,20 @@ WalReceiverMain(const void *startup_data, size_t startup_data_len) /* Unblock signals (they were blocked when the postmaster forked us) */ sigprocmask(SIG_SETMASK, &UnBlockSig, NULL); + /* + * Switch the WAL receiver state as ready for display before doing a + * connection attempt, so as its connecting state is visible before + * attempting to contact the primary server. Note that this resets the + * original conninfo, sender_port and sender_host, for security. These + * fields are filled once the connection is fully established. + */ + SpinLockAcquire(&walrcv->mutex); + memset(walrcv->conninfo, 0, MAXCONNINFO); + memset(walrcv->sender_host, 0, NI_MAXHOST); + walrcv->sender_port = 0; + walrcv->ready_to_display = true; + SpinLockRelease(&walrcv->mutex); + /* Establish the connection to the primary for XLOG streaming */ appname = cluster_name[0] ? cluster_name : "walreceiver"; wrconn = walrcv_connect(conninfo, true, false, false, appname, &err); @@ -277,23 +291,17 @@ WalReceiverMain(const void *startup_data, size_t startup_data_len) appname, err))); /* - * Save user-visible connection string. This clobbers the original - * conninfo, for security. Also save host and port of the sender server - * this walreceiver is connected to. + * Save user-visible connection string, now that the connection has been + * achieved. */ tmp_conninfo = walrcv_get_conninfo(wrconn); walrcv_get_senderinfo(wrconn, &sender_host, &sender_port); SpinLockAcquire(&walrcv->mutex); - memset(walrcv->conninfo, 0, MAXCONNINFO); if (tmp_conninfo) strlcpy(walrcv->conninfo, tmp_conninfo, MAXCONNINFO); - - memset(walrcv->sender_host, 0, NI_MAXHOST); if (sender_host) strlcpy(walrcv->sender_host, sender_host, NI_MAXHOST); - walrcv->sender_port = sender_port; - walrcv->ready_to_display = true; SpinLockRelease(&walrcv->mutex); if (tmp_conninfo) -- 2.54.0