Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions sql/b6_buffercache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pct_of_cache denominator currently counts all forks, but the main query filters to b.relforknumber = 0. If the intent is “main fork only” everywhere, consider adding the same filter to the denominator so the percentage stays consistent.

Suggested change
where reldatabase = (select oid from pg_database where datname = current_database())
where reldatabase = (select oid from pg_database where datname = current_database())
and relfilenode is not null
and relforknumber = 0

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)
)
Comment on lines +37 to +40
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor readability/perf nit: the scalar subquery wrapper around pg_filenode_relation(...) doesn’t buy much here and can be inlined in the join condition.

Suggested change
join pg_class as c
on c.oid = (
select pg_filenode_relation(b.reltablespace, b.relfilenode)
)
on c.oid = 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
Expand Down