Need defense against oversize index entries in btree CREATE INDEX,
authorTom Lane
Sat, 8 Jan 2000 21:24:49 +0000 (21:24 +0000)
committerTom Lane
Sat, 8 Jan 2000 21:24:49 +0000 (21:24 +0000)
as well as when inserting entries into an existing index.

src/backend/access/nbtree/nbtsort.c

index 48386c113f05113e07ac90edec3d760f25478d43..fff65b7d79d514dcb379d48ae9ad0b11c946bdf7 100644 (file)
@@ -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;