skip to content

How do SQLite's rollback-journal modes DELETE, TRUNCATE, PERSIST, MEMORY and OFF differ, and what does each one put at risk?

level: middleimportance: should knowfreq 38%

answer

  1. original pages saved first
  2. how the commit is marked
  3. delete, shrink, or zero header
  4. RAM journal dies with process
  5. OFF breaks ROLLBACK

basics

~20 s

SQLite's rollback journal saves original pages before overwriting the database. DELETE (default), TRUNCATE and PERSIST differ only in how commit retires the -journal: delete, truncate or zero its header. MEMORY keeps it in RAM and OFF has none, so a crash can corrupt.

solid answer

~50 s

A writer first copies the original content of every page it will change into `app.db-journal`, syncs it, then writes new pages into the database; after a crash the leftover **hot journal** is played back by the next connection. The modes differ in how a commit retires that file. `DELETE`, the default, deletes it, and the delete is the commit. `TRUNCATE` truncates it to zero length, often faster because the directory does not change. `PERSIST` keeps the file and zeroes its header, cheapest where deleting and truncating are both slow, but the file keeps its disk space unless `journal_size_limit` caps it. `MEMORY` keeps the journal in RAM: `ROLLBACK` works, but a crash mid-transaction will very likely corrupt the database. `OFF` keeps no journal: `ROLLBACK` behaves in an undefined way and a failed statement cannot unwind. Each of these is per-connection; only `WAL` is stored in the file.

code

sql · 3 lines
sql
PRAGMA journal_mode;
PRAGMA journal_mode = TRUNCATE;
PRAGMA journal_size_limit = 1048576;

go deeper

for a junior

Recall that the default rollback journal saves original pages to a -journal file, and that DELETE mode commits by deleting that file.

for a middle

Explain the four-step commit, what makes a journal hot, and why DELETE, TRUNCATE and PERSIST are equally safe while MEMORY and OFF give up crash recovery.

for a senior

Show judgment about when a non-default mode is worth it: PERSIST or TRUNCATE on a slow filesystem with journal_size_limit set, and MEMORY or OFF only for throwaway data you could rebuild.

for a principal

Weigh whether a team should allow MEMORY or OFF at all, given that a crash then corrupts rather than rolls back, and whether WAL makes the rollback-mode tuning moot.

## How a rollback journal makes a commit atomic In every journal mode except WAL, SQLite protects a write transaction with a **rollback journal**, a file named after the database with `-journal` appended (`app.db-journal`). Simplified, one write transaction goes like this: 1. before changing a page, the writer copies the page's **original content** into the journal, together with a header that records the database's original size; 2. the journal is flushed to stable storage, so the undo information is safe before anything else changes; 3. the new page content is written directly into the database file, which is then flushed as well; 4. the journal is retired, and retiring it is the commit. If the process or the machine dies before step 4, the database file may hold a mix of old and new pages, but the journal still holds every original page needed to undo them. ## Hot journals and recovery A leftover journal that still has to be played back is a **hot journal**. SQLite treats a journal as hot when it exists, is not empty, has a well-formed header that has not been zeroed, and no connection holds a RESERVED lock on the database (plus a condition about the super-journal used for commits across attached databases). The next connection to open the database notices it, takes an exclusive lock so that only one process repairs it, copies the original pages back, truncates the file to its original size if the aborted transaction grew it, and then retires the journal. The journal is therefore part of the database's state after a crash. Delete it, rename it, or move the database without it, and recovery cannot happen. ## The five rollback-journal modes `PRAGMA journal_mode` chooses how step 4 happens, or whether there is a journal on disk at all. | Mode | How a commit retires the journal | After a crash mid-transaction | Why choose it | |---|---|---|---| | `DELETE` (default) | Deletes `-journal`; the delete is the commit | Recovered from the hot journal | Works everywhere | | `TRUNCATE` | Truncates the file to zero length | Recovered from the hot journal | Truncating is often faster than deleting, because the directory does not change | | `PERSIST` | Keeps the file and overwrites its header with zeros | Recovered from the hot journal | Cheapest where deleting and truncating are both expensive | | `MEMORY` | The journal lives in RAM, never on disk | The database will very likely be corrupt | Saves disk I/O; `ROLLBACK` still works | | `OFF` | There is no journal | The database will very likely be corrupt | No journal I/O at all | The first three are equally recoverable. They differ only in which filesystem operation marks the commit: a deleted file does not exist, a zero-length file is empty, and a zeroed header is not well formed, so in none of the three cases does the old journal look hot. The zeroed header is also why `PERSIST` stops another connection from rolling the old journal back. ## What MEMORY and OFF really give up - **MEMORY** still keeps the undo data, so `ROLLBACK` and a statement that fails half-way unwind correctly while the process lives. What it loses is crash recovery: the journal disappears with the process, and a crash in the middle of a transaction will very likely leave the file corrupt. - **OFF** keeps nothing. `ROLLBACK` no longer works and behaves in an undefined way, so applications must avoid it. A statement that fails half-way cannot unwind either: a `CREATE UNIQUE INDEX` that hits a duplicate can leave a partially built, corrupt index behind. Because OFF lets ordinary SQL corrupt the file, it is disabled when `SQLITE_DBCONFIG_DEFENSIVE` is on. ## Settings that travel with the journal mode - Every rollback-journal mode is a **per-connection** setting. A connection that sets `TRUNCATE`, closes and reopens the database comes back in `DELETE`. Only `WAL` is recorded in the file. - The journal mode cannot be changed while a transaction is active, and an in-memory database can use only `MEMORY` or `OFF`. - `PERSIST`, like exclusive locking mode, can leave a journal on disk after each commit, and after a large transaction such as a `VACUUM` that journal can be very large. `PRAGMA journal_size_limit = N` truncates a leftover journal to N bytes after each commit; the default is -1, meaning no limit, and the limit is set separately for each attached database. - How safe the on-disk modes are also depends on `PRAGMA synchronous`. `FULL` is the default with a rollback journal; `EXTRA` additionally syncs the directory after the journal is deleted in DELETE mode, closing a window in which the last commit before a power loss could roll back on reboot. ```sql PRAGMA journal_mode; -- query the current mode PRAGMA journal_mode = PERSIST; -- returns the mode now in force PRAGMA journal_size_limit = 1048576; ```

  • Why can a SQLite database in PERSIST mode end up with a huge -journal file, and how do you cap it?
    PERSIST never deletes or truncates the journal; it only zeroes the header, so the file stays as large as the biggest transaction made it, and a VACUUM can make it very large. PRAGMA journal_size_limit = N truncates a leftover journal to N bytes after each commit. The default is -1, no limit, and each attached database needs its own setting.
  • What makes a leftover SQLite -journal file hot, and what does the next connection do with it?
    It is hot when it exists, is not empty, its header is well formed rather than zeroed, and no connection holds a RESERVED lock on the database. The next connection takes an exclusive lock, copies the original pages back, truncates the database to its original size and retires the journal, which returns the file to its state before the aborted transaction.

  • TRUNCATE and PERSIST trade away crash safety to make commits faster.
  • MEMORY journal mode is crash-safe because ROLLBACK still works.
  • journal_mode=OFF only speeds up writes and has no other effect.
  • Setting journal_mode=TRUNCATE once makes every later connection use it.
  • In PERSIST mode another connection can roll back the leftover journal.