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 just the 400.
CREATE INDEX jobs_pending ON jobs (run_at) WHERE status = 'pending';The index stays tiny, inserting finished rows never touches it, and the planner uses it whenever the query's WHERE matches the index's. It's the most underused feature in Postgres.
postgresqlperformance