skip to content

In PostgreSQL, how do you compute the shared-buffer hit ratio from pg_stat_database and pg_statio_user_tables, and what does blks_read not tell you?

level: middleimportance: should knowfreq 48%

answer

  1. hits plus reads is every request
  2. database first, then per table
  3. heap, index and TOAST pairs
  4. the kernel cache is invisible

basics

~20 s

The ratio is blks_hit / (blks_hit + blks_read) in pg_stat_database, or each heap, index and TOAST hit/read pair per table in pg_statio_user_tables. A blks_read is a block requested from the kernel, which may still come from the OS page cache, not disk.

solid answer

~40 s

`pg_stat_database` has `blks_hit`, blocks found already in shared buffers, and `blks_read`, blocks PostgreSQL had to request from the operating system. The hit ratio is `blks_hit / (blks_hit + blks_read)`. `pg_statio_user_tables` splits the same counting per table into `heap_blks_*`, `idx_blks_*`, `toast_blks_*` and `tidx_blks_*` pairs, so you can see which table or which part of it misses. A `blks_read` is not necessarily a disk read: the kernel may serve it from its page cache, and PostgreSQL cannot tell the difference. To learn whether reads were actually slow, turn on `track_io_timing` (off by default) and look at `blk_read_time`. All of these are cumulative since `stats_reset`, so compare windows rather than lifetime totals.

code

sql · 9 lines
sql
SELECT relname,
       heap_blks_read, heap_blks_hit,
       idx_blks_read,  idx_blks_hit,
       toast_blks_read, toast_blks_hit,
       round(100.0 * heap_blks_hit / NULLIF(heap_blks_hit + heap_blks_read, 0), 1) AS heap_hit_pct
FROM pg_statio_user_tables
ORDER BY coalesce(heap_blks_read, 0) + coalesce(idx_blks_read, 0)
       + coalesce(toast_blks_read, 0) DESC
LIMIT 10;

go deeper

for a junior

Recall the two counters, blks_hit and blks_read in pg_stat_database, and that the ratio is hits over hits plus reads.

for a middle

Explain the per-table split in pg_statio_user_tables, heap, index and TOAST, and why a counted read may come from the kernel's cache.

for a senior

Show disciplined measurement: window the counters, enable track_io_timing to price reads in milliseconds, and go from database to table to statement before changing memory settings.

for a principal

Argue which signals a team should alert on, and why a single cluster-wide hit ratio makes a weak service-level indicator compared with read latency and per-query buffer counts.

## The counters PostgreSQL keeps table and index pages in its own buffer cache, **shared buffers**. Each time a backend needs a block it either finds it there, a **hit**, or asks the operating system to read it, a **read**. The cumulative statistics system counts both at two granularities. | View | Columns | Scope | |---|---|---| | `pg_stat_database` | `blks_hit`, `blks_read`, `blk_read_time`, `stats_reset` | one row per database, plus a row with `datid` 0 for shared catalogs | | `pg_statio_user_tables` | `heap_blks_hit/read`, `idx_blks_hit/read`, `toast_blks_hit/read`, `tidx_blks_hit/read` | one row per user table in the current database | | `pg_statio_user_indexes` | `idx_blks_hit/read` | one row per index | | `pg_statio_user_sequences` | `blks_hit/read` | one row per sequence | ## Computing the ratio ```sql SELECT datname, blks_hit, blks_read, round(100.0 * blks_hit / NULLIF(blks_hit + blks_read, 0), 2) AS hit_pct, stats_reset FROM pg_stat_database WHERE datname = current_database(); ``` `blks_hit + blks_read` is every block request, so the expression is the share served from shared buffers. Guard the division with `NULLIF` for a freshly reset database. ## Per-table breakdown A database-wide figure hides where the misses happen. In a CMS, `cms.page` may be hot and fully cached while an archive of `cms.page_revision` bodies is read cold from TOAST. `pg_statio_user_tables` shows that split: - `heap_blks_*` are the table's own pages; - `idx_blks_*` are summed over all of the table's indexes; - `toast_blks_*` are the TOAST table, where wide values live out of line; - `tidx_blks_*` are the TOAST table's index. Sorting by `heap_blks_read + idx_blks_read + toast_blks_read` descending lists the tables that cause the most block requests to the kernel, which is often more useful than any ratio. ## What a read really is `blks_read` counts blocks PostgreSQL could not find in shared buffers. The manual is explicit that `blks_hit` includes only PostgreSQL's own cache, not the operating system's file system cache. PostgreSQL reads through the kernel, so a counted read may be: - served from the **OS page cache** in microseconds; or - a real **device read** taking far longer. The counters cannot separate the two. To see the cost: 1. Turn on `track_io_timing`. It is off by default because it queries the clock repeatedly, which is expensive on some platforms; the `pg_test_timing` tool measures that overhead on your machine. Changing it per session with `SET` needs superuser or a granted `SET` privilege. 2. Read `blk_read_time` in `pg_stat_database`, in milliseconds, and divide by the `blks_read` gained in the same window to get average time per read. 3. For per-statement detail, `pg_stat_statements` and `EXPLAIN (ANALYZE, BUFFERS)` report shared hit and read counts per query. In PostgreSQL 18, `EXPLAIN ANALYZE` shows `BUFFERS` by default. ## Where else hits and reads show up The same hit-or-read split appears in three other places, each at a different grain: | Surface | Grain | Columns | |---|---|---| | `pg_stat_io` | backend type, object and context, cluster-wide | `hits`, `reads`, `read_bytes`, `evictions` | | `pg_stat_statements` | one normalized statement | `shared_blks_hit`, `shared_blks_read` | | `EXPLAIN (ANALYZE, BUFFERS)` | one plan node of one execution | `shared hit=` and `read=` | A practical drill-down in a CMS runs top to bottom: `pg_stat_database` says the database misses more than last week, `pg_statio_user_tables` says the misses are on `cms.page_revision` TOAST, `pg_stat_statements` names the revision-history query, and its plan shows which node reads the cold pages. The statement-level and plan-level steps belong to the query-stats and explain leaves; this leaf supplies the first two. ## Using it without fooling yourself - **Cumulative since reset.** A ratio over two years of uptime barely moves when today's workload changes. Snapshot the counters and compute the ratio for a window. - **Lost on unclean starts.** After a crash, an immediate shutdown, a start from a base backup or point-in-time recovery, every counter starts again at zero. - **Stats lag.** Backends flush counts when they go idle, at most about once a second, so a long-running query shows up only when it ends. - **What a good number is**, and why a high ratio can coexist with slow queries, is buffer-pool theory, covered by the engine-neutral buffer-pool leaf. This leaf's job is knowing which columns to read and what they count.

  • Why can toast_blks_read be NULL for some rows of pg_statio_user_tables?
    The TOAST columns come from the table's TOAST relation. A table whose column types can never need out-of-line storage has no TOAST table, so there is nothing to count and the TOAST and TOAST-index columns are NULL rather than zero.
  • How do you tell whether PostgreSQL's block reads are coming from disk or from the OS cache?
    The counters cannot tell you. Enable `track_io_timing` and compare `blk_read_time` with the `blks_read` gained over the same window: an average of a few microseconds per block means the kernel cache served it, while milliseconds mean real device reads. Operating-system I/O tools give the same answer from the other side.

  • blks_read counts only blocks that were physically read from the disk.
  • The hit ratio is blks_hit divided by blks_read.
  • pg_stat_database hit counters include hits in the operating system's page cache.
  • track_io_timing is on by default, so blk_read_time is always populated.
  • A database-wide hit ratio shows which tables are being read cold.