Snowflake SQL Tutorial 161 - Snowflake Let Me Write A Grant That Does Absolutely Nothing

Snowflake SQL · Episode 161

In this post we are going to cover database roles in Snowflake.

An account role floats above everything on the account. A database role lives inside one database, is named with that database in front of it, and can only be granted privileges on objects in that same database.

We will start by making one and looking for it in the catalogue, and then we will try to use it, which is where this gets interesting. Snowflake will let us write one grant here that does absolutely nothing, and tell us the statement succeeded.

Every query below was run on a real Snowflake account, and every result you see is the output it gave back, refusals included.

Episode 161 of the Snowflake SQL: Zero to Hero series on TechBrothersIT.

Let's create some sample data

We need a schema of our own, a table in it, and some rows to read.

Sample data
-- Setup for this post.
-- A schema, a table, three rows, and a database role to put them behind.
USE ROLE ACCOUNTADMIN;
USE DATABASE DEMO_DB;

CREATE OR REPLACE SCHEMA DEMO_DB.DBR_T161B
  COMMENT = 'episode 161 - database roles, asked with the control on';

CREATE OR REPLACE TABLE DEMO_DB.DBR_T161B.SALES_T161B (
  SALE_ID NUMBER, REGION VARCHAR(20)
);

INSERT INTO DEMO_DB.DBR_T161B.SALES_T161B (SALE_ID, REGION)
VALUES (1,'north'),(2,'south'),(3,'east');
Query
SELECT * FROM DEMO_DB.DBR_T161B.SALES_T161B ORDER BY SALE_ID;
Result3 rows · 2 columns
SALE_IDREGION
1north
2south
3east

Three rows. That is what a working read looks like, so we can tell later on whether a database role is really the thing answering.

And before we make anything, let's see what database roles this database already has.

Query
SHOW DATABASE ROLES IN DATABASE DEMO_DB;
Result0 rows · 11 columns · scroll →
created_onnameis_defaultis_currentis_inheritedgranted_to_rolesgranted_to_database_rolesgranted_database_rolesownercommentowner_role_type

None. No rows at all, so everything from here on is ours.

Now let's make a database role and look at its name

Query
CREATE OR REPLACE DATABASE ROLE DEMO_DB.DR_T161
    COMMENT = 'episode 161 - a role that lives in a database';
Result1 row · 1 column
status
Role DEMO_DB.DR_T161 successfully created.

Read the status line, because the name is the whole idea: DEMO_DB.DR_T161. The database is part of what the role is called.

And here it is in the listing.

Query
SELECT "created_on", "name", "comment", "owner", "granted_to_roles",
       "granted_database_roles"
  FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
 WHERE "name" LIKE '%\\_T161' ESCAPE '\\';
Result1 row · 6 columns · scroll →
created_onnamecommentownergranted_to_rolesgranted_database_roles
2026-10-06 11:31:00.265000-07:00DR_T161episode 161 - a role that lives in a databaseACCOUNTADMIN00

Owner ACCOUNTADMIN, our comment, and two counters both sitting at zero because nothing has been granted to it yet.

Now watch where it does not appear

Let's look for that same role with plain SHOW ROLES.

Query
SHOW ROLES LIKE 'DR\\_T161';
Result0 rows · 11 columns · scroll →
created_onnameis_defaultis_currentis_inheritedassigned_to_usersgranted_to_rolesgranted_rolesownercommentis_from_organization_user_group

No rows. It is not there.

A database role does not appear in SHOW ROLES at all. Two different catalogues, and you have to ask the right one.

That matters more than it sounds. A script or a tool that lists your roles the usual way will never show you these, and will not tell you it has not looked.

The columns are different too. SHOW ROLES has granted_to_roles, which you have seen. SHOW DATABASE ROLES also has granted_database_roles, which only exists here: a count of roles holding roles, inside one database.

While we are on catalogues, here is an attempt I made to find them through INFORMATION_SCHEMA instead.

Query
SELECT ROLE_NAME, ROLE_OWNER, IS_INHERITED, IS_GRANTABLE
  FROM DEMO_DB.INFORMATION_SCHEMA.APPLICABLE_ROLES
 ORDER BY ROLE_NAME;
Query failed — Snowflake returned an error
000904 (42000): SQL compilation error: error line 1 at position 30
invalid identifier 'IS_INHERITED'

000904, on a column name. That view does not have an IS_INHERITED column, so the query never ran and tells us nothing about database roles either way. It is here because it is the first thing I tried.

Can we grant a database role straight to a user?

I was sure the answer was no. Let's try it.

