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:
|