nbtinsert.c 51.0 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*-------------------------------------------------------------------------
 *
 * btinsert.c--
 *    Item insertion in Lehman and Yao btrees for Postgres.
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
B
Bruce Momjian 已提交
10
 *    $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtinsert.c,v 1.17 1997/08/20 14:53:15 momjian Exp $
11 12 13 14
 *
 *-------------------------------------------------------------------------
 */

M
Marc G. Fournier 已提交
15
#include <postgres.h>
16

M
Marc G. Fournier 已提交
17 18 19
#include <utils/memutils.h>
#include <storage/bufpage.h>
#include <access/nbtree.h>
20
#include <access/heapam.h>
M
Marc G. Fournier 已提交
21
#include <storage/bufmgr.h>
V
Vadim B. Mikheev 已提交
22
#include <fmgr.h>
M
Marc G. Fournier 已提交
23

24
#ifndef HAVE_MEMMOVE
M
Marc G. Fournier 已提交
25
# include <regex/utils.h>
26 27 28 29
#else
# include <string.h>
#endif

30
static InsertIndexResult _bt_insertonpg(Relation rel, Buffer buf, BTStack stack, int keysz, ScanKey scankey, BTItem btitem, BTItem afteritem);
V
Vadim B. Mikheev 已提交
31
static Buffer _bt_split(Relation rel, Buffer buf, OffsetNumber firstright);
32 33 34 35
static OffsetNumber _bt_findsplitloc(Relation rel, Page page, OffsetNumber start, OffsetNumber maxoff, Size llimit);
static void _bt_newroot(Relation rel, Buffer lbuf, Buffer rbuf);
static OffsetNumber _bt_pgaddtup(Relation rel, Buffer buf, int keysz, ScanKey itup_scankey, Size itemsize, BTItem btitem, BTItem afteritem);
static bool _bt_goesonpg(Relation rel, Buffer buf, Size keysz, ScanKey scankey, BTItem afteritem);
36
static void _bt_updateitem(Relation rel, Size keysz, Buffer buf, BTItem oldItem, BTItem newItem);
V
Vadim B. Mikheev 已提交
37
static bool _bt_isequal (TupleDesc itupdesc, Page page, OffsetNumber offnum, int keysz, ScanKey scankey);
38 39 40 41 42 43 44 45 46

/*
 *  _bt_doinsert() -- Handle insertion of a single btitem in the tree.
 *
 *	This routine is called by the public interface routines, btbuild
 *	and btinsert.  By here, btitem is filled in, and has a unique
 *	(xid, seqno) pair.
 */
InsertIndexResult
47
_bt_doinsert(Relation rel, BTItem btitem, bool index_is_unique, Relation heapRel)
48 49 50 51 52 53
{
    ScanKey itup_scankey;
    IndexTuple itup;
    BTStack stack;
    Buffer buf;
    BlockNumber blkno;
V
Vadim B. Mikheev 已提交
54
    int natts = rel->rd_rel->relnatts;
55 56 57 58 59 60 61 62 63
    InsertIndexResult res;
    
    itup = &(btitem->bti_itup);
    
    /* we need a scan key to do our search, so build one */
    itup_scankey = _bt_mkscankey(rel, itup);
    
    /* find the page containing this key */
    stack = _bt_search(rel, natts, itup_scankey, &buf);
64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    blkno = BufferGetBlockNumber(buf);
    
    /* trade in our read lock for a write lock */
    _bt_relbuf(rel, buf, BT_READ);
    buf = _bt_getbuf(rel, blkno, BT_WRITE);
    
    /*
     *  If the page was split between the time that we surrendered our
     *  read lock and acquired our write lock, then this page may no
     *  longer be the right place for the key we want to insert.  In this
     *  case, we need to move right in the tree.  See Lehman and Yao for
     *  an excruciatingly precise description.
     */
    
    buf = _bt_moveright(rel, buf, natts, itup_scankey, BT_WRITE);
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

    /* if we're not allowing duplicates, make sure the key isn't */
    /* already in the node */
    if ( index_is_unique )
    {
	OffsetNumber offset, maxoff;
	Page page;

	page = BufferGetPage(buf);
	maxoff = PageGetMaxOffsetNumber (page);

	offset = _bt_binsrch(rel, buf, natts, itup_scankey, BT_DESCENT);

	/* make sure the offset we're given points to an actual */
	/* key on the page before trying to compare it */
	if ( !PageIsEmpty (page) && offset <= maxoff )
	{
	    TupleDesc itupdesc;
	    BTItem btitem;
	    IndexTuple itup;
	    HeapTuple htup;
	    BTPageOpaque opaque;
	    Buffer nbuf;
	    BlockNumber blkno;
	    
	    itupdesc = RelationGetTupleDescriptor(rel);
	    nbuf = InvalidBuffer;
	    opaque = (BTPageOpaque) PageGetSpecialPointer(page);
V
Vadim B. Mikheev 已提交
108 109 110 111 112 113
	    /*
	     * _bt_compare returns 0 for (1,NULL) and (1,NULL) -
	     * this's how we handling NULLs - and so we must not use
	     * _bt_compare in real comparison, but only for
	     * ordering/finding items on pages.	- vadim 03/24/97

114 115
	    while ( !_bt_compare (rel, itupdesc, page, 
			    	natts, itup_scankey, offset) )
V
Vadim B. Mikheev 已提交
116 117
	     */
	    while ( _bt_isequal (itupdesc, page, offset, natts, itup_scankey) )
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	    {	/* they're equal */
		btitem = (BTItem) PageGetItem(page, PageGetItemId(page, offset));
		itup = &(btitem->bti_itup);
		htup = heap_fetch (heapRel, SelfTimeQual, &(itup->t_tid), NULL);
		if ( htup != (HeapTuple) NULL )
		{	/* it is a duplicate */
		    elog(WARN, "Cannot insert a duplicate key into a unique index.");
		}
	    	/* get next offnum */
	    	if ( offset < maxoff )
	    	{
	    	    offset = OffsetNumberNext(offset);
	    	}
	    	else
	    	{	/* move right ? */
		    if ( P_RIGHTMOST (opaque) )
		    	break;
V
Vadim B. Mikheev 已提交
135 136
	    	    if ( !_bt_isequal (itupdesc, page, P_HIKEY, 
	    	    				natts, itup_scankey) )
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
			break;
		    /* 
		     * min key of the right page is the same,
		     * ooh - so many dead duplicates...
		     */
		    blkno = opaque->btpo_next;
		    if ( nbuf != InvalidBuffer )
		    	_bt_relbuf (rel, nbuf, BT_READ);
		    for (nbuf = InvalidBuffer; ; )
		    {
		    	nbuf = _bt_getbuf (rel, blkno, BT_READ);
		    	page = BufferGetPage (nbuf);
		    	maxoff = PageGetMaxOffsetNumber(page);
		    	opaque = (BTPageOpaque) PageGetSpecialPointer(page);
		    	offset = P_RIGHTMOST(opaque) ? P_HIKEY : P_FIRSTKEY;
		    	if ( ! PageIsEmpty (page) && offset <= maxoff )
		    	{	/* Found some key */
			    break;
			}
			else
			{	/* Empty or "pseudo"-empty page - get next */
			    blkno = opaque->btpo_next;
		    	    _bt_relbuf (rel, nbuf, BT_READ);
		    	    nbuf = InvalidBuffer;
		    	    if ( blkno == P_NONE )
		    	    	break;
			}
		    }
		    if ( nbuf == InvalidBuffer )
		    	break;
	    	}
	    }
	    if ( nbuf != InvalidBuffer )
		_bt_relbuf(rel, nbuf, BT_READ);
	}
    }
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    
    /* do the insertion */
    res = _bt_insertonpg(rel, buf, stack, natts, itup_scankey,
			 btitem, (BTItem) NULL);
    
    /* be tidy */
    _bt_freestack(stack);
    _bt_freeskey(itup_scankey);
    
    return (res);
}

/*
 *  _bt_insertonpg() -- Insert a tuple on a particular page in the index.
 *
 *	This recursive procedure does the following things:
 *
 *	    +  if necessary, splits the target page.
 *	    +  finds the right place to insert the tuple (taking into
 *	       account any changes induced by a split).
 *	    +  inserts the tuple.
 *	    +  if the page was split, pops the parent stack, and finds the
 *	       right place to insert the new child pointer (by walking
 *	       right using information stored in the parent stack).
 *	    +  invoking itself with the appropriate tuple for the right
 *	       child page on the parent.
 *
 *	On entry, we must have the right buffer on which to do the
 *	insertion, and the buffer must be pinned and locked.  On return,
 *	we will have dropped both the pin and the write lock on the buffer.
 *
 *	The locking interactions in this code are critical.  You should
 *	grok Lehman and Yao's paper before making any changes.  In addition,
 *	you need to understand how we disambiguate duplicate keys in this
 *	implementation, in order to be able to find our location using
 *	L&Y "move right" operations.  Since we may insert duplicate user
 *	keys, and since these dups may propogate up the tree, we use the
 *	'afteritem' parameter to position ourselves correctly for the
 *	insertion on internal pages.
 */
static InsertIndexResult
_bt_insertonpg(Relation rel,
	       Buffer buf,
	       BTStack stack,
	       int keysz,
	       ScanKey scankey,
	       BTItem btitem,
	       BTItem afteritem)
{
    InsertIndexResult res;
    Page page;
V
Vadim B. Mikheev 已提交
224
    BTPageOpaque lpageop;
V
Vadim B. Mikheev 已提交
225
    BlockNumber itup_blkno;
226
    OffsetNumber itup_off;
V
Vadim B. Mikheev 已提交
227
    OffsetNumber firstright = InvalidOffsetNumber;
228
    int itemsz;
V
Vadim B. Mikheev 已提交
229 230
    bool do_split = false;
    bool keys_equal = false;
231 232
    
    page = BufferGetPage(buf);
V
Vadim B. Mikheev 已提交
233 234
    lpageop = (BTPageOpaque) PageGetSpecialPointer(page);

235 236 237 238 239
    itemsz = IndexTupleDSize(btitem->bti_itup)
	+ (sizeof(BTItemData) - sizeof(IndexTupleData));

    itemsz = DOUBLEALIGN(itemsz);	/* be safe, PageAddItem will do this
					   but we need to be consistent */
V
Vadim B. Mikheev 已提交
240 241 242 243 244
    /*
     * If we have to insert item on the leftmost page which is the first
     * page in the chain of duplicates then:
     * 1. if scankey == hikey (i.e. - new duplicate item) then
     *    insert it here;
V
Vadim B. Mikheev 已提交
245 246 247
     * 2. if scankey < hikey then:
     *    2.a if there is duplicate key(s) here - we force splitting;
     *    2.b else - we may "eat" this page from duplicates chain.
V
Vadim B. Mikheev 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
     */
    if ( lpageop->btpo_flags & BTP_CHAIN )
    {
    	OffsetNumber maxoff = PageGetMaxOffsetNumber (page);
    	ItemId hitemid;
    	BTItem hitem;
    	
	Assert ( !P_RIGHTMOST(lpageop) );
	hitemid = PageGetItemId(page, P_HIKEY);
	hitem = (BTItem) PageGetItem(page, hitemid);
    	if ( maxoff > P_HIKEY && 
    		!_bt_itemcmp (rel, keysz, hitem, 
    		   (BTItem) PageGetItem(page, PageGetItemId(page, P_FIRSTKEY)),
    	    		    BTEqualStrategyNumber) )
	    elog (FATAL, "btree: bad key on the page in the chain of duplicates");

    	if ( !_bt_skeycmp (rel, keysz, scankey, page, hitemid, 
    	    				BTEqualStrategyNumber) )
	{
    	    if ( !P_LEFTMOST(lpageop) )
	    	elog (FATAL, "btree: attempt to insert bad key on the non-leftmost page in the chain of duplicates");
    	    if ( !_bt_skeycmp (rel, keysz, scankey, page, hitemid, 
    	    				BTLessStrategyNumber) )
	    	elog (FATAL, "btree: attempt to insert higher key on the leftmost page in the chain of duplicates");
V
Vadim B. Mikheev 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
	    if ( maxoff > P_HIKEY )	/* have duplicate(s) */
	    {
	    	firstright = P_FIRSTKEY;
	    	do_split = true;
	    }
	    else			/* "eat" page */
	    {
    		Buffer pbuf;
    		Page ppage;
    		
    		itup_blkno = BufferGetBlockNumber(buf);
    		itup_off = PageAddItem(page, (Item) btitem, itemsz, 
    						P_FIRSTKEY, LP_USED);
    		if ( itup_off == InvalidOffsetNumber )
    		    elog (FATAL, "btree: failed to add item");
    		lpageop->btpo_flags &= ~BTP_CHAIN;
	    	pbuf = _bt_getstackbuf(rel, stack, BT_WRITE);
	    	ppage = BufferGetPage(pbuf);
       		PageIndexTupleDelete(ppage, stack->bts_offset);
		pfree(stack->bts_btitem);
		stack->bts_btitem = _bt_formitem(&(btitem->bti_itup));
		ItemPointerSet(&(stack->bts_btitem->bti_itup.t_tid), 
			       itup_blkno, P_HIKEY);
		_bt_wrtbuf(rel, buf);
		res = _bt_insertonpg(rel, pbuf, stack->bts_parent,
					keysz, scankey, stack->bts_btitem,
					NULL);
    		ItemPointerSet(&(res->pointerData), itup_blkno, itup_off);
    		return (res);
	    }
V
Vadim B. Mikheev 已提交
302
	}
V
Vadim B. Mikheev 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
	else
	{
	    keys_equal = true;
    	    if ( PageGetFreeSpace(page) < itemsz )
    	    	do_split = true;
    	}
    }
    else if ( PageGetFreeSpace(page) < itemsz )
    	do_split = true;
    else if ( PageGetFreeSpace(page) < 3*itemsz + 2*sizeof(ItemIdData) )
    {
    	OffsetNumber offnum = (P_RIGHTMOST(lpageop)) ? P_HIKEY : P_FIRSTKEY;
    	OffsetNumber maxoff = PageGetMaxOffsetNumber (page);
    	ItemId itid;
    	BTItem previtem, chkitem;
    	Size maxsize;
    	Size currsize;
    	
    	itid = PageGetItemId(page, offnum);
    	previtem = (BTItem) PageGetItem(page, itid);
    	maxsize = currsize = (ItemIdGetLength(itid) + sizeof(ItemIdData));
    	for (offnum = OffsetNumberNext(offnum);
			offnum <= maxoff; offnum = OffsetNumberNext(offnum) )
	{
    	    itid = PageGetItemId(page, offnum);
	    chkitem = (BTItem) PageGetItem(page, itid);
    	    if ( !_bt_itemcmp (rel, keysz, previtem, chkitem,
    	    		    			BTEqualStrategyNumber) )
    	    {
    	    	if ( currsize > maxsize )
    	    	    maxsize = currsize;
    	    	currsize = 0;
    	    	previtem = chkitem;
    	    }
    	    currsize += (ItemIdGetLength(itid) + sizeof(ItemIdData));
	}
    	if ( currsize > maxsize )
    	    maxsize = currsize;
    	maxsize += sizeof (PageHeaderData) + 
    			DOUBLEALIGN (sizeof (BTPageOpaqueData));
    	if ( maxsize >= PageGetPageSize (page) / 2 )
    	    do_split = true;
V
Vadim B. Mikheev 已提交
345
    }
346
    
V
Vadim B. Mikheev 已提交
347
    if ( do_split )
V
Vadim B. Mikheev 已提交
348
    {
V
Vadim B. Mikheev 已提交
349 350 351 352 353 354 355 356
    	Buffer rbuf;
    	Page rpage;
    	BTItem ritem;
    	BlockNumber rbknum;
    	BTPageOpaque rpageop;
    	Buffer pbuf;
    	Page ppage;
    	BTPageOpaque ppageop;
V
Vadim B. Mikheev 已提交
357 358
    	BlockNumber bknum = BufferGetBlockNumber(buf);
    	BTItem lowLeftItem;
V
Vadim B. Mikheev 已提交
359 360 361
    	OffsetNumber maxoff;
    	bool shifted = false;
    	bool left_chained = ( lpageop->btpo_flags & BTP_CHAIN ) ? true : false;
V
Vadim B. Mikheev 已提交
362 363
	
	/* 
V
Vadim B. Mikheev 已提交
364 365
	 * If we have to split leaf page in the chain of duplicates by
	 * new duplicate then we try to look at our right sibling first.
V
Vadim B. Mikheev 已提交
366 367
	 */
	if ( ( lpageop->btpo_flags & BTP_CHAIN ) &&
V
Vadim B. Mikheev 已提交
368
		( lpageop->btpo_flags & BTP_LEAF ) && keys_equal )
V
Vadim B. Mikheev 已提交
369 370 371
	{
    	    bool use_left = true;
    		
372 373 374 375 376
	    rbuf = _bt_getbuf(rel, lpageop->btpo_next, BT_WRITE);
	    rpage = BufferGetPage(rbuf);
	    rpageop = (BTPageOpaque) PageGetSpecialPointer(rpage);
	    if ( !P_RIGHTMOST (rpageop) )	/* non-rightmost page */
	    {  	/*
V
Vadim B. Mikheev 已提交
377
	    	 * If we have the same hikey here then it's
378 379
	    	 * yet another page in chain.
	     	 */
V
Vadim B. Mikheev 已提交
380 381 382 383 384 385 386 387
    	    	if ( _bt_skeycmp (rel, keysz, scankey, rpage, 
    	    				PageGetItemId(rpage, P_HIKEY), 
    	    				BTEqualStrategyNumber) )
    	    	{
    	    	    if ( !( rpageop->btpo_flags & BTP_CHAIN ) )
    	    	    	elog (FATAL, "btree: lost page in the chain of duplicates");
    	    	}
    	    	else if ( _bt_skeycmp (rel, keysz, scankey, rpage, 
388 389 390
    	    			PageGetItemId(rpage, P_HIKEY), 
    	    			BTGreaterStrategyNumber) )
    	            elog (FATAL, "btree: hikey is out of order");
V
Vadim B. Mikheev 已提交
391 392 393 394
    	        else if ( rpageop->btpo_flags & BTP_CHAIN )
	    	/* 
	    	 * If hikey > scankey then it's last page in chain and
	    	 * BTP_CHAIN must be OFF 
V
Vadim B. Mikheev 已提交
395
	    	 */
V
Vadim B. Mikheev 已提交
396 397
    	    	    elog (FATAL, "btree: lost last page in the chain of duplicates");
    	    	
398
    	    	/* if there is room here then we use this page. */
V
Vadim B. Mikheev 已提交
399
    	    	if ( PageGetFreeSpace (rpage) > itemsz )
400 401 402 403 404 405 406
	    	    use_left = false;
	    }
	    else	/* rightmost page */
	    {
		Assert ( !( rpageop->btpo_flags & BTP_CHAIN ) );
    	    	/* if there is room here then we use this page. */
    	    	if ( PageGetFreeSpace (rpage) > itemsz )
V
Vadim B. Mikheev 已提交
407 408 409 410 411 412 413 414 415 416
	    	    use_left = false;
	    }
	    if ( !use_left )	/* insert on the right page */
	    {
	    	_bt_relbuf(rel, buf, BT_WRITE);
		return ( _bt_insertonpg(rel, rbuf, stack, keysz, 
		    			scankey, btitem, afteritem) );
	    }
	    _bt_relbuf(rel, rbuf, BT_WRITE);
	}
V
Vadim B. Mikheev 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
	/*
	 * If after splitting un-chained page we'll got chain of pages 
	 * with duplicates then we want to know 
	 * 1. on which of two pages new btitem will go (current
	 *    _bt_findsplitloc is quite bad);
	 * 2. what parent (if there's one) thinking about it
	 *    (remember about deletions)
	 */
	else if ( !( lpageop->btpo_flags & BTP_CHAIN ) )
	{
    	    OffsetNumber start = ( P_RIGHTMOST(lpageop) ) ? P_HIKEY : P_FIRSTKEY;
    	    Size llimit;
    	    
    	    maxoff = PageGetMaxOffsetNumber (page);
    	    llimit = PageGetPageSize(page) - sizeof (PageHeaderData) - 
    	    		DOUBLEALIGN (sizeof (BTPageOpaqueData)) 
    	    		+ sizeof(ItemIdData);
    	    llimit /= 2;
    	    firstright = _bt_findsplitloc(rel, page, start, maxoff, llimit);
    	    
    	    if ( _bt_itemcmp (rel, keysz, 
	    	(BTItem) PageGetItem(page, PageGetItemId(page, start)),
	    	(BTItem) PageGetItem(page, PageGetItemId(page, firstright)), 
	    			BTEqualStrategyNumber) )
	    {
    	    	if ( _bt_skeycmp (rel, keysz, scankey, page, 
    	    				PageGetItemId(page, firstright), 
    	    				BTLessStrategyNumber) )
    	    	/* 
    	    	 * force moving current items to the new page:
    	    	 * new item will go on the current page.
    	    	 */
    	    	    firstright = start;
    	    	else
    	    	/* 
    	    	 * new btitem >= firstright, start item == firstright -
    	    	 * new chain of duplicates: if this non-leftmost leaf
    	    	 * page and parent item < start item then force moving
    	    	 * all items to the new page - current page will be
    	    	 * "empty" after it.
    	    	 */
    	    	{
    	    	    if ( !P_LEFTMOST (lpageop) && 
    	    	    	( lpageop->btpo_flags & BTP_LEAF ) )
    	    	    {
	    		ItemPointerSet(&(stack->bts_btitem->bti_itup.t_tid), 
			       bknum, P_HIKEY);
	    		pbuf = _bt_getstackbuf(rel, stack, BT_WRITE);
	    		if ( _bt_itemcmp (rel, keysz, stack->bts_btitem, 
	    				(BTItem) PageGetItem(page, 
	    					PageGetItemId(page, start)),
	                    		BTLessStrategyNumber) )
	                {
    	    	    	    firstright = start;
    	    	    	    shifted = true;
    	    	    	}
			_bt_relbuf(rel, pbuf, BT_WRITE);
    	    	    }
    	    	}
    	    }	/* else - no new chain if start item < firstright one */
	}
478 479
	
	/* split the buffer into left and right halves */
V
Vadim B. Mikheev 已提交
480
	rbuf = _bt_split(rel, buf, firstright);
481 482 483 484 485 486 487 488 489 490 491 492 493 494
	
	/* which new page (left half or right half) gets the tuple? */
	if (_bt_goesonpg(rel, buf, keysz, scankey, afteritem)) {
	    /* left page */
	    itup_off = _bt_pgaddtup(rel, buf, keysz, scankey,
				    itemsz, btitem, afteritem);
	    itup_blkno = BufferGetBlockNumber(buf);
	} else {
	    /* right page */
	    itup_off = _bt_pgaddtup(rel, rbuf, keysz, scankey,
				    itemsz, btitem, afteritem);
	    itup_blkno = BufferGetBlockNumber(rbuf);
	}
	
V
Vadim B. Mikheev 已提交
495 496 497 498 499 500 501 502 503 504 505 506
    	maxoff = PageGetMaxOffsetNumber (page);
    	if ( shifted )
    	{
    	    if ( maxoff > P_FIRSTKEY )
    	    	elog (FATAL, "btree: shifted page is not empty");
    	    lowLeftItem = (BTItem) NULL;
    	}
    	else
    	{
    	    if ( maxoff < P_FIRSTKEY )
    	    	elog (FATAL, "btree: un-shifted page is empty");
	    lowLeftItem = (BTItem) PageGetItem(page,
V
Vadim B. Mikheev 已提交
507
					 PageGetItemId(page, P_FIRSTKEY));
V
Vadim B. Mikheev 已提交
508
	    if ( _bt_itemcmp (rel, keysz, lowLeftItem, 
V
Vadim B. Mikheev 已提交
509 510
	    	(BTItem) PageGetItem(page, PageGetItemId(page, P_HIKEY)), 
	    			BTEqualStrategyNumber) ) 
V
Vadim B. Mikheev 已提交
511 512
	    	lpageop->btpo_flags |= BTP_CHAIN;
	}
V
Vadim B. Mikheev 已提交
513

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
	/*
	 *  By here,
	 *
	 *	+  our target page has been split;
	 *	+  the original tuple has been inserted;
	 *	+  we have write locks on both the old (left half) and new
	 *	   (right half) buffers, after the split; and
	 *	+  we have the key we want to insert into the parent.
	 *
	 *  Do the parent insertion.  We need to hold onto the locks for
	 *  the child pages until we locate the parent, but we can release
	 *  them before doing the actual insertion (see Lehman and Yao for
	 *  the reasoning).
	 */
	
	if (stack == (BTStack) NULL) {
	    
	    /* create a new root node and release the split buffers */
	    _bt_newroot(rel, buf, rbuf);
	    _bt_relbuf(rel, buf, BT_WRITE);
	    _bt_relbuf(rel, rbuf, BT_WRITE);
	    
	} else {
V
Vadim B. Mikheev 已提交
537 538 539 540 541
    	    ScanKey newskey;
    	    InsertIndexResult newres;
    	    BTItem new_item;
    	    OffsetNumber upditem_offset = P_HIKEY;
    	    bool do_update = false;
V
Vadim B. Mikheev 已提交
542 543
    	    bool update_in_place = true;
    	    bool parent_chained;
544 545 546 547 548 549 550

	    /* form a index tuple that points at the new right page */
	    rbknum = BufferGetBlockNumber(rbuf);
	    rpage = BufferGetPage(rbuf);
	    rpageop = (BTPageOpaque) PageGetSpecialPointer(rpage);
	    
	    /*
V
Vadim B. Mikheev 已提交
551
	     *  By convention, the first entry (1) on every
552 553
	     *  non-rightmost page is the high key for that page.  In
	     *  order to get the lowest key on the new right page, we
V
Vadim B. Mikheev 已提交
554
	     *  actually look at its second (2) entry.
555 556
	     */
	    
V
Vadim B. Mikheev 已提交
557 558
	    if (! P_RIGHTMOST(rpageop))
	    {
559 560
		ritem = (BTItem) PageGetItem(rpage,
					     PageGetItemId(rpage, P_FIRSTKEY));
V
Vadim B. Mikheev 已提交
561 562 563 564 565 566 567
    		if ( _bt_itemcmp (rel, keysz, ritem, 
	    		(BTItem) PageGetItem(rpage, 
	    				PageGetItemId(rpage, P_HIKEY)), 
	    			BTEqualStrategyNumber) ) 
	    	rpageop->btpo_flags |= BTP_CHAIN;
	    }
	    else
568 569 570 571 572 573 574 575
		ritem = (BTItem) PageGetItem(rpage,
					     PageGetItemId(rpage, P_HIKEY));
	    
	    /* get a unique btitem for this key */
	    new_item = _bt_formitem(&(ritem->bti_itup));
	    
	    ItemPointerSet(&(new_item->bti_itup.t_tid), rbknum, P_HIKEY);
	    
V
Vadim B. Mikheev 已提交
576 577 578 579 580 581 582 583 584
	    /* 
	     * Find the parent buffer and get the parent page.
	     *
	     * Oops - if we were moved right then we need to
	     * change stack item! We want to find parent pointing to
	     * where we are, right ?	- vadim 05/27/97
	     */
	    ItemPointerSet(&(stack->bts_btitem->bti_itup.t_tid), 
			       bknum, P_HIKEY);
585
	    pbuf = _bt_getstackbuf(rel, stack, BT_WRITE);
V
Vadim B. Mikheev 已提交
586 587
	    ppage = BufferGetPage(pbuf);
	    ppageop = (BTPageOpaque) PageGetSpecialPointer(ppage);
V
Vadim B. Mikheev 已提交
588 589 590 591
	    parent_chained = (( ppageop->btpo_flags & BTP_CHAIN )) ? true : false;
	    
	    if ( parent_chained && !left_chained )
	    	elog (FATAL, "nbtree: unexpected chained parent of unchained page");
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
	    
	    /*
	     *  If the key of new_item is < than the key of the item
	     *  in the parent page pointing to the left page
	     *  (stack->bts_btitem), we have to update the latter key;
	     *  otherwise the keys on the parent page wouldn't be
	     *  monotonically increasing after we inserted the new
	     *  pointer to the right page (new_item). This only
	     *  happens if our left page is the leftmost page and a
	     *  new minimum key had been inserted before, which is not
	     *  reflected in the parent page but didn't matter so
	     *  far. If there are duplicate keys and this new minimum
	     *  key spills over to our new right page, we get an
	     *  inconsistency if we don't update the left key in the
	     *  parent page.
V
Vadim B. Mikheev 已提交
607 608 609 610 611
	     *
	     *  Also, new duplicates handling code require us to update
	     *  parent item if some smaller items left on the left page 
	     *  (which is possible in splitting leftmost page) and 
	     *  current parent item == new_item.	- vadim 05/27/97
612
	     */
V
Vadim B. Mikheev 已提交
613 614
	    if ( _bt_itemcmp (rel, keysz, stack->bts_btitem, new_item,
	                    			BTGreaterStrategyNumber) ||
V
Vadim B. Mikheev 已提交
615 616
	    		( !shifted && 
	    		  _bt_itemcmp(rel, keysz, stack->bts_btitem, 
V
Vadim B. Mikheev 已提交
617 618 619 620 621
	    				new_item, BTEqualStrategyNumber) &&
	    		  _bt_itemcmp(rel, keysz, lowLeftItem, 
	    				new_item, BTLessStrategyNumber) ) )
	    {
	        do_update = true;
622
		/* 
V
Vadim B. Mikheev 已提交
623 624
		 * figure out which key is leftmost (if the parent page
		 * is rightmost, too, it must be the root)
625
		 */
V
Vadim B. Mikheev 已提交
626 627 628 629 630 631
		if(P_RIGHTMOST(ppageop))
		    upditem_offset = P_HIKEY;
		else
		    upditem_offset = P_FIRSTKEY;
		if ( !P_LEFTMOST(lpageop) || 
			stack->bts_offset != upditem_offset )
632 633
		    elog (FATAL, "btree: items are out of order (leftmost %d, stack %u, update %u)",
		    	P_LEFTMOST(lpageop), stack->bts_offset, upditem_offset);
V
Vadim B. Mikheev 已提交
634 635 636 637
	    }
	    
	    if ( do_update )
	    {
V
Vadim B. Mikheev 已提交
638 639 640 641 642 643 644 645
	    	if ( shifted )
    	    	    elog (FATAL, "btree: attempt to update parent for shifted page");
		/* 
		 * Try to update in place. If out parent page is chained
		 * then we must forse insertion.
		 */
	    	if ( !parent_chained && 
	    		DOUBLEALIGN (IndexTupleDSize (lowLeftItem->bti_itup)) ==
646 647 648
       			DOUBLEALIGN (IndexTupleDSize (stack->bts_btitem->bti_itup)) ) 
       		{
		    _bt_updateitem(rel, keysz, pbuf, 
649
		    		stack->bts_btitem, lowLeftItem);
650 651 652 653 654
		    _bt_relbuf(rel, buf, BT_WRITE);
		    _bt_relbuf(rel, rbuf, BT_WRITE);
		}
		else
		{
V
Vadim B. Mikheev 已提交
655
		    update_in_place = false;
V
Vadim B. Mikheev 已提交
656
       		    PageIndexTupleDelete(ppage, upditem_offset);
657
		
658 659 660
		   /* 
		    * don't write anything out yet--we still have the write
		    * lock, and now we call another _bt_insertonpg to
V
Vadim B. Mikheev 已提交
661 662 663 664
		    * insert the correct key.
		    * First, make a new item, using the tuple data from
		    * lowLeftItem. Point it to the left child.
		    * Update it on the stack at the same time.
665 666 667 668
		    */
		    pfree(stack->bts_btitem);
		    stack->bts_btitem = _bt_formitem(&(lowLeftItem->bti_itup));
		    ItemPointerSet(&(stack->bts_btitem->bti_itup.t_tid), 
669 670
			       bknum, P_HIKEY);
		
V
Vadim B. Mikheev 已提交
671 672 673 674 675
		    /* 
		     * Unlock the children before doing this
		     *
		     * Mmm ... I foresee problems here.	- vadim 06/10/97
		     */
676 677
		    _bt_relbuf(rel, buf, BT_WRITE);
		    _bt_relbuf(rel, rbuf, BT_WRITE);
678
		
679
		    /* 
V
Vadim B. Mikheev 已提交
680
		     * A regular _bt_binsrch should find the right place to
V
Vadim B. Mikheev 已提交
681 682
		     * put the new entry, since it should be lower than any 
		     * other key on the page. 
V
Vadim B. Mikheev 已提交
683
		     * Therefore set afteritem to NULL.
684 685 686
		     */
		    newskey = _bt_mkscankey(rel, &(stack->bts_btitem->bti_itup));
		    newres = _bt_insertonpg(rel, pbuf, stack->bts_parent,
687 688 689
					keysz, newskey, stack->bts_btitem,
					NULL);

690 691
		    pfree(newres);
		    pfree(newskey);
692
	    
693 694 695 696 697 698 699 700 701 702 703
		    /* 
		     * we have now lost our lock on the parent buffer, and
		     * need to get it back.
		     */
		    pbuf = _bt_getstackbuf(rel, stack, BT_WRITE);
		}
	    }
	    else
	    {
	    	_bt_relbuf(rel, buf, BT_WRITE);
	    	_bt_relbuf(rel, rbuf, BT_WRITE);
704
	    }
705 706
	    
	    newskey = _bt_mkscankey(rel, &(new_item->bti_itup));
V
Vadim B. Mikheev 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
	    
	    afteritem = stack->bts_btitem;
	    if ( parent_chained && !update_in_place )
	    {
	    	ppage = BufferGetPage(pbuf);
	    	ppageop = (BTPageOpaque) PageGetSpecialPointer(ppage);
	    	if ( ppageop->btpo_flags & BTP_CHAIN )
	    	    elog (FATAL, "btree: unexpected BTP_CHAIN flag in parent after update");
	    	if ( P_RIGHTMOST (ppageop) )
	    	    elog (FATAL, "btree: chained parent is RIGHTMOST after update");
	    	maxoff = PageGetMaxOffsetNumber (ppage);
	    	if ( maxoff != P_FIRSTKEY )
	    	    elog (FATAL, "btree: FIRSTKEY was unexpected in parent after update");
    	    	if ( _bt_skeycmp (rel, keysz, newskey, ppage, 
    	    				PageGetItemId(ppage, P_FIRSTKEY), 
    	    				BTLessEqualStrategyNumber) )
	    	    elog (FATAL, "btree: parent FIRSTKEY is >= duplicate key after update");
    	    	if ( !_bt_skeycmp (rel, keysz, newskey, ppage, 
    	    				PageGetItemId(ppage, P_HIKEY), 
    	    				BTEqualStrategyNumber) )
	    	    elog (FATAL, "btree: parent HIGHKEY is not equal duplicate key after update");
	    	afteritem = (BTItem) NULL;
	    }
	    else if ( left_chained && !update_in_place )
	    {
	    	ppage = BufferGetPage(pbuf);
	    	ppageop = (BTPageOpaque) PageGetSpecialPointer(ppage);
	    	if ( !P_RIGHTMOST (ppageop) &&
    	    		_bt_skeycmp (rel, keysz, newskey, ppage, 
    	    				PageGetItemId(ppage, P_HIKEY), 
    	    				BTGreaterStrategyNumber) )
	    	    afteritem = (BTItem) NULL;
	    }
	    if ( afteritem == (BTItem) NULL)
	    {
		rbuf = _bt_getbuf(rel, ppageop->btpo_next, BT_WRITE);
		_bt_relbuf(rel, pbuf, BT_WRITE);
		pbuf = rbuf;
	    }
	    
747 748
	    newres = _bt_insertonpg(rel, pbuf, stack->bts_parent,
				    keysz, newskey, new_item,
V
Vadim B. Mikheev 已提交
749
				    afteritem);
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
	    
	    /* be tidy */
	    pfree(newres);
	    pfree(newskey);
	    pfree(new_item);
	}
    } else {
	itup_off = _bt_pgaddtup(rel, buf, keysz, scankey,
				itemsz, btitem, afteritem);
	itup_blkno = BufferGetBlockNumber(buf);
	
	_bt_relbuf(rel, buf, BT_WRITE);
    }
    
    /* by here, the new tuple is inserted */
    res = (InsertIndexResult) palloc(sizeof(InsertIndexResultData));
    ItemPointerSet(&(res->pointerData), itup_blkno, itup_off);
    
    return (res);
}

