Torn Pages: How MySQL, PostgreSQL and Oracle Deal With Half-Written Blocks

Categories: MySQL · PostgreSQL · Oracle Database · Backup and recovery · Storage

Imagine the following: Your database is busy writing a page/block to disk and, halfway through, the power goes out. When the server comes back, that page/block is half new and half old. That is a torn page/block, and every database that writes pages/blocks bigger than what the disk writes in one go has to deal with it. In this post I will use page and block interchangeably since MySQL and PostgreSQL call them pages and Oracle calls them blocks, but in essence a page or block is the smallest unit of data known and managed by the database.

MySQL, PostgreSQL and Oracle all know about this torn pages problem. But each one solves it in a very different way. MySQL writes every page twice. PostgreSQL puts a full copy of the page in the WAL. Oracle does neither, and instead counts on finding the damage and fixing it from another good copy.

In this post I break a page on purpose in each engine and show you what happens. All the MySQL and PostgreSQL output below comes from real runs on my lab box (MySQL 8.4.13 and PostgreSQL 18.7 on Ubuntu 26.04).

What a torn page really is

A torn page happens because the database and the disk don’t agree on what “one write” is. The database thinks in pages (or blocks, in Oracle words). The operating system and the disk think in much smaller units.

Look at my lab box:

$ getconf PAGESIZE
4096

$ stat -f -c 'fs block size: %S' /
fs block size: 4096

$ lsblk -d -o NAME,PHY-SEC,LOG-SEC /dev/sda
NAME PHY-SEC LOG-SEC
sda      512     512

The file system writes 4 KB at a time. The disk (a virtual disk on this VM) reports 512 byte sectors, both physical and logical. Now look at the databases:

-- PostgreSQL
postgres=# SHOW block_size;
 block_size
------------
 8192

-- MySQL
mysql> SELECT @@innodb_page_size;
16384

-- Oracle
SQL> show parameter db_block_size
NAME           TYPE    VALUE
-------------- ------- -----
db_block_size  integer 8192

So one PostgreSQL page is two file system blocks. One InnoDB page is four. The kernel and the disk only promise that each small piece lands whole. Nobody promises that all the pieces of one database page land together. If the power drops after the first two 4 KB pieces of a 16 KB InnoDB page hit the disk, you get a page where the first half is the new version and the second half is the old one.

A crash after 2 of 4 writes leaves a 16 KB InnoDB page half new, half old

The nasty part: both halves are valid on their own. The disk reports no error. Nothing looks wrong until the database reads that page and tries to make sense of it.

Two jobs: find it, then fix it

Before we go engine by engine, keep two jobs separate in your head, because people mix them up all the time.

  1. Detect. Notice that a page is broken. This is what checksums do. The database computes a number from the page bytes when it writes the page, stores it in the page, and checks it again on read. A torn page almost never matches.
  2. Repair. Get a good copy of the page back. A checksum can’t do this. It only tells you the page is bad. You need a second, clean copy of the page from somewhere.

Why can’t the redo log or WAL just fix it? Because those logs mostly store small changes, like “in page 4, at slot 7, change balance to 501”. To replay that change, you need a page that was correct before the change. A torn page is not correct, so replaying on top of it just gives you a broken page with one more change in it.

So for each engine, the question is where the clean copy comes from. The three answers are very different.

MySQL (InnoDB): write it twice

InnoDB’s answer is the doublewrite buffer. When InnoDB flushes dirty pages, it first writes them to a separate doublewrite area, in one big sequential write, and calls fsync. Only after that does it write each page to its real place in the .ibd file. If the server dies in the middle of that second write, the first copy is still safe.

During crash recovery, InnoDB checks pages. If a page in a data file fails its checksum and the doublewrite area has a good copy of it, InnoDB copies the good one over the broken one. Then it applies redo as usual.

Since MySQL 8.0.20 the doublewrite area lives in its own files, not inside the system tablespace:

mysql> SHOW VARIABLES WHERE Variable_name IN
    ->  ('innodb_page_size','innodb_doublewrite','innodb_doublewrite_files',
    ->   'innodb_doublewrite_pages','innodb_doublewrite_dir','innodb_checksum_algorithm');
+---------------------------+-------+
| Variable_name             | Value |
+---------------------------+-------+
| innodb_checksum_algorithm | crc32 |
| innodb_doublewrite        | ON    |
| innodb_doublewrite_dir    |       |
| innodb_doublewrite_files  | 2     |
| innodb_doublewrite_pages  | 128   |
| innodb_page_size          | 16384 |
+---------------------------+-------+
$ ls -l /var/lib/mysql-files/dw_on/data | grep dblwr
-rw-r----- 1 mysql mysql  4194304 Nov 28 13:57 #ib_16384_0.dblwr
-rw-r----- 1 mysql mysql 12582912 Nov 28 13:57 #ib_16384_1.dblwr

The demo: tear a page with doublewrite ON

I made a small table. All 50 rows fit in page 4 of the .ibd file, which is the root page of the clustered index.

CREATE TABLE bank.accounts (
  id      INT PRIMARY KEY,
  owner   VARCHAR(40),
  balance DECIMAL(10,2)
) ENGINE=InnoDB;

-- 50 rows, balance 1000 each

Then I did this:

  1. Saved a copy of page 4 (the “old” version).
  2. Ran UPDATE bank.accounts SET balance = balance - 500 WHERE id = 7;
  3. Waited until InnoDB flushed page 4 to disk, and killed mysqld with kill -9 right away.
  4. Built a torn page: first 8 KB from the new page, last 8 KB from the old copy, and wrote it back into the file with dd.
head -c 8192 page_new.bin >  torn.bin
tail -c 8192 page_old.bin >> torn.bin
dd if=torn.bin of=accounts.ibd bs=16384 seek=4 count=1 conv=notrunc

The page is broken now, and innochecksum agrees:

$ innochecksum --page=4 /var/lib/mysql-files/dw_on/data/bank/accounts.ibd
Fail: page 4 invalid
Exceeded the maximum allowed checksum mismatch count::0

Now start MySQL and read the error log (I used log_error_verbosity=3 so the notes show up):

[Note] [MY-012552] [InnoDB] Starting crash recovery.
[Note] [MY-012550] [InnoDB] Doing recovery: scanned up to log sequence number 19346038
[Note] [MY-013557] [InnoDB] Database page corruption or a failed file read of page
       [page id: space=2, page number=4]. Trying to recover it from the doublewrite file.
[Note] [MY-013550] [InnoDB] Recovered page [page id: space=2, page number=4]
       from the doublewrite buffer.
[Note] [MY-013911] [Server] Crash recovery finished in InnoDB engine. No attempts to
       commit, rollback or prepare any transactions.

And the data is all there, including the committed update to id 7:

mysql> SELECT id, balance FROM bank.accounts WHERE id IN (6,7,8);
+----+---------+
| id | balance |
+----+---------+
|  6 | 1000.00 |
|  7 |  500.00 |
|  8 | 1000.00 |
+----+---------+

mysql> SELECT COUNT(*), SUM(balance) FROM bank.accounts;
+----------+--------------+
| COUNT(*) | SUM(balance) |
+----------+--------------+
|       50 |     49500.00 |
+----------+--------------+

With the default log verbosity you won’t even see those two notes. The fix is silent.

Same test with doublewrite OFF

I set innodb_doublewrite=OFF, ran the exact same steps, and started MySQL:

[Note]  [MY-012552] [InnoDB] Starting crash recovery.
[ERROR] [MY-011906] [InnoDB] Database page corruption on disk or a failed file read of page
        [page id: space=2, page number=4]. You may have to recover from a backup.
[Note]  [MY-011878] [InnoDB] Uncompressed page, stored checksum in field1 3696705864,
        calculated checksums for field1: crc32 3696705864/3539929114, ...
        stored checksum in field2 1257192092, calculated checksums for field2:
        crc32 3696705864/3539929114, ...
        page LSN 0 19345728, low 4 bytes of LSN at page end 19344369, ...

Look at that checksum line. The first half of the page carries the new checksum in field1, and the tail of the page still has the old one in field2. The LSN says the same thing: 19345728 in the page header, 19344369 in the last bytes of the page. That is a torn page, written right there in the log.

The server never got to “ready for connections”. It stayed stuck in crash recovery:

$ mysql -S /run/mysqld/dw_off.sock -uroot -e "select 1"
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/dw_off.sock' (111)

