Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kzEoi-0006vS-Gu for pgsql-general@arkaria.postgresql.org; Tue, 12 Jan 2021 08:14:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kzEoh-0007cv-6Y for pgsql-general@arkaria.postgresql.org; Tue, 12 Jan 2021 08:14:15 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kzEog-0007co-6F for pgsql-general@lists.postgresql.org; Tue, 12 Jan 2021 08:14:14 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kzEod-0001FG-1I for pgsql-general@postgresql.org; Tue, 12 Jan 2021 08:14:13 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id B37E25C01E7 for ; Tue, 12 Jan 2021 03:14:09 -0500 (EST) Received: from imap2 ([10.202.2.52]) by compute5.internal (MEProxy); Tue, 12 Jan 2021 03:14:09 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=compiler.org; h= mime-version:message-id:date:from:to:subject:content-type; s= fm3; bh=FolmZ/47EB9AimEEemTmlMCdY6GV2C7Q1M66k0elVKg=; b=lNfjiU55 cASLZwhQHHBwYUUsjEVUGpoA4OGPtru5U0MGff12h/jPHhOBYl94xexoX3vwiXmm Ow10yUJpTBJDQAVkFwQ8FNrUNtGeP9Xay5uQCdbK2FkG1Jhtl2QMf0CPGdMZDma2 Ch0GlSTcnmvh0lJtcpZj08fsDusBpkIo01j2Sk/nofKQ5PFdUYtaDHClDY5CNcFg wM8qL6/gdk22G/VmsQ/nQF7evf6jbO3dY9yamUvcCsi1Q/Gr6y4gROSd2ajTslEf nPkrgChpchUj00ZVehoc3BgveqzYvJbk+IhIgmhNpMuxcSGxCM8h9MXEO30KOml6 1zMEg/7PLIHm5Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:message-id :mime-version:subject:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; bh=FolmZ/47EB9AimEEemTmlMCdY6GV2 C7Q1M66k0elVKg=; b=TQ9k7A1DR2sinR0cNlZZ1dp705/ZXjXNUCs0HZTgI31/U XMO4FMrFmCApdCm++eRdVcHkiqM20d9TkrkgBKcOH55uMWZeabxoUdFtTeOWkLUn 7uz0ErC9Ye9S5JkKq4StOvqadeAzLbFHpUPxz+PRtWW+8c+LvdhSzbiXguvVne79 pA5qndVkTqDlR1/sryCU4xmgsKHN8bMIvbwTpk0JMLR//VqESOemuuJ+0EAFgxXB mKedeSQRko3XKsexK3tK6eTMPW0L/NrvzvPCdkU9CIZZnkh0CRhgTpGiI9RO4itY 1QTIgzKcQDjR+iV2Ic6poZewPFZzrKv66cxGSCWpg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrvdehvddguddukecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepofgfggfkfffhvffutgesrgdtre erreertdenucfhrhhomhepfdflohgvlhculfgrtghosghsohhnfdcuoehjohgvlhestgho mhhpihhlvghrrdhorhhgqeenucggtffrrghtthgvrhhnpeehieekudevffevteefgfekfe dugedtteejkedvgfehuefgleduleevudeiffefffenucffohhmrghinhepphhoshhtghhr vghsqhhlrdhorhhgpdhgihhthhhusgdrtghomhenucevlhhushhtvghrufhiiigvpedtne curfgrrhgrmhepmhgrihhlfhhrohhmpehjohgvlhestghomhhpihhlvghrrdhorhhg X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id C2818E00C7; Tue, 12 Jan 2021 03:14:08 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-45-g4839256-fm-20210104.001-g48392560 Mime-Version: 1.0 Message-Id: <6aab85ba-a97c-4394-ab80-6077bb14be2b@www.fastmail.com> Date: Tue, 12 Jan 2021 12:13:47 +0400 From: "Joel Jacobson" To: pgsql-general@postgresql.org Subject: CASE WHEN idiomatic for functions with side-effect? Content-Type: multipart/alternative; boundary=cf3f6b47f7824c6c83c46d5bb65d55e8 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --cf3f6b47f7824c6c83c46d5bb65d55e8 Content-Type: text/plain 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 --cf3f6b47f7824c6c83c46d5bb65d55e8 Content-Type: text/html Content-Transfer-Encoding: quoted-printable
Is it idiomatic= and safe to use

SELECT
 = ; CASE boolean_expression WHEN TRUE THEN function_with_side_effects() EN= D
...

in a query to ensure th= e function_with_side_effects() is only
execute if boo= lean_expression is true?

function_with_side= _effects() is known to be a normal function,
and not an ag= gregate function.

The documentation at
<= /div>

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

<= div>says the following:

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

On the other hand, it also say= s:

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

Can I trust PostgreSQL not= to execute function_with_side_effects() unless boolean_expres= sion IS TRUE?

Or should I rewrite my code t= o use a PL/pgSQL function with IF-THEN-ELSE here instead?
=
The affected real code: https://github.com/truthly/u= niphant/blob/rls/FUNCTIONS/api/verify_assertion.sql

Many thanks for clarifying.

/Joel<= br>
--cf3f6b47f7824c6c83c46d5bb65d55e8--