USERS and QUERY_ HISTORY views (ACCOUNT_ USAGE) and QUERY_ HISTORY function: New columns¶
Attention
This behavior change is in the 2024_08 bundle.
For the current status of the bundle, refer to Bundle History.
When this behavior change bundle is enabled, the USERS and QUERY_HISTORY views and the output of the QUERY_HISTORY function include the following new columns.
USERS view (ACCOUNT_ USAGE)¶
When the user TYPE is SNOWFLAKE_SERVICE, indicating it is a service user, the following new columns provide the service’s database and schema information. Otherwise, these columns are null.
| Column name | Type | Description |
|---|---|---|
| DATABASE_NAME | VARCHAR | When the user TYPE is SNOWFLAKE_SERVICE, it specifies the service’s database name; otherwise, it is NULL. |
| DATABASE_ID | NUMBER | When the user TYPE is SNOWFLAKE_SERVICE, it specifies the internal, Snowflake-generated identifier for the service’s database; otherwise, it’s NULL. |
| SCHEMA_NAME | VARCHAR | When the user type is SNOWFLAKE_SERVICE, it specifies the service’s schema name; otherwise, it is NULL. |
| SCHEMA_ID | NUMBER | When the user type is SNOWFLAKE_SERVICE, it specifies the internal, Snowflake-generated identifier for the service’s schema; otherwise, it’s NULL. |
QUERY_ HISTORY view (ACCOUNT_ USAGE)¶
When the QUERY_HISTORY USER_TYPE is SNOWFLAKE_SERVICE, indicating the query is executed by a Snowpark Container Services service, the other columns provide the service’s database and schema information.
| Column name | Type | Description |
|---|---|---|
| USER_TYPE | VARCHAR | Specifies the type of the user executing the query. It is the same as the type field on the user entity (see USERS view). If a Snowpark Container Services service executes the query, the user type is SNOWFLAKE_SERVICE. |
| USER_DATABASE_NAME | VARCHAR | When the value in the user_type column is SNOWFLAKE_SERVICE, it specifies the service’s database name; otherwise, it’s NULL. |
| USER_DATABASE_ID | NUMBER | When the value in the user_type column is SNOWFLAKE_SERVICE, it specifies the internal, Snowflake-generated identifier for the service’s database; otherwise, it’s NULL. |
| USER_SCHEMA_NAME | VARCHAR | When the value in the user_type column is SNOWFLAKE_SERVICE, it specifies the service’s schema name; otherwise, it’s NULL. |
| USER_SCHEMA_ID | NUMBER | When the value in the user_type column is SNOWFLAKE_SERVICE, it specifies the internal, Snowflake-generated identifier for the service’s schema, otherwise it’s NULL. |
QUERY_ HISTORY function¶
The following new columns are added to the output (see QUERY_HISTORY function). They identify the user type (USER_TYPE). When the USER_TYPE is SNOWFLAKE_SERVICE, the other columns identify the service’s database and schema.
| Column name | Type | Description |
|---|---|---|
| USER_TYPE | VARCHAR | Specifies the type of the user executing the query. It’s the same as the type field on the user entity (see USERS view). If a Snowpark Container Services service executes the query, the user type is SNOWFLAKE_SERVICE. |
| USER_DATABASE_NAME | VARCHAR | When the value in the user_type column is SNOWFLAKE_SERVICE, it specifies the service’s database name; otherwise, it’s NULL. |
| USER_SCHEMA_NAME | VARCHAR | When the value in the user_type column is SNOWFLAKE_SERVICE, it specifies the service’s schema name; otherwise, it’s NULL. |
Examples¶
The service’s database and schema information, along with the user name (which, for the SNOWFLAKE_SERVICE user type, is also the service name) can help during debugging. For example, you can write a query to retrieve information about the service that executed the queries. The following are some example queries:
Example 1: Fetch queries run by a service.
In the WHERE clause:
user_name = '<service-name>'because a service executes queries as the service user, and the service user’s name is the same as the service name.user_type = 'SNOWFLAKE_SERVICE'anduser_name = '<service-name>'retrieve only queries executed by a service.user_database_nameanduser_schema_namenames, for a service user, are the service’s database and schema.
You can get the same results by querying the information_schema.query_history function.
In the WHERE clause:
user_type = 'SNOWFLAKE_SERVICE'anduser_name = '<service-name>'retrieves only queries executed by a service.user_database_nameanduser_schema_namenames, for a service user, are the service’s database and schema.
Example 2: Fetch queries run by services and corresponding service information.
The query joins the QUERY_HISTORY and SERVICES views to retrieve information about the queries and services that executed the queries. Note the following:
- For queries run by services, the
query_history.user_nameis the service user’s name, which is the same as the service name. - The query joins the views using the schema IDs (not schema name) to ensure you refer to the same schema, because if you drop and recreate a schema, the schema ID changes but the name remains the same.
You can add optional filters to the query. For example:
- Filter
query_historyto retrieve only services that executed specific queries. - Filter
servicesto retrieve only queries executed by specific services.
Example 3: For every service, fetch service user information.
The query join SERVICES and USERS views in the ACCOUNT_USAGE schema to retrieve services and service user information. Note the following:
- When a service runs queries, it runs the queries as service user and the service user’s name is the same as the service name. Therefore, you specify the join condition:
users.name = services.service_name. - Service names are unique only within a schema. Therefore, the query specifies the join condition (
users.schema_id = services.service_schema_id) to ensure each service user is matched against the specific service they belong to (and not any other same-named service running in different schemas).
Ref: 1771