Skip to content

Commit 8253dd9

Browse files
committed
Add btree_bloat metric
Based on the bloat estimation queries from https://github.com/ioguix/pgsql-bloat-estimation
1 parent ca7813c commit 8253dd9

1 file changed

Lines changed: 117 additions & 3 deletions

File tree

internal/metrics/metrics.yaml

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4187,9 +4187,123 @@ metrics:
41874187
pg_ls_archive_statusdir() a
41884188
where
41894189
name ~ '[0-9A-F]{24}.ready';
4190-
4191-
4192-
4190+
btree_bloat:
4191+
description:
4192+
Estimates B-tree indexes bloat by comparing actual index size against expected size derived from pg_stats and pg_class catalog metadata.
4193+
It reports bloat percentage, real size, extra bloat bytes, and fillfactor for each index, helping identify candidates for REINDEX or maintenance.
4194+
Based on the bloat estimation queries from https://github.com/ioguix/pgsql-bloat-estimation.
4195+
sqls:
4196+
14: |-
4197+
SELECT /* pgwatch_generated */
4198+
(extract(epoch from now()) * 1e9)::bigint as epoch_ns,
4199+
current_database() AS tag_dbname, nspname AS tag_schemaname, tblname AS tag_tblname, idxname as tag_idxname,
4200+
bs*(relpages)::bigint AS real_size,
4201+
bs*(relpages-est_pages)::bigint AS extra_size,
4202+
100 * (relpages-est_pages)::float / relpages AS extra_pct,
4203+
fillfactor,
4204+
CASE WHEN relpages > est_pages_ff
4205+
THEN bs*(relpages-est_pages_ff)
4206+
ELSE 0
4207+
END AS bloat_size,
4208+
100 * (relpages-est_pages_ff)::float / relpages AS bloat_pct,
4209+
is_na
4210+
-- , 100-(pst).avg_leaf_density AS pst_avg_bloat, est_pages, index_tuple_hdr_bm, maxalign, pagehdr, nulldatawidth, nulldatahdrwidth, reltuples, relpages -- (DEBUG INFO)
4211+
FROM (
4212+
SELECT coalesce(1 +
4213+
ceil(reltuples/floor((bs-pageopqdata-pagehdr)/(4+nulldatahdrwidth)::float)), 0 -- ItemIdData size + computed avg size of a tuple (nulldatahdrwidth)
4214+
) AS est_pages,
4215+
coalesce(1 +
4216+
ceil(reltuples/floor((bs-pageopqdata-pagehdr)*fillfactor/(100*(4+nulldatahdrwidth)::float))), 0
4217+
) AS est_pages_ff,
4218+
bs, nspname, tblname, idxname, relpages, fillfactor, is_na
4219+
-- , pgstatindex(idxoid) AS pst, index_tuple_hdr_bm, maxalign, pagehdr, nulldatawidth, nulldatahdrwidth, reltuples -- (DEBUG INFO)
4220+
FROM (
4221+
SELECT maxalign, bs, nspname, tblname, idxname, reltuples, relpages, idxoid, fillfactor,
4222+
( index_tuple_hdr_bm +
4223+
maxalign - CASE -- Add padding to the index tuple header to align on MAXALIGN
4224+
WHEN index_tuple_hdr_bm%maxalign = 0 THEN maxalign
4225+
ELSE index_tuple_hdr_bm%maxalign
4226+
END
4227+
+ nulldatawidth + maxalign - CASE -- Add padding to the data to align on MAXALIGN
4228+
WHEN nulldatawidth = 0 THEN 0
4229+
WHEN nulldatawidth::integer%maxalign = 0 THEN maxalign
4230+
ELSE nulldatawidth::integer%maxalign
4231+
END
4232+
)::numeric AS nulldatahdrwidth, pagehdr, pageopqdata, is_na
4233+
-- , index_tuple_hdr_bm, nulldatawidth -- (DEBUG INFO)
4234+
FROM (
4235+
SELECT n.nspname, i.tblname, i.idxname, i.reltuples, i.relpages,
4236+
i.idxoid, i.fillfactor, current_setting('block_size')::numeric AS bs,
4237+
CASE -- MAXALIGN: 4 on 32bits, 8 on 64bits (and mingw32 ?)
4238+
WHEN version() ~ 'mingw32' OR version() ~ '64-bit|x86_64|ppc64|ia64|amd64' THEN 8
4239+
ELSE 4
4240+
END AS maxalign,
4241+
/* per page header, fixed size: 20 for 7.X, 24 for others */
4242+
24 AS pagehdr,
4243+
/* per page btree opaque data */
4244+
16 AS pageopqdata,
4245+
/* per tuple header: add IndexAttributeBitMapData if some cols are null-able */
4246+
CASE WHEN max(coalesce(s.null_frac,0)) = 0
4247+
THEN 8 -- IndexTupleData size
4248+
ELSE 8 + (( 32 + 8 - 1 ) / 8) -- IndexTupleData size + IndexAttributeBitMapData size ( max num filed per index + 8 - 1 /8)
4249+
END AS index_tuple_hdr_bm,
4250+
/* data len: we remove null values save space using it fractionnal part from stats */
4251+
sum( (1-coalesce(s.null_frac, 0)) * coalesce(s.avg_width, 1024)) AS nulldatawidth,
4252+
max( CASE WHEN i.atttypid = 'pg_catalog.name'::regtype THEN 1 ELSE 0 END ) > 0 AS is_na
4253+
FROM (
4254+
SELECT ct.relname AS tblname, ct.relnamespace, ic.idxname, ic.attpos, ic.indkey, ic.indkey[ic.attpos], ic.reltuples, ic.relpages, ic.tbloid, ic.idxoid, ic.fillfactor,
4255+
coalesce(a1.attnum, a2.attnum) AS attnum, coalesce(a1.attname, a2.attname) AS attname, coalesce(a1.atttypid, a2.atttypid) AS atttypid,
4256+
CASE WHEN a1.attnum IS NULL
4257+
THEN ic.idxname
4258+
ELSE ct.relname
4259+
END AS attrelname
4260+
FROM (
4261+
SELECT idxname, reltuples, relpages, tbloid, idxoid, fillfactor, indkey,
4262+
pg_catalog.generate_series(1,indnatts) AS attpos
4263+
FROM (
4264+
SELECT ci.relname AS idxname, ci.reltuples, ci.relpages, i.indrelid AS tbloid,
4265+
i.indexrelid AS idxoid,
4266+
coalesce(substring(
4267+
array_to_string(ci.reloptions, ' ')
4268+
from 'fillfactor=([0-9]+)')::smallint, 90) AS fillfactor,
4269+
i.indnatts,
4270+
pg_catalog.string_to_array(pg_catalog.textin(
4271+
pg_catalog.int2vectorout(i.indkey)),' ')::int[] AS indkey
4272+
FROM pg_catalog.pg_index i
4273+
JOIN pg_catalog.pg_class ci ON ci.oid = i.indexrelid
4274+
WHERE ci.relam=(SELECT oid FROM pg_am WHERE amname = 'btree')
4275+
AND ci.relpages > 0
4276+
) AS idx_data
4277+
) AS ic
4278+
JOIN pg_catalog.pg_class ct ON ct.oid = ic.tbloid
4279+
LEFT JOIN pg_catalog.pg_attribute a1 ON
4280+
ic.indkey[ic.attpos] <> 0
4281+
AND a1.attrelid = ic.tbloid
4282+
AND a1.attnum = ic.indkey[ic.attpos]
4283+
LEFT JOIN pg_catalog.pg_attribute a2 ON
4284+
ic.indkey[ic.attpos] = 0
4285+
AND a2.attrelid = ic.idxoid
4286+
AND a2.attnum = ic.attpos
4287+
) i
4288+
JOIN pg_catalog.pg_namespace n ON n.oid = i.relnamespace
4289+
JOIN pg_catalog.pg_stats s ON s.schemaname = n.nspname
4290+
AND s.tablename = i.attrelname
4291+
AND s.attname = i.attname
4292+
GROUP BY 1,2,3,4,5,6,7,8,9,10,11
4293+
) AS rows_data_stats
4294+
) AS rows_hdr_pdg_stats
4295+
) AS relation_stats
4296+
WHERE (bs * relpages::float / (1024 * 1024)) > 1 /* exclude indexes below 1 MiB */
4297+
ORDER BY is_na, bloat_pct desc
4298+
LIMIT 100
4299+
gauges:
4300+
- real_size
4301+
- extra_size
4302+
- extra_pct
4303+
- fillfactor
4304+
- bloat_size
4305+
- bloat_pct
4306+
- is_na
41934307
presets:
41944308
aiven:
41954309
description: aiven database metrics

0 commit comments

Comments
 (0)