plpgsql memory leaks

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: plpgsql memory leaks
Date: 2024-01-12 09:27:25
Message-ID: CAFj8pRCWZBS4pXSZd8ibsSivVkzsUn1d8eX0y4jOYmRB+Cw2yg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi

I have reported very memory expensive pattern:

CREATE OR REPLACE FUNCTION public.fx(iter integer)
RETURNS void
LANGUAGE plpgsql
AS $function$
declare
c cursor(m bigint) for select distinct i from generate_series(1, m) g(i);
t bigint;
s bigint;
begin
for i in 1..iter
loop
open c(m := i * 10000);
s := 0;
loop
fetch c into t;
exit when not found;
s := s + t;
end loop;
close c; raise notice '%=%', i, s;
end loop;
end;
$function$
;

This script takes for 100 iterations 100MB

but rewritten

CREATE OR REPLACE FUNCTION public.fx(iter integer)
RETURNS void
LANGUAGE plpgsql
AS $function$
declare
t bigint;
s bigint;
begin
for i in 1..iter
loop
s := 0;
for t in select ic from generate_series(1, i * 10000) g(ic)
loop
s := s + t;
end loop;
raise notice '%=%', i, s;
end loop;
end;
$function$

takes lot of megabytes of memory too.

Regards

Pavel

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2024-01-12 09:58:21 Re: alter table add x wrong error position
Previous Message Masahiko Sawada 2024-01-12 08:49:04 Re: [PoC] Improve dead tuple storage for lazy vacuum