From 91cb53fdd50505be2b7b79e593ec3cc0b9a6575d Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Wed, 3 Jun 2026 18:19:35 -0400
Subject: [PATCH v17 14/21] Add client backend write combining test

Test that a client backend evicting dirty buffers through a strategy
ring combines contiguous dirty blocks into larger writes, both via
normal victim buffer eviction and via WriteBufferAndNeighbors()
directly, and that non-resident gaps fall back to single-block writes.

The normal buffer eviction path is likely both too expensive and too
prone to flakiness to be committable in the end, but it is nice to
exercise this at least during development.
---
 src/backend/storage/buffer/bufmgr.c           |   2 +-
 src/include/storage/buf_internals.h           |   3 +
 .../modules/test_aio/t/005_write_combining.pl | 215 +++++++++++++++++-
 src/test/modules/test_aio/test_aio--1.0.sql   |   6 +-
 src/test/modules/test_aio/test_aio.c          |  38 ++++
 5 files changed, 261 insertions(+), 3 deletions(-)

diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index e1def5fc7fa..82c50550861 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -2601,7 +2601,7 @@ InvalidateVictimBuffer(BufferDesc *buf_hdr)
  * with it in a single write. The victim buffer must be already pinned and
  * locked and will remain pinned upon return.
  */
-static void
+void
 WriteBufferAndNeighbors(Buffer bufnum, BufferDesc *buf_hdr, IOContext io_context,
 						WritebackContext *wb_context)
 {
diff --git a/src/include/storage/buf_internals.h b/src/include/storage/buf_internals.h
index 406d2a0e03c..45260ecff60 100644
--- a/src/include/storage/buf_internals.h
+++ b/src/include/storage/buf_internals.h
@@ -597,6 +597,9 @@ extern StartBufferIOResult StartSharedBufferIO(BufferDesc *buf, bool forInput, b
 extern void TerminateBufferIO(BufferDesc *buf, bool clear_dirty, uint64 set_flag_bits,
 							  bool forget_owner, bool release_aio);
 
+extern void WriteBufferAndNeighbors(Buffer bufnum, BufferDesc *buf_hdr, IOContext io_context,
+									WritebackContext *wb_context);
+
 
 /* freelist.c */
 extern IOContext IOContextForStrategy(BufferAccessStrategy strategy);
diff --git a/src/test/modules/test_aio/t/005_write_combining.pl b/src/test/modules/test_aio/t/005_write_combining.pl
index 36c866ade17..fbebe822e0e 100644
--- a/src/test/modules/test_aio/t/005_write_combining.pl
+++ b/src/test/modules/test_aio/t/005_write_combining.pl
@@ -15,7 +15,11 @@ $node->append_conf(
 max_wal_size = '4GB'
 checkpoint_timeout = '1d'
 autovacuum = off
-io_combine_limit = '128kB'
+# Use block counts so the six-page write fits at every block size, with
+# enough shared buffers for the backend to pin the whole batch.
+shared_buffers = 4096
+io_max_combine_limit = 6
+io_combine_limit = 6
 bgwriter_lru_maxpages = 0
 ));
 
@@ -27,6 +31,8 @@ my $block_size = $node->safe_psql('postgres',
 	"SELECT current_setting('block_size')::int");
 
 test_checkpointer_combines_writes($node, $block_size);
+test_regular_backend_combines_writes($node, $block_size);
+test_eager_clean_combines_writes($node, $block_size);
 
 $node->stop();
 
@@ -50,6 +56,20 @@ sub io_stat_writes
 	return split /\|/, $result;
 }
 
+sub io_stat_evictions
+{
+	my ($node, $backend_type, $context) = @_;
+
+	return $node->safe_psql(
+		'postgres', qq(
+	SELECT COALESCE(sum(evictions), 0)::bigint
+	FROM pg_stat_io
+	WHERE backend_type = '$backend_type'
+	AND object = 'relation'
+	AND context = '$context';
+	));
+}
+
 sub assert_combined_writes
 {
 	local $Test::Builder::Level = $Test::Builder::Level + 1;
@@ -63,6 +83,20 @@ sub assert_combined_writes
 	ok($avg_write_bytes > $block_size, "$label combined writes");
 }
 
