                     SELECT ( 4 + tpl_hdr_size + tpl_data_size + (2*ma)
                                - CASE WHEN tpl_hdr_size%ma = 0 THEN ma ELSE tpl_hdr_size%ma END
                                - CASE WHEN ceil(tpl_data_size)::int%ma = 0 THEN ma ELSE ceil(tpl_data_size)::int%ma END
-                           ) AS tpl_size, block_size - page_hdr AS size_per_block, (heappages + toastpages) AS tblpages, heappages,
-                           toastpages, reltuples, toasttuples, block_size, page_hdr, tblid, schemaname, tblname, fillfactor, is_na
+                           ) AS tpl_size
+                           ,block_size - page_hdr AS size_per_block
+                           ,(heappages + toastpages) AS tblpages
+                           ,heappages
+                           ,toastpages
+                           ,reltuples
+                           ,toasttuples
+                           ,block_size
+                           ,page_hdr
+                           ,tblid
+                           ,schemaname
+                           ,tblname
+                           ,fillfactor
+                           ,is_na
+                           ,n_dead_tup
+                           ,n_live_tup
                           FROM (
-                                SELECT  tbl.oid AS tblid
-                                       ,ns.nspname AS schemaname
-                                       ,tbl.relname AS tblname
-                                       ,tbl.reltuples
-                                       ,tbl.relpages AS heappages
-                                       ,coalesce(toast.relpages, 0) AS toastpages
-                                       ,coalesce(toast.reltuples, 0) AS toasttuples
+                                SELECT  tbl.oid                       AS tblid
+                                       ,ns.nspname                    AS schemaname
+                                       ,tbl.relname                   AS tblname
+                                       ,tbl.reltuples                 AS reltuples
+                                       ,tbl.relpages                  AS heappages
+                                       ,coalesce(toast.relpages, 0)   AS toastpages
+                                       ,coalesce(toast.reltuples, 0)  AS toasttuples
+                                       ,psat.n_dead_tup               AS n_dead_tup
+                                       ,psat.n_live_tup               AS n_live_tup
+                                       ,24                            AS page_hdr
+                                       ,current_setting('block_size')::numeric AS block_size
                                        ,coalesce(substring( array_to_string(tbl.reloptions, ' ') FROM 'fillfactor=([0-9]+)')::smallint, 100) AS fillfactor
-                                       ,current_setting('block_size')::numeric AS block_size
-                                       ,CASE WHEN version()~'mingw32' OR version()~'64-bit|x86_64|ppc64|ia64|amd64' THEN 8 ELSE 4 END AS ma
-                                       ,24 AS page_hdr
-                                       ,23 + CASE WHEN MAX(coalesce(null_frac,0)) > 0 THEN ( 7 + count(*) ) / 8 ELSE 0::int END AS tpl_hdr_size
-                                       ,sum( (1-coalesce(s.null_frac, 0)) * coalesce(s.avg_width, 1024) ) AS tpl_data_size
-                                       ,bool_or(att.atttypid = 'pg_catalog.name'::regtype) OR count(att.attname) <> count(s.attname) AS is_na                                                                                                                                FROM pg_attribute AS att
-                                  JOIN pg_class AS tbl ON att.attrelid = tbl.oid
-                                  JOIN pg_namespace AS ns ON ns.oid = tbl.relnamespace
-                                  LEFT JOIN pg_stats AS s ON s.schemaname=ns.nspname
-                                   AND s.tablename = tbl.relname AND s.inherited=false AND s.attname=att.attname
-                            LEFT JOIN pg_class AS toast ON tbl.reltoastrelid = toast.oid
-                                WHERE att.attnum > 0 AND NOT att.attisdropped
-                                  AND tbl.relkind = 'r'
-                             GROUP BY 1,2,3,4,5,6,7,8,9,10
-                             ORDER BY 2,3
-                             ) AS s
+                                       ,CASE WHEN version()~'mingw32' OR version()~'64-bit|x86_64|ppc64|ia64|amd64' THEN 8 ELSE 4 END        AS ma
+                                       ,23 + CASE WHEN MAX(coalesce(null_frac,0)) > 0 THEN ( 7 + count(*) ) / 8 ELSE 0::int END              AS tpl_hdr_size
+                                       ,sum( (1-coalesce(s.null_frac, 0)) * coalesce(s.avg_width, 1024) )                                    AS tpl_data_size
+                                       ,bool_or(att.atttypid = 'pg_catalog.name'::regtype) OR count(att.attname) <> count(s.attname)         AS is_na
+                                  FROM  pg_attribute       AS att
+                                  JOIN  pg_class           AS tbl    ON (att.attrelid = tbl.oid)
+                                  JOIN  pg_stat_all_tables AS psat   ON (tbl.oid = psat.relid)
+                                  JOIN  pg_namespace       AS ns     ON (ns.oid = tbl.relnamespace)
+                             LEFT JOIN  pg_stats           AS s      ON (s.schemaname=ns.nspname AND s.tablename = tbl.relname AND s.inherited=false AND s.attname=att.attname)
+                             LEFT JOIN  pg_class           AS toast  ON (tbl.reltoastrelid = toast.oid)
+                                 WHERE  att.attnum > 0
+                                   AND  NOT att.attisdropped
+                                   AND  tbl.relkind = 'r'
+                              GROUP BY  tbl.oid, ns.nspname, tbl.relname, tbl.reltuples, tbl.relpages, toastpages, toasttuples, fillfactor, block_size, ma, n_dead_tup, n_live_tup
+                              ORDER BY  schemaname, tblname
+                           ) AS s
                  ) AS s2
        ) AS s3
- ORDER BY bloat_ratio DESC
+ ORDER BY bloat_size DESC
 )
 SELECT * FROM report WHERE bloat_ratio != 0;
 

                  ,tblid
                  ,schemaname
                  ,tblname
                  ,heappages
                  ,toastpages
                  ,is_na

WITH report AS (
   SELECT   schemaname
           ,tblname
           ,n_dead_tup
           ,n_live_tup
           ,block_size*tblpages AS real_size
           ,(tblpages-est_tblpages)*block_size AS extra_size
           ,CASE WHEN tblpages - est_tblpages > 0
