Skip to content

.tip

· one of the small things

Use jsonb_path_ops for containment queries

A default GIN index on a jsonb column supports every operator, and it's big. If all you ever run is @> containment, the jsonb_path_ops class is a third of the size and faster.

CREATE INDEX events_payload_idx ON events USING gin (payload jsonb_path_ops);
SELECT * FROM events WHERE payload @> '{"type": "refund"}';

It doesn't support key-existence queries with ?, so choose based on the queries you actually run.

postgresqlperformance