DBCC CHECKDB – Object being checked

I was struggling to find which object the CHECKDB was processing. If there is a latch, we can get the waitresource and find out, but if there isn’t, it has shown to be quite difficult.I have tried 2 jobs in parallel writing to the same file, one writes a timestamp every 5 seconds the other runs CHECKDB with ALLERRORMSG. What happens is that the CHECKDB just gets written to the file when all the process is finished.
I did some research also using XE and Profiler, but I haven’t got anything. Finally, I came up with this script. It will get the most recent page in buffer for the given db id, which we use the snapshot DB id:

DECLARE @snapshot_dbid BIGINT = 15
DECLARE @schema_name NVARCHAR(200)
DECLARE @object_name NVARCHAR(200)

DECLARE @object_id INT
DECLARE @page_id INT
DECLARE @file_id INT
DECLARE @dbcc_page_output table (

parent_obj VARCHAR (MAX)
, obj VARCHAR(MAX)
, field varchar(max)
, value varchar(max)

)

SELECT TOP 1
@page_id = page_id
, @file_id = file_id
FROM sys.dm_os_buffer_descriptors
WHERE database_id = @snapshot_dbid
ORDER BY
allocation_unit_id DESC
, page_id DESC

INSERT INTO @dbcc_page_output
EXEC (‘DBCC PAGE (‘ + @snapshot_dbid+ ‘, ‘ + @file_id + ‘, ‘ + @page_id + ‘, 3 ) WITH TABLERESULTS’)

SELECT @object_id = value
FROM @dbcc_page_output
WHERE field = ‘Metadata: ObjectId’

SELECT @schema_name = SCHEMA_NAME(schema_id)
, @object_name = name
FROM sys.objects
WHERE object_id = @object_id

SELECT @schema_name + ‘.’ + @object_name as OBJECT_being_checked

SELECT field as [column]
, value
FROM @dbcc_page_output
WHERE obj LIKE ‘Slot 0 Column%’

Any questions, please let me know. 🙂