From 9297c5f00ff3230e5faf1c7dad5de7d2d5868ec9 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Fri, 13 Oct 2023 11:23:16 -0700
Subject: [PATCH v1 2/2] Assert buffer in ReadBufferBI() is for correct
 relation

Author:
Reviewed-by:
Discussion: https://postgr.es/m/
Backpatch:
---
 src/backend/access/heap/hio.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/src/backend/access/heap/hio.c b/src/backend/access/heap/hio.c
index caa62708aa5..ed9f72379ba 100644
--- a/src/backend/access/heap/hio.c
+++ b/src/backend/access/heap/hio.c
@@ -99,6 +99,17 @@ ReadBufferBI(Relation relation, BlockNumber targetBlock,
 	/* If we have the desired block already pinned, re-pin and return it */
 	if (bistate->current_buf != InvalidBuffer)
 	{
+#ifdef USE_ASSERT_CHECKING
+		{
+			RelFileLocator rlocator;
+			ForkNumber forknum;
+			BlockNumber blknum;
+
+			BufferGetTag(bistate->current_buf, &rlocator, &forknum, &blknum);
+			Assert(RelFileLocatorEquals(rlocator, relation->rd_locator));
+		}
+#endif
+
 		if (BufferGetBlockNumber(bistate->current_buf) == targetBlock)
 		{
 			/*
-- 
2.38.0

