Repository navigation
fix(tasks): pick the oldest due task without sorting the queue's backlog - #2155
Conversation
…klog A pick scans each queue name (the name plus its legacy aliases) in task_queue_name_status_idx order with its own LIMIT 1, takes the oldest of those, and loads the queue config for that one row. With 100k due tasks a pick takes 0.2-0.6 ms on PG 16, 17 and 18. FOR UPDATE SKIP LOCKED sits under each name's LIMIT 1, so a row another worker holds is skipped before the limit counts it. A pick locks at most one row per queue name until it commits. Tests cover oldest-first order across a queue's names with task_id breaking ties, a pick while another worker holds the oldest rows, and concurrent pickers taking each task once.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
WalkthroughThe PostgreSQL task picker now selects the oldest eligible task across current and legacy queue names. It skips locked rows, resolves configuration for the selected task, and includes tests for ordering and concurrent picking. ChangesTask picking
Priority: ⬇️ Low Estimated code review effort: 3 (Moderate) | ~20 minutes Change: Refactor Merge Risk: ⚪ Minimal · up to
|
✅ PR Title Formatted CorrectlyThe title of this PR match the correct format. Thank you! |
Summary
Every time a worker asked for a task, Postgres sorted all due tasks of that queue. With 100k due tasks one pick took 70–140 ms, and every worker of the queue paid this on every poll.
queue_name = ANY($1)keeps Postgres from readingtask_queue_name_status_idxinscheduled_fororder, even when the array has a single element.The pick now reads each queue name (the name plus its legacy aliases) in index order with its own
LIMIT 1, takes the oldest of those, and loads the queue config for that one row only. With 100k due tasks a pick takes 0.2–0.6 ms on PG 16, 17 and 18, with the same plan on all three. Which task a worker gets does not change: oldestscheduled_forfirst,task_idbreaking ties, across all names of the queue.FOR UPDATE SKIP LOCKEDsits inside each name's scan, under itsLIMIT 1. A row another worker holds is skipped before the limit counts it, so it never hides the due tasks behind it. A pick locks at most one row per queue name until it commits.No migration.
Measurements
Median of 11 warm runs; 100k due tasks (90k under the queue's name, 10k under its legacy name) plus 200k due tasks of another queue. Custom / generic plan:
One case is slower: a resource-demand bound that matches no due task walks the queue's due backlog in index order, about 2x the old custom plan. Lakekeeper itself never sets a bound; only an extension that passes a resource-demand range to the worker reaches this case.
Related issue
Follow-up to #2147.
How was this tested?
test_pick_takes_the_oldest_due_task_across_queue_names: order across a queue's name and its legacy name, ties broken bytask_id, a task not yet due left alone.test_pick_skips_the_tasks_another_picker_holds: with the oldest task of each name held by another transaction, the pick returns the next due task. This test fails if the lock moves outside the per-nameLIMIT 1.test_concurrent_pickers_pick_each_task_once: 8 concurrent pickers over 50 tasks pick each task exactly once.Summary by CodeRabbit