On this page
  1. Indexes & EXPLAIN ANALYZE
  2. Contents
  3. Choosing a PostgreSQL Index Type
  4. Reading EXPLAIN (ANALYZE, BUFFERS)

Indexes & EXPLAIN ANALYZE

Prev: JSONB, Arrays & Advanced Types | Up: postgresql | Next: Security, Roles & Row-Level Security
Rel: Indexing & Storage Engines, SQL Query Execution Lifecycle
Ref: PostgreSQL — Index Types
Tags: #public #databases #postgresql #indexing #performance


Contents


Choosing a PostgreSQL Index Type

Index Type Best For
B-Tree (default) Equality (=), ranges (<, BETWEEN), ORDER BY, prefix LIKE 'abc%'
GIN JSONB (@>), TEXT[] arrays, tsvector full-text search
GiST Ranges (&&), PostGIS geometry, trigram similarity (pg_trgm)
BRIN Huge append-only tables correlated with physical order (timestamps/logs)
Hash Pure equality (=) on large values where ordering isn't needed

Build indexes without blocking production writes using CREATE INDEX CONCURRENTLY ....


Reading EXPLAIN (ANALYZE, BUFFERS)

Always include BUFFERS to see actual page hits vs. disk reads (note: ANALYZE executes the query; wrap writes in BEGIN; EXPLAIN ANALYZE ...; ROLLBACK;):

EXPLAIN (ANALYZE, BUFFERS)
SELECT id, total_cents
FROM orders
WHERE user_id = 42 AND status = 'paid'
ORDER BY created_at DESC
LIMIT 20;

Watch for: large mismatches between estimated rows and actual rows (stale stats -> run ANALYZE), unexpected Seq Scan on large tables, Rows Removed by Filter, and Sort Method: external merge Disk (insufficient work_mem).

Linked from

Related notes