Re: plpython memory leak uppon empty resultsets in all versions

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: pgsql-bugs(at)postgresql(dot)org, Peter Eisentraut <peter_e(at)gmx(dot)net>
Subject: Re: plpython memory leak uppon empty resultsets in all versions
Date: 2010-05-01 17:08:40
Message-ID: 4942.1272733720@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Andres Freund <andres(at)anarazel(dot)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);
}

/*

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Andres Freund 2010-05-01 18:00:34 Re: plpython memory leak uppon empty resultsets in all versions
Previous Message Tom Lane 2010-05-01 15:36:57 Re: PostgreSQL 8.4 - dumping database connection privileges