Row-level locking helps prevent conflicts when multiple people try to change the same data at the same time. It locks specific rows so others can't change them until you're done.
Row-level locking (FOR UPDATE, FOR SHARE) in PostgreSQL
Start learning this pattern below
Jump into concepts and practice - no test required
SELECT columns FROM table WHERE condition FOR UPDATE; SELECT columns FROM table WHERE condition FOR SHARE;
FOR UPDATE locks the selected rows for writing (updates, deletes).
FOR SHARE locks the selected rows for reading, allowing others to read but not update them.
SELECT * FROM accounts WHERE id = 1 FOR UPDATE;
SELECT * FROM orders WHERE status = 'pending' FOR SHARE;
BEGIN; SELECT * FROM products WHERE id = 10 FOR UPDATE; -- do some updates COMMIT;
This example locks the employee row with id 5 so no other transaction can change it while we update the salary. After the update, the lock is released with COMMIT.
BEGIN; SELECT * FROM employees WHERE id = 5 FOR UPDATE; -- Imagine we update the employee's salary here UPDATE employees SET salary = salary + 1000 WHERE id = 5; COMMIT;
Row-level locks only last during the transaction. Always use BEGIN and COMMIT to control locking.
FOR UPDATE blocks other transactions trying to update or delete the locked rows.
FOR SHARE allows others to read but prevents updates or deletes until the lock is released.
Row-level locking controls access to specific rows during transactions.
Use FOR UPDATE to lock rows for writing.
Use FOR SHARE to lock rows for reading.
Practice
FOR UPDATE clause do in a PostgreSQL query?Solution
Step 1: Understand the purpose of
TheFOR UPDATEFOR UPDATEclause locks the rows returned by the query to prevent other transactions from modifying them.Step 2: Compare with other locking types
UnlikeFOR SHARE, which allows reading but prevents writing,FOR UPDATEblocks other transactions from modifying the locked rows until the current transaction ends.Final Answer:
Locks selected rows to prevent other transactions from modifying them until the current transaction ends. -> Option AQuick Check:
FOR UPDATE= exclusive row lock [OK]
- Confusing FOR UPDATE with table-level locks
- Thinking FOR UPDATE allows other writes
- Mixing FOR UPDATE with FOR SHARE behavior
Solution
Step 1: Identify the clause for read locks
In PostgreSQL,FOR SHAREis used to lock rows for reading, allowing other transactions to read but not modify.Step 2: Check syntax correctness
Options B and C are invalid syntax. SELECT * FROM orders FOR UPDATE; locks rows for writing, not reading.Final Answer:
SELECT * FROM orders FOR SHARE; -> Option BQuick Check:
Read lock = FOR SHARE [OK]
- Using FOR UPDATE instead of FOR SHARE for read locks
- Using non-existent LOCK ROW or WITH LOCK syntax
- Confusing table-level locks with row-level locks
-- Transaction 1
BEGIN;
SELECT * FROM products WHERE id = 10 FOR UPDATE;
-- Transaction 2
BEGIN;
SELECT * FROM products WHERE id = 10 FOR SHARE;What will happen when Transaction 2 tries to execute its SELECT statement?
Solution
Step 1: Understand locking conflict between FOR UPDATE and FOR SHARE
AFOR UPDATElock is exclusive and blocks other transactions from acquiring conflicting locks, includingFOR SHARE.Step 2: Analyze Transaction 2 behavior
Transaction 2'sFOR SHARElock request conflicts with Transaction 1'sFOR UPDATElock, so it must wait until Transaction 1 finishes.Final Answer:
Transaction 2 will wait until Transaction 1 commits or rolls back before proceeding. -> Option CQuick Check:
FOR UPDATE blocks FOR SHARE until commit [OK]
- Assuming FOR SHARE can proceed during FOR UPDATE lock
- Expecting deadlock without waiting
- Ignoring lock conflicts between FOR UPDATE and FOR SHARE
SELECT * FROM customers FOR SHARE;But you want to prevent other transactions from modifying these rows. What is wrong and how to fix it?
Solution
Step 1: Identify the locking behavior of FOR SHARE
FOR SHARElocks rows to allow reading but does not prevent other transactions from modifying them.Step 2: Choose correct clause to prevent modifications
To block other transactions from modifying rows, useFOR UPDATEinstead ofFOR SHARE.Final Answer:
FOR SHARE locks rows only for reading; replace it with FOR UPDATE to lock for writing. -> Option DQuick Check:
Prevent writes = use FOR UPDATE [OK]
- Thinking FOR SHARE blocks writes
- Using LOCK TABLE unnecessarily
- Missing WHERE clause but unrelated to locking type
Solution
Step 1: Understand the locking levels
FOR UPDATElocks rows exclusively, blocking writes by others until commit, but does not block reads.Step 2: Compare with other lock types
FOR SHAREandFOR KEY SHAREallow reads;FOR NO KEY UPDATEis less restrictive and allows some concurrent updates.Step 3: Choose the strictest lock to block writes
OnlyFOR UPDATEfully blocks other transactions from modifying the rows, but it does not block reads.Final Answer:
SELECT * FROM table_name FOR UPDATE; -> Option AQuick Check:
Block writes = FOR UPDATE [OK]
- Choosing FOR SHARE or KEY SHARE which allow writes
- Using NO KEY UPDATE which is less strict
- Not understanding lock levels and their effects
