pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #6698: sub-query with join producing out of memory in where clause
4+ messages / 4 participants
[nested] [flat]

* BUG #6698: sub-query with join producing out of memory in where clause
@ 2012-06-19 01:01 armando.miraglia@stud-inf.unibz.it
  2012-06-19 12:39 ` Re: BUG #6698: sub-query with join producing out of memory in where clause Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  0 siblings, 1 reply; 4+ messages in thread

From: armando.miraglia@stud-inf.unibz.it @ 2012-06-19 01:01 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      6698
Logged by:          Armando Miraglia
Email address:      armando.miraglia@stud-inf.unibz.it
PostgreSQL version: 9.1.2
Operating system:   Arch Linux/Ubuntu
Description:        

Hi everybody!

Fact: while I was trying to produce a c-function I got an OOM which
RhodiumToad helped me to debug. The OOM is reproducible with also standard
query.

Environment: I tested the POC using 9.1.2 but also 9.2devel compiled
"by-hand"

Reproducibility:
- limit the memory usage
ulimit -S -v 500000
- start postgresql
postgres -D ../data.ascii/

- run the following query from psql
SELECT *                                                                    
       
  FROM generate_series(1,1000000) i                                         
       
 WHERE 100 <= (SELECT COUNT(*)                                              
       
                 FROM unnest(array(select j from
generate_series(i-100,i+100) j)) u1
                      JOIN                                                  
       
                      unnest(array(select j from
generate_series(i-100,i+100) j)) u2
                      ON (u1.u1=u2.u2));                                    
       
Error:
- psql side:
ERROR:  out of memory
DETAIL:  Failed on request of size 828.
- server side:
...
  PortalMemory: 8192 total in 1 blocks; 7888 free (0 chunks); 304 used
    PortalHeapMemory: 1024 total in 1 blocks; 824 free (0 chunks); 200 used
      ExecutorState: 458358928 total in 67 blocks; 794136 free (15965
chunks); 457564792 used
        accumArrayResult: 8192 total in 1 blocks; 5744 free (4 chunks); 2448
used
        HashTableContext: 0 total in 0 blocks; 0 free (0 chunks); 0 used
          HashBatchContext: 32768 total in 2 blocks; 8416 free (1 chunks);
24352 used
...

As you can see I am using enconding sqlascii.

Thanks a lot!
Armando Miraglia




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

* Re: BUG #6698: sub-query with join producing out of memory in where clause
  2012-06-19 01:01 BUG #6698: sub-query with join producing out of memory in where clause armando.miraglia@stud-inf.unibz.it
@ 2012-06-19 12:39 ` Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  2012-06-20 06:33   ` Re: BUG #6698: sub-query with join producing out of memory in where clause Amit Kapila <amit.kapila@huawei.com>
  2012-06-21 19:32   ` Re: BUG #6698: sub-query with join producing out of memory in where clause Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 4+ messages in thread

From: Heikki Linnakangas @ 2012-06-19 12:39 UTC (permalink / raw)
  To: armando.miraglia@stud-inf.unibz.it; +Cc: pgsql-bugs

On 19.06.2012 04:01, armando.miraglia@stud-inf.unibz.it wrote:
> The following bug has been logged on the website:
>
> Bug reference:      6698
> Logged by:          Armando Miraglia
> Email address:      armando.miraglia@stud-inf.unibz.it
> PostgreSQL version: 9.1.2
> Operating system:   Arch Linux/Ubuntu
> Description:
>
> Hi everybody!
>
> Fact: while I was trying to produce a c-function I got an OOM which
> RhodiumToad helped me to debug. The OOM is reproducible with also standard
> query.
>
> Environment: I tested the POC using 9.1.2 but also 9.2devel compiled
> "by-hand"
>
> Reproducibility:
> - limit the memory usage
> ulimit -S -v 500000
> - start postgresql
> postgres -D ../data.ascii/
>
> - run the following query from psql
> SELECT *
>
>    FROM generate_series(1,1000000) i
>
>   WHERE 100<= (SELECT COUNT(*)
>
>                   FROM unnest(array(select j from
> generate_series(i-100,i+100) j)) u1
>                        JOIN
>
>                        unnest(array(select j from
> generate_series(i-100,i+100) j)) u2
>                        ON (u1.u1=u2.u2));
>
> Error:
> - psql side:
> ERROR:  out of memory
> DETAIL:  Failed on request of size 828.
> - server side:
> ...
>    PortalMemory: 8192 total in 1 blocks; 7888 free (0 chunks); 304 used
>      PortalHeapMemory: 1024 total in 1 blocks; 824 free (0 chunks); 200 used
>        ExecutorState: 458358928 total in 67 blocks; 794136 free (15965
> chunks); 457564792 used
>          accumArrayResult: 8192 total in 1 blocks; 5744 free (4 chunks); 2448
> used
>          HashTableContext: 0 total in 0 blocks; 0 free (0 chunks); 0 used
>            HashBatchContext: 32768 total in 2 blocks; 8416 free (1 chunks);
> 24352 used
> ...

This test case can be further reduced into:

explain analyze
SELECT * FROM generate_series(1,100000) i
WHERE (SELECT array(select repeat('a', 10000) || i) u1) is not null;

We're leaking the array constructed on each row, in ExecSetParamPlan(). 
At line 1000 in nodeSubplan.c, we create a new array and store it as the 
value of the PARAM_EXEC parameter. But it's never free'd. The old value 
of the parameter is simply overwritten.

I'm not sure what the correct fix is. I suppose we could pfree() the old 
value before overwriting it, but I'm not sure if that's safe, or if 
there might still be references to the old value somewhere in the executor.

-- 
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com



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

* Re: BUG #6698: sub-query with join producing out of memory in where clause
  2012-06-19 01:01 BUG #6698: sub-query with join producing out of memory in where clause armando.miraglia@stud-inf.unibz.it
  2012-06-19 12:39 ` Re: BUG #6698: sub-query with join producing out of memory in where clause Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
@ 2012-06-20 06:33   ` Amit Kapila <amit.kapila@huawei.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Amit Kapila @ 2012-06-20 06:33 UTC (permalink / raw)
  To: 'Heikki Linnakangas' <heikki.linnakangas@enterprisedb.com>; armando.miraglia@stud-inf.unibz.it; +Cc: pgsql-bugs

> I'm not sure what the correct fix is. I suppose we could pfree() the old 
> value before overwriting it, but I'm not sure if that's safe, or if 
> there might still be references to the old value somewhere in the executor.

It will resolve the current problem but I am also not sure whether it can create
any other problem because in this function most of the work is done in per-query memory context.
One thing if we can clarify that why per-tuple memory context is not sufficient for this value
than it can be easy to conclude on solution for this problem. 

Another thing I have noticed is that in function ExecScanSubPlan, the similar work is not done in
Per-query memory context, I am not sure if it is of any relevance for the problem you mentioned.


With Regards,
Amit Kapila.




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

* Re: BUG #6698: sub-query with join producing out of memory in where clause
  2012-06-19 01:01 BUG #6698: sub-query with join producing out of memory in where clause armando.miraglia@stud-inf.unibz.it
  2012-06-19 12:39 ` Re: BUG #6698: sub-query with join producing out of memory in where clause Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
@ 2012-06-21 19:32   ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 4+ messages in thread

From: Tom Lane @ 2012-06-21 19:32 UTC (permalink / raw)
  To: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>; +Cc: armando.miraglia@stud-inf.unibz.it; pgsql-bugs

Heikki Linnakangas <heikki.linnakangas@enterprisedb.com> writes:
> This test case can be further reduced into:

> explain analyze
> SELECT * FROM generate_series(1,100000) i
> WHERE (SELECT array(select repeat('a', 10000) || i) u1) is not null;

> We're leaking the array constructed on each row, in ExecSetParamPlan(). 
> At line 1000 in nodeSubplan.c, we create a new array and store it as the 
> value of the PARAM_EXEC parameter. But it's never free'd. The old value 
> of the parameter is simply overwritten.

> I'm not sure what the correct fix is. I suppose we could pfree() the old 
> value before overwriting it, but I'm not sure if that's safe, or if 
> there might still be references to the old value somewhere in the executor.

I think it should be safe to pfree the prior value at this point.  If
you compare the code for scalar subplan results, a few lines above here,
pass-by-ref param values are references into node->curTuple which gets
summarily freed on the next cycle.  So if anybody is keeping a pointer
around then it would already be failing for non-ARRAY cases, and we've
not seen any such reports.  I think we can handle array results the same
way as curTuple, ie, keep a link in the node's state.  (I thought first
about trying to pfree the prior contents of prm->value itself, but I'm
less sure that that is safe --- if memory serves, the same ParamExecData
slot can sometimes be used for multiple purposes.)

Will give it a try ...

			regards, tom lane



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


end of thread, other threads:[~2012-06-21 19:32 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-06-19 01:01 BUG #6698: sub-query with join producing out of memory in where clause armando.miraglia@stud-inf.unibz.it
2012-06-19 12:39 ` Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
2012-06-20 06:33   ` Amit Kapila <amit.kapila@huawei.com>
2012-06-21 19:32   ` Tom Lane <tgl@sss.pgh.pa.us>

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