Hi hackers,

Currently walrcv->walRcvState is set to WALRCV_STREAMING at the
beginning of WalReceiverMain().

But it seems that after this assignment things could be wrong before the
walreicever actually starts streaming (like not being able to connect
to the primary).

It looks to me that WALRCV_STREAMING should be set once walrcv_startstreaming()
returns true: this is the proposal of this patch.

I don't think the current assignment location is causing any issues, but I
think it's more appropriate to move it like in the attached.

Looking forward to your feedback,

Regards,

--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com
From 000bb21e54085abcf9df8cc75e4a86c21e24700e Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot...@gmail.com>
Date: Mon, 11 Dec 2023 20:05:25 +0000
Subject: [PATCH v1] move walrcv->walRcvState assignment to WALRCV_STREAMING

walrcv->walRcvState = WALRCV_STREAMING was set to early. Move the assignement
to a more appropriate place.
---
 src/backend/replication/walreceiver.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
 100.0% src/backend/replication/

diff --git a/src/backend/replication/walreceiver.c 
b/src/backend/replication/walreceiver.c
index 26ded928a7..47f1d50144 100644
--- a/src/backend/replication/walreceiver.c
+++ b/src/backend/replication/walreceiver.c
@@ -242,7 +242,6 @@ WalReceiverMain(void)
        }
        /* Advertise our PID so that the startup process can kill us */
        walrcv->pid = MyProcPid;
-       walrcv->walRcvState = WALRCV_STREAMING;
 
        /* Fetch information required to start streaming */
        walrcv->ready_to_display = false;
@@ -413,9 +412,14 @@ WalReceiverMain(void)
                if (walrcv_startstreaming(wrconn, &options))
                {
                        if (first_stream)
+                       {
+                               SpinLockAcquire(&walrcv->mutex);
+                               walrcv->walRcvState = WALRCV_STREAMING;
+                               SpinLockRelease(&walrcv->mutex);
                                ereport(LOG,
                                                (errmsg("started streaming WAL 
from primary at %X/%X on timeline %u",
                                                                
LSN_FORMAT_ARGS(startpoint), startpointTLI)));
+                       }
                        else
                                ereport(LOG,
                                                (errmsg("restarted WAL 
streaming at %X/%X on timeline %u",
-- 
2.34.1

Reply via email to