Skip to content

fix(tasks): pick the oldest due task without sorting the queue's backlog - #2155

Merged
c-thiel merged 1 commit into
mainfrom
ct/pick-one-per-queue-name
Oct 21, 2026
Merged

c-thiel merged 1 commit into
mainfrom
ct/pick-one-per-queue-name

Conversation

@c-thiel

@c-thiel c-thiel commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

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 reading task_queue_name_status_idx in scheduled_for order, 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: oldest scheduled_for first, task_id breaking ties, across all names of the queue.

FOR UPDATE SKIP LOCKED sits inside each name's scan, under its LIMIT 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:

Case PG 16 before PG 16 after PG 17 before PG 17 after PG 18 before PG 18 after
1 name 81 / 140 ms 0.45 / 0.17 ms 73 / 109 ms 0.58 / 0.17 ms 71 / 92 ms 0.17 / 0.17 ms
2 names 109 / 136 ms 0.58 / 0.24 ms 81 / 111 ms 0.48 / 0.22 ms 81 / 101 ms 0.26 / 0.24 ms
Resource demand 100..500 35 / 78 ms 1.2 / 0.4 ms 33 / 51 ms 1.1 / 0.3 ms 32 / 46 ms 0.3 / 0.4 ms
Resource demand matches no task 16 / 41 ms 41 / 29 ms 15 / 39 ms 30 / 32 ms 15 / 27 ms 28 / 26 ms

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?

  • Unit tests added or updated
    • test_pick_takes_the_oldest_due_task_across_queue_names: order across a queue's name and its legacy name, ties broken by task_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-name LIMIT 1.
    • test_concurrent_pickers_pick_each_task_once: 8 concurrent pickers over 50 tasks pick each task exactly once.
  • Integration tests added or updated
  • On PG 16, 17 and 18, by hand:
    • The old and new queries return the same rows over 60 consecutive picks (ties, project-level tasks, legacy names, canonical-over-legacy config, resource-demand bounds).
    • With the 1 or 5 oldest rows held by another session, a pick returns the next unheld row.
  • Query engine / Storage / Authorization: not affected.

Summary by CodeRabbit

  • Bug Fixes
    • Task processing now consistently selects the oldest eligible task across current and legacy queues.
    • Locked tasks are skipped before queue limits are applied, improving task availability.
    • Queue settings are correctly resolved for tasks using legacy queue names.
    • Concurrent workers are less likely to select the same task, preventing duplicate processing.

…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.
@coderabbitai

coderabbitai Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4db28bdd-8f08-45e4-8e49-bd7c835ff7ec
📥 Commits

Reviewing files that changed from the base of the PR and between 2e43ea6 and 8c6a579.

⛔ Files ignored due to path filters: 1
  • .sqlx/query-a57b9fa0feee0c4faf59482151a172540eb5d1d295bc8bebcc7f18927f86013a.json is excluded by !.sqlx/**
📒 Files selected for processing: 1
  • crates/lakekeeper-storage-postgres/src/tasks.rs

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 0 remain after this review.

Walkthrough

The 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.

Changes

Task picking

Layer / File(s) Summary
Cross-queue selection and validation
crates/lakekeeper-storage-postgres/src/tasks.rs
The picker selects the oldest eligible task across queue names, skips locked rows, and resolves configuration from the selected task. Tests cover tie-breaking, lock skipping, and concurrent picking across two queue names.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Merge Risk: ⚪ Minimal · up to 8c6a5

Task picking remains mergeable: concurrent contention may delay a pick until the next poll, but it does not lose or permanently block tasks.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 1 files.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title clearly describes the main change: selecting the oldest due task without sorting the full queue backlog.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checks the queues at dawn
And finds the oldest task to run
Locked rows pass; the next steps through
Config follows the task picked true
Eight paws race; each task is done
Then carrots mark the work well spun

Comment @coderabbitai help to get the list of available commands.

@c-thiel c-thiel changed the title perf(tasks): pick the oldest due task without sorting the queue's backlog fix(tasks): pick the oldest due task without sorting the queue's backlog Oct 21, 2026
@c-thiel
c-thiel merged commit 059030f into main Oct 21, 2026
76 of 78 checks passed
@c-thiel
c-thiel deleted the ct/pick-one-per-queue-name branch October 21, 2026 14:48
@github-actions

Copy link
Copy Markdown
Contributor

✅ PR Title Formatted Correctly

The title of this PR match the correct format. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant