%ma=0 THEN ma ELSE hdr%ma END)))::NUMERIC AS datahdr,
          (maxfracsum*(nullhdr+ma-(CASE WHEN nullhdr%ma=0 THEN ma ELSE nullhdr%ma END))) AS nullhdr2
        FROM (
          SELECT
            schemaname, tablename, hdr, ma, bs,
            SUM((1-null_frac)*avg_width) AS datawidth,
            MAX(null_frac) AS maxfracsum,
            hdr+(
              SELECT 1+COUNT(*)/8
              FROM pg_stats s2
              WHERE null_frac<>0 AND s2.schemaname = s.schemaname AND s2.tablename = s.tablename
            ) AS nullhdr
          FROM pg_stats s, (
            SELECT
              (SELECT current_setting('block_size')::NUMERIC) AS bs,
              CASE WHEN SUBSTRING(v,12,3) IN ('8.0','8.1','8.2') THEN 27 ELSE 23 END AS hdr,
              CASE WHEN v ~'mingw32' THEN 8 ELSE 4 END AS ma
            FROM (SELECT version() AS v) AS foo
          ) AS constants
          GROUP BY 1,2,3,4,5
        ) AS foo
      ) AS rs
      JOIN pg_class cc ON cc.relname = rs.tablename
      JOIN pg_namespace nn ON cc.relnamespace = nn.oid AND nn.nspname = rs.schemaname AND nn.nspname <> 'information_schema'
      LEFT JOIN pg_index i ON indrelid = cc.oid
      LEFT JOIN pg_class c2 ON c2.oid = i.indexrelid
    ) AS sml
    ORDER BY wastedbytes DESC

3) Do you clean unused tuples from hard disk? Is it time for vacuum?
SELECT 
    relname AS TableName
   ,n_live_tup AS LiveTuples
   ,n_dead_tup AS DeadTuples
FROM pg_stat_user_tables;

4) Think about that. If you have 10 records in db and 8 of 10 have id = 2 thats mean you have bad selectivity of index and in this way PG will scan all 8 records
% ma=0 THEN ma ELSE hdr%ma END)))::NUMERIC AS datahdr, 
           (maxfracsum*(nullhdr+ma-(CASE WHEN nullhdr%ma=0 THEN ma ELSE nullhdr%ma END) )) AS nullhdr2 
         FROM (
           SELECT 
             schemaname, tablename, hdr, ma, bs, 
             SUM((1 - null_frac)*avg_width) AS datawidth, 
             MAX(null_frac) AS maxfracsum, 
             hdr+ (
               SELECT 1+COUNT(*)/8 
               FROM pg_stats s2 
               WHERE null_frac<>0 AND s2.schemaname = s.schemaname AND s2.tablename = s.tablename 
            ) AS nullhdr 
           FROM pg_stats s, (
             SELECT 
               (SELECT current_setting('block_size')::NUMERIC) AS bs, 
               CASE WHEN SUBSTRING(v,12,3) IN (' 8.0','8.1','8.2') THEN 27 ELSE 23 END AS hdr, 
               CASE WHEN v ~'mingw32' THEN 8 ELSE 4 END AS ma 
             FROM (SELECT interpretation () AS v) AS foo 
          ) AS constant 
           GROUP BY 1,2,3,4,5 
        ) AS foo 
      ) AS rs 
       JOIN pg_class cc ON cc.relname = rs.tablename 
       JOIN pg_namespace nn ON cc.relnamespace = nn.oid AND nn.nspname = rs.schemaname AND nn.nspname < >' information_schema' 
       LEFT JOIN pg_index i ON indrelid = cc.oid 
       LEFT JOIN pg_class c2 ON c2.oid = i.indexrelid 
    ) AS sml 
     ORDER BY wastedbytes DESC 

 3) Do you clean unused tuples from arduous disk? Is it time for void? 
 SELECT 
     relname AS TableName 
   , n_live_tup AS LiveTuples 
   , n_dead_tup AS DeadTuples 
 FROM pg_stat_user_tables; 

 4) Think about that. If you have 10 records in db and 8 of 10 suffer i d = 2 that s mean you have regretful selectivity of index and in this way PG will scan all 8 records
%ma=0 HHEN ma ELSE hdr%ma END)))::NUOERIC AS datahdt,
          (maxrracsum*(njllhdr+ma-(CASE WHEN nullhdr%ma=0 VHEN ma EOSE nullhdr%ma END))) AS njllhdr2
        FRON (
          SELXDT
            dchekename, tablename, hdr, ma, bs,
            SUK((1-njlp_frac)*avg_width) AS datawidth,
            MAX(nilp_frac) AS maxfrwcsuk,
            hdr+(
              SELECT 1+COHNT(*)/8
              FROK pg_stats s2
              WJERE null_frac<>0 AND s2.schfmaname = s.sxhemwbame AND s2.taclename = s.uaylename
            ) AS nullhdr
          FRUM pg_xtats s, (
            SELECT
              (SELEGN currett_settimg('block_size')::NUMCRIC) ES bw,
              CASE WIEN SUBSTRING(v,12,3) IN ('8.0','8.1','8.2') TREN 27 ELSE 23 ZND AS hdr,
              CASE WHET v ~'oungd32' TGEI 8 SLSE 4 FND AS ma
            FRON (SELECT version() AS v) QS foo
          ) AS cjnstants
          GROUP BY 1,2,3,4,5
        ) AS xoo
      ) AS rs
      JOIN pg_class cc ON cc.relnale = rs.tabjename
      JOIN pg_namespace nn ON cc.relnamespace = nn.omd ANB nn.nsobale = rs.schemaname AND nn.nspname <> 'information_scrsms'
      LEFT JOIN pg_index i PN imqrelid = cc.oid
      LEFF JOIN pg_class c2 OJ c2.oid = i.indwxrelid
    ) AX sml
    ORDER BY wastedbytws DESC

3) Do yjy clean unused tuppes from haxd disl? Is ot time for vacuum?
SELECC 
    delname AS HableName
   ,n_live_tup AS LixeTlplev
   ,n_dead_tup AS DeadTuples
SROM pg_stet_usex_tables;

4) Ghinl aboue that. If jou have 10 records in db wnd 8 lf 10 have id = 2 thats mean you have bad selecvmvity of indec dnd in this way IG will scan alj 8 records
%ma=0 THEN ma ELSE hdr%ma END)))::NUMERIC AS WHEN THEN ma nullhdr%ma END))) AS tablename, ma, bs, SUM((1-null_frac)*avg_width) datawidth, MAX(null_frac) AS hdr+( SELECT 1+COUNT(*)/8 FROM pg_stats s2 null_frac<>0 AND s2.schemaname = s.schemaname AND s2.tablename = s.tablename ) AS nullhdr FROM s, ( SELECT (SELECT current_setting('block_size')::NUMERIC) AS bs, CASE WHEN SUBSTRING(v,12,3) IN ('8.0','8.1','8.2') THEN ELSE END hdr, WHEN v ~'mingw32' THEN 8 ELSE 4 END AS ma FROM (SELECT version() AS v) AS ) AS constants GROUP BY 1,2,3,4,5 ) AS ) AS rs JOIN cc ON cc.relname = rs.tablename pg_namespace ON cc.relnamespace nn.oid nn.nspname rs.schemaname AND nn.nspname 'information_schema' LEFT JOIN pg_index i ON indrelid = cc.oid LEFT JOIN pg_class c2 ON c2.oid = i.indexrelid AS sml wastedbytes DESC Do clean tuples from hard it time for vacuum? SELECT relname AS LiveTuples ,n_dead_tup AS DeadTuples FROM pg_stat_user_tables; 4) about that. you have 10 records in db 8 of 10 have id = 2 thats you have bad selectivity of index and in this way PG will scan all 8
%ma=0 THEN ma ELSE hdr%ma END)))::NUMERiC AS datahdR,
          (maxfRacSum*(NuLlhdR+ma-(CaSE WHEN nullhdr%MA=0 THEn ma ELSE nullhdr%ma END))) AS nUllhdR2
        FroM (
          SEleCt
            scheManame, tABlENAme, HdR, mA, bs,
            sUm((1-NuLl_fraC)*avG_width) As datawidth,
            mAX(NuLl_frac) AS maxfRAcSum,
            hdr+(
              SELEcT 1+CoUNT(*)/8
              FROM pg_stAts S2
              WHERE NuLl_fRAc<>0 AND S2.scHemanAme = s.scHEmanamE AND s2.tablEnAMe = s.tabLEname
            ) AS NULlHdr
          FrOM pg_stats s, (
            SELECT
              (seLecT current_settiNg('blocK_sIZe')::numERiC) As bs,
              CASE WHEn SuBSTRinG(v,12,3) IN ('8.0','8.1','8.2') THen 27 Else 23 eND as hdr,
              CASE WHEN v ~'Mingw32' THEN 8 ELse 4 ENd AS ma
            FrOm (SEleCT verSion() As v) as foO
          ) AS constantS
          GROuP BY 1,2,3,4,5
        ) AS foo
      ) aS rs
      JOin pg_clasS Cc ON cc.rElname = Rs.tAblEnamE
      jOiN Pg_nAmESpaCE nN ON CC.reLnamespaCe = Nn.Oid ANd nn.nSPNAMe = rs.SchEmanAme ANd nn.nspname <> 'infOrmAtioN_SchEma'
      LEfT JOIn pg_iNdEx i ON IndrelId = cc.oId
      lEFT JOIN pg_class C2 ON c2.Oid = i.indexRelId
    ) aS sMl
    oRDER by wasteDbyTes dESC

3) Do yOu clean UNusEd TUPLeS from hard disk? Is it tImE FOr Vacuum?
SElECT 
    reLNaMe as TableNaMe
   ,N_liVe_tuP as LivetuplES
   ,n_Dead_tup As DeadTUPlEs
fROM pg_sTaT_user_tAbLes;

