---
title: "BEGIN"
url: "https://docs.yugabyte.com/stable/api/ysql/the-sql-language/statements/txn_begin/"
---

# BEGIN

Use the `BEGIN` statement to start a transaction with the default (or specified) isolation level.

## Synopsis

Use the `BEGIN` statement to start a transaction with the default (or specified) isolation level.

## Syntax

- [![Grammar Icon](/icons/file-lines.svg)Grammar](#grammar-1)
- [![Diagram Icon](/icons/diagram.svg)Diagram](#diagram-1)

```output.ebnf
begin ::= BEGIN [ TRANSACTION | WORK ] [ transaction_mode [ ... ] ]

transaction_mode ::= isolation_level
                     | read_write_mode
                     | deferrable_mode

isolation_level ::= ISOLATION LEVEL { READ UNCOMMITTED
                                      | READ COMMITTED
                                      | REPEATABLE READ
                                      | SERIALIZABLE }

read_write_mode ::= READ ONLY | READ WRITE

deferrable_mode ::= [ NOT ] DEFERRABLE
```

#### begin

BEGINTRANSACTIONWORK[transaction\_mode](#transaction-mode)

#### transaction\_mode

[isolation\_level](#isolation-level)[read\_write\_mode](#read-write-mode)[deferrable\_mode](#deferrable-mode)

#### isolation\_level

ISOLATIONLEVELREADUNCOMMITTEDREADCOMMITTEDREPEATABLEREADSERIALIZABLE

#### read\_write\_mode

READONLYREADWRITE

#### deferrable\_mode

NOTDEFERRABLE

## Semantics

### *begin*

```plpgsql
BEGIN [ TRANSACTION | WORK ] [ transaction_mode [ ... ] ]
```

#### WORK

Add optional keyword — has no effect.

#### TRANSACTION

Add optional keyword — has no effect.

### *transaction\_mode*

Supports isolation level, read write, and deferrable modes using the transaction mode syntax of `ISOLATION LEVEL`, `READ ONLY`, `READ WRITE`, and `[NOT] DEFERRABLE`.

For isolation level, you can specify Serializable, Snapshot, and Read Committed isolation using the PostgreSQL isolation level syntax of `SERIALIZABLE`, `REPEATABLE READ`, and `READ COMMITTED` respectively. PostgreSQL's `READ UNCOMMITTED` also maps to Read Committed isolation.

Read Committed isolation is supported only if the YB-TServer flag `yb_enable_read_committed_isolation` 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).

## Examples

Create a sample table.

```plpgsql
CREATE TABLE sample(k1 int, k2 int, v1 int, v2 text, PRIMARY KEY (k1, k2));
```

Begin a transaction and insert some rows.

```plpgsql
BEGIN TRANSACTION; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
```

```plpgsql
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
BEGIN TRANSACTION; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
```

```plpgsql
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
```

```output
 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
```

```output
 k1 | k2 | v1 | v2
----+----+----+----
  2 |  2 |  3 | a
  2 |  3 |  4 | b
(2 rows)
```

Commit the first transaction and abort the second one.

```plpgsql
COMMIT TRANSACTION; -- run in first shell.
```

Abort the current transaction (from the first shell).

```plpgsql
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.
```

```output
 k1 | k2 | v1 | v2
----+----+----+----
  1 |  2 |  3 | a
  1 |  3 |  4 | b
(2 rows)
```

```plpgsql
yugabyte=# SELECT * FROM sample; -- run in second shell.
```

```output
 k1 | k2 | v1 | v2
----+----+----+----
  1 |  2 |  3 | a
  1 |  3 |  4 | b
(2 rows)
```

Start a `READ WRITE` transaction using `SERIALIZABLE` isolation level and making foreign keys `DEFERRABLE`:

```plpgsql
yugabyte=# BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE READ WRITE DEFERRABLE;
yugabyte=# -- run queries
yugabyte=# COMMIT;
```

## See also

- [ABORT](/stable/api/ysql/the-sql-language/statements/txn_abort "ABORT")
- [COMMIT](/stable/api/ysql/the-sql-language/statements/txn_commit "COMMIT")
- [END](/stable/api/ysql/the-sql-language/statements/txn_end "END")
- [ROLLBACK](/stable/api/ysql/the-sql-language/statements/txn_rollback "ROLLBACK")
- [SET TRANSACTION](/stable/api/ysql/the-sql-language/statements/txn_set "SET TRANSACTION")
- [START TRANSACTION](/stable/api/ysql/the-sql-language/statements/txn_start "START TRANSACTION")
