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

From Alexander Farber
Subject Re: You might be able to move the set-returning function into aLATERAL FROM item.
Date
Msg-id CAADeyWjkUPBXoQ0GQBO60XK0sMrN=63aobK_4ktTJhVk6syQEA@mail.gmail.com
Whole thread Raw
In response to Re: You might be able to move the set-returning function into a LATERAL FROM item.  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
Thank you -

On Tue, Mar 20, 2018 at 3:27 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
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".

I think I will just introduce a separate column (until now I was trying to squeeze 2 different kinds of data - JSON array of objects and a string - into the one column)... I believe Adrian had suggested it before :-)

pgsql-general by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: FDW Foreign Table Access: strange LOG message
Next
From: Adrian Klaver
Date:
Subject: Re: FATAL: semctl(15073290, 4, SETVAL, 0) failed: Invalid argument