Skip to content

Remove XCom pickling - #43905

Merged
kaxil merged 1 commit into
apache:mainfrom
astronomer:remove-xcom-pickling
Nov 18, 2024
Merged

Remove XCom pickling#43905
kaxil merged 1 commit into
apache:mainfrom
astronomer:remove-xcom-pickling

Conversation

@kaxil

@kaxilkaxil commented Nov 12, 2024

Copy link
Copy Markdown
Member

XCom pickling was disabled by default in Airflow 2.0.0: https://airflow.apache.org/docs/apache-airflow/1.10.15/configurations-ref.html#enable-xcom-pickling

Discussion

To avoid a time-consuming DB migration, I have not changed the column type of value; it is still LargeBinary/LONGBLOB (MySQL).

As part of Airflow 3, we should strongly recommend users to use the airflow db clean command to prune the DBs to the minimum required. If we assume, most users would do that, we can run the following migration:

Option 1: Try to migrate pickle to JSON

defupgrade():
bind=op.get_bind()
session=Session(bind=bind)
forrowinsession.query(XCom).all():
try:
unpickled_data=pickle.loads(row.value)
row.value=json.dumps(unpickled_data).encode('utf-8')
except (pickle.UnpicklingError, json.JSONDecodeError):
# If unpickling fails, assume it's already JSON and skipcontinueop.alter_column("xcom", "value", type_=sa.Text)

Option 2: Delete XCom rows with pickle

fromairflow.models.xcomimportBaseXComdefupgrade():
bind=op.get_bind()
session=Session(bind=bind)
pickled_xcoms=session.query(BaseXCom).filter(BaseXCom.value.isnot(None))
deleted_count=0forxcominpickled_xcoms:
ifxcom.value.startswith(b'\x80'): # Identify as pickled by protocol marker: https://github.com/python/cpython/blob/494360afd00dc8f6b549f160525c3e86ec14905d/Lib/pickletools.py#L2122-L2133session.delete(xcom)

Option 3: Keep the current column type

Not optimal, but we can keep the current column type to binary/long-blob

Any thoughts?


^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in newsfragments.

@kaxilkaxil added the airflow3.0:breaking Candidates for Airflow 3.0 that contain breaking changes label Nov 12, 2024
@boring-cyborgboring-cyborgBot added the area:API Airflow's REST/HTTP API label Nov 12, 2024
@kaxil
kaxil requested review from eladkal and potiukNovember 12, 2024 01:09
@kaxil
kaxilforce-pushed the remove-xcom-pickling branch 2 times, most recently from 39d2306 to fd62961CompareNovember 12, 2024 01:16
@kaxilkaxil added the legacy api Whether legacy API changes should be allowed in PR label Nov 12, 2024
@kaxil
kaxilforce-pushed the remove-xcom-pickling branch from fd62961 to e6cdc10CompareNovember 12, 2024 01:24
@kaxil
kaxilforce-pushed the remove-xcom-pickling branch from e6cdc10 to f57006eCompareNovember 12, 2024 11:33
@pgagnon

Copy link
Copy Markdown
Contributor

@kaxil

Option 1: Try to migrate pickle to JSON

# If unpickling fails, assume it's already JSON and skip

If JSON decoding fails, maybe we could save the value in a backup blob field (to be deprecated and removed in a specified future version).

While XComs are generally not that important I feel it might be better to soft-delete wherever there's a risk of data loss as a general practice.

@jscheffl

jscheffl commented Nov 12, 2024

Copy link
Copy Markdown
Contributor

I like the drop of the pickle type - but I'd favor not keeping this as blob/binary in the DB. Then we can not use any DB feature to efficiently "use" the data other than a blob.

If not in this PR, can we have a follow-up that converts the data into json data type as described in https://www.postgresql.org/docs/17/datatype-json.html ?

