-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathb6_buffercache.sql
More file actions
60 lines (59 loc) · 1.83 KB
/
b6_buffercache.sql
File metadata and controls
60 lines (59 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
-- Buffer cache contents (requires pg_buffercache; expensive on large shared_buffers)
with buf as (
select
c.oid as relid,
n.nspname as schema_name,
c.relname,
case c.relkind
when 'r' then 'table'
when 'i' then 'index'
when 't' then 'TOAST table'
when 'm' then 'materialized view'
when 'S' then 'sequence'
else c.relkind::text
end as object_type,
count(*) as buffers,
count(*) filter (where b.isdirty) as dirty_buffers,
round(
100.0 * count(*) / (
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(
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 c.oid = (
select pg_filenode_relation(b.reltablespace, b.relfilenode)
)
join pg_namespace as n on c.relnamespace = n.oid
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
)
select
coalesce(nullif(schema_name, 'public') || '.', '') || relname as "Object",
object_type as "Type",
rel_size as "Size",
cached_size as "Cached",
pct_of_rel || '%' as "% of Rel",
pct_of_cache || '%' as "% of Cache",
buffers as "Buffers",
dirty_buffers as "Dirty"
from buf
order by buffers desc
limit 50;