pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Ken Tanzer <ken.tanzer@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: PG-General Mailing List <pgsql-general@postgresql.org>
Subject: Re: Immutable function WAY slower than Stable function?
Date: Tue, 07 Aug 2018 07:59:59 +0200
Message-ID: <1533621599.2438.4.camel@cybertec.at> (raw)
In-Reply-To: <CAD3a31XELjttH7y8TVxva56jqBcnUA3koPy02xySzWaVDBif4A@mail.gmail.com>
References: <CAD3a31Vh6f5eLhK0kQL6b5mxr2Fc0qda1yW8YqTvDQRxcpg9jg@mail.gmail.com>
	<17961.1533597095@sss.pgh.pa.us>
	<CAD3a31XELjttH7y8TVxva56jqBcnUA3koPy02xySzWaVDBif4A@mail.gmail.com>

Ken Tanzer wrote:
> On Mon, Aug 6, 2018 at 4:11 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> > Ken Tanzer <ken.tanzer@gmail.com> writes:
> > > Hi.  I was recently troubleshooting a function, and realized it had
> > > incorrectly been declared as Immutable, when it should have been declared
> > > Stable.  When I changed it to Stable, the query I was running ran
> > > dramatically faster. Digging into this a little more, this is what I found:
>
> spc=> EXPLAIN (ANALYZE,BUFFERS,VERBOSE) SELECT client_id,si_imm(client_id) FROM tbl_residence_own;
>                                                           QUERY PLAN                                                           
> -------------------------------------------------------------------------------------------------------------------------------
>  Seq Scan on public.tbl_residence_own  (cost=0.00..1990.02 rows=6977 width=8) (actual time=3.771..22665.604 rows=6983 loops=1)
>    Output: client_id, si_imm(client_id)
>    Buffers: shared hit=199814
>  Planning time: 0.156 ms
>  Execution time: 22677.333 ms
> (5 rows)
> 
> spc=> EXPLAIN (ANALYZE,BUFFERS,VERBOSE) SELECT client_id,si_stable(client_id) FROM tbl_residence_own;
>                                                           QUERY PLAN                                                          
> ------------------------------------------------------------------------------------------------------------------------------
>  Seq Scan on public.tbl_residence_own  (cost=0.00..3734.27 rows=6977 width=8) (actual time=3.100..1302.888 rows=6983 loops=1)
>    Output: client_id, staff_inspector_stable(client_id, target_date())
>    Buffers: shared hit=60174
>  Planning time: 0.354 ms
>  Execution time: 1315.746 ms
> (5 rows)

That seems to say that the _stable function is running much faster.
Buth functions don't get inlined.

I'd dig into the functions and find out how long the queries in
them take.  auto_explain is a good helper for that.

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com




view thread (19+ messages)  latest in thread

Message-ID: <1533621599.2438.4.camel@cybertec.at>
Permalink:  ../1533621599.2438.4.camel@cybertec.at/
Also on:    postgresql.org/message-id/1533621599.2438.4.camel@cybertec.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: laurenz.albe@cybertec.at, ken.tanzer@gmail.com, tgl@sss.pgh.pa.us
  Subject: Re: Immutable function WAY slower than Stable function?
  In-Reply-To: <1533621599.2438.4.camel@cybertec.at>
  References: <CAD3a31Vh6f5eLhK0kQL6b5mxr2Fc0qda1yW8YqTvDQRxcpg9jg@mail.gmail.com> <17961.1533597095@sss.pgh.pa.us> <CAD3a31XELjttH7y8TVxva56jqBcnUA3koPy02xySzWaVDBif4A@mail.gmail.com> <1533621599.2438.4.camel@cybertec.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox