{"id":2867,"date":"2015-12-02T07:23:47","date_gmt":"2015-12-02T12:23:47","guid":{"rendered":"https:\/\/academy.sqlbak.com\/?p=2867"},"modified":"2023-10-17T05:23:54","modified_gmt":"2023-10-17T09:23:54","slug":"log-sequence-number","status":"publish","type":"post","link":"https:\/\/academy.sqlbak.com\/log-sequence-number\/","title":{"rendered":"Log Sequence Number"},"content":{"rendered":"
Every record in SQL Server transaction log has its own log sequence number or LSN. Log sequence numbers are ordered in such a way that changes related to the record with greater LSN occur after the changes related to records with smaller LSN. Each backup that is set in SQL Server has FirstLSN<\/em> and LastLSN<\/em> values. To find out all backup’s LSN values\u00a0you need to look at its header using the following syntax:<\/p>\n <\/p>\n To understand what these LSNs mean in\u00a0each database backup let’s consider a\u00a0backup sequence created (the database uses full or bulk-logged recovery model) using the following commands:<\/p>\n This shows that\u00a0we have created three different types of database backups: Full backup, Differential backup and Transaction Log backup. Now let’s check their\u00a0LSNs and compare them:<\/p>\n These commands produce the following results: where:<\/p>\n The DatabaseBackupLSN <\/em>of the first full backup is always equal to zero:<\/p>\n The FirstLSN<\/em> of the first full backup is the same as the CheckpointLSN<\/em><\/p>\n Full database backup is the main type on which all other backups (especially differential backups) are dependent. In the restore sequence, a differential backup is always restored after the full backup. In order to determine the full backup to which the differential backup relates, you need to check the values of DatabaseBackupLSN<\/em> of differential backup and CheckpointLSN<\/em> of the full backup – their values should be identical.:\u00a0<\/p>\n <\/p>\n But what if there is no differential backup, and there is a need to restore the database with the full and transaction log backups? To find out which transaction log backup should be restored right\u00a0after the\u00a0full backup is restored\u00a0take a look at the LastLSN<\/em> of the full backup and the FirstLSN<\/em> and the LastLSN<\/em> of the transaction log backup. The LastLSN<\/em> of the full backup should be between the FirstLSN<\/em> and the LastLSN<\/em> of the transaction log backup.\u00a0<\/p>\n Likewise, in order to find out which transaction log backup should\u00a0be restored after the differential backup you need to pay attention to the LastLSN<\/em> of the differential backup and the FirstLSN<\/em> and the LastLSN<\/em> of the transaction log backup. The LastLSN<\/em> of the differential backup should be between FirstLSN<\/em> and LastLSN<\/em> of the transaction log backup followed right after it:\u00a0The CheckpointLSN<\/em> of the differential backup maps to the CheckpointLSN<\/em> of the first transaction log backup created after this differential backup:<\/p>\n <\/p>\n First transaction log backup has the same FirstLSN<\/em> and CheckpointLSN<\/em> as\u00a0the first full database backup’s CheckpointLSN<\/em>: Note that LastLSN<\/em> value of the previous transaction log backup is equal to LSN of\u00a0the first log record stored in the following transaction log backup, as shown on\u00a0the picture above.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":" Every record in SQL Server transaction log has its own log sequence number or LSN. Log sequence numbers are ordered in such a way that changes related to the record with greater LSN occur after the changes related to records with smaller LSN. Each backup that is set in SQL Server has FirstLSN and LastLSN […]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[157],"tags":[],"yoast_head":"\nRESTORE HEADERONLY FROM DISK<\/span> = 'full.bak'\u00a0<\/span><\/pre>\n
BACKUP DATABASE<\/span> your_database<\/em> TO DISK<\/span> = 'full.bak'<\/span>\r\nBACKUP<\/span> LOG<\/span> your_database<\/em> TO DISK<\/span> = 'log.bak'<\/span>\r\nBACKUP<\/span> LOG<\/span> your_database<\/em> TO DISK <\/span>= 'log1.bak'<\/span>\r\nBACKUP DATABASE<\/span> your_database<\/em> TO DISK<\/span> = 'diff.bak'<\/span> WITH DIFFERENTIAL\r\nBACKUP<\/span> LOG<\/span> your_database<\/em> TO DISK<\/span> = 'log2.bak'<\/span>\r\nBACKUP <\/span>LOG<\/span> your_database<\/em> TO DISK <\/span>= 'log3.bak'<\/span>\r\nBACKUP DATABASE<\/span> your_database<\/em> TO DISK<\/span> = 'diff1.bak'<\/span> WITH DIFFERENTIAL\r\nBACKUP DATABASE <\/span>your_database<\/em> TO DISK<\/span> = 'full1.bak'<\/span>\r\nBACKUP<\/span> LOG<\/span> your_database<\/em> TO DISK<\/span> = 'log4.bak'<\/span><\/pre>\n
RESTORE HEADERONLY FROM DISK<\/span> = 'full.bak'<\/span> \r\nRESTORE HEADERONLY FROM DISK<\/span> = 'log.bak'<\/span> \r\nRESTORE HEADERONLY FROM DISK<\/span> = 'log1.bak'<\/span> \r\nRESTORE HEADERONLY FROM DISK<\/span> = 'diff.bak'<\/span> \r\nRESTORE HEADERONLY FROM DISK<\/span> = 'log2.bak'<\/span> \r\nRESTORE HEADERONLY FROM DISK<\/span> = 'log3.bak'\r\n<\/span>RESTORE HEADERONLY FROM DISK<\/span> = 'diff1.bak'<\/span> \r\nRESTORE HEADERONLY FROM DISK<\/span> = 'full1.bak'\r\n<\/span>RESTORE HEADERONLY FROM DISK<\/span> = 'log4.bak'\u00a0<\/span><\/pre>\n
\n<\/span><\/p>\n\n
How do\u00a0FirstLSN, LastLSN,\u00a0CheckpointLSN and DatabaseBackupLSN Relate to Each Other<\/h2>\n
\nFor a transaction log backup, the LastLSN<\/em> value is the same as the FirstLSN<\/em> of the next transaction log backup:
\n<\/p>\n