/*
 *  _bt_split() -- split a page in the btree.
 *
 *	On entry, buf is the page to split, and is write-locked and pinned.
 *	Returns the new right sibling of buf, pinned and write-locked.  The
 *	pin and lock on buf are maintained.
 */
static Buffer
V
Vadim B. Mikheev 已提交
779
_bt_split(Relation rel, Buffer buf, OffsetNumber firstright)
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
{
    Buffer rbuf;
    Page origpage;
    Page leftpage, rightpage;
    BTPageOpaque ropaque, lopaque, oopaque;
    Buffer sbuf;
    Page spage;
    BTPageOpaque sopaque;
    Size itemsz;
    ItemId itemid;
    BTItem item;
    OffsetNumber leftoff, rightoff;
    OffsetNumber start;
    OffsetNumber maxoff;
    OffsetNumber i;
    
    rbuf = _bt_getbuf(rel, P_NEW, BT_WRITE);
    origpage = BufferGetPage(buf);
    leftpage = PageGetTempPage(origpage, sizeof(BTPageOpaqueData));
    rightpage = BufferGetPage(rbuf);
    
    _bt_pageinit(rightpage, BufferGetPageSize(rbuf));
    _bt_pageinit(leftpage, BufferGetPageSize(buf));
    
    /* init btree private data */
    oopaque = (BTPageOpaque) PageGetSpecialPointer(origpage);
    lopaque = (BTPageOpaque) PageGetSpecialPointer(leftpage);
    ropaque = (BTPageOpaque) PageGetSpecialPointer(rightpage);
    
    /* if we're splitting this page, it won't be the root when we're done */
    oopaque->btpo_flags &= ~BTP_ROOT;
V
Vadim B. Mikheev 已提交
811
    oopaque->btpo_flags &= ~BTP_CHAIN;
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
    lopaque->btpo_flags = ropaque->btpo_flags = oopaque->btpo_flags;
    lopaque->btpo_prev = oopaque->btpo_prev;
    ropaque->btpo_prev = BufferGetBlockNumber(buf);
    lopaque->btpo_next = BufferGetBlockNumber(rbuf);
    ropaque->btpo_next = oopaque->btpo_next;
    
    /*
     *  If the page we're splitting is not the rightmost page at its
     *  level in the tree, then the first (0) entry on the page is the
     *  high key for the page.  We need to copy that to the right
     *  half.  Otherwise (meaning the rightmost page case), we should
     *  treat the line pointers beginning at zero as user data.
     *
     *  We leave a blank space at the start of the line table for the
     *  left page.  We'll come back later and fill it in with the high
     *  key item we get from the right key.
     */
    
    leftoff = P_FIRSTKEY;
    ropaque->btpo_next = oopaque->btpo_next;
    if (! P_RIGHTMOST(oopaque)) {
	/* splitting a non-rightmost page, start at the first data item */
	start = P_FIRSTKEY;

V
Vadim B. Mikheev 已提交
836 837 838
	itemid = PageGetItemId(origpage, P_HIKEY);
	itemsz = ItemIdGetLength(itemid);
	item = (BTItem) PageGetItem(origpage, itemid);
839 840
	if ( PageAddItem(rightpage, (Item) item, itemsz, P_HIKEY, LP_USED) == InvalidOffsetNumber )
	    elog (FATAL, "btree: failed to add hikey to the right sibling");
841 842 843 844 845 846 847 848 849
	rightoff = P_FIRSTKEY;
    } else {
	/* splitting a rightmost page, "high key" is the first data item */
	start = P_HIKEY;

	/* the new rightmost page will not have a high key */
	rightoff = P_HIKEY;
    }
    maxoff = PageGetMaxOffsetNumber(origpage);
V
Vadim B. Mikheev 已提交
850 851 852 853 854
    if ( firstright == InvalidOffsetNumber )
    {
    	Size llimit = PageGetFreeSpace(leftpage) / 2;
    	firstright = _bt_findsplitloc(rel, origpage, start, maxoff, llimit);
    }
855 856 857 858 859 860 861 862
    
    for (i = start; i <= maxoff; i = OffsetNumberNext(i)) {
	itemid = PageGetItemId(origpage, i);
	itemsz = ItemIdGetLength(itemid);
	item = (BTItem) PageGetItem(origpage, itemid);
	
	/* decide which page to put it on */
	if (i < firstright) {
863 864 865
	    if ( PageAddItem(leftpage, (Item) item, itemsz, leftoff, 
			       LP_USED) == InvalidOffsetNumber )
		elog (FATAL, "btree: failed to add item to the left sibling");
866 867
	    leftoff = OffsetNumberNext(leftoff);
	} else {
868 869 870
	    if ( PageAddItem(rightpage, (Item) item, itemsz, rightoff,
			       LP_USED) == InvalidOffsetNumber )
		elog (FATAL, "btree: failed to add item to the right sibling");
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
	    rightoff = OffsetNumberNext(rightoff);
	}
    }
    
    /*
     *  Okay, page has been split, high key on right page is correct.  Now
     *  set the high key on the left page to be the min key on the right
     *  page.
     */
    
    if (P_RIGHTMOST(ropaque)) {
	itemid = PageGetItemId(rightpage, P_HIKEY);
    } else {
	itemid = PageGetItemId(rightpage, P_FIRSTKEY);
    }
    itemsz = ItemIdGetLength(itemid);
    item = (BTItem) PageGetItem(rightpage, itemid);
    
    /*
     *  We left a hole for the high key on the left page; fill it.  The
     *  modal crap is to tell the page manager to put the new item on the
     *  page and not screw around with anything else.  Whoever designed
     *  this interface has presumably crawled back into the dung heap they
     *  came from.  No one here will admit to it.
     */
    
    PageManagerModeSet(OverwritePageManagerMode);
898 899
    if ( PageAddItem(leftpage, (Item) item, itemsz, P_HIKEY, LP_USED) == InvalidOffsetNumber )
    	elog (FATAL, "btree: failed to add hikey to the left sibling");
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
    PageManagerModeSet(ShufflePageManagerMode);
    
    /*
     *  By here, the original data page has been split into two new halves,
     *  and these are correct.  The algorithm requires that the left page
     *  never move during a split, so we copy the new left page back on top
     *  of the original.  Note that this is not a waste of time, since we
     *  also require (in the page management code) that the center of a
     *  page always be clean, and the most efficient way to guarantee this
     *  is just to compact the data by reinserting it into a new left page.
     */
    
    PageRestoreTempPage(leftpage, origpage);
    
    /* write these guys out */
    _bt_wrtnorelbuf(rel, rbuf);
    _bt_wrtnorelbuf(rel, buf);
    
    /*
     *  Finally, we need to grab the right sibling (if any) and fix the
     *  prev pointer there.  We are guaranteed that this is deadlock-free
     *  since no other writer will be moving holding a lock on that page
     *  and trying to move left, and all readers release locks on a page
     *  before trying to fetch its neighbors.
     */
    
    if (! P_RIGHTMOST(ropaque)) {
	sbuf = _bt_getbuf(rel, ropaque->btpo_next, BT_WRITE);
	spage = BufferGetPage(sbuf);
	sopaque = (BTPageOpaque) PageGetSpecialPointer(spage);
	sopaque->btpo_prev = BufferGetBlockNumber(rbuf);
	
	/* write and release the old right sibling */
	_bt_wrtbuf(rel, sbuf);
    }
    
    /* split's done */
    return (rbuf);
}

