Re: Function runtime increases after 5 calls in the same session.

From: Ilya Ashchepkov <koctep(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Function runtime increases after 5 calls in the same session.
Date: 2014-10-22 02:43:00
Message-ID: m275jq$4dv$1@ger.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Marti Raudsepp <marti(at)juffo(dot)org> Wrote in message:
> Hi
>
> On Tue, Oct 21, 2014 at 12:53 PM, Ilya I. Ashchepkov <koctep(at)gmail(dot)com> wrote:
>> I wrote a function and during testing it I came across the strange
>> behaviour.
>> Function runtime is about 200ms first 5 times, 6th and futher calls takes
>> ~22000 ms.
>> I simplified my schema, you can see it in attached file.
>> I've tested on 9.4beta3 and 9.3.5.
>
> I didn't look at the test case. But most likely the problem is that
> after 5 executions, plancache decides that a generic plan is no more
> expensive than a custom plan, and reverts to executing the generic
> one. But in reality there is a big difference in execution time.
>
> See this for how the logic works:
> https://github.com/postgres/postgres/blob/master/src/backend/utils/cache/plancache.c#L1036
>
> As a workaround you could use PL/pgSQL EXECUTE to run the query, which
> isn't subject to generic plans:
> http://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
>
> Regards,
> Marti
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>
>

Thank you. I tried execute and it helps.
--

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2014-10-22 03:53:59 Re: Function runtime increases after 5 calls in the same session.
Previous Message John R Pierce 2014-10-22 00:54:32 Re: Yosemite (OSX 10.0) problems with Postgresql