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 1pNbWM-0000Z8-Dj for pgsql-general@arkaria.postgresql.org; Thu, 02 Feb 2023 15:29:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pNbWK-0000a1-DB for pgsql-general@arkaria.postgresql.org; Thu, 02 Feb 2023 15:29:04 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pNbWK-0000ZW-2M for pgsql-general@lists.postgresql.org; Thu, 02 Feb 2023 15:29:04 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pNbWH-0000B3-Ke for pgsql-general@lists.postgresql.org; Thu, 02 Feb 2023 15:29:02 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 312FT06h1537294; Thu, 2 Feb 2023 10:29:00 -0500 From: Tom Lane To: Zahir Lalani cc: "pgsql-general@lists.postgresql.org" Subject: Re: From Clause Conditional In-reply-to: References: Comments: In-reply-to Zahir Lalani message dated "Thu, 02 Feb 2023 12:54:20 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1537292.1675351740.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 02 Feb 2023 10:29:00 -0500 Message-ID: <1537293.1675351740@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Zahir Lalani writes: > LEFT JOIN lateral ( > SELECT > CASE > WHEN (0 > 0) THEN > convert_from(crypto_secretbox_open, 'utf8')::JSON > ELSE > NULL > END AS edata > FROM > crypto_secretbox_open(coalesce(null, '')= ::bytea, coalesce(null, '')::bytea,0) > where (0>0) > ) enc ON true > The issue is that, even when the key is 0, the select is still run (its = part of a lateral join) and what we need to achieve is to effectively have= a conditional where we only run the select if the key > 0 otherwise we re= turn null - I have a brain freeze on this! I am sure there is an easy solu= tion, but right now I can't see it. Is crypto_secretbox_open marked IMMUTABLE? I think recent PG versions are capable of folding this all to a constant if so, whereas older ones might not have. If you care about when/whether crypto_secretbox_open runs, then it must have side-effects, so it shouldn't be IMMUTABLE. regards, tom lane