I was tasked with troubleshooting a Ruby Rufus Task Scheduler issue today so I started with reviewing the log files. Upon digging into the logs I noticed numerous errors which are expanded below. The primary error appeared to be a connection issue from Ruby to PostgreSQL as it was complaining about too many connections. While the server I was looking into is actively used in a development environment the connections limit of 100 should have been more than enough to handle the load. After looking into the issue more I found out that some large Postgres queries from within the Ruby code were causing the issue and once those were optimized things started working properly. Below I describe the errors noticed via the Ruby logs, steps taken to troubleshoot the errors, and final resolution.

The exceptions above were thrown from a daily report that is run at night in what should be a low traffic time. To get past the above error and work towards finding final resolution I initially raised the maximum allowed conenctions by PostgreSQL from the default of 100 to 1000. To do this modify the postgresql.conf file typically located in the /var/lib/pgsql/data directory. The new line would look similar to the below.

Modify PostgreSQL max_connections Variable:

code

max_connections = 100 # (change requires restart)

As noted in the configuration file you are required to restart PostgreSQL ater modifying max_connections so issue the below command to restart postgresql from a Linux shell. The below example is from CentOS Linux however should be similar on most distrobution.

Restart PostgreSQL On CentOS Linux:

bash

[root@dev ~]# /etc/init.d/postgresql restart

Stopping postgresql service: [ OK ]

Starting postgresql service: [ OK ]

Once restarted I set the report causing the issues to run and sure enough it ran without issue this time. While the report was running I watched the PostgreSQL logs by using the below command.

Please note that your log files are more than likely named something different and will depend on what you have configured in postgresql.conf. The main thing I would suggest you look out for in this scenario is the “duration” lines which describe, in milliseconds, how long a query takes to run. In doing this I noticed several queries from the report code that were taking seconds to run which should never be the case if possible. Once we optimized these queries and I set max_connections back to the default of 100 things started working properly again.