Change Data Feed
Downstream tables usually need only what changed since their last run, including updates and deletes, not just new files. Delta's Change Data Feed records every changed row with what happened to it. This lesson shows how to turn it on, read it, and use it for incremental pipelines.
Step 3 · Data lake & lakehouse · Silver · Changing data, lesson 3 of 3
On this page
Show code in
Every code block on the page follows this.
Read first: MERGE INTO and upserts · The Delta transaction log (skip if you know them)
In plain words first
Instead of re-reading a whole contact list every night to spot what changed, keep a diary: "Monday, Asha moved to Pune; Tuesday, Ravi deleted". Tomorrow you only read the new diary entries.
The Change Data Feed is that diary for a Delta table: one row per inserted, updated or deleted row, with the version it happened in.
Why it exists
Downstream tables built from a source table usually need only the rows that changed since the last run. Without row-level changes, you either reprocess everything or compare snapshotssnapshot: A complete, consistent version of a table at one moment. Readers always see one snapshot, never a half-finished write. Learn more →, both expensive. Reading appended files alone misses updates and deletes. Change Data Feed (CDF) gives you exact row changes.
Enabling it
spark.sql("ALTER TABLE customers SET TBLPROPERTIES (delta.enableChangeDataFeed = true)") # or at creation spark.sql(""" CREATE TABLE customers (...) TBLPROPERTIES (delta.enableChangeDataFeed = true) """)
ALTER TABLE customers SET TBLPROPERTIES (delta.enableChangeDataFeed = true); -- or at creation CREATE TABLE customers (...) TBLPROPERTIES (delta.enableChangeDataFeed = true);
Changes are recorded only from the version where it was enabled onward. For inserts and whole-file deletes, Delta derives changes from the log; for updates, merges and partial deletes, it writes extra change files in a _change_data folder.
What you get back
| Column | Meaning |
|---|---|
| Table columns | The row's values |
_change_type | insert, update_preimage, update_postimage or delete |
_commit_version | The table version of the change |
_commit_timestamp | When that version was committed |
customers, version 5 → 6
| id | tier |
|---|---|
| 1 | silver |
| 2 | gold |
| 3 | free |
changes in version 6
| id | tier | _change_type |
|---|---|---|
| 1 | silver | update_preimage |
| 1 | gold | update_postimage |
| 3 | free | delete |
| 4 | trial | insert |
Version 6 upgraded customer 1, deleted customer 3 and added customer 4. Customer 2 did not change and does not appear.
Reading changes
# Batch: changes in versions 5 through 10 changes = (spark.read.option("readChangeFeed", "true") .option("startingVersion", 5).option("endingVersion", 10) .table("customers")) # Streaming: continuously, from where the checkpoint left off stream = (spark.readStream.option("readChangeFeed", "true") .option("startingVersion", 5).table("customers"))
SELECT * FROM table_changes('customers', 5, 10); SELECT * FROM table_changes('customers', '2025-03-01 00:00:00');
Using it downstream
- 1Read changes since the last processed version (stored in your job state, or tracked by a streaming checkpointcheckpoint: In streaming, the folder where Spark records which input it has processed and its state, so a restarted query continues where it stopped. Learn more →).
- 2Drop
update_preimagerows unless you need them (for example to subtract old values from aggregates). - 3Keep the latest change per key within the batch: a row can change several times between runs.
- 4MERGE the result into the target: delete on
delete, upsertupsert: Update a row if its key exists, insert it if not. Done with MERGE. Learn more → otherwise.
Iceberg offers a similar capability through changelog views (create_changelog_view procedure in Spark), computed from snapshots.
Try it yourself
Each example says what it shows, then the output from a real run and what to notice in it, so you can follow without running anything. To try one yourself, paste it into a local Spark session with Delta Lake, with spark already created: each builds its own data.
Goal: Read only what changed since the last version you processed.
import os path = os.path.abspath("lab/cdf/customers") spark.sql(f"""CREATE OR REPLACE TABLE delta.`{path}` (id INT, city STRING) USING delta TBLPROPERTIES (delta.enableChangeDataFeed = true)""") # v1 spark.sql(f"INSERT INTO delta.`{path}` VALUES (1, 'Pune'), (2, 'Delhi'), (3, 'Goa')") # v2 spark.sql(f"UPDATE delta.`{path}` SET city = 'Mumbai' WHERE id = 1") # v3 spark.sql(f"DELETE FROM delta.`{path}` WHERE id = 3") last_processed = 1 # kept in a control table between runs changes = (spark.read.format("delta") .option("readChangeFeed", "true") .option("startingVersion", last_processed + 1) .load(path)) # keep inserts, new values, deletes (changes.where("_change_type != 'update_preimage'") .select("id", "city", "_change_type", "_commit_version") .orderBy("_commit_version", "id") .show())
Output from a real run
+---+------+----------------+---------------+ | id| city| _change_type|_commit_version| +---+------+----------------+---------------+ | 1|Mumbai|update_postimage| 2| | 3| Goa| delete| 3| +---+------+----------------+---------------+
What to notice:
- Starting after version 1, two changes came back: customer 1's new city (version 2) and customer 3's deletion (version 3).
- Customer 2 is not there: it did not change.
- To apply these downstream, take the latest change per key, MERGE it, and record version 3 as processed.
Common mistakes
- Expecting changes before CDF was enabled — It records changes only from that version on.
- Ignoring multiple changes per key in a batch — Apply only the latest, or ordering bugs appear.
- Falling behind retention — Vacuumed change files are gone; plan a full reload path.
Key takeaways
- CDF records row-level inserts, updates and deletes per commit.
- Enable it with delta.enableChangeDataFeed; changes start from then.
- Read with readChangeFeed or table_changes; columns include _change_type and _commit_version.
- Downstream: latest change per key, then MERGE.
Interview prep
THE QUESTION
"How would you incrementally propagate updates and deletes from a silver Delta table to a gold table?"
Avoid: reading the whole silver table every day and diffing it. CDF gives you exactly the changed rows.
Follow-ups · answer out loud, then open
Follow-up"What are the _change_type values?"
insert, delete, update_preimage (the row before) and update_postimage (the row after). For propagating changes you usually keep postimages and deletes, and drop preimages.Scenario"A gold job read changes from version 100 to 150, then failed. How do you make the next run correct?"
Trap"CDF can be read for any version of the table."
Check yourself
3 questionsHow is an update represented in the change feed?
Show the answer
Two rows: update_preimage and update_postimage. Updates produce before and after images.
Can you read changes from before CDF was enabled?
Show the answer
No, only from the version where it was enabled. Changes are captured from enablement onward.
Which SQL function reads the change feed?
Show the answer
table_changes. table_changes(table, start, end) returns row changes.
Practise this
Problems that use this, graded on hidden tests
Keep going
Up next · new module: Keeping tables fast · 6 min readPartitioning done right
When to partition a table, how to choose the column, and how over-partitioning backfires.
Related lessons
MERGE INTO and upsertsData lake & lakehouse · 6 min read
The medallion architectureData lake & lakehouse · 5 min read
Time travel and RESTORE
Previous: Slowly changing dimensions (SCD Type 2)
Primary sources: Delta: change data feed