Skip to content

Commit f1f7d13

Browse files
committed
Support PostgreSQL 12
1 parent fd4d039 commit f1f7d13

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

table/table_bloat.sql

+10-8
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,20 @@ SELECT current_database(), schemaname, tblname, bs*tblpages AS real_size,
1515
THEN 100 * (tblpages - est_tblpages_ff)/tblpages::float
1616
ELSE 0
1717
END AS bloat_ratio, is_na
18-
-- , (pst).free_percent + (pst).dead_tuple_percent AS real_frag
18+
-- , tpl_hdr_size, tpl_data_size, (pst).free_percent + (pst).dead_tuple_percent AS real_frag -- (DEBUG INFO)
1919
FROM (
2020
SELECT ceil( reltuples / ( (bs-page_hdr)/tpl_size ) ) + ceil( toasttuples / 4 ) AS est_tblpages,
2121
ceil( reltuples / ( (bs-page_hdr)*fillfactor/(tpl_size*100) ) ) + ceil( toasttuples / 4 ) AS est_tblpages_ff,
2222
tblpages, fillfactor, bs, tblid, schemaname, tblname, heappages, toastpages, is_na
23-
-- , stattuple.pgstattuple(tblid) AS pst
23+
-- , tpl_hdr_size, tpl_data_size, pgstattuple(tblid) AS pst -- (DEBUG INFO)
2424
FROM (
2525
SELECT
2626
( 4 + tpl_hdr_size + tpl_data_size + (2*ma)
2727
- CASE WHEN tpl_hdr_size%ma = 0 THEN ma ELSE tpl_hdr_size%ma END
2828
- CASE WHEN ceil(tpl_data_size)::int%ma = 0 THEN ma ELSE ceil(tpl_data_size)::int%ma END
2929
) AS tpl_size, bs - page_hdr AS size_per_block, (heappages + toastpages) AS tblpages, heappages,
3030
toastpages, reltuples, toasttuples, bs, page_hdr, tblid, schemaname, tblname, fillfactor, is_na
31+
-- , tpl_hdr_size, tpl_data_size
3132
FROM (
3233
SELECT
3334
tbl.oid AS tblid, ns.nspname AS schemaname, tbl.relname AS tblname, tbl.reltuples,
@@ -39,23 +40,24 @@ FROM (
3940
current_setting('block_size')::numeric AS bs,
4041
CASE WHEN version()~'mingw32' OR version()~'64-bit|x86_64|ppc64|ia64|amd64' THEN 8 ELSE 4 END AS ma,
4142
24 AS page_hdr,
42-
23 + CASE WHEN MAX(coalesce(null_frac,0)) > 0 THEN ( 7 + count(*) ) / 8 ELSE 0::int END
43-
+ CASE WHEN tbl.relhasoids THEN 4 ELSE 0 END AS tpl_hdr_size,
44-
sum( (1-coalesce(s.null_frac, 0)) * coalesce(s.avg_width, 1024) ) AS tpl_data_size,
43+
23 + CASE WHEN MAX(coalesce(s.null_frac,0)) > 0 THEN ( 7 + count(s.attname) ) / 8 ELSE 0::int END
44+
+ CASE WHEN bool_or(att.attname = 'oid' and att.attnum < 0) THEN 4 ELSE 0 END AS tpl_hdr_size,
45+
sum( (1-coalesce(s.null_frac, 0)) * coalesce(s.avg_width, 0) ) AS tpl_data_size,
4546
bool_or(att.atttypid = 'pg_catalog.name'::regtype)
46-
OR count(att.attname) <> count(s.attname) AS is_na
47+
OR sum(CASE WHEN att.attnum > 0 THEN 1 ELSE 0 END) <> count(s.attname) AS is_na
4748
FROM pg_attribute AS att
4849
JOIN pg_class AS tbl ON att.attrelid = tbl.oid
4950
JOIN pg_namespace AS ns ON ns.oid = tbl.relnamespace
5051
LEFT JOIN pg_stats AS s ON s.schemaname=ns.nspname
5152
AND s.tablename = tbl.relname AND s.inherited=false AND s.attname=att.attname
5253
LEFT JOIN pg_class AS toast ON tbl.reltoastrelid = toast.oid
53-
WHERE att.attnum > 0 AND NOT att.attisdropped
54+
WHERE NOT att.attisdropped
5455
AND tbl.relkind = 'r'
55-
GROUP BY 1,2,3,4,5,6,7,8,9,10, tbl.relhasoids
56+
GROUP BY 1,2,3,4,5,6,7,8,9,10
5657
ORDER BY 2,3
5758
) AS s
5859
) AS s2
5960
) AS s3
6061
-- WHERE NOT is_na
6162
-- AND tblpages*((pst).free_percent + (pst).dead_tuple_percent)::float4/100 >= 1
63+
ORDER BY schemaname, tblname;

0 commit comments

Comments
 (0)