Skip to content

Commit 25a2ba3

Browse files
committed
When updating reltuples after ANALYZE, just extrapolate from our sample.
The existing logic for updating pg_class.reltuples trusted the sampling results only for the pages ANALYZE actually visited, preferring to believe the previous tuple density estimate for all the unvisited pages. While there's some rationale for doing that for VACUUM (first that VACUUM is likely to visit a very nonrandom subset of pages, and second that we know for sure that the unvisited pages did not change), there's no such rationale for ANALYZE: by assumption, it's looked at an unbiased random sample of the table's pages. Furthermore, in a very large table ANALYZE will have examined only a tiny fraction of the table's pages, meaning it cannot slew the overall density estimate very far at all. In a table that is physically growing, this causes reltuples to increase nearly proportionally to the change in relpages, regardless of what is actually happening in the table. This has been observed to cause reltuples to become so much larger than reality that it effectively shuts off autovacuum, whose threshold for doing anything is a fraction of reltuples. (Getting to the point where that would happen seems to require some additional, not well understood, conditions. But it's undeniable that if reltuples is seriously off in a large table, ANALYZE alone will not fix it in any reasonable number of iterations, especially not if the table is continuing to grow.) Hence, restrict the use of vac_estimate_reltuples() to VACUUM alone, and in ANALYZE, just extrapolate from the sample pages on the assumption that they provide an accurate model of the whole table. If, by very bad luck, they don't, at least another ANALYZE will fix it; in the old logic a single bad estimate could cause problems indefinitely. In HEAD, let's remove vac_estimate_reltuples' is_analyze argument altogether; it was never used for anything and now it's totally pointless. But keep it in the back branches, in case any third-party code is calling this function. Per bug #15005. Back-patch to all supported branches. David Gould, reviewed by Alexander Kuzmenkov, cosmetic changes by me Discussion: https://postgr.es/m/20180117164916.3fdcf2e9@engels
1 parent 95f08d3 commit 25a2ba3

File tree

2 files changed

+24
-39
lines changed

2 files changed

+24
-39
lines changed

src/backend/commands/analyze.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,19 +1295,22 @@ acquire_sample_rows(Relation onerel, int elevel,
12951295
qsort((void *) rows, numrows, sizeof(HeapTuple), compare_rows);
12961296

12971297
/*
1298-
* Estimate total numbers of rows in relation. For live rows, use
1299-
* vac_estimate_reltuples; for dead rows, we have no source of old
1300-
* information, so we have to assume the density is the same in unseen
1301-
* pages as in the pages we scanned.
1298+
* Estimate total numbers of live and dead rows in relation, extrapolating
1299+
* on the assumption that the average tuple density in pages we didn't
1300+
* scan is the same as in the pages we did scan. Since what we scanned is
1301+
* a random sample of the pages in the relation, this should be a good
1302+
* assumption.
13021303
*/
1303-
*totalrows = vac_estimate_reltuples(onerel, true,
1304-
totalblocks,
1305-
bs.m,
1306-
liverows);
13071304
if (bs.m > 0)
1305+
{
1306+
*totalrows = floor((liverows / bs.m) * totalblocks + 0.5);
13081307
*totaldeadrows = floor((deadrows / bs.m) * totalblocks + 0.5);
1308+
}
13091309
else
1310+
{
1311+
*totalrows = 0.0;
13101312
*totaldeadrows = 0.0;
1313+
}
13111314

13121315
/*
13131316
* Emit some interesting relation info

src/backend/commands/vacuum.c

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -587,13 +587,13 @@ vacuum_set_xid_limits(Relation rel,
587587
* vac_estimate_reltuples() -- estimate the new value for pg_class.reltuples
588588
*
589589
* If we scanned the whole relation then we should just use the count of
590-
* live tuples seen; but if we did not, we should not trust the count
591-
* unreservedly, especially not in VACUUM, which may have scanned a quite
592-
* nonrandom subset of the table. When we have only partial information,
593-
* we take the old value of pg_class.reltuples as a measurement of the
590+
* live tuples seen; but if we did not, we should not blindly extrapolate
591+
* from that number, since VACUUM may have scanned a quite nonrandom
592+
* subset of the table. When we have only partial information, we take
593+
* the old value of pg_class.reltuples as a measurement of the
594594
* tuple density in the unscanned pages.
595595
*
596-
* This routine is shared by VACUUM and ANALYZE.
596+
* The is_analyze argument is historical.
597597
*/
598598
double
599599
vac_estimate_reltuples(Relation relation, bool is_analyze,
@@ -604,9 +604,8 @@ vac_estimate_reltuples(Relation relation, bool is_analyze,
604604
BlockNumber old_rel_pages = relation->rd_rel->relpages;
605605
double old_rel_tuples = relation->rd_rel->reltuples;
606606
double old_density;
607-
double new_density;
608-
double multiplier;
609-
double updated_density;
607+
double unscanned_pages;
608+
double total_tuples;
610609

611610
/* If we did scan the whole table, just use the count as-is */
612611
if (scanned_pages >= total_pages)
@@ -630,31 +629,14 @@ vac_estimate_reltuples(Relation relation, bool is_analyze,
630629

631630
/*
632631
* Okay, we've covered the corner cases. The normal calculation is to
633-
* convert the old measurement to a density (tuples per page), then update
634-
* the density using an exponential-moving-average approach, and finally
635-
* compute reltuples as updated_density * total_pages.
636-
*
637-
* For ANALYZE, the moving average multiplier is just the fraction of the
638-
* table's pages we scanned. This is equivalent to assuming that the
639-
* tuple density in the unscanned pages didn't change. Of course, it
640-
* probably did, if the new density measurement is different. But over
641-
* repeated cycles, the value of reltuples will converge towards the
642-
* correct value, if repeated measurements show the same new density.
643-
*
644-
* For VACUUM, the situation is a bit different: we have looked at a
645-
* nonrandom sample of pages, but we know for certain that the pages we
646-
* didn't look at are precisely the ones that haven't changed lately.
647-
* Thus, there is a reasonable argument for doing exactly the same thing
648-
* as for the ANALYZE case, that is use the old density measurement as the
649-
* value for the unscanned pages.
650-
*
651-
* This logic could probably use further refinement.
632+
* convert the old measurement to a density (tuples per page), then
633+
* estimate the number of tuples in the unscanned pages using that figure,
634+
* and finally add on the number of tuples in the scanned pages.
652635
*/
653636
old_density = old_rel_tuples / old_rel_pages;
654-
new_density = scanned_tuples / scanned_pages;
655-
multiplier = (double) scanned_pages / (double) total_pages;
656-
updated_density = old_density + (new_density - old_density) * multiplier;
657-
return floor(updated_density * total_pages + 0.5);
637+
unscanned_pages = (double) total_pages - (double) scanned_pages;
638+
total_tuples = old_density * unscanned_pages + scanned_tuples;
639+
return floor(total_tuples + 0.5);
658640
}
659641

660642

0 commit comments

Comments
 (0)