Your options from here are innodb_force_recovery to dump what you can, or a restore from backup. Neither is fun.

Doublewrite is not a backup

My first try at this demo actually failed, and the failure teaches something. I waited a few seconds before killing mysqld. In that time InnoDB took a checkpoint past my update. Then I broke page 4.

Result: MySQL started fine, and the first SELECT on the table crashed the server:

[ERROR] [MY-011906] [InnoDB] Database page corruption on disk or a failed file read of page
        [page id: space=2, page number=4]. You may have to recover from a backup.
InnoDB: Page may be an index page where index id is 154
...
[ERROR] [MY-011899] [InnoDB] [FATAL] Unable to read page [page id: space=2, page number=4]
        into the buffer pool after 100 attempts. ...
[ERROR] [MY-013183] [InnoDB] Assertion failure: buf0buf.cc:4149:ib::fatal triggered thread ...
mysqld got signal 6 ;

ERROR 2013 (HY000) at line 1: Lost connection to MySQL server during query

Here is the surprise: when I killed the server, the doublewrite file still held a clean copy of page 4. I scanned the #ib_*.dblwr files to check. InnoDB never used it, because in my runs crash recovery only repaired pages that redo had to change. The checkpoint was already past my update, so redo never read page 4, and the torn page sat there until the first query hit it. Waiting for the checkpoint was enough; I didn’t need to push any other pages through the doublewrite buffer.

So the doublewrite buffer protects a write that was cut in the middle, during the crash recovery right after it. It does nothing for a page that gets damaged later by a bad disk, a bad controller or someone with dd.

Useful knobs

  • innodb_doublewrite = ON | OFF | DETECT_AND_RECOVER | DETECT_ONLY. From 8.0.30, DETECT_AND_RECOVER is the same as ON. DETECT_ONLY writes only page metadata, not the page content, so InnoDB can tell you a write was torn but can’t fix it.
  • innodb_doublewrite_dir lets you put the doublewrite files on a faster device.
  • Innodb_dblwr_pages_written and Innodb_dblwr_writes show how hard it works. In my run it was 124 pages in 3 writes, so many pages per fsync.
  • innochecksum checks pages offline. Run it on a stopped server or on a copy.

The cost is lower than “twice the I/O” because the doublewrite part is one sequential chunk with one fsync. Most people should leave it on. Turn it off only when the storage gives you real atomic 16 KB writes (for example a copy-on-write file system like ZFS, or the Fusion-io atomic write case that MySQL detects on its own).

PostgreSQL: put the whole page in the WAL

PostgreSQL has no doublewrite area. It uses full page writes instead. The rule is simple: after each checkpoint, the first time a page is changed, PostgreSQL writes the whole page into the WAL, not just the change. Later changes to the same page, until the next checkpoint, are small records again.

Why does this work? Crash recovery always starts at the last checkpoint. So for every page changed since then, the first WAL record recovery meets is a full copy of that page. Recovery writes that copy over whatever is on disk, torn or not, and then replays the small changes on top. The torn page never gets read at all.

postgres=# SHOW full_page_writes;
 full_page_writes
------------------
 on

postgres=# SHOW data_checksums;
 data_checksums
----------------
 on

See the full page image in the WAL

Same idea as the MySQL test: a table accounts with 50 rows, all in block 0. Every row started at 1000. I ran UPDATE accounts SET balance = balance + 1 and a CHECKPOINT, so the page on disk holds the 1001 version. I also turned autovacuum off, so nothing else touched the page. After a second CHECKPOINT, I updated one row and looked at the WAL with pg_waldump:

UPDATE accounts SET balance = balance - 500 WHERE id = 7;
$ pg_waldump -p $PGDATA/pg_wal -s 0/17EA420 -e 0/17EB930
rmgr: XLOG        len (rec/tot):  49/5273, tx:   0, lsn: 0/017EA420, desc: FPI_FOR_HINT ,
                  blkref #0: rel 1663/5/16401 blk 0 FPW
rmgr: Heap        len (rec/tot):  70/  70, tx: 756, lsn: 0/017EB8C0, desc: HOT_UPDATE old_xmax: 756,
                  old_off: 57, ..., new_off: 101, blkref #0: rel 1663/5/16401 blk 0
