BEGIN, COMMIT, ROLLBACK, SET

v3.0.0

Group statements into a transaction, and choose its isolation level and durability.

In this article

Statements outside BEGIN ... COMMIT run in autocommit mode: each one is its own transaction. BEGIN starts an explicit transaction that ends with COMMIT or ROLLBACK.

Syntax

BEGIN [TRANSACTION | WORK] [ISOLATION LEVEL {READ COMMITTED | REPEATABLE READ}] [DURABILITY {STRICT | GROUP | RELAXED}]
COMMIT [TRANSACTION | WORK]
ROLLBACK [TRANSACTION | WORK]

SET ISOLATION LEVEL {READ COMMITTED | REPEATABLE READ}
SET DURABILITY {STRICT | GROUP | RELAXED}

START is accepted for BEGIN, and ABORT for ROLLBACK. The ISOLATION LEVEL and DURABILITY clauses can be written in either order.

Examples

Update two rows and commit

BEGIN;
UPDATE tickets SET checked_in = 1 WHERE id = 3;
UPDATE tickets SET checked_in = 1 WHERE id = 4;
COMMIT;

Choose the level and durability for one transaction

BEGIN ISOLATION LEVEL REPEATABLE READ DURABILITY STRICT;
BEGIN trx_id=14 isolation=repeatable read durability=strict

Change the session default

SET DURABILITY RELAXED;
SET durability=relaxed

A transaction commits or rolls back whole

There are no savepoints and no chained transactions. A word after COMMIT or ROLLBACK, other than TRANSACTION or WORK, is refused with its byte position:

kdbs> COMMIT AND CHAIN;
ERR UNSUPPORTED retryable=0 COMMIT takes no 'AND' (byte 7): there are no savepoints or chained transactions, and a transaction commits or rolls back whole (docs/spec/txn.md §9)

Isolation levels

LevelEach statement sees
READ COMMITTED (default)Data committed before the statement started.
REPEATABLE READData committed before the transaction started.

SERIALIZABLE and READ UNCOMMITTED are refused by name.

Durability classes

ClassAlsoCOMMIT returns after
STRICTD1The commit record is synced to disk.
GROUP (default)D2The commit record is synced to disk, with the sync shared by commits that arrive together.
RELAXEDD3The commit is in the log buffer and is synced within relaxed_flush_interval_us (default 10 ms). A crash can lose commits from that window; use it for data you can rebuild.

The class is chosen in three layers, each overriding the one before: the server setting durability, then SET DURABILITY for the session, then BEGIN ... DURABILITY for one transaction. SET DURABILITY applies to the next transaction, never to the open one:

kdbs> BEGIN;
kdbs> SET DURABILITY strict;
ERR cannot change the durability class inside a transaction

Conflicts and errors

  • When two transactions write the same row, the first writer wins. A second writer waits for the first to finish. Under REPEATABLE READ, if the first commits, the second is refused with a retryable error:

    ERR TXN_CONFLICT retryable=1 row id=5 was written by transaction 14
    

    Under READ COMMITTED the waiting statement usually runs once the first transaction ends, against the committed row. It can still be refused the same way if the row changes between the statement’s start and its write.

  • A statement that fails while it runs aborts the transaction, for example an assertion violation. Until you send ROLLBACK, every other statement is refused. A statement refused when it is compiled, such as one naming an unknown column, leaves the transaction open.

    kdbs> BEGIN
    BEGIN
    kdbs> UPDATE tickets SET baggage_kg = 30 WHERE id = 1
    ERR ASSERTION_VIOLATION retryable=0 assertion "baggage_cap" group (passenger_id=1): SUM(baggage_kg) would exceed bound 46
    kdbs> SELECT * FROM tickets WHERE id = 1
    ERR current transaction is aborted; commands are ignored until ROLLBACK
    
  • Closing the connection with a transaction open rolls it back.

  • An explicit transaction older than txn_lifetime_ceiling_ms (default 60 seconds) is rolled back by the server.

Schema changes inside a transaction

StatementRolled back by ROLLBACKHidden from other sessions until COMMIT
CREATE TABLE, CREATE INDEX, DROP INDEXYesYes
DROP TABLEYesNo
ALTER TABLE, Cabins, assertionsNoNo

Schema changes run in a transaction and their catalog writes are logged, so a committed one survives a crash (SHOW META reports ddl_durable=1 catalog_recovered=1). The table shows only which statements ROLLBACK undoes and when other sessions see them.

Limitations

  • No nested transactions. A second BEGIN is an error.
  • No savepoints and no chained transactions. SAVEPOINT, RELEASE, ROLLBACK TO ... and COMMIT AND CHAIN are refused with UNSUPPORTED, and so is COMMIT AND NO CHAIN.
  • A refused ROLLBACK ... form undoes nothing and aborts the transaction. The work it meant to undo can no longer be committed; only a plain ROLLBACK ends the transaction.
  • A refused COMMIT ... form commits nothing and leaves the transaction open.
  • SET ISOLATION LEVEL inside an open transaction is an error.

See also