pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: turon.david@seznam.cz
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #14149: when use LATERAL functions with IMMUTABLE called multiple times
Date: Thu, 19 May 2016 09:43:08 -0400
Message-ID: <15800.1463665388@sss.pgh.pa.us> (raw)
In-Reply-To: <20160519090609.12503.12814@wrigleys.postgresql.org>
References: <20160519090609.12503.12814@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

turon.david@seznam.cz writes:
> we found strange behavior LATERAL when we upgrade from 9.3.12 to 9.5.3,
> simple example:

> CREATE OR REPLACE FUNCTION f_imutable(OUT a int, out b int) AS $$ 
> BEGIN 
>         a := 1; 
>         b := 2; 
>         RAISE NOTICE 'call function f_imutable'; 
> END; 
> $$ LANGUAGE plpgsql IMMUTABLE; 

> --execution on 9.3.12, for one row one call

> SELECT (x.y).a, (x.y).b FROM generate_series(1,1), LATERAL (SELECT
> f_imutable()) AS x(y);

> --execution on 9.5.3 called 2x

I think you are confusing an implementation artifact of older versions
with a guaranteed behavior.  Declaring a function IMMUTABLE (or STABLE)
says that it's okay if the generated plan calls the function more or fewer
times than naive analysis might suggest.  9.3 happened not to do so, for
this specific query, but 9.5 does.

Really the best fix for this is to mark a function VOLATILE if you can't
afford for the planner to rearrange the calls.  In this particular case,
you might also consider rearranging the query so that the function is
called as a FROM item rather than a select-list item:

# SELECT x.a, x.b FROM generate_series(1,1), LATERAL f_imutable() as x;
NOTICE:  call function f_imutable
 a | b 
---+---
 1 | 2
(1 row)

but I wouldn't really want to promise that that won't ever change behavior
either.  The argument for it is as much that it's a less messy notation
as anything else.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (2+ messages)

Message-ID: <15800.1463665388@sss.pgh.pa.us>
Permalink:  ../15800.1463665388@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/15800.1463665388@sss.pgh.pa.us

 · 

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-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, turon.david@seznam.cz
  Subject: Re: BUG #14149: when use LATERAL functions with IMMUTABLE called multiple times
  In-Reply-To: <15800.1463665388@sss.pgh.pa.us>
  References: <20160519090609.12503.12814@wrigleys.postgresql.org> <15800.1463665388@sss.pgh.pa.us>

* 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