BEGIN

Start the transaction block.

Summary

BEGIN [WORK | TRANSACTION] [transaction_mode]

Where transaction_mode is:

   ISOLATION LEVEL {READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE}
   READ WRITE | READ ONLY
   [ NOT ] DEFERRABLE

describe

BEGIN starts the transaction block, i.e. all statements after the BEGIN command will be executed in a single transaction until an explicit COMMIT or ROLLBACK is given. By default (without BEGIN), the YMatrix database executes transactions in an automatic commit mode, i.e. each statement executes in its own transaction and implicitly executes the commit at the end of the statement (if the execution is successful, otherwise rollback).

In transaction blocks, statements execute faster because transaction startup/commit requires a lot of CPU and disk activity. Executing multiple statements in a transaction is also useful for ensuring consistency when making some related changes: Other sessions will not see the intermediate state, where not all related updates are completed.

If isolation level, read/write mode, or delayable mode is specified, the new transaction has those characteristics, just like executing SET TRANSACTION.

Parameters

WORK

TRANSACTION

  • Optional keywords. They have no effect.

SERIALIZABLE

READ COMMITTED

READ UNCOMMITTED

  • The SQL standard defines four transaction isolation levels: READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ and SERIALIZABLE.
  • READ UNCOMMITTED Allows transactions to view changes made by uncommitted concurrent transactions. This is not possible in the YMatrix database, so READ UNCOMMITTED is the same as READ COMMITTED.
  • READ COMMITTED is the default isolation level in the YMatrix database, ensuring that statements can only see rows submitted before they begin. If another concurrent transaction is submitted after the first execution of the statement, the same statement executed twice in one transaction may produce different results.
  • REPEATABLE READ The isolation level ensures that the transaction can only see rows committed before the transaction begins. REPEATABLE READ is the strictest level of transaction isolation supported by YMatrix databases. Because serialization fails, you must prepare an application with the REPEATABLE READ isolation level to retry the transaction.
  • SERIALIZABLE Transaction isolation level ensures that execution of multiple concurrent transactions has the same effect as executing the same transaction in serial. If SERIALIZABLE is specified, the YMatrix database will fall back to REPEATABLE READ.
  • Specifies that DEFERRABLE is invalid in the YMatrix database, but supports syntax for PostgreSQL compatibility. Defer transactions only if the transaction is READ ONLY and SERIALIZABLE, and the YMatrix database does not support the SERIALIAZABLE transaction.

Note

START TRANSACTION has the same functionality as BEGIN.

Use COMMIT or ROLLBACK to terminate the transaction block.

If it is already in a transaction block, issuing a BEGIN will cause a warning message. Transaction status is not affected. To nest transactions in transaction blocks, use a savepoint (see SAVEPOINT).

Example

To start a transaction block:

BEGIN;

To start a transaction block at a repeatable isolation level:

BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;

compatibility

BEGIN is an YMatrix database language extension. It is equivalent to the SQL standard command START TRANSACTION.

DEFERRABLE transaction_mode is an extension of the YMatrix database language.

Incidentally, the BEGIN keyword is used for different purposes in embedded SQL. It is recommended that you pay attention to transactional semantics when migrating database applications.

See also

COMMIT , ROLLBACK , START TRANSACTION , SAVEPOINT , SAVEPOINT