GIN Indexes — Complete Guide
GIN Indexes — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of PostgreSQL Tutorial on Toolliyo Academy.
On this page
PostgreSQL Tutorial · Lesson 22 of 100
GIN Indexes
SQL → Advanced
SQL · 1 — Queries · ~6 min · PostgreSQL — Indexing & Performance
What is this?
GIN (Generalized Inverted Index) excels at containment queries on arrays, jsonb, and full-text vectors. It maps keys to row locations — ideal for @>, ?, @@ operators.
Why should you care?
Flipkart product search on PostgresVerse specs jsonb needs GIN — B-Tree cannot index “contains key warranty inside JSON”.
See it live — copy this example
Run in pgAdmin or psql.
CREATE INDEX idx_products_specs_gin
ON products USING GIN (specs jsonb_path_ops);
SELECT name, specs
FROM products
WHERE specs @> '{"warranty":"2y"}';
What happened?
- jsonb_path_ops GIN index supports @> containment.
- Query finds products whose specs include warranty 2y without scanning every json blob.
Practice next
- Ensure products.specs is jsonb with varied documents.
- CREATE INDEX USING GIN as shown.
- Run EXPLAIN ANALYZE on the @> query.
- Index tags text[] with GIN and query WHERE tags @> ARRAY['sale'];
- Compare jsonb_ops vs jsonb_path_ops index sizes.
Remember
GIN powers jsonb, array, and FTS lookups. @> checks json containment. GIN indexes are larger and slower to build than B-Tree.
PostgresVerse facet filters
Category page filters laptops with 2y warranty via jsonb @> using GIN.
Outcome: Filter API stays under 50ms on 2M SKUs.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!