0
0
PostgreSQLquery~10 mins

Repeatable read behavior in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Repeatable read behavior
Start Transaction
Read Data (Snapshot)
Perform Operations
Read Data Again
Data Same as First Read?
NoWait or Error
Yes
Commit or Rollback
In repeatable read, a transaction reads a consistent snapshot of data. All reads see the same data, preventing changes from other transactions during its lifetime.
Execution Sample
PostgreSQL
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SELECT balance FROM accounts WHERE id = 1;
-- Another transaction updates balance
SELECT balance FROM accounts WHERE id = 1;
COMMIT;
This transaction reads the balance twice under repeatable read isolation, ensuring both reads see the same value even if another transaction updates it.
Execution Table
StepActionData ReadOther Transaction UpdateResult
1Begin transaction with repeatable readN/AN/ATransaction snapshot taken
2First SELECT balancebalance = 1000No update yetReads initial balance 1000
3Other transaction updates balance to 1200N/ABalance updated to 1200Update not visible to this transaction
4Second SELECT balancebalance = 1000Update happenedReads same balance 1000 as before
5Commit transactionN/AN/ATransaction ends, changes visible to others
💡 Transaction ends after commit; repeatable read ensures consistent snapshot despite concurrent updates
Variable Tracker
VariableStartAfter Step 2After Step 4Final
balance_snapshotN/A100010001000
other_tx_balanceN/AN/A12001200
Key Moments - 2 Insights
Why does the second SELECT still show the old balance even after the other transaction updated it?
Because repeatable read uses a snapshot taken at transaction start (see execution_table step 1 and 4), so it does not see changes made by other transactions after that snapshot.
What happens if the transaction tries to update the same row changed by another transaction?
The transaction will wait or get a serialization error because repeatable read prevents concurrent conflicting updates to maintain consistency.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what value does the balance have at step 4?
Anull
B1200
C1000
DError
💡 Hint
Check the 'Data Read' column at step 4 in the execution_table
At which step does the other transaction update the balance?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Other Transaction Update' column in the execution_table
If the isolation level was READ COMMITTED instead of REPEATABLE READ, what would the second SELECT likely return?
A1000
B1200
Cnull
DError
💡 Hint
In READ COMMITTED, each SELECT sees the latest committed data, unlike repeatable read snapshot
Concept Snapshot
Repeatable Read Isolation Level in PostgreSQL:
- Transaction sees a consistent snapshot of data at start
- All reads return the same data even if others update
- Prevents non-repeatable reads and phantom reads
- Conflicting writes cause waits or errors
- Use: when consistent repeated reads needed within transaction
Full Transcript
Repeatable read isolation means a transaction works with a fixed snapshot of the database taken at its start. This snapshot ensures that all SELECT queries inside the transaction see the same data, even if other transactions change the data meanwhile. For example, if you read a balance twice, both reads show the same value, preventing surprises from concurrent updates. If another transaction updates the data, your transaction does not see those changes until it ends. This isolation prevents inconsistent reads but may cause waits or errors if you try to update the same data concurrently. It is useful when you want stable, repeatable reads inside a transaction.