rmgr: Transaction len (rec/tot):  34/  34, tx: 756, lsn: 0/017EB908, desc: COMMIT

The actual change is 70 bytes. The full page image (FPW) is 5,273 bytes, which is the 8 KB page minus its empty middle. That is the price of torn page protection, paid once per page per checkpoint.

One detail worth knowing: this FPI is tagged FPI_FOR_HINT. With checksums on, even setting hint bits on a page (which the scan of my UPDATE did) counts as a change, so it also gets a full page image after a checkpoint. That is part of why checksums add some WAL.

You can watch the totals in pg_stat_wal:

postgres=# SELECT wal_records, wal_fpi, wal_bytes FROM pg_stat_wal;
 wal_records | wal_fpi | wal_bytes
-------------+---------+-----------
       25542 |     873 |   8240030

The demo: crash, tear, restart

Next I wrote the changed block 0 to disk without a checkpoint, with pg_buffercache_evict() from the pg_buffercache extension (PostgreSQL 17+). Then I stopped the server the rough way (pg_ctl stop -m immediate, same effect as a crash) and glued a torn block 0 together: the first 4 KB from that newer copy of the page, the last 4 KB from the older copy the checkpoint wrote.

head -c 4096 page_v2.bin >  torn.bin
tail -c 4096 page_v1.bin >> torn.bin
dd if=torn.bin of=$PGDATA/base/5/16401 bs=8192 count=1 conv=notrunc

Start it again:

LOG:  database system was not properly shut down; automatic recovery in progress
LOG:  redo starts at 0/17EA350
LOG:  invalid record length at 0/17EB930: expected at least 24, got 0
LOG:  redo done at 0/17EB908 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
LOG:  checkpoint complete: wrote 1 buffers (0.0%), wrote 3 SLRU buffers; ...
LOG:  database system is ready to accept connections

(The “invalid record length” line is normal. It is just recovery finding the end of the WAL.)

postgres=# SELECT id, balance FROM accounts WHERE id IN (6,7,8) ORDER BY id;
 id | balance
----+---------
  6 |    1001
  7 |     501
  8 |    1001

postgres=# SELECT count(*), sum(balance) FROM accounts;
 count |  sum
-------+-------
    50 | 49550

No warning, no checksum error. Redo started before the full page image, wrote the clean page over the torn one, and replayed the update. 50 rows, and 50 x 1001 - 500 = 49,550. Correct.

Same test with full_page_writes = off (checksums on)

LOG:  database system was not properly shut down; automatic recovery in progress
LOG:  redo starts at 0/178B480
LOG:  page verification failed, calculated checksum 50190 but expected 64551
CONTEXT:  WAL redo at 0/178B580 for Heap/HOT_UPDATE: old_xmax: 756, old_off: 57, ...
LOG:  invalid page in block 0 of relation "base/5/16401"
CONTEXT:  WAL redo at 0/178B580 for Heap/HOT_UPDATE: old_xmax: 756, old_off: 57, ...
FATAL:  invalid page in block 0 of relation "base/5/16401"
CONTEXT:  WAL redo at 0/178B580 for Heap/HOT_UPDATE: old_xmax: 756, old_off: 57, ...
LOG:  startup process (PID 26729) exited with exit code 1
LOG:  terminating any other active server processes
LOG:  shutting down due to startup process failure
LOG:  database system is shut down

This time there was no full page image to start from. Redo read the torn page from disk, the checksum caught it, and recovery stopped. One bad 8 KB page in one table, and the whole cluster won’t start. Notice the WAL looked different too: the FPI_FOR_HINT record was 44 bytes instead of 5,273, because it had no page image in it.

Same test with full_page_writes = off and no checksums

This is the scary one. A second cluster, created with --no-data-checksums (on PostgreSQL 18 you have to opt out), same steps, with one change: the old half of the torn page comes from the copy of block 0 taken before the +1 update, when every row was still 1000. That is what the disk holds when both updates land in memory before the page is written out again, and it makes the damage easier to see:

LOG:  database system was not properly shut down; automatic recovery in progress
LOG:  redo starts at 0/1543FC0
LOG:  invalid record length at 0/1544118: expected at least 24, got 0
LOG:  redo done at 0/15440F0 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
LOG:  database system is ready to accept connections

