0
0
PostgreSQLquery~30 mins

Deadlock detection and prevention in PostgreSQL - Mini Project: Build & Apply

Choose your learning style9 modes available
Deadlock Detection and Prevention in PostgreSQL
📖 Scenario: You are managing a small online bookstore database. Sometimes, when multiple users try to update orders and inventory at the same time, the system experiences deadlocks. This causes delays and errors in processing orders.To improve the system, you will create a setup to detect and prevent deadlocks using PostgreSQL features.
🎯 Goal: Build a simple PostgreSQL setup that simulates transactions causing deadlocks, then implement a query to detect deadlocks, and finally apply a prevention technique using transaction ordering.
📋 What You'll Learn
Create two tables: orders and inventory with specified columns
Insert initial data into both tables
Write two concurrent transactions that can cause a deadlock
Write a query to detect deadlocks from PostgreSQL system views
Apply a transaction ordering strategy to prevent deadlocks
💡 Why This Matters
🌍 Real World
Deadlocks happen in real databases when multiple users or processes try to access the same data in conflicting ways. Detecting and preventing deadlocks keeps applications running smoothly without errors or delays.
💼 Career
Database administrators and backend developers must understand deadlocks to maintain data integrity and system performance in multi-user environments.
Progress0 / 4 steps
1
Create tables and insert initial data
Create a table called orders with columns order_id (integer primary key) and product_id (integer). Also create a table called inventory with columns product_id (integer primary key) and stock (integer). Insert one row into orders with order_id 1 and product_id 101. Insert one row into inventory with product_id 101 and stock 50.
PostgreSQL
Need a hint?

Use CREATE TABLE statements for both tables with the specified columns and types. Then use INSERT INTO to add one row to each table.

2
Write transactions that can cause deadlock
Write two SQL transaction blocks named transaction1 and transaction2. In transaction1, lock the row in orders where order_id = 1 using SELECT FOR UPDATE, then lock the row in inventory where product_id = 101. In transaction2, lock the row in inventory where product_id = 101 first, then lock the row in orders where order_id = 1. Use BEGIN and COMMIT to define each transaction.
PostgreSQL
Need a hint?

Use BEGIN; and COMMIT; to define transactions. Use SELECT ... FOR UPDATE to lock rows. Make sure the locking order is reversed between the two transactions.

3
Detect deadlocks using PostgreSQL system views
Write a SQL query that selects all columns from pg_locks joined with pg_stat_activity on pid, filtering to show only rows where granted is false. This query will help detect waiting locks that may cause deadlocks.
PostgreSQL
Need a hint?

Use pg_locks and pg_stat_activity joined on pid. Filter with WHERE NOT l.granted to find locks that are waiting.

4
Prevent deadlocks by consistent transaction ordering
Modify the transactions so both transaction1 and transaction2 lock rows in the same order: first lock the row in orders where order_id = 1, then lock the row in inventory where product_id = 101. Use BEGIN and COMMIT to define each transaction.
PostgreSQL
Need a hint?

Ensure both transactions lock orders first, then inventory. This consistent order prevents deadlocks.