pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit
10+ messages / 4 participants
[nested] [flat]

* BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit
@ 2015-01-28 18:12 olaf.gw@googlemail.com
  2015-01-29 13:09 ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 10+ messages in thread

From: olaf.gw@googlemail.com @ 2015-01-28 18:12 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

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', '...');


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit
  2015-01-28 18:12 BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit olaf.gw@googlemail.com
@ 2015-01-29 13:09 ` Michael Paquier <michael.paquier@gmail.com>
  2015-01-29 14:07   ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Heikki Linnakangas <hlinnakangas@vmware.com>
  0 siblings, 1 reply; 10+ messages in thread

From: Michael Paquier @ 2015-01-29 13:09 UTC (permalink / raw)
  To: olaf.gw@googlemail.com; +Cc: pgsql-bugs

On Thu, Jan 29, 2015 at 3:12 AM,  <olaf.gw@googlemail.com> 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];
-- 
Michael


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit
  2015-01-28 18:12 BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit olaf.gw@googlemail.com
  2015-01-29 13:09 ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Michael Paquier <michael.paquier@gmail.com>
@ 2015-01-29 14:07   ` Heikki Linnakangas <hlinnakangas@vmware.com>
  2015-01-29 14:44     ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Heikki Linnakangas <hlinnakangas@vmware.com>
  2015-01-29 15:26     ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 10+ messages in thread

From: Heikki Linnakangas @ 2015-01-29 14:07 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; olaf.gw@googlemail.com; +Cc: pgsql-bugs

On 01/29/2015 03:09 PM, Michael Paquier wrote:
> On Thu, Jan 29, 2015 at 3:12 AM,  <olaf.gw@googlemail.com> 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

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

gin-rescan-assert-9.3.txt (text/plain, 615B) download 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;
0001-Fix-bug-where-GIN-scan-keys-were-not-initialized-wit.patch (text/x-diff, 2.0K) download From b5b337ed772999e03d6a4d4391f4631b6af19d59 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas <heikki.linnakangas@iki.fi> 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
^ permalink raw reply [nested|flat] 10+ messages in thread

* Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit
  2015-01-28 18:12 BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit olaf.gw@googlemail.com
  2015-01-29 13:09 ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Michael Paquier <michael.paquier@gmail.com>
  2015-01-29 14:07   ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Heikki Linnakangas <hlinnakangas@vmware.com>
@ 2015-01-29 14:44     ` Heikki Linnakangas <hlinnakangas@vmware.com>
  1 sibling, 0 replies; 10+ messages in thread

From: Heikki Linnakangas @ 2015-01-29 14:44 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; olaf.gw@googlemail.com; +Cc: pgsql-bugs

On 01/29/2015 04:07 PM, Heikki Linnakangas wrote:
> 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.

Never mind, I had some debugging code in place in my 9.3 branch that 
caused the assertion failure. Still, it looks broken on 9.3 and below, 
too. Either that, or GinNewKey() check in gingetbitmap is always true, 
and the startScanKey() calls were unnecessary.

- Heikki



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit
  2015-01-28 18:12 BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit olaf.gw@googlemail.com
  2015-01-29 13:09 ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Michael Paquier <michael.paquier@gmail.com>
  2015-01-29 14:07   ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Heikki Linnakangas <hlinnakangas@vmware.com>
@ 2015-01-29 15:26     ` Tom Lane <tgl@sss.pgh.pa.us>
  2015-01-29 15:54       ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Heikki Linnakangas <hlinnakangas@vmware.com>
  1 sibling, 1 reply; 10+ messages in thread

From: Tom Lane @ 2015-01-29 15:26 UTC (permalink / raw)
  To: Heikki Linnakangas <hlinnakangas@vmware.com>; +Cc: Michael Paquier <michael.paquier@gmail.com>; olaf.gw@googlemail.com; pgsql-bugs

Heikki Linnakangas <hlinnakangas@vmware.com> writes:
> 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.

Another, even simpler fix would be to just move the startScanKey()
call loop to before the "if (GinFuzzySearchLimit > 0)" block.
Is there a particular reason why it's a good idea to do things in
the current order?  It almost looks like a patch application error
as it stands.

