I need to store a list of ids in array per each row.
I thought to use JSONB, then store it as : {ids: [1, 2, 3, 4]}
.
Then, I found Array
type, that I can use to store ids directly into.
The question is, what is the recommended data type for my case?
PostgreSQL JSONB – Should JSONB Be Used for Integer Arrays?
postgresql
Related Question
- PostgreSQL JSONB – Why Does JSONB Use More Space for Storing Integers?
- PostgreSQL JSONB Array vs JSONB – Which to Use?
- PostgreSQL – Row as JSONB Array, Not Object
- PostgreSQL 9.5.x – Btree Partial Index on JSONB Array Corruption
- PostgreSQL Database Design – When to Use JSON or JSONB Array vs SQL Array
- PostgreSQL – Calculating Average Value of JSONB Array
Best Answer
Unless you have a very good reason to do otherwise, the recommended storage is always normalised.
Store the integers in a separate table of
(other_row_id, the_integer)
and join on them.If you must use arrays, use PostgreSQL's native arrays
ARRAY[1,2,3]
.Storing them as
jsonb
is a spectacularly inefficient and clumsy way to do it with essentially no advantages.