NusaDB

Documentation / Transactions

Transactions

Multi-version rows and optimistic concurrency: what a conflict looks like, the retry loop every write path needs, isolation levels, savepoints, locks, schema changes, cursors and two-phase commit.

NusaDB uses MVCC with optimistic concurrency control: readers never block writers, and writers never block readers. When two transactions contend for the same write, the engine aborts one of them instead of making it wait, so applications retry aborted transactions.

What your application will see

SituationNusaDB behaviourSQLSTATE
Two transactions write the same row/keySecond writer aborts immediately (no wait)40001
Deadlock between transactionsBoth abort (no hang)40P01
SERIALIZABLE rw-antidependency cycleOne transaction aborts at COMMIT40001
DDL on a table a transaction has written and not yet endedThe DDL fails at once (no wait); in autocommit the server retries it first40001
A write (INSERT, UPDATE, DELETE, MERGE, COPY ... FROM) to a table another transaction is altering (a column, a constraint, an index, a rename) and has not yet committedThe write fails at once, even when no row matches40001
Reading or writing a table another transaction has dropped and not yet committedFails until the drop commits or rolls back40001
Creating a table with the name of one another transaction created and has not yet committedFails at once until that transaction ends40001
A REPEATABLE READ or SERIALIZABLE snapshot older than a committed column rewrite or index build on a tableThe next read of that table fails40001
A READ COMMITTED statement inside a transaction block that resolved a table just before such a change committed and read it just afterThe statement fails (outside a block it is retried automatically)40001
DDL on a table a prepared transaction wroteThe DDL fails at once until the prepared transaction is finished55006
A deferred constraint fails its check at COMMITThe transaction is rolled back; see deferred constraint checks23505 / 23503 / 23P01

Neither 40001 (serialization_failure) nor 40P01 (deadlock_detected) is an application bug. Both mean the transaction should be run again, so retry on the class (any code starting with 40) instead of on single values. The engine's own retry helper treats both as retryable. Exactly one of the conflicting writers commits, so no update is lost.

The retry loop

Wrap every write transaction in a bounded retry loop that re-runs the whole transaction on a class-40 error. Never retry a single statement in isolation, because the aborted transaction's earlier reads may be stale.

Python:

python
import time, random

def with_retry(conn, work, attempts=5):
    for attempt in range(attempts):
        try:
            cur = conn.cursor()
            cur.execute("BEGIN")
            result = work(cur)
            cur.execute("COMMIT")
            return result
        except Exception as e:
            cur.execute("ROLLBACK")
            # Retry the whole class: `40001` and `40P01` are both "run it again".
            sqlstate = getattr(e, "sqlstate", None) or ""
            if not sqlstate.startswith("40") or attempt == attempts - 1:
                raise
            time.sleep(random.uniform(0, 0.05 * 2**attempt))  # jittered backoff

The same shape applies through any driver or ORM: catch class 40, roll back, back off with jitter, re-run. ORMs with a "retry on serialization failure" option (e.g. SQLAlchemy retrying decorators) should enable it.

Class 25: transaction state errors

A class 25 error means the current transaction cannot accept the statement. Running the transaction again does not apply:

CodeWhat happenedWhat to do
25P02a statement already failed, so the transaction is aborted and every later statement is refusedROLLBACK (or roll back to a SAVEPOINT taken before the failure), then start again
25P01SAVEPOINT / RELEASE / ROLLBACK TO outside a transaction blockopen a transaction first; there is no savepoint scope without one
25001SET TRANSACTION ISOLATION LEVEL after the transaction has already run a query, or COMMIT PREPARED / ROLLBACK PREPARED inside a transaction blockissue it before the transaction's first statement, or outside the block

The server does not treat these as errors:

  • COMMIT and ROLLBACK outside a transaction block succeed as no-ops. A pool that ends a transaction defensively before returning a connection can do so unconditionally.
  • A redundant BEGIN inside an open transaction is a no-op, not 25001. Transactions do not nest; use SAVEPOINT for a nested scope.

Handle 25P02 explicitly. The retry loop above rolls back before deciding whether to retry. A handler that re-runs only the statement on a connection in this state gets 25P02 on every attempt, because only ending the transaction clears it. No class 25 error can be retried in place.

25006 (read-only transaction) is defined but does not occur over the wire, because BEGIN READ ONLY and SET TRANSACTION READ ONLY are refused with 0A000 (see isolation levels).

Isolation levels over the wire

The default is READ COMMITTED. All four standard levels are supported end-to-end, requested any of these ways:

sql
BEGIN ISOLATION LEVEL SERIALIZABLE;                                   -- this transaction
BEGIN; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; ...           -- before its first query
SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE;  -- session default
SET default_transaction_isolation = 'serializable';                   -- session default (GUC)

SET TRANSACTION ISOLATION LEVEL after the transaction has already run a query is refused with 25001: it must be set before the transaction runs any query. BEGIN READ ONLY and SET TRANSACTION READ ONLY are refused with 0A000, because the wire layer does not yet enforce access modes.

Higher isolation levels produce more class 40 aborts under contention, so SERIALIZABLE depends on the retry loop.

