Keeping Queue Tables Fast with PostgreSQL Partial Indexes
Why indexing only pending rows keeps B-tree size tiny and avoids vacuum bloat on append-heavy tables.
The problem with full-table status indexes
A table holding millions of completed jobs and only a few hundred pending tasks wastes cache pages if the status column is indexed across the entire relation.
A partial index scoped to active states stays small enough to remain resident in shared_buffers indefinitely.
CREATE INDEX CONCURRENTLY idx_jobs_pending
ON background_jobs (scheduled_at, id)
WHERE status IN ('queued', 'retrying');
Pairing with SKIP LOCKED
Workers polling with FOR UPDATE SKIP LOCKED hit the tiny partial index directly, giving predictable sub-millisecond dequeue times even while old partitions are archived.