• Peter Geoghegan's avatar
    VACUUM ANALYZE: Always update pg_class.reltuples. · 5f8727f5
    Peter Geoghegan authored
    vacuumlazy.c sometimes fails to update pg_class entries for each index
    (to ensure that pg_class.reltuples is current), even though analyze.c
    assumed that that must have happened during VACUUM ANALYZE.  There are
    at least a couple of reasons for this.  For example, vacuumlazy.c could
    fail to update pg_class when the index AM indicated that its statistics
    are merely an estimate, per the contract for amvacuumcleanup() routines
    established by commit e5734597 back in 2006.
    
    Stop assuming that pg_class must have been updated with accurate
    statistics within VACUUM ANALYZE -- update pg_class for indexes at the
    same time as the table relation in all cases.  That way VACUUM ANALYZE
    will never fail to keep pg_class.reltuples reasonably accurate.
    
    The only downside of this approach (compared to the old approach) is
    that it might inaccurately set pg_class.reltuples for indexes whose heap
    relation ends up with the same inaccurate value anyway.  This doesn't
    seem too bad.  We already consistently called vac_update_relstats() (to
    update pg_class) for the heap/table relation twice during any VACUUM
    ANALYZE -- once in vacuumlazy.c, and once in analyze.c.  We now make
    sure that we call vac_update_relstats() at least once (though often
    twice) for each index.
    
    This is follow up work to commit 9f3665fb, which dealt with issues in
    btvacuumcleanup().  Technically this fixes an unrelated issue, though.
    btvacuumcleanup() no longer provides an accurate num_index_tuples value
    following commit 9f3665fb (when there was no btbulkdelete() call during
    the VACUUM operation in question), but hashvacuumcleanup() has worked in
    the same way for many years now.
    
    Author: Peter Geoghegan <pg@bowt.ie>
    Reviewed-By: default avatarMasahiko Sawada <sawada.mshk@gmail.com>
    Discussion: https://postgr.es/m/CAH2-WzknxdComjhqo4SUxVFk_Q1171GJO2ZgHZ1Y6pion6u8rA@mail.gmail.com
    Backpatch: 13-, just like commit 9f3665fb.
    5f8727f5
analyze.c 85.7 KB