Skip to content

Instantly share code, notes, and snippets.

@Turbo87
Created February 15, 2024 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Turbo87/80d03e5c0ee70beef2e2d2c7c01f9f52 to your computer and use it in GitHub Desktop.
Save Turbo87/80d03e5c0ee70beef2e2d2c7c01f9f52 to your computer and use it in GitHub Desktop.
crates.io database bloat
-- from https://github.com/ioguix/pgsql-bloat-estimation/blob/8fde3c9e0e015ece51e10c1450228d23f7747db1/table/table_bloat.sql
SELECT tblname, bs*tblpages AS real_size,
(tblpages-est_tblpages)*bs AS extra_size,
CASE WHEN tblpages > 0 AND tblpages - est_tblpages > 0
THEN 100 * (tblpages - est_tblpages)/tblpages::float
ELSE 0
END AS extra_pct, fillfactor,
CASE WHEN tblpages - est_tblpages_ff > 0
THEN (tblpages-est_tblpages_ff)*bs
ELSE 0
END AS bloat_size,
CASE WHEN tblpages > 0 AND tblpages - est_tblpages_ff > 0
THEN 100 * (tblpages - est_tblpages_ff)/tblpages::float
ELSE 0
END AS bloat_pct
-- , tpl_hdr_size, tpl_data_size, (pst).free_percent + (pst).dead_tuple_percent AS real_frag -- (DEBUG INFO)
FROM (
SELECT ceil( reltuples / ( (bs-page_hdr)/tpl_size ) ) + ceil( toasttuples / 4 ) AS est_tblpages,
ceil( reltuples / ( (bs-page_hdr)*fillfactor/(tpl_size*100) ) ) + ceil( toasttuples / 4 ) AS est_tblpages_ff,
tblpages, fillfactor, bs, tblid, schemaname, tblname, heappages, toastpages, is_na
-- , tpl_hdr_size, tpl_data_size, pgstattuple(tblid) AS pst -- (DEBUG INFO)
FROM (
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, bs - page_hdr AS size_per_block, (heappages + toastpages) AS tblpages, heappages,
toastpages, reltuples, toasttuples, bs, page_hdr, tblid, schemaname, tblname, fillfactor, is_na
-- , tpl_hdr_size, tpl_data_size
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,
coalesce(substring(
array_to_string(tbl.reloptions, ' ')
FROM 'fillfactor=([0-9]+)')::smallint, 100) AS fillfactor,
current_setting('block_size')::numeric AS bs,
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(s.null_frac,0)) > 0 THEN ( 7 + count(s.attname) ) / 8 ELSE 0::int END
+ CASE WHEN bool_or(att.attname = 'oid' and att.attnum < 0) THEN 4 ELSE 0 END AS tpl_hdr_size,
sum( (1-coalesce(s.null_frac, 0)) * coalesce(s.avg_width, 0) ) AS tpl_data_size,
bool_or(att.atttypid = 'pg_catalog.name'::regtype)
OR sum(CASE WHEN att.attnum > 0 THEN 1 ELSE 0 END) <> 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 NOT att.attisdropped
AND tbl.relkind in ('r','m')
GROUP BY 1,2,3,4,5,6,7,8,9,10
ORDER BY 2,3
) AS s
) AS s2
) AS s3
WHERE schemaname = 'public'
ORDER BY tblname;
tblname real_size extra_size extra_pct fillfactor bloat_size bloat_pct
__diesel_schema_migrations 16384 0 0 100 0 0
api_tokens 13737984 1794048 13.059033989266547 100 1794048 13.059033989266547
background_jobs 0 0 0 100 0 0
badges 1122304 212992 18.978102189781023 100 212992 18.978102189781023
categories 114688 90112 78.57142857142857 100 90112 78.57142857142857
crate_owner_invitations 229376 65536 28.571428571428573 100 65536 28.571428571428573
crate_owners 12419072 1531904 12.335092348284961 100 1531904 12.335092348284961
crates 3181912064 1856782336 58.354294482476305 100 1856782336 58.354294482476305
crates_categories 3555328 417792 11.751152073732719 100 417792 11.751152073732719
crates_keywords 9912320 1359872 13.71900826446281 100 1359872 13.71900826446281
dependencies 735862784 27099136 3.6826343972302316 100 27099136 3.6826343972302316
emails 5259264 73728 1.4018691588785046 100 73728 1.4018691588785046
follows 3309568 147456 4.455445544554456 100 147456 4.455445544554456
keywords 1875968 131072 6.986899563318778 100 131072 6.986899563318778
metadata 5513216 5505024 99.85141158989599 100 5505024 99.85141158989599
processed_log_files 278528 90112 32.35294117647059 100 90112 32.35294117647059
publish_limit_buckets 2351104 73728 3.1358885017421603 100 73728 3.1358885017421603
publish_rate_overrides 8192 0 0 100 0 0
readme_renderings 37928960 5939200 15.658747300215984 100 5939200 15.658747300215984
recent_crate_downloads 12681216 6619136 52.19638242894057 100 6619136 52.19638242894057
reserved_crate_names 8192 0 0 100 0 0
teams 139264 16384 11.764705882352942 100 16384 11.764705882352942
users 14729216 1695744 11.512791991101224 100 1695744 11.512791991101224
version_downloads 13443547136 1173258240 8.727296658619013 100 1173258240 8.727296658619013
version_owner_actions 54165504 2826240 5.217785843920145 100 2826240 5.217785843920145
versions 498188288 191332352 38.40563028250074 100 191332352 38.40563028250074
versions_published_by 53542912 3915776 7.313341493268054 100 3915776 7.313341493268054
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment