Performance
5 min read
November 22, 2026
PostgreSQL performance optimization: a practical checklist
A PostgreSQL performance optimization checklist: finding expensive queries with pg_stat_statements, reading EXPLAIN plans, composite and partial indexes, keyset pagination, vacuum, pooling, replicas and partitioning.
By Parth Patel
When a PostgreSQL-backed application gets slow, the database is often blamed and rarely at fault on its own. Usually a few queries are doing far more work than they need to. PostgreSQL performance optimization is mostly about finding those queries and giving the planner a better path, before reaching for bigger hardware or a cache.
This is the checklist I work through, roughly in order of payoff.
1. Find the queries that matter
Don't guess. Enable pg_stat_statements and sort by total time, not by the slowest single query. A query that takes 5 ms but runs two million times a day costs more than a 2-second report that runs twice.
SELECT query, calls, round(total_exec_time) AS total_ms, round(mean_exec_time, 1) AS mean_ms, rows
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 20;Also look for queries issued in loops from application code: the classic N+1 problem, where a list page runs one query per row. Fixing it in the application (a join or a batched WHERE id = ANY($1)) often beats any database tuning.
2. Read the plan
For each expensive query, run EXPLAIN (ANALYZE, BUFFERS) on realistic data:
EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM orders
WHERE tenant_id = '…' AND status = 'open'
ORDER BY created_at DESC
LIMIT 50;What to look for:
- Sequential scans on large tables where you expected an index.
- Estimated rows far from actual rows, a sign of stale or insufficient statistics.
- Sorts and hashes spilling to disk ("external merge"), which point to missing indexes or low
work_mem. - Large numbers of buffers read for few rows returned.
3. Index for your actual queries
Indexes should match how you filter and sort, in that order:
- Composite indexes follow the query: equality columns first, then range or sort columns. For the query above,
(tenant_id, status, created_at DESC)lets PostgreSQL find and order rows in one pass. - Partial indexes cover the rows you actually query:
CREATE INDEX ON orders (created_at) WHERE status = 'open'stays small when most orders are closed. - Covering indexes with
INCLUDElet a query be answered from the index alone. - Expression indexes match computed filters such as
lower(email).
Create indexes on busy tables with CREATE INDEX CONCURRENTLY to avoid blocking writes, and remove indexes that are never used (check pg_stat_user_indexes); every index slows down writes.
4. Fix pagination
OFFSET 10000 LIMIT 50 reads and discards ten thousand rows. For deep pagination use keyset pagination:
SELECT * FROM orders
WHERE tenant_id = $1 AND (created_at, id) < ($2, $3)
ORDER BY created_at DESC, id DESC
LIMIT 50;It stays fast on page one and page one thousand.
5. Select only what you need
SELECT * pulls every column, including large text and JSON fields, across the network and prevents index-only scans. List the columns the screen actually uses.
6. Keep statistics and vacuum healthy
PostgreSQL's planner relies on table statistics, and its MVCC design relies on vacuum to clean up dead rows.
- Make sure autovacuum keeps up on busy tables; tune its thresholds per table if large tables bloat.
- Run
ANALYZEafter bulk loads. - For columns with skewed or correlated data, raise the statistics target or create extended statistics so estimates improve.
Table and index bloat makes every query read more pages than it needs.
7. Manage connections
Each PostgreSQL connection is a process with its own memory. Hundreds of idle connections from many application instances waste memory and can exhaust max_connections. Use a connection pooler (PgBouncer in transaction mode, or your platform's equivalent) and keep application pools small.
8. Tune memory settings sensibly
The defaults are conservative. Common starting points, adjusted to your workload and server:
shared_buffersaround a quarter of RAM.effective_cache_sizearound half to three quarters of RAM (it informs the planner; it doesn't allocate memory).work_memhigh enough that common sorts don't spill to disk, remembering it applies per operation, per connection.
Change one setting at a time and measure.
9. Separate reads from writes
When read load is the problem and queries are already efficient, route read-only traffic (reports, dashboards, search pages) to read replicas. Remember replication lag: a user who just saved something should read it from the primary.
For heavy analytics, move data to a warehouse rather than running large aggregations on the transactional database.
10. Partition very large tables
Time-series and log-like tables with hundreds of millions of rows benefit from declarative partitioning by time. Queries that filter by date touch only the relevant partitions, and old data can be dropped by detaching a partition instead of running huge deletes.
11. Then consider caching
Once the database is doing efficient work, a cache can absorb repeated reads of data that changes rarely. Caching first only hides problems that come back when the cache is cold. See Redis caching strategies for doing it safely.
A quick checklist
- Rank queries by total time with
pg_stat_statements; fix N+1 patterns. - Read
EXPLAIN (ANALYZE, BUFFERS)for the top offenders. - Add composite, partial or covering indexes that match real queries; drop unused ones.
- Use keyset pagination and select only needed columns.
- Keep vacuum and statistics healthy.
- Pool connections.
- Tune memory settings, one at a time.
- Add replicas, partitioning and caching when the evidence calls for them.
Most PostgreSQL performance problems are solved in the first four steps.