0Pricing
Advanced PostgreSQL: Indexing, Partitioning, Replication · Lezione

Diagnosi del bloat e strategia di vacuum

Comprenda come MVCC generi bloat nelle tabelle e negli indici, come misurarlo e come ottimizzare autovacuum per mantenere alte le prestazioni.

Diagnosi del bloat e strategia di vacuum è una lezione Advanced PostgreSQL: Indexing, Partitioning, Replication gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Advanced PostgreSQL: Indexing, Partitioning, Replication, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Advanced PostgreSQL: Indexing, Partitioning, Replication include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.2

Tuning 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 = 2000

Transaction 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.

Domande Frequenti

La lezione «Diagnosi del bloat e strategia di vacuum» è gratuita?

Sì — il testo completo di «Diagnosi del bloat e strategia di vacuum» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Advanced PostgreSQL: Indexing, Partitioning, Replication, passa a CoddyKit PRO. Il corso Advanced PostgreSQL: Indexing, Partitioning, Replication include 4 lezioni in totale.

Cosa imparerò in «Diagnosi del bloat e strategia di vacuum»?

Comprenda come MVCC generi bloat nelle tabelle e negli indici, come misurarlo e come ottimizzare autovacuum per mantenere alte le prestazioni. Eserciti Advanced PostgreSQL: Indexing, Partitioning, Replication con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Advanced PostgreSQL: Indexing, Partitioning, Replication?

Non è richiesta alcuna esperienza precedente. Advanced PostgreSQL: Indexing, Partitioning, Replication su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Diagnosi del bloat e strategia di vacuum»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Advanced PostgreSQL: Indexing, Partitioning, Replication?

Sì. Ogni lezione Advanced PostgreSQL: Indexing, Partitioning, Replication include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Ottimizzazione globale delle prestazioni
  2. Monitoraggio e avvisi avanzati
  3. Tendenze future di PostgreSQL
  4. Diagnosi del bloat e strategia di vacuum
← Torna a Advanced PostgreSQL: Indexing, Partitioning, Replication