Im-joongyeon opened a new pull request, #77738: URL: https://github.com/apache/airflow/pull/77738
The scheduler runs `_remove_unreferenced_triggers()` on an interval. It deletes triggers that nothing references, using correlated `NOT EXISTS` subqueries. The `task_instance` subquery names `TaskInstance` in its FROM clause, so the session hook in `models/taskinstance.py` adds `working_set IS TRUE` to it. That causes two problems: - **Full table scan on MySQL.** The extra column means MySQL can no longer answer the subquery from `ti_trigger_id`, so it reads the whole `task_instance` table. It does this twice per cleanup, because `synchronize_session="fetch"` runs a SELECT with the same WHERE clause before the DELETE. - **Cascade-deleting archived attempts.** A trigger held only by an archived attempt (`working_set IS NULL`) looks unreferenced. Deleting it cascade-deletes that attempt's row through `task_instance_trigger_id_fkey` (`ON DELETE CASCADE`). This switches to uncorrelated `NOT IN` subqueries over the non-NULL `trigger_id`s and sets `include_all_attempts=True`. With that, the database range-scans only the rows that hold a trigger, and archived attempts count as references again. The `IS NOT NULL` filters are also needed for correctness, because one NULL in a `NOT IN` list makes it match nothing. `test_delete_unreferenced_triggers_with_null_trigger_id_ti` already covers that case. related: #77725 (same fix for the triggerer's `Trigger._delete_unused_batch()`), #75147 #75147 replaces this method with a call to `Trigger._delete_unused_batch()` to batch the deletes. That PR and this one touch the same lines. If #75147 and #77725 both merge, the scheduler gets this fix through the triggerer's method and this PR can be closed. Until then, this PR fixes the scheduler on its own. #### Plans (2M `task_instance` rows, 1,000 deferred; 2,000 triggers, 500 unreferenced; fresh stats) MySQL 8.0, the SELECT that `synchronize_session="fetch"` issues: | | `task_instance` access | SELECT | Whole cleanup (SELECT + DELETE of 500 rows) | |---|---|---|---| | Before | `Table scan on task_instance` → `Filter: ((0 <> task_instance.working_set) is true)` (rows=2e+6) | 456 ms | 1,031 ms | | After | `Covering index range scan on task_instance using ti_trigger_id over (NULL < trigger_id)` (rows=1000) | 1.8 ms | 66 ms | PostgreSQL 15: the planner already picked an index-based merge anti join for this data shape, so the timing is unchanged (0.72 ms → 0.69 ms for the SELECT). After the change it runs one `Index Only Scan using ti_trigger_id`. SQLite: the correlated per-trigger lookup becomes a single `SEARCH task_instance USING COVERING INDEX ti_trigger_id (trigger_id>?)` list subquery. The new plan test asserts this. #### Tests run - `breeze run --backend mysql pytest airflow-core/tests/unit/jobs/test_scheduler_job.py -k trigger`: 29 passed, 1 skipped (the SQLite-only plan test). - `breeze run --backend postgres pytest airflow-core/tests/unit/jobs/test_scheduler_job.py -k trigger`: 29 passed, 1 skipped (the same test). - SQLite: `test_scheduler_job.py -k unreferenced_triggers`, 7 passed. - Both new tests fail on `main`. `test_delete_unreferenced_triggers_keeps_trigger_held_by_archived_attempt` fails because the trigger is deleted. `test_delete_unreferenced_triggers_reads_only_task_instances_holding_a_trigger` fails because the plan is a correlated per-trigger lookup. - `prek run --stage pre-commit` on the changed files, including `mypy-airflow-core`. --- ##### Was generative AI tooling used to co-author this PR? - [X] Yes — Claude Code (Opus 5.5) Generated-by: Claude Code (Opus 5.5) 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]
