Create array of data from JSONB in PG 9.5

Started by Arup Rakshitover 6 years ago3 messagesgeneral
Jump to latest
#1Arup Rakshit
ar@zeit.io

Hi,

I wrote the below SQL which works and give me what I want, but it doesn’t work in 9.5 version. I wrote this query in PG 10. I want to change it so that I can run it on 9.5 as production using 9.5 version. I need help on this.

SELECT DISTINCT
ARRAY [jsonb_array_elements(data) ->> 'group', jsonb_array_elements(jsonb_array_elements(data) -> 'properties') ->> 'name']
FROM
"vessels"
WHERE
"vessels"."deleted_at" IS NULL
AND "vessels"."company_id" = '7d105acd-be5a-4225-a2db-b549105e4172';

When I ran the same query to production I get error:

ERROR: set-valued function called in context that cannot accept a set

Thanks,

Arup Rakshit
ar@zeit.io

#2Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Arup Rakshit (#1)
Re: Create array of data from JSONB in PG 9.5

On 11/15/19 2:01 AM, Arup Rakshit wrote:

Hi,

I wrote the below SQL which works and give me what I want, but it doesn’t work in 9.5 version. I wrote this query in PG 10. I want to change it so that I can run it on 9.5 as production using 9.5 version. I need help on this.

SELECT DISTINCT
ARRAY [jsonb_array_elements(data) ->> 'group', jsonb_array_elements(jsonb_array_elements(data) -> 'properties') ->> 'name']
FROM
"vessels"
WHERE
"vessels"."deleted_at" IS NULL
AND "vessels"."company_id" = '7d105acd-be5a-4225-a2db-b549105e4172';

When I ran the same query to production I get error:

ERROR: set-valued function called in context that cannot accept a set

I would break this down into smaller units to see what is actually
triggering the above ERROR. In other words run the
jsonb_array_elements() independently and then start adding them together.

Thanks,

Arup Rakshit
ar@zeit.io

--
Adrian Klaver
adrian.klaver@aklaver.com

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Arup Rakshit (#1)
Re: Create array of data from JSONB in PG 9.5

Arup Rakshit <ar@zeit.io> writes:

I wrote the below SQL which works and give me what I want, but it doesn’t work in 9.5 version. I wrote this query in PG 10. I want to change it so that I can run it on 9.5 as production using 9.5 version. I need help on this.

SELECT DISTINCT
ARRAY [jsonb_array_elements(data) ->> 'group', jsonb_array_elements(jsonb_array_elements(data) -> 'properties') ->> 'name']
FROM
"vessels"
WHERE
"vessels"."deleted_at" IS NULL
AND "vessels"."company_id" = '7d105acd-be5a-4225-a2db-b549105e4172';

When I ran the same query to production I get error:
ERROR: set-valued function called in context that cannot accept a set

IIRC, you can't nest calls of set-returning functions before about v10.
One workaround is to put the inner set-returning function call into a
sub-select.

regards, tom lane