

For a DevOps or DBA managing mission-critical environments, few problems are as pungent and misunderstood as lock contention and deadlocks. They are subtle elements that transform a robust infrastructure into a slow system, generate inexplicable timeouts, and, in the worst-case scenario, bring down entire applications during Black Friday.
Some professionals deal with locks as if they were just any slowness, or treat deadlocks as random database errors that resolve themselves with a simple retry in the application. This proves to be a dangerous approach, because locks are not a simple hardware problem; on the contrary, they are a concurrency, design, and engineering problem of your data environment.
In this definitive guide, we consolidate all the knowledge necessary for you to master the subject: from the mechanical theory of relational databases to ready-to-use diagnostic queries and advanced resolution strategies.
1. What Are Locks and Why Do They Exist?
Before demonizing the lock, it is necessary to understand that it is part of the game. Locks are inherent and essential mechanisms in any Relational DBMS (Database Management System) to ensure data integrity and ACID properties (Atomicity, Consistency, Isolation, and Durability).
To understand better, think of a banking system: two users try to withdraw money from the same account at the same time. How to ensure they don’t withdraw duplicate amounts and leave a negative balance? For this, the database “locks” the account record for the first user, forcing the second to wait, and only releases it after the transaction concludes.
The Essential Types of Locks
Although each database (Oracle, SQL Server, PostgreSQL) has its own nomenclature, the universal concepts are:
- Read Locks (Shared Locks – S): Allow multiple users to read data simultaneously (read concurrency) but prevent any transaction from modifying that data while reading occurs.
- Write Locks (Exclusive Locks – X): The total block. If a transaction holds an exclusive lock to perform an UPDATE or DELETE, no one else reads and no one else writes to that resource until the lock is released (COMMIT or ROLLBACK).
- Update Locks (Update Locks – U): A hybrid state used to prevent deadlocks. The transaction signals that it will modify data but is still in the reading phase. This prevents multiple sessions from trying to escalate from reading to writing at the same time.
- Intent Locks (Intent Locks): A hierarchical signaling. The database warns: “I am going to lock a row inside this table, so don’t try to lock the whole table now.”
When the Queue Stops (Blocking)
Therefore, we know that the problem is not the existence of the lock, but actually its duration and granularity. The lock becomes a performance problem when:
- Excessive Concurrency: Hundreds of sessions trying to access the same table or record simultaneously.
- Long Transactions: An UPDATE that takes 30 seconds holds the resource for 30 seconds. The rest of the system stops and enters a wait queue (Wait Events).
- Inadequate Granularity: The database decides to lock the entire table (Table Lock) to update just a few rows, paralyzing the operation for all users.
2. The Deadlock
While the Lock can be considered a queue, the Deadlock is a gridlock where nothing moves.
A deadlock occurs when two or more sessions enter a cycle of mutual dependency, known as Circular Wait.
- Session A has the key to Resource 1 and wants to access Resource 2.
- Session B has the key to Resource 2 and wants to access Resource 1.
- No passage is granted and no one advances. The database detects this mathematically impossible-to-solve cycle and intervenes drastically: it kills one of the sessions to allow the other to move forward. For the application, this generates a fatal error output, like 1205 in SQL Server.
The 4 Mathematical Conditions of Deadlock (Coffman Conditions)
For a deadlock to occur, four conditions must be true simultaneously. If you break one of them through design or code, the deadlock disappears:
- Mutual Exclusion: The resource accepts only one owner at a time (Write Locks).
- Hold and Wait: I hold a resource while waiting for another.
- No Preemption: The database cannot forcibly rip the lock from your hand (except by killing the session).
- Circular Wait: The chain closes on itself (A waits for B, which waits for A).
3. Practical Diagnosis: How to Detect Locks?
An operation that consistently meets its SLA cannot wait for user complaints; real-time visibility is necessary. Below are the essential queries to identify who is blocking whom.
1. SQL Server (DMVs)
This query identifies active blocks in real-time:
SELECT
blocking_session_id AS Blocker_Session,
session_id AS Victim_Session,
wait_type,
wait_time AS Wait_Time_MS,
last_wait_type,
TEXT AS Query_Text
FROM sys.dm_exec_requests
CROSS APPLY sys.dm_exec_sql_text(sql_handle)
WHERE blocking_session_id <> 0;
2. Oracle Database
To identify the blocking chain in Oracle:
SELECT
s1.username || '@' || s1.machine || ' (SID=' || s1.sid || ' ) is blocking '
|| s2.username || '@' || s2.machine || ' (SID=' || s2.sid || ' ) ' AS blocking_status
FROM v$lock l1, v$session s1, v$lock l2, v$session s2
WHERE s1.sid=l1.sid AND s2.sid=l2.sid
AND l1.BLOCK=1 AND l2.request > 0
AND l1.id1 = l2.id1 AND l2.id2 = l2.id2;
3. PostgreSQL
To see who is blocking whom in Postgres:
SELECT
blocked_locks.pid AS blocked_pid,
blocked_activity.usename AS blocked_user,
blocking_locks.pid AS blocking_pid,
blocking_activity.usename AS blocking_user,
blocked_activity.query AS blocked_statement,
blocking_activity.query AS current_statement_in_blocking_process
FROM pg_catalog.pg_locks blocked_locks
JOIN pg_catalog.pg_stat_activity blocked_activity ON blocked_activity.pid = blocked_locks.pid
JOIN pg_catalog.pg_locks blocking_locks
ON blocking_locks.locktype = blocked_locks.locktype
AND blocking_locks.database IS NOT DISTINCT FROM blocked_locks.database
AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation
AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page
AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple
WHERE NOT blocked_locks.granted;
4. Definitive Strategies to Eliminate and Prevent Locks

To cure the problem at the root, we need to act on four fronts: SQL, Modeling, Application, and Infrastructure.
a. Query and Index Optimization (The #1 Cause)
A fast query holds locks for less time.
If an UPDATE needs to scan the entire table (Table Scan) to find the record to be changed, it can lock the entire table in the process.
- The Solution: Create indexes that support WHERE and JOIN clauses. If the database finds the record instantly (Index Seek), the lock will be applied only to that row (Row Lock), freeing the rest of the table for other users.
b. Short and Atomic Transactions
The most common mistake by developers is opening a transaction (BEGIN TRAN), performing an update, and then making external API calls, sending emails, or complex in-memory processing before the COMMIT.
- The Solution: Keep the transaction as short as possible. Prepare data before opening the transaction. Do the Update. Commit. Only then send the email or call the API.
c. Access Order Standardization (End of Deadlocks)
Remember “Circular Wait.” If Application X accesses Table A and then B, and Application Y accesses Table B and then A, a deadlock will certainly occur.
- The Solution: Create a rigorous coding convention. “We always access tables in alphabetical order” or “We always update Master before Detail.” If everyone follows the same flow, the cycle does not close.
d. Hardware and Disk Configuration
The reason for a long lock can also be your slow disk. If the database struggles to write the transaction log to an old HDD or saturated disk, the lock will stay active waiting for disk I/O to complete.
- The Solution: High-performance databases require low-latency storage (SSD/NVMe). Monitor disk latency; if it is high, your locks will last longer than necessary. Also learn about ultra-fast, though volatile, RAM processing.
e. Isolation Levels (RCSI in SQL Server)
Many blocks occur between readers (SELECT) and writers (UPDATE).
- The Solution: In databases like SQL Server, enabling RCSI (Read Committed Snapshot Isolation) causes readers to use row versioning (in TempDB) instead of locks. This means readers don’t block writers, and writers don’t block readers.
5. Real Case Study: Retail ERP
Let’s analyze a dbsnOOp use case during a Black Friday:
A large retail ERP was processing thousands of orders per minute. CPU and Memory were plentiful (below 40%), but the system was slow, with customers receiving timeout errors at checkout. By using dbsnOOp’s deep, AI-driven observability, one doesn’t look only at CPU: it is possible to go straight to wait analysis (Wait Statistics).
A massive lock chain was identified. The root cause was not sales volume, but an inventory update Job running every 5 minutes.
- The Job opened a giant transaction to update 5,000 products at once.
- While it ran (lasted 45 seconds), it blocked the products table.
- Customers trying to buy (SELECT and UPDATE on inventory) queued up waiting for the Job to finish.
- The accumulation of connections waiting burst the application connection pool, generating the timeout error.
Solution (Mitigation in 15 minutes):
With precise identification of the blocking query (the Job) and the victims (Checkout), the team acted:
- Immediate: The Job was paused during peak hours.
- Definitive: The Job was refactored to update in small batches of 100 products at a time, with COMMIT after each batch. This allowed sales to “sneak in” between batch intervals, eliminating the perception of freezing.
- Result: Checkout response time dropped from 8 seconds (with timeouts) to 200ms.
6. Why Traditional Monitoring Fails with Locks?
Rudimentary infrastructure monitoring tools (Zabbix, Nagios, CloudWatch) look only at resources (CPU, RAM, Disk). However, locks do not consume resources directly; locks consume time.
If your database is frozen by a deadlock, CPU usage might drop to 1% because no one can do anything. Traditional monitoring will say the server is “healthy” and “idle,” while your phone won’t stop ringing with complaining users.
How dbsnOOp Solves This
The dbsnOOp platform was designed specifically for database depth.
- Deadlock History: We don’t just alert; we store the deadlock graph. You can go back in time and see exactly which query conflicted with which last night at 03:00 AM.
- Correlation with Deploy: We cross-reference performance data with recent changes. If locks started after the last deploy, you know where to look.
- AI and Query Performance: Our AI analyzes wait patterns and automatically suggests indexes or query refactorings to prevent recurrence.
In short, we know that locks are not fatalities, but signs that your Database environment needs adjustments. In this context, it is essential that proper optimizations be made so your team doesn’t get stuck from lock to lock.
The difference between a system that crashes and one that scales is Observability. You cannot fix what you do not see. If you want to stop guessing why the database is slow and start seeing the root cause in seconds, it is time to evolve your monitoring.
Schedule a demo here.
Learn more about dbsnOOp!
Learn about database monitoring with advanced tools here.
Visit our YouTube channel to learn about the platform and watch tutorials.

Recommended Reading
- The dbsnOOp Step-by-Step: From a Slow Database Environment to an Agile, High-Performance Operation: This article serves as a comprehensive guide connecting observability to operational agility. It details how to transform data management from a reactive bottleneck into a pillar of high performance, aligned with DevOps and SRE practices.
- Why Relying Solely on Monitoring Is Risky Without a Technical Assessment: Explore the critical difference between passive monitoring, which merely observes symptoms, and a deep technical assessment that investigates the root cause of problems. The text covers the risks of operating with a false sense of security based only on monitoring dashboards.
- What is query degradation and why does it happen?: A database assessment is, in essence, a hunt for the causes of performance problems. This article details the most common “disease” that an assessment diagnoses: the silent degradation of queries. It is a fundamental reading to understand the type of deep problem that an analysis like dbsnOOp’s reveals.