Re: functions with side effect

Поиск
Список
Период
Сортировка
От Adrian Klaver
Тема Re: functions with side effect
Дата
Msg-id 18412673-0643-82f9-1288-fdd1370e7b8e@aklaver.com
обсуждение исходный текст
Ответ на Re: functions with side effect  (Torsten Förtsch <tfoertsch123@gmail.com>)
Список pgsql-general
On 07/19/2018 09:43 AM, Torsten Förtsch wrote:
> On Thu, Jul 19, 2018 at 6:35 PM Adrian Klaver <adrian.klaver@aklaver.com 
> <mailto: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.
> 

Can't answer definitively, but:

create sequence order_test;

DO
$$
DECLARE
     rs record;
BEGIN
     FOR i IN 1..1000 LOOP
         SELECT INTO rs nextval('order_test'), currval('order_test');
         RAISE NOTICE 'Currval is %', rs.currval;
     END LOOP;
END$$;

has not failed over multiple runs.

-- 
Adrian Klaver
adrian.klaver@aklaver.com


В списке pgsql-general по дате отправления:

Предыдущее
От: "Peter J. Holzer"
Дата:
Сообщение: Re: User documentation vs Official Docs
Следующее
От: Pavel Luzanov
Дата:
Сообщение: Re: functions with side effect