Skip to content

Commit 013c1f6

Browse files
nbtree: Pass down MAXALIGN()'d itemsz for new item.
Refactor nbtinsert.c so that the final itemsz of each new non-pivot tuple (the MAXALIGN()'d size) is determined once. Most of the functions used by leaf page inserts used the insertstate.itemsz value already. This commit makes everything use insertstate.itemsz as standard practice. The goal is to decouple tuple size from "effective" tuple size. Making this distinction isn't truly necessary right now, but that might change in the future. Also explain why we consistently apply MAXALIGN() to get an effective index tuple size. This was rather unclear, in part because it isn't actually strictly necessary right now.
1 parent fc34b0d commit 013c1f6

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/backend/access/nbtree/nbtinsert.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static void _bt_insertonpg(Relation rel, BTScanInsert itup_key,
4444
Buffer cbuf,
4545
BTStack stack,
4646
IndexTuple itup,
47+
Size itemsz,
4748
OffsetNumber newitemoff,
4849
int postingoff,
4950
bool split_only_page);
@@ -118,10 +119,18 @@ _bt_doinsert(Relation rel, IndexTuple itup,
118119

119120
/*
120121
* Fill in the BTInsertState working area, to track the current page and
121-
* position within the page to insert on
122+
* position within the page to insert on.
123+
*
124+
* Note that itemsz is passed down to lower level code that deals with
125+
* inserting the item. It must be MAXALIGN()'d. This ensures that space
126+
* accounting code consistently considers the alignment overhead that we
127+
* expect PageAddItem() will add later. (Actually, index_form_tuple() is
128+
* already conservative about alignment, but we don't rely on that from
129+
* this distance. Besides, preserving the "true" tuple size in index
130+
* tuple headers for the benefit of nbtsplitloc.c might happen someday.
131+
* Note that heapam does not MAXALIGN() each heap tuple's lp_len field.)
122132
*/
123133
insertstate.itup = itup;
124-
/* PageAddItem will MAXALIGN(), but be consistent */
125134
insertstate.itemsz = MAXALIGN(IndexTupleSize(itup));
126135
insertstate.itup_key = itup_key;
127136
insertstate.bounds_valid = false;
@@ -299,7 +308,8 @@ _bt_doinsert(Relation rel, IndexTuple itup,
299308
newitemoff = _bt_findinsertloc(rel, &insertstate, checkingunique,
300309
stack, heapRel);
301310
_bt_insertonpg(rel, itup_key, insertstate.buf, InvalidBuffer, stack,
302-
itup, newitemoff, insertstate.postingoff, false);
311+
itup, insertstate.itemsz, newitemoff,
312+
insertstate.postingoff, false);
303313
}
304314
else
305315
{
@@ -1058,13 +1068,13 @@ _bt_insertonpg(Relation rel,
10581068
Buffer cbuf,
10591069
BTStack stack,
10601070
IndexTuple itup,
1071+
Size itemsz,
10611072
OffsetNumber newitemoff,
10621073
int postingoff,
10631074
bool split_only_page)
10641075
{
10651076
Page page;
10661077
BTPageOpaque lpageop;
1067-
Size itemsz;
10681078
IndexTuple oposting = NULL;
10691079
IndexTuple origitup = NULL;
10701080
IndexTuple nposting = NULL;
@@ -1082,6 +1092,7 @@ _bt_insertonpg(Relation rel,
10821092
BTreeTupleGetNAtts(itup, rel) <=
10831093
IndexRelationGetNumberOfKeyAttributes(rel));
10841094
Assert(!BTreeTupleIsPosting(itup));
1095+
Assert(MAXALIGN(IndexTupleSize(itup)) == itemsz);
10851096

10861097
/*
10871098
* Every internal page should have exactly one negative infinity item at
@@ -1096,10 +1107,6 @@ _bt_insertonpg(Relation rel,
10961107
elog(ERROR, "cannot insert to incompletely split page %u",
10971108
BufferGetBlockNumber(buf));
10981109

1099-
itemsz = IndexTupleSize(itup);
1100-
itemsz = MAXALIGN(itemsz); /* be safe, PageAddItem will do this but we
1101-
* need to be consistent */
1102-
11031110
/*
11041111
* Do we need to split an existing posting list item?
11051112
*/
@@ -2103,8 +2110,8 @@ _bt_insert_parent(Relation rel,
21032110

21042111
/* Recursively insert into the parent */
21052112
_bt_insertonpg(rel, NULL, pbuf, buf, stack->bts_parent,
2106-
new_item, stack->bts_offset + 1, 0,
2107-
is_only);
2113+
new_item, MAXALIGN(IndexTupleSize(new_item)),
2114+
stack->bts_offset + 1, 0, is_only);
21082115

21092116
/* be tidy */
21102117
pfree(new_item);

0 commit comments

Comments
 (0)