Close to Halloween we noticed that some SQL Servers had started acting spooky, as if they wanted to add to the sentiment of Halloween. Last week I had one SQL Server doing exactly that. The server had been running perfectly for the past months but that day itsTempdDB was starting to fill up. Of course this required some investigation, and I found that VersionStore was the problem for my unexpected TempDB growth. The strange thing was that one database was taking up almost all of my tempdb space in version store, but it did not have any open transactions. Spooky stuff indeed!
So the ghost I was investigating was the fact that version store data of one database would not be removed from tempdb after the transaction was committed. To show you how the ghost worked and how you can solve it, I’ll continue with code examples of my haunting companion.
Since I am working on a Consolidated Server, I start off by creating two databases that have nothing to do with each other except for the fact that they are on the same server instance and share the resources.
CREATE DATABASE TESTDB_1
GO
CREATE DATABASE TESTDB_2
GO
Next, I’m going to create a table in each database
USE [TESTDB_1]
GO
CREATE TABLE TestTable (TTID int identity(-2147483648,1),c1 varchar(200))
GO
USE [TESTDB_2]
GO
CREATE TABLE TestTable (TTID int identity(-2147483648,1),c1 varchar(200))
GO
And insert some data into it
USE [TESTDB_1]
DECLARE @Insert int
SET @Insert = 1
WHILE @Insert <= 10000
BEGIN
INSERT INTO dbo.TestTable
VALUES (‘TEST’+cast(@Insert as varchar))
SET @Insert = @Insert + 1
END
GO
select top 10000 * from dbo.TestTable
GO
USE [TESTDB_2]
GO
DECLARE @Insert int
SET @Insert = 1
WHILE @Insert <= 10000
BEGIN
INSERT INTO dbo.TestTable
VALUES (‘TEST’+cast(@Insert as varchar))
SET @Insert = @Insert + 1
END
GO
select top 10000 * from dbo.TestTable
GO
Now I’m going to allow my databases to use snapshot so that we will be able to use the version store
ALTER DATABASE TESTDB_1 SET allow_snapshot_isolation ON
GO
ALTER DATABASE TESTDB_2 SET allow_snapshot_isolation ON
GO
I’m now going to execute an update on one of the tables in a committed transaction
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
BEGIN TRAN
USE TESTDB_1
update dbo.TestTable
SET c1 = ‘UpdatedValue’
COMMIT
If I go on and check the version store…
SELECT count(*) as amountofrec,d.name DBName from sys.dm_tran_version_store tvs
inner join sys.databases d on tvs.database_id = d.database_id
group by d.name
…we can see that there are some values in version store
So now we wait for the Tempdb garbage collector to kick in (this will take about a minute) and execute the query from above, so we can see that our version store is once again empty.
This is normal behavior. But today, I did not get the normal behavior. There was spooky behavior; this really small transaction stayed open for about two hours on one of my databases, plus a lot of update action on another database which I will demonstrate in the following code.
This is the open small transaction
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
BEGIN TRAN
USE TESTDB_1
update dbo.TestTable
SET c1 = ‘SmallTransactionUpdateNotCommitted’
where TTID = -2147483648
If we check the version store we see our one row
If we look for an OPEN transaction we can see that the transaction is still open on the database
Because it is an open transaction this row will not be deleted from our version store.
Now I will start mass updating on the other table in the other database. TESTDB_2
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
BEGIN TRAN
USE TESTDB_2
update dbo.TestTable
SET c1 = ‘MultipleUpdates’
COMMIT
GO 10
As we can see this updated all the values ten times and committed the transaction
There are no open transactions left on the second database
As we check our version store, we see that the rows are now in the version store
If we wait a minute to let our garbage collector kick in, we can see that…
… the rows are still in version store!
So not just the row of the uncommitted transaction of one database will stay in version store, all the other rows of transactions that are committed after the first transaction was opened. This means that one open transaction can cause your tempdb to fill up with data from other databases!
To prove my point, I will now commit the open transaction and see if our version store gets cleared.
If we wait approximately one minute and check our version store again we can see that it has been cleared.
A spooky problem indeed! Even the smallest open transaction that uses version store in your consolidated database environment can haunt all of your databases.
The solution for this problem is quite straightforward: commit the open transaction or if you cannot commit it, kill it. One minute later, your version store will be empty again.
Thank you for reading, and stay tuned!
© 2023 Kohera
Crafted by
© 2022 Kohera
Crafted by
Cookie | Duration | Description |
---|---|---|
ARRAffinity | session | ARRAffinity cookie is set by Azure app service, and allows the service to choose the right instance established by a user to deliver subsequent requests made by that user. |
ARRAffinitySameSite | session | This cookie is set by Windows Azure cloud, and is used for load balancing to make sure the visitor page requests are routed to the same server in any browsing session. |
cookielawinfo-checkbox-advertisement | 1 year | Set by the GDPR Cookie Consent plugin, this cookie records the user consent for the cookies in the "Advertisement" category. |
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
CookieLawInfoConsent | 1 year | CookieYes sets this cookie to record the default button state of the corresponding category and the status of CCPA. It works only in coordination with the primary cookie. |
elementor | never | The website's WordPress theme uses this cookie. It allows the website owner to implement or change the website's content in real-time. |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |
Cookie | Duration | Description |
---|---|---|
__cf_bm | 30 minutes | Cloudflare set the cookie to support Cloudflare Bot Management. |
pll_language | 1 year | Polylang sets this cookie to remember the language the user selects when returning to the website and get the language information when unavailable in another way. |
Cookie | Duration | Description |
---|---|---|
_ga | 1 year 1 month 4 days | Google Analytics sets this cookie to calculate visitor, session and campaign data and track site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognise unique visitors. |
_ga_* | 1 year 1 month 4 days | Google Analytics sets this cookie to store and count page views. |
_gat_gtag_UA_* | 1 minute | Google Analytics sets this cookie to store a unique user ID. |
_gid | 1 day | Google Analytics sets this cookie to store information on how visitors use a website while also creating an analytics report of the website's performance. Some of the collected data includes the number of visitors, their source, and the pages they visit anonymously. |
ai_session | 30 minutes | This is a unique anonymous session identifier cookie set by Microsoft Application Insights software to gather statistical usage and telemetry data for apps built on the Azure cloud platform. |
CONSENT | 2 years | YouTube sets this cookie via embedded YouTube videos and registers anonymous statistical data. |
vuid | 1 year 1 month 4 days | Vimeo installs this cookie to collect tracking information by setting a unique ID to embed videos on the website. |
Cookie | Duration | Description |
---|---|---|
ai_user | 1 year | Microsoft Azure sets this cookie as a unique user identifier cookie, enabling counting of the number of users accessing the application over time. |
VISITOR_INFO1_LIVE | 5 months 27 days | YouTube sets this cookie to measure bandwidth, determining whether the user gets the new or old player interface. |
YSC | session | Youtube sets this cookie to track the views of embedded videos on Youtube pages. |
yt-remote-connected-devices | never | YouTube sets this cookie to store the user's video preferences using embedded YouTube videos. |
yt-remote-device-id | never | YouTube sets this cookie to store the user's video preferences using embedded YouTube videos. |
yt.innertube::nextId | never | YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen. |
yt.innertube::requests | never | YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen. |
Cookie | Duration | Description |
---|---|---|
WFESessionId | session | No description available. |