perf(scheduler): chunk tuple IN list in _create_dag_runs to bound SQL params#68316
Closed
ingaleniranjan365 wants to merge 1 commit into
Closed
Conversation
… params
On large Airflow deployments (hundreds of DAGs), _create_dag_runs() emits
WHERE (dag_id, logical_date) IN (('dag1', ts1), ..., ('dagN', tsN)) with
O(N) bind parameters on every scheduler heartbeat (default: 5s interval).
PostgreSQL re-plans this from scratch each time -- causing perceived scheduler
hanging reported in apache#61453 (26 thumbsup).
Replace the single unbounded IN with a chunked loop of at most 1000-row
batches so the parameter count is bounded regardless of fleet size. The same
pattern was fixed at a neighbouring call site in PR apache#62114; this finishes it.
Fixes part of: apache#61453
Co-authored-by: Wibey VSCode Extension <wibey@walmart.com>
|
Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Performance issue
File:
airflow-core/src/airflow/jobs/scheduler_job_runner.py:2234On large Airflow deployments (hundreds of DAGs),
_create_dag_runs()emits aWHERE (dag_id, logical_date) IN (('dag1', ts1), ..., ('dagN', tsN))clause with O(N) bind parameters on every scheduler heartbeat (default: every 5 seconds). PostgreSQL's query planner re-plans this from scratch each time, causing perceived scheduler "hanging" — the root cause reported in #61453 (26 👍).The same pattern was already fixed for a neighbouring call site in PR #62114 (merged 2026-03-13). This PR fixes the remaining location.
Fix
Replace the single unbounded
tuple_.in_()with a chunked loop of ≤1000-row batches so the SQL parameter count is bounded regardless of fleet size. Semantics are identical — the results are combined into the sameexisting_dagrunsdict before use.Evidence
Before: 1 query with O(N) bind parameters (N = number of scheduled DAGs) — re-planned by Postgres on every 5s heartbeat.
After: ⌈N/1000⌉ queries each with ≤2000 bind parameters — stable query plan, no planner thrashing.
Validation
pytest airflow-core/tests/unit/jobs/test_scheduler_job.py -k test_create_dag_runsFixes part of: #61453