Categories:

Context functions (General)

SYS_CONTEXT (snowflake$session_attributes namespace)

Returns a custom session attribute that was set using SET_SYS_CONTEXT in the snowflake$session_attributes namespace.

Custom session attributes are immutable once set and persist for the duration of the session. They are useful for tracking metadata about a session, such as application context, user attributes, or audit information.

See also:

SYS_CONTEXT , SET_SYS_CONTEXT

Syntax

SYS_CONTEXT(
  'snowflake$session_attributes' ,
  '<key>'
)

Arguments

'snowflake$session_attributes'

Specifies that you want to retrieve a custom session attribute.

'key'

The name of the custom attribute to retrieve. Attribute names are case-sensitive.

Returns

The function returns a VARCHAR value:

  • The value of the specified attribute if it has been set in the current session using SET_SYS_CONTEXT.

  • NULL if the attribute has not been set.

Access control requirements

No special privileges are required to retrieve custom session attributes. Any user can retrieve attributes from their own session.

Usage notes

  • Attributes must be set using SET_SYS_CONTEXT before they can be retrieved.

  • Attribute names are case-sensitive. app_context and APP_CONTEXT are treated as different attributes.

  • Attributes are session-scoped and are not visible to other sessions.

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

Examples

The following example sets a custom attribute and then retrieves it:

-- Set a custom session attribute
CALL SET_SYS_CONTEXT('snowflake$session_attributes', 'app_context', 'production');

-- Retrieve the custom attribute
SELECT SYS_CONTEXT('snowflake$session_attributes', 'app_context');
+---------------------------------------------------------------+
| SYS_CONTEXT('snowflake$session_attributes', 'app_context')   |
|---------------------------------------------------------------|
| production                                                    |
+---------------------------------------------------------------+

Retrieving an attribute that has not been set returns NULL:

SELECT SYS_CONTEXT('snowflake$session_attributes', 'nonexistent_attr');
+------------------------------------------------------------------+
| SYS_CONTEXT('snowflake$session_attributes', 'nonexistent_attr') |
|------------------------------------------------------------------|
| NULL                                                             |
+------------------------------------------------------------------+

Attribute names are case-sensitive:

-- Set attributes with different cases
CALL SET_SYS_CONTEXT('snowflake$session_attributes', 'mykey', 'lowercase');
CALL SET_SYS_CONTEXT('snowflake$session_attributes', 'MyKey', 'mixedcase');
CALL SET_SYS_CONTEXT('snowflake$session_attributes', 'MYKEY', 'uppercase');

-- Each is a distinct attribute
SELECT
  SYS_CONTEXT('snowflake$session_attributes', 'mykey') AS lower,
  SYS_CONTEXT('snowflake$session_attributes', 'MyKey') AS mixed,
  SYS_CONTEXT('snowflake$session_attributes', 'MYKEY') AS upper;
+-----------+-----------+-----------+
| LOWER     | MIXED     | UPPER     |
|-----------+-----------+-----------|
| lowercase | mixedcase | uppercase |
+-----------+-----------+-----------+