From e5efda442cfcfa20a0cf6166952c671a0f39689b Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 24 Mar 2009 22:06:03 +0000 Subject: 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. --- src/backend/access/gin/ginfast.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/backend/access/gin/ginfast.c') diff --git a/src/backend/access/gin/ginfast.c b/src/backend/access/gin/ginfast.c index d8624237ec1..f474ad6598e 100644 --- a/src/backend/access/gin/ginfast.c +++ b/src/backend/access/gin/ginfast.c @@ -11,7 +11,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/access/gin/ginfast.c,v 1.1 2009/03/24 20:17:10 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/access/gin/ginfast.c,v 1.2 2009/03/24 22:06:03 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -749,9 +749,10 @@ ginInsertCleanup(Relation index, GinState *ginstate, * XXX using up maintenance_work_mem here is probably unreasonably * much, since vacuum might already be using that much. */ - if ( GinPageGetOpaque(page)->rightlink == InvalidBlockNumber || - ( GinPageHasFullRow(page) && - accum.allocatedMemory > maintenance_work_mem * 1024L ) ) + if (GinPageGetOpaque(page)->rightlink == InvalidBlockNumber || + (GinPageHasFullRow(page) && + (accum.allocatedMemory >= maintenance_work_mem * 1024L || + accum.maxdepth > GIN_MAX_TREE_DEPTH))) { ItemPointerData *list; uint32 nlist; -- cgit v1.2.3