when I do some profiling of plpgsql, usually I surprised how significant overhead has expression execution. Any calculations are very slow.
This is not typical example of plpgsql, but it shows cleanly where is a overhead
CREATE OR REPLACE FUNCTION public.foo() RETURNS void LANGUAGE plpgsql IMMUTABLE AS $function$ declare i bigint = 0; begin while i < 100000000 loop i := i + 1; end loop; end; $function$
Is interesting so overhead of plan cache about 15%
The execution needs 32 sec on Postgres13 and 27sec on Postgres8.2
On same computer same example in Perl needs only 7 sec.