Bird
Raised Fist0
PostgreSQLquery~10 mins

Read committed behavior in PostgreSQL - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Read committed behavior
Start Transaction T1
T1 reads data
Start Transaction T2
T2 modifies and commits data
T1 reads data again
T1 sees T2's committed changes
T1 commits or rolls back
In Read Committed, each query sees only data committed before it started, so a transaction can see changes committed by others between its queries.
Execution Sample
PostgreSQL
BEGIN;
SELECT balance FROM accounts WHERE id=1;
-- T2 updates balance and commits
SELECT balance FROM accounts WHERE id=1;
COMMIT;
Transaction T1 reads a balance, then after T2 commits an update, T1 reads the updated balance.
Execution Table
StepTransactionActionData ReadData State
1T1BEGIN transactionN/ABalance=100
2T1SELECT balance100Balance=100
3T2BEGIN transactionN/ABalance=100
4T2UPDATE balance=150N/ABalance=150 (uncommitted)
5T2COMMITN/ABalance=150 (committed)
6T1SELECT balance150Balance=150 (committed)
7T1COMMITN/ABalance=150
💡 T1 ends after seeing T2's committed update in its second read.
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 6Final
balance100100150150150
Key Moments - 2 Insights
Why does T1 see the updated balance 150 in step 6 but not in step 2?
Because in Read Committed, each query sees only data committed before it starts. At step 2, T2's update was not committed yet, but by step 6, T2 committed, so T1 sees the new value.
Does T1 see uncommitted changes from T2 during its transaction?
No. T1 only sees committed data at the start of each query, so uncommitted changes by T2 are invisible to T1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the balance read by T1 at step 2?
A150
B100
CNULL
DUncommitted value
💡 Hint
Check the 'Data Read' column at step 2 in the execution_table.
At which step does T1 see the updated balance of 150?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look for the step where T1 reads balance after T2 commits in the execution_table.
If T2 did not commit at step 5, what would T1 read at step 6?
A100
BNULL
C150
DUncommitted 150
💡 Hint
Read Committed only shows committed data; uncommitted changes are invisible (see key_moments).
Concept Snapshot
Read Committed isolation means:
- Each query sees only data committed before it starts.
- Changes committed by other transactions between queries become visible.
- Uncommitted changes are never visible.
- Prevents dirty reads but allows non-repeatable reads.
- Default isolation level in PostgreSQL.
Full Transcript
This visual trace shows how Read Committed isolation works in PostgreSQL. Transaction T1 starts and reads a balance of 100. Meanwhile, T2 starts, updates the balance to 150, and commits. When T1 reads again, it sees the updated balance 150 because the change is now committed. This demonstrates that each query in Read Committed sees only committed data at its start, so T1 does not see uncommitted changes but can see committed changes made by others between its queries.

Practice

(1/5)
1. What does the Read Committed isolation level guarantee in PostgreSQL?
easy
A. It prevents phantom reads by locking the entire table.
B. It allows reading uncommitted (dirty) data from other transactions.
C. It only reads data that has been committed by other transactions.
D. It locks all rows read until the transaction ends.

Solution

  1. Step 1: Understand Read Committed isolation

    Read Committed isolation level ensures that a transaction sees only data committed before the query began, avoiding dirty reads.
  2. Step 2: Compare options with definition

    It only reads data that has been committed by other transactions. matches this definition exactly. The other options describe behaviors of other isolation levels or incorrect behaviors.
  3. Final Answer:

    It only reads data that has been committed by other transactions. -> Option C
  4. Quick Check:

    Read Committed = no dirty reads [OK]
Hint: Read Committed means no dirty reads, only committed data [OK]
Common Mistakes:
  • Confusing Read Committed with Read Uncommitted
  • Thinking it locks rows until transaction ends
  • Assuming it prevents phantom reads
2. Which of the following is the correct way to set the transaction isolation level to Read Committed in PostgreSQL?
easy
A. SET TRANSACTION LEVEL READ COMMITTED;
B. SET ISOLATION LEVEL READ COMMITTED TRANSACTION;
C. BEGIN TRANSACTION ISOLATION READ COMMITTED;
D. SET TRANSACTION ISOLATION LEVEL READ COMMITTED;