(MySQL is: https://dev.mysql.com/doc/refman/8.4/en/json.html)

And totally forgot about the vote: I think we can also consider to provide an offline migration tool which we request to be executed by everybody manually prior upgrade such that we don't need to carry complex inline migration. Everybody who uses XCom pickling should know this from the config. So Option 1 or Option 1a (a=offline tool)

@hussein-awalahussein-awala left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

+1

@kaxilkaxil added this to the Airflow 3.0.0 milestone Nov 13, 2024
@kaxil
kaxilforce-pushed the remove-xcom-pickling branch from f57006e to da0e059CompareNovember 18, 2024 15:58
@kaxil

Copy link
Copy Markdown
MemberAuthor

I will create a separate PR to handle the migration so that can be reviewed independently -- will have a PR by EOD today

@kaxil
kaxilforce-pushed the remove-xcom-pickling branch from da0e059 to 234406bCompareNovember 18, 2024 16:30
@kaxil
kaxilforce-pushed the remove-xcom-pickling branch from 234406b to fc0c60bCompareNovember 18, 2024 17:06
@kaxil
kaxil merged commit 6faa720 into apache:mainNov 18, 2024
@kaxil
kaxil deleted the remove-xcom-pickling branch November 18, 2024 17:44
@kaxil

Copy link
Copy Markdown
MemberAuthor

PR created: #44166

kandharvishnu pushed a commit to kandharvishnu/airflow that referenced this pull request Nov 19, 2024
@kaxilkaxil mentioned this pull request Nov 19, 2024
2 tasks
kaxil added a commit that referenced this pull request Nov 19, 2024
follow-up of #43905
Changes:
- Changed `XCom.value` column to JSON for all dbs.
- Archived pickled XCom data to `_xcom_archive` and removed it from the `xcom` table.
- Removed encoded string in XCom serialization and deserialization logic.
- Updated logic for `XComObjectStorageBackend` to make it compatible for AF 2 & 3
kosteev pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request May 27, 2025
follow-up of apache/airflow#43905
Changes:
- Changed `XCom.value` column to JSON for all dbs.
- Archived pickled XCom data to `_xcom_archive` and removed it from the `xcom` table.
- Removed encoded string in XCom serialization and deserialization logic.
- Updated logic for `XComObjectStorageBackend` to make it compatible for AF 2 & 3
GitOrigin-RevId: 86c4c6fa9decd626cdea14aaf79d76252d4d7145
kosteev pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Sep 23, 2025
follow-up of apache/airflow#43905
Changes:
- Changed `XCom.value` column to JSON for all dbs.
- Archived pickled XCom data to `_xcom_archive` and removed it from the `xcom` table.
- Removed encoded string in XCom serialization and deserialization logic.
- Updated logic for `XComObjectStorageBackend` to make it compatible for AF 2 & 3
GitOrigin-RevId: 86c4c6fa9decd626cdea14aaf79d76252d4d7145
kosteev pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Oct 20, 2025
follow-up of apache/airflow#43905
Changes:
- Changed `XCom.value` column to JSON for all dbs.
- Archived pickled XCom data to `_xcom_archive` and removed it from the `xcom` table.
- Removed encoded string in XCom serialization and deserialization logic.
- Updated logic for `XComObjectStorageBackend` to make it compatible for AF 2 & 3
GitOrigin-RevId: 86c4c6fa9decd626cdea14aaf79d76252d4d7145
kosteev pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Feb 26, 2026
follow-up of apache/airflow#43905
Changes:
- Changed `XCom.value` column to JSON for all dbs.
- Archived pickled XCom data to `_xcom_archive` and removed it from the `xcom` table.
- Removed encoded string in XCom serialization and deserialization logic.
- Updated logic for `XComObjectStorageBackend` to make it compatible for AF 2 & 3
GitOrigin-RevId: 86c4c6fa9decd626cdea14aaf79d76252d4d7145
kosteev pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Jul 27, 2026
follow-up of apache/airflow#43905
Changes:
- Changed `XCom.value` column to JSON for all dbs.
- Archived pickled XCom data to `_xcom_archive` and removed it from the `xcom` table.
- Removed encoded string in XCom serialization and deserialization logic.
- Updated logic for `XComObjectStorageBackend` to make it compatible for AF 2 & 3
GitOrigin-RevId: 86c4c6fa9decd626cdea14aaf79d76252d4d7145
kosteev pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Jul 29, 2026
follow-up of apache/airflow#43905
Changes:
- Changed `XCom.value` column to JSON for all dbs.
- Archived pickled XCom data to `_xcom_archive` and removed it from the `xcom` table.
- Removed encoded string in XCom serialization and deserialization logic.
- Updated logic for `XComObjectStorageBackend` to make it compatible for AF 2 & 3
GitOrigin-RevId: 86c4c6fa9decd626cdea14aaf79d76252d4d7145
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

airflow3.0:breakingCandidates for Airflow 3.0 that contain breaking changesarea:APIAirflow's REST/HTTP APIlegacy apiWhether legacy API changes should be allowed in PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@kaxil@pgagnon@jscheffl@potiuk@pierrejeambrun@hussein-awala@vincbeck