3

I am comparing how PostgreSQL and SQL Server protect data pages from torn writes.

PostgreSQL has a feature called full_page_writes. On the first modification to a page after a checkpoint, PostgreSQL writes the entire 8 KB page image into the WAL. This allows PostgreSQL to fully restore the page even if the data file contains a torn or partially written page caused by a power loss or crash.

My question is:

👉 Does SQL Server have a similar mechanism to recover from torn page writes?

In other words:

How does SQL Server handle torn or partially written pages?

Does SQL Server store full page images in the transaction log?

Is torn page detection or recovery handled differently in SQL Server compared to PostgreSQL?

I know SQL Server has page checksums and torn-page detection, but I want to understand whether the storage engine logs enough information (like PostgreSQL does with full-page writes) to fully reconstruct a damaged page during crash recovery

1 Answer 1

2

Postgres' WAL is analogous to SQL Server's transaction log, which is also truncated only on a checkpoint. Both PG's WAL and SQL Server's transaction log are only truncated when all those pages are written to the data files and nothing else depends upon those log files. For more info see the docs.

This allows PostgreSQL to fully restore the page even if the data file contains a torn or partially written page caused by a power loss or crash.

Only if the WAL hasn't been checkpointed yet. It's not a backup which can restore arbitrary pages, it's just a way to harden the transaction to disk quickly without writing changes to all different parts of the data files. That happens long before the page is actually written to the file, which only happens during the checkpointing process.

How does SQL Server handle torn or partially written pages?

Is torn page detection or recovery handled differently in SQL Server compared to PostgreSQL?

Similarly to Postgres, it only truncates once the full checkpoint has happened. If it crashed then it will just try to checkpoint again on restart.

Does SQL Server store full page images in the transaction log?

Not always, for small modifications it only stored the changed rows. You can see more in this article.

I know SQL Server has page checksums and torn-page detection, but I want to understand whether the storage engine logs enough information (like PostgreSQL does with full-page writes) to fully reconstruct a damaged page during crash recovery.

Checksums only tell you whether a page isn't written correctly, and only when it's read. All that will happen is an error is thrown. DBCC CHECKDB (which also does more thorough checks) can recover data from the transaction log if it's there.

Torn page detection (which covers the case of a chcekpoint going bad) will detect and redo any partial page update. So if only part of a page got updated then the checkpoint redo will only write over the rest of the page. So Postgres' full_page_writes logic isn't necessary.

4
  • 1
    Most of this seems to be irrelevant to the issue the full_page_writes is targeted to solve, of a crash during a checkpoint causing a torn write of the checkpoints page. If sqlserver is always writing full pages to the transaction log that would mediate that, but that seems like it would lead to a high level of write amplification for small writes. Commented Nov 25 at 18:16
  • In the case it only has a partial page update (where it's after a full buffer flush or restart) SQL Server will read the page out first, make the updates in memory then write it back out. If a torn write happens on checkpointing, it will be detected and only the rest of the page will be overwritten. Commented Nov 25 at 18:50
  • 1
    Also SQL Server can automatically restore a damaged page from a Always On replica that PG is unnnable to do. See msdb.dbo.supect_pages to know wich pages have been automatically repair (very rare in practice). Commented Nov 26 at 9:25
  • 1
    And finally you can repair damaged pages by a RESTORE with option PAGE command. You need to find the lastest full backup and apply it firts for the damaged pages and next all the backuped transaction logs. This feature can be used in ONLINE mode for Enterprise edition, that PG is unnable to do... See some differants behaviour between PG and SQL Server at : mssqlserver.fr/wp-content/uploads/2025/04/… Commented Nov 26 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.