dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Scripts Library/postgresql/#9
Script #9
postgresql
Monitoringread-onlyMin Version: 9.x

PostgreSQL Buffer Cache Hit Ratio

Computes the shared buffer hit ratio per database. Shows whether memory is undersized and whether too much data is read from disk.

#cache-hit#shared_buffers#pg_stat_database
postgresql · sql
SELECT datname, blks_hit, blks_read,
       round(100.0 * blks_hit / NULLIF(blks_hit + blks_read, 0), 2) AS hit_pct
FROM pg_stat_database
WHERE datname IS NOT NULL
ORDER BY blks_read DESC;
Operational Notes & Remediation Guidance

Expected to be above 99% in OLTP systems. Since the OS page cache also operates beneath PostgreSQL, interpret alongside `pg_stat_io` (PG 16+).

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 9.x).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.