BUG #3418: Memory leak with sql EXCEPTION? - Mailing list pgsql-bugs

From Viatcheslav
Subject BUG #3418: Memory leak with sql EXCEPTION?
Date
Msg-id 200706291914.l5TJETf5086437@wwwmaster.postgresql.org
Whole thread Raw
Responses Re: BUG #3418: Memory leak with sql EXCEPTION?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-bugs
The following bug has been logged online:

Bug reference:      3418
Logged by:          Viatcheslav
Email address:      vka@ipcb.net
PostgreSQL version: 8.2.4
Operating system:   RedHat Linux kernel 2.6.9-34.EL
Description:        Memory leak with sql EXCEPTION?
Details:

On our system we came across the following issue:

/---------------------------------------------------/
create table dummy(
    id integer primary key,
    value varchar(10)
);

CREATE OR REPLACE FUNCTION "public"."test" (fi integer) RETURNS
"pg_catalog"."void" AS
$body$
declare
 vi integer;
 idx integer := 0;
begin

while idx < fi loop
    idx := idx + 1;
    begin
        insert into dummy values (idx, idx::varchar);
    exception
    when others then
       raise exception '% %', idx, 'stop';
    end;
end loop;
end;
$body$
LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY DEFINER;

select test(10000000);

/---------------------------------------------------/

We've been watching CPU and memory usage during the execution of the
'select' with and without exception trap of 'insert'. While in the second
case memory usage kept being relatively low and stayed constant at the level
of approx 2.9Mb across the execution time, in the first case it gradually
raised with step of approx 6Mb up to somewhat 140Mb in the end.
With more complicated PL/PgSQL code (with the same structure: begin -
exception - end block in the main function, some complex function (without
exceptions inside them) doing actual job were called from there) we've run
out of virtual memory (> 1Gb) already with 30000 cycles loop and the process
got killed by operating system. Again, everything worked just fine with
negligible memory consumption without exceptions.
We are aware of additional costs of exceptions yet this one appears to be a
memory leak.

My apologies for poor-styled report in advance for this is my first one.

pgsql-bugs by date:

Previous
From: David Boesch
Date:
Subject: Re: BUG #3417: Foreign key constraint violation occurs unexpectedly
Next
From: Tom Lane
Date:
Subject: Re: BUG #3418: Memory leak with sql EXCEPTION?