Skip to content
Silver 5 min read · Delta 3 practice problems ↓

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

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

PySparkSpark SQL
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

ColumnMeaning
Table columnsThe row's values
_change_typeinsert, update_preimage, update_postimage or delete
_commit_versionThe table version of the change
_commit_timestampWhen that version was committed

customers, version 5 → 6

idtier
1silver
2gold
3free

changes in version 6

idtier_change_type
1silverupdate_preimage
1goldupdate_postimage
3freedelete
4trialinsert

Version 6 upgraded customer 1, deleted customer 3 and added customer 4. Customer 2 did not change and does not appear.

Reading changes

PySparkSpark SQL
# 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

  1. 1
    Read 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 →).
  2. 2
    Drop update_preimage rows unless you need them (for example to subtract old values from aggregates).
  3. 3
    Keep the latest change per key within the batch: a row can change several times between runs.
  4. 4
    MERGE 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.
Note: CDF data follows the table's retention: VACUUMVACUUM: The command that deletes data files no longer in the table, once they are older than the retention period. Learn more → removes old change files too. Consumers must keep up within the retention window or restart from a full snapshot.

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.

Production patternPySpark · Read the change feed incrementally, from 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?"

Enable Change Data Feed on silver, stream or batch-read changes since the last version, filter out pre-images, keep the latest change per key by _commit_version, then MERGE into gold with delete for _change_type = delete and upsert otherwise. Mention retention limits and idempotency via checkpoints.

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?"
Track the last version applied in a control table (or use a streaming read with a checkpoint). Apply changes in one atomic MERGE, and update the control table only after it succeeds. On rerun, start again from 101; MERGE logic that takes the latest change per key makes reprocessing safe.
Trap"CDF can be read for any version of the table."
Only versions after CDF was enabled, and only while change files are retained; VACUUM removes them like other files.

Check yourself

3 questions

How 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 read
Partitioning done right
When to partition a table, how to choose the column, and how over-partitioning backfires.

Related lessons

Previous: Slowly changing dimensions (SCD Type 2)

Primary sources: Delta: change data feed