Re: Getting an optimal plan on the first execution of a pl/pgsql function - Mailing list pgsql-performance

From Tom Lane
Subject Re: Getting an optimal plan on the first execution of a pl/pgsql function
Date
Msg-id 5607.1450125717@sss.pgh.pa.us
Whole thread Raw
In response to Re: Getting an optimal plan on the first execution of a pl/pgsql function  ("David G. Johnston" <david.g.johnston@gmail.com>)
Responses Re: Getting an optimal plan on the first execution of a pl/pgsql function  (Pedro França <pedro.franca@golsat.com.br>)
List pgsql-performance
"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Mon, Dec 14, 2015 at 11:53 AM, Pedro França <pedro.franca@golsat.com.br>
> wrote:
>> When I test with EXPLAIN ANALYZE after the first execution, the query runs
>> really fast but the aplication sessions call the function only once and
>> then are terminated. I need that the first execution use the actual
>> optimized plan.

> Your problem that the performance improvement is
> seen due to caching effects.  If you throw away the cache you loose the
> improvement.

Yeah.  And it's not only the function itself, it's catalog caches and a
bunch of other stuff.  Basically, you should expect that the first few
queries executed by any PG session are going to be slower than those
executed later.  If you can't fix your application to hold sessions open
for a reasonable amount of time, use a connection pooler to do it for you
(pgpooler for instance).

            regards, tom lane


pgsql-performance by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: Getting an optimal plan on the first execution of a pl/pgsql function
Next
From: Pedro França
Date:
Subject: Re: Getting an optimal plan on the first execution of a pl/pgsql function