Change Data Capture in Snowflake Using Streams and Tasks

Change Data Capture in Snowflake Using Streams and Tasks: A Practical Guide

Celestinfo Software Solutions Pvt. Ltd. • Feb 27, 2025

Last updated: October 2026

Quick answer: Snowflake Streams track row-level changes (inserts, updates, deletes) on a source table. Tasks execute SQL on a schedule, or as triggered tasks whenever a stream has new data. Together, they form a native CDC pipeline in which the Stream captures what changed and the Task processes those changes into a target table, with no external orchestrator required.

What Streams Are and Why They Exist


A Stream is a change-tracking object attached to a table. It doesn't store a copy of the data. Instead, it records an offset - a pointer to where you last consumed changes. When you query the Stream, Snowflake compares the current table state against that offset and returns the delta: every row that was inserted, updated, or deleted since your last read.


Each row returned by a Stream includes three metadata columns:


ColumnValuesMeaning
METADATA$ACTIONINSERT or DELETEThe type of change. Updates appear as a DELETE + INSERT pair.
METADATA$ISUPDATETRUE or FALSETRUE when the INSERT/DELETE pair represents an UPDATE.
METADATA$ROW_IDStringA unique identifier for the row, stable across changes.

The UPDATE representation is the part that trips people up. Snowflake doesn't have a native UPDATE action in Streams. Instead, an update to a row shows up as two rows: one with METADATA$ACTION = 'DELETE' (the old values) and one with METADATA$ACTION = 'INSERT' (the new values), both with METADATA$ISUPDATE = TRUE. You filter on these columns in your merge logic.


Stream Types


Snowflake offers three Stream types, each tracking different change categories:



Streams are not limited to tables. Views, including secure views, directory tables, dynamic tables, Iceberg tables and event tables can all have streams.


SQL - Creating Streams
-- Standard stream (full CDC)
CREATE OR REPLACE STREAM orders_stream ON TABLE raw.orders;

-- Append-only stream (inserts only)
CREATE OR REPLACE STREAM events_stream ON TABLE raw.click_events
  APPEND_ONLY = TRUE;

Tasks: Scheduled SQL Inside Snowflake


A Task is a Snowflake object that runs a SQL statement on a schedule. The schedule can be an interval, such as '5 MINUTES' or even '10 SECONDS', or a cron expression with USING CRON. Tasks run on a warehouse you name, or on serverless compute when you leave WAREHOUSE out. Resuming a task requires the account-level EXECUTE TASK privilege, and serverless tasks also need EXECUTE MANAGED TASK, so grant these to the role that owns your pipelines.


SQL - Creating a Task
-- Run every 5 minutes
CREATE OR REPLACE TASK process_orders_task
  WAREHOUSE = etl_wh
  SCHEDULE = '5 MINUTE'
  WHEN SYSTEM$STREAM_HAS_DATA('orders_stream')
AS
  INSERT INTO analytics.orders_target
  SELECT order_id, customer_id, amount, order_date
  FROM orders_stream
  WHERE METADATA$ACTION = 'INSERT';

The WHEN SYSTEM$STREAM_HAS_DATA('orders_stream') condition is critical. Without it, the Task fires every 5 minutes regardless of whether there's new data, burning warehouse credits for nothing. With it, the Task only runs when the Stream has unconsumed changes. The WHEN check itself is evaluated in the cloud services layer, which carries a small charge, so skipped runs are cheap but not entirely free.


If you don't need a fixed schedule at all, a triggered task leaves out SCHEDULE and runs whenever the stream has data, at most every 30 seconds by default. The USER_TASK_MINIMUM_TRIGGER_INTERVAL_IN_SECONDS parameter can lower that to 10 seconds. A serverless triggered task takes a TARGET_COMPLETION_INTERVAL in place of a warehouse:

SQL - Triggered Task
-- No SCHEDULE: runs when orders_stream has data, on serverless compute
CREATE OR REPLACE TASK process_orders_triggered
  TARGET_COMPLETION_INTERVAL = '5 MINUTES'
  WHEN SYSTEM$STREAM_HAS_DATA('orders_stream')
AS
  INSERT INTO analytics.orders_target
  SELECT order_id, customer_id, amount, order_date
  FROM orders_stream
  WHERE METADATA$ACTION = 'INSERT';

Tasks are created in a suspended state. You must explicitly resume them:

SQL
ALTER TASK process_orders_task RESUME;

Want this running in your own account? We build Snowflake platforms end to end: ingestion, modelling, access control, and the monitoring that tells you when a load drifts. Not slide decks. Not roadmaps. Real working systems you can point at.

Book a Snowflake call Snowflake consulting

Building a Complete CDC Pipeline


Here's the real-world pattern: a source table receives raw data from an ingestion tool, a Stream tracks changes, and a Task processes those changes into a clean target table using MERGE. This handles inserts, updates, and deletes in one operation.


