From 65b907b7ba23a1ac2237c37b7778d14e7e26d6f5 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Tue, 4 May 2021 21:51:50 +0300 Subject: [PATCH] Use correct LSN in GetPage@LSN calls when nothing has been evicted yet. If no page has been evicted from page cache yet, use the latest flushed LSN. Commit 153dee9b442, "Request special lsn during bootstrap", changed this but it is not clear to me why. As far as I can see, this isn't related to bootstrapping, and using the latest flushed LSN should always work. While we're at it, rearrange the code slightly for readability, and add comments. --- src/backend/storage/smgr/pagestore_smgr.c | 29 ++++++++++++++++------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/backend/storage/smgr/pagestore_smgr.c b/src/backend/storage/smgr/pagestore_smgr.c index d62ab6c7425..67fbc0de5ac 100644 --- a/src/backend/storage/smgr/pagestore_smgr.c +++ b/src/backend/storage/smgr/pagestore_smgr.c @@ -337,21 +337,32 @@ zenith_get_request_lsn(bool nonrel) } else { - lsn = GetLastWrittenPageLSN(); + XLogRecPtr flushlsn = GetFlushRecPtr(); + /* + * Use the latest LSN that was evicted from the buffer cache. Any + * pages modified by later WAL records must still in the buffer cache, + * so our request cannot concern those. + */ + lsn = GetLastWrittenPageLSN(); elog(DEBUG1, "zenith_get_request_lsn GetLastWrittenPageLSN lsn %X/%X ", (uint32) ((lsn) >> 32), (uint32) (lsn)); - - if (lsn > GetFlushRecPtr()) - XLogFlush(lsn); if (lsn == InvalidXLogRecPtr) { - /* we haven't evicted anything yet since the server was started */ - lsn = GetFlushRecPtr(); - elog(DEBUG1, "zenith_get_request_lsn GetFlushRecPtr lsn %X/%X request 0", - (uint32) ((lsn) >> 32), (uint32) (lsn)); - lsn = InvalidXLogRecPtr; + /* + * We haven't evicted anything yet since the server was + * started. Then just use the latest flushed LSN. That's always + * safe, using the latest evicted LSN is really just an + * optimization. + */ + lsn = flushlsn; + elog(DEBUG1, "zenith_get_request_lsn GetFlushRecPtr lsn %X/%X", + (uint32) ((lsn) >> 32), (uint32) (lsn)); } + + /* The record should've been flushed already, since it was evicted, but let's be safe */ + if (lsn > flushlsn) + XLogFlush(lsn); } return lsn; }