Skip to content

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

Key points

  • Rank queries by total time, not by slowest.
  • Sequential scans on large tables are the top offender.
  • Stale statistics make good indexes go unused.
  • A sudden change suggests a new plan, a deploy or a traffic mix shift.

Where the CPU goes

Query execution dominates: scanning, sorting, joining, computing aggregates. Other contributors are autovacuum, too many active connections, and parallel workers. Each active backend uses at most one core per process, so you can tell a lot by counting active sessions.

Common causes

  • Missing indexes forcing sequential scans.
  • Bad plans from outdated statistics.
  • An N+1 pattern, millions of individually fast queries.
  • More active sessions than cores.
  • Functions in WHERE clauses that prevent index use.

Diagnose it

Enable pg_stat_statements through shared_preload_libraries, and rank queries by total execution time.

bash
-- 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.

Frequently asked questions

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.

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.