Savepoints, Failed Statements and Transactional DDL
This page covers partial rollback inside a transaction: savepoints at the transaction layer, the engine's handling of a failed statement, and the undo log that makes DDL roll back with the transaction. The transaction model itself is on MVCC.
Savepoints in the transaction layer
A savepoint records positions in the transaction's logs, not a copy of its state:
SavepointState field |
Meaning |
|---|---|
name |
Savepoint name (map key, case-sensitive at this layer) |
write_set_len |
Length of write_set at creation |
read_set_len |
Length of read_set at creation |
undo_len |
Length of undo_log at creation |
lock_log_len |
Length of lock_log at creation |
creation_order |
Monotonic counter, used to find later savepoints |
(source: src/transaction/mvcc.rs, SavepointState, Transaction::create_savepoint)
While at least one savepoint exists, every buffered write appends (key, previous buffer entry) to undo_log, and every lock acquisition or release appends (key, acquired) to lock_log. When the last savepoint goes away, trim_savepoint_logs drops both logs, so transactions without savepoints pay nothing. (source: src/transaction/mvcc.rs, Transaction::record_write, record_lock_acquired, record_lock_released, trim_savepoint_logs)
Not wired: record_lock_acquired and record_lock_released have no callers outside src/transaction/mvcc.rs. The SQL engine takes row and key locks through LockManager directly, so lock_log stays empty and ROLLBACK TO SAVEPOINT releases no locks; locks taken after the savepoint are held until the transaction ends. Only test_savepoint_with_locks, which calls the recording methods by hand, exercises the lock log. (source: src/transaction/mvcc.rs, Transaction::record_lock_acquired; src/query/engine.rs, acquire_row_lock)
rollback_to_savepoint(name):
- Pops
undo_logback toundo_len, restoring each key's previous write-buffer entry, or removing the key if it was not buffered before. - Truncates
write_setandread_setto the saved lengths. - Pops
lock_logback tolock_log_lenand returns the set of lock keys acquired after the savepoint, for the caller to release. - Removes every savepoint created after the target. The target itself stays, as in PostgreSQL.
A missing name is Error::Transaction("Savepoint '...' does not exist"). (source: src/transaction/mvcc.rs, Transaction::rollback_to_savepoint)
release_savepoint(name) removes only that savepoint, keeps all changes, and errors if the name is missing. It does not remove savepoints created after it; the engine handles that in its own bookkeeping (below). (source: src/transaction/mvcc.rs, Transaction::release_savepoint)
Creating a savepoint with an existing name replaces it. (source: src/transaction/mvcc.rs, Transaction::create_savepoint)
Tests in src/transaction/mvcc.rs: test_savepoint_basic, test_savepoint_rollback, test_savepoint_restores_overwritten_values, test_savepoint_release, test_savepoint_not_found, test_savepoint_nested, test_savepoint_replace, test_savepoint_with_locks, test_savepoint_names, test_partial_rollback_write_set. The design note is docs/status/2026-11-24-fast-savepoints.md.
Savepoints in the SQL engine
| Statement | Engine behaviour |
|---|---|
SAVEPOINT s |
Starts a transaction if none is open (MySQL compatibility). Creates the transaction savepoint, then pushes (lowercased name, number of tables created so far, storage undo mark) on the session savepoint stack. |
RELEASE SAVEPOINT s |
Outside a transaction: succeeds silently. Inside: calls release_savepoint, ignoring a missing name, then truncates the session stack at the most recent entry of that name, dropping it and every later one. |
ROLLBACK TO SAVEPOINT s |
Outside a transaction: succeeds silently. Inside: calls rollback_to_savepoint and releases the returned lock keys (always none in practice, see above); drops tables created after the savepoint; restores DDL done since the savepoint's undo mark; keeps the savepoint. |
(source: src/query/engine.rs, create_savepoint, release_savepoint, rollback_to_savepoint)
ROLLBACK TO a name that does not exist returns Error::Transaction. The PostgreSQL wire maps that variant to the default XX000, not to PostgreSQL's 3B001. Error::SavepointNotFound, which maps to 3B001, is never constructed. (source: src/transaction/mvcc.rs, Transaction::rollback_to_savepoint; src/protocol/postgres/mod.rs, PostgresHandler::error_to_sqlstate; src/error.rs, SavepointNotFound) Not verified: no wire-level test of this case was found.
A failed statement inside a transaction
In PostgreSQL a failed statement leaves the transaction aborted until ROLLBACK or ROLLBACK TO SAVEPOINT. SynergyDB rolls the transaction back at once when the statement fails, so its row locks are released immediately and other sessions are not kept waiting. The session stays "inside" the aborted block until the client sends ROLLBACK or COMMIT. (source: src/query/engine.rs, abort_failed_transaction_now)
Exceptions:
- If the transaction has any savepoint, nothing is rolled back; the client is expected to
ROLLBACK TOit. - SQL Server sessions are skipped: with
XACT_ABORT OFFthe transaction survives a failed statement, and the TDS wire manages that itself.
(source: src/query/engine.rs, abort_failed_transaction_now)
Tested by failed_statement_releases_the_transactions_row_locks and failed_statement_then_commit_rolls_back_ddl in src/query/engine.rs tests.
Transactional DDL
DDL writes catalog keys and rewrites rows directly to storage, outside the transaction's write buffer. To make it roll back, the storage view keeps a prior-value log.
The DdlUndoLog
Each NamespacedStorage (the per-database storage view) owns a DdlUndoLog behind a std::sync::Mutex, shared by clones of the view:
| Field | Meaning |
|---|---|
recording |
When true, every put or delete through the view first saves the key's prior value |
entries |
(unprefixed key, prior value) in write order; None means the key did not exist |
seen |
Keys already saved since the latest mark, so only the first prior value per savepoint level is kept |
| Method | Behaviour |
|---|---|
undo_set_recording(on) |
Turns recording on or off; returns the previous setting |
undo_mark() |
Returns the current length and clears seen (starts a new savepoint level) |
undo_take_since(mark) |
Removes and returns entries after mark
|
undo_clear() |
Forgets the log (at BEGIN and COMMIT) |
record_priors(rows) |
Saves prior values the caller already has, such as rows scanned before a range delete |
(source: src/storage/namespaced.rs, DdlUndoLog, NamespacedStorage::undo_set_recording, undo_mark, undo_take_since, undo_clear, record_priors)
When recording is on
is_transactional_ddl(query) returns true when the first keyword (after leading comments) is CREATE, ALTER, DROP, COMMENT or TRUNCATE. Inside an explicit transaction, such a statement runs with recording on. Autocommit DDL is not recorded. (source: src/query/engine.rs, is_transactional_ddl)
Undo
undo_storage_ddl_since(mark) (mark 0 means the whole transaction):
- Takes the entries after
markand turns recording off while it restores. - Keeps the oldest saved prior value for each key, which is its state at the mark.
- Writes the restored values back with
write_batchin chunks of 4096 keys, at a fresh timestamp. A failed chunk is logged with a warning and the restore continues. - Marks the in-memory secondary-index and vector-index registries of every touched table dirty, and invalidates the metadata cache.
(source: src/query/engine.rs, undo_storage_ddl_since)
ROLLBACK, ROLLBACK TO SAVEPOINT and a COMMIT that fails validation all call it. Function, domain and type catalog entries written through ddl_catalog_write are restored by apply_ddl_undo. Tables created in the transaction are dropped. (source: src/query/engine.rs, rollback_transaction, commit_transaction, apply_ddl_undo, rollback_to_savepoint)
Writes to keys starting __catalog__: or __sequence__: advance the storage engine's catalog epoch, so cached plans and metadata see the restored catalog. (source: src/storage/namespaced.rs, bumps_catalog_epoch)
Tests in src/query/engine.rs: rollback_undoes_create_table, rollback_to_savepoint_undoes_later_create_table, rollback_undoes_add_column_not_null_default, rollback_undoes_column_alterations, rollback_undoes_drop_and_rename_table, rollback_undoes_index_view_sequence_and_constraint, rollback_to_savepoint_undoes_only_later_ddl, failed_statement_then_commit_rolls_back_ddl, committed_ddl_stays_and_autocommit_ddl_unaffected.
Limits
These limits come from the operator guide, which cites ROADMAP.md item 1.64, and match the code:
- The restore writes prior bytes directly to storage, so a concurrent session's commits to rows that a rolled-back DDL rewrote are overwritten.
- RLS policies and roles are stored by the RBAC catalog repository, outside the namespaced view's undo log, and are not undone.
-
nextvalandsetvalare not transactional: sequence counters advance through the counter path, not the undo log. See Sequences.
(source: src/storage/namespaced.rs, DdlUndoLog; src/catalog/repository.rs, CatalogRepository; How it works)
Because DDL goes to storage rather than to the write buffer, other sessions can read it before the transaction commits. Not verified: no test was found for what a concurrent session sees of uncommitted DDL.