Why the Version Store won’t clear if any database has open transactions
Chapters
- 00:00:00 – Introduction
- 00:01:32 – Video Description and Links
- 00:03:15 – The Version Store and Open Transactions
- 00:05:32 – Explanation and Transaction Management
- 00:08:45 – Cleanup Watermark and Transaction Rollback
- 00:10:00 – Conclusion
Full Transcript
Erik Darling here with Darling Data and in today’s video we are going to talk about why the version store won’t clear if any database has open transactions. I realized that I read it a little bit differently than it looks there because I had to say it is what we’re talking about where this asks a question, right, so the words don’t precisely line up. But don’t blame the robots for that. That looks weird in the transcript. Anyway, it sounded weird in my head as I was trying to phrase it because I’m looking at the words and trying to rearrange them. That was tough. I’m not as sharp as I used to be. Anyway, down in the video description, many very helpful links for you and I to interact in ways that are largely profitable towards me, but you might get some sense of kinship or, I don’t know, joy. Something from exercising when you click on them. For instance, you can hire me for consulting. You can buy my very, very reasonably priced training, which you will get for life. You can become a supporting member of the channel. You can ask me office hours questions. And if you really, really like the things that I’m doing here or any portion of the content, like, subscribe, tell a friend, all that good stuff.
That’s all appreciated. If you want some free SQL Server and Postgres, including Aurora Postgres performance monitoring, you can have mine for free at code.erikdarling.com. Go to the performance monitor repo. From there, you can download, you can run, you can get the light version and just double click something and add your server and just immediately start monitoring it. If you need something beefier, more enterprisey, you can get the darling version of it, which is a headless Postgres. Postgres service, then it can monitor like 500 databases. It’s wonderful. Or 500 servers. I mean, if you have 500 servers, you probably have 500, at least 500 databases. That lines up. Anyway, it’s free. It’s great. You should check it out. I make the same amount of money either way. The only difference is how much you spend. I will be attending. I will be attending as a speaker with both a pre-con and a regular session past Summit West this, this Seattle, Washington in November, this, this November in Seattle, Washington from November 9th to 11th at the lovely Hyatt Regency Seattle.
Never been to that one that I can remember. May have probably have walked by it. I won’t be staying there though. Anyway, let’s talk about this here. Now, this is an explanatory post. My dear friend, Brent, wrote a post called, the version store won’t clear if any database has open transactions. The post is, no notes. Everything in here is right, but there’s just no explanation. Much like Brent, it is wildly salacious and utterly bereft of an explanation. It’s a joke. If we didn’t have jokes, we’d have to just fall asleep and die. Why would you want to fall asleep and die? You’d miss out on so many good things.
Anyway, let’s figure out which of these blue icons is Management Studio and let’s talk about why that happens. There was no why. You can’t leave people hanging like that. Getting blue parted various ways. It’s no fun. So, the explanation for it is SQL Server made by very smart people. And very smart people anticipate the wild and crazy things that you out there in the world do. So, at any given time, a transaction, begin transaction. SQL Server doesn’t look forward through the entire transaction and say, ah, this is safe. It doesn’t do that. SQL Server says, well, hang on. This numbskull has begun a transaction. Who knows what they’re going to do with it?
And that transaction could become a cross-database transaction. And if it becomes a cross-database transaction, then the version store across those databases must agree. SQL Server doesn’t know what database you might escalate a transaction to. So, it doesn’t know. It has to keep all of those version stores safe. And I’ve got a bunch of queries here that will show you what I mean here. So, if we run this set of queries, this top section is completely empty for a good reason. There is no query currently doing anything.
The second section shows currently the size of the 10db version store and persistent version store and stuff. I have different demos where I show this buildup, but it’s unnecessary for what we’re doing here. And there’s also this row here, which shows that the Stack Overflow database, it’s the only one that has accelerated database recovery on, which again is unimportant to this because accelerated database recovery does not help with this scenario.
Because the version store watermark has to agree across all the databases. So, ADR doesn’t help. You would think that the persistent version store would help, but it does not. It’s a shame, but there we have it.
Anyway, the reason why that thing has anything to it is because if you’ve ever seen the PVS pre-allocate way, SQL Server pre-allocates pages to the version store. So, when you start putting stuff in there, it doesn’t have to allocate pages immediately. Isn’t that thoughtful? But the important part down here is this final result set where we have a cleanup watermark XSN.
We have the oldest active XSN. It’s probably transaction, but I don’t know. Anyway, oldest active session ID and oldest active database, right? So, we’ve got these columns in here, but they’re all null, right?
But the important ones, well, I mean, there are a few important ones to keep track of. This is kind of one of them right there, right? This one’s important. So, well, these will become important in a moment.
If we come over here to this tab, we can run, we can start a transaction in any database, right? And all I’ve done is run a select query, right? Using snapshot isolation. Now, with snapshot isolation, you don’t immediately start, like, you know, you don’t create your snapshot time when you say begin transaction.
You run it when the first query runs. So, right now, that select count from sysjobs, that marks the start of when we need to start caring about what point in time things are in the database.
And if we come over here to this tab now and we run this query again, things look a little bit different. Now we’ve got this line up here and we can see that we have this old sleepy thing that all it’s done is, I don’t know, some nonsense, right?
Nothing of value. It’s selected a count from a table. This is the same. But now that result set that I told you would become very important is down here, right?
Our cleanup watermark has incremented, right? We can’t clean stuff up until that thing up there goes away. We now have this and this, right?
We have our session ID from up here, right? 52, right? And that is holding this 2686 number hostage. And we can see our oldest active database with the transaction is MSDB.
There wasn’t a great way to just show this as one, but you can see that that’s the thing there. So if you ever want to know why SQL Server behaves this way, it’s because you probably have a bunch of databases on it.
If you don’t have a bunch of databases, well, you don’t have to worry about other databases messing up other databases. You just have the one database. Of course, you probably do have MSDB on there. So someone could technically, even with just a system database, give you a hard time.
But we hope they don’t do that. But this is why it happened. SQL Server has to think about all the dumb things that people do. And one of those dumb things is, of course, cross database transactions where we need to start caring about all of these versions, agreeing at what point in time queries can start reading stuff.
So come over here and we roll back our transaction. Let’s highlight the right stuff. There we go. There all that goes. And we rerun this.
All of a sudden things have returned mostly to normal. This result is gone. These have stayed the same. And this one here. Now we have a new cleanup watermark that is slightly higher than it was before. And we don’t have any transactions keeping the watermark where it is and making other databases unable to clean up the version stuff.
So if you read that post and you said, why on earth would that happen? Now you know. If you didn’t read that post, you could still if you want. If you feel like it.
It’s not a waste of time. Anyway, where was I? Thank you for watching. I hope you enjoyed yourselves.
I hope you learned something. And I will see you in tomorrow’s video where we will, in longer form, answer a question from yesterday’s Office Hours video. Anyway, goodbye.
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.