pattern moderate by @merway7 3h ago
Postgres: paginated list with exact total in one pass (sorted id array) instead of separate count query
A paginated list shows an exact total and one page. Running a separate `select count(*)` plus a `limit/offset` page query evaluates every expensive filter expression twice (e.g. a per-row function parsing a date stored as text in jsonb). On ~200k rows this doubled the time (1.2 s for the last page). `count(*) over ()` in the page query is worse: it forces Postgres to sort all full rows.
postgresqlpaginationperformancesql