pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Erik Wienhold <ewie@ewie.name>
To: Zahir Lalani <ZahirLalani@oliver.agency>
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: From Clause Conditional
Date: Thu, 2 Feb 2023 16:28:09 +0100 (CET)
Message-ID: <956888536.169901.1675351689652@office.mailbox.org> (raw)
In-Reply-To: <DB9P251MB05468E6775913F3BE44BC2B3A7D69@DB9P251MB0546.EURP251.PROD.OUTLOOK.COM>
References: <CALfOM6Jk9_OkSqjyXxLNfbu15JC0=W38ba6BcoFAtyVhgBmY4Q@mail.gmail.com>
	<CABUevEwgUedHv1iEXMJnALw0BgJrDasJgUvi5JGpbwRyReRcPA@mail.gmail.com>
	<CALfOM6JaRquBA7ZZtFg-iS4gOh=ju0jDVKSPhpcSXZNe5Zs7Rw@mail.gmail.com>
	<CABUevEzgKkSUF4Tg3ZnbL=T0BmiM6VpfSuOrjUXJENyTJgUDNA@mail.gmail.com>
	<CALfOM6Jf7vXFP0dc9G26K0q4tVSPq-boB6E5NhrWnVc=ogOqfQ@mail.gmail.com>
	<DB9P251MB05468E6775913F3BE44BC2B3A7D69@DB9P251MB0546.EURP251.PROD.OUTLOOK.COM>

> On 02/02/2023 13:54 CET Zahir Lalani <zahirlalani@oliver.agency> wrote:
>
> Confidential
>
> Hello All
>
> We are testing a upgrade from pg11 to pg14 and have some issues to overcome.
> One of these is that we have upgraded pgsodium to the latest and there is a
> functional change – this question is not about sodium BTW.
>
> So here is a sample bit of code that I will use to explain the issue – this
> would usually be params passed in, but I have hard coded one particular case
> that does not need the decryption. The code below would return decrypted data
> if the key is supplied (non 0) otherwise return null. This code used to work
> because the secret box call would allow null params. It no longer does. When
> the key is 0, the data passed in would be null.
>
> 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 return
> null – I have a brain freeze on this! I am sure there is an easy solution,
> but right now I can’t see it.

Looks similar to a post from a few days ago:
https://www.postgresql.org/message-id/flat/CAALojA-nHoxDr7B2k0e1-EtGMPsGPZiCVeS_ds0aHG0SEOrPxg%40mai...

I don't know pgsodium but the query optimizer will evalute crypto_secretbox_open
if the function is immutable and is called with constant arguments.

How is the key and the data passed to this query?  Is it a prepared statement or
is the query generated on the fly for specific key and data that is already
known?  In the latter case you can handle the case expression with two different
queries (one with crypto_secret_box and one without) depending on whether a key
exists or not.

--
Erik

PS: Please don't hijack threads on this mailing list.





view thread (3+ messages)  latest in thread

Message-ID: <956888536.169901.1675351689652@office.mailbox.org>
Permalink:  ../956888536.169901.1675351689652@office.mailbox.org/
Also on:    postgresql.org/message-id/956888536.169901.1675351689652@office.mailbox.org

 · 

reply

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: ewie@ewie.name, ZahirLalani@oliver.agency
  Subject: Re: From Clause Conditional
  In-Reply-To: <956888536.169901.1675351689652@office.mailbox.org>
  References: <CALfOM6Jk9_OkSqjyXxLNfbu15JC0=W38ba6BcoFAtyVhgBmY4Q@mail.gmail.com> <CABUevEwgUedHv1iEXMJnALw0BgJrDasJgUvi5JGpbwRyReRcPA@mail.gmail.com> <CALfOM6JaRquBA7ZZtFg-iS4gOh=ju0jDVKSPhpcSXZNe5Zs7Rw@mail.gmail.com> <CABUevEzgKkSUF4Tg3ZnbL=T0BmiM6VpfSuOrjUXJENyTJgUDNA@mail.gmail.com> <CALfOM6Jf7vXFP0dc9G26K0q4tVSPq-boB6E5NhrWnVc=ogOqfQ@mail.gmail.com> <DB9P251MB05468E6775913F3BE44BC2B3A7D69@DB9P251MB0546.EURP251.PROD.OUTLOOK.COM> <956888536.169901.1675351689652@office.mailbox.org>

* 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