Skip to content

Commit c389760

Browse files
committed
Remove the no-longer-useful BTItem/BTItemData level of structure, and
just refer to btree index entries as plain IndexTuples, which is what they have been for a very long time. This is mostly just an exercise in removing extraneous notation, but it does save a palloc/pfree cycle per index insertion.
1 parent 9b01231 commit c389760

File tree

8 files changed

+180
-263
lines changed

8 files changed

+180
-263
lines changed

src/backend/access/nbtree/nbtinsert.c

Lines changed: 65 additions & 72 deletions
Large diffs are not rendered by default.

src/backend/access/nbtree/nbtpage.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtpage.c,v 1.91 2006/01/17 00:09:01 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtpage.c,v 1.92 2006/01/25 23:04:20 tgl Exp $
1313
*
1414
* NOTES
1515
* Postgres btree pages look like ordinary relation pages. The opaque
@@ -766,8 +766,8 @@ _bt_pagedel(Relation rel, Buffer buf, bool vacuum_full)
766766
uint32 targetlevel,
767767
ilevel;
768768
ItemId itemid;
769-
BTItem targetkey,
770-
btitem;
769+
IndexTuple targetkey,
770+
itup;
771771
ScanKey itup_scankey;
772772
BTStack stack;
773773
Buffer lbuf,
@@ -803,7 +803,7 @@ _bt_pagedel(Relation rel, Buffer buf, bool vacuum_full)
803803
targetlevel = opaque->btpo.level;
804804
leftsib = opaque->btpo_prev;
805805
itemid = PageGetItemId(page, P_HIKEY);
806-
targetkey = CopyBTItem((BTItem) PageGetItem(page, itemid));
806+
targetkey = CopyIndexTuple((IndexTuple) PageGetItem(page, itemid));
807807

