Updated on 2024-05-07 GMT+08:00

START TRANSACTION

Function

START TRANSACTION starts a transaction. If the isolation level or read/write mode is specified, a new transaction will have those characteristics. You can also specify them using SET TRANSACTION.

Precautions

None

Syntax

Format 1: START TRANSACTION

START TRANSACTION
  [ 
    { 
       ISOLATION LEVEL { READ COMMITTED | SERIALIZABLE | REPEATABLE READ }
       | { READ WRITE | READ ONLY }
     } [...] 
  ];

Format 2: BEGIN

BEGIN [ WORK | TRANSACTION ]
  [ 
    { 
       ISOLATION LEVEL { READ COMMITTED | SERIALIZABLE | REPEATABLE READ }
       | { READ WRITE | READ ONLY }
      } [...] 
  ];

Parameter Description

  • WORK | TRANSACTION

    Specifies the optional keyword in BEGIN format without functions.

  • ISOLATION LEVEL

    Specifies the transaction isolation level that determines the data that a transaction can view if other concurrent transactions exist.

    The isolation level of a transaction cannot be reset after the first clause (SELECT, INSERT, DELETE, UPDATE, FETCH, COPY) for modifying data is executed in the transaction.

    Value range:

    • READ COMMITTED: Only submitted data is read. It is the default value.
    • 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).

Examples

-- Create a schema.
gaussdb=# CREATE SCHEMA tpcds;

-- Create the tpcds.reason table.
gaussdb=# CREATE TABLE tpcds.reason (c1 int, c2 int);

-- Start a transaction in default mode.
gaussdb=# START TRANSACTION;
gaussdb=# SELECT * FROM tpcds.reason;
gaussdb=# END;

-- Start a transaction in default mode.
gaussdb=# BEGIN;
gaussdb=# SELECT * FROM tpcds.reason;
gaussdb=# END;

-- Start a transaction with the isolation level being READ COMMITTED and the access mode being READ WRITE:
gaussdb=# START TRANSACTION ISOLATION LEVEL READ COMMITTED READ WRITE;
gaussdb=# SELECT * FROM tpcds.reason;
gaussdb=# COMMIT;

-- Delete the tpcds.reason table.
gaussdb=# DROP TABLE tpcds.reason;

-- Delete a schema.
gaussdb=# DROP SCHEMA tpcds;

Helpful Links

COMMIT | END, ROLLBACK, and SET TRANSACTION