almessadi.
Back to Index

Postgres Index Types Only Help When They Match the Query Pattern_

B-tree is the default for a reason, but PostgreSQL has other index types for equality lookups, document search, geometry, and other specialized access patterns.

PublishedSeptember 22, 2024
Reading Time5 min read

“Add an index” is not bad advice. It is incomplete advice.

The real question is: what kind of lookup are you optimizing?

PostgreSQL supports multiple index types because not all query shapes look alike. A good index choice follows the access pattern rather than defaulting blindly to whatever is easiest to generate from the GUI.

Start With the Query

Ask what the query is actually doing:

  • equality lookup
  • range scan
  • full-text search
  • geometric proximity
  • document membership

That question should come before the index type.

B-Tree Is the Default, Not the Universal Answer

B-tree is versatile and usually right for:

  • equality
  • ordering
  • ranges

That is why it is the default.

But other cases call for other tools. For example, GiST becomes relevant for geometric and nearest-neighbor style searches, while GIN often fits document and full-text patterns much better.

That difference becomes concrete very quickly:

CREATE INDEX articles_search_idx
  ON articles
  USING GIN (to_tsvector('english', title || ' ' || body));

That is solving a different problem than a simple B-tree on created_at.

The Engineering Habit That Scales

Do not memorize exotic index types first. Memorize the workflow:

  1. inspect the query pattern
  2. inspect the execution plan
  3. choose the index type that matches the actual work

That keeps indexing attached to evidence instead of folklore.

Further Reading