📄 nbtsort.c
字号:
/*------------------------------------------------------------------------- * * nbtsort.c * Build a btree from sorted input by loading leaf pages sequentially. * * NOTES * * We use tuplesort.c to sort the given index tuples into order. * Then we scan the index tuples in order and build the btree pages * for each level. We load source tuples into leaf-level pages. * Whenever we fill a page at one level, we add a link to it to its * parent level (starting a new parent level if necessary). When * done, we write out each final page on each level, adding it to * its parent level. When we have only one page on a level, it must be * the root -- it can be attached to the btree metapage and we are done. * * This code is moderately slow (~10% slower) compared to the regular * btree (insertion) build code on sorted or well-clustered data. On * random data, however, the insertion build code is unusable -- the * difference on a 60MB heap is a factor of 15 because the random * probes into the btree thrash the buffer pool. (NOTE: the above * "10%" estimate is probably obsolete, since it refers to an old and * not very good external sort implementation that used to exist in * this module. tuplesort.c is almost certainly faster.) * * It is not wise to pack the pages entirely full, since then *any* * insertion would cause a split (and not only of the leaf page; the need * for a split would cascade right up the tree). The steady-state load * factor for btrees is usually estimated at 70%. We choose to pack leaf * pages to 90% and upper pages to 70%. This gives us reasonable density * (there aren't many upper pages if the keys are reasonable-size) without * incurring a lot of cascading splits during early insertions. * * * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION * $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtsort.c,v 1.77 2003/09/29 23:40:26 tgl Exp $ * *------------------------------------------------------------------------- */#include "postgres.h"#include "access/nbtree.h"#include "miscadmin.h"#include "utils/tuplesort.h"/* * Status record for spooling. */struct BTSpool{ Tuplesortstate *sortstate; /* state data for tuplesort.c */ Relation index; bool isunique;};/* * Status record for a btree page being built. We have one of these * for each active tree level. * * The reason we need to store a copy of the minimum key is that we'll * need to propagate it to the parent node when this page is linked * into its parent. However, if the page is not a leaf page, the first * entry on the page doesn't need to contain a key, so we will not have * stored the key itself on the page. (You might think we could skip * copying the minimum key on leaf pages, but actually we must have a * writable copy anyway because we'll poke the page's address into it * before passing it up to the parent...) */typedef struct BTPageState{ Buffer btps_buf; /* current buffer & page */ Page btps_page; BTItem btps_minkey; /* copy of minimum key (first item) on * page */ OffsetNumber btps_lastoff; /* last item offset loaded */ uint32 btps_level; /* tree level (0 = leaf) */ Size btps_full; /* "full" if less than this much free * space */ struct BTPageState *btps_next; /* link to parent level, if any */} BTPageState;#define BTITEMSZ(btitem) \ ((btitem) ? \ (IndexTupleDSize((btitem)->bti_itup) + \ (sizeof(BTItemData) - sizeof(IndexTupleData))) : \ 0)static void _bt_blnewpage(Relation index, Buffer *buf, Page *page, uint32 level);static BTPageState *_bt_pagestate(Relation index, uint32 level);static void _bt_slideleft(Relation index, Buffer buf, Page page);static void _bt_sortaddtup(Page page, Size itemsize, BTItem btitem, OffsetNumber itup_off);static void _bt_buildadd(Relation index, BTPageState *state, BTItem bti);static void _bt_uppershutdown(Relation index, BTPageState *state);static void _bt_load(Relation index, BTSpool *btspool, BTSpool *btspool2);/* * Interface routines *//* * create and initialize a spool structure */BTSpool *_bt_spoolinit(Relation index, bool isunique){ BTSpool *btspool = (BTSpool *) palloc0(sizeof(BTSpool)); btspool->index = index; btspool->isunique = isunique; btspool->sortstate = tuplesort_begin_index(index, isunique, false); /* * Currently, tuplesort provides sort functions on IndexTuples. If we * kept anything in a BTItem other than a regular IndexTuple, we'd * need to modify tuplesort to understand BTItems as such. */ Assert(sizeof(BTItemData) == sizeof(IndexTupleData)); return btspool;}/* * clean up a spool structure and its substructures. */void_bt_spooldestroy(BTSpool *btspool){ tuplesort_end(btspool->sortstate); pfree((void *) btspool);}/* * spool a btitem into the sort file. */void_bt_spool(BTItem btitem, BTSpool *btspool){ /* A BTItem is really just an IndexTuple */ tuplesort_puttuple(btspool->sortstate, (void *) btitem);}/* * given a spool loaded by successive calls to _bt_spool, * create an entire btree. */void_bt_leafbuild(BTSpool *btspool, BTSpool *btspool2){#ifdef BTREE_BUILD_STATS if (log_btree_build_stats) { ShowUsage("BTREE BUILD (Spool) STATISTICS"); ResetUsage(); }#endif /* BTREE_BUILD_STATS */ tuplesort_performsort(btspool->sortstate); if (btspool2) tuplesort_performsort(btspool2->sortstate); _bt_load(btspool->index, btspool, btspool2);}/* * Internal routines. *//* * allocate a new, clean btree page, not linked to any siblings. */static void_bt_blnewpage(Relation index, Buffer *buf, Page *page, uint32 level){ BTPageOpaque opaque; *buf = _bt_getbuf(index, P_NEW, BT_WRITE); *page = BufferGetPage(*buf); /* Zero the page and set up standard page header info */ _bt_pageinit(*page, BufferGetPageSize(*buf)); /* Initialize BT opaque state */ opaque = (BTPageOpaque) PageGetSpecialPointer(*page); opaque->btpo_prev = opaque->btpo_next = P_NONE; opaque->btpo.level = level; opaque->btpo_flags = (level > 0) ? 0 : BTP_LEAF; /* Make the P_HIKEY line pointer appear allocated */ ((PageHeader) *page)->pd_lower += sizeof(ItemIdData);}/* * emit a completed btree page, and release the lock and pin on it. * This is essentially _bt_wrtbuf except we also emit a WAL record. */static void_bt_blwritepage(Relation index, Buffer buf){ Page pg = BufferGetPage(buf); /* NO ELOG(ERROR) from here till newpage op is logged */ START_CRIT_SECTION(); /* XLOG stuff */ if (!index->rd_istemp) { xl_btree_newpage xlrec; XLogRecPtr recptr; XLogRecData rdata[2]; xlrec.node = index->rd_node; xlrec.blkno = BufferGetBlockNumber(buf); rdata[0].buffer = InvalidBuffer; rdata[0].data = (char *) &xlrec; rdata[0].len = SizeOfBtreeNewpage; rdata[0].next = &(rdata[1]); rdata[1].buffer = buf; rdata[1].data = (char *) pg; rdata[1].len = BLCKSZ; rdata[1].next = NULL; recptr = XLogInsert(RM_BTREE_ID, XLOG_BTREE_NEWPAGE, rdata); PageSetLSN(pg, recptr); PageSetSUI(pg, ThisStartUpID); } END_CRIT_SECTION(); _bt_wrtbuf(index, buf);}/* * allocate and initialize a new BTPageState. the returned structure * is suitable for immediate use by _bt_buildadd. */static BTPageState *_bt_pagestate(Relation index, uint32 level){ BTPageState *state = (BTPageState *) palloc0(sizeof(BTPageState)); /* create initial page */ _bt_blnewpage(index, &(state->btps_buf), &(state->btps_page), level); state->btps_minkey = (BTItem) NULL; /* initialize lastoff so first item goes into P_FIRSTKEY */ state->btps_lastoff = P_HIKEY; state->btps_level = level; /* set "full" threshold based on level. See notes at head of file. */ if (level > 0) state->btps_full = (PageGetPageSize(state->btps_page) * 3) / 10; else state->btps_full = PageGetPageSize(state->btps_page) / 10; /* no parent level, yet */ state->btps_next = (BTPageState *) NULL; return state;}/* * slide an array of ItemIds back one slot (from P_FIRSTKEY to * P_HIKEY, overwriting P_HIKEY). we need to do this when we discover * that we have built an ItemId array in what has turned out to be a * P_RIGHTMOST page. */static void_bt_slideleft(Relation index, Buffer buf, Page page){ OffsetNumber off; OffsetNumber maxoff; ItemId previi; ItemId thisii; if (!PageIsEmpty(page)) { maxoff = PageGetMaxOffsetNumber(page); previi = PageGetItemId(page, P_HIKEY); for (off = P_FIRSTKEY; off <= maxoff; off = OffsetNumberNext(off)) { thisii = PageGetItemId(page, off); *previi = *thisii; previi = thisii; } ((PageHeader) page)->pd_lower -= sizeof(ItemIdData); }}/* * Add an item to a page being built. * * The main difference between this routine and a bare PageAddItem call * is that this code knows that the leftmost data item on a non-leaf * btree page doesn't need to have a key. Therefore, it strips such * items down to just the item header. * * This is almost like nbtinsert.c's _bt_pgaddtup(), but we can't use * that because it assumes that P_RIGHTMOST() will return the correct * answer for the page. Here, we don't know yet if the page will be * rightmost. Offset P_FIRSTKEY is always the first data key. */static void_bt_sortaddtup(Page page, Size itemsize, BTItem btitem, OffsetNumber itup_off){ BTPageOpaque opaque = (BTPageOpaque) PageGetSpecialPointer(page); BTItemData truncitem; if (!P_ISLEAF(opaque) && itup_off == P_FIRSTKEY) { memcpy(&truncitem, btitem, sizeof(BTItemData)); truncitem.bti_itup.t_info = sizeof(BTItemData); btitem = &truncitem; itemsize = sizeof(BTItemData); } if (PageAddItem(page, (Item) btitem, itemsize, itup_off, LP_USED) == InvalidOffsetNumber) elog(ERROR, "failed to add item to the index page");}/*---------- * Add an item to a disk page from the sort output. * * We must be careful to observe the page layout conventions of nbtsearch.c: * - rightmost pages start data items at P_HIKEY instead of at P_FIRSTKEY. * - on non-leaf pages, the key portion of the first item need not be * stored, we should store only the link. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -