Fix an invalid SQL exception that occurs when the paginator tries to
do a COUNT(*) to calculate the page count of a relation that already
includes a GROUP BY + COUNT(*) clause. We need to nest the whole query
inside a `SELECT COUNT(*) FROM (...)` subquery so the inner COUNT(*)
doesn't mess up the outer COUNT(*).
Fixes#4285.