Commit e369f370 authored by Tom Lane's avatar Tom Lane

Reduce the number of GetFlushRecPtr() calls done by walsenders.

Since the WAL flush position only moves forward, it's safe to cache
its previous value within each walsender process, and update from
shared memory only once we've caught up to the previously-seen value.
When there are many active walsenders, this makes for a very significant
reduction in the amount of contention on the XLogCtl->info_lck spinlock.

This patch also adjusts the logic so that we update our idea of the
flush position after processing a WAL record, rather than beforehand.
This may cause us to realize we're not caught up when the preceding
coding would've thought that we were, but that seems all to the good;
it may avoid a useless sleep-and-wakeup cycle.

Back-patch to v12.  The contention problem exists in prior branches,
but it's much less severe (due to inefficiencies elsewhere) so there
seems no need to take any risk of back-patching further.

Pierre Ducroquet, reviewed by Julien Rouhaud

Discussion: https://postgr.es/m/2931018.Vxl9zapr77@pierred-pdoc
parent 20d6225d
...@@ -2774,7 +2774,14 @@ XLogSendLogical(void) ...@@ -2774,7 +2774,14 @@ XLogSendLogical(void)
{ {
XLogRecord *record; XLogRecord *record;
char *errm; char *errm;
XLogRecPtr flushPtr;
/*
* We'll use the current flush point to determine whether we've caught up.
* This variable is static in order to cache it across calls. Caching is
* helpful because GetFlushRecPtr() needs to acquire a heavily-contended
* spinlock.
*/
static XLogRecPtr flushPtr = InvalidXLogRecPtr;
/* /*
* Don't know whether we've caught up yet. We'll set WalSndCaughtUp to * Don't know whether we've caught up yet. We'll set WalSndCaughtUp to
...@@ -2791,11 +2798,6 @@ XLogSendLogical(void) ...@@ -2791,11 +2798,6 @@ XLogSendLogical(void)
if (errm != NULL) if (errm != NULL)
elog(ERROR, "%s", errm); elog(ERROR, "%s", errm);
/*
* We'll use the current flush point to determine whether we've caught up.
*/
flushPtr = GetFlushRecPtr();
if (record != NULL) if (record != NULL)
{ {
/* /*
...@@ -2808,7 +2810,16 @@ XLogSendLogical(void) ...@@ -2808,7 +2810,16 @@ XLogSendLogical(void)
sentPtr = logical_decoding_ctx->reader->EndRecPtr; sentPtr = logical_decoding_ctx->reader->EndRecPtr;
} }
/* Set flag if we're caught up. */ /*
* If first time through in this session, initialize flushPtr. Otherwise,
* we only need to update flushPtr if EndRecPtr is past it.
*/
if (flushPtr == InvalidXLogRecPtr)
flushPtr = GetFlushRecPtr();
else if (logical_decoding_ctx->reader->EndRecPtr >= flushPtr)
flushPtr = GetFlushRecPtr();
/* If EndRecPtr is still past our flushPtr, it means we caught up. */
if (logical_decoding_ctx->reader->EndRecPtr >= flushPtr) if (logical_decoding_ctx->reader->EndRecPtr >= flushPtr)
WalSndCaughtUp = true; WalSndCaughtUp = true;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment