From 3661f9a76a2d2bdec52982c0cb876306a94668b5 Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Mon, 12 Jan 2026 11:49:41 -0500
Subject: [PATCH v14 10/12] Normal backend write combining

When flushing a dirty buffer, check if the two blocks following it are
in shared buffers and whether or not they are dirty. If they are, flush
them together with the victim buffer.

Author: Melanie Plageman <melanieplageman@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/flat/0198DBB9-4A76-49E4-87F8-43D46DD0FD76%40gmail.com#1d8677fc75dc8b39f0eb5dd6bbafb65d
---
 src/backend/storage/buffer/bufmgr.c | 129 +++++++++++++++++++++++-----
 1 file changed, 109 insertions(+), 20 deletions(-)

diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 5466aa431ed..c406a3d5987 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -651,7 +651,11 @@ static void FlushBuffer(BufferDesc *buf, SMgrRelation reln,
 static void FlushBufferBatch(BufferWriteBatch *batch, IOContext io_context);
 static BufferDesc *PrepareOrRejectEagerFlushBuffer(Buffer bufnum,
 												   BufferTag *require,
+												   LWLock *buftable_lock,
 												   XLogRecPtr *lsn);
+static void FindFlushAdjacents(BufferDesc *batch_start,
+							   uint32 batch_limit,
+							   BufferWriteBatch *batch);
 static void FindStrategyFlushAdjacents(BufferAccessStrategy strategy, Buffer sweep_end,
 									   BufferDesc *batch_start,
 									   uint32 max_batch_size,
@@ -2580,17 +2584,18 @@ again:
 			goto again;
 		}
 
-		if (from_ring && StrategySupportsEagerFlush(strategy))
+		if (!StartBufferIO(buf_hdr, false, false))
+		{
+			/* May be nothing to do if buffer was cleaned */
+			LockBuffer(buf, BUFFER_LOCK_UNLOCK);
+		}
+		else if (from_ring && StrategySupportsEagerFlush(strategy))
 		{
 			BufferDesc *next_bufdesc = buf_hdr;
 			Buffer		sweep_end = buf;
 			int			cursor = StrategyGetCurrentIndex(strategy);
 			uint32		max_batch_size = MaxWriteBatchSize(strategy);
 
-			/* Start IO on the first buffer */
-			if (!StartBufferIO(buf_hdr, false, false))
-				goto clean;
-
 			/* Pin victim again so it stays ours even after batch released */
 			ReservePrivateRefCountEntry();
 			ResourceOwnerEnlarge(CurrentResourceOwner);
@@ -2654,22 +2659,35 @@ again:
 				 * until we complete one full sweep around the ring.
 				 */
 				next_bufdesc = PrepareOrRejectEagerFlushBuffer(next_buf,
+															   NULL,
 															   NULL,
 															   &next_buf_lsn);
 			}
 		}
 		else
 		{
-			/* OK, do the I/O */
-			FlushBuffer(buf_hdr, NULL, IOOBJECT_RELATION, io_context);
-			LockBuffer(buf, BUFFER_LOCK_UNLOCK);
+			BufferWriteBatch batch;
+			BlockNumber limit;
+			uint32		max_batch_size = 3; /* we only look for two successors */
+
+			/* Pin victim again so it stays ours even after batch released */
+			ReservePrivateRefCountEntry();
+			ResourceOwnerEnlarge(CurrentResourceOwner);
+			IncrBufferRefCount(BufferDescriptorGetBuffer(buf_hdr));
 
-			ScheduleBufferTagForWriteback(&BackendWritebackContext, io_context,
-										  &buf_hdr->tag);
+			/*
+			 * If we are allowed more pins and there are more blocks in the
+			 * relation and the victim buffer's block's successors are
+			 * eligible for eager flushing, combine them into a batch.
+			 */
+			limit = WriteBatchInit(buf_hdr, max_batch_size, &batch);
+			if (limit > 1)
+				FindFlushAdjacents(buf_hdr, limit, &batch);
+			FlushBufferBatch(&batch, io_context);
+			CompleteWriteBatchIO(&batch, io_context, &BackendWritebackContext);
 		}
 	}
 