+sub assert_writes
+{
+	local $Test::Builder::Level = $Test::Builder::Level + 1;
+
+	my ($node, $label, $backend_type, $context, $expected_writes, $expected_bytes) = @_;
+	my ($writes, $write_bytes, $avg_write_bytes) =
+	  io_stat_writes($node, $backend_type, $context);
+
+	note "$label: writes=$writes write_bytes=$write_bytes avg_write_bytes=$avg_write_bytes";
+
+	is($writes, $expected_writes, "$label write count");
+	is($write_bytes, $expected_bytes, "$label write bytes");
+}
+
 sub assert_writes_at_least
 {
 	local $Test::Builder::Level = $Test::Builder::Level + 1;
@@ -78,6 +112,18 @@ sub assert_writes_at_least
 		"$label wrote at least $expected_bytes bytes");
 }
 
+sub assert_evictions_at_least
+{
+	local $Test::Builder::Level = $Test::Builder::Level + 1;
+
+	my ($node, $label, $expected_evictions) = @_;
+	my $evictions = io_stat_evictions($node, 'client backend', 'normal');
+
+	note "$label: evictions=$evictions";
+	ok($evictions >= $expected_evictions,
+		"$label evicted at least $expected_evictions buffers");
+}
+
 sub assert_blocks_dirty
 {
 	local $Test::Builder::Level = $Test::Builder::Level + 1;
@@ -100,6 +146,14 @@ sub assert_any_blocks_dirty
 		$expected, $label);
 }
 
+sub flush_and_reset_io_stats
+{
+	my ($node, $psql) = @_;
+
+	$psql->query_safe('SELECT pg_stat_force_next_flush()');
+	$node->safe_psql('postgres', "SELECT pg_stat_reset_shared('io')");
+}
+
 sub wait_for_io_stats
 {
 	my ($node, $backend_type, $expected_bytes) = @_;
@@ -119,6 +173,40 @@ sub dirty_blocks
 		"SELECT make_blocks_dirty_flushed('$table', ARRAY[$blocks])");
 }
 
+sub dirty_reusable_blocks
+{
+	my ($psql, $table, $blocks) = @_;
+
+	$psql->query_safe(
+		"SELECT make_blocks_dirty_flushed('$table', ARRAY[$blocks], true)");
+}
+
+sub allocate_until_blocks_clean
+{
+	my ($node, $psql, $table, $blocks, $filler, $label) = @_;
+	my $shared_buffers_blocks = $node->safe_psql(
+		'postgres',
+		"SELECT pg_size_bytes(current_setting('shared_buffers')) / current_setting('block_size')::int"
+	);
+	my $extend_by = 1024;
+	my $allocated = 0;
+	my $max_allocations = 3 * $shared_buffers_blocks;
+
+	while ($node->safe_psql('postgres',
+			"SELECT true = ANY (rel_blocks_are_dirty('$table', ARRAY[$blocks]))") eq 't')
+	{
+		if ($allocated >= $max_allocations)
+		{
+			die "$label: blocks $blocks still dirty after $allocated buffer allocations";
+		}
+
+		$psql->query_safe("SELECT grow_rel('$filler'::regclass, $extend_by)");
+		$allocated += $extend_by;
+	}
+
+	note "$label: allocated $allocated buffers to reach regular backend victims";
+	$psql->query_safe('SELECT pg_stat_force_next_flush()');
+}
 
 sub test_checkpointer_combines_writes
 {
@@ -170,3 +258,128 @@ sub test_checkpointer_combines_writes
 
 	$psql->quit();
 }
