sqlinsight-pro/SQL Scripts/_collectors/TempdbCollector.sql
Søren Eggert Lundsteen Olsen 2284e7f082 SQL Scripts: one reviewable script per rule
A DBA should be able to review every statement SQLInsight sends to a
monitored server before granting it access. "SQL Scripts/<rule-id>.sql"
holds, per rule (82 built-in, 16 community), a Danish header (what and
why, how C# judges the data, thresholds with config keys, severity,
exact GRANTs, versions, collector file and member) and the collector's
SQL verbatim, every version variant as its own batch.
"_collectors/<Collector>.sql" holds each collector's queries once.

The only statement that is not strictly read-only is documented in the
scripts: the deadlock collector's session temp table (#system_health).

Inline SQL in the collectors moved to internal constants and Build*Sql
methods (no text changed), so SqlScriptTests can check that the scripts
still contain the SQL the code sends, that every rule has a script, that
every collector query is registered, and that nothing writes.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 13:20:06 +02:00

67 lines
4.5 KiB
Transact-SQL

-- ===================================================================================================================
-- Collector: TempdbCollector ("tempdb")
-- ===================================================================================================================
-- Hvad: tempdb: fri plads, version store, bruger- og interne objekter, de tungeste sessioner, ældste
-- snapshot-transaktion og hvilke databaser der laver rækkeversioner.
-- Fil: src/SQLInsight.Collectors/Instance/TempdbCollector.cs
-- Interval: 5 min som standard (SQLInsight:CollectorIntervals:tempdb)
-- Versioner: SQL Server 2005 og nyere
--
-- Rettigheder til agentens login (se db/permissions/grant-least-privilege.sql; [agent] = agentens login):
-- GRANT VIEW SERVER STATE TO [agent]; -- SQL Server 2005-2019
-- GRANT VIEW SERVER PERFORMANCE STATE TO [agent]; -- SQL Server 2022+ (erstatter VIEW SERVER STATE)
-- GRANT VIEW SERVER SECURITY STATE TO [agent]; -- SQL Server 2022+
--
-- Bruges af: SQLI-TEMP-001, SQLI-TEMP-002, SQLI-TEMP-003
-- Read-only: Ja: kun SELECT samt SET-sessionsindstillinger. Intet ændrer data, indstillinger eller objekter.
-- Kørsel: Kør i SSMS som agentens login. Hver variant er sin egen batch (GO); en variant for en anden SQL Server-version
-- end din kan fejle - det er forventet. Agenten kører altid med sessionsindstillingerne i _collectors/InstanceProbe.sql.
-- ===================================================================================================================
-- Variant: SQL Server 2005 og nyere
-- Kilde: TempdbCollector.SpaceSql
-- Kode: src/SQLInsight.Collectors/Instance/TempdbCollector.cs : TempdbCollector.SpaceSql
SELECT SUM(CAST(u.unallocated_extent_page_count AS bigint)), SUM(CAST(u.version_store_reserved_page_count AS bigint)),
SUM(CAST(u.user_object_reserved_page_count AS bigint)), SUM(CAST(u.internal_object_reserved_page_count AS bigint)),
(SELECT SUM(CAST(f.size AS bigint)) FROM tempdb.sys.database_files f WHERE f.type = 0),
(SELECT MAX(CASE WHEN f.growth > 0 AND (f.max_size = -1 OR f.size < f.max_size) THEN 1 ELSE 0 END) FROM tempdb.sys.database_files f WHERE f.type = 0),
(SELECT MAX(CAST(p.cntr_value AS bigint)) FROM sys.dm_os_performance_counters p
WHERE p.counter_name = N'Version Store Size (KB)' AND p.object_name LIKE N'%Transactions%')
FROM tempdb.sys.dm_db_file_space_usage u
GO
-- Variant: SQL Server 2005 og nyere
-- Kilde: TempdbCollector.SessionsSql
-- Kode: src/SQLInsight.Collectors/Instance/TempdbCollector.cs : TempdbCollector.SessionsSql
SELECT TOP 5 u.session_id, DATEADD(minute, DATEDIFF(minute, GETDATE(), GETUTCDATE()), s.login_time), u.pages, s.login_name, s.program_name, s.host_name, s.status
FROM (SELECT a.session_id, SUM(a.uo_alloc - a.uo_dealloc + a.io_alloc - a.io_dealloc) AS pages
FROM (SELECT session_id, CAST(user_objects_alloc_page_count AS bigint) AS uo_alloc, CAST(user_objects_dealloc_page_count AS bigint) AS uo_dealloc,
CAST(internal_objects_alloc_page_count AS bigint) AS io_alloc, CAST(internal_objects_dealloc_page_count AS bigint) AS io_dealloc
FROM sys.dm_db_session_space_usage
UNION ALL
SELECT session_id, CAST(user_objects_alloc_page_count AS bigint), CAST(user_objects_dealloc_page_count AS bigint),
CAST(internal_objects_alloc_page_count AS bigint), CAST(internal_objects_dealloc_page_count AS bigint)
FROM sys.dm_db_task_space_usage) a
GROUP BY a.session_id) u
JOIN sys.dm_exec_sessions s ON s.session_id = u.session_id
WHERE s.is_user_process = 1 AND u.pages > 0
ORDER BY u.pages DESC
GO
-- Variant: SQL Server 2005 og nyere
-- Kilde: TempdbCollector.OldestSnapshotSql
-- Kode: src/SQLInsight.Collectors/Instance/TempdbCollector.cs : TempdbCollector.OldestSnapshotSql
SELECT TOP 1 t.session_id, t.elapsed_time_seconds, s.login_name, s.program_name
FROM sys.dm_tran_active_snapshot_database_transactions t
LEFT JOIN sys.dm_exec_sessions s ON s.session_id = t.session_id
ORDER BY t.elapsed_time_seconds DESC
GO
-- Variant: SQL Server 2005 og nyere, kun når version store er over halvdelen af grænsen, højst én gang i timen
-- Kilde: TempdbCollector.GeneratorsSql
-- Kode: src/SQLInsight.Collectors/Instance/TempdbCollector.cs : TempdbCollector.GeneratorsSql
SELECT TOP 10 DB_NAME(database_id), SUM(CAST(aggregated_record_length_in_bytes AS bigint))
FROM sys.dm_tran_top_version_generators
GROUP BY database_id
ORDER BY SUM(CAST(aggregated_record_length_in_bytes AS bigint)) DESC
GO