This sample describes how to read and interpret the Transaction ID (TID) stored in row data pages.
Optimized Locking is a database engine feature designed to reduce the memory used for lock management, decrease the phenomenon known as lock escalation, and increase workload concurrency.
Optimized Locking depends on two technologies that have long been part of the SQL Server engine:
- Accelerated Database Recovery (ADR) is a required prerequisite for enabling Optimized Locking
- Read Committed Snapshot Isolation (RCSI) is not a strict requirement, but allows full benefit from Optimized Locking
Optimized Locking is based on two key mechanisms:
- Transaction ID (TID) locking
- Lock After Qualification (LAQ)
The Transaction ID (TID) is a unique transaction identifier.
When a row-versioning based isolation level is active, or when Accelerated Database Recovery (ADR) is enabled, every row in the database internally contains a transaction identifier.
The TID is stored on disk in the additional 14 bytes that are associated with each row when features such as RCSI or ADR are enabled.
Every transaction that modifies a row tags that row with its own TID, so each row in the database is labeled with the last TID that modified it.
About this sample
Before you begin
Run this sample
Sample Details
Disclaimers
Related links
- Applies to: SQL Server 2025 (or higher)
- Key features: Optimized Locking
- Workload: No workload related to this sample
- Programming Language: T-SQL
- Authors: Sergio Govoni | Microsoft MVP Profile | Blog | GitHub | X
To run this sample, you need the following prerequisites.
Software prerequisites:
- SQL Server 2025 (or higher)
- Download create-configure-optimizedlocking-db.sql T-SQL script from sql-scripts folder
- Verify that a database named OptimizedLocking does not already exist in your SQL Server instance
- Execute create-configure-optimizedlocking-db.sql script on your SQL Server instance
- Run the commands described in the sample details section
Currently, the only way to read the TID of a row is by using the DBCC PAGE command.
Let's consider the table dbo.TelemetryPacket, with the schema defined in the following T-SQL code snippet.
USE [OptimizedLocking]
GO
CREATE TABLE dbo.TelemetryPacket
(
PacketID INT IDENTITY(1, 1)
,Device CHAR(8000) DEFAULT ('Something')
);
GOThe table schema is designed so that each row occupies exactly one data page.
Insert three rows with default values into the dbo.TelemetryPacket table. Note that this is done in a single transaction.
BEGIN TRANSACTION
INSERT INTO dbo.TelemetryPacket DEFAULT VALUES;
INSERT INTO dbo.TelemetryPacket DEFAULT VALUES;
INSERT INTO dbo.TelemetryPacket DEFAULT VALUES;
COMMITLet's explore the content of the dbo.TelemetryPacket table, enriched with the PageId column, which shows the result of the undocumented function sys.fn_PhysLocFormatter. Use this function to correlate the rows returned by the SELECT with their physical location on disk.
USE [OptimizedLocking]
GO
SELECT
*
,PageId = sys.fn_PhysLocFormatter(%%physloc%%)
FROM
dbo.TelemetryPacket;The output is similar to the following, except for the values in the PageId column.
| PageId | PacketID | Device |
|---|---|---|
| (1:2456:0) | 1 | Something |
| (1:2457:0) | 2 | Something |
| (1:2458:0) | 3 | Something |
Each value in the PageId column follows the format (FileID:PageID:SlotID) and represents the physical location of the data.
Let's examine the row where PacketID equals 1. The value (1:2456:0) is composed of three parts separated by ":". Here is what each part represents:
- 1 - the numeric identifier of the database file (FileID)
- 2456 - the page number within the file (PageID)
- 0 - the slot number on the page (SlotID)
Use the DBCC PAGE command to inspect the TID of page 2456.
-- Enable trace flag for DBCC PAGE output
DBCC TRACEON(3604);
GO
DBCC PAGE ('OptimizedLocking', 1, 2456, 3);The value of the unique TID that modified the row with PacketID equal to 1 is in the Version Information section, under the Transaction Timestamp attribute, as shown in the following sample data.
Version Information =
Transaction Timestamp: 985
Version Pointer: Null
Slot 0 Column 1 Offset 0x4 Length 4 Length (physical) 4
PacketID = 1
Slot 0 Column 2 Offset 0x8 Length 8000 Length (physical) 8000
Device = Something…TID 985 represents the identifier of the transaction that inserted the rows; every subsequent change to the table rows will update the TID.
The code included in this sample is not intended to be a set of best practices on how to build scalable enterprise grade applications. This is beyond the scope of this sample.
Note: The
DBCC PAGEcommand is undocumented and intended for troubleshooting and diagnostic purposes only. It should not be used in production environments without proper understanding and testing.
