1.6 Buffer Pool Management Rules by Database

The principles seen in Section 1.5 are the same in every database: keep frequently used pages in memory, evict one when the pool is full, and write dirty pages to disk later. But the detailed rules differ by product, and the differences lead to differences in performance. This section compares four databases through three questions.

  1. The size of the buffer pool
  2. The criterion for choosing which page to evict first when the pool is full
  3. When dirty pages are written

PostgreSQL

The buffer pool is called shared_buffers, and its default is 128MB. On a dedicated database server, it is common to start at about 25% of memory. The design leaves the rest of memory to the operating system page cache, so it accepts double buffering and keeps the buffer pool small.

Pages to evict are chosen by a clock method (Clock Sweep). Each page has a usage count that rises by 1 each time it is read (up to 5), and the clock hand sweeps around the buffers, lowering the count of each page it passes by 1. When it meets a page whose count is 0, it frees that slot. Frequently read pages have high counts and survive several laps, while a page read once is pushed out quickly.

When full-scanning a large table or running VACUUM or a bulk load, PostgreSQL does not use the whole buffer pool but cycles through only a small dedicated area called a ring buffer. So the large-scan problem from Section 1.5 does not occur by default. Dirty pages are written out a little at a time ahead of need by the background writer (bgwriter), and the checkpointer periodically flushes all of them to disk.

MySQL (InnoDB)

The buffer pool size is set with innodb_buffer_pool_size. InnoDB does not use the operating system cache and instead manages caching itself, so on a dedicated database server it is usual to set 50 to 80% of memory as the buffer pool.

Figure. Operating rules of the InnoDB midpoint insertion LRU

flowchart LR
  D["Page newly read from disk"]
  subgraph LRU ["InnoDB LRU list"]
    NEW["new sublist (about 5/8)<br/>Frequently used pages"]
    MID["Boundary of new and old sublists<br/>(head of the old sublist)"]
    OLD["Tail of the old sublist (about 3/8)"]
  end
  D -->|"① Insert"| MID
  MID -->|"② Read again after<br/>a set time (default 1 second)"| NEW
  MID -->|"③ Not read again"| OLD
  OLD -->|"④ Evict"| E["Removed from the buffer pool"]

The figure shows the path by which a page enters and leaves the InnoDB LRU list. The LRU list is divided into the new sublist (about 5/8), which holds frequently used pages, and the old sublist (about 3/8), which holds pages on probation.

  1. A page newly read from disk enters not at the head of the list but at the boundary between the two sublists, that is, the head of the old sublist (①).
  2. If this page is read again after a set time (default 1 second) has passed, it is promoted to the new sublist (②).
  3. A page not read again is pushed toward the tail of the old sublist by pages entering behind it (③) and is evicted when the buffer pool needs space (④).

A page that is scanned once and passed over, as in a full scan, goes only along the path ① to ③ to ④, so the popular pages in the new sublist stay.

Pages to evict are chosen by the midpoint insertion LRU rule. The LRU list is divided into a new sublist (about 5/8) for frequently used pages and an old sublist (about 3/8) for pages on probation, and a page newly read from disk is inserted not at the head but at the boundary between the two. It is promoted to the front of the new sublist only if it is read again after a set time (default 1 second), so a page that is scanned once and passed over, as in a full scan, is simply pushed out of the old sublist.

Dirty pages are written by page cleaner threads, using Adaptive Flushing, which adjusts the write rate to the rate at which log accumulates. In addition, a feature that saves the list of pages in the buffer pool at shutdown and reads them in ahead of time at startup is on by default, which reduces the "slow right after restart" phenomenon covered in Section 1.9.

Oracle

The buffer pool is the buffer cache inside shared memory called the SGA. You can set its size directly with DB_CACHE_SIZE, or leave it to automatic memory management to adjust together with other memory areas. Pages to evict are chosen by a touch count based LRU that counts accesses per page (block), and it resembles InnoDB in that a new block is placed in the middle of the list.

A distinctive feature of Oracle is the direct path read, which handles full scans of large tables outside the buffer cache. When scanning a large table, it reads pages directly into the session's private memory without loading them into the buffer cache, so the popular pages are not touched at all. The buffer cache can also be divided into DEFAULT, KEEP (resident), and RECYCLE (no reuse) pools and assigned per table. Dirty blocks are written by the DBWn processes.

SQL Server / Azure SQL

SQL Server manages the buffer pool by using as much memory as needed within max server memory, without going through the operating system cache. Azure SQL Database has its memory set by the chosen service tier (number of vCores and so on), so the user does not set the buffer pool size directly, and memory is allocated additionally as needed.

Pages to evict are chosen by LRU-2. It looks not only at the last time a page was read but also at the last two access times together, so it keeps a page read steadily many times in the buffer pool longer than a page read just once a moment ago. Free slots are prepared ahead of time by the lazy writer, and dirty pages are written by checkpoints. Indirect checkpoint, the default in recent versions, flushes dirty pages to disk a little at a time so that recovery time does not grow too long when dirty pages accumulate.

Figure 1-6. PostgreSQL clock sweep and InnoDB midpoint insertion LRU

block-beta
  columns 9
  t1["[PostgreSQL] clock sweep · usage count: +1 per access (max 5)"]:9
  hand<["hand"]>(down) space:8
  b0["b0:2"] b1["b1:0"] b2["b2:5"] b3["b3:1"] b4["b4:3"] b5["b5:0"] b6["b6:4"] b7["b7:1"] back<["back to b0"]>(left)
  n1["pass b0 : 2 -> 1, move on · reach b1 : 0 -> evict, load the new page into b1"]:9
  t2["[InnoDB] midpoint insertion LRU · head -> tail"]:9
  nw["new sublist (~5/8)"]:5 od["old sublist (~3/8)"]:3 ev<["evict"]>(right)
  up1<["re-read after 1s: promote"]>(up):5 up2<["new page from disk"]>(up):3 space
  class t1,t2,n1,hand,back plain
  class b0 hot
  class b1 warn
  class b2,b3,b4,b5,b6,b7 slot
  class nw data
  class od muted
  class ev warn
  class up1 data
  class up2 hot

The top of the figure is PostgreSQL's clock sweep (Clock Sweep), and the bottom is InnoDB's midpoint insertion LRU. Both methods share the goal of "evict pages read only once quickly and keep frequently read pages longer", but they record the criterion differently.

PostgreSQL (top). b0 to b7 are slots of the buffer pool, and the number after the colon is the usage count of the page in that slot. Each time a page is read, its usage count rises by 1, up to a maximum of 5. When a slot to evict is needed, the clock hand (hand) moves around the slots in turn and lowers the usage count of each slot it passes by 1. In the figure, the hand lowers b0's count from 2 to 1 and moves on, then meets b1 with a count of 0, frees that slot, and loads the new page. After passing the last slot (b7), it returns to b0. A frequently read page like b2, with a usage count of 5, survives several laps, while a page read once soon drops to 0 and is pushed out.

InnoDB (bottom). The LRU list is divided into a new sublist (about 5/8) on the left and an old sublist (about 3/8) on the right. A page newly read from disk enters not at the head of the list (head) but at the boundary between the two sublists (new page from disk). Only a page read again after a set time (default 1 second) is promoted to the head (re-read after 1s: promote); other pages are pushed to the right end (tail) and evicted (evict).

ItemPostgreSQLMySQL (InnoDB)OracleSQL Server / Azure SQL
Size settingshared_buffersinnodb_buffer_pool_sizeDB_CACHE_SIZE or automatic managementmax server memory / automatic by tier on Azure
Common sizeAbout 25% of memory50 to 80% of memoryAllocated within the SGAMost of the permitted memory
Eviction ruleClock sweep (usage count)Midpoint insertion LRUTouch count LRULRU-2
Large-scan protectionRing bufferOld sublist and promotion wait timeDirect path read, RECYCLE poolReflects the second access time
Dirty page writesbgwriter, checkpointerpage cleaner, adaptive flushingDBWnlazy writer, indirect checkpoint
Pre-filling after restartpg_prewarm extensionBuffer pool dump and load (on by default)No built-in featureNo built-in feature
Operating system cacheUsed togetherBypassed by setting (O_DIRECT)Bypassed by settingNot used

For a more detailed look at the buffer pool by DBMS, see the wiki pages on shared_buffers, the InnoDB buffer pool, the Oracle buffer cache, the SQL Server buffer pool, and the operating system cache and direct I/O.

Whether the buffer pool is large enough to hold the set of pages used by the main workload largely determines performance. When all the frequently used pages, the working set, fit in the buffer pool, the hit ratio stays above 99% and disk reads nearly disappear after the first read. From the moment data grows and the working set exceeds the buffer pool, pages are repeatedly pushed out of the buffer pool and read back from disk, and response time suddenly becomes several times slower even though the query is unchanged. PostgreSQL is still cushioned by the operating system cache in this situation, but InnoDB and SQL Server must read pages from disk again without an OS cache.

The buffer pool's page eviction policy affects system stability. When short lookups such as order processing and large scans such as reports run in one database, the scan protection keeps order-related pages in the buffer pool even after the report ends, so processing does not slow down.

How dirty pages are flushed to disk (the checkpointer) affects the tail latency (p99). If dirty pages are written all at once at a checkpoint, disk activity spikes at that moment and other queries slow down too. This is why each database has a mechanism that spreads dirty page writes evenly (PostgreSQL's checkpoint spreading, InnoDB's adaptive flushing, SQL Server's indirect checkpoint). The principles of dirty pages and checkpoints are covered in more detail in Chapter 6.

Recovery speed after restart differs. With a feature that pre-fills the buffer pool, the hit ratio recovers quickly right after restart; without it, disk reads pile up for a while. The larger the buffer pool, the longer it takes to refill.

In practice, the following metrics are used to check buffer pool health.

  • PostgreSQL: the ratio of blks_hit (hits) to blks_read (disk reads) in pg_stat_database
  • MySQL (InnoDB): Innodb_buffer_pool_reads (the number of reads from disk because the page was not in the buffer pool) relative to Innodb_buffer_pool_read_requests
  • Oracle: the buffer cache hit ratio and the advisor that shows the expected effect of different buffer cache sizes
  • SQL Server / Azure SQL: Page Life Expectancy (the average time a page stays in the buffer pool) and the buffer cache hit ratio

NOTE Default values and setting names may differ by version.