1fanwang opened a new pull request, #73745:
URL: https://github.com/apache/airflow/pull/73745

   Operators running `airflow db clean` cannot prune 
`rendered_task_instance_fields` (RTIF) independently of `task_instance`. 
Because RTIF tables store full rendered templates, configuration payloads, and 
Kubernetes pod specs for every task attempt, they grow rapidly into gigabytes 
of database storage while task instances are retained for audit. `airflow db 
clean --tables rendered_task_instance_fields` fails with `No tables selected 
for db cleanup`.
   
   ### What changed
   
   This adds support for cleaning child tables that lack an independent recency 
timestamp by allowing `_TableConfig` to join a parent recency table on primary 
key columns. `rendered_task_instance_fields` is configured to join 
`task_instance` on composite keys `(dag_id, task_id, run_id, map_index)` and 
filter by `task_instance.start_date`, enabling independent RTIF pruning while 
retaining task instances.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes
   
   Generated-by: GitHub Copilot CLI following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   
   ---
   
   ### Testing Done
   
   #### Before (main branch)
   
   Attempting to clean `rendered_task_instance_fields` on the main branch fails 
because the table is unregistered:
   
   ```bash
   airflow db clean --tables rendered_task_instance_fields 
--clean-before-timestamp 2026-01-01 -y
   ```
   
   ```
   SystemExit: No tables selected for db cleanup. Please choose valid table 
names.
   ```
   
   #### After (this branch)
   
   Executed database setup with 1 task instance and 1 rendered task instance 
field row:
   
   ```bash
   python /tmp/reproduce_rtif_cleanup.py
   ```
   
   ```
   Seed complete: inserted 1 task_instance and 1 rendered_task_instance_fields 
row.
   ```
   
   CLI cleanup purges expired RTIF rows into the archive table while leaving 
parent task instances intact:
   
   ```bash
   airflow db clean --tables rendered_task_instance_fields 
--clean-before-timestamp 2026-01-01 -y
   ```
   
   ```
   Checking table rendered_task_instance_fields
   Found 1 rows meeting deletion criteria.
   Performing Delete...
   Moving data to table 
_airflow_deleted__rendered_task_instance_fields__20260926014710
   Finished Performing Delete
   ```
   
   Inspected post-clean row counts:
   
   ```bash
   python -c "
   from sqlalchemy import func, select
   from airflow.models import TaskInstance
   from airflow.models.renderedtifields import RenderedTaskInstanceFields
   from airflow.utils.session import create_session
   
   with create_session() as session:
       ti_cnt = 
session.scalar(select(func.count(TaskInstance.id)).where(TaskInstance.dag_id == 
'rtif_demo'))
       rtif_cnt = 
session.scalar(select(func.count(RenderedTaskInstanceFields.dag_id)).where(RenderedTaskInstanceFields.dag_id
 == 'rtif_demo'))
       print(f'task_instance remaining: {ti_cnt}')
       print(f'rendered_task_instance_fields remaining: {rtif_cnt}')
   "
   ```
   
   ```
   task_instance remaining: 1
   rendered_task_instance_fields remaining: 0
   ```
   
   <details>
   <summary>Reproducer source: reproduce_rtif_cleanup.py</summary>
   
   ```python
   #!/usr/bin/env python3
   import pendulum
   from airflow.models import DAG, DagModel, DagRun, TaskInstance
   from airflow.models.dag_version import DagVersion
   from airflow.models.dagbundle import DagBundleModel
   from airflow.models.renderedtifields import RenderedTaskInstanceFields
   from airflow.models.serialized_dag import SerializedDagModel
   from airflow.providers.standard.operators.python import PythonOperator
   from airflow.serialization.serialized_objects import LazyDeserializedDAG
   from airflow.utils.session import create_session
   from airflow.utils.types import DagRunType
   
   base_date = pendulum.DateTime(2023, 1, 1, tzinfo=pendulum.timezone("UTC"))
   bundle_name = "dags"
   
   with create_session() as session:
       session.add(DagBundleModel(name=bundle_name))
       session.flush()
   
       dag_id = "rtif_demo"
       dag = DAG(dag_id=dag_id)
       dag.fileloc = __file__
       dm = DagModel(dag_id=dag_id, bundle_name=bundle_name)
       session.add(dm)
       SerializedDagModel.write_dag(LazyDeserializedDAG.from_dag(dag), 
bundle_name=bundle_name)
       dag_version = DagVersion.get_latest_version(dag.dag_id)
   
       dr = DagRun(dag.dag_id, run_id="run_1", run_type=DagRunType.SCHEDULED, 
start_date=base_date)
       ti = TaskInstance(
           PythonOperator(task_id="task_1", python_callable=print),
           run_id=dr.run_id,
           dag_version_id=dag_version.id,
       )
       ti.dag_id = dag.dag_id
       ti.start_date = base_date
       session.add(dr)
       session.add(ti)
       session.flush()
   
       rtif = RenderedTaskInstanceFields(ti=ti, render_templates=False, 
rendered_fields={"sql": "SELECT 1"})
       session.add(rtif)
       session.commit()
       print("Seed complete: inserted 1 task_instance and 1 
rendered_task_instance_fields row.")
   ```
   </details>
   
   ---
   
   * Read the **[Pull Request 
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
 for more information. Note: commit author/co-author name and email in commits 
become permanently public when merged.
   * For fundamental code changes, an Airflow Improvement Proposal 
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
 is needed.
   * When adding dependency, check compliance with the [ASF 3rd Party License 
Policy](https://www.apache.org/legal/resolved.html#category-x).
   * For significant user-facing changes create newsfragment: 
`{pr_number}.significant.rst`, in 
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
 You can add this file in a follow-up commit after the PR is created so you 
know the PR number.
   


-- 
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]

Reply via email to