diff --git a/sql/b6_buffercache.sql b/sql/b6_buffercache.sql index 7f3905a..697ed82 100644 --- a/sql/b6_buffercache.sql +++ b/sql/b6_buffercache.sql @@ -17,20 +17,31 @@ with buf as ( count(*) filter (where b.isdirty) as dirty_buffers, round( 100.0 * count(*) / ( - select count(*) from pg_buffercache where relfilenode is not null + select count(*) + from pg_buffercache + where reldatabase = (select oid from pg_database where datname = current_database()) + and relfilenode is not null ), 1 ) as pct_of_cache, round( - 100.0 * count(*) / greatest(pg_relation_size(c.oid) / current_setting('block_size')::int, 1), + 100.0 * count(*) / greatest( + ceil(pg_relation_size(c.oid)::numeric / current_setting('block_size')::numeric), + 1 + ), 1 ) as pct_of_rel, pg_size_pretty(count(*) * current_setting('block_size')::int) as cached_size, pg_size_pretty(pg_relation_size(c.oid)) as rel_size from pg_buffercache as b - join pg_class as c on b.relfilenode = pg_relation_filenode(c.oid) + join pg_class as c + on c.oid = ( + select pg_filenode_relation(b.reltablespace, b.relfilenode) + ) join pg_namespace as n on c.relnamespace = n.oid - where b.relfilenode is not null + where b.reldatabase = (select oid from pg_database where datname = current_database()) + and b.relfilenode is not null + and b.relforknumber = 0 and n.nspname not in ('pg_catalog', 'information_schema') and n.nspname !~ '^pg_toast' group by c.oid, n.nspname, c.relname, c.relkind