@@ -17,20 +17,31 @@ with buf as (
1717 count (* ) filter (where b .isdirty ) as dirty_buffers,
1818 round(
1919 100 .0 * count (* ) / (
20- select count (* ) from pg_buffercache where relfilenode is not null
20+ select count (* )
21+ from pg_buffercache
22+ where reldatabase = (select oid from pg_database where datname = current_database())
23+ and relfilenode is not null
2124 ),
2225 1
2326 ) as pct_of_cache,
2427 round(
25- 100 .0 * count (* ) / greatest(pg_relation_size(c .oid ) / current_setting(' block_size' )::int , 1 ),
28+ 100 .0 * count (* ) / greatest(
29+ ceil(pg_relation_size(c .oid )::numeric / current_setting(' block_size' )::numeric ),
30+ 1
31+ ),
2632 1
2733 ) as pct_of_rel,
2834 pg_size_pretty(count (* ) * current_setting(' block_size' )::int ) as cached_size,
2935 pg_size_pretty(pg_relation_size(c .oid )) as rel_size
3036 from pg_buffercache as b
31- join pg_class as c on b .relfilenode = pg_relation_filenode(c .oid )
37+ join pg_class as c
38+ on c .oid = (
39+ select pg_filenode_relation(b .reltablespace , b .relfilenode )
40+ )
3241 join pg_namespace as n on c .relnamespace = n .oid
33- where b .relfilenode is not null
42+ where b .reldatabase = (select oid from pg_database where datname = current_database())
43+ and b .relfilenode is not null
44+ and b .relforknumber = 0
3445 and n .nspname not in (' pg_catalog' , ' information_schema' )
3546 and n .nspname !~ ' ^pg_toast'
3647 group by c .oid , n .nspname , c .relname , c .relkind
0 commit comments