pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #5842: Memory leak in PL/Python when taking slices of results
6+ messages / 5 participants
[nested] [flat]

* BUG #5842: Memory leak in PL/Python when taking slices of results
@ 2011-01-18 16:30 Daniel Popowich <danielpopowich@gmail.com>
  2011-03-11 11:02 ` Re: [BUGS] BUG #5842: Memory leak in PL/Python when taking slices of results Bruce Momjian <bruce@momjian.us>
  2011-03-17 18:31 ` Re: BUG #5842: Memory leak in PL/Python when taking slices of results Alvaro Herrera <alvherre@commandprompt.com>
  0 siblings, 2 replies; 6+ messages in thread

From: Daniel Popowich @ 2011-01-18 16:30 UTC (permalink / raw)
  To: pgsql-bugs


The following bug has been logged online:

Bug reference:      5842
Logged by:          Daniel Popowich
Email address:      danielpopowich@gmail.com
PostgreSQL version: 8.4.6
Operating system:   x86_64-pc-linux-gnu (Ubuntu 10.04.1)
Description:        Memory leak in PL/Python when taking slices of results
Details: 

There is a memory leak in PL/Python when taking slices of results.  This was
first discussed in pgsql-general:

  http://archives.postgresql.org/pgsql-general/2011-01/msg00367.php

Thanks to Alex Hunsaker for pinpointing the problem to slices.  The
following code (a modification of Alex's) demonstrates the problem well...in
a database with plpythonu installed:

    -- leaks big time
    CREATE  or replace FUNCTION py_leak() RETURNS void
       LANGUAGE plpythonu
       AS $$
    results = plpy.execute("""select generate_series(0, 1000000)""")
    slice_creates_leak = results[:]
    for r in slice_creates_leak:
        pass
    return
    $$;

    -- does not leak
    CREATE  or replace FUNCTION py_no_leak() RETURNS void
       LANGUAGE plpythonu
       AS $$
    results = plpy.execute("""select generate_series(0, 1000000)""")
    for noleak in results:
        pass
    return
    $$;


I traced the bug to PLy_result_slice() in src/pl/plpython/plpython.c.  That
function calls the python API function PyList_GetSlice() and erroneously
increments the reference count before returning the result to the caller. 
PyList_GetSlice returns a *new* reference, not a borrowed one, so it should
just return the object as-is.

A patch is attached below.

Cheers,

Dan Popowich


----------------------------------------------------------------------


inline patch 1: src/pl/plpython/plpython.c (+1/-6 lines) download inline-1.patch*** src/pl/plpython/plpython.c~ 2010-12-13 21:59:19.000000000 -0500 --- src/pl/plpython/plpython.c 2011-01-18 11:18:28.857831733 -0500 *************** *** 2328,2341 **** static PyObject * PLy_result_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx) { - PyObject *rv; PLyResultObject *ob = (PLyResultObject *) arg; ! rv = PyList_GetSlice(ob->rows, lidx, hidx); ! if (rv == NULL) ! return NULL; ! Py_INCREF(rv); ! return rv; } static int --- 2328,2336 ---- static PyObject * PLy_result_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx) { PLyResultObject *ob = (PLyResultObject *) arg; ! return PyList_GetSlice(ob->rows, lidx, hidx); } static int
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: [BUGS] BUG #5842: Memory leak in PL/Python when taking slices of results
  2011-01-18 16:30 BUG #5842: Memory leak in PL/Python when taking slices of results Daniel Popowich <danielpopowich@gmail.com>
@ 2011-03-11 11:02 ` Bruce Momjian <bruce@momjian.us>
  2011-03-17 02:40   ` Re: Re: [BUGS] BUG #5842: Memory leak in PL/Python when taking slices of results Robert Haas <robertmhaas@gmail.com>
  1 sibling, 1 reply; 6+ messages in thread

From: Bruce Momjian @ 2011-03-11 11:02 UTC (permalink / raw)
  To: Daniel Popowich <danielpopowich@gmail.com>; +Cc: PostgreSQL-development <pgsql-hackers@postgreSQL.org>


What has been done with this report/fix?

---------------------------------------------------------------------------

