PostgreSQL Enterprise (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
PostgreSQL Index Types: B-Tree vs GIN vs GiST vs BRIN.
senior LevelMost AskedDatabases & SQLPostgreSQLData Structures
In Simple Words:
- B-Tree (Default): Best for unique keys, equality (
=), and range comparisons (<,>,BETWEEN,ORDER BY). - GIN (Generalized Inverted Index): Indexes composite values where a single column contains multiple elements (JSONB, Arrays, Full-Text Search). Fast search, slower write updates.
- GiST (Generalized Search Tree): Hierarchical geometric and range structures (PostGIS spatial points, overlapping date ranges via
&&). - BRIN (Block Range Index): Extremely small index that stores the minimum and maximum value for each block of disk pages (e.g. 128 pages). Ideal for multi-billion row append-only tables sorted by timestamp, using 99% less RAM than a B-Tree!