]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Improve pg_stat_wal_receiver for CONNECTING status
authorMichael Paquier <michael@paquier.xyz>
Fri, 22 May 2026 19:04:26 +0000 (04:04 +0900)
committerMichael Paquier <michael@paquier.xyz>
Fri, 22 May 2026 19:04:26 +0000 (04:04 +0900)
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 with walrcv_connect():
- 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.  This code path is
taken only once when a WAL receiver is spawned, so the extra acquisition
does not matter performance-wise.

Reported-by: Chao Li <li.evan.chao@gmail.com>
Author: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/EF91FF76-1E2B-4F3B-9162-290B4DC517FF@gmail.com

src/backend/replication/walreceiver.c

index 07eac07b9ce4c8d45823dd3b4b9c1e7e456c5280..d19317703c1f2f090bf21138fd16d0a53315b8d9 100644 (file)
@@ -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)