improve.dk
Just another mindless drone looking for the perfect stack
posts - 227, comments - 489

Converting page pointers into a human readable format

Written on April 28, 2011 by Mark S. Rasmussen in SQL Server: Internals, SQL Server: Tricks

I often like to spend my weekends perusing the sys.system_internals_allocation_units table, looking for the remnants of Frodo and his crew. In the sys.system_internals_allocation_units there are several references to relevant pages:

select
	first_page,
	root_page,
	first_iam_page
from
	sys.system_internals_allocation_units

image

Once you get used to reading these pointers, it becomes rather trivial – byte swap the last two pointers to get the file ID (0 or 1 in all of the above rows), and byte swap the first four bytes to get the page ID. To make it a bit more easier for myself and for those who do not read HEX natively, I’ve made a simple function to convert the pointers into a more easily read format.

create function getPageLocationFromPointer
(
	@Pointer binary(6)
)
returns varchar(17)
begin
	
	return 
		'(' + 
		cast(
			cast(
				substring(@Pointer, 6, 1) +
				substring(@Pointer, 5, 1)
				as smallint
			) as varchar
		) +
		':' +
		cast(
			cast(
				substring(@Pointer, 4, 1) +
				substring(@Pointer, 3, 1) +
				substring(@Pointer, 2, 1) +
				substring(@Pointer, 1, 1)
				as int
			) as varchar
		) +
		')'
	
end

While not beautiful, it is rather simple. The result:

select
	first_page,
	dbo.getPageLocationFromPointer(first_page) as first_page_location,
	root_page,
	dbo.getPageLocationFromPointer(root_page) as root_page_location,
	first_iam_page,
	dbo.getPageLocationFromPointer(first_iam_page) as first_iam_page_location
from
	sys.system_internals_allocation_units

image

Feedback

Gravatar

Martin Cairns wrote on 8/11/2011 11:19 AM

There is an undocumented builtin function called sys.fn_PhysLocFormatter which does a similar job, it also included the slot number. This goes with the %%phyloc%% virtual column which gives the row location.

www.sqlskills.com/.../SQL-Server-2008-New-(undocumented)-physical-row-locator-function.aspx
Gravatar

Mark S. Rasmussen wrote on 8/15/2011 12:27 AM

@Martin Cairns
Indeed the fn_PhysLocFormatter function can be very useful at times. However, in this case I'm really not interested in the slot number as it doesn't make sense in this case.

In this example I'm solely looking for the root/first page pointers in the system_internals_allocation_units table for the purposes of parsing those pages. As such, there's no "first" slot per se, just a first page. On that page however, there is a first slot. Problem is, without a reference to that first page, we won't know what data is on there, and we won't be able to query it.

We could query a clustered index and sort by the clustered key, in which case we'd get a reference to the first page, from the first row. Won't work for a heap though - in that case we really do need a pointer to the first IAM page :)

Post Comment

Name  
Email
Url
Comment
Please add 2 and 5 and type the answer here: