Re: plpython memory leak uppon empty resultsets in all versions - Mailing list pgsql-bugs

From Tom Lane
Subject Re: plpython memory leak uppon empty resultsets in all versions
Date
Msg-id 4942.1272733720@sss.pgh.pa.us
Whole thread Raw
In response to Re: plpython memory leak uppon empty resultsets in all versions  (Andres Freund <andres@anarazel.de>)
Responses Re: plpython memory leak uppon empty resultsets in all versions  (Andres Freund <andres@anarazel.de>)
List pgsql-bugs
Andres Freund <andres@anarazel.de> writes:
> The one I measured was 9.0 only:

>  diff --git a/src/pl/plpython/plpython.c b/src/pl/plpython/plpython.c
>  index 6063628..a6dd9d0 100644
>  *** a/src/pl/plpython/plpython.c
>  --- b/src/pl/plpython/plpython.c
>  *************** plpython_inline_handler(PG_FUNCTION_ARGS
>  *** 538,546 ****
>  --- 538,548 ----
>                  PLy_procedure_compile(proc, codeblock->source_text);
>                  PLy_curr_procedure = proc;
>                  PLy_function_handler(&fake_fcinfo, proc);
>  +               PLy_free(proc);
>          }
>          PG_CATCH();
>          {
>  +               PLy_free(proc);
>                  PLy_curr_procedure = save_curr_proc;
>                  PyErr_Clear();
>                  PG_RE_THROW();


> Found by running something like:

> while true; do echo 'DO LANGUAGE plpythonu $$import
> gc;gc.collect();plpy.execute("SELECT unknown"); $$;';done|psql -h /tmp -p 5433
> postgres

I tried this and found there was still a leak after applying your patch.
What seems like the correct thing is to use PLy_procedure_delete(),
as in the attached applied patch.  With this, I see zero leak rate for
either this test or the no-error-thrown variant.

This shows that there is a pre-existing leak in PLy_procedure_delete(),
since it was failing to release the proc block itself.  I did not bother
to back-patch that, though, because the one pre-existing call was not in
a place where it'd be likely to get executed over and over.

Thanks for the report!

            regards, tom lane


Index: plpython.c
===================================================================
RCS file: /cvsroot/pgsql/src/pl/plpython/plpython.c,v
retrieving revision 1.142
diff -c -r1.142 plpython.c
*** plpython.c    30 Apr 2010 19:15:45 -0000    1.142
--- plpython.c    1 May 2010 17:03:35 -0000
***************
*** 541,552 ****
--- 541,555 ----
      }
      PG_CATCH();
      {
+         PLy_procedure_delete(proc);
          PLy_curr_procedure = save_curr_proc;
          PyErr_Clear();
          PG_RE_THROW();
      }
      PG_END_TRY();

+     PLy_procedure_delete(proc);
+
      /* Pop the error context stack */
      error_context_stack = plerrcontext.previous;

***************
*** 1664,1669 ****
--- 1667,1673 ----
      }
      if (proc->argnames)
          PLy_free(proc->argnames);
+     PLy_free(proc);
  }

  /*

pgsql-bugs by date:

Previous
From: Martin Pitt
Date:
Subject: Re: [9.0beta5/cvs head] build failure due to unchecked results
Next
From: Tom Lane
Date:
Subject: Re: CVS build problem - make world target