Commit 013c1f6a authored by Peter Geoghegan's avatar Peter Geoghegan

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.
parent fc34b0d9
...@@ -44,6 +44,7 @@ static void _bt_insertonpg(Relation rel, BTScanInsert itup_key, ...@@ -44,6 +44,7 @@ static void _bt_insertonpg(Relation rel, BTScanInsert itup_key,
Buffer cbuf, Buffer cbuf,
BTStack stack, BTStack stack,
IndexTuple itup, IndexTuple itup,
Size itemsz,
OffsetNumber newitemoff, OffsetNumber newitemoff,
int postingoff, int postingoff,
bool split_only_page); bool split_only_page);
...@@ -118,10 +119,18 @@ _bt_doinsert(Relation rel, IndexTuple itup, ...@@ -118,10 +119,18 @@ _bt_doinsert(Relation rel, IndexTuple itup,
/* /*
* Fill in the BTInsertState working area, to track the current page and * Fill in the BTInsertState working area, to track the current page and
* position within the page to insert on * position within the page to insert on.
*
* Note that itemsz is passed down to lower level code that deals with
* inserting the item. It must be MAXALIGN()'d. This ensures that space
* accounting code consistently considers the alignment overhead that we
* expect PageAddItem() will add later. (Actually, index_form_tuple() is
* already conservative about alignment, but we don't rely on that from
* this distance. Besides, preserving the "true" tuple size in index
* tuple headers for the benefit of nbtsplitloc.c might happen someday.
* Note that heapam does not MAXALIGN() each heap tuple's lp_len field.)
*/ */
insertstate.itup = itup; insertstate.itup = itup;
/* PageAddItem will MAXALIGN(), but be consistent */
insertstate.itemsz = MAXALIGN(IndexTupleSize(itup)); insertstate.itemsz = MAXALIGN(IndexTupleSize(itup));
insertstate.itup_key = itup_key; insertstate.itup_key = itup_key;
insertstate.bounds_valid = false; insertstate.bounds_valid = false;
...@@ -299,7 +308,8 @@ top: ...@@ -299,7 +308,8 @@ top:
newitemoff = _bt_findinsertloc(rel, &insertstate, checkingunique, newitemoff = _bt_findinsertloc(rel, &insertstate, checkingunique,
stack, heapRel); stack, heapRel);
_bt_insertonpg(rel, itup_key, insertstate.buf, InvalidBuffer, stack, _bt_insertonpg(rel, itup_key, insertstate.buf, InvalidBuffer, stack,
itup, newitemoff, insertstate.postingoff, false); itup, insertstate.itemsz, newitemoff,
insertstate.postingoff, false);
} }
else else
{ {
...@@ -1058,13 +1068,13 @@ _bt_insertonpg(Relation rel, ...@@ -1058,13 +1068,13 @@ _bt_insertonpg(Relation rel,
Buffer cbuf, Buffer cbuf,
BTStack stack, BTStack stack,
IndexTuple itup, IndexTuple itup,
Size itemsz,
OffsetNumber newitemoff, OffsetNumber newitemoff,
int postingoff, int postingoff,
bool split_only_page) bool split_only_page)
{ {
Page page; Page page;
BTPageOpaque lpageop; BTPageOpaque lpageop;
Size itemsz;
IndexTuple oposting = NULL; IndexTuple oposting = NULL;
IndexTuple origitup = NULL; IndexTuple origitup = NULL;
IndexTuple nposting = NULL; IndexTuple nposting = NULL;
...@@ -1082,6 +1092,7 @@ _bt_insertonpg(Relation rel, ...@@ -1082,6 +1092,7 @@ _bt_insertonpg(Relation rel,
BTreeTupleGetNAtts(itup, rel) <= BTreeTupleGetNAtts(itup, rel) <=
IndexRelationGetNumberOfKeyAttributes(rel)); IndexRelationGetNumberOfKeyAttributes(rel));
Assert(!BTreeTupleIsPosting(itup)); Assert(!BTreeTupleIsPosting(itup));
Assert(MAXALIGN(IndexTupleSize(itup)) == itemsz);
/* /*
* Every internal page should have exactly one negative infinity item at * Every internal page should have exactly one negative infinity item at
...@@ -1096,10 +1107,6 @@ _bt_insertonpg(Relation rel, ...@@ -1096,10 +1107,6 @@ _bt_insertonpg(Relation rel,
elog(ERROR, "cannot insert to incompletely split page %u", elog(ERROR, "cannot insert to incompletely split page %u",
BufferGetBlockNumber(buf)); BufferGetBlockNumber(buf));
itemsz = IndexTupleSize(itup);
itemsz = MAXALIGN(itemsz); /* be safe, PageAddItem will do this but we
* need to be consistent */
/* /*
* Do we need to split an existing posting list item? * Do we need to split an existing posting list item?
*/ */
...@@ -2103,8 +2110,8 @@ _bt_insert_parent(Relation rel, ...@@ -2103,8 +2110,8 @@ _bt_insert_parent(Relation rel,
/* Recursively insert into the parent */ /* Recursively insert into the parent */
_bt_insertonpg(rel, NULL, pbuf, buf, stack->bts_parent, _bt_insertonpg(rel, NULL, pbuf, buf, stack->bts_parent,
new_item, stack->bts_offset + 1, 0, new_item, MAXALIGN(IndexTupleSize(new_item)),
is_only); stack->bts_offset + 1, 0, is_only);
/* be tidy */ /* be tidy */
pfree(new_item); pfree(new_item);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment