Skip to content

Commit e86c8ef

Browse files
Use "low key" terminology in nbtsort.c.
nbtree index builds once stashed the "minimum key" for a page, which was used as the basis of the pivot tuple that gets placed in the next level up (i.e. the tuple that stores the downlink to the page in question). It doesn't quite work that way anymore, so the "minimum key" terminology now seems misleading (these days the minimum key is actually a straight copy of the high key from the left sibling, which is a distinct thing in subtle but important ways). Rename this concept to "low key". This name is a lot clearer given that there is now a sharp distinction between pivot and non-pivot tuples. Also remove comments that describe obsolete details about how the minimum key concept used to work. Rather than generating the minus infinity item for the leftmost page on a level by copying the new item and truncating that copy, simply allocate a small buffer. The old approach confusingly created the impression that the new item had some kind of significance. This was another artifact of how things used to work before commits 8224de4 and dd299df.
1 parent c10fae2 commit e86c8ef

File tree

1 file changed

+29
-40
lines changed

1 file changed

+29
-40
lines changed

src/backend/access/nbtree/nbtsort.c

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -236,21 +236,12 @@ typedef struct BTBuildState
236236
/*
237237
* Status record for a btree page being built. We have one of these
238238
* for each active tree level.
239-
*
240-
* The reason we need to store a copy of the minimum key is that we'll
241-
* need to propagate it to the parent node when this page is linked
242-
* into its parent. However, if the page is not a leaf page, the first
243-
* entry on the page doesn't need to contain a key, so we will not have
244-
* stored the key itself on the page. (You might think we could skip
245-
* copying the minimum key on leaf pages, but actually we must have a
246-
* writable copy anyway because we'll poke the page's address into it
247-
* before passing it up to the parent...)
248239
*/
249240
typedef struct BTPageState
250241
{
251242
Page btps_page; /* workspace for page building */
252243
BlockNumber btps_blkno; /* block # to write this page at */
253-
IndexTuple btps_minkey; /* copy of minimum key (first item) on page */
244+
IndexTuple btps_lowkey; /* page's strict lower bound pivot tuple */
254245
OffsetNumber btps_lastoff; /* last item offset loaded */
255246
uint32 btps_level; /* tree level (0 = leaf) */
256247
Size btps_full; /* "full" if less than this much free space */
@@ -717,7 +708,7 @@ _bt_pagestate(BTWriteState *wstate, uint32 level)
717708
/* and assign it a page position */
718709
state->btps_blkno = wstate->btws_pages_alloced++;
719710

720-
state->btps_minkey = NULL;
711+
state->btps_lowkey = NULL;
721712
/* initialize lastoff so first item goes into P_FIRSTKEY */
722713
state->btps_lastoff = P_HIKEY;
723714
state->btps_level = level;
@@ -980,28 +971,27 @@ _bt_buildadd(BTWriteState *wstate, BTPageState *state, IndexTuple itup)
980971
}
981972

982973
/*
983-
* Link the old page into its parent, using its minimum key. If we
984-
* don't have a parent, we have to create one; this adds a new btree
985-
* level.
974+
* Link the old page into its parent, using its low key. If we don't
975+
* have a parent, we have to create one; this adds a new btree level.
986976
*/
987977
if (state->btps_next == NULL)
988978
state->btps_next = _bt_pagestate(wstate, state->btps_level + 1);
989979

