Offset-based LIMIT/OFFSET vs keyset pagination using primary key for large datasets in Next.js API Routes
Goal: build a paginated API endpoint in a Next.js Jamstack application that returns a subset of rows from a large PostgreSQL table without degrading response time as the dataset grows. Constraint: the simplest approach uses LIMIT/OFFSET via req.query, but OFFSET forces the database to scan and discard preceding rows, which becomes costly for deep pages. An a