---
title: "Choosing the right Postgres indexes"
description: "Postgres indexes pay off for frequently run queries that discard most rows they read, need results in a specific order, or must enforce uniqueness."
url: "https://swe.quest/articles/choosing-the-right-postgres-indexes-8f3c9e0e9ee3e588"
canonical: "https://incident.io/blog/choosing-the-right-postgres-indexes"
source: "incident.io"
published: "2024-10-25"
difficulty: "medium"
kind: "How-to"
reading_minutes: 9
tags: ["postgres","performance","sql","observability"]
---

# Choosing the right Postgres indexes

A post on [incident.io](https://swe.quest/sources/incident-io.md), published 2024-10-25, 9 min read, How-to. Original: [https://incident.io/blog/choosing-the-right-postgres-indexes](https://incident.io/blog/choosing-the-right-postgres-indexes)

## Summary

Postgres indexes pay off for frequently run queries that discard most rows they read, need results in a specific order, or must enforce uniqueness. In a 500k-row example, adding a B-tree index cuts a lookup from 215ms to 0.05ms. Put the most frequently filtered columns first in composite indexes, prefer reusable indexes over narrowly tailored ones, and use partial indexes (e.g. WHERE archived\_at IS NULL) to keep them small. Missing indexes can be spotted with a Grafana panel on pg\_stat\_user\_tables\_seq\_tup\_read and confirmed with EXPLAIN ANALYZE.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Observability](https://swe.quest/tags/observability.md) (Reliability & operations)

## Prerequisites

- basic SQL ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- relational database tables ([easy posts on Databases](https://swe.quest/tags/databases.md?difficulty=easy))

## Keywords

PostgreSQL, EXPLAIN ANALYZE, B-tree index, partial index, Grafana, pg\_stat\_user\_tables

## Related posts

- [If I have read-only tables in Postgres, should I index them heavily?](https://swe.quest/articles/if-i-have-read-only-tables-in-postgres-should-i-index-them-heavily-cd42f0cfb1d86dc0.md): Neon · 2024-07-17 · Medium · 9 min read · PostgreSQL, Performance engineering, SQL
  Even when writes are off the table, heavily indexing read-only Postgres tables only pays off under certain conditions: table size, query complexity, data cardinality and resource constraints.
- [The side quest that freed 10% of our database](https://swe.quest/articles/the-side-quest-that-freed-10-of-our-database-649662623bb94a1e.md): incident.io · 2026-11-24 · Medium · 13 min read · PostgreSQL, Query engines, Performance engineering, SQL
  A single lateral-join query answering "who's on call for this rotation layer right now" consumed 34% of incident.io's Postgres replica CPU.
- [Fix a top cause of slow queries in PostgreSQL (no slow query log needed)](https://swe.quest/articles/fix-a-top-cause-of-slow-queries-in-postgresql-no-slow-query-log-needed-4d1a4c0df1aa9a0d.md): Render · 2024-12-12 · Medium · 7 min read · PostgreSQL, Performance engineering, SQL
  PostgreSQL automatically indexes primary keys and UNIQUE columns but not the referencing column of a foreign key, so lookups on that side and ON DELETE CASCADE / SET NULL triggers fall back to full table scans.
- [PostgreSQL Stories: From slow query to fast—via stats](https://swe.quest/articles/postgresql-stories-from-slow-query-to-fast-via-stats-ac7001ab0fde8224.md): Render · 2024-05-14 · Medium · 13 min read · PostgreSQL, Query engines, Performance engineering, Debugging
  A simple LIMIT 1 join between a ~100k-row postgres\_dbs table and a ~90M-row events table ran for hours.
- [Postgres Support Recap: Investigating Postgres Query Performance](https://swe.quest/articles/postgres-support-recap-investigating-postgres-query-performance-82d2d3f876d71410.md): Neon · 2024-02-29 · Medium · 9 min read · PostgreSQL, Performance engineering, SQL, Observability
  Three starting points for diagnosing slow Postgres queries: use pg\_stat\_statements to find long-running queries by mean execution time and then EXPLAIN ANALYZE them, check the cache hit ratio (in Neon via the neon\_stat\_file\_cache view, since Neon extends shared buffers with a local file cache), and look for table and index bloat caused by MVCC dead tuples.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
