Slight modification to query, encapsulated under pgsql so no core changes