With either fix, I concur that we should back-patch it.  It's not at all
clear how come older branches don't fail because of this.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit
  2015-01-28 18:12 BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit olaf.gw@googlemail.com
  2015-01-29 13:09 ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Michael Paquier <michael.paquier@gmail.com>
  2015-01-29 14:07   ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Heikki Linnakangas <hlinnakangas@vmware.com>
  2015-01-29 15:26     ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Tom Lane <tgl@sss.pgh.pa.us>
@ 2015-01-29 15:54       ` Heikki Linnakangas <hlinnakangas@vmware.com>
  2015-01-29 16:04         ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 10+ messages in thread

From: Heikki Linnakangas @ 2015-01-29 15:54 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Michael Paquier <michael.paquier@gmail.com>; olaf.gw@googlemail.com; pgsql-bugs

On 01/29/2015 05:26 PM, Tom Lane wrote:
> Heikki Linnakangas <hlinnakangas@vmware.com> writes:
>> 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.
>
> Another, even simpler fix would be to just move the startScanKey()
> call loop to before the "if (GinFuzzySearchLimit > 0)" block.
> Is there a particular reason why it's a good idea to do things in
> the current order?  It almost looks like a patch application error
> as it stands.

The code in startScanKey() uses the predictNumberResult estimates, which 
the loop modifies, so it would change how that behaves. I'm not sure if 
it would actually be better that way though; it's not clear to me how 
the fuzzy search limit should interact with the fast scan code. It won't 
affect correctness either way.

The for()-loop sets the so->entries[i]-reduceResult fields, so it makes 
sense to do it right after the startScanEntry() calls. Otherwise the 
logic would be "1. do some initialization on entries, 2. initialize 
keys, 3. do more initialization on entries", which would be a bit strange.

<looks at the loop a bit more>

Hmm, isn't the if-check in the second loop unnecessary? The first loop 
checks that so->entries[i]->predictNumberResult > so->totalentries * 
GinFuzzySearchLimit for all entries. If there are any where that doesn't 
hold, it returns (which is a bad idea). There's no need to check for the 
same condition again in the second loop.

- Heikki



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit
  2015-01-28 18:12 BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit olaf.gw@googlemail.com
  2015-01-29 13:09 ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Michael Paquier <michael.paquier@gmail.com>
  2015-01-29 14:07   ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Heikki Linnakangas <hlinnakangas@vmware.com>
  2015-01-29 15:26     ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Tom Lane <tgl@sss.pgh.pa.us>
  2015-01-29 15:54       ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Heikki Linnakangas <hlinnakangas@vmware.com>
@ 2015-01-29 16:04         ` Tom Lane <tgl@sss.pgh.pa.us>
  2015-01-29 17:58           ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Heikki Linnakangas <hlinnakangas@vmware.com>
  0 siblings, 1 reply; 10+ messages in thread

From: Tom Lane @ 2015-01-29 16:04 UTC (permalink / raw)
  To: Heikki Linnakangas <hlinnakangas@vmware.com>; +Cc: Michael Paquier <michael.paquier@gmail.com>; olaf.gw@googlemail.com; pgsql-bugs

Heikki Linnakangas <hlinnakangas@vmware.com> writes:
> On 01/29/2015 05:26 PM, Tom Lane wrote:
>> Another, even simpler fix would be to just move the startScanKey()
>> call loop to before the "if (GinFuzzySearchLimit > 0)" block.
>> Is there a particular reason why it's a good idea to do things in
>> the current order?  It almost looks like a patch application error
>> as it stands.

> The code in startScanKey() uses the predictNumberResult estimates, which 
> the loop modifies, so it would change how that behaves.

Oh!  Sorry, I'd missed looking at what the qsort comparator did.

> I'm not sure if 
> it would actually be better that way though; it's not clear to me how 
> the fuzzy search limit should interact with the fast scan code.

Yeah, it might be better but it's not very clear what the implications
are.  Probably shouldn't touch that as part of an emergency bug fix.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit
  2015-01-28 18:12 BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit olaf.gw@googlemail.com
  2015-01-29 13:09 ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Michael Paquier <michael.paquier@gmail.com>
  2015-01-29 14:07   ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Heikki Linnakangas <hlinnakangas@vmware.com>
  2015-01-29 15:26     ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Tom Lane <tgl@sss.pgh.pa.us>
  2015-01-29 15:54       ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Heikki Linnakangas <hlinnakangas@vmware.com>
  2015-01-29 16:04         ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Tom Lane <tgl@sss.pgh.pa.us>
