arifrhm opened a new pull request, #76565:
URL: https://github.com/apache/airflow/pull/76565
closes: #76558
### Description
In `ti_run` (`PATCH /execution/task-instances/{id}/run`), when a DAG Run is
triggered by many asset events, serializing `consumed_asset_events` on `DagRun`
triggers lazy N+1 queries for `AssetEvent.asset` and
`AssetEvent.source_aliases` for each event inside a transaction holding a `FOR
UPDATE OF task_instance` row lock. When runs have thousands of asset events,
this leads to task execution API statement timeouts.
This PR uses `selectinload` to eager-load `AssetEvent.asset` and
`AssetEvent.source_aliases` along with `DR.consumed_asset_events`, mirroring
the pattern already used in `_eager_load_dag_run_for_validation()` in
`scheduler_job_runner.py`.
### Testing
Added unit test `test_ti_run_consumed_asset_events_eager_loaded` in
`airflow-core/tests/unit/api_fastapi/execution_api/versions/head/test_task_instances.py`
verifying that both `AssetEvent.asset` and `AssetEvent.source_aliases` are
preloaded in the run context response.
---
##### Was generative AI tooling used to co-author this PR?
- [X] Yes (Antigravity)
Generated-by: Antigravity following [the
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]