PostgreSQL Enterprise (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
What are the 6 primary Index Types in PostgreSQL?
senior LevelMost AskedCode ExampleSystem DesignDatabases & SQLPostgreSQL
In Simple Words:
- B-Tree (Default): Self-balancing search tree. Best for equality (
=) and range queries (<,<=,>,BETWEEN). - GIN (Generalized Inverted Index): Indexes composite items (Arrays, Full-Text Search tsvectors, JSONB documents).
- GiST (Generalized Search Tree): Tree structure for geometric shapes, spatial ranges, and full-text search (PostGIS coordinates).
- BRIN (Block Range Index): Stores min/max values for ranges of physical disk pages. Extremely compact; ideal for massive append-only sequential data (timestamps, serial IDs).
- SP-GiST (Space-Partitioned GiST): For unbalanced data structures (e.g. Radix trees, quad-trees, telephone area codes).
- Hash: Fast exact equality matching (
=).
text
PostgreSQL Index Types:
┌──────────┬──────────────────────────┬─────────────────────────────────┐
│ Type │ Operators Supported │ Ideal Use Case │
├──────────┼──────────────────────────┼─────────────────────────────────┤
│ B-Tree │ <, <=, =, >=, >, BETWEEN │ Primary keys, timestamps, names │
│ GIN │ @>, ?, ?&, @@ (contain) │ JSONB, Arrays, Text Search │
│ GiST │ &&, &<, &>, <-> (geo) │ PostGIS Lat/Long, Geometric │
│ BRIN │ <, <=, =, >=, > │ Multi-billion row time-series │
└──────────┴──────────────────────────┴─────────────────────────────────┘