patternsqlMinor
Is there any sense in using Postgres array of JSONB values instead of only JSONB?
Viewed 0 times
postgresarrayanyinsteadsenseusingonlyvaluestherejsonb
Problem
Is there any reason to use array of JSONB values instead of just raw JSONB?
My use case is that I need to store custom forms definition in list of JSON objects. And my question is whether it would be better to store it as raw JSON value or split this into separate JSONB objects.
What are pros and cons of each solution?
My use case is that I need to store custom forms definition in list of JSON objects. And my question is whether it would be better to store it as raw JSON value or split this into separate JSONB objects.
What are pros and cons of each solution?
Solution
Generally, if you have an array of one thing (statically typed), it's better to store them as
While Array has a far more, but most of those features are implemented as operators.
Ultimately, in most use cases it won't matter much. You're better off normalizing your data. You really shouldn't be storing multiple json documents in one row. That sounds like a big "con" to begin with.
jsonb[] (a native SQL array). There are far more functions that operate over a SQL arrays, and indexes. It also tends to less overhead in storage. JSONb only has,jsonb_array_length(jsonb)
jsonb_array_elements(jsonb)
jsonb_array_elements_text(jsonb)While Array has a far more, but most of those features are implemented as operators.
Ultimately, in most use cases it won't matter much. You're better off normalizing your data. You really shouldn't be storing multiple json documents in one row. That sounds like a big "con" to begin with.
Code Snippets
jsonb_array_length(jsonb)
jsonb_array_elements(jsonb)
jsonb_array_elements_text(jsonb)Context
StackExchange Database Administrators Q#164276, answer score: 8
Revisions (0)
No revisions yet.