How to fix high CPU on a PostgreSQL database
Quick answer: High CPU is most often caused by the queries, not the config, so start there. See what is running now in
pg_stat_activity(rows wherestate = 'active'andbackend_type = 'client backend'), rank cumulative cost inpg_stat_statementsbytotal_exec_time, and fix the cause. Common causes include a missing index forcing sequential scans, a plan that regressed after stats drift, or too many active connections.
Step 1: What is running right now?
Section titled “Step 1: What is running right now?”pg_stat_activity is the live view, with one row per server process:
SELECT pid, state, wait_event_type, wait_event, now() - query_start AS running_for, queryFROM pg_stat_activityWHERE state = 'active' AND backend_type = 'client backend'ORDER BY running_for DESC;Filter on backend_type = 'client backend' or the list fills with autovacuum
workers, parallel workers, and replication processes. Those processes can also be
active, but they are not the application queries you are looking for.
- Many
activerows running the same query shape usually indicate a hot query using the CPU, often an un-indexed query that runs constantly. - A few very long-running
activequeries usually indicate an expensive scan, sort, or aggregate. wait_event_typedistinguishes compute from waiting: no wait event = running on CPU;IOorLock= blocked, not compute-bound.
Step 2: What costs the most over time?
Section titled “Step 2: What costs the most over time?”Live activity shows the current moment; pg_stat_statements shows the pattern:
SELECT calls, total_exec_time, mean_exec_time, queryFROM pg_stat_statementsORDER BY total_exec_time DESCLIMIT 20;See identifying slow queries
for guidance on reading the results. Remember that total_exec_time = calls × mean_exec_time, so a
cheap query run constantly can dominate.
Step 3: Match the symptom to the cause
Section titled “Step 3: Match the symptom to the cause”| What you see | Likely cause | Fix |
|---|---|---|
Top query does a Seq Scan on a big table | Missing index | Add the index (find missing indexes) |
| A query that used to be fast is now hot | Plan regression from stats drift | ANALYZE; see why it got slow |
| Hundreds of active backends | Too many connections | Add a connection pooler |
| Slow sorts/hashes spilling to disk | Under-sized work_mem or SELECT * moving too much | Select fewer columns; tune work_mem carefully, note a spill is mostly I/O, with some added CPU from extra merge passes |
Usually, change the query or add an index rather than changing a setting. Configuration and contention can also drive CPU. Common examples include far more connections than cores, no connection pooler in front of a serverless app, or JIT compilation firing on short queries where it costs more than it saves. Check the queries first, then inspect configuration and contention.
Why does AI-generated code cause CPU spikes?
Section titled “Why does AI-generated code cause CPU spikes?”AI coding agents can produce queries whose cost is not obvious in the source: an
un-indexed filter that becomes a Seq Scan, an N+1 loop that fires thousands of
small queries, or a SELECT * that hashes far more data than needed. CPU load
appears when those queries run against production data volumes, which the model does
not see.
How do I keep CPU under control?
Section titled “How do I keep CPU under control?”- Watch
pg_stat_activityfor a pile-up of the same active query. - Keep
pg_stat_statementssorted bytotal_exec_timein a dashboard. - Index the scans, fix the regressions, and pool the connections before touching server config.
What causes high CPU usage in PostgreSQL?
Most often the queries rather than configuration. The common causes are missing indexes forcing sequential scans, a plan regression after statistics drift, and expensive sorts or aggregates. Configuration and contention can be the primary cause too, too many connections thrashing context switches, no connection pooling, or JIT overhead on short queries, so check both, but start with the queries.
How do I find which query is burning CPU right now?
Query pg_stat_activity for rows where state = active, ordered by how long they have been running (now() - query_start). That shows what is executing this instant; pg_stat_statements ordered by total_exec_time shows what costs the most cumulatively.
Is high CPU an I/O problem or a compute problem?
Check wait_event_type in pg_stat_activity. Backends with no wait event are actively running on CPU; backends waiting on IO or Lock are blocked, not compute-bound. High CPU with little waiting points to raw query work like scans, sorts, and hashing.
Will raising max_connections or shared_buffers fix high CPU?
Usually not. High CPU is normally caused by the queries themselves. Adding indexes, fixing a regressed plan, or pooling connections addresses the cause; raising config limits often just lets more expensive work run at once.