Solution

  1. Step 1: Recall correct syntax for setting isolation level

    The correct syntax in PostgreSQL is: SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
  2. Step 2: Check each option

    SET TRANSACTION ISOLATION LEVEL READ COMMITTED; matches the correct syntax exactly. The other options have incorrect word order or missing keywords.
  3. Final Answer:

    SET TRANSACTION ISOLATION LEVEL READ COMMITTED; -> Option D
  4. Quick Check:

    SET TRANSACTION ISOLATION LEVEL READ COMMITTED; [OK]
Hint: Remember: SET TRANSACTION ISOLATION LEVEL ... [OK]
Common Mistakes:
  • Mixing order of keywords
  • Omitting 'ISOLATION' or 'LEVEL'
  • Using BEGIN with isolation level incorrectly
3. Consider two transactions running concurrently under Read Committed isolation. Transaction 1 updates a row but has not committed yet. Transaction 2 tries to read that same row. What will Transaction 2 see?
medium
A. The original data before Transaction 1's update.
B. The updated but uncommitted data from Transaction 1.
C. An error due to concurrent update conflict.
D. No data, the row is locked and invisible.

Solution

  1. Step 1: Understand Read Committed behavior on concurrent reads

    Under Read Committed, a transaction sees only committed data. Uncommitted changes from other transactions are invisible.
  2. Step 2: Apply to scenario

    Transaction 1's update is uncommitted, so Transaction 2 reads the original committed data before the update.
  3. Final Answer:

    The original data before Transaction 1's update. -> Option A
  4. Quick Check:

    Read Committed hides uncommitted changes [OK]
Hint: Uncommitted changes are invisible under Read Committed [OK]
Common Mistakes:
  • Assuming dirty reads are allowed
  • Thinking a read error occurs
  • Believing the row is locked and unreadable
4. You wrote this code in PostgreSQL:
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
SELECT balance FROM accounts WHERE id = 1;
COMMIT;
But you notice the SELECT shows the updated balance even before COMMIT. Why?
medium
A. Because the transaction reads its own uncommitted changes under Read Committed.
B. Because the isolation level is set to Read Uncommitted.
C. Because SELECT statements ignore transaction boundaries.
D. Because the UPDATE was not executed properly.

Solution

  1. Step 1: Understand transaction visibility in Read Committed

    In Read Committed, a transaction sees its own changes immediately, even if not committed yet.
  2. Step 2: Apply to given code

    The SELECT inside the same transaction sees the updated balance from the UPDATE before COMMIT.
  3. Final Answer:

    Because the transaction reads its own uncommitted changes under Read Committed. -> Option A
  4. Quick Check:

    Transaction sees own changes before commit [OK]
Hint: A transaction always sees its own changes immediately [OK]
Common Mistakes:
  • Confusing Read Committed with Read Uncommitted
  • Thinking SELECT ignores transaction boundaries
  • Assuming UPDATE failed without checking
5. You want to avoid phantom reads in a banking app using PostgreSQL. You currently use Read Committed isolation. Which approach best prevents phantom reads while keeping most benefits of Read Committed?
hard
A. Switch to Repeatable Read isolation level for the transaction.
B. Use explicit row-level locks with SELECT FOR UPDATE.
C. Increase the transaction timeout to avoid conflicts.
D. Use Read Committed but commit after every statement.

Solution

  1. Step 1: Understand phantom reads and Read Committed

    Read Committed does not prevent phantom reads (new rows appearing during a transaction).
  2. Step 2: Evaluate options to prevent phantom reads

    Switching to Repeatable Read prevents phantom reads but changes the isolation level and may reduce concurrency. Increasing transaction timeout or committing after every statement does not prevent phantom reads. Using explicit row-level locks with SELECT FOR UPDATE keeps Read Committed while preventing changes to selected rows, mitigating phantom reads.
  3. Final Answer:

    Use explicit row-level locks with SELECT FOR UPDATE. -> Option B
  4. Quick Check:

    Row-level locks prevent phantom reads under Read Committed [OK]
Hint: Use SELECT FOR UPDATE to lock rows and avoid phantoms [OK]
Common Mistakes:
  • Assuming Repeatable Read is always best
  • Ignoring phantom reads in Read Committed
  • Thinking timeout affects phantom reads