DBAtlas · Database operations atlas

Diagnose tempdb contention and space usage

SQL ServerPerformance

Command

-- 1) Current tempdb page latch waits (2:1:1 PFS, 2:1:2 GAM, 2:1:3 SGAM):
SELECT wt.session_id, wt.wait_type, wt.wait_duration_ms, wt.resource_description
FROM   sys.dm_os_waiting_tasks wt
WHERE  wt.wait_type LIKE N'PAGELATCH[_]%'
  AND  wt.resource_description LIKE N'2:%';

-- 2) Sessions using the most tempdb (MB):
SELECT TOP (20) su.session_id, s.login_name, s.program_name,
       (su.user_objects_alloc_page_count - su.user_objects_dealloc_page_count) * 8 / 1024         AS user_obj_mb,
       (su.internal_objects_alloc_page_count - su.internal_objects_dealloc_page_count) * 8 / 1024 AS internal_obj_mb
FROM   sys.dm_db_session_space_usage su
  JOIN sys.dm_exec_sessions s ON s.session_id = su.session_id
WHERE  s.is_user_process = 1
ORDER BY su.user_objects_alloc_page_count + su.internal_objects_alloc_page_count
       - su.user_objects_dealloc_page_count - su.internal_objects_dealloc_page_count DESC;

-- 3) Overall breakdown and version store:
SELECT SUM(unallocated_extent_page_count) * 8 / 1024       AS free_mb,
       SUM(user_object_reserved_page_count) * 8 / 1024     AS user_obj_mb,
       SUM(internal_object_reserved_page_count) * 8 / 1024 AS internal_obj_mb,
       SUM(version_store_reserved_page_count) * 8 / 1024   AS version_store_mb
FROM   tempdb.sys.dm_db_file_space_usage;

SELECT DB_NAME(database_id) AS db, reserved_page_count * 8 / 1024 AS version_store_mb
FROM   sys.dm_tran_version_store_space_usage
ORDER BY reserved_page_count DESC;

-- 4) File count / equal sizing and memory-optimized metadata:
SELECT name, type_desc, size * 8 / 1024 AS size_mb, growth * 8 / 1024 AS growth_mb, is_percent_growth
FROM   tempdb.sys.database_files;
SELECT SERVERPROPERTY('IsTempdbMetadataMemoryOptimized') AS tempdb_metadata_memopt;

What it does

Shows PAGELATCH waits on tempdb allocation pages (PFS/GAM/SGAM) and system tables, the sessions using the most tempdb (temp tables vs. sort/hash spills), which database is inflating the version store, and the file configuration.

When to use it

When PAGELATCH_UP/EX waits rise under heavy temp table workloads, when tempdb fills up (1105/9002), or when the version store grows.

Caution

Pages 2:1:1, 2:1:2, 2:1:3 and PFS pages at multiples of 8088 mean allocation contention: the fix is multiple equally sized data files with equal growth (one per core up to 8, then add 4 at a time). For contention on system table pages, 2019+ offers memory-optimized tempdb metadata (requires a restart). If the version store grows, find the oldest open snapshot transaction; shrinking tempdb is not a fix. Read-only query; safe.

Open in DBAtlas →

Related