GuideIntermediate2 min read

PostgreSQL Index Design from Real Query Plans

Design composite and partial indexes for a real query shape, read EXPLAIN ANALYZE, and avoid expensive write-side regressions.

Author
Pradeep Gajendra
Published
Updated

Scope of this article

Versions
PostgreSQL 16
Environment
Query example uses illustrative schema and placeholder UUID.

An index is not a reward for a frequently used column. It is a data structure built for a query shape. The right starting point is a slow query with realistic parameters and a measured plan, not a list of columns that feel searchable.

Work backward from the query#

slow-query.sqlsql
select id, published_at, title
from articles
where status = 'PUBLISHED' and topic_id = $1
order by published_at desc, id desc
limit 30;

This workload wants equality filters followed by ordered pagination. A candidate B-tree index can put the selective topic first and then the sort keys, with a partial predicate for published records.

index.sqlsql
create index concurrently articles_topic_feed_idx
on articles (topic_id, published_at desc, id desc)
where status = 'PUBLISHED';

PostgreSQL documents the importance of leftmost columns in multicolumn B-tree indexes [1]. Index usage still depends on statistics, selectivity, row count and cost estimation.

Measure, then compare#

explain.sqlsql
explain (analyze, buffers, settings)
select id, published_at, title
from articles
where status = 'PUBLISHED' and topic_id = '00000000-0000-0000-0000-000000000001'
order by published_at desc, id desc
limit 30;

EXPLAIN shows the chosen plan; ANALYZE actually executes the statement. Inspect estimated versus actual rows, the node doing most work, buffers read/hit and any explicit sort or heap fetches [2]. Replace the placeholder UUID with a real topic ID. Run safely against a representative environment before production.

Five traps that show up in reviews#

  • Adding overlapping indexes without counting write amplification, vacuum overhead and storage.

  • Benchmarking a table with 100 rows, then extrapolating to 100 million.

  • Confusing an index scan with proof of a fast query; repeated random heap reads may dominate.

  • Relying on OFFSET for deep pagination when a stable keyset cursor would avoid scanning skipped rows.

  • Creating a normal index on a hot production table without planning lock impact; CREATE INDEX CONCURRENTLY has its own restrictions and costs.

A keyset alternative#

keyset.sqlsql
select id, published_at, title
from articles
where status = 'PUBLISHED' and topic_id = $1
  and (published_at, id) < ($2, $3)
order by published_at desc, id desc
limit 30;

Use the last row's timestamp and ID as the next cursor. This stabilizes ordering for a snapshot-like view but does not freeze changing data; edits and inserts can still change visible membership. Account for nullability and tie-breaking explicitly.

Sources

  1. [1]
    PostgreSQL 16 — Multicolumn Indexes

    PostgreSQL Global Development Group · publication date unknown · PostgreSQL 16 · retrieved 2026-11-02

  2. [2]
    PostgreSQL 16 — Using EXPLAIN

    PostgreSQL Global Development Group · publication date unknown · PostgreSQL 16 · retrieved 2026-11-02

Spotted an error? Use “Suggest a correction” in the discussion, or read our corrections policy.

Discussion

Sign in to join the discussion. Reading is always open.

Loading discussion…

Report comment