mdcms/neuraldb-docs/pages/nql-transactions.md

1.6 KiB

title sort section-id keywords description language
Transactions 140 query-language transactions, ACID, isolation levels, MVCC, BEGIN, COMMIT, ROLLBACK ACID transactions in NeuralDB — isolation levels, MVCC, savepoints, and advisory locks en

Transactions

NeuralDB provides full ACID transactions with MVCC. Unlike most vector databases, NeuralDB guarantees atomicity across both relational and vector data.

Basic Transaction Syntax

BEGIN;
INSERT INTO documents (content, embedding) VALUES ($1, $2);
UPDATE document_stats SET total_count = total_count + 1;
COMMIT;

Isolation Levels

BEGIN;
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
-- each statement sees only rows committed before it
COMMIT;

BEGIN ISOLATION LEVEL REPEATABLE READ;
-- reads are stable throughout the transaction
COMMIT;

BEGIN ISOLATION LEVEL SERIALIZABLE;
-- may raise: ERROR: could not serialize access
COMMIT;

Savepoints

BEGIN;
INSERT INTO documents (content, embedding) VALUES ($1, $2);
SAVEPOINT after_insert;
UPDATE document_stats SET count = count + 1 WHERE id = $3;
ROLLBACK TO SAVEPOINT after_insert;
UPDATE document_stats SET count = count + 1 WHERE id = $4;
COMMIT;

Vector Transactions

BEGIN;
INSERT INTO documents (id, content, embedding) VALUES ($1, $2, $3);
-- If ROLLBACK, neither row nor index entry exists
ROLLBACK;

Advisory Locks

SELECT pg_advisory_lock(42);
SELECT pg_try_advisory_lock(42);  -- returns boolean
SELECT pg_advisory_unlock(42);
SELECT pg_advisory_xact_lock(42); -- auto-released at commit/rollback