From 714ee0a8f517157c5f59ff82aa1fc82e6be36921 Mon Sep 17 00:00:00 2001
From: Arseniy Mukhin <arseniy.mukhin.dev@gmail.com>
Date: Wed, 16 Apr 2025 11:26:45 +0300
Subject: [PATCH v9 1/4] brin refactoring

For adding BRIN index support in amcheck we need some tiny changes in BRIN
core code:

* We need to have tuple descriptor for on-disk storage of BRIN tuples.
  It is a public field 'bd_disktdesc' in BrinDesc, but to access it we
  need function 'brtuple_disk_tupdesc' which is internal. This commit
  makes it extern and renames it to 'brin_tuple_tupdesc'.

* For meta page check we need to know pages_per_range upper limit. It's
  hardcoded now. This commit moves its value to macros BRIN_MAX_PAGES_PER_RANGE
  so that we can use it in amcheck too.
---
 src/backend/access/brin/brin_tuple.c   | 10 +++++-----
 src/backend/access/common/reloptions.c |  3 ++-
 src/include/access/brin.h              |  1 +
 src/include/access/brin_tuple.h        |  2 ++
 4 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/backend/access/brin/brin_tuple.c b/src/backend/access/brin/brin_tuple.c
index 861f397e6db..fc67a708dda 100644
--- a/src/backend/access/brin/brin_tuple.c
+++ b/src/backend/access/brin/brin_tuple.c
@@ -57,8 +57,8 @@ static inline void brin_deconstruct_tuple(BrinDesc *brdesc,
 /*
  * Return a tuple descriptor used for on-disk storage of BRIN tuples.
  */
-static TupleDesc
-brtuple_disk_tupdesc(BrinDesc *brdesc)
+TupleDesc
+brin_tuple_tupdesc(BrinDesc *brdesc)
 {
 	/* We cache these in the BrinDesc */
 	if (brdesc->bd_disktdesc == NULL)
@@ -280,7 +280,7 @@ brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple,
 
 	len = hoff = MAXALIGN(len);
 
-	data_len = heap_compute_data_size(brtuple_disk_tupdesc(brdesc),
+	data_len = heap_compute_data_size(brin_tuple_tupdesc(brdesc),
 									  values, nulls);
 	len += data_len;
 
@@ -299,7 +299,7 @@ brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple,
 	 * need to pass a valid null bitmap so that it will correctly skip
 	 * outputting null attributes in the data area.
 	 */
-	heap_fill_tuple(brtuple_disk_tupdesc(brdesc),
+	heap_fill_tuple(brin_tuple_tupdesc(brdesc),
 					values,
 					nulls,
 					(char *) rettuple + hoff,
@@ -682,7 +682,7 @@ brin_deconstruct_tuple(BrinDesc *brdesc,
 	 * may reuse attribute entries for more than one column, we cannot cache
 	 * offsets here.
 	 */
-	diskdsc = brtuple_disk_tupdesc(brdesc);
+	diskdsc = brin_tuple_tupdesc(brdesc);
 	stored = 0;
 	off = 0;
 	for (attnum = 0; attnum < brdesc->bd_tupdesc->natts; attnum++)
diff --git a/src/backend/access/common/reloptions.c b/src/backend/access/common/reloptions.c
index 50747c16396..bc494847341 100644
--- a/src/backend/access/common/reloptions.c
+++ b/src/backend/access/common/reloptions.c
@@ -22,6 +22,7 @@
 #include "access/heaptoast.h"
 #include "access/htup_details.h"
 #include "access/nbtree.h"
+#include "access/brin.h"
 #include "access/reloptions.h"
 #include "access/spgist_private.h"
 #include "catalog/pg_type.h"
@@ -343,7 +344,7 @@ static relopt_int intRelOpts[] =
 			"Number of pages that each page range covers in a BRIN index",
 			RELOPT_KIND_BRIN,
 			AccessExclusiveLock
-		}, 128, 1, 131072
+		}, 128, 1, BRIN_MAX_PAGES_PER_RANGE
 	},
 	{
 		{
diff --git a/src/include/access/brin.h b/src/include/access/brin.h
index 821f1e02806..334ce973b67 100644
--- a/src/include/access/brin.h
+++ b/src/include/access/brin.h
@@ -37,6 +37,7 @@ typedef struct BrinStatsData
 
 
 #define BRIN_DEFAULT_PAGES_PER_RANGE	128
+#define BRIN_MAX_PAGES_PER_RANGE	131072
 #define BrinGetPagesPerRange(relation) \
 	(AssertMacro(relation->rd_rel->relkind == RELKIND_INDEX && \
 				 relation->rd_rel->relam == BRIN_AM_OID), \
diff --git a/src/include/access/brin_tuple.h b/src/include/access/brin_tuple.h
index 010ba4ea3c0..2a12ab03c43 100644
--- a/src/include/access/brin_tuple.h
+++ b/src/include/access/brin_tuple.h
@@ -109,4 +109,6 @@ extern BrinMemTuple *brin_memtuple_initialize(BrinMemTuple *dtuple,
 extern BrinMemTuple *brin_deform_tuple(BrinDesc *brdesc,
 									   BrinTuple *tuple, BrinMemTuple *dMemtuple);
 
+extern TupleDesc brin_tuple_tupdesc(BrinDesc *brdesc);
+
 #endif							/* BRIN_TUPLE_H */
-- 
2.43.0

