Hello Amelia,
Thank you for the query. It was very informative. I read the Microsoft official article regarding the transaction log architecture on the link below:
https://learn.microsoft.com/en-us/sql/relational-databases/sql-server-transaction-log-architecture-and-management-guide?view=sql-server-ver15
I also slightly modified your query to include log files used spaces below:
drop table if exists #temp
create table #temp (name nvarchar(128), [LOG Space Used] float)
EXEC sp_MSforeachdb 'use ? insert into #temp select DB_NAME(),FILEPROPERTY(Name , ''SpaceUsed'') /128.0 from sys.database_files where file_id = 2'
SELECT s.name AS 'Database Name',
[LOG Space Used],
COUNT(li.database_id) AS 'VLF Count',
SUM(li.vlf_size_mb) AS 'VLF Size (MB)',
SUM(CAST(li.vlf_active AS INT)) AS 'Active VLF',
SUM(li.vlf_active*li.vlf_size_mb) AS 'Active VLF Size (MB)',
COUNT(li.database_id)-SUM(CAST(li.vlf_active AS INT)) AS 'Inactive VLF',
SUM(li.vlf_size_mb)-SUM(li.vlf_active*li.vlf_size_mb) AS 'Inactive VLF Size (MB)'
FROM sys.databases s
CROSS APPLY sys.dm_db_log_info(s.database_id) li
join #temp dbf
on s.name = dbf.name
GROUP BY s.name, [LOG Space Used]
ORDER BY [LOG Space Used] DESC;
Then I executed the query and got the following result:
As you see the Active VLF Size and LOG used space are slightly relatively correlated. Then I performed some DML operations on the StackOverflow2010 database to fill part of the physical log file and this following new result was achieved:
As you see the used space has remarkably increased, but the Active VLF size remained unchanged. Then I performed a LOG backup to truncate the log but nothing truncated. I guess it was because the used space did not exceed the active VLF size.
With continuing the DML and exceeding the previous active VLF size the following result acquired:
After another LOG backup the following result showed up:
The LOG truncated! This is the "SELECT [Transaction Name],* FROM fn_dblog(NULL, NULL) WHERE [Transaction Name] is not null" result afterwards:
Eventually, if I am to conclude, I would say that the last LOG used space associates with the record of the LOG backup and some other factors pertain to what Microsoft has designed behind the scenes.