High CPU Usage By Postgres Process High CPU Usage By Postgres Process codeigniter codeigniter

High CPU Usage By Postgres Process


I came across with the similar kind of issue. The reason was - some transactions were getting stuck and running since long time. Hence CPU utilization got increased to 100%. Following command helped to find out the connections running for the longest time:

SELECT max(now() - xact_start) FROM pg_stat_activity                           WHERE state IN ('idle in transaction', 'active');

This command shows the the amount of time a connection has been running. This time should not be greater than an hour. So killing the connection which was running for a long long time or stuck at any point worked for me. I followed this post for monitoring and solving my issue. Post includes lots of useful commands to monitor this situation.


You need to find out what PostgreSQL is doing. Relevant resources:

Once you find what the slow or the most common queries are use, use EXPLAIN to make sure they are being executed efficiently.