From d880c921264962135cc506b627e9241ded42e0c6 Mon Sep 17 00:00:00 2001
From: David Christensen <david.christensen@crunchydata.com>
Date: Tue, 19 Dec 2023 17:32:38 -0500
Subject: [PATCH v3 05/28] feature: Create Calc, Limit, and Dynamic forms for
 former constants

The CalcXXX() form computes the constants in terms of an abstract usable space,
which is passed in as a parameter.

The XXXLimit constant is the same thing as
the old value of the constant, which is in fact computed at compile time and is
used in static allocations.

The XXXDynamic expression is the runtime usage of this, computed including the
reserved page size.

We continue to define the old constant with its old value here for the purposes
of confirming that the backend compiles successfully with the refactor.

Future commits will mechanically replace all uses of the old constant and remove
this definition.
---
 contrib/bloom/bloom.h               |  2 +-
 src/bin/pg_upgrade/file.c           |  2 +-
 src/include/access/htup_details.h   | 13 ++++++++++---
 src/include/access/itup.h           |  8 ++++++--
 src/include/access/nbtree.h         |  8 +++++---
 src/include/storage/fsm_internals.h |  2 +-
 6 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/contrib/bloom/bloom.h b/contrib/bloom/bloom.h
index 5a2002dd37..c3d7fe8372 100644
--- a/contrib/bloom/bloom.h
+++ b/contrib/bloom/bloom.h
@@ -112,7 +112,7 @@ typedef struct BloomOptions
  */
 typedef BlockNumber FreeBlockNumberArray[
 										 MAXALIGN_DOWN(
-													   PageUsableSpace - MAXALIGN(sizeof(BloomPageOpaqueData))
+													   PageUsableSpaceMax - MAXALIGN(sizeof(BloomPageOpaqueData))
 													   - MAXALIGN(sizeof(uint16) * 2 + sizeof(uint32) + sizeof(BloomOptions))
 													   ) / sizeof(BlockNumber)
 ];
diff --git a/src/bin/pg_upgrade/file.c b/src/bin/pg_upgrade/file.c
index 087c0c1bd4..888bb49ed3 100644
--- a/src/bin/pg_upgrade/file.c
+++ b/src/bin/pg_upgrade/file.c
@@ -187,7 +187,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\": %s",
diff --git a/src/include/access/htup_details.h b/src/include/access/htup_details.h
index 370dbbe540..450d14ebd4 100644
--- a/src/include/access/htup_details.h
+++ b/src/include/access/htup_details.h
@@ -560,7 +560,10 @@ StaticAssertDecl(MaxOffsetNumber < SpecTokenOffsetNumber,
  * ItemIds and tuples have different alignment requirements, don't assume that
  * you can, say, fit 2 tuples of size MaxHeapTupleSize/2 on the same page.
  */
-#define MaxHeapTupleSize  (PageUsableSpace - MAXALIGN(sizeof(ItemIdData)))
+#define CalcMaxHeapTupleSize(usablespace)  ((usablespace) - MAXALIGN(sizeof(ItemIdData)))
+#define MaxHeapTupleSizeLimit CalcMaxHeapTupleSize(PageUsableSpaceMax)
+#define MaxHeapTupleSizeDynamic CalcMaxHeapTupleSize(PageUsableSpace)
+#define MaxHeapTupleSize MaxHeapTupleSizeLimit
 #define MinHeapTupleSize  MAXALIGN(SizeofHeapTupleHeader)
 
 /*
@@ -574,9 +577,13 @@ StaticAssertDecl(MaxOffsetNumber < SpecTokenOffsetNumber,
  * pointers to this anyway, to avoid excessive line-pointer bloat and not
  * require increases in the size of work arrays.
  */
-#define MaxHeapTuplesPerPage	\
-	((int) ((PageUsableSpace) / \
+
+#define CalcMaxHeapTuplesPerPage(usablespace)									\
+	((int) ((usablespace) /							\
 			(MAXALIGN(SizeofHeapTupleHeader) + sizeof(ItemIdData))))
+#define MaxHeapTuplesPerPageLimit (CalcMaxHeapTuplesPerPage(PageUsableSpaceMax))
+#define MaxHeapTuplesPerPageDynamic (CalcMaxHeapTuplesPerPage(PageUsableSpace))
+#define MaxHeapTuplesPerPage MaxHeapTuplesPerPageLimit
 
 /*
  * MaxAttrSize is a somewhat arbitrary upper limit on the declared size of
diff --git a/src/include/access/itup.h b/src/include/access/itup.h
index 4b4209e43e..84579fe63f 100644
--- a/src/include/access/itup.h
+++ b/src/include/access/itup.h
@@ -163,8 +163,12 @@ index_getattr(IndexTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
  * estimated here, seemingly allowing one more tuple than estimated here.
  * But such a page always has at least MAXALIGN special space, so we're safe.
  */
-#define MaxIndexTuplesPerPage	\
-	((int) ((PageUsableSpace) / \
+#define CalcMaxIndexTuplesPerPage(usablespace)		\
+	((int) ((usablespace) /												\
 			(MAXALIGN(sizeof(IndexTupleData) + 1) + sizeof(ItemIdData))))
+#define MaxIndexTuplesPerPageLimit (CalcMaxIndexTuplesPerPage(BLCKSZ - SizeOfPageHeaderData))
+#define MaxIndexTuplesPerPageDynamic (CalcMaxIndexTuplesPerPage(PageUsableSpace))
 
+// temporary to compile
+#define MaxIndexTuplesPerPage MaxIndexTuplesPerPageLimit
 #endif							/* ITUP_H */
diff --git a/src/include/access/nbtree.h b/src/include/access/nbtree.h
index 27d42a3ff0..f1db466007 100644
--- a/src/include/access/nbtree.h
+++ b/src/include/access/nbtree.h
@@ -182,10 +182,12 @@ typedef struct BTMetaPageData
  * special area).  The value is slightly higher (i.e. more conservative)
  * than necessary as a result, which is considered acceptable.
  */
-#define MaxTIDsPerBTreePage \
-	(int) ((PageUsableSpace - sizeof(BTPageOpaqueData)) / \
+#define CalcMaxTIDsPerBTreePage(usablespace)			  \
+	(int) ((usablespace) - sizeof(BTPageOpaqueData) / \
 		   sizeof(ItemPointerData))
-
+#define MaxTIDsPerBTreePageLimit (CalcMaxTIDsPerBTreePage(PageUsableSpaceMax))
+#define MaxTIDsPerBTreePageDynamic (CalcMaxTIDsPerBTreePage(PageUsableSpace))
+#define MaxTIDsPerBTreePage MaxTIDsPerBTreePageLimit
 /*
  * The leaf-page fillfactor defaults to 90% but is user-adjustable.
  * For pages above the leaf level, we use a fixed 70% fillfactor.
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

