// SQL snippet

Check the buffer cache hit ratio in PostgreSQL

The cache hit ratio: what share of page reads PostgreSQL served from shared_buffers instead of asking the operating system.

Main statement
PostgreSQL
同時涵蓋
SQL Server

The task

PostgreSQL reads data in 8 KB pages and first looks in shared_buffers. pg_stat_database counts, per database, the pages found there (blks_hit) and the pages it had to request from the operating system (blks_read). A ratio near 99 percent means the working set fits in memory; anything meaningfully below that on an OLTP system points to undersized shared_buffers or a working set that grew past RAM.

The snippet

Written for PostgreSQL. Versions for SQL Server are under Variations.

PostgreSQL
SELECT datname,
       blks_hit, blks_read,
       ROUND(100.0 * blks_hit / NULLIF(blks_hit + blks_read, 0), 2) AS cache_hit_pct
FROM pg_stat_database
WHERE datname IS NOT NULL
ORDER BY blks_hit + blks_read DESC;

它的運作方式

blks_hit counts pages found in shared_buffers; blks_read counts pages PostgreSQL asked the OS for. The ratio hit / (hit + read) is cumulative since the last stats reset, so a warm-up after restart barely moves a long window, and a problem that started yesterday barely moves it either. Snapshot the two counters and compute the ratio on the difference to see recent behaviour. A blks_read is not necessarily a disk read: PostgreSQL relies on the operating system's file cache too, and many of those reads are served from it. Check disk I/O at the OS level (iostat) before concluding the server needs more memory. The row with a NULL datname holds shared catalogs and is filtered out.

Variations

SQL Server: buffer cache hit ratio

SQL Server
SELECT CAST(100.0 * hit.cntr_value / NULLIF(base.cntr_value, 0) AS DECIMAL(5,2)) AS buffer_cache_hit_pct
FROM sys.dm_os_performance_counters hit
JOIN sys.dm_os_performance_counters base
  ON base.object_name = hit.object_name
 AND RTRIM(base.counter_name) = 'Buffer cache hit ratio base'
WHERE hit.object_name LIKE '%Buffer Manager%'
  AND RTRIM(hit.counter_name) = 'Buffer cache hit ratio';

Per-table and per-index cache usage

SELECT relname AS table_name,
       heap_blks_hit, heap_blks_read,
       ROUND(100.0 * heap_blks_hit / NULLIF(heap_blks_hit + heap_blks_read, 0), 1) AS heap_hit_pct
FROM pg_statio_user_tables
WHERE heap_blks_hit + heap_blks_read > 0
ORDER BY heap_blks_read DESC
LIMIT 20;

Questions that come up

Is 95 percent a bad cache hit ratio?

For an OLTP database whose working set should fit in memory, it is worth investigating; 99 percent or more is typical. A warehouse running large sequential scans will sit lower and that is expected, partly because PostgreSQL deliberately runs big sequential scans through a small ring of buffers so they do not evict everything else. A nightly batch can also drag the cumulative ratio down without hurting interactive users.

How do I improve the ratio?

In rough order of cost: make queries touch fewer pages (better indexes, fewer full scans), raise shared_buffers (25 percent of RAM is the usual starting point on a dedicated server; it needs a restart), and add RAM. Use the per-table variant first to see which tables drive the misses, since one scan-heavy table often accounts for most of them.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.