SQL - Complete CDC Pipeline
-- 1. Source table (populated by your ingestion tool)
CREATE OR REPLACE TABLE raw.customers (
  customer_id   INT,
  name          VARCHAR(200),
  email         VARCHAR(200),
  status        VARCHAR(20),
  updated_at    TIMESTAMP_NTZ
);

-- 2. Target table (clean, deduplicated)
CREATE OR REPLACE TABLE analytics.dim_customers (
  customer_id   INT PRIMARY KEY,
  name          VARCHAR(200),
  email         VARCHAR(200),
  status        VARCHAR(20),
  updated_at    TIMESTAMP_NTZ,
  cdc_action    VARCHAR(10),
  cdc_timestamp TIMESTAMP_NTZ DEFAULT CURRENT_TIMESTAMP()
);

-- 3. Stream on source
CREATE OR REPLACE STREAM raw.customers_stream ON TABLE raw.customers;

-- 4. Task that processes changes every 3 minutes
CREATE OR REPLACE TASK analytics.sync_customers_task
  WAREHOUSE = etl_wh
  SCHEDULE = '3 MINUTE'
  WHEN SYSTEM$STREAM_HAS_DATA('raw.customers_stream')
AS
  MERGE INTO analytics.dim_customers tgt
  USING (
    SELECT customer_id, name, email, status, updated_at,
           METADATA$ACTION AS action,
           METADATA$ISUPDATE AS is_update
    FROM raw.customers_stream
  ) src
  ON tgt.customer_id = src.customer_id
  WHEN MATCHED AND src.action = 'DELETE' AND src.is_update = FALSE
    THEN DELETE
  WHEN MATCHED AND src.action = 'INSERT' AND src.is_update = TRUE
    THEN UPDATE SET
      tgt.name = src.name,
      tgt.email = src.email,
      tgt.status = src.status,
      tgt.updated_at = src.updated_at,
      tgt.cdc_action = 'UPDATE',
      tgt.cdc_timestamp = CURRENT_TIMESTAMP()
  WHEN NOT MATCHED AND src.action = 'INSERT'
    THEN INSERT (customer_id, name, email, status, updated_at, cdc_action)
    VALUES (src.customer_id, src.name, src.email, src.status, src.updated_at, 'INSERT');

-- 5. Resume the task
ALTER TASK analytics.sync_customers_task RESUME;

The Stale Stream Problem


A stream does not store changes itself. It relies on the source table's Time Travel history, and DATA_RETENTION_TIME_IN_DAYS defaults to 1 day. When a table has a stream whose changes have not been consumed, Snowflake temporarily extends the table's retention to cover the stream's offset, up to MAX_DATA_EXTENSION_TIME_IN_DAYS, which defaults to 14 days. If the stream is not consumed within that window, it goes stale, meaning Snowflake can no longer reconstruct the change delta.


A stale Stream can't be recovered. You'll need to drop it, recreate it, and do an initial full load of the target table. To avoid this:



Task Graphs: Multi-Step Pipelines


A single Task often isn't enough. You might need to stage data, apply business rules, and then load a final table. Task graphs (DAGs) let you chain Tasks with dependencies.


SQL - Task Graph
-- Root task: copies new orders into staging.
-- raw.orders_stream is an append-only stream on raw.orders.
CREATE OR REPLACE TASK stage_orders
  WAREHOUSE = etl_wh
  SCHEDULE = '10 MINUTE'
  WHEN SYSTEM$STREAM_HAS_DATA('raw.orders_stream')
AS
  INSERT INTO staging.orders_staged (order_id, customer_id, amount, order_date)
  SELECT order_id, customer_id, amount, order_date
  FROM raw.orders_stream
  WHERE METADATA$ACTION = 'INSERT';

-- Child task: applies business rules to the staged batch
CREATE OR REPLACE TASK transform_orders
  WAREHOUSE = etl_wh
  AFTER stage_orders
AS
  INSERT INTO analytics.orders_clean (order_id, customer_id, amount, order_date, tier)
  SELECT order_id, customer_id, amount, order_date,
         CASE WHEN amount > 1000 THEN 'high_value' ELSE 'standard' END AS tier
  FROM staging.orders_staged;

-- Grandchild task: rebuilds the daily summary
CREATE OR REPLACE TASK aggregate_orders
  WAREHOUSE = etl_wh
  AFTER transform_orders
AS
  MERGE INTO analytics.daily_order_summary tgt
  USING (
    SELECT order_date, tier, SUM(amount) AS total, COUNT(*) AS cnt
    FROM analytics.orders_clean
    GROUP BY order_date, tier
  ) src ON tgt.order_date = src.order_date AND tgt.tier = src.tier
  WHEN MATCHED THEN UPDATE SET tgt.total = src.total, tgt.cnt = src.cnt
  WHEN NOT MATCHED THEN INSERT (order_date, tier, total, cnt)
    VALUES (src.order_date, src.tier, src.total, src.cnt);

