What Is a Database Transaction? How to Make Related Changes Succeed or Fail Together
A database transaction groups related SQL operations into one logical unit. The database either commits the complete group or, after a failure, discards its changes so that only part of the operation is not left behind. In PostgreSQL, use `BEGIN` to start an explicit transaction, `COMMIT` to make it final, or `ROLLBACK` to cancel it. A transaction defines the boundary for a group of changes; what concurrent transactions can see is also affected by separate settings such as the isolation level.
Why group several SQL statements?
A single business action can require multiple data changes. A bank transfer, for example, must subtract money from the sender and add it to the recipient. If only the first change is saved and the server fails before the second, the database can be left in a state that does not represent a completed transfer. Put both changes in a transaction: the database commits them together on success, or rolls back the transaction’s changes if the operation is canceled after an error.
Other sessions do not see the transaction’s uncommitted intermediate changes; after commit, they become visible together. This prevents another user from observing only one side of the transfer. Still, the mere presence of a transaction does not automatically protect every business rule under concurrent access. The isolation level, locks, and constraints affect what concurrent operations can observe and do.
What do begin, commit, and rollback do?
In PostgreSQL, BEGIN starts a transaction block. Subsequent SQL statements belong to it until COMMIT or ROLLBACK. COMMIT makes the changes visible to other transactions and records them so that a completed transaction remains durable after a crash. ROLLBACK cancels the changes made in the current transaction.
BEGIN;
UPDATE accounts
SET balance = balance - 100.00
WHERE account_id = 101 AND balance >= 100.00;
-- The application checks that the first UPDATE changed exactly one row.
-- If the check fails, it runs ROLLBACK instead of continuing.
UPDATE accounts
SET balance = balance + 100.00
WHERE account_id = 202;
COMMIT;This is an illustrative example that assumes an accounts table and particular business rules; it was not run or verified. A real implementation must check that the debit changed one row and that the recipient is valid. If a condition is not met or a statement fails, the application should roll back, record the error, and decide whether a safe retry is appropriate. It should report the transfer as complete only after a successful commit. In PostgreSQL, a transaction can enter an error-aborted state; the session may need a rollback or savepoint recovery before it can continue.
What happens if you do not start one explicitly?
PostgreSQL runs every SQL statement within a transaction. Without an explicit BEGIN, each statement normally forms its own transaction: a successful statement is committed implicitly, and a failed one is rolled back. This is called autocommit behavior. Running several statements one after another therefore does not, by itself, make them one all-or-nothing operation. If an early statement has committed and a later one fails, the earlier change remains.
Some database clients and drivers send BEGIN and COMMIT automatically or offer a transaction API. In that case, check the documentation for the interface you use instead of inferring the transaction boundary from the SQL visible in a screen or log. In applications using connection pools, keep the work in the transaction on the same connection and return a connection to a usable state after an exception.
How can you undo only part of the work?
A savepoint marks a point inside a transaction. If something goes wrong after it, ROLLBACK TO SAVEPOINT cancels changes made since that point while preserving earlier work, so the transaction can continue. This is useful when one optional subtask may fail but the rest of the operation can still be committed under the business rules.
For example, an application might save the basic order and then attempt optional discount processing. If discount processing fails, it could roll back only the discount changes and continue without the discount, but only if that fallback is valid for the business. A savepoint is not an independently committed nested transaction: none of the outer transaction’s changes become final until the outer COMMIT. The application’s rules determine which partial recovery is acceptable.
Can a database transaction also undo an external notification?
No. A database transaction controls changes inside that database. If the application calls a payment service or sends an email before commit, SQL ROLLBACK cannot cancel that external action. If it sends the email after commit, the application might stop in between: the data is saved, but the notification is missing. This mismatch is one form of the dual-write problem.
When a system must coordinate a database update with an event notification, it can consider the transactional outbox pattern. The application writes both the business data and an outbox row describing the event in the same database transaction. A separate process reads committed outbox rows and publishes the messages. Because a relay can send a message more than once, consumers should be designed to handle duplicates safely. This is an option for message-driven systems, not a requirement for every application.
Are a transaction and an isolation level the same thing?
A transaction defines when a group of work starts and ends, and which changes are committed or canceled together. The isolation level determines what that transaction can see while other transactions run at the same time. In PostgreSQL, the default READ COMMITTED level lets each statement see rows committed before that statement began. REPEATABLE READ and SERIALIZABLE apply stronger snapshot rules, and serializable work can fail with a serialization error that the application must handle. The separate isolation-level topic covers how to choose among them.
Practical checks for an application
Identify the reads and writes needed for one business action, and decide which failures require canceling the whole operation.
Check whether the database driver uses autocommit or starts transactions automatically.
Run the related changes on the same transaction connection, then verify success conditions such as affected-row counts and constraints.
On failure, promptly run `ROLLBACK` or recover to an appropriate savepoint. Commit only after all success conditions hold; report success after the commit succeeds.
Avoid leaving transactions open longer than necessary. In PostgreSQL, an old open transaction can delay cleanup of row versions that are no longer needed by other work. Design the flow so it does not wait for user input or a network response while holding a database transaction open.
Test failure paths, duplicate requests, and concurrent requests in a nonproduction environment. The SQL in this article was not run.
Scope and sources
[PostgreSQL 18 tutorial: Transactions](https://www.postgresql.org/docs/18/tutorial-transactions.html)
[PostgreSQL 18: BEGIN](https://www.postgresql.org/docs/18/sql-begin.html)
[PostgreSQL 18: COMMIT](https://www.postgresql.org/docs/18/sql-commit.html)
[PostgreSQL 18: ROLLBACK](https://www.postgresql.org/docs/18/sql-rollback.html)
[PostgreSQL 18: SET TRANSACTION and isolation levels](https://www.postgresql.org/docs/18/sql-set-transaction.html)
[PostgreSQL 18: Routine vacuuming and old row versions](https://www.postgresql.org/docs/18/routine-vacuuming.html)
The PostgreSQL 18 documentation was checked on 2026-11-22. Autocommit, command behavior, and the default isolation level described here are PostgreSQL-specific; other products and drivers may behave differently. The SQL is illustrative and was not run. The account-transfer example does not include a real schema, constraints, or a complete error-handling implementation.
Transactional outbox source: AWS Prescriptive Guidance