Savepoints

A savepoint marks a point a transaction can roll back to without abandoning the whole transaction. Transactions do not nest; a savepoint is the nested scope.

sql
BEGIN;
INSERT INTO audit (event) VALUES ('import started');
SAVEPOINT before_rows;
INSERT INTO rows_in (payload) VALUES ('...');
ROLLBACK TO SAVEPOINT before_rows;   -- the audit row survives
RELEASE SAVEPOINT before_rows;       -- optional: forget the savepoint, keep the work
COMMIT;

ROLLBACK TO SAVEPOINT is also the way out of an aborted transaction (25P02) when the work before the savepoint is worth keeping.

Deferred constraint checks

A DEFERRABLE key, foreign key or exclusion constraint in deferred mode is checked when the transaction commits instead of by each statement (declaring one and SET CONSTRAINTS: SQL reference). COMMIT, END or the end of an auto-committed statement can then fail with 23505, 23503 or 23P01. The transaction is rolled back and none of its changes are kept. PREPARE TRANSACTION runs the same checks first and does not prepare a transaction that fails them.

SET CONSTRAINTS ... IMMEDIATE checks what is pending at that point. A violation fails the transaction block like any failed statement, and ROLLBACK TO SAVEPOINT to a savepoint taken before the writes recovers it. In this example slot_n is UNIQUE DEFERRABLE INITIALLY DEFERRED and the table already holds n = 1:

sql
BEGIN;
SAVEPOINT before_load;
INSERT INTO slot VALUES (1, 'clash');     -- deferred: accepted for now
SET CONSTRAINTS slot_n IMMEDIATE;
-- ERROR 23505: constraint violation: duplicate key violates unique constraint "slot_n" on (n)
ROLLBACK TO SAVEPOINT before_load;        -- undoes the insert, its pending check and the mode
COMMIT;

ROLLBACK TO SAVEPOINT drops the checks deferred after the savepoint and restores the SET CONSTRAINTS mode the savepoint had.

Deferring a check does not defer its locks. A deferred write takes its key locks when it writes and holds them until the transaction ends, so a concurrent writer of the same key fails at once with 40001. A transaction that holds a deferred child row also holds its parent key, and another transaction that deletes that parent fails with 40001 until the first one ends.

Row and table locks

Locks never wait, so SELECT ... FOR UPDATE on a row another transaction has locked fails at once with 40001; NOWAIT is accepted and changes nothing. SKIP LOCKED skips such rows instead, so in a job queue each worker claims a different row without a conflict.

sql
BEGIN;
SELECT * FROM jobs WHERE state = 'queued' ORDER BY id LIMIT 1 FOR UPDATE SKIP LOCKED;
UPDATE jobs SET state = 'running' WHERE id = 42;
COMMIT;

LOCK TABLE name IN ACCESS SHARE MODE and IN ACCESS EXCLUSIVE MODE are available inside a transaction; the other modes are refused.

Schema changes inside a transaction

DDL is transactional, and until it commits only its own transaction sees it. Every other session keeps resolving tables, columns and indexes as they were last committed, whatever its isolation level:

  • a table another transaction created is not there yet (42P01), and creating one with the same name fails with 40001 until that transaction ends;
  • a table another transaction altered keeps its committed columns, so SELECT * returns the committed shape and an INSERT without a column list counts the committed columns;
  • a table another transaction renamed answers to its committed name only;
  • a table another transaction dropped still exists, and reading or writing it fails with 40001 until the drop commits or rolls back;
  • an index another transaction created is not used by the planner;
  • constraints another transaction added or dropped (a CHECK, UNIQUE, primary key or foreign key) keep their committed state: information_schema lists the committed ones, and a foreign key still being added is not enforced against writes elsewhere.

INSERT, UPDATE, DELETE, MERGE and COPY ... FROM take the target table's shared lock before they evaluate anything, so a write to a table another transaction is altering (a column, a constraint, an index, a rename or a drop) fails at once with 40001, even when no row matches. The shared lock is then held until the writing transaction ends, so DDL on that table from another session fails with 40001 meanwhile (see DDL next to open and prepared transactions for what the exclusive lock covers). Dropping a foreign key also takes its referenced table exclusively.

Once the DDL commits, the next statement of every session sees it. Table definitions are not part of a snapshot, so a transaction that began earlier still resolves the new definition. When the change rewrote the table's rows (adding, dropping or retyping a column) or built an index on it, a read of that table under a snapshot that does not see the change fails with 40001 instead of returning rows of the wrong shape or an incomplete index result:

  • a REPEATABLE READ or SERIALIZABLE transaction whose snapshot is older than such a change gets 40001 the next time it reads the table, and should be retried;
  • a READ COMMITTED statement that resolved the table just before such a change committed and read it just after is retried automatically outside a transaction block, and reports 40001 inside one.

A rename, SET NOT NULL, DROP NOT NULL or a constraint change does not rewrite rows and does not cause this. Each 40001 on this page is an ordinary class-40 error, so the retry loop above handles them.

Cursors

