Skip to main content
The SET can modify one of the session configuration variables. These can also be queried via . By default, session variable values are set for all future sessions to the database; they do not affect the current session.
The SET statement for session variables is unrelated to the other and statements.
In some cases, client drivers can drop and restart the connection to the server. When this happens, any session configurations made with SET statements are lost. It is therefore more reliable to configure the session in the client’s connection string.

Required privileges

To set the role session variable, the current user must be a member of the admin role, or a member of the target role. All other session variables do not require to modify.

Synopsis

The SET statement can set a session variable for the duration of the current session (SET {variable}/SET SESSION {variable}), or for the duration of a single transaction (SET LOCAL {variable}).

SET SESSION

set_session syntax diagram
By default, session variables are set for the duration of the current session. As a result, SET {variable} and SET SESSION {variable} are equivalent.

SET LOCAL

set_local syntax diagram
SET LOCAL is compatible with . Executing a , ROLLBACK TO SAVEPOINT, or RELEASE TO SAVEPOINT statement rolls back any variables set by SET LOCAL.

Parameters

Supported variables

The following session variables are exposed only for backwards compatibility with earlier CockroachDB releases and have no impact on how CockroachDB runs:

Examples

Set simple variables

The following examples demonstrate how to use SET to configure the default database for the current session:

Set variables to values containing spaces

The following demonstrates how to use quoting to use values containing spaces:

Set variables to a list of values

The following demonstrates how to assign a list of values:

Reset a variable to its default value

You can use to reset a session variable as well.

Set a variable for the duration of a single transaction

To set a variable for the duration of a single transaction, use the SET LOCAL statement.

Roll back session variables set for a transaction

You can roll back session variable settings to .

Assume another role

To assume another for the duration of a session, use SET ROLE <role>. SET ROLE <role> is equivalent to SET role = <role>.
To assume a new role, the current user must be a member of the admin role, or a member of the target role.
To reset the role of the current user, use a statement. RESET ROLE is equivalent to SET role = 'none' and SET role = current_user().
To assume a role for the duration of a single transaction, use SET LOCAL ROLE.

SET TIME ZONE

As a best practice, we recommend not using this setting and avoid setting a session time for your database. We instead recommend converting UTC values to the appropriate time zone on the client side.
You can control the default time zone for a session with SET TIME ZONE. This will apply an offset to all and values in the session. By default, CockroachDB uses UTC as the time zone for SET TIME ZONE offsets.

Parameters

The input passed to SET TIME ZONE indicates the time zone for the current session. This value can be a string representation of a local system-defined time zone (e.g., 'EST', 'America/New_York') or a positive or negative numeric offset from UTC (e.g., -7, +7, or UTC-7, UTC+7) or GMT (e.g., GMT-7, GMT+7). The numeric offset input can also be colon-delimited (e.g., -7:00, GMT+7:00). When setting a time zone, note the following:
  • Timezone abbreviations are case-insensitive.
  • To see a list of supported timezones, their nicknames, and their offsets, run the following query:
  • DEFAULT, LOCAL, or 0 sets the session time zone to UTC.
  • Only offsets specified by integers (e.g., -7, 7) use the ISO 8601 time offset (i.e., the offset input is parsed as hours east of UTC). If you explicitly specify UTC or GMT for the time zone offset (e.g., UTC-7,GMT+7), or if the numeric input is colon-delimited (e.g., -7:00, GMT+7:00), CockroachDB uses the POSIX time offset instead (i.e., hours west of the specified time zone). This means that specifying an offset of -7 (i.e., -7 east of UTC) is equivalent to specifying GMT+7 (i.e., 7 west of UTC).

Example: Set the default time zone via SET TIME ZONE

SET TRACING

SET TRACING changes the trace recording state of the current session. A trace recording can be inspected with the statement.

Considerations

Session variable precedence

When a starts, CockroachDB determines the initial value of each by evaluating the settings in the following order (items earlier in the list take precedence over later items):
  1. : A value supplied as a query parameter in the connection URL (for example, .../movr?sslmode=disable&timezone=UTC).
  2. : A value set by ALTER ROLE {role_name} IN DATABASE {db_name} SET {var}={value}.
  3. : A value set by ALTER ROLE {role_name} SET {var}={value}.
  4. : A value set by ALTER ROLE ALL IN DATABASE {db_name} SET {var}={value} or equivalently by ALTER DATABASE {db_name} SET {var}={value}.
  5. : A value set by ALTER ROLE ALL SET {var}={value}.
If a session variable is not modified using any of the preceding methods, the built-in system default value is used. Note that the is exempt from settings 3–5. The root user is only affected by values specified in the connection string. You can set session variables for the duration of a single transaction by using . You can also for executions of a specific statement fingerprint by using the function.
Changes to defaults using the preceding methods only apply to future sessions. This is because session variable resolution happens at session start time. To change a default value in an existing open session, set the variable explicitly with .

Known limitations

  • SET and RESET do not properly apply within a transaction. For example:
  • When setting the default_int_size in a batch of statements such as SET default_int_size='int4'; SELECT 1::INT, the default_int_size variable will not take effect until the next statement. Statement parsing is asynchronous with statement execution. As a workaround, set default_int_size via your database driver, or ensure that SET default_int_size is in its own statement.

See also