← Back to PostgreSQL, page 2

postgresql-0036 · Medium mid

Which GIN operator class should index a JSONB column queried with @>, and how do you confirm the index is used?

The default jsonb_ops GIN class supports @>, ?, ?| and ?& (containment and key existence). jsonb_path_ops supports only @> and path operators but is smaller and usually faster for containment. Confirm with EXPLAIN that the plan shows a Bitmap Index Scan on the index.

Full answer

Setup for the example:

sql
CREATE TABLE events (id int, payload jsonb);
INSERT INTO events SELECT g, jsonb_build_object('type', CASE WHEN g % 10 = 0 THEN 'purchase' ELSE 'view' END, 'user', g % 100) FROM generate_series(1, 5000) g;
CREATE INDEX events_payload_path ON events USING gin (payload jsonb_path_ops);
ANALYZE events;
sql
EXPLAIN (COSTS OFF) SELECT count(*) FROM events WHERE payload @> '{"type": "purchase"}';

Result:

text
Aggregate
  ->  Bitmap Heap Scan on events
        Recheck Cond: (payload @> '{"type": "purchase"}'::jsonb)
        ->  Bitmap Index Scan on events_payload_path
              Index Cond: (payload @> '{"type": "purchase"}'::jsonb)
sql
EXPLAIN (COSTS OFF) SELECT count(*) FROM events WHERE payload ? 'user';

Output:

text
Aggregate
  ->  Seq Scan on events
        Filter: (payload ? 'user'::text)
sql
SELECT count(*) AS purchases FROM events WHERE payload @> '{"type": "purchase", "user": 50}';

PostgreSQL returns:

text
 purchases
-----------
 50
(1 row)

The containment query uses the jsonb_path_ops index through a bitmap scan. The key-existence query with ? cannot use that operator class, so it falls back to a sequential scan; it would need a default jsonb_ops index. Containment with several keys narrows the match; 50 rows here. A filter on one scalar field, such as payload->>'type' = 'purchase', is not helped by either GIN class; an expression B-tree index on (payload->>'type') serves that form.

sql
CREATE TABLE events (id int, payload jsonb);
INSERT INTO events SELECT g, jsonb_build_object('type', CASE WHEN g % 10 = 0 THEN 'purchase' ELSE 'view' END, 'user', g % 100) FROM generate_series(1, 5000) g;
CREATE INDEX events_payload_path ON events USING gin (payload jsonb_path_ops);
ANALYZE events;

EXPLAIN (COSTS OFF) SELECT count(*) FROM events WHERE payload @> '{"type": "purchase"}';

EXPLAIN (COSTS OFF) SELECT count(*) FROM events WHERE payload ? 'user';

SELECT count(*) AS purchases FROM events WHERE payload @> '{"type": "purchase", "user": 50}';

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • jsonb_ops supports @> and key existence
  • jsonb_path_ops supports @> and paths only
  • path_ops is smaller and faster
  • Check plans with EXPLAIN
  • Use expression B-tree indexes for ->> equality

Common mistakes

  • Indexing with path_ops and querying with ?
  • Expecting GIN to help ->> comparisons

Follow-ups

  • How big do GIN indexes get compared with the table?
  • What does fastupdate do?

Tips

  • Show the fallback plan