Retrieve table row count from statistics rather than table scan

See https://wiki.postgresql.org/wiki/Slow_Counting
Use query parameters rather than Sprintf
Move statements out of Client

This reverts e071532d350c332e48d1fbe8a6a81712e789f574