asdsdf
jgfkjhg
never main
May 20, 20262 min read7 views
Read Committed
READ COMMITTEDIt is the default isolation level in PostgreSQL.
Simple Meaning
You cannot see uncommitted changes from another transaction.
But you can see new committed changes if another transaction commits before your next query.
Example
Transaction A:
sqlBEGIN; UPDATE accounts SET balance = 500 WHERE id = 1; -- not committed yet
Transaction B:
sqlSELECT balance FROM accounts WHERE id = 1;
Transaction B will not see yet.
500Because Transaction A has not committed.
After Commit
Transaction A:
sqlCOMMIT;
Transaction B runs again:
sqlSELECT balance FROM accounts WHERE id = 1;
Now Transaction B can see the updated value.
Important Behavior
In , each query sees the latest committed data when that query starts.
READ COMMITTEDSo inside one transaction:
sqlBEGIN; SELECT balance FROM accounts WHERE id = 1; -- another transaction commits update here SELECT balance FROM accounts WHERE id = 1; COMMIT;
The two queries can return different values.
SELECTWhat It Prevents
| Problem | Prevented? |
|---|---|
| Dirty read | Yes |
| Non-repeatable read | No |
| Phantom read | No |
Dirty Read Prevention
Dirty read is prevented because uncommitted data is hidden.
textREAD COMMITTED = only read saved data
Non-Repeatable Read Can Happen
Same row can show different values inside the same transaction if another transaction commits an update between your reads.
Phantom Read Can Happen
Same query can return different rows inside the same transaction if another transaction inserts or deletes matching rows and commits.
Easy Memory Trick
READ COMMITTEDtextI only read committed data. But each query gets a fresh committed view.
When To Use
Use for most normal app queries.
READ COMMITTEDGood for:
- reading user data
- normal CRUD operations
- simple updates
- most API requests
When Not Enough
Use stronger isolation when you need the same transaction to see stable data.
Example:
- financial calculations
- complex reports
- stock/booking logic
- multiple reads that must stay consistent
Then consider:
sqlREPEATABLE READ
or
sqlSERIALIZABLE
Found this useful? Have thoughts or questions?
Reach out →