Constant Splitting/Refactoring
As part of the reserved page space/page features[1]https://commitfest.postgresql.org/47/3986/ work, there is a need
to convert some of the existing constants into variables, since the size of
those will no longer be fixed at compile time. At FOSDEM, there was some
interest expressed about seeing what a cleanup patch like that would look
like.
Enclosed is a series of cleanup patches for HEAD. This splits each of the
4 constants that care about page size into Cluster-specific and -Limit
variants, the first intended to become a variable in time, and the second
being the maximum value such a variable may take (largely used for static
allocations).
Since these patches define these symbols to have the same values
they previously had, there are no changes in functionality. These were
largely mechanical changes, and as such we should perhaps consider making
the same changes to back-branches to make it so context lines and the like
would be the same, simplifying maintainer's efforts when applying code in
back branches that touch similar areas.
The script I have to make these changes is simple, and could be run against
the back branches with only the comments surrounding Calc() pieces needing
to be adjusted once.
These were based on HEAD as a few minutes ago, 902900b308f, and pass all
tests.
Thanks,
David
Attachments:
v1-0002-Split-MaxHeapTupleSize-into-runtime-and-max-value.patchapplication/octet-stream; name=v1-0002-Split-MaxHeapTupleSize-into-runtime-and-max-value.patchDownload
From 1e387dbcf45d2c2adce7ec697f0e959e80ef23b7 Mon Sep 17 00:00:00 2001
From: David Christensen <david.christensen@crunchydata.com>
Date: Wed, 7 Feb 2024 10:49:31 -0500
Subject: [PATCH v1 2/4] Split MaxHeapTupleSize into runtime and max values
---
src/backend/access/heap/heapam.c | 12 +++++-----
src/backend/access/heap/hio.c | 6 ++---
src/backend/access/heap/rewriteheap.c | 4 ++--
.../replication/logical/reorderbuffer.c | 2 +-
src/backend/storage/freespace/freespace.c | 2 +-
src/include/access/heaptoast.h | 2 +-
src/include/access/htup_details.h | 22 ++++++++++++++-----
src/test/regress/expected/insert.out | 2 +-
src/test/regress/sql/insert.sql | 2 +-
9 files changed, 32 insertions(+), 22 deletions(-)
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index f740d4225e..dcbb926916 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -9231,7 +9231,7 @@ heap_xlog_insert(XLogReaderState *record)
union
{
HeapTupleHeaderData hdr;
- char data[MaxHeapTupleSize];
+ char data[MaxHeapTupleSizeLimit];
} tbuf;
HeapTupleHeader htup;
xl_heap_header xlhdr;
@@ -9287,7 +9287,7 @@ heap_xlog_insert(XLogReaderState *record)
data = XLogRecGetBlockData(record, 0, &datalen);
newlen = datalen - SizeOfHeapHeader;
- Assert(datalen > SizeOfHeapHeader && newlen <= MaxHeapTupleSize);
+ Assert(datalen > SizeOfHeapHeader && newlen <= ClusterMaxHeapTupleSize);
memcpy((char *) &xlhdr, data, SizeOfHeapHeader);
data += SizeOfHeapHeader;
@@ -9353,7 +9353,7 @@ heap_xlog_multi_insert(XLogReaderState *record)
union
{
HeapTupleHeaderData hdr;
- char data[MaxHeapTupleSize];
+ char data[MaxHeapTupleSizeLimit];
} tbuf;
HeapTupleHeader htup;
uint32 newlen;
@@ -9431,7 +9431,7 @@ heap_xlog_multi_insert(XLogReaderState *record)
tupdata = ((char *) xlhdr) + SizeOfMultiInsertTuple;
newlen = xlhdr->datalen;
- Assert(newlen <= MaxHeapTupleSize);
+ Assert(newlen <= ClusterMaxHeapTupleSize);
htup = &tbuf.hdr;
MemSet((char *) htup, 0, SizeofHeapTupleHeader);
/* PG73FORMAT: get bitmap [+ padding] [+ oid] + data */
@@ -9510,7 +9510,7 @@ heap_xlog_update(XLogReaderState *record, bool hot_update)
union
{
HeapTupleHeaderData hdr;
- char data[MaxHeapTupleSize];
+ char data[MaxHeapTupleSizeLimit];
} tbuf;
xl_heap_header xlhdr;
uint32 newlen;
@@ -9666,7 +9666,7 @@ heap_xlog_update(XLogReaderState *record, bool hot_update)
recdata += SizeOfHeapHeader;
tuplen = recdata_end - recdata;
- Assert(tuplen <= MaxHeapTupleSize);
+ Assert(tuplen <= ClusterMaxHeapTupleSize);
htup = &tbuf.hdr;
MemSet((char *) htup, 0, SizeofHeapTupleHeader);
diff --git a/src/backend/access/heap/hio.c b/src/backend/access/heap/hio.c
index 58f0a384c8..3e3963503b 100644
--- a/src/backend/access/heap/hio.c
+++ b/src/backend/access/heap/hio.c
@@ -530,11 +530,11 @@ RelationGetBufferForTuple(Relation relation, Size len,
/*
* If we're gonna fail for oversize tuple, do it right away
*/
- if (len > MaxHeapTupleSize)
+ if (len > ClusterMaxHeapTupleSize)
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("row is too big: size %zu, maximum size %zu",
- len, MaxHeapTupleSize)));
+ len, ClusterMaxHeapTupleSize)));
/* Compute desired extra freespace due to fillfactor option */
saveFreeSpace = RelationGetTargetPageFreeSpace(relation,
@@ -546,7 +546,7 @@ RelationGetBufferForTuple(Relation relation, Size len,
* somewhat arbitrary, but it should prevent most unnecessary relation
* extensions while inserting large tuples into low-fillfactor tables.
*/
- nearlyEmptyFreeSpace = MaxHeapTupleSize -
+ nearlyEmptyFreeSpace = ClusterMaxHeapTupleSize -
(ClusterMaxHeapTuplesPerPage / 8 * sizeof(ItemIdData));
if (len + saveFreeSpace > nearlyEmptyFreeSpace)
targetFreeSpace = Max(len, nearlyEmptyFreeSpace);
diff --git a/src/backend/access/heap/rewriteheap.c b/src/backend/access/heap/rewriteheap.c
index 34107323ff..75e0d1ffe8 100644
--- a/src/backend/access/heap/rewriteheap.c
+++ b/src/backend/access/heap/rewriteheap.c
@@ -653,11 +653,11 @@ raw_heap_insert(RewriteState state, HeapTuple tup)
/*
* If we're gonna fail for oversize tuple, do it right away
*/
- if (len > MaxHeapTupleSize)
+ if (len > ClusterMaxHeapTupleSize)
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("row is too big: size %zu, maximum size %zu",
- len, MaxHeapTupleSize)));
+ len, ClusterMaxHeapTupleSize)));
/* Compute desired extra freespace due to fillfactor option */
saveFreeSpace = RelationGetTargetPageFreeSpace(state->rs_new_rel,
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..07441d630b 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -4879,7 +4879,7 @@ ReorderBufferToastReplace(ReorderBuffer *rb, ReorderBufferTXN *txn,
* the tuplebuf because attrs[] will point back into the current content.
*/
tmphtup = heap_form_tuple(desc, attrs, isnull);
- Assert(newtup->t_len <= MaxHeapTupleSize);
+ Assert(newtup->t_len <= ClusterMaxHeapTupleSize);
Assert(newtup->t_data == (HeapTupleHeader) ((char *) newtup + HEAPTUPLESIZE));
memcpy(newtup->t_data, tmphtup->t_data, tmphtup->t_len);
diff --git a/src/backend/storage/freespace/freespace.c b/src/backend/storage/freespace/freespace.c
index 15e3a07341..f31edadabf 100644
--- a/src/backend/storage/freespace/freespace.c
+++ b/src/backend/storage/freespace/freespace.c
@@ -63,7 +63,7 @@
*/
#define FSM_CATEGORIES 256
#define FSM_CAT_STEP (BLCKSZ / FSM_CATEGORIES)
-#define MaxFSMRequestSize MaxHeapTupleSize
+#define MaxFSMRequestSize ClusterMaxHeapTupleSize
/*
* Depth of the on-disk tree. We need to be able to address 2^32-1 blocks,
diff --git a/src/include/access/heaptoast.h b/src/include/access/heaptoast.h
index c376dff48d..6fe836f7d1 100644
--- a/src/include/access/heaptoast.h
+++ b/src/include/access/heaptoast.h
@@ -65,7 +65,7 @@
* compress it (we can't move it out-of-line, however). Note that this
* number is per-datum, not per-tuple, for simplicity in index_form_tuple().
*/
-#define TOAST_INDEX_TARGET (MaxHeapTupleSize / 16)
+#define TOAST_INDEX_TARGET (ClusterMaxHeapTupleSize / 16)
/*
* When we store an oversize datum externally, we divide it into chunks
diff --git a/src/include/access/htup_details.h b/src/include/access/htup_details.h
index 7a35baf4b1..bb5c746b98 100644
--- a/src/include/access/htup_details.h
+++ b/src/include/access/htup_details.h
@@ -550,17 +550,27 @@ StaticAssertDecl(MaxOffsetNumber < SpecTokenOffsetNumber,
#define BITMAPLEN(NATTS) (((int)(NATTS) + 7) / 8)
/*
- * MaxHeapTupleSize is the maximum allowed size of a heap tuple, including
- * header and MAXALIGN alignment padding. Basically it's BLCKSZ minus the
- * other stuff that has to be on a disk page. Since heap pages use no
- * "special space", there's no deduction for that.
+ * ClusterMaxHeapTupleSize is a cluster-specific maximum allowed size of a
+ * heap tuple, including header and MAXALIGN alignment padding. Basically
+ * it's BLCKSZ minus the other stuff that has to be on a disk page. Since
+ * heap pages use no "special space", there's no deduction for that.
+ *
+ * MaxHeapTuplesSizeLimit is the largest value that ClusterMaxHeapTupleSize
+ * could be. While these currently evaluate to the same value, these are
+ * being split out so ClusterMaxHeapTupleSize can become a variable
+ * instead of a constant.
+ *
+ * The CalcMaxHeapTupleSize() macro is used to determine the appropriate
+ * values, given the usable page space on a given page.
*
* NOTE: we allow for the ItemId that must point to the tuple, ensuring that
* an otherwise-empty page can indeed hold a tuple of this size. Because
* 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.
+ * you can, say, fit 2 tuples of size ClusterMaxHeapTupleSize/2 on the same page.
*/
-#define MaxHeapTupleSize (BLCKSZ - MAXALIGN(SizeOfPageHeaderData + sizeof(ItemIdData)))
+#define CalcMaxHeapTupleSize(size) (size - sizeof(ItemIdData))
+#define ClusterMaxHeapTupleSize CalcMaxHeapTupleSize(BLCKSZ - SizeOfPageHeaderData)
+#define MaxHeapTupleSizeLimit CalcMaxHeapTupleSize(BLCKSZ - SizeOfPageHeaderData)
#define MinHeapTupleSize MAXALIGN(SizeofHeapTupleHeader)
/*
diff --git a/src/test/regress/expected/insert.out b/src/test/regress/expected/insert.out
index dd4354fc7d..eebf3c6d4d 100644
--- a/src/test/regress/expected/insert.out
+++ b/src/test/regress/expected/insert.out
@@ -86,7 +86,7 @@ drop table inserttest;
--
CREATE TABLE large_tuple_test (a int, b text) WITH (fillfactor = 10);
ALTER TABLE large_tuple_test ALTER COLUMN b SET STORAGE plain;
--- create page w/ free space in range [nearlyEmptyFreeSpace, MaxHeapTupleSize)
+-- create page w/ free space in range [nearlyEmptyFreeSpace, ClusterMaxHeapTupleSize)
INSERT INTO large_tuple_test (select 1, NULL);
-- should still fit on the page
INSERT INTO large_tuple_test (select 2, repeat('a', 1000));
diff --git a/src/test/regress/sql/insert.sql b/src/test/regress/sql/insert.sql
index bdcffd0314..53f46e7960 100644
--- a/src/test/regress/sql/insert.sql
+++ b/src/test/regress/sql/insert.sql
@@ -43,7 +43,7 @@ drop table inserttest;
CREATE TABLE large_tuple_test (a int, b text) WITH (fillfactor = 10);
ALTER TABLE large_tuple_test ALTER COLUMN b SET STORAGE plain;
--- create page w/ free space in range [nearlyEmptyFreeSpace, MaxHeapTupleSize)
+-- create page w/ free space in range [nearlyEmptyFreeSpace, ClusterMaxHeapTupleSize)
INSERT INTO large_tuple_test (select 1, NULL);
-- should still fit on the page
--
2.40.1
v1-0001-Split-MaxHeapTuplesPerPage-into-runtime-and-max-v.patchapplication/octet-stream; name=v1-0001-Split-MaxHeapTuplesPerPage-into-runtime-and-max-v.patchDownload
From 5c83a63b0fc3d96f7830a5123abd8a7bbd323b9d Mon Sep 17 00:00:00 2001
From: David Christensen <david.christensen@crunchydata.com>
Date: Wed, 7 Feb 2024 10:49:31 -0500
Subject: [PATCH v1 1/4] Split MaxHeapTuplesPerPage into runtime and max values
---
contrib/pg_surgery/heap_surgery.c | 4 +--
src/backend/access/brin/brin_bloom.c | 8 +++---
src/backend/access/brin/brin_minmax_multi.c | 8 +++---
src/backend/access/gin/ginpostinglist.c | 6 ++---
src/backend/access/heap/README.HOT | 2 +-
src/backend/access/heap/heapam.c | 6 ++---
src/backend/access/heap/heapam_handler.c | 8 +++---
src/backend/access/heap/hio.c | 2 +-
src/backend/access/heap/pruneheap.c | 22 ++++++++--------
src/backend/access/heap/vacuumlazy.c | 22 ++++++++--------
src/backend/nodes/tidbitmap.c | 2 +-
src/backend/storage/page/bufpage.c | 18 ++++++-------
src/include/access/ginblock.h | 2 +-
src/include/access/heapam.h | 6 ++---
src/include/access/htup_details.h | 26 ++++++++++++++-----
.../test_ginpostinglist/test_ginpostinglist.c | 6 ++---
16 files changed, 81 insertions(+), 67 deletions(-)
diff --git a/contrib/pg_surgery/heap_surgery.c b/contrib/pg_surgery/heap_surgery.c
index 37dffe3f7d..86aff2494e 100644
--- a/contrib/pg_surgery/heap_surgery.c
+++ b/contrib/pg_surgery/heap_surgery.c
@@ -89,7 +89,7 @@ heap_force_common(FunctionCallInfo fcinfo, HeapTupleForceOption heap_force_opt)
Relation rel;
OffsetNumber curr_start_ptr,
next_start_ptr;
- bool include_this_tid[MaxHeapTuplesPerPage];
+ bool include_this_tid[MaxHeapTuplesPerPageLimit];
if (RecoveryInProgress())
ereport(ERROR,
@@ -225,7 +225,7 @@ heap_force_common(FunctionCallInfo fcinfo, HeapTupleForceOption heap_force_opt)
}
/* Mark it for processing. */
- Assert(offno < MaxHeapTuplesPerPage);
+ Assert(offno < ClusterMaxHeapTuplesPerPage);
include_this_tid[offno] = true;
}
diff --git a/src/backend/access/brin/brin_bloom.c b/src/backend/access/brin/brin_bloom.c
index 13c1e681f3..652a67f356 100644
--- a/src/backend/access/brin/brin_bloom.c
+++ b/src/backend/access/brin/brin_bloom.c
@@ -166,7 +166,7 @@ typedef struct BloomOptions
* on the fact that the filter header is ~20B alone, which is about
* the same as the filter bitmap for 16 distinct items with 1% false
* positive rate. So by allowing lower values we'd not gain much. In
- * any case, the min should not be larger than MaxHeapTuplesPerPage
+ * any case, the min should not be larger than ClusterMaxHeapTuplesPerPage
* (~290), which is the theoretical maximum for single-page ranges.
*/
#define BLOOM_MIN_NDISTINCT_PER_RANGE 16
@@ -478,7 +478,7 @@ brin_bloom_opcinfo(PG_FUNCTION_ARGS)
*
* Adjust the ndistinct value based on the pagesPerRange value. First,
* if it's negative, it's assumed to be relative to maximum number of
- * tuples in the range (assuming each page gets MaxHeapTuplesPerPage
+ * tuples in the range (assuming each page gets ClusterMaxHeapTuplesPerPage
* tuples, which is likely a significant over-estimate). We also clamp
* the value, not to over-size the bloom filter unnecessarily.
*
@@ -493,7 +493,7 @@ brin_bloom_opcinfo(PG_FUNCTION_ARGS)
* seems better to rely on the upper estimate.
*
* XXX We might also calculate a better estimate of rows per BRIN range,
- * instead of using MaxHeapTuplesPerPage (which probably produces values
+ * instead of using ClusterMaxHeapTuplesPerPage (which probably produces values
* much higher than reality).
*/
static int
@@ -508,7 +508,7 @@ brin_bloom_get_ndistinct(BrinDesc *bdesc, BloomOptions *opts)
Assert(BlockNumberIsValid(pagesPerRange));
- maxtuples = MaxHeapTuplesPerPage * pagesPerRange;
+ maxtuples = ClusterMaxHeapTuplesPerPage * pagesPerRange;
/*
* Similarly to n_distinct, negative values are relative - in this case to
diff --git a/src/backend/access/brin/brin_minmax_multi.c b/src/backend/access/brin/brin_minmax_multi.c
index 3ffaad3e42..901676122a 100644
--- a/src/backend/access/brin/brin_minmax_multi.c
+++ b/src/backend/access/brin/brin_minmax_multi.c
@@ -2007,10 +2007,10 @@ brin_minmax_multi_distance_tid(PG_FUNCTION_ARGS)
* We use the no-check variants here, because user-supplied values may
* have (ip_posid == 0). See ItemPointerCompare.
*/
- da1 = ItemPointerGetBlockNumberNoCheck(pa1) * MaxHeapTuplesPerPage +
+ da1 = ItemPointerGetBlockNumberNoCheck(pa1) * ClusterMaxHeapTuplesPerPage +
ItemPointerGetOffsetNumberNoCheck(pa1);
- da2 = ItemPointerGetBlockNumberNoCheck(pa2) * MaxHeapTuplesPerPage +
+ da2 = ItemPointerGetBlockNumberNoCheck(pa2) * ClusterMaxHeapTuplesPerPage +
ItemPointerGetOffsetNumberNoCheck(pa2);
PG_RETURN_FLOAT8(da2 - da1);
@@ -2461,7 +2461,7 @@ brin_minmax_multi_add_value(PG_FUNCTION_ARGS)
* much lower, but meh.
*/
maxvalues = Min(target_maxvalues * MINMAX_BUFFER_FACTOR,
- MaxHeapTuplesPerPage * pagesPerRange);
+ ClusterMaxHeapTuplesPerPage * pagesPerRange);
/* but always at least the original value */
maxvalues = Max(maxvalues, target_maxvalues);
@@ -2507,7 +2507,7 @@ brin_minmax_multi_add_value(PG_FUNCTION_ARGS)
* much lower, but meh.
*/
maxvalues = Min(serialized->maxvalues * MINMAX_BUFFER_FACTOR,
- MaxHeapTuplesPerPage * pagesPerRange);
+ ClusterMaxHeapTuplesPerPage * pagesPerRange);
/* but always at least the original value */
maxvalues = Max(maxvalues, serialized->maxvalues);
diff --git a/src/backend/access/gin/ginpostinglist.c b/src/backend/access/gin/ginpostinglist.c
index 708f9f49ec..8aa0f17bf8 100644
--- a/src/backend/access/gin/ginpostinglist.c
+++ b/src/backend/access/gin/ginpostinglist.c
@@ -26,7 +26,7 @@
* lowest 32 bits are the block number. That leaves 21 bits unused, i.e.
* only 43 low bits are used.
*
- * 11 bits is enough for the offset number, because MaxHeapTuplesPerPage <
+ * 11 bits is enough for the offset number, because ClusterMaxHeapTuplesPerPage <
* 2^11 on all supported block sizes. We are frugal with the bits, because
* smaller integers use fewer bytes in the varbyte encoding, saving disk
* space. (If we get a new table AM in the future that wants to use the full
@@ -74,9 +74,9 @@
/*
* How many bits do you need to encode offset number? OffsetNumber is a 16-bit
* integer, but you can't fit that many items on a page. 11 ought to be more
- * than enough. It's tempting to derive this from MaxHeapTuplesPerPage, and
+ * than enough. It's tempting to derive this from ClusterMaxHeapTuplesPerPage, and
* use the minimum number of bits, but that would require changing the on-disk
- * format if MaxHeapTuplesPerPage changes. Better to leave some slack.
+ * format if ClusterMaxHeapTuplesPerPage changes. Better to leave some slack.
*/
#define MaxHeapTuplesPerPageBits 11
diff --git a/src/backend/access/heap/README.HOT b/src/backend/access/heap/README.HOT
index 74e407f375..e286e1dec3 100644
--- a/src/backend/access/heap/README.HOT
+++ b/src/backend/access/heap/README.HOT
@@ -264,7 +264,7 @@ of line pointer bloat: we might end up with huge numbers of line pointers
and just a few actual tuples on a page. To limit the damage in the worst
case, and to keep various work arrays as well as the bitmaps in bitmap
scans reasonably sized, the maximum number of line pointers per page
-is arbitrarily capped at MaxHeapTuplesPerPage (the most tuples that
+is arbitrarily capped at ClusterMaxHeapTuplesPerPage (the most tuples that
could fit without HOT pruning).
Effectively, space reclamation happens during tuple retrieval when the
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 707460a536..f740d4225e 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -478,7 +478,7 @@ heapgetpage(TableScanDesc sscan, BlockNumber block)
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
- Assert(ntup <= MaxHeapTuplesPerPage);
+ Assert(ntup <= ClusterMaxHeapTuplesPerPage);
scan->rs_ntuples = ntup;
}
@@ -6750,8 +6750,8 @@ heap_freeze_execute_prepared(Relation rel, Buffer buffer,
/* Now WAL-log freezing if necessary */
if (RelationNeedsWAL(rel))
{
- xl_heap_freeze_plan plans[MaxHeapTuplesPerPage];
- OffsetNumber offsets[MaxHeapTuplesPerPage];
+ xl_heap_freeze_plan plans[MaxHeapTuplesPerPageLimit];
+ OffsetNumber offsets[MaxHeapTuplesPerPageLimit];
int nplans;
xl_heap_freeze_page xlrec;
XLogRecPtr recptr;
diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c
index d15a02b2be..58fc0dd618 100644
--- a/src/backend/access/heap/heapam_handler.c
+++ b/src/backend/access/heap/heapam_handler.c
@@ -1191,7 +1191,7 @@ heapam_index_build_range_scan(Relation heapRelation,
TransactionId OldestXmin;
BlockNumber previous_blkno = InvalidBlockNumber;
BlockNumber root_blkno = InvalidBlockNumber;
- OffsetNumber root_offsets[MaxHeapTuplesPerPage];
+ OffsetNumber root_offsets[MaxHeapTuplesPerPageLimit];
/*
* sanity checks
@@ -1754,8 +1754,8 @@ heapam_index_validate_scan(Relation heapRelation,
EState *estate;
ExprContext *econtext;
BlockNumber root_blkno = InvalidBlockNumber;
- OffsetNumber root_offsets[MaxHeapTuplesPerPage];
- bool in_index[MaxHeapTuplesPerPage];
+ OffsetNumber root_offsets[MaxHeapTuplesPerPageLimit];
+ bool in_index[MaxHeapTuplesPerPageLimit];
BlockNumber previous_blkno = InvalidBlockNumber;
/* state variables for the merge */
@@ -2220,7 +2220,7 @@ heapam_scan_bitmap_next_block(TableScanDesc scan,
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
- Assert(ntup <= MaxHeapTuplesPerPage);
+ Assert(ntup <= ClusterMaxHeapTuplesPerPage);
hscan->rs_ntuples = ntup;
return ntup > 0;
diff --git a/src/backend/access/heap/hio.c b/src/backend/access/heap/hio.c
index c7248d7c68..58f0a384c8 100644
--- a/src/backend/access/heap/hio.c
+++ b/src/backend/access/heap/hio.c
@@ -547,7 +547,7 @@ RelationGetBufferForTuple(Relation relation, Size len,
* extensions while inserting large tuples into low-fillfactor tables.
*/
nearlyEmptyFreeSpace = MaxHeapTupleSize -
- (MaxHeapTuplesPerPage / 8 * sizeof(ItemIdData));
+ (ClusterMaxHeapTuplesPerPage / 8 * sizeof(ItemIdData));
if (len + saveFreeSpace > nearlyEmptyFreeSpace)
targetFreeSpace = Max(len, nearlyEmptyFreeSpace);
else
diff --git a/src/backend/access/heap/pruneheap.c b/src/backend/access/heap/pruneheap.c
index 5917633567..4073b0db35 100644
--- a/src/backend/access/heap/pruneheap.c
+++ b/src/backend/access/heap/pruneheap.c
@@ -44,17 +44,17 @@ typedef struct
int ndead;
int nunused;
/* arrays that accumulate indexes of items to be changed */
- OffsetNumber redirected[MaxHeapTuplesPerPage * 2];
- OffsetNumber nowdead[MaxHeapTuplesPerPage];
- OffsetNumber nowunused[MaxHeapTuplesPerPage];
+ OffsetNumber redirected[MaxHeapTuplesPerPageLimit * 2];
+ OffsetNumber nowdead[MaxHeapTuplesPerPageLimit];
+ OffsetNumber nowunused[MaxHeapTuplesPerPageLimit];
/*
* marked[i] is true if item i is entered in one of the above arrays.
*
- * This needs to be MaxHeapTuplesPerPage + 1 long as FirstOffsetNumber is
+ * This needs to be ClusterMaxHeapTuplesPerPage + 1 long as FirstOffsetNumber is
* 1. Otherwise every access would need to subtract 1.
*/
- bool marked[MaxHeapTuplesPerPage + 1];
+ bool marked[MaxHeapTuplesPerPageLimit + 1];
} PruneState;
/* Local functions */
@@ -496,7 +496,7 @@ heap_prune_chain(Buffer buffer, OffsetNumber rootoffnum,
OffsetNumber latestdead = InvalidOffsetNumber,
maxoff = PageGetMaxOffsetNumber(dp),
offnum;
- OffsetNumber chainitems[MaxHeapTuplesPerPage];
+ OffsetNumber chainitems[MaxHeapTuplesPerPageLimit];
int nchain = 0,
i;
@@ -777,7 +777,7 @@ static void
heap_prune_record_redirect(PruneState *prstate,
OffsetNumber offnum, OffsetNumber rdoffnum)
{
- Assert(prstate->nredirected < MaxHeapTuplesPerPage);
+ Assert(prstate->nredirected < ClusterMaxHeapTuplesPerPage);
prstate->redirected[prstate->nredirected * 2] = offnum;
prstate->redirected[prstate->nredirected * 2 + 1] = rdoffnum;
prstate->nredirected++;
@@ -791,7 +791,7 @@ heap_prune_record_redirect(PruneState *prstate,
static void
heap_prune_record_dead(PruneState *prstate, OffsetNumber offnum)
{
- Assert(prstate->ndead < MaxHeapTuplesPerPage);
+ Assert(prstate->ndead < ClusterMaxHeapTuplesPerPage);
prstate->nowdead[prstate->ndead] = offnum;
prstate->ndead++;
Assert(!prstate->marked[offnum]);
@@ -823,7 +823,7 @@ heap_prune_record_dead_or_unused(PruneState *prstate, OffsetNumber offnum)
static void
heap_prune_record_unused(PruneState *prstate, OffsetNumber offnum)
{
- Assert(prstate->nunused < MaxHeapTuplesPerPage);
+ Assert(prstate->nunused < ClusterMaxHeapTuplesPerPage);
prstate->nowunused[prstate->nunused] = offnum;
prstate->nunused++;
Assert(!prstate->marked[offnum]);
@@ -1036,7 +1036,7 @@ page_verify_redirects(Page page)
* If item k is part of a HOT-chain with root at item j, then we set
* root_offsets[k - 1] = j.
*
- * The passed-in root_offsets array must have MaxHeapTuplesPerPage entries.
+ * The passed-in root_offsets array must have ClusterMaxHeapTuplesPerPage entries.
* Unused entries are filled with InvalidOffsetNumber (zero).
*
* The function must be called with at least share lock on the buffer, to
@@ -1053,7 +1053,7 @@ heap_get_root_tuples(Page page, OffsetNumber *root_offsets)
maxoff;
MemSet(root_offsets, InvalidOffsetNumber,
- MaxHeapTuplesPerPage * sizeof(OffsetNumber));
+ ClusterMaxHeapTuplesPerPage * sizeof(OffsetNumber));
maxoff = PageGetMaxOffsetNumber(page);
for (offnum = FirstOffsetNumber; offnum <= maxoff; offnum = OffsetNumberNext(offnum))
diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index fa56480808..cd234f2910 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -893,8 +893,8 @@ lazy_scan_heap(LVRelState *vacrel)
* dead_items TIDs, pause and do a cycle of vacuuming before we tackle
* this page.
*/
- Assert(dead_items->max_items >= MaxHeapTuplesPerPage);
- if (dead_items->max_items - dead_items->num_items < MaxHeapTuplesPerPage)
+ Assert(dead_items->max_items >= ClusterMaxHeapTuplesPerPage);
+ if (dead_items->max_items - dead_items->num_items < ClusterMaxHeapTuplesPerPage)
{
/*
* Before beginning index vacuuming, we release any pin we may
@@ -1376,8 +1376,8 @@ lazy_scan_prune(LVRelState *vacrel,
all_frozen;
TransactionId visibility_cutoff_xid;
int64 fpi_before = pgWalUsage.wal_fpi;
- OffsetNumber deadoffsets[MaxHeapTuplesPerPage];
- HeapTupleFreeze frozen[MaxHeapTuplesPerPage];
+ OffsetNumber deadoffsets[MaxHeapTuplesPerPageLimit];
+ HeapTupleFreeze frozen[MaxHeapTuplesPerPageLimit];
Assert(BufferGetBlockNumber(buf) == blkno);
@@ -1905,7 +1905,7 @@ lazy_scan_noprune(LVRelState *vacrel,
HeapTupleHeader tupleheader;
TransactionId NoFreezePageRelfrozenXid = vacrel->NewRelfrozenXid;
MultiXactId NoFreezePageRelminMxid = vacrel->NewRelminMxid;
- OffsetNumber deadoffsets[MaxHeapTuplesPerPage];
+ OffsetNumber deadoffsets[MaxHeapTuplesPerPageLimit];
Assert(BufferGetBlockNumber(buf) == blkno);
@@ -2449,7 +2449,7 @@ lazy_vacuum_heap_page(LVRelState *vacrel, BlockNumber blkno, Buffer buffer,
{
VacDeadItems *dead_items = vacrel->dead_items;
Page page = BufferGetPage(buffer);
- OffsetNumber unused[MaxHeapTuplesPerPage];
+ OffsetNumber unused[MaxHeapTuplesPerPageLimit];
int nunused = 0;
TransactionId visibility_cutoff_xid;
bool all_frozen;
@@ -3100,16 +3100,16 @@ dead_items_max_items(LVRelState *vacrel)
max_items = Min(max_items, MAXDEADITEMS(MaxAllocSize));
/* curious coding here to ensure the multiplication can't overflow */
- if ((BlockNumber) (max_items / MaxHeapTuplesPerPage) > rel_pages)
- max_items = rel_pages * MaxHeapTuplesPerPage;
+ if ((BlockNumber) (max_items / ClusterMaxHeapTuplesPerPage) > rel_pages)
+ max_items = rel_pages * ClusterMaxHeapTuplesPerPage;
/* stay sane if small maintenance_work_mem */
- max_items = Max(max_items, MaxHeapTuplesPerPage);
+ max_items = Max(max_items, ClusterMaxHeapTuplesPerPage);
}
else
{
/* One-pass case only stores a single heap page's TIDs at a time */
- max_items = MaxHeapTuplesPerPage;
+ max_items = ClusterMaxHeapTuplesPerPage;
}
return (int) max_items;
@@ -3129,7 +3129,7 @@ dead_items_alloc(LVRelState *vacrel, int nworkers)
int max_items;
max_items = dead_items_max_items(vacrel);
- Assert(max_items >= MaxHeapTuplesPerPage);
+ Assert(max_items >= ClusterMaxHeapTuplesPerPage);
/*
* Initialize state for a parallel vacuum. As of now, only one worker can
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..d02ddedfa5 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -53,7 +53,7 @@
* the per-page bitmaps variable size. We just legislate that the size
* is this:
*/
-#define MAX_TUPLES_PER_PAGE MaxHeapTuplesPerPage
+#define MAX_TUPLES_PER_PAGE ClusterMaxHeapTuplesPerPage
/*
* 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 be6f1f62d2..f78efce9ad 100644
--- a/src/backend/storage/page/bufpage.c
+++ b/src/backend/storage/page/bufpage.c
@@ -186,7 +186,7 @@ PageIsVerifiedExtended(Page page, BlockNumber blkno, int flags)
* one that is both unused and deallocated.
*
* If flag PAI_IS_HEAP is set, we enforce that there can't be more than
- * MaxHeapTuplesPerPage line pointers on the page.
+ * ClusterMaxHeapTuplesPerPage line pointers on the page.
*
* !!! EREPORT(ERROR) IS DISALLOWED HERE !!!
*/
@@ -295,9 +295,9 @@ PageAddItemExtended(Page page,
}
/* Reject placing items beyond heap boundary, if heap */
- if ((flags & PAI_IS_HEAP) != 0 && offsetNumber > MaxHeapTuplesPerPage)
+ if ((flags & PAI_IS_HEAP) != 0 && offsetNumber > ClusterMaxHeapTuplesPerPage)
{
- elog(WARNING, "can't put more than MaxHeapTuplesPerPage items in a heap page");
+ elog(WARNING, "can't put more than ClusterMaxHeapTuplesPerPage items in a heap page");
return InvalidOffsetNumber;
}
@@ -702,7 +702,7 @@ PageRepairFragmentation(Page page)
Offset pd_upper = ((PageHeader) page)->pd_upper;
Offset pd_special = ((PageHeader) page)->pd_special;
Offset last_offset;
- itemIdCompactData itemidbase[MaxHeapTuplesPerPage];
+ itemIdCompactData itemidbase[MaxHeapTuplesPerPageLimit];
itemIdCompact itemidptr;
ItemId lp;
int nline,
@@ -979,12 +979,12 @@ PageGetExactFreeSpace(Page page)
* reduced by the space needed for a new line pointer.
*
* The difference between this and PageGetFreeSpace is that this will return
- * zero if there are already MaxHeapTuplesPerPage line pointers in the page
+ * zero if there are already ClusterMaxHeapTuplesPerPage line pointers in the page
* and none are free. We use this to enforce that no more than
- * MaxHeapTuplesPerPage line pointers are created on a heap page. (Although
+ * ClusterMaxHeapTuplesPerPage line pointers are created on a heap page. (Although
* no more tuples than that could fit anyway, in the presence of redirected
* or dead line pointers it'd be possible to have too many line pointers.
- * To avoid breaking code that assumes MaxHeapTuplesPerPage is a hard limit
+ * To avoid breaking code that assumes ClusterMaxHeapTuplesPerPage is a hard limit
* on the number of line pointers, we make this extra check.)
*/
Size
@@ -999,10 +999,10 @@ PageGetHeapFreeSpace(Page page)
nline;
/*
- * Are there already MaxHeapTuplesPerPage line pointers in the page?
+ * Are there already ClusterMaxHeapTuplesPerPage line pointers in the page?
*/
nline = PageGetMaxOffsetNumber(page);
- if (nline >= MaxHeapTuplesPerPage)
+ if (nline >= ClusterMaxHeapTuplesPerPage)
{
if (PageHasFreeLinePointers(page))
{
diff --git a/src/include/access/ginblock.h b/src/include/access/ginblock.h
index b3b7daa049..c55d11be64 100644
--- a/src/include/access/ginblock.h
+++ b/src/include/access/ginblock.h
@@ -162,7 +162,7 @@ extern bool GinPageIsRecyclable(Page page);
* pointers for that page
* Note that these are all distinguishable from an "invalid" item pointer
* (which is InvalidBlockNumber/0) as well as from all normal item
- * pointers (which have item numbers in the range 1..MaxHeapTuplesPerPage).
+ * pointers (which have item numbers in the range 1..ClusterMaxHeapTuplesPerPage).
*/
#define ItemPointerSetMin(p) \
ItemPointerSet((p), (BlockNumber)0, (OffsetNumber)0)
diff --git a/src/include/access/heapam.h b/src/include/access/heapam.h
index 4b133f6859..3217d72f99 100644
--- a/src/include/access/heapam.h
+++ b/src/include/access/heapam.h
@@ -75,7 +75,7 @@ typedef struct HeapScanDescData
/* these fields only used in page-at-a-time mode and for bitmap scans */
int rs_cindex; /* current tuple's index in vistuples */
int rs_ntuples; /* number of visible tuples on page */
- OffsetNumber rs_vistuples[MaxHeapTuplesPerPage]; /* their offsets */
+ OffsetNumber rs_vistuples[MaxHeapTuplesPerPageLimit]; /* their offsets */
} HeapScanDescData;
typedef struct HeapScanDescData *HeapScanDesc;
@@ -205,10 +205,10 @@ typedef struct PruneResult
* This is of type int8[], instead of HTSV_Result[], so we can use -1 to
* indicate no visibility has been computed, e.g. for LP_DEAD items.
*
- * This needs to be MaxHeapTuplesPerPage + 1 long as FirstOffsetNumber is
+ * This needs to be ClusterMaxHeapTuplesPerPage + 1 long as FirstOffsetNumber is
* 1. Otherwise every access would need to subtract 1.
*/
- int8 htsv[MaxHeapTuplesPerPage + 1];
+ int8 htsv[MaxHeapTuplesPerPageLimit + 1];
} PruneResult;
/*
diff --git a/src/include/access/htup_details.h b/src/include/access/htup_details.h
index 5e38ef8696..7a35baf4b1 100644
--- a/src/include/access/htup_details.h
+++ b/src/include/access/htup_details.h
@@ -564,19 +564,33 @@ StaticAssertDecl(MaxOffsetNumber < SpecTokenOffsetNumber,
#define MinHeapTupleSize MAXALIGN(SizeofHeapTupleHeader)
/*
- * MaxHeapTuplesPerPage is an upper bound on the number of tuples that can
- * fit on one heap page. (Note that indexes could have more, because they
- * use a smaller tuple header.) We arrive at the divisor because each tuple
- * must be maxaligned, and it must have an associated line pointer.
+ * ClusterMaxHeapTuplesPerPage is a cluster-specific upper bound on the number
+ * of tuples that can fit on one heap page. (Note that indexes could have
+ * more, because they use a smaller tuple header.) We arrive at the divisor
+ * because each tuple must be maxaligned, and it must have an associated line
+ * pointer.
+ *
+ * MaxHeapTuplesPerPageLimit is the largest value that
+ * ClusterMaxHeapTuplesPerPage could be. While these currently evaluate to
+ * the same value, these are being split out so ClusterMaxHeapTuplesPerPage
+ * can become a variable instead of a constant.
+ *
+ * The CalcMaxHeapTuplesPerPage() macro is used to determine the appropriate
+ * values, given the usable page space on a given page.
+ *
+ * The old MaxHeapTuplesPerPage symbol has been removed; static allocations
+ * should use the MaxHeapTuplesPerPageLimit constant, while runtime code
+ * should use ClusterMaxHeapTuplesPerPage.
*
* Note: with HOT, there could theoretically be more line pointers (not actual
* tuples) than this on a heap page. However we constrain the number of line
* pointers to this anyway, to avoid excessive line-pointer bloat and not
* require increases in the size of work arrays.
*/
-#define MaxHeapTuplesPerPage \
- ((int) ((BLCKSZ - SizeOfPageHeaderData) / \
+#define CalcMaxHeapTuplesPerPage(size) ((int) ((size) / \
(MAXALIGN(SizeofHeapTupleHeader) + sizeof(ItemIdData))))
+#define ClusterMaxHeapTuplesPerPage CalcMaxHeapTuplesPerPage(BLCKSZ - SizeOfPageHeaderData)
+#define MaxHeapTuplesPerPageLimit CalcMaxHeapTuplesPerPage(BLCKSZ - SizeOfPageHeaderData)
/*
* MaxAttrSize is a somewhat arbitrary upper limit on the declared size of
diff --git a/src/test/modules/test_ginpostinglist/test_ginpostinglist.c b/src/test/modules/test_ginpostinglist/test_ginpostinglist.c
index 04215cadd9..f79dbc2bdd 100644
--- a/src/test/modules/test_ginpostinglist/test_ginpostinglist.c
+++ b/src/test/modules/test_ginpostinglist/test_ginpostinglist.c
@@ -88,9 +88,9 @@ Datum
test_ginpostinglist(PG_FUNCTION_ARGS)
{
test_itemptr_pair(0, 2, 14);
- test_itemptr_pair(0, MaxHeapTuplesPerPage, 14);
- test_itemptr_pair(MaxBlockNumber, MaxHeapTuplesPerPage, 14);
- test_itemptr_pair(MaxBlockNumber, MaxHeapTuplesPerPage, 16);
+ test_itemptr_pair(0, ClusterMaxHeapTuplesPerPage, 14);
+ test_itemptr_pair(MaxBlockNumber, ClusterMaxHeapTuplesPerPage, 14);
+ test_itemptr_pair(MaxBlockNumber, ClusterMaxHeapTuplesPerPage, 16);
PG_RETURN_VOID();
}
--
2.40.1
v1-0004-Split-MaxTIDsPerBTreePage-into-runtime-and-max-va.patchapplication/octet-stream; name=v1-0004-Split-MaxTIDsPerBTreePage-into-runtime-and-max-va.patchDownload
From 0027dcec705d9267d7639d482587287c5480e582 Mon Sep 17 00:00:00 2001
From: David Christensen <david.christensen@crunchydata.com>
Date: Wed, 7 Feb 2024 10:49:31 -0500
Subject: [PATCH v1 4/4] Split MaxTIDsPerBTreePage into runtime and max values
---
contrib/amcheck/verify_nbtree.c | 4 ++--
src/backend/access/nbtree/nbtdedup.c | 4 ++--
src/backend/access/nbtree/nbtinsert.c | 4 ++--
src/backend/access/nbtree/nbtree.c | 4 ++--
src/backend/access/nbtree/nbtsearch.c | 8 ++++----
src/include/access/nbtree.h | 24 ++++++++++++++++++------
6 files changed, 30 insertions(+), 18 deletions(-)
diff --git a/contrib/amcheck/verify_nbtree.c b/contrib/amcheck/verify_nbtree.c
index 7a20a967ad..60324780df 100644
--- a/contrib/amcheck/verify_nbtree.c
+++ b/contrib/amcheck/verify_nbtree.c
@@ -533,12 +533,12 @@ bt_check_every_level(Relation rel, Relation heaprel, bool heapkeyspace,
/*
* Size Bloom filter based on estimated number of tuples in index,
* while conservatively assuming that each block must contain at least
- * MaxTIDsPerBTreePage / 3 "plain" tuples -- see
+ * ClusterMaxTIDsPerBTreePage / 3 "plain" tuples -- see
* bt_posting_plain_tuple() for definition, and details of how posting
* list tuples are handled.
*/
total_pages = RelationGetNumberOfBlocks(rel);
- total_elems = Max(total_pages * (MaxTIDsPerBTreePage / 3),
+ total_elems = Max(total_pages * (ClusterMaxTIDsPerBTreePage / 3),
(int64) state->rel->rd_rel->reltuples);
/* Generate a random seed to avoid repetition */
seed = pg_prng_uint64(&pg_global_prng_state);
diff --git a/src/backend/access/nbtree/nbtdedup.c b/src/backend/access/nbtree/nbtdedup.c
index 456d86b51c..c8af6cbc2a 100644
--- a/src/backend/access/nbtree/nbtdedup.c
+++ b/src/backend/access/nbtree/nbtdedup.c
@@ -355,8 +355,8 @@ _bt_bottomupdel_pass(Relation rel, Buffer buf, Relation heapRel,
delstate.bottomup = true;
delstate.bottomupfreespace = Max(BLCKSZ / 16, newitemsz);
delstate.ndeltids = 0;
- delstate.deltids = palloc(MaxTIDsPerBTreePage * sizeof(TM_IndexDelete));
- delstate.status = palloc(MaxTIDsPerBTreePage * sizeof(TM_IndexStatus));
+ delstate.deltids = palloc(ClusterMaxTIDsPerBTreePage * sizeof(TM_IndexDelete));
+ delstate.status = palloc(ClusterMaxTIDsPerBTreePage * sizeof(TM_IndexStatus));
minoff = P_FIRSTDATAKEY(opaque);
maxoff = PageGetMaxOffsetNumber(page);
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 4674e5267a..ccc2454801 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -2829,8 +2829,8 @@ _bt_simpledel_pass(Relation rel, Buffer buffer, Relation heapRel,
delstate.bottomup = false;
delstate.bottomupfreespace = 0;
delstate.ndeltids = 0;
- delstate.deltids = palloc(MaxTIDsPerBTreePage * sizeof(TM_IndexDelete));
- delstate.status = palloc(MaxTIDsPerBTreePage * sizeof(TM_IndexStatus));
+ delstate.deltids = palloc(ClusterMaxTIDsPerBTreePage * sizeof(TM_IndexDelete));
+ delstate.status = palloc(ClusterMaxTIDsPerBTreePage * sizeof(TM_IndexStatus));
for (offnum = minoff;
offnum <= maxoff;
diff --git a/src/backend/access/nbtree/nbtree.c b/src/backend/access/nbtree/nbtree.c
index 2a4f990583..520e52ee12 100644
--- a/src/backend/access/nbtree/nbtree.c
+++ b/src/backend/access/nbtree/nbtree.c
@@ -263,8 +263,8 @@ btgettuple(IndexScanDesc scan, ScanDirection dir)
*/
if (so->killedItems == NULL)
so->killedItems = (int *)
- palloc(MaxTIDsPerBTreePage * sizeof(int));
- if (so->numKilled < MaxTIDsPerBTreePage)
+ palloc(ClusterMaxTIDsPerBTreePage * sizeof(int));
+ if (so->numKilled < ClusterMaxTIDsPerBTreePage)
so->killedItems[so->numKilled++] = so->currPos.itemIndex;
}
diff --git a/src/backend/access/nbtree/nbtsearch.c b/src/backend/access/nbtree/nbtsearch.c
index 63ee9ba225..4925ace477 100644
--- a/src/backend/access/nbtree/nbtsearch.c
+++ b/src/backend/access/nbtree/nbtsearch.c
@@ -1726,7 +1726,7 @@ _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum,
if (!continuescan)
so->currPos.moreRight = false;
- Assert(itemIndex <= MaxTIDsPerBTreePage);
+ Assert(itemIndex <= ClusterMaxTIDsPerBTreePage);
so->currPos.firstItem = 0;
so->currPos.lastItem = itemIndex - 1;
so->currPos.itemIndex = 0;
@@ -1734,7 +1734,7 @@ _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum,
else
{
/* load items[] in descending order */
- itemIndex = MaxTIDsPerBTreePage;
+ itemIndex = ClusterMaxTIDsPerBTreePage;
offnum = Min(offnum, maxoff);
@@ -1836,8 +1836,8 @@ _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum,
Assert(itemIndex >= 0);
so->currPos.firstItem = itemIndex;
- so->currPos.lastItem = MaxTIDsPerBTreePage - 1;
- so->currPos.itemIndex = MaxTIDsPerBTreePage - 1;
+ so->currPos.lastItem = ClusterMaxTIDsPerBTreePage - 1;
+ so->currPos.itemIndex = ClusterMaxTIDsPerBTreePage - 1;
}
return (so->currPos.firstItem <= so->currPos.lastItem);
diff --git a/src/include/access/nbtree.h b/src/include/access/nbtree.h
index 676fa5e0a9..568646a146 100644
--- a/src/include/access/nbtree.h
+++ b/src/include/access/nbtree.h
@@ -172,9 +172,17 @@ typedef struct BTMetaPageData
MAXALIGN(sizeof(BTPageOpaqueData))) / 3)
/*
- * MaxTIDsPerBTreePage is an upper bound on the number of heap TIDs tuples
- * that may be stored on a btree leaf page. It is used to size the
- * per-page temporary buffers.
+ * ClusterMaxTIDsPerBTreePage is a cluster-specific upper bound on the number
+ * of heap TIDs tuples that may be stored on a btree leaf page. It is used to
+ * size the per-page temporary buffers.
+ *
+ * MaxTIDsPerBTreePageLimit is the largest value that
+ * ClusterMaxTIDsPerBTreePage could be. While these currently evaluate to the
+ * same value, these are being split out so ClusterMaxTIDsPerBTreePage can
+ * become a variable instead of a constant.
+ *
+ * The CalcMaxTIDsPerBTreePage() macro is used to determine the appropriate
+ * values, given the usable page space on a given page.
*
* Note: we don't bother considering per-tuple overheads here to keep
* things simple (value is based on how many elements a single array of
@@ -182,9 +190,13 @@ 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) ((BLCKSZ - SizeOfPageHeaderData - sizeof(BTPageOpaqueData)) / \
+#define CalcMaxTIDsPerBTreePage(size) \
+ (int) (((size) - sizeof(BTPageOpaqueData)) / \
sizeof(ItemPointerData))
+#define ClusterMaxTIDsPerBTreePage \
+ CalcMaxTIDsPerBTreePage(BLCKSZ - SizeOfPageHeaderData)
+#define MaxTIDsPerBTreePageLimit \
+ CalcMaxTIDsPerBTreePage(BLCKSZ - SizeOfPageHeaderData)
/*
* The leaf-page fillfactor defaults to 90% but is user-adjustable.
@@ -982,7 +994,7 @@ typedef struct BTScanPosData
int lastItem; /* last valid index in items[] */
int itemIndex; /* current index in items[] */
- BTScanPosItem items[MaxTIDsPerBTreePage]; /* MUST BE LAST */
+ BTScanPosItem items[MaxTIDsPerBTreePageLimit]; /* MUST BE LAST */
} BTScanPosData;
typedef BTScanPosData *BTScanPos;
--
2.40.1
v1-0003-Split-MaxIndexTuplesPerPage-into-runtime-and-max-.patchapplication/octet-stream; name=v1-0003-Split-MaxIndexTuplesPerPage-into-runtime-and-max-.patchDownload
From 97c5d15d29c8d32e78bc328858b23e064fb29a10 Mon Sep 17 00:00:00 2001
From: David Christensen <david.christensen@crunchydata.com>
Date: Wed, 7 Feb 2024 10:49:31 -0500
Subject: [PATCH v1 3/4] Split MaxIndexTuplesPerPage into runtime and max
values
---
contrib/amcheck/verify_nbtree.c | 6 +++---
src/backend/access/gist/gist.c | 2 +-
src/backend/access/gist/gistget.c | 8 ++++----
src/backend/access/hash/hash.c | 4 ++--
src/backend/access/hash/hashovfl.c | 6 +++---
src/backend/access/hash/hashpage.c | 4 ++--
src/backend/access/hash/hashsearch.c | 10 +++++-----
src/backend/access/nbtree/nbtinsert.c | 2 +-
src/backend/access/nbtree/nbtpage.c | 8 ++++----
src/backend/access/nbtree/nbtree.c | 4 ++--
src/backend/access/nbtree/nbtxlog.c | 4 ++--
src/backend/access/spgist/spgdoinsert.c | 2 +-
src/backend/access/spgist/spgscan.c | 2 +-
src/backend/access/spgist/spgvacuum.c | 22 +++++++++++-----------
src/backend/storage/page/bufpage.c | 6 +++---
src/include/access/hash.h | 2 +-
src/include/access/itup.h | 23 ++++++++++++++++-------
src/include/access/nbtree.h | 2 +-
src/include/access/spgist_private.h | 12 ++++++------
19 files changed, 69 insertions(+), 60 deletions(-)
diff --git a/contrib/amcheck/verify_nbtree.c b/contrib/amcheck/verify_nbtree.c
index 91caa53dd8..7a20a967ad 100644
--- a/contrib/amcheck/verify_nbtree.c
+++ b/contrib/amcheck/verify_nbtree.c
@@ -3446,12 +3446,12 @@ palloc_btree_page(BtreeCheckState *state, BlockNumber blocknum)
* to move left, in the case of backward index scans).
*/
maxoffset = PageGetMaxOffsetNumber(page);
- if (maxoffset > MaxIndexTuplesPerPage)
+ if (maxoffset > ClusterMaxIndexTuplesPerPage)
ereport(ERROR,
(errcode(ERRCODE_INDEX_CORRUPTED),
- errmsg("Number of items on block %u of index \"%s\" exceeds MaxIndexTuplesPerPage (%u)",
+ errmsg("Number of items on block %u of index \"%s\" exceeds ClusterMaxIndexTuplesPerPage (%u)",
blocknum, RelationGetRelationName(state->rel),
- MaxIndexTuplesPerPage)));
+ ClusterMaxIndexTuplesPerPage)));
if (!P_ISLEAF(opaque) && !P_ISDELETED(opaque) && maxoffset < P_FIRSTDATAKEY(opaque))
ereport(ERROR,
diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c
index 943ae91019..04962b1082 100644
--- a/src/backend/access/gist/gist.c
+++ b/src/backend/access/gist/gist.c
@@ -1664,7 +1664,7 @@ freeGISTstate(GISTSTATE *giststate)
static void
gistprunepage(Relation rel, Page page, Buffer buffer, Relation heapRel)
{
- OffsetNumber deletable[MaxIndexTuplesPerPage];
+ OffsetNumber deletable[MaxIndexTuplesPerPageLimit];
int ndeletable = 0;
OffsetNumber offnum,
maxoff;
diff --git a/src/backend/access/gist/gistget.c b/src/backend/access/gist/gistget.c
index d5c8ae8166..1caa072581 100644
--- a/src/backend/access/gist/gistget.c
+++ b/src/backend/access/gist/gistget.c
@@ -659,12 +659,12 @@ gistgettuple(IndexScanDesc scan, ScanDirection dir)
MemoryContextSwitchTo(so->giststate->scanCxt);
so->killedItems =
- (OffsetNumber *) palloc(MaxIndexTuplesPerPage
+ (OffsetNumber *) palloc(ClusterMaxIndexTuplesPerPage
* sizeof(OffsetNumber));
MemoryContextSwitchTo(oldCxt);
}
- if (so->numKilled < MaxIndexTuplesPerPage)
+ if (so->numKilled < ClusterMaxIndexTuplesPerPage)
so->killedItems[so->numKilled++] =
so->pageData[so->curPageData - 1].offnum;
}
@@ -696,12 +696,12 @@ gistgettuple(IndexScanDesc scan, ScanDirection dir)
MemoryContextSwitchTo(so->giststate->scanCxt);
so->killedItems =
- (OffsetNumber *) palloc(MaxIndexTuplesPerPage
+ (OffsetNumber *) palloc(ClusterMaxIndexTuplesPerPage
* sizeof(OffsetNumber));
MemoryContextSwitchTo(oldCxt);
}
- if (so->numKilled < MaxIndexTuplesPerPage)
+ if (so->numKilled < ClusterMaxIndexTuplesPerPage)
so->killedItems[so->numKilled++] =
so->pageData[so->curPageData - 1].offnum;
}
diff --git a/src/backend/access/hash/hash.c b/src/backend/access/hash/hash.c
index fa5b59a150..3f66b2570a 100644
--- a/src/backend/access/hash/hash.c
+++ b/src/backend/access/hash/hash.c
@@ -312,9 +312,9 @@ hashgettuple(IndexScanDesc scan, ScanDirection dir)
*/
if (so->killedItems == NULL)
so->killedItems = (int *)
- palloc(MaxIndexTuplesPerPage * sizeof(int));
+ palloc(ClusterMaxIndexTuplesPerPage * sizeof(int));
- if (so->numKilled < MaxIndexTuplesPerPage)
+ if (so->numKilled < ClusterMaxIndexTuplesPerPage)
so->killedItems[so->numKilled++] = so->currPos.itemIndex;
}
diff --git a/src/backend/access/hash/hashovfl.c b/src/backend/access/hash/hashovfl.c
index c280ae885e..ec3a1a8356 100644
--- a/src/backend/access/hash/hashovfl.c
+++ b/src/backend/access/hash/hashovfl.c
@@ -903,9 +903,9 @@ _hash_squeezebucket(Relation rel,
OffsetNumber roffnum;
OffsetNumber maxroffnum;
OffsetNumber deletable[MaxOffsetNumber];
- IndexTuple itups[MaxIndexTuplesPerPage];
- Size tups_size[MaxIndexTuplesPerPage];
- OffsetNumber itup_offsets[MaxIndexTuplesPerPage];
+ IndexTuple itups[MaxIndexTuplesPerPageLimit];
+ Size tups_size[MaxIndexTuplesPerPageLimit];
+ OffsetNumber itup_offsets[MaxIndexTuplesPerPageLimit];
uint16 ndeletable = 0;
uint16 nitups = 0;
Size all_tups_size = 0;
diff --git a/src/backend/access/hash/hashpage.c b/src/backend/access/hash/hashpage.c
index 69b07b1453..16503931f6 100644
--- a/src/backend/access/hash/hashpage.c
+++ b/src/backend/access/hash/hashpage.c
@@ -1087,8 +1087,8 @@ _hash_splitbucket(Relation rel,
Page npage;
HashPageOpaque oopaque;
HashPageOpaque nopaque;
- OffsetNumber itup_offsets[MaxIndexTuplesPerPage];
- IndexTuple itups[MaxIndexTuplesPerPage];
+ OffsetNumber itup_offsets[MaxIndexTuplesPerPageLimit];
+ IndexTuple itups[MaxIndexTuplesPerPageLimit];
Size all_tups_size = 0;
int i;
uint16 nitups = 0;
diff --git a/src/backend/access/hash/hashsearch.c b/src/backend/access/hash/hashsearch.c
index 8de3eab498..d033950f71 100644
--- a/src/backend/access/hash/hashsearch.c
+++ b/src/backend/access/hash/hashsearch.c
@@ -532,7 +532,7 @@ _hash_readpage(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
itemIndex = _hash_load_qualified_items(scan, page, offnum, dir);
- if (itemIndex != MaxIndexTuplesPerPage)
+ if (itemIndex != ClusterMaxIndexTuplesPerPage)
break;
/*
@@ -571,8 +571,8 @@ _hash_readpage(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
}
so->currPos.firstItem = itemIndex;
- so->currPos.lastItem = MaxIndexTuplesPerPage - 1;
- so->currPos.itemIndex = MaxIndexTuplesPerPage - 1;
+ so->currPos.lastItem = ClusterMaxIndexTuplesPerPage - 1;
+ so->currPos.itemIndex = ClusterMaxIndexTuplesPerPage - 1;
}
if (so->currPos.buf == so->hashso_bucket_buf ||
@@ -652,13 +652,13 @@ _hash_load_qualified_items(IndexScanDesc scan, Page page,
offnum = OffsetNumberNext(offnum);
}
- Assert(itemIndex <= MaxIndexTuplesPerPage);
+ Assert(itemIndex <= ClusterMaxIndexTuplesPerPage);
return itemIndex;
}
else
{
/* load items[] in descending order */
- itemIndex = MaxIndexTuplesPerPage;
+ itemIndex = ClusterMaxIndexTuplesPerPage;
while (offnum >= FirstOffsetNumber)
{
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..4674e5267a 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -2685,7 +2685,7 @@ _bt_delete_or_dedup_one_page(Relation rel, Relation heapRel,
bool simpleonly, bool checkingunique,
bool uniquedup, bool indexUnchanged)
{
- OffsetNumber deletable[MaxIndexTuplesPerPage];
+ OffsetNumber deletable[MaxIndexTuplesPerPageLimit];
int ndeletable = 0;
OffsetNumber offnum,
minoff,
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..fb24aa4f56 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -1160,7 +1160,7 @@ _bt_delitems_vacuum(Relation rel, Buffer buf,
bool needswal = RelationNeedsWAL(rel);
char *updatedbuf = NULL;
Size updatedbuflen = 0;
- OffsetNumber updatedoffsets[MaxIndexTuplesPerPage];
+ OffsetNumber updatedoffsets[MaxIndexTuplesPerPageLimit];
/* Shouldn't be called unless there's something to do */
Assert(ndeletable > 0 || nupdatable > 0);
@@ -1291,7 +1291,7 @@ _bt_delitems_delete(Relation rel, Buffer buf,
bool needswal = RelationNeedsWAL(rel);
char *updatedbuf = NULL;
Size updatedbuflen = 0;
- OffsetNumber updatedoffsets[MaxIndexTuplesPerPage];
+ OffsetNumber updatedoffsets[MaxIndexTuplesPerPageLimit];
/* Shouldn't be called unless there's something to do */
Assert(ndeletable > 0 || nupdatable > 0);
@@ -1524,8 +1524,8 @@ _bt_delitems_delete_check(Relation rel, Buffer buf, Relation heapRel,
OffsetNumber postingidxoffnum = InvalidOffsetNumber;
int ndeletable = 0,
nupdatable = 0;
- OffsetNumber deletable[MaxIndexTuplesPerPage];
- BTVacuumPosting updatable[MaxIndexTuplesPerPage];
+ OffsetNumber deletable[MaxIndexTuplesPerPageLimit];
+ BTVacuumPosting updatable[MaxIndexTuplesPerPageLimit];
/* Use tableam interface to determine which tuples to delete first */
snapshotConflictHorizon = table_index_delete_tuples(heapRel, delstate);
diff --git a/src/backend/access/nbtree/nbtree.c b/src/backend/access/nbtree/nbtree.c
index 696d79c085..2a4f990583 100644
--- a/src/backend/access/nbtree/nbtree.c
+++ b/src/backend/access/nbtree/nbtree.c
@@ -1159,9 +1159,9 @@ backtrack:
}
else if (P_ISLEAF(opaque))
{
- OffsetNumber deletable[MaxIndexTuplesPerPage];
+ OffsetNumber deletable[MaxIndexTuplesPerPageLimit];
int ndeletable;
- BTVacuumPosting updatable[MaxIndexTuplesPerPage];
+ BTVacuumPosting updatable[MaxIndexTuplesPerPageLimit];
int nupdatable;
OffsetNumber offnum,
minoff,
diff --git a/src/backend/access/nbtree/nbtxlog.c b/src/backend/access/nbtree/nbtxlog.c
index f683c21056..9ab55075a9 100644
--- a/src/backend/access/nbtree/nbtxlog.c
+++ b/src/backend/access/nbtree/nbtxlog.c
@@ -40,8 +40,8 @@ _bt_restore_page(Page page, char *from, int len)
IndexTupleData itupdata;
Size itemsz;
char *end = from + len;
- Item items[MaxIndexTuplesPerPage];
- uint16 itemsizes[MaxIndexTuplesPerPage];
+ Item items[MaxIndexTuplesPerPageLimit];
+ uint16 itemsizes[MaxIndexTuplesPerPageLimit];
int i;
int nitems;
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..3cbe1e2e51 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -135,7 +135,7 @@ spgPageIndexMultiDelete(SpGistState *state, Page page,
BlockNumber blkno, OffsetNumber offnum)
{
OffsetNumber firstItem;
- OffsetNumber sortednos[MaxIndexTuplesPerPage];
+ OffsetNumber sortednos[MaxIndexTuplesPerPageLimit];
SpGistDeadTuple tuple = NULL;
int i;
diff --git a/src/backend/access/spgist/spgscan.c b/src/backend/access/spgist/spgscan.c
index 03293a7816..5690fc4981 100644
--- a/src/backend/access/spgist/spgscan.c
+++ b/src/backend/access/spgist/spgscan.c
@@ -961,7 +961,7 @@ storeGettuple(SpGistScanOpaque so, ItemPointer heapPtr,
SpGistLeafTuple leafTuple, bool recheck,
bool recheckDistances, double *nonNullDistances)
{
- Assert(so->nPtrs < MaxIndexTuplesPerPage);
+ Assert(so->nPtrs < ClusterMaxIndexTuplesPerPage);
so->heapPtrs[so->nPtrs] = *heapPtr;
so->recheck[so->nPtrs] = recheck;
so->recheckDistances[so->nPtrs] = recheckDistances;
diff --git a/src/backend/access/spgist/spgvacuum.c b/src/backend/access/spgist/spgvacuum.c
index ff82b97dd8..1d547d55d0 100644
--- a/src/backend/access/spgist/spgvacuum.c
+++ b/src/backend/access/spgist/spgvacuum.c
@@ -128,14 +128,14 @@ vacuumLeafPage(spgBulkDeleteState *bds, Relation index, Buffer buffer,
{
Page page = BufferGetPage(buffer);
spgxlogVacuumLeaf xlrec;
- OffsetNumber toDead[MaxIndexTuplesPerPage];
- OffsetNumber toPlaceholder[MaxIndexTuplesPerPage];
- OffsetNumber moveSrc[MaxIndexTuplesPerPage];
- OffsetNumber moveDest[MaxIndexTuplesPerPage];
- OffsetNumber chainSrc[MaxIndexTuplesPerPage];
- OffsetNumber chainDest[MaxIndexTuplesPerPage];
- OffsetNumber predecessor[MaxIndexTuplesPerPage + 1];
- bool deletable[MaxIndexTuplesPerPage + 1];
+ OffsetNumber toDead[MaxIndexTuplesPerPageLimit];
+ OffsetNumber toPlaceholder[MaxIndexTuplesPerPageLimit];
+ OffsetNumber moveSrc[MaxIndexTuplesPerPageLimit];
+ OffsetNumber moveDest[MaxIndexTuplesPerPageLimit];
+ OffsetNumber chainSrc[MaxIndexTuplesPerPageLimit];
+ OffsetNumber chainDest[MaxIndexTuplesPerPageLimit];
+ OffsetNumber predecessor[MaxIndexTuplesPerPageLimit + 1];
+ bool deletable[MaxIndexTuplesPerPageLimit + 1];
int nDeletable;
OffsetNumber i,
max = PageGetMaxOffsetNumber(page);
@@ -408,7 +408,7 @@ vacuumLeafRoot(spgBulkDeleteState *bds, Relation index, Buffer buffer)
{
Page page = BufferGetPage(buffer);
spgxlogVacuumRoot xlrec;
- OffsetNumber toDelete[MaxIndexTuplesPerPage];
+ OffsetNumber toDelete[MaxIndexTuplesPerPageLimit];
OffsetNumber i,
max = PageGetMaxOffsetNumber(page);
@@ -498,8 +498,8 @@ vacuumRedirectAndPlaceholder(Relation index, Relation heaprel, Buffer buffer)
firstPlaceholder = InvalidOffsetNumber;
bool hasNonPlaceholder = false;
bool hasUpdate = false;
- OffsetNumber itemToPlaceholder[MaxIndexTuplesPerPage];
- OffsetNumber itemnos[MaxIndexTuplesPerPage];
+ OffsetNumber itemToPlaceholder[MaxIndexTuplesPerPageLimit];
+ OffsetNumber itemnos[MaxIndexTuplesPerPageLimit];
spgxlogVacuumRedirect xlrec;
GlobalVisState *vistest;
diff --git a/src/backend/storage/page/bufpage.c b/src/backend/storage/page/bufpage.c
index f78efce9ad..bc37eae9f2 100644
--- a/src/backend/storage/page/bufpage.c
+++ b/src/backend/storage/page/bufpage.c
@@ -1165,8 +1165,8 @@ PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
Offset pd_upper = phdr->pd_upper;
Offset pd_special = phdr->pd_special;
Offset last_offset;
- itemIdCompactData itemidbase[MaxIndexTuplesPerPage];
- ItemIdData newitemids[MaxIndexTuplesPerPage];
+ itemIdCompactData itemidbase[MaxIndexTuplesPerPageLimit];
+ ItemIdData newitemids[MaxIndexTuplesPerPageLimit];
itemIdCompact itemidptr;
ItemId lp;
int nline,
@@ -1178,7 +1178,7 @@ PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
OffsetNumber offnum;
bool presorted = true; /* For now */
- Assert(nitems <= MaxIndexTuplesPerPage);
+ Assert(nitems <= ClusterMaxIndexTuplesPerPage);
/*
* If there aren't very many items to delete, then retail
diff --git a/src/include/access/hash.h b/src/include/access/hash.h
index 9c7d81525b..b797872bd4 100644
--- a/src/include/access/hash.h
+++ b/src/include/access/hash.h
@@ -124,7 +124,7 @@ typedef struct HashScanPosData
int lastItem; /* last valid index in items[] */
int itemIndex; /* current index in items[] */
- HashScanPosItem items[MaxIndexTuplesPerPage]; /* MUST BE LAST */
+ HashScanPosItem items[MaxIndexTuplesPerPageLimit]; /* MUST BE LAST */
} HashScanPosData;
#define HashScanPosIsPinned(scanpos) \
diff --git a/src/include/access/itup.h b/src/include/access/itup.h
index 94885751e5..61fa8ff538 100644
--- a/src/include/access/itup.h
+++ b/src/include/access/itup.h
@@ -148,11 +148,19 @@ index_getattr(IndexTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
#endif
/*
- * MaxIndexTuplesPerPage is an upper bound on the number of tuples that can
- * fit on one index page. An index tuple must have either data or a null
- * bitmap, so we can safely assume it's at least 1 byte bigger than a bare
- * IndexTupleData struct. We arrive at the divisor because each tuple
- * must be maxaligned, and it must have an associated line pointer.
+ * ClusterMaxIndexTuplesPerPage is a cluster-specific upper bound on the
+ * number of tuples that can fit on one index page. An index tuple must have
+ * either data or a null bitmap, so we can safely assume it's at least 1 byte
+ * bigger than a bare IndexTupleData struct. We arrive at the divisor because
+ * each tuple must be maxaligned, and it must have an associated line pointer.
+ *
+ * MaxIndexTuplesPerPageLimit is the largest value that
+ * ClusterMaxIndexTuplesPerPage could be. While these currently evaluate to
+ * the same value, these are being split out so ClusterMaxIndexTuplesPerPage
+ * can become a variable instead of a constant.
+ *
+ * The CalcMaxIndexTuplesPerPage() macro is used to determine the appropriate
+ * values, given the usable page space on a given page.
*
* To be index-type-independent, this does not account for any special space
* on the page, and is thus conservative.
@@ -163,8 +171,9 @@ 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) ((BLCKSZ - SizeOfPageHeaderData) / \
+#define CalcMaxIndexTuplesPerPage(size) ((int) ((size) / \
(MAXALIGN(sizeof(IndexTupleData) + 1) + sizeof(ItemIdData))))
+#define ClusterMaxIndexTuplesPerPage CalcMaxIndexTuplesPerPage(BLCKSZ - SizeOfPageHeaderData)
+#define MaxIndexTuplesPerPageLimit CalcMaxIndexTuplesPerPage(BLCKSZ - SizeOfPageHeaderData)
#endif /* ITUP_H */
diff --git a/src/include/access/nbtree.h b/src/include/access/nbtree.h
index 6eb162052e..676fa5e0a9 100644
--- a/src/include/access/nbtree.h
+++ b/src/include/access/nbtree.h
@@ -887,7 +887,7 @@ typedef struct BTDedupStateData
* are implicitly unchanged by deduplication pass).
*/
int nintervals; /* current number of intervals in array */
- BTDedupInterval intervals[MaxIndexTuplesPerPage];
+ BTDedupInterval intervals[MaxIndexTuplesPerPageLimit];
} BTDedupStateData;
typedef BTDedupStateData *BTDedupState;
diff --git a/src/include/access/spgist_private.h b/src/include/access/spgist_private.h
index 2e9c757b30..d8ca2b7e0f 100644
--- a/src/include/access/spgist_private.h
+++ b/src/include/access/spgist_private.h
@@ -226,17 +226,17 @@ typedef struct SpGistScanOpaqueData
TupleDesc reconTupDesc; /* if so, descriptor for reconstructed tuples */
int nPtrs; /* number of TIDs found on current page */
int iPtr; /* index for scanning through same */
- ItemPointerData heapPtrs[MaxIndexTuplesPerPage]; /* TIDs from cur page */
- bool recheck[MaxIndexTuplesPerPage]; /* their recheck flags */
- bool recheckDistances[MaxIndexTuplesPerPage]; /* distance recheck
+ ItemPointerData heapPtrs[MaxIndexTuplesPerPageLimit]; /* TIDs from cur page */
+ bool recheck[MaxIndexTuplesPerPageLimit]; /* their recheck flags */
+ bool recheckDistances[MaxIndexTuplesPerPageLimit]; /* distance recheck
* flags */
- HeapTuple reconTups[MaxIndexTuplesPerPage]; /* reconstructed tuples */
+ HeapTuple reconTups[MaxIndexTuplesPerPageLimit]; /* reconstructed tuples */
/* distances (for recheck) */
- IndexOrderByDistance *distances[MaxIndexTuplesPerPage];
+ IndexOrderByDistance *distances[MaxIndexTuplesPerPageLimit];
/*
- * Note: using MaxIndexTuplesPerPage above is a bit hokey since
+ * Note: using ClusterMaxIndexTuplesPerPage above is a bit hokey since
* SpGistLeafTuples aren't exactly IndexTuples; however, they are larger,
* so this is safe.
*/
--
2.40.1
This should target PG 18, but that status is not available in the CF app yet, so just making a note here.
Here is a version 2 of this patch, rebased atop 97d85be365.
As before, this is a cleanup/prerequisite patch series for the page
features/reserved page size patches[1]https://commitfest.postgresql.org/47/3986/. (Said patch series is going
to be updated shortly.)
This splits each of the 4 constants that care about page size into
Cluster-specific and -Limit variants, the first intended to become a
variable in time, and the second being the maximum value such a
variable may take (largely used for static
allocations).
Since these patches define these symbols to have the same values they
previously had, there are no changes in functionality. These were
largely mechanical changes, and as such we should perhaps consider
making the same changes to back-branches to make it so context lines
and the like
would be the same, simplifying maintainer's efforts when applying code
in back branches that touch similar areas.
The script I have to make these changes is simple, and could be run
against the back branches with only the comments surrounding Calc()
pieces needing
to be adjusted once.
Thanks,
David
Attachments:
v2-0001-Split-MaxHeapTuplesPerPage-into-runtime-and-max-v.patchapplication/octet-stream; name=v2-0001-Split-MaxHeapTuplesPerPage-into-runtime-and-max-v.patchDownload
From be3a586e734f73be52f338b8e4fe0282dd427dcc Mon Sep 17 00:00:00 2001
From: David Christensen <david.christensen@crunchydata.com>
Date: Wed, 7 Feb 2024 10:49:31 -0500
Subject: [PATCH v2 1/4] Split MaxHeapTuplesPerPage into runtime and max values
---
contrib/pg_surgery/heap_surgery.c | 4 +--
src/backend/access/brin/brin_bloom.c | 8 +++---
src/backend/access/brin/brin_minmax_multi.c | 8 +++---
src/backend/access/gin/ginpostinglist.c | 6 ++---
src/backend/access/heap/README.HOT | 2 +-
src/backend/access/heap/heapam.c | 6 ++---
src/backend/access/heap/heapam_handler.c | 8 +++---
src/backend/access/heap/hio.c | 2 +-
src/backend/access/heap/pruneheap.c | 22 ++++++++--------
src/backend/access/heap/vacuumlazy.c | 22 ++++++++--------
src/backend/nodes/tidbitmap.c | 2 +-
src/backend/storage/page/bufpage.c | 18 ++++++-------
src/include/access/ginblock.h | 2 +-
src/include/access/heapam.h | 6 ++---
src/include/access/htup_details.h | 26 ++++++++++++++-----
.../test_ginpostinglist/test_ginpostinglist.c | 6 ++---
16 files changed, 81 insertions(+), 67 deletions(-)
diff --git a/contrib/pg_surgery/heap_surgery.c b/contrib/pg_surgery/heap_surgery.c
index 37dffe3f7d..86aff2494e 100644
--- a/contrib/pg_surgery/heap_surgery.c
+++ b/contrib/pg_surgery/heap_surgery.c
@@ -89,7 +89,7 @@ heap_force_common(FunctionCallInfo fcinfo, HeapTupleForceOption heap_force_opt)
Relation rel;
OffsetNumber curr_start_ptr,
next_start_ptr;
- bool include_this_tid[MaxHeapTuplesPerPage];
+ bool include_this_tid[MaxHeapTuplesPerPageLimit];
if (RecoveryInProgress())
ereport(ERROR,
@@ -225,7 +225,7 @@ heap_force_common(FunctionCallInfo fcinfo, HeapTupleForceOption heap_force_opt)
}
/* Mark it for processing. */
- Assert(offno < MaxHeapTuplesPerPage);
+ Assert(offno < ClusterMaxHeapTuplesPerPage);
include_this_tid[offno] = true;
}
diff --git a/src/backend/access/brin/brin_bloom.c b/src/backend/access/brin/brin_bloom.c
index ebf3301627..236446f880 100644
--- a/src/backend/access/brin/brin_bloom.c
+++ b/src/backend/access/brin/brin_bloom.c
@@ -163,7 +163,7 @@ typedef struct BloomOptions
* on the fact that the filter header is ~20B alone, which is about
* the same as the filter bitmap for 16 distinct items with 1% false
* positive rate. So by allowing lower values we'd not gain much. In
- * any case, the min should not be larger than MaxHeapTuplesPerPage
+ * any case, the min should not be larger than ClusterMaxHeapTuplesPerPage
* (~290), which is the theoretical maximum for single-page ranges.
*/
#define BLOOM_MIN_NDISTINCT_PER_RANGE 16
@@ -475,7 +475,7 @@ brin_bloom_opcinfo(PG_FUNCTION_ARGS)
*
* Adjust the ndistinct value based on the pagesPerRange value. First,
* if it's negative, it's assumed to be relative to maximum number of
- * tuples in the range (assuming each page gets MaxHeapTuplesPerPage
+ * tuples in the range (assuming each page gets ClusterMaxHeapTuplesPerPage
* tuples, which is likely a significant over-estimate). We also clamp
* the value, not to over-size the bloom filter unnecessarily.
*
@@ -490,7 +490,7 @@ brin_bloom_opcinfo(PG_FUNCTION_ARGS)
* seems better to rely on the upper estimate.
*
* XXX We might also calculate a better estimate of rows per BRIN range,
- * instead of using MaxHeapTuplesPerPage (which probably produces values
+ * instead of using ClusterMaxHeapTuplesPerPage (which probably produces values
* much higher than reality).
*/
static int
@@ -505,7 +505,7 @@ brin_bloom_get_ndistinct(BrinDesc *bdesc, BloomOptions *opts)
Assert(BlockNumberIsValid(pagesPerRange));
- maxtuples = MaxHeapTuplesPerPage * pagesPerRange;
+ maxtuples = ClusterMaxHeapTuplesPerPage * pagesPerRange;
/*
* Similarly to n_distinct, negative values are relative - in this case to
diff --git a/src/backend/access/brin/brin_minmax_multi.c b/src/backend/access/brin/brin_minmax_multi.c
index c5962c00d6..234dd3d2df 100644
--- a/src/backend/access/brin/brin_minmax_multi.c
+++ b/src/backend/access/brin/brin_minmax_multi.c
@@ -2006,10 +2006,10 @@ brin_minmax_multi_distance_tid(PG_FUNCTION_ARGS)
* We use the no-check variants here, because user-supplied values may
* have (ip_posid == 0). See ItemPointerCompare.
*/
- da1 = ItemPointerGetBlockNumberNoCheck(pa1) * MaxHeapTuplesPerPage +
+ da1 = ItemPointerGetBlockNumberNoCheck(pa1) * ClusterMaxHeapTuplesPerPage +
ItemPointerGetOffsetNumberNoCheck(pa1);
- da2 = ItemPointerGetBlockNumberNoCheck(pa2) * MaxHeapTuplesPerPage +
+ da2 = ItemPointerGetBlockNumberNoCheck(pa2) * ClusterMaxHeapTuplesPerPage +
ItemPointerGetOffsetNumberNoCheck(pa2);
PG_RETURN_FLOAT8(da2 - da1);
@@ -2460,7 +2460,7 @@ brin_minmax_multi_add_value(PG_FUNCTION_ARGS)
* much lower, but meh.
*/
maxvalues = Min(target_maxvalues * MINMAX_BUFFER_FACTOR,
- MaxHeapTuplesPerPage * pagesPerRange);
+ ClusterMaxHeapTuplesPerPage * pagesPerRange);
/* but always at least the original value */
maxvalues = Max(maxvalues, target_maxvalues);
@@ -2506,7 +2506,7 @@ brin_minmax_multi_add_value(PG_FUNCTION_ARGS)
* much lower, but meh.
*/
maxvalues = Min(serialized->maxvalues * MINMAX_BUFFER_FACTOR,
- MaxHeapTuplesPerPage * pagesPerRange);
+ ClusterMaxHeapTuplesPerPage * pagesPerRange);
/* but always at least the original value */
maxvalues = Max(maxvalues, serialized->maxvalues);
diff --git a/src/backend/access/gin/ginpostinglist.c b/src/backend/access/gin/ginpostinglist.c
index 708f9f49ec..8aa0f17bf8 100644
--- a/src/backend/access/gin/ginpostinglist.c
+++ b/src/backend/access/gin/ginpostinglist.c
@@ -26,7 +26,7 @@
* lowest 32 bits are the block number. That leaves 21 bits unused, i.e.
* only 43 low bits are used.
*
- * 11 bits is enough for the offset number, because MaxHeapTuplesPerPage <
+ * 11 bits is enough for the offset number, because ClusterMaxHeapTuplesPerPage <
* 2^11 on all supported block sizes. We are frugal with the bits, because
* smaller integers use fewer bytes in the varbyte encoding, saving disk
* space. (If we get a new table AM in the future that wants to use the full
@@ -74,9 +74,9 @@
/*
* How many bits do you need to encode offset number? OffsetNumber is a 16-bit
* integer, but you can't fit that many items on a page. 11 ought to be more
- * than enough. It's tempting to derive this from MaxHeapTuplesPerPage, and
+ * than enough. It's tempting to derive this from ClusterMaxHeapTuplesPerPage, and
* use the minimum number of bits, but that would require changing the on-disk
- * format if MaxHeapTuplesPerPage changes. Better to leave some slack.
+ * format if ClusterMaxHeapTuplesPerPage changes. Better to leave some slack.
*/
#define MaxHeapTuplesPerPageBits 11
diff --git a/src/backend/access/heap/README.HOT b/src/backend/access/heap/README.HOT
index 74e407f375..e286e1dec3 100644
--- a/src/backend/access/heap/README.HOT
+++ b/src/backend/access/heap/README.HOT
@@ -264,7 +264,7 @@ of line pointer bloat: we might end up with huge numbers of line pointers
and just a few actual tuples on a page. To limit the damage in the worst
case, and to keep various work arrays as well as the bitmaps in bitmap
scans reasonably sized, the maximum number of line pointers per page
-is arbitrarily capped at MaxHeapTuplesPerPage (the most tuples that
+is arbitrarily capped at ClusterMaxHeapTuplesPerPage (the most tuples that
could fit without HOT pruning).
Effectively, space reclamation happens during tuple retrieval when the
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 34bc60f625..d9ccffbcbb 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -474,7 +474,7 @@ heapgetpage(TableScanDesc sscan, BlockNumber block)
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
- Assert(ntup <= MaxHeapTuplesPerPage);
+ Assert(ntup <= ClusterMaxHeapTuplesPerPage);
scan->rs_ntuples = ntup;
}
@@ -6746,8 +6746,8 @@ heap_freeze_execute_prepared(Relation rel, Buffer buffer,
/* Now WAL-log freezing if necessary */
if (RelationNeedsWAL(rel))
{
- xl_heap_freeze_plan plans[MaxHeapTuplesPerPage];
- OffsetNumber offsets[MaxHeapTuplesPerPage];
+ xl_heap_freeze_plan plans[MaxHeapTuplesPerPageLimit];
+ OffsetNumber offsets[MaxHeapTuplesPerPageLimit];
int nplans;
xl_heap_freeze_page xlrec;
XLogRecPtr recptr;
diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c
index 680a50bf8b..dd894afaf1 100644
--- a/src/backend/access/heap/heapam_handler.c
+++ b/src/backend/access/heap/heapam_handler.c
@@ -1189,7 +1189,7 @@ heapam_index_build_range_scan(Relation heapRelation,
TransactionId OldestXmin;
BlockNumber previous_blkno = InvalidBlockNumber;
BlockNumber root_blkno = InvalidBlockNumber;
- OffsetNumber root_offsets[MaxHeapTuplesPerPage];
+ OffsetNumber root_offsets[MaxHeapTuplesPerPageLimit];
/*
* sanity checks
@@ -1752,8 +1752,8 @@ heapam_index_validate_scan(Relation heapRelation,
EState *estate;
ExprContext *econtext;
BlockNumber root_blkno = InvalidBlockNumber;
- OffsetNumber root_offsets[MaxHeapTuplesPerPage];
- bool in_index[MaxHeapTuplesPerPage];
+ OffsetNumber root_offsets[MaxHeapTuplesPerPageLimit];
+ bool in_index[MaxHeapTuplesPerPageLimit];
BlockNumber previous_blkno = InvalidBlockNumber;
/* state variables for the merge */
@@ -2218,7 +2218,7 @@ heapam_scan_bitmap_next_block(TableScanDesc scan,
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
- Assert(ntup <= MaxHeapTuplesPerPage);
+ Assert(ntup <= ClusterMaxHeapTuplesPerPage);
hscan->rs_ntuples = ntup;
return ntup > 0;
diff --git a/src/backend/access/heap/hio.c b/src/backend/access/heap/hio.c
index 7c662cdf46..a17431fda1 100644
--- a/src/backend/access/heap/hio.c
+++ b/src/backend/access/heap/hio.c
@@ -546,7 +546,7 @@ RelationGetBufferForTuple(Relation relation, Size len,
* extensions while inserting large tuples into low-fillfactor tables.
*/
nearlyEmptyFreeSpace = MaxHeapTupleSize -
- (MaxHeapTuplesPerPage / 8 * sizeof(ItemIdData));
+ (ClusterMaxHeapTuplesPerPage / 8 * sizeof(ItemIdData));
if (len + saveFreeSpace > nearlyEmptyFreeSpace)
targetFreeSpace = Max(len, nearlyEmptyFreeSpace);
else
diff --git a/src/backend/access/heap/pruneheap.c b/src/backend/access/heap/pruneheap.c
index 4f12413b8b..deb153198f 100644
--- a/src/backend/access/heap/pruneheap.c
+++ b/src/backend/access/heap/pruneheap.c
@@ -42,17 +42,17 @@ typedef struct
int ndead;
int nunused;
/* arrays that accumulate indexes of items to be changed */
- OffsetNumber redirected[MaxHeapTuplesPerPage * 2];
- OffsetNumber nowdead[MaxHeapTuplesPerPage];
- OffsetNumber nowunused[MaxHeapTuplesPerPage];
+ OffsetNumber redirected[MaxHeapTuplesPerPageLimit * 2];
+ OffsetNumber nowdead[MaxHeapTuplesPerPageLimit];
+ OffsetNumber nowunused[MaxHeapTuplesPerPageLimit];
/*
* marked[i] is true if item i is entered in one of the above arrays.
*
- * This needs to be MaxHeapTuplesPerPage + 1 long as FirstOffsetNumber is
+ * This needs to be ClusterMaxHeapTuplesPerPage + 1 long as FirstOffsetNumber is
* 1. Otherwise every access would need to subtract 1.
*/
- bool marked[MaxHeapTuplesPerPage + 1];
+ bool marked[MaxHeapTuplesPerPageLimit + 1];
} PruneState;
/* Local functions */
@@ -494,7 +494,7 @@ heap_prune_chain(Buffer buffer, OffsetNumber rootoffnum,
OffsetNumber latestdead = InvalidOffsetNumber,
maxoff = PageGetMaxOffsetNumber(dp),
offnum;
- OffsetNumber chainitems[MaxHeapTuplesPerPage];
+ OffsetNumber chainitems[MaxHeapTuplesPerPageLimit];
int nchain = 0,
i;
@@ -775,7 +775,7 @@ static void
heap_prune_record_redirect(PruneState *prstate,
OffsetNumber offnum, OffsetNumber rdoffnum)
{
- Assert(prstate->nredirected < MaxHeapTuplesPerPage);
+ Assert(prstate->nredirected < ClusterMaxHeapTuplesPerPage);
prstate->redirected[prstate->nredirected * 2] = offnum;
prstate->redirected[prstate->nredirected * 2 + 1] = rdoffnum;
prstate->nredirected++;
@@ -789,7 +789,7 @@ heap_prune_record_redirect(PruneState *prstate,
static void
heap_prune_record_dead(PruneState *prstate, OffsetNumber offnum)
{
- Assert(prstate->ndead < MaxHeapTuplesPerPage);
+ Assert(prstate->ndead < ClusterMaxHeapTuplesPerPage);
prstate->nowdead[prstate->ndead] = offnum;
prstate->ndead++;
Assert(!prstate->marked[offnum]);
@@ -821,7 +821,7 @@ heap_prune_record_dead_or_unused(PruneState *prstate, OffsetNumber offnum)
static void
heap_prune_record_unused(PruneState *prstate, OffsetNumber offnum)
{
- Assert(prstate->nunused < MaxHeapTuplesPerPage);
+ Assert(prstate->nunused < ClusterMaxHeapTuplesPerPage);
prstate->nowunused[prstate->nunused] = offnum;
prstate->nunused++;
Assert(!prstate->marked[offnum]);
@@ -1034,7 +1034,7 @@ page_verify_redirects(Page page)
* If item k is part of a HOT-chain with root at item j, then we set
* root_offsets[k - 1] = j.
*
- * The passed-in root_offsets array must have MaxHeapTuplesPerPage entries.
+ * The passed-in root_offsets array must have ClusterMaxHeapTuplesPerPage entries.
* Unused entries are filled with InvalidOffsetNumber (zero).
*
* The function must be called with at least share lock on the buffer, to
@@ -1051,7 +1051,7 @@ heap_get_root_tuples(Page page, OffsetNumber *root_offsets)
maxoff;
MemSet(root_offsets, InvalidOffsetNumber,
- MaxHeapTuplesPerPage * sizeof(OffsetNumber));
+ ClusterMaxHeapTuplesPerPage * sizeof(OffsetNumber));
maxoff = PageGetMaxOffsetNumber(page);
for (offnum = FirstOffsetNumber; offnum <= maxoff; offnum = OffsetNumberNext(offnum))
diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index 1800490775..c147c77984 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -866,8 +866,8 @@ lazy_scan_heap(LVRelState *vacrel)
* dead_items TIDs, pause and do a cycle of vacuuming before we tackle
* this page.
*/
- Assert(dead_items->max_items >= MaxHeapTuplesPerPage);
- if (dead_items->max_items - dead_items->num_items < MaxHeapTuplesPerPage)
+ Assert(dead_items->max_items >= ClusterMaxHeapTuplesPerPage);
+ if (dead_items->max_items - dead_items->num_items < ClusterMaxHeapTuplesPerPage)
{
/*
* Before beginning index vacuuming, we release any pin we may
@@ -1426,8 +1426,8 @@ lazy_scan_prune(LVRelState *vacrel,
all_frozen;
TransactionId visibility_cutoff_xid;
int64 fpi_before = pgWalUsage.wal_fpi;
- OffsetNumber deadoffsets[MaxHeapTuplesPerPage];
- HeapTupleFreeze frozen[MaxHeapTuplesPerPage];
+ OffsetNumber deadoffsets[MaxHeapTuplesPerPageLimit];
+ HeapTupleFreeze frozen[MaxHeapTuplesPerPageLimit];
Assert(BufferGetBlockNumber(buf) == blkno);
@@ -1955,7 +1955,7 @@ lazy_scan_noprune(LVRelState *vacrel,
HeapTupleHeader tupleheader;
TransactionId NoFreezePageRelfrozenXid = vacrel->NewRelfrozenXid;
MultiXactId NoFreezePageRelminMxid = vacrel->NewRelminMxid;
- OffsetNumber deadoffsets[MaxHeapTuplesPerPage];
+ OffsetNumber deadoffsets[MaxHeapTuplesPerPageLimit];
Assert(BufferGetBlockNumber(buf) == blkno);
@@ -2499,7 +2499,7 @@ lazy_vacuum_heap_page(LVRelState *vacrel, BlockNumber blkno, Buffer buffer,
{
VacDeadItems *dead_items = vacrel->dead_items;
Page page = BufferGetPage(buffer);
- OffsetNumber unused[MaxHeapTuplesPerPage];
+ OffsetNumber unused[MaxHeapTuplesPerPageLimit];
int nunused = 0;
TransactionId visibility_cutoff_xid;
bool all_frozen;
@@ -3150,16 +3150,16 @@ dead_items_max_items(LVRelState *vacrel)
max_items = Min(max_items, MAXDEADITEMS(MaxAllocSize));
/* curious coding here to ensure the multiplication can't overflow */
- if ((BlockNumber) (max_items / MaxHeapTuplesPerPage) > rel_pages)
- max_items = rel_pages * MaxHeapTuplesPerPage;
+ if ((BlockNumber) (max_items / ClusterMaxHeapTuplesPerPage) > rel_pages)
+ max_items = rel_pages * ClusterMaxHeapTuplesPerPage;
/* stay sane if small maintenance_work_mem */
- max_items = Max(max_items, MaxHeapTuplesPerPage);
+ max_items = Max(max_items, ClusterMaxHeapTuplesPerPage);
}
else
{
/* One-pass case only stores a single heap page's TIDs at a time */
- max_items = MaxHeapTuplesPerPage;
+ max_items = ClusterMaxHeapTuplesPerPage;
}
return (int) max_items;
@@ -3179,7 +3179,7 @@ dead_items_alloc(LVRelState *vacrel, int nworkers)
int max_items;
max_items = dead_items_max_items(vacrel);
- Assert(max_items >= MaxHeapTuplesPerPage);
+ Assert(max_items >= ClusterMaxHeapTuplesPerPage);
/*
* Initialize state for a parallel vacuum. As of now, only one worker can
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index e8ab5d78fc..1afa431062 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 MaxHeapTuplesPerPage
+#define MAX_TUPLES_PER_PAGE ClusterMaxHeapTuplesPerPage
/*
* 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 be6f1f62d2..f78efce9ad 100644
--- a/src/backend/storage/page/bufpage.c
+++ b/src/backend/storage/page/bufpage.c
@@ -186,7 +186,7 @@ PageIsVerifiedExtended(Page page, BlockNumber blkno, int flags)
* one that is both unused and deallocated.
*
* If flag PAI_IS_HEAP is set, we enforce that there can't be more than
- * MaxHeapTuplesPerPage line pointers on the page.
+ * ClusterMaxHeapTuplesPerPage line pointers on the page.
*
* !!! EREPORT(ERROR) IS DISALLOWED HERE !!!
*/
@@ -295,9 +295,9 @@ PageAddItemExtended(Page page,
}
/* Reject placing items beyond heap boundary, if heap */
- if ((flags & PAI_IS_HEAP) != 0 && offsetNumber > MaxHeapTuplesPerPage)
+ if ((flags & PAI_IS_HEAP) != 0 && offsetNumber > ClusterMaxHeapTuplesPerPage)
{
- elog(WARNING, "can't put more than MaxHeapTuplesPerPage items in a heap page");
+ elog(WARNING, "can't put more than ClusterMaxHeapTuplesPerPage items in a heap page");
return InvalidOffsetNumber;
}
@@ -702,7 +702,7 @@ PageRepairFragmentation(Page page)
Offset pd_upper = ((PageHeader) page)->pd_upper;
Offset pd_special = ((PageHeader) page)->pd_special;
Offset last_offset;
- itemIdCompactData itemidbase[MaxHeapTuplesPerPage];
+ itemIdCompactData itemidbase[MaxHeapTuplesPerPageLimit];
itemIdCompact itemidptr;
ItemId lp;
int nline,
@@ -979,12 +979,12 @@ PageGetExactFreeSpace(Page page)
* reduced by the space needed for a new line pointer.
*
* The difference between this and PageGetFreeSpace is that this will return
- * zero if there are already MaxHeapTuplesPerPage line pointers in the page
+ * zero if there are already ClusterMaxHeapTuplesPerPage line pointers in the page
* and none are free. We use this to enforce that no more than
- * MaxHeapTuplesPerPage line pointers are created on a heap page. (Although
+ * ClusterMaxHeapTuplesPerPage line pointers are created on a heap page. (Although
* no more tuples than that could fit anyway, in the presence of redirected
* or dead line pointers it'd be possible to have too many line pointers.
- * To avoid breaking code that assumes MaxHeapTuplesPerPage is a hard limit
+ * To avoid breaking code that assumes ClusterMaxHeapTuplesPerPage is a hard limit
* on the number of line pointers, we make this extra check.)
*/
Size
@@ -999,10 +999,10 @@ PageGetHeapFreeSpace(Page page)
nline;
/*
- * Are there already MaxHeapTuplesPerPage line pointers in the page?
+ * Are there already ClusterMaxHeapTuplesPerPage line pointers in the page?
*/
nline = PageGetMaxOffsetNumber(page);
- if (nline >= MaxHeapTuplesPerPage)
+ if (nline >= ClusterMaxHeapTuplesPerPage)
{
if (PageHasFreeLinePointers(page))
{
diff --git a/src/include/access/ginblock.h b/src/include/access/ginblock.h
index b3b7daa049..c55d11be64 100644
--- a/src/include/access/ginblock.h
+++ b/src/include/access/ginblock.h
@@ -162,7 +162,7 @@ extern bool GinPageIsRecyclable(Page page);
* pointers for that page
* Note that these are all distinguishable from an "invalid" item pointer
* (which is InvalidBlockNumber/0) as well as from all normal item
- * pointers (which have item numbers in the range 1..MaxHeapTuplesPerPage).
+ * pointers (which have item numbers in the range 1..ClusterMaxHeapTuplesPerPage).
*/
#define ItemPointerSetMin(p) \
ItemPointerSet((p), (BlockNumber)0, (OffsetNumber)0)
diff --git a/src/include/access/heapam.h b/src/include/access/heapam.h
index 4b133f6859..3217d72f99 100644
--- a/src/include/access/heapam.h
+++ b/src/include/access/heapam.h
@@ -75,7 +75,7 @@ typedef struct HeapScanDescData
/* these fields only used in page-at-a-time mode and for bitmap scans */
int rs_cindex; /* current tuple's index in vistuples */
int rs_ntuples; /* number of visible tuples on page */
- OffsetNumber rs_vistuples[MaxHeapTuplesPerPage]; /* their offsets */
+ OffsetNumber rs_vistuples[MaxHeapTuplesPerPageLimit]; /* their offsets */
} HeapScanDescData;
typedef struct HeapScanDescData *HeapScanDesc;
@@ -205,10 +205,10 @@ typedef struct PruneResult
* This is of type int8[], instead of HTSV_Result[], so we can use -1 to
* indicate no visibility has been computed, e.g. for LP_DEAD items.
*
- * This needs to be MaxHeapTuplesPerPage + 1 long as FirstOffsetNumber is
+ * This needs to be ClusterMaxHeapTuplesPerPage + 1 long as FirstOffsetNumber is
* 1. Otherwise every access would need to subtract 1.
*/
- int8 htsv[MaxHeapTuplesPerPage + 1];
+ int8 htsv[MaxHeapTuplesPerPageLimit + 1];
} PruneResult;
/*
diff --git a/src/include/access/htup_details.h b/src/include/access/htup_details.h
index 5e38ef8696..7a35baf4b1 100644
--- a/src/include/access/htup_details.h
+++ b/src/include/access/htup_details.h
@@ -564,19 +564,33 @@ StaticAssertDecl(MaxOffsetNumber < SpecTokenOffsetNumber,
#define MinHeapTupleSize MAXALIGN(SizeofHeapTupleHeader)
/*
- * MaxHeapTuplesPerPage is an upper bound on the number of tuples that can
- * fit on one heap page. (Note that indexes could have more, because they
- * use a smaller tuple header.) We arrive at the divisor because each tuple
- * must be maxaligned, and it must have an associated line pointer.
+ * ClusterMaxHeapTuplesPerPage is a cluster-specific upper bound on the number
+ * of tuples that can fit on one heap page. (Note that indexes could have
+ * more, because they use a smaller tuple header.) We arrive at the divisor
+ * because each tuple must be maxaligned, and it must have an associated line
+ * pointer.
+ *
+ * MaxHeapTuplesPerPageLimit is the largest value that
+ * ClusterMaxHeapTuplesPerPage could be. While these currently evaluate to
+ * the same value, these are being split out so ClusterMaxHeapTuplesPerPage
+ * can become a variable instead of a constant.
+ *
+ * The CalcMaxHeapTuplesPerPage() macro is used to determine the appropriate
+ * values, given the usable page space on a given page.
+ *
+ * The old MaxHeapTuplesPerPage symbol has been removed; static allocations
+ * should use the MaxHeapTuplesPerPageLimit constant, while runtime code
+ * should use ClusterMaxHeapTuplesPerPage.
*
* Note: with HOT, there could theoretically be more line pointers (not actual
* tuples) than this on a heap page. However we constrain the number of line
* pointers to this anyway, to avoid excessive line-pointer bloat and not
* require increases in the size of work arrays.
*/
-#define MaxHeapTuplesPerPage \
- ((int) ((BLCKSZ - SizeOfPageHeaderData) / \
+#define CalcMaxHeapTuplesPerPage(size) ((int) ((size) / \
(MAXALIGN(SizeofHeapTupleHeader) + sizeof(ItemIdData))))
+#define ClusterMaxHeapTuplesPerPage CalcMaxHeapTuplesPerPage(BLCKSZ - SizeOfPageHeaderData)
+#define MaxHeapTuplesPerPageLimit CalcMaxHeapTuplesPerPage(BLCKSZ - SizeOfPageHeaderData)
/*
* MaxAttrSize is a somewhat arbitrary upper limit on the declared size of
diff --git a/src/test/modules/test_ginpostinglist/test_ginpostinglist.c b/src/test/modules/test_ginpostinglist/test_ginpostinglist.c
index 04215cadd9..f79dbc2bdd 100644
--- a/src/test/modules/test_ginpostinglist/test_ginpostinglist.c
+++ b/src/test/modules/test_ginpostinglist/test_ginpostinglist.c
@@ -88,9 +88,9 @@ Datum
test_ginpostinglist(PG_FUNCTION_ARGS)
{
test_itemptr_pair(0, 2, 14);
- test_itemptr_pair(0, MaxHeapTuplesPerPage, 14);
- test_itemptr_pair(MaxBlockNumber, MaxHeapTuplesPerPage, 14);
- test_itemptr_pair(MaxBlockNumber, MaxHeapTuplesPerPage, 16);
+ test_itemptr_pair(0, ClusterMaxHeapTuplesPerPage, 14);
+ test_itemptr_pair(MaxBlockNumber, ClusterMaxHeapTuplesPerPage, 14);
+ test_itemptr_pair(MaxBlockNumber, ClusterMaxHeapTuplesPerPage, 16);
PG_RETURN_VOID();
}
--
2.40.1
v2-0002-Split-MaxHeapTupleSize-into-runtime-and-max-value.patchapplication/octet-stream; name=v2-0002-Split-MaxHeapTupleSize-into-runtime-and-max-value.patchDownload
From 945a6c26e09c1ca4abb14940299898f437529e54 Mon Sep 17 00:00:00 2001
From: David Christensen <david.christensen@crunchydata.com>
Date: Wed, 7 Feb 2024 10:49:31 -0500
Subject: [PATCH v2 2/4] Split MaxHeapTupleSize into runtime and max values
---
src/backend/access/heap/heapam.c | 12 +++++-----
src/backend/access/heap/hio.c | 6 ++---
src/backend/access/heap/rewriteheap.c | 4 ++--
.../replication/logical/reorderbuffer.c | 2 +-
src/backend/storage/freespace/freespace.c | 2 +-
src/include/access/heaptoast.h | 2 +-
src/include/access/htup_details.h | 22 ++++++++++++++-----
src/test/regress/expected/insert.out | 2 +-
src/test/regress/sql/insert.sql | 2 +-
9 files changed, 32 insertions(+), 22 deletions(-)
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index d9ccffbcbb..633c6e4303 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -9227,7 +9227,7 @@ heap_xlog_insert(XLogReaderState *record)
union
{
HeapTupleHeaderData hdr;
- char data[MaxHeapTupleSize];
+ char data[MaxHeapTupleSizeLimit];
} tbuf;
HeapTupleHeader htup;
xl_heap_header xlhdr;
@@ -9283,7 +9283,7 @@ heap_xlog_insert(XLogReaderState *record)
data = XLogRecGetBlockData(record, 0, &datalen);
newlen = datalen - SizeOfHeapHeader;
- Assert(datalen > SizeOfHeapHeader && newlen <= MaxHeapTupleSize);
+ Assert(datalen > SizeOfHeapHeader && newlen <= ClusterMaxHeapTupleSize);
memcpy((char *) &xlhdr, data, SizeOfHeapHeader);
data += SizeOfHeapHeader;
@@ -9349,7 +9349,7 @@ heap_xlog_multi_insert(XLogReaderState *record)
union
{
HeapTupleHeaderData hdr;
- char data[MaxHeapTupleSize];
+ char data[MaxHeapTupleSizeLimit];
} tbuf;
HeapTupleHeader htup;
uint32 newlen;
@@ -9427,7 +9427,7 @@ heap_xlog_multi_insert(XLogReaderState *record)
tupdata = ((char *) xlhdr) + SizeOfMultiInsertTuple;
newlen = xlhdr->datalen;
- Assert(newlen <= MaxHeapTupleSize);
+ Assert(newlen <= ClusterMaxHeapTupleSize);
htup = &tbuf.hdr;
MemSet((char *) htup, 0, SizeofHeapTupleHeader);
/* PG73FORMAT: get bitmap [+ padding] [+ oid] + data */
@@ -9506,7 +9506,7 @@ heap_xlog_update(XLogReaderState *record, bool hot_update)
union
{
HeapTupleHeaderData hdr;
- char data[MaxHeapTupleSize];
+ char data[MaxHeapTupleSizeLimit];
} tbuf;
xl_heap_header xlhdr;
uint32 newlen;
@@ -9662,7 +9662,7 @@ heap_xlog_update(XLogReaderState *record, bool hot_update)
recdata += SizeOfHeapHeader;
tuplen = recdata_end - recdata;
- Assert(tuplen <= MaxHeapTupleSize);
+ Assert(tuplen <= ClusterMaxHeapTupleSize);
htup = &tbuf.hdr;
MemSet((char *) htup, 0, SizeofHeapTupleHeader);
diff --git a/src/backend/access/heap/hio.c b/src/backend/access/heap/hio.c
index a17431fda1..a515a45b00 100644
--- a/src/backend/access/heap/hio.c
+++ b/src/backend/access/heap/hio.c
@@ -529,11 +529,11 @@ RelationGetBufferForTuple(Relation relation, Size len,
/*
* If we're gonna fail for oversize tuple, do it right away
*/
- if (len > MaxHeapTupleSize)
+ if (len > ClusterMaxHeapTupleSize)
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("row is too big: size %zu, maximum size %zu",
- len, MaxHeapTupleSize)));
+ len, ClusterMaxHeapTupleSize)));
/* Compute desired extra freespace due to fillfactor option */
saveFreeSpace = RelationGetTargetPageFreeSpace(relation,
@@ -545,7 +545,7 @@ RelationGetBufferForTuple(Relation relation, Size len,
* somewhat arbitrary, but it should prevent most unnecessary relation
* extensions while inserting large tuples into low-fillfactor tables.
*/
- nearlyEmptyFreeSpace = MaxHeapTupleSize -
+ nearlyEmptyFreeSpace = ClusterMaxHeapTupleSize -
(ClusterMaxHeapTuplesPerPage / 8 * sizeof(ItemIdData));
if (len + saveFreeSpace > nearlyEmptyFreeSpace)
targetFreeSpace = Max(len, nearlyEmptyFreeSpace);
diff --git a/src/backend/access/heap/rewriteheap.c b/src/backend/access/heap/rewriteheap.c
index 473f3aa9be..1350af2a7d 100644
--- a/src/backend/access/heap/rewriteheap.c
+++ b/src/backend/access/heap/rewriteheap.c
@@ -634,11 +634,11 @@ raw_heap_insert(RewriteState state, HeapTuple tup)
/*
* If we're gonna fail for oversize tuple, do it right away
*/
- if (len > MaxHeapTupleSize)
+ if (len > ClusterMaxHeapTupleSize)
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("row is too big: size %zu, maximum size %zu",
- len, MaxHeapTupleSize)));
+ len, ClusterMaxHeapTupleSize)));
/* Compute desired extra freespace due to fillfactor option */
saveFreeSpace = RelationGetTargetPageFreeSpace(state->rs_new_rel,
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index 001f901ee6..0aac767974 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -4878,7 +4878,7 @@ ReorderBufferToastReplace(ReorderBuffer *rb, ReorderBufferTXN *txn,
* the tuplebuf because attrs[] will point back into the current content.
*/
tmphtup = heap_form_tuple(desc, attrs, isnull);
- Assert(newtup->t_len <= MaxHeapTupleSize);
+ Assert(newtup->t_len <= ClusterMaxHeapTupleSize);
Assert(newtup->t_data == (HeapTupleHeader) ((char *) newtup + HEAPTUPLESIZE));
memcpy(newtup->t_data, tmphtup->t_data, tmphtup->t_len);
diff --git a/src/backend/storage/freespace/freespace.c b/src/backend/storage/freespace/freespace.c
index bcdb182193..1604937242 100644
--- a/src/backend/storage/freespace/freespace.c
+++ b/src/backend/storage/freespace/freespace.c
@@ -63,7 +63,7 @@
*/
#define FSM_CATEGORIES 256
#define FSM_CAT_STEP (BLCKSZ / FSM_CATEGORIES)
-#define MaxFSMRequestSize MaxHeapTupleSize
+#define MaxFSMRequestSize ClusterMaxHeapTupleSize
/*
* Depth of the on-disk tree. We need to be able to address 2^32-1 blocks,
diff --git a/src/include/access/heaptoast.h b/src/include/access/heaptoast.h
index c376dff48d..6fe836f7d1 100644
--- a/src/include/access/heaptoast.h
+++ b/src/include/access/heaptoast.h
@@ -65,7 +65,7 @@
* compress it (we can't move it out-of-line, however). Note that this
* number is per-datum, not per-tuple, for simplicity in index_form_tuple().
*/
-#define TOAST_INDEX_TARGET (MaxHeapTupleSize / 16)
+#define TOAST_INDEX_TARGET (ClusterMaxHeapTupleSize / 16)
/*
* When we store an oversize datum externally, we divide it into chunks
diff --git a/src/include/access/htup_details.h b/src/include/access/htup_details.h
index 7a35baf4b1..bb5c746b98 100644
--- a/src/include/access/htup_details.h
+++ b/src/include/access/htup_details.h
@@ -550,17 +550,27 @@ StaticAssertDecl(MaxOffsetNumber < SpecTokenOffsetNumber,
#define BITMAPLEN(NATTS) (((int)(NATTS) + 7) / 8)
/*
- * MaxHeapTupleSize is the maximum allowed size of a heap tuple, including
- * header and MAXALIGN alignment padding. Basically it's BLCKSZ minus the
- * other stuff that has to be on a disk page. Since heap pages use no
- * "special space", there's no deduction for that.
+ * ClusterMaxHeapTupleSize is a cluster-specific maximum allowed size of a
+ * heap tuple, including header and MAXALIGN alignment padding. Basically
+ * it's BLCKSZ minus the other stuff that has to be on a disk page. Since
+ * heap pages use no "special space", there's no deduction for that.
+ *
+ * MaxHeapTuplesSizeLimit is the largest value that ClusterMaxHeapTupleSize
+ * could be. While these currently evaluate to the same value, these are
+ * being split out so ClusterMaxHeapTupleSize can become a variable
+ * instead of a constant.
+ *
+ * The CalcMaxHeapTupleSize() macro is used to determine the appropriate
+ * values, given the usable page space on a given page.
*
* NOTE: we allow for the ItemId that must point to the tuple, ensuring that
* an otherwise-empty page can indeed hold a tuple of this size. Because
* 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.
+ * you can, say, fit 2 tuples of size ClusterMaxHeapTupleSize/2 on the same page.
*/
-#define MaxHeapTupleSize (BLCKSZ - MAXALIGN(SizeOfPageHeaderData + sizeof(ItemIdData)))
+#define CalcMaxHeapTupleSize(size) (size - sizeof(ItemIdData))
+#define ClusterMaxHeapTupleSize CalcMaxHeapTupleSize(BLCKSZ - SizeOfPageHeaderData)
+#define MaxHeapTupleSizeLimit CalcMaxHeapTupleSize(BLCKSZ - SizeOfPageHeaderData)
#define MinHeapTupleSize MAXALIGN(SizeofHeapTupleHeader)
/*
diff --git a/src/test/regress/expected/insert.out b/src/test/regress/expected/insert.out
index dd4354fc7d..eebf3c6d4d 100644
--- a/src/test/regress/expected/insert.out
+++ b/src/test/regress/expected/insert.out
@@ -86,7 +86,7 @@ drop table inserttest;
--
CREATE TABLE large_tuple_test (a int, b text) WITH (fillfactor = 10);
ALTER TABLE large_tuple_test ALTER COLUMN b SET STORAGE plain;
--- create page w/ free space in range [nearlyEmptyFreeSpace, MaxHeapTupleSize)
+-- create page w/ free space in range [nearlyEmptyFreeSpace, ClusterMaxHeapTupleSize)
INSERT INTO large_tuple_test (select 1, NULL);
-- should still fit on the page
INSERT INTO large_tuple_test (select 2, repeat('a', 1000));
diff --git a/src/test/regress/sql/insert.sql b/src/test/regress/sql/insert.sql
index bdcffd0314..53f46e7960 100644
--- a/src/test/regress/sql/insert.sql
+++ b/src/test/regress/sql/insert.sql
@@ -43,7 +43,7 @@ drop table inserttest;
CREATE TABLE large_tuple_test (a int, b text) WITH (fillfactor = 10);
ALTER TABLE large_tuple_test ALTER COLUMN b SET STORAGE plain;
--- create page w/ free space in range [nearlyEmptyFreeSpace, MaxHeapTupleSize)
+-- create page w/ free space in range [nearlyEmptyFreeSpace, ClusterMaxHeapTupleSize)
INSERT INTO large_tuple_test (select 1, NULL);
-- should still fit on the page
--
2.40.1
v2-0004-Split-MaxTIDsPerBTreePage-into-runtime-and-max-va.patchapplication/octet-stream; name=v2-0004-Split-MaxTIDsPerBTreePage-into-runtime-and-max-va.patchDownload
From 3f02fe6f75a9eb49bea6582d10ba4bdb8a62c29b Mon Sep 17 00:00:00 2001
From: David Christensen <david.christensen@crunchydata.com>
Date: Wed, 7 Feb 2024 10:49:31 -0500
Subject: [PATCH v2 4/4] Split MaxTIDsPerBTreePage into runtime and max values
---
contrib/amcheck/verify_nbtree.c | 4 ++--
src/backend/access/nbtree/nbtdedup.c | 4 ++--
src/backend/access/nbtree/nbtinsert.c | 4 ++--
src/backend/access/nbtree/nbtree.c | 4 ++--
src/backend/access/nbtree/nbtsearch.c | 8 ++++----
src/include/access/nbtree.h | 24 ++++++++++++++++++------
6 files changed, 30 insertions(+), 18 deletions(-)
diff --git a/contrib/amcheck/verify_nbtree.c b/contrib/amcheck/verify_nbtree.c
index b1089693be..73352296e2 100644
--- a/contrib/amcheck/verify_nbtree.c
+++ b/contrib/amcheck/verify_nbtree.c
@@ -534,12 +534,12 @@ bt_check_every_level(Relation rel, Relation heaprel, bool heapkeyspace,
/*
* Size Bloom filter based on estimated number of tuples in index,
* while conservatively assuming that each block must contain at least
- * MaxTIDsPerBTreePage / 3 "plain" tuples -- see
+ * ClusterMaxTIDsPerBTreePage / 3 "plain" tuples -- see
* bt_posting_plain_tuple() for definition, and details of how posting
* list tuples are handled.
*/
total_pages = RelationGetNumberOfBlocks(rel);
- total_elems = Max(total_pages * (MaxTIDsPerBTreePage / 3),
+ total_elems = Max(total_pages * (ClusterMaxTIDsPerBTreePage / 3),
(int64) state->rel->rd_rel->reltuples);
/* Generate a random seed to avoid repetition */
seed = pg_prng_uint64(&pg_global_prng_state);
diff --git a/src/backend/access/nbtree/nbtdedup.c b/src/backend/access/nbtree/nbtdedup.c
index 456d86b51c..c8af6cbc2a 100644
--- a/src/backend/access/nbtree/nbtdedup.c
+++ b/src/backend/access/nbtree/nbtdedup.c
@@ -355,8 +355,8 @@ _bt_bottomupdel_pass(Relation rel, Buffer buf, Relation heapRel,
delstate.bottomup = true;
delstate.bottomupfreespace = Max(BLCKSZ / 16, newitemsz);
delstate.ndeltids = 0;
- delstate.deltids = palloc(MaxTIDsPerBTreePage * sizeof(TM_IndexDelete));
- delstate.status = palloc(MaxTIDsPerBTreePage * sizeof(TM_IndexStatus));
+ delstate.deltids = palloc(ClusterMaxTIDsPerBTreePage * sizeof(TM_IndexDelete));
+ delstate.status = palloc(ClusterMaxTIDsPerBTreePage * sizeof(TM_IndexStatus));
minoff = P_FIRSTDATAKEY(opaque);
maxoff = PageGetMaxOffsetNumber(page);
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 711b7afff3..da8352d403 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -2829,8 +2829,8 @@ _bt_simpledel_pass(Relation rel, Buffer buffer, Relation heapRel,
delstate.bottomup = false;
delstate.bottomupfreespace = 0;
delstate.ndeltids = 0;
- delstate.deltids = palloc(MaxTIDsPerBTreePage * sizeof(TM_IndexDelete));
- delstate.status = palloc(MaxTIDsPerBTreePage * sizeof(TM_IndexStatus));
+ delstate.deltids = palloc(ClusterMaxTIDsPerBTreePage * sizeof(TM_IndexDelete));
+ delstate.status = palloc(ClusterMaxTIDsPerBTreePage * sizeof(TM_IndexStatus));
for (offnum = minoff;
offnum <= maxoff;
diff --git a/src/backend/access/nbtree/nbtree.c b/src/backend/access/nbtree/nbtree.c
index 55bd14fecd..ad37c05205 100644
--- a/src/backend/access/nbtree/nbtree.c
+++ b/src/backend/access/nbtree/nbtree.c
@@ -246,8 +246,8 @@ btgettuple(IndexScanDesc scan, ScanDirection dir)
*/
if (so->killedItems == NULL)
so->killedItems = (int *)
- palloc(MaxTIDsPerBTreePage * sizeof(int));
- if (so->numKilled < MaxTIDsPerBTreePage)
+ palloc(ClusterMaxTIDsPerBTreePage * sizeof(int));
+ if (so->numKilled < ClusterMaxTIDsPerBTreePage)
so->killedItems[so->numKilled++] = so->currPos.itemIndex;
}
diff --git a/src/backend/access/nbtree/nbtsearch.c b/src/backend/access/nbtree/nbtsearch.c
index 23e723a233..1d77f1303f 100644
--- a/src/backend/access/nbtree/nbtsearch.c
+++ b/src/backend/access/nbtree/nbtsearch.c
@@ -1726,7 +1726,7 @@ _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum,
if (!continuescan)
so->currPos.moreRight = false;
- Assert(itemIndex <= MaxTIDsPerBTreePage);
+ Assert(itemIndex <= ClusterMaxTIDsPerBTreePage);
so->currPos.firstItem = 0;
so->currPos.lastItem = itemIndex - 1;
so->currPos.itemIndex = 0;
@@ -1734,7 +1734,7 @@ _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum,
else
{
/* load items[] in descending order */
- itemIndex = MaxTIDsPerBTreePage;
+ itemIndex = ClusterMaxTIDsPerBTreePage;
offnum = Min(offnum, maxoff);
@@ -1836,8 +1836,8 @@ _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum,
Assert(itemIndex >= 0);
so->currPos.firstItem = itemIndex;
- so->currPos.lastItem = MaxTIDsPerBTreePage - 1;
- so->currPos.itemIndex = MaxTIDsPerBTreePage - 1;
+ so->currPos.lastItem = ClusterMaxTIDsPerBTreePage - 1;
+ so->currPos.itemIndex = ClusterMaxTIDsPerBTreePage - 1;
}
return (so->currPos.firstItem <= so->currPos.lastItem);
diff --git a/src/include/access/nbtree.h b/src/include/access/nbtree.h
index 676fa5e0a9..568646a146 100644
--- a/src/include/access/nbtree.h
+++ b/src/include/access/nbtree.h
@@ -172,9 +172,17 @@ typedef struct BTMetaPageData
MAXALIGN(sizeof(BTPageOpaqueData))) / 3)
/*
- * MaxTIDsPerBTreePage is an upper bound on the number of heap TIDs tuples
- * that may be stored on a btree leaf page. It is used to size the
- * per-page temporary buffers.
+ * ClusterMaxTIDsPerBTreePage is a cluster-specific upper bound on the number
+ * of heap TIDs tuples that may be stored on a btree leaf page. It is used to
+ * size the per-page temporary buffers.
+ *
+ * MaxTIDsPerBTreePageLimit is the largest value that
+ * ClusterMaxTIDsPerBTreePage could be. While these currently evaluate to the
+ * same value, these are being split out so ClusterMaxTIDsPerBTreePage can
+ * become a variable instead of a constant.
+ *
+ * The CalcMaxTIDsPerBTreePage() macro is used to determine the appropriate
+ * values, given the usable page space on a given page.
*
* Note: we don't bother considering per-tuple overheads here to keep
* things simple (value is based on how many elements a single array of
@@ -182,9 +190,13 @@ 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) ((BLCKSZ - SizeOfPageHeaderData - sizeof(BTPageOpaqueData)) / \
+#define CalcMaxTIDsPerBTreePage(size) \
+ (int) (((size) - sizeof(BTPageOpaqueData)) / \
sizeof(ItemPointerData))
+#define ClusterMaxTIDsPerBTreePage \
+ CalcMaxTIDsPerBTreePage(BLCKSZ - SizeOfPageHeaderData)
+#define MaxTIDsPerBTreePageLimit \
+ CalcMaxTIDsPerBTreePage(BLCKSZ - SizeOfPageHeaderData)
/*
* The leaf-page fillfactor defaults to 90% but is user-adjustable.
@@ -982,7 +994,7 @@ typedef struct BTScanPosData
int lastItem; /* last valid index in items[] */
int itemIndex; /* current index in items[] */
- BTScanPosItem items[MaxTIDsPerBTreePage]; /* MUST BE LAST */
+ BTScanPosItem items[MaxTIDsPerBTreePageLimit]; /* MUST BE LAST */
} BTScanPosData;
typedef BTScanPosData *BTScanPos;
--
2.40.1
v2-0003-Split-MaxIndexTuplesPerPage-into-runtime-and-max-.patchapplication/octet-stream; name=v2-0003-Split-MaxIndexTuplesPerPage-into-runtime-and-max-.patchDownload
From 9921a637e370cebeee0cc5129522ceb5bddf51b0 Mon Sep 17 00:00:00 2001
From: David Christensen <david.christensen@crunchydata.com>
Date: Wed, 7 Feb 2024 10:49:31 -0500
Subject: [PATCH v2 3/4] Split MaxIndexTuplesPerPage into runtime and max
values
---
contrib/amcheck/verify_nbtree.c | 6 +++---
src/backend/access/gist/gist.c | 2 +-
src/backend/access/gist/gistget.c | 8 ++++----
src/backend/access/hash/hash.c | 4 ++--
src/backend/access/hash/hashovfl.c | 6 +++---
src/backend/access/hash/hashpage.c | 4 ++--
src/backend/access/hash/hashsearch.c | 10 +++++-----
src/backend/access/nbtree/nbtinsert.c | 2 +-
src/backend/access/nbtree/nbtpage.c | 8 ++++----
src/backend/access/nbtree/nbtree.c | 4 ++--
src/backend/access/nbtree/nbtxlog.c | 4 ++--
src/backend/access/spgist/spgdoinsert.c | 2 +-
src/backend/access/spgist/spgscan.c | 2 +-
src/backend/access/spgist/spgvacuum.c | 22 +++++++++++-----------
src/backend/storage/page/bufpage.c | 6 +++---
src/include/access/hash.h | 2 +-
src/include/access/itup.h | 23 ++++++++++++++++-------
src/include/access/nbtree.h | 2 +-
src/include/access/spgist_private.h | 12 ++++++------
19 files changed, 69 insertions(+), 60 deletions(-)
diff --git a/contrib/amcheck/verify_nbtree.c b/contrib/amcheck/verify_nbtree.c
index 1ef4cff88e..b1089693be 100644
--- a/contrib/amcheck/verify_nbtree.c
+++ b/contrib/amcheck/verify_nbtree.c
@@ -3447,12 +3447,12 @@ palloc_btree_page(BtreeCheckState *state, BlockNumber blocknum)
* to move left, in the case of backward index scans).
*/
maxoffset = PageGetMaxOffsetNumber(page);
- if (maxoffset > MaxIndexTuplesPerPage)
+ if (maxoffset > ClusterMaxIndexTuplesPerPage)
ereport(ERROR,
(errcode(ERRCODE_INDEX_CORRUPTED),
- errmsg("Number of items on block %u of index \"%s\" exceeds MaxIndexTuplesPerPage (%u)",
+ errmsg("Number of items on block %u of index \"%s\" exceeds ClusterMaxIndexTuplesPerPage (%u)",
blocknum, RelationGetRelationName(state->rel),
- MaxIndexTuplesPerPage)));
+ ClusterMaxIndexTuplesPerPage)));
if (!P_ISLEAF(opaque) && !P_ISDELETED(opaque) && maxoffset < P_FIRSTDATAKEY(opaque))
ereport(ERROR,
diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c
index ed4ffa63a7..c2f1762c36 100644
--- a/src/backend/access/gist/gist.c
+++ b/src/backend/access/gist/gist.c
@@ -1663,7 +1663,7 @@ freeGISTstate(GISTSTATE *giststate)
static void
gistprunepage(Relation rel, Page page, Buffer buffer, Relation heapRel)
{
- OffsetNumber deletable[MaxIndexTuplesPerPage];
+ OffsetNumber deletable[MaxIndexTuplesPerPageLimit];
int ndeletable = 0;
OffsetNumber offnum,
maxoff;
diff --git a/src/backend/access/gist/gistget.c b/src/backend/access/gist/gistget.c
index b35b8a9757..32443a7047 100644
--- a/src/backend/access/gist/gistget.c
+++ b/src/backend/access/gist/gistget.c
@@ -658,12 +658,12 @@ gistgettuple(IndexScanDesc scan, ScanDirection dir)
MemoryContextSwitchTo(so->giststate->scanCxt);
so->killedItems =
- (OffsetNumber *) palloc(MaxIndexTuplesPerPage
+ (OffsetNumber *) palloc(ClusterMaxIndexTuplesPerPage
* sizeof(OffsetNumber));
MemoryContextSwitchTo(oldCxt);
}
- if (so->numKilled < MaxIndexTuplesPerPage)
+ if (so->numKilled < ClusterMaxIndexTuplesPerPage)
so->killedItems[so->numKilled++] =
so->pageData[so->curPageData - 1].offnum;
}
@@ -695,12 +695,12 @@ gistgettuple(IndexScanDesc scan, ScanDirection dir)
MemoryContextSwitchTo(so->giststate->scanCxt);
so->killedItems =
- (OffsetNumber *) palloc(MaxIndexTuplesPerPage
+ (OffsetNumber *) palloc(ClusterMaxIndexTuplesPerPage
* sizeof(OffsetNumber));
MemoryContextSwitchTo(oldCxt);
}
- if (so->numKilled < MaxIndexTuplesPerPage)
+ if (so->numKilled < ClusterMaxIndexTuplesPerPage)
so->killedItems[so->numKilled++] =
so->pageData[so->curPageData - 1].offnum;
}
diff --git a/src/backend/access/hash/hash.c b/src/backend/access/hash/hash.c
index 01d06b7c32..210371ce25 100644
--- a/src/backend/access/hash/hash.c
+++ b/src/backend/access/hash/hash.c
@@ -312,9 +312,9 @@ hashgettuple(IndexScanDesc scan, ScanDirection dir)
*/
if (so->killedItems == NULL)
so->killedItems = (int *)
- palloc(MaxIndexTuplesPerPage * sizeof(int));
+ palloc(ClusterMaxIndexTuplesPerPage * sizeof(int));
- if (so->numKilled < MaxIndexTuplesPerPage)
+ if (so->numKilled < ClusterMaxIndexTuplesPerPage)
so->killedItems[so->numKilled++] = so->currPos.itemIndex;
}
diff --git a/src/backend/access/hash/hashovfl.c b/src/backend/access/hash/hashovfl.c
index c280ae885e..ec3a1a8356 100644
--- a/src/backend/access/hash/hashovfl.c
+++ b/src/backend/access/hash/hashovfl.c
@@ -903,9 +903,9 @@ _hash_squeezebucket(Relation rel,
OffsetNumber roffnum;
OffsetNumber maxroffnum;
OffsetNumber deletable[MaxOffsetNumber];
- IndexTuple itups[MaxIndexTuplesPerPage];
- Size tups_size[MaxIndexTuplesPerPage];
- OffsetNumber itup_offsets[MaxIndexTuplesPerPage];
+ IndexTuple itups[MaxIndexTuplesPerPageLimit];
+ Size tups_size[MaxIndexTuplesPerPageLimit];
+ OffsetNumber itup_offsets[MaxIndexTuplesPerPageLimit];
uint16 ndeletable = 0;
uint16 nitups = 0;
Size all_tups_size = 0;
diff --git a/src/backend/access/hash/hashpage.c b/src/backend/access/hash/hashpage.c
index d09c349e28..314c7f557b 100644
--- a/src/backend/access/hash/hashpage.c
+++ b/src/backend/access/hash/hashpage.c
@@ -1087,8 +1087,8 @@ _hash_splitbucket(Relation rel,
Page npage;
HashPageOpaque oopaque;
HashPageOpaque nopaque;
- OffsetNumber itup_offsets[MaxIndexTuplesPerPage];
- IndexTuple itups[MaxIndexTuplesPerPage];
+ OffsetNumber itup_offsets[MaxIndexTuplesPerPageLimit];
+ IndexTuple itups[MaxIndexTuplesPerPageLimit];
Size all_tups_size = 0;
int i;
uint16 nitups = 0;
diff --git a/src/backend/access/hash/hashsearch.c b/src/backend/access/hash/hashsearch.c
index 0d99d6abc8..785bd953ea 100644
--- a/src/backend/access/hash/hashsearch.c
+++ b/src/backend/access/hash/hashsearch.c
@@ -532,7 +532,7 @@ _hash_readpage(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
itemIndex = _hash_load_qualified_items(scan, page, offnum, dir);
- if (itemIndex != MaxIndexTuplesPerPage)
+ if (itemIndex != ClusterMaxIndexTuplesPerPage)
break;
/*
@@ -571,8 +571,8 @@ _hash_readpage(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
}
so->currPos.firstItem = itemIndex;
- so->currPos.lastItem = MaxIndexTuplesPerPage - 1;
- so->currPos.itemIndex = MaxIndexTuplesPerPage - 1;
+ so->currPos.lastItem = ClusterMaxIndexTuplesPerPage - 1;
+ so->currPos.itemIndex = ClusterMaxIndexTuplesPerPage - 1;
}
if (so->currPos.buf == so->hashso_bucket_buf ||
@@ -652,13 +652,13 @@ _hash_load_qualified_items(IndexScanDesc scan, Page page,
offnum = OffsetNumberNext(offnum);
}
- Assert(itemIndex <= MaxIndexTuplesPerPage);
+ Assert(itemIndex <= ClusterMaxIndexTuplesPerPage);
return itemIndex;
}
else
{
/* load items[] in descending order */
- itemIndex = MaxIndexTuplesPerPage;
+ itemIndex = ClusterMaxIndexTuplesPerPage;
while (offnum >= FirstOffsetNumber)
{
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 7e8902e48c..711b7afff3 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -2685,7 +2685,7 @@ _bt_delete_or_dedup_one_page(Relation rel, Relation heapRel,
bool simpleonly, bool checkingunique,
bool uniquedup, bool indexUnchanged)
{
- OffsetNumber deletable[MaxIndexTuplesPerPage];
+ OffsetNumber deletable[MaxIndexTuplesPerPageLimit];
int ndeletable = 0;
OffsetNumber offnum,
minoff,
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 01bbece6bf..f728b36c80 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -1160,7 +1160,7 @@ _bt_delitems_vacuum(Relation rel, Buffer buf,
bool needswal = RelationNeedsWAL(rel);
char *updatedbuf = NULL;
Size updatedbuflen = 0;
- OffsetNumber updatedoffsets[MaxIndexTuplesPerPage];
+ OffsetNumber updatedoffsets[MaxIndexTuplesPerPageLimit];
/* Shouldn't be called unless there's something to do */
Assert(ndeletable > 0 || nupdatable > 0);
@@ -1291,7 +1291,7 @@ _bt_delitems_delete(Relation rel, Buffer buf,
bool needswal = RelationNeedsWAL(rel);
char *updatedbuf = NULL;
Size updatedbuflen = 0;
- OffsetNumber updatedoffsets[MaxIndexTuplesPerPage];
+ OffsetNumber updatedoffsets[MaxIndexTuplesPerPageLimit];
/* Shouldn't be called unless there's something to do */
Assert(ndeletable > 0 || nupdatable > 0);
@@ -1519,8 +1519,8 @@ _bt_delitems_delete_check(Relation rel, Buffer buf, Relation heapRel,
OffsetNumber postingidxoffnum = InvalidOffsetNumber;
int ndeletable = 0,
nupdatable = 0;
- OffsetNumber deletable[MaxIndexTuplesPerPage];
- BTVacuumPosting updatable[MaxIndexTuplesPerPage];
+ OffsetNumber deletable[MaxIndexTuplesPerPageLimit];
+ BTVacuumPosting updatable[MaxIndexTuplesPerPageLimit];
/* Use tableam interface to determine which tuples to delete first */
snapshotConflictHorizon = table_index_delete_tuples(heapRel, delstate);
diff --git a/src/backend/access/nbtree/nbtree.c b/src/backend/access/nbtree/nbtree.c
index 41df1027d2..55bd14fecd 100644
--- a/src/backend/access/nbtree/nbtree.c
+++ b/src/backend/access/nbtree/nbtree.c
@@ -1142,9 +1142,9 @@ backtrack:
}
else if (P_ISLEAF(opaque))
{
- OffsetNumber deletable[MaxIndexTuplesPerPage];
+ OffsetNumber deletable[MaxIndexTuplesPerPageLimit];
int ndeletable;
- BTVacuumPosting updatable[MaxIndexTuplesPerPage];
+ BTVacuumPosting updatable[MaxIndexTuplesPerPageLimit];
int nupdatable;
OffsetNumber offnum,
minoff,
diff --git a/src/backend/access/nbtree/nbtxlog.c b/src/backend/access/nbtree/nbtxlog.c
index b5b0e22447..c912260d2c 100644
--- a/src/backend/access/nbtree/nbtxlog.c
+++ b/src/backend/access/nbtree/nbtxlog.c
@@ -38,8 +38,8 @@ _bt_restore_page(Page page, char *from, int len)
IndexTupleData itupdata;
Size itemsz;
char *end = from + len;
- Item items[MaxIndexTuplesPerPage];
- uint16 itemsizes[MaxIndexTuplesPerPage];
+ Item items[MaxIndexTuplesPerPageLimit];
+ uint16 itemsizes[MaxIndexTuplesPerPageLimit];
int i;
int nitems;
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index a4995c168b..3c3ede6f3a 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -134,7 +134,7 @@ spgPageIndexMultiDelete(SpGistState *state, Page page,
BlockNumber blkno, OffsetNumber offnum)
{
OffsetNumber firstItem;
- OffsetNumber sortednos[MaxIndexTuplesPerPage];
+ OffsetNumber sortednos[MaxIndexTuplesPerPageLimit];
SpGistDeadTuple tuple = NULL;
int i;
diff --git a/src/backend/access/spgist/spgscan.c b/src/backend/access/spgist/spgscan.c
index 03293a7816..5690fc4981 100644
--- a/src/backend/access/spgist/spgscan.c
+++ b/src/backend/access/spgist/spgscan.c
@@ -961,7 +961,7 @@ storeGettuple(SpGistScanOpaque so, ItemPointer heapPtr,
SpGistLeafTuple leafTuple, bool recheck,
bool recheckDistances, double *nonNullDistances)
{
- Assert(so->nPtrs < MaxIndexTuplesPerPage);
+ Assert(so->nPtrs < ClusterMaxIndexTuplesPerPage);
so->heapPtrs[so->nPtrs] = *heapPtr;
so->recheck[so->nPtrs] = recheck;
so->recheckDistances[so->nPtrs] = recheckDistances;
diff --git a/src/backend/access/spgist/spgvacuum.c b/src/backend/access/spgist/spgvacuum.c
index d2e1624924..586f367b50 100644
--- a/src/backend/access/spgist/spgvacuum.c
+++ b/src/backend/access/spgist/spgvacuum.c
@@ -127,14 +127,14 @@ vacuumLeafPage(spgBulkDeleteState *bds, Relation index, Buffer buffer,
{
Page page = BufferGetPage(buffer);
spgxlogVacuumLeaf xlrec;
- OffsetNumber toDead[MaxIndexTuplesPerPage];
- OffsetNumber toPlaceholder[MaxIndexTuplesPerPage];
- OffsetNumber moveSrc[MaxIndexTuplesPerPage];
- OffsetNumber moveDest[MaxIndexTuplesPerPage];
- OffsetNumber chainSrc[MaxIndexTuplesPerPage];
- OffsetNumber chainDest[MaxIndexTuplesPerPage];
- OffsetNumber predecessor[MaxIndexTuplesPerPage + 1];
- bool deletable[MaxIndexTuplesPerPage + 1];
+ OffsetNumber toDead[MaxIndexTuplesPerPageLimit];
+ OffsetNumber toPlaceholder[MaxIndexTuplesPerPageLimit];
+ OffsetNumber moveSrc[MaxIndexTuplesPerPageLimit];
+ OffsetNumber moveDest[MaxIndexTuplesPerPageLimit];
+ OffsetNumber chainSrc[MaxIndexTuplesPerPageLimit];
+ OffsetNumber chainDest[MaxIndexTuplesPerPageLimit];
+ OffsetNumber predecessor[MaxIndexTuplesPerPageLimit + 1];
+ bool deletable[MaxIndexTuplesPerPageLimit + 1];
int nDeletable;
OffsetNumber i,
max = PageGetMaxOffsetNumber(page);
@@ -407,7 +407,7 @@ vacuumLeafRoot(spgBulkDeleteState *bds, Relation index, Buffer buffer)
{
Page page = BufferGetPage(buffer);
spgxlogVacuumRoot xlrec;
- OffsetNumber toDelete[MaxIndexTuplesPerPage];
+ OffsetNumber toDelete[MaxIndexTuplesPerPageLimit];
OffsetNumber i,
max = PageGetMaxOffsetNumber(page);
@@ -497,8 +497,8 @@ vacuumRedirectAndPlaceholder(Relation index, Relation heaprel, Buffer buffer)
firstPlaceholder = InvalidOffsetNumber;
bool hasNonPlaceholder = false;
bool hasUpdate = false;
- OffsetNumber itemToPlaceholder[MaxIndexTuplesPerPage];
- OffsetNumber itemnos[MaxIndexTuplesPerPage];
+ OffsetNumber itemToPlaceholder[MaxIndexTuplesPerPageLimit];
+ OffsetNumber itemnos[MaxIndexTuplesPerPageLimit];
spgxlogVacuumRedirect xlrec;
GlobalVisState *vistest;
diff --git a/src/backend/storage/page/bufpage.c b/src/backend/storage/page/bufpage.c
index f78efce9ad..bc37eae9f2 100644
--- a/src/backend/storage/page/bufpage.c
+++ b/src/backend/storage/page/bufpage.c
@@ -1165,8 +1165,8 @@ PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
Offset pd_upper = phdr->pd_upper;
Offset pd_special = phdr->pd_special;
Offset last_offset;
- itemIdCompactData itemidbase[MaxIndexTuplesPerPage];
- ItemIdData newitemids[MaxIndexTuplesPerPage];
+ itemIdCompactData itemidbase[MaxIndexTuplesPerPageLimit];
+ ItemIdData newitemids[MaxIndexTuplesPerPageLimit];
itemIdCompact itemidptr;
ItemId lp;
int nline,
@@ -1178,7 +1178,7 @@ PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
OffsetNumber offnum;
bool presorted = true; /* For now */
- Assert(nitems <= MaxIndexTuplesPerPage);
+ Assert(nitems <= ClusterMaxIndexTuplesPerPage);
/*
* If there aren't very many items to delete, then retail
diff --git a/src/include/access/hash.h b/src/include/access/hash.h
index 9c7d81525b..b797872bd4 100644
--- a/src/include/access/hash.h
+++ b/src/include/access/hash.h
@@ -124,7 +124,7 @@ typedef struct HashScanPosData
int lastItem; /* last valid index in items[] */
int itemIndex; /* current index in items[] */
- HashScanPosItem items[MaxIndexTuplesPerPage]; /* MUST BE LAST */
+ HashScanPosItem items[MaxIndexTuplesPerPageLimit]; /* MUST BE LAST */
} HashScanPosData;
#define HashScanPosIsPinned(scanpos) \
diff --git a/src/include/access/itup.h b/src/include/access/itup.h
index 94885751e5..61fa8ff538 100644
--- a/src/include/access/itup.h
+++ b/src/include/access/itup.h
@@ -148,11 +148,19 @@ index_getattr(IndexTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
#endif
/*
- * MaxIndexTuplesPerPage is an upper bound on the number of tuples that can
- * fit on one index page. An index tuple must have either data or a null
- * bitmap, so we can safely assume it's at least 1 byte bigger than a bare
- * IndexTupleData struct. We arrive at the divisor because each tuple
- * must be maxaligned, and it must have an associated line pointer.
+ * ClusterMaxIndexTuplesPerPage is a cluster-specific upper bound on the
+ * number of tuples that can fit on one index page. An index tuple must have
+ * either data or a null bitmap, so we can safely assume it's at least 1 byte
+ * bigger than a bare IndexTupleData struct. We arrive at the divisor because
+ * each tuple must be maxaligned, and it must have an associated line pointer.
+ *
+ * MaxIndexTuplesPerPageLimit is the largest value that
+ * ClusterMaxIndexTuplesPerPage could be. While these currently evaluate to
+ * the same value, these are being split out so ClusterMaxIndexTuplesPerPage
+ * can become a variable instead of a constant.
+ *
+ * The CalcMaxIndexTuplesPerPage() macro is used to determine the appropriate
+ * values, given the usable page space on a given page.
*
* To be index-type-independent, this does not account for any special space
* on the page, and is thus conservative.
@@ -163,8 +171,9 @@ 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) ((BLCKSZ - SizeOfPageHeaderData) / \
+#define CalcMaxIndexTuplesPerPage(size) ((int) ((size) / \
(MAXALIGN(sizeof(IndexTupleData) + 1) + sizeof(ItemIdData))))
+#define ClusterMaxIndexTuplesPerPage CalcMaxIndexTuplesPerPage(BLCKSZ - SizeOfPageHeaderData)
+#define MaxIndexTuplesPerPageLimit CalcMaxIndexTuplesPerPage(BLCKSZ - SizeOfPageHeaderData)
#endif /* ITUP_H */
diff --git a/src/include/access/nbtree.h b/src/include/access/nbtree.h
index 6eb162052e..676fa5e0a9 100644
--- a/src/include/access/nbtree.h
+++ b/src/include/access/nbtree.h
@@ -887,7 +887,7 @@ typedef struct BTDedupStateData
* are implicitly unchanged by deduplication pass).
*/
int nintervals; /* current number of intervals in array */
- BTDedupInterval intervals[MaxIndexTuplesPerPage];
+ BTDedupInterval intervals[MaxIndexTuplesPerPageLimit];
} BTDedupStateData;
typedef BTDedupStateData *BTDedupState;
diff --git a/src/include/access/spgist_private.h b/src/include/access/spgist_private.h
index 2e9c757b30..d8ca2b7e0f 100644
--- a/src/include/access/spgist_private.h
+++ b/src/include/access/spgist_private.h
@@ -226,17 +226,17 @@ typedef struct SpGistScanOpaqueData
TupleDesc reconTupDesc; /* if so, descriptor for reconstructed tuples */
int nPtrs; /* number of TIDs found on current page */
int iPtr; /* index for scanning through same */
- ItemPointerData heapPtrs[MaxIndexTuplesPerPage]; /* TIDs from cur page */
- bool recheck[MaxIndexTuplesPerPage]; /* their recheck flags */
- bool recheckDistances[MaxIndexTuplesPerPage]; /* distance recheck
+ ItemPointerData heapPtrs[MaxIndexTuplesPerPageLimit]; /* TIDs from cur page */
+ bool recheck[MaxIndexTuplesPerPageLimit]; /* their recheck flags */
+ bool recheckDistances[MaxIndexTuplesPerPageLimit]; /* distance recheck
* flags */
- HeapTuple reconTups[MaxIndexTuplesPerPage]; /* reconstructed tuples */
+ HeapTuple reconTups[MaxIndexTuplesPerPageLimit]; /* reconstructed tuples */
/* distances (for recheck) */
- IndexOrderByDistance *distances[MaxIndexTuplesPerPage];
+ IndexOrderByDistance *distances[MaxIndexTuplesPerPageLimit];
/*
- * Note: using MaxIndexTuplesPerPage above is a bit hokey since
+ * Note: using ClusterMaxIndexTuplesPerPage above is a bit hokey since
* SpGistLeafTuples aren't exactly IndexTuples; however, they are larger,
* so this is safe.
*/
--
2.40.1
On Wed, 13 Mar 2024 at 20:42, David Christensen
<david.christensen@crunchydata.com> wrote:
Here is a version 2 of this patch, rebased atop 97d85be365.
As before, this is a cleanup/prerequisite patch series for the page
features/reserved page size patches[1]. (Said patch series is going
to be updated shortly.)This splits each of the 4 constants that care about page size into
Cluster-specific and -Limit variants, the first intended to become a
variable in time, and the second being the maximum value such a
variable may take (largely used for static
allocations).Since these patches define these symbols to have the same values they
previously had, there are no changes in functionality. These were
largely mechanical changes, and as such we should perhaps consider
making the same changes to back-branches to make it so context lines
and the like
would be the same, simplifying maintainer's efforts when applying code
in back branches that touch similar areas.The script I have to make these changes is simple, and could be run
against the back branches with only the comments surrounding Calc()
pieces needing
to be adjusted once.Thanks,
David
Hi! Your patchset needs a rebase. Are you going to push it forward?
Also, It is better to have more informative commit messages in patches.
--
Best regards,
Kirill Reshke