From e0ca1f4558380fedc08121aa303d6024de467e3f Mon Sep 17 00:00:00 2001
From: David Christensen <david.christensen@crunchydata.com>
Date: Tue, 12 Dec 2023 16:55:52 -0500
Subject: [PATCH v4 03/22] feature: Add ReservedPageSize variable

We redefine PageUsableSpace to account for this variable, as well as introduce
the PageUsableSpaceMax to reflect /just/ BLCKSZ - SizeOfPageHeaderData in the
few call sites that care at this point.
---
 src/backend/access/heap/visibilitymap.c |  3 +--
 src/backend/nodes/tidbitmap.c           |  2 +-
 src/backend/storage/page/bufpage.c      |  2 +-
 src/bin/pg_upgrade/file.c               |  2 +-
 src/include/storage/bufpage.h           | 10 +++++-----
 src/include/storage/fsm_internals.h     |  2 +-
 6 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/src/backend/access/heap/visibilitymap.c b/src/backend/access/heap/visibilitymap.c
index 5c48b7d63c..6bde6f4388 100644
--- a/src/backend/access/heap/visibilitymap.c
+++ b/src/backend/access/heap/visibilitymap.c
@@ -414,8 +414,7 @@ visibilitymap_count(Relation rel, BlockNumber *all_visible, BlockNumber *all_fro
 		 */
 		map = (uint64 *) PageGetContents(BufferGetPage(mapBuffer));
 
-		StaticAssertStmt(MAPSIZE % sizeof(uint64) == 0,
-						 "unsupported MAPSIZE");
+		Assert(MAPSIZE % sizeof(uint64) == 0);
 		if (all_frozen == NULL)
 		{
 			for (i = 0; i < MAPSIZE / sizeof(uint64); i++)
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 1afa431062..8df8ddb02f 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -54,7 +54,7 @@
  * the per-page bitmaps variable size.  We just legislate that the size
  * is this:
  */
-#define MAX_TUPLES_PER_PAGE  ClusterMaxHeapTuplesPerPage
+#define MAX_TUPLES_PER_PAGE  MaxHeapTuplesPerPageLimit
 
 /*
  * When we have to switch over to lossy storage, we use a data structure
diff --git a/src/backend/storage/page/bufpage.c b/src/backend/storage/page/bufpage.c
index bc37eae9f2..353ae286a4 100644
--- a/src/backend/storage/page/bufpage.c
+++ b/src/backend/storage/page/bufpage.c
@@ -26,7 +26,7 @@
 /* GUC variable */
 bool		ignore_checksum_failure = false;
 
-
+int ReservedPageSize = 0;
 /* ----------------------------------------------------------------
  *						Page support functions
  * ----------------------------------------------------------------
diff --git a/src/bin/pg_upgrade/file.c b/src/bin/pg_upgrade/file.c
index 284af5077b..1ad589ca45 100644
--- a/src/bin/pg_upgrade/file.c
+++ b/src/bin/pg_upgrade/file.c
@@ -227,7 +227,7 @@ rewriteVisibilityMap(const char *fromfile, const char *tofile,
 	struct stat statbuf;
 
 	/* Compute number of old-format bytes per new page */
-	rewriteVmBytesPerPage = (PageUsableSpace) / 2;
+	rewriteVmBytesPerPage = (PageUsableSpaceMax) / 2;
 
 	if ((src_fd = open(fromfile, O_RDONLY | PG_BINARY, 0)) < 0)
 		pg_fatal("error while copying relation \"%s.%s\": could not open file \"%s\": %m",
diff --git a/src/include/storage/bufpage.h b/src/include/storage/bufpage.h
index 6eee77e57e..5a6060ca89 100644
--- a/src/include/storage/bufpage.h
+++ b/src/include/storage/bufpage.h
@@ -214,11 +214,11 @@ typedef PageHeaderData *PageHeader;
 #define SizeOfPageHeaderData (offsetof(PageHeaderData, pd_linp))
 
 /*
- * how much space is left after smgr's bookkeeping, etc
+ * how much space is left after smgr's bookkeeping, etc; should be MAXALIGN
  */
-#define PageUsableSpace (BLCKSZ - SizeOfPageHeaderData)
-StaticAssertDecl(PageUsableSpace == MAXALIGN(PageUsableSpace),
-				 "SizeOfPageHeaderData must be MAXALIGN'd");
+extern int ReservedPageSize;
+
+#define PageUsableSpace (BLCKSZ - SizeOfPageHeaderData - ReservedPageSize)
 #define PageUsableSpaceMax (BLCKSZ - SizeOfPageHeaderData)
 
 /*
@@ -317,7 +317,7 @@ PageSetPageSizeAndVersion(Page page, Size size, uint8 version)
 static inline uint16
 PageGetUsablePageSize(Page page)
 {
-	return PageGetPageSize(page) - SizeOfPageHeaderData;
+	return PageGetPageSize(page) - SizeOfPageHeaderData - ReservedPageSize;
 }
 
 /* ----------------
diff --git a/src/include/storage/fsm_internals.h b/src/include/storage/fsm_internals.h
index 148693d977..195fb7804a 100644
--- a/src/include/storage/fsm_internals.h
+++ b/src/include/storage/fsm_internals.h
@@ -48,7 +48,7 @@ typedef FSMPageData *FSMPage;
  * Number of non-leaf and leaf nodes, and nodes in total, on an FSM page.
  * These definitions are internal to fsmpage.c.
  */
-#define NodesPerPage (PageUsableSpace - \
+#define NodesPerPage (PageUsableSpaceMax - \
 					  offsetof(FSMPageData, fp_nodes))
 
 #define NonLeafNodesPerPage (BLCKSZ / 2 - 1)
-- 
2.40.1