It started fine. No warning at all. Now look at the data:

postgres=# SELECT count(*), sum(balance) FROM accounts;
 count |  sum
-------+-------
    66 | 64515

postgres=# SELECT count(DISTINCT id) AS distinct_ids FROM accounts;
 distinct_ids
--------------
           50

postgres=# SELECT id, balance FROM accounts WHERE id IN (6,7,8) ORDER BY id;
 id | balance
----+---------
  6 |    1000
  7 |    1000
  8 |    1000

postgres=# SELECT ctid, id, left(owner,15) AS owner, left(balance::text,15) AS balance FROM accounts;
  ctid   | id |    owner    | balance
---------+----+-------------+---------
 (0,1)   |  1 | customer_1  | 1000
 ...
 (0,50)  | 50 | customer_50 | 1000
 (0,86)  | 36 | cus         | 0
 (0,87)  | 37 | customer_37 | 1001
 ...
 (0,100) | 50 | customer_50 | 1001
 (0,101) |  7 | customer_7  | 501
(66 rows)

66 rows in a 50 row table. The page header and the row pointers sit at the start of the page, so they come from the new half and point at 101 row versions. The original 50 rows sit at the end of the page, in the old half, from before either update, so nothing marks them as replaced and all 50 are visible again at 1000. Ids 37 to 50 show up a second time with 1001, because those newer versions happen to sit in the new half. Id 36 lands right on the 4 KB line and comes back as owner cus with a balance of 0. Ask for id 7 and you get 1000, so both committed updates are gone, while a full scan adds up to 64,515 instead of 49,550. And PostgreSQL never said a word. If you remember one thing from this post, make it this demo.

Checksums: on by default from PostgreSQL 18

Up to PostgreSQL 17, initdb creates clusters without checksums unless you pass --data-checksums (-k). From PostgreSQL 18, checksums are on by default and you opt out with --no-data-checksums. If you upgrade an old cluster with pg_upgrade, the checksum setting must match, so keep that in mind.

For an existing cluster, pg_checksums (PostgreSQL 12+) can turn them on. The cluster must be cleanly shut down, and it rewrites every block, so plan the downtime:

$ pg_checksums --check -D $PGDATA
pg_checksums: error: data checksums are not enabled in cluster

$ pg_checksums --enable --progress -D $PGDATA
Checksum operation completed
Files scanned:   954
Blocks scanned:  2926
Files written:  784
Blocks written: 2926
pg_checksums: syncing data directory
pg_checksums: updating control file
Checksums enabled in cluster

One catch: pg_checksums --enable computes checksums over whatever is in the blocks right now. I ran it on the cluster from the last demo, torn page and all, and pg_checksums --check afterwards reported Bad checksums: 0. The damage now carries a valid checksum. pg_amcheck does see it, before and after, so run it before you enable checksums (output trimmed; it flags every offset from 51 to 101):

$ pg_amcheck --install-missing --heapallindexed -d postgres
btree index "postgres.public.accounts_pkey":
    ERROR:  failed to find parent tuple for heap-only tuple at (0,86) in table "accounts"
heap table "postgres.public.accounts", block 0, offset 51:
    tuple data should begin at byte 24, but actually begins at byte 0 (0 attributes, no nulls)
...
heap table "postgres.public.accounts", block 0, offset 86:
    tuple is root of chain but is marked as heap-only tuple
...
heap table "postgres.public.accounts", block 0, offset 101:
    tuple is root of chain but is marked as heap-only tuple

Useful knobs

  • full_page_writes = on. Leave it on unless your storage really gives you atomic 8 KB writes (ZFS is the usual example).
  • wal_compression (lz4 or zstd from PostgreSQL 15) compresses full page images, which are most of the WAL on busy systems.
  • Longer checkpoint_timeout and bigger max_wal_size mean fewer checkpoints, so fewer “first change after a checkpoint” full page images. The trade is a longer crash recovery.
  • pg_amcheck checks tables and indexes online.
  • ignore_checksum_failure and zero_damaged_pages exist for emergencies. They let you get past a bad page by losing its data. Use them on a copy, never as a fix.

And the same warning as MySQL: full page writes only cover pages changed since the last checkpoint. A page that goes bad later needs a replica or a backup.

Oracle: catch it, then repair it from another copy

