Object Tagging¶
This topic provides concepts and instructions on how to use tags in Snowflake.
To learn more about using a masking policy with a tag, see Tag-based masking policies.
What is a tag?¶
Tags enable data stewards to monitor sensitive data for compliance, discovery, protection, and resource usage use cases through either a centralized or decentralized data governance management approach.
A tag is a schema-level object that can be assigned to another Snowflake object. A tag can be assigned an arbitrary string value upon assigning the tag to a Snowflake object. Snowflake stores the tag and its string value as a key-value pair. The tag must be unique for your schema, and the tag value is always a string.
You create a tag using a CREATE TAG statement, and you specify the tag string value when assigning the tag to an object. The tag can be assigned to an object while creating the object, using a CREATE <object> statement, assuming that the tag already exists. Alternatively, you can assign the tag to an existing object using an ALTER <object> statement.
A single tag can be assigned to different object types at the same time (e.g. warehouse and table simultaneously). At the time of assignment, the tag string value can be duplicated or remain unique. For example, multiple tables can be assigned the cost_center tag and the tag can always have the string value be sales. Alternatively, the string value could be different (e.g. engineering, marketing, finance). After defining the tags and assigning the tags to Snowflake objects, tags can be queried to monitor usage on the objects to facilitate data governance operations, such as monitoring, auditing, and reporting.
Because tags can be assigned to tables, views, and columns, setting a tag and then querying the tag enables the discovery of a multitude of database objects and columns that contain sensitive information. Upon discovery, data stewards can determine how best to make that data available, such as selective filtering using row access policies, or using masking policies to determine whether the data is tokenized, fully masked, partially masked, or unmasked.
Assigning tags to warehouses enables accurate resource usage monitoring. Querying tags on resources allows for easy resource grouping by cost center or other organization units. Additionally, the tag can facilitate analyzing relatively short-term business activities, such as projects, to provide a more granular insight into what, when, and how resources were used.
Tag quotas for objects and columns¶
The string value for each tag can be up to 256 characters, with the option to specify allowed values for a tag.
The following description applies to all objects that are not tables and views:
Snowflake allows a maximum number of 50 unique tags that can be set on a single object. In a CREATE <object> or ALTER <object> statement, 100 is the maximum number of tags that can be specified in a single statement.
The maximum number of unique tags is slightly different for tables and views, including the columns in those tables and views.
Tables, views, and columns¶
For a table or view and its columns, the maximum number of unique tags that can be specified in a single CREATE <object> or ALTER <object> statement is 100. This total value has the following limits:
A single table or view object: 50 unique tags.
All columns combined in a single table or view: 50 unique tags.
For example, if a single column in a table has 10 unique tags set on the column, Snowflake allows:
Setting 40 additional unique tags on either that same column, other columns in the table, or some combination of the columns in the table.
Setting 50 additional unique tags on the table itself.
Once the limit of 50 unique tags is met for the table itself and its columns, no additional tags can be set on the table or its columns. At this point, if there is a desire to set additional tags on the table or its columns, the next step to consider is how to manage the tag quotas for an object.
Manage tag quotas¶
The maximum number of 50 unique tags includes dropped tags for a time period of 24 hours starting from when the tag is dropped using a DROP TAG statement. The reason for this time period is to allow the user who dropped the tag to execute an UNDROP TAG statement, if necessary. When the UNDROP TAG operation executes within the 24-hour time interval, Snowflake restores the tag assignments (i.e. references) that were current prior to the drop operation.
After the 24-hour time period expires, Snowflake purges any references pertaining to the dropped tag. At this point, a new tag can be assigned to the object or column that once referenced the dropped tag.
Use the following procedure to manage the tag quotas for an object:
Query the TAG_REFERENCES view (in Account Usage) to determine the tag assignments.
Unset the tag from the object or column. For example:
For objects, use the corresponding
ALTER <object> ... UNSET TAG
command.For a table or view column, use the corresponding
ALTER { TABLE | VIEW } ... { ALTER | MODIFY } COLUMN ... UNSET TAG
command.Drop the tag using a DROP TAG statement.
Specify tag values¶
The ALLOWED_VALUES
tag property enables specifying the possible string values that can be assigned to the tag when the tag is set
on an object. The maximum number of possible string values for a single tag is 300.
You can specify these values when creating or replacing a tag with a CREATE TAG statement, or while modifying an existing tag key with an ALTER TAG statement. Note that the ALTER TAG statement supports adding allowed values for a tag and dropping existing values for a tag.
To determine the list of allowed values for a tag, call the GET_DDL function or the SYSTEM$GET_TAG_ALLOWED_VALUES function.
For example:
Create a tag named
cost_center
with'finance'
and'engineering'
as the only two allowed string values:create tag cost_center allowed_values 'finance', 'engineering';Verify the allowed values:
select get_ddl('tag', 'cost_center') +------------------------------------------------------------------------------+ | GET_DDL('tag', 'cost_center') | |------------------------------------------------------------------------------| | create or replace tag cost_center allowed_values = 'finance', 'engineering'; | +------------------------------------------------------------------------------+Modify the tag named
cost_center
to add'marketing'
as an allowed string value:alter tag cost_center add allowed_values 'marketing';Modify the tag named
cost_center
to drop'engineering'
as an allowed string value:alter tag cost_center drop allowed_values 'engineering';
To obtain the list of allowed string values for a given tag, call either the GET_DDL function or the
SYSTEM$GET_TAG_ALLOWED_VALUES function. For example, assuming that the tag cost_center
is stored in a
database named governance
and a schema named tags
:
select system$get_tag_allowed_values('governance.tags.cost_center'); +--------------------------------------------------------------+ | SYSTEM$GET_TAG_ALLOWED_VALUES('GOVERNANCE.TAGS.COST_CENTER') | |--------------------------------------------------------------| | ["finance","marketing"] | +--------------------------------------------------------------+
Tag lineage¶
A tag is inherited based on the Snowflake securable object hierarchy. Snowflake recommends defining the tag keys as closely as possible to the securable object hierarchy in your Snowflake environment.
Tag inheritance means that if a tag is applied to a table, the tag also applies to the columns in that table. This behavior is referred to as tag lineage.
It is possible to override an inherited tag on a given object. For example, if a table column inherits the tag named cost_center with a string value called sales, the tag can be updated with a more specific tag string value such as sales_na, to specify the North America sales cost center. Additionally, a new tag can be applied to the table column. Use an ALTER TABLE … ALTER COLUMN statement to update the tag string value on the column and to set one or more additional tags on a column.
After defining the tag keys and assigning tags to Snowflake objects, monitor the tags, tag references, and tag lineage using the specified table functions or query the views as shown in Monitor tags with SQL (in this topic).
Note
Tag lineage does not include propagation to nested objects. For example:
table_1
»view_1
»materialized_view_1
If nested objects already exist relative to an underlying table or view, a tag set on underlying object does not automatically result
in a tag being set on the nested object. In this example, a tag set on table_1
does not result in the same tag being set on
view_1
and materialized_view_1
. This behavior is also true for columns.
If it is necessary to have tags on underlying objects or columns carry over to nested objects, execute a CREATE OR REPLACE statement on the nested object and make sure the SQL statement specifies the tag on the nested object or column.
Benefits¶
- Ease of Use:
Define a tag once and apply it to as many different objects as desirable.
- Tag Lineage:
Since tags are inherited, applying the tag to objects higher in the securable objects hierarchy results in the tag being applied to all child objects. For example, if a tag is set on a table, the tag will be inherited by all columns in that table.
- Consistent Assignment with Replication:
Snowflake replicates tags and their assignments within the primary database to the secondary database.
For more information, see Replication (in this topic).
- Sensitive Data Tracking and Resource Usage:
Tags simplify identifying sensitive data (e.g. PII, Secret) and bring visibility to Snowflake resource usage. With data and metadata in the same system, analysts can quickly determine which resources consume the most Snowflake credits based on the tag definition (e.g.
cost_center
,department
).- Centralized or Decentralized Management:
Tags supports different management approaches to facilitate compliance with internal and external regulatory requirements.
In a centralized approach, the
tag_admin
custom role creates and applies tags to Snowflake objects.In a decentralized approach, individual teams apply tags to Snowflake objects and the
tag_admin
custom role creates tags to ensure consistent tag naming.
Considerations¶
- Future grants:
Future grants of privileges on tags are not supported.
As a workaround, grant the APPLY TAG privilege to a custom role to allow that role to apply tags to another object.
- Snowflake Native App:
Use caution when creating the setup script when tags exist in a versioned schema. For details, see version schema considerations.