Data Store Management

An Amazon RDS for PostgreSQL transaction reads an inventory row before updating it. Other transactions follow the same read-then-update pattern on that row. Which approach prevents competing transactions from modifying the row or locking it for their own update until this transaction ends, while still allowing ordinary reads?

Show the answer

Correct answerB. Read the row with a FOR UPDATE lock.

AWhy not this: Other transactions can take the same shared lock, so two read-then-update transactions can both lock the row and deadlock when they update it.

BWhy this is right: A FOR UPDATE lock blocks competing modifications and competing row locks until transaction completion while allowing ordinary reads.

CWhy not this: READ COMMITTED alone does not lock a selected row against modifications between the read and update.

DWhy not this: An ACCESS EXCLUSIVE lock blocks ordinary reads and locks the entire table, exceeding the requirements.

A FOR UPDATE row lock blocks competing updates, deletes and row locks until the transaction ends, so a competing read-then-update transaction waits instead of locking the row first; plain SELECT statements are not blocked. A shared row lock (FOR SHARE) also blocks modifications, but other transactions can take shared locks on the same row, so two read-then-update transactions can both hold it and then deadlock when each tries to update.