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 1fgCZm-000515-6U for pgsql-general@arkaria.postgresql.org; Thu, 19 Jul 2018 17:18:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fgCZk-0006EB-L3 for pgsql-general@arkaria.postgresql.org; Thu, 19 Jul 2018 17:18:48 +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 1fgCZk-0006E4-81 for pgsql-general@lists.postgresql.org; Thu, 19 Jul 2018 17:18:48 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fgCZg-00041i-Ph for pgsql-general@lists.postgresql.org; Thu, 19 Jul 2018 17:18:47 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id C8F9A21C27F6 for ; Thu, 19 Jul 2018 20:18:43 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [192.168.27.176] (gw.postgrespro.ru [93.174.131.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 9DBAE21C27F4 for ; Thu, 19 Jul 2018 20:18:43 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1532020723; bh=qtJ2ji0olRD6NopqUCC3OTwPlgfKi6xt9/sKYsBFnEo=; h=Subject:To:References:From:Date:In-Reply-To; b=pc0K8pwyONmmsf+Ck9S8hrKQAahIgzPALfcLiBgdcuOsGlkhMBI3irl1KR8GU/00i koYfu7+4bNYzT4ckPqLkM2wSRDN5ncqLd5VVv8DuqA2tnLI8ZNF7TqwSHXNxlUcGTw vN7OGLO4Nc0zB7L7g+dsPL6qB74DnFRatddPwSqg= Subject: Re: functions with side effect To: pgsql-general@lists.postgresql.org References: From: Pavel Luzanov Message-ID: Date: Thu, 19 Jul 2018 20:18:43 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------9EEDE9DBE2A2C8F244140290" Content-Language: ru-RU List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------9EEDE9DBE2A2C8F244140290 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Very interesting question. postgres=# create sequence s; CREATE SEQUENCE postgres=# select currval('s'), nextval('s'); ERROR:  currval of sequence "s" is not yet defined in this session postgres=# select nextval('s'), currval('s');  nextval | currval ---------+---------        1 |       1 We see different result with different order of functions. So the question is: in which order expressions evaluated. And I don't think that we can rely on this order. Moreover, according to SQL standard[1]: "If there are multiple instances of s specifying the same sequence generator within a single SQL-statement, all those instances return the same value for a given row processed by that SQL-statement." But in fact nextval return new value each time: postgres=# select nextval('s'), nextval('s') from generate_series (1,3);  nextval | nextval ---------+---------        2 |       3        4 |       5        6 |       7 [1] http://www.wiscorp.com/sql_2003_standard.zip     5WD-02-Foundation-2003-09.pdf         4.21.2 Operations involving sequence generators ----- Pavel Luzanov Postgres Professional: http://www.postgrespro.com The Russian Postgres Company On 19.07.2018 19:43, Torsten Förtsch wrote: > On Thu, Jul 19, 2018 at 6:35 PM Adrian Klaver > > wrote: > > On 07/19/2018 07:15 AM, Torsten Förtsch wrote: > > Hi, > > > > assuming > > > > SELECT nextval('s'), currval('s'); > > > > or > > > > SELECT * FROM (VALUES (nextval('s'), currval('s'))) t; > > > > is there any guarantee that the 2 output values are the same? > > Assuming you are only working in single session: > > https://www.postgresql.org/docs/10/static/functions-sequence.html > > "currval > >      Return the value most recently obtained by nextval for this > sequence in the current session. (An error is reported if nextval has > never been called for this sequence in this session.) Because this is > returning a session-local value, it gives a predictable answer > whether > or not other sessions have executed nextval since the current > session did." > > > 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. > --------------9EEDE9DBE2A2C8F244140290 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit Very interesting question.

postgres=# create sequence s;
CREATE SEQUENCE
postgres=# select currval('s'), nextval('s');
ERROR:  currval of sequence "s" is not yet defined in this session
postgres=# select nextval('s'), currval('s');
 nextval | currval
---------+---------
       1 |       1

We see different result with different order of functions.
So the question is: in which order expressions evaluated.
And I don't think that we can rely on this order.

Moreover, according to SQL standard[1]:
"If there are multiple instances of <next value expression>s specifying the same sequence generator within a
single SQL-statement, all those instances return the same value for a given row processed by that SQL-statement."

But in fact nextval return new value each time:

postgres=# select nextval('s'), nextval('s') from generate_series (1,3);
 nextval | nextval
---------+---------
       2 |       3
       4 |       5
       6 |       7

[1] http://www.wiscorp.com/sql_2003_standard.zip
    5WD-02-Foundation-2003-09.pdf
        4.21.2 Operations involving sequence generators
-----
Pavel Luzanov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company
On 19.07.2018 19:43, Torsten Förtsch wrote:
On Thu, Jul 19, 2018 at 6:35 PM Adrian Klaver <adrian.klaver@aklaver.com> wrote:
On 07/19/2018 07:15 AM, Torsten Förtsch wrote:
> Hi,
>
> assuming
>
> SELECT nextval('s'), currval('s');
>
> or
>
> SELECT * FROM (VALUES (nextval('s'), currval('s'))) t;
>
> is there any guarantee that the 2 output values are the same?

Assuming you are only working in single session:

https://www.postgresql.org/docs/10/static/functions-sequence.html

"currval

     Return the value most recently obtained by nextval for this
sequence in the current session. (An error is reported if nextval has
never been called for this sequence in this session.) Because this is
returning a session-local value, it gives a predictable answer whether
or not other sessions have executed nextval since the current session did."

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.


--------------9EEDE9DBE2A2C8F244140290--