I was using the fn_dblog command and was not able to get why the log reserve was negative.
Sql-server – Why is log reserve negative in the SQL Server compensation log when using fn_dblog command
database-internalssql servert-sql
Related Question
- Sql-server – How to demonstrate Transaction Log activity
- Sql-server – SQL Server 2012 SQL Log “the log file is not using Unicode format”:
- Sql-server – Only do log backup if necessary in SQL Server
- Sql-server – Microsoft SQL Server || Log Truncation
- Sql-server – routine in MySQL similar to fn_dblog in SQL-server
- SQL Server 2017 – Safe Log Backups on Primary with Log Shipping or Availability Groups
- Sql-server – Why do we have undocumented and unsupported functions in SQL Server
Best Answer
The function is technically undocumented, so I can't find any official word on this. However, my understanding is that those "compensation" rows are related to a rollback. A rollback is essentially erasing some previous log records, so it frees up some space in the transaction log. The negative values are saying "give this much free space back to the log file."
You can observe this by performing a demo like this:
That sets up a database in the SIMPLE recovery model with one table in it, then clears the log (with a checkpoint). Here's the baseline transaction log usage:
Now I'll run an insert inside a transaction:
We've gone from 5.5% usage to 13%:
And now we'll roll that insert back:
And we've gone from 13% down to 8.9%.
You might notice that the compensations don't free up all the log space used by the insert. In my test, the inserts reserve 178 bytes, while the rollbacks free up -74 bytes.
I think this is because the log reservations are extra big in case of page splits, which don't get rolled back. But that's just an educated guess.