Working with 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 caused by running warehouses, Snowflake provides resource monitors. Resource monitors can be used to impose limits on the number of credits that warehouses consume within a specified interval or date range. When these limits are reached and/or are approaching, the resource monitor can trigger various actions, such as sending alert notifications and/or 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
- Modifying a Resource Monitor
- Setting a Resource Monitor for Your Account
- Assigning Warehouses to a Resource Monitor
- Viewing Resource Monitors
Resource Monitor Properties¶
A resource monitor is a first-class object in Snowflake, consisting of the following properties.
Credit Quota and Usage¶
Credit quota specifies the number of Snowflake credits allocated to the monitor for the specified frequency interval. Any number can be specified.
Credit usage tracks the total number of Snowflake credits consumed within the specified frequency interval by all warehouses assigned to the monitor. At the specified interval,
this number resets back to
This property specifies whether the resource monitor is used to monitor the credit usage for your entire Account (i.e. all warehouses in the account) or a specific set of individual warehouses.
If this property is not set, the resource monitor doesn’t monitor any credit usage. It simply remains dormant.
By default, a resource monitor starts monitoring credit usage immediately and the usage resets at the beginning of each calendar month (i.e. the start of the standard Snowflake billing cycle).
However, you can define a custom schedule to implement more granular control over the following resource monitor behavior:
The interval at which the credit usage resets back to
Note that the actual date when the credit usage resets is relative to the specified start date and time (see next property).
Date and time (i.e. timestamp) when Snowflake starts monitoring the assigned warehouses. Supported values:
Snowflake also uses this date and time to determine when to reset the credit usage, relative to the specified interval. If you specify the last day of a month as the start date,
Snowflake resets credit usage on the last day of all following months, regardless of the number of days in each month. For example, you set the start date to February 28. Snowflake
subsequently resets credit usage back to
Date and time (i.e. timestamp) at which Snowflake suspends the warehouses associated with the resource monitor, regardless of whether the credit usage has reached any of the thresholds defined for the resource monitor’s actions. Supported values:
Any timestamp beyond the current timestamp can be specified.
Note that this property is not commonly used.
If you specify a frequency when customizing the schedule for a resource monitor, you must also specify a start timestamp, and vice versa (i.e. you cannot set one without setting the other).
Also referred to as triggers, each action specifies a threshold, as a percentage of the credit quota for the resource monitor, and the action to perform when the threshold is
reached within the specified interval. Note that actions support thresholds greater than
Resource monitors support the following actions:
|Notify & Suspend:|
|Send a notification (to all account administrators with notifications enabled) and suspend all assigned warehouses after all statements being executed by the warehouse(s) have completed.|
|Notify & Suspend Immediately:|
|Send a notification (to all account administrators with notifications enabled) and suspend all assigned warehouses immediately, which cancels any statements being executed by the warehouses at the time.|
|Notify:||Perform no action, but send an alert notification (to all account administrators with notifications enabled).|
Each resource monitor can have one Suspend action, one Suspend Immediate action, and up to five Notify actions.
A resource monitor must have at least one action defined; if no actions have been defined, nothing happens when the credit usage reaches the threshold.
Assignment of Resource Monitors¶
A single monitor can be set at the account level to control credit usage for all warehouses in your account.
In addition, a monitor can be assigned to one or more warehouses, thereby controlling the credit usage for each assigned warehouse. Note, however, that a warehouse can be assigned to only a single resource monitor.
The following diagram illustrates a scenario in which one resource monitor is set at the account level and individual warehouses are assigned to two other resource monitors:
Based on this diagram:
- The credit quota for the entire account is 5000 for the interval (month, week, etc.), as controlled by Resource Monitor 1; if this quota is reached within the interval, the actions defined for the resource monitor (Suspend, Suspend Immediate, etc. ) are enforced for all five warehouses.
- Warehouse 3 can consume a maximum of 1000 credits within the interval.
- Warehouse 4 and 5 can consume a maximum combined total of 2500 credits within the interval.
Note that 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.
An account-level resource monitor does not override resource monitor assignment for individual warehouses. If either the account resource monitor or the warehouse resource monitor reaches its defined threshold and a suspend action has been defined, the warehouse is suspended.
This enables controlling global credit usage while also providing fine-grained control over credit usage for individual/specific warehouses.
In addition, an account-level resource monitor does not control credit usage by the Snowflake-provided warehouses (used for Snowpipe, automatic reclustering, and materialized views); the monitor only controls the virtual warehouses created in your account.
Warehouse Suspension and Resumption¶
The credit usage for a resource monitor reflects the sum of all credits used by all assigned warehouses within the specified interval. If a monitor has a Suspend or Suspend Immediately action defined and its usage reaches the threshold for the action, any warehouses assigned to the monitor are suspended and cannot be resumed until one of the following conditions is met:
- The next interval, if any, starts, as dictated by the start date for the monitor.
- The credit quota for the monitor is increased.
- The credit threshold for the suspend action is increased.
- The warehouses are no longer assigned to the monitor.
- 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 per interval (day, week, month, etc.). 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 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 multiple warehouses are assigned to the same resource monitor, they share the same quota thresholds, which may result in credit usage for one warehouse impacting the other assigned warehouses.
Resource Monitor Notifications¶
When a resource monitor reaches the threshold for an action, it generates one of the following notifications, based on the action performed:
- The assigned warehouses will be suspended after all running queries complete.
- All running queries in the assigned warehouses will be canceled and the warehouses suspended immediately.
- A threshold has been reached, but no action has been performed.
The notification is sent to all account administrators who have enabled receipt of notifications.
Notifications can be received by account administrators through the web interface and/or email; however, by default, notifications are not enabled:
- 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 a valid email address (and verify the 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 enabling other users to view and modify 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 for yourself in the web interface before you can receive any notifications generated by resource monitors.
To enable notifications in the 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.
- If you enter an email address, you must then validate the address by following the instructions provided on-screen.
Creating Resource Monitors¶
Resource monitors can be created through either the web interface or SQL; however, only account administrators (i.e. users with the ACCOUNTADMIN role) can create resource monitors.
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 are required to do this at creation time.
- In SQL, you must create the resource monitor first, then assign one or more warehouses to it by executing /sql-reference/sql/alter-resource-monitor` as a separate, additional step.
Also, to receive notifications when resource monitor actions are triggered, you must enable notifications in your preferences in the Snowflake web interface.
Creating a Resource Monitor with a Default Schedule¶
To create a resource monitor that uses the default schedule (i.e. starts monitoring immediately and resets on the first day of each calendar month):
- Make sure you are using the ACCOUNTADMIN role.
- Click on Account » Resource Monitors » Create Resource Monitor.
- Enter a name and quota.
- Specify whether the resource monitor will be used for monitoring your account or individual warehouses.
- Enter a threshold for at least one of the suspend actions. You can also specify up to five notify actions that simply send alerts without suspending any warehouses.
In SQL, this task is performed in two steps:
- Execute a CREATE RESOURCE MONITOR command, but do do not specify any scheduling properties.
- Execute an ALTER WAREHOUSE command to assign warehouses to the resource monitor or an ALTER ACCOUNT command to set the resource monitor for the account.
To create a monitor that starts monitoring immediately, resets at the beginning of each month, and suspends the assigned warehouse assigned when the credit usage reaches 100% of the credit quota:USE ROLE ACCOUNTADMIN; CREATE OR REPLACE RESOURCE MONITOR limit1 WITH CREDIT_QUOTA=1000 TRIGGERS ON 100 PERCENT DO SUSPEND; ALTER WAREHOUSE wh1 SET RESOURCE_MONITOR = limit1;
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.
To create a monitor that is similar to the first example, but suspends at 90% and suspends immediately at 100% to prevent all warehouses in the account from consuming credits after the quota has been reached: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; ALTER WAREHOUSE wh1 SET RESOURCE_MONITOR = limit1;
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.
To create a monitor that is similar to the first example, but lets the assigned warehouse exceed the quota by 10% and also includes two notification actions to alert account administrators as credit usage reaches the halfway and three-quarters points for the quota:USE ROLE ACCOUNTADMIN; CREATE OR REPLACE RESOURCE MONITOR limit1 WITH CREDIT_QUOTA=1000 TRIGGERS ON 50 PERCENT DO NOTIFY ON 75 PERCENT DO NOTIFY ON 100 PERCENT DO SUSPEND ON 110 PERCENT DO SUSPEND_IMMEDIATE; ALTER WAREHOUSE wh1 SET RESOURCE_MONITOR = limit1;
In this example:
- When 50% and 75% usage is reached, an alert notification is sent to all account administrators who have enabled notifications, but no other actions are performed.
- When 100% usage is reached, the assigned warehouse is suspended.
- If the warehouse is still running when 110% usage is reached, it is suspended immediately.
Creating a Resource Monitor with a Custom Schedule¶
To create a resource monitor that uses a schedule other than the default:
Execute a CREATE RESOURCE MONITOR command, with one or more of the following scheduling properties:
Modifying a Resource Monitor¶
You can modify the following properties for an existing resource monitor:
- Increase or decrease the credit quota for the monitor.
- If the monitor is monitoring your account, convert it to monitor individual warehouses.
- If the monitor is monitoring individual warehouses:
- Add or remove warehouses from the list.
- Convert it to monitor your account.
- Customize the schedule (frequency, start timestamp, and end timestamp) for the monitor.
- Add or remove actions, or modify the threshold percentages for existing actions.
Changing any of these properties does not affect the credit usage to-date for the monitor. All changes only affect credit usage after the changes are saved.
Resource monitors can be modified through either the web interface or SQL:
The SQL commands you use depend on the change you are making:
For example, to increase the credit quota for
Setting a Resource Monitor for Your Account¶
A resource monitor can be set for your account through either the web interface or SQL:
The process is the same as creating or modifying a resource monitor (see instructions in this topic).
In SQL, this task is performed in two steps:
To view whether a resource monitor is set for your account, use the web interface or the SHOW RESOURCE MONITORS command. The LEVEL column for a resource monitor displays whether it is set for your account or individual warehouses.
An account-level resource monitor does not control credit usage by the Snowflake-provided warehouses (used for Snowpipe, automatic reclustering, and materialized views); the monitor only controls the virtual warehouses created in your account.
Assigning Warehouses to a Resource Monitor¶
Warehouses can be assigned to an existing resource monitor through either the web interface or SQL:
Execute an ALTER WAREHOUSE command.
For example, using the resource monitor (
Viewing Resource Monitors¶
Resource monitors can be viewed through either the web interface or SQL:
Execute a SHOW RESOURCE MONITORS command.
In addition, using any role, you can execute a 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.