The goal: no more running COPY by hand
In lesson 4 we loaded two months of ShopKart orders by running COPY INTO ourselves. From December, new order files arrive all through the day, and the founders want a daily orders table that's always up to date. Here's what we'll build, all tested on our Snowflake account:
๐New file
orders_2026-12-02.csv lands in the stage
๐ฐPipe
Snowpipe runs COPY INTO raw.orders
๐Stream
Remembers the 30 new rows
โฐTask
Every 5 min: MERGE changes into analytics.daily_orders
Snowpipe: load files as they arrive
USE SCHEMA shopkart.raw;
-- A pipe is a saved COPY INTO that Snowflake runs for you when new files arrive
CREATE OR REPLACE PIPE orders_pipe AS
COPY INTO orders FROM @landing/live/;-- A new file arrives (we upload it; in production the app writes it to S3/Azure/GCS)
PUT file://~/shopkart/data/live/orders_2026-12-01.csv @landing/live/;
-- Internal stages send no event notifications, so we ask the pipe to look for new files
ALTER PIPE orders_pipe REFRESH;| File | Status |
|---|---|
| orders_2026-12-01.csv.gz | SENT |
About 15 seconds later:
SELECT SYSTEM$PIPE_STATUS('shopkart.raw.orders_pipe') AS status;
SELECT file_name, status, row_count, pipe_name, last_load_time
FROM TABLE(information_schema.copy_history(
TABLE_NAME => 'SHOPKART.RAW.ORDERS',
START_TIME => DATEADD(minute, -10, CURRENT_TIMESTAMP())));{"executionState":"RUNNING","pendingFileCount":0,"lastIngestedTimestamp":"2026-11-22T09:28:34.32Z",
"lastIngestedFilePath":"orders_2026-12-01.csv.gz","pendingHistoryRefreshJobsCount":0}| FILE_NAME | STATUS | ROW_COUNT | PIPE_NAME | LAST_LOAD_TIME |
|---|---|---|---|---|
| orders_2026-12-01.csv.gz | Loaded | 25 | ORDERS_PIPE | 2026-11-22 01:28:46 |
25 orders loaded, with no warehouse of ours involved: Snowpipe uses Snowflake-managed (serverless) compute and bills per second of work. Like COPY, a pipe remembers which files it loaded (for 14 days), so the same file is never loaded twice.
In production: AUTO_INGEST
ALTER PIPE ... REFRESH is a manual nudge (it queues files staged in the last 7 days). In real projects files land in cloud storage, and the storage service tells Snowpipe about each new file. This setup needs an AWS account, so we didn't run it here; it follows Snowflake's documented steps:
-- Production version on Amazon S3 (one-time setup by an admin)
CREATE STORAGE INTEGRATION s3_int
TYPE = EXTERNAL_STAGE
STORAGE_PROVIDER = 'S3'
STORAGE_AWS_ROLE_ARN = 'arn:aws:iam::123456789012:role/snowflake-shopkart'
STORAGE_ALLOWED_LOCATIONS = ('s3://shopkart-exports/orders/')
ENABLED = TRUE;
CREATE STAGE s3_orders URL = 's3://shopkart-exports/orders/' STORAGE_INTEGRATION = s3_int
FILE_FORMAT = csv_with_header;
CREATE PIPE orders_auto_pipe
AUTO_INGEST = TRUE -- S3 sends an event for each new file; no REFRESH needed
AS
COPY INTO orders FROM @s3_orders;
SHOW PIPES LIKE 'ORDERS_AUTO_PIPE'; -- copy notification_channel into the S3 bucket's event settings| Cloud | How Snowpipe hears about new files |
|---|---|
| AWS S3 | S3 event notifications to the pipe's SQS queue (notification_channel) |
| Azure Blob / ADLS Gen2 | Event Grid โ a storage queue, through a notification integration |
| Google Cloud Storage | Pub/Sub, through a notification integration |
Streams: what changed since last time?
A stream on a table is a bookmark. It doesn't copy the table; it remembers a position (an offset) in the table's history and shows every row that changed after it. First, the summary table we want to keep updated, then the stream:
-- A summary table that we want to keep up to date automatically
CREATE OR REPLACE TABLE shopkart.analytics.daily_orders (
order_date DATE,
orders NUMBER,
revenue NUMBER(12,2),
updated_at TIMESTAMP_LTZ
);
INSERT INTO shopkart.analytics.daily_orders
SELECT order_ts::DATE, COUNT(*), SUM(quantity * unit_price), CURRENT_TIMESTAMP()
FROM orders
GROUP BY 1;
-- A stream: remembers which rows of orders change from now on
CREATE OR REPLACE STREAM orders_changes ON TABLE orders;Tasks: run SQL on a schedule
-- A task runs SQL on a schedule. No WAREHOUSE given = serverless: Snowflake sizes the compute.
CREATE OR REPLACE TASK refresh_daily_orders
SCHEDULE = '5 MINUTE'
WHEN SYSTEM$STREAM_HAS_DATA('orders_changes') -- skip the run (and the cost) if nothing changed
AS
MERGE INTO shopkart.analytics.daily_orders t
USING (
SELECT order_ts::DATE AS order_date, COUNT(*) AS orders, SUM(quantity * unit_price) AS revenue
FROM orders_changes
WHERE METADATA$ACTION = 'INSERT' AND NOT METADATA$ISUPDATE -- brand-new rows only
GROUP BY 1
) s
ON t.order_date = s.order_date
WHEN MATCHED THEN UPDATE SET t.orders = t.orders + s.orders,
t.revenue = t.revenue + s.revenue,
t.updated_at = CURRENT_TIMESTAMP()
WHEN NOT MATCHED THEN INSERT VALUES (s.order_date, s.orders, s.revenue, CURRENT_TIMESTAMP());
SHOW TASKS LIKE 'REFRESH_DAILY_ORDERS';| name | warehouse | schedule | state | condition |
|---|---|---|---|---|
| REFRESH_DAILY_ORDERS | NULL (serverless) | 5 MINUTE | suspended | SYSTEM$STREAM_HAS_DATA('orders_changes') |
| Option | Means |
|---|---|
| SCHEDULE = '5 MINUTE' | Every 5 minutes. Or a cron: 'USING CRON 0 6 * * * Asia/Kolkata' = 6 a.m. IST daily |
| WHEN SYSTEM$STREAM_HAS_DATA(...) | Checked first; if false, the run is skipped and uses no compute |
| No WAREHOUSE | Serverless task: Snowflake picks and sizes compute. Or WAREHOUSE = shopkart_wh |
| AFTER other_task | Run when another task finishes (task graphs) |
Watch it work
-- The next day's file arrives through the pipe
PUT file://~/shopkart/data/live/orders_2026-12-02.csv @landing/live/;
ALTER PIPE orders_pipe REFRESH;
-- ...and the stream sees the new rows (reading it with SELECT doesn't use it up)
SELECT COUNT(*) AS new_rows_in_stream FROM orders_changes;| NEW_ROWS_IN_STREAM |
|---|
| 30 |
-- Run the task once now (normally ALTER TASK ... RESUME lets the schedule run it)
EXECUTE TASK refresh_daily_orders;| status |
|---|
| Task REFRESH_DAILY_ORDERS is scheduled to run immediately. |
SELECT name, state, scheduled_time, completed_time, error_message
FROM TABLE(information_schema.task_history(TASK_NAME => 'REFRESH_DAILY_ORDERS'))
ORDER BY scheduled_time DESC
LIMIT 1;
SELECT order_date, orders, revenue FROM shopkart.analytics.daily_orders ORDER BY order_date DESC LIMIT 3;
-- The MERGE read the stream inside a transaction, so the stream is empty again
SELECT COUNT(*) AS rows_in_stream FROM orders_changes;| NAME | STATE | SCHEDULED_TIME | COMPLETED_TIME | ERROR_MESSAGE |
|---|---|---|---|---|
| REFRESH_DAILY_ORDERS | SUCCEEDED | 01:31:31.988 | 01:31:32.914 | NULL |
| ORDER_DATE | ORDERS | REVENUE |
|---|---|---|
| 2026-12-02 | 30 | 61270.00 |
| 2026-12-01 | 25 | 42475.00 |
| 2026-11-30 | 99 | 233771.00 |
| ROWS_IN_STREAM |
|---|
| 0 |
The new day appeared in under a second of serverless compute, and the stream is empty again. A stream moves forward only when a statement that reads it (our MERGE) commits. If the MERGE had failed, the 30 rows would still be waiting for the next run: nothing is lost, nothing is processed twice.
Streams and updates: the trap
-- What does a stream show for an UPDATE?
UPDATE orders SET status = 'DELIVERED' WHERE order_id IN (106001, 106002);
SELECT order_id, status, METADATA$ACTION, METADATA$ISUPDATE
FROM orders_changes
ORDER BY order_id, METADATA$ACTION;| ORDER_ID | STATUS | METADATA$ACTION | METADATA$ISUPDATE |
|---|---|---|---|
| 106001 | PLACED | DELETE | True |
| 106001 | DELIVERED | INSERT | True |
| 106002 | PLACED | DELETE | True |
| 106002 | DELIVERED | INSERT | True |
| Stream type | Records | Use for |
|---|---|---|
| Standard (default) | Inserts, updates, deletes (net changes) | Full CDC: keeping a copy in sync |
| APPEND_ONLY = TRUE | Inserts only (cheaper) | Event or log tables that are only appended to |
| INSERT_ONLY = TRUE | New rows in external or Iceberg tables | Files landing in a lake |
Task graphs
-- A task graph: this task runs right after refresh_daily_orders finishes
CREATE OR REPLACE TASK refresh_top_days
AFTER refresh_daily_orders
AS
CREATE OR REPLACE TABLE shopkart.analytics.top_days AS
SELECT order_date, revenue FROM shopkart.analytics.daily_orders ORDER BY revenue DESC LIMIT 5;
-- Tasks are created suspended. Resume children first, then the root.
ALTER TASK refresh_top_days RESUME;
ALTER TASK refresh_daily_orders RESUME;-- About 6 minutes later
SELECT name, state, scheduled_time, completed_time
FROM TABLE(information_schema.task_history(
SCHEDULED_TIME_RANGE_START => DATEADD(minute, -15, CURRENT_TIMESTAMP())))
ORDER BY scheduled_time DESC
LIMIT 4;
SELECT * FROM shopkart.analytics.top_days ORDER BY revenue DESC;
ALTER TASK refresh_daily_orders SUSPEND; -- stop the schedule when you're done practising| NAME | STATE | SCHEDULED_TIME | COMPLETED_TIME |
|---|---|---|---|
| REFRESH_DAILY_ORDERS | SCHEDULED | 01:42:45 | NULL |
| REFRESH_TOP_DAYS | SUCCEEDED | 01:37:46 | 01:37:48 |
| REFRESH_DAILY_ORDERS | SUCCEEDED | 01:37:45 | 01:37:46 |
| REFRESH_DAILY_ORDERS | SUCCEEDED | 01:31:31 | 01:31:32 |
| ORDER_DATE | REVENUE |
|---|---|
| 2026-10-20 | 397699.00 |
| 2026-10-21 | 324594.00 |
| 2026-10-01 | 255580.00 |
| 2026-11-21 | 251772.00 |
| 2026-10-08 | 251772.00 |
On its own, the root task ran on schedule (the stream had the two updates, so the condition was true), the child ran 1.5 seconds later, and the next run was already queued. The top days are 20 and 21 October: ShopKart's Diwali sale. A task graph can have many children and joins (a task with several AFTERs waits for all of them), plus a finalizer task that runs at the end even if something failed, which is handy for alerts and clean-up.
Monitoring and errors
| Check | How |
|---|---|
| Did files load? | COPY_HISTORY / SYSTEM$PIPE_STATUS; pipe errors also go to an error integration |
| Did tasks succeed? | TASK_HISTORY (or Snowsight โ Monitoring โ Task History) |
| Get told about failures | ERROR_INTEGRATION on the task, or a finalizer task / alert that emails or posts to Slack |
| Stop retrying forever | SUSPEND_TASK_AFTER_NUM_FAILURES = 3 |
| Stream healthy? | SHOW STREAMS: the stale and stale_after columns |
๐ฏ Interview questions on this topic
Try to answer out loud first, then tap to check.
Q1What is Snowpipe and how does it differ from COPY INTO?Fresher+
Q2What are streams and tasks used for?Fresher+
Q3How does a stream know what's already been processed?1โ3 yrs+
Q4How does an UPDATE appear in a standard stream?1โ3 yrs+
Q5Serverless tasks vs warehouse tasks?1โ3 yrs+
Q6Design a near-real-time pipeline in Snowflake for orders arriving as files in S3.3+ yrs+
Key takeaways
- โPipes load, streams track, tasks process: together they replace manual runs.
- โStreams make processing incremental and exactly-once.
- โUpdates arrive as delete + insert pairs, so plan for them.
- โCondition tasks on SYSTEM$STREAM_HAS_DATA to pay only when there's work.
Frequently asked questions
What is Snowpipe?
A serverless service that loads files into a table soon after they land in a stage, usually within a minute. It runs a COPY INTO you define in a PIPE object. With AUTO_INGEST, cloud storage events (S3, Azure Event Grid, GCS Pub/Sub) tell Snowpipe about new files; otherwise you call its REST API or ALTER PIPE ... REFRESH.
Snowpipe or COPY INTO on a schedule?
Snowpipe for files arriving all day (near real time, pay per file processed, no warehouse to manage). Scheduled COPY INTO for big batches once or a few times a day, where a warehouse loading everything at once is simpler and cheaper.
What is a stream in Snowflake?
An object that records which rows of a table were inserted, updated or deleted since the stream was last consumed (change data capture). Selecting from it shows the changed rows plus METADATA$ACTION, METADATA$ISUPDATE and METADATA$ROW_ID. A DML statement that reads it (like INSERT or MERGE) moves its offset forward.
Do serverless tasks cost more?
Serverless compute has a higher rate per credit-hour than a warehouse, but you pay only for the seconds the task runs, with no idle time and no 60-second minimum. For short, frequent tasks that's usually cheaper; for long, heavy tasks a dedicated warehouse can be cheaper.
What happens if a stream isn't read for a long time?
It can become stale: if it isn't consumed within the table's data retention period (extended up to 14 days by default for streams via MAX_DATA_EXTENSION_TIME_IN_DAYS), its offset falls out of history and it must be recreated. Make sure a task consumes it regularly.
๐ง Quick check: Snowpipe, streams and tasks
1. Which object loads new files automatically?
2. A plain SELECT from a stream...
3. An UPDATE of one row appears in a stream as...
4. Why add WHEN SYSTEM$STREAM_HAS_DATA(...) to a task?
5. Newly created tasks are...
Checking your accessโฆ