Skip to content

Instantly share code, notes, and snippets.

@krisiye
Created August 18, 2022 18:42
Show Gist options
  • Save krisiye/59428720a6d3f328d50db670c92b9a9b to your computer and use it in GitHub Desktop.
Save krisiye/59428720a6d3f328d50db670c92b9a9b to your computer and use it in GitHub Desktop.
Useful queries to evaluate database level stats
-- Cache hit ratio.
-- Sweet spot here are values close to 100 which also means all necessary data were read from shared buffers.
-- values near 90 or lower show that postgres read from disk time to time.
SELECT
round(100 * sum(blks_hit) / sum(blks_hit + blks_read), 3) as cache_hit_ratio
FROM pg_stat_database;
-- estimate commit ratio and detect errors
-- values that are closer to 100 mean that you database has very few errors.
SELECT
datname, 100 * xact_commit / (xact_commit + xact_rollback) as commit_ratio
FROM pg_stat_database WHERE (xact_commit + xact_rollback) > 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment