The SET statement can modify one of the session configuration variables. These can also be queried via SHOW. By default, session variable values are set for the duration of the current session.
New in v21.2:
 CockroachDB supports setting session variables for the duration of a single transaction, using the LOCAL keyword.
The SET statement for session variables is unrelated to the other SET TRANSACTION and SET CLUSTER SETTING 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. For examples in different languages, see the Build an App with CockroachDB tutorials.
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 privileges 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
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 is compatible with savepoints. Executing a ROLLBACK, ROLLBACK TO SAVEPOINT, or RELEASE TO SAVEPOINT statement rolls back any variables set by SET LOCAL.
Parameters
| Parameter | Description | 
|---|---|
| var_name | The name of the session variable to set. The variable name is case-insensitive. | 
| var_value | The value, or list of values, to assign to the session variable. | 
Supported variables
| Variable name | Description | Initial value | Modify with SET? | View with SHOW? | 
|---|---|---|---|---|
| application_name | The current application name for statistics collection. | Empty string, or cockroachfor sessions from the built-in SQL client. | Yes | Yes | 
| bytea_output | The mode for conversions from STRINGtoBYTES. | hex | Yes | Yes | 
| client_min_messages | The severity level of notices displayed in the SQL shell. Accepted values include debug5,debug4,debug3,debug2,debug1,log,notice,warning, anderror. | notice | Yes | Yes | 
| crdb_version | The version of CockroachDB. | CockroachDB OSS version | No | Yes | 
| database | The current database. | Database in connection string, or empty if not specified. | Yes | Yes | 
| datestyle | New in v21.2:
 The input string format for DATEandTIMESTAMPvalues.Accepted values include ISO,MDY,ISO,DMY, andISO,YMD.To set datestyleto a value other than the default (ISO,MDY), you must first set thedatestyle_enabledsession variable totrue. | The value set by the sql.defaults.datestylecluster setting (ISO,MDY, by default). | Yes | Yes | 
| datestyle_enabled | New in v21.2:
 Enables setting the datestylesession variable to a supported format. | The value set by the sql.defaults.datestyle.enabledcluster setting (false, by default). | Yes | Yes | 
