Support "nested" transactions in interactive transactions #15212
Open
Description
Problem
Handle nested transaction blocks in an interactive transaction (or in transactions in general). This allows for writing transactions in a more composable manner without having to think about it all the time, but at the risk of allowing too much nesting.
Suggested solution
I'm not sure what the best solution would be, but here are some examples of how this is implemented in other libraries as well as tools in other languages.
- Ecto. Another article about Ecto (Ecto, Elixir's ORM collapses transactions into a single transaction, I think they use SAVEPOINTS).
- Ecto's Multi is a supercharged version of Prisma's interactive transactions API, allowing you to cleanly write database interactions almost like a workflow, and these can be nested too. If you're familiar with monads from functional programming, Multi's are like a database transaction monad.
- pg-promise transactions
- Sequelize transactions / https://sequelize.org/docs/v6/other-topics/transactions/
Alternatives
The alternative is to not nest transactions, but I'm not sure if that's the best alternative.
Additional context
Initially suggested here