ROLLBACK
Attention
This page documents an earlier version. Go to the latest (v2.0)version.Synopsis
ROLLBACK
command rolls back the current transactions. All changes included in this transactions will be discarded.
Grammar
Diagrams
Syntax
rollback_transaction ::= { 'ROLLBACK' } [ 'TRANSACTION' | 'WORK' ] ;
Semantics
Supports both Serializable and Snapshot Isolation using the PostgreSQL isolation level syntax of SERIALIZABLE
and REPEATABLE READS
respectively. Even READ COMMITTED
and READ UNCOMMITTED
isolation levels are mapped to Snapshot Isolation.
Note that the Serializable isolation level support was added in v1.2.6. The examples on this page have not been updated to reflect this recent addition.
Examples
Create a sample table.
postgres=# CREATE TABLE sample(k1 int, k2 int, v1 int, v2 text, PRIMARY KEY (k1, k2));
Begin a transaction and insert some rows.
postgres=# BEGIN TRANSACTION; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
postgres=# 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.
postgres=# BEGIN TRANSACTION; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
postgres=# 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.
postgres=# SELECT * FROM sample; -- run in first shell
k1 | k2 | v1 | v2
----+----+----+----
1 | 2 | 3 | a
1 | 3 | 4 | b
(2 rows)
2nd shell
postgres=# 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.
postgres=# COMMIT TRANSACTION; -- run in first shell.
Abort the current transaction (from the first shell).
postgres=# ABORT TRANSACTION; -- run second shell.
In each shell check that only the rows from the committed transaction are visible.
postgres=# SELECT * FROM sample; -- run in first shell.
k1 | k2 | v1 | v2
----+----+----+----
1 | 2 | 3 | a
1 | 3 | 4 | b
(2 rows)
postgres=# SELECT * FROM sample; -- run in second shell.
k1 | k2 | v1 | v2
----+----+----+----
1 | 2 | 3 | a
1 | 3 | 4 | b
(2 rows)