Have you ever wanted to know what’s happening inside your PostgreSQL buffer cache? Is your bgwriter keeping up, or are you suffering with a lot of dirty buffers during a checkpoint?
Uncloak some of the mystery by using the pg_buffercache extension:
CREATE EXTENSION pg_buffercache;
You can then use the following query:
SELECT buffer_status, sum(count) AS count
FROM (SELECT CASE isdirty
WHEN true THEN 'dirty'
WHEN false THEN 'clean'
ELSE 'empty'
END AS buffer_status,
count(*) AS count
FROM pg_buffercache
GROUP BY buffer_status
UNION ALL
SELECT * FROM (VALUES ('dirty', 0), ('clean', 0), ('empty', 0)) AS tab2 (buffer_status,count)) tab1
GROUP BY buffer_status;
ACTION: If there is a large clearance of dirty blocks during the checkpoint, then you should tune bgwriter to be more aggressive.