diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index 74330f8c84..fbb30c6847 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -2765,7 +2765,13 @@ XLogSendLogical(void) { XLogRecord *record; 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 accross calls. This caching + * is needed because calling GetFlushRecPtr needs to acquire an expensive lock. + */ + static XLogRecPtr flushPtr = InvalidXLogRecPtr; /* * Don't know whether we've caught up yet. We'll set WalSndCaughtUp to @@ -2782,11 +2788,6 @@ 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) { /* @@ -2799,7 +2800,16 @@ XLogSendLogical(void) sentPtr = logical_decoding_ctx->reader->EndRecPtr; } - /* Set flag if we're caught up. */ + + /* Initialize flushPtr if needed */ + if (flushPtr == InvalidXLogRecPtr) + flushPtr = GetFlushRecPtr(); + + /* If EndRecPtr is past our flushPtr, we must update it to know if we caught up */ + 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) WalSndCaughtUp = true;