Collecting Statistics from PostgreSQL in Rails

The PostgreSQL database includes a statistics collection facility that can give you information about your database, tables, indexes and sequences. I just posted a new Rails plug-in that makes it very easy to gather this information and display it in a Rails application.

pgsql_stats screenshot

All of the counters described in the PostgreSQL manual are represented in the models in the plug-in. To name a few:

  • Number of scans over the table or index
  • Cache hit/miss counters (and cache hit rate, by a simple computation)
  • On-disk size

In the above screenshot (taken very soon after the server was started), it’s easy to see that the cron_runs table is by far the largest in the database, followed by its primary key index. Of the entities that have been touched, a large percentage of requests are being satisfied by the buffer cache. You can’t see it in that image, but I’ve defined ranges that turn the green bars red if the cache hit rate falls below 75%.

I’ve set up a Google Group for further discussion. Some additional information is available in the README, and the plug-in can be installed like any other:

$ script/plugin install \ http://svn.lightyearsoftware.com/svn/plugins/pgsql_stats

All on one line, of course.

Update Sep. 10: There is now a usage example on the Google Group that shows how to get the results shown in the screenshot.

2 Responses to “Collecting Statistics from PostgreSQL in Rails”

  1. Roderick van Domburg Says:

    Thanks, this is looking very useful. Could you share the pieces of controller and view with us to render the overview like the one in the screenshot? That’d be stellar!

  2. Steve Says:

    Hi, Roderick.

    Sure, I’m happy to share some of the logic I use in my controller and view to get the results I show in the screenshot. I’ll put them on the Google Group in the next few days and make another mention here on the blog.

Leave a Reply

Contact us: info (at) lightyearsoftware.com