Repository navigation
Description
Describe the bug
ROLLBACK TO SAVEPOINT sp undoes the work since sp but leaves sp established ("The savepoint remains valid and can be rolled back to again later", PostgreSQL docs; SQLite behaves the same). Only RELEASE SAVEPOINT sp ends that nesting level. _PostgresSavepoint.rollback() correctly sends just ROLLBACK TO SAVEPOINT $id; (serverpod_database/lib/src/adapters/postgres/database_connection.dart:1219-1221; SQLite adapters/sqlite/database_connection.dart:1928-1930), and calling release() after rollback() is valid. But:
- Serverpod's own helpers roll back and never release:
DatabaseUtil.runInTransactionOrSavepoint(serverpod_database/lib/src/util/database_util.dart:58-63) calls onlysavepoint?.rollback()on error. It is used by the ORM's row-wise insert/upsert fallback (database_connection.dart:163,:233) and throughoutserverpod_auth_core_server(AuthUsers,UserProfiles,AccountMerger).serverpod_test'sTransactionManager.rollbackToPreviousSavepoint(lib/src/transaction_manager.dart:99-102) pops the savepoint from its own stack and only rolls back; it runs for every failed database call and every failedsession.db.transactionin a test with database rollbacks (test_database_proxy.dart:337,:563).
- Neither the dartdoc of
Savepoint.rollback()(lib/src/concepts/transaction.dart:14-15) nor the "Transactions" docs page mention that the savepoint is still established afterwards.
Each caught error inside an outer transaction therefore leaves one more nesting level behind. All later work runs one level deeper, and the first write below assigns a subtransaction id to the empty level as well (PostgreSQL assigns xids to all parents without one). Beyond 64 live subtransaction ids per backend the PGPROC subxid cache overflows (PGPROC_MAX_CACHED_SUBXIDS), and every snapshot of every backend has to consult pg_subtrans while that transaction runs. The nesting depth also grows with each caught error until the transaction ends.
To reproduce
await session.db.transaction((tx) async {
for (var i = 0; i < 70; i++) {
try {
await DatabaseUtil.runInTransactionOrSavepoint(session.db, tx, (tx) async {
await Thing.db.insertRow(session, Thing(name: 'x'), transaction: tx);
throw StateError('row failed');
});
} on StateError {
// isolate the failed row, carry on
}
}
// One write outside any helper: it now runs 70 levels deep and every
// level has an xid. On PostgreSQL 16+:
// SELECT * FROM pg_stat_get_backend_subxact(pg_backend_pid());
// shows subxact_count >= 64 / subxact_overflowed = true.
await Thing.db.insertRow(session, Thing(name: 'y'), transaction: tx);
});The leftover level is also visible directly, on any PostgreSQL version: after var sp = await tx.createSavepoint(); ...; await sp.rollback();, a session.db.unsafeExecute('ROLLBACK TO SAVEPOINT ${sp.id}', transaction: tx) still succeeds; after sp.release() it fails with SQLSTATE 3B001.
Expected behavior
After rolling back to a savepoint whose block is finished, the helpers also release it, so the savepoint stack in the database matches the caller's view:
} catch (_) {
try {
await savepoint?.rollback();
await savepoint?.release();
} catch (e) {
throw RollbackToSavepointFailedException(e);
}
rethrow;
}and the same in TransactionManager.rollbackToPreviousSavepoint. The dartdoc of rollback() and the docs page say that the savepoint stays established after a rollback and should be released when the block is done (or offer a rollbackAndRelease() convenience).
Library version
serverpod_database 4.1.0(lib/src/util/database_util.dart,lib/src/concepts/transaction.dart),serverpod_test 4.1.0(lib/src/transaction_manager.dart), callers inserverpod_auth_core_server 4.1.0.- Unchanged on
main(file history checked on 2026-11-13).
Platform information
- Not platform specific; PostgreSQL semantics (SQLite keeps the savepoint as well, without the subxid cost).
- Our setup: Dart SDK 3.13.7 (stable), PostgreSQL 16.
Additional context
We hit this in our own savepoint wrapper, which isolates failed rows of an import inside one transaction and caps the number of savepoints to protect the subxid cache of other backends; rolled-back blocks escaped the cap. We now release after every rollback. The extra RELEASE costs one statement on the error path only. Related: #3051 (closed; savepoint docs), #2950 (typed savepoints).
How experienced are you with this library?
Expert - Experienced and comfortable with using this library in complex projects
Are you interested in working on a PR for this?
- I want to work on this
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsInbox 📫
Activity