Query
GRANT DATABASE ROLE DEMO_DB.DR_T161 TO USER YOUR_USER;
Result1 row · 1 column
status
Statement executed successfully.

Statement executed successfully. So I was wrong, and that is the more interesting answer. It can be granted to a user.

Now let's wear it, which was the entire point of granting it.

Query
USE ROLE DEMO_DB.DR_T161;
Query failed — Snowflake returned an error
002043 (02000): SQL compilation error:
Object does not exist, or operation cannot be performed.

002043. Object does not exist, or operation cannot be performed.

Maybe the qualifier is the problem. Let's try it without the database in front.

Query
USE ROLE DR_T161;
Query failed — Snowflake returned an error
002043 (02000): SQL compilation error:
Object does not exist, or operation cannot be performed.

Same error, same words. Neither spelling works.

So we hold it, and there is no way to put it on. That grant did precisely nothing for us, and the statement said it succeeded. No warning, no hint.

Succeeded is not the same as useful

This is the thing to take away from this episode, and it is not a complaint about Snowflake. A statement working and a statement helping are two different questions. You have to ask the second one yourself, because nothing is going to ask it for you.

The grid for the user's side of this is not on the page. Every row of it carries the account's user name in the grantee_name column, and the builder here cannot leave one column out of a grid, so the whole grid is left out instead.

So what does hold a database role usefully?

An account role, because an account role is something a person can actually wear. Let's build that properly, with the privileges on the database role and nothing on the account role.

Two roles, arranged on purpose
-- The database role holds everything that touches data.
CREATE OR REPLACE DATABASE ROLE DEMO_DB.DR_T161B
  COMMENT = 'episode 161b - lives in the database, holds the SELECT';

GRANT USAGE  ON SCHEMA DEMO_DB.DBR_T161B
    TO DATABASE ROLE DEMO_DB.DR_T161B;
GRANT SELECT ON TABLE  DEMO_DB.DBR_T161B.SALES_T161B
    TO DATABASE ROLE DEMO_DB.DR_T161B;

-- The account role gets the two things a database role cannot hold,
-- and nothing whatsoever on the table.
CREATE OR REPLACE ROLE RACCT_T161B
  COMMENT = 'episode 161b - holds the database role and NOTHING on the table';
GRANT USAGE ON DATABASE  DEMO_DB    TO ROLE RACCT_T161B;
GRANT USAGE ON WAREHOUSE COMPUTE_WH TO ROLE RACCT_T161B;
GRANT ROLE  RACCT_T161B TO USER YOUR_USER;

Let's see what the database role holds.

Query
SELECT "privilege", "granted_on", "name", "granted_to", "grantee_name"
  FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
 ORDER BY "granted_on";
Result3 rows · 5 columns
privilegegranted_onnamegranted_tograntee_name
USAGEDATABASEDEMO_DBDATABASE_ROLEDR_T161B
USAGESCHEMADEMO_DB.DBR_T161BDATABASE_ROLEDR_T161B
SELECTTABLEDEMO_DB.DBR_T161B.SALES_T161BDATABASE_ROLEDR_T161B

The database, the schema and the table. The chain from Episode 159, all of it sitting on the database role.

Now, before the account role holds anything, let's prove it genuinely cannot read the table.

Wear the account role
USE ROLE RACCT_T161B;
Query
SELECT SALE_ID, REGION
  FROM DEMO_DB.DBR_T161B.SALES_T161B
 ORDER BY SALE_ID;
Query failed — Snowflake returned an error
002003 (02000): SQL compilation error:
Schema 'DEMO_DB.DBR_T161B' does not exist or not authorized. Your primary role RACCT_T161B must have USAGE or any other privilege granted on SCHEMA DEMO_DB.DBR_T161B.

002003 on the schema, naming the account role. Good. Now the test can fail, which means it is worth running.

Now let's hand the database role to the account role

Query
USE ROLE ACCOUNTADMIN;
GRANT DATABASE ROLE DEMO_DB.DR_T161B TO ROLE RACCT_T161B;

And let's look at what the account role holds now.

Query
SELECT "privilege", "granted_on", "name", "grantee_name"
  FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
 ORDER BY "granted_on";
Result3 rows · 4 columns
privilegegranted_onnamegrantee_name
USAGEDATABASEDEMO_DBRACCT_T161B
USAGEDATABASE_ROLEDEMO_DB.DR_T161BRACCT_T161B
USAGEWAREHOUSECOMPUTE_WHRACCT_T161B

Three rows, and no table anywhere in them. The middle row is the interesting one: privilege USAGE, granted on DATABASE_ROLE, and the name fully qualified.

That granted_on value does not appear anywhere else in this course. It is particular to this.

And does the privilege inside reach us through two hops?

This needs care, because USE ROLE only sets your primary role. Any other role you hold stays switched on underneath, so a read that succeeds proves nothing on its own.

Switch the secondary roles off
USE SECONDARY ROLES NONE;
USE ROLE RACCT_T161B;
Query
SELECT CURRENT_ROLE() AS THE_ROLE, CURRENT_SECONDARY_ROLES() AS THE_SECONDARY_ROLES;
Result1 row · 2 columns
THE_ROLETHE_SECONDARY_ROLES
RACCT_T161B{"roles":"","value":""}

Current role is the account role, and the secondary roles are empty. Nothing is hiding behind this. Now the read.

Query
SELECT SALE_ID, REGION
  FROM DEMO_DB.DBR_T161B.SALES_T161B
 ORDER BY SALE_ID;
Result3 rows · 2 columns
SALE_IDREGION
1north
2south
3east

Three rows. Nothing on that table was ever granted to the account role directly, and the same query failed a minute ago. It came through the database role.

And one that has to work something out as well, so it is not just a row fetch.

Query
SELECT COUNT(DISTINCT REGION) AS HOW_MANY_REGIONS, MAX(REGION) AS LAST_REGION
  FROM DEMO_DB.DBR_T161B.SALES_T161B;
Result1 row · 2 columns
HOW_MANY_REGIONSLAST_REGION
3south

That is what database roles are for: the privileges live inside the database, and an account role carries them out to somebody who can wear it.

Can a database role hold a database role?

Yes. Let's make a second one and grant the first one to it.

Query
CREATE OR REPLACE DATABASE ROLE DEMO_DB.DR2_T161
    COMMENT = 'episode 161 - the second one';
Result1 row · 1 column
status
Role DEMO_DB.DR2_T161 successfully created.
Query
GRANT DATABASE ROLE DEMO_DB.DR_T161 TO DATABASE ROLE DEMO_DB.DR2_T161;
Result1 row · 1 column
status
Statement executed successfully.
Query
SELECT "privilege", "granted_on", "name", "granted_to", "grantee_name"
  FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
 ORDER BY "granted_on", "privilege";
Result2 rows · 5 columns
privilegegranted_onnamegranted_tograntee_name
USAGEDATABASEDEMO_DBDATABASE_ROLEDR2_T161
USAGEDATABASE_ROLEDEMO_DB.DR_T161DATABASE_ROLEDR2_T161

Two rows. Usage on the database, and usage on the first database role. Stacking works.

And let's look at both of them in the listing, because one of those odd columns has woken up.

Query
SELECT "name", "comment", "granted_to_roles", "granted_database_roles",
       "owner"
  FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
 WHERE "name" LIKE '%\\_T161' ESCAPE '\\'
 ORDER BY "name";
Result2 rows · 5 columns
namecommentgranted_to_rolesgranted_database_rolesowner
DR2_T161episode 161 - the second one01ACCOUNTADMIN
DR_T161episode 161 - a role that lives in a database10ACCOUNTADMIN

granted_database_roles reads 1 against the second role, and granted_to_roles reads 1 against the first. The column that only exists in this listing is earning its keep.

And now three things that do not work

Granting an account role to a database role

Query
GRANT ROLE RACCT_T161 TO DATABASE ROLE DEMO_DB.DR_T161;
Query failed — Snowflake returned an error
001003 (42000): SQL compilation error:
syntax error line 1 at position 39 unexpected 'DEMO_DB'.

001003, a syntax error, at the position where the database name starts.

So that is not refused on permissions. The grammar does not have that shape at all. It is not a rule we are breaking. It is a sentence the language cannot say.

A database role in a database that does not exist

Query
CREATE OR REPLACE DATABASE ROLE NO_SUCH_DB_T161.DRX_T161;
Query failed — Snowflake returned an error
002003 (02000): SQL compilation error:
Database 'NO_SUCH_DB_T161' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have USAGE or any other privilege granted on DATABASE NO_SUCH_DB_T161.

002003, naming the database. Familiar by now.

Reaching into the SNOWFLAKE database

Every account has a SNOWFLAKE database. Let's try to give our database role usage on it.

Query
GRANT USAGE ON DATABASE SNOWFLAKE TO DATABASE ROLE DEMO_DB.DR_T161;
Query failed — Snowflake returned an error
003028 (0A000): SQL compilation error:
Granting individual privileges on imported database is not allowed. Use 'GRANT IMPORTED PRIVILEGES' instead.

003028. Granting individual privileges on an imported database is not allowed, and it tells us to use GRANT IMPORTED PRIVILEGES instead.

Read that one properly, because it is doing us a favour. We asked about one thing and it told us the name of the mechanism we actually needed. SNOWFLAKE is a shared database, and shared databases have their own verb.

And the refusals changed nothing.

Query
SELECT "privilege", "granted_on", "name"
  FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
 ORDER BY "granted_on", "privilege";
Result3 rows · 3 columns
privilegegranted_onname
USAGEDATABASEDEMO_DB
USAGESCHEMADEMO_DB.DBR_T161
SELECTTABLEDEMO_DB.DBR_T161.SALES_T161

One last thing: who holds this role, and who can use it

There is one more statement worth knowing: SHOW GRANTS OF DATABASE ROLE, which answers “who holds this” rather than “what does it hold”.

Query
SHOW GRANTS OF DATABASE ROLE DEMO_DB.DR_T161;

SELECT "role", "granted_to", "grantee_name", "granted_by"
  FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));

That grid is not on this page. By this point in the episode the role is held by three different kinds of thing, and one of them is the account's own user, so every version of that grid carries the user name in the grantee_name column. The builder here cannot leave one column out of a grid, so the grid is left out instead. Run it on your own account and you will see your own.

What the granted_to column says is the part worth taking away. A database role, an account role and a user can all hold the same database role, and they all sit in the same column, formatted identically, with nothing to tell them apart.

  • The account role works. You wear it, and the privileges arrive.
  • The database role works too, as a way of stacking one inside another.
  • The user is the one that does nothing, because there is no way to put it on.

So when you audit this, the question is not who holds the role. It is who can use it. And nothing in that grid answers the second question.

Everything above was created by this post and removed again afterwards.

Teardown
-- Putting the account back.
USE ROLE ACCOUNTADMIN;

REVOKE DATABASE ROLE DEMO_DB.DR_T161B FROM ROLE RACCT_T161B;
REVOKE USAGE ON DATABASE  DEMO_DB    FROM ROLE RACCT_T161B;
REVOKE USAGE ON WAREHOUSE COMPUTE_WH FROM ROLE RACCT_T161B;
REVOKE ROLE RACCT_T161B FROM USER YOUR_USER;

DROP ROLE          IF EXISTS RACCT_T161B;
DROP DATABASE ROLE IF EXISTS DEMO_DB.DR_T161B;
DROP DATABASE ROLE IF EXISTS DEMO_DB.DR_T161;
DROP DATABASE ROLE IF EXISTS DEMO_DB.DR2_T161;
DROP SCHEMA        IF EXISTS DEMO_DB.DBR_T161B CASCADE;

-- The secondary roles go back the way we found them.
USE SECONDARY ROLES ALL;

-- And the session check that ends every one of these files.
SELECT CURRENT_USER()      AS THE_USER,
       CURRENT_ROLE()      AS THE_ROLE,
       CURRENT_DATABASE()  AS THE_DATABASE,
       CURRENT_SCHEMA()    AS THE_SCHEMA,
       CURRENT_WAREHOUSE() AS THE_WAREHOUSE;

Key points

  • A database role lives inside one database and is named with the database in front of it.
  • It does not appear in SHOW ROLES at all. SHOW DATABASE ROLES IN DATABASE is the listing, and it has a column the other one does not.
  • You can grant a database role to a user. The statement succeeds and says so.
  • And then there is no way to wear it. USE ROLE gives 002043, qualified or not.
  • So that grant buys the user nothing, and nothing anywhere tells you that. A statement working and a statement helping are different questions.
  • What a database role is for is being granted to an account role, which a person can hold.
  • Prove a two-hop privilege with the secondary roles switched off and with a read that failed first. Otherwise a success proves nothing.
  • A database role can be granted to another database role, and granted_database_roles counts it.
  • Granting an account role to a database role is a syntax error, not a permissions one.
  • Individual privileges on an imported database are refused with 003028, and the message names GRANT IMPORTED PRIVILEGES instead.
Next in this series

In the next post we will take SHOW GRANTS seriously and ask it the one question it looks like it answers: who can read this table. The grid it gives back is true, and it is not the whole answer.

Every query and every result grid on this page was captured from a live Snowflake account (version 10.36.101) running the demo script for this episode, across two runs: the first one for the catalogue and the refusals, and a second one for the two-hop read, with the secondary roles switched off and proven off before a single test ran, which is why those object names end in B. The statement that grants a database role to a user has the account's user name rewritten to YOUR_USER and is otherwise untouched, and every grid that carried that name is left off the page rather than edited. Nothing is timed and no performance claims are made anywhere in this series. Behaviour follows the Snowflake database roles documentation.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.