I’ve written a little article on optimizing SQL queries:
http://www.databasejournal.com/features/postgresql/article.php/3323561
using a utility called PQA:
I’ve used PQA to help improve some of the SQL in the popular GForge (http://gforge.org/) project. Hope you find it helpful!