From: Tom Lane <tgl@sss.pgh.pa.us>
To: Torsten Förtsch <tfoertsch123@gmail.com>
Cc: Adrian Klaver <adrian.klaver@aklaver.com>
Cc: PostgreSQL mailing lists <pgsql-general@postgresql.org>
Subject: Re: functions with side effect
Date: Thu, 19 Jul 2018 14:10:21 -0400
Message-ID: <21876.1532023821@sss.pgh.pa.us> (raw)
In-Reply-To: <CAKkG4_njDJ+DirVT1of3375zFyLdHvKNvCUzy09pNiTL8crJrw@mail.gmail.com>
References: <CAKkG4_=MTjK6bAJDLM8brKR+U6JggZNhEta6nXgANUyWo8VcDA@mail.gmail.com>
<ebb85d10-94a2-505a-8e8e-d4bf2f59e5f8@aklaver.com>
<CAKkG4_njDJ+DirVT1of3375zFyLdHvKNvCUzy09pNiTL8crJrw@mail.gmail.com>
=?UTF-8?Q?Torsten_F=C3=B6rtsch?= <tfoertsch123@gmail.com> 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
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: tgl@sss.pgh.pa.us, tfoertsch123@gmail.com, adrian.klaver@aklaver.com
Subject: Re: functions with side effect
In-Reply-To: <21876.1532023821@sss.pgh.pa.us>
References: <CAKkG4_=MTjK6bAJDLM8brKR+U6JggZNhEta6nXgANUyWo8VcDA@mail.gmail.com> <ebb85d10-94a2-505a-8e8e-d4bf2f59e5f8@aklaver.com> <CAKkG4_njDJ+DirVT1of3375zFyLdHvKNvCUzy09pNiTL8crJrw@mail.gmail.com> <21876.1532023821@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