Index the rows you query, with a WHERE on the index
A queue table has 90 million rows and 400 of them are pending. An index on status covers all 90 million. A partial index covers the 400.
CREATE INDEX jobs_pending ON jobs (run_at) WHERE status = 'pending';The index stays tiny, inserts of finished rows never touch it, and the planner uses it whenever the query's WHERE matches the index's. This is the single most underused feature in Postgres.
postgresqlperformance