Introduction
A -journal file is the rollback journal of an
SQLite 3 database. It sits in the same folder
as the database and takes its full name with -journal added:
app.db has app.db-journal. Before
SQLite changes a page of the database, it copies
the page’s original content into the journal. If the program or the computer stops in
the middle of a transaction, the journal is still there, and the next program to open
the database uses it to put the original pages back.
This is SQLite’s original way of keeping a database safe, used by every SQLite 3 and
still the default. Normally the journal exists only while a transaction is writing,
and is deleted, emptied or cleared when it commits; a -journal file that
stays beside a database is a sign that a program stopped mid-transaction. SQLite
documents the format in full. Only SQLite writes it.
A -journal file dropped on any page of this site opens in the explorer,
which shows every segment and page record, checks each record’s checksum, says what a
rollback would do with it and opens the original page as a database page, down to the
rows. It also reads the super-journal record and SQLite 2’s journals.
Structure
A header padded to the size of one disk sector, then the page records. Every number is big-endian. A journal can hold more than one segment, each a header and its records, and a journal of a transaction across several databases ends with a super-journal record.
0Journal header header string, page count, nonce, initial size, sector size, page size; then zeros to the end of the sector1 sectorsectorPage records each a page number, the page’s original content and a checksumpage size + 8 each…Further segments another header at a sector boundary, with its recordsoptional…Super-journal record the name of the super-journal file, at the very end20 + name, optional
Journal header
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 8 | bytes | Header string | D9 D5 05 F9 20 A1 63 D7. |
| 8 | 4 | i32 | Page count | The number of page records in this segment, or −1 (FF FF FF FF) for every record to the end of the file. |
| 12 | 4 | u32 | Nonce | A random number that starts each record’s checksum. |
| 16 | 4 | u32 | Initial size | The database size in pages before the transaction. Rolling back truncates the database to this size. |
| 20 | 4 | u32 | Sector size | The disk sector size the writer assumed, a power of two from 32 to 65,536. The header fills one sector, and each segment starts on a sector boundary. |
| 24 | 4 | u32 | Page size | The size of the pages in this journal, a power of two from 512 to 65,536. SQLite before 3.5.8 wrote 0 here, and a reader then uses the page size it already knows. |
| 28 | sector − 28 | bytes | Padding | Zeros to the end of the sector. |
The page count of a segment is written last, when the journal is
synced. A count of −1, written when PRAGMA synchronous=OFF means there
will be no sync, or when the file system guarantees safe appends
(SQLITE_IOCAP_SAFE_APPEND), makes every record to the end of the file
count; such a journal never gets a second segment. A count of 0 means
the segment has not been synced: a rollback after a crash reads no records from it.
Only the transaction’s own rollback, in the process that wrote them, reads a last
segment’s records with a count of 0, working out how many there are from the size of
the file.
All headers in a journal must give the same sector size and page size. SQLite reads both only from the first header. If either is outside its range, SQLite takes the header to have been left unfinished by a crash, and rolls nothing back.
A journal is valid only while its header is. Committing makes it invalid: SQLite
deletes the file, truncates it to nothing, or overwrites the header, usually with
zeros, depending on PRAGMA journal_mode. A journal left in any of those
states is not this format any more and holds nothing to play back.
In journals made for this page, a journal copied before SQLite had synced it to disk had its first 12 bytes, the header string and page count, still zero. SQLite fills them in when it syncs the journal, before it changes the database, so a journal caught in that state is not yet valid either.
Page records
Each record holds one database page as it was before the transaction changed it. Records follow the header, from the first sector boundary, one after another.
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 4 | u32 | Page number | The database page this record restores. |
| 4 | page size | bytes | Original page | The page’s content before the transaction. |
| 4 + page size | 4 | u32 | Checksum | See Checksum. |
A page is saved once per transaction, the first time it is changed, and appears only once in a journal. Pages added to the database by the transaction are not saved: truncating the database to the initial size removes them. Freelist leaf pages are not saved either, since they hold nothing that needs restoring. When the page size is smaller than the device's sector size, every page in the same sector as a changed page is saved too, so that a torn sector write can be undone. The original page is a database page, laid out as in the SQLite 3 database.
To roll back, SQLite first truncates the database to the initial size in the first header, then reads the records in order, each segment in turn, and writes each original page back to its place in the database. For each record it checks, in this order:
- a page number of 0 or the page number of the lock-byte page stops the rollback there (the super-journal record starts with that number for this reason);
- a page number past the initial size is skipped, and the rollback carries on;
- a checksum that does not match stops the rollback there.
When the records are finished, the journal is made invalid. The lock-byte page is the
page holding bytes 1,073,741,824 onwards (page 1,048,577 with 1,024-byte pages), never
used for data. These rules come from pager_playback_one_page in SQLite’s
pager.c.
Checksum
The checksum guards against a record torn by a power failure, half old and half new. It samples every 200th byte of the page, from near the end:
- Start with sum = the nonce from the header of the record's segment.
- Let X = page size − 200.
- While X ≥ 0: add the byte at offset X of the page to the sum, then subtract 200 from X.
- The checksum is the sum, in 32 bits.
With 1,024-byte pages it adds the bytes at offsets 824, 624, 424, 224 and 24; with 4,096-byte pages, 20 bytes from offset 3,896 down to 96. A page whose sampled bytes are all zero has a checksum equal to the nonce.
Segments
A journal may hold more than one segment. Each starts with its own header at the next sector boundary after the records before it, followed by its own records, and a reader plays back each segment in turn. New segments appear when a transaction changes more pages than fit in memory and SQLite has to sync the journal part of the way through. Any bytes after the records and before the next sector boundary are padding.
A reader stops looking for segments when the next sector boundary does not hold the
header string, or when the header would not fit in the file. Whatever follows is old
content, left from an earlier transaction when the journal is kept between
transactions (PRAGMA journal_mode=PERSIST).
Super-journal record
A transaction that writes to several attached databases keeps one journal per database, and a further file, the super-journal (formerly the master journal), which names them. Each journal then records the super-journal’s name, in a record that is always the last thing in the file:
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 4 | u32 | Lock-byte page number | The page holding byte 1,073,741,824: the page size’s share of 1,073,741,824, plus 1. A reader playing records back stops at this number. |
| 4 | N | bytes | Name | The super-journal file’s path, in UTF-8, with no terminator. |
| 4 + N | 4 | u32 | Name length | N, the name’s length in bytes. |
| 8 + N | 4 | u32 | Name checksum | The sum of the name’s bytes. |
| 12 + N | 8 | bytes | Journal magic | The 8 header-string bytes, D9 D5 05 F9 20 A1 63 D7. |
SQLite finds the record from the end of the file: the last 8 bytes must be the header
string, and the length before them must be within the file and not 0. The checksum
adds each byte of the name as a C char, which is a signed 8-bit number on
most computers, so names with bytes above 127 can differ between platforms. If the
checksum does not match, SQLite ignores the name.
The source of SQLite, writeSuperJournal and
readSuperJournal in pager.c, is the reference here. SQLite’s
Atomic Commit page calls the place of the name the journal’s header, but the code
writes it at the end of the file, starting at the next sector boundary when the writer
syncs fully.
Hot journals
A journal is hot, and rolled back when the database is next opened, if all of these hold:
- the journal exists and is not empty;
- no connection holds a reserved lock on the database;
- its header is well formed, and in particular has not been zeroed;
- it names no super-journal, or the super-journal it names still exists.
Two details of the check: SQLite only tests that the journal's first byte is not 0 (a zeroed header starts with a zero byte), and a journal beside a database of 0 pages is deleted rather than played back.
The last rule is what makes a transaction across several databases atomic: deleting the super-journal is the moment the whole transaction commits, because every journal that names it stops being hot at once.
A real journal
A rollback journal made with the sqlite3 shell for this page, copied while the
transaction was open: a database of two 1,024-byte pages with 12 bytes reserved at
the end of each, holding a table t with the row (1, 'one'),
and the transaction UPDATE t SET b='uno'. The journal is 1,544 bytes: a
512-byte header sector and one record of 4 + 1,024 + 4 bytes.
00000000 D9 D5 05 F9 20 A1 63 D7 FF FF FF FF 94 72 0C C2
00000010 00 00 00 02 00 00 02 00 00 00 04 00 00 00 00 00
…
00000200 00 00 00 02 0D 00 00 00 01 03 EC 00 03 EC 00 00
…
000005F0 06 01 03 09 13 6F 6E 65 00 00 00 00 00 00 00 00
00000600 00 00 00 00 94 72 0C C2
- The header string, then a page count of −1. This journal was written with
PRAGMA synchronous=OFF, where SQLite leaves the count as −1 and the records run to the end of the file. - Nonce
94 72 0C C2. Initial size00 00 00 02, 2 pages. Sector size00 00 02 00, 512. Page size00 00 04 00, 1,024. - At offset 512, the first sector boundary, the record: page 2, then page 2’s
original content. It is a leaf table
page with one cell at page offset 1,004, which is journal offset 4 + 512 + 1,004
= 1,520 (
5F0):06 01 03 09 13 6F 6E 65, the row(1, 'one')as it was before the UPDATE. - The checksum at offset 1,540 is
94 72 0C C2, the nonce itself: the bytes at page offsets 824, 624, 424, 224 and 24 are all zero.
SQLite 2 journals
SQLite 2 also wrote -journal
files, in three layouts of its own. The header string is
D9 D5 05 F9 20 A1 63 and a last byte that says which: D4
format 1, D5 format 2, D6 format 3. SQLite 2.8.0 added format
3 and always writes it, but reads all three. There are no segments, no sector padding
and no page size in the file: SQLite 2 builds the page size in, normally 1,024 bytes.
Formats 2 and 3 are big-endian. Format 1 is in the byte order of the machine that wrote
it, which the file does not state.
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 8 | bytes | Header string | As above. |
| 8 | 4 | u32 | Page count (format 3 only) | The number of page records, or FF FF FF FF for every record to the end of the file. 0 until the journal is synced. |
| 12 | 4 | u32 | Checksum start (format 3 only) | A random number. |
| 16 (8 in formats 1 and 2) | 4 | u32 | Initial size | The database size in pages before the transaction. |
The header is 20 bytes in format 3 and 12 in formats 1 and 2. Records follow it straight away, one after another:
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 4 | u32 | Page number | The database page this record restores. |
| 4 | 1,024 | bytes | Original page | The page’s content before the transaction, laid out as in the SQLite 2 database. |
| 1,028 | 4 | u32 | Checksum (format 3 only) | The checksum start plus the page number. Despite the name, it does not cover the page’s bytes. |
In formats 1 and 2 the number of records comes from the size of the file. To roll back, SQLite 2 truncates the database to the initial size and plays the records in order: a page number of 0 stops it, a page number past the initial size is skipped, and in format 3 a checksum that does not match stops it. SQLite 2’s pages do not say what they are, so the explorer shows a page as a b-tree page only when it reads as one, and uses the copy of page 1, when the journal has one, for the byte order of the pages.
Telling it apart
The file starts with the 8 bytes D9 D5 05 F9 20 A1 63 D7, or, for an
SQLite 2 journal, the same 7 bytes and D4, D5 or
D6.
The explorer’s parser checks those bytes and needs a whole header; a file that fails either is turned down. It reports a page size that is not a power of two from 512 to 65,536, a sector size outside SQLite’s range, a page count larger than the file holds, a later header whose sizes differ from the first, the record that stops a rollback (a bad checksum, page number 0 or the lock-byte page), and a super-journal record whose checksum does not match, and still shows the file. No rollback journal was among the files examined for this site; the parser was tested with journals made for the purpose and with journals SQLite itself wrote.
Related formats
- SQLite 3 database – the database the journal belongs to
- SQLite write-ahead log (-wal) – the other way SQLite protects a transaction: the new pages instead of the old
Links
Software
- SQLite – the library that writes the journal and plays a hot journal back when it opens the database (Windows, macOS, Linux, iOS, Android)
- The sqlite3 command-line shell – opening the database with it rolls back a hot journal;
PRAGMA journal_modesets how journals end (Windows, macOS, Linux) - DB Browser for SQLite – opens the database, and with it any hot journal (Windows, macOS, Linux)
Developers
- SQLite – the SQLite library, the reference reader and writer of the journal (C)
Reference
- Database File Format – SQLite’s description of the file, with the rollback journal’s header, records and checksum
- Atomic Commit In SQLite – SQLite’s step-by-step account of how a transaction uses the journal
- Temporary Files Used By SQLite – SQLite’s list of the files it makes beside a database