What you will be able to do
- Write a CREATE MASKING POLICY statement whose conditions use CURRENT_ROLE, IS_ROLE_IN_SESSION or an entitlement table, and apply it to a table or view column
- Choose between Dynamic Data Masking and External Tokenization for a given sensitive column
- Grant masking privileges so policy administrators are kept separate from object owners
- Manage the policy lifecycle with ALTER, SET, UNSET and DROP, and avoid the errors each one raises
- Assign a masking policy to a tag so that every tagged column is protected automatically
Key concept
Masking policy (query-time column protection) — A masking policy is a schema-level object attached to a column. It does not change the stored data. Every time a query reads the column, the policy's conditions decide whether that caller sees the real value, a partly masked value, a fully masked value or a tokenized value.
1.How masking policies protect a column at query time
Column-level security in Snowflake means attaching a masking policy to a column in a table or view. It comes in two features. Dynamic Data Masking masks plain-text data when a query runs. External Tokenization stores the data as tokens and turns them back into real values (detokenizes them) when a query runs. Both use the same policy object.
A policy has one data type, one or more conditions, and one or more masking functions. Because it is a separate object from the tables it protects, you write it once and attach it to any column with a matching data type. One email policy can cover thousands of email columns across databases and schemas. Changing the policy body changes protection everywhere it is attached, with no need to reattach it.
Snowflake rewrites the query so the policy expression replaces the column wherever it appears: in the SELECT list, in join predicates, in WHERE, in ORDER BY and in GROUP BY. This has a practical side effect. Suppose a policy returns 0 for an unauthorized role and that role joins on the masked column. The join compares the masked values, not the stored ones, so rows that match in the real data may not match in the query.
The other option is a secure view per audience, which does not scale. Every audience needs its own view, and every view grows its own BI dashboards. Masking policies avoid that sprawl because one table serves every role, and each role sees what its policy allows.
Checkpoint 1 of 9· Check yourself
A masking policy on the NUMBER column CUSTOMERS.CUSTOMER_ID returns the real value for the FINANCE role and 0 for every other role. ORDERS.CUSTOMER_ID has no policy. A user with the REPORTING role runs a query that joins ORDERS to CUSTOMERS on CUSTOMER_ID. What happens?
Snowflake rewrites the query so the policy expression replaces the column wherever it appears, including join predicates. For REPORTING, CUSTOMERS.CUSTOMER_ID becomes 0 and is compared with the unmasked ORDERS values, so rows that match in the stored data can fail to match.
“At query runtime, the masking policy is applied to the column at every location where the column appears.”Source: docs.snowflake.com
2.Dynamic Data Masking versus External Tokenization
The two features use the same policy syntax. They differ in where the protected value comes from. With Dynamic Data Masking, plain text is stored in Snowflake and the policy hides it from unauthorized roles. With External Tokenization, a third-party provider tokenizes the data before it is loaded, so Snowflake never stores the plain text. For authorized roles, the policy body calls an external function, which makes a REST call to the provider to detokenize the value. Everyone else sees the token.
CREATE MASKING POLICY employee_ssn_mask AS (val string) RETURNS string ->
CASE
WHEN CURRENT_ROLE() IN ('PAYROLL') THEN val
ELSE '******'
END;CREATE MASKING POLICY employee_ssn_detokenize AS (val string) RETURNS string ->
CASE
WHEN CURRENT_ROLE() IN ('PAYROLL') THEN ssn_unprotect(VAL)
ELSE val -- sees tokenized data
END;Take a card number that arrives already tokenized by a vault, where the support team sometimes needs the real number. That is a masking policy whose authorized branch calls the detokenizing external function. Table-level SELECT grants are not enough, because they cannot show the support role a value the table does not contain. A UDF that decrypts with a key stored next to the data is not tokenization either. Role mapping must also exist on both sides: the provider applies its own tokenization policy, which is defined outside Snowflake, to decide what to return.
Checkpoint 2 of 9· Fill the gap
A third-party provider tokenizes SSNs before they are loaded into Snowflake. Members of the PAYROLL role must see the real SSN, and everyone else should see the token. What goes in the authorized branch of this policy?
CREATE MASKING POLICY employee_ssn_detokenize AS (val string) RETURNS string ->
CASE
WHEN CURRENT_ROLE() IN ('PAYROLL') THEN ?
ELSE val -- sees tokenized data
END;The table stores only tokens, so returning val would show PAYROLL the token, not the SSN. In the authorized branch, the policy must call the external function ssn_unprotect, which makes a REST call to the provider to detokenize the value. That external function is the only structural difference from a Dynamic Data Masking policy.
Source: docs.snowflake.comCheckpoint 3 of 9· Exam question
A payment_card_number column arrives already tokenized by a third-party vault before it is loaded into Snowflake, and the support team occasionally needs the real card number revealed for verified customer calls. Which design correctly combines column-level security mechanisms for this requirement?
Correct answer: A — Apply a masking policy that calls an external function to detokenize the value, combining tokenization with masking for the roles allowed to see it.
- A. External tokenization keeps the sensitive value outside Snowflake as an opaque token, and a masking policy that invokes an external function to detokenize it at query time is exactly the documented pattern for combining the two so only authorized roles see the real value.
- B. Storing a decryption key alongside the tokenized data in the same table defeats the purpose of external tokenization, since anyone with column access could reconstruct the original value without ever calling the vault's detokenization service.
- C. A projection policy that blocks the column for everyone permanently would prevent the support team from ever recovering the real card number, which contradicts the stated requirement to reveal it for verified calls.
- D. Table-level SELECT grants control access to the whole table, not to whether a specific column shows the token or the detokenized value, so RBAC alone cannot satisfy the need for conditional column-level protection.
Sources3
3.Writing role-based conditions
Most policies branch on the caller's role. CURRENT_ROLE() checks only the role active in the session. That is exact, but it ignores role hierarchy: a role that inherits ANALYST does not pass CURRENT_ROLE() IN ('ANALYST'). When inherited or secondary roles should count, use IS_ROLE_IN_SESSION. For views and shares, the context functions INVOKER_ROLE and INVOKER_SHARE cover the same need.
A policy body can return a range of outputs:
- NULL, or a fixed string such as '********', for a full mask
REGEXP_REPLACEto keep part of the value, for example an email domainSHA2for a hash. Hashes can collide, so use this with care- a UDF, or
OBJECT_INSERTto mask a single key inside VARIANT data CURRENT_ACCOUNT()comparisons, so that a production account sees real values and development accounts do not
For entitlements that change often, look up a custom entitlement table instead of hard-coding role names. A subquery in the policy body must be wrapped in EXISTS. A memoizable function that caches the lookup works too: the policy calls is_role_authorized(CURRENT_ROLE()) and returns NULL when it is false.
Conditional masking passes more than one column to the policy. In email_visibility (email varchar, visibility string), the first argument is always the column being masked. The others are conditional columns from the same table or view. In that example, a row whose visibility is 'PUBLIC' shows its email to every role.
Checkpoint 4 of 9· Check yourself
Role SR_ANALYST has been granted the ANALYST role, so it inherits ANALYST. A masking policy on an email column unmasks only WHEN CURRENT_ROLE() IN ('ANALYST') THEN val. A user whose active role is SR_ANALYST queries the column. What do they see, and what change lets inherited roles count?
CURRENT_ROLE() returns only the role active in the session, so SR_ANALYST fails the IN ('ANALYST') check even though it inherits ANALYST. IS_ROLE_IN_SESSION takes role hierarchy into account. Table grants do not change how the policy decides, and EXISTS is only for wrapping subqueries.
“If role hierarchy needs to be considered, this policy could similarly use IS_ROLE_IN_SESSION to be more inclusive of other roles”Source: docs.snowflake.com
4.Privileges and segregation of duties
Masking policies separate the people who write policies from the people who own data. A security or privacy officer, usually a custom MASKING_ADMIN role, decides which columns are protected. The table owner does not. Secure views offer no such separation, which is a large part of why they fall short for governance. A well-written policy can also stop ACCOUNTADMIN and SECURITYADMIN from seeing data they have no need to see.
| Privilege | Granted on | What it allows |
|---|---|---|
| CREATE MASKING POLICY | Schema | Creating masking policies in that schema |
| APPLY MASKING POLICY | Account | Setting or unsetting any masking policy on any column. ACCOUNTADMIN has it by default. It grants no extra table privileges |
| APPLY | A single masking policy | Optional. Lets the policy owner allow object owners to set or unset that one policy (decentralized management) |
| OWNERSHIP | A single masking policy | Full control, including altering most of the policy's properties |
use role securityadmin; GRANT CREATE MASKING POLICY on SCHEMA <db_name.schema_name> to ROLE masking_admin; GRANT APPLY MASKING POLICY on ACCOUNT to ROLE masking_admin;Centralized management means MASKING_ADMIN applies every policy. In a large organization, where hundreds of teams own sensitive columns, the policy owner can grant APPLY ON MASKING POLICY to those teams' owner roles instead. Each team then attaches the shared policy to its own columns, while only the security team can change what the policy does. This is the hybrid or decentralized approach.
Checkpoint 5 of 9· Match them up
A security team is setting up segregation of duties for masking policies. Match each privilege to what it lets the grantee do.
Tap a term, then the definition that fits it.
CREATE MASKING POLICY is granted on a schema and APPLY MASKING POLICY on the account. Together they are the masking administrator's core privileges. APPLY on one policy is how the policy owner hands the set and unset operations to object owners (decentralized management), and only OWNERSHIP lets a role change what the policy does.
“used by a policy owner to decentralize the [un]set operations of a given masking policy on columns to the object owners”Source: docs.snowflake.com
5.Policy lifecycle: set, alter, replace, unset, drop
You can attach a policy to a column when you create the table or view, or later with ALTER. A column holds one masking policy at a time. Setting a second policy on a protected column fails. To swap policies in a single statement, add the FORCE keyword to the SET MASKING POLICY clause. Without FORCE, you first unset the old policy and then set the new one.
-- apply masking policy to a table column
ALTER TABLE IF EXISTS user_info MODIFY COLUMN email SET MASKING POLICY email_mask;To change what a policy does, run ALTER MASKING POLICY. The policy stays attached during the change, so the column is never unprotected. Use GET_DDL or DESCRIBE MASKING POLICY first to read the current body. Don't add IF EXISTS to the ALTER: if the name is wrong, the statement still reports success and nothing changes.
You cannot drop a policy while it is attached to anything. Detach it from each column with ALTER TABLE … MODIFY COLUMN email UNSET MASKING POLICY (or the ALTER VIEW equivalent), then run DROP MASKING POLICY. To audit where a policy is attached, query the Account Usage view POLICY_REFERENCES or the Information Schema table function of the same name. MASKING_POLICIES lists every masking policy in the account. To see which policies a particular query used, look in its Query Profile; QUERY_HISTORY does not record policy names.
Grant that role APPLY on the specific masking policy (GRANT APPLY ON MASKING POLICY policy_name TO ROLE role_name). Owning the clone does not include the right to unset a policy.
Checkpoint 6 of 9· Put it in order
A security team wants to retire the masking policy email_mask. It is still attached to email columns in several tables and views. Put the steps in the order that removes it without an error.
- 1.Unset email_mask from each of those columns with ALTER TABLE or ALTER VIEW … MODIFY COLUMN … UNSET MASKING POLICY
- 2.Run DROP MASKING POLICY email_mask
- 3.Query POLICY_REFERENCES to list every table and view column where email_mask is set
DROP MASKING POLICY fails while the policy is attached anywhere. It does not detach the policy silently. First find every attachment with POLICY_REFERENCES, then unset the policy from each column, and only then drop it.
“statement to UNSET the policy first, then try the DROP statement again.”Source: docs.snowflake.com
Checkpoint 7 of 9· Exam question
A data engineer runs ALTER TABLE customers MODIFY COLUMN email SET MASKING POLICY email_mask; on a column that already has a different masking policy, legacy_email_mask, applied. The statement fails with an error that a policy is already assigned. What lets the engineer replace the existing policy in one statement?
Correct answer: A — Reissue the ALTER TABLE statement with the FORCE keyword appended, which lets Snowflake swap the currently assigned policy for the new one directly.
- A. Snowflake blocks a second SET MASKING POLICY on a column that already has one unless the FORCE keyword is included, which explicitly authorizes replacing the currently assigned policy with the new one in a single ALTER TABLE statement.
- B. Snowflake does not grant column-level OWNERSHIP privileges, and ownership of the table would not bypass the policy-replacement check that FORCE is specifically designed to satisfy.
- C. Dropping and recreating the column would lose existing data and any dependent objects, which is unnecessary and far more disruptive than simply reissuing the ALTER TABLE statement with FORCE.
- D. Wrapping the statement in a transaction does not change how Snowflake validates masking policy assignment conflicts, since the FORCE keyword, not transactional scope, is what permits the replacement.
6.Tag-based masking: protect every tagged column at once
Tags are key–value labels attached to objects and columns, as explained in Object tagging and tag propagation. A tag-based masking policy sets a masking policy on the tag itself with ALTER TAG. Any column that carries the tag, and whose data type matches the policy signature, is then masked automatically. Nobody has to set the policy on each column.
A tag can hold one masking policy per data type. A common starting point is one generic policy each for STRING, NUMBER and TIMESTAMP_LTZ. Authorized roles see the raw value and every other role sees a fixed mask. Attach all of them to a single PII tag. Tag a newly loaded column and it is protected immediately. You can also set the tag-based policy at database, schema or table level. The policy body can branch on which policy came from the tag, or on the tag's string value on the column.
Checkpoint 8 of 9· Check yourself
Your governance team wants any column tagged PII to be masked automatically as soon as it is tagged. The PII columns include VARCHAR emails, NUMBER account IDs and TIMESTAMP_LTZ birth dates. No one should need to run a per-column ALTER TABLE. Which setup does this?
A tag-based policy masks only tagged columns whose data type matches the policy signature, and a tag can hold one masking policy per data type. You therefore need one policy per type, each returning a value of that type. A STRING policy cannot mask NUMBER or TIMESTAMP columns. Setting the policy on each column by hand defeats the purpose of putting it on the tag.
“You can apply the masking policy to one or more table/view columns with the matching data type.”Source: docs.snowflake.com
Sources5
7.Best practices for masking policies
Most masking-policy failures come from a few limits:
- Input and output types must match. A TIMESTAMP policy cannot return '*MASKED*'. Return a made-up timestamp such as
date_from_parts(0001, 01, 01)::timestamp_ntzinstead. - Keep conditional columns few. Snowflake evaluates every argument column at query time, and every column must come from the same table or view.
- Use EXISTS for subqueries and memoizable functions for mapping lookups.
- Be careful with hashes. SHA2 output can collide and can be joined on.
- Watch materialized views. A policy cannot go on a column that a materialized view uses, and a materialized view cannot include a masked column. Virtual columns cannot take a policy either; mask the source column.
- Test with two roles. Query once with the authorized role and once with PUBLIC, then confirm the masked output.
Checkpoint 9 of 9· Check yourself
Before release, a reviewer reads four draft masking policies. Which draft fails when someone runs CREATE MASKING POLICY?
The policy's input and output types must be the same. A TIMESTAMP policy that returns a string fails with an argument and return type mismatch error. The fix is to return a made-up timestamp, as the second draft does. SHA2 and an EXISTS-wrapped subquery are both allowed. Hashes only need care because they can collide.
“the input and output data types must match.”Source: docs.snowflake.com
Sources
(5)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“the masking policy is applied to the column at every location where the column appears”
↩︎ How masking policies protect a column at query time“can prohibit privileged users with the ACCOUNTADMIN or SECURITYADMIN role from unnecessarily viewing data”
↩︎ Privileges and segregation of duties“A column cannot be attached to multiple masking policies.”
↩︎ Policy lifecycle: set, alter, replace, unset, drop“Masking policy names are not included in the QUERY_HISTORY view.”
↩︎ Policy lifecycle: set, alter, replace, unset, drop“Policy <policy_name> cannot be dropped/replaced as it is associated with one or more entities.”
↩︎ Exam trap 3“At query runtime, the masking policy is applied to the column at every location where the column appears.”
↩︎ Checkpoint“statement to UNSET the policy first, then try the DROP statement again.”
↩︎ Checkpoint - 2.
“e.g. projections, join predicate, where clause predicate, order by, and group by”
↩︎ How masking policies protect a column at query time“Always use EXISTS when including a subquery in the masking policy body.”
↩︎ Writing role-based conditions“This privilege controls who can [un]set masking policies on columns and is granted to the ACCOUNTADMIN role by default.”
↩︎ Privileges and segregation of duties“Using a hashing function in a masking policy may result in collisions; therefore, exercise caution with this approach.”
↩︎ Best practices for masking policies“Currently, Snowflake does not support different input and output data types in a masking policy”
↩︎ Exam trap 2“used by a policy owner to decentralize the [un]set operations of a given masking policy on columns to the object owners”
↩︎ Checkpoint“the input and output data types must match.”
↩︎ Checkpoint - 3.
“Masking policies solve this management challenge by avoiding an explosion of views and dashboards to manage.”
↩︎ How masking policies protect a column at query time“External Tokenization requires a third-party tokenization provider to tokenize data before loading data into Snowflake.”
↩︎ Dynamic Data Masking versus External Tokenization“role mapping must exist in Snowflake and the tokenization provider to ensure that the correct data can be returned from a given query”
↩︎ Dynamic Data Masking versus External Tokenization“The first column always specifies the column to mask.”
↩︎ Writing role-based conditions“This command does not require unsetting a masking policy from a column, if the masking policy is set on a column.”
↩︎ Policy lifecycle: set, alter, replace, unset, drop“Minimize the number of column arguments in the policy definition.”
↩︎ Best practices for masking policies“sensitive data in Snowflake is not modified in an existing table (i.e. no static masking)”
↩︎ Key concept“Object owners (i.e. the role that has the OWNERSHIP privilege on the object) do not have the privilege to unset masking policies.”
↩︎ Exam trap 1“Object owners cannot view column data in which a masking policy applies.”
↩︎ Prediction“You can apply the masking policy to one or more table/view columns with the matching data type.”
↩︎ Checkpoint - 4.
“If role hierarchy needs to be considered, this policy could similarly use IS_ROLE_IN_SESSION to be more inclusive of other roles”
↩︎ Writing role-based conditions - 5.
“allow a masking policy to be set on a tag using an ALTER TAG command”
↩︎ Tag-based masking: protect every tagged column at once“The tag can support one masking policy for each data type that Snowflake supports.”
↩︎ Tag-based masking: protect every tagged column at once