808808
/*
809809
* We need to get an approximate pointer to the page's parent page. Use
@@ -814,7 +814,7 @@ _bt_pagedel(Relation rel, Buffer buf, bool vacuum_full)
814814
*/
815815
_bt_relbuf(rel, buf);
816816
/* we need an insertion scan key to do our search, so build one */
817-
itup_scankey = _bt_mkscankey(rel, &(targetkey->bti_itup));
817+
itup_scankey = _bt_mkscankey(rel, targetkey);
818818
/* find the leftmost leaf page containing this key */
819819
stack = _bt_search(rel, rel->rd_rel->relnatts, itup_scankey, false,
820820
&lbuf, BT_READ);
@@ -908,8 +908,7 @@ _bt_pagedel(Relation rel, Buffer buf, bool vacuum_full)
908908
* Next find and write-lock the current parent of the target page. This is
909909
* essentially the same as the corresponding step of splitting.
910910
*/
911-
ItemPointerSet(&(stack->bts_btitem.bti_itup.t_tid),
912-
target, P_HIKEY);
911+
ItemPointerSet(&(stack->bts_btentry.t_tid), target, P_HIKEY);
913912
pbuf = _bt_getstackbuf(rel, stack, BT_WRITE);
914913
if (pbuf == InvalidBuffer)
915914
elog(ERROR, "failed to re-find parent key in \"%s\"",
@@ -1008,15 +1007,15 @@ _bt_pagedel(Relation rel, Buffer buf, bool vacuum_full)
10081007
OffsetNumber nextoffset;
10091008

10101009
itemid = PageGetItemId(page, poffset);
1011-
btitem = (BTItem) PageGetItem(page, itemid);
1012-
Assert(ItemPointerGetBlockNumber(&(btitem->bti_itup.t_tid)) == target);
1013-
ItemPointerSet(&(btitem->bti_itup.t_tid), rightsib, P_HIKEY);
1010+
itup = (IndexTuple) PageGetItem(page, itemid);
1011+
Assert(ItemPointerGetBlockNumber(&(itup->t_tid)) == target);
1012+
ItemPointerSet(&(itup->t_tid), rightsib, P_HIKEY);
10141013

10151014
nextoffset = OffsetNumberNext(poffset);
10161015
/* This part is just for double-checking */
10171016
itemid = PageGetItemId(page, nextoffset);
1018-
btitem = (BTItem) PageGetItem(page, itemid);
1019-
if (ItemPointerGetBlockNumber(&(btitem->bti_itup.t_tid)) != rightsib)
1017+
itup = (IndexTuple) PageGetItem(page, itemid);
1018+
if (ItemPointerGetBlockNumber(&(itup->t_tid)) != rightsib)
10201019
elog(PANIC, "right sibling is not next child in \"%s\"",
10211020
RelationGetRelationName(rel));
10221021
PageIndexTupleDelete(page, nextoffset);

src/backend/access/nbtree/nbtree.c

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1994, Regents of the University of California
1313
*
1414
* IDENTIFICATION
15-
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtree.c,v 1.135 2005/12/07 19:37:53 tgl Exp $
15+
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtree.c,v 1.136 2006/01/25 23:04:20 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -167,38 +167,34 @@ btbuildCallback(Relation index,
167167
{
168168
BTBuildState *buildstate = (BTBuildState *) state;
169169
IndexTuple itup;
170-
BTItem btitem;
171170

172171
/* form an index tuple and point it at the heap tuple */
173172
itup = index_form_tuple(RelationGetDescr(index), values, isnull);
174173
itup->t_tid = htup->t_self;
175174

176-
btitem = _bt_formitem(itup);
177-
178175
/*
179176
* if we are doing bottom-up btree build, we insert the index into a spool
180177
* file for subsequent processing. otherwise, we insert into the btree.
181178
*/
182179
if (buildstate->usefast)
183180
{
184181
if (tupleIsAlive || buildstate->spool2 == NULL)
185-
_bt_spool(btitem, buildstate->spool);
182+
_bt_spool(itup, buildstate->spool);
186183
else
187184
{
188185
/* dead tuples are put into spool2 */
189186
buildstate->haveDead = true;
190-
_bt_spool(btitem, buildstate->spool2);
187+
_bt_spool(itup, buildstate->spool2);
191188
}
192189
}
193190
else
194191
{
195-
_bt_doinsert(index, btitem,
192+
_bt_doinsert(index, itup,
196193
buildstate->isUnique, buildstate->heapRel);
197194
}
198195

199196
buildstate->indtuples += 1;
200197

201-
pfree(btitem);
202198
pfree(itup);
203199
}
204200

@@ -217,17 +213,14 @@ btinsert(PG_FUNCTION_ARGS)
217213
ItemPointer ht_ctid = (ItemPointer) PG_GETARG_POINTER(3);
218214
Relation heapRel = (Relation) PG_GETARG_POINTER(4);
219215
bool checkUnique = PG_GETARG_BOOL(5);
220-
BTItem btitem;
221216
IndexTuple itup;
222217

223218
/* generate an index tuple */
224219
itup = index_form_tuple(RelationGetDescr(rel), values, isnull);
225220
itup->t_tid = *ht_ctid;
226-
btitem = _bt_formitem(itup);
227221

228-
_bt_doinsert(rel, btitem, checkUnique, heapRel);
222+
_bt_doinsert(rel, itup, checkUnique, heapRel);
229223

230-
pfree(btitem);
231224
pfree(itup);
232225

233226
PG_RETURN_BOOL(true);
@@ -616,12 +609,12 @@ btbulkdelete(PG_FUNCTION_ARGS)
616609
offnum <= maxoff;
617610
offnum = OffsetNumberNext(offnum))
618611
{
619-
BTItem btitem;
612+
IndexTuple itup;
620613
ItemPointer htup;
621614

622-
btitem = (BTItem) PageGetItem(page,
623-
PageGetItemId(page, offnum));
624-
htup = &(btitem->bti_itup.t_tid);
615+
itup = (IndexTuple)
616+
PageGetItem(page, PageGetItemId(page, offnum));
617+
htup = &(itup->t_tid);
625618
if (callback(htup, callback_state))
626619
{
627620
deletable[ndeletable++] = offnum;
@@ -872,7 +865,7 @@ _bt_restscan(IndexScanDesc scan)
872865
BTPageOpaque opaque;
873866
Buffer nextbuf;
874867
ItemPointer target = &(so->curHeapIptr);
875-
BTItem item;
868+
IndexTuple itup;
876869
BlockNumber blkno;
877870

878871
/*
@@ -909,8 +902,8 @@ _bt_restscan(IndexScanDesc scan)
909902
offnum <= maxoff;
910903
offnum = OffsetNumberNext(offnum))
911904
{
912-
item = (BTItem) PageGetItem(page, PageGetItemId(page, offnum));
913-
if (BTTidSame(item->bti_itup.t_tid, *target))
905+
itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
906+
if (BTTidSame(itup->t_tid, *target))
914907
{
915908
/* Found it */
916909
current->ip_posid = offnum;

src/backend/access/nbtree/nbtsearch.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.102 2006/01/25 20:29:23 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.103 2006/01/25 23:04:20 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -65,7 +65,6 @@ _bt_search(Relation rel, int keysz, ScanKey scankey, bool nextkey,
6565
BTPageOpaque opaque;
6666
OffsetNumber offnum;
6767
ItemId itemid;
68-
BTItem btitem;
6968
IndexTuple itup;
7069
BlockNumber blkno;
7170
BlockNumber par_blkno;
@@ -90,8 +89,7 @@ _bt_search(Relation rel, int keysz, ScanKey scankey, bool nextkey,
9089
*/
9190
offnum = _bt_binsrch(rel, *bufP, keysz, scankey, nextkey);
9291
itemid = PageGetItemId(page, offnum);
93-
btitem = (BTItem) PageGetItem(page, itemid);
94-
itup = &(btitem->bti_itup);
92+
itup = (IndexTuple) PageGetItem(page, itemid);
9593
blkno = ItemPointerGetBlockNumber(&(itup->t_tid));
9694
par_blkno = BufferGetBlockNumber(*bufP);
9795

@@ -108,7 +106,7 @@ _bt_search(Relation rel, int keysz, ScanKey scankey, bool nextkey,
108106
new_stack = (BTStack) palloc(sizeof(BTStackData));
109107
new_stack->bts_blkno = par_blkno;
110108
new_stack->bts_offset = offnum;
111-
memcpy(&new_stack->bts_btitem, btitem, sizeof(BTItemData));
109+
memcpy(&new_stack->bts_btentry, itup, sizeof(IndexTupleData));
112110
new_stack->bts_parent = stack_in;
113111

114112
/* drop the read lock on the parent page, acquire one on the child */
@@ -338,7 +336,6 @@ _bt_compare(Relation rel,
338336
{
339337
TupleDesc itupdesc = RelationGetDescr(rel);
340338
BTPageOpaque opaque = (BTPageOpaque) PageGetSpecialPointer(page);
341-
BTItem btitem;
342339
IndexTuple itup;
343340
int i;
344341

@@ -349,8 +346,7 @@ _bt_compare(Relation rel,
349346
if (!P_ISLEAF(opaque) && offnum == P_FIRSTDATAKEY(opaque))
350347
return 1;
351348

352-
btitem = (BTItem) PageGetItem(page, PageGetItemId(page, offnum));
353-
itup = &(btitem->bti_itup);
349+
itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
354350

355351
/*
356352
* The scan key is set up with the attribute number associated with each
@@ -1189,7 +1185,6 @@ _bt_get_endpoint(Relation rel, uint32 level, bool rightmost)
11891185
BTPageOpaque opaque;
11901186
OffsetNumber offnum;
11911187
BlockNumber blkno;
1192-
BTItem btitem;
11931188
IndexTuple itup;
11941189

11951190
/*
@@ -1243,8 +1238,7 @@ _bt_get_endpoint(Relation rel, uint32 level, bool rightmost)
12431238
else
12441239
offnum = P_FIRSTDATAKEY(opaque);
12451240

1246-
btitem = (BTItem) PageGetItem(page, PageGetItemId(page, offnum));
1247-
itup = &(btitem->bti_itup);
1241+
itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
12481242
blkno = ItemPointerGetBlockNumber(&(itup->t_tid));
12491243

12501244
buf = _bt_relandgetbuf(rel, buf, blkno, BT_READ);

0 commit comments

Comments
 (0)