Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita6a7031

Browse files
committed
It turns out that the item size limit for btree indexes is about BLCKSZ/3,
not BLCKSZ/2 as some of us thought. Add check for oversize item so thatfailure is detected before corrupting the index, not after.
1 parentad322de commita6a7031

File tree

1 file changed

+44
-26
lines changed

1 file changed

+44
-26
lines changed

‎src/backend/access/nbtree/nbtinsert.c

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtinsert.c,v 1.51 1999/11/22 17:55:54 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtinsert.c,v 1.52 1999/12/26 03:48:22 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -267,6 +267,18 @@ _bt_insertonpg(Relation rel,
267267
* this but we need to be
268268
* consistent */
269269

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+
270282
/*
271283
* If we have to insert item on the leftmost page which is the first
272284
* page in the chain of duplicates then: 1. if scankey == hikey (i.e.
@@ -342,36 +354,42 @@ _bt_insertonpg(Relation rel,
342354
{
343355
OffsetNumberoffnum= (P_RIGHTMOST(lpageop)) ?P_HIKEY :P_FIRSTKEY;
344356
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... */
356359
{
360+
ItemIditid;
361+
BTItemprevitem,
362+
chkitem;
363+
Sizemaxsize;
364+
Sizecurrsize;
365+
366+
/* find largest group of identically-keyed items on page */
357367
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))
361372
{
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));
366384
}
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;
368392
}
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;
375393
}
376394

377395
if (do_split)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp