Transactions
A transaction groups changes into one unit of commit or rollback. Statements outside an explicit transaction autocommit.
Controls
| Statement | Effect |
|---|---|
BEGIN |
Start a transaction. |
COMMIT |
Keep its changes. |
ROLLBACK |
Undo its changes. |
SAVEPOINT name |
Create an inner undo point. |
ROLLBACK TO name |
Undo later changes and retain that savepoint. |
RELEASE name |
Remove the savepoint. |
Example
Undoing one adjustment
sql
CREATE TABLE balances(id INTEGER PRIMARY KEY, amount INTEGER);
BEGIN;
INSERT INTO balances VALUES (1, 10);
SAVEPOINT adjustment;
UPDATE balances SET amount = 20;
ROLLBACK TO adjustment;
RELEASE adjustment;
COMMIT;sql
SELECT id, amount
FROM balances
FORMAT csv;text
1,10The insert remains, while the adjustment after the savepoint is undone.
Connection scope
A native connection retains its open transaction. Closing it rolls back uncommitted changes.
Every HTTP request has a separate session. Put BEGIN, the related statements and COMMIT in one request; an unclosed transaction is rolled back before the response is sent.
Several connections in one process can share a file. Simultaneous access from separate processes is outside the supported concurrency model. See deployment modes.