Updated on 2024-10-14 GMT+08:00

SET TRANSACTION

Function

SET TRANSACTION sets characteristics of a transaction. Available transaction characteristics include the transaction separation level and transaction access mode (read/write or read only). You can set the current transaction characteristics using LOCAL or the default transaction characteristics of a session using SESSION.

Precautions

The current transaction characteristics must be set in a transaction, that is, START TRANSACTION or BEGIN must be executed before SET TRANSACTION is executed. Otherwise, the setting does not take effect.

Syntax

Set the isolation level and access mode of the transaction.
1
2
3
{ SET [ LOCAL ] TRANSACTION|SET SESSION CHARACTERISTICS AS TRANSACTION }
  { ISOLATION LEVEL { READ COMMITTED | READ UNCOMMITTED | SERIALIZABLE | REPEATABLE READ }
  | { READ WRITE | READ ONLY } } [, ...]

Parameter Description

  • LOCAL

    Specifies that the specified statement takes effect only for the current transaction.

  • SESSION

    Specifies that the specified parameters take effect for the current session.

    Value range: a string. It must comply with the naming convention.

  • ISOLATION_LEVEL_CLAUSE
    Specifies the transaction isolation level that determines the data that a transaction can view if other concurrent transactions exist.
    • The isolation level cannot be changed after data is modified using INSERT, DELETE, UPDATE, FETCH, or COPY in the current transaction.

    Value range:

    • READ COMMITTED: Only committed data is read. It is the default value.
    • READ UNCOMMITTED: Uncommitted data is probably read. This isolation level is provided to handle CN breakdown emergencies. On this isolation level, you are advised to only read data to prevent inconsistency.
    • REPEATABLE READ: Only the data committed before transaction start is read. Uncommitted data or data committed in other concurrent transactions cannot be read.
    • SERIALIZABLE: Currently, this isolation level is not supported in GaussDB. It is equivalent to REPEATABLE READ.
  • READ WRITE | READ ONLY

    Specifies the transaction access mode (read/write or read only).

    The access mode of the default transaction feature of the session can be set only when the database is started or by sending the HUP signal.

Examples

1
2
3
4
-- Start a transaction and set its isolation level to READ COMMITTED and access mode to READ ONLY.
openGauss=# START TRANSACTION;
openGauss=# SET LOCAL TRANSACTION ISOLATION LEVEL READ COMMITTED READ ONLY;
openGauss=# COMMIT;