Updated on 2025-06-30 GMT+08:00

Transactions

GaussDB is compatible with MySQL transactions, but there are some differences. This section describes transaction-related differences in GaussDB.

Default Transaction Isolation Levels

The default isolation level in GaussDB is READ COMMITTED, and that of MySQL is REPEATABLE-READ.

-- View the current transaction isolation level.
m_db=# SHOW transaction_isolation;
 transaction_isolation
-----------------------
 read committed
(1 row)

Sub-transactions

In GaussDB, you can use SAVEPOINT to create a savepoint (sub-transaction) in the current transaction, and ROLLBACK TO SAVEPOINT to roll back to the savepoint. After the rollback, the current transaction can continue to run, whose status will not be affected.

No savepoint (sub-transaction) can be created in MySQL.

Nested Transactions

A nested transaction refers to a new transaction started in a transaction block.

In GaussDB, if you run a command to start a new transaction in a normal transaction block, a warning is displayed, indicating an ongoing transaction exists and the command will be ignored; while if you do so in an abnormal transaction block, an error is reported and the transaction can only be started after ROLLBACK is executed or COMMIT is used to commit other statements.

In MySQL, if a new transaction is started in a normal transaction block, the previous transaction is committed and then the new transaction is started. If a new transaction is started in an abnormal transaction block, the error is ignored, and the previous error-free statement is committed and the new transaction is started.

-- In GaussDB, if a new transaction is started in a normal transaction block, a warning is generated and the transaction is ignored.
m_db=# DROP TABLE IF EXISTS test_t;
m_db=# CREATE TABLE test_t(a int, b int);
m_db=# BEGIN;
m_db=# INSERT INTO test_t values(1, 2);
m_db=# BEGIN; -- The warning "There is already a transaction in progress" is displayed.
m_db=# SELECT * FROM test_t ORDER BY 1;
m_db=# COMMIT; 

-- In GaussDB, if a new transaction is started in an abnormal transaction block, an error is reported. The transaction can be executed only after ROLLBACK or COMMIT is executed.
m_db=# BEGIN;
m_db=# ERROR sql; -- Error statement.
m_db=# BEGIN; -- An error is reported.
m_db=# COMMIT; -- It can be executed only after ROLLBACK/COMMIT is executed.

Statements Committed Implicitly

Databases under GaussDB use GaussDB for storage and inherits the GaussDB transaction mechanism. If a DDL or DCL statement is executed in a transaction, the transaction is not automatically committed.

In MySQL, if DDL, DCL, management-related, or lock-related statements are executed, the transaction is automatically committed.

-- In GaussDB, table creation and GUC parameter settings can be rolled back.
m_db=# DROP TABLE IF EXISTS test_table_rollback;
m_db=# BEGIN;
m_db=# CREATE TABLE test_table_rollback(a int, b int);
m_db=# \d test_table_rollback;
m_db=# ROLLBACK;
m_db=# \d test_table_rollback; -- This table does not exist.
Did not find any relation named "test_table_rollback".

Differences in SET TRANSACTION

In GaussDB, if SET TRANSACTION is used to set the isolation level or transaction access mode for multiple times, only the last setting takes effect. Transaction features can be separated by spaces and commas (,).

In MySQL, SET TRANSACTION cannot be used to set the isolation level or transaction access mode for multiple times. Transaction features can only be separated by commas (,).

Table 1 Differences in SET TRANSACTION

Syntax

Description

Difference

SET TRANSACTION

Sets transactions.

In GaussDB, if the m_format_dev_version parameter is not set to 's2', SET TRANSACTION takes effect at the session level, with the same functionality as SET SESSION TRANSACTION. If the m_format_dev_version parameter is set to 's2', SET TRANSACTION sets the next transaction feature. In MySQL, SET TRANSACTION takes effect in the next transaction.

SET SESSION TRANSACTION

Sets session-level transactions.

-

SET GLOBAL TRANSACTION

Sets global session-level transactions. This feature applies to subsequent sessions and has no impact on the current session.

In GaussDB, GLOBAL takes effect in transactions at the global session level and applies only to the current database instance.

In MySQL, this feature takes effect in all databases.

-- SET TRANSACTION takes effect in session-level transactions.
m_db=# SET TRANSACTION ISOLATION LEVEL READ COMMITTED READ WRITE;
m_db=# SHOW transaction_isolation;
m_db=# SHOW transaction_read_only;
-- In GaussDB, if the isolation level or transaction access mode is set for multiple times, only the last one takes effect.
m_db=# SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED, ISOLATION LEVEL REPEATABLE READ, READ WRITE, READ ONLY;
m_db=# SHOW transaction_isolation; -- repeatable read
 transaction_isolation