@ 2015-01-29 17:58           ` Heikki Linnakangas <hlinnakangas@vmware.com>
  2015-01-29 19:59             ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 10+ messages in thread

From: Heikki Linnakangas @ 2015-01-29 17:58 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Michael Paquier <michael.paquier@gmail.com>; olaf.gw@googlemail.com; pgsql-bugs

On 01/29/2015 06:04 PM, Tom Lane wrote:
> Heikki Linnakangas <hlinnakangas@vmware.com> writes:
>> On 01/29/2015 05:26 PM, Tom Lane wrote:
>> I'm not sure if
>> it would actually be better that way though; it's not clear to me how
>> the fuzzy search limit should interact with the fast scan code.
>
> Yeah, it might be better but it's not very clear what the implications
> are.  Probably shouldn't touch that as part of an emergency bug fix.

Committed and backpatched a minimal fix.

For master and 9.4, I'm thinking of applying the attached. It makes it 
clear that startScan() is not used to re-start a scan with existing scan 
keys, but is always called on a newly initialized scan keys.

It also plugs the obvious leaking of the arrays. I'll look at the other 
memory leaks separately, but this seems appropriate for 9.4.

- Heikki

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

0001-Fix-query-duration-memory-leak-with-GIN-rescans.patch (text/x-diff, 4.2K) download From bdfee2394844bbbbbff3e7f7884a3ca83f2f0939 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas <heikki.linnakangas@iki.fi> Date: Thu, 29 Jan 2015 15:48:02 +0200 Subject: [PATCH] Fix query-duration memory leak with GIN rescans. The requiredEntries / additionalEntries arrays were not freed in freeScanKeys() like other per-key stuff. It's not obvious, but startScanKey() was only ever called after the keys have been initialized with ginNewScanKey(). That's why it doesn't need to worry about freeing existing arrays. The ginIsNewKey() test in gingetbitmap was never true, because ginrescan free's the existing keys, and it's not OK to call gingetbitmap twice in a row without calling ginrescan in between. To make that clear, remove the unnecessary ginIsNewKey(). And just to be extra sure that nothing funny happens if there is an existing key after all, call freeScanKeys() to free it if it exists. This makes the code more straightforward. (I'm seeing other similar leaks in testing a query that rescans an GIN index scan, but that's a different issue. This just fixes the obvious leak with those two arrays.) diff --git a/src/backend/access/gin/ginget.c b/src/backend/access/gin/ginget.c index 57d378a..d85191f 100644 --- a/src/backend/access/gin/ginget.c +++ b/src/backend/access/gin/ginget.c @@ -560,6 +560,10 @@ startScan(IndexScanDesc scan) } } + /* + * Now that we have the estimates for the entry frequencies, finish + * initializing the scan keys. + */ for (i = 0; i < so->nkeys; i++) startScanKey(ginstate, so, so->keys + i); } @@ -1763,13 +1767,13 @@ scanPendingInsert(IndexScanDesc scan, TIDBitmap *tbm, int64 *ntids) } -#define GinIsNewKey(s) ( ((GinScanOpaque) scan->opaque)->keys == NULL ) #define GinIsVoidRes(s) ( ((GinScanOpaque) scan->opaque)->isVoidRes ) Datum gingetbitmap(PG_FUNCTION_ARGS) { IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0); + GinScanOpaque so = (GinScanOpaque) scan->opaque; TIDBitmap *tbm = (TIDBitmap *) PG_GETARG_POINTER(1); int64 ntids; ItemPointerData iptr; @@ -1778,8 +1782,8 @@ gingetbitmap(PG_FUNCTION_ARGS) /* * Set up the scan keys, and check for unsatisfiable query. */ - if (GinIsNewKey(scan)) - ginNewScanKey(scan); + ginFreeScanKeys(so); /* there should be no keys yet, but just to be sure */ + ginNewScanKey(scan); if (GinIsVoidRes(scan)) PG_RETURN_INT64(0); diff --git a/src/backend/access/gin/ginscan.c b/src/backend/access/gin/ginscan.c index 7ab795c..b3a2de1 100644 --- a/src/backend/access/gin/ginscan.c +++ b/src/backend/access/gin/ginscan.c @@ -163,6 +163,10 @@ ginFillScanKey(GinScanOpaque so, OffsetNumber attnum, key->curItemMatches = false; key->recheckCurItem = false; key->isFinished = false; + key->nrequired = 0; + key->nadditional = 0; + key->requiredEntries = NULL; + key->additionalEntries = NULL; ginInitConsistentFunction(ginstate, key); @@ -223,8 +227,8 @@ ginFillScanKey(GinScanOpaque so, OffsetNumber attnum, } } -static void -freeScanKeys(GinScanOpaque so) +void +ginFreeScanKeys(GinScanOpaque so) { uint32 i; @@ -237,6 +241,10 @@ freeScanKeys(GinScanOpaque so) pfree(key->scanEntry); pfree(key->entryRes); + if (key->requiredEntries) + pfree(key->requiredEntries); + if (key->additionalEntries) + pfree(key->additionalEntries); } pfree(so->keys); @@ -416,7 +424,7 @@ ginrescan(PG_FUNCTION_ARGS) /* remaining arguments are ignored */ GinScanOpaque so = (GinScanOpaque) scan->opaque; - freeScanKeys(so); + ginFreeScanKeys(so); if (scankey && scan->numberOfKeys > 0) { @@ -434,7 +442,7 @@ ginendscan(PG_FUNCTION_ARGS) IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0); GinScanOpaque so = (GinScanOpaque) scan->opaque; - freeScanKeys(so); + ginFreeScanKeys(so); MemoryContextDelete(so->tempCtx); diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h index cf2ef80..c949c97 100644 --- a/src/include/access/gin_private.h +++ b/src/include/access/gin_private.h @@ -899,6 +899,7 @@ extern Datum ginrescan(PG_FUNCTION_ARGS); extern Datum ginmarkpos(PG_FUNCTION_ARGS); extern Datum ginrestrpos(PG_FUNCTION_ARGS); extern void ginNewScanKey(IndexScanDesc scan); +extern void ginFreeScanKeys(GinScanOpaque so); /* ginget.c */ extern Datum gingetbitmap(PG_FUNCTION_ARGS); -- 2.1.4
^ permalink raw reply [nested|flat] 10+ messages in thread

* Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit
  2015-01-28 18:12 BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit olaf.gw@googlemail.com
  2015-01-29 13:09 ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Michael Paquier <michael.paquier@gmail.com>
  2015-01-29 14:07   ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Heikki Linnakangas <hlinnakangas@vmware.com>
  2015-01-29 15:26     ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Tom Lane <tgl@sss.pgh.pa.us>
  2015-01-29 15:54       ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Heikki Linnakangas <hlinnakangas@vmware.com>
  2015-01-29 16:04         ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Tom Lane <tgl@sss.pgh.pa.us>
  2015-01-29 17:58           ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Heikki Linnakangas <hlinnakangas@vmware.com>
@ 2015-01-29 19:59             ` Tom Lane <tgl@sss.pgh.pa.us>
  2015-01-30 17:18               ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Heikki Linnakangas <hlinnakangas@vmware.com>
  0 siblings, 1 reply; 10+ messages in thread

