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

DBMSDefault approachSetting to change it
PostgreSQLUses 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_DIRECTinnodb_flush_method
OracleDepends on the platform and file system. Bypassed with ASMFILESYSTEMIO_OPTIONS (SETALL = direct + asynchronous)
SQL ServerAlways bypassesNone

Difference between the two approaches

Uses the operating system cacheDirect I/O
Memory efficiencyThe same page is loaded twiceOnce, in the buffer pool only
Buffer pool sizeSmall (about 25% of memory), the rest is the operating system cacheLarge (most of memory)
On a buffer pool missIf the page is in the operating system cache, no disk accessAlways disk I/O
Read-aheadThe operating system's read-ahead also helpsThe DBMS must read ahead by itself
Write durabilityAfter writing, fsync must flush to diskWrites bypass the cache, but fsync is still needed because of the device cache
Operational burdenMemory is easy to share with other processesA 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.

Related pages