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
Recall & Review
beginner
What does Repeatable Read isolation level guarantee in PostgreSQL?
It guarantees that all reads within a transaction see a consistent snapshot of the database as of the start of the transaction, preventing non-repeatable reads but not phantom reads.
Click to reveal answer
beginner
What is a non-repeatable read?
A non-repeatable read happens when a transaction reads the same row twice and gets different data because another transaction modified it in between.
Click to reveal answer
intermediate
Does Repeatable Read prevent phantom reads in PostgreSQL?
No, PostgreSQL's Repeatable Read prevents non-repeatable reads but does not prevent phantom reads; phantom reads are prevented only at the Serializable isolation level.
Click to reveal answer
intermediate
How does PostgreSQL implement Repeatable Read isolation internally?
PostgreSQL uses Multi-Version Concurrency Control (MVCC) to provide a consistent snapshot of the database at the start of the transaction for Repeatable Read isolation.
Click to reveal answer
intermediate
What happens if two transactions try to update the same row under Repeatable Read isolation?
One transaction will succeed, and the other will wait or fail with a serialization error to maintain consistency.
Click to reveal answer
Which isolation level in PostgreSQL guarantees a consistent snapshot for all reads within a transaction?
ARead Uncommitted
BRead Committed
CRepeatable Read
DSerializable
✗ Incorrect
Repeatable Read ensures all reads see the same snapshot taken at the start of the transaction.
Can phantom reads occur under Repeatable Read isolation in PostgreSQL?
AOnly with Read Uncommitted
BYes
CNo
DOnly with Serializable
✗ Incorrect
Phantom reads can occur under Repeatable Read in PostgreSQL; they are prevented only at the Serializable isolation level.
What concurrency control method does PostgreSQL use to implement Repeatable Read?
AMulti-Version Concurrency Control (MVCC)
BLock-based concurrency control
CTimestamp ordering
DTwo-phase locking
✗ Incorrect
PostgreSQL uses MVCC to provide consistent snapshots for transactions.
If a transaction reads a row twice under Repeatable Read, what will it see?
AThe same data both times
BDifferent data if another transaction updated it
CAn error
DOnly the first read is allowed
✗ Incorrect
Repeatable Read guarantees the same data is seen on repeated reads within the same transaction.
What happens if two transactions update the same row simultaneously under Repeatable Read?
ABoth updates succeed without conflict
BThe second update overwrites the first silently
CThe database crashes
DOne transaction waits or fails with a serialization error
✗ Incorrect
To maintain consistency, PostgreSQL will block or abort one transaction to avoid conflicts.
Explain how Repeatable Read isolation level works in PostgreSQL and what read phenomena it prevents or allows.
Think about what data a transaction sees when it reads the same row multiple times.
You got /4 concepts.
Describe what happens when two transactions try to update the same row under Repeatable Read isolation in PostgreSQL.
Consider how PostgreSQL handles conflicts to keep data correct.
You got /4 concepts.
Practice
(1/5)
1.
What does the REPEATABLE READ isolation level guarantee in PostgreSQL?
easy
A. It ensures all queries in a transaction see the same data snapshot.
B. It allows reading uncommitted changes from other transactions.
C. It locks all rows in the database for the transaction duration.
D. It automatically commits after each query in the transaction.
Solution
Step 1: Understand Repeatable Read isolation
Repeatable Read ensures that all queries in a transaction see the same snapshot of data, preventing changes made by others from appearing during the transaction.
Step 2: Compare options with definition
It ensures all queries in a transaction see the same data snapshot. matches this definition exactly. Options B, C, and D describe behaviors not related to Repeatable Read.
Final Answer:
It ensures all queries in a transaction see the same data snapshot. -> Option A
Quick Check:
Repeatable Read = Same snapshot [OK]
Hint: Repeatable Read = stable snapshot during transaction [OK]
Common Mistakes:
Confusing Repeatable Read with Read Uncommitted
Thinking it locks all rows
Assuming auto-commit after each query
2.
Which of the following is the correct way to start a transaction with REPEATABLE READ isolation level in PostgreSQL?
BEGIN;
-- your queries
COMMIT;
easy
A. SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; BEGIN;
B. BEGIN ISOLATION LEVEL REPEATABLE READ;
C. BEGIN; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
D. BEGIN; SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Solution
Step 1: Recall correct syntax for setting isolation level
In PostgreSQL, you start the transaction with BEGIN, then set the isolation level for that transaction using SET TRANSACTION ISOLATION LEVEL.
Step 2: Match options to syntax
BEGIN; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; correctly shows BEGIN; then SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;. SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; BEGIN; sets isolation before BEGIN which is invalid. BEGIN ISOLATION LEVEL REPEATABLE READ; uses invalid syntax. BEGIN; SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ; sets session level, not transaction level.
Final Answer:
BEGIN; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; -> Option C
Quick Check:
Set isolation after BEGIN = BEGIN; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; [OK]
Hint: Set isolation level after BEGIN with SET TRANSACTION [OK]
Common Mistakes:
Trying to set isolation before BEGIN
Using BEGIN with isolation level directly
Confusing session and transaction level commands
3.
Consider this sequence in PostgreSQL with REPEATABLE READ isolation:
-- Transaction 1
BEGIN;
SELECT balance FROM accounts WHERE id = 1; -- returns 100
-- Transaction 2
BEGIN;
UPDATE accounts SET balance = 200 WHERE id = 1;
COMMIT;
-- Back to Transaction 1
SELECT balance FROM accounts WHERE id = 1;
COMMIT;
What will be the result of the second SELECT in Transaction 1?
medium
A. Error due to concurrent update
B. 100
C. NULL
D. 200
Solution
Step 1: Understand snapshot behavior in Repeatable Read
Transaction 1 sees a consistent snapshot from its start. Changes committed by Transaction 2 after Transaction 1 began are not visible.
Step 2: Apply to the SELECT query
The first SELECT returned 100. The second SELECT in the same transaction will also return 100, ignoring the update committed by Transaction 2.
Final Answer:
100 -> Option B
Quick Check:
Repeatable Read = same snapshot = 100 [OK]
Hint: Repeatable Read ignores later commits in same transaction [OK]
Common Mistakes:
Expecting updated value 200 inside same transaction
Thinking it causes error on concurrent update
Assuming NULL if data changed
4.
Given this transaction in PostgreSQL:
BEGIN ISOLATION LEVEL REPEATABLE READ;
UPDATE products SET stock = stock - 1 WHERE id = 10;
SELECT stock FROM products WHERE id = 10;
COMMIT;
But you get an error: ERROR: syntax error at or near "ISOLATION". What is the fix?
medium
A. Change to BEGIN; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
B. Remove the isolation level, just use BEGIN;
C. Use START TRANSACTION ISOLATION LEVEL REPEATABLE READ;
D. Use SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ; before BEGIN
Solution
Step 1: Identify syntax error cause
PostgreSQL does not support specifying isolation level directly in BEGIN statement.
Step 2: Correct syntax to set isolation level
You must first BEGIN; then run SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; before queries.
Final Answer:
Change to BEGIN; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; -> Option A
Quick Check:
Set isolation after BEGIN, not inside [OK]
Hint: Set isolation after BEGIN with SET TRANSACTION [OK]
Common Mistakes:
Trying to put isolation level inside BEGIN
Using unsupported START TRANSACTION syntax
Setting session level instead of transaction level
5.
You want to run multiple SELECT queries in a transaction and ensure the data does not change during the transaction, but you also want to allow other transactions to update data concurrently without blocking. Which isolation level should you choose in PostgreSQL?
hard
A. READ COMMITTED
B. READ UNCOMMITTED
C. SERIALIZABLE
D. REPEATABLE READ
Solution
Step 1: Understand isolation levels and concurrency
READ COMMITTED allows seeing changes committed during the transaction, so data can change between queries. SERIALIZABLE is strict and may block or abort concurrent updates. READ UNCOMMITTED is not supported in PostgreSQL.
Step 2: Match requirement to isolation level
REPEATABLE READ provides a stable snapshot for all queries in the transaction, preventing data changes from appearing, while allowing concurrent updates without blocking reads.