From 511be8d301d22f3c6baf4765f2d39ef4feb6f43b Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Mon, 2 Feb 2026 11:58:07 -0500
Subject: [PATCH v17 06/21] Remove SyncOneBuffer() and refactor BgBufferSync()

Since checkpointer write combining made checkpointer stop using
SyncOneBuffer(), bgwriter was the only caller. Now it can be simplified
and inlined.

Reviewed-by: Andrey Borodin <x4mmm@yandex-team.ru>
---
 src/backend/access/transam/README   |   6 +-
 src/backend/commands/sequence.c     |  16 ++--
 src/backend/storage/buffer/bufmgr.c | 142 +++++++++-------------------
 src/include/storage/proc.h          |   2 +-
 4 files changed, 59 insertions(+), 107 deletions(-)

diff --git a/src/backend/access/transam/README b/src/backend/access/transam/README
index 595afa01fa7..a0b6f8b3f5c 100644
--- a/src/backend/access/transam/README
+++ b/src/backend/access/transam/README
@@ -447,8 +447,10 @@ critical section.)
 
 3. Apply the required changes to the shared buffer(s).
 
-4. Mark the shared buffer(s) as dirty with MarkBufferDirty().  (This must
-happen before the WAL record is inserted; see notes in SyncOneBuffer().)
+4. Mark the shared buffer(s) as dirty with MarkBufferDirty() before inserting
+the WAL record. Otherwise, a checkpoint could establish its redo point after
+the record and still see the buffer as clean. The checkpoint would skip the
+buffer, and recovery would start too late to replay the change.
 Note that marking a buffer dirty with MarkBufferDirty() should only
 happen iff you write a WAL record; see Writing Hints below.
 
diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c
index b9843f8cdc1..e36ceea0637 100644
--- a/src/backend/commands/sequence.c
+++ b/src/backend/commands/sequence.c
@@ -821,13 +821,15 @@ nextval_internal(Oid relid, bool check_permissions)
 	START_CRIT_SECTION();
 
 	/*
-	 * We must mark the buffer dirty before doing XLogInsert(); see notes in
-	 * SyncOneBuffer().  However, we don't apply the desired changes just yet.
-	 * This looks like a violation of the buffer update protocol, but it is in
-	 * fact safe because we hold exclusive lock on the buffer.  Any other
-	 * process, including a checkpoint, that tries to examine the buffer
-	 * contents will block until we release the lock, and then will see the
-	 * final state that we install below.
+	 * Mark the buffer dirty before XLogInsert(). Otherwise, a checkpoint
+	 * could establish its redo point after the WAL record and still see the
+	 * buffer as clean. The checkpoint would skip the buffer, and recovery
+	 * would start too late to replay the change.
+	 *
+	 * We can defer the actual changes because we hold the exclusive content
+	 * lock. A checkpoint that sees the dirty bit must acquire the content
+	 * lock before writing the page, so it will see the final state we install
+	 * below.
 	 */
 	MarkBufferDirty(buf);
 
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 628b39e9260..06779833504 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -81,10 +81,6 @@
 #define LocalBufHdrGetBlock(bufHdr) \
 	LocalBufferBlockPointers[-((bufHdr)->buf_id + 2)]
 
-/* Bits in SyncOneBuffer's return value */
-#define BUF_WRITTEN				0x01
-#define BUF_REUSABLE			0x02
-
 #define RELS_BSEARCH_THRESHOLD		20
 
 /*
@@ -671,8 +667,6 @@ static bool PinBuffer(BufferDesc *buf, BufferUsageCountChange usage_count_change
 static void PinBuffer_Locked(BufferDesc *buf);
 static void UnpinBuffer(BufferDesc *buf);
 static void UnpinBufferNoOwner(BufferDesc *buf);
-static int	SyncOneBuffer(int buf_id, bool skip_recently_used,
-						  WritebackContext *wb_context);
 static void WaitIO(BufferDesc *buf);
 static void AbortBufferIO(Buffer buffer);
 static void shared_buffer_write_error_callback(void *arg);
@@ -3696,8 +3690,12 @@ CheckPointBuffers(int flags)
 		uint64		buf_state;
 
 		/*
-		 * Header spinlock is enough to examine BM_DIRTY, see comment in
-		 * SyncOneBuffer.
+		 * We can make this check without taking the buffer content lock so
+		 * long as we mark pages dirty in access methods *before* logging
+		 * changes with XLogInsert(): if someone marks the buffer dirty just
+		 * after our check we don't worry because our checkpoint.redo points
+		 * before log record for upcoming changes and so we are not required
+		 * to write such dirty buffer.
 		 */
 		buf_state = LockBufHdr(bufHdr);
 