From: Tom Lane @ 2015-01-29 19:59 UTC (permalink / raw)
  To: Heikki Linnakangas <hlinnakangas@vmware.com>; +Cc: Michael Paquier <michael.paquier@gmail.com>; olaf.gw@googlemail.com; pgsql-bugs

Heikki Linnakangas <hlinnakangas@vmware.com> writes:
> For master and 9.4, I'm thinking of applying the attached. It makes it 
> clear that startScan() is not used to re-start a scan with existing scan 
> keys, but is always called on a newly initialized scan keys.

Looks reasonable to me, but should ginFreeScanKeys() null out the pointers
after freeing them, to be sure we find any incorrect accesses?  It might
not be worth the trouble; but if you have any doubts at all about the
order of operations this seems like a good safety feature.

Also, in the department of nitpicks, I'd do this:

 {
 	IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0);
+	GinScanOpaque so = (GinScanOpaque) scan->opaque;
 	TIDBitmap  *tbm = (TIDBitmap *) PG_GETARG_POINTER(1);
 	int64		ntids;

more like this:

 {
 	IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0);
 	TIDBitmap  *tbm = (TIDBitmap *) PG_GETARG_POINTER(1);
+	GinScanOpaque so = (GinScanOpaque) scan->opaque;
 	int64		ntids;

