Every now and then I get a SQL epiphany and figure out another way of doing something. Forever, I have used DBCC SQLPERF('logspace') to get the utilization information for the database transaction logs in SQL Server. A question on the forums asked how to get the code for this DBCC command because sp_help_text wouldn't provide it. While you can get the code for a number of system stored procedures, you can't get the code for DBCC commands and Extended Stored Procedures because they are compiled unmanaged code, not TSQL.
Until today, my response has been, DBCC SQLPERF('logspace') is the only way to get what you are after. However, for whatever reason, my brain clicked two different events together and made a connection. This week while doing some consulting work, I was asked about logs filling faster than backups were scheduled and how to work to prevent the logs from filling completely. The solution which I will blog about in another post is to use a SQL Agent Alert on the Percent Log User Performance Counter that fires a SQL Agent Job to performs a Log Backup of the Database on the server. Since we can read that performance counter from SQL Server using the sys.dm_os_performance_counters DMV, then we can also use the same DMV to get the log space information for all of our databases:
SELECT instance_name AS DatabaseName,
[Data File(s) Size (KB)],
[LOG File(s) Size (KB)],
[Log File(s) Used Size (KB)],
[Percent Log Used]
FROM
(
SELECT *
FROM sys.dm_os_performance_counters
WHERE counter_name IN
(
'Data File(s) Size (KB)',
'Log File(s) Size (KB)',
'Log File(s) Used Size (KB)',
'Percent Log Used'
)
AND instance_name != '_Total'
) AS Src
PIVOT
(
MAX(cntr_value)
FOR counter_name IN
(
[Data File(s) Size (KB)],
[LOG File(s) Size (KB)],
[Log File(s) Used Size (KB)],
[Percent Log Used]
)
) AS pvt
No sooner had I posted this solution than I got the response that this has to work for SQL 2000 as well. So I had to boot up my SQL 2000 instance on my laptop to actually see how to work that out, and the solution isn't that much different. The sysperfinfo system view can be used against SQL Server 2000, 2005, and 2008 to get the information using an older "pivot" methodology as follows:
SELECT instance_name AS 'Database Name',
MAX(CASE
WHEN counter_name = 'Data File(s) Size (KB)'
THEN cntr_value
ELSE 0
END) AS 'Data File(s) Size (KB)',
MAX(CASE
WHEN counter_name = 'Log File(s) Size (KB)'
THEN cntr_value
ELSE 0
END) AS 'Log File(s) Size (KB)',
MAX(CASE
WHEN counter_name = 'Log File(s) Used Size (KB)'
THEN cntr_value
ELSE 0
END) AS 'Log File(s) Used Size (KB)',
MAX(CASE
WHEN counter_name = 'Percent Log Used'
THEN cntr_value
ELSE 0
END) AS 'Percent Log Used'
FROM sysperfinfo
WHERE counter_name IN
(
'Data File(s) Size (KB)',
'Log File(s) Size (KB)',
'Log File(s) Used Size (KB)',
'Percent Log Used'
)
AND instance_name != '_total'
GROUP BY instance_name
As a warning, the sysperfinfo view is a deprecated feature in 2005 and 2008 and is only carried forward for backwards code compatibility. You can expect that it will be removed in a future version of SQL so if you have 2005 installed, it is better to use the DMV's for this.