Skip to content

A rolled-back savepoint stays established: runInTransactionOrSavepoint and serverpod_test never release it, and the docs do not say it must be released #5999

Open

Description

@t-kietzmann

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:

  1. Serverpod's own helpers roll back and never release:
    • DatabaseUtil.runInTransactionOrSavepoint (serverpod_database/lib/src/util/database_util.dart:58-63) calls only savepoint?.rollback() on error. It is used by the ORM's row-wise insert/upsert fallback (database_connection.dart:163, :233) and throughout serverpod_auth_core_server (AuthUsers, UserProfiles, AccountMerger).
    • serverpod_test's TransactionManager.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 failed session.db.transaction in a test with database rollbacks (test_database_proxy.dart:337, :563).
  2. 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 in serverpod_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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions