---
title: Advanced AUTO CDC topics
description: Advanced AUTO CDC topics in Lakeflow pipelines, including editing and reading CDC-produced data, and understanding advanced configuration options.
canonical_url: https://docs.databricks.com/aws/en/ldp/cdc-advanced
---

# Advanced AUTO CDC topics

Beyond the basic `AUTO CDC` and `AUTO CDC FROM SNAPSHOT` APIs, you can run DML on target tables, read change data feeds from CDC targets, monitor processing metrics, and apply partial updates. For bitemporal storage, see [Bitemporal AUTO CDC](https://docs.databricks.com/aws/en/ldp/cdc-bitemporal.md). For an introduction to the `AUTO CDC` APIs, see [The AUTO CDC APIs: Simplify change data capture with pipelines](https://docs.databricks.com/aws/en/ldp/cdc.md).

## <a id="streaming-tables-dml-cdc" />Add, change, or delete data in a target streaming table

If your pipeline publishes tables to Unity Catalog, you can use [data manipulation language](https://docs.databricks.com/aws/en/sql/language-manual/index.md#dml-statements) (DML) statements, including insert, update, delete, and merge statements, to modify the target streaming tables created by `AUTO CDC ... INTO` statements.

> [!NOTE]
>
> * DML statements that modify the table schema of a streaming table are not supported. Ensure that your DML statements do not attempt to evolve the table schema.
> * DML statements that update a streaming table can be run only in a shared Unity Catalog cluster or a SQL warehouse using Databricks Runtime 13.3 LTS and above.
> * Because streaming requires append-only data sources, if your processing requires streaming from a source streaming table with changes (for example, by DML statements), set the [skipChangeCommits flag](https://docs.databricks.com/aws/en/ldp/load.md#ignore-changes) when reading the source streaming table. When `skipChangeCommits` is set, transactions that delete or modify records on the source table are ignored. If your processing does not require a streaming table, you can use a materialized view (which does not have the append-only restriction) as the target table.

Because the pipeline uses a specified `SEQUENCE BY` column and propagates appropriate sequencing values to the `__START_AT` and `__END_AT` columns of the target table (for SCD Type 2), you must ensure that DML statements use valid values for these columns to maintain the proper ordering of records. See [How AUTO CDC works](https://docs.databricks.com/aws/en/ldp/cdc.md#how-is-cdc-implemented).

For more information about using DML statements with streaming tables, see [Add, change, or delete data in a streaming table](https://docs.databricks.com/aws/en/ldp/unity-catalog.md#streaming-tables-dml-statements).

The following example inserts an active record with a start sequence of 5:

```sql
INSERT INTO my_streaming_table (id, name, __START_AT, __END_AT) VALUES (123, 'John Doe', 5, NULL);
```

> [!TIP]
>
> If you need to rename the `__START_AT` and `__END_AT` columns in your SCD Type 2 target table (for example, to match downstream schema requirements), create a view over the target table:
>
> ```sql
> CREATE VIEW my_employees_view AS
> SELECT
>   *,
>   __START_AT AS valid_from,
>   __END_AT AS valid_to
> FROM my_scd2_target_table;
> ```

## <a id="cdf-from-applychanges-target" />Read a change data feed from an AUTO CDC target table

In Databricks Runtime 15.2 and above, you can read a change data feed from a streaming table that is the target of `AUTO CDC` or `AUTO CDC FROM SNAPSHOT` queries in the same way that you read a change data feed from other Delta tables. The following are required to read the change data feed from a target streaming table:

* The target streaming table must be published to Unity Catalog. See [Use Unity Catalog with pipelines](https://docs.databricks.com/aws/en/ldp/unity-catalog.md).
* To read the change data feed from the target streaming table, you must use Databricks Runtime 15.2 or above. To read the change data feed in a different pipeline, the pipeline must be configured to use Databricks Runtime 15.2 or above.

You read the change data feed from a target streaming table that was created in a Lakeflow pipeline the same way as reading a change data feed from other Delta tables. To learn more about using the Delta change data feed functionality, including examples in Python and SQL, see [Use change data feed on Databricks](https://docs.databricks.com/aws/en/tables/features/change-data-feed.md).

> [!NOTE]
>
> The change data feed record includes [metadata](https://docs.databricks.com/aws/en/tables/features/change-data-feed.md#cdf-metadata) identifying the type of change event. When a record is updated in a table, the metadata for the associated change records typically includes `_change_type` values set to `update_preimage` and `update_postimage` events.
>
> However, the `_change_type` values are different if updates are made to the target streaming table that include changing primary key values. When changes include updates to primary keys, the `_change_type` metadata fields are set to `insert` and `delete` events. Changes to primary keys can occur when manual updates are made to one of the key fields with an `UPDATE` or `MERGE` statement or, for SCD type 2 tables, when the `__start_at` field changes to reflect an earlier start sequence value.
>
> The `AUTO CDC` query determines the primary key values, which differ for SCD type 1 and SCD type 2 processing:
>
> | SCD Type                                       | Primary key                                                                                                                                                                                                                                                                                                                                             |
> | ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
> | SCD type 1, and the pipelines Python interface | The primary key is the value of the `keys` parameter in the `create_auto_cdc_flow()` function. For the SQL interface the primary key is the columns defined by the `KEYS` clause in the `AUTO CDC ... INTO` statement.                                                                                                                                  |
> | SCD type 2                                     | The primary key is the `keys` parameter or `KEYS` clause plus the return value from the `coalesce(__START_AT, __END_AT)` operation, where `__START_AT` and `__END_AT` are the corresponding columns from the target streaming table. This uses `__START_AT` when available, and `__END_AT` when `__START_AT` is null (for example, the initial record). |

## <a id="cdf-from-mv" />Read a change data feed from a materialized view

> [!NOTE]
> **Beta**
>
> This feature is in [Beta](https://docs.databricks.com/aws/en/release-notes/release-types.md).

You can read a change data feed from a materialized view created in a Lakeflow pipeline or in Databricks SQL. Use this to replicate materialized view changes to destinations outside Databricks, or to keep a history of materialized view changes for auditing and reporting.

Materialized views use automatic change data feed, so you do not turn on the change data feed itself. Instead, you enable the change data feed on each materialized view that you need it on by meeting the following requirements. See [Automatic change data feed](https://docs.databricks.com/aws/en/tables/features/change-data-feed.md#auto-cdf).

* To read the change data feed, you must use Databricks Runtime 18 LTS or above, on classic compute, serverless compute, or Databricks SQL.

* The materialized view, the pipeline that creates it, or the pipeline that reads it must use the `PREVIEW` channel.

* The materialized view must have row tracking enabled. Materialized views on serverless compute have row tracking enabled by default. See [Row tracking in Databricks](https://docs.databricks.com/aws/en/tables/features/row-tracking.md). To check whether row tracking is enabled on a materialized view, run:

  ```sql
  SHOW TBLPROPERTIES my_mv ('delta.enableRowTracking');
  ```

* To read the change data feed from a materialized view, enable the external metadata flag on the pipeline or the materialized view. For instructions, see [How to enable access for a dataset](https://docs.databricks.com/aws/en/external-access/external-for-pipelines.md#enable).

You read the change data feed from a materialized view the same way as from other Delta tables, using the `table_changes()` function, a streaming read, or the `readChangeFeed` option. For syntax and examples in SQL and Python, see [Use change data feed on Databricks](https://docs.databricks.com/aws/en/tables/features/change-data-feed.md).

You can read a materialized view change data feed from inside a Databricks SQL materialized view or streaming table:

```sql
CREATE OR REFRESH STREAMING TABLE sales
  AS SELECT * FROM STREAM my_mv WITH (readChangeFeed=true)
```

### Limitations

In addition to the [automatic change data feed limitations](https://docs.databricks.com/aws/en/tables/features/change-data-feed.md#limitations), the following apply when you read a change data feed from a materialized view:

* The change data feed includes unchanged rows when the materialized view is fully rewritten, and it does not consolidate multiple updates to the same row into a single event. To filter these out, aggregate the change data feed by grouping on all columns to find inserts and deletes that share the same row values.
* Only Databricks can query the change data feed for a materialized view. External Delta Lake and Iceberg clients cannot.
* Within Lakeflow pipelines, you can read a materialized view change data feed only from a different pipeline, and that pipeline must use the `PREVIEW` channel. Reading the change data feed of a materialized view in the same pipeline that creates it is not supported.
* You cannot create a vector search index from a materialized view.

## Get data about records processed by a CDC query in pipelines

> [!NOTE]
>
> The following metrics are captured only by `AUTO CDC` queries and not by `AUTO CDC FROM SNAPSHOT` queries.

The following metrics are captured by `AUTO CDC` queries:

* **`num_upserted_rows`**: The number of output rows upserted into the dataset during an update.
* **`num_deleted_rows`**: The number of existing output rows deleted from the dataset during an update.

The `num_output_rows` metric, output for non-CDC flows, is not captured for `AUTO CDC` queries.

## <a id="partial-updates" />Apply partial updates

When a source sends only the columns that changed, `AUTO CDC` must distinguish between a column that is absent from a change record, which should leave the target value unchanged, and a column that is explicitly set to `null`, which should overwrite the target value with `null`. By default, `IGNORE NULL UPDATES` treats every `null` as a "do not update" marker, so it cannot apply an explicit `null`. To resolve this ambiguity, choose one of the following three methods:

| Method                                               | When to use                                                                                                      | Behavior                                                                                                                                                                                                            |
| ---------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `IGNORE NULL UPDATES ON (columnList)`                | A small, fixed set of columns should ignore `null` values, while all other columns apply explicit `null` values. | The listed columns keep their existing target value when the incoming value is `null`. All other columns apply explicit `null` values.                                                                              |
| `IGNORE NULL UPDATES ON * EXCEPT (exceptColumnList)` | Most columns should ignore `null` values, and only a few should apply explicit `null` values.                    | The listed columns apply explicit `null` values. All other columns keep their existing target value when the incoming value is `null`.                                                                              |
| `COLUMNS TO UPDATE`                                  | Each change record updates a different set of columns, or the set of updatable columns changes over time.        | A source column names the columns to update for each change record. The listed columns are written from the source, including explicit `null` values. Columns that are not listed keep their existing target value. |

`COLUMNS TO UPDATE` cannot be combined with `IGNORE NULL UPDATES`, and is not supported for bitemporal tables.

As a rule of thumb, choose `COLUMNS TO UPDATE` when the producer knows which columns changed in each record and can carry that information in a source column, such as when multiple producers write to the same source or the set of updatable columns grows over time. Choose `IGNORE NULL UPDATES ON` when the pipeline owner knows the fixed set of updatable columns in advance and prefers to control them in pipeline code.

The following example uses a source column named `columnsToUpdate` to control which columns each change record updates, including columns set explicitly to `null`:

### Python

```python
from pyspark import pipelines as dp

dp.create_streaming_table("target")

dp.create_auto_cdc_flow(
  target = "target",
  source = "cdc_source",
  keys = ["id"],
  sequence_by = "sequenceNum",
  stored_as_scd_type = 1,
  columns_to_update = "columnsToUpdate"
)
```

### SQL

```sql
CREATE OR REFRESH STREAMING TABLE target;

CREATE FLOW apply_cdc AS AUTO CDC INTO
  target
FROM
  stream(cdc_source)
KEYS
  (id)
SEQUENCE BY
  sequenceNum
STORED AS
  SCD TYPE 1
COLUMNS TO UPDATE
  columnsToUpdate;
```

For the full parameter reference, see [AUTO CDC INTO (pipelines)](https://docs.databricks.com/aws/en/ldp/developer/ldp-sql-ref-apply-changes-into.md) and [create\_auto\_cdc\_flow](https://docs.databricks.com/aws/en/ldp/developer/ldp-python-ref-apply-changes.md).

## <a id="cdc-objects" />What data objects are used for CDC processing in a pipeline?

When you declare the target table in the Hive metastore, two data structures are created:

* A view using the name assigned to the target table.
* An internal backing table used by the pipeline to manage CDC processing. This table is named by prepending `__apply_changes_storage_` to the target table name.

For example, if you declare a target table named `dp_cdc_target`, you see a view named `dp_cdc_target` and a table named `__apply_changes_storage_dp_cdc_target` in the metastore. Query the view to access the processed data. Do not modify the backing table directly.

> [!NOTE]
>
> These data structures apply only to `AUTO CDC` processing, not `AUTO CDC FROM SNAPSHOT` processing. They also apply only to Hive metastore, not Unity Catalog.
