From: Tom Lane <tgl@sss.pgh.pa.us>
To: Joel Jacobson <joel@compiler.org>
Cc: pgsql-general@postgresql.org
Subject: Re: CASE WHEN idiomatic for functions with side-effect?
Date: Tue, 12 Jan 2021 09:48:27 -0500
Message-ID: <1126920.1610462907@sss.pgh.pa.us> (raw)
In-Reply-To: <6aab85ba-a97c-4394-ab80-6077bb14be2b@www.fastmail.com>
References: <6aab85ba-a97c-4394-ab80-6077bb14be2b@www.fastmail.com>
"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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, joel@compiler.org
Subject: Re: CASE WHEN idiomatic for functions with side-effect?
In-Reply-To: <1126920.1610462907@sss.pgh.pa.us>
References: <6aab85ba-a97c-4394-ab80-6077bb14be2b@www.fastmail.com> <1126920.1610462907@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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