Before you beginSet up the Maple Market governance lab
Why hiding an email still leaves a sales row visible
Masking changes what a value looks like. An India analyst could still see US order amounts if a table exposes every row. Row-level security limits which rows survive. A row access policy returns a Boolean, meaning TRUE or FALSE. Only rows with a TRUE result are visible. A NULL result does not admit the row.
Snowflake applies a policy using a secure inline view and passes the bound column values into it. The policy owner’s rights allow it to read the entitlement table. Readers need SELECT on SALES, but they do not need rights on the entitlement table. The role context rules still test the caller’s active role hierarchy. Masking is evaluated after row access when both apply. Use separate fields for the regional rule and email masking in this lab.
- Three sales rowsPass REGION and role context to Row access policy
- Row access policyKeep India row to Email masking policy
- Email masking policyReturn permitted values to Query result
- Three sales rows
- IN, EU and US.
- Row access policy
- E.g. M18_IN is entitled to IN.
- Email masking policy
- Replace buyer email for regional reporting.
- Query result
- Sale 701, amount 100, REDACTED.
Choose a short role rule or a mapping table
For one team, the rule could be IS_ROLE_IN_SESSION('M18_IN') AND REQUESTED_REGION = 'IN'. Maple Market has several regional teams. The mapping table holds one role and region per row, so adding an approved team is a data change rather than a policy rewrite. Use the same pattern for department access with role and department columns. For an intersection such as finance in Europe only, require both conditions in the same entitlement row.
Enterprise Edition or higher is required. The schema grants CREATE ROW ACCESS POLICY to the creator. Attaching can use a central APPLY ROW ACCESS POLICY privilege or an object owner with APPLY on this policy. In the scratch lab the owner creates the rule, owns the map and attaches it. Treat access-map changes as security changes and keep their write grants narrow.
USE ROLE M18_OWNER;
CREATE TABLE LS_M18_GOV_LAB.GOV.REGION_ENTITLEMENT (ROLE_NAME VARCHAR, REGION VARCHAR);
INSERT INTO LS_M18_GOV_LAB.GOV.REGION_ENTITLEMENT VALUES
('M18_IN','IN'),('M18_EU','EU'),('M18_US','US');
CREATE ROW ACCESS POLICY LS_M18_GOV_LAB.GOV.SALES_REGION
AS (REQUESTED_REGION VARCHAR) RETURNS BOOLEAN ->
IS_ROLE_IN_SESSION('M18_OWNER')
OR IS_ROLE_IN_SESSION('M18_GLOBAL')
OR EXISTS (
SELECT 1 FROM LS_M18_GOV_LAB.GOV.REGION_ENTITLEMENT E
WHERE E.REGION = REQUESTED_REGION AND IS_ROLE_IN_SESSION(E.ROLE_NAME)
);
GRANT APPLY ON ROW ACCESS POLICY LS_M18_GOV_LAB.GOV.SALES_REGION TO ROLE M18_OWNER;
ALTER TABLE LS_M18_GOV_LAB.CORE.SALES
ADD ROW ACCESS POLICY LS_M18_GOV_LAB.GOV.SALES_REGION ON (REGION);Prove regional, global and restricted access
The masking lesson must finish attaching FULL_CONTACT to SALES.BUYER_EMAIL before granting reader SELECT. After row access is attached, publish SALES with the grants below. M18_ANALYST gives the inherited ability to query SALES. The regional entitlement then supplies the rows. M18_GLOBAL bypasses regional filtering by explicit rule. It still gets masked email.
USE ROLE M18_OWNER;
GRANT USAGE ON DATABASE LS_M18_GOV_LAB TO ROLE M18_ANALYST;
GRANT USAGE ON DATABASE LS_M18_GOV_LAB TO ROLE M18_RESTRICTED;
GRANT USAGE ON SCHEMA LS_M18_GOV_LAB.CORE TO ROLE M18_ANALYST;
GRANT USAGE ON SCHEMA LS_M18_GOV_LAB.CORE TO ROLE M18_RESTRICTED;
GRANT SELECT ON TABLE LS_M18_GOV_LAB.CORE.SALES TO ROLE M18_ANALYST;
GRANT SELECT ON TABLE LS_M18_GOV_LAB.CORE.SALES TO ROLE M18_RESTRICTED;Run the same query in India, Europe, US, global and restricted sessions
Select the approved warehouse and the assigned role in each test session, then run this block. The restricted role owns no entitlement and returns zero rows even though it has SELECT. Filter an India query with WHERE REGION = 'US' before ORDER BY to prove the requested region cannot override the policy. The India user also gets an access error when it tries to read the GOV mapping table or CORE.EMPLOYEES.
| Active role | Visible sale IDs | Amounts | BUYER_EMAIL |
|---|---|---|---|
| M18_IN | 701 | 100 | REDACTED |
| M18_EU | 702 | 200 | REDACTED |
| M18_US | 703 | 150 | REDACTED |
| M18_GLOBAL | 701, 702, 703 | 100, 200, 150 | REDACTED on each row |
| M18_RESTRICTED | None | No rows | No rows |
USE SECONDARY ROLES NONE;
SELECT CURRENT_ROLE(), IS_ROLE_IN_SESSION('M18_ANALYST') AS HAS_ANALYST;
SELECT SALE_ID, REGION, AMOUNT, BUYER_EMAIL
FROM LS_M18_GOV_LAB.CORE.SALES
ORDER BY SALE_ID;Inspect bindings and troubleshoot missing or extra rows
If no rows appear, first check CURRENT_ROLE, role inheritance and the exact region values in the map. If too many rows appear, check the global bypass and secondary roles, then verify the binding on the object queried. A view over SALES receives the table’s policy first. If a view also has a policy, its rule evaluates after the table policy. Be careful when writing data: a row policy is not an input validation rule and is not a substitute for write permissions.
Use the lab owner to inspect the row and masking bindings. To update a rule in place, use ALTER ROW ACCESS POLICY ... SET BODY -> expression and retest. To remove it on a disposable private object, first remove consumer access, then ALTER TABLE ... DROP ROW ACCESS POLICY policy_name. A large mapping table or complex policy can add overhead. Measure a representative query and consider a simpler rule or a documented memoizable lookup when appropriate.
SELECT POLICY_KIND, POLICY_NAME, REF_COLUMN_NAME, POLICY_STATUS
FROM TABLE(LS_M18_GOV_LAB.INFORMATION_SCHEMA.POLICY_REFERENCES(
REF_ENTITY_NAME => 'LS_M18_GOV_LAB.CORE.SALES', REF_ENTITY_DOMAIN => 'TABLE'))
ORDER BY POLICY_KIND, REF_COLUMN_NAME;Interview practice
Explain the decision and the evidence you would use to test it.
Can masking and row access apply together?
Yes. I would put the row policy on region and the mask on buyer email. Snowflake filters the rows first. For India, the result is sale 701 and REDACTED email.
Why can the India role read sales without selecting the mapping table?
The rule uses the policy owner’s rights for the private map. The reporting role still needs SELECT on sales, and the policy checks that role’s session context.
How do you add Europe Finance without opening all Europe orders?
I would decide the approved scope first and use a map keyed by both region and department when both matter. I would test the allowed pair, a department that fails and a cross-region read under separate identities.
Words used in this lesson
- Row access policy
- A rule that keeps a row only when its Boolean result is TRUE.
- Mapping table
- A private table of approved role-to-region or role-to-department pairs.
- POLICY_REFERENCES
- Find bindings and their status.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Snowflake row access policies for regional and department security
Combine row access and masking
The row filter decides which orders survive. The masking rule decides what email each retained row shows.
Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.
Read the complete diagram explanation
- Combine row access and masking
The row filter decides which orders survive. The masking rule decides what email each retained row shows.
Three sales rows → Row access policy → Email masking policy → Query result
Try the SQL or command
Read → predict → run in your learning environment → compare. You can also learn by reading without an account.
New to SQL? Where to run it and how to read it
A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.
- No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
- For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
- Read the entire example, including comments beginning with
--. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor. - Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
- SELECT / FROM
- Choose the values to return / identify their input.
- VALUES / COLUMN1
- VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
- AS / WITH
- AS gives a result a readable name. WITH names a temporary query step within one statement.
- NULL / ::
- NULL means missing or unknown. The :: notation converts a value to a named data type.
- Session
- One connection and its settings. Keep related transaction steps in the same session.
- Database.schema.table
- A full address for stored data. It is separate from the warehouse that processes queries.
Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →
Help with common practice errors
- No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
- Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
- Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
- Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.
Before running this lesson
All records are fictional. Outputs are expected or simulated. Live practice needs an authorized scratch account, test roles, an approved warehouse and the editions listed in the lesson.
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Write a test that distinguishes a missing SELECT grant from an empty entitlement.
Compare your reasoning
Run an ordinary SELECT over SALES as the intended reader. A privilege denial indicates object access is missing. A successful zero-row result calls for checking the entitlement and role context.
Connect this lesson to the learning path
Official sources and curriculum
Module 18 · See module scope
By Rohit Ray · Independent educational lesson · Explore this visual lab