summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlvaro Herrera <alvherre@alvh.no-ip.org>2019-10-17 15:06:05 +0200
committerAlvaro Herrera <alvherre@alvh.no-ip.org>2019-10-17 15:06:05 +0200
commitb2ab06e0251a6733ea07d3a010e4a868b0595fce (patch)
tree60bb3ab87a14bba8118d000f020ea50bb687a07e
parentcdbb3921308ec71e7d090b734a1c8c0bf3269088 (diff)
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 c6c333436. 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
-rw-r--r--src/backend/replication/walsender.c47
1 files changed, 18 insertions, 29 deletions
diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index 534f052c319..685cb40d089 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -1172,9 +1172,8 @@ WalSndWaitForWal(XLogRecPtr loc)
int wakeEvents;
static XLogRecPtr RecentFlushPtr = InvalidXLogRecPtr;
-
/*
- * Fast path to avoid acquiring the spinlock in the we already know we
+ * Fast path to avoid acquiring the spinlock in case we already know we
* have enough WAL available. This is particularly interesting if we're
* far behind.
*/
@@ -2489,6 +2488,7 @@ XLogSendLogical(void)
{
XLogRecord *record;
char *errm;
+ XLogRecPtr flushPtr;
/*
* Don't know whether we've caught up yet. We'll set WalSndCaughtUp to
@@ -2505,40 +2505,29 @@ XLogSendLogical(void)
if (errm != NULL)
elog(ERROR, "%s", errm);
+ /*
+ * We'll use the current flush point to determine whether we've caught up.
+ */
+ flushPtr = GetFlushRecPtr();
+
if (record != NULL)
{
- /* XXX: Note that logical decoding cannot be used while in recovery */
- XLogRecPtr flushPtr = GetFlushRecPtr();
-
LogicalDecodingProcessRecord(logical_decoding_ctx, logical_decoding_ctx->reader);
sentPtr = logical_decoding_ctx->reader->EndRecPtr;
-
- /*
- * If we have sent a record that is at or beyond the flushed point, we
- * have caught up.
- */
- if (sentPtr >= flushPtr)
- WalSndCaughtUp = true;
}
- else
- {
- /*
- * If the record we just wanted read is at or beyond the flushed
- * point, then we're caught up.
- */
- if (logical_decoding_ctx->reader->EndRecPtr >= GetFlushRecPtr())
- {
- WalSndCaughtUp = true;
- /*
- * Have WalSndLoop() terminate the connection in an orderly
- * manner, after writing out all the pending data.
- */
- if (got_STOPPING)
- got_SIGUSR2 = true;
- }
- }
+ /* Set flag if we're caught up. */
+ if (logical_decoding_ctx->reader->EndRecPtr >= flushPtr)
+ WalSndCaughtUp = true;
+
+ /*
+ * If we're caught up and have been requested to stop, have WalSndLoop()
+ * terminate the connection in an orderly manner, after writing out all
+ * the pending data.
+ */
+ if (WalSndCaughtUp && got_STOPPING)
+ got_SIGUSR2 = true;
/* Update shared memory status */
{