Обсуждение: execution of prepared statements leaks memory

Поиск
Список
Период
Сортировка

execution of prepared statements leaks memory

От
Dmitry Karasik
Дата:
Hi,

I'm not sure whether I've stumbled over a bug or a feature, so I'm
curious if someone might explain that. The statement below declares
a stored procedure:

CREATE LANGUAGE PLPGSQL;
DROP TABLE memleak_test;
CREATE TABLE memleak_test (id serial);

CREATE OR REPLACE FUNCTION memleak(INTEGER) RETURNS INTEGER AS $$
DECLARE     c ALIAS FOR $1;     i INTEGER;
BEGINEXECUTE 'PREPARE leak AS INSERT into memleak_test values (1);'; FOR i IN 1..c LOOP    EXECUTE 'EXECUTE leak;';END
LOOP;EXECUTE'DEALLOCATE leak;';
 
RETURN 0;
END;
$$ LANGUAGE PLPGSQL;

and that procedure, if being called repeatedly, say, as 
'SELECT memleak(30000)' causes postmaster to eat memory rather noticeably,
one my machine, for example, first call to memleak(30000) eats 100M, second
50M, and the subsequent calls leak 1-3M per call.
The same leak occurs when I've tried the same code writte on plperl, so
it doesn't seem that the effect is related to plpgsql.

-- 
Sincerely,Dmitry Karasik



Re: execution of prepared statements leaks memory

От
Tom Lane
Дата:
Dmitry Karasik <dmitry@karasik.eu.org> writes:
> I'm not sure whether I've stumbled over a bug or a feature, so I'm
> curious if someone might explain that.

It's a bug --- _SPI_execute_plan isn't handling the possibility that
a utility command could create a tupletable that could be passed back
to the caller.  So the tuptable gets leaked.  I'll try to fix this
for 8.1, not sure if it'd be wise to try to back-patch.
        regards, tom lane