You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: samples/features/optimized-locking/transaction-id-locking/README.md
+46-48Lines changed: 46 additions & 48 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,7 +39,7 @@ Every transaction that modifies a row tags that row with its own TID, so each ro
39
39
<aname=about-this-sample></a>
40
40
## About this sample
41
41
42
-
-**Applies to:** SQL Server 2025 (or higher)
42
+
-**Applies to:** SQL Server 2025 (or higher), Azure SQL Database
43
43
-**Key features:** Optimized Locking
44
44
-**Workload:** No workload related to this sample
45
45
-**Programming Language:** T-SQL
@@ -52,7 +52,7 @@ To run this sample, you need the following prerequisites.
52
52
53
53
**Software prerequisites:**
54
54
55
-
1. SQL Server 2025 (or higher)
55
+
1. SQL Server 2025 (or higher) or Azure SQL Database
56
56
57
57
<aname=run-this-sample></a>
58
58
## Run this sample
@@ -67,8 +67,6 @@ To run this sample, you need the following prerequisites.
67
67
<aname=sample-details></a>
68
68
## Sample Details
69
69
70
-
Currently, the only way to read the TID of a row is by using the `DBCC PAGE` command.
71
-
72
70
Let's consider the table dbo.TelemetryPacket, with the schema defined in the following T-SQL code snippet.
73
71
74
72
```sql
@@ -87,74 +85,74 @@ The table schema is designed so that each row occupies exactly one data page.
87
85
88
86
Insert three rows with default values into the dbo.TelemetryPacket table. Note that this is done in a single transaction.
89
87
88
+
Before committing the transaction, we query the [sys.dm_tran_locks](https://learn.microsoft.com/sql/relational-databases/system-dynamic-management-views/sys-dm-tran-locks-transact-sql) DMV, which exposes the TID locks as a new resource type = `XACT`.
89
+
90
90
```sql
91
-
BEGIN TRANSACTION
91
+
BEGIN TRANSACTION;
92
+
92
93
INSERT INTOdbo.TelemetryPacket DEFAULT VALUES;
93
94
INSERT INTOdbo.TelemetryPacket DEFAULT VALUES;
94
95
INSERT INTOdbo.TelemetryPacket DEFAULT VALUES;
95
-
COMMIT
96
-
```
97
-
98
-
Let'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.
99
-
100
-
```sql
101
-
USE [OptimizedLocking]
102
-
GO
103
96
104
97
SELECT
105
-
*
106
-
,PageId =sys.fn_PhysLocFormatter(%%physloc%%)
98
+
l.resource_description
99
+
,l.resource_associated_entity_id
100
+
,l.resource_lock_partition
101
+
,l.request_mode
102
+
,l.request_type
103
+
,l.request_status
104
+
,l.request_owner_type
107
105
FROM
108
-
dbo.TelemetryPacket;
109
-
```
106
+
sys.dm_tran_locksAS l
107
+
WHERE
108
+
(l.request_session_id= @@SPID)
109
+
AND (l.resource_type='XACT');
110
110
111
-
The output is similar to the following, except for the values in the PageId column.
112
-
113
-
| PageId | PacketID | Device |
114
-
| ----------- | --------- | --------- |
115
-
| (1:2456:0) | 1 | Something |
116
-
| (1:2457:0) | 2 | Something |
117
-
| (1:2458:0) | 3 | Something |
111
+
COMMIT;
112
+
```
118
113
119
-
Each value in the PageId column follows the format **(FileID:PageID:SlotID)** and represents the physical location of the data.
114
+
The resource_description column, in this example, reports the `XACT` value equal to `10:1147:0`.
120
115
121
-
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:
122
-
-**1** - the numeric identifier of the database file (FileID)
123
-
-**2456** - the page number within the file (PageID)
124
-
-**0** - the slot number on the page (SlotID)
116
+
TID `1147` represents the identifier of the transaction that inserted the rows and it will be stored in the row data page if the transaction is confirmed. Every subsequent change to the rows will update the TID.
125
117
126
-
Use the `DBCC PAGE` command to inspect the TID of page 2456.
118
+
Now let's make a change on the row identified by the PacketID value 2 and before confirming the transaction let's repeat again the query on the DMV.
127
119
128
120
```sql
129
-
-- Enable trace flag for DBCC PAGE output
130
-
DBCC TRACEON(3604);
131
-
GO
121
+
BEGIN TRANSACTION;
132
122
133
-
DBCC PAGE ('OptimizedLocking', 1, 2456, 3);
134
-
```
123
+
UPDATE
124
+
t
125
+
SET
126
+
t.Device='Something updated'
127
+
FROM
128
+
dbo.TelemetryPacketAS t
129
+
WHERE
130
+
t.PacketID=2;
135
131
136
-
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.
TID 985 represents the identifier of the transaction that inserted the rows; every subsequent change to the table rows will update the TID.
149
+
Even for the `UPDATE` command, the resource_description column displays the TID of the transaction that is modifying the row. If the transaction is confirmed, the TID will be stored in the data page of the row itself.
150
150
151
151
<aname=disclaimers></a>
152
152
## Disclaimers
153
153
154
154
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.
155
155
156
-
> **Note:** The `DBCC PAGE` command is undocumented and intended for troubleshooting and diagnostic purposes only. It should not be used in production environments without proper understanding and testing.
0 commit comments