Skip to content
Databasesbeginner

What is a database transaction?

Updated Nov 30, 20263 min readBased on official documentation
What is a database transaction?

In short

A database transaction is a group of database operations that succeed or fail together as one unit. If any step fails, the database undoes the steps already done, so data never ends up half-updated. Transactions are usually started with BEGIN and finished with COMMIT or ROLLBACK.

A database transaction is a way to treat several changes as one single action. Either every change in the group is saved, or none of them are. This idea is often called all-or-nothing. It is one of the main reasons databases can be trusted with money, orders and other data that must stay correct.

Why transactions matter

Many everyday operations need more than one change. Think of a bank transfer. Money has to leave one account and arrive in another. If the system stops after the first change but before the second, the money simply vanishes or appears from nowhere. A transaction prevents this. It makes sure the whole transfer completes, or the database behaves as if the transfer never started.

The official PostgreSQL documentation uses a bank example like this one. It describes the transfer as several separate updates that must be treated as a single operation.

How it works

A transaction usually goes through three steps:

  • It starts. Your code tells the database that a group of commands begins.
  • It runs its commands. Each change is made, but it is not final yet.
  • It ends. If everything went well, the changes are confirmed with COMMIT. If something went wrong or you changed your mind, they are undone with ROLLBACK.

Databases give three main guarantees for transactions. The first is atomicity, which means the group is all-or-nothing. The second is durability, which means that once a commit is confirmed, the data is written to permanent storage, so a crash afterwards will not erase it. The third is isolation, which means other users of the database cannot see half-finished changes. They see the old data until the transaction commits, and then they see all the new data at once.

In PostgreSQL, you wrap your commands between BEGIN and COMMIT. Here is a simplified transfer:

BEGIN; UPDATE accounts SET balance = balance - 100.00 WHERE name = 'Alice'; UPDATE accounts SET balance = balance + 100.00 WHERE name = 'Bob'; COMMIT;

If you notice a problem before COMMIT, for example that Alice's balance would go negative, you can run ROLLBACK instead. All the updates made so far are then canceled.

Autocommit: the default behavior

PostgreSQL treats every single SQL statement as a transaction on its own. If you do not write BEGIN, each statement is wrapped automatically and committed when it succeeds. This is handy for simple queries, but it means that a multi-step change is not protected unless you group it yourself. Some client libraries also send BEGIN and COMMIT for you, so check the documentation of the tool you use.

Errors and savepoints

If an error happens inside a transaction block, PostgreSQL does not close the transaction. Instead, the transaction enters an aborted state. In this state, every command is rejected except COMMIT and ROLLBACK. Both of those end the failed transaction, so you can start fresh.

Savepoints give you finer control. A savepoint is a named marker inside a transaction. You can create one with SAVEPOINT and later undo only the work done after it with ROLLBACK TO. Changes made before the savepoint are kept. This is useful when only one part of a larger group needs to be discarded.

Common mistakes

A common mistake is forgetting that a group of statements is not a transaction until you wrap it. Without BEGIN, each statement commits on its own. Another mistake is leaving a transaction open for a long time. Other users may be blocked or may keep seeing old data while your transaction waits. Finally, do not assume a failed statement has changed nothing. Check whether the transaction was rolled back, and always end it with COMMIT or ROLLBACK.

Where to go next

If you are new to SQL, learn the basic UPDATE, INSERT and DELETE commands first, since transactions group exactly these kinds of statements. Then read the official PostgreSQL chapter on transactions to see the full list of options, including the AND CHAIN form of COMMIT and ROLLBACK.

FAQ

Do I always need to write BEGIN in PostgreSQL?

No. Without BEGIN, PostgreSQL wraps each single statement in its own transaction and commits it automatically if it succeeds. Use BEGIN when several statements must succeed or fail together.

What is the difference between COMMIT and ROLLBACK?

COMMIT saves all the changes made in the transaction and makes them visible to other sessions. ROLLBACK cancels all those changes, as if they never happened.

What is a savepoint used for?

A savepoint lets you undo part of a transaction without throwing away the whole thing. Changes made before the savepoint are kept, while changes made after it can be rolled back.

Can other users see my changes before I commit?

No. Changes made inside an open transaction are invisible to other sessions until the transaction commits. Then all of them become visible together.

Sources

This page is based on the official documentation below. Read it for full detail.

Database transaction — official documentation ↗

Database transaction in the news

blozhikDeveloper news from across the web, explained. Every story links to its original sources.