Transactions
Use with tables.transaction(): to run multiple Tables operations as one unit: either all their transactional database changes are committed, or none are. This is useful when updating related rows must not leave your data partially changed.
Using the existing Tables functions
transaction() takes no arguments. Inside the block, use the same Tables functions, parameters, and return values as outside it: insert, select, select_one, select_by_id, select_df, update, update_by_id, delete, delete_by_id, and run_sql.
Each call runs immediately in the same database transaction. You can use its result in the next call or make a decision in Python. The context manager does not return a transaction object; continue calling tables directly.
For example, create a version and make it the active version for its base. Before running this example, create a versions table with base_id (text) and active (boolean) columns, in addition to its default id column.
The insert returns the new row immediately, including its id. Both changes are committed when the block exits normally. Outside a transaction block, Tables functions keep their usual behavior.
A returned row does not mean it has been committed yet. Do not report success or trigger actions that depend on the changes until the block has exited successfully.
Commit and rollback
- A normal exit commits all the block's transactional Tables changes.
- An exception escaping the block rolls those changes back and propagates the original exception.
- A failed Tables call, including an invalid argument or a SQL constraint violation, prevents the block from committing even if you catch its exception inside the block. Further Tables operations cannot continue that transaction; exiting it raises
TablesTransactionErrorwithstate == "aborted".
For example, an application validation error undoes an insert:
Catch errors outside the block when you want to handle a failed transaction. Catching a Python exception inside the block does not itself force rollback if all Tables calls succeeded.
The transaction covers Tables database operations only. It does not undo Python variables, sent emails, connector calls, file changes, or other external effects. PostgreSQL effects that are not transactional, such as advancing a sequence, are not undone either.
Handling transaction errors
Import TablesTransactionError from abstra.tables. Its state attribute describes the transaction outcome when a transaction request fails:
| State | Meaning |
|---|---|
unknown | The commit could not be confirmed. The changes may already have been committed. |
committed | The block committed, but a later Tables call using its old context did not run. |
aborted, rolled_back, unavailable, active | The affected transaction's changes were not committed. active can occur when a nested transaction is rejected; it does not end the outer transaction by itself. |
Ordinary Python exceptions and errors from individual Tables calls can also propagate; not every error is a TablesTransactionError.
If the connection fails while confirming the commit, rollback cannot be guaranteed. The SDK does not automatically replay transaction operations or commit requests. Verify the outcome using application-specific identifiers before deciding whether another attempt is safe.
Limits and supported SQL
- Keep transactions short. The backend rolls back transactions left idle for about 60 seconds or open for 5 minutes. Avoid user input, long-running computation, and network calls inside the block.
- Nested transaction blocks and savepoints are not supported.
- Use the transaction in the thread that opened it. Calls from threads that copy its context, such as
asyncio.to_thread, are rejected. A separate thread does not automatically join the transaction. Do not run concurrent operations or let tasks created inside the block use its context after the block has ended. - Inside a transaction,
run_sqlaccepts one statement per call:SELECT,INSERT,UPDATE,DELETE, orMERGE. Schema changes, explicit transaction control (BEGIN,COMMIT,ROLLBACK,SAVEPOINT),set_config, and system catalog access are not allowed. Let the context manager control commit and rollback. - Transactions require a backend that supports them. An older backend produces a clear incompatibility error (
TablesTransactionErrorwithstate == "unavailable"); the SDK never falls back to separately committed calls.
Atomicity is not idempotency and does not by itself prevent conflicting business decisions from concurrent transactions. Use appropriate database constraints and application-specific concurrency rules; this API does not provide a separate lock-by-key or automatic retry feature.