PostgreSQL interview question · Most asked

What index types does PostgreSQL support, and when do you use each?

Summary

B-tree (default: equality, ranges, sorting), Hash (equality only), GIN (inverted index for JSONB, arrays, full-text, trigrams), GiST (geometric data, ranges, nearest-neighbour, exclusion constraints), SP-GiST (partitioned search trees such as quadtrees), and BRIN (tiny block-range summaries for huge, naturally ordered tables such as time-series).

Longer Answer

  • B-tree: almost everything; supports multicolumn, unique, INCLUDE columns and index-only scans.
  • GIN: "does this document contain key X / this array contain element Y / this text match these words"; slower to update (uses a pending list), fast to search.
  • GiST: PostGIS geometries, tsrange overlaps, <-> distance ordering (k-nearest neighbour).
  • BRIN: stores min/max per range of pages; a few kilobytes for billions of rows, effective only when values correlate with physical order (append-only created_at).
  • Extensions add more, for example HNSW and IVFFlat indexes in pgvector for similarity search.

Key Points

  • B-tree default.
  • GIN for JSONB/arrays/full-text.
  • GiST for geometry, ranges, KNN.
  • BRIN for huge ordered tables.

More most-asked PostgreSQL questions

← All PostgreSQL questions