When a PolarDB for PostgreSQL cluster experiences high or sustained CPU utilization, use this guide to identify the root cause and apply the appropriate fix.
In this topic, you can learn:
How to determine whether increased workloads are driving CPU usage
How to identify slow queries using
pg_stat_statementsandpg_stat_activityHow to detect sequential scans and high buffer reads
How to cancel or terminate problem queries and optimize query performance
Causes
High CPU utilization typically has one of two root causes:
Increasing workloads — more active connections consume more compute resources. This is the most common cause.
Slow queries — one or more queries with poor performance hold CPU for an extended period.
Solutions
Once you have identified the problem queries, apply one or more of the following fixes.
Cancel or terminate problem queries
If a small number of unexpected queries are consuming most of the CPU, cancel or terminate them using their pid value from the pid column of pg_stat_activity.
SELECT pg_cancel_backend(pid);
SELECT pg_terminate_backend(pid);After either operation succeeds, CPU utilization returns to normal.
Update table statistics for the query optimizer
If the slow query is necessary, update the statistics on the tables it uses. Fresh statistics let the query optimizer generate a better execution plan.
Running ANALYZE consumes CPU resources. Run it during off-peak hours.
ANALYZE <Table name>;Create indexes on frequently scanned columns
For tables that require frequent sequential scans, create indexes on the columns used in filter conditions. An index allows the database to locate matching rows directly, without scanning every row in the table and filtering in memory.