Home / Data Platform / Warehouses

Data PlatformHow-toIntermediate

Snowflake alerts: email on failed tasks, stale loads and long-running queries with CREATE ALERT

Set up Snowflake alerts that email on-call when tasks fail, tables stop loading or queries run too long, without duplicate notifications, and troubleshoot alerts that never fire.

Deepak Bhardwaj7 min read

Applies toSnowflake (all editions), alerts on a schedule, serverless and warehouse alerts, email notification integrations

Goal

You want Snowflake to tell you when something breaks, before the dashboard owner does. Snowflake alerts run a condition query on a schedule. When the query returns one or more rows, the alert runs an action, usually an email. This guide covers three alerts most teams need:

  1. A task failed in the last interval.
  2. A table hasn't received new rows for N hours.
  3. A query has been running longer than expected.

It also covers how to stop an alert from repeating the same notification, and how to check alert history when an alert never fires.

Prerequisites and grants

Run the setup as ACCOUNTADMIN, or as a role that can create integrations and grant account-level privileges.

USE ROLE ACCOUNTADMIN;

CREATE ROLE IF NOT EXISTS OPS_ALERTS;
GRANT ROLE OPS_ALERTS TO ROLE SYSADMIN;

-- Run alerts (warehouse-based)
GRANT EXECUTE ALERT ON ACCOUNT TO ROLE OPS_ALERTS;
-- Only needed for serverless alerts (no WAREHOUSE clause)
GRANT EXECUTE MANAGED ALERT ON ACCOUNT TO ROLE OPS_ALERTS;

CREATE DATABASE IF NOT EXISTS OPS;
CREATE SCHEMA IF NOT EXISTS OPS.MONITORING;
GRANT USAGE ON DATABASE OPS TO ROLE OPS_ALERTS;
GRANT USAGE, CREATE ALERT ON SCHEMA OPS.MONITORING TO ROLE OPS_ALERTS;

-- Lets the role read ACCOUNT_USAGE views
GRANT IMPORTED PRIVILEGES ON DATABASE SNOWFLAKE TO ROLE OPS_ALERTS;

To query INFORMATION_SCHEMA.TASK_HISTORY for tasks owned by other roles, the alert owner also needs MONITOR on those tasks, or ownership of them.

Step 1: Create the email integration

CREATE NOTIFICATION INTEGRATION OPS_EMAIL_INT
  TYPE = EMAIL
  ENABLED = TRUE
  ALLOWED_RECIPIENTS = ('oncall-data@example.com', 'jane.doe@example.com');

GRANT USAGE ON INTEGRATION OPS_EMAIL_INT TO ROLE OPS_ALERTS;

Important

SYSTEM$SEND_EMAIL only sends to email addresses that belong to users in your Snowflake account and have been verified. For a distribution list, create a Snowflake user for it, set its email and verify it. Otherwise the call fails.

Test the integration before you build alerts on it:

CALL SYSTEM$SEND_EMAIL(
  'OPS_EMAIL_INT',
  'oncall-data@example.com',
  'Snowflake alert test',
  'If you can read this, the integration works.'
);

For Slack, Teams or PagerDuty, use a webhook notification integration (TYPE = WEBHOOK) with SYSTEM$SEND_SNOWFLAKE_NOTIFICATION. Webhook integrations need a secret and a body template. Follow the current Snowflake docs for your target.

Step 2: Alert on failed tasks

USE ROLE OPS_ALERTS;
USE SCHEMA OPS.MONITORING;

CREATE OR REPLACE ALERT ALERT_TASK_FAILED
  WAREHOUSE = OPS_WH
  SCHEDULE = '10 MINUTE'
  IF (EXISTS (
    SELECT 1
    FROM TABLE(PROD.INFORMATION_SCHEMA.TASK_HISTORY(
           SCHEDULED_TIME_RANGE_START => SNOWFLAKE.ALERT.LAST_SUCCESSFUL_SCHEDULED_TIME(),
           SCHEDULED_TIME_RANGE_END   => SNOWFLAKE.ALERT.SCHEDULED_TIME(),
           ERROR_ONLY => TRUE))
  ))
  THEN
    CALL SYSTEM$SEND_EMAIL(
      'OPS_EMAIL_INT',
      'oncall-data@example.com',
      'Snowflake: task failure in PROD',
      'One or more tasks failed. Check TASK_HISTORY with ERROR_ONLY => TRUE for details.');

SNOWFLAKE.ALERT.LAST_SUCCESSFUL_SCHEDULED_TIME() and SNOWFLAKE.ALERT.SCHEDULED_TIME() limit each check to the window since the last successful run. This stops a failure from being reported on every run for the next hour, and it closes gaps if an alert run is skipped.

Note

The window functions are null on the very first run. Surround them with COALESCE(..., DATEADD('hour', -1, CURRENT_TIMESTAMP())) if you want the first run to look back a fixed amount.

Step 3: Alert on a table that stopped loading

Use a timestamp column that the pipeline sets on every load:

CREATE OR REPLACE ALERT ALERT_ORDERS_STALE
  SCHEDULE = 'USING CRON 0 * * * * UTC'    -- hourly, serverless (no WAREHOUSE)
  IF (EXISTS (
    SELECT 1
    FROM PROD.SALES.ORDERS
    HAVING MAX(_LOADED_AT) < DATEADD('hour', -3, CURRENT_TIMESTAMP())
        OR MAX(_LOADED_AT) IS NULL
  ))
  THEN
    CALL SYSTEM$SEND_EMAIL(
      'OPS_EMAIL_INT', 'oncall-data@example.com',
      'Snowflake: PROD.SALES.ORDERS has no new rows for 3 hours',
      'Check Snowpipe/COPY_HISTORY and the upstream pipeline.');

