From 8a7cfbebf65f714cd2e3f24b490ed219e042e15a Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Wed, 13 Mar 2024 22:37:07 +1300
Subject: [PATCH v5 3/3] Vectored ring buffer writes.

When using eg BAS_VACUUM, we usually finish up writing recently dirtied
pages out to disk.  If the blocks are sequential, we can try to write
out multiple dirty buffers in ring order, to clean them in bulk.

XXX Should we try to detect cases when it's not working and suppress the
behaviour (ie if the dirty pages are in random order)?

XXX The reported statistics need work
---
 src/backend/storage/buffer/bufmgr.c   | 90 +++++++++++++++++++++++++--
 src/backend/storage/buffer/freelist.c | 23 +++++++
 src/include/storage/buf_internals.h   |  1 +
 3 files changed, 108 insertions(+), 6 deletions(-)

diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 073088ab938..3a975a05737 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1976,6 +1976,11 @@ again:
 		if (strategy != NULL)
 		{
 			XLogRecPtr	lsn;
+			Buffer		buffers[MAX_IO_COMBINE_LIMIT];
+			int			nbuffers;
+			BufferDesc *hdrs[MAX_IO_COMBINE_LIMIT];
+			int			nlocked;
+			int			nflushed;
 
 			/* Read the LSN while holding buffer header lock */
 			buf_state = LockBufHdr(buf_hdr);
@@ -1989,14 +1994,87 @@ again:
 				UnpinBuffer(buf_hdr);
 				goto again;
 			}
-		}
 
-		/* OK, do the I/O */
-		FlushBuffer(buf_hdr, NULL, IOOBJECT_RELATION, io_context);
-		LWLockRelease(content_lock);
+			/*
+			 * See if any future buffers could be written out at the same
+			 * time.  They must be valid, dirty, unpinned, low usage count and
+			 * consecutive, and we must be able to acquire the content lock
+			 * without waiting to avoid deadlocks.
+			 */
+			nbuffers = StrategyPeek(strategy, buffers, io_combine_limit);
+			Assert(nbuffers >= 1);
+			Assert(buffers[0] == buf);
+			hdrs[0] = buf_hdr;
+			nlocked = 1;
+			while (nlocked < nbuffers)
+			{
+				BufferDesc *hdr;
+				uint32		state;
+
+				ReservePrivateRefCountEntry();
+				ResourceOwnerEnlarge(CurrentResourceOwner);
+				hdr = GetBufferDescriptor(buffers[nlocked] - 1);
+				state = LockBufHdr(hdr);
+				if ((state & (BM_DIRTY | BM_VALID)) == (BM_DIRTY | BM_VALID) &&
+					BUF_STATE_GET_REFCOUNT(state) == 0 &&
+					BUF_STATE_GET_USAGECOUNT(state) <= 1)
+				{
+					PinBuffer_Locked(hdr);
+					if (BufferTagsConsecutive(&hdrs[nlocked - 1]->tag,
+											  &hdr->tag) &&
+						LWLockConditionalAcquire(BufferDescriptorGetContentLock(hdr),
+												 LW_SHARED))
+					{
+						/* Yes, we can extend the range by one. */
+						hdrs[nlocked++] = hdr;
+					}
+					else
+					{
+						UnpinBuffer(hdr);
+						break;
+					}
+				}
+				else
+				{
+					UnlockBufHdr(hdr, state);
+					break;
+				}
+			}
 
-		ScheduleBufferTagForWriteback(&BackendWritebackContext, io_context,
-									  &buf_hdr->tag, 1);
+			/*
+			 * Flush as many of these as we can.  This may stop partway
+			 * through, if it can't get an I/O lock, but it always flushes at
+			 * least one.  Since we're only trying to be helpful by doing more
+			 * than one, no need to loop for the rest.
+			 */
+			nflushed = FlushBuffers(hdrs, nlocked, NULL,
+									IOOBJECT_RELATION, io_context,
+									NULL, NULL);
+			Assert(nflushed >= 1);
+
+			/* Unlock in reverse order because it goes faster. */
+			for (int i = nlocked - 1; i >= 0; --i)
+				LWLockRelease(BufferDescriptorGetContentLock(hdrs[i]));
+
+			ScheduleBufferTagForWriteback(&BackendWritebackContext, io_context,
+										  &buf_hdr->tag, nflushed);
+
+			/* Unpin all but the first. */
+			for (int i = 1; i < nlocked; ++i)
+				UnpinBuffer(hdrs[i]);
+		}
+		else
+		{
+			/*
+			 * Do single block I/O.  There is no reason to expect buffers that
+			 * don't come from a ring to be consecutive.
+			 */
+			FlushBuffer(buf_hdr, NULL, IOOBJECT_RELATION, io_context);
+			LWLockRelease(content_lock);
+
+			ScheduleBufferTagForWriteback(&BackendWritebackContext, io_context,
+										  &buf_hdr->tag, 1);
+		}
 	}
 
 
diff --git a/src/backend/storage/buffer/freelist.c b/src/backend/storage/buffer/freelist.c
index 3611357fa30..6a3e548e7ca 100644
--- a/src/backend/storage/buffer/freelist.c
+++ b/src/backend/storage/buffer/freelist.c
@@ -772,3 +772,26 @@ StrategyRejectBuffer(BufferAccessStrategy strategy, BufferDesc *buf, bool from_r
 
 	return true;
 }
+
+/*
+ * StrategyPeek -- which buffers will be returned by future calls to
+ * GetBufferFromRing()?  The 'current' buffer, ie most recently returned by
+ * GetBufferFromRing() will be in position 0.
+ */
+int
+StrategyPeek(BufferAccessStrategy strategy, Buffer *buffers, int nbuffers)
+{
+	int			cursor = strategy->current;
+	int			n = 0;
+
+	while (strategy->buffers[cursor] != InvalidBuffer && n < nbuffers)
+	{
+		buffers[n++] = strategy->buffers[cursor++];
+		if (cursor == strategy->nbuffers)
+			cursor = 0;
+		if (cursor == strategy->current)
+			break;
+	}
+
+	return n;
+}
diff --git a/src/include/storage/buf_internals.h b/src/include/storage/buf_internals.h
index 93822dd8407..588864e156c 100644
--- a/src/include/storage/buf_internals.h
+++ b/src/include/storage/buf_internals.h
@@ -438,6 +438,7 @@ extern BufferDesc *StrategyGetBuffer(BufferAccessStrategy strategy,
 extern void StrategyFreeBuffer(BufferDesc *buf);
 extern bool StrategyRejectBuffer(BufferAccessStrategy strategy,
 								 BufferDesc *buf, bool from_ring);
+extern int	StrategyPeek(BufferAccessStrategy strategy, Buffer *buffers, int nbuffers);
 
 extern int	StrategySyncStart(uint32 *complete_passes, uint32 *num_buf_alloc);
 extern void StrategyNotifyBgWriter(int bgwprocno);
-- 
2.44.0

