|
| 1 | +-- Buffer cache contents (requires pg_buffercache; expensive on large shared_buffers) |
| 2 | + |
| 3 | +with buf as ( |
| 4 | + select |
| 5 | + c.oid as relid, |
| 6 | + n.nspname as schema_name, |
| 7 | + c.relname, |
| 8 | + case c.relkind |
| 9 | + when 'r' then 'table' |
| 10 | + when 'i' then 'index' |
| 11 | + when 't' then 'TOAST table' |
| 12 | + when 'm' then 'materialized view' |
| 13 | + when 'S' then 'sequence' |
| 14 | + else c.relkind::text |
| 15 | + end as object_type, |
| 16 | + count(*) as buffers, |
| 17 | + count(*) filter (where b.isdirty) as dirty_buffers, |
| 18 | + round( |
| 19 | + 100.0 * count(*) / ( |
| 20 | + select count(*) from pg_buffercache where relfilenode is not null |
| 21 | + ), |
| 22 | + 1 |
| 23 | + ) as pct_of_cache, |
| 24 | + round( |
| 25 | + 100.0 * count(*) / greatest(pg_relation_size(c.oid) / current_setting('block_size')::int, 1), |
| 26 | + 1 |
| 27 | + ) as pct_of_rel, |
| 28 | + pg_size_pretty(count(*) * current_setting('block_size')::int) as cached_size, |
| 29 | + pg_size_pretty(pg_relation_size(c.oid)) as rel_size |
| 30 | + from pg_buffercache as b |
| 31 | + join pg_class as c on b.relfilenode = pg_relation_filenode(c.oid) |
| 32 | + join pg_namespace as n on c.relnamespace = n.oid |
| 33 | + where b.relfilenode is not null |
| 34 | + and n.nspname not in ('pg_catalog', 'information_schema') |
| 35 | + and n.nspname !~ '^pg_toast' |
| 36 | + group by c.oid, n.nspname, c.relname, c.relkind |
| 37 | +) |
| 38 | +select |
| 39 | + coalesce(nullif(schema_name, 'public') || '.', '') || relname as "Object", |
| 40 | + object_type as "Type", |
| 41 | + rel_size as "Size", |
| 42 | + cached_size as "Cached", |
| 43 | + pct_of_rel || '%' as "% of Rel", |
| 44 | + pct_of_cache || '%' as "% of Cache", |
| 45 | + buffers as "Buffers", |
| 46 | + dirty_buffers as "Dirty" |
| 47 | +from buf |
| 48 | +order by buffers desc |
| 49 | +limit 50; |
0 commit comments