-- Also after transform_orders: clears the batch so the next run sees only new orders
CREATE OR REPLACE TASK clear_staged_orders
  WAREHOUSE = etl_wh
  AFTER transform_orders
AS
  TRUNCATE TABLE staging.orders_staged;

-- Resume the root task and all of its dependents in one call
SELECT SYSTEM$TASK_DEPENDENTS_ENABLE('stage_orders');

Only the root Task has a SCHEDULE, and child Tasks run when their predecessors finish. SYSTEM$TASK_DEPENDENTS_ENABLE resumes the root and every dependent in one call, so you no longer need to resume the children one by one before the root. Task graphs also support a finalizer task that runs after the rest of the graph has finished, and automatic retries through TASK_AUTO_RETRY_ATTEMPTS on the root task, which are off by default.


Error Handling and Monitoring


Tasks support an ERROR_INTEGRATION parameter for sending failure notifications, and you can configure SUSPEND_TASK_AFTER_NUM_FAILURES to auto-suspend a Task after repeated failures. The default is 10, and the setting applies to standalone tasks and to the root task of a graph.


Monitor Task execution with TASK_HISTORY:

SQL
SELECT name, state, scheduled_time, completed_time, error_message
FROM TABLE(information_schema.task_history(
  task_name => 'PROCESS_ORDERS_TASK',
  scheduled_time_range_start => DATEADD('hour', -24, CURRENT_TIMESTAMP())
))
ORDER BY scheduled_time DESC;

The Critical Gotcha: Transaction Control


Here's the mistake that catches many teams on their first CDC build. A stream's offset advances when a DML transaction that reads from it commits, and under autocommit, which is the default, every DML statement commits on its own. If a procedure inserts the stream's changes into a target table and a later step fails, that first insert has already committed, and the changes are no longer in the stream. A transaction that rolls back, or never commits, does not move the offset.


The fix is to put the consuming statement and every step that depends on it inside one explicit transaction. Within that transaction, every query on the stream sees the same set of changes, and the offset moves once, at COMMIT:

SQL - Safe Stream Consumption
BEGIN;
  -- Both statements read the same changes from the stream
  INSERT INTO analytics.orders_current
    SELECT order_id, customer_id, amount, order_date
    FROM raw.orders_stream
    WHERE METADATA$ACTION = 'INSERT';

  INSERT INTO audit.order_changes
    SELECT order_id, METADATA$ACTION, METADATA$ISUPDATE, CURRENT_TIMESTAMP()
    FROM raw.orders_stream;
COMMIT;  -- the stream offset advances here, once

Key Takeaways



Frequently Asked Questions

Q: What is a Snowflake Stream?

A Snowflake Stream is a change tracking object on a table that records inserts, updates, and deletes. It exposes metadata columns (METADATA$ACTION, METADATA$ISUPDATE, METADATA$ROW_ID) that let you identify exactly what changed since the last time the Stream was consumed.

Q: What happens when a Snowflake Stream goes stale?

A stream goes stale when its offset falls outside the source table's data retention period. To help prevent this, Snowflake temporarily extends retention on a table with an unconsumed stream, up to MAX_DATA_EXTENSION_TIME_IN_DAYS, which defaults to 14 days. Once stale, the stream can no longer provide change data and must be recreated.

Q: How do Snowflake Tasks differ from cron jobs?

Snowflake Tasks run SQL statements on a schedule (cron syntax or fixed intervals) entirely within Snowflake, on a warehouse or on serverless compute. Unlike external cron jobs, Tasks support WHEN conditions to skip runs when there's no work, can run as triggered tasks when a stream has data, and can form task graphs for multi-step pipelines.

Q: Does consuming a Snowflake Stream advance its offset even if the transaction fails?

No. The offset advances only when the DML transaction that reads the stream commits. Under autocommit, though, each statement commits on its own, so a later failing step cannot undo an earlier insert that already consumed the stream. Put the consuming DML and its dependent steps in one explicit BEGIN ... COMMIT block.

Share this article:
Pranay Vatsal, Founder & CEO

Pranay Vatsal is the Founder & CEO of CelestInfo with deep expertise in Snowflake, data architecture, and building production-grade data systems for global enterprises.

Related Articles

Burning Questions
About CelestInfo

Simple answers to make things clear.

Our AI insights are continuously trained on large datasets and validated by experts to ensure high accuracy.

Absolutely. CelestInfo supports integration with a wide range of industry-standard software and tools.

We implement enterprise-grade encryption, access controls, and regular audits to ensure your data is safe.

Insights are updated in real-time as new data becomes available.

We offer 24/7 support via chat, email, and dedicated account managers.

Still have questions?

Get Assistance

Ready? Let's Talk!

Get expert insights and answers tailored to your business requirements and transformation.

Get Assistance