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

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Joel Jacobson" <joel(at)compiler(dot)org>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: CASE WHEN idiomatic for functions with side-effect?
Date: 2021-01-12 14:48:27
Message-ID: 1126920.1610462907@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Joel Jacobson" <joel(at)compiler(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Deepti Sharma S 2021-01-12 14:49:42 RE: Compatible compiler with Postgresql C++
Previous Message David G. Johnston 2021-01-12 14:45:12 Re: CASE WHEN idiomatic for functions with side-effect?