Oracle is the odd one out. In normal running it has no doublewrite area and no full block images in the redo. Redo carries change vectors, small “do this to that block” records. So, like we saw above, redo alone can’t rebuild a block whose two halves don’t match.

Oracle’s approach is: write each block with one I/O, detect a torn block the moment it’s read, and repair it from a good copy that lives somewhere else. Oracle calls a torn block a fractured block.

How Oracle detects a fractured block

Every Oracle block has a small header at the start and a 4 byte “tail” at the very end. The header holds the SCN of the last change and a sequence number. The tail holds a copy of part of that same information: the low 2 bytes of the SCN, the block type and the sequence. When Oracle reads a block, it compares head and tail. If the start of the block was written by one change and the end by another, they won’t match.

On top of that there is the block checksum, controlled by DB_BLOCK_CHECKSUM. The default is TYPICAL: DBWn computes a checksum when it writes the block, and Oracle checks it on every read. Oracle puts the cost of TYPICAL at 1 to 2 percent. FULL also checks blocks in memory before changes, at a higher cost. DB_BLOCK_CHECKING adds logical checks inside the block, and its default is FALSE.

SQL> show parameter db_block_check

NAME                TYPE    VALUE
------------------- ------- -------
db_block_checking   string  FALSE
db_block_checksum   string  TYPICAL

The demo: a fractured block in Oracle

Same test again. A small table in its own tablespace, a backup first (we’ll need it), then a torn block glued together with dd while the database is down.

SQL> CREATE TABLESPACE torn_ts DATAFILE '/u02/oradata/ORCL/torn01.dbf' SIZE 10M;
SQL> CREATE TABLE app.accounts (id NUMBER PRIMARY KEY, owner VARCHAR2(40), balance NUMBER)
     TABLESPACE torn_ts;
SQL> INSERT INTO app.accounts
     SELECT level, 'customer_'||level, 1000 FROM dual CONNECT BY level <= 50;
SQL> COMMIT;

SQL> SELECT DISTINCT dbms_rowid.rowid_relative_fno(rowid) AS file#,
                     dbms_rowid.rowid_block_number(rowid) AS block#
     FROM app.accounts;

     FILE#     BLOCK#
---------- ----------
        13        131
RMAN> BACKUP TABLESPACE torn_ts;

# save the old block, change data, checkpoint, save the new block
$ dd if=/u02/oradata/ORCL/torn01.dbf of=blk_old.bin bs=8192 skip=131 count=1
SQL> UPDATE app.accounts SET balance = balance - 500 WHERE id = 7; COMMIT;
SQL> ALTER SYSTEM CHECKPOINT;
$ dd if=/u02/oradata/ORCL/torn01.dbf of=blk_new.bin bs=8192 skip=131 count=1

SQL> SHUTDOWN IMMEDIATE

# first 4 KB new, last 4 KB old
$ head -c 4096 blk_new.bin >  torn.bin
$ tail -c 4096 blk_old.bin >> torn.bin
$ dd if=torn.bin of=/u02/oradata/ORCL/torn01.dbf bs=8192 seek=131 count=1 conv=notrunc

Start the database and read the table:

SQL> STARTUP
Database opened.

SQL> SELECT SUM(balance) FROM app.accounts;
SELECT SUM(balance) FROM app.accounts
                         *
