Repository navigation
Fix airflow db clean silently skipping three tables when scoped to Dags - #73173
Conversation
The cleanup config named a dag_id column on three tables that do not have one. task_reschedule.dag_id was dropped in 3.0.0, deadline.dag_id in 3.1.0, and asset_event has only ever had source_dag_id. Any run passing --dag-ids or --exclude-dag-ids built SQL against a column that does not exist, and run_cleanup suppresses a failing table into a warning, so the command reported success and exited 0 while never purging a row from any of them. deadline and task_reschedule now reach their Dag the way the schema does, through dag_run and task_instance, so a table with no dag id of its own can be scoped through a foreign key. Dropping the filter instead would have been worse for deadline: it is cleaned as a dependent of dag_run precisely so its rows are archived before the ON DELETE CASCADE removes them, so an unscoped pass would purge deadlines belonging to the very Dags whose runs --exclude-dag-ids preserves. --exclude-dag-ids now also treats a NULL dag id as eligible. A row attributed to no Dag is not one of the excluded Dags' rows, but NOT IN alone yields NULL for it and retains it forever. That was every job row, since core never sets Job.dag_id, and it becomes reachable for asset_event and log too. The guard test only ever called run_cleanup without Dag filters, which is the branch that dereferences these column names, so all three configs drifted unnoticed across two releases. It is now parametrized over the filter combinations and fails on each of them.
|
Two PRs were already working on this, both are tagged "closes: #69030", and I've been working with those authors. #66350 has been open since 4 May and has been through several rounds of review. #71732 came in August and was cherry-picked into #66350 to avoid the two conflicting. Same three tables, same cause, same fix. Your Please don't close #66350. It also re-applies |
Commented on that PR too, thanks Dennis |
The Airflow image ran on apache/airflow:3.3.2-python3.13, a Debian 12 base whose scan by Trivy (#430) found 13 CRITICAL vulnerabilities in its OS packages and 4 in its Python packages, all with a fixed version. Airflow 3.4.0, released on 6 November 2026, is the newest release; its image for Python 3.13 is built on Debian 13 from the Docker Hardened Images, and its constraints hold the fixes of Authlib, certifi and uv. What the upgrade changes for this code: - The constraints of 3.4.0 pin Apprise 2.1.0 and certifi 2026.10.23, so requirements.txt follows Apprise, and the override of certifi above the constraints goes. Apprise 2 also splits a list of addresses at a semicolon, and the plugin of the alerts now does too, so that each address is still masked on its own. - 3.4.0 makes one run for each event of a DAG that runs on one asset or on an OR of assets. The histories run on TRANSFORMED and would plan once for each run of the transform; compose.yaml keeps the batching of 3.3.2 with [scheduler] batch_asset_events, which also spares the DAGs of the extensions folder a change of behavior at the update. - 3.4.0 fixes the filter of db clean by DAG (apache/airflow#73173), so airflow-init cleans the database of Airflow in one call again, and names the tables as 3.4.0 does: xcom_v1 and xcom_v2 for xcom, and task_instance for task_instance_history, which the migration folds into it. - The comments that state a behavior "as of Airflow 3.3.2" now name 3.4.0 where it was checked again; those of the retries said that True would be read as 1, while both versions read it as 2.0. The image of 3.4.0 has no hostname command, so a stack test reads the name of the host through Python. The workflow Upgrade passed from v0.6.0 to this branch on 26 November 2026 (run 45880133190). Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: drrragn <[email protected]>
Follow-up to #70923, which restored the coverage check for
db clean. That check compares table names against the config, not the columns the config names, so it cannot catch this.airflow db clean --dag-idsor--exclude-dag-idssilently purges nothing from three tables. Each names adag_idcolumn it does not have:task_reschedule.dag_idwas dropped in 3.0.0,deadline.dag_idin 3.1.0, andasset_eventhas only ever hadsource_dag_id. The query fails on a missing column,run_cleanupsuppresses a failing table into a warning, and the command exits 0. An operator who always scopes by Dag has been growing those three tables forever while cleanup reported success.deadlineandtask_reschedulenow reach their Dag the way the schema does, throughdag_runandtask_instance.Why scope them rather than just drop the filter, which would be the smaller diff:
deadlineis cleaned as a dependent ofdag_runprecisely so its rows are archived before theON DELETE CASCADEremoves them. An unscoped pass would purge deadlines belonging to the very Dags whose runs--exclude-dag-idsis preserving, turning a silent no-op into silent data loss.--exclude-dag-idsnow also treats a NULL dag id as eligible. A row attributed to no Dag is not one of the excluded Dags' rows, butNOT INalone yields NULL for it and retains it forever. That is everyjobrow, since core never setsJob.dag_id, and it becomes reachable forasset_eventandlogtoo. This changes behaviour for those tables, in the direction the flag already documents.The guard test is the part that stops this recurring. It only ever called
run_cleanupwithout Dag filters, and the Dag filter is the only thing that dereferences these column names, so all three configs drifted unnoticed across two releases. It is now parametrized over the filter combinations and fails on each of them.