HAVING without GROUP BY returns one row when the condition is true and none when it is false. The alert fires only when the table is stale.

This alert keeps firing every hour until the data arrives. That is usually what you want for staleness, but it can be noisy. Raise the interval or suspend the alert while you work the incident.

Step 4: Alert on long-running queries

ACCOUNT_USAGE.QUERY_HISTORY has latency, so it can't see a query that is running right now. Use the INFORMATION_SCHEMA table function instead:

CREATE OR REPLACE ALERT ALERT_LONG_QUERY
  WAREHOUSE = OPS_WH
  SCHEDULE = '15 MINUTE'
  IF (EXISTS (
    SELECT 1
    FROM TABLE(PROD.INFORMATION_SCHEMA.QUERY_HISTORY(RESULT_LIMIT => 1000))
    WHERE EXECUTION_STATUS = 'RUNNING'
      AND START_TIME < DATEADD('minute', -60, CURRENT_TIMESTAMP())
  ))
  THEN
    CALL SYSTEM$SEND_EMAIL(
      'OPS_EMAIL_INT', 'oncall-data@example.com',
      'Snowflake: query running longer than 60 minutes',
      'Find it with INFORMATION_SCHEMA.QUERY_HISTORY and decide whether to cancel it.');

The table function only shows queries the alert owner's role is allowed to see. If you need visibility across the whole account, run this alert as a role that has MONITOR on the relevant warehouses. Prevent runaway queries with STATEMENT_TIMEOUT_IN_SECONDS as well. The alert only tells you after the fact.

Step 5: Resume and test

Alerts are created suspended. Nothing happens until you resume them.

ALTER ALERT ALERT_TASK_FAILED RESUME;
ALTER ALERT ALERT_ORDERS_STALE RESUME;
ALTER ALERT ALERT_LONG_QUERY RESUME;

-- Run one immediately instead of waiting for the schedule
EXECUTE ALERT ALERT_ORDERS_STALE;

SHOW ALERTS IN SCHEMA OPS.MONITORING;

Check the state column in SHOW ALERTS. It should read started.

Including the offending rows in the email

The action is a single SQL statement. To put details in the message, use a Snowflake Scripting block that reads the condition's result with SNOWFLAKE.ALERT.GET_CONDITION_RESULT_QUERY_ID():

CREATE OR REPLACE ALERT ALERT_TASK_FAILED_DETAIL
  WAREHOUSE = OPS_WH
  SCHEDULE = '10 MINUTE'
  IF (EXISTS (
    SELECT NAME, ERROR_MESSAGE
    FROM TABLE(PROD.INFORMATION_SCHEMA.TASK_HISTORY(
           SCHEDULED_TIME_RANGE_START => SNOWFLAKE.ALERT.LAST_SUCCESSFUL_SCHEDULED_TIME(),
           SCHEDULED_TIME_RANGE_END   => SNOWFLAKE.ALERT.SCHEDULED_TIME(),
           ERROR_ONLY => TRUE))
  ))
  THEN
    EXECUTE IMMEDIATE $$
    DECLARE
      body STRING;
    BEGIN
      SELECT LISTAGG(NAME || ': ' || LEFT(ERROR_MESSAGE, 300), '\n') INTO :body
      FROM TABLE(RESULT_SCAN(SNOWFLAKE.ALERT.GET_CONDITION_RESULT_QUERY_ID()));
      CALL SYSTEM$SEND_EMAIL('OPS_EMAIL_INT', 'oncall-data@example.com',
                             'Snowflake: task failures in PROD', :body);
    END;
    $$;

Troubleshooting alerts that never fire

Start with history. INFORMATION_SCHEMA.ALERT_HISTORY is near real time. ACCOUNT_USAGE.ALERT_HISTORY keeps a longer history but has latency.

SELECT NAME, STATE, SCHEDULED_TIME, COMPLETED_TIME, SQL_ERROR_MESSAGE
FROM TABLE(OPS.INFORMATION_SCHEMA.ALERT_HISTORY(
       SCHEDULED_TIME_RANGE_START => DATEADD('hour', -24, CURRENT_TIMESTAMP())))
ORDER BY SCHEDULED_TIME DESC;
What you see Cause Fix
No rows in history at all Alert still suspended ALTER ALERT ... RESUME
CONDITION_FALSE every run Condition returns no rows, or the window functions exclude the event Run the condition query by hand with fixed timestamps
CONDITION_FAILED Missing grants on the queried objects, or a SQL error Read SQL_ERROR_MESSAGE and grant USAGE/SELECT/MONITOR to the owner role
ACTION_FAILED Recipient not verified or not in ALLOWED_RECIPIENTS, or no USAGE on the integration Verify the user's email and fix the integration
TRIGGERED but no email Mail filtered or quarantined Check spam rules for Snowflake's sender address
Runs skipped Warehouse suspended by a resource monitor, or the previous run still executing Use a serverless alert or a dedicated small warehouse

Values in STATE can vary by release. Check the docs for the full list.

Prevent it next time

  • Keep every alert in one schema (OPS.MONITORING) and in Git, deployed like any other object.
  • Own alerts with a dedicated role, not a person's role, so they survive staff changes.
  • Run a weekly EXECUTE ALERT on a test alert to prove email delivery still works.
  • Alert on the alerts: query ALERT_HISTORY for CONDITION_FAILED and ACTION_FAILED from a second alert.
  • Use window functions on event-based conditions, and keep staleness alerts on hourly or slower schedules to avoid alert fatigue.