diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c index 800815dfbc..5583563d08 100644 --- a/src/backend/postmaster/autovacuum.c +++ b/src/backend/postmaster/autovacuum.c @@ -3052,9 +3052,11 @@ relation_needs_vacanalyze(Oid relid, if (PointerIsValid(tabentry) && AutoVacuumingActive()) { float4 pcnt_unfrozen = 1; + float4 pcnt_unrelallvisible = 1; float4 reltuples = classForm->reltuples; int32 relpages = classForm->relpages; int32 relallfrozen = classForm->relallfrozen; + int32 relallvisible = classForm->relallvisible; vactuples = tabentry->dead_tuples; instuples = tabentry->ins_since_vacuum; @@ -3080,15 +3082,30 @@ relation_needs_vacanalyze(Oid relid, relallfrozen = Min(relallfrozen, relpages); pcnt_unfrozen = 1 - ((float4) relallfrozen / relpages); } + /* If we have data for relallvisible, calculate the unrelallvisible percentage + * of the table to modify vac_scale_factor. This helps us decide + * whether or not to vacuum an update-heavy table based on the number + * of vac_scale_factor to the more "relallvisible" part of the table. + */ + if (relpages > 0 && relallvisible > 0) + { + /* + * It could be the stats were updated manually and relallvisible > + * relpages. Clamp relallvisible to relpages to avoid nonsensical + * calculations. + */ + relallvisible = Min(relallvisible, relpages); + pcnt_unrelallvisible = 1 - ((float4) relallvisible / relpages); + } - vacthresh = (float4) vac_base_thresh + vac_scale_factor * reltuples; + vacthresh = (float4) vac_base_thresh + vac_scale_factor * reltuples * pcnt_unfrozen; if (vac_max_thresh >= 0 && vacthresh > (float4) vac_max_thresh) vacthresh = (float4) vac_max_thresh; vacinsthresh = (float4) vac_ins_base_thresh + vac_ins_scale_factor * reltuples * pcnt_unfrozen; - anlthresh = (float4) anl_base_thresh + anl_scale_factor * reltuples; - + anlthresh = (float4) anl_base_thresh + anl_scale_factor * reltuples * pcnt_unrelallvisible; + elog(DEBUG2, "vacthresh: %f,anlthresh: %f, the %s has %f reltuples, pcnt_unfrozen: %f, pcnt_unrelallvisible: %f ", vacthresh, anlthresh,NameStr(classForm->relname), reltuples, pcnt_unfrozen, pcnt_unrelallvisible); /* * Note that we don't need to take special consideration for stat * reset, because if that happens, the last vacuum and analyze counts