Skip to content

Conversation

@GaneshPatil7517
Copy link

What does this PR do?

Adds database indexes on task_instance.dag_version_id and dag_run.created_dag_version_id to fix the slow performance of airflow db clean -t dag_version command.

Why is this needed?

When running airflow db clean -t dag_version --batch-size 1000, the cleanup process was taking ~6 minutes per batch on tables with 300K+ rows. The root cause was missing indexes on the foreign key columns that reference dag_version.id.

The cleanup code in db_cleanup.py defines dag_version with dependent_tables=["task_instance", "dag_run"], meaning every delete operation needs to check both tables for FK violations. Without indexes, PostgreSQL performs full table scans.

What changed?

  1. New migration (0098_3_2_0_add_dag_version_id_indexes_for_db_cleanup.py):

    • Adds idx_task_instance_dag_version_id on task_instance(dag_version_id)
    • Adds idx_dag_run_created_dag_version_id on dag_run(created_dag_version_id)
  2. Updated ORM models: Added indexes to __table_args__ for schema consistency

Performance Impact

Scenario Before After
db clean -t dag_version (300K rows, batch=1000) ~6 min/batch < 2 min total

Fixes #60145

Adds indexes on task_instance.dag_version_id and dag_run.created_dag_version_id
to speed up the airflow db clean command when cleaning dag_version records.

Without these indexes, the cleanup operation performs full table scans on both
tables for each batch, causing ~6 minutes per batch on tables with 300K+ rows.
With the indexes, the same operation completes in under 2 minutes total.

Fixes apache#60145
@boring-cyborg
Copy link

boring-cyborg bot commented Jan 9, 2026

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 (https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our prek-hooks will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example DAG that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
  • Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: dev@airflow.apache.org
    Slack: https://s.apache.org/airflow-slack

@boring-cyborg boring-cyborg bot added the area:db-migrations PRs with DB migration label Jan 9, 2026
- Renamed migration from 0098 to 0099 to chain after new upstream migration
- Updated down_revision to e79fc784f145 (timetable migration)
- Updated migrations-ref.rst with correct chain
- Our migration 62fb1d0a1252 is now the new HEAD for 3.2.0
Copilot AI review requested due to automatic review settings January 14, 2026 17:30
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request addresses a performance issue with the airflow db clean -t dag_version command by adding database indexes on foreign key columns that reference dag_version.id. Without these indexes, the cleanup process was performing full table scans resulting in ~6 minutes per batch on tables with 300K+ rows.

Changes:

  • Added migration 0099 to create indexes on task_instance.dag_version_id and dag_run.created_dag_version_id
  • Updated ORM models (TaskInstance and DagRun) to include the new indexes in their __table_args__
  • Updated the database revision head mapping to point to the new migration

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
airflow-core/src/airflow/utils/db.py Updated the 3.2.0 revision head to point to the new migration
airflow-core/src/airflow/models/taskinstance.py Added index on dag_version_id column to table_args
airflow-core/src/airflow/models/dagrun.py Added index on created_dag_version_id column to table_args
airflow-core/src/airflow/migrations/versions/0099_3_2_0_add_dag_version_id_indexes_for_db_cleanup.py New migration that creates the indexes with proper upgrade/downgrade logic
airflow-core/docs/migrations-ref.rst Updated migration reference documentation to include the new migration as head

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…g_version_id_indexes_for_db_cleanup.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:db-migrations PRs with DB migration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possible missing indices on the database tables task_instance and dag_run

4 participants