Skip to content

Conversation

@kaxil
Copy link
Member

@kaxil kaxil commented Jan 22, 2026

The XComModel.clear() method was loading all matching XCom records into memory and deleting them one by one:

# Before: Load all records, delete one by one
for xcom in session.scalars(query):
    session.delete(xcom)

For tasks with many XCom entries (e.g., mapped tasks with many keys), this caused:

  • Unnecessary memory usage (loading all XCom objects into Python)
  • N+1 DELETE statements (one per record)

This change uses a single bulk DELETE statement:

# After: Single bulk delete
session.execute(delete_stmt)

This is both more memory-efficient and faster.

The clear() method was loading all matching XCom records into memory
and deleting them one by one. For tasks with many XCom entries, this
caused unnecessary memory usage and N+1 DELETE statements.

This change uses a single bulk DELETE statement which is both more
memory-efficient and faster.
@kaxil kaxil requested review from XD-DENG and ashb as code owners January 22, 2026 21:09
@kaxil kaxil added this to the Airflow 3.1.7 milestone Jan 22, 2026
@kaxil kaxil changed the title Use bulk DELETE for XComModel.clear() instead of loading records Use bulk DELETE for XComModel.clear() instead of loading records Jan 22, 2026
@jedcunningham jedcunningham requested a review from Copilot January 22, 2026 21:29
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 PR optimizes the XComModel.clear() method by replacing inefficient record-by-record deletion with a single bulk DELETE operation, improving both memory usage and performance for tasks with many XCom entries.

Changes:

  • Replaced select() + loop-based deletion with a single delete() statement
  • Removed commented-out debug print statement
  • Maintained all existing filtering logic (dag_id, task_id, run_id, map_index)

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

@kaxil kaxil merged commit b651403 into apache:main Jan 22, 2026
71 checks passed
@kaxil kaxil deleted the fix-xcom-clear-bulk-delete branch January 22, 2026 22:03
suii2210 pushed a commit to suii2210/airflow that referenced this pull request Jan 26, 2026
…che#60955)

The clear() method was loading all matching XCom records into memory
and deleting them one by one. For tasks with many XCom entries, this
caused unnecessary memory usage and N+1 DELETE statements.

This change uses a single bulk DELETE statement which is both more
memory-efficient and faster.
ephraimbuddy pushed a commit that referenced this pull request Jan 27, 2026
)

The clear() method was loading all matching XCom records into memory
and deleting them one by one. For tasks with many XCom entries, this
caused unnecessary memory usage and N+1 DELETE statements.

This change uses a single bulk DELETE statement which is both more
memory-efficient and faster.

(cherry picked from commit b651403)
@ephraimbuddy ephraimbuddy added the type:misc/internal Changelog: Misc changes that should appear in change log label Jan 28, 2026
shreyas-dev pushed a commit to shreyas-dev/airflow that referenced this pull request Jan 29, 2026
…che#60955)

The clear() method was loading all matching XCom records into memory
and deleting them one by one. For tasks with many XCom entries, this
caused unnecessary memory usage and N+1 DELETE statements.

This change uses a single bulk DELETE statement which is both more
memory-efficient and faster.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:misc/internal Changelog: Misc changes that should appear in change log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants