Skip to content
Databasesintermediate

How do ACID properties work?

Updated Nov 30, 20263 min readBased on official documentation
How do ACID properties work?

In short

ACID is a set of four promises a database makes about transactions: Atomicity, Consistency, Isolation and Durability. Together they keep data correct even when work fails or runs at the same time as other work. PostgreSQL uses transaction isolation levels to control how much concurrent transactions can see of each other.

What the four letters mean

A transaction is a group of database operations that should succeed or fail as one unit. ACID describes the four guarantees a reliable database gives to such groups.

  • Atomicity means all operations in a transaction happen, or none of them do.
  • Consistency means each transaction moves the data from one valid state to another, respecting the rules you defined, such as constraints.
  • Isolation means concurrent transactions should not interfere with each other in unwanted ways.
  • Durability means that once a transaction is committed, its changes survive crashes.

This article focuses on isolation, because it is the part that you control most directly with settings.

Why isolation is hard

Databases run many transactions at once. If two transactions touch the same rows, they can see each other's half-finished work. The SQL standard names several problems that can happen in this case. A dirty read happens when a transaction reads data written by another transaction that has not committed yet. A nonrepeatable read happens when re-reading a row returns a changed value because another transaction committed in between. A phantom read happens when a repeated query returns a different set of rows. A serialization anomaly happens when the final result cannot match any order in which the transactions could have run one by one.

The standard defines four isolation levels, from weakest to strictest: Read uncommitted, Read committed, Repeatable read and Serializable. Each level forbids some of these problems. Stricter levels forbid more, but they usually cost more in waiting and retries.

How PostgreSQL applies the levels

PostgreSQL lets you request any of the four standard levels. Internally it implements only three distinct behaviors. Its Read uncommitted mode behaves like Read committed, because that is the sensible way to map the standard levels onto its multiversion design. Its Repeatable read level also blocks phantom reads, which the standard allows.

Read committed is the default. In this mode, a plain SELECT sees only data committed before the query started. It sees a snapshot of the database taken at that moment. Two SELECT commands in the same transaction can still return different results if another transaction commits between them.

You set the level for a transaction with the SET TRANSACTION command. Check the official documentation for the exact syntax and the full behavior of each level.

A simple example

Consider a bank transfer of 100 dollars between two accounts. Both updates should happen together, so you wrap them in one transaction:

BEGIN;
UPDATE accounts SET balance = balance + 100.00 WHERE acctnum = 12345;
UPDATE accounts SET balance = balance - 100.00 WHERE acctnum = 7534;
COMMIT;

If the second update fails, a rollback undoes the first one as well. That is atomicity in action. Read committed is usually enough here, because each statement changes a row it has already identified.

More complex statements can behave in surprising ways at this level. The documentation gives an example where a DELETE with a WHERE condition matches no rows, because another transaction changed the values between the time the statement started and the time it locked the rows. Queries with complicated conditions on changing data are the cases where stricter levels help.

Common mistakes

A common mistake is assuming that a transaction sees one frozen view of the database from start to finish. At Read committed, each statement gets its own snapshot. If your logic reads a value and then makes a decision based on it, another transaction may change that value in between.

Another mistake is forgetting that some objects do not follow transaction rules. In PostgreSQL, changes to a sequence are visible to other transactions at once and are not rolled back if the transaction aborts. This matters when you use serial columns, because a rolled-back insert can leave a gap in the numbers.

Finally, avoid raising the isolation level everywhere by default. Use stricter levels for the transactions that need them, and keep simple transactions at the default. If you are new to transactions, you may also want to read our page on SQL basics before going further.

Not every system needs full ACID guarantees. Some key-value stores and caches trade these guarantees for speed or simplicity. For relational data, though, ACID transactions are the standard tool, and most relational databases offer the same four properties with different isolation options.

When you design a system, first decide which operations must succeed together. Then choose the isolation level that prevents the problems your application can actually face. The official PostgreSQL documentation on transaction isolation describes each level in detail and is the best place to verify the behavior you rely on.

FAQ

Is ACID the same as a transaction?

No. A transaction is a group of operations. ACID is the set of guarantees that a reliable database gives to those transactions.

Which isolation level does PostgreSQL use by default?

PostgreSQL uses Read committed by default. In this mode, each statement sees data committed before it started, but not changes that other transactions commit later.

Does a rollback undo changes to sequences?

No. In PostgreSQL, sequence changes are visible to other transactions immediately and are not rolled back if the transaction aborts.

Should I always use the strictest isolation level?

Not usually. Stricter levels can cause more waiting and retries. Use them for transactions that need them and keep the default for simpler work.

Sources

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

ACID — official documentation ↗

ACID in the news

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