Project: YugabyteDB (App Definition and Development / Database)
File: txn_set.md

title: SET TRANSACTION statement [YSQL] headerTitle: SET TRANSACTION linkTitle: SET TRANSACTION description: Use the `SET TRANSACTION` statement to set the current transaction isolation level. summary: SET TRANSACTION menu: v2.18: identifier: txn_set parent: statements type: docs Use the `SET TRANSACTION` statement to set the current transaction isolation level. {{%ebnf%}} set_transaction, transaction_mode, isolation_level, readwritemode, deferrable_mode {{%/ebnf%}} Supports Serializable, Snapshot and Read Committed Isolation<sup>$</sup> using the PostgreSQL isolation level syntax of `SERIALIZABLE`, `REPEATABLE READ` and `READ COMMITTED` respectively. PostgreSQL's `READ UNCOMMITTED` also maps to Read Committed Isolation. <sup>$</sup> Read Committed Isolation is supported only if the YB-TServer flag `ybenablereadcommittedisolation` is set to `true`. By default this flag is `false` and in this case the Read Committed isolation level of YugabyteDB's transactional layer falls back to the stricter Snapshot Isolation (in which case `READ COMMITTED` and `READ UNCOMMITTED` of YSQL also in turn use Snapshot Isolation). Read Committed support is currently in . Set the transaction mode to one of the following. `ISOLATION LEVEL` clause Access mode `DEFERRABLE` mode Default in ANSI SQL standard. Maps to Snapshot Isolation of YugabyteDB. Read Committed support is currently in . Default in PostgreSQL and YSQL. If `ybenablereadcommittedisolation=true`, `READ COMMITTED` is mapped to Read Committed of YugabyteDB's transactional layer (that is, a statement will see all rows that are committed before it begins). But, by default `ybenablereadcommittedisolation=false` and in this case Read Committed of YugabyteDB's transactional layer falls back to the stricter Snapshot Isolation. Essentially this boils down to the fact that Snapshot Isolation is the default in YSQL. `READ UNCOMMITTED` maps to Read Committed of YugabyteDB's transactional layer (note that Read Committed in the transactional layer might in turn map to Snapshot Isolation if `ybenablereadcommittedisolation=false`). In PostgreSQL, `READ UNCOMMITTED` is mapped to `READ COMMITTED`. Default. The `READ ONLY` mode does not prevent all writes to disk. When a transaction is `READ ONLY`, the following SQL statements are: Disallowed if the table they would write to is not a temporary table. INSERT UPDATE DELETE COPY FROM Always disallowed COMMENT GRANT REVOKE TRUNCATE Disallowed when the statement that would be executed is one of the above EXECUTE EXPLAIN ANALYZE Use to defer a transaction only when both `SERIALIZABLE` and `READ ONLY` modes are also

If used, then the transaction may block when first acquiring its snapshot, after which it is able to run without the normal overhead of a `SERIALIZABLE` transaction and without any risk of contributing to, or being canceled by a serialization failure. The `DEFERRABLE` mode may be useful for long-running reports or back-ups. Create a sample table. ```plpgsql yugabyte=# CREATE TABLE sample(k1 int, k2 int, v1 int, v2 text, PRIMARY KEY (k1, k2)); ``` Begin a transaction and insert some rows. ```plpgsql yugabyte=# BEGIN TRANSACTION; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; ``` ```plpgsql yugabyte=# INSERT INTO sample(k1, k2, v1, v2) VALUES (1, 2.0, 3, 'a'), (1, 3.0, 4, 'b'); ``` Start a new shell with `ysqlsh` and begin another transaction to insert some more rows. ```plpgsql yugabyte=# BEGIN TRANSACTION; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; ``` ```plpgsql yugabyte=# INSERT INTO sample(k1, k2, v1, v2) VALUES (2, 2.0, 3, 'a'), (2, 3.0, 4, 'b'); ``` In each shell, check the only the rows from the current transaction are visible. 1st shell. ```plpgsql yugabyte=# SELECT * FROM sample; -- run in first shell ``` ``` k1 | k2 | v1 | v2 -+-+-+- 1 | 2 | 3 | a 1 | 3 | 4 | b (2 rows) ``` 2nd shell ```plpgsql yugabyte=# SELECT * FROM sample; -- run in second shell ``` ``` k1 | k2 | v1 | v2 -+-+-+- 2 | 2 | 3 | a 2 | 3 | 4 | b (2 rows) ``` Commit the first transaction and abort the second one. ```plpgsql yugabyte=# COMMIT TRANSACTION; -- run in first shell. ``` Abort the current transaction (from the first shell). ```plpgsql yugabyte=# ABORT TRANSACTION; -- run second shell. ``` In each shell check that only the rows from the committed transaction are visible. ```plpgsql yugabyte=# SELECT * FROM sample; -- run in first shell. ``` ``` k1 | k2 | v1 | v2 -+-+-+- 1 | 2 | 3 | a 1 | 3 | 4 | b (2 rows) ``` ```plpgsql yugabyte=# SELECT * FROM sample; -- run in second shell. ``` ``` k1 | k2 | v1 | v2 -+-+-+- 1 | 2 | 3 | a 1 | 3 | 4 | b (2 rows) ```