0
0
PostgreSQLquery~10 mins

Row-level locking (FOR UPDATE, FOR SHARE) in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Row-level locking (FOR UPDATE, FOR SHARE)
Start Transaction
Execute SELECT ... FOR UPDATE/SHARE
Lock rows matching condition
Other transactions wait if they try to lock same rows
Commit or Rollback transaction
Release locks
Begin a transaction, select rows with locking clause, lock those rows, block other conflicting locks, then commit or rollback to release locks.
Execution Sample
PostgreSQL
BEGIN;
SELECT * FROM accounts WHERE id = 1 FOR UPDATE;
-- do some updates
COMMIT;
Starts a transaction, locks the row with id=1 for update, then commits to release the lock.
Execution Table
StepActionQuery/ConditionLock AcquiredOther Transactions BehaviorResult
1Start transactionBEGIN;No lock yetNo blockingTransaction started
2Select row with FOR UPDATESELECT * FROM accounts WHERE id=1 FOR UPDATE;Row id=1 locked for updateOther txns wait if they try FOR UPDATE on id=1Row locked, data returned
3Another txn tries FOR UPDATE on same rowSELECT * FROM accounts WHERE id=1 FOR UPDATE;BlockedWaits until first txn commits or rollbacksBlocked until lock released
4Commit first transactionCOMMIT;Locks releasedBlocked txns proceedLocks released, changes saved
5Blocked txn proceedsSELECT * FROM accounts WHERE id=1 FOR UPDATE;Row id=1 locked for updateNo blocking nowRow locked, data returned
6End-No locks heldNo blockingAll locks released, transactions complete
💡 Locks are released after COMMIT or ROLLBACK, allowing other transactions to proceed.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
locked_rowsnonerow id=1 lockednone (locks released)row id=1 lockednone (locks released)
transaction_statenoneactivecommittedactivecommitted
Key Moments - 3 Insights
Why does the second transaction wait at step 3?
Because the first transaction holds a FOR UPDATE lock on the same row, PostgreSQL blocks the second transaction to prevent conflicting changes until the first transaction commits or rolls back (see execution_table step 3).
What happens to locks after COMMIT at step 4?
All row-level locks held by the transaction are released, allowing other waiting transactions to proceed (see execution_table step 4).
What is the difference between FOR UPDATE and FOR SHARE?
FOR UPDATE locks rows for exclusive updates blocking others from modifying them, while FOR SHARE allows concurrent reads but blocks updates. This example uses FOR UPDATE to show exclusive locking.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What lock is acquired on the row with id=1?
ANo lock acquired
BRow locked for shared read
CRow locked for update (exclusive lock)
DTable-level lock acquired
💡 Hint
Check the 'Lock Acquired' column at step 2 in execution_table.
At which step does the first transaction release its locks?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for 'Locks released' in the 'Lock Acquired' column in execution_table.
If the first transaction did not commit or rollback, what would happen to the second transaction at step 3?
AIt would proceed immediately
BIt would be blocked indefinitely
CIt would get an error
DIt would lock a different row
💡 Hint
See 'Other Transactions Behavior' at step 3 in execution_table.
Concept Snapshot
Row-level locking in PostgreSQL:
- Use FOR UPDATE to lock selected rows exclusively.
- Use FOR SHARE to lock rows for shared access.
- Locks block other transactions trying conflicting locks.
- Locks are held until COMMIT or ROLLBACK.
- Prevents race conditions in concurrent updates.
Full Transcript
Row-level locking in PostgreSQL works by starting a transaction, then selecting rows with FOR UPDATE or FOR SHARE to lock them. The locked rows prevent other transactions from modifying them until the first transaction commits or rolls back. This ensures data consistency when multiple users access the same rows. The example shows a transaction locking a row with FOR UPDATE, blocking another transaction trying to lock the same row until the first transaction finishes. Locks are released after commit or rollback, allowing waiting transactions to proceed.