Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YGplr-0007at-GI for pgsql-bugs@arkaria.postgresql.org; Thu, 29 Jan 2015 14:08:35 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YGplq-00064y-VE for pgsql-bugs@arkaria.postgresql.org; Thu, 29 Jan 2015 14:08:35 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YGplo-00064n-Ph for pgsql-bugs@postgresql.org; Thu, 29 Jan 2015 14:08:32 +0000 Received: from smtp-outbound-1.vmware.com ([208.91.2.12]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YGplj-0004Jz-N5 for pgsql-bugs@postgresql.org; Thu, 29 Jan 2015 14:08:29 +0000 Received: from sc9-mailhost1.vmware.com (sc9-mailhost1.vmware.com [10.113.161.71]) by smtp-outbound-1.vmware.com (Postfix) with ESMTP id 6D34829D04; Thu, 29 Jan 2015 06:08:25 -0800 (PST) Received: from EX13-CAS-009.vmware.com (EX13-CAS-009.vmware.com [10.113.191.61]) by sc9-mailhost1.vmware.com (Postfix) with ESMTP id 6A4C618B40; Thu, 29 Jan 2015 06:08:25 -0800 (PST) Received: from [192.168.42.220] (10.113.160.246) by EX13-MBX-024.vmware.com (10.113.191.44) with Microsoft SMTP Server (TLS) id 15.0.913.22; Thu, 29 Jan 2015 06:08:09 -0800 Message-ID: <54CA3EB5.3030804@vmware.com> Date: Thu, 29 Jan 2015 16:07:49 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.3.0 MIME-Version: 1.0 To: Michael Paquier , CC: Subject: Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit References: <20150128181243.2579.80775@wrigleys.postgresql.org> In-Reply-To: Content-Type: multipart/mixed; boundary="------------070303030103030001030104" X-Originating-IP: [10.113.160.246] X-ClientProxiedBy: EX13-CAS-013.vmware.com (10.113.191.65) To EX13-MBX-024.vmware.com (10.113.191.44) X-Pg-Spam-Score: -6.9 (------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --------------070303030103030001030104 Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit On 01/29/2015 03:09 PM, Michael Paquier wrote: > On Thu, Jan 29, 2015 at 3:12 AM, wrote: >> Bug reference: 12694 >> Logged by: Olaf Gawenda >> Email address: olaf.gw@googlemail.com >> PostgreSQL version: 9.4.0 >> Operating system: Linux 3.2.0-4-amd64 #1 SMP Debian 3.2.65-1+deb7 >> Description: >> >> the following sequence of commands get a crash if the numer of result rows >> is lower than gin_fuzzy_search_limit: >> >> create table test (t text, ts_vec tsvector); >> >> insert into test (t) values (),(),(), ...; -- test data not posted >> >> update test set ts_vec = to_tsvector('english', t); >> >> create index on test using gin(ts_vec); >> analyze test; >> set enable_seqscan = off; >> set gin_fuzzy_search_limit = 1000; >> >> select t from test where ts_vec @@ to_tsquery('english', '...'); > > This can be reproduced easily with a test case like that: > create table aa as > select array[(random() * 1000000)::int, > (random() * 1000000)::int, > (random() * 1000000)::int] as a > from generate_series(1,10); > create index aai on aa using gin(a); > set gin_fuzzy_search_limit = 1; > set enable_seqscan = off; > select * from aa where a <@ array[1,2]; The problem is in startScan() function: > if (GinFuzzySearchLimit > 0) > { > /* > * If all of keys more than threshold we will try to reduce result, we > * hope (and only hope, for intersection operation of array our > * supposition isn't true), that total result will not more than > * minimal predictNumberResult. > */ > > for (i = 0; i < so->totalentries; i++) > if (so->entries[i]->predictNumberResult <= so->totalentries * GinFuzzySearchLimit) > return; > > for (i = 0; i < so->totalentries; i++) > if (so->entries[i]->predictNumberResult > so->totalentries * GinFuzzySearchLimit) > { > so->entries[i]->predictNumberResult /= so->totalentries; > so->entries[i]->reduceResult = TRUE; > } > } > > for (i = 0; i < so->nkeys; i++) > startScanKey(ginstate, so, so->keys + i); > } If the early return is taken, startScanKey() is not called, and many fields in the GinScanKey struct are left uninitialized. That causes the segfault later. This was not as big a problem before 9.4, because startScanKey() didn't do very much. It just reset a few fields, which in a new scan were reset already by ginNewScanKey(). But it is in fact possible to get an assertion failure on 9.3 too, if the plan contains a re-scan of GIN scan, and gin_fuzzy_search_limit is set. Attached is a script that does it. Not sure why, but I'm not seeing a segfault or assert failure on earlier branches. The plan of the segfaulting query looks identical between 9.2 and 9.3, so perhaps there have been some changes to the executor on how and when it calls rescan. Nevertheless, the code looks just as wrong on earlier branches, so I think it should be fixed all the way to 9.1 where that early return in startScan() was introduced. The fix is simple: make sure that startScanKey() is always called, by getting rid of the early return above. Attached. I'll apply this later today or tomorrow unless someone sees a problem with this. - Heikki --------------070303030103030001030104 Content-Type: text/plain; charset="UTF-8"; name="gin-rescan-assert-9.3.txt" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="gin-rescan-assert-9.3.txt" create table test2 (t text); insert into test2 values ('foo'); insert into test2 values ('bar'); create table test (t text, ts_vec tsvector); insert into test (t, ts_vec) values ('foo', to_tsvector('foo')); insert into test (t, ts_vec) values ('bar', to_tsvector('bar')); insert into test (t, ts_vec) values ('foo bar', to_tsvector('foo bar')); create index on test using gin(ts_vec); vacuum test; set enable_seqscan = off; set enable_hashjoin = off; set enable_mergejoin = off; set gin_fuzzy_search_limit = 1000; select test.t from test2, test where ts_vec @@ to_tsquery('english', 'foo') and test.t = test2.t; --------------070303030103030001030104 Content-Type: text/x-diff; name="0001-Fix-bug-where-GIN-scan-keys-were-not-initialized-wit.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-Fix-bug-where-GIN-scan-keys-were-not-initialized-wit.pa"; filename*1="tch" From b5b337ed772999e03d6a4d4391f4631b6af19d59 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Thu, 29 Jan 2015 15:48:02 +0200 Subject: [PATCH 1/1] Fix bug where GIN scan keys were not initialized with gin_fuzzy_search_limit. In 9.4, startScanKey() does a lot of stuff, setting up the requiredEntries array, and you readily get a segfault if it's not initialized. In earlier versions it was harder to trigger incorrect behaviour, because it just set a few flags which were initialized in ginNewScanKey() already. But even on on earlier versions, you would get an assertion failure or missing result rows if the query involved a re-scan of the GIN scan. Reported by Olaf Gawenda, bug #12694, Backpatch to 9.1 and above, 9.0 didn't have the early return in startScan(). --- src/backend/access/gin/ginget.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/backend/access/gin/ginget.c b/src/backend/access/gin/ginget.c index 3494d6b..306a3ce 100644 --- a/src/backend/access/gin/ginget.c +++ b/src/backend/access/gin/ginget.c @@ -540,17 +540,28 @@ startScan(IndexScanDesc scan) * supposition isn't true), that total result will not more than * minimal predictNumberResult. */ + bool reduce = true; for (i = 0; i < so->totalentries; i++) + { if (so->entries[i]->predictNumberResult <= so->totalentries * GinFuzzySearchLimit) - return; + { + reduce = false; + break; + } + } - for (i = 0; i < so->totalentries; i++) - if (so->entries[i]->predictNumberResult > so->totalentries * GinFuzzySearchLimit) + if (reduce) + { + for (i = 0; i < so->totalentries; i++) { - so->entries[i]->predictNumberResult /= so->totalentries; - so->entries[i]->reduceResult = TRUE; + if (so->entries[i]->predictNumberResult > so->totalentries * GinFuzzySearchLimit) + { + so->entries[i]->predictNumberResult /= so->totalentries; + so->entries[i]->reduceResult = TRUE; + } } + } } for (i = 0; i < so->nkeys; i++) -- 2.1.4 --------------070303030103030001030104 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------070303030103030001030104--