From: Tom Lane Date: Sat, 8 Jan 2000 21:24:49 +0000 (+0000) Subject: Need defense against oversize index entries in btree CREATE INDEX, X-Git-Tag: REL7_0~928 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b79e75d66f1b2169ad9552d430f699a4b5c95862;p=postgresql Need defense against oversize index entries in btree CREATE INDEX, as well as when inserting entries into an existing index. --- diff --git a/src/backend/access/nbtree/nbtsort.c b/src/backend/access/nbtree/nbtsort.c index 48386c113f..fff65b7d79 100644 --- a/src/backend/access/nbtree/nbtsort.c +++ b/src/backend/access/nbtree/nbtsort.c @@ -27,7 +27,7 @@ * Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtsort.c,v 1.47 1999/10/17 22:15:04 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtsort.c,v 1.48 2000/01/08 21:24:49 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -301,6 +301,23 @@ _bt_buildadd(Relation index, BTPageState *state, BTItem bti, int flags) pgspc = PageGetFreeSpace(npage); btisz = BTITEMSZ(bti); btisz = MAXALIGN(btisz); + + /* + * Check whether the item can fit on a btree page at all. + * (Eventually, we ought to try to apply TOAST methods if not.) + * We actually need to be able to fit three items on every page, + * so restrict any one item to 1/3 the per-page available space. + * Note that at this point, btisz doesn't include the ItemId. + * + * NOTE: similar code appears in _bt_insertonpg() to defend against + * oversize items being inserted into an already-existing index. + * But during creation of an index, we don't go through there. + */ + if (btisz > (PageGetPageSize(npage)-sizeof(PageHeaderData)-MAXALIGN(sizeof(BTPageOpaqueData)))/3 - sizeof(ItemIdData)) + elog(ERROR, "btree: index item size %d exceeds maximum %d", + btisz, + (PageGetPageSize(npage)-sizeof(PageHeaderData)-MAXALIGN(sizeof(BTPageOpaqueData)))/3 - sizeof(ItemIdData)); + if (pgspc < btisz) { Buffer obuf = nbuf;