Daniel Popowich wrote:
> 
> The following bug has been logged online:
> 
> Bug reference:      5842
> Logged by:          Daniel Popowich
> Email address:      danielpopowich@gmail.com
> PostgreSQL version: 8.4.6
> Operating system:   x86_64-pc-linux-gnu (Ubuntu 10.04.1)
> Description:        Memory leak in PL/Python when taking slices of results
> Details: 
> 
> There is a memory leak in PL/Python when taking slices of results.  This was
> first discussed in pgsql-general:
> 
>   http://archives.postgresql.org/pgsql-general/2011-01/msg00367.php
> 
> Thanks to Alex Hunsaker for pinpointing the problem to slices.  The
> following code (a modification of Alex's) demonstrates the problem well...in
> a database with plpythonu installed:
> 
>     -- leaks big time
>     CREATE  or replace FUNCTION py_leak() RETURNS void
>        LANGUAGE plpythonu
>        AS $$
>     results = plpy.execute("""select generate_series(0, 1000000)""")
>     slice_creates_leak = results[:]
>     for r in slice_creates_leak:
>         pass
>     return
>     $$;
> 
>     -- does not leak
>     CREATE  or replace FUNCTION py_no_leak() RETURNS void
>        LANGUAGE plpythonu
>        AS $$
>     results = plpy.execute("""select generate_series(0, 1000000)""")
>     for noleak in results:
>         pass
>     return
>     $$;
> 
> 
> I traced the bug to PLy_result_slice() in src/pl/plpython/plpython.c.  That
> function calls the python API function PyList_GetSlice() and erroneously
> increments the reference count before returning the result to the caller. 
> PyList_GetSlice returns a *new* reference, not a borrowed one, so it should
> just return the object as-is.
> 
> A patch is attached below.
> 
> Cheers,
> 
> Dan Popowich
> 
> 
> ----------------------------------------------------------------------
> 
> 
> *** src/pl/plpython/plpython.c~	2010-12-13 21:59:19.000000000 -0500
> --- src/pl/plpython/plpython.c	2011-01-18 11:18:28.857831733 -0500
> ***************
> *** 2328,2341 ****
>   static PyObject *
>   PLy_result_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx)
>   {
> - 	PyObject   *rv;
>   	PLyResultObject *ob = (PLyResultObject *) arg;
>   
> ! 	rv = PyList_GetSlice(ob->rows, lidx, hidx);
> ! 	if (rv == NULL)
> ! 		return NULL;
> ! 	Py_INCREF(rv);
> ! 	return rv;
>   }
>   
>   static int
> --- 2328,2336 ----
>   static PyObject *
>   PLy_result_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx)
>   {
>   	PLyResultObject *ob = (PLyResultObject *) arg;
>   
> ! 	return PyList_GetSlice(ob->rows, lidx, hidx);
>   }
>   
>   static int
> 
> -- 
> Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-bugs

-- 
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + It's impossible for everything to be true. +



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

* Re: Re: [BUGS] BUG #5842: Memory leak in PL/Python when taking slices of results
  2011-01-18 16:30 BUG #5842: Memory leak in PL/Python when taking slices of results Daniel Popowich <danielpopowich@gmail.com>
  2011-03-11 11:02 ` Re: [BUGS] BUG #5842: Memory leak in PL/Python when taking slices of results Bruce Momjian <bruce@momjian.us>
@ 2011-03-17 02:40   ` Robert Haas <robertmhaas@gmail.com>
  2011-03-17 15:01     ` Re: Re: [BUGS] BUG #5842: Memory leak in PL/Python when taking slices of results Marko Kreen <markokr@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Robert Haas @ 2011-03-17 02:40 UTC (permalink / raw)
  To: Bruce Momjian <bruce@momjian.us>; +Cc: Daniel Popowich <danielpopowich@gmail.com>; PostgreSQL-development <pgsql-hackers@postgresql.org>

On Fri, Mar 11, 2011 at 6:02 AM, Bruce Momjian <bruce@momjian.us> wrote:
> What has been done with this report/fix?

AFAIK, nothing.  Added to 9.1 open items list.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company



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

* Re: Re: [BUGS] BUG #5842: Memory leak in PL/Python when taking slices of results
  2011-01-18 16:30 BUG #5842: Memory leak in PL/Python when taking slices of results Daniel Popowich <danielpopowich@gmail.com>
  2011-03-11 11:02 ` Re: [BUGS] BUG #5842: Memory leak in PL/Python when taking slices of results Bruce Momjian <bruce@momjian.us>
  2011-03-17 02:40   ` Re: Re: [BUGS] BUG #5842: Memory leak in PL/Python when taking slices of results Robert Haas <robertmhaas@gmail.com>
@ 2011-03-17 15:01     ` Marko Kreen <markokr@gmail.com>
  2011-03-17 15:15       ` Re: Re: [BUGS] BUG #5842: Memory leak in PL/Python when taking slices of results Alvaro Herrera <alvherre@commandprompt.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Marko Kreen @ 2011-03-17 15:01 UTC (permalink / raw)
  To: Robert Haas <robertmhaas@gmail.com>; +Cc: Bruce Momjian <bruce@momjian.us>; Daniel Popowich <danielpopowich@gmail.com>; PostgreSQL-development <pgsql-hackers@postgresql.org>

On Thu, Mar 17, 2011 at 4:40 AM, Robert Haas <robertmhaas@gmail.com> wrote:
> On Fri, Mar 11, 2011 at 6:02 AM, Bruce Momjian <bruce@momjian.us> wrote:
>> What has been done with this report/fix?
>
> AFAIK, nothing.  Added to 9.1 open items list.

The patch seems to do the right thing.

-- 
marko



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

* Re: Re: [BUGS] BUG #5842: Memory leak in PL/Python when taking slices of results
  2011-01-18 16:30 BUG #5842: Memory leak in PL/Python when taking slices of results Daniel Popowich <danielpopowich@gmail.com>
  2011-03-11 11:02 ` Re: [BUGS] BUG #5842: Memory leak in PL/Python when taking slices of results Bruce Momjian <bruce@momjian.us>
  2011-03-17 02:40   ` Re: Re: [BUGS] BUG #5842: Memory leak in PL/Python when taking slices of results Robert Haas <robertmhaas@gmail.com>
  2011-03-17 15:01     ` Re: Re: [BUGS] BUG #5842: Memory leak in PL/Python when taking slices of results Marko Kreen <markokr@gmail.com>
