0
0
Snowflakecloud~5 mins

Task trees and dependencies in Snowflake - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you have multiple tasks that need to run in a specific order, task trees help you organize them. Snowflake task trees let you set up tasks that depend on each other, so they run automatically in the right sequence.
When you want to load data step-by-step, like first loading raw data, then cleaning it, then aggregating it.
When you have a daily report that depends on several smaller data processing steps.
When you want to automate a workflow where one task must finish before the next starts.
When you want to reduce manual work by chaining tasks together.
When you want to make sure tasks run only after their dependencies succeed.
Config File - task_tree.sql
task_tree.sql
CREATE OR REPLACE TASK task_a
  WAREHOUSE = compute_wh
  SCHEDULE = 'USING CRON 0 * * * * UTC'
AS
  INSERT INTO raw_data_table SELECT * FROM source_table;

CREATE OR REPLACE TASK task_b
  WAREHOUSE = compute_wh
  AFTER task_a
AS
  INSERT INTO cleaned_data_table SELECT * FROM raw_data_table WHERE valid = TRUE;

CREATE OR REPLACE TASK task_c
  WAREHOUSE = compute_wh
  AFTER task_b
AS
  INSERT INTO aggregated_data_table SELECT category, COUNT(*) FROM cleaned_data_table GROUP BY category;

This file creates three tasks: task_a, task_b, and task_c.

task_a runs every hour and loads raw data.

task_b depends on task_a and cleans the data.

task_c depends on task_b and aggregates the cleaned data.

The AFTER keyword sets the dependency order.

Commands
This command starts the first task so it can run on schedule.
Terminal
ALTER TASK task_a RESUME
Expected OutputExpected
Task TASK_A resumed.
This command starts the second task, which depends on task_a.
Terminal
ALTER TASK task_b RESUME
Expected OutputExpected
Task TASK_B resumed.
This command starts the third task, which depends on task_b.
Terminal
ALTER TASK task_c RESUME
Expected OutputExpected
Task TASK_C resumed.
This command lists all tasks and their current status to verify they are active.
Terminal
SHOW TASKS
Expected OutputExpected
name | database | schema | state | schedule -------|----------|--------|---------|---------- task_a | MY_DB | PUBLIC | started | 0 * * * * task_b | MY_DB | PUBLIC | started | task_c | MY_DB | PUBLIC | started |
This command checks the recent run history of task_c to see if it ran after its dependencies.
Terminal
SELECT SYSTEM$TASK_HISTORY('task_c', DATEADD('hour', -1, CURRENT_TIMESTAMP()), CURRENT_TIMESTAMP())
Expected OutputExpected
TASK_NAME | STATUS | START_TIME | END_TIME ----------|---------|----------------------|---------------------- task_c | SUCCESS | 2024-06-01 10:00:00 | 2024-06-01 10:01:00
Key Concept

If you remember nothing else from this pattern, remember: tasks run automatically in order when you set dependencies using AFTER.

Common Mistakes
Not resuming tasks after creating them
Tasks stay paused and never run if you forget to resume them.
Always run ALTER TASK task_name RESUME to activate tasks.
Setting circular dependencies between tasks
Tasks will never run because they wait on each other forever.
Make sure dependencies form a clear chain without loops.
Expecting tasks to run immediately after creation without schedule or manual trigger
Tasks only run on schedule or when triggered; they don't run just by existing.
Set a schedule or manually run tasks to start the workflow.
Summary
Create tasks with SQL commands and set dependencies using AFTER to form a task tree.
Resume tasks to activate them so they can run on schedule or after their dependencies.
Use SHOW TASKS and SYSTEM$TASK_HISTORY to monitor task status and execution order.