Vacuum
0-day streakReview

Your progress

Level 1

0 XP100 XP to level 2
0day streak
0best streak
0topics started
0cards reviewed
    Review cards

    Progress is stored only in this browser. No account is required.

    Vacuum and bloat: the 60-second story

    Vacuum and bloat

    Your table is 400 GB. It holds 50 GB of data.

    Every UPDATE and DELETE leaves an old row version behind. Bloat is what piles up when VACUUM can’t keep up.

    A station cloakroom clears dead slips. The rack never gets shorter.

    That’s VACUUM: dead row versions become free space in their page, listed in the free space map for this table’s new rows.

    100M

    dead tuples autovacuum waits for on 500M rows

    The default trigger is 50 + 20% of rows. At 2,000 updates a second, that’s 14 hours between runs. A per-table 1% runs every ≈42 minutes.

    Autovacuum is running on a 500 GB table. Why does each pass take almost four hours?

    The throttle

    All workers share one cost budget, so more workers just split it. Raise the cost limit instead.

    Vacuum runs but removes nothing? Something holds the horizon

    1. Long or idle-in-transaction sessions
    2. Replication slots whose consumer is gone
    3. Standbys with hot_standby_feedback on
    4. Prepared transactions from a crashed coordinator

    Already 400 GB. How do you shrink it?

    Bad: VACUUM FULL

    Every read and write waits for the whole rewrite.

    Good: pg_repack

    Copies rows online, then swaps. Two brief locks, and ≈2× the table in free disk.

    Transaction IDs wrap. Old rows must be frozen first.

    IDs are 32 bits, compared on a circle. About 2.1 billion transactions later, an unfrozen row looks like the future, so it turns invisible.

    MySQL has no VACUUM. Does InnoDB escape all this?

    In InnoDB

    Purge threads clean undo, but long transactions still grow the history list. 6-byte IDs last ≈89 years at 100,000 writes/s — no wraparound in practice.

    Remember

    VACUUM makes space reusable; it doesn’t shrink files. Tune big tables, clear horizon holders, watch XID age, repack online.