1.检查当前执行的sql,如果开启增强监控,那么根据增强监控来确定pid。比如pid为1,2 ,3
select procpid, start, now() - start as lap, current_query
from
(select backendid, pg_stat_get_backend_pid(s.backendid) as procpid, pg_stat_get_backend_activity_start(s.backendid) as start, pg_stat_get_backend_activity(s.backendid) as current_query
from
(select pg_stat_get_backend_idset() as backendid) as s
) as s
where
current_query <> '<IDLE>' and procpid IN (1,2,3) --加入查找到的进程ID
order by
lap desc;
2.开启pg_stat_statements
CREATE EXTENSION pg_stat_statements;
按 total_time 列出查询,并查看哪个查询在数据库中花费的时间最多:
SELECT round(total_time*1000)/1000 AS total_time,query
FROM pg_stat_statements
ORDER BY total_time DESC limit 2;
之后根据explain来进行分析。
参考文档:https://aws.amazon.com/cn/premiumsupport/knowledge-center/rds-aurora-postgresql-high-cpu/
https://aws.amazon.com/cn/premiumsupport/knowledge-center/rds-postgresql-running-queries/