Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 21ef4d4

Browse files
committed
Revert "libpqwalreceiver: Convert to libpq-be-fe-helpers.h"
This reverts commit 728f86f. The signal handling was a few bricks shy of a load in that commit, which made the walreceiver non-responsive to SIGTERM while it was waiting for the connection to be established. That prevented a standby from being promoted. Since it was non-essential refactoring, let's revert it to make v16 work the same as earlier releases. I reverted it in 'master' too, to keep the branches in sync. The refactoring was a good idea as such, but it needs a bit more work. Once we have developed a complete patch with this issue fixed, let's re-apply that to 'master'. Reported-by: Kyotaro Horiguchi Backpatch-through: 16 Discussion: https://www.postgresql.org/message-id/20231231.200741.1078989336605759878.horikyota.ntt@gmail.com
1 parent 9b1a6f5 commit 21ef4d4

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed

src/backend/replication/libpqwalreceiver/libpqwalreceiver.c

+47-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "common/connect.h"
2525
#include "funcapi.h"
2626
#include "libpq-fe.h"
27-
#include "libpq/libpq-be-fe-helpers.h"
2827
#include "mb/pg_wchar.h"
2928
#include "miscadmin.h"
3029
#include "pgstat.h"
@@ -133,6 +132,7 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
133132
const char *appname, char **err)
134133
{
135134
WalReceiverConn *conn;
135+
PostgresPollingStatusType status;
136136
const char *keys[6];
137137
const char *vals[6];
138138
int i = 0;
@@ -188,17 +188,56 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
188188
Assert(i < sizeof(keys));
189189

190190
conn = palloc0(sizeof(WalReceiverConn));
191-
conn->streamConn =
192-
libpqsrv_connect_params(keys, vals,
193-
/* expand_dbname = */ true,
194-
WAIT_EVENT_LIBPQWALRECEIVER_CONNECT);
191+
conn->streamConn = PQconnectStartParams(keys, vals,
192+
/* expand_dbname = */ true);
193+
if (PQstatus(conn->streamConn) == CONNECTION_BAD)
194+
goto bad_connection_errmsg;
195+
196+
/*
197+
* Poll connection until we have OK or FAILED status.
198+
*
199+
* Per spec for PQconnectPoll, first wait till socket is write-ready.
200+
*/
201+
status = PGRES_POLLING_WRITING;
202+
do
203+
{
204+
int io_flag;
205+
int rc;
206+
207+
if (status == PGRES_POLLING_READING)
208+
io_flag = WL_SOCKET_READABLE;
209+
#ifdef WIN32
210+
/* Windows needs a different test while waiting for connection-made */
211+
else if (PQstatus(conn->streamConn) == CONNECTION_STARTED)
212+
io_flag = WL_SOCKET_CONNECTED;
213+
#endif
214+
else
215+
io_flag = WL_SOCKET_WRITEABLE;
216+
217+
rc = WaitLatchOrSocket(MyLatch,
218+
WL_EXIT_ON_PM_DEATH | WL_LATCH_SET | io_flag,
219+
PQsocket(conn->streamConn),
220+
0,
221+
WAIT_EVENT_LIBPQWALRECEIVER_CONNECT);
222+
223+
/* Interrupted? */
224+
if (rc & WL_LATCH_SET)
225+
{
226+
ResetLatch(MyLatch);
227+
ProcessWalRcvInterrupts();
228+
}
229+
230+
/* If socket is ready, advance the libpq state machine */
231+
if (rc & io_flag)
232+
status = PQconnectPoll(conn->streamConn);
233+
} while (status != PGRES_POLLING_OK && status != PGRES_POLLING_FAILED);
195234

196235
if (PQstatus(conn->streamConn) != CONNECTION_OK)
197236
goto bad_connection_errmsg;
198237

199238
if (must_use_password && !PQconnectionUsedPassword(conn->streamConn))
200239
{
201-
libpqsrv_disconnect(conn->streamConn);
240+
PQfinish(conn->streamConn);
202241
pfree(conn);
203242

204243
ereport(ERROR,
@@ -234,7 +273,7 @@ libpqrcv_connect(const char *conninfo, bool logical, bool must_use_password,
234273

235274
/* error path, error already set */
236275
bad_connection:
237-
libpqsrv_disconnect(conn->streamConn);
276+
PQfinish(conn->streamConn);
238277
pfree(conn);
239278
return NULL;
240279
}
@@ -770,7 +809,7 @@ libpqrcv_PQgetResult(PGconn *streamConn)
770809
static void
771810
libpqrcv_disconnect(WalReceiverConn *conn)
772811
{
773-
libpqsrv_disconnect(conn->streamConn);
812+
PQfinish(conn->streamConn);
774813
PQfreemem(conn->recvBuf);
775814
pfree(conn);
776815
}

0 commit comments

Comments
 (0)