Skip to content

PostgreSQL GIN Index Not Working Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about PostgreSQL GIN Index Not Working Fix. We cover key concepts, practical examples, and best practices.

A GIN index on a JSONB column is not being used for JSON queries because the query uses the -> operator instead of the @> containment operator.

The Wrong Way

CREATE INDEX idx_orders_attrs ON orders USING GIN(attributes);
EXPLAIN ANALYZE SELECT * FROM orders WHERE attributes->'color' = '"red"';

Output:

Seq Scan on orders ...
Filter: ((attributes -> 'color'::text) = '"red"'::jsonb)
Time: 3450.123 ms

The Right Way

CREATE INDEX idx_orders_attrs ON orders USING GIN(attributes jsonb_path_ops);
EXPLAIN ANALYZE SELECT * FROM orders WHERE attributes @> '{"color": "red"}';

Output:

Bitmap Heap Scan using idx_orders_attrs ...
Time: 12.456 ms

Step-by-Step Fix

1. Check that the GIN index uses the correct operator class (jsonb_path_ops for @>)

2. Rewrite the query to use @> containment operator instead of -> and =

3. Ensure work_mem is large enough for GIN index scans (at least 4MB)

4. Verify the index is used with EXPLAIN ANALYZE

5. Test with different operator class if the default jsonb_ops is too slow

Prevention Tips

  • Test all queries with the database's explain plan tool before deploying to production.
  • Monitor query performance trends using built-in monitoring tools.
  • Set up automated index usage analysis in CI/CD pipelines.
  • Review database configuration quarterly against workload patterns.
  • Keep database statistics up to date with regular maintenance operations.
  • Use DodaTech's monitoring tools to track query performance regressions.

Common Mistakes with index gin

  1. Mixing let bindings with <- bindings in do notation, producing type errors
  2. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  3. Non-exhaustive pattern matches that compile with warnings then crash at runtime

These mistakes appear frequently in real-world POSTGRESQL code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between jsonb_ops and jsonb_path_ops?

jsonb_ops (default) indexes every key and value in the JSONB document, supporting operators like ? (exists), ?| (any), and ?& (all). jsonb_path_ops indexes only hashed paths, making @> (containment) queries 3-5x faster but dropping support for existence operators. For most workloads jsonb_path_ops is preferred.

How do I verify this fix is working?

Use psql to connect to the database and run an explain plan query. Check that the output shows an index scan (IXSCAN, Index Scan, or similar) instead of a full scan (Seq Scan, COLLSCAN, or ALL). Monitor query latency to confirm improvement.

Can this fix impact other queries?

Index and configuration changes can affect other query patterns. Always test in a staging environment first. Review the explain plans of your top 5-10 queries after making changes to ensure no regressions.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro