Home sqlite-journal-mode
Post
Cancel

sqlite-journal-mode

Doc

PRAGMA schema.journal_mode;

PRAGMA schema.**journal_mode = *DELETE TRUNCATE PERSIST MEMORY WAL OFF***

The first form of this pragma queries the current journaling mode for database. When database is omitted, the “main” database is queried.

The second form changes the journaling mode for “database” or for all attached databases if “database” is omitted. The new journal mode is returned. If the journal mode could not be changed, the original journal mode is returned.

Delete

The DELETE journaling mode is the normal behavior.

In the DELETE mode, the rollback journal is deleted at the conclusion of each transaction.

Indeed, the delete operation is the action that causes the transaction to commit. (See the document titled Atomic Commit In SQLite for additional detail.)

TRUNCATE

The TRUNCATE journaling mode commits transactions by truncating the rollback journal to zero-length instead of deleting it.

On many systems, truncating a file is much faster than deleting the file since the containing directory does not need to be changed.

PERSIST

The PERSIST journaling mode prevents the rollback journal from being deleted at the end of each transaction.

Instead, the header of the journal is overwritten with zeros. This will prevent other database connections from rolling the journal back.

The PERSIST journaling mode is useful as an optimization on platforms where deleting or truncating a file is much more expensive than overwriting the first block of a file with zeros.

MEMORY

The MEMORY journaling mode stores the rollback journal in volatile RAM.

This saves disk I/O but at the expense of database safety and integrity. If the application using SQLite crashes in the middle of a transaction when the MEMORY journaling mode is set, then the database file will very likely go corrupt.

WAL

The WAL journaling mode uses a write-ahead log instead of a rollback journal to implement transactions.

The WAL journaling mode is persistent; after being set it stays in effect across multiple database connections and after closing and reopening the database.

A database in WAL journaling mode can only be accessed by SQLite version 3.7.0 (2010-07-21) or later.

OFF

The OFF journaling mode disables the rollback journal completely.

No rollback journal is ever created and hence there is never a rollback journal to delete.

The OFF journaling mode disables the atomic commit and rollback capabilities of SQLite.

The ROLLBACK command no longer works; it behaves in an undefined way.

Applications must avoid using the ROLLBACK command when the journal mode is OFF.

If the application crashes in the middle of a transaction when the OFF journaling mode is set, then the database file will very likely go corrupt.

Without a journal, there is no way for a statement to unwind partially completed operations following a constraint error. This might also leave the database in a corrupted state.

This post is licensed under CC BY 4.0 by the author.