Re: You might be able to move the set-returning function into a LATERAL FROM item. - Mailing list pgsql-general

From Tom Lane
Subject Re: You might be able to move the set-returning function into a LATERAL FROM item.
Date
Msg-id 11515.1521556077@sss.pgh.pa.us
Whole thread Raw
In response to You might be able to move the set-returning function into a LATERALFROM item.  (Alexander Farber <alexander.farber@gmail.com>)
Responses Re: You might be able to move the set-returning function into aLATERAL FROM item.  (Alexander Farber <alexander.farber@gmail.com>)
List pgsql-general
Alexander Farber <alexander.farber@gmail.com> writes:
> I am trying to fetch a history/protocol of a game with:

>                 SELECT
>                     CASE WHEN JSONB_TYPEOF(tiles) = 'array' THEN
> JSONB_ARRAY_ELEMENTS(tiles) ELSE NULL END AS x

I think you could push the conditionality into a plpgsql function,
something like (untested)

create function jsonb_elements_if_array(j jsonb) returns setof jsonb as $$
begin
  if jsonb_typeof(j) = 'array' then
    return query select jsonb_array_elements(j);
  end if;
end$$
strict immutable language plpgsql;

Note that this gives *no* elements, rather than a single NULL value,
if the input isn't an array --- but that seems to me to make more sense
than your existing code anyhow.  If you disagree, add "else return next
null::jsonb".

            regards, tom lane


pgsql-general by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: Foreign Key locking / deadlock issue.
Next
From: Jimmy Augustine
Date:
Subject: Re: PostgreSQL 9.6 Temporary files