Operating system cache and direct I/O
When the operating system reads a file, it keeps the content in the page cache in memory, so reading the same part again does not go to disk. A DBMS also has its own buffer pool, so using both puts the same page in memory twice (double caching). Direct I/O avoids this by skipping the operating system cache and reading and writing directly between disk and the buffer pool.
Approach by DBMS
| DBMS | Default approach | Setting to change it |
|---|---|---|
| PostgreSQL | Uses the operating system cache together (buffered I/O) | No production direct I/O setting |
| MySQL (InnoDB) | On Linux, usually bypasses the cache for data files with O_DIRECT | innodb_flush_method |
| Oracle | Depends on the platform and file system. Bypassed with ASM | FILESYSTEMIO_OPTIONS (SETALL = direct + asynchronous) |
| SQL Server | Always bypasses | None |
Difference between the two approaches
| Uses the operating system cache | Direct I/O | |
|---|---|---|
| Memory efficiency | The same page is loaded twice | Once, in the buffer pool only |
| Buffer pool size | Small (about 25% of memory), the rest is the operating system cache | Large (most of memory) |
| On a buffer pool miss | If the page is in the operating system cache, no disk access | Always disk I/O |
| Read-ahead | The operating system's read-ahead also helps | The DBMS must read ahead by itself |
| Write durability | After writing, fsync must flush to disk | Writes bypass the cache, but fsync is still needed because of the device cache |
| Operational burden | Memory is easy to share with other processes | A large amount of memory is fixed for the DBMS |
Checking
On Linux, you can see the memory used by the operating system cache as buff/cache in free -h. In PostgreSQL EXPLAIN (ANALYZE, BUFFERS), many of the blocks counted as read may have been read from the operating system cache rather than from the actual disk. Turning on track_io_timing and looking at the read time lets you tell them apart.
-- PostgreSQL: block read time
SET track_io_timing = on;
EXPLAIN (ANALYZE, BUFFERS) SELECT count(*) FROM orders;
-- MySQL
SHOW VARIABLES LIKE 'innodb_flush_method';
-- Oracle
SELECT value FROM v$parameter WHERE name = 'filesystemio_options';
In the articles
NOTE The default I/O approach can vary with the version, operating system, and file system.