diff --git a/contrib/bloom/blvacuum.c b/contrib/bloom/blvacuum.c index 3282adde03..c458fe15d3 100644 --- a/contrib/bloom/blvacuum.c +++ b/contrib/bloom/blvacuum.c @@ -45,6 +45,10 @@ blbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, BloomMetaPageData *metaData; GenericXLogState *gxlogState; + /* Skip if index cleanup is disabled */ + if (!info->index_cleanup) + return NULL; + if (stats == NULL) stats = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult)); @@ -172,7 +176,8 @@ blvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats) BlockNumber npages, blkno; - if (info->analyze_only) + /* No-op in ANALYZE ONLY mode or index cleanup is disabled */ + if (info->analyze_only || !info->index_cleanup) return stats; if (stats == NULL) diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c index 7db3ae5ee0..f93871c7d8 100644 --- a/src/backend/access/brin/brin.c +++ b/src/backend/access/brin/brin.c @@ -782,6 +782,10 @@ IndexBulkDeleteResult * brinbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state) { + /* Skip if index cleanup is disabled */ + if (!info->index_cleanup) + return NULL; + /* allocate stats if first time through, else re-use existing struct */ if (stats == NULL) stats = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult)); @@ -798,8 +802,8 @@ brinvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats) { Relation heapRel; - /* No-op in ANALYZE ONLY mode */ - if (info->analyze_only) + /* No-op in ANALYZE ONLY mode or index cleanup is disabled */ + if (info->analyze_only || !info->index_cleanup) return stats; if (!stats) diff --git a/src/backend/access/gin/ginvacuum.c b/src/backend/access/gin/ginvacuum.c index 8ae4fd95a7..d6f5a6908c 100644 --- a/src/backend/access/gin/ginvacuum.c +++ b/src/backend/access/gin/ginvacuum.c @@ -571,6 +571,10 @@ ginbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, BlockNumber rootOfPostingTree[BLCKSZ / (sizeof(IndexTupleData) + sizeof(ItemId))]; uint32 nRoot; + /* Skip if index cleanup is disabled */ + if (!info->index_cleanup) + return NULL; + gvs.tmpCxt = AllocSetContextCreate(CurrentMemoryContext, "Gin vacuum temporary context", ALLOCSET_DEFAULT_SIZES); @@ -694,6 +698,10 @@ ginvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats) GinState ginstate; GinStatsData idxStat; + /* Skip if index cleanup is disabled */ + if (!info->index_cleanup) + return NULL; + /* * In an autovacuum analyze, we want to clean up pending insertions. * Otherwise, an ANALYZE-only call is a no-op. diff --git a/src/backend/access/gist/gistvacuum.c b/src/backend/access/gist/gistvacuum.c index a9c616c772..8dbe4be217 100644 --- a/src/backend/access/gist/gistvacuum.c +++ b/src/backend/access/gist/gistvacuum.c @@ -59,6 +59,10 @@ IndexBulkDeleteResult * gistbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state) { + /* Skip if index cleanup is disabled */ + if (!info->index_cleanup) + return NULL; + /* allocate stats if first time through, else re-use existing struct */ if (stats == NULL) stats = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult)); @@ -74,8 +78,8 @@ gistbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteResult * gistvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats) { - /* No-op in ANALYZE ONLY mode */ - if (info->analyze_only) + /* No-op in ANALYZE ONLY mode or index cleanup is disabled */ + if (info->analyze_only || !info->index_cleanup) return stats; /* diff --git a/src/backend/access/hash/hash.c b/src/backend/access/hash/hash.c index 3ec6d528e7..0f2cbd3514 100644 --- a/src/backend/access/hash/hash.c +++ b/src/backend/access/hash/hash.c @@ -467,6 +467,10 @@ hashbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, HashMetaPage metap; HashMetaPage cachedmetap; + /* Skip if index cleanup is disabled */ + if (!info->index_cleanup) + return NULL; + tuples_removed = 0; num_index_tuples = 0; @@ -645,7 +649,7 @@ hashvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats) BlockNumber num_pages; /* If hashbulkdelete wasn't called, return NULL signifying no change */ - /* Note: this covers the analyze_only case too */ + /* Note: this covers the analyze_only case and disabled index_cleanup case too */ if (stats == NULL) return NULL; diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c index 3bef0e124b..c775218c16 100644 --- a/src/backend/access/heap/vacuumlazy.c +++ b/src/backend/access/heap/vacuumlazy.c @@ -209,9 +209,12 @@ typedef struct LVShared * index cleanup case. * * estimated_count is true if reltuples is an estimated value. + * + * index_cleanup is true if INDEX_CLEANUP option is true. */ double reltuples; bool estimated_count; + bool index_cleanup; /* * In single process lazy vacuum we could consume more memory during index @@ -294,6 +297,8 @@ typedef struct LVRelStats char *relname; /* useindex = true means two-pass strategy; false means one-pass */ bool useindex; + /* INDEX_CLEANUP option value */ + bool index_cleanup; /* Overall statistics about rel */ BlockNumber old_rel_pages; /* previous value of pg_class.relpages */ BlockNumber rel_pages; /* total number of pages */ @@ -340,10 +345,12 @@ static void lazy_vacuum_all_indexes(Relation onerel, Relation *Irel, LVRelStats *vacrelstats, LVParallelState *lps, int nindexes); static void lazy_vacuum_index(Relation indrel, IndexBulkDeleteResult **stats, - LVDeadTuples *dead_tuples, double reltuples, LVRelStats *vacrelstats); + LVDeadTuples *dead_tuples, double reltuples, + bool index_cleanup, LVRelStats *vacrelstats); static void lazy_cleanup_index(Relation indrel, IndexBulkDeleteResult **stats, - double reltuples, bool estimated_count, LVRelStats *vacrelstats); + double reltuples, bool estimated_count, + bool index_cleanup, LVRelStats *vacrelstats); static int lazy_vacuum_page(Relation onerel, BlockNumber blkno, Buffer buffer, int tupindex, LVRelStats *vacrelstats, Buffer *vmbuffer); static bool should_attempt_truncation(VacuumParams *params, @@ -487,8 +494,8 @@ heap_vacuum_rel(Relation onerel, VacuumParams *params, /* Open all indexes of the relation */ vac_open_indexes(onerel, RowExclusiveLock, &nindexes, &Irel); - vacrelstats->useindex = (nindexes > 0 && - params->index_cleanup == VACOPT_TERNARY_ENABLED); + vacrelstats->useindex = (nindexes > 0); + vacrelstats->index_cleanup = (params->index_cleanup == VACOPT_TERNARY_ENABLED); /* * Setup error traceback support for ereport(). The idea is to set up an @@ -813,7 +820,7 @@ lazy_scan_heap(Relation onerel, VacuumParams *params, LVRelStats *vacrelstats, * be used for an index, so we invoke parallelism only if there are at * least two indexes on a table. */ - if (params->nworkers >= 0 && vacrelstats->useindex && nindexes > 1) + if (params->nworkers >= 0 && nindexes >= 2) { /* * Since parallel workers cannot access data in temporary tables, we @@ -1049,8 +1056,25 @@ lazy_scan_heap(Relation onerel, VacuumParams *params, LVRelStats *vacrelstats, lazy_vacuum_all_indexes(onerel, Irel, indstats, vacrelstats, lps, nindexes); - /* Remove tuples from heap */ - lazy_vacuum_heap(onerel, vacrelstats); + if (vacrelstats->index_cleanup) + { + /* Remove tuples from heap */ + lazy_vacuum_heap(onerel, vacrelstats); + } + else + { + /* + * Here, we have indexes but index cleanup is disabled. + * Instead of vacuuming the dead tuples on the heap, we just + * forget them. + * + * Note that vacrelstats->dead_tuples could have tuples which + * became dead after HOT-pruning but are not marked dead yet. + * We do not process them because it's a very rare condition, + * and the next vacuum will process them anyway. + */ + Assert(params->index_cleanup == VACOPT_TERNARY_DISABLED); + } /* * Forget the now-vacuumed tuples, and press on, but be careful @@ -1499,27 +1523,10 @@ lazy_scan_heap(Relation onerel, VacuumParams *params, LVRelStats *vacrelstats, */ if (!vacrelstats->useindex && dead_tuples->num_tuples > 0) { - if (nindexes == 0) - { - /* Remove tuples from heap if the table has no index */ - lazy_vacuum_page(onerel, blkno, buf, 0, vacrelstats, &vmbuffer); - vacuumed_pages++; - has_dead_tuples = false; - } - else - { - /* - * Here, we have indexes but index cleanup is disabled. - * Instead of vacuuming the dead tuples on the heap, we just - * forget them. - * - * Note that vacrelstats->dead_tuples could have tuples which - * became dead after HOT-pruning but are not marked dead yet. - * We do not process them because it's a very rare condition, - * and the next vacuum will process them anyway. - */ - Assert(params->index_cleanup == VACOPT_TERNARY_DISABLED); - } + /* Remove tuples from heap if the table has no index */ + lazy_vacuum_page(onerel, blkno, buf, 0, vacrelstats, &vmbuffer); + vacuumed_pages++; + has_dead_tuples = false; /* * Forget the now-vacuumed tuples, and press on, but be careful @@ -1792,7 +1799,8 @@ lazy_vacuum_all_indexes(Relation onerel, Relation *Irel, for (idx = 0; idx < nindexes; idx++) lazy_vacuum_index(Irel[idx], &stats[idx], vacrelstats->dead_tuples, - vacrelstats->old_live_tuples, vacrelstats); + vacrelstats->old_live_tuples, vacrelstats->index_cleanup, + vacrelstats); } /* Increase and report the number of index scans */ @@ -2305,10 +2313,12 @@ vacuum_one_index(Relation indrel, IndexBulkDeleteResult **stats, /* Do vacuum or cleanup of the index */ if (lvshared->for_cleanup) lazy_cleanup_index(indrel, stats, lvshared->reltuples, - lvshared->estimated_count, vacrelstats); + lvshared->estimated_count, lvshared->index_cleanup, + vacrelstats); else lazy_vacuum_index(indrel, stats, dead_tuples, - lvshared->reltuples, vacrelstats); + lvshared->reltuples, lvshared->index_cleanup, + vacrelstats); /* * Copy the index bulk-deletion result returned from ambulkdelete and @@ -2384,6 +2394,7 @@ lazy_cleanup_all_indexes(Relation *Irel, IndexBulkDeleteResult **stats, lazy_cleanup_index(Irel[idx], &stats[idx], vacrelstats->new_rel_tuples, vacrelstats->tupcount_pages < vacrelstats->rel_pages, + vacrelstats->index_cleanup, vacrelstats); } } @@ -2399,7 +2410,8 @@ lazy_cleanup_all_indexes(Relation *Irel, IndexBulkDeleteResult **stats, */ static void lazy_vacuum_index(Relation indrel, IndexBulkDeleteResult **stats, - LVDeadTuples *dead_tuples, double reltuples, LVRelStats *vacrelstats) + LVDeadTuples *dead_tuples, double reltuples, bool index_cleanup, + LVRelStats *vacrelstats) { IndexVacuumInfo ivinfo; const char *msg; @@ -2410,6 +2422,7 @@ lazy_vacuum_index(Relation indrel, IndexBulkDeleteResult **stats, ivinfo.index = indrel; ivinfo.analyze_only = false; + ivinfo.index_cleanup = index_cleanup; ivinfo.report_progress = false; ivinfo.estimated_count = true; ivinfo.message_level = elevel; @@ -2454,7 +2467,8 @@ lazy_vacuum_index(Relation indrel, IndexBulkDeleteResult **stats, static void lazy_cleanup_index(Relation indrel, IndexBulkDeleteResult **stats, - double reltuples, bool estimated_count, LVRelStats *vacrelstats) + double reltuples, bool estimated_count, bool index_cleanup, + LVRelStats *vacrelstats) { IndexVacuumInfo ivinfo; const char *msg; @@ -2465,6 +2479,7 @@ lazy_cleanup_index(Relation indrel, ivinfo.index = indrel; ivinfo.analyze_only = false; + ivinfo.index_cleanup = index_cleanup; ivinfo.report_progress = false; ivinfo.estimated_count = estimated_count; ivinfo.message_level = elevel; @@ -3297,6 +3312,7 @@ begin_parallel_vacuum(Oid relid, Relation *Irel, LVRelStats *vacrelstats, MemSet(shared, 0, est_shared); shared->relid = relid; shared->elevel = elevel; + shared->index_cleanup = vacrelstats->index_cleanup; shared->maintenance_work_mem_worker = (nindexes_mwm > 0) ? maintenance_work_mem / Min(parallel_workers, nindexes_mwm) : diff --git a/src/backend/access/nbtree/nbtree.c b/src/backend/access/nbtree/nbtree.c index e947addef6..c256aed60f 100644 --- a/src/backend/access/nbtree/nbtree.c +++ b/src/backend/access/nbtree/nbtree.c @@ -826,6 +826,11 @@ _bt_vacuum_needs_cleanup(IndexVacuumInfo *info) */ result = true; } + else if (!info->index_cleanup) + { + /* Skip cleanup if INDEX_CLEANUP is disabled */ + result = false; + } else { BTOptions *relopts; @@ -871,6 +876,10 @@ btbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, Relation rel = info->index; BTCycleId cycleid; + /* Skip if index cleanup is disabled */ + if (!info->index_cleanup) + return NULL; + /* allocate stats if first time through, else re-use existing struct */ if (stats == NULL) stats = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult)); diff --git a/src/backend/access/spgist/spgvacuum.c b/src/backend/access/spgist/spgvacuum.c index bd98707f3c..850d90b394 100644 --- a/src/backend/access/spgist/spgvacuum.c +++ b/src/backend/access/spgist/spgvacuum.c @@ -903,6 +903,10 @@ spgbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, { spgBulkDeleteState bds; + /* Skip if index cleanup is disabled */ + if (!info->index_cleanup) + return NULL; + /* allocate stats if first time through, else re-use existing struct */ if (stats == NULL) stats = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult)); @@ -933,8 +937,8 @@ spgvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats) { spgBulkDeleteState bds; - /* No-op in ANALYZE ONLY mode */ - if (info->analyze_only) + /* No-op in ANALYZE ONLY mode or index cleanup is disabled */ + if (info->analyze_only || !info->index_cleanup) return stats; /* diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index 924ef37c81..92d58a924f 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -675,6 +675,7 @@ do_analyze_rel(Relation onerel, VacuumParams *params, ivinfo.index = Irel[ind]; ivinfo.analyze_only = true; + ivinfo.index_cleanup = true; ivinfo.estimated_count = true; ivinfo.message_level = elevel; ivinfo.num_heap_tuples = onerel->rd_rel->reltuples; diff --git a/src/include/access/genam.h b/src/include/access/genam.h index 931257bd81..7394bbb682 100644 --- a/src/include/access/genam.h +++ b/src/include/access/genam.h @@ -50,6 +50,7 @@ typedef struct IndexVacuumInfo int message_level; /* ereport level for progress messages */ double num_heap_tuples; /* tuples remaining in heap */ BufferAccessStrategy strategy; /* access strategy for reads */ + bool index_cleanup; /* INDEX_CLEANUP option */ } IndexVacuumInfo; /*