In SQLite's rollback-journal mode, what do the SHARED, RESERVED, PENDING and EXCLUSIVE locks mean, and when does a writer take each?
answer
- five states, starting at UNLOCKED
- one RESERVED beside many SHARED
- PENDING turns new readers away
- EXCLUSIVE only to write the file
basics
~20 sIn SQLite rollback-journal mode a reader holds SHARED. A writer takes RESERVED (one at a time, readers continue), then PENDING to turn away new readers, then EXCLUSIVE once existing readers leave, only while writing the file and committing.
solid answer
~40 sSQLite locks the whole database file, and each connection is in one of five states: `UNLOCKED`, `SHARED`, `RESERVED`, `PENDING` or `EXCLUSIVE`. Any number of readers hold `SHARED`. A writer takes `RESERVED` at its first write; only one connection can hold it, so a second would-be writer gets `SQLITE_BUSY`, but readers continue and new ones may still arrive. The writer keeps its changes in memory, saving original pages to the rollback journal. When it must write the database file, at commit or when its cache spills, it takes `PENDING`, which refuses new `SHARED` locks, waits for the existing readers to leave, then takes `EXCLUSIVE`, writes the pages and deletes the journal (in the default `DELETE` mode), which is the commit. `PENDING` exists to stop a stream of readers starving the writer.
go deeper
Recall the order a writer climbs: SHARED, RESERVED, PENDING, EXCLUSIVE, and that many readers can hold SHARED at once.
Explain what each state lets other connections do, when the writer actually needs EXCLUSIVE, and why PENDING refuses new readers.
Connect the states to incidents you have debugged: long readers stalling commits, cache spills locking readers out early, and a second writer refused at RESERVED.
Use the lock ladder to judge whether a rollback-journal deployment can meet its read-latency goals under writes, or whether WAL mode or another store is needed.
## A lock on the whole file In its rollback-journal modes (the default `DELETE`, and `TRUNCATE` and `PERSIST`), SQLite controls concurrency with **locks on the database file**, not on rows or pages. Threads and processes are treated alike: at any moment, each database connection is in one of five states. WAL mode uses a different scheme and is not described by these states. | State | How many at once | What it allows | New `SHARED` locks while held? | |---|---|---|---| | `UNLOCKED` | Any | Nothing; the starting state | Yes | | `SHARED` | Many | Reading, never writing | Yes | | `RESERVED` | One | Reading now, writing later | Yes | | `PENDING` | One | Waiting for readers to clear | No | | `EXCLUSIVE` | One, with no other lock of any kind | Writing the database file | No | `PENDING` is a stepping stone. The operating-system layer tracks all five states, but SQLite's pager tracks only four, because a `PENDING` lock is always on its way to `EXCLUSIVE`. ## How a reader takes its lock To read, a connection first obtains `SHARED`. If it cannot, because another connection holds `PENDING` or `EXCLUSIVE`, it waits under its busy handler if one is set, or gets `SQLITE_BUSY`. Before reading it checks for a **hot journal** left by a crash and rolls it back first if one exists. The lock is released when the read transaction ends: straight after the statement in autocommit mode, at `COMMIT` or `ROLLBACK` inside `BEGIN`. ## How a write climbs the ladder 1. **`SHARED`.** The writer starts as a reader. 2. **`RESERVED`.** Taken at the first `INSERT`, `UPDATE` or `DELETE`, or at `BEGIN IMMEDIATE`. Only one connection may hold it; if another already does, the write fails with `SQLITE_BUSY`. Other connections keep reading and can still acquire new `SHARED` locks. 3. **Work in memory.** Before changing a page, the writer copies its original content into the rollback journal. The changed pages stay in its own memory, so the database file is untouched and readers are unaffected. 4. **`PENDING`.** When it must write to the file, at commit or earlier if its page cache fills, the writer makes sure the journal is on disk and takes `PENDING`. Existing readers carry on; no new reader is let in. 5. **`EXCLUSIVE`.** Once every `SHARED` lock has cleared, the writer takes `EXCLUSIVE` and writes its pages into the database file. 6. **Commit.** The file changes are flushed to disk and the journal is deleted (truncated or zeroed in the `TRUNCATE` and `PERSIST` modes): that instant is the commit. The writer drops its locks and readers can return. ## Why PENDING exists In SQLite version 2 a steady stream of overlapping readers could keep a writer waiting indefinitely, because there was never a moment without a read lock: **writer starvation**. Version 3 added `PENDING`. Once a writer holds it, new readers are refused, so the readers already inside eventually finish and the writer gets its turn. The price is that a reader arriving during a commit waits under its busy handler or is turned away with `SQLITE_BUSY`. ## What this means for an application - **One writer per database file.** Two connections writing to different tables still take turns, because `RESERVED` is one lock on the whole file. - **Readers delay commits.** A commit cannot take `EXCLUSIVE` until every current reader drops `SHARED`, so a long-running read holds up the writer at its commit. - **Large transactions block readers early.** If a writer's page cache fills, it must take `EXCLUSIVE` to spill pages into the file, and it keeps that lock until it commits; no other connection can read in the meantime. - **`BEGIN` itself takes nothing.** A plain `BEGIN` acquires `SHARED` at the first `SELECT` and `RESERVED` at the first write; `BEGIN IMMEDIATE` takes `RESERVED` at once and `BEGIN EXCLUSIVE` takes `EXCLUSIVE` at once. - **Locks can be made sticky.** With `PRAGMA locking_mode=EXCLUSIVE`, a connection never releases its file locks: the first read keeps `SHARED` and the first write keeps `EXCLUSIVE` until the connection closes or the mode is set back to `NORMAL` and the file is accessed again. That suits a single-process application and locks out everyone else.
- In SQLite's rollback-journal mode, why can a large write transaction block readers long before it commits?A writer normally keeps changed pages in memory and needs `EXCLUSIVE` only to write them at commit. If its page cache fills first, it must spill pages into the database file, which takes `PENDING` and then `EXCLUSIVE`, and that lock is held until the transaction commits. From the spill onward, no other connection can read the database.
- In SQLite's rollback-journal mode, what happens when a connection tries to take RESERVED while another connection already holds it?The write fails with `SQLITE_BUSY`: only one `RESERVED` lock can exist. If the connection had no transaction open, its busy handler retries until the timeout. If it already holds `SHARED` inside an open transaction, SQLite returns `SQLITE_BUSY` at once, because the other writer may be waiting for that very `SHARED` lock to clear.
A gallery room: visitors look around (SHARED), a restorer books the room (RESERVED) while visitors keep coming, then closes the entrance (PENDING) and waits for those inside to leave before working alone (EXCLUSIVE).
- SQLite takes row-level locks, so writers to different tables run together.
- A RESERVED lock stops other connections from reading the database.
- A writer holds EXCLUSIVE from its first INSERT until COMMIT.
- PENDING still admits new readers and only blocks other writers.
- Several connections can hold RESERVED at once if their rows differ.