Is there a way to determine the used space of a Virtual Log File?

amomen 381 Reputation points
2021-09-15T19:34:00.677+00:00

Hello everybody,

I searched through the internet and didn't find an answer to this question. Is there a query that can obtain the used space inside a single VLF?
I appreciate your assistance!

Best regards,
Ali

Developer technologies | Transact-SQL
SQL Server | Other
{count} votes

Accepted answer
  1. Erland Sommarskog 121.9K Reputation points MVP Volunteer Moderator
    2021-09-16T21:28:32.687+00:00

    I guess it could be possible to compute something with help of the output from DBCC LOGINFO and sys.dm_db_log_space_usage. All active VLFs but the current one should be full, and by taking the total size of the active VLFs and deducting the total space used, you get how much is used in that VLF.

    Is there any particular reason you want to know this?


2 additional answers

Sort by: Most helpful
  1. CathyJi-MSFT 22,396 Reputation points Microsoft External Staff
    2021-09-16T01:30:09.607+00:00

    Hi @amomen-8749,

    As far as I know, it is impossible for us to monitor the space usage of one VLF. But we can use DBCC SQLPERF(LOGSPACE) to monitor the space usage of the log file. We can also use below T-SQL to get the information about the VLF count and its size.

    SELECT [name], s.database_id,  
    COUNT(l.database_id) AS 'VLF Count',  
    SUM(vlf_size_mb) AS 'VLF Size (MB)',  
    SUM(CAST(vlf_active AS INT)) AS 'Active VLF',  
    SUM(vlf_active*vlf_size_mb) AS 'Active VLF Size (MB)',  
    COUNT(l.database_id)-SUM(CAST(vlf_active AS INT)) AS 'In-active VLF',  
    SUM(vlf_size_mb)-SUM(vlf_active*vlf_size_mb) AS 'In-active VLF Size (MB)'  
    FROM sys.databases s  
    CROSS APPLY sys.dm_db_log_info(s.database_id) l  
    GROUP BY [name], s.database_id  
    ORDER BY 'VLF Count' DESC  
    GO  
    

    132552-screenshot-2021-09-16-092902.jpg


    If the response is helpful, please click "Accept Answer" and upvote it, as this could help other community members looking for similar thread.

    1 person found this answer helpful.
    0 comments No comments

  2. CathyJi-MSFT 22,396 Reputation points Microsoft External Staff
    2021-09-23T01:54:31.577+00:00

    Hi @amomen-8749,

    Suggest you starting from MS document SQL Server Transaction Log Architecture and Management Guide as Tom mentioned.

    Q 1: When the database Transaction Log file is truncated, the truncated space will be freed up and become available for reuse. But the Transaction Log file size will not be decreased, as the truncated space will not be deallocated. On the other hand, the process of recovering the Transaction Log space by deallocating the free VLFs and returning it back to the operating system is called a Transaction Log Shrink. operation.

    Q2: Each new log record is written to the logical end of the log with an LSN that is higher than the LSN of the record before it.

    Q3: Yes.


    If the response is helpful, please click "Accept Answer" and upvote it, as this could help other community members looking for similar thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.