docs/en/sql-reference/statements/create/masking-policy.md
import CloudOnlyBadge from '@theme/badges/CloudOnlyBadge';
<CloudOnlyBadge/>Creates a masking policy, which allows dynamically transforming or masking column values for specific users or roles when they query a table.
:::tip Masking policies provide column-level data security by transforming sensitive data at query time without modifying the stored data. :::
Syntax:
CREATE MASKING POLICY [IF NOT EXISTS | OR REPLACE] policy_name ON [database.]table
UPDATE column1 = expression1 [, column2 = expression2 ...]
[WHERE condition]
TO {role1 [, role2 ...] | ALL | ALL EXCEPT role1 [, role2 ...]}
[PRIORITY priority_number]
The UPDATE clause specifies which columns to mask and how to transform them. You can mask multiple columns in a single policy.
Examples:
UPDATE email = '***masked***'UPDATE email = concat(substring(email, 1, 3), '***@***.***')UPDATE email = concat('masked_', substring(hex(cityHash64(email)), 1, 8))UPDATE email = '***@***.***', phone = '***-***-****'The optional WHERE clause allows conditional masking based on row values. Only rows matching the condition will have the masking applied.
Example:
CREATE MASKING POLICY mask_high_salaries ON employees
UPDATE salary = 0
WHERE salary > 100000
TO analyst;
In the TO section, specify which users and roles the policy should apply to.
TO user1, user2: Apply to specific users/rolesTO ALL: Apply to all usersTO ALL EXCEPT user1, user2: Apply to all users except specified ones:::note Unlike row policies, masking policies do not affect users who don't have the policy applied. If no masking policy applies to a user, they see the original data. :::
When multiple masking policies target the same column for a user, the PRIORITY clause determines the application order. Policies are applied in order from highest to lowest priority.
Default priority is 0. Policies with the same priority are applied in an undefined order.
Example:
-- Applied second (lower priority)
CREATE MASKING POLICY mask1 ON users
UPDATE email = '[email protected]'
TO analyst
PRIORITY 1;
-- Applied first (higher priority)
CREATE MASKING POLICY mask2 ON users
UPDATE email = '[email protected]'
TO analyst
PRIORITY 10;
-- analyst sees '[email protected]' because it's applied last
:::note Performance Considerations