| default_int_size | The size, in bytes, of an INTtype. | 8 | Yes | Yes | 
| default_transaction_isolation | All transactions execute with SERIALIZABLEisolation. See Transactions: Isolation levels. | SERIALIZABLE | No | Yes | 
| default_transaction_priority | The default transaction priority for the current session. The supported options are low,normal, andhigh. | normal | Yes | Yes | 
| default_transaction_read_only | The default transaction access mode for the current session. If set to on, only read operations are allowed in transactions in the current session; if set tooff, both read and write operations are allowed. SeeSET TRANSACTIONfor more details. | off | Yes | Yes | 
| default_transaction_use_follower_reads | If set to on, all read-only transactions useAS OF SYSTEM TIME follower_read_timestamp(), to allow the transaction to use follower reads.If set to off, read-only transactions will only use follower reads if anAS OF SYSTEM TIMEclause is specified in the statement, with an interval of at least 4.8 seconds. | off | Yes | Yes | 
| disallow_full_table_scans | If set to on, all queries that have planned a full table or full secondary index scan will return an error message.This setting does not apply to internal queries, which may plan full table or index scans without checking the session variable. | off | Yes | Yes | 
| distsql | The query distribution mode for the session. By default, CockroachDB determines which queries are faster to execute if distributed across multiple nodes, and all other queries are run through the gateway node. | auto | Yes | Yes | 
| enable_drop_enum_value | Indicates whether DROP VALUEclauses are enabled forALTER TYPEstatements. | off | Yes | Yes | 
| enable_implicit_select_for_update | Indicates whether UPDATEandUPSERTstatements acquire locks using theFOR UPDATElocking mode during their initial row scan, which improves performance for contended workloads.For more information about how FOR UPDATElocking works, see the documentation forSELECT FOR UPDATE. | on | Yes | Yes | 
| enable_insert_fast_path | Indicates whether CockroachDB will use a specialized execution operator for inserting into a table. We recommend leaving this setting on. | on | Yes | Yes | 
| enable_zigzag_join | Indicates whether the cost-based optimizer will plan certain queries using a zig-zag merge join algorithm, which searches for the desired intersection by jumping back and forth between the indexes based on the fact that after constraining indexes, they share an ordering. | on | Yes | Yes | 
| extra_float_digits | The number of digits displayed for floating-point values. Only values between -15and3are supported. | 0 | Yes | Yes | 
| force_savepoint_restart | When set to true, allows theSAVEPOINTstatement to accept any name for a savepoint. | off | Yes | Yes | 
| foreign_key_cascades_limit | Limits the number of cascading operations that run as part of a single query. | 10000 | Yes | Yes | 
| idle_in_session_timeout | Automatically terminates sessions that idle past the specified threshold. When set to 0, the session will not timeout. | The value set by the sql.defaults.idle_in_session_timeoutcluster setting (0s, by default). | Yes | Yes | 
| idle_in_transaction_session_timeout | Automatically terminates sessions that are idle in a transaction past the specified threshold. When set to 0, the session will not timeout. | The value set by the sql.defaults.idle_in_transaction_session_timeoutcluster setting (0s, by default). | Yes | Yes | 
| intervalstyle | New in v21.2:
 The input string format for INTERVALvalues.Accepted values include postgres,iso_8601, andsql_standard.To set intervalstyleto a value other than the default (postgres), you must first set theintervalstyle_enabledsession variable totrue. | The value set by the sql.defaults.intervalstylecluster setting (postgres, by default). | Yes | Yes | 
| intervalstyle_enabled | New in v21.2:
 Enables setting the intervalstylesession variable to a supported format. | The value set by the sql.defaults.intervalstyle.enabledcluster setting (false, by default). | Yes | Yes | 
| is_superuser | New in v21.2:
 If onortrue, the current user is a member of the `admin` role. | User-dependent | No | Yes | 
| large_full_scan_rows | New in v21.2:
 Determines which tables are considered "large" such that disallow_full_table_scansrejects full table or index scans of "large" tables. The default value is1000. To reject all full table or index scans, set to0. | User-dependent | No | Yes | 
| locality | The location of the node. For more information, see Locality. | Node-dependent | No | Yes | 
| lock_timeout | New in v21.2:
 The amount of time a query can spend acquiring or waiting for a single row-level lock. In CockroachDB, unlike in PostgreSQL, non-locking reads wait for conflicting locks to be released. As a result, the lock_timeoutconfiguration applies to writes, and to locking and non-locking reads in read-write and read-only transactions.If lock_timeout = 0, queries do not timeout due to lock acquisitions. | The value set by the sql.defaults.lock_timeoutcluster setting (0, by default) | Yes | Yes | 