-clean:
 	if (buf_state & BM_VALID)
 	{
 		/*
@@ -3576,7 +3594,7 @@ TrackNewBufferPin(Buffer buf)
 bool
 BufferNeedsWALFlush(BufferDesc *bufdesc, bool exclusive_locked)
 {
-	uint32		buf_state = pg_atomic_read_u64(&bufdesc->state);
+	uint64		buf_state = pg_atomic_read_u64(&bufdesc->state);
 	Buffer		buffer;
 	char	   *page;
 	XLogRecPtr	lsn;
@@ -4883,11 +4901,16 @@ WriteBatchInit(BufferDesc *batch_start, uint32 max_batch_size,
  * accept it, they will provide the required block number and its
  * RelFileLocator and fork.
  *
- * If returning a buffer, also return its LSN.
+ * If the caller is holding the buftable_lock, it will be released after
+ * acquiring a pin on the buffer.
+ *
+ * max_lsn may be updated if the provided buffer LSN exceeds the current max
+ * LSN.
  */
 static BufferDesc *
 PrepareOrRejectEagerFlushBuffer(Buffer bufnum,
 								BufferTag *require,
+								LWLock *buftable_lock,
 								XLogRecPtr *lsn)
 {
 	BufferDesc *bufdesc;
@@ -4948,6 +4971,12 @@ PrepareOrRejectEagerFlushBuffer(Buffer bufnum,
 	if (!PinBuffer(bufdesc, BUC_ZERO, true /* skip_if_not_valid */ ))
 		goto reject_buffer;
 
+	if (buftable_lock)
+	{
+		LWLockRelease(buftable_lock);
+		buftable_lock = NULL;
+	}
+
 	CheckBufferIsPinnedOnce(bufnum);
 
 	/* Now that we have the buffer pinned, recheck it's got the right block */
@@ -4979,6 +5008,8 @@ reject_buffer_unpin:
 	UnpinBuffer(bufdesc);
 
 reject_buffer:
+	if (buftable_lock)
+		LWLockRelease(buftable_lock);
 	return NULL;
 }
 
@@ -4987,15 +5018,17 @@ reject_buffer:
  * flushing, find additional buffers from the ring that can be combined into a
  * single write batch with the starting buffer.
  *
- * This function will pin and content lock all of the buffers that it
- * assembles for the IO batch. The caller is responsible for issuing the IO.
- *
- * batch_limit is the largest batch we are allowed to construct given the
- * remaining blocks in the table, the number of available pins, and the
- * current configuration.
+ * max_batch_size is the maximum number of blocks that can be combined into a
+ * single write in general. This function, based on the block number of start,
+ * will determine the maximum IO size for this particular write given how much
+ * of the file remains. max_batch_size is provided by the caller so it doesn't
+ * have to be recalculated for each write.
  *
  * batch is an output parameter that this function will fill with the needed
  * information to issue this IO.
+ *
+ * This function will pin and content lock all of the buffers that it
+ * assembles for the IO batch. The caller is responsible for issuing the IO.
  */
 static void
 FindStrategyFlushAdjacents(BufferAccessStrategy strategy,
@@ -5034,6 +5067,7 @@ FindStrategyFlushAdjacents(BufferAccessStrategy strategy,
 		batch->bufdescs[batch->n] =
 			PrepareOrRejectEagerFlushBuffer(bufnum,
 											&require,
+											NULL,
 											&lsn);
 
 		/*
@@ -5052,10 +5086,65 @@ FindStrategyFlushAdjacents(BufferAccessStrategy strategy,
 	}
 }
 
+
 /*
- * Given a prepared batch of buffers write them out as a vector.
+ * Check if the blocks after my block are in shared buffers and dirty and if
+ * they are, write them out too.
  */
 static void
+FindFlushAdjacents(BufferDesc *batch_start,
+				   uint32 batch_limit,
+				   BufferWriteBatch *batch)
+{
+	BufferTag	require;		/* requested block's buffer tag */
+	uint32		newHash;		/* hash value for require */
+	LWLock	   *newPartitionLock;	/* buffer partition lock for it */
+	int			buf_id;
+
+	/* create a tag so we can lookup the buffers */
+	InitBufferTag(&require, &batch->reln->smgr_rlocator.locator,
+				  batch->forkno, InvalidBlockNumber);
+
+	for (; batch->n < batch_limit; batch->n++)
+	{
+		XLogRecPtr	lsn;
+
+		require.blockNum = batch->start + batch->n;
+
+		Assert(BlockNumberIsValid(require.blockNum));
+
+		/* determine its hash code and partition lock ID */
+		newHash = BufTableHashCode(&require);
+		newPartitionLock = BufMappingPartitionLock(newHash);
+
+		/* see if the block is in the buffer pool already */
+		LWLockAcquire(newPartitionLock, LW_SHARED);
+		buf_id = BufTableLookup(&require, newHash);
+
+		/* The block may not even be in shared buffers. */
+		if (buf_id < 0)
+		{
+			LWLockRelease(newPartitionLock);
+			break;
+		}
+
+		batch->bufdescs[batch->n] =
+			PrepareOrRejectEagerFlushBuffer(buf_id + 1,
+											&require,
+											newPartitionLock,
+											&lsn);
+		if (lsn > batch->max_lsn)
+			batch->max_lsn = lsn;
+
+		if (batch->bufdescs[batch->n] == NULL)
+			break;
+	}
+}
+
+/*
+ * Given a prepared batch of buffers write them out as a vector.
+ */
+void
 FlushBufferBatch(BufferWriteBatch *batch,
 				 IOContext io_context)
 {
-- 
2.43.0

