On this page
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 |
If you do not want the transaction to run as SERIALIZABLE (CockroachDB's default, which provides the highest level of isolation), you can set the isolation level to SNAPSHOT , which can provide better performance in high-contention scenarios.For more information, see Transactions: Isolation Levels. 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. Default: NORMAL |
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)