High PostgreSQL CPU
High CPU is a symptom, not a diagnosis.
Rank statements by total workload cost, not only worst single latency.
Check whether CPU is actually the primary constraint or merely correlated with another bottleneck.
Retries, connection storms and N+1 behavior can multiply database work after a release.
Before upgrading the instance, establish whether the workload would simply consume the larger instance too.
Read-only. Run them during the problem window if you can; most of these views show current state, not history.
-- Top statements by total execution time (pg_stat_statements, PostgreSQL 13+)
SELECT queryid,
calls,
round(total_exec_time::numeric / 1000, 1) AS total_s,
round(mean_exec_time::numeric, 2) AS mean_ms,
round((100 * total_exec_time / sum(total_exec_time) OVER ())::numeric, 1) AS pct_of_total,
shared_blks_hit + shared_blks_read AS blocks,
left(query, 80) AS query
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 15;
-- What active sessions are doing right now
SELECT coalesce(wait_event_type, 'none (on CPU)') AS wait_type,
wait_event,
count(*)
FROM pg_stat_activity
WHERE state = 'active' AND pid <> pg_backend_pid()
GROUP BY 1, 2
ORDER BY 3 DESC;To rank the statements behind the CPU, see finding slow queries with pg_stat_statements. For the index decision, see how to test BRIN against B-tree on large, physically ordered tables.
Start with the problem and the evidence you already have. You will get a direct answer on whether a scoped investigation is the right next step.
Prefer to talk first? Book a 20-minute triage call.