• Peter Geoghegan's avatar
    Refactor lazy_scan_heap() loop. · 7ab96cf6
    Peter Geoghegan authored
    Add a lazy_scan_heap() subsidiary function that handles heap pruning and
    tuple freezing: lazy_scan_prune().  This is a great deal cleaner.  The
    code that remains in lazy_scan_heap()'s per-block loop can now be
    thought of as code that either comes before or after the call to
    lazy_scan_prune(), which is now the clear focal point.  This division is
    enforced by the way in which we now manage state.  lazy_scan_prune()
    outputs state (using its own struct) that describes what to do with the
    page following pruning and freezing (e.g., visibility map maintenance,
    recording free space in the FSM).  It doesn't get passed any special
    instructional state from the preamble code, though.
    
    Also cleanly separate the logic used by a VACUUM with INDEX_CLEANUP=off
    from the logic used by single-heap-pass VACUUMs.  The former case is now
    structured as the omission of index and heap vacuuming by a two pass
    VACUUM.  The latter case goes back to being used only when the table
    happens to have no indexes (just as it was before commit a96c41fe).
    This structure is much more natural, since the whole point of
    INDEX_CLEANUP=off is to skip the index and heap vacuuming that would
    otherwise take place.  The single-heap-pass case doesn't skip any useful
    work, though -- it just does heap pruning and heap vacuuming together
    when the table happens to have no indexes.
    
    Both of these changes are preparation for an upcoming patch that
    generalizes the mechanism used by INDEX_CLEANUP=off.  The later patch
    will allow VACUUM to give up on index and heap vacuuming dynamically, as
    problems emerge (e.g., with wraparound), so that an affected VACUUM
    operation can finish up as soon as possible.
    
    Also fix a very old bug in single-pass VACUUM VERBOSE output.  We were
    reporting the number of tuples deleted via pruning as a direct
    substitute for reporting the number of LP_DEAD items removed in a
    function that deals with the second pass over the heap.  But that
    doesn't work at all -- they're two different things.
    
    To fix, start tracking the total number of LP_DEAD items encountered
    during pruning, and use that in the report instead.  A single pass
    VACUUM will always vacuum away whatever LP_DEAD items a heap page has
    immediately after it is pruned, so the total number of LP_DEAD items
    encountered during pruning equals the total number vacuumed-away.
    (They are _not_ equal in the INDEX_CLEANUP=off case, but that's okay
    because skipping index vacuuming is now a totally orthogonal concept to
    one-pass VACUUM.)
    
    Also stop reporting the count of LP_UNUSED items in VACUUM VERBOSE
    output.  This makes the output of VACUUM VERBOSE more consistent with
    log_autovacuum's output (because it never showed information about
    LP_UNUSED items).  VACUUM VERBOSE reported LP_UNUSED items left behind
    by the last VACUUM, and LP_UNUSED items created via pruning HOT chains
    during the current VACUUM (it never included LP_UNUSED items left behind
    by the current VACUUM's second pass over the heap).  This makes it
    useless as an indicator of line pointer bloat, which must have been the
    original intention. (Like the first VACUUM VERBOSE issue, this issue was
    arguably an oversight in commit 282d2a03, which added the heap-only
    tuple optimization.)
    
    Finally, stop reporting empty_pages in VACUUM VERBOSE output, and start
    reporting pages_removed instead.  This also makes the output of VACUUM
    VERBOSE more consistent with log_autovacuum's output (which does not
    show empty_pages, but does show pages_removed).  An empty page isn't
    meaningfully different to a page that is almost empty, or a page that is
    empty but for only a small number of remaining LP_UNUSED items.
    
    Author: Peter Geoghegan <pg@bowt.ie>
    Reviewed-By: default avatarRobert Haas <robertmhaas@gmail.com>
    Reviewed-By: default avatarMasahiko Sawada <sawada.mshk@gmail.com>
    Discussion: https://postgr.es/m/CAH2-WznneCXTzuFmcwx_EyRQgfsfJAAsu+CsqRFmFXCAar=nJw@mail.gmail.com
    7ab96cf6
vacuumlazy.c 126 KB