/*
 *  _bt_findsplitloc() -- find a safe place to split a page.
 *
 *	In order to guarantee the proper handling of searches for duplicate
 *	keys, the first duplicate in the chain must either be the first
 *	item on the page after the split, or the entire chain must be on
 *	one of the two pages.  That is,
 *		[1 2 2 2 3 4 5]
 *	must become
 *		[1] [2 2 2 3 4 5]
 *	or
 *		[1 2 2 2] [3 4 5]
 *	but not
 *		[1 2 2] [2 3 4 5].
 *	However,
 *		[2 2 2 2 2 3 4]
 *	may be split as
 *		[2 2 2 2] [2 3 4].
 */
static OffsetNumber
_bt_findsplitloc(Relation rel,
		 Page page,
		 OffsetNumber start,
		 OffsetNumber maxoff,
		 Size llimit)
{
    OffsetNumber i;
    OffsetNumber saferight;
    ItemId nxtitemid, safeitemid;
    BTItem safeitem, nxtitem;
    Size nbytes;
    int natts;
    
V
Vadim B. Mikheev 已提交
973 974 975
    if ( start >= maxoff )
    	elog (FATAL, "btree: cannot split if start (%d) >= maxoff (%d)",
    	    		start, maxoff);
976 977 978 979 980 981 982 983
    natts = rel->rd_rel->relnatts;
    saferight = start;
    safeitemid = PageGetItemId(page, saferight);
    nbytes = ItemIdGetLength(safeitemid) + sizeof(ItemIdData);
    safeitem = (BTItem) PageGetItem(page, safeitemid);
    
    i = OffsetNumberNext(start);
    
V
Vadim B. Mikheev 已提交
984 985
    while (nbytes < llimit)
    {
986 987 988 989 990
	/* check the next item on the page */
	nxtitemid = PageGetItemId(page, i);
	nbytes += (ItemIdGetLength(nxtitemid) + sizeof(ItemIdData));
	nxtitem = (BTItem) PageGetItem(page, nxtitemid);

991 992 993 994 995 996 997 998 999 1000
	/* 
	 * Test against last known safe item:
	 * if the tuple we're looking at isn't equal to the last safe 
	 * one we saw, then it's our new safe tuple.
	 */
	if ( !_bt_itemcmp (rel, natts, 
			safeitem, nxtitem, BTEqualStrategyNumber) )
	{
	    safeitem = nxtitem;
	    saferight = i;
1001
	}
V
Vadim B. Mikheev 已提交
1002 1003 1004 1005
	if ( i < maxoff )
	    i = OffsetNumberNext(i);
	else
	    break;
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
    }
    
    /*
     *  If the chain of dups starts at the beginning of the page and extends
     *  past the halfway mark, we can split it in the middle.
     */
    
    if (saferight == start)
	saferight = i;
    
V
Vadim B. Mikheev 已提交
1016 1017 1018
    if ( saferight == maxoff  && ( maxoff - start ) > 1 )
    	saferight = start + ( maxoff - start ) / 2;
    
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
    return (saferight);
}