@ 2011-03-17 15:15       ` Alvaro Herrera <alvherre@commandprompt.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Alvaro Herrera @ 2011-03-17 15:15 UTC (permalink / raw)
  To: Marko Kreen <markokr@gmail.com>; +Cc: Robert Haas <robertmhaas@gmail.com>; Bruce Momjian <bruce@momjian.us>; Daniel Popowich <danielpopowich@gmail.com>; PostgreSQL-development <pgsql-hackers@postgresql.org>

Excerpts from Marko Kreen's message of jue mar 17 12:01:13 -0300 2011:
> On Thu, Mar 17, 2011 at 4:40 AM, Robert Haas <robertmhaas@gmail.com> wrote:
> > On Fri, Mar 11, 2011 at 6:02 AM, Bruce Momjian <bruce@momjian.us> wrote:
> >> What has been done with this report/fix?
> >
> > AFAIK, nothing.  Added to 9.1 open items list.
> 
> The patch seems to do the right thing.

Looking into this.  AFAICT this needs to be backported.

-- 
Álvaro Herrera <alvherre@commandprompt.com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support



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

* Re: BUG #5842: Memory leak in PL/Python when taking slices of results
  2011-01-18 16:30 BUG #5842: Memory leak in PL/Python when taking slices of results Daniel Popowich <danielpopowich@gmail.com>
@ 2011-03-17 18:31 ` Alvaro Herrera <alvherre@commandprompt.com>
  1 sibling, 0 replies; 6+ messages in thread

From: Alvaro Herrera @ 2011-03-17 18:31 UTC (permalink / raw)
  To: Daniel Popowich <danielpopowich@gmail.com>; +Cc: pgsql-bugs

Excerpts from Daniel Popowich's message of mar ene 18 13:30:04 -0300 2011:

> I traced the bug to PLy_result_slice() in src/pl/plpython/plpython.c.  That
> function calls the python API function PyList_GetSlice() and erroneously
> increments the reference count before returning the result to the caller. 
> PyList_GetSlice returns a *new* reference, not a borrowed one, so it should
> just return the object as-is.
> 
> A patch is attached below.

Thanks, applied and backpatched all the way back to 8.2.

-- 
Álvaro Herrera <alvherre@commandprompt.com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support



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


end of thread, other threads:[~2011-03-17 18:31 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-01-18 16:30 BUG #5842: Memory leak in PL/Python when taking slices of results Daniel Popowich <danielpopowich@gmail.com>
2011-03-11 11:02 ` Bruce Momjian <bruce@momjian.us>
2011-03-17 02:40   ` Robert Haas <robertmhaas@gmail.com>
2011-03-17 15:01     ` Marko Kreen <markokr@gmail.com>
2011-03-17 15:15       ` Alvaro Herrera <alvherre@commandprompt.com>
2011-03-17 18:31 ` Alvaro Herrera <alvherre@commandprompt.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