A cursor reads a large result in pieces without materialising it on the client. Its rows are captured when the cursor is declared. A cursor declared without WITH HOLD belongs to its transaction: it can only be declared inside BEGIN ... COMMIT (25P01 otherwise) and closes when the transaction commits or rolls back. A WITH HOLD cursor stays open after its transaction commits, until CLOSE or the end of the session, and is closed if that transaction rolls back. See the SQL reference for FETCH and MOVE.

sql
BEGIN;
DECLARE c CURSOR FOR SELECT id, email FROM customers ORDER BY id;
FETCH 100 FROM c;
FETCH NEXT FROM c;
CLOSE c;
COMMIT;

Two-phase commit

PREPARE TRANSACTION is the first phase of a two-phase commit, for an external coordinator (an XA transaction manager, a saga runner) that must make a transaction durable before it decides its outcome. It ends the transaction on the connection without ending the transaction itself: the writes are durable but invisible to everyone, the rows it changed or locked stay locked, and it waits under its global identifier for the second phase, which any connection may run.

sql
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 7;
PREPARE TRANSACTION 'transfer-42';   -- the connection is idle again

-- later, from this or any other connection, after the coordinator decided
COMMIT PREPARED 'transfer-42';       -- or ROLLBACK PREPARED 'transfer-42'

A prepared transaction survives a crash and a restart: after recovery it is still prepared, its writes still invisible and its locks held again, until it is finished. Checkpoints go on while it waits, and the log is truncated as usual; the checkpoint image carries the transaction. A standby applies it from the primary's log and shows it prepared; it is finished on the primary (or on the standby once promoted).

SELECT * FROM nusadb_prepared_xacts lists the database's prepared transactions: transaction, gid, prepared (when it was prepared), owner (the role that prepared it) and database. A superuser sees every row, any other role its own. The server allows 64 to wait at once in each database by default; --max-prepared-transactions changes that, and 0 turns two-phase commit off (see configuration).

SituationSQLSTATE
PREPARE TRANSACTION with no transaction open (an error, so a coordinator cannot record a branch that does not exist)25P01
PREPARE TRANSACTION in a failed transaction: it is rolled back and the tag is ROLLBACKnone
an identifier longer than 200 bytes22023
an identifier already in use42710
the transaction changed the schema, a sequence or statistics, wrote a temporary table, ran LISTEN / UNLISTEN / NOTIFY, or declared a cursor WITH HOLD0A000
--max-prepared-transactions reached53400
prepared transactions disabled (--max-prepared-transactions 0)55000
COMMIT PREPARED / ROLLBACK PREPARED of an unknown identifier42704
COMMIT PREPARED / ROLLBACK PREPARED inside a transaction block (the block is then failed)25001
finishing another role's prepared transaction without being a superuser42501
DROP TABLE, TRUNCATE, ALTER TABLE (adding a CHECK, UNIQUE, primary key or foreign key, or attaching or detaching a partition included), CREATE INDEX, DROP INDEX, LOCK TABLE ... ACCESS EXCLUSIVE or DROP SCHEMA ... CASCADE on a table a prepared transaction wrote; a foreign key added over existing rows also when it wrote the referenced table55006
DROP DATABASE of a database with a prepared transaction waiting in it55006

A refused PREPARE TRANSACTION rolls the transaction back, whatever the reason.

A prepared transaction keeps its locks and holds back the reclamation of old row versions until it is finished. A coordinator should finish every transaction it prepares; an operator can list leftover ones in nusadb_prepared_xacts and roll them back.

DDL next to open and prepared transactions

A statement that drops, reshapes, re-indexes or validates a table takes it exclusively. Next to an ordinary transaction that has written the table but not ended, such a statement fails at once with the usual no-wait 40001. While a prepared transaction waits, the tables it wrote cannot be dropped, truncated, altered or (re)indexed, and its database cannot be dropped: such a statement fails at once with 55006 (object in use) and succeeds after COMMIT PREPARED or ROLLBACK PREPARED.

Once it holds the table, such a statement reads the latest committed rows (plus its own transaction's), not the snapshot its transaction or statement began with, whatever the isolation level. A writer that committed just before the lock was taken is therefore included when a constraint is validated (ADD CHECK, SET NOT NULL, ADD UNIQUE, ADD FOREIGN KEY, ATTACH PARTITION), when rows are rewritten (ADD COLUMN ... DEFAULT, a type change), and when an index is built.

A DDL statement run outside a transaction block is retried like any other autocommit conflict (max_autocommit_retries attempts, 50 by default, with a growing back-off capped at 5 ms, so about a quarter of a second in all) and goes through if the writer ends meanwhile. Past that, or inside a transaction block, the 40001 is reported for you to retry.

Timeouts and cancellation

  • SET statement_timeout = '30s' bounds any single statement on this session; the server flag --statement-timeout sets a default for every session. A cancelled statement reports 57014.
  • A connection cap (--max-connections) queues excess connections by default; --reject-excess-connections refuses them immediately with 53300 so a pool can back off.
  • CHECKPOINT refuses while any transaction is open, including one on the connection issuing it; run it from a connection in autocommit. A prepared transaction waiting for its second phase does not hold checkpoints back: the checkpoint carries it into the new image.