+
+# A higher level test that might be too expensive to commit
+sub test_regular_backend_combines_writes
+{
+	my ($node, $block_size) = @_;
+	my $psql = $node->background_psql('postgres', on_error_stop => 0);
+
+	# Flush existing dirty buffers to reduce unrelated writes during the test
+	$node->safe_psql('postgres', 'CHECKPOINT');
+
+	$node->safe_psql(
+		'postgres', qq(
+	CREATE TABLE wc_backend (id int, payload text);
+	INSERT INTO wc_backend SELECT g, repeat('y', 200) FROM generate_series(1, 1000) AS g;
+	SELECT flush_rel_buffers('wc_backend'::regclass);
+	CREATE UNLOGGED TABLE wc_backend_filler (id int);
+	CHECKPOINT;
+	));
+
+	####
+	# Test one big combined write from regular backend buffer allocation.
+	####
+
+	dirty_reusable_blocks($psql, 'wc_backend', '0,1,2,3,4,5');
+	assert_blocks_dirty($node, 'wc_backend', '0,1,2,3,4,5', 't',
+		'contiguous buffers are dirty before regular backend allocation');
+
+	flush_and_reset_io_stats($node, $psql);
+	allocate_until_blocks_clean($node, $psql, 'wc_backend', '0,1,2,3,4,5',
+		'wc_backend_filler', 'contiguous regular backend');
+
+	# Assert that combining happened at all, which shouldn't fail even if the
+	# more specific test below it flakes.
+	assert_combined_writes($node, 'contiguous regular backend', 'client backend',
+		'normal', $block_size);
+	# Unlike the checkpointer/bgwriter/eager-clean cases, we force writes here
+	# by growing a filler relation until the target blocks are clean. Unrelated
+	# dirty victims (e.g. a catalog page freshly dirtied by a hint bit) can
+	# increase the write count without preventing combining, so this exact write
+	# count test may fail. That's okay since we think this whole test routine is
+	# probably too expensive to commit.
+	assert_writes($node, 'contiguous regular backend', 'client backend',
+		'normal', 1, 6 * $block_size);
+	assert_evictions_at_least($node, 'contiguous regular backend', 6);
+	assert_any_blocks_dirty($node, 'wc_backend', '0,1,2,3,4,5', 'f',
+		'regular backend wrote contiguous dirty buffers');
+
+	####
+	# Test multiple single block writes when interspersed blocks are not in
+	# shared buffers.
+	####
+
+	$psql->query_safe(
+		"SELECT invalidate_rel_blocks('wc_backend', ARRAY[1,3,5])");
+	dirty_reusable_blocks($psql, 'wc_backend', '0,2,4');
+	flush_and_reset_io_stats($node, $psql);
+
+	allocate_until_blocks_clean($node, $psql, 'wc_backend', '0,2,4',
+		'wc_backend_filler', 'nonresident gaps regular backend');
+	# The three dirty blocks are separated by non-resident gaps, so they must
+	# not be combined: at least three separate writes. As above, stray dirty
+	# pages evicted by the same sweep may push the count higher, so assert "at
+	# least" rather than an exact count.
+	assert_writes_at_least($node, 'nonresident gaps regular backend', 'client backend',
+		'normal', 3,
+		3 * $block_size);
+	assert_evictions_at_least($node, 'nonresident gaps regular backend', 3);
+	assert_any_blocks_dirty($node, 'wc_backend', '0,2,4', 'f',
+		'regular backend wrote dirty buffers separated by nonresident gaps');
+
+	$psql->quit();
+}
+
+sub test_eager_clean_combines_writes
+{
+	my ($node, $block_size) = @_;
+	my $psql = $node->background_psql('postgres', on_error_stop => 0);
+
+	$node->safe_psql(
+		'postgres', qq(
+	CREATE TABLE wc_victim (id int, payload text);
+	INSERT INTO wc_victim SELECT g, repeat('y', 200) FROM generate_series(1, 1000) AS g;
+	SELECT flush_rel_buffers('wc_victim'::regclass);
+	CHECKPOINT;
+	));
+
+	####
+	# Test one big combined write when WriteBufferAndNeighbors() is called directly
+	####
+
+	dirty_blocks($psql, 'wc_victim', '0,1,2,3,4,5');
+	assert_blocks_dirty($node, 'wc_victim', '0,1,2,3,4,5', 't',
+		'contiguous buffers are dirty before direct eager clean');
+
+	flush_and_reset_io_stats($node, $psql);
+	$psql->query_safe("SELECT eager_clean_rel_block('wc_victim', 0)");
+	$psql->query_safe('SELECT pg_stat_force_next_flush()');
+
+	assert_writes($node, 'contiguous direct eager clean', 'client backend',
+		'normal', 1, 6 * $block_size);
+	assert_any_blocks_dirty($node, 'wc_victim', '0,1,2,3,4,5', 'f',
+		'direct eager clean wrote contiguous dirty buffers');
+
+	####
+	# Test multiple single block writes when interspersed blocks are not in
+	# shared buffers.
+	####
+
+	$psql->query_safe(
+		"SELECT invalidate_rel_blocks('wc_victim', ARRAY[1,3,5])");
+	dirty_blocks($psql, 'wc_victim', '0,2,4');
+	flush_and_reset_io_stats($node, $psql);
+
+	$psql->query_safe("SELECT eager_clean_rel_block('wc_victim', 0)");
+	$psql->query_safe("SELECT eager_clean_rel_block('wc_victim', 2)");
+	$psql->query_safe("SELECT eager_clean_rel_block('wc_victim', 4)");
+	$psql->query_safe('SELECT pg_stat_force_next_flush()');
+	assert_writes($node, 'nonresident gaps direct eager clean', 'client backend',
+		'normal', 3,
+		3 * $block_size);
+	assert_any_blocks_dirty($node, 'wc_victim', '0,2,4', 'f',
+		'direct eager clean wrote dirty buffers separated by nonresident gaps');
+
+	$psql->quit();
+}
diff --git a/src/test/modules/test_aio/test_aio--1.0.sql b/src/test/modules/test_aio/test_aio--1.0.sql
index 49bf945c030..1afd430ccdf 100644
--- a/src/test/modules/test_aio/test_aio--1.0.sql
+++ b/src/test/modules/test_aio/test_aio--1.0.sql
@@ -41,7 +41,11 @@ CREATE FUNCTION flush_rel_buffers(rel regclass)
 RETURNS pg_catalog.void STRICT
 AS 'MODULE_PATHNAME' LANGUAGE C;
 