-----------------------
 repeatable read
(1 row)
m_db=# SHOW transaction_read_only; -- on
 transaction_read_only
-----------------------
 on
(1 row)

Differences in START TRANSACTION

In GaussDB, when START TRANSACTION is used to start a transaction, the isolation level can be set. If the isolation level or transaction access mode is set for multiple times, only the last setting takes effect. In the current version, consistency snapshot cannot be enabled immediately. Transaction features can be separated by spaces or commas (,).

In MySQL, if START TRANSACTION is used to start a transaction, the isolation level cannot be set and the transaction access mode cannot be set for multiple times. Transaction features can only be separated by commas (,).

In MySQL, a transaction at the repeatable read isolation level starts snapshot read only after the first SELECT statement is executed. In GaussDB, once a transaction is started, not only the first SELECT statement performs snapshot read, but also the first executed DDL, DML, or DCL statement creates a consistent read snapshot of the transaction.

-- Start a transaction and set the isolation level.
m_db=# START TRANSACTION ISOLATION LEVEL READ COMMITTED;
m_db=# COMMIT;
-- Set the access mode for multiple times.
m_db=# START TRANSACTION READ ONLY, READ WRITE;
m_db=# COMMIT;

Transaction-related GUC Parameters

Table 2 Differences in transaction-related GUC parameters

GUC Parameter

Description

Difference

autocommit

Sets the automatic transaction commit mode.

-

transaction_isolation

Sets the isolation level of the current transaction in GaussDB.

Sets the isolation level of a session-level transaction in MySQL.

  • In GaussDB, you can only change the isolation level of the current transaction by running the SET transaction_isolation = value command. To change the session-level isolation level, use default_transaction_isolation. In MySQL, you can run the SET command to change the isolation level of a session-level transaction.
  • The supported range is different.
    MySQL supports the following isolation levels, which are case-insensitive but space-sensitive:
    • READ-COMMITTED
    • READ-UNCOMMITTED
    • REPEATABLE-READ
    • SERIALIZABLE
    GaussDB supports the following isolation levels, which are case-sensitive and space-sensitive:
    • read committed
    • read uncommitted
    • repeatable read
    • serializable
    • default (The level is set to be the same as the default isolation level in the session.)
  • In GaussDB, the value of transaction_isolation of a new transaction is initialized to the value of default_transaction_isolation.
  • When m_format_dev_version is set to 's2':
    • You can set the features of the next transaction by running set @@transaction_isolation = value; set transaction isolation level value.
    • You can modify the features of a session-level transaction by running set [local|session|@@session.] transaction_isolation = value.
    • The features of the next transaction cannot be used within the current transaction. If an error is reported for an implicit transaction, that is, a single SQL statement, the features of the next transaction are retained.

tx_isolation

Sets the transaction isolation level.

tx_isolation and transaction_isolation are synonyms.

In GaussDB, only the select @@ syntax can be used for query. The show syntax cannot be used for query or modification.

default_transaction_isolation

Sets the transaction isolation level.

In GaussDB, the SET command is used to change the isolation level of a session-level transaction.

MySQL does not support this system parameter.

transaction_read_only

In GaussDB, this parameter is used to set the access mode of the current transaction.

In MySQL, this parameter is used to set the access mode of session-level transactions.

  • In GaussDB, only the access mode of the current transaction can be changed by using the SET command. If you want to change the access mode of a session-level transaction, you can use default_transaction_read_only.

    In MySQL, you can run the SET command to change the isolation level of a session-level transaction.

  • In GaussDB, the value of transaction_read_only of a new transaction is initialized to the value of default_transaction_read_only.
  • When m_format_dev_version is set to 's2':
    • You can set the next transaction feature by running set @@transaction_read_only = value; set transaction {read write | read only}.
    • You can modify the features of a session-level transaction by running set [local|session|@@session.] transaction_read_only = value.
    • The features of the next transaction cannot be used within the current transaction. If an error is reported for an implicit transaction, that is, a single SQL statement, the features of the next transaction are retained.

tx_read_only

Sets the access mode of a transaction. tx_read_only and transaction_read_only are synonyms.

In GaussDB, only the select @@ syntax can be used for query. The show syntax cannot be used for query or modification.

default_transaction_read_only

Sets the access mode of a transaction.

In GaussDB, the SET command is used to change the access mode of a session-level transaction. MySQL does not support this system parameter.