Install a search tree depth limit in GIN bulk-insert operations, to prevent

them from degrading badly when the input is sorted or nearly so.  In this
scenario the tree is unbalanced to the point of becoming a mere linked list,
so insertions become O(N^2).  The easiest and most safely back-patchable
solution is to stop growing the tree sooner, ie limit the growth of N.  We
might later consider a rebalancing tree algorithm, but it's not clear that
the benefit would be worth the cost and complexity.  Per report from Sergey
Burladyan and an earlier complaint from Heikki.

Back-patch to 8.2; older versions didn't have GIN indexes.
This commit is contained in:
Tom Lane 2009-03-24 22:06:24 +00:00
parent e1cf329061
commit e760463c77
2 changed files with 15 additions and 8 deletions

View File

@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/gin/gininsert.c,v 1.11.2.1 2008/11/13 17:42:18 tgl Exp $
* $PostgreSQL: pgsql/src/backend/access/gin/gininsert.c,v 1.11.2.2 2009/03/24 22:06:24 tgl Exp $
*-------------------------------------------------------------------------
*/
@ -238,7 +238,9 @@ ginBuildCallback(Relation index, HeapTuple htup, Datum *values,
buildstate->indtuples += ginHeapTupleBulkInsert(buildstate, *values, &htup->t_self);
/* If we've maxed out our available memory, dump everything to the index */
if (buildstate->accum.allocatedMemory >= maintenance_work_mem * 1024L)
/* Also dump if the tree seems to be getting too unbalanced */
if (buildstate->accum.allocatedMemory >= maintenance_work_mem * 1024L ||
buildstate->accum.maxdepth > GIN_MAX_TREE_DEPTH)
{
ItemPointerData *list;
Datum entry;

View File

@ -4,7 +4,7 @@
*
* Copyright (c) 2006-2008, PostgreSQL Global Development Group
*
* $PostgreSQL: pgsql/src/include/access/gin.h,v 1.16.2.1 2008/04/22 17:54:19 teodor Exp $
* $PostgreSQL: pgsql/src/include/access/gin.h,v 1.16.2.2 2009/03/24 22:06:24 tgl Exp $
*--------------------------------------------------------------------------
*/
@ -32,6 +32,14 @@
#define GIN_CONSISTENT_PROC 4
#define GINNProcs 4
/*
* Max depth allowed in search tree during bulk inserts. This is to keep from
* degenerating to O(N^2) behavior when the tree is unbalanced due to sorted
* or nearly-sorted input. (Perhaps it would be better to use a balanced-tree
* algorithm, but in common cases that would only add useless overhead.)
*/
#define GIN_MAX_TREE_DEPTH 100
/*
* Page opaque data in a inverted index page.
*
@ -314,12 +322,9 @@ extern IndexTuple ginPageGetLinkItup(Buffer buf);
/* gindatapage.c */
extern int compareItemPointers(ItemPointer a, ItemPointer b);
extern void
MergeItemPointers(
ItemPointerData *dst,
extern void MergeItemPointers(ItemPointerData *dst,
ItemPointerData *a, uint32 na,
ItemPointerData *b, uint32 nb
);
ItemPointerData *b, uint32 nb);
extern void GinDataPageAddItem(Page page, void *data, OffsetNumber offset);
extern void PageDeletePostingItem(Page page, OffsetNumber offset);