Bird
Raised Fist0
PostgreSQLquery~20 mins

Repeatable read behavior in PostgreSQL - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Repeatable Read Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Repeatable Read: Consistent Snapshot Query

Consider two concurrent transactions in PostgreSQL using REPEATABLE READ isolation level. Transaction A reads a row, then Transaction B updates that row and commits. Transaction A reads the same row again.

What will Transaction A see on the second read?

PostgreSQL
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SELECT balance FROM accounts WHERE id = 1; -- returns 100
-- Concurrently, another transaction updates balance to 200 and commits
SELECT balance FROM accounts WHERE id = 1; -- What is the output here?
A100 (the original value, snapshot is consistent)
B200 (the updated value from the other transaction)
CNULL (the row is locked and not visible)
DError: serialization failure
Attempts:
2 left
💡 Hint

Think about how REPEATABLE READ isolation keeps a consistent snapshot for the duration of the transaction.

🧠 Conceptual
intermediate
2:00remaining
Understanding Phantom Reads in Repeatable Read

Which of the following statements about phantom reads under REPEATABLE READ isolation level in PostgreSQL is true?

APhantom reads can occur because new rows inserted by other transactions are visible.
BPhantom reads cause immediate transaction abort in REPEATABLE READ.
CPhantom reads are prevented by locking all rows in the table at the start.
DPhantom reads cannot occur because the transaction sees a consistent snapshot including all rows at start.
Attempts:
2 left
💡 Hint

Consider how PostgreSQL's MVCC handles visibility of new rows in REPEATABLE READ.

📝 Syntax
advanced
2:00remaining
Setting Repeatable Read Isolation Level Syntax

Which of the following SQL commands correctly sets the transaction isolation level to REPEATABLE READ in PostgreSQL for the current transaction?

ABEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
BSTART TRANSACTION WITH ISOLATION LEVEL REPEATABLE READ;
CSET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
DSET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Attempts:
2 left
💡 Hint

Focus on the command that sets isolation level for the current transaction before it starts.

optimization
advanced
2:00remaining
Avoiding Serialization Failures in Repeatable Read

In PostgreSQL, under REPEATABLE READ isolation, serialization failures can occur due to concurrent updates. Which approach best reduces serialization failures while keeping repeatable read guarantees?

ASwitch to READ COMMITTED isolation level to avoid serialization failures.
BUse explicit row-level locking (SELECT ... FOR UPDATE) to serialize access.
CUse advisory locks to coordinate transactions externally.
DIncrease max_connections to reduce contention.
Attempts:
2 left
💡 Hint

Think about how locking can prevent concurrent conflicting updates.

🔧 Debug
expert
2:00remaining
Diagnosing Serialization Failure in Repeatable Read

Two concurrent transactions under REPEATABLE READ isolation update the same row and commit. One transaction gets a serialization failure error. What is the most likely cause?

AThe row was locked by a long-running SELECT query without FOR UPDATE.
BThe transactions used different isolation levels causing inconsistent visibility.
CBoth transactions tried to update the same row concurrently, causing a conflict detected by PostgreSQL's serialization mechanism.
DThe database ran out of disk space during the commit.
Attempts:
2 left
💡 Hint

Consider how PostgreSQL detects conflicts under REPEATABLE READ.

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

  1. 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.
  2. 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.
  3. Final Answer:

    It ensures all queries in a transaction see the same data snapshot. -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    BEGIN; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    100 -> Option B
  4. 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

  1. Step 1: Identify syntax error cause

    PostgreSQL does not support specifying isolation level directly in BEGIN statement.
  2. Step 2: Correct syntax to set isolation level

    You must first BEGIN; then run SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; before queries.
  3. Final Answer:

    Change to BEGIN; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    REPEATABLE READ -> Option D
  4. Quick Check:

    Stable snapshot + concurrency = REPEATABLE READ [OK]
Hint: Repeatable Read = stable snapshot without blocking writes [OK]
Common Mistakes:
  • Choosing SERIALIZABLE which blocks more
  • Thinking READ COMMITTED prevents data changes
  • Assuming READ UNCOMMITTED exists in PostgreSQL