Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 28c16f4

Browse files
Remove unnecessary PageIsEmpty() nbtree build check.
nbtree index builds cannot write out an empty page. That would mean that there was no way to create a pivot tuple pointing to the page one level up, since _bt_truncate() generates one based on page's firstright tuple. Replace the unnecessary PageIsEmpty() check with an assertion that checks that the page has space for at least two line pointers (the would-be high key line pointer, plus at least one valid "data item" tuple line pointer). The PageIsEmpty() check was added by commit 5d9f146 over 20 years ago. It looks like it has always been unnecessary.
1 parent f7f70d5 commit 28c16f4

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/backend/access/nbtree/nbtsort.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ static void _bt_build_callback(Relation index, ItemPointer tid, Datum *values,
267267
bool *isnull, bool tupleIsAlive, void *state);
268268
static Page _bt_blnewpage(uint32 level);
269269
static BTPageState *_bt_pagestate(BTWriteState *wstate, uint32 level);
270-
static void _bt_slideleft(Page page);
270+
static void _bt_slideleft(Page rightmostpage);
271271
static void _bt_sortaddtup(Page page, Size itemsize,
272272
IndexTuple itup, OffsetNumber itup_off,
273273
bool newfirstdataitem);
@@ -721,31 +721,32 @@ _bt_pagestate(BTWriteState *wstate, uint32 level)
721721
}
722722

723723
/*
724-
* slide an array of ItemIds back one slot (from P_FIRSTKEY to
725-
* P_HIKEY, overwriting P_HIKEY). we need to do this when we discover
726-
* that we have built an ItemId array in what has turned out to be a
727-
* P_RIGHTMOST page.
724+
* Slide the array of ItemIds from the page back one slot (from P_FIRSTKEY to
725+
* P_HIKEY, overwriting P_HIKEY).
726+
*
727+
* _bt_blnewpage() makes the P_HIKEY line pointer appear allocated, but the
728+
* rightmost page on its level is not supposed to get a high key. Now that
729+
* it's clear that this page is a rightmost page, remove the unneeded empty
730+
* P_HIKEY line pointer space.
728731
*/
729732
static void
730-
_bt_slideleft(Page page)
733+
_bt_slideleft(Page rightmostpage)
731734
{
732735
OffsetNumber off;
733736
OffsetNumber maxoff;
734737
ItemId previi;
735-
ItemId thisii;
736738

737-
if (!PageIsEmpty(page))
739+
maxoff = PageGetMaxOffsetNumber(rightmostpage);
740+
Assert(maxoff >= P_FIRSTKEY);
741+
previi = PageGetItemId(rightmostpage, P_HIKEY);
742+
for (off = P_FIRSTKEY; off <= maxoff; off = OffsetNumberNext(off))
738743
{
739-
maxoff = PageGetMaxOffsetNumber(page);
740-
previi = PageGetItemId(page, P_HIKEY);
741-
for (off = P_FIRSTKEY; off <= maxoff; off = OffsetNumberNext(off))
742-
{
743-
thisii = PageGetItemId(page, off);
744-
*previi = *thisii;
745-
previi = thisii;
746-
}
747-
((PageHeader) page)->pd_lower -= sizeof(ItemIdData);
744+
ItemId thisii = PageGetItemId(rightmostpage, off);
745+
746+
*previi = *thisii;
747+
previi = thisii;
748748
}
749+
((PageHeader) rightmostpage)->pd_lower -= sizeof(ItemIdData);
749750
}
750751

751752
/*

0 commit comments

Comments
 (0)