Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fgDNu-0007Yk-5q for pgsql-general@arkaria.postgresql.org; Thu, 19 Jul 2018 18:10:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fgDNs-0006i7-EK for pgsql-general@arkaria.postgresql.org; Thu, 19 Jul 2018 18:10:36 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fgDNs-0006i0-6X for pgsql-general@lists.postgresql.org; Thu, 19 Jul 2018 18:10:36 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fgDNl-0005CX-Qk for pgsql-general@postgresql.org; Thu, 19 Jul 2018 18:10:32 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w6JIALAZ021877; Thu, 19 Jul 2018 14:10:21 -0400 From: Tom Lane To: =?UTF-8?Q?Torsten_F=C3=B6rtsch?= cc: Adrian Klaver , PostgreSQL mailing lists Subject: Re: functions with side effect In-reply-to: References: Comments: In-reply-to =?UTF-8?Q?Torsten_F=C3=B6rtsch?= message dated "Thu, 19 Jul 2018 18:43:48 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <21875.1532023821.1@sss.pgh.pa.us> Date: Thu, 19 Jul 2018 14:10:21 -0400 Message-ID: <21876.1532023821@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk =?UTF-8?Q?Torsten_F=C3=B6rtsch?= writes: > I know that. My question was about the execution order of f1 and f2 in > "SELECT f1(), f2()". In theory they can be executed in any order. But since > the side effect in nextval determines the result of currval, I am asking if > that order is well-defined or considered an implementation detail like in C. The current implementation evaluates select-list items left to right. I doubt we'd be eager to change that, since there are surely many applications that depend on that behavior, whether it's formally specified or not. But elsewhere in a query than the select target list, there are no guarantees, and there's lots of precedent for whacking around the evaluation order in e.g. WHERE. I'd be a little more wary with examples like your other one: SELECT * FROM (VALUES (nextval('s'), currval('s'))) t; since there's an additional unspecified question there, which is whether the planner will "flatten" the sub-select. To put it more clearly, you'd be taking big risks with SELECT y, x FROM (VALUES (nextval('s'), currval('s'))) t(x, y); Right now it seems the nextval is done first, but I would not want to bet on that staying true in the future. [ experiments some more ... ] Actually, looks like we have a rule against flattening sub-selects whose targetlists contain volatile functions, so maybe you'd get away with that for the indefinite future too. regards, tom lane