Skip to content

Commit fca85f8

Browse files
committed
Fix walsender to exit promptly if client requests shutdown.
It's possible for WalSndWaitForWal to be asked to wait for WAL that doesn't exist yet. That's fine, in fact it's the normal situation if we're caught up; but when the client requests shutdown we should not keep waiting. The previous coding could wait indefinitely if the source server was idle. In passing, improve the rather weak comments in this area, and slightly rearrange some related code for better readability. Back-patch to 9.4 where this code was introduced. Discussion: https://postgr.es/m/14154.1498781234@sss.pgh.pa.us
1 parent 13a5771 commit fca85f8

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

src/backend/replication/walsender.c

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -764,15 +764,14 @@ logical_read_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int req
764764
/* make sure we have enough WAL available */
765765
flushptr = WalSndWaitForWal(targetPagePtr + reqLen);
766766

767-
/* more than one block available */
768-
if (targetPagePtr + XLOG_BLCKSZ <= flushptr)
769-
count = XLOG_BLCKSZ;
770-
/* not enough WAL synced, that can happen during shutdown */
771-
else if (targetPagePtr + reqLen > flushptr)
767+
/* fail if not (implies we are going to shut down) */
768+
if (flushptr < targetPagePtr + reqLen)
772769
return -1;
773-
/* part of the page available */
770+
771+
if (targetPagePtr + XLOG_BLCKSZ <= flushptr)
772+
count = XLOG_BLCKSZ; /* more than one block available */
774773
else
775-
count = flushptr - targetPagePtr;
774+
count = flushptr - targetPagePtr; /* part of the page available */
776775

777776
/* now actually read the data, we know it's there */
778777
XLogRead(cur_page, targetPagePtr, XLOG_BLCKSZ);
@@ -1266,7 +1265,11 @@ WalSndUpdateProgress(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId
12661265
}
12671266

12681267
/*
1269-
* Wait till WAL < loc is flushed to disk so it can be safely read.
1268+
* Wait till WAL < loc is flushed to disk so it can be safely sent to client.
1269+
*
1270+
* Returns end LSN of flushed WAL. Normally this will be >= loc, but
1271+
* if we detect a shutdown request (either from postmaster or client)
1272+
* we will return early, so caller must always check.
12701273
*/
12711274
static XLogRecPtr
12721275
WalSndWaitForWal(XLogRecPtr loc)
@@ -1333,9 +1336,7 @@ WalSndWaitForWal(XLogRecPtr loc)
13331336
RecentFlushPtr = GetXLogReplayRecPtr(NULL);
13341337

13351338
/*
1336-
* If postmaster asked us to stop, don't wait here anymore. This will
1337-
* cause the xlogreader to return without reading a full record, which
1338-
* is the fastest way to reach the mainloop which then can quit.
1339+
* If postmaster asked us to stop, don't wait anymore.
13391340
*
13401341
* It's important to do this check after the recomputation of
13411342
* RecentFlushPtr, so we can send all remaining data before shutting
@@ -1366,14 +1367,20 @@ WalSndWaitForWal(XLogRecPtr loc)
13661367
WalSndCaughtUp = true;
13671368

13681369
/*
1369-
* Try to flush pending output to the client. Also wait for the socket
1370-
* becoming writable, if there's still pending output after an attempt
1371-
* to flush. Otherwise we might just sit on output data while waiting
1372-
* for new WAL being generated.
1370+
* Try to flush any pending output to the client.
13731371
*/
13741372
if (pq_flush_if_writable() != 0)
13751373
WalSndShutdown();
13761374

1375+
/*
1376+
* If we have received CopyDone from the client, sent CopyDone
1377+
* ourselves, and the output buffer is empty, it's time to exit
1378+
* streaming, so fail the current WAL fetch request.
1379+
*/
1380+
if (streamingDoneReceiving && streamingDoneSending &&
1381+
!pq_is_send_pending())
1382+
break;
1383+
13771384
now = GetCurrentTimestamp();
13781385

13791386
/* die if timeout was reached */
@@ -1382,6 +1389,13 @@ WalSndWaitForWal(XLogRecPtr loc)
13821389
/* Send keepalive if the time has come */
13831390
WalSndKeepaliveIfNecessary(now);
13841391

1392+
/*
1393+
* Sleep until something happens or we time out. Also wait for the
1394+
* socket becoming writable, if there's still pending output.
1395+
* Otherwise we might sit on sendable output data while waiting for
1396+
* new WAL to be generated. (But if we have nothing to send, we don't
1397+
* want to wake on socket-writable.)
1398+
*/
13851399
sleeptime = WalSndComputeSleeptime(now);
13861400

13871401
wakeEvents = WL_LATCH_SET | WL_POSTMASTER_DEATH |
@@ -1390,7 +1404,6 @@ WalSndWaitForWal(XLogRecPtr loc)
13901404
if (pq_is_send_pending())
13911405
wakeEvents |= WL_SOCKET_WRITEABLE;
13921406

1393-
/* Sleep until something happens or we time out */
13941407
WaitLatchOrSocket(MyLatch, wakeEvents,
13951408
MyProcPort->sock, sleeptime,
13961409
WAIT_EVENT_WAL_SENDER_WAIT_WAL);
@@ -2115,7 +2128,8 @@ WalSndLoop(WalSndSendDataCallback send_data)
21152128
* ourselves, and the output buffer is empty, it's time to exit
21162129
* streaming.
21172130
*/
2118-
if (!pq_is_send_pending() && streamingDoneSending && streamingDoneReceiving)
2131+
if (streamingDoneReceiving && streamingDoneSending &&
2132+
!pq_is_send_pending())
21192133
break;
21202134

21212135
/*

0 commit comments

Comments
 (0)