I think of the PG_GETARG calls as being an ugly stepchild of a proper
function header declaration, and as such, they should come first unless
there is an unavoidable reason not to.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit
  2015-01-28 18:12 BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit olaf.gw@googlemail.com
  2015-01-29 13:09 ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Michael Paquier <michael.paquier@gmail.com>
  2015-01-29 14:07   ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Heikki Linnakangas <hlinnakangas@vmware.com>
  2015-01-29 15:26     ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Tom Lane <tgl@sss.pgh.pa.us>
  2015-01-29 15:54       ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Heikki Linnakangas <hlinnakangas@vmware.com>
  2015-01-29 16:04         ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Tom Lane <tgl@sss.pgh.pa.us>
  2015-01-29 17:58           ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Heikki Linnakangas <hlinnakangas@vmware.com>
  2015-01-29 19:59             ` Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit Tom Lane <tgl@sss.pgh.pa.us>
@ 2015-01-30 17:18               ` Heikki Linnakangas <hlinnakangas@vmware.com>
  0 siblings, 0 replies; 10+ messages in thread

From: Heikki Linnakangas @ 2015-01-30 17:18 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Michael Paquier <michael.paquier@gmail.com>; olaf.gw@googlemail.com; pgsql-bugs

On 01/29/2015 08:59 PM, Tom Lane wrote:
> Heikki Linnakangas <hlinnakangas@vmware.com> writes:
>> For master and 9.4, I'm thinking of applying the attached. It makes it
>> clear that startScan() is not used to re-start a scan with existing scan
>> keys, but is always called on a newly initialized scan keys.
>
> Looks reasonable to me, but should ginFreeScanKeys() null out the pointers
> after freeing them, to be sure we find any incorrect accesses?  It might
> not be worth the trouble; but if you have any doubts at all about the
> order of operations this seems like a good safety feature.

Nah, I'm not worried about that. ginFreeScanKeys() frees the whole 
'keys' array, so we'd have bigger problems if there was a 
reference-after-free.

> Also, in the department of nitpicks, I'd do this:
>
>   {
>   	IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0);
> +	GinScanOpaque so = (GinScanOpaque) scan->opaque;
>   	TIDBitmap  *tbm = (TIDBitmap *) PG_GETARG_POINTER(1);
>   	int64		ntids;
>
> more like this:
>
>   {
>   	IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0);
>   	TIDBitmap  *tbm = (TIDBitmap *) PG_GETARG_POINTER(1);
> +	GinScanOpaque so = (GinScanOpaque) scan->opaque;
>   	int64		ntids;
>
> I think of the PG_GETARG calls as being an ugly stepchild of a proper
> function header declaration, and as such, they should come first unless
> there is an unavoidable reason not to.

Ok, committed with that fix.

- Heikki


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 10+ messages in thread


end of thread, other threads:[~2015-01-30 17:18 UTC | newest]

Thread overview: 10+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2015-01-28 18:12 BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit olaf.gw@googlemail.com
2015-01-29 13:09 ` Michael Paquier <michael.paquier@gmail.com>
2015-01-29 14:07   ` Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-29 14:44     ` Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-29 15:26     ` Tom Lane <tgl@sss.pgh.pa.us>
2015-01-29 15:54       ` Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-29 16:04         ` Tom Lane <tgl@sss.pgh.pa.us>
2015-01-29 17:58           ` Heikki Linnakangas <hlinnakangas@vmware.com>
2015-01-29 19:59             ` Tom Lane <tgl@sss.pgh.pa.us>
2015-01-30 17:18               ` Heikki Linnakangas <hlinnakangas@vmware.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox