From 11c67779bc256090d3d0ade3dec22ff0966868fb Mon Sep 17 00:00:00 2001
From: David Christensen <david.christensen@crunchydata.com>
Date: Wed, 17 Jan 2024 13:31:29 -0500
Subject: [PATCH v3 02/28] refactor: Make PageGetUsablePageSize() routine

This is the equivalent change, but for locations which utilized
PageGetPageSize() and SizeOfPageHeaderData.
---
 src/backend/access/nbtree/nbtdedup.c    |  2 +-
 src/backend/access/nbtree/nbtsplitloc.c |  2 +-
 src/include/access/hash.h               |  7 +++----
 src/include/access/nbtree.h             |  8 ++++----
 src/include/storage/bufpage.h           | 10 ++++++++++
 5 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/src/backend/access/nbtree/nbtdedup.c b/src/backend/access/nbtree/nbtdedup.c
index 456d86b51c..6808dbe064 100644
--- a/src/backend/access/nbtree/nbtdedup.c
+++ b/src/backend/access/nbtree/nbtdedup.c
@@ -825,7 +825,7 @@ _bt_singleval_fillfactor(Page page, BTDedupState state, Size newitemsz)
 	int			reduction;
 
 	/* This calculation needs to match nbtsplitloc.c */
-	leftfree = PageGetPageSize(page) - SizeOfPageHeaderData -
+	leftfree = PageGetUsablePageSize(page) -
 		MAXALIGN(sizeof(BTPageOpaqueData));
 	/* Subtract size of new high key (includes pivot heap TID space) */
 	leftfree -= newitemsz + MAXALIGN(sizeof(ItemPointerData));
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..31bbe1812a 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -156,7 +156,7 @@ _bt_findsplitloc(Relation rel,
 
 	/* Total free space available on a btree page, after fixed overhead */
 	leftspace = rightspace =
-		PageGetPageSize(origpage) - SizeOfPageHeaderData -
+		PageGetUsablePageSize(origpage) -
 		MAXALIGN(sizeof(BTPageOpaqueData));
 
 	/* The right page will have the same high key as the old page */
diff --git a/src/include/access/hash.h b/src/include/access/hash.h
index 9c7d81525b..e777ac4d06 100644
--- a/src/include/access/hash.h
+++ b/src/include/access/hash.h
@@ -285,8 +285,7 @@ typedef struct HashOptions
  * Maximum size of a hash index item (it's okay to have only one per page)
  */
 #define HashMaxItemSize(page) \
-	MAXALIGN_DOWN(PageGetPageSize(page) - \
-				  SizeOfPageHeaderData - \
+	MAXALIGN_DOWN(PageGetUsablePageSize(page) - \
 				  sizeof(ItemIdData) - \
 				  MAXALIGN(sizeof(HashPageOpaqueData)))
 
@@ -317,8 +316,8 @@ typedef struct HashOptions
 	((uint32 *) PageGetContents(page))
 
 #define HashGetMaxBitmapSize(page) \
-	(PageGetPageSize((Page) page) - \
-	 (MAXALIGN(SizeOfPageHeaderData) + MAXALIGN(sizeof(HashPageOpaqueData))))
+	(PageGetUsablePageSize((Page) page) - \
+	 MAXALIGN(sizeof(HashPageOpaqueData)))
 
 #define HashPageGetMeta(page) \
 	((HashMetaPage) PageGetContents(page))
diff --git a/src/include/access/nbtree.h b/src/include/access/nbtree.h
index 915a51bc23..27d42a3ff0 100644
--- a/src/include/access/nbtree.h
+++ b/src/include/access/nbtree.h
@@ -162,13 +162,13 @@ typedef struct BTMetaPageData
  * attribute, which we account for here.
  */
 #define BTMaxItemSize(page) \
-	(MAXALIGN_DOWN((PageGetPageSize(page) - \
-					MAXALIGN(SizeOfPageHeaderData + 3*sizeof(ItemIdData)) - \
+	(MAXALIGN_DOWN((PageGetUsablePageSize(page) - \
+					MAXALIGN(3*sizeof(ItemIdData)) - \
 					MAXALIGN(sizeof(BTPageOpaqueData))) / 3) - \
 					MAXALIGN(sizeof(ItemPointerData)))
 #define BTMaxItemSizeNoHeapTid(page) \
-	MAXALIGN_DOWN((PageGetPageSize(page) - \
-				   MAXALIGN(SizeOfPageHeaderData + 3*sizeof(ItemIdData)) - \
+	MAXALIGN_DOWN((PageGetUsablePageSize(page) - \
+				   MAXALIGN(3*sizeof(ItemIdData)) - \
 				   MAXALIGN(sizeof(BTPageOpaqueData))) / 3)
 
 /*
diff --git a/src/include/storage/bufpage.h b/src/include/storage/bufpage.h
index aff24a5ea2..5492a3682c 100644
--- a/src/include/storage/bufpage.h
+++ b/src/include/storage/bufpage.h
@@ -309,6 +309,16 @@ PageSetPageSizeAndVersion(Page page, Size size, uint8 version)
 	((PageHeader) page)->pd_pagesize_version = size | version;
 }
 
+/*
+ * PageGetUsablePageSize
+ *		Returns the usable space on a page (from end of page header to reserved space)
+ */
+static inline uint16
+PageGetUsablePageSize(Page page)
+{
+	return PageGetPageSize(page) - SizeOfPageHeaderData;
+}
+
 /* ----------------
  *		page special data functions
  * ----------------
-- 
2.40.1

