1.7 Double Buffering

In fact, the buffer pool is not the only memory where a page read from disk stays. The operating system also has its own cache (page cache) when reading files. When a database reads a page from disk, the page stays once in the operating system cache and is then loaded into the database's buffer pool. The same page can exist at the same time in the OS-level cache and the database-level buffer pool. This phenomenon is called double buffering.

Figure 1-7. Double buffering and direct I/O

flowchart TB
  subgraph BUF ["Buffered I/O"]
    D1["Disk<br/>Page original"] -->|"① Read"| OS1["OS page cache<br/>Copy 1"]
    OS1 -->|"② Copy"| BP1["DB buffer pool<br/>Copy 2"]
  end
  subgraph DIO ["Direct I/O"]
    D2["Disk<br/>Page original"] -->|"① Read (skips the OS cache)"| BP2["DB buffer pool<br/>Copy 1"]
    OS2["OS page cache<br/>Does not keep this page"]
  end

The two boxes in the figure (buffered I/O and direct I/O) are two paths for reading the same page from disk.

  • Buffered I/O. When the database reads a page from disk, the page first comes into the operating system's page cache (①) and is then copied into the database's buffer pool (②). When the read finishes, the same page stays in memory as two copies, one in the OS page cache and one in the DB buffer pool. This is double buffering.
  • Direct I/O. The database exchanges pages directly with the disk without going through the operating system cache (①). The page does not remain in the OS page cache, and exactly one copy exists, in the buffer pool.

With the same amount of memory, direct I/O does not hold the same page twice, so it can keep that many more distinct pages in memory.

The way to avoid this is direct I/O, a method in which the database exchanges data directly with the disk without going through the operating system cache. But not every database makes the same choice. PostgreSQL is designed to make active use of the operating system cache, so it sets its own buffer pool to only part of memory and leaves the rest to the operating system. SQL Server, on the other hand, bypasses the operating system cache by default, and InnoDB and Oracle can bypass it by setting. Neither is simply right; it is a difference in design philosophy about "who manages memory".