Diagnóstico da fragmentação e estratégia de limpeza
Entenda como o MVCC cria fragmentação em tabelas e índices, como medi-la e como ajustar a limpeza automática para manter o alto desempenho.
Diagnóstico da fragmentação e estratégia de limpeza é uma aula grátis de Advanced PostgreSQL: Indexing, Partitioning, Replication no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Advanced PostgreSQL: Indexing, Partitioning, Replication, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Advanced PostgreSQL: Indexing, Partitioning, Replication inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
MVCC and Dead Tuples
PostgreSQL uses MVCC: updates and deletes leave behind old row versions called dead tuples. Until they are cleaned up, they occupy space and slow scans. This wasted space is bloat.
What VACUUM Does
VACUUM reclaims dead tuples for reuse and updates visibility information. It usually does not return space to the OS; VACUUM FULL does but rewrites the whole table and takes a strong lock.
Measuring Bloat
Inspect dead tuple counts per table from the statistics view.
SELECT relname, n_live_tup, n_dead_tup,
last_autovacuum
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC;Autovacuum Basics
Autovacuum runs in the background, triggering when dead tuples exceed a threshold based on table size and the scale factor setting.
-- trigger ~ threshold + scale_factor * n_live_tup
autovacuum_vacuum_scale_factor = 0.2Tuning Hot Tables
For large, frequently updated tables, the default 20% scale factor is too lazy. Lower it per table so vacuum runs more often on less garbage.
ALTER TABLE orders SET (
autovacuum_vacuum_scale_factor = 0.02);Vacuum Throttling
Autovacuum throttles itself with cost limits to avoid I/O storms. On modern hardware you can raise autovacuum_vacuum_cost_limit so vacuum finishes faster.
autovacuum_vacuum_cost_limit = 2000Transaction ID Wraparound
VACUUM also prevents transaction ID wraparound, a catastrophic condition. Aggressive anti-wraparound vacuums are non-negotiable and cannot be skipped.
SELECT datname, age(datfrozenxid)
FROM pg_database ORDER BY 2 DESC;Index Bloat
Indexes bloat too. REINDEX CONCURRENTLY rebuilds an index without blocking writes, restoring its compactness.
REINDEX INDEX CONCURRENTLY orders_pkey;HOT Updates
Heap-Only Tuple updates avoid index churn when no indexed column changes. Leaving some free space via a lower fillfactor helps HOT updates and reduces bloat.
ALTER TABLE orders SET (fillfactor = 90);VACUUM vs ANALYZE
VACUUM reclaims space; ANALYZE refreshes the planner statistics. Autovacuum does both, but after big bulk loads run ANALYZE manually for fresh plans.
ANALYZE orders;A Monitoring Habit
Alert on rising n_dead_tup, growing table size with stable row counts, and high age(datfrozenxid). These early signals let you tune before queries slow down.
Quick Check
A large, hot table keeps growing despite stable row counts. What is the likely cause and fix?
Recap
You learned to diagnose bloat from MVCC dead tuples, measure it with pg_stat_user_tables, tune autovacuum per table, guard against ID wraparound, and use REINDEX CONCURRENTLY and fillfactor to keep performance high.
Aprenda Advanced PostgreSQL: Indexing, Partitioning, Replication com um tutor de IA — grátis
Escreva e execute código real no seu navegador, obtenha ajuda instantânea de um tutor de IA 24/7 e continue de onde parou na web ou no app.
- Cursos
- 11
- Aulas
- 44
Perguntas Frequentes
A aula “Diagnóstico da fragmentação e estratégia de limpeza” é grátis?
Sim — o texto completo de “Diagnóstico da fragmentação e estratégia de limpeza” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Advanced PostgreSQL: Indexing, Partitioning, Replication, atualize para CoddyKit PRO. O curso de Advanced PostgreSQL: Indexing, Partitioning, Replication inclui 4 aulas no total.
O que vou aprender em “Diagnóstico da fragmentação e estratégia de limpeza”?
Entenda como o MVCC cria fragmentação em tabelas e índices, como medi-la e como ajustar a limpeza automática para manter o alto desempenho. Você pratica Advanced PostgreSQL: Indexing, Partitioning, Replication com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Advanced PostgreSQL: Indexing, Partitioning, Replication?
Nenhuma experiência prévia é necessária. Advanced PostgreSQL: Indexing, Partitioning, Replication no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “Diagnóstico da fragmentação e estratégia de limpeza”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Advanced PostgreSQL: Indexing, Partitioning, Replication?
Sim. Cada aula de Advanced PostgreSQL: Indexing, Partitioning, Replication inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Ajuste holístico de desempenho
- Monitoramento e alertas avançados
- Tendências futuras do PostgreSQL
- Diagnóstico da fragmentação e estratégia de limpeza