Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ezYgW-0007QG-CU for pgsql-performance@arkaria.postgresql.org; Sat, 24 Mar 2018 02:13:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ezYgU-0004ej-O5 for pgsql-performance@arkaria.postgresql.org; Sat, 24 Mar 2018 02:13:30 +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.89) (envelope-from ) id 1ezYgU-0004eU-DY for pgsql-performance@lists.postgresql.org; Sat, 24 Mar 2018 02:13:30 +0000 Received: from uucp.dinoex.sub.de ([2001:1440:5001:1::2]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ezYgR-0004gW-2y for pgsql-performance@lists.postgresql.org; Sat, 24 Mar 2018 02:13:29 +0000 Received: from uucp.dinoex.sub.de (uucp.dinoex.sub.de [194.45.71.2]) by uucp.dinoex.sub.de (8.15.2/8.15.2) with ESMTPS id w2O2D49l054134 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO) for ; Sat, 24 Mar 2018 03:13:05 +0100 (CET) (envelope-from pmc@citylink.dinoex.sub.org) X-MDaemon-Deliver-To: Received: from citylink.dinoex.sub.org (uucp@localhost) by uucp.dinoex.sub.de (8.15.2/8.15.2/Submit) with UUCP id w2O2D4Pr054133 for pgsql-performance@lists.postgresql.org; Sat, 24 Mar 2018 03:13:04 +0100 (CET) (envelope-from pmc@citylink.dinoex.sub.org) Received: from gate.oper.dinoex.org (gate-e [192.168.98.2]) by citylink.dinoex.sub.de (8.15.2/8.15.2) with ESMTP id w2O1ULFS019037 for ; Sat, 24 Mar 2018 02:30:21 +0100 (CET) (envelope-from peter@gate.oper.dinoex.org) Received: from gate.oper.dinoex.org (gate-e [192.168.98.2]) by gate.oper.dinoex.org (8.15.2/8.15.2) with ESMTP id w2O1RlLE018537 for ; Sat, 24 Mar 2018 02:27:47 +0100 (CET) (envelope-from peter@gate.oper.dinoex.org) Received: (from peter@localhost) by gate.oper.dinoex.org (8.15.2/8.15.2/Submit) id w2O1RlwR018536 for pgsql-performance@lists.postgresql.org; Sat, 24 Mar 2018 02:27:47 +0100 (CET) (envelope-from peter) Date: Sat, 24 Mar 2018 02:27:47 +0100 From: Peter To: pgsql-performance@lists.postgresql.org Subject: functions: VOLATILE performs better than STABLE Message-ID: <20180324012746.GA14631@gate.oper.dinoex.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.8.3 (2017-05-23) X-Milter: Spamilter (Reciever: uucp.dinoex.sub.de; Sender-ip: 194.45.71.2; Sender-helo: uucp.dinoex.sub.de;) X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (uucp.dinoex.sub.de [194.45.71.2]); Sat, 24 Mar 2018 03:13:06 +0100 (CET) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Given an arbitrary function fn(x) returning numeric. Question: how often is the function executed? A. select fn('const'), fn('const'); Answer: Twice. This is not a surprize. B. select v,v from fn('const') as v; [1] Answer: Once. C. select v.v,v.v from (select fn('const') as v) as v; Answer: Once if declared VOLATILE. Twice if declared STABLE. Now this IS a surprize. It is clear that the system is not allowed to execute the function twice when declared VOLATILE. It IS ALLOWED to execute it twice when STABLE - but to what point, except prolonging execution time? Over all, VOLATILE performs better than STABLE. [1] I seem to remember that I was not allowed to do this when I coded my SQL, because expressions in the from clause must return SETOF, not a single value. Now it seems to work.