Locks and Concurrency

Locks and Concurrency is a practical SQL concept used to design, query, secure, operate, or analyze relational data correctly.

Lesson content

Locks and Concurrency Locks and Concurrency is a practical SQL concept used to design, query, secure, operate, or analyze relational data correctly. Example -- Apply Locks and Concurrency to a small, testable schema. SELECT * FROM orders LIMIT 10; Key point Use the smallest correct statement, test it with representative data, and verify constraints and performance before production use. Real-life example A bank transfer debits one account and credits another. Both changes must succeed together, and concurrent transfers must not produce an incorrect balance. Advanced example BEGIN; SELECT balance FROM accounts WHERE id = 1 FOR UPDATE; UPDATE accounts SET balance = balance - 100 WHERE id = 1; UPDATE accounts SET balance = balance + 100 WHERE id = 2; COMMIT; Expected result The database preserves a valid state even when an operation fails or another transaction runs concurrently. Production check Test with empty, duplicate, null, and boundary values. Use a transaction for related writes. Inspect the execution plan before adding an index. Use parameterized queries for application input. Continue with the PicoStore database This lesson reuses picostore . Relevant tables: accounts, customers . Keep the starter rows from the Introduction lesson so results remain comparable. Another practical example BEGIN; UPDATE accounts SET balance = balance - 500 WHERE account_id = 1 AND balance >= 500; UPDATE accounts SET balance = balance + 500 WHERE account_id = 2; COMMIT; SELECT account_id, balance FROM accounts WHERE account_id IN (1, 2); Check the result Run the verification query, compare the returned rows with the starter data, and explain why every included or excluded row is correct. Easy example Start with a small customer table and retrieve active customers in a predictable order. SELECT customer_id, name, email FROM customers WHERE status = 'active' ORDER BY name; How to verify the easy example Run it with representative input. Confirm the expected output. Try one missing, invalid, or boundary value. Advanced example Use a CTE and a window function to rank customer revenue while keeping the query readable and testable. WITH customer_revenue AS ( SELECT customer_id, SUM(total_amount) AS revenue FROM orders WHERE order_status = 'completed' GROUP BY customer_id ) SELECT customer_id, revenue, DENSE_RANK() OVER (ORDER BY revenue DESC) AS revenue_rank FROM customer_revenue ORDER BY revenue_rank, customer_id; Advanced review Explain the tradeoffs and assumptions. Test failure, scale, security, and recovery behavior. Capture evidence from tests, execution plans, logs, or review output. Additional practical guidance Locks and Concurrency: MySQL and PostgreSQL Both engines provide ACID transactions, but default isolation, lock visibility, deadlock diagnostics, and DDL transaction behavior differ. Keep transactions short and retry work selected as a deadlock victim. Required verification Run the simple case. Test a NULL, duplicate, empty, or boundary case where relevant. Confirm the affected rows or query result. Use EXPLAIN for performance-sensitive queries.