Troubleshooting guide
PostgreSQL high CPU usage: how to find the cause
High CPU on Postgres is almost always a query problem. A cheap query run millions of times can matter more than one slow report.
3 min read10 sectionsUpdated
On this page
Diagnose it
Enable pg_stat_statements through shared_preload_libraries, and rank queries by total execution time.
-- What used the most time overall
SELECT calls, round(total_exec_time) AS total_ms, round(mean_exec_time, 1) AS mean_ms, left(query, 90)
FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 10;
-- What is running right now
SELECT count(*) FILTER (WHERE state = 'active') AS active FROM pg_stat_activity;
-- Inspect one query
EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM orders WHERE customer_id = 42;Fixes
Add the index your plan shows it needs, using CREATE INDEX CONCURRENTLY so that writes are not blocked. Run ANALYZE on tables after bulk changes. Rewrite predicates so columns stay bare: use a range rather than date(created_at) = .... Eliminate N+1 queries with joins or batching. Cache repeated identical reads. Add a pooler if active sessions routinely outnumber cores.
Reading the plan
In EXPLAIN ANALYZE, compare estimated rows with actual rows. A large mismatch means the planner is guessing poorly, and better statistics or a raised default_statistics_target on that column may help. Rows Removed by Filter in the millions marks a scan that reads far more than it returns: that is where an index pays off. Buffers: shared read shows data coming from disk rather than cache. Look for the node where the actual time jumps; that one node usually accounts for most of the cost.
Catch it automatically
Tie CPU spikes to specific queries by adding database spans in APM. Deployment markers tell you if a release introduced the query. For the wider method, see finding slow queries.
Should I add a bigger instance?
Fix the top queries first. One missing index can do more than doubling the server.
Does an index always help?
No. Each index slows writes and uses space, and the planner skips it when most of the table matches.
Can autovacuum cause high CPU?
Yes, briefly. Check pg_stat_activity for autovacuum workers before you tune anything.
Related reading
- High CPU usage on a Linux server: find the cause: Diagnose high CPU on a Linux server: read top, load, steal and iowait, find the busy process and thread, profile it and spot throttling.
- High iowait on Linux: how to find the cause: Diagnose high iowait on Linux with iostat and iotop: read await and %util correctly, find the process doing the I/O, and fix slow queries and swap.
- PostgreSQL idle in transaction: find and fix it: Find and fix PostgreSQL idle in transaction sessions: why they block vacuum and hold locks, which application patterns cause them and which timeout to set.
- Java high CPU: how to find the thread with a thread dump: Find which Java thread causes high CPU: match top -H thread IDs against jstack output, read thread states, spot GC problems and use async-profiler safely.
- PostgreSQL replication lag: causes and how to fix it: Diagnose PostgreSQL replication lag: use pg_stat_replication to tell send, write and replay delays apart, and fix slow replicas, long queries and WAL pile-ups.
- PostgreSQL table bloat: how to find and remove it: Find and remove PostgreSQL table bloat: how dead tuples build up, autovacuum tuning for busy tables, when pg_repack beats VACUUM FULL, and what blocks cleanup.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.