PostgreSQL Performance & Query Optimization · Aula

Configuração e ajuste do autovacuum

Aprenda a configurar e ajustar o processo autovacuum para obter desempenho e manutenção ideais.

Aula 2 de 411 etapas

Configuração e ajuste do autovacuum é uma aula grátis de PostgreSQL Performance & Query Optimization no CoddyKit. Esta é a aula 2 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 PostgreSQL Performance & Query Optimization, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de PostgreSQL Performance & Query Optimization inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

Optimize Your Database with Autovacuum

Welcome to tuning PostgreSQL's autovacuum! After learning about MVCC and VACUUM, let's dive into how to manage this crucial background process.

Autovacuum automatically reclaims space and updates statistics, preventing performance issues like table bloat and slow queries.

Autovacuum's Automatic Tasks

The autovacuum daemon runs in the background, constantly monitoring your database for tables that need attention. It performs two main operations:

  • VACUUM: Reclaims space occupied by "dead" rows, making it available for new data.
  • ANALYZE: Updates table statistics, helping the query planner choose the most efficient execution plans.

Where to Find Autovacuum Settings

Most autovacuum settings are found in your PostgreSQL configuration file, usually named postgresql.conf. You can also change them at the database or table level.

Remember to restart PostgreSQL or reload the configuration for changes to take effect!

SHOW config_file;

Turning Autovacuum On or Off

The most basic setting is autovacuum. It's usually enabled by default, and for most production systems, you should keep it that way!

Disabling it requires manual vacuuming, which can be easily missed, leading to severe performance problems.

ALTER SYSTEM SET autovacuum = on;

When Autovacuum Vacuums Tables

Autovacuum triggers a VACUUM when a certain number of dead rows accumulate. This is controlled by two parameters:

  • autovacuum_vacuum_scale_factor: A percentage of the table size (e.g., 0.2 for 20%).
  • autovacuum_vacuum_threshold: A fixed minimum number of dead rows.

The vacuum triggers when (dead_rows > autovacuum_vacuum_threshold + table_rows * autovacuum_vacuum_scale_factor).

Calculating Vacuum Triggers

Let's say autovacuum_vacuum_threshold is 50 and autovacuum_vacuum_scale_factor is 0.2 (20%). For a table with 1000 rows, a vacuum will trigger when:

  • dead_rows > 50 + (1000 * 0.2)
  • dead_rows > 50 + 200
  • dead_rows > 250

You can adjust these values for very active or very static tables.

When Autovacuum Analyzes Tables

Similar to vacuuming, autovacuum triggers an ANALYZE operation based on a threshold:

  • autovacuum_analyze_scale_factor: A percentage of the table size.
  • autovacuum_analyze_threshold: A fixed minimum number of changed rows.

Analyzing ensures the query planner has up-to-date statistics for optimal query plans, preventing slow queries.

Autovacuum Frequency & Concurrency

Two more important parameters:

  • autovacuum_naptime: How long autovacuum waits between checks on databases (e.g., '1min'). Shorter naptime means more frequent checks.
  • autovacuum_max_workers: The maximum number of autovacuum processes that can run simultaneously across all databases. More workers mean more concurrent vacuuming/analyzing.

Customizing Autovacuum Per Table

Sometimes, a table might need different autovacuum settings than the global defaults. For instance, a very large, frequently updated table could benefit from more aggressive vacuuming.

You can override most autovacuum parameters for individual tables using ALTER TABLE.

ALTER TABLE my_large_table SET (autovacuum_vacuum_scale_factor = 0.05);

Quick Check on Autovacuum

Autovacuum helps keep your database healthy. Let's test your understanding of how it decides when to vacuum a table.

Autovacuum Tuning Recap

Great job! You've learned how to configure and tune PostgreSQL's autovacuum daemon.

  • Autovacuum performs automatic VACUUM and ANALYZE.
  • Key parameters control when (scale factor, threshold) and how often (naptime) it runs.
  • You can customize settings globally in postgresql.conf or per-table using ALTER TABLE.

Proper autovacuum tuning is essential for maintaining database performance and preventing bloat.

Grátis para começar

Aprenda SQL 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
22
Aulas
88

Perguntas Frequentes

A aula “Configuração e ajuste do autovacuum” é grátis?

Sim — o texto completo de “Configuração e ajuste do autovacuum” é 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 PostgreSQL Performance & Query Optimization, atualize para CoddyKit PRO. O curso de PostgreSQL Performance & Query Optimization inclui 4 aulas no total.

O que vou aprender em “Configuração e ajuste do autovacuum”?

Aprenda a configurar e ajustar o processo autovacuum para obter desempenho e manutenção ideais. Você pratica PostgreSQL Performance & Query Optimization 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 PostgreSQL Performance & Query Optimization?

Nenhuma experiência prévia é necessária. PostgreSQL Performance & Query Optimization 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 2 de 4.

Quanto tempo leva a aula “Configuração e ajuste do autovacuum”?

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 PostgreSQL Performance & Query Optimization?

Sim. Cada aula de PostgreSQL Performance & Query Optimization 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

  1. Entendendo MVCC e VACUUM
  2. Configuração e ajuste do autovacuum
  3. Impacto dos níveis de isolamento de transações
  4. Evitando a reutilização circular de identificadores de transação
← Voltar para PostgreSQL Performance & Query Optimization