postgresqlvacuum
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
postgresqlvacuum [2021/09/18 20:24] – z0hpvk | postgresqlvacuum [2025/03/08 22:24] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
===== Vacuuming ===== | ===== Vacuuming ===== | ||
- | The following SQL code displays the thresholds for when Autovacuum will " | + | ==== Parameters ==== |
+ | < | ||
+ | autovacuum_analyze_scale_factor | ||
+ | autovacuum_analyze_threshold | ||
+ | autovacuum_vacuum_scale_factor | ||
+ | autovacuum_vacuum_threshold | ||
+ | </ | ||
+ | |||
+ | ==== Dead Rows ==== | ||
+ | The following SQL code displays the thresholds for when Autovacuum will clean up dead rows.\\ | ||
+ | Dead rows (or tuples) occur in postgres when records are updated or deleted.\\ | ||
<code SQL> | <code SQL> | ||
select relname, n_live_tup, n_dead_tup, | select relname, n_live_tup, n_dead_tup, | ||
Line 15: | Line 25: | ||
order by 5 desc limit 10; | order by 5 desc limit 10; | ||
</ | </ | ||
+ | |||
+ | ==== Vacuum Progress ==== | ||
+ | Will not display the progress of VACUUM FULL commands. | ||
+ | <code SQL> | ||
+ | SELECT p.pid, now() - a.xact_start AS duration, | ||
+ | | ||
+ | CASE | ||
+ | WHEN a.query ~*' | ||
+ | WHEN a.query ~*' | ||
+ | ELSE ' | ||
+ | END AS mode, | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | FROM pg_stat_progress_vacuum p | ||
+ | JOIN pg_stat_activity a using (pid) | ||
+ | ORDER BY now() - a.xact_start DESC; | ||
+ | </ | ||
+ | |||
+ | ==== Table Bloat ==== | ||
+ | Table bloat can be fixed by performing a " | ||
+ | This will create a new unfragmented table and rebuild all associated indexes.\\ | ||
+ | However this will cause a full lock on the table, even SELECT queries will have to wait for the process to complete.\\ | ||
+ | Instead you can use the extension [[https:// | ||
+ | |||
+ | The basis of the below SQL query was taken from: [[https:// | ||
+ | |||
+ | <code SQL> | ||
+ | SELECT current_database(), | ||
+ | | ||
+ | | ||
+ | CASE WHEN tblpages - est_tblpages > 0 | ||
+ | THEN round(cast(100 * (tblpages - est_tblpages)/ | ||
+ | ELSE 0 | ||
+ | END AS extra_pct | ||
+ | FROM ( | ||
+ | SELECT ceil( reltuples / ( (bs-page_hdr)/ | ||
+ | | ||
+ | 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):: | ||
+ | ) AS tpl_size, bs - page_hdr AS size_per_block, | ||
+ | toastpages, reltuples, toasttuples, | ||
+ | FROM ( | ||
+ | SELECT | ||
+ | ns.nspname AS schemaname, tbl.relname AS tblname, tbl.reltuples, | ||
+ | tbl.relpages AS heappages, coalesce(toast.relpages, | ||
+ | coalesce(toast.reltuples, | ||
+ | current_setting(' | ||
+ | CASE WHEN version()~' | ||
+ | 24 AS page_hdr, | ||
+ | 23 + CASE WHEN MAX(coalesce(s.null_frac, | ||
+ | + CASE WHEN bool_or(att.attname = ' | ||
+ | sum( (1-coalesce(s.null_frac, | ||
+ | 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 (' | ||
+ | GROUP BY 1, | ||
+ | ) AS s | ||
+ | ) AS s2 | ||
+ | ) AS s3 | ||
+ | WHERE schemaname in (' | ||
+ | AND (tblpages - est_tblpages)*bs > 1024000 | ||
+ | ORDER BY (tblpages - est_tblpages)*bs DESC; | ||
+ | </ | ||
+ |
postgresqlvacuum.1631996672.txt.gz · Last modified: 2025/03/08 22:23 (external edit)