@@ -4318,27 +4316,54 @@ BgBufferSync(WritebackContext *wb_context)
 	/* Execute the LRU scan */
 	while (num_to_scan > 0 && reusable_buffers < upcoming_alloc_est)
 	{
-		int			sync_state = SyncOneBuffer(next_to_clean, true,
-											   wb_context);
+		uint64		buf_state;
+		BufferDesc *bufHdr;
+		BufferTag	tag;
 
+		/* Reserve space for the pin before taking the header lock. */
+		ReservePrivateRefCountEntry();
+		ResourceOwnerEnlarge(CurrentResourceOwner);
+
+		bufHdr = GetBufferDescriptor(next_to_clean);
+		num_to_scan--;
 		if (++next_to_clean >= NBuffers)
 		{
 			next_to_clean = 0;
 			next_passes++;
 		}
-		num_to_scan--;
 
-		if (sync_state & BUF_WRITTEN)
+		buf_state = LockBufHdr(bufHdr);
+		if (BUF_STATE_GET_REFCOUNT(buf_state) != 0 ||
+			BUF_STATE_GET_USAGECOUNT(buf_state) != 0)
 		{
-			reusable_buffers++;
-			if (++num_written >= bgwriter_lru_maxpages)
-			{
-				PendingBgWriterStats.maxwritten_clean++;
-				break;
-			}
+			UnlockBufHdr(bufHdr);
+			continue;
+		}
+
+		reusable_buffers++;
+
+		if (!(buf_state & BM_VALID) || !(buf_state & BM_DIRTY))
+		{
+			UnlockBufHdr(bufHdr);
+			continue;
+		}
+
+		/* Pin the selected buffer and release the header lock. */
+		PinBuffer_Locked(bufHdr);
+
+		FlushUnlockedBuffer(bufHdr, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
+
+		/* Snapshot the tag before unpinning */
+		tag = bufHdr->tag;
+		UnpinBuffer(bufHdr);
+
+		ScheduleBufferTagForWriteback(wb_context, IOCONTEXT_NORMAL, &tag);
+
+		if (++num_written >= bgwriter_lru_maxpages)
+		{
+			PendingBgWriterStats.maxwritten_clean++;
+			break;
 		}
-		else if (sync_state & BUF_REUSABLE)
-			reusable_buffers++;
 	}
 
 	PendingBgWriterStats.buf_written_clean += num_written;
@@ -4379,83 +4404,6 @@ BgBufferSync(WritebackContext *wb_context)
 	return (bufs_to_lap == 0 && recent_alloc == 0);
 }
 
-/*
- * SyncOneBuffer -- process a single buffer during syncing.
- *
- * If skip_recently_used is true, we don't write currently-pinned buffers, nor
- * buffers marked recently used, as these are not replacement candidates.
- *
- * Returns a bitmask containing the following flag bits:
- *	BUF_WRITTEN: we wrote the buffer.
- *	BUF_REUSABLE: buffer is available for replacement, ie, it has
- *		pin count 0 and usage count 0.
- *
- * (BUF_WRITTEN could be set in error if FlushBuffer finds the buffer clean
- * after locking it, but we don't care all that much.)
- */
-static int
-SyncOneBuffer(int buf_id, bool skip_recently_used, WritebackContext *wb_context)
-{
-	BufferDesc *bufHdr = GetBufferDescriptor(buf_id);
-	int			result = 0;
-	uint64		buf_state;
-	BufferTag	tag;
-
-	/* Make sure we can handle the pin */
-	ReservePrivateRefCountEntry();
-	ResourceOwnerEnlarge(CurrentResourceOwner);
-
-	/*
-	 * Check whether buffer needs writing.
-	 *
-	 * We can make this check without taking the buffer content lock so long
-	 * as we mark pages dirty in access methods *before* logging changes with
-	 * XLogInsert(): if someone marks the buffer dirty just after our check we
-	 * don't worry because our checkpoint.redo points before log record for
-	 * upcoming changes and so we are not required to write such dirty buffer.
-	 */
-	buf_state = LockBufHdr(bufHdr);
-
-	if (BUF_STATE_GET_REFCOUNT(buf_state) == 0 &&
-		BUF_STATE_GET_USAGECOUNT(buf_state) == 0)
-	{
-		result |= BUF_REUSABLE;
-	}
-	else if (skip_recently_used)
-	{
-		/* Caller told us not to write recently-used buffers */
-		UnlockBufHdr(bufHdr);
-		return result;
-	}
-
-	if (!(buf_state & BM_VALID) || !(buf_state & BM_DIRTY))
-	{
-		/* It's clean, so nothing to do */
-		UnlockBufHdr(bufHdr);
-		return result;
-	}
-
-	/*
-	 * Pin it, share-exclusive-lock it, write it.  (FlushBuffer will do
-	 * nothing if the buffer is clean by the time we've locked it.)
-	 */
-	PinBuffer_Locked(bufHdr);
-
-	FlushUnlockedBuffer(bufHdr, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
-
-	tag = bufHdr->tag;
-
-	UnpinBuffer(bufHdr);
-
-	/*
-	 * SyncOneBuffer() is only called by checkpointer and bgwriter, so
-	 * IOContext will always be IOCONTEXT_NORMAL.
-	 */
-	ScheduleBufferTagForWriteback(wb_context, IOCONTEXT_NORMAL, &tag);
-
-	return result | BUF_WRITTEN;
-}
-
 /*
  *		AtEOXact_Buffers - clean up at end of transaction.
  *
diff --git a/src/include/storage/proc.h b/src/include/storage/proc.h
index abe40001d9a..f7ed21c9db9 100644
--- a/src/include/storage/proc.h
+++ b/src/include/storage/proc.h
@@ -126,7 +126,7 @@ extern PGDLLIMPORT int FastPathLockGroupsPerBackend;
  * and we acquire an exclusive content lock and MarkBufferDirty() on the
  * relevant buffers before writing WAL, this mechanism is not needed, because
  * phase 2 will block until we release the content lock and then flush the
- * modified data to disk.  See transam/README and SyncOneBuffer().)
+ * modified data to disk.  See transam/README.)
  *
  * Setting DELAY_CHKPT_COMPLETE prevents the system from moving from phase 2
  * to phase 3. This is useful if we are performing a WAL-logged operation that
-- 
2.43.0

