|
7 | 7 | * |
8 | 8 | * |
9 | 9 | * IDENTIFICATION |
10 | | - * $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtinsert.c,v 1.42.2.2 1999/09/01 17:54:00 scrappy Exp $ |
| 10 | + * $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtinsert.c,v 1.42.2.3 1999/12/26 20:44:15 tgl Exp $ |
11 | 11 | * |
12 | 12 | *------------------------------------------------------------------------- |
13 | 13 | */ |
@@ -267,6 +267,18 @@ _bt_insertonpg(Relation rel, |
267 | 267 | * this but we need to be |
268 | 268 | * consistent */ |
269 | 269 |
|
| 270 | +/* |
| 271 | + * Check whether the item can fit on a btree page at all. |
| 272 | + * (Eventually, we ought to try to apply TOAST methods if not.) |
| 273 | + * We actually need to be able to fit three items on every page, |
| 274 | + * so restrict any one item to 1/3 the per-page available space. |
| 275 | + * Note that at this point, itemsz doesn't include the ItemId. |
| 276 | + */ |
| 277 | +if (itemsz> (PageGetPageSize(page)-sizeof(PageHeaderData)-MAXALIGN(sizeof(BTPageOpaqueData)))/3-sizeof(ItemIdData)) |
| 278 | +elog(ERROR,"btree: index item size %d exceeds maximum %d", |
| 279 | +itemsz, |
| 280 | + (PageGetPageSize(page)-sizeof(PageHeaderData)-MAXALIGN(sizeof(BTPageOpaqueData)))/3-sizeof(ItemIdData)); |
| 281 | + |
270 | 282 | /* |
271 | 283 | * If we have to insert item on the leftmost page which is the first |
272 | 284 | * page in the chain of duplicates then: 1. if scankey == hikey (i.e. |
@@ -342,36 +354,42 @@ _bt_insertonpg(Relation rel, |
342 | 354 | { |
343 | 355 | OffsetNumberoffnum= (P_RIGHTMOST(lpageop)) ?P_HIKEY :P_FIRSTKEY; |
344 | 356 | OffsetNumbermaxoff=PageGetMaxOffsetNumber(page); |
345 | | -ItemIditid; |
346 | | -BTItemprevitem, |
347 | | -chkitem; |
348 | | -Sizemaxsize; |
349 | | -Sizecurrsize; |
350 | | - |
351 | | -itid=PageGetItemId(page,offnum); |
352 | | -previtem= (BTItem)PageGetItem(page,itid); |
353 | | -maxsize=currsize= (ItemIdGetLength(itid)+sizeof(ItemIdData)); |
354 | | -for (offnum=OffsetNumberNext(offnum); |
355 | | -offnum <=maxoff;offnum=OffsetNumberNext(offnum)) |
| 357 | + |
| 358 | +if (offnum<maxoff)/* can't split unless at least 2 items... */ |
356 | 359 | { |
| 360 | +ItemIditid; |
| 361 | +BTItemprevitem, |
| 362 | +chkitem; |
| 363 | +Sizemaxsize; |
| 364 | +Sizecurrsize; |
| 365 | + |
| 366 | +/* find largest group of identically-keyed items on page */ |
357 | 367 | itid=PageGetItemId(page,offnum); |
358 | | -chkitem= (BTItem)PageGetItem(page,itid); |
359 | | -if (!_bt_itemcmp(rel,keysz,previtem,chkitem, |
360 | | -BTEqualStrategyNumber)) |
| 368 | +previtem= (BTItem)PageGetItem(page,itid); |
| 369 | +maxsize=currsize= (ItemIdGetLength(itid)+sizeof(ItemIdData)); |
| 370 | +for (offnum=OffsetNumberNext(offnum); |
| 371 | +offnum <=maxoff;offnum=OffsetNumberNext(offnum)) |
361 | 372 | { |
362 | | -if (currsize>maxsize) |
363 | | -maxsize=currsize; |
364 | | -currsize=0; |
365 | | -previtem=chkitem; |
| 373 | +itid=PageGetItemId(page,offnum); |
| 374 | +chkitem= (BTItem)PageGetItem(page,itid); |
| 375 | +if (!_bt_itemcmp(rel,keysz,previtem,chkitem, |
| 376 | +BTEqualStrategyNumber)) |
| 377 | +{ |
| 378 | +if (currsize>maxsize) |
| 379 | +maxsize=currsize; |
| 380 | +currsize=0; |
| 381 | +previtem=chkitem; |
| 382 | +} |
| 383 | +currsize+= (ItemIdGetLength(itid)+sizeof(ItemIdData)); |
366 | 384 | } |
367 | | -currsize+= (ItemIdGetLength(itid)+sizeof(ItemIdData)); |
| 385 | +if (currsize>maxsize) |
| 386 | +maxsize=currsize; |
| 387 | +/* Decide to split if largest group is > 1/2 page size */ |
| 388 | +maxsize+=sizeof(PageHeaderData)+ |
| 389 | +MAXALIGN(sizeof(BTPageOpaqueData)); |
| 390 | +if (maxsize >=PageGetPageSize(page) /2) |
| 391 | +do_split= true; |
368 | 392 | } |
369 | | -if (currsize>maxsize) |
370 | | -maxsize=currsize; |
371 | | -maxsize+=sizeof(PageHeaderData)+ |
372 | | -MAXALIGN(sizeof(BTPageOpaqueData)); |
373 | | -if (maxsize >=PageGetPageSize(page) /2) |
374 | | -do_split= true; |
375 | 393 | } |
376 | 394 |
|
377 | 395 | if (do_split) |
|