diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c index d8f847b0e6..45c673e471 100644 --- a/src/backend/access/heap/vacuumlazy.c +++ b/src/backend/access/heap/vacuumlazy.c @@ -313,6 +313,7 @@ typedef struct LVRelStats int num_index_scans; TransactionId latestRemovedXid; bool lock_waiter_detected; + IndexBulkDeleteResult **indstats; /* used for autovacuum logs */ /* Used for error callback */ char *indname; @@ -523,9 +524,6 @@ heap_vacuum_rel(Relation onerel, VacuumParams *params, /* Do the vacuuming */ lazy_scan_heap(onerel, params, vacrelstats, Irel, nindexes, aggressive); - /* Done with indexes */ - vac_close_indexes(nindexes, Irel, NoLock); - /* * Compute whether we actually scanned the all unfrozen pages. If we did, * we can adjust relfrozenxid and relminmxid. @@ -606,13 +604,18 @@ heap_vacuum_rel(Relation onerel, VacuumParams *params, pgstat_progress_end_command(); /* and log the action if appropriate */ - if (IsAutoVacuumWorkerProcess() && params->log_min_duration >= 0) + if (IsAutoVacuumWorkerProcess()) { - TimestampTz endtime = GetCurrentTimestamp(); + TimestampTz endtime = 0; + int i; + + if (params->log_min_duration >= 0) + endtime = GetCurrentTimestamp(); - if (params->log_min_duration == 0 || - TimestampDifferenceExceeds(starttime, endtime, - params->log_min_duration)) + if (endtime > 0 && + (params->log_min_duration == 0 || + TimestampDifferenceExceeds(starttime, endtime, + params->log_min_duration))) { StringInfoData buf; char *msgfmt; @@ -672,6 +675,21 @@ heap_vacuum_rel(Relation onerel, VacuumParams *params, (long long) VacuumPageHit, (long long) VacuumPageMiss, (long long) VacuumPageDirty); + for (i = 0; i < nindexes; i++) + { + IndexBulkDeleteResult *stats = vacrelstats->indstats[i]; + + if (!stats) + continue; + + appendStringInfo(&buf, + _("index \"%s\": pages: %u remain, %u newly deleted, %u currently deleted, %u reusable\n"), + RelationGetRelationName(Irel[i]), + stats->num_pages, + stats->pages_newly_deleted, + stats->pages_deleted, + stats->pages_free); + } appendStringInfo(&buf, _("avg read rate: %.3f MB/s, avg write rate: %.3f MB/s\n"), read_rate, write_rate); appendStringInfo(&buf, _("system usage: %s\n"), pg_rusage_show(&ru0)); @@ -685,7 +703,17 @@ heap_vacuum_rel(Relation onerel, VacuumParams *params, (errmsg_internal("%s", buf.data))); pfree(buf.data); } + + /* Cleanup index statistics */ + for (i = 0; i < nindexes; i++) + { + if (vacrelstats->indstats[i]) + pfree(vacrelstats->indstats[i]); + } } + + /* Done with indexes */ + vac_close_indexes(nindexes, Irel, NoLock); } /* @@ -1737,7 +1765,11 @@ lazy_scan_heap(Relation onerel, VacuumParams *params, LVRelStats *vacrelstats, end_parallel_vacuum(indstats, lps, nindexes); /* Update index statistics */ - update_index_statistics(Irel, indstats, nindexes); + if (vacrelstats->useindex) + { + update_index_statistics(Irel, indstats, nindexes); + vacrelstats->indstats = indstats; + } /* If no indexes, make log report that lazy_vacuum_heap would've made */ if (vacuumed_pages) @@ -3206,7 +3238,13 @@ update_index_statistics(Relation *Irel, IndexBulkDeleteResult **stats, InvalidTransactionId, InvalidMultiXactId, false); - pfree(stats[i]); + + /* + * Autovacuum worker keeps the index statistics until the end + * of lazy vacuum for autovacuum logs. + */ + if (!IsAutoVacuumWorkerProcess()) + pfree(stats[i]); } }