Transactions & COPY
SQL Reference: Transactions
Section titled “SQL Reference: Transactions”Transaction control, savepoints, isolation semantics, and autocommit behavior.
Transaction Control
Section titled “Transaction Control”BEGIN;-- ... your SQL statements ...COMMIT; -- persist all changes atomicallyROLLBACK; -- discard all changesAutocommit
Section titled “Autocommit”Single statements outside an explicit BEGIN block run in implicit autocommit mode. The system begins a transaction, executes the statement, and commits (or rolls back on error). Retryable TiKV errors trigger automatic retry with backoff.
Savepoints
Section titled “Savepoints”BEGIN;INSERT INTO users (name) VALUES ('Alice');
SAVEPOINT sp1;INSERT INTO users (name) VALUES ('Bob');ROLLBACK TO SAVEPOINT sp1; -- Bob's insert is undone
RELEASE SAVEPOINT sp1; -- destroys savepoint, merges stateCOMMIT; -- only Alice is committedSavepoint behavior matches PostgreSQL: duplicate names are allowed (most recent is targeted), RELEASE destroys the named savepoint and all later ones, ROLLBACK TO re-establishes the savepoint for reuse.
Isolation Level
Section titled “Isolation Level”Transactions are pessimistic, and the default isolation level is READ COMMITTED.
| Level | Behavior |
|---|---|
READ COMMITTED (default) | Statement-level snapshots, as in PostgreSQL — each statement sees rows committed before it started |
READ UNCOMMITTED | Behaves as READ COMMITTED, as in PostgreSQL |
REPEATABLE READ | One snapshot held for the transaction’s lifetime |
SERIALIZABLE | Not implemented — downgraded to REPEATABLE READ |
Under READ COMMITTED, a statement later in the transaction sees another session’s committed writes:
-- Session A -- Session BBEGIN; -- default: READ COMMITTEDSELECT count(*) FROM t; -- 1 INSERT INTO t VALUES (2); -- commitsSELECT count(*) FROM t; -- 2COMMIT;Under REPEATABLE READ the same sequence returns 1 both times.
Read-Only Transactions
Section titled “Read-Only Transactions”BEGIN READ ONLY, SET TRANSACTION READ ONLY, and SET default_transaction_read_only = on all
enable read-only transaction enforcement. DML and DDL writes are rejected with SQLSTATE 25006:
BEGIN READ ONLY;INSERT INTO t VALUES (1);-- ERROR: cannot execute INSERT in a read-only transaction-- SQLSTATE: 25006ROLLBACK;For credential-level write restrictions that apply across sessions, issue a token with a read-only
scope (db9 token create --scope mydb:ro) — see CLI reference.
Sequence Behavior
Section titled “Sequence Behavior”Failed Transaction State
Section titled “Failed Transaction State”When an error occurs in a transaction, it enters a failed state. Only ROLLBACK, COMMIT, and END are accepted. All other statements return an error. This matches PostgreSQL behavior.
SQL Reference: COPY
Section titled “SQL Reference: COPY”Bulk data import and export.
-- Import from file (via client)COPY table_name FROM STDIN;
-- Import from Parquet (with parquet extension)COPY table_name FROM '/data/file.parquet' FORMAT parquet;Text, CSV, and Parquet formats are supported for COPY FROM.