Skip to content

Commit 9505391

Browse files
committed
Back-patch defense against oversize index tuples and fix for trying to
split a page that only has one entry.
1 parent 2fbaa45 commit 9505391

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.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 $
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
OffsetNumber offnum = (P_RIGHTMOST(lpageop)) ? P_HIKEY : P_FIRSTKEY;
344356
OffsetNumber maxoff = PageGetMaxOffsetNumber(page);
345-
ItemId itid;
346-
BTItem previtem,
347-
chkitem;
348-
Size maxsize;
349-
Size currsize;
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+
ItemId itid;
361+
BTItem previtem,
362+
chkitem;
363+
Size maxsize;
364+
Size currsize;
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy