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. There are currently no properties available in this namespace.

'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 functions in this namespace when testing policies, use the corresponding POLICY_CONTEXT arguments. For example, use 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:

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;