Torn Page Detection

Torn page detection ensures database consistency. In SQL Server 7.0, pages are 8 KB, and Windows NT Server performs I/O in 512-byte segments. This discrepancy makes it possible for a page to be written partially, which could happen if there is a power failure or another problem between the time when the first 512-byte segment is written and the 8 KB of I/O is complete.

If the first 512-byte segment is written, it may appear that the page has been updated when it was not. (The timestamp for the page is in the header, the first 96 bytes of the page.) There are several ways to deal with this problem. You can use battery-backed cached I/O devices that guarantee all-or-nothing I/O. If you have one of these systems, torn page detection is unnecessary.

SQL Server can detect incomplete I/O by creating a mask of bits, one bit from each segment in the page. Every time a page is written, the bit is flipped from its previous state (as it was on disk), and the actual state is saved in the page header. If a page is read and a bit is in the wrong state, it indicates that an I/O did not complete and there is a torn page. This mechanism is less expensive than computing a checksum.

You can set torn page detection on and off because the page header is marked when the bit is flipped. If torn page detection is set on and off, then the state in the pages that have been flipped is observed and corrected the next time they are read.