Sobes.tech
Junior — Middle

Could you list the types of index structures used in PostgreSQL and explain their features?

sobes.tech AI

Answer from AI

PostgreSQL uses various types of index structures, each suitable for specific tasks:

  • B-tree — the most common index type, suitable for equality and range queries (>, <, BETWEEN). Used by default.
  • Hash — optimized for equality operations (=), but less versatile than B-tree.
  • GIN (Generalized Inverted Index) — effective for indexing arrays, JSONB, full-text search, when searching for elements within collections.
  • GiST (Generalized Search Tree) — flexible index supporting various user-defined data types, such as geometric objects.
  • SP-GiST (Space-partitioned GiST) — specialized version of GiST for spatial data with space partitioning.
  • BRIN (Block Range Index) — compact index, effective for very large tables with ordered data, indexes block ranges.

The choice of index depends on the data type and query nature, for example, GIN is used for full-text search, while B-tree is used for regular numeric or string fields.