Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b3OEM-00061F-BS for pgsql-bugs@arkaria.postgresql.org; Thu, 19 May 2016 13:43:14 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1b3OEL-0004a0-Pg for pgsql-bugs@arkaria.postgresql.org; Thu, 19 May 2016 13:43:13 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1b3OEK-0004Xi-Qs for pgsql-bugs@postgresql.org; Thu, 19 May 2016 13:43:12 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1b3OEI-0002bS-5O for pgsql-bugs@postgresql.org; Thu, 19 May 2016 13:43:11 +0000 Received: from pro.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id u4JDh8uR002316; Thu, 19 May 2016 09:43:09 -0400 From: Tom Lane To: turon.david@seznam.cz cc: pgsql-bugs@postgresql.org Subject: Re: BUG #14149: when use LATERAL functions with IMMUTABLE called multiple times In-reply-to: <20160519090609.12503.12814@wrigleys.postgresql.org> References: <20160519090609.12503.12814@wrigleys.postgresql.org> Comments: In-reply-to turon.david@seznam.cz message dated "Thu, 19 May 2016 09:06:09 -0000" Date: Thu, 19 May 2016 09:43:08 -0400 Message-ID: <15800.1463665388@sss.pgh.pa.us> X-Pg-Spam-Score: -3.3 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org 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