/*
 *  _bt_newroot() -- Create a new root page for the index.
 *
 *	We've just split the old root page and need to create a new one.
 *	In order to do this, we add a new root page to the file, then lock
 *	the metadata page and update it.  This is guaranteed to be deadlock-
 *	free, because all readers release their locks on the metadata page
 *	before trying to lock the root, and all writers lock the root before
 *	trying to lock the metadata page.  We have a write lock on the old
 *	root page, so we have not introduced any cycles into the waits-for
 *	graph.
 *
 *	On entry, lbuf (the old root) and rbuf (its new peer) are write-
 *	locked.  We don't drop the locks in this routine; that's done by
 *	the caller.  On exit, a new root page exists with entries for the
 *	two new children.  The new root page is neither pinned nor locked.
 */
static void
_bt_newroot(Relation rel, Buffer lbuf, Buffer rbuf)
{
    Buffer rootbuf;
    Page lpage, rpage, rootpage;
    BlockNumber lbkno, rbkno;
    BlockNumber rootbknum;
    BTPageOpaque rootopaque;
    ItemId itemid;
    BTItem item;
    Size itemsz;
    BTItem new_item;
    
    /* get a new root page */
    rootbuf = _bt_getbuf(rel, P_NEW, BT_WRITE);
    rootpage = BufferGetPage(rootbuf);
    _bt_pageinit(rootpage, BufferGetPageSize(rootbuf));
    
    /* set btree special data */
    rootopaque = (BTPageOpaque) PageGetSpecialPointer(rootpage);
    rootopaque->btpo_prev = rootopaque->btpo_next = P_NONE;
    rootopaque->btpo_flags |= BTP_ROOT;
    
    /*
     *  Insert the internal tuple pointers.
     */
    
    lbkno = BufferGetBlockNumber(lbuf);
    rbkno = BufferGetBlockNumber(rbuf);
    lpage = BufferGetPage(lbuf);
    rpage = BufferGetPage(rbuf);
1070

1071 1072 1073 1074 1075 1076 1077 1078
    /*
     * step over the high key on the left page while building the 
     * left page pointer.
     */
    itemid = PageGetItemId(lpage, P_FIRSTKEY);
    itemsz = ItemIdGetLength(itemid);
    item = (BTItem) PageGetItem(lpage, itemid);
    new_item = _bt_formitem(&(item->bti_itup));
V
Vadim B. Mikheev 已提交
1079
    ItemPointerSet(&(new_item->bti_itup.t_tid), lbkno, P_HIKEY);
1080 1081 1082 1083 1084 1085
    
    /*
     * insert the left page pointer into the new root page.  the root
     * page is the rightmost page on its level so the "high key" item
     * is the first data item.
     */
1086 1087
    if ( PageAddItem(rootpage, (Item) new_item, itemsz, P_HIKEY, LP_USED) == InvalidOffsetNumber )
    	elog (FATAL, "btree: failed to add leftkey to new root page");
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
    pfree(new_item);
    
    /*
     * the right page is the rightmost page on the second level, so 
     * the "high key" item is the first data item on that page as well.
     */
    itemid = PageGetItemId(rpage, P_HIKEY);
    itemsz = ItemIdGetLength(itemid);
    item = (BTItem) PageGetItem(rpage, itemid);
    new_item = _bt_formitem(&(item->bti_itup));
    ItemPointerSet(&(new_item->bti_itup.t_tid), rbkno, P_HIKEY);
    
    /*
     * insert the right page pointer into the new root page.
     */
1103 1104
    if ( PageAddItem(rootpage, (Item) new_item, itemsz, P_FIRSTKEY, LP_USED) == InvalidOffsetNumber )
    	elog (FATAL, "btree: failed to add rightkey to new root page");
1105 1106 1107 1108 1109 1110 1111
    pfree(new_item);
    
    /* write and let go of the root buffer */
    rootbknum = BufferGetBlockNumber(rootbuf);
    _bt_wrtbuf(rel, rootbuf);
    
    /* update metadata page with new root block number */
1112
    _bt_metaproot(rel, rootbknum, 0);
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
}

/*
 *  _bt_pgaddtup() -- add a tuple to a particular page in the index.
 *
 *	This routine adds the tuple to the page as requested, and keeps the
 *	write lock and reference associated with the page's buffer.  It is
 *	an error to call pgaddtup() without a write lock and reference.  If
 *	afteritem is non-null, it's the item that we expect our new item
 *	to follow.  Otherwise, we do a binary search for the correct place
 *	and insert the new item there.
 */
static OffsetNumber
_bt_pgaddtup(Relation rel,
	     Buffer buf,
	     int keysz,
	     ScanKey itup_scankey,
	     Size itemsize,
	     BTItem btitem,
	     BTItem afteritem)
{
    OffsetNumber itup_off;
    OffsetNumber first;
    Page page;
    BTPageOpaque opaque;
    BTItem chkitem;
    
    page = BufferGetPage(buf);
    opaque = (BTPageOpaque) PageGetSpecialPointer(page);
    first = P_RIGHTMOST(opaque) ? P_HIKEY : P_FIRSTKEY;
    
    if (afteritem == (BTItem) NULL) {
	itup_off = _bt_binsrch(rel, buf, keysz, itup_scankey, BT_INSERTION);
    } else {
	itup_off = first;
	
	do {
	    chkitem =
		(BTItem) PageGetItem(page, PageGetItemId(page, itup_off));
	    itup_off = OffsetNumberNext(itup_off);
1153
	} while ( ! BTItemSame (chkitem, afteritem) );
1154 1155
    }

1156 1157
    if ( PageAddItem(page, (Item) btitem, itemsize, itup_off, LP_USED) == InvalidOffsetNumber )
    	elog (FATAL, "btree: failed to add item to the page");
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
    
    /* write the buffer, but hold our lock */
    _bt_wrtnorelbuf(rel, buf);
    
    return (itup_off);
}

