diff options
Diffstat (limited to 'src/backend/access/nbtree/nbtinsert.c')
-rw-r--r-- | src/backend/access/nbtree/nbtinsert.c | 91 |
1 files changed, 48 insertions, 43 deletions
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c index f4c1a974ef8..8c6e867c619 100644 --- a/src/backend/access/nbtree/nbtinsert.c +++ b/src/backend/access/nbtree/nbtinsert.c @@ -30,7 +30,8 @@ #define BTREE_FASTPATH_MIN_LEVEL 2 -static BTStack _bt_search_insert(Relation rel, BTInsertState insertstate); +static BTStack _bt_search_insert(Relation rel, Relation heaprel, + BTInsertState insertstate); static TransactionId _bt_check_unique(Relation rel, BTInsertState insertstate, Relation heapRel, IndexUniqueCheck checkUnique, bool *is_unique, @@ -41,8 +42,9 @@ static OffsetNumber _bt_findinsertloc(Relation rel, bool indexUnchanged, BTStack stack, Relation heapRel); -static void _bt_stepright(Relation rel, BTInsertState insertstate, BTStack stack); -static void _bt_insertonpg(Relation rel, BTScanInsert itup_key, +static void _bt_stepright(Relation rel, Relation heaprel, + BTInsertState insertstate, BTStack stack); +static void _bt_insertonpg(Relation rel, Relation heaprel, BTScanInsert itup_key, Buffer buf, Buffer cbuf, BTStack stack, @@ -51,13 +53,13 @@ static void _bt_insertonpg(Relation rel, BTScanInsert itup_key, OffsetNumber newitemoff, int postingoff, bool split_only_page); -static Buffer _bt_split(Relation rel, BTScanInsert itup_key, Buffer buf, - Buffer cbuf, OffsetNumber newitemoff, Size newitemsz, - IndexTuple newitem, IndexTuple orignewitem, +static Buffer _bt_split(Relation rel, Relation heaprel, BTScanInsert itup_key, + Buffer buf, Buffer cbuf, OffsetNumber newitemoff, + Size newitemsz, IndexTuple newitem, IndexTuple orignewitem, IndexTuple nposting, uint16 postingoff); -static void _bt_insert_parent(Relation rel, Buffer buf, Buffer rbuf, - BTStack stack, bool isroot, bool isonly); -static Buffer _bt_newroot(Relation rel, Buffer lbuf, Buffer rbuf); +static void _bt_insert_parent(Relation rel, Relation heaprel, Buffer buf, + Buffer rbuf, BTStack stack, bool isroot, bool isonly); +static Buffer _bt_newroot(Relation rel, Relation heaprel, Buffer lbuf, Buffer rbuf); static inline bool _bt_pgaddtup(Page page, Size itemsize, IndexTuple itup, OffsetNumber itup_off, bool newfirstdataitem); static void _bt_delete_or_dedup_one_page(Relation rel, Relation heapRel, @@ -108,7 +110,7 @@ _bt_doinsert(Relation rel, IndexTuple itup, bool checkingunique = (checkUnique != UNIQUE_CHECK_NO); /* we need an insertion scan key to do our search, so build one */ - itup_key = _bt_mkscankey(rel, itup); + itup_key = _bt_mkscankey(rel, heapRel, itup); if (checkingunique) { @@ -162,7 +164,7 @@ search: * searching from the root page. insertstate.buf will hold a buffer that * is locked in exclusive mode afterwards. */ - stack = _bt_search_insert(rel, &insertstate); + stack = _bt_search_insert(rel, heapRel, &insertstate); /* * checkingunique inserts are not allowed to go ahead when two tuples with @@ -255,8 +257,8 @@ search: */ newitemoff = _bt_findinsertloc(rel, &insertstate, checkingunique, indexUnchanged, stack, heapRel); - _bt_insertonpg(rel, itup_key, insertstate.buf, InvalidBuffer, stack, - itup, insertstate.itemsz, newitemoff, + _bt_insertonpg(rel, heapRel, itup_key, insertstate.buf, InvalidBuffer, + stack, itup, insertstate.itemsz, newitemoff, insertstate.postingoff, false); } else @@ -312,7 +314,7 @@ search: * since each per-backend cache won't stay valid for long. */ static BTStack -_bt_search_insert(Relation rel, BTInsertState insertstate) +_bt_search_insert(Relation rel, Relation heaprel, BTInsertState insertstate) { Assert(insertstate->buf == InvalidBuffer); Assert(!insertstate->bounds_valid); @@ -375,8 +377,8 @@ _bt_search_insert(Relation rel, BTInsertState insertstate) } /* Cannot use optimization -- descend tree, return proper descent stack */ - return _bt_search(rel, insertstate->itup_key, &insertstate->buf, BT_WRITE, - NULL); + return _bt_search(rel, heaprel, insertstate->itup_key, &insertstate->buf, + BT_WRITE, NULL); } /* @@ -885,7 +887,7 @@ _bt_findinsertloc(Relation rel, _bt_compare(rel, itup_key, page, P_HIKEY) <= 0) break; - _bt_stepright(rel, insertstate, stack); + _bt_stepright(rel, heapRel, insertstate, stack); /* Update local state after stepping right */ page = BufferGetPage(insertstate->buf); opaque = BTPageGetOpaque(page); @@ -969,7 +971,7 @@ _bt_findinsertloc(Relation rel, pg_prng_uint32(&pg_global_prng_state) <= (PG_UINT32_MAX / 100)) break; - _bt_stepright(rel, insertstate, stack); + _bt_stepright(rel, heapRel, insertstate, stack); /* Update local state after stepping right */ page = BufferGetPage(insertstate->buf); opaque = BTPageGetOpaque(page); @@ -1022,7 +1024,7 @@ _bt_findinsertloc(Relation rel, * indexes. */ static void -_bt_stepright(Relation rel, BTInsertState insertstate, BTStack stack) +_bt_stepright(Relation rel, Relation heaprel, BTInsertState insertstate, BTStack stack) { Page page; BTPageOpaque opaque; @@ -1048,7 +1050,7 @@ _bt_stepright(Relation rel, BTInsertState insertstate, BTStack stack) */ if (P_INCOMPLETE_SPLIT(opaque)) { - _bt_finish_split(rel, rbuf, stack); + _bt_finish_split(rel, heaprel, rbuf, stack); rbuf = InvalidBuffer; continue; } @@ -1099,6 +1101,7 @@ _bt_stepright(Relation rel, BTInsertState insertstate, BTStack stack) */ static void _bt_insertonpg(Relation rel, + Relation heaprel, BTScanInsert itup_key, Buffer buf, Buffer cbuf, @@ -1209,8 +1212,8 @@ _bt_insertonpg(Relation rel, Assert(!split_only_page); /* split the buffer into left and right halves */ - rbuf = _bt_split(rel, itup_key, buf, cbuf, newitemoff, itemsz, itup, - origitup, nposting, postingoff); + rbuf = _bt_split(rel, heaprel, itup_key, buf, cbuf, newitemoff, itemsz, + itup, origitup, nposting, postingoff); PredicateLockPageSplit(rel, BufferGetBlockNumber(buf), BufferGetBlockNumber(rbuf)); @@ -1233,7 +1236,7 @@ _bt_insertonpg(Relation rel, * page. *---------- */ - _bt_insert_parent(rel, buf, rbuf, stack, isroot, isonly); + _bt_insert_parent(rel, heaprel, buf, rbuf, stack, isroot, isonly); } else { @@ -1254,7 +1257,7 @@ _bt_insertonpg(Relation rel, Assert(!isleaf); Assert(BufferIsValid(cbuf)); - metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_WRITE); + metabuf = _bt_getbuf(rel, heaprel, BTREE_METAPAGE, BT_WRITE); metapg = BufferGetPage(metabuf); metad = BTPageGetMeta(metapg); @@ -1418,7 +1421,7 @@ _bt_insertonpg(Relation rel, * call _bt_getrootheight while holding a buffer lock. */ if (BlockNumberIsValid(blockcache) && - _bt_getrootheight(rel) >= BTREE_FASTPATH_MIN_LEVEL) + _bt_getrootheight(rel, heaprel) >= BTREE_FASTPATH_MIN_LEVEL) RelationSetTargetBlock(rel, blockcache); } @@ -1459,8 +1462,8 @@ _bt_insertonpg(Relation rel, * The pin and lock on buf are maintained. */ static Buffer -_bt_split(Relation rel, BTScanInsert itup_key, Buffer buf, Buffer cbuf, - OffsetNumber newitemoff, Size newitemsz, IndexTuple newitem, +_bt_split(Relation rel, Relation heaprel, BTScanInsert itup_key, Buffer buf, + Buffer cbuf, OffsetNumber newitemoff, Size newitemsz, IndexTuple newitem, IndexTuple orignewitem, IndexTuple nposting, uint16 postingoff) { Buffer rbuf; @@ -1712,7 +1715,7 @@ _bt_split(Relation rel, BTScanInsert itup_key, Buffer buf, Buffer cbuf, * way because it avoids an unnecessary PANIC when either origpage or its * existing sibling page are corrupt. */ - rbuf = _bt_getbuf(rel, P_NEW, BT_WRITE); + rbuf = _bt_getbuf(rel, heaprel, P_NEW, BT_WRITE); rightpage = BufferGetPage(rbuf); rightpagenumber = BufferGetBlockNumber(rbuf); /* rightpage was initialized by _bt_getbuf */ @@ -1885,7 +1888,7 @@ _bt_split(Relation rel, BTScanInsert itup_key, Buffer buf, Buffer cbuf, */ if (!isrightmost) { - sbuf = _bt_getbuf(rel, oopaque->btpo_next, BT_WRITE); + sbuf = _bt_getbuf(rel, heaprel, oopaque->btpo_next, BT_WRITE); spage = BufferGetPage(sbuf); sopaque = BTPageGetOpaque(spage); if (sopaque->btpo_prev != origpagenumber) @@ -2092,6 +2095,7 @@ _bt_split(Relation rel, BTScanInsert itup_key, Buffer buf, Buffer cbuf, */ static void _bt_insert_parent(Relation rel, + Relation heaprel, Buffer buf, Buffer rbuf, BTStack stack, @@ -2118,7 +2122,7 @@ _bt_insert_parent(Relation rel, Assert(stack == NULL); Assert(isonly); /* create a new root node and update the metapage */ - rootbuf = _bt_newroot(rel, buf, rbuf); + rootbuf = _bt_newroot(rel, heaprel, buf, rbuf); /* release the split buffers */ _bt_relbuf(rel, rootbuf); _bt_relbuf(rel, rbuf); @@ -2157,7 +2161,8 @@ _bt_insert_parent(Relation rel, BlockNumberIsValid(RelationGetTargetBlock(rel)))); /* Find the leftmost page at the next level up */ - pbuf = _bt_get_endpoint(rel, opaque->btpo_level + 1, false, NULL); + pbuf = _bt_get_endpoint(rel, heaprel, opaque->btpo_level + 1, false, + NULL); /* Set up a phony stack entry pointing there */ stack = &fakestack; stack->bts_blkno = BufferGetBlockNumber(pbuf); @@ -2183,7 +2188,7 @@ _bt_insert_parent(Relation rel, * new downlink will be inserted at the correct offset. Even buf's * parent may have changed. */ - pbuf = _bt_getstackbuf(rel, stack, bknum); + pbuf = _bt_getstackbuf(rel, heaprel, stack, bknum); /* * Unlock the right child. The left child will be unlocked in @@ -2207,7 +2212,7 @@ _bt_insert_parent(Relation rel, RelationGetRelationName(rel), bknum, rbknum))); /* Recursively insert into the parent */ - _bt_insertonpg(rel, NULL, pbuf, buf, stack->bts_parent, + _bt_insertonpg(rel, heaprel, NULL, pbuf, buf, stack->bts_parent, new_item, MAXALIGN(IndexTupleSize(new_item)), stack->bts_offset + 1, 0, isonly); @@ -2227,7 +2232,7 @@ _bt_insert_parent(Relation rel, * and unpinned. */ void -_bt_finish_split(Relation rel, Buffer lbuf, BTStack stack) +_bt_finish_split(Relation rel, Relation heaprel, Buffer lbuf, BTStack stack) { Page lpage = BufferGetPage(lbuf); BTPageOpaque lpageop = BTPageGetOpaque(lpage); @@ -2240,7 +2245,7 @@ _bt_finish_split(Relation rel, Buffer lbuf, BTStack stack) Assert(P_INCOMPLETE_SPLIT(lpageop)); /* Lock right sibling, the one missing the downlink */ - rbuf = _bt_getbuf(rel, lpageop->btpo_next, BT_WRITE); + rbuf = _bt_getbuf(rel, heaprel, lpageop->btpo_next, BT_WRITE); rpage = BufferGetPage(rbuf); rpageop = BTPageGetOpaque(rpage); @@ -2252,7 +2257,7 @@ _bt_finish_split(Relation rel, Buffer lbuf, BTStack stack) BTMetaPageData *metad; /* acquire lock on the metapage */ - metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_WRITE); + metabuf = _bt_getbuf(rel, heaprel, BTREE_METAPAGE, BT_WRITE); metapg = BufferGetPage(metabuf); metad = BTPageGetMeta(metapg); @@ -2269,7 +2274,7 @@ _bt_finish_split(Relation rel, Buffer lbuf, BTStack stack) elog(DEBUG1, "finishing incomplete split of %u/%u", BufferGetBlockNumber(lbuf), BufferGetBlockNumber(rbuf)); - _bt_insert_parent(rel, lbuf, rbuf, stack, wasroot, wasonly); + _bt_insert_parent(rel, heaprel, lbuf, rbuf, stack, wasroot, wasonly); } /* @@ -2304,7 +2309,7 @@ _bt_finish_split(Relation rel, Buffer lbuf, BTStack stack) * offset number bts_offset + 1. */ Buffer -_bt_getstackbuf(Relation rel, BTStack stack, BlockNumber child) +_bt_getstackbuf(Relation rel, Relation heaprel, BTStack stack, BlockNumber child) { BlockNumber blkno; OffsetNumber start; @@ -2318,13 +2323,13 @@ _bt_getstackbuf(Relation rel, BTStack stack, BlockNumber child) Page page; BTPageOpaque opaque; - buf = _bt_getbuf(rel, blkno, BT_WRITE); + buf = _bt_getbuf(rel, heaprel, blkno, BT_WRITE); page = BufferGetPage(buf); opaque = BTPageGetOpaque(page); if (P_INCOMPLETE_SPLIT(opaque)) { - _bt_finish_split(rel, buf, stack->bts_parent); + _bt_finish_split(rel, heaprel, buf, stack->bts_parent); continue; } @@ -2428,7 +2433,7 @@ _bt_getstackbuf(Relation rel, BTStack stack, BlockNumber child) * lbuf, rbuf & rootbuf. */ static Buffer -_bt_newroot(Relation rel, Buffer lbuf, Buffer rbuf) +_bt_newroot(Relation rel, Relation heaprel, Buffer lbuf, Buffer rbuf) { Buffer rootbuf; Page lpage, @@ -2454,12 +2459,12 @@ _bt_newroot(Relation rel, Buffer lbuf, Buffer rbuf) lopaque = BTPageGetOpaque(lpage); /* get a new root page */ - rootbuf = _bt_getbuf(rel, P_NEW, BT_WRITE); + rootbuf = _bt_getbuf(rel, heaprel, P_NEW, BT_WRITE); rootpage = BufferGetPage(rootbuf); rootblknum = BufferGetBlockNumber(rootbuf); /* acquire lock on the metapage */ - metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_WRITE); + metabuf = _bt_getbuf(rel, heaprel, BTREE_METAPAGE, BT_WRITE); metapg = BufferGetPage(metabuf); metad = BTPageGetMeta(metapg); |