ERROR at line 1:
ORA-01578: ORACLE data block corrupted (file # 13, block # 131)
ORA-01110: data file 13: '/u02/oradata/ORCL/torn01.dbf'

The database opens fine. Only that block is unusable, and Oracle tells you exactly which one. Now ask dbv what it thinks:

$ dbv file=/u02/oradata/ORCL/torn01.dbf blocksize=8192

DBVERIFY - Verification starting : FILE = /u02/oradata/ORCL/torn01.dbf
Page 131 is influx - most likely media corrupt
Corrupt block relative dba: 0x03400083 (file 13, block 131)
Fractured block found during dbv:
Data in bad block:
 type: 6 format: 2 rdba: 0x03400083
 last change scn: 0x0000.002a1c4f seq: 0x2 flg: 0x06
 spare3: 0x0
 consistency value in tail: 0x1b3e0601
 check value in block header: 0x8f2d
 computed block checksum: 0x6a3c

DBVERIFY - Verification complete

Total Pages Examined         : 1280
Total Pages Marked Corrupt   : 1
Total Pages Influx           : 1

Compare the two ends of the block. The header says SCN ...1c4f, seq 2. The tail says 1b3e 06 01, which is SCN ...1b3e, type 6, seq 1. If the block were whole, the tail would be 0x1c4f0602. Head from the new write, tail from the old one: fractured.

RMAN sees it too, and records it:

RMAN> VALIDATE DATAFILE 13;
...
File Status Marked Corrupt Empty Blocks Blocks Examined High SCN
---- ------ -------------- ------------ --------------- ----------
13   FAILED 0              1150         1280            2759759

SQL> SELECT file#, block#, blocks, corruption_type FROM v$database_block_corruption;

     FILE#     BLOCK#     BLOCKS CORRUPTION_TYPE
---------- ---------- ---------- ---------------
        13        131          1 FRACTURED

And now the repair. This is where the backup comes in. Block media recovery restores only that one block from the backup and rolls it forward with archived redo, while the rest of the file stays online:

RMAN> RECOVER CORRUPTION LIST;

Starting recover at 23-NOV-26
channel ORA_DISK_1: restoring block(s)
channel ORA_DISK_1: specifying block(s) to restore from backup set
restoring blocks of datafile 00013
channel ORA_DISK_1: restored block(s) from backup piece 1
starting media recovery
media recovery complete, elapsed time: 00:00:01
Finished recover at 23-NOV-26

SQL> SELECT COUNT(*), SUM(balance) FROM app.accounts;

  COUNT(*) SUM(BALANCE)
---------- ------------
        50        49500

You can also target one block directly with RECOVER DATAFILE 13 BLOCK 131;. Notice the big difference from MySQL and PostgreSQL: there, a torn page is fixed during crash recovery with what’s on the same server. In Oracle, you fix it from a copy that lives outside the block: a backup, a standby, or a mirror.

Where Oracle gets the good copy

  • RMAN block media recovery, like above. It can use backups, image copies, and flashback logs if Flashback Database is on.
  • Automatic Block Media Repair with Active Data Guard (11.2+). If the primary reads a corrupt block, it fetches a good copy from a real-time query standby, and the other way around. The user sees a short delay instead of ORA-01578. It needs the Active Data Guard license.
  • ASM mirroring. With normal or high redundancy, if a read fails the checks, the database reads the mirror copy and ASM rewrites the bad one.
  • Exadata. HARD checks and disk scrubbing find bad blocks and repair them from the mirror copies.

The one place Oracle does “full page writes”: hot backup mode

Oracle does have a feature that looks a lot like PostgreSQL’s full page writes. It shows up only in user-managed hot backups:

SQL> ALTER TABLESPACE users BEGIN BACKUP;
-- copy the data files with cp, rsync, a storage snapshot...
SQL> ALTER TABLESPACE users END BACKUP;

While a tablespace is in backup mode, the first change to each block writes the whole block into the redo. The reason is the same torn page problem, just on the read side: cp may read a block while DBWn is halfway through writing it, so the backup copy can be fractured. The full block in redo fixes it at restore time. That’s why redo volume jumps while a tablespace is in backup mode. Check redo size in v$mystat before and after one small update and you’ll see it.

RMAN doesn’t need any of this. RMAN reads the blocks itself, checks head and tail, and if a block looks fractured because DBWn was writing it, it simply reads it again until it gets a clean one.

A close cousin: lost writes

A torn write is a write that half happened. A lost write is a write the storage said was done but that never landed. The block looks perfectly valid, just old, so checksums can’t catch it. Oracle has two tools for this:

  • DB_LOST_WRITE_PROTECT (11g+), mostly useful with Data Guard. The primary logs the SCN of blocks it reads, and the standby compares them.
  • Shadow lost write protection (18c+), which needs no standby. Oracle stores the SCN of tracked blocks in a special shadow tablespace and raises an error as soon as it reads an older version:
SQL> CREATE BIGFILE TABLESPACE shadow_lwp DATAFILE '/u02/oradata/ORCL/shadow01.dbf' SIZE 1G
     LOST WRITE PROTECTION;
SQL> ALTER DATABASE ENABLE LOST WRITE PROTECTION;
SQL> ALTER TABLESPACE app_data ENABLE LOST WRITE PROTECTION;

Neither MySQL nor PostgreSQL has a built-in answer to lost writes.

Side by side

MySQL (InnoDB) PostgreSQL Oracle
Page / block size 16 KB default 8 KB 8 KB typical
Main torn write fix Doublewrite buffer Full page writes in WAL None at write time; repair from another copy
Where the clean copy lives #ib_*.dblwr files WAL, first change after each checkpoint Backup, standby, ASM mirror, flashback logs
When it repairs Crash recovery, automatic Crash recovery, automatic On read (ABMR) or by the DBA (RMAN block recovery)
Detection crc32 page checksum, header and trailer copies Data checksums: default on from PG 18, opt-in before Head/tail check plus DB_BLOCK_CHECKSUM=TYPICAL by default
Main switch innodb_doublewrite full_page_writes DB_BLOCK_CHECKSUM, DB_BLOCK_CHECKING
Main cost Extra sequential write plus fsync per flush batch Bigger WAL after every checkpoint 1 to 2 percent CPU for checksums, plus a backup or standby to repair from
If you turn it off Crash recovery can stop on a torn page Recovery fails, or silent garbage without checksums Head/tail check always runs; only checksums can be set lower
Offline check tool innochecksum pg_checksums --check, pg_amcheck dbv, RMAN VALIDATE
Lost write protection No No DB_LOST_WRITE_PROTECT, shadow lost write (18c+)
Backup-time torn reads Handled by backup tools Handled by pg_basebackup and WAL BEGIN BACKUP logs full blocks; RMAN rereads fractured blocks

How to stay safe: a short checklist

MySQL

  • Keep innodb_doublewrite=ON. Turn it off only on storage with real atomic 16 KB writes, and test that claim yourself.
  • Leave innodb_checksum_algorithm=crc32.
  • Run innochecksum on a copy or a replica from time to time.
  • Have a backup and a replica. Doublewrite only covers the pages that were in flight.
SELECT @@innodb_doublewrite, @@innodb_checksum_algorithm;
SHOW GLOBAL STATUS LIKE 'Innodb_dblwr%';

PostgreSQL

  • Keep full_page_writes=on.
  • Turn on data checksums. New clusters on 18 get them by default. Older clusters: initdb -k, or pg_checksums --enable during a maintenance window.
  • Use wal_compression = lz4 (or zstd) to make full page images cheaper.
  • Watch checksum_failures in pg_stat_database, and run pg_amcheck now and then.
SHOW full_page_writes;
SHOW data_checksums;
SELECT datname, checksum_failures, checksum_last_failure
FROM pg_stat_database WHERE datname IS NOT NULL;

Oracle

  • Keep DB_BLOCK_CHECKSUM=TYPICAL (or FULL if you can afford it). Think about DB_BLOCK_CHECKING=MEDIUM on critical systems.
  • Make sure you can actually do block media recovery: RMAN backups plus all archived redo since then. Flashback Database helps too.
  • If you have Active Data Guard, Automatic Block Media Repair comes with it.
  • Check V$DATABASE_BLOCK_CORRUPTION after every RMAN VALIDATE or backup.
  • Use ASM normal or high redundancy if you are not on Exadata.
SHOW PARAMETER db_block_check
SHOW PARAMETER db_lost_write_protect
SELECT * FROM v$database_block_corruption;

All three

  • A UPS and a storage write cache with a battery are great, but they are not a reason to turn off any of the above.
  • Test the bad day. Break a page on a test box like I did here, and walk through the repair once before you need it for real.

Wrapping up

All three engines face the same physics: a database page is bigger than what the disk writes in one go. What changes is where each one keeps the spare copy.

MySQL keeps it in the doublewrite files. PostgreSQL keeps it in the WAL. Oracle keeps it outside the server, in a backup, a standby or a mirror, and relies on its head/tail check and checksums to catch the damage the moment it’s read.

The demo I’d show anyone who wants to save a bit of I/O is the PostgreSQL one with no full page writes and no checksums. The database started, said nothing, and returned 66 rows from a 50 row table, with both committed updates gone. A torn page you can see is a bad day. A torn page you can’t see is a much worse one.

Sources

Comments