/*
 *  _bt_goesonpg() -- Does a new tuple belong on this page?
 *
 *	This is part of the complexity introduced by allowing duplicate
 *	keys into the index.  The tuple belongs on this page if:
 *
 *		+ there is no page to the right of this one; or
 *		+ it is less than the high key on the page; or
 *		+ the item it is to follow ("afteritem") appears on this
 *		  page.
 */
static bool
_bt_goesonpg(Relation rel,
	     Buffer buf,
	     Size keysz,
	     ScanKey scankey,
	     BTItem afteritem)
{
    Page page;
    ItemId hikey;
    BTPageOpaque opaque;
    BTItem chkitem;
    OffsetNumber offnum, maxoff;
    bool found;
    
    page = BufferGetPage(buf);
    
    /* no right neighbor? */
    opaque = (BTPageOpaque) PageGetSpecialPointer(page);
    if (P_RIGHTMOST(opaque))
	return (true);
    
    /*
     *  this is a non-rightmost page, so it must have a high key item.
     *
     *  If the scan key is < the high key (the min key on the next page),
     *  then it for sure belongs here.
     */
    hikey = PageGetItemId(page, P_HIKEY);
    if (_bt_skeycmp(rel, keysz, scankey, page, hikey, BTLessStrategyNumber))
	return (true);
    
    /*
     *  If the scan key is > the high key, then it for sure doesn't belong
     *  here.
     */
    
    if (_bt_skeycmp(rel, keysz, scankey, page, hikey, BTGreaterStrategyNumber))
	return (false);
    
    /*
     *  If we have no adjacency information, and the item is equal to the
     *  high key on the page (by here it is), then the item does not belong
     *  on this page.
V
Vadim B. Mikheev 已提交
1219 1220
     *
     *  Now it's not true in all cases.		- vadim 06/10/97
1221 1222 1223
     */
    
    if (afteritem == (BTItem) NULL)
V
Vadim B. Mikheev 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232
    {
    	if ( opaque->btpo_flags & BTP_LEAF )
	    return (false);
    	if ( opaque->btpo_flags & BTP_CHAIN )
	    return (true);
    	if ( _bt_skeycmp (rel, keysz, scankey, page, 
    				PageGetItemId(page, P_FIRSTKEY), 
    				BTEqualStrategyNumber) )
	    return (true);
1233
	return (false);
V
Vadim B. Mikheev 已提交
1234
    }
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
    
    /* damn, have to work for it.  i hate that. */
    maxoff = PageGetMaxOffsetNumber(page);
    
    /*
     *  Search the entire page for the afteroid.  We need to do this, rather
     *  than doing a binary search and starting from there, because if the
     *  key we're searching for is the leftmost key in the tree at this
     *  level, then a binary search will do the wrong thing.  Splits are
     *  pretty infrequent, so the cost isn't as bad as it could be.
     */
    
    found = false;
    for (offnum = P_FIRSTKEY;
	 offnum <= maxoff;
	 offnum = OffsetNumberNext(offnum)) {
	chkitem = (BTItem) PageGetItem(page, PageGetItemId(page, offnum));
1252 1253

	if ( BTItemSame (chkitem, afteritem) ) {
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
	    found = true;
	    break;
	}
    }
    
    return (found);
}

/*
 *	_bt_itemcmp() -- compare item1 to item2 using a requested
 *		         strategy (<, <=, =, >=, >)
 *
 */
bool
_bt_itemcmp(Relation rel,
	    Size keysz,
	    BTItem item1,
	    BTItem item2,
	    StrategyNumber strat)
{
    TupleDesc tupDes;
    IndexTuple indexTuple1, indexTuple2;
    Datum attrDatum1, attrDatum2;
    int i;
V
Vadim B. Mikheev 已提交
1278
    bool isFirstNull, isSecondNull;
1279
    bool compare;
V
Vadim B. Mikheev 已提交
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
    bool useEqual = false;
    
    if ( strat == BTLessEqualStrategyNumber )
    {
	useEqual = true;
	strat = BTLessStrategyNumber;
    }
    else if ( strat == BTGreaterEqualStrategyNumber )
    {
	useEqual = true;
	strat = BTGreaterStrategyNumber;
    }
1292 1293 1294 1295 1296 1297
    
    tupDes = RelationGetTupleDescriptor(rel);
    indexTuple1 = &(item1->bti_itup);
    indexTuple2 = &(item2->bti_itup);
    
    for (i = 1; i <= keysz; i++) {
V
Vadim B. Mikheev 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
	attrDatum1 = index_getattr(indexTuple1, i, tupDes, &isFirstNull);
	attrDatum2 = index_getattr(indexTuple2, i, tupDes, &isSecondNull);
	
	/* see comments about NULLs handling in btbuild */
	if ( isFirstNull )	/* attr in item1 is NULL */
	{
    	    if ( isSecondNull )	/* attr in item2 is NULL too */
    	    	compare = ( strat == BTEqualStrategyNumber ) ? true : false;
    	    else
    	    	compare = ( strat == BTGreaterStrategyNumber ) ? true : false;
    	}
    	else if ( isSecondNull )	/* attr in item1 is NOT_NULL and */
    	{				/* and attr in item2 is NULL */
    	    compare = ( strat == BTLessStrategyNumber ) ? true : false;
    	}
    	else
    	{
	    compare = _bt_invokestrat(rel, i, strat, attrDatum1, attrDatum2);
	}
	
	if ( compare )	/* true for one of ">, <, =" */
	{
	    if ( strat != BTEqualStrategyNumber )
	    	return (true);
	}
	else		/* false for one of ">, <, =" */
	{
	    if ( strat == BTEqualStrategyNumber )
		return (false);
	    /*
	     * if original strat was "<=, >=" OR
	     * "<, >" but some attribute(s) left
	     * - need to test for Equality
	     */
	    if ( useEqual || i < keysz )
	    {
	    	if ( isFirstNull || isSecondNull )
	    	    compare = ( isFirstNull && isSecondNull ) ? true : false;
	    	else
		    compare = _bt_invokestrat(rel, i, BTEqualStrategyNumber, 
						attrDatum1, attrDatum2);
		if ( compare )	/* item1' and item2' attributes are equal */
		    continue;	/* - try to compare next attributes */
	    }
1342 1343 1344 1345 1346 1347 1348 1349
	    return (false);
	}
    }
    return (true);
}

/*
 *	_bt_updateitem() -- updates the key of the item identified by the
1350 1351
 *			    oid with the key of newItem (done in place if
 *			    possible)
1352 1353 1354 1355 1356 1357
 *
 */
static void
_bt_updateitem(Relation rel,
	       Size keysz,
	       Buffer buf,
1358
	       BTItem oldItem,
1359 1360 1361 1362 1363 1364
	       BTItem newItem)
{
    Page page;
    OffsetNumber maxoff;
    OffsetNumber i;
    ItemPointerData itemPtrData;
1365
    BTItem item;
1366
    IndexTuple oldIndexTuple, newIndexTuple;
1367
    int first;
1368 1369 1370 1371 1372
    
    page = BufferGetPage(buf);
    maxoff = PageGetMaxOffsetNumber(page);
    
    /* locate item on the page */
1373
    first = P_RIGHTMOST((BTPageOpaque) PageGetSpecialPointer(page))
1374 1375
        ? P_HIKEY : P_FIRSTKEY;
    i = first;
1376 1377 1378
    do {
	item = (BTItem) PageGetItem(page, PageGetItemId(page, i));
	i = OffsetNumberNext(i);
1379
    } while (i <= maxoff && ! BTItemSame (item, oldItem));
1380 1381
    
    /* this should never happen (in theory) */
1382
    if ( ! BTItemSame (item, oldItem) ) {
1383 1384 1385
	elog(FATAL, "_bt_getstackbuf was lying!!");
    }
    
1386 1387 1388 1389
    /*
     * It's  defined by caller (_bt_insertonpg)
     */
    /*
1390 1391 1392 1393 1394
    if(IndexTupleDSize(newItem->bti_itup) >
       IndexTupleDSize(item->bti_itup)) {
	elog(NOTICE, "trying to overwrite a smaller value with a bigger one in _bt_updateitem");
	elog(WARN, "this is not good.");
    }
1395
     */
1396

1397 1398
    oldIndexTuple = &(item->bti_itup);
    newIndexTuple = &(newItem->bti_itup);
1399 1400

	/* keep the original item pointer */
1401 1402 1403 1404
    ItemPointerCopy(&(oldIndexTuple->t_tid), &itemPtrData);
    CopyIndexTuple(newIndexTuple, &oldIndexTuple);
    ItemPointerCopy(&itemPtrData, &(oldIndexTuple->t_tid));
    
1405
}
V
Vadim B. Mikheev 已提交
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447

