Categories:

Context functions (General)

SYS_CONTEXT (SNOWFLAKE$CURRENT namespace)

Returns information about the current execution context in which the function is called. The current execution context can differ from the session context when the function is called inside an owner’s rights executable or during an agent invocation.

You can call this function in the following contexts:

  • You can call this function directly in the current session.
  • You can run a caller’s rights executable (for example, a caller’s rights stored procedure) that calls this function.
  • You can run an owner’s rights executable (for example, an owner’s rights stored procedure) that calls this function.

Unlike the SNOWFLAKE$SESSION namespace, calling this function in an owner’s rights executable doesn’t require the READ SESSION privilege.

In any other context, the function returns NULL.

See also:

SYS_CONTEXT , SYS_CONTEXT (SNOWFLAKE$SESSION namespace) , IS_AGENT_ACTIVATED (SYS_CONTEXT function) , IS_DATABASE_ROLE_ACTIVATED (SYS_CONTEXT function) , IS_ROLE_ACTIVATED (SYS_CONTEXT function)

Syntax

Syntax for retrieving properties:

SYS_CONTEXT(
  'SNOWFLAKE$CURRENT' ,
  '<property>'
)

Syntax for calling functions:

SYS_CONTEXT(
  'SNOWFLAKE$CURRENT' ,
  '<function>' [ , '<argument>' , ... ]
)

Arguments

'SNOWFLAKE$CURRENT'

Specifies that you want to retrieve a property or call a function to return information about the current execution context.

'property'

Name of the property that you want to retrieve. You can specify the following properties:

PropertyDescription
AGENT_TYPE

Type of the agent in the current execution context. This property can have one of the following values:

  • SNOWFLAKE_INTELLIGENCE, if the current execution context is inside a Snowflake Intelligence agent.
  • CORTEX_CODE_CLI, if the current context is a Cortex Code CLI client.
  • CORTEX_CODE_DESKTOP, if the current context is a Cortex Code Desktop client.
  • CORTEX_CODE_SNOWSIGHT, if the current context is a Cortex Code UI (Snowsight) client.
  • NULL, if no agent is active in the current context.
AGENT_DATABASE

Name of the database that contains the agent. For Snowflake Intelligence agents, this is the database component of the agent’s fully qualified name.

If the agent is not a database-level object (for example, a Cortex Lite Agent), or if no agent is active, the value is NULL.

AGENT_SCHEMA

Name of the schema that contains the agent. For Snowflake Intelligence agents, this is the schema component of the agent’s fully qualified name.

If the agent is not a schema-level object (for example, a Cortex Lite Agent), or if no agent is active, the value is NULL.

AGENT_NAME

Name of the agent in the current execution context. For Snowflake Intelligence agents, this is the entity name. For other agent types, this is the client type name.

If no agent is active, the value is NULL.

'function'

Name of the function that you want to call. When called via the SNOWFLAKE$CURRENT namespace, IS_ROLE_ACTIVATED and IS_DATABASE_ROLE_ACTIVATED check the current execution context rather than the session.

You can call the following functions:

'argument' [ , ... ]

Arguments to pass to the function that you want to call.

Returns

The function returns a VARCHAR value or NULL:

Usage notes

  • The SNOWFLAKE$CURRENT namespace returns values for the innermost execution context. Inside an owner’s rights stored procedure, for example, this reflects the owner’s context, not the caller’s session context. Use the SNOWFLAKE$SESSION namespace if you need session-level values instead.

  • The following table summarizes when SNOWFLAKE$CURRENT and SNOWFLAKE$SESSION return different values:

    ScenarioSNOWFLAKE$CURRENTSNOWFLAKE$SESSION
    Direct query in the sessionSession contextSession context
    Inside a caller’s rights stored procedureSession contextSession context
    Inside an owner’s rights stored procedureOwner’s context (activated roles reflect the owner; no READ SESSION required)Session context (requires READ SESSION privilege)
    During an agent invocationAgent’s execution context (agent properties are populated)Session context of the invoking user
  • To simulate the values of properties and functions in this namespace when testing policies, use the corresponding POLICY_CONTEXT arguments. For example, use SNOWFLAKE$CURRENT_AGENT_NAME to simulate the AGENT_NAME property, or SNOWFLAKE$CURRENT_ACTIVATED_ROLES to simulate the result of IS_ROLE_ACTIVATED.

  • If you are specifying the function call in a double-quoted string in a shell, escape the $ character with a backslash (\) so that $CURRENT is not interpreted as a shell variable.

Examples

The following examples demonstrate how to retrieve context information about the current execution context:

Retrieving information about the current agent

The following example returns the type and name of the agent in the current execution context:

SELECT SYS_CONTEXT('SNOWFLAKE$CURRENT', 'AGENT_TYPE') AS agent_type,
  SYS_CONTEXT('SNOWFLAKE$CURRENT', 'AGENT_DATABASE') AS agent_db,
  SYS_CONTEXT('SNOWFLAKE$CURRENT', 'AGENT_SCHEMA') AS agent_schema,
  SYS_CONTEXT('SNOWFLAKE$CURRENT', 'AGENT_NAME') AS agent_name;

Checking role activation in the current context

The following example checks whether a role is activated in the current execution context (which may differ from the session context inside an owner’s rights executable):

SELECT SYS_CONTEXT('SNOWFLAKE$CURRENT', 'IS_ROLE_ACTIVATED', 'analyst')::BOOLEAN;

Checking database role activation in the current context

The following example checks whether a database role is activated in the current execution context:

SELECT SYS_CONTEXT('SNOWFLAKE$CURRENT', 'IS_DATABASE_ROLE_ACTIVATED', 'ANALYST_ROLE')::BOOLEAN;