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

Commit 38ddeab

Browse files
committed
Fix minor bug in logical-replication walsender shutdown
Logical walsender should exit when it catches up with sending WAL during shutdown; but there was a rare corner case when it failed to because of a race condition that puts it back to wait for more WAL instead -- but since there wasn't any, it'd not shut down immediately. It would only continue the shutdown when wal_sender_timeout terminates the sleep, which causes annoying waits during shutdown procedure. Restructure the code so that we no longer forget to set WalSndCaughtUp in that case. This was an oversight in commit c6c3334. Backpatch all the way down to 9.4. Author: Craig Ringer, Álvaro Herrera Discussion: https://postgr.es/m/CAMsr+YEuz4XwZX_QmnX_-2530XhyAmnK=zCmicEnq1vLr0aZ-g@mail.gmail.com
1 parent 1752e35 commit 38ddeab

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

src/backend/replication/walsender.c

+17-28
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,6 @@ WalSndWaitForWal(XLogRecPtr loc)
12951295
int wakeEvents;
12961296
static XLogRecPtr RecentFlushPtr = InvalidXLogRecPtr;
12971297

1298-
12991298
/*
13001299
* Fast path to avoid acquiring the spinlock in case we already know we
13011300
* have enough WAL available. This is particularly interesting if we're
@@ -2814,6 +2813,7 @@ XLogSendLogical(void)
28142813
{
28152814
XLogRecord *record;
28162815
char *errm;
2816+
XLogRecPtr flushPtr;
28172817

28182818
/*
28192819
* Don't know whether we've caught up yet. We'll set WalSndCaughtUp to
@@ -2830,11 +2830,13 @@ XLogSendLogical(void)
28302830
if (errm != NULL)
28312831
elog(ERROR, "%s", errm);
28322832

2833+
/*
2834+
* We'll use the current flush point to determine whether we've caught up.
2835+
*/
2836+
flushPtr = GetFlushRecPtr();
2837+
28332838
if (record != NULL)
28342839
{
2835-
/* XXX: Note that logical decoding cannot be used while in recovery */
2836-
XLogRecPtr flushPtr = GetFlushRecPtr();
2837-
28382840
/*
28392841
* Note the lack of any call to LagTrackerWrite() which is handled by
28402842
* WalSndUpdateProgress which is called by output plugin through
@@ -2843,32 +2845,19 @@ XLogSendLogical(void)
28432845
LogicalDecodingProcessRecord(logical_decoding_ctx, logical_decoding_ctx->reader);
28442846

28452847
sentPtr = logical_decoding_ctx->reader->EndRecPtr;
2846-
2847-
/*
2848-
* If we have sent a record that is at or beyond the flushed point, we
2849-
* have caught up.
2850-
*/
2851-
if (sentPtr >= flushPtr)
2852-
WalSndCaughtUp = true;
28532848
}
2854-
else
2855-
{
2856-
/*
2857-
* If the record we just wanted read is at or beyond the flushed
2858-
* point, then we're caught up.
2859-
*/
2860-
if (logical_decoding_ctx->reader->EndRecPtr >= GetFlushRecPtr())
2861-
{
2862-
WalSndCaughtUp = true;
28632849

2864-
/*
2865-
* Have WalSndLoop() terminate the connection in an orderly
2866-
* manner, after writing out all the pending data.
2867-
*/
2868-
if (got_STOPPING)
2869-
got_SIGUSR2 = true;
2870-
}
2871-
}
2850+
/* Set flag if we're caught up. */
2851+
if (logical_decoding_ctx->reader->EndRecPtr >= flushPtr)
2852+
WalSndCaughtUp = true;
2853+
2854+
/*
2855+
* If we're caught up and have been requested to stop, have WalSndLoop()
2856+
* terminate the connection in an orderly manner, after writing out all
2857+
* the pending data.
2858+
*/
2859+
if (WalSndCaughtUp && got_STOPPING)
2860+
got_SIGUSR2 = true;
28722861

28732862
/* Update shared memory status */
28742863
{

0 commit comments

Comments
 (0)