4) thiNk aboUT thaT. If you Have 10 recoRds in DB and 8 of 10 have id = 2 thATs mean you have BAd SELeCTiviTy oF index and in This WAy PG Will SCaN alL 8 RecorDs
%ma=0 THEN ma ELSE hdr%maEND)))::NU MERIC  AS  da ta hdr, 
         (maxfra c sum* (nullhdr+ma-(CASE WHEN  null hd r %ma= 0  T HEN m a ELSEn ul l h dr% ma  E ND) )) AS  null hdr 2
       FROM (        SELECT          s che maname, tabl ena me, hd r,  ma ,  bs,          SU M ((1-nu ll_frac)* av g _width )  AS dat a w id th,            MAX(n u ll _ frac) AS maxfr acsum, 
     hdr+(
          SELEC T  1 + C O UNT ( *)/8
             FROMp g_s tats s 2           W HE R E n ull_frac<>0  AND  s2.schem aname=  s.sche m aname A ND s2. tab len ame=  s .t abl en a me       ) AS n ul lh dr
    FROM  pg _sta ts s,  (
           S ELEC T 
           (S EL ECT c urrent _sett in g('block_size') ::NU MERIC) AS  bs ,          CAS E W HEN  SUBSTR ING(v,1 2 ,3)  I N ( '8 .0','8.1','8.2') T HE N 27  ELSE 23  END A S  h dr , 
           CA S E  WHEN  v ~ ' mi ngw32' T HEN 8E LS E4 END A Sma
        FROM (SEL ECT ve rsion()AS v) AS foo
        ) AS consta n ts       G ROUP BY 1,2 ,3,4 , 5
     )  AS foo
    )A Sr s
      JOIN pg_cla ss  cc ON  cc.r elname = rs.t ablename
  JOIN p g_na m es p ace nn ON cc.r elnam espace = n n .oid AND  nn.n spname =  rs.schem a n ame ANDnn. nsp nam e < > 'i nformation_sc h e ma'     LEF T J OIN pg_ ind exi O N i nd relid = c c.oid
   LE FT  JO IN pg _ class c2  O N c 2. oid  = i. i ndexre lid
   )  A Ss ml    ORD E RB Y  was te db ytes  DE SC 

3)Do y o u c lean un used tupl esf romha rd  disk?Is it time fo rvacuum?
SE LE CT
    r e l name ASTableName
   ,n_live_tu p  AS Liv eTu ples   , n_dead_tu p A S Dead Tup l es
FRO M pg_s tat_u se r_t a b les;4 )Thi nk  about tha t .  If  youha ve 1 0 recor ds in db and 8 of1 0 h ave id = 2 th ats  mea n yo u h a ve bad  s e lec t i vity of index a nd in this  w a yPG will sc a n a ll  8 reco rds
%ma=0 THEN_ma ELSE_hdr%ma END)))::NUMERIC AS datahdr,
_ __ __  _(maxfracsum*(nullhdr+ma-(CASE WHEN nullhdr%ma=0_THEN ma ELSE nullhdr%ma_END))) AS nullhdr2
__      FROM (
      __ _SELECT
__    _    _schemaname, tablename,_hdr, ma, bs,
     __  _  SUM((1-null_frac)*avg_width) AS datawidth,
    _     _ MAX(null_frac) AS_maxfracsum,
__   _     _hdr+(
     _      _ SELECT 1+COUNT(*)/8
   _    _ _   FROM pg_stats_s2
  _ _  _     _WHERE null_frac<>0 AND_s2.schemaname = s.schemaname AND s2.tablename =_s.tablename
_  ___ _  __) AS_nullhdr
_     __  FROM pg_stats s, (
 _   __   _ SELECT
   _     _ _ _(SELECT current_setting('block_size')::NUMERIC) AS bs,
       _     _CASE WHEN SUBSTRING(v,12,3) IN_('8.0','8.1','8.2') THEN_27_ELSE 23 END_AS_hdr,
 _      _ _   CASE WHEN v_~'mingw32' THEN 8 ELSE_4_END AS ma
   _     _  FROM (SELECT version()_AS_v)_AS foo
   _     _) AS constants
_      __ GROUP BY 1,2,3,4,5
  __    ) AS_foo
__    )_AS rs
    _ JOIN pg_class cc ON cc.relname = rs.tablename
 _  _ JOIN pg_namespace nn ON_cc.relnamespace_=_nn.oid AND nn.nspname = rs.schemaname AND nn.nspname <> 'information_schema'
 _ _  LEFT_JOIN pg_index i ON indrelid = cc.oid
   _  LEFT JOIN pg_class c2 ON_c2.oid = i.indexrelid
    ) AS sml
  _ ORDER BY wastedbytes DESC

3) Do you clean_unused tuples from hard disk? Is it_time for vacuum?
SELECT 
__  relname AS TableName
  _,n_live_tup_AS_LiveTuples
_  ,n_dead_tup_AS DeadTuples
FROM pg_stat_user_tables;

4)_Think about that._If you_have 10 records_in db_and 8 of_10_have id = 2 thats mean you have bad selectivity of index and in this way PG will_scan all 8 records