Heads up: counting may be slow in PostgreSQL
The linked article points out that
COUNT queries might be unexpectetly slow in psql.
If you just need to know "are there any records" use
present? instead. The latter uses
SELECT 1 AS one FROM ... LIMIT 1 under the hood.
In short: Replace
foo.count > 0 with