@@ -27,10 +27,12 @@ with data as (
2727 coalesce(substring(array_to_string(tbl.reloptions, ' ') from '%fillfactor=#"__#"%' for '#')::int2, 100) as fillfactor,
2828 current_setting('block_size')::numeric as bs,
2929 case when version() ~ 'mingw32|64-bit|x86_64|ppc64|ia64|amd64' then 8 else 4 end as ma, -- NS: TODO: check it
30+
3031 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+
3436 bool_or(att.atttypid = 'pg_catalog.name'::regtype) or count(att.attname) <> count(s.attname) as is_na
3537 from pg_attribute as att
3638 join pg_class as tbl on att.attrelid = tbl.oid and tbl.relkind = 'r'
@@ -42,11 +44,10 @@ with data as (
4244 and s.attname = att.attname
4345 left join pg_class as toast on tbl.reltoastrelid = toast.oid
4446 where
45- att.attnum > 0
46- and not att.attisdropped
47+ not att.attisdropped
4748 and (s.schemaname <> 'information_schema' or s.schemaname is null)
4849 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
5051 order by 2, 3
5152 ), step2 as (
5253 select
@@ -64,7 +65,7 @@ with data as (
6465 *,
6566 ceil(reltuples / ((bs - page_hdr) / tpl_size)) + ceil(toasttuples / 4) as est_tblpages,
6667 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)
6869 from step2
6970 ), step4 as (
7071 select
@@ -86,7 +87,7 @@ with data as (
8687 then 100 * (tblpages - est_tblpages_ff) / tblpages::float
8788 else 0
8889 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)
9091 from step3
9192 left join pg_stat_user_tables su on su.relid = tblid
9293 -- WHERE NOT is_na
0 commit comments