3

Is a Postgres Array column more easily indexed than a JSONB column with a JSON array in it?

https://www.postgresql.org/docs/current/arrays.html

https://www.compose.com/articles/faster-operations-with-the-jsonb-data-type-in-postgresql/

3
  • What do you mean by easy? Commented Feb 25, 2020 at 20:44
  • I guess both in terms of effort from the DB and also effort for developer Commented Feb 25, 2020 at 20:48
  • 1
    Unrelated to your question, but: Postgres 9.4 is no longer supported you should plan an upgrade as soon as possible. Commented Feb 25, 2020 at 21:45

1 Answer 1

3

Syntactically, the JSONB array may be easier to use as you don't have to wrap your query value in a dummy array constructor:

where jsonbcolumn ? 'abc';

vs

where textarraycolumn @> ARRAY['abc']

On the other hand, the planner is likely to make better decisions with the PostgreSQL array, as it collects statistics on its contents, but doesn't on JSONB.

Also, you should read the docs for the version of PostgreSQL you are using, which is hopefully greater than 9.4 and really really should be greater than 9.1.

Sign up to request clarification or add additional context in comments.

2 Comments

The second condition can also be written as where 'abc' = any(textarraycolumn)
@a_horse_with_no_name True but that formulation won't use an index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.