990-
Assert((BTreeTupleGetNAtts(state->btps_minkey, wstate->index) <=
980+
Assert((BTreeTupleGetNAtts(state->btps_lowkey, wstate->index) <=
991981
IndexRelationGetNumberOfKeyAttributes(wstate->index) &&
992-
BTreeTupleGetNAtts(state->btps_minkey, wstate->index) > 0) ||
982+
BTreeTupleGetNAtts(state->btps_lowkey, wstate->index) > 0) ||
993983
P_LEFTMOST((BTPageOpaque) PageGetSpecialPointer(opage)));
994-
Assert(BTreeTupleGetNAtts(state->btps_minkey, wstate->index) == 0 ||
984+
Assert(BTreeTupleGetNAtts(state->btps_lowkey, wstate->index) == 0 ||
995985
!P_LEFTMOST((BTPageOpaque) PageGetSpecialPointer(opage)));
996-
BTreeInnerTupleSetDownLink(state->btps_minkey, oblkno);
997-
_bt_buildadd(wstate, state->btps_next, state->btps_minkey);
998-
pfree(state->btps_minkey);
986+
BTreeInnerTupleSetDownLink(state->btps_lowkey, oblkno);
987+
_bt_buildadd(wstate, state->btps_next, state->btps_lowkey);
988+
pfree(state->btps_lowkey);
999989

1000990
/*
1001-
* Save a copy of the high key from the old page. It is also used as
1002-
* the minimum key for the new page.
991+
* Save a copy of the high key from the old page. It is also the low
992+
* key for the new page.
1003993
*/
1004-
state->btps_minkey = CopyIndexTuple(oitup);
994+
state->btps_lowkey = CopyIndexTuple(oitup);
1005995

1006996
/*
1007997
* Set the sibling links for both pages.
@@ -1032,18 +1022,17 @@ _bt_buildadd(BTWriteState *wstate, BTPageState *state, IndexTuple itup)
10321022
* was created that became the current page. Either way, the current page
10331023
* definitely has space for new item.
10341024
*
1035-
* If the new item is the first for its page, stash a copy for later. Note
1036-
* this will only happen for the first item on a level; on later pages,
1037-
* the first item for a page is copied from the prior page in the code
1038-
* above. The minimum key for an entire level is nothing more than a
1039-
* minus infinity (downlink only) pivot tuple placeholder.
1025+
* If the new item is the first for its page, it must also be the first
1026+
* item on its entire level. On later same-level pages, a low key for a
1027+
* page will be copied from the prior page in the code above. Generate a
1028+
* minus infinity low key here instead.
10401029
*/
10411030
if (last_off == P_HIKEY)
10421031
{
1043-
Assert(state->btps_minkey == NULL);
1044-
state->btps_minkey = CopyIndexTuple(itup);
1045-
/* _bt_sortaddtup() will perform full truncation later */
1046-
BTreeTupleSetNAtts(state->btps_minkey, 0);
1032+
Assert(state->btps_lowkey == NULL);
1033+
state->btps_lowkey = palloc0(sizeof(IndexTupleData));
1034+
state->btps_lowkey->t_info = sizeof(IndexTupleData);
1035+
BTreeTupleSetNAtts(state->btps_lowkey, 0);
10471036
}
10481037

10491038
/*
@@ -1083,7 +1072,7 @@ _bt_uppershutdown(BTWriteState *wstate, BTPageState *state)
10831072
* We have to link the last page on this level to somewhere.
10841073
*
10851074
* If we're at the top, it's the root, so attach it to the metapage.
1086-
* Otherwise, add an entry for it to its parent using its minimum key.
1075+
* Otherwise, add an entry for it to its parent using its low key.
10871076
* This may cause the last page of the parent level to split, but
10881077
* that's not a problem -- we haven't gotten to it yet.
10891078
*/
@@ -1095,16 +1084,16 @@ _bt_uppershutdown(BTWriteState *wstate, BTPageState *state)
10951084
}
10961085
else
10971086
{
1098-
Assert((BTreeTupleGetNAtts(s->btps_minkey, wstate->index) <=
1087+
Assert((BTreeTupleGetNAtts(s->btps_lowkey, wstate->index) <=
10991088
IndexRelationGetNumberOfKeyAttributes(wstate->index) &&
1100-
BTreeTupleGetNAtts(s->btps_minkey, wstate->index) > 0) ||
1089+
BTreeTupleGetNAtts(s->btps_lowkey, wstate->index) > 0) ||
11011090
P_LEFTMOST(opaque));
1102-
Assert(BTreeTupleGetNAtts(s->btps_minkey, wstate->index) == 0 ||
1091+
Assert(BTreeTupleGetNAtts(s->btps_lowkey, wstate->index) == 0 ||
11031092
!P_LEFTMOST(opaque));
1104-
BTreeInnerTupleSetDownLink(s->btps_minkey, blkno);
1105-
_bt_buildadd(wstate, s->btps_next, s->btps_minkey);
1106-
pfree(s->btps_minkey);
1107-
s->btps_minkey = NULL;
1093+
BTreeInnerTupleSetDownLink(s->btps_lowkey, blkno);
1094+
_bt_buildadd(wstate, s->btps_next, s->btps_lowkey);
1095+
pfree(s->btps_lowkey);
1096+
s->btps_lowkey = NULL;
11081097
}
11091098

11101099
/*

0 commit comments

Comments
 (0)