// SQL snippet

Find the sessions consuming tempdb space in SQL Server

tempdb fills up at the worst moment. This shows which session is holding or spilling the pages right now.

Main statement
SQL Server
还支持
PostgreSQL

The task

tempdb holds user objects (temp tables, table variables) and internal objects (sort runs, hash join and hash aggregate work files, spools) that SQL Server tracks per session, plus the version store for snapshot isolation and online index builds, which is shared and not charged to any session. When tempdb fills, the session with the largest outstanding internal allocation is usually the spilling query to chase.

The snippet

Written for SQL Server. Versions for PostgreSQL are under Variations.

SQL Server
WITH running AS (
    SELECT session_id,
           SUM(user_objects_alloc_page_count - user_objects_dealloc_page_count) AS user_pages,
           SUM(internal_objects_alloc_page_count - internal_objects_dealloc_page_count) AS internal_pages
    FROM sys.dm_db_task_space_usage
    GROUP BY session_id
)
SELECT s.session_id, s.login_name, s.program_name,
       r.command, r.status,
       (su.user_objects_alloc_page_count - su.user_objects_dealloc_page_count
          + ISNULL(t.user_pages, 0)) / 128 AS user_objects_mb,
       (su.internal_objects_alloc_page_count - su.internal_objects_dealloc_page_count
          + ISNULL(t.internal_pages, 0)) / 128 AS internal_objects_mb,
       ISNULL(t.internal_pages, 0) / 128 AS running_request_internal_mb
FROM sys.dm_db_session_space_usage su
JOIN sys.dm_exec_sessions s ON s.session_id = su.session_id
LEFT JOIN running t ON t.session_id = su.session_id
LEFT JOIN sys.dm_exec_requests r ON r.session_id = su.session_id
WHERE s.is_user_process = 1
ORDER BY internal_objects_mb DESC, user_objects_mb DESC;

工作原理

Page counts divide by 128 to become megabytes. sys.dm_db_session_space_usage holds what each session's finished tasks allocated and have not freed; it does not include the request running right now. sys.dm_db_task_space_usage covers the running tasks, one row per task, so a parallel query has several rows and the CTE sums them per session. Adding the two gives what each session holds at this moment, and running_request_internal_mb isolates the live spiller during an incident. Internal objects are work tables and work files for sorts, hashes, spools and index builds; if they dominate, a query is spilling, and better estimates or indexes fix it more cheaply than a bigger tempdb. User objects are temp tables and table variables that are still allocated.

Variations

PostgreSQL: log temp file spills over 100 MB automatically

PostgreSQL
ALTER SYSTEM SET log_temp_files = 102400;
SELECT pg_reload_conf();

Questions that come up

tempdb is nearly full. What can I do without restarting?

Find the session with the largest allocation here; killing a runaway query frees its pages as soon as its rollback completes. If this view shows little but tempdb is still full, check the version store with sys.dm_tran_version_store_space_usage: it cannot shrink while an old snapshot transaction is open, and sys.dm_tran_active_snapshot_database_transactions names the oldest. Adding a tempdb data file on a volume with free space works online and buys time.

Do temp tables count against this view?

Yes. User object pages cover the temp tables and table variables a session has created and not yet dropped. A local #temp table lives until it is dropped or the session ends, so a pooled connection that never drops its temp tables keeps holding them; that shows here as a large user_objects_mb on an idle session.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.