0
0
PostgreSQLquery~20 mins

Row-level locking (FOR UPDATE, FOR SHARE) in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Row Locking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What rows are locked by FOR UPDATE?

Consider a table accounts with columns id and balance. Which rows will be locked by this query?

SELECT * FROM accounts WHERE balance > 1000 FOR UPDATE;
PostgreSQL
CREATE TABLE accounts (id SERIAL PRIMARY KEY, balance INT);
INSERT INTO accounts (balance) VALUES (500), (1500), (2000);
AOnly rows with balance > 1000 are locked for update.
BAll rows in the table are locked for update.
CNo rows are locked; FOR UPDATE does not lock rows.
DOnly rows with balance <= 1000 are locked for update.
Attempts:
2 left
💡 Hint

FOR UPDATE locks rows returned by the query to prevent concurrent updates.

query_result
intermediate
2:00remaining
What is the effect of FOR SHARE?

Given the table orders with columns order_id and status, what does this query do?

SELECT * FROM orders WHERE status = 'pending' FOR SHARE;
PostgreSQL
CREATE TABLE orders (order_id SERIAL PRIMARY KEY, status TEXT);
INSERT INTO orders (status) VALUES ('pending'), ('shipped'), ('pending');
ALocks rows with status 'pending' to prevent other transactions from updating or deleting them.
BLocks rows with status 'pending' to prevent other transactions from reading them.
CLocks all rows in the table regardless of status.
DDoes not lock any rows; FOR SHARE is ignored.
Attempts:
2 left
💡 Hint

FOR SHARE locks rows to prevent updates or deletes but allows reads.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this locking query

Which option contains a syntax error when trying to lock rows for update?

SELECT * FROM products WHERE price > 100 FOR UPDATE NOWAIT;
ASELECT * FROM products WHERE price > 100 FOR UPDATE NOWAIT;
BSELECT * FROM products WHERE price > 100 FOR UPDATE OF products NOWAIT;
CSELECT * FROM products WHERE price > 100 FOR SHARE NOWAIT;
DSELECT * FROM products WHERE price > 100 FOR UPDATE WAIT;
Attempts:
2 left
💡 Hint

Check the valid locking options and keywords in PostgreSQL.

optimization
advanced
2:00remaining
Which query minimizes locking duration?

You want to lock only necessary rows for update to reduce lock contention. Which query is best?

PostgreSQL
CREATE TABLE inventory (item_id SERIAL PRIMARY KEY, quantity INT);
INSERT INTO inventory (quantity) VALUES (10), (0), (5);
ASELECT * FROM inventory FOR SHARE;
BSELECT * FROM inventory WHERE quantity > 0 FOR UPDATE;
CSELECT * FROM inventory WHERE quantity <= 0 FOR SHARE;
DSELECT * FROM inventory FOR UPDATE;
Attempts:
2 left
💡 Hint

Lock only rows you intend to update.

🧠 Conceptual
expert
3:00remaining
What happens if two transactions lock the same row with FOR UPDATE?

Transaction A and Transaction B both execute SELECT * FROM employees WHERE id = 1 FOR UPDATE; at the same time. What happens?

PostgreSQL
CREATE TABLE employees (id SERIAL PRIMARY KEY, name TEXT);
INSERT INTO employees (name) VALUES ('Alice');
ABoth transactions acquire the lock simultaneously without waiting.
BTransaction B immediately fails with a deadlock error.
CTransaction B waits until Transaction A commits or rolls back before acquiring the lock.
DTransaction A releases the lock immediately after selecting the row.
Attempts:
2 left
💡 Hint

Consider how row-level locks serialize concurrent updates.