Feature policy rules¶
A feature policy can carry a YAML body with rules that block a statement only when a condition you define is true. These rules let you allow users to create or change objects of a type in general but block specific uses, such as a particular creation option or a property value you don’t permit.
For example, a feature policy can:
- Block temporary or transient tables.
- Restrict object creation to a specific schema.
- Require request logging on Cortex Search Services, both when a service is created and whenever it’s altered.
- Block an
ALTERthat changes a task’s warehouse.
To block every creation of an object type, regardless of the statement, list
the type in BLOCKED_OBJECT_TYPES_FOR_CREATION instead. See
Blockable object types.
Creation rules and creation and alteration rules¶
A policy body can contain two lists of rules. Both use the same rule properties, conditions, and blocking expressions:
blocked_creation_rules: Creation rules. They apply only to statements that create an object:CREATEandCREATE OR ALTER.blocked_creation_alteration_rules: Creation and alteration rules. They apply toCREATE,CREATE OR ALTER, and standaloneALTERstatements.
Creation rules are the simpler choice for properties that can’t change after
an object is created, such as whether a table is temporary or transient. An
ALTER can’t change these properties, so checking them when the object is
created is enough. On CREATE, every property has a value, including the
default for a property that the statement omits, so an expression such as
$IS_TEMPORARY always evaluates to TRUE or FALSE and needs no extra
handling.
A creation rule can test any supported property, including one that an
ALTER can change later. In that case, the rule doesn’t prevent a later
standalone ALTER from changing the property, even to a value that the rule
blocks at creation. For example, a creation rule that requires
REQUEST_LOGGING = TRUE on a Cortex Search Service doesn’t stop a user from
later running ALTER CORTEX SEARCH SERVICE ... SET REQUEST_LOGGING = FALSE.
Use creation and alteration rules when a requirement on a property that an
ALTER can change must hold for the life of the object, not only when it’s
created. A creation and alteration rule for REQUEST_LOGGING blocks both of
the statements in the previous example. Because an ALTER reports only the
properties that it sets, expressions in these rules must handle properties
that are missing. See
Properties on ALTER and
Safely handling missing properties.
Policy body syntax¶
The body is a YAML document attached to the policy with an AS clause:
The YAML body has the following structure:
The policy body supports the following top-level keys:
conditions: Optional. A list of named, reusable expressions that rules reference throughblock_when_any. See Condition properties.blocked_creation_rules: Optional. A list of rules that block matchingCREATEandCREATE OR ALTERstatements. See Rule properties.blocked_creation_alteration_rules: Optional. A list of rules that block matchingCREATE,CREATE OR ALTER, and standaloneALTERstatements. See Rule properties.
Condition properties¶
The conditions list is optional. Each condition gives a name to a SQL Boolean
expression, so you can define the expression once and reuse it across multiple
rules instead of repeating it in each rule’s block_when. A rule references
conditions by name in its block_when_any list. Rules in both
blocked_creation_rules and blocked_creation_alteration_rules can reference
the same conditions.
Each entry in conditions supports these properties:
name: Required. The name that rules use to reference the condition inblock_when_any. Must be unique withinconditions.expression: Required. A SQL Boolean expression. See Blocking expressions. If a rule inblocked_creation_alteration_rulesreferences the condition, the expression must evaluate toTRUEorFALSEon bothCREATEandALTER. See Validation rules.
Rule properties¶
Each entry in blocked_creation_rules and blocked_creation_alteration_rules
supports these properties:
object_type: Required. The object type that the rule applies to, such asTABLE,TASK, orCORTEX_SEARCH_SERVICE, or theALLwildcard. Snowflake rejects an unsupported type when the policy is created or altered. See Supported object types.block_when: Optional. A SQL Boolean expression. The rule blocks the statement when the expression evaluates toTRUE. Can’t be an empty string.block_when_any: Optional. A list of condition names. The rule blocks the statement when any of the named conditions evaluates toTRUE. Must contain at least one name, and each name must match an entry inconditions.error_message: Optional. Text that Snowflake appends to the error for a blocked statement. See Custom error messages.
A rule can specify block_when or block_when_any, but not both. If a rule
specifies neither, it blocks every statement of its object_type that the
list applies to: every CREATE for blocked_creation_rules, and every CREATE
and ALTER for blocked_creation_alteration_rules. An ALL rule must specify
one of them.
An ALL rule applies to every supported object type, not to every object type
in Snowflake. For the list of supported types, see
Supported object types.
The expression in an ALL rule can use only the following properties:
TYPEREQUEST_OPERATIONIS_TEMPORARYIS_TRANSIENTDATABASESCHEMA
To use a property that applies only to specific object types, such as
WAREHOUSE on TASK, write a rule for that object type instead.
Attention
A property used in an ALL rule can be NULL for some of the object types
that the rule applies to. A comparison against NULL evaluates to NULL, and a
rule whose expression evaluates to NULL blocks the statement. To avoid
blocking statements unintentionally, check for NULL before comparing a
property’s value. See Using NULL checks.
Supported object types¶
A rule’s object_type can name any of the following types, in either singular
or plural form (for example, both TABLE and TABLES are accepted):
| Category | Object types |
|---|---|
| Account-level | COMPUTE_POOL, DATABASE, WAREHOUSE |
| Database-level | SCHEMA |
| Schema-level | APPLICATION_SERVICE, ARTIFACT_REPOSITORY, AGENT, CORTEX_SEARCH_SERVICE, DYNAMIC_TABLE, FILE_FORMAT, FUNCTION, GIT_REPOSITORY, ICEBERG_TABLE, MATERIALIZED_VIEW, MCP_SERVER, PROCEDURE, RESTRICTED_SESSION_SCOPE, SECRET, STAGE, TABLE, TASK, VIEW, WORKSPACE |
Attention
Account-level types are evaluated only when a native app creates the object and the policy applies to the application’s database. They have no effect for objects that users create directly.
Each of these types has at least one object property that a blocking expression
can test. For the properties that apply to each type, see
Object properties. The ALL
wildcard covers every supported type.
To block every creation of other object types, list them in
BLOCKED_OBJECT_TYPES_FOR_CREATION. See Blockable object types.
Blocking expressions¶
This section applies to both a rule’s block_when expression and a
condition’s expression. Each is a SQL Boolean expression written as a quoted
string. Snowflake validates the expression when the policy is created or
altered. An expression in blocked_creation_alteration_rules is validated once
for a CREATE and once for an ALTER.
Object properties¶
A blocking expression can refer to properties of the object that the statement creates or changes, such as the values that the statement sets. It can also refer to properties of the operation’s context, such as the type of statement. Refer to a property in either of the following ways:
$<PROPERTY>: Resolves the property directly. The variable has the same type as the property. For example,$IS_TRANSIENTis a Boolean, so use it directly or compare it with an unquotedTRUEorFALSE(for example,$IS_TRANSIENTorNOT $IS_TRANSIENT).$SCHEMAis a string, so compare it with a string literal (for example,$SCHEMA = 'STAGING'). Using an unrecognized name, or a property that doesn’t apply to the rule’sobject_type, is a compilation error when the policy is created or altered.SYS_CONTEXT('SNOWFLAKE$REQUEST', 'GET_OBJECT_PROPERTY', '<property>'): The full form. Always returns a string, so Boolean comparisons require= 'TRUE'. A property that doesn’t apply to the rule’sobject_typereturnsNULLat runtime instead of causing an error when the policy is created.
For example, the following two rules are equivalent:
The following properties describe the context of the operation: what kind of
statement it is and what type of object it acts on. They apply to every object
type, and are populated on both CREATE and ALTER:
| Property | Type | Returns |
|---|---|---|
REQUEST_OPERATION | String | The kind of statement being evaluated: CREATE for CREATE and CREATE OR ALTER, and ALTER for a standalone ALTER. Always CREATE in blocked_creation_rules. |
TYPE | String | The type of the object being created or altered, using the same name as a rule’s object_type, for example TABLE, AGENT, or TASK. Useful in ALL rules and in shared conditions. |
The following properties describe attributes of the object itself. Each one is
available only for the listed object types. The notes describe the values on
CREATE. For values on ALTER, see
Properties on ALTER.
| Property | Type | Object types | Notes |
|---|---|---|---|
IS_TEMPORARY | Boolean | Every supported type | TRUE for CREATE TEMPORARY ... of TABLE, VIEW, STAGE, FILE_FORMAT, FUNCTION, PROCEDURE, SECRET, or AGENT. FALSE otherwise. |
IS_TRANSIENT | Boolean | Every supported type | TRUE for CREATE TRANSIENT ... of TABLE, ICEBERG_TABLE, DYNAMIC_TABLE, SCHEMA, or DATABASE. FALSE otherwise. |
REQUEST_LOGGING | Boolean | CORTEX_SEARCH_SERVICE | FALSE when the statement omits the property. |
WAREHOUSE | String | TASK | NULL for a serverless task. |
EXTERNAL_VOLUME | String | ICEBERG_TABLE | The external volume resolved for the request, which can come from the table, schema, database, or account level. NULL if none. |
DATABASE | String | Every supported type except DATABASE | The name of the database that contains the object. |
SCHEMA | String | Every supported schema-level type | The name of the schema that contains the object. |
IS_TEMPORARY and IS_TRANSIENT are FALSE for object types that don’t
support that creation option, so a single rule applied across mixed object
types behaves predictably. For example, $IS_TRANSIENT is FALSE for STAGE.
For the other properties, using the $<PROPERTY> form in a rule for an object
type that isn’t listed causes an error when the policy is created or altered.
For example, $WAREHOUSE in a TABLE rule and $SCHEMA in a DATABASE or
SCHEMA rule are both rejected. The
SYS_CONTEXT('SNOWFLAKE$REQUEST', 'GET_OBJECT_PROPERTY', '<property>') form
returns NULL for those object types instead.
Properties on CREATE and CREATE OR ALTER¶
A blocking expression can read the properties that the statement explicitly sets. It can also read properties that the statement doesn’t set but that have a default or a resolved value for the object type. Those properties report that value.
- Boolean properties are always present. If the statement omits one, the value
is that property’s default.
IS_OBJECT_PROPERTY_PRESENTreturnsTRUE. - Other properties that have a default or a resolved value for the object type,
such as
EXTERNAL_VOLUMEonICEBERG_TABLE, are present at that value when the statement omits them.IS_OBJECT_PROPERTY_PRESENTreturnsTRUE. - Properties without a default are
NULLwhen the statement omits them or sets them toNULL.IS_OBJECT_PROPERTY_PRESENTreturnsTRUEonly when the statement supplies a value. - The context properties (
REQUEST_OPERATIONandTYPE) are always present, andDATABASEandSCHEMAare always present when they apply to the object type.
Properties on ALTER¶
Rules in blocked_creation_alteration_rules are also evaluated for a
standalone ALTER. On an ALTER, a blocking expression can read only the
object properties that the statement sets or unsets, plus the context
properties, DATABASE, and SCHEMA. Object properties that the statement doesn’t mention don’t
report a default or a resolved value.
- Object properties such as
IS_TEMPORARY,REQUEST_LOGGING, andWAREHOUSEare present only when the statement sets or unsets them. That includes Boolean properties. An omitted Boolean is absent: reading it returnsNULL, andIS_OBJECT_PROPERTY_PRESENTreturnsFALSE. TYPEandREQUEST_OPERATIONare populated on everyALTER.DATABASEandSCHEMAare populated when the object type has that container, and areNULLotherwise, the same as onCREATE.
Safely handling missing properties¶
A string property can be NULL, for example WAREHOUSE on a serverless task.
A comparison against NULL is itself NULL, and a rule whose expression
evaluates to NULL blocks the statement. For example,
$WAREHOUSE NOT IN ('CORP_M', 'CORP_XL') is NULL for a serverless task, so
the rule blocks every serverless task, even if you intended to allow them.
Missing properties are more common in blocked_creation_alteration_rules,
because an ALTER reports only the properties it mentions. For example,
NOT $REQUEST_LOGGING is NULL for an ALTER CORTEX SEARCH SERVICE that
doesn’t set REQUEST_LOGGING. Snowflake rejects an alteration rule like this
when you create or alter the policy. See
Validation rules.
To write a rule that behaves predictably for every statement, use one of the following approaches instead of comparing the property directly.
Using NULL checks¶
Test whether a property is NULL before comparing its value. A property is
NULL when it has no value, when a statement explicitly sets or unsets it to
NULL, and when an ALTER doesn’t mention it. An IS NOT NULL check makes the
expression FALSE instead of NULL in those cases.
The following rule requires that a task’s warehouse, when set, is CORP_M or
CORP_XL. A task without a warehouse is allowed:
The rule handles each statement as follows:
CREATE TASKwithWAREHOUSE = CORP_MorWAREHOUSE = CORP_XL: allowed.CREATE TASKwith a different warehouse: blocked.CREATE TASKwith no warehouse (a serverless task): allowed.
The same expression works in blocked_creation_alteration_rules, where it also
covers changes to existing tasks:
In addition to the CREATE TASK cases in the previous example, the rule handles ALTER as
follows:
ALTER TASK ... SET WAREHOUSE = CORP_MorCORP_XL: allowed.ALTER TASK ... SET WAREHOUSEto a different warehouse: blocked.ALTER TASK ... UNSET WAREHOUSE: allowed.ALTER TASKthat doesn’t mentionWAREHOUSE: allowed.
Without the IS NOT NULL check, the NOT IN comparison would be NULL for an
ALTER that doesn’t mention WAREHOUSE, and Snowflake would reject the
alteration rule when you create the policy.
Using IS_ OBJECT_ PROPERTY_ SET_ TO¶
Use IS_OBJECT_PROPERTY_SET_TO to require that a property has a specific value
whenever a statement supplies it. The function returns TRUE when the
statement sets the property to the value, or when the statement doesn’t supply
the property at all. The value is compared case-insensitively, and an explicit
NULL doesn’t match any value. An omitted Boolean is present at its default,
so the function compares that default. The function returns NULL for an
unrecognized property name.
The following rule requires that a task that names a warehouse uses
CORP_M:
The rule handles each statement as follows:
CREATE TASKwithWAREHOUSE = CORP_M: allowed.CREATE TASKwith a different warehouse: blocked.CREATE TASKwith no warehouse (a serverless task): allowed.
Because the function returns TRUE when the statement doesn’t mention the
property, it’s well suited to blocked_creation_alteration_rules: it allows an
ALTER that leaves the property alone. The following rule requires request
logging on Cortex Search Services, both at creation and on every later change:
The rule handles each statement as follows:
CREATEorCREATE OR ALTERwithREQUEST_LOGGING = TRUE: allowed.CREATEorCREATE OR ALTERthat omitsREQUEST_LOGGING: blocked. The omitted Boolean is present at its default,FALSE.ALTER ... SET REQUEST_LOGGING = TRUE: allowed.ALTER ... SET REQUEST_LOGGING = FALSE: blocked.ALTERthat doesn’t mentionREQUEST_LOGGING: allowed.
Using IS_ OBJECT_ PROPERTY_ PRESENT¶
Use IS_OBJECT_PROPERTY_PRESENT to block a statement based on whether a
property is present, regardless of its value. The function returns TRUE when
the property is present and FALSE when it isn’t. For the properties that
count as present, see
Properties on CREATE and CREATE OR ALTER
and Properties on ALTER.
The function returns NULL for an unrecognized property name.
The following rule allows only serverless tasks:
The rule handles each statement as follows:
CREATE TASKwith anyWAREHOUSE: blocked.CREATE TASKwith no warehouse (a serverless task): allowed.
On an ALTER, a property is present when the statement sets or unsets it. The
following rule blocks any ALTER that changes a task’s warehouse:
The rule handles each statement as follows:
CREATE TASKorCREATE OR ALTER TASK, with or withoutWAREHOUSE: allowed, becauseREQUEST_OPERATIONisCREATE.ALTER TASK ... SET WAREHOUSE = <name>: blocked.ALTER TASK ... UNSET WAREHOUSE: blocked.ALTER TASKthat doesn’t mentionWAREHOUSE: allowed.
The REQUEST_OPERATION check limits the rule to ALTER. Without it, a
CREATE TASK that sets WAREHOUSE would also be blocked, because the property
is present on that statement.
Each of these expressions evaluates to TRUE or FALSE for every statement,
so a missing property never causes the rule to block the statement.
Validation rules¶
Snowflake rejects the following expressions when the policy is created or altered:
- Expressions that don’t evaluate to
TRUEorFALSE, such as a numeric or string literal. Inblocked_creation_alteration_rules, this includes an expression that’sNULLon anALTERbecause the statement doesn’t mention a property that the expression uses. - Expressions that reference tables or other database objects (for example,
(SELECT COUNT(*) FROM t) > 0). - Expressions that call a user-defined function (UDF) or user-defined table function (UDTF). Only built-in functions are allowed.
- Expressions that call functions with side effects (for example,
SYSTEM$RUN_QUERY_SYNC). - Expressions that call functions that must run in a warehouse.
- Expressions that call a function that doesn’t exist.
- Expressions that use
$<NAME>where<NAME>isn’t a recognized object property, or where the property doesn’t apply to the rule’sobject_type(for example,$WAREHOUSEin aTABLErule).
Attention
An expression that compares a property can evaluate to NULL at runtime, and
a rule whose expression evaluates to NULL blocks the statement. Validation
doesn’t catch the following cases:
- The property has no value for the statement, for example
WAREHOUSEon a serverless task. - The expression uses
SYS_CONTEXTto read a property that doesn’t apply to the rule’sobject_type, or a misspelled property name. - The statement explicitly sets the property to
NULL. This applies to both rule lists.
To avoid these failures, check for NULL before comparing a property’s value.
See Using NULL checks.
How statements are evaluated¶
Snowflake evaluates the rules that apply to the statement:
- A
CREATEorCREATE OR ALTERis checked againstblocked_creation_rulesandblocked_creation_alteration_rules. Property values follow Properties on CREATE and CREATE OR ALTER, including defaults. - A standalone
ALTERis checked againstblocked_creation_alteration_rulesonly. Property values follow Properties on ALTER. Ablocked_creation_rulesentry never blocks anALTER.
Snowflake evaluates CREATE OR ALTER as a creation, even when the object
already exists. The statement uses creation property values, and
REQUEST_OPERATION is CREATE.
Custom error messages¶
When a rule blocks the statement, the statement is denied. By default, the error names the object type and points to the effective feature policy, for example:
Add an error_message to a rule to tell the user why the statement was blocked
and what to do instead:
When this policy is applied to all databases, a CREATE TRANSIENT TABLE statement
fails with an error that includes the rule’s text after Your policy::
The following rules apply to error_message:
- The value is trimmed of leading and trailing whitespace. The trimmed text must be 1 to 512 characters long.
- The value must be a single line. Newlines, tabs, and other control characters aren’t allowed.
- The sequence
${, followed later by}, is reserved and isn’t allowed. - Snowflake validates the value when the policy is created or altered. An invalid value causes the statement to fail.
Snowflake shows the message from the first rule in the list that blocks the
statement. If a statement is blocked because its object type is listed in
BLOCKED_OBJECT_TYPES_FOR_CREATION, Snowflake shows the generic error without any
rule’s error_message, even if a rule for the same object type also matches.
If both blocked_creation_rules and blocked_creation_alteration_rules block a
CREATE, Snowflake shows the message from the matching blocked_creation_rules
entry.
When an alteration rule blocks a standalone ALTER, the error starts with
Alter instead of Create. For example, the rule in
Using IS_OBJECT_PROPERTY_PRESENT
produces the following error for ALTER TASK ... SET WAREHOUSE:
Examples¶
The following examples assume the feature policy is attached to all databases, an application, or a specific database after creation. For attachment syntax, see Apply a feature policy.
Each example includes an error_message. Snowflake recommends adding one to
every rule so that users who are blocked understand why and know what to do
instead. For more information, see
Custom error messages.
Block all temporary tables¶
Block all transient tables¶
Transient tables don’t carry the same Time Travel and Fail-safe protections as
permanent tables. A policy that requires only permanent tables can block
CREATE TRANSIENT TABLE statements:
To block both temporary and transient tables in one rule, see the example in Custom error messages.
Block transient and temporary objects across multiple object types¶
A single named condition can be reused across object types, including object
types that don’t support TRANSIENT (such as STAGE, VIEW, FUNCTION,
PROCEDURE). For those types, $IS_TRANSIENT is FALSE, so the rule fires
only on temporary creations:
Block serverless tasks¶
Serverless tasks are tasks created without a WAREHOUSE. The following policy
permits warehouse-attached tasks but blocks serverless ones:
Require request logging for Cortex Search Services¶
The following policy blocks creating a Cortex Search Service unless the statement
sets REQUEST_LOGGING = TRUE. Because REQUEST_LOGGING defaults to FALSE, a
statement that omits the property is also blocked. For more information about
request logging, see
Enable request logging.
This rule applies only when a service is created. It doesn’t block an
ALTER CORTEX SEARCH SERVICE statement that later disables request logging. To
also block that change, use blocked_creation_alteration_rules. See
Using IS_OBJECT_PROPERTY_SET_TO.
Block a property across every object type with ALL¶
Use the ALL wildcard to apply one rule to every supported object type without
listing each one. Because Boolean properties such as $IS_TEMPORARY are FALSE
for object types that can’t be temporary, the following policy blocks every
temporary creation across all types and permits everything else:
To exempt specific types from an ALL rule, test $TYPE. The following policy
blocks every temporary creation except temporary tables:
Restrict object creation to a specific schema¶
Use the $SCHEMA variable to block creations outside an approved schema.
A schema-level object such as a table always carries a SCHEMA value, so the
comparison never evaluates to NULL:
The $SCHEMA variable returns the schema name as stored in the catalog (uppercase
unless the schema was created with a quoted, case-sensitive identifier). The
companion $DATABASE variable restricts by containing database in the same way.
Block creation based on user or session properties¶
A block_when expression can use session context functions such as
CURRENT_USER and
IS_ROLE_IN_SESSION. The
following policy lets only specific users or roles create application services:
With this policy in place:
- The user
APPMGRcan create application services. - Any user with the
APP_CREATORrole active in the session, as a primary or secondary role, can create application services. - Any other user is blocked.
A creation rule is enough here because an ALTER statement doesn’t change who
created the service.
Block every table¶
Omit block_when to block every creation of an object type, regardless of
request attributes:
Unlike listing TABLE in BLOCKED_OBJECT_TYPES_FOR_CREATION, this rule shows
its error_message when it blocks a statement.
Reuse a condition across rules¶
Combine type-level blocks and rules¶
BLOCKED_OBJECT_TYPES_FOR_CREATION and blocked_creation_rules can be used
together in a single policy. Listing an object type in
BLOCKED_OBJECT_TYPES_FOR_CREATION blocks every creation of that type
regardless of any rule. Rules covering different object types are evaluated
independently. The following policy blocks every warehouse creation and also
blocks temporary tables:
Block every ALTER of a task¶
REQUEST_OPERATION is ALTER only for a standalone ALTER. The following
policy blocks ALTER TASK and still allows CREATE TASK and
CREATE OR ALTER TASK:
Block every CREATE and ALTER of one type¶
Omit block_when from an alteration rule to block every CREATE and every
ALTER of that type:
Block ALTER statements across every object type with ALL¶
REQUEST_OPERATION applies to every object type, so an ALL rule can use it.
The following policy blocks standalone ALTER statements for every supported
type and still allows CREATE:
Combine creation rules and alteration rules¶
blocked_creation_rules and blocked_creation_alteration_rules can appear in
the same body, and their rules can share conditions. The following policy
blocks temporary tables at creation time and blocks every alter of a task:
A condition written for creation defaults can’t be referenced from an
alteration rule. For example, a condition with the expression
$IS_TEMPORARY = FALSE is NULL on an ALTER that doesn’t set the property,
so Snowflake rejects a policy that names it in
blocked_creation_alteration_rules.
Modify or remove a policy body¶
Use ALTER FEATURE POLICY to set, replace, or clear the body of an existing policy.
To replace the body, use the AS clause, with or without other SET clauses:
To remove the body but keep any BLOCKED_OBJECT_TYPES_FOR_CREATION clause:
To inspect the current body, use
DESCRIBE FEATURE POLICY. The body appears as the
policy_definition property in the result.
Apply a policy with rules¶
Rules are enforced through the same attachments as the rest of the feature policy: all native apps, a specific application, all databases, a specific database, or all personal databases. The effective policy for a statement is resolved from the object’s containing database, falling back to the policy attached to all databases. For attachment syntax and precedence, see Apply a feature policy and Feature policy precedence.