/*
 * _bt_isequal - used in _bt_doinsert in check for duplicates.
 *
 * Rule is simple: NOT_NULL not equal NULL, NULL not_equal NULL too.
 */
static bool
_bt_isequal (TupleDesc itupdesc, Page page, OffsetNumber offnum,
					int keysz, ScanKey scankey)
{
    Datum datum;
    BTItem btitem;
    IndexTuple itup;
    ScanKey entry;
    AttrNumber attno;
    long result;
    int i;
    bool null;
    
    btitem = (BTItem) PageGetItem(page, PageGetItemId(page, offnum));
    itup = &(btitem->bti_itup);
    
    for (i = 1; i <= keysz; i++)
    {
	entry = &scankey[i - 1];
	attno = entry->sk_attno;
	Assert (attno == i);
	datum = index_getattr(itup, attno, itupdesc, &null);

	/* NULLs are not equal */
	if ( entry->sk_flags & SK_ISNULL || null )
	    return (false);
	
	result = (long) FMGR_PTR2(entry->sk_func, entry->sk_procedure,
					entry->sk_argument, datum);
	if (result != 0)
	    return (false);
    }
    
    /* by here, the keys are equal */
    return (true);
}
V
Vadim B. Mikheev 已提交
1448

B
Bruce Momjian 已提交
1449
#ifdef NOT_USED
V
Vadim B. Mikheev 已提交
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
/*
 * _bt_shift - insert btitem on the passed page after shifting page 
 *	       to the right in the tree.
 *
 * NOTE: tested for shifting leftmost page only, having btitem < hikey.
 */
static InsertIndexResult 
_bt_shift (Relation rel, Buffer buf, BTStack stack, int keysz, 
			ScanKey scankey, BTItem btitem, BTItem hikey)
{
    InsertIndexResult res;
    int itemsz;
    Page page;
    BlockNumber bknum;
    BTPageOpaque pageop;
    Buffer rbuf;
    Page rpage;
    BTPageOpaque rpageop;
    Buffer pbuf;
    Page ppage;
    BTPageOpaque ppageop;
    Buffer nbuf;
    Page npage;
    BTPageOpaque npageop;
    BlockNumber nbknum;
    BTItem nitem;
    OffsetNumber afteroff;
    
    btitem = _bt_formitem(&(btitem->bti_itup));
    hikey = _bt_formitem(&(hikey->bti_itup));

    page = BufferGetPage(buf);

    /* grab new page */
    nbuf = _bt_getbuf(rel, P_NEW, BT_WRITE);
    nbknum = BufferGetBlockNumber(nbuf);
    npage = BufferGetPage(nbuf);
    _bt_pageinit(npage, BufferGetPageSize(nbuf));
    npageop = (BTPageOpaque) PageGetSpecialPointer(npage);
    
    /* copy content of the passed page */
    memmove ((char *) npage, (char *) page, BufferGetPageSize(buf));
    
    /* re-init old (passed) page */
    _bt_pageinit(page, BufferGetPageSize(buf));
    pageop = (BTPageOpaque) PageGetSpecialPointer(page);

    /* init old page opaque */
    pageop->btpo_flags = npageop->btpo_flags;	/* restore flags */
    pageop->btpo_flags &= ~BTP_CHAIN;
    if ( _bt_itemcmp (rel, keysz, hikey, btitem, BTEqualStrategyNumber) ) 
	pageop->btpo_flags |= BTP_CHAIN;
    pageop->btpo_prev = npageop->btpo_prev;	/* restore prev */
    pageop->btpo_next = nbknum;			/* next points to the new page */

    /* init shifted page opaque */
    npageop->btpo_prev = bknum = BufferGetBlockNumber(buf);
    
    /* shifted page is ok, populate old page */

    /* add passed hikey */
    itemsz = IndexTupleDSize(hikey->bti_itup)
	+ (sizeof(BTItemData) - sizeof(IndexTupleData));
    itemsz = DOUBLEALIGN(itemsz);
1514 1515
    if ( PageAddItem(page, (Item) hikey, itemsz, P_HIKEY, LP_USED) == InvalidOffsetNumber )
    	elog (FATAL, "btree: failed to add hikey in _bt_shift");
V
Vadim B. Mikheev 已提交
1516 1517 1518 1519 1520 1521
    pfree (hikey);

    /* add btitem */
    itemsz = IndexTupleDSize(btitem->bti_itup)
	+ (sizeof(BTItemData) - sizeof(IndexTupleData));
    itemsz = DOUBLEALIGN(itemsz);
1522 1523
    if ( PageAddItem(page, (Item) btitem, itemsz, P_FIRSTKEY, LP_USED) == InvalidOffsetNumber )
    	elog (FATAL, "btree: failed to add firstkey in _bt_shift");
V
Vadim B. Mikheev 已提交
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
    pfree (btitem);
    nitem = (BTItem) PageGetItem(page, PageGetItemId(page, P_FIRSTKEY));
    btitem = _bt_formitem(&(nitem->bti_itup));
    ItemPointerSet(&(btitem->bti_itup.t_tid), bknum, P_HIKEY);
    
    /* ok, write them out */
    _bt_wrtnorelbuf(rel, nbuf);
    _bt_wrtnorelbuf(rel, buf);

    /* fix btpo_prev on right sibling of old page */
    if ( !P_RIGHTMOST (npageop) )
    {
	rbuf = _bt_getbuf(rel, npageop->btpo_next, BT_WRITE);
	rpage = BufferGetPage(rbuf);
	rpageop = (BTPageOpaque) PageGetSpecialPointer(rpage);
	rpageop->btpo_prev = nbknum;
	_bt_wrtbuf(rel, rbuf);
    }

    /* get parent pointing to the old page */
    ItemPointerSet(&(stack->bts_btitem->bti_itup.t_tid), 
			       bknum, P_HIKEY);
    pbuf = _bt_getstackbuf(rel, stack, BT_WRITE);
    ppage = BufferGetPage(pbuf);
    ppageop = (BTPageOpaque) PageGetSpecialPointer(ppage);

    _bt_relbuf(rel, nbuf, BT_WRITE);
    _bt_relbuf(rel, buf, BT_WRITE);
    
    /* re-set parent' pointer - we shifted our page to the right ! */
    nitem = (BTItem) PageGetItem (ppage, 
    				PageGetItemId (ppage, stack->bts_offset));
    ItemPointerSet(&(nitem->bti_itup.t_tid), nbknum, P_HIKEY);
    ItemPointerSet(&(stack->bts_btitem->bti_itup.t_tid), nbknum, P_HIKEY);
    _bt_wrtnorelbuf(rel, pbuf);

    /* 
     * Now we want insert into the parent pointer to our old page. It has to
     * be inserted before the pointer to new page. You may get problems here
     * (in the _bt_goesonpg and/or _bt_pgaddtup), but may be not - I don't 
     * know. It works if old page is leftmost (nitem is NULL) and 
     * btitem < hikey and it's all what we need currently. - vadim 05/30/97
     */
    nitem = NULL;
    afteroff = P_FIRSTKEY;
    if ( !P_RIGHTMOST (ppageop) )
    	afteroff = OffsetNumberNext (afteroff);
    if ( stack->bts_offset >= afteroff )
    {
    	afteroff = OffsetNumberPrev (stack->bts_offset);
    	nitem = (BTItem) PageGetItem (ppage, PageGetItemId (ppage, afteroff));
    	nitem = _bt_formitem(&(nitem->bti_itup));
    }
    res = _bt_insertonpg(rel, pbuf, stack->bts_parent,
				    keysz, scankey, btitem, nitem);
    pfree (btitem);
    		
    ItemPointerSet(&(res->pointerData), nbknum, P_HIKEY);
    
    return (res);
}
V
Vadim B. Mikheev 已提交
1585
#endif