patternMinor
merging many jsonb objects using aggregate functions in PostgreSQL?
Viewed 0 times
postgresqlobjectsmergingusingmanyfunctionsjsonbaggregate
Problem
Is there a standard function in PostgreSQL (as of 12.x) to concatenate or merge many jsonb objects in a database column into a single jsonb object?
I know there is a the || operator since PostgreSQL 9.5 to merge two jsonb objects. But I need to merge many jsonb objects from a column. The linked documentation does not seem to have one unless I am missing something.
I know there is a the || operator since PostgreSQL 9.5 to merge two jsonb objects. But I need to merge many jsonb objects from a column. The linked documentation does not seem to have one unless I am missing something.
Solution
I had the same problem and this post solved it:
https://blog.faraday.io/how-to-aggregate-jsonb-in-postgres/
The idea is to create an aggregate:
See the linked article for more details.
https://blog.faraday.io/how-to-aggregate-jsonb-in-postgres/
The idea is to create an aggregate:
CREATE AGGREGATE jsonb_object_agg(jsonb) (
SFUNC = 'jsonb_concat',
STYPE = jsonb,
INITCOND = '{}'
);See the linked article for more details.
Code Snippets
CREATE AGGREGATE jsonb_object_agg(jsonb) (
SFUNC = 'jsonb_concat',
STYPE = jsonb,
INITCOND = '{}'
);Context
StackExchange Database Administrators Q#255127, answer score: 5
Revisions (0)
No revisions yet.