| node_id | The ID of the node currently connected to. This variable is particularly useful for verifying load balanced connections. | Node-dependent | No | Yes | 
| optimizer_use_histograms | If on, the optimizer uses collected histograms for cardinality estimation. | on | No | Yes | 
| optimizer_use_multicol_stats | If on, the optimizer uses collected multi-column statistics for cardinality estimation. | on | No | Yes | 
| prefer_lookup_joins_for_fks | If on, the optimizer preferslookup joinstomerge joinswhen performingforeign keychecks. | off | Yes | Yes | 
| reorder_joins_limit | Maximum number of joins that the optimizer will attempt to reorder when searching for an optimal query execution plan. For more information, see Join reordering. | 8 | Yes | Yes | 
| results_buffer_size | The default size of the buffer that accumulates results for a statement or a batch of statements before they are sent to the client. This can also be set for all connections using the sql.defaults.results_buffer_sizecluster setting. Note that auto-retries generally only happen while no results have been delivered to the client, so reducing this size can increase the number of retriable errors a client receives. On the other hand, increasing the buffer size can increase the delay until the client receives the first result row. Setting to 0 disables any buffering. | 16384 | Yes | Yes | 
| require_explicit_primary_keys | If on, CockroachDB throws on error for all tables created without an explicit primary key defined. | off | Yes | Yes | 
| search_path | A list of schemas that will be searched to resolve unqualified table or function names. For more details, see SQL name resolution. | public | Yes | Yes | 
| serial_normalization | Specifies the default handling of SERIALin table definitions. Valid options include'rowid','virtual_sequence',sql_sequence, andsql_sequence_cached.If set to 'virtual_sequence', theSERIALtype auto-creates a sequence for better compatibility with Hibernate sequences.If set to sql_sequence_cached, thesql.defaults.serial_sequences_cache_sizecluster setting can be used to control the number of values to cache in a user's session, with a default of 256. | 'rowid' | Yes | Yes | 
| server_version | The version of PostgreSQL that CockroachDB emulates. | Version-dependent | No | Yes | 
| server_version_num | The version of PostgreSQL that CockroachDB emulates. | Version-dependent | Yes | Yes | 
| session_id | The ID of the current session. | Session-dependent | No | Yes | 
| session_user | The user connected for the current session. | User in connection string | No | Yes | 
| sql_safe_updates | If false, potentially unsafe SQL statements are allowed, includingDROPof a non-empty database and all dependent objects,DELETEwithout aWHEREclause,UPDATEwithout aWHEREclause, andALTER TABLE .. DROP COLUMN.See Allow Potentially Unsafe SQL Statements for more details. | truefor interactive sessions from the built-in SQL client,falsefor sessions from other clients | Yes | Yes | 
| statement_timeout | The amount of time a statement can run before being stopped. This value can be an int(e.g.,10) and will be interpreted as milliseconds. It can also be an interval or string argument, where the string can be parsed as a valid interval (e.g.,'4s').A value of 0turns it off. | The value set by the sql.defaults.statement_timeoutcluster setting (0s, by default). | Yes | Yes | 
| stub_catalog_tables | If off, querying an unimplemented, emptypg_catalogtable will result in an error, as is the case in v20.2 and earlier.If on, querying an unimplemented, emptypg_catalogtable simply returns no rows. | on | Yes | Yes | 
| timezone | The default time zone for the current session. This session variable was named "time zone"(with a space) in CockroachDB 1.x. It has been renamed for compatibility with PostgreSQL. | UTC | Yes | Yes | 
| tracing | The trace recording state. | off | Yes | |
| transaction_isolation | All transactions execute with SERIALIZABLEisolation.See Transactions: Isolation levels. This session variable was called transaction isolation level(with spaces) in CockroachDB 1.x. It has been renamed for compatibility with PostgreSQL. | SERIALIZABLE | No | Yes | 
| transaction_priority | The priority of the current transaction. See Transactions: Transaction priorities for more details. This session variable was called transaction priority(with a space) in CockroachDB 1.x. It has been renamed for compatibility with PostgreSQL. | NORMAL | Yes | Yes | 
| transaction_read_only | The access mode of the current transaction. See Set Transaction for more details. | off | Yes | Yes | 
| transaction_rows_read_err | New in v21.2:
 The limit for the number of rows read by a SQL transaction. If this value is exceeded the transaction will fail (or the event will be logged to SQL_INTERNAL_PERFfor internal transactions). | 0 | Yes | Yes | 
| transaction_rows_read_log | New in v21.2:
 The threshold for the number of rows read by a SQL transaction. If this value is exceeded, the event will be logged to SQL_PERF(orSQL_INTERNAL_PERFfor internal transactions). | 0 | Yes | Yes | 
| transaction_rows_written_err | New in v21.2:
 The limit for the number of rows written by a SQL transaction. If this value is exceeded the transaction will fail (or the event will be logged to SQL_INTERNAL_PERFfor internal transactions). | 0 | Yes | Yes | 
| transaction_rows_written_log | New in v21.2:
 The threshold for the number of rows written by a SQL transaction. If this value is exceeded, the event will be logged to SQL_PERF(orSQL_INTERNAL_PERFfor internal transactions). | 0 | Yes | Yes | 
| transaction_status | The state of the current transaction. See Transactions for more details. This session variable was called transaction status(with a space) in CockroachDB 1.x. It has been renamed for compatibility with PostgreSQL. | NoTxn | No | Yes | 
| vectorize | The vectorized execution engine mode. Options include onandoff.For more details, see Configure vectorized execution for CockroachDB. | on | Yes | Yes | 
| backslash_quote | (Reserved; exposed only for ORM compatibility.) | safe_encoding | No | Yes | 
| client_encoding | (Reserved; exposed only for ORM compatibility.) | UTF8 | No | Yes | 
| default_tablespace | (Reserved; exposed only for ORM compatibility.) |  | No | Yes | 
| enable_seqscan | (Reserved; exposed only for ORM compatibility.) | on | Yes | Yes | 
| escape_string_warning | (Reserved; exposed only for ORM compatibility.) | on | No | Yes | 
| integer_datetimes | (Reserved; exposed only for ORM compatibility.) | on | No | Yes | 
| max_identifier_length | (Reserved; exposed only for ORM compatibility.) | 128 | No | Yes | 
| max_index_keys | (Reserved; exposed only for ORM compatibility.) | 32 | No | Yes | 
| row_security | (Reserved; exposed only for ORM compatibility.) | off | No | Yes | 
| standard_conforming_strings | (Reserved; exposed only for ORM compatibility.) | on | No | Yes | 
| server_encoding | (Reserved; exposed only for ORM compatibility.) | UTF8 | Yes | Yes | 
| synchronize_seqscans | (Reserved; exposed only for ORM compatibility.) | on | No | Yes | 
| synchronous_commit | (Reserved; exposed only for ORM compatibility.) | on | Yes | Yes | 
| troubleshooting_mode_enabled | When enabled, avoid performing additional work on queries, such as collecting and emitting telemetry data. This session variable is particularly useful when the cluster is experiencing issues, unavailability, or failure. | off | Yes | Yes | 
Special syntax cases
CockroachDB supports the following syntax cases, for compatibility with common SQL syntax patterns:
| Syntax | Equivalent to | Notes | 
|---|---|---|
| USE ... | SET database = ... | This is provided as convenience for users with a MySQL/MSSQL background. | 
| SET NAMES ... | SET client_encoding = ... | This is provided for compatibility with PostgreSQL clients. | 
| SET ROLE <role> | SET role = <role> | New in v21.2: This is provided for compatibility with PostgreSQL clients. | 
| RESET ROLE | SET role = 'none'/SET role = current_user() | New in v21.2: This is provided for compatibility with PostgreSQL clients. | 
| SET SCHEMA <name> | SET search_path = <name> | This is provided for better compatibility with PostgreSQL. | 
| SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL ... | SET default_transaction_isolation = ... | This is provided for compatibility with standard SQL. | 
| SET TIME ZONE ... | SET timezone = ... | This is provided for compatibility with PostgreSQL clients. | 
Examples
Set simple variables
The following examples demonstrate how to use SET to configure the default database for the current session:
SET application_name = movr_app;
SHOW application_name;
  application_name
--------------------
  movr_app
(1 row)
Set variables to values containing spaces
The following demonstrates how to use quoting to use values containing spaces:
SET application_name = "movr app";
SHOW application_name;
  application_name
--------------------
  movr app
(1 row)
Set variables to a list of values
The following demonstrates how to assign a list of values:
SET search_path = pg_catalog,public;
SHOW search_path;
     search_path
----------------------
  pg_catalog, public
(1 row)
Reset a variable to its default value
You can use RESET to reset a session variable as well.
SHOW search_path;
     search_path
----------------------
  pg_catalog, public
(1 row)
SET search_path = DEFAULT;
SHOW search_path;
    search_path
-------------------
  "$user", public
(1 row)
Set a variable for the duration of a single transaction
New in v21.2:
 To set a variable for the duration of a single transaction, use the SET LOCAL statement.
SHOW application_name;
  application_name
--------------------
  movr app
(1 row)
BEGIN;
SET LOCAL application_name = demo;
SHOW application_name;
  application_name
--------------------
  demo
(1 row)
COMMIT;
SHOW application_name;
  application_name
--------------------
  movr app
(1 row)
Roll back session variables set for a transaction
New in v21.2: You can roll back session variable settings to savepoints.
SHOW timezone;
  timezone
------------
  UTC
(1 row)
BEGIN;
SET timezone = '+3';
SAVEPOINT s1;
SHOW timezone;
  timezone
------------
  +3
(1 row)
SET LOCAL timezone = '+1';
SHOW timezone;
  timezone
------------
  +1
(1 row)
ROLLBACK TO SAVEPOINT s1;
SHOW timezone;
  timezone
------------
  +3
(1 row)
COMMIT;
SHOW timezone;
  timezone
------------
  +3
(1 row)
Assume another role
New in v21.2:
 To assume another role 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.
SHOW role;
  role
--------
  root
(1 row)
CREATE ROLE new_role;
SHOW ROLES;
  username | options | member_of
-----------+---------+------------
  admin    |         | {}
  new_role | NOLOGIN | {}
  root     |         | {admin}
(3 rows)
SET ROLE new_role;
SHOW role;
    role
------------
  new_role
(1 row)
To reset the role of the current user, use a RESET statement. RESET ROLE is equivalent to SET role = 'none' and SET role = current_user().
RESET ROLE;
SHOW role;
  role
--------
  root
(1 row)
To assume a role for the duration of a single transaction, use SET LOCAL ROLE.
BEGIN;
SET LOCAL ROLE new_role;
SHOW role;
    role
------------
  new_role
(1 row)
COMMIT;
SHOW role;
  role
--------
  root
(1 row)
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 TIMESTAMPTZ/TIMESTAMP WITH TIME ZONE and TIMETZ/TIME WITH TIME ZONE 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. 
- Timezone abbreviations must be part of the tz database, as recognized by the - tzdataGolang package.
- DEFAULT,- LOCAL, or- 0sets 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- UTCor- GMTfor 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 TIME ZONE 'EST'; -- same as SET "timezone" = 'EST'
> SHOW TIME ZONE;
  timezone
+----------+
  EST
(1 row)
> SET TIME ZONE DEFAULT; -- same as SET "timezone" = DEFAULT
> SHOW TIME ZONE;
  timezone
+----------+
  UTC
(1 row)
SET TRACING
SET TRACING changes the trace recording state of the current session. A trace recording can be inspected with the SHOW TRACE FOR SESSION statement.
| Value | Description | 
|---|---|
| off | Trace recording is disabled. | 
| cluster | Trace recording is enabled; distributed traces are collected. | 
| on | Same as cluster. | 
| kv | Same as clusterexcept that "kv messages" are collected instead of regular trace messages. SeeSHOW TRACE FOR SESSION. | 
| results | Result rows and row counts are copied to the session trace. This must be specified in order for the output of a query to be printed in the session trace. Example: SET tracing = kv, results; | 
Known Limitations
SET  does not properly apply ROLLBACK within a transaction. For example, in the following transaction, showing the TIME ZONE variable does not return 2 as expected after the rollback:
SET TIME ZONE +2;
BEGIN;
SET TIME ZONE +3;
ROLLBACK;
SHOW TIME ZONE;
timezone
------------
3