CREATE ROW ACCESS POLICY
statementsupportedGovernance (policies, tags, data quality)Enterprise+Creates a new row access policy in the current/specified schema or replaces an existing row access policy. After creating a row access policy, add the policy to a table using an ALTER TABLE command or a view using an ALTER VIEW command. This command supports the following variants: - CREATE OR ALTER ROW ACCESS POLICY: Creates a row access policy if it doesn’t exist or alters an existing row access policy.
Syntax
supportedpartialnot supportednot scored
Snowflake supports the following syntax to create a row access policy.
CREATE [ OR REPLACE ] ROW ACCESS POLICY [ IF NOT EXISTS ] <name> AS
( <arg_name> <arg_type> [ , ... ] ) RETURNS BOOLEAN -> <body>
[ COMMENT = '<string_literal>' ]
For more information, see CREATE OR ALTER <object>.
CREATE OR ALTER ROW ACCESS POLICY <name> AS
( <arg_name> <arg_type> [ , ... ] ) RETURNS BOOLEAN -> <body>
[ COMMENT = '<string_literal>' ]
Parameters
| Name | Required | Description |
|---|---|---|
name | required | Identifier for the row access policy; must be unique for your schema. The identifier value must start with an alphabetic character and cannot contain spaces or special characters unless the entire identifier string is enclosed in double quotes (for example, “My object”). Identifiers enclosed in double quotes are also case-sensitive. For more details, see Identifier requirements. |
{AS ( <arg_name> <arg_type> [ , ... ] )} | required | The signature for the row access policy. A signature specifies a set of attributes that must be considered to determine whether the row is accessible. The attribute values come from the database object (for example, a table or view) to be protected by the row access policy. |
RETURNS BOOLEAN | required | A row access policy must evaluate to true or false. A user that queries a table protected by a row access policy sees rows in the output based on how the body is written. |
body | required | SQL expression that operates on the argument values in the signature to determine which rows to return for a query on a table that is protected by a row access policy. The body can be any boolean-valued SQL expression. Snowflake supports expressions that invoke User-defined functions overview, Writing external functions, and expressions that use sub-queries. |
COMMENT = 'string_literal' | optional | Specifies a comment for the row access policy. Default: No value |
Usage notes
- Including one or more subqueries in the policy body may cause errors. When possible, limit the number of subqueries, limit the number of JOIN operations, and simplify WHERE clause conditions.
- If a database object has both a row access policy and one or more masking policies, the row access policy is evaluated first. For more information on row access policies during query runtime, see Understanding row access policies.
- A given table or view column can be specified in either a masking policy signature or a row access policy signature. In other words, the same column cannot be specified in both a masking policy signature and a row access policy signature at the same time. For more information, see CREATE MASKING POLICY.
- You cannot change the policy signature (that is, argument name or input/output data type) using CREATE OR REPLACE ROW ACCESS POLICY if the policy is attached to a table or view, or using ALTER ROW ACCESS POLICY. If you need to change the signature, execute a DROP ROW ACCESS POLICY statement on the policy and create a new row access policy.
- If the policy body contains a mapping table lookup, create a centralized mapping table and store the mapping table in the same database as the protected table. This is particularly important if the body calls the IS_DATABASE_ROLE_IN_SESSION function. For details, see the function usage notes.
- A data sharing provider cannot create a row access policy in a reader account.
- If you specify the CURRENT_DATABASE or CURRENT_SCHEMA function in the body of a masking or row access policy, the function returns the database or schema that contains the protected table, not the database or schema in use for the session.
- Regarding metadata: Attention Customers should ensure that no personal data (other than for a User object), sensitive data, export-controlled data, or other regulated data is entered as metadata when using the Snowflake service. For more information, see Metadata fields in Snowflake.
- The OR REPLACE and IF NOT EXISTS clauses are mutually exclusive. They can’t both be used in the same statement.
- CREATE OR REPLACE <object> statements are atomic. That is, when an object is replaced, the old object is deleted and the new object is created in a single transaction.
- All limitations of the ALTER ROW ACCESS POLICY command apply.
- The row access policy signature (argument names and data types) can’t be changed for an existing policy. If you need to change the signature, drop the existing policy and create a new one.
- Renaming a row access policy is not supported.
- Setting or unsetting a tag is not supported.
Access control requirements
A role used to execute this operation must have the following privileges at a minimum: Privilege | Object | Notes CREATE ROW ACCESS POLICY | Schema | OWNERSHIP | Row Access Policy | Required to execute a CREATE OR ALTER ROW ACCESS POLICY statement for an existing row access policy. Operating on an object in a schema requires at least one privilege on the parent database and at least one privilege on the parent schema. For instructions on creating a custom role with a specified set of privileges, see Creating custom roles. For general information about roles and privilege grants for performing SQL actions on securable objects, see Overview of Access Control. For additional details on masking policy DDL and privileges, see Managing Column-level Security.