Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id E9FB464030C for ; Tue, 24 Mar 2009 08:10:58 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 72193-02 for ; Tue, 24 Mar 2009 08:10:47 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from exprod7og114.obsmtp.com (exprod7og114.obsmtp.com [64.18.2.215]) by mail.postgresql.org (Postfix) with SMTP id A4D466330E4 for ; Tue, 24 Mar 2009 08:07:40 -0300 (ADT) Received: from source ([209.85.219.170]) by exprod7ob114.postini.com ([64.18.6.12]) with SMTP ID DSNKSci++p27ufledw94849DCGd3goc+JmDz@postini.com; Tue, 24 Mar 2009 04:07:42 PDT Received: by ewy18 with SMTP id 18so1799190ewy.3 for ; Tue, 24 Mar 2009 04:07:38 -0700 (PDT) Received: by 10.216.7.212 with SMTP id 62mr2685078wep.92.1237892857879; Tue, 24 Mar 2009 04:07:37 -0700 (PDT) Received: from ?80.223.212.7? (dsl-hkibrasgw2-fed4df00-7.dhcp.inet.fi [80.223.212.7]) by mx.google.com with ESMTPS id 10sm3007212eyd.53.2009.03.24.04.07.35 (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 24 Mar 2009 04:07:36 -0700 (PDT) Message-ID: <49C8BEF6.3080909@enterprisedb.com> Date: Tue, 24 Mar 2009 13:07:34 +0200 From: Heikki Linnakangas Organization: EnterpriseDB User-Agent: Mozilla-Thunderbird 2.0.0.19 (X11/20090103) MIME-Version: 1.0 To: Tom Lane CC: Sergey Burladyan , Oleg Bartunov , Teodor Sigaev , pgsql-performance@postgresql.org Subject: Re: Why creating GIN table index is so slow than inserting data into empty table with the same index? References: <874oxjrb0p.fsf@seb.progtech.ru> <18581.1237865712@sss.pgh.pa.us> In-Reply-To: <18581.1237865712@sss.pgh.pa.us> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200903/344 X-Sequence-Number: 33266 Tom Lane wrote: > Sergey Burladyan writes: >> show maintenance_work_mem ; >> maintenance_work_mem >> ---------------------- >> 128MB > >> create table a (i1 int, i2 int, i3 int, i4 int, i5 int, i6 int); >> insert into a select n, n, n, n, n, n from generate_series(1, 100000) as n; >> create index arr_gin on a using gin ( (array[i1, i2, i3, i4, i5, i6]) ); > > [ takes forever ] > > Seems the reason this is so awful is that the incoming data is exactly > presorted, meaning that the tree structure that ginInsertEntry() is > trying to build degenerates to a linear list (ie, every new element > becomes the right child of the prior one). So the processing becomes > O(N^2) up till you reach maintenance_work_mem and flush the tree. With > a large setting for maintenance_work_mem it gets spectacularly slow. Yes, this is probably the same issue I bumped into a while ago: http://archives.postgresql.org/message-id/49350A13.3020105@enterprisedb.com > I think a reasonable solution for this might be to keep an eye on > maxdepth and force a flush if that gets too large (more than a few > hundred, perhaps?). Something like this: > > /* If we've maxed out our available memory, dump everything to the index */ > + /* Also dump if the tree seems to be getting too unbalanced */ > - if (buildstate->accum.allocatedMemory >= maintenance_work_mem * 1024L) > + if (buildstate->accum.allocatedMemory >= maintenance_work_mem * 1024L || > + buildstate->accum.maxdepth > DEPTH_LIMIT) > { > > The new fast-insert code likely will need a similar defense. I fooled around with a balanced tree, which solved the problem but unfortunately made the unsorted case slower. Limiting the depth like that should avoid that so it's worth trying. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com