Search Postgresql Archives

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Jan 12, 2021 at 1:14 AM Joel Jacobson <joel@xxxxxxxxxxxx> 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 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.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]

  Powered by Linux