Press "Enter" to skip to content

Category: Locks, Blocks, and Deadlocks

Working with Latch Contention in SQL Server

M A A Mehedi Hasan gets into latches:

As a DBA, you are often vigilant to the common suspects like blocking, deadlocks, or long-running queries. Sometimes, performance issues creep in silently due to latch contention. When it overwhelms, even the most optimized queries may crawl. This article explores what SQL Server latch contention is, how latches differ from locks and how to detect latch issues. Then we will simulate a latch contention scenario and check best practices to minimize its impact on your SQL Server database.

Read on for notes around what latches are and how they differ from locks, why you might get latch contention, and a few ways you might be able to resolve contention issues if you have them. I will say that, in my own experience, latch contention typically wasn’t that big of a deal, in the sense that people make databases slow in other ways long before latch contention becomes the primary issue.

Leave a Comment

The Importance of Blocking in Databases

Louis Davidson explains why we suffer:

Nope, this is not a clickbait title (ok, well sort of). Blocking is good….otherwise SQL Server (and all the other database platforms would eliminate it for good. And before you say something about a pure MVCC implementation like SQL Server implemented with in-memory OLTP… there is no blocking, but in place of blocking you can do a lot of work, perhaps hours, and when you go to commit, it fails… blocking is better than that in any case where you are doing large transactions.

Read on for the nuance. I fully agree with Louis on this, with the caveat that SQL Server, by default, does too much blocking due to its Read Committed isolation level. A default of Read Committed Snapshot Isolation would have been a good switch about 15 years ago.

2 Comments

When NOLOCK Does Permanent Damage

Aaron Bertrand shares examples:

This is not another “NOLOCK can show you dirty data” rant. There are plenty of posts about that, usually explaining how the hint is a misnomer because it doesn’t really mean “no” locks, how dirty or phantom data can sneak into a report and render it inaccurate, and why RCSI is often a much better solution to reader/writer conflicts.

No, I’ve largely given up trying to swim against the current and fight draconian policies that have taught everyone, far and wide, the commandment:

THOU SHALT USE NOLOCK ON EVERY TABLE REFERENCE.
EVER.
UNTIL THE END OF DAYS.

I don’t know where people learned this, but it certainly feels that way in, well, every place I’ve worked, either as a full-time employee or as a consultant. In each case, it’s been a long road to get them down RCSI, removing NOLOCK, and realizing that things are better.

By the way, I’m glad Aaron included the two non-examples of UPDATE and DELETE with NOLOCK, as I’ve seen cases of this in the wild as well, even though it (thankfully) has zero impact because SQL Server ignores the hint.

Comments closed

Five Locking Behaviors to Watch for in PostgreSQL

Shinya Kato has a list:

PostgreSQL uses MVCC (Multi-Version Concurrency Control) for concurrency control: reads never block writes, and writes never block reads.

Its locking system has 8 table-level lock modes and 4 row-level lock modes, and the conflict tables in the documentation tell you exactly which lock modes conflict with which.

In practice, though, once you actually operate PostgreSQL, locks end up conflicting in places you never expected. Queries take far longer than anticipated, and in the worst case you end up with an outage.

Click through for five notes around locking that may not be immediately apparent.

Comments closed

Adding Foreign Keys and Deadlocks

Michael J. Swart explains a challenge in adding a foreign key to an existing table:

Schema modification locks (SCH-M) are taken by DDL (Data Definition Language) statements like CREATE/ALTER/DROP.
Schema stability locks (SCH-S) are taken by DML (Data Manipulation Language) statements like INSERT/UPDATE/DELETE.

Those two types of locks are incompatible. Meaning, I can’t get a SCH-S lock on some table if you’ve already got a SCH-M lock on it (and vice versa).
Paul Randal describes the SCH-M lock as a super-table-X lock. It makes sense to me, if I’m half way through querying a table, I don’t want its definition to change.

Such a pessimistic lock can be awkward for a busy system. The SCH-M can cause a lot of blocking. For example, creating (and dropping) foreign keys requires a SCH-M lock not only on the parent table, but also on the referenced table which leads to trouble.

Click through for a demonstration of the problem. Michael also has some guidance on how to minimize the issue. I’d note the degenerative form of this guidance: understand your data model up-front and apply foreign key constraints at table creation time. That’s not always possible, sure, so when you can’t do that, Michael has some good advice.

Comments closed

Optimized Locking in SQL Server 2025

John Deardurff enables a feature:

Modern SQL Server workloads demand high concurrency without sacrificing consistency. Traditionally, we relied on locking to enforce correctness, but that came with blocking and performance challenges. To resolve some of these issues, row versioning was used to reduce some of that contention overhead. But now with optimized locking in SQL Server 2025, we have a more efficient and scalable concurrency model.

Click through for a demo. My point of curiosity is, what’s the impact in a practical but busy environment? I don’t have one of those running SQL Server 2025, so I do wonder when we’ll get the tell-all post from someone in a very busy environment who has it on. (Or, if we already have and I’ve missed it, please do let me know!)

2 Comments

Reviewing Blocking and Deadlocking

Erik Darling digs into more functionality in his performance monitoring tool:

In this video, I delve into the blocking and deadlock monitoring capabilities of FreeSQL Server Performance Monitoring, a tool I’ve developed and made available on GitHub. With a focus on practicality and ease-of-use, I explain how we leverage extended events for both blocking and deadlock scenarios, ensuring that you can identify and address performance issues efficiently. Whether you’re using system health or prefer to rely on the block process report, my monitoring tool streamlines the process by automatically setting up necessary configurations and providing easy-to-understand visualizations of your SQL Server’s performance trends over time.

Click through for the video and transcript.

Comments closed

Lock after Qualification in SQL Server 2025

Hugo Kornelis has a new video:

One of these two features is Lock After Qualification (LAQ). This feature avoids the scenario where a delete or update is blocked by a locked row that would not qualify. But, like any good thing, there is a price. This video shows the feature, explains how it works, and shows some of the potentially undesired side effects.

Click through for the video.

Comments closed

Locks in Microsoft Fabric Data Warehouse

Twinkle Cyril enumerates the lock types in Fabric Data Warehouse:

Fabric DW supports ACID-compliant transactions using standard T-SQL (BEGIN TRANSACTION, COMMIT, ROLLBACK) and enforces snapshot isolation across all operations. Locks in Fabric Data Warehouse are used to manage concurrent access to metadata and data, especially during DDL operations. Here’s how locking works:

Click through for a chart. The locking policy is a lot simpler than what we see in SQL Server and you can see a description of the pros and cons of that simpler approach.

Comments closed