-CREATE FUNCTION make_blocks_dirty_flushed(rel regclass, blocks int4[])
+CREATE FUNCTION make_blocks_dirty_flushed(rel regclass, blocks int4[], clear_usage_count boolean DEFAULT false)
+RETURNS pg_catalog.void STRICT
+AS 'MODULE_PATHNAME' LANGUAGE C;
+
+CREATE FUNCTION eager_clean_rel_block(rel regclass, blockno int4)
 RETURNS pg_catalog.void STRICT
 AS 'MODULE_PATHNAME' LANGUAGE C;
 
diff --git a/src/test/modules/test_aio/test_aio.c b/src/test/modules/test_aio/test_aio.c
index 50aa25ba745..022d8279c8f 100644
--- a/src/test/modules/test_aio/test_aio.c
+++ b/src/test/modules/test_aio/test_aio.c
@@ -633,6 +633,7 @@ make_blocks_dirty_flushed(PG_FUNCTION_ARGS)
 {
 	Oid			relid = PG_GETARG_OID(0);
 	ArrayType  *blocksarray = PG_GETARG_ARRAYTYPE_P(1);
+	bool		clear_usage_count = PG_GETARG_BOOL(2);
 	Relation	rel;
 	uint32	   *blocks;
 	int			nblocks;
@@ -660,6 +661,15 @@ make_blocks_dirty_flushed(PG_FUNCTION_ARGS)
 		LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
 		FlushOneBuffer(buf);
 		MarkBufferDirty(buf);
+
+		/* Make this block eligible for the clock sweep once we unpin it. */
+		if (clear_usage_count)
+		{
+			BufferDesc *desc = GetBufferDescriptor(buf - 1);
+			uint64		buf_state = LockBufHdr(desc);
+
+			UnlockBufHdrExt(desc, buf_state, 0, BUF_USAGECOUNT_MASK, 0);
+		}
 		UnlockReleaseBuffer(buf);
 	}
 
@@ -668,6 +678,34 @@ make_blocks_dirty_flushed(PG_FUNCTION_ARGS)
 	PG_RETURN_VOID();
 }
 
+PG_FUNCTION_INFO_V1(eager_clean_rel_block);
+Datum
+eager_clean_rel_block(PG_FUNCTION_ARGS)
+{
+	Oid			relid = PG_GETARG_OID(0);
+	BlockNumber blkno = PG_GETARG_UINT32(1);
+	Relation	rel;
+	Buffer		buf;
+	WritebackContext wb_context;
+	int			flush_after = 0;
+
+	rel = relation_open(relid, AccessShareLock);
+	if (RelationUsesLocalBuffers(rel))
+		ereport(ERROR,
+				(errmsg("cannot eager clean local buffers")));
+
+	buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, NULL);
+	WritebackContextInit(&wb_context, &flush_after);
+	LockBuffer(buf, BUFFER_LOCK_SHARE_EXCLUSIVE);
+	WriteBufferAndNeighbors(buf, GetBufferDescriptor(buf - 1), IOCONTEXT_NORMAL,
+							&wb_context);
+	ReleaseBuffer(buf);
+
+	relation_close(rel, NoLock);
+
+	PG_RETURN_VOID();
+}
+
 PG_FUNCTION_INFO_V1(rel_blocks_are_dirty);
 Datum
 rel_blocks_are_dirty(PG_FUNCTION_ARGS)
-- 
2.43.0

