Skip to content

Commit e482e6b

Browse files
authored
Merge pull request #87 from NikolayS/fix/b6-buffercache-improvements
fix(b6): correct buffer attribution and pct_of_rel calculation
2 parents 9aa6b97 + 91f65b2 commit e482e6b

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

sql/b6_buffercache.sql

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)