Re: CASE WHEN idiomatic for functions with side-effect?

2021-01-12 Thread Joel Jacobson
Many thanks Tom and David for clarifying the semantics. /Joel

Re: CASE WHEN idiomatic for functions with side-effect?

2021-01-12 Thread Tom Lane
"Joel Jacobson" writes: > Is it idiomatic and safe to use > SELECT > CASE boolean_expression WHEN TRUE THEN function_with_side_effects() END > in a query to ensure the function_with_side_effects() is only > execute if boolean_expression is true? As long as function_with_side_effects() is

Re: CASE WHEN idiomatic for functions with side-effect?

2021-01-12 Thread David G. Johnston
On Tue, Jan 12, 2021 at 1:14 AM Joel Jacobson wrote: > Is it idiomatic and safe to use > > SELECT > CASE boolean_expression WHEN TRUE THEN function_with_side_effects() END > As long as function_with_side_effects() is defined volatile it is forced to be executed at runtime, once per row. That

CASE WHEN idiomatic for functions with side-effect?

2021-01-12 Thread Joel Jacobson
Is it idiomatic and safe to use SELECT CASE boolean_expression WHEN TRUE THEN function_with_side_effects() END ... in a query to ensure the function_with_side_effects() is only execute if boolean_expression is true? function_with_side_effects() is known to be a normal function, and not an

Re: functions with side effect

2018-07-19 Thread Torsten Förtsch
On Thu, Jul 19, 2018 at 8:10 PM Tom Lane wrote: > =?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

Re: functions with side effect

2018-07-19 Thread Tom Lane
=?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

Re: functions with side effect

2018-07-19 Thread Adrian Klaver
On 07/19/2018 09:43 AM, 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 >

Re: functions with side effect

2018-07-19 Thread Pavel Luzanov
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

Re: functions with side effect

2018-07-19 Thread Adrian Klaver
On 07/19/2018 09:43 AM, 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 >

Re: functions with side effect

2018-07-19 Thread Torsten Förtsch
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

Re: functions with side effect

2018-07-19 Thread Adrian Klaver
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:

Re: functions with side effect

2018-07-19 Thread Laurenz Albe
Torsten Förtsch wrote: > 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? You can use at EXPLAIN (VERBOSE) output to see if it will work in this special case,

functions with side effect

2018-07-19 Thread Torsten Förtsch
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? Thanks, Torsten