Re: Performance of pl/pgsql functions?

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Wells Oliver <wellsoliver(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Performance of pl/pgsql functions?
Date: 2012-09-14 05:27:02
Message-ID: CAFj8pRALqqmw_dA5mzGMy_2AcHR8Wu_r2oDaTfY4Uei9Q3kPZA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

2012/9/14 Wells Oliver <wellsoliver(at)gmail(dot)com>:
> Do these tend to perform well? I have some simple formulas in functions like
> so:
>
> CREATE OR REPLACE FUNCTION public.stat_foo(a integer, b integer, c integer)
> RETURNS numeric AS
> $BODY$
>
> declare ret numeric;
>
> begin
> select (a+b) / c::numeric into ret;
> return round(ret, 3);
> end
>
> $BODY$
> LANGUAGE plpgsql IMMUTABLE COST 100;

it is not good

CREATE OR REPLACE FUNCTION public.stat_foo(a integer, b integer, c integer)
RETURNS numeric AS $$
BEGIN
RETURN round((a + b) / c::numeric), 3)::numeric;
END
$$ LANGUAGE plpgsql IMMUTABLE;

will be significantly faster

probably SQL function will be fastest

CREATE OR REPLACE FUNCTION public.stat_foo(a integer, b integer, c integer)
RETURNS numeric AS $$
SELECT round(($1 + $2) / $3::numeric), 3)::numeric;
$$ LANGUAGE sql;

Regards

Pavel Stehule

>
> The reason I'm doing this is because i repeat this formula in a bunch of
> views and queries, and it's easier to have one function. Would this somehow
> be slower than reproducing the formula in every view its used? I'm hoping
> not...
>
> --
> Wells Oliver
> wellsoliver(at)gmail(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Johan Nel 2012-09-14 06:17:56 Re: Best free tool for relationship extraction
Previous Message Wells Oliver 2012-09-14 05:17:29 Performance of pl/pgsql functions?