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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
| create view dba.v_freeze as select e.*, a.* from (select current_setting('autovacuum_freeze_max_age')::int as v1, current_setting('autovacuum_multixact_freeze_max_age')::int as v2, current_setting('vacuum_freeze_min_age')::int as v3, current_setting('vacuum_multixact_freeze_min_age')::int as v4, current_setting('vacuum_freeze_table_age')::int as v5, current_setting('vacuum_multixact_freeze_table_age')::int as v6, current_setting('autovacuum_vacuum_cost_delay') as v7, current_setting('autovacuum_vacuum_cost_limit') as v8, current_setting('vacuum_cost_delay') as v9, current_setting('vacuum_cost_limit') as v10, current_setting('autovacuum') as autovacuum ) a, LATERAL ( select pg_size_pretty(pg_total_relation_size(oid)) sz, oid::regclass as reloid, relkind, coalesce( least( substring(reloptions::text, 'autovacuum_freeze_max_age=(\d+)')::int, substring(reloptions::text, 'autovacuum_freeze_table_age=(\d+)')::int ), a.v1 ) - age(case when relfrozenxid::text::int<3 then null else relfrozenxid end) as remain_ages_xid, coalesce( least( substring(reloptions::text, 'autovacuum_multixact_freeze_max_age=(\d+)')::int, substring(reloptions::text, 'autovacuum_multixact_freeze_table_age=(\d+)')::int ), a.v2 ) - age(case when relminmxid::text::int<3 then null else relminmxid end) as remain_ages_mxid, coalesce( least( substring(reloptions::text, 'autovacuum_freeze_min_age=(\d+)')::int ), a.v3 ) as xid_lower_to_minage, coalesce( least( substring(reloptions::text, 'autovacuum_multixact_freeze_min_age=(\d+)')::int ), a.v4 ) as mxid_lower_to_minage, case when v5 <= age(case when relfrozenxid::text::int<3 then null else relfrozenxid end) then 'YES' else 'NOT' end as vacuum_trigger_freeze1, case when v6 <= age(case when relminmxid::text::int<3 then null else relminmxid end) then 'YES' else 'NOT' end as vacuum_trigger_freeze2, reloptions from pg_class where relkind in ('r','m') ) e order by least(e.remain_ages_xid , e.remain_ages_mxid), pg_total_relation_size(reloid) desc ;
create view dba.v_freeze_stat as select wb, cnt, pg_size_pretty(ssz) as ssz1, pg_size_pretty(ssz) as ssz2, pg_size_pretty(ssz*3) as ssz3, pg_size_pretty(min_sz) as ssz4, pg_size_pretty(max_sz) as ssz5, pg_size_pretty(avg_sz) as ssz6, pg_size_pretty(stddev_sz) as ssz7, min_rest_age, max_rest_age, stddev_rest_age, corr_rest_age_sz, round(100*(ssz/(sum(ssz) over ())), 2)||' %' as ratio from ( select a.*, b.* from ( select min(least(remain_ages_xid, remain_ages_mxid)) as v_min, max(least(remain_ages_xid, remain_ages_mxid)) as v_max from v_freeze ) as a, LATERAL ( select width_bucket( least(remain_ages_xid, remain_ages_mxid), a.v_min, a.v_max, greatest((a.v_max-a.v_min)/1000000, 1) ) as wb, count(*) as cnt, sum(pg_total_relation_size(reloid)) as ssz, stddev_samp(pg_total_relation_size(reloid) order by least(remain_ages_xid, remain_ages_mxid)) as stddev_sz, min(pg_total_relation_size(reloid)) as min_sz, max(pg_total_relation_size(reloid)) as max_sz, avg(pg_total_relation_size(reloid)) as avg_sz, min(least(remain_ages_xid, remain_ages_mxid)) as min_rest_age, max(least(remain_ages_xid, remain_ages_mxid)) as max_rest_age, stddev_samp(least(remain_ages_xid, remain_ages_mxid) order by least(remain_ages_xid, remain_ages_mxid)) as stddev_rest_age, corr(least(remain_ages_xid, remain_ages_mxid), pg_total_relation_size(reloid)) as corr_rest_age_sz from v_freeze group by wb ) as b ) t order by wb;
create view dba.v_freeze_stat_detail as select pg_size_pretty(t.ssz) as ssz2, pg_size_pretty(t.ssz*3) as ssz3, pg_size_pretty(t.ssz_sum) as ssz4, round(100*(t.ssz/t.ssz_sum), 2)||' %' as ratio_batch, round(100*(pg_total_relation_size(t.reloid)/t.ssz), 2)||' %' as ratio_table, t.* from ( select a.*, b.* from ( select min(least(remain_ages_xid, remain_ages_mxid)) as v_min, max(least(remain_ages_xid, remain_ages_mxid)) as v_max from v_freeze ) as a, LATERAL ( select count(*) over w as cnt, sum(pg_total_relation_size(reloid)) over () as ssz_sum, sum(pg_total_relation_size(reloid)) over w as ssz, pg_size_pretty(min(pg_total_relation_size(reloid)) over w) as min_sz, pg_size_pretty(max(pg_total_relation_size(reloid)) over w) as max_sz, pg_size_pretty(avg(pg_total_relation_size(reloid)) over w) as avg_sz, pg_size_pretty(stddev_samp(pg_total_relation_size(reloid)) over w) as stddev_sz, min(least(remain_ages_xid, remain_ages_mxid)) over w as min_rest_age, max(least(remain_ages_xid, remain_ages_mxid)) over w as max_rest_age, stddev_samp(least(remain_ages_xid, remain_ages_mxid)) over w as stddev_rest_age, corr(least(remain_ages_xid, remain_ages_mxid), pg_total_relation_size(reloid)) over w as corr_rest_age_sz, t1.* from ( select width_bucket( least(tt.remain_ages_xid, tt.remain_ages_mxid), a.v_min, a.v_max, greatest((a.v_max-a.v_min)/1000000, 1) ) as wb, * from v_freeze tt ) as t1 window w as ( partition by t1.wb ) ) as b ) t order by t.wb, least(t.remain_ages_xid, t.remain_ages_mxid), pg_total_relation_size(t.reloid) desc ; create view dba.top20freezebigtable as select relowner::regrole, relnamespace::regnamespace, relname, age(relfrozenxid),pg_size_pretty(pg_total_relation_size(oid)) , coalesce( least( substring(reloptions::text, 'autovacuum_freeze_max_age=(\d+)')::int, substring(reloptions::text, 'autovacuum_freeze_table_age=(\d+)')::int ), current_setting('autovacuum_freeze_max_age')::int ) - age(case when relfrozenxid::text::int<3 then null else relfrozenxid end) as remain_ages_xid, coalesce( least( substring(reloptions::text, 'autovacuum_freeze_min_age=(\d+)')::int ), current_setting('vacuum_freeze_min_age')::int ) as xid_lower_to_minage from pg_class where relkind='r' order by pg_total_relation_size(oid) desc limit 20;
|