Re: Transaction-lifespan memory leak with plpgsql DO blocks

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>, Yeb Havinga <yebhavinga(at)gmail(dot)com>
Subject: Re: Transaction-lifespan memory leak with plpgsql DO blocks
Date: 2013-11-13 20:06:17
Message-ID: 23586.1384373177@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> On Tue, Nov 12, 2013 at 11:18 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> Or we could say "what the heck are you doing executing tens of
>> thousands of DO blocks? Make it into a real live function;
>> you'll save a lot of cycles on parsing costs." I'm not sure that
>> this is a usage pattern we ought to be optimizing for.

> I'm not volunteering to spend time fixing this, but I disagree with
> the premise that it isn't worth fixing, because I think it's a POLA
> violation.

Yeah, I'm not terribly comfortable with letting it go either. Attached
is a proposed patch. I couldn't see any nice way to do it without adding
a field to PLpgSQL_execstate, so this isn't a feasible solution for
back-patching (it'd break the plpgsql debugger). However, given the
infrequency of complaints, I think fixing it in 9.4 and up is good enough.

I checked that this eliminates the memory leak using this test case:

do $outer$
begin
for i in 1..1000000 loop
execute $e$
do $$
declare x int = 0;
begin
x := x + 1;
end;
$$;
$e$;
end loop;
end;
$outer$;

which eats a couple GB in HEAD and nothing with the patch.
The run time seems to be the same or a bit less, too.

Any objections to applying this to HEAD?

regards, tom lane

Attachment Content-Type Size
do-block-memory-leak-fix.patch text/x-diff 18.7 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Gavin Flower 2013-11-13 20:19:48 Re: hail the CFM
Previous Message Peter Eisentraut 2013-11-13 19:53:44 Re: nested hstore patch