@@ -27,10 +27,12 @@ with data as (
27
27
coalesce(substring(array_to_string(tbl.reloptions, ' ') from '%fillfactor=#"__#"%' for '#')::int2, 100) as fillfactor,
28
28
current_setting('block_size')::numeric as bs,
29
29
case when version() ~ 'mingw32|64-bit|x86_64|ppc64|ia64|amd64' then 8 else 4 end as ma, -- NS: TODO: check it
30
+
30
31
24 as page_hdr,
31
- 23 + case when max(coalesce(null_frac, 0)) > 0 then (7 + count(*)) / 8 else 0::int end
32
- + case when tbl.relhasoids then 4 else 0 end as tpl_hdr_size,
33
- sum((1 - coalesce(s.null_frac, 0)) * coalesce(s.avg_width, 1024)) as tpl_data_size,
32
+ 23 + case when max(coalesce(s.null_frac,0)) > 0 then (7 + count(s.attname)) / 8 else 0::int end
33
+ + case when bool_or(att.attname = 'oid' and att.attnum < 0) then 4 else 0 end as tpl_hdr_size,
34
+ sum((1 - coalesce(s.null_frac, 0)) * coalesce(s.avg_width, 0) ) as tpl_data_size,
35
+
34
36
bool_or(att.atttypid = 'pg_catalog.name'::regtype) or count(att.attname) <> count(s.attname) as is_na
35
37
from pg_attribute as att
36
38
join pg_class as tbl on att.attrelid = tbl.oid and tbl.relkind = 'r'
@@ -42,11 +44,10 @@ with data as (
42
44
and s.attname = att.attname
43
45
left join pg_class as toast on tbl.reltoastrelid = toast.oid
44
46
where
45
- att.attnum > 0
46
- and not att.attisdropped
47
+ not att.attisdropped
47
48
and (s.schemaname <> 'information_schema' or s.schemaname is null)
48
49
and tbl.relpages > ${MIN_RELPAGES}
49
- group by 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, tbl.relhasoids
50
+ group by 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
50
51
order by 2, 3
51
52
), step2 as (
52
53
select
@@ -64,7 +65,7 @@ with data as (
64
65
*,
65
66
ceil(reltuples / ((bs - page_hdr) / tpl_size)) + ceil(toasttuples / 4) as est_tblpages,
66
67
ceil(reltuples / ((bs - page_hdr) * fillfactor / (tpl_size * 100))) + ceil(toasttuples / 4) as est_tblpages_ff
67
- -- , stattuple. pgstattuple(tblid) as pst
68
+ -- , tpl_hdr_size, tpl_data_size, pgstattuple(tblid) AS pst -- (DEBUG INFO)
68
69
from step2
69
70
), step4 as (
70
71
select
@@ -86,7 +87,7 @@ with data as (
86
87
then 100 * (tblpages - est_tblpages_ff) / tblpages::float
87
88
else 0
88
89
end as bloat_ratio
89
- -- , (pst).free_percent + (pst).dead_tuple_percent as real_frag
90
+ -- , tpl_hdr_size, tpl_data_size, (pst).free_percent + (pst).dead_tuple_percent AS real_frag -- (DEBUG INFO)
90
91
from step3
91
92
left join pg_stat_user_tables su on su.relid = tblid
92
93
-- WHERE NOT is_na
0 commit comments