Msg 1105, Level 17, State 2
Could not allocate space for object 'dbo.SORT temporary run storage' in database 'tempdb'
because the 'PRIMARY' filegroup is full.
Or the monitoring alert says tempdb's drive is at 98%, and restarting SQL Server "fixes" it until next week. tempdb is shared by every database on the instance, so when it fills up, everything fails. Here's how to find out what's using it and stop it happening again.
What lives in tempdb
- User objects:
#temptables, table variables and table-valued function results. - Internal objects: sort and hash spills, spools, cursors,
DBCC CHECKDBwork, and intermediate results of big queries. - The version store: row versions for RCSI / snapshot isolation, online index rebuilds, triggers (the
inserted/deletedtables) and MARS. (With Accelerated Database Recovery, the database's own persistent version store is used instead.)
Who is using it right now?
-- Overall breakdown
SELECT SUM(user_object_reserved_page_count) * 8 / 1024 AS user_objects_mb,
SUM(internal_object_reserved_page_count) * 8 / 1024 AS internal_objects_mb,
SUM(version_store_reserved_page_count) * 8 / 1024 AS version_store_mb,
SUM(unallocated_extent_page_count) * 8 free_mb
tempdb.sys.dm_db_file_space_usage;
s.session_id, s.login_name, s.program_name,
(su.user_objects_alloc_page_count su.internal_objects_alloc_page_count
ISNULL(tu.alloc, )) allocated_mb
sys.dm_db_session_space_usage su
sys.dm_exec_sessions s s.session_id su.session_id
( session_id,
(user_objects_alloc_page_count internal_objects_alloc_page_count) alloc
sys.dm_db_task_space_usage session_id) tu tu.session_id su.session_id
allocated_mb ;
