Managing Resource Monitors¶
A virtual warehouse consumes Snowflake credits while it runs. The number of credits consumed depends on the size of the warehouse and how long it runs.
To help control costs and avoid unexpected credit usage related to using warehouses, Snowflake provides resource monitors. Resource Monitors can be used to impose limits on the number of credits that warehouses consume within each monthly billing period. When these limits are close and/or reached, the resource monitor can trigger various actions, such as sending alert notifications and suspending the warehouses.
Resource monitors can only be created by account administrators (i.e. users with the ACCOUNTADMIN role); however, account administrators can choose to enable users with other roles to view and modify resource monitors.
In this Topic:
- DDL for Resource Monitors
- Access Control Privileges for Resource Monitors
- Enabling Receipt of Notifications
- Creating Resource Monitors
- Assigning Warehouses to a Resource Monitor
- Modifying a Resource Monitor
- Viewing Resource Monitors
- Defining a Global Resource Monitor for Your Account
Resource Monitor Properties¶
A resource monitor is a first-class object in Snowflake, consisting of the following properties:
The number of Snowflake credits allocated to the monitor for each monthly billing period.
The total accumulated number of Snowflake credits used/consumed during the current monthly billing period by all warehouses assigned to the monitor. At the beginning of each monthly billing cycle, this number resets to 0.
A credit quota threshold, specified as a percentage (values greater than 100 are allowed), and an action to perform when the threshold is reached. If credit usage reaches the threshold for the monthly billing period, the trigger fires and the action defined for the threshold is performed. The following actions are supported:
A resource monitor must have at least one trigger defined to perform any actions (i.e. if a monitor does not have any triggers defined, nothing happens when the credit usage reaches the credit quota threshold for the month).
Resource Monitor Assignment¶
A monitor can be assigned one or more warehouses, thereby controlling the credit usage for each warehouse assigned to it. Note, however, each warehouse can be assigned to only a single resource monitor. In addition, a monitor can also be set at the account level to control credit usage for all warehouses in your account.
An account-level resource monitor does not override resource monitor assignment for individual warehouses. If either resource monitor reaches its defined threshold, the warehouse is suspended. This enables controlling global credit usage while also providing fine-grained control over credit usage for individual/specific warehouses.
The following diagram illustrates a resource monitor set at the account level, as well as resource monitors assigned individual warehouses:
Based on this diagram:
- The credit quota for the entire account is 5000 per month (dictated by Resource Monitor 1); if this quota is reached, the action(s) defined for the resource monitor (e.g. warehouse suspension) are enforced.
- Warehouse 3 can consume a maximum of 1000 credits for the month.
- Warehouse 4 and 5 can consume a maximum combined total of 2500 credits for the month.
- The actual credits consumed by Warehouses 3, 4, and 5 may be less than their quotas if the quota for the account is reached first.
Warehouse Suspension and Resumption¶
The credit usage for a resource monitor reflects the sum of all credits used by all assigned warehouses during the current billing period. If a monitor’s usage reaches its quota for the month and a suspend action has been triggered for the monitor, any warehouses assigned to the monitor are suspended and cannot be resumed until one of the following conditions is met:
- A new monthly billing cycle starts.
- The credit quota for the monitor is increased.
- The credit threshold for the monitor trigger is increased.
- The monitor is no longer assigned to the warehouse.
- The monitor is dropped.
Resource monitors are not intended for strictly controlling consumption on an hourly basis; they are intended for tracking and controlling credit consumption on a monthly basis. Also, they are not intended for setting precise limits on credit usage (i.e. down to the level of individual credits). For example, when credit quota thresholds are reached for a resource monitor, the assigned warehouses may take some time to suspend, even when the action is SUSPEND IMMEDIATE, thereby consuming additional credits.
If you wish to strictly enforce your quotas, we recommend the following:
- Utilize buffers in the quota thresholds for trigger actions (e.g. set a threshold to 90% instead of 100%). This will help ensure that your credit usage doesn’t exceed the quota.
- To more strictly control credit usage for individual warehouses, assign only a single warehouse to each resource monitor. When warehouses share the same resource monitor, they share monitor thresholds, which may result in credit usage for one warehouse impacting the other assigned warehouses.
Resource Monitor Notifications¶
When a resource monitor reaches a threshold for a trigger, it generates a notification for the action defined for the threshold:
- Suspend the assigned warehouse(s) after all running queries complete.
- Immediately cancel all running queries and suspend the assigned warehouse(s).
- Send an alert that the threshold has been reached.
Notifications are received by account administrators through the web interface and/or email; however, by default, notifications are disabled:
- To receive notifications, each account administrator must explicitly enable notifications through their preferences in the web interface.
- In addition, if an account administrator chooses to receive email notifications, they must provide (and verify) a valid email address before they will receive any emails.
DDL for Resource Monitors¶
Snowflake provides the following DDL commands for creating and using/managing resource monitors:
In addition, the following DDL commands can be used to assign a resource monitor to a warehouse and view whether a warehouse is assigned to a monitor:
Access Control Privileges for Resource Monitors¶
By default, resource monitors can only be created by account administrators and, therefore, can only be viewed and maintained by them.
However, Snowflake supports passing these privileges on to other users to enable them to view and update resource monitors as needed. This functionality is implemented through the following access control privileges, which can be granted on specified resource monitors to specified roles:
Enabling Receipt of Notifications¶
As an account administrator, you must enable notifications before you can receive any notifications generated by resource monitors. To enable notifications for yourself:
- Log into the Snowflake web interface.
- Make sure you are using the ACCOUNTADMIN role. If you aren’t, in the drop-down menu next to your name in the upper-right corner, select Switch role » ACCOUNTADMIN.
- In the same drop-down menu, select Preferences » Notifications.
- Select one of the options. If you selected Email or All and you do not already have a validated email address, you must enter an email address.
- To complete the process, you must validate the email address you entered by following the instructions provided on-screen.
Creating Resource Monitors¶
Only account administrators (i.e. users with the ACCOUNTADMIN role) can create resource monitors.
Resource monitors can be created through either the web interface or SQL:
- Make sure you are using the ACCOUNTADMIN role.
- Click on Account » Resource Monitors » Create Resource Monitor.
- Enter a name and quota, and specify whether the resource monitor will be used for monitoring your account or individual warehouses (default is Account).
- You can also specify suspend actions and up to 5 alert notifications.
Execute a CREATE RESOURCE MONITOR command.
You must assign at least one warehouse to a resource monitor or set the monitor at the account level for it to begin monitoring/tracking credit usage:
- In the web interface, you can do this at creation time.
- In SQL, you must create the resource monitor first, then assign one or more warehouses to it using a separate command.
Also, to receive notifications when resource monitor actions are triggered, you must enable notifications in your preferences in the Snowflake web interface.
The following three examples illustrate using SQL to create a resource monitor:
The first SQL example creates a monitor named
limit1that generates a notification and suspends all associated warehouses when 100% of the quota (1000 credits) is reached for the month:
USE ROLE ACCOUNTADMIN; CREATE OR REPLACE RESOURCE MONITOR limit1 WITH CREDIT_QUOTA=1000 TRIGGERS ON 100 PERCENT DO SUSPEND;
The SUSPEND action waits for currently-executing queries to finish before suspending the warehouse. This means that a query started before the trigger fires executes to completion and the warehouse consumes credits even after the quota is reached.
This example includes a SUSPEND_IMMEDIATE action to prevent warehouses from consuming credits after the quota has been reached. For example:
USE ROLE ACCOUNTADMIN; CREATE OR REPLACE RESOURCE MONITOR limit1 WITH CREDIT_QUOTA=1000 TRIGGERS ON 90 PERCENT DO SUSPEND ON 100 PERCENT DO SUSPEND_IMMEDIATE;
In this example, a notification is generated and the assigned warehouses are suspended when 90% usage is reached, which prevents the warehouses from executing any new queries, but allows currently-executing queries to complete. If the assigned warehouses reach 100% usage, a notification is generated and the warehouses are suspended immediately, canceling all currently-executing queries.
This example includes two NOTIFY actions to alert account administrators when usage for the resource monitor is nearing the quota:
CREATE OR REPLACE RESOURCE MONITOR limit WITH CREDIT_QUOTA=1000 TRIGGERS ON 50 PERCENT DO NOTIFY ON 80 PERCENT DO NOTIFY ON 100 PERCENT DO SUSPEND ON 120 PERCENT DO SUSPEND_IMMEDIATE;
In this example, when 50% and 80% usage is reached, an alert notification is sent to all account administrators who have enabled notifications, but no other actions are performed.
Assigning Warehouses to a Resource Monitor¶
Warehouses can be assigned to a resource monitor through either the web interface or SQL:
The following SQL example uses the warehouse (
mywh) and resource monitor (
limit1) created in the previous example:
ALTER WAREHOUSE mywh SET RESOURCE_MONITOR = limit1;
Modifying a Resource Monitor¶
You can modify the monthly credit quota for a monitor, as well as change the trigger percentages for the monitor and add more notifications.
Resource monitors can be modified through either the web interface or SQL:
- Make sure you are using the ACCOUNTADMIN role (or a role that has been granted the MODIFY privilege on the desired resource monitor).
- Click on Account » Resource Monitors.
- Click on the resource monitor to display the details panel.
- Click Edit.
- Change the credit quota or suspend action percentages, or add more notifications.
Execute an ALTER RESOURCE MONITOR command.
The following SQL example changes the monthly credit quota for
ALTER RESOURCE MONITOR limit1 SET CREDIT_QUOTA=2000;
Viewing Resource Monitors¶
Resource monitors can be viewed through either the web interface or SQL:
Web Interface: SQL:
Execute a SHOW RESOURCE MONITORS command.
In addition, with any role, you can use the SHOW WAREHOUSES command to view the warehouses owned by the role (or for which the role has been granted USAGE privilege). The output includes the resource monitor, if any, to which the warehouse is assigned.
Defining a Global Resource Monitor for Your Account¶
Global monthly credit usage for an account can be controlled by setting a resource monitor at the account level. When the combined credit usage for all warehouses in the account reaches the defined threshold, all the warehouses are suspended.
USE ROLE ACCOUNTADMIN; CREATE RESOURCE MONITOR accountmax WITH CREDIT_QUOTA=10000 TRIGGERS ON 100 PERCENT DO SUSPEND; ALTER ACCOUNT SET RESOURCE_MONITOR = accountmax;
To view whether a resource monitor is set for your account, use the SHOW RESOURCE MONITORS command. In the output, if ACCOUNT appears in the LEVEL column for a resource monitor, it is set for the account.