From 343d6836e7147b1cf56df8cb04365ee214ea357f Mon Sep 17 00:00:00 2001
From: Maxim Orlov <orlovmg@gmail.com>
Date: Wed, 25 Feb 2026 17:48:42 +0300
Subject: [PATCH v2 2/5] Add test case for custom SLRU IO error

---
 src/test/modules/test_slru/expected/test_slru.out |  7 +++++++
 src/test/modules/test_slru/sql/test_slru.sql      |  4 ++++
 src/test/modules/test_slru/test_slru--1.0.sql     |  2 +-
 src/test/modules/test_slru/test_slru.c            | 15 +++++++++++++--
 4 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/src/test/modules/test_slru/expected/test_slru.out b/src/test/modules/test_slru/expected/test_slru.out
index 185c56e5d62..0dda6b60f0b 100644
--- a/src/test/modules/test_slru/expected/test_slru.out
+++ b/src/test/modules/test_slru/expected/test_slru.out
@@ -23,6 +23,13 @@ SELECT test_slru_page_exists(12345);
  t
 (1 row)
 
+-- should fail with custom error msg
+SELECT test_slru_page_read(54321);
+ERROR:  could not access test_slru entry
+DETAIL:  Could not open file "pg_test_slru/0000000000006A1": No such file or directory.
+SELECT test_slru_page_read(54321, false, '123'::xid);
+ERROR:  could not access test_slru entry 123
+DETAIL:  Could not open file "pg_test_slru/0000000000006A1": No such file or directory.
 -- 48 extra pages
 SELECT count(test_slru_page_write(a, 'Test SLRU'))
   FROM generate_series(12346, 12393, 1) as a;
diff --git a/src/test/modules/test_slru/sql/test_slru.sql b/src/test/modules/test_slru/sql/test_slru.sql
index b1b376581ab..4f66f4207b7 100644
--- a/src/test/modules/test_slru/sql/test_slru.sql
+++ b/src/test/modules/test_slru/sql/test_slru.sql
@@ -5,6 +5,10 @@ SELECT test_slru_page_write(12345, 'Test SLRU');
 SELECT test_slru_page_read(12345);
 SELECT test_slru_page_exists(12345);
 
+-- should fail with custom error msg
+SELECT test_slru_page_read(54321);
+SELECT test_slru_page_read(54321, false, '123'::xid);
+
 -- 48 extra pages
 SELECT count(test_slru_page_write(a, 'Test SLRU'))
   FROM generate_series(12346, 12393, 1) as a;
diff --git a/src/test/modules/test_slru/test_slru--1.0.sql b/src/test/modules/test_slru/test_slru--1.0.sql
index abecb5e2183..22f4f64b988 100644
--- a/src/test/modules/test_slru/test_slru--1.0.sql
+++ b/src/test/modules/test_slru/test_slru--1.0.sql
@@ -7,7 +7,7 @@ CREATE OR REPLACE FUNCTION test_slru_page_writeall() RETURNS VOID
   AS 'MODULE_PATHNAME', 'test_slru_page_writeall' LANGUAGE C;
 CREATE OR REPLACE FUNCTION test_slru_page_sync(bigint) RETURNS VOID
   AS 'MODULE_PATHNAME', 'test_slru_page_sync' LANGUAGE C;
-CREATE OR REPLACE FUNCTION test_slru_page_read(bigint, bool DEFAULT true) RETURNS text
+CREATE OR REPLACE FUNCTION test_slru_page_read(bigint, bool DEFAULT true, xid DEFAULT NULL) RETURNS text
   AS 'MODULE_PATHNAME', 'test_slru_page_read' LANGUAGE C;
 CREATE OR REPLACE FUNCTION test_slru_page_readonly(bigint) RETURNS text
   AS 'MODULE_PATHNAME', 'test_slru_page_readonly' LANGUAGE C;
diff --git a/src/test/modules/test_slru/test_slru.c b/src/test/modules/test_slru/test_slru.c
index 59ce4900173..19c69b36646 100644
--- a/src/test/modules/test_slru/test_slru.c
+++ b/src/test/modules/test_slru/test_slru.c
@@ -93,6 +93,7 @@ test_slru_page_read(PG_FUNCTION_ARGS)
 {
 	int64		pageno = PG_GETARG_INT64(0);
 	bool		write_ok = PG_GETARG_BOOL(1);
+	TransactionId xid = PG_GETARG_TRANSACTIONID(2);
 	char	   *data = NULL;
 	int			slotno;
 	LWLock	   *lock = SimpleLruGetBankLock(TestSlruCtl, pageno);
@@ -100,7 +101,7 @@ test_slru_page_read(PG_FUNCTION_ARGS)
 	/* find page in buffers, reading it if necessary */
 	LWLockAcquire(lock, LW_EXCLUSIVE);
 	slotno = SimpleLruReadPage(TestSlruCtl, pageno,
-							   write_ok, NULL);
+							   write_ok, PG_ARGISNULL(2) ? NULL : &xid);
 	data = (char *) TestSlruCtl->shared->page_buffer[slotno];
 	LWLockRelease(lock);
 
@@ -210,6 +211,16 @@ test_slru_page_precedes_logically(int64 page1, int64 page2)
 	return page1 < page2;
 }
 
+static inline char *
+test_io_error_msg(const void *opaque_data)
+{
+	if (opaque_data)
+		return psprintf("could not access test_slru entry %u",
+						*(TransactionId *) opaque_data);
+
+	return psprintf("could not access test_slru entry");
+}
+
 static void
 test_slru_shmem_startup(void)
 {
@@ -245,7 +256,7 @@ test_slru_shmem_startup(void)
 	}
 
 	TestSlruCtl->PagePrecedes = test_slru_page_precedes_logically;
-	TestSlruCtl->IoErrorMsg = TransactionIdIoErrorMsg;
+	TestSlruCtl->IoErrorMsg = test_io_error_msg;
 	SimpleLruInit(TestSlruCtl, "TestSLRU",
 				  NUM_TEST_BUFFERS, 0, slru_dir_name,
 				  test_buffer_tranche_id, test_tranche_id, SYNC_HANDLER_NONE,
-- 
2.50.1 (Apple Git-155)

