openGauss Managing Transactions
Tags: opengauss
Published: 2023-03-10 02:27:17

A transaction is a customized sequence of database operations, which form an integral unit of work. In openGauss, you can start, set, commit, and roll back transactions. The openGauss database supports the following transaction isolation levels: READ COMMITTED, READ UNCOMMITTED (not recommended), REPEATABLE READ, and SERIALIZABLE. SERIALIZABLE is equivalent to REPEATABLE READ.

Controlling Transactions
The following describes transaction operations supported by the database:

Starting transactions

You can start transactions using START TRANSACTION and BEGIN.

Setting transactions

You can use the SET TRANSACTION or SET LOCAL TRANSACTION syntax to set transactions. For details, see SET TRANSACTION.

Committing transactions

You can use the COMMIT or END syntax to commit all operations of a transaction. For details, see COMMIT | END.

Rolling back transactions

Rollback indicates that the system cancels all changes that a transaction has made to a database if the transaction fails to be executed due to a fault. For details, see ROLLBACK.

Transaction Isolation Levels
A transaction isolation level specifies how concurrent transactions process the same object.

 NOTE:

The isolation level cannot be changed after data is modified using SELECT, INSERT, DELETE, UPDATE, FETCH, or COPY in the transaction.

READ COMMITTED: At this level, a transaction can access only committed data. This is the default level.

The SELECT statement accesses the snapshot of the database taken when the query begins. It can also access the data updates in its transaction, Even if the previous update has not been submitted. Note that different database snapshots may be returned to two consecutive SELECT statements for the same transaction, because data may be committed for other transactions while the first SELECT statement is executed.

At the READ COMMITTED level, the execution of each statement begins with a new snapshot, which contains all the transactions that have been committed by the execution time. Therefore, during a transaction, a statement can access the result of other committed transactions. This level ensures that one statement can access consistent views in a database.

Transaction isolation at this level meets the requirements of many applications, and is fast and easy to use. However, applications performing complicated queries and updates may require data that is more consistent than this level can provide.

REPEATABLE READ: At this level, a transaction can only read data committed before it starts. Uncommitted data or data committed in other concurrent transactions cannot be read. However, a query can read earlier data updates in its transaction, regardless of whether they have been committed.

READ COMMITTED differs from this level in that a transaction reads the snapshot taken at the start of the transaction, not at the beginning of the current query within the transaction. Therefore, the SELECT statement within a transaction always reads the same data, and cannot read data committed by other concurrent transactions after the transaction starts. Applications at this level must be able to retry transactions, because serialization failures may occur.