Skip to content

Commit b898452

Browse files
committed
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
1 parent 8c2bfd9 commit b898452

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/backend/replication/walsender.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2813,7 +2813,14 @@ XLogSendLogical(void)
28132813
{
28142814
XLogRecord *record;
28152815
char *errm;
2816-
XLogRecPtr flushPtr;
2816+
2817+
/*
2818+
* We'll use the current flush point to determine whether we've caught up.
2819+
* This variable is static in order to cache it across calls. Caching is
2820+
* helpful because GetFlushRecPtr() needs to acquire a heavily-contended
2821+
* spinlock.
2822+
*/
2823+
static XLogRecPtr flushPtr = InvalidXLogRecPtr;
28172824

28182825
/*
28192826
* Don't know whether we've caught up yet. We'll set WalSndCaughtUp to
@@ -2830,11 +2837,6 @@ XLogSendLogical(void)
28302837
if (errm != NULL)
28312838
elog(ERROR, "%s", errm);
28322839

2833-
/*
2834-
* We'll use the current flush point to determine whether we've caught up.
2835-
*/
2836-
flushPtr = GetFlushRecPtr();
2837-
28382840
if (record != NULL)
28392841
{
28402842
/*
@@ -2847,7 +2849,16 @@ XLogSendLogical(void)
28472849
sentPtr = logical_decoding_ctx->reader->EndRecPtr;
28482850
}
28492851

2850-
/* Set flag if we're caught up. */
2852+
/*
2853+
* If first time through in this session, initialize flushPtr. Otherwise,
2854+
* we only need to update flushPtr if EndRecPtr is past it.
2855+
*/
2856+
if (flushPtr == InvalidXLogRecPtr)
2857+
flushPtr = GetFlushRecPtr();
2858+
else if (logical_decoding_ctx->reader->EndRecPtr >= flushPtr)
2859+
flushPtr = GetFlushRecPtr();
2860+
2861+
/* If EndRecPtr is still past our flushPtr, it means we caught up. */
28512862
if (logical_decoding_ctx->reader->EndRecPtr >= flushPtr)
28522863
WalSndCaughtUp = true;
28532864

0 commit comments

Comments
 (0)