Sobes.tech
Senior

What indexes do you use in PostgreSQL?

sobes.tech AI

Answer from AI

In PostgreSQL, the following index types are commonly used depending on the task:

  • B-tree — the most common, suitable for most comparison operations (=, <, >, BETWEEN).
  • Hash — for fast equality operations, but less versatile and used less frequently.
  • GIN (Generalized Inverted Index) — for full-text search and indexing arrays, JSONB.
  • GiST (Generalized Search Tree) — for geospatial data, full-text search, ranges.
  • BRIN (Block Range Index) — for very large tables with natural data order, saves space.

Example of using a B-tree index:

CREATE INDEX idx_users_email ON users(email);

The choice of index depends on the data type and queries. For example, GIN is often used for JSONB fields, GiST for geospatial data.