Postgresql index for jsonb array field
The index is only used when Postgres expects it to help performance - which is only the case if the filter is expected to be selective enough (~ 5 % or less of the rows qualify, percentage heavily depends on various details).

One problem with document types like jsonb: Postgres currently does not maintain statistics about value frequencies of embedded elements (still true in Postgres 10). So it has to base its decision whether or not to use an index on generic frequency estimations. Meaning, even if your particular filter data -&gt; 'school_id' -&gt;&gt; 1 IS NOT NULL is very selective and using the index would pay, Postgres works with a generic average estimation and might miss the opportunity.

There are ways around this with expression or partial indexes, because Postgres collects separate statistics for index expressions. The best you could do for this particular query would be a partial index:

CREATE INDEX data_school_id ON events (event_id)  -- idx column largely irrelevant here
WHERE (data -&gt; 'school_id' -&gt;&gt; 1) IS NOT NULL;


And VACUUM ANALYZE events; at least once (or wait until autovacuum kicks in).

Related:


Index that is not used, yet influences query
Inconsistent statistics on jsonb column with btree index