pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
CASE WHEN idiomatic for functions with side-effect?
4+ messages / 3 participants
[nested] [flat]

* CASE WHEN idiomatic for functions with side-effect?
@ 2021-01-12 08:13 Joel Jacobson <joel@compiler.org>
  2021-01-12 14:45 ` Re: CASE WHEN idiomatic for functions with side-effect? David G. Johnston <david.g.johnston@gmail.com>
  2021-01-12 14:48 ` Re: CASE WHEN idiomatic for functions with side-effect? Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 4+ messages in thread

From: Joel Jacobson @ 2021-01-12 08:13 UTC (permalink / raw)
  To: pgsql-general

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 aggregate function.

The documentation at

   https://www.postgresql.org/docs/current/sql-expressions.html#SYNTAX-EXPRESS-EVAL

says the following:

"But this is safe:
SELECT ... WHERE CASE WHEN x > 0 THEN y/x > 1.5 ELSE false END;"

On the other hand, it also says:

"related cases that don't obviously involve constants can occur in queries executed within functions, since the values of function arguments and local variables can be inserted into queries as constants for planning purposes. Within PL/pgSQL functions, for example, using an IF-THEN-ELSE statement to protect a risky computation is much safer than just nesting it in a CASE expression."

Can I trust PostgreSQL not to execute function_with_side_effects() unless boolean_expression IS TRUE?

Or should I rewrite my code to use a PL/pgSQL function with IF-THEN-ELSE here instead?

The affected real code: https://github.com/truthly/uniphant/blob/rls/FUNCTIONS/api/verify_assertion.sql

Many thanks for clarifying.

/Joel

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: CASE WHEN idiomatic for functions with side-effect?
  2021-01-12 08:13 CASE WHEN idiomatic for functions with side-effect? Joel Jacobson <joel@compiler.org>
@ 2021-01-12 14:45 ` David G. Johnston <david.g.johnston@gmail.com>
  1 sibling, 0 replies; 4+ messages in thread

From: David G. Johnston @ 2021-01-12 14:45 UTC (permalink / raw)
  To: Joel Jacobson <joel@compiler.org>; +Cc: pgsql-general

On Tue, Jan 12, 2021 at 1:14 AM Joel Jacobson <joel@compiler.org> 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 situation is always protected
by the case expression.


> "related cases that don't obviously involve constants can occur in queries
> executed within functions, since the values of function arguments and local
> variables can be inserted into queries as constants for planning purposes.
> Within PL/pgSQL functions, for example, using an IF-THEN-ELSE statement to
> protect a risky computation is much safer than just nesting it in
> a CASE expression."
>
> The affected real code:
> https://github.com/truthly/uniphant/blob/rls/FUNCTIONS/api/verify_assertion.sql
>

The relevant function takes in a column argument - it is thus impossible
for the planner to evaluate the expression.  And, as above, the planner
respects the "volatile" attribute of functions.

David J.

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: CASE WHEN idiomatic for functions with side-effect?
  2021-01-12 08:13 CASE WHEN idiomatic for functions with side-effect? Joel Jacobson <joel@compiler.org>
@ 2021-01-12 14:48 ` Tom Lane <tgl@sss.pgh.pa.us>
  2021-01-12 15:16   ` Re: CASE WHEN idiomatic for functions with side-effect? Joel Jacobson <joel@compiler.org>
  1 sibling, 1 reply; 4+ messages in thread

From: Tom Lane @ 2021-01-12 14:48 UTC (permalink / raw)
  To: Joel Jacobson <joel@compiler.org>; +Cc: pgsql-general

"Joel Jacobson" <joel@compiler.org> 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 properly marked volatile,
it's safe (whether it's idiomatic is in the eye of the beholder).

The issue with the divide-by-zero example is that the division operator
is marked immutable, so if the planner sees that it has constant arguments
it will try to simplify-on-sight, even within a CASE expression.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: CASE WHEN idiomatic for functions with side-effect?
  2021-01-12 08:13 CASE WHEN idiomatic for functions with side-effect? Joel Jacobson <joel@compiler.org>
  2021-01-12 14:48 ` Re: CASE WHEN idiomatic for functions with side-effect? Tom Lane <tgl@sss.pgh.pa.us>
@ 2021-01-12 15:16   ` Joel Jacobson <joel@compiler.org>
  0 siblings, 0 replies; 4+ messages in thread

From: Joel Jacobson @ 2021-01-12 15:16 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-general

Many thanks Tom and David for clarifying the semantics.

/Joel

^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2021-01-12 15:16 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-01-12 08:13 CASE WHEN idiomatic for functions with side-effect? Joel Jacobson <joel@compiler.org>
2021-01-12 14:45 ` David G. Johnston <david.g.johnston@gmail.com>
2021-01-12 14:48 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-01-12 15:16   ` Joel Jacobson <joel@compiler.org>

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