On this page
Warning:
As of October 4, 2019, CockroachDB v2.0 is no longer supported. For more details, refer to the Release Support Policy.
The SET TRANSACTION
statement sets the transaction isolation level or priority after you BEGIN
it but before executing the first statement that manipulates a database.
Note:
You can also set the session's default isolation level.Synopsis
Required Privileges
No privileges are required to set the transaction isolation level or priority. However, privileges are required for each statement within a transaction.
Parameters
Parameter | Description |
---|---|
ISOLATION LEVEL |
By default, transactions in CockroachDB implement the strongest ANSI isolation level: SERIALIZABLE . At this isolation level, transactions will never result in anomalies. The SNAPSHOT isolation level is still supported as well for backwards compatibility, but you should avoid using it. It provides little benefit in terms of performance and can result in inconsistent state under certain complex workloads. For more information, see Transactions: Isolation Levels.New in v2.0: The current isolation level is also exposed as the session variable transaction_isolation .Default: SERIALIZABLE |
PRIORITY |
If you do not want the transaction to run with NORMAL priority, you can set it to LOW or HIGH .Transactions with higher priority are less likely to need to be retried. For more information, see Transactions: Priorities. The current priority is also exposed as the session variable transaction_priority .Default: NORMAL |
READ |
New in v2.0: Set the transaction access mode to READ ONLY or READ WRITE . The current transaction access mode is also exposed as the session variable transaction_read_only .Default: READ WRITE |
Examples
Set Isolation & Priority
You can set a transaction's isolation level to SNAPSHOT
, as well as its priority to LOW
or HIGH
.
> BEGIN;
> SET TRANSACTION ISOLATION LEVEL SNAPSHOT, PRIORITY HIGH;
Tip:
You can also set both transaction options as a space-separated list, e.g., SET TRANSACTION ISOLATION LEVEL SNAPSHOT PRIORITY HIGH
.> SAVEPOINT cockroach_restart;
> UPDATE products SET inventory = 0 WHERE sku = '8675309';
> INSERT INTO orders (customer, sku, status) VALUES (1001, '8675309', 'new');
> RELEASE SAVEPOINT cockroach_restart;
> COMMIT;
Warning:
This example assumes you're using client-side intervention to handle transaction retries.Set Session's Default Isolation
You can also set the default isolation level for all transactions in the client's current session using SET DEFAULT_TRANSACTION_ISOLATION TO <isolation level>
.
> SHOW DEFAULT_TRANSACTION_ISOLATION;
+-------------------------------+
| default_transaction_isolation |
+-------------------------------+
| SERIALIZABLE |
+-------------------------------+
(1 row)
> SET DEFAULT_TRANSACTION_ISOLATION TO SNAPSHOT;
SET
> SHOW DEFAULT_TRANSACTION_ISOLATION;
+-------------------------------+
| default_transaction_isolation |
+-------------------------------+
| SNAPSHOT |
+-------------------------------+
(1 row)