Skip to content
Ameen Abozaid
Planned lessonMID-015-L06

Demo: lost update in two terminals

  • Lesson 6 of 7
  • Mid-level
On This Lesson 3 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

reproduce a lost update and fix it.

Draft outline details

Step by step

  1. Create stock(product_id, qty) with one row, qty 10.
  2. In two psql terminals, both BEGIN; SELECT qty (10), then both UPDATE stock SET qty = 9 and commit; final qty is 9, not 8.
  3. Fix 1: atomic UPDATE stock SET qty = qty - 1 WHERE qty > 0 (final 8).
  4. Planned mistake: assume REPEATABLE READ always lets both commit; show T2 fails with a serialization error ("could not serialize access due to concurrent update"). Explain this is correct and needs a retry.
  5. Fix 2: SELECT ... FOR UPDATE so the second waits.

Prepares MID-015-LAB1.

Check your understanding

Which fix needs no extra round trip?

Show answer

The atomic UPDATE expression.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.