Re: plpgsql leaking memory when stringifying datums
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Jan Urbański <wulczer@wulczer.org>
Cc: Postgres - Hackers <pgsql-hackers@postgresql.org>
Date: 2012-02-11T02:05:04Z
Lists: pgsql-hackers
Attachments
- plpgsql-convert-value-leak-2.patch (text/x-patch) patch
=?UTF-8?B?SmFuIFVyYmHFhHNraQ==?= <wulczer@wulczer.org> writes: > While chasing a PL/Python memory leak, I did a few tests with PL/pgSQL > and I think there are places where memory is not freed sufficiently early. I think the basic issue here is that the type output function might generate (and not bother to free) additional cruft besides its output string, so that pfree'ing the output alone is not sufficient to avoid a memory leak if the call occurs in a long-lived context. However, I don't much care for the details of the proposed patch: if we're going to fix this by running the output function in the per-tuple memory context, and expecting the caller to do exec_eval_cleanup later, why should we add extra pstrdup/pfree overhead? We can just leave the result in the temp context in most cases, and thus get a net savings rather than a net cost from fixing this. The attached modified patch does it like that. BTW, it occurs to me to wonder whether we need to worry about such subsidiary leaks in type input functions as well. I see at least one place where pl_exec.c is tediously freeing the result of exec_simple_cast_value, but if there are secondary leaks that's not going to be good enough. Maybe we should switch over to a similar definition where the cast result is in the per-tuple context, and you've got to copy it if you want it to be long-lived. regards, tom lane