postgres.git / summary / log / commit / refs

commit    eacabd7d031cdc449ae7938dd8b75e6edb3d03b8
Author:   Melanie Plageman <melanieplageman@gmail.com>
Date:     Thu Nov 12 21:08:59 2026 +0000
First released in: 19.0

    Scale VACUUM reltuples estimates after relation growth
    
    VACUUM kept the old reltuples value when it scanned at most one page,
    even if the relation size changed. This left stale estimates after table
    growth or shrinkage. Commit b46e1e54d made scanning one page more likely
    by allowing newly populated pages to become all-visible before VACUUM.
    
    Preserve reltuples only when relation size is unchanged; otherwise, scale
    the old tuple density to the current size.
    
    Only backpatch as far as 19, even though it was present before, to avoid
    changing stats logic in stable branches.
    
    Reported-by: Vaibhav Jain <jainva@google.com>
    Author: Vaibhav Jain <jainva@google.com>
    Reviewed-by: Melanie Plageman <melanieplageman@gmail.com>
    Discussion: https://postgr.es/m/flat/CABa-Az2YXuX%2B_JH8VL89i7oA%2BDC74GvfkSsM9%2B92fGCeWYVCqg%40mail.gmail.com
    Backpatch-through: 19


src/backend/commands/vacuum.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index 1cfbb50f578..ef9e091d9fe 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -1395,14 +1395,15 @@ vac_estimate_reltuples(Relation relation, * If the relation is _exactly_ the same size according to the existing * pg_class entry, and only a few of its pages (less than 2%) were * scanned, keep the existing value of reltuples. Also keep the existing - * value when only a subset of rel's pages <= a single page were scanned. + * value when at most one page was scanned. When the relation size has + * changed, use the old density to account for the added or removed pages + * instead of preserving a stale tuple count. * * (Note: we might be returning -1 here.) */ if (old_rel_pages == total_pages && - scanned_pages < (double) total_pages * 0.02) - return old_rel_tuples; - if (scanned_pages <= 1) + (scanned_pages <= 1 || + scanned_pages < (double) total_pages * 0.02)) return old_rel_tuples; /* [parent: 1a001d930ee8]