PostgreSQL JSONB Array vs JSONB – Which to Use?

arrayjsonpostgresql

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?

Best Answer

Generally, if you have an array of one thing (statically typed), it's better to store them as 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.