Skip to content

fix(db_cleanup): skip dag rows whose dag_version cascade is blocked by task_instance FK - #71880

Closed
waterWang wants to merge 1 commit into
apache:mainfrom
waterWang:fix/airflow-db-clean-dag-cascade-fk-bug
Closed

waterWang wants to merge 1 commit into
apache:mainfrom
waterWang:fix/airflow-db-clean-dag-cascade-fk-bug

Conversation

@waterWang

Copy link
Copy Markdown

Problem

airflow db clean fails with an IntegrityError when deleting old dag rows. The dag → dag_version ON DELETE CASCADE propagates to dag_version, but task_instance.dag_version_id is ON DELETE RESTRICT, so the cascade fails the foreign key constraint (and, on MySQL, can leave the cleanup command blocked on metadata locks).

PR #68339 added skip_if_referenced to protect the dag_version table's own cleanup query, but did not protect the dag → dag_version cascade path.

Fix

Add a new skip_if_cascade_blocked config option to _TableConfig that generates a correlated NOT EXISTS subquery across the cascade path. Apply it to the dag table config: exclude dags whose dag_version rows are still referenced by any task_instance.

Closes #71864

…y task_instance FK

When ``airflow db clean`` deletes old dag rows, ``ON DELETE CASCADE`` on
``dag_version.dag_id`` propagates the delete to ``dag_version``. If any
``dag_version`` row is still referenced by a ``task_instance`` via the
``ON DELETE RESTRICT`` foreign key ``task_instance.dag_version_id``, the
cascade fails with an ``IntegrityError`` (and, on MySQL, can block the
command indefinitely on metadata locks).

Add a new ``skip_if_cascade_blocked`` config option to ``_TableConfig``
that generates a correlated ``NOT EXISTS`` subquery on the cascade path.
When set on the ``dag`` table config, the cleanup query excludes dags
whose ``dag_version`` rows are still referenced by any ``task_instance``.
These dags become eligible once their task instances age out and are
cleaned by a later run.

Related: apache#68339 (which added ``skip_if_referenced`` for the direct
``dag_version`` → ``task_instance`` path, but did not protect the
``dag`` → ``dag_version`` cascade).

Closes apache#71864

@ColtenOuO ColtenOuO left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for PR, but I have some suggestions and my personal opinion

PR title does not follow the naming convention

The PR title should follow the convention.

Suggested title: Fix airflow db clean failing when a dag's cascade delete hits a dag_version still referenced by a task instance

Design concern: skip_if_cascade_blocked

Adding skip_if_cascade_blocked as a separate parameter feels like the wrong direction here — it's structurally the same NOT EXISTS guard as skip_if_referenced, just with one extra JOIN hop, so now we're maintaining two near-identical mechanisms for the same concept. Since dag_version already has skip_if_referenced=[("task_instance", "dag_version_id")] to protect itself, could this instead extend skip_if_referenced to handle one intermediate hop, rather than introducing a whole new field?

ruff / pre-commit

Did you run pre-commit? Some of the formatting here looks like ruff should have caught it.

Gen-AI disclosure

Sorry, this part is purely my personal opinion and unrelated to the project itself (I happened to give feedback on one of your other PRs before, so I have some sense of the situation across a few of your PRs).

I've noticed several of your PRs on Airflow were closed shortly after being opened, and you also appear to have opened a large number of PRs on other projects in a short time with the help of AI. Using AI assistance on Airflow is fine, but it should follow the disclosure requirements in the contributing guide.

AI is a great tool that can help us diagnose problems quickly, but if it's only used to churn out PRs quickly, it just creates trouble and extra work for maintainers, and wastes CI resources.

If you'd like to help Airflow keep getting better, I'd suggest reading through the contributing guide first: https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions

If you choose to use AI, please make sure you thoroughly analyze the problem yourself, understand and verify the changes, and properly run the relevant tests and checks locally using breeze and pre-commit.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ForeignKeyViolation when doing airflow db clean (dag table)

3 participants