Quick revision
The short answer
Both transactions read the same two on-call rows, each decides it is safe to leave, and each updates a different row. PostgreSQL's REPEATABLE READ only stops two transactions that write the same row, so both commit and the rule breaks. This is called write skew, and we prevent it with SERIALIZABLE isolation, with SELECT ... FOR UPDATE on the rows we read, or by locking one shared row for the shift.
What to remember
In this answer, we saw that write skew happens when two transactions read the same rows, decide independently, and write different rows. PostgreSQL's REPEATABLE READ only detects two writes to the same row, so it lets both commit. We can prevent it with SERIALIZABLE, by locking the rows we read with FOR UPDATE, or by locking one shared row that stands for the rule, and in every case the application must retry the whole transaction when the database refuses it.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Both transactions read the same two on-call rows, each decides it is safe to leave, and each updates a different row. PostgreSQL's REPEATABLE READ only stops two transactions that write the same row, so both commit and the rule breaks. This is called write skew, and we prevent it with SERIALIZABLE isolation, with SELECT ... FOR UPDATE on the rows we read, or by locking one shared row for the shift.
Why this question is asked
Most concurrency bugs we hear about involve two people changing the same thing. This one is different: the two transactions change different rows, and that is exactly why the database lets it happen. Interviewers ask it to see whether we know what an isolation level really promises, and how to protect a rule that spans more than one row. In this answer, we will recap how snapshot isolation works, reproduce the problem with two sessions, see why PostgreSQL allows it, and then go through three ways to prevent it.
1A Quick Recap of Snapshot Isolation
When transactions run at the same time, the isolation level decides how much each one is affected by the others. In PostgreSQL, REPEATABLE READ gives each transaction a snapshot: a frozen view of the database as it was when the transaction started. Whatever other transactions commit afterwards, ours keeps seeing its own snapshot.
Snapshot isolation does stop one kind of clash: two transactions updating the same row. The second one waits, and as soon as the first commits, its update fails with a serialization error (could not serialize access due to concurrent update). It does not compare what each transaction read with what the other wrote. That gap is where write skew lives.
The rule in our question, at least one doctor on call per shift, is called an invariant: something that must always be true. The database knows nothing about it unless we give it a way to see it.
2Reproducing the Problem With Two Sessions
We can reproduce the problem with two psql sessions side by side. Doctors A and B start on call for shift 7.
8 messages in order. Play them, or step through them one at a time.
Each session decided on a picture that was out of date. Nobody is left on call.
Notice that each session saw two doctors on call, so each decided it was safe to leave. Both commits succeed, and the shift is left with nobody on call.
3Why PostgreSQL Allows It
Each transaction made a correct decision based on its snapshot. The trouble is that each snapshot was out of date by the time the transaction acted on it, because the other transaction was changing the same group of rows.
PostgreSQL only checks for conflicts on the rows each transaction writes. Session A wrote row A and session B wrote row B. There is no overlap, so at REPEATABLE READ there is nothing to stop. The connection between the two changes exists only in our rule about the group, and the database was never told about it.
4Fix 1: Run the Transaction at SERIALIZABLE
At SERIALIZABLE, PostgreSQL also tracks what each transaction read. It notices that A read the rows B changed and B read the rows A changed, which could never happen if the two had run one after the other, and it aborts one of them.
Our application must catch this error and run the whole transaction again. On the retry, that doctor sees only one colleague on call and is refused. SERIALIZABLE is the most general fix, because it protects every rule we did not think to protect by hand, at the cost of more aborted transactions under heavy contention.
5Fix 2: Lock the Rows We Read With FOR UPDATE
Instead of only counting, we select the on-call rows with FOR UPDATE. That locks them, so the second transaction has to wait until the first one finishes, and then it reads the fresh data.
Notice that we select the rows themselves and count them in our code. PostgreSQL rejects SELECT count(*) ... FOR UPDATE with the error FOR UPDATE is not allowed with aggregate functions, because a count is not a row it can lock.
6Fix 3: Lock One Row That Stands for the Rule
We can also lock a single shared row, such as the row for shift 7 in a shifts table, at the start of every 'go off call' request. Requests for the same shift then queue up one at a time.
- Can we afford retries under contention?YesSERIALIZABLE protects every ruleNoLock explicitly instead
- Do the rows that decide the rule already exist?YesSELECT ... FOR UPDATE on those rowsNoLock a parent row or add an exclusion constraint
- Retry the whole transaction when the database refusesfor every fix
3 steps in order. Play them, or step through them one at a time.
This approach also covers a harder case: rules about rows that do not exist yet. If the rule were 'no two bookings may overlap in one room', both transactions would insert new rows, so there would be nothing to lock with FOR UPDATE and both would pass. For those cases we lock a parent row like this, use SERIALIZABLE, or let the database enforce the rule with an exclusion constraint.
7Retrying the Whole Transaction
Whichever fix we choose, a transaction can now fail or wait because of another one, so the application must be ready to run it again from the start. In Spring, serialization failures and lock failures are translated into subclasses of ConcurrencyFailureException, which makes a retry easy to write (check your Spring version for the exact subclass).
8Follow-up questions
Write the exclusion constraint for a room booking table.
With the btree_gist extension enabled: ALTER TABLE booking ADD CONSTRAINT no_overlap EXCLUDE USING gist (room_id WITH =, tstzrange(starts_at, ends_at) WITH &&); The database then refuses any booking whose time range overlaps an existing booking for the same room, even when both arrive at the same moment.
Why does FOR UPDATE work here but not for double booking?
Here the rows that decide the rule already exist, so locking them makes the second transaction wait. In double booking, the conflicting row is the other transaction's new insert, which does not exist yet when we read, so there is nothing to lock.
Does MySQL behave the same way?
Yes, for plain reads. In InnoDB's REPEATABLE READ, an ordinary SELECT reads from a snapshot, so the same write skew can happen. Locking reads (SELECT ... FOR UPDATE) or SERIALIZABLE prevent it there too.
9Conclusion
In this answer, we saw that write skew happens when two transactions read the same rows, decide independently, and write different rows. PostgreSQL's REPEATABLE READ only detects two writes to the same row, so it lets both commit. We can prevent it with SERIALIZABLE, by locking the rows we read with FOR UPDATE, or by locking one shared row that stands for the rule, and in every case the application must retry the whole transaction when the database refuses it.
Open with
This is write skew. Under PostgreSQL's REPEATABLE READ each transaction reads from its own snapshot, so both see two doctors on call, and each updates a different row.
Then, if they want more
Snapshot isolation only detects conflicts when two transactions write the same row, so both commit and the rule across rows breaks.
To prevent it I would either run the operation at SERIALIZABLE, where PostgreSQL tracks read-write dependencies and aborts one transaction with SQLSTATE 40001, or lock the rows I read with SELECT FOR UPDATE so the second transaction waits and then sees the new state.
If the rule depended on rows that may not exist yet, such as overlapping bookings, I would lock a parent row, use SERIALIZABLE, or use an exclusion constraint. In each case the application retries the whole transaction.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A hospital rule says at least one doctor must stay on call per shift. Two doctors who...": https://scrivio-interview-library.netlify.app/databases/acid-and-isolation/72d4893b57/