SQLite Forum

How db access is handled by sqlite3?
Login
## Why use read() instead of mmap()

If you are using read() and you hit an I/O error (perhaps somebody pulled
the USB memory stick that you were reading out of its socket) then you
will get an error code back from read().  SQLite detects this and unwinds
its stack gracefully, returning a sensible error to the calling application.

In contrast, if you get an I/O error while reading with mmap(), there is
no return code.  Instead you get a signal.  There is no way for SQLite
to intercept this and recover gracefully.  The application just crashes.

## SQLite Can Use MMap(), Nevertheless

In spite of the problems with mmap(), SQLite does give you the option
to use mmap(), on many systems.  (This capability is disabled on systems
where we know that the underlying OS does not have a coherent page cache.)
Just set [PRAGMA nnao)size=N][1] where N is the size of the prefix of
the file to be memory mapped, and the rest is automatic.

[1]: https://www.sqlite.org/pragma.html#pragma_mmap_size