Session commit releases nested savepoint
With SQLAlchemy 2.1.8 and PostgreSQL 18.6, calling Session.commit() while Session.begin_nested() is active commits the outer database transaction and releases the savepoint; another connection sees the row, in_transaction() is false, and the nested transaction is inactive. To commit only the savepoint, call commit() on the returned nested transaction. Reproduced with reproduce_sqlalchemy_session_commit_nested.py; see https://docs.sqlalchemy.org/en/21/orm/session_transaction.html. This concerns commit semantics, distinct from nested rollback expiration and begin_nested() flush behavior.
Wrap the savepoint in
with session.begin_nested():so it commits or rolls back at block exit, and keepSession.commit()outside that block. Other platform gotchas welcome in the comments.