From 7478c898954099c79c0a34743d2292f740120009 Mon Sep 17 00:00:00 2001 From: Petr Jelinek Date: Sat, 4 Mar 2017 07:43:17 +0100 Subject: [PATCH] Reorder the asynchronous libpq calls for replication connection --- .../replication/libpqwalreceiver/libpqwalreceiver.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c index 048d2aa..b24eb3f 100644 --- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c +++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c @@ -113,7 +113,7 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname, char **err) { WalReceiverConn *conn; - PostgresPollingStatusType status; + PostgresPollingStatusType status = PGRES_POLLING_WRITING; const char *keys[5]; const char *vals[5]; int i = 0; @@ -155,12 +155,14 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname, return NULL; } - /* Poll connection. */ - do + /* + * Poll connection until we have OK or FAILED status. + * + * Note the initial state after PQconnectStartParams is + * PGRES_POLLING_WRITING. + */ + while (status != PGRES_POLLING_OK && status != PGRES_POLLING_FAILED) { - /* Determine current state of the connection. */ - status = PQconnectPoll(conn->streamConn); - /* Sleep a bit if waiting for socket. */ if (status == PGRES_POLLING_READING || status == PGRES_POLLING_WRITING) @@ -189,8 +191,9 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname, CHECK_FOR_INTERRUPTS(); } - /* Otherwise loop until we have OK or FAILED status. */ - } while (status != PGRES_POLLING_OK && status != PGRES_POLLING_FAILED); + /* Determine new state of the connection. */ + status = PQconnectPoll(conn->streamConn); + } if (PQstatus(conn->streamConn) != CONNECTION_OK) { -- 2.7.4