Sub topic 10
When to use Change Data Feed instead
Time travel shows you what a table looked like at one point. Change Data Feed (CDF) shows you what changed between two points, row by row. This chapter explains how they differ and how to choose between them.
This chapter assumes you have read the earlier chapters on DESCRIBE HISTORY, VERSION AS OF, and VACUUM. You should also be comfortable with basic PySpark.
What time travel does not give you
Earlier in this tutorial you found changed rows by comparing two versions with EXCEPT. That works for a quick audit, but it has limits:
- Both versions are scanned in full, so the cost grows with table size.
EXCEPTreturns rows that exist in one version but not the other. It does not label a row as inserted, updated, or deleted.- An updated row shows up as a new row, with nothing that links it to its old values.
- A downstream job that needs only the changes would have to repeat this comparison on every run.
CDF solves these problems. Delta records row-level changes as part of each commit, so a reader can ask for them directly.
Enable the change feed
CDF is off by default. Turn it on with a table property.
ALTER TABLE main.sales.orders
SET TBLPROPERTIES (delta.enableChangeDataFeed = true);The feed only covers commits made after you enable it. It is not retroactive, so you cannot use it to recover changes from before that point. Time travel can still read the older versions.
Read the changes
In SQL, use the table_changes function with a start and end version. Both bounds are inclusive. Suppose the property was enabled in version 6, so version 7 is the first commit with change data.
SELECT order_id, status, _change_type, _commit_version, _commit_timestamp
FROM table_changes('main.sales.orders', 7, 9)
ORDER BY _commit_version;In PySpark, set the readChangeFeed option.
changes = (
spark.read
.option("readChangeFeed", "true")
.option("startingVersion", 7)
.option("endingVersion", 9)
.table("main.sales.orders")
)
changes.select(
"order_id", "status", "_change_type", "_commit_version", "_commit_timestamp"
).show()You can also use startingTimestamp and endingTimestamp instead of versions. The query adds three metadata columns to the table's own columns:
| Column | Meaning |
|---|---|
| `_change_type` | One of `insert`, `update_preimage`, `update_postimage`, or `delete` |
| `_commit_version` | The table version that made the change |
| `_commit_timestamp` | The time of that commit |
An update produces two rows. The update_preimage row holds the values before the change, and the update_postimage row holds the values after it. This pairing is what EXCEPT cannot give you.
Choosing between the two
The table below summarizes the differences.
| Question | Time travel | Change Data Feed |
|---|---|---|
| What does it return? | A full snapshot of the table | Only the rows that changed |
| Does it label change types? | No | Yes, through `_change_type` |
| Can it read the past before setup? | Yes, within retention | No, only after enabling |
| Can it undo a change? | Yes, with `RESTORE` | No, it only reports changes |
| Best use | Audit, debugging, rollback | Incremental pipelines, change auditing, syncing to other systems |
Use time travel when you need to see the whole table as it was, or to roll back. Use CDF when a downstream process needs to know exactly which rows were added, changed, or removed. Examples are updating a summary table, sending changes to another system, or keeping an audit trail of updates.
Retention still applies
The change data lives in the table's storage and follows the same retention rules as other data files. VACUUM removes change data files that fall outside the retention period. If you ask for a version range whose files are gone, the read fails. A pipeline that reads the feed should therefore run more often than your retention period, or it will miss changes.
The feed also adds some storage and write cost, because Delta writes extra change files for operations such as UPDATE, DELETE, and MERGE. Enable it only on tables where someone will read it.
Exercise
A nightly job must update a daily summary table using only the rows that changed in main.sales.orders since its last run. The table's change feed was enabled three weeks ago, and the job has run every night since. Would you use time travel with EXCEPT or Change Data Feed, and why?
Show answer
Use Change Data Feed. The job needs only the changed rows, and table_changes (or readChangeFeed) returns them with _change_type labels, including both the before and after values of updates. EXCEPT would scan two full snapshots on every run and would not tell you whether a row was inserted, updated, or deleted. CDF works here because the feed has been on for the whole period and the nightly job runs more often than the retention period.
Exercise
You enabled the change feed today, and you now want to know which rows changed last month. Can table_changes answer that? What can you use instead?
Show answer
No. The change feed only records commits made after it was enabled. For last month, use time travel: compare a version from before the period with a version from after it, for example with EXCEPT, as long as the versions are still within log and file retention.