Skip to content
GreenSnippets

Query JSONB and index it with GIN

Filter on JSONB fields with the containment operator and make it fast with a GIN index.

SQL
CREATE INDEX events_data_gin ON events USING gin (data jsonb_path_ops);

-- Rows whose JSON contains {"type": "signup", "plan": "pro"}
SELECT id, data->>'email' AS email, created_at
FROM events
WHERE data @> '{"type": "signup", "plan": "pro"}'
ORDER BY created_at DESC
LIMIT 50;

Example / expected output

Output
-- jsonb_path_ops indexes are smaller and faster for @> but do not support the ? operators.

How it works

@> tests whether the left JSONB contains the right one and is the operator a default GIN index accelerates. ->> extracts a field as text for display or ordering. For a single hot key, an expression index on (data->>'status') can be smaller and faster.

This snippet has been reviewed but was not executed by an automated test. Test it in your own environment before relying on it.