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

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: You might be able to move the set-returning function into a LATERAL FROM item.
Date: 2018-03-20 14:27:57
Message-ID: 11515.1521556077@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Alexander Farber <alexander(dot)farber(at)gmail(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jimmy Augustine 2018-03-20 14:28:30 Re: PostgreSQL 9.6 Temporary files
Previous Message Adrian Klaver 2018-03-20 14:04:11 Re: Foreign Key locking / deadlock issue.