A Little About Serializable Escalation In SQL Server
Video Summary
In this video, I delve into a fascinating aspect of SQL Server that often goes unnoticed: how certain actions can escalate to the serializable isolation level without explicitly setting it. I explain why you might not always see these escalations in standard monitoring tools like `sp_whoisactive` or deadlock XML reports and demonstrate with an example involving indexed views, which can trigger this behavior when updating referenced tables. By walking through the process of creating an indexed view and performing updates that cascade to multiple tables, I show how to identify these hints using my `What’s Up Locks` tool and trace flags, revealing the complexity behind SQL Server’s locking mechanisms.
Full Transcript
Erik Darling here with Darling Data. And today’s video is kind of a fun one because something that, you know, like you’ll hear a lot, but not actually be able to see very easily. And this is about how doing some things in SQL Server will escalate to the serializable escalation level, but not tell, serializable escalation level, isolation level. I mean, it is an escalation level too, but, but not really tell you. And how like, you know, like all the normal stuff that you would look at if you were trying to figure this stuff out doesn’t really show you like, you know, like in a, in a meaningful way that the serializable escalation happened. So like, like if you’re looking at the block pro, if you’re looking at like SP who is active, right? Well, there’s blocking going on. It won’t, it can’t tell you about this. If you look at, um, if you look at like the block process report, the deadlock XML report, it won’t tell you about this. And the reason why is because the, the serializable, uh, isolation level escalation happens as a hint. So like it’s not setting the transaction level for like the whole thing to serializable, right? When, when you set transaction isolation level something, then you can see that in who is active. Then you can see that in the block process report.
Then you can see that in the XML deadlock report. If you just change the, if you ask for something different at the, using a query hint, it’s not for the whole transaction. And so SQL Server usually like, if you’re using all the defaults, we’ll just report read committed. All right. So, uh, we’ll talk about that in this video. Anyway, uh, if you think this stuff is interesting and you have interesting SQL Server problems or even really boring ones that you just want someone else to work on, you can hire me for consulting. Uh, if you want to learn more about SQL Server, stuff like this, you can buy my training. Uh, if you like this content enough to, uh, the, you know, uh, like donate a bit to the channel, you can become a channel member. All of these things are down in the video description, uh, along with, uh, the link to ask me office hours questions, which I try to answer five of every week. And of course, if you enjoy this content, uh, do the kind thing, uh, like subscribe, tell a friend, all that good stuff. Uh, the only thing that I have left on my speaking schedule, uh, through the end of the year is past data community summit, uh, taking place in Seattle, November 17th to 21st, where I will be banging out two days of T SQL pre-cons with Kendra little. Uh, they will, are undoubtedly the finest T SQL content ever produced. Uh, so I will, I will see you over there.
Anyway, let’s, let’s get this party start. Do do do come over here. So I’ve gotten in, so the way, so this can happen for two reasons. I’m going to show you one of them because showing you both of them would be boring and redundant. So, uh, I’ve created an indexed view. Uh, it’ll, it will happen for this. Um, if the index view has more than one table referenced in it. So like, like there’s an, obviously a join between users and posts in here.
This will also happen if you have foreign keys with cascading updates or deletes. So there are two things that at least I’m aware of where this will happen, right? So, uh, modifying tables that are referenced by index views, assuming that there is more than one table in the index view. Otherwise you just get regular X lock hints, uh, and cascading foreign keys. So I’m going to show you the example with, uh, index views.
So this view is already created or altered, and this index is already created. When I run this, I’ll get an error. Great. So, uh, one way that you can kind of see this is for some, this doesn’t like these types of locks don’t always show up when you, uh, modify a table reference in the index view. Um, that, that, that I, that I don’t have full details on, but I can tell you that for this update takes around four seconds.
And if we use, uh, my little helper thing called what’s up locks, uh, this is available at my GitHub repo. Uh, the short link for that is code.erikdarling.com. That’s where you get all my other store procedures like quickie store and pressure detector and stuff. But if we run that update and we get the locks and then we, uh, you know, roll back the transaction within that, we can see range XX lock.
So range locks like this are, uh, hints about serializable stuff going on. So that’s the first thing, right? Pretty, pretty obvious that we got some serializable locks that way.
The only way to see the hints that SQL Server supplies that I’ve found is to run the update. Um, you need to recompile hint. So the stuff shows up, but then to use this sort of smattering of trace flags to get other information out.
So if we run this, this will also take about four seconds and, um, down to the messages tab, you’ll have all this crazy stuff, right? There’s stuff, this stuff goes on basically forever. But if we look, um, if we look through this enough, we’ll see some hints in here, right?
So if we scroll down and we find where, uh, we start looking at tables and things, then we will see, uh, parts where SQL Server started applying hints. But, uh, it’s, it’s really kind of a nightmare to find and all this stuff. Uh, you really have to go scrolling and looking and, uh, it’s, it’s not a lot of fun.
Um, so, um, I’m not going to make you sit through all that, uh, but this is what part of the output in that looks like, right? You’ll see something like, uh, FIOP, which is, you know, physical, physical operation and, uh, range. And then we’ll see for the POST table.
Remember that was one of the tables that was in the indexed view. Uh, we’ll see, uh, some information in here. And then of course we will see hints applied. Those hints will be, uh, serializable internal and detect snapshot conflict.
Uh, detect snapshot conflict is in there in case you have snapshot isolation level enabled. The snapshot isolation level enabled in SQL Server needs to figure out if your rights are going to conflict with another one. But the important thing is the serializable internal.
And then on the indexed view itself, user post score, right? So this was on the POST table, but on user post score, we get some different ones. We get an upd lock and along with serializable, uh, because we actually have to update, we have to maintain the index view.
We’re not updating the POST table in this. Remember, we were just updating the users table. Uh, so the, but the POST table needs to be read from using the serializable isolation level for the indexed view itself.
We, we take an upd lock cause we have to maintain the index view, right? Cause the column in the users table we were updating is in the indexed view. So we have to update the index view.
And of course we get the same serializable hint when reading from the POST, from the index view to figure out which rows need to be maintained. Uh, and we of course get the detect snapshot conflict, um, hint as well, just in case snapshot isolation is enabled. So, like I said before, you have to go pretty deep into SQL Server stuff to find where these hints get applied.
Uh, it takes a lot of trace flagging and other annoying stuff to, to get this information. And then looking at the trace flag output, which is also not fun. Um, so, you know, there’s that, but once you find it, you can prove to people that, uh, doing certain things in SQL Server, primarily, uh, uh, creating an index view that spans more than one table.
And, uh, having to update one of the tables in that index view and, uh, and, uh, foreign keys with cascading updates or deletes will also show this same stuff. So it does happen. This is the only way I’ve found to really prove that it happens, but it does.
And, uh, that’s my story and I’m sticking to it. Anyway, thank you for watching. I hope you enjoyed yourselves. I hope you learned something and I will see you next time.
Adios. I’m hitting the wrong button. There we go.
Going Further
If this is the kind of SQL Server stuff you love learning about, you’ll love my training. Blog readers get 25% off the Everything Bundle — over 100 hours of performance tuning content. Need hands-on help? I offer consulting engagements from targeted investigations to ongoing retainers. Want a quick sanity check before committing to a full engagement? Schedule a call — no commitment required.