Skip to content

Cache hit ratio

Share of reads served from shared buffers, for tables and for indexes. Below 99% on a warm OLTP server means the working set no longer fits.

PostgreSQL · SQL
SELECT 'tables' AS kind,       ROUND(100.0 * SUM(heap_blks_hit) / NULLIF(SUM(heap_blks_hit) + SUM(heap_blks_read), 0), 2) AS hit_pctFROM pg_statio_user_tablesUNION ALLSELECT 'indexes',       ROUND(100.0 * SUM(idx_blks_hit) / NULLIF(SUM(idx_blks_hit) + SUM(idx_blks_read), 0), 2)FROM pg_statio_user_indexes

This query is part of the "PostgreSQL health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.

What this query shows

PostgreSQL keeps recently used pages in shared buffers, its own memory cache. A read served from there is a hit. A read that has to ask the operating system is a read. The cache hit ratio is the share of hits.

The query returns two rows: one for table data (pg_statio_user_tables, heap blocks) and one for indexes (pg_statio_user_indexes). It is a quick check on whether the working set fits in memory, and a common item in a PostgreSQL health check.

How to read each column

  • kind: tables or indexes.
  • hit_pct: 100 times hits divided by hits plus reads, rounded to two decimals. Hits are heap_blks_hit or idx_blks_hit. Reads are heap_blks_read or idx_blks_read.

A read counted here is a block that PostgreSQL asked the operating system for. The operating system may still serve it from its own file cache without touching the disk, so a miss is not always a slow disk read.

Healthy and worrying numbers

As a rule of thumb, an OLTP database that has been running for a while should sit at 99% or more. Between 95% and 99% is worth watching. Below 95% on a transactional workload usually means the working set is bigger than the memory you give it, or large scans are pushing hot data out.

The rule does not fit every workload. An analytics or reporting database that scans large tables will show a lower ratio, and that can be perfectly fine. Right after a restart the cache is cold and the number is low for a while too.

What to do about it

Before you add memory, find who causes the misses. The slowest statements query shows cache_hit_pct per statement, so you can see if a few scans are responsible. Fix those with an index or a rewrite first.

If the whole working set is too big, raise shared_buffers. A common starting point on a dedicated server is about 25% of RAM, and the change needs a restart.

SHOW shared_buffers;
ALTER SYSTEM SET shared_buffers = '4GB';
-- restart PostgreSQL to apply

Also check effective_cache_size. It does not allocate memory. It tells the planner how much cache to expect, including the operating system's, and affects whether it chooses index scans.

Limits of this query

  • The counters accumulate since the last statistics reset, so the value is an average over a long period and hides today's drop. Compare readings over time.
  • It covers user tables and indexes of the connected database. System catalogs are not included.
  • A high ratio does not prove the database is fast. A bad query can still read the same cached pages over and over.
  • On a replica, the numbers describe that replica only.

Run it on a schedule in DataRunner

Connect the database to DataRunner, paste this query or start from the template, approve the SQL, then pick a schedule and a channel. Weekly or daily, so you see the number move rather than read it once. Each run executes the query on your database and delivers the result as CSV, Excel, PDF or inline HTML by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook or Google Sheets.

Every run is kept in the run history, so you can set today's result next to last week's. A schedule carries its own time zone, so a Monday at 8:00 is your Monday.

If the database sits in a private network, the bridge agent opens the connection from the inside out, so you do not need to open an inbound port.

Frequently asked questions

What is a good cache hit ratio in PostgreSQL?
For a transactional database, 99% or higher is the usual target, and it is a rule of thumb, not a documented limit. Reporting databases that scan large tables can run lower and still be fine. Judge it against your workload and how it moves over time.
How do I calculate the PostgreSQL cache hit ratio?
Divide blocks hit by blocks hit plus blocks read from pg_statio_user_tables (heap_blks_hit and heap_blks_read) and from pg_statio_user_indexes (idx_blks_hit and idx_blks_read). The query on this page does that for tables and indexes.
How do I improve the cache hit ratio?
Remove the scans that flush the cache by adding indexes, then raise shared_buffers if the working set still does not fit. Adding RAM to the server helps both PostgreSQL and the operating system cache.
Does a low cache hit ratio always mean disk reads?
No. A block that missed shared buffers can still come from the operating system's cache. The ratio only says how often PostgreSQL had to ask for it.