views:

67

answers:

2

I'm trying to efficiently determine if a log backup will contain any data.

The best I have come up with is the following:

DECLARE @last_lsn numeric(25,0)
SELECT @last_lsn = last_log_backup_lsn
    FROM sys.database_recovery_status WHERE database_id = DB_ID()
SELECT TOP 1 [Current LSN] FROM ::fn_dblog(@last_lsn, NULL)

The problem is when there are no transactions since the last backup, fn_dblog throws error 9003 with severity 20(!!) and logs it to the ERRORLOG file and event log. That makes me nervous -- I wish it just returned no records.

FYI, the reason I care is I have hundreds of small databases that can have activity at any time of day, but are typically used 8 hours/day. That means 2/3 of my log backups are empty. Those extra thousands of files can have a measurable impact on the time required for both off-site backup and recovering from a disaster.

A: 

If I run

SELECT [Current LSN] FROM ::fn_dblog(null, NULL)

It seems to return my current LSN at the top that matches the last log backup.

way0utwest
The first [Current LSN] seems to be the same as last_log_backup_lsn after a CHECKPOINT. But it can be an earlier record before the checkpoint is run. I'm not sure if that can be used to get a reliable result.
Ted Pitts
A: 

What happens if you change the select from ::fn_dblog to a count(*)? Does that eliminate the error?

If not, maybe select the log records into a temp table (top 100 from ::fn_dblog(null, NULL), ordering by a date, if there is one) and then query that.

GilaMonster
count(*) still throws the error. The problem seems to be passing a non-existant lsn as a parameter.Interesting thought to just look at the first 100 records. Selecting MAX([Current LSN]) from the entire log would get the info needed, but it can be a very expensive calculation to run through the entire active log. Is there a reason to believe last_log_backup_lsn would reliably be found in the first few records?
Ted Pitts
Depends what's been run since the last log backup. Could be a few records, could be thousands or more. Not sure there's a good way to do this.
GilaMonster