From 3f4ab89c338ad7be4b09b407ff8541f642d7b1ff Mon Sep 17 00:00:00 2001
From: Masahiko Sawada <sawada.mshk@gmail.com>
Date: Mon, 25 Jan 2021 16:20:37 +0900
Subject: [PATCH v4 3/3] PoC: disable index cleanup when an anti-wraparound and
 aggressive vacuum.

---
 src/backend/access/heap/vacuumlazy.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index 079359951e..ea074ad11d 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -530,6 +530,23 @@ heap_vacuum_rel(Relation onerel, VacuumParams *params,
 	if (params->options & VACOPT_DISABLE_PAGE_SKIPPING)
 		aggressive = true;
 
+	/*
+	 * If the vacuum is initiated to prevent xid-wraparound and is an aggressive
+	 * scan, we disable index cleanup to make freezing heap tuples and moving
+	 * relfrozenxid forward complete faster.
+	 *
+	 * Note that this applies only autovacuums as is_wraparound can be true
+	 * in autovacuums.
+	 *
+	 * XXX: should we not disable index cleanup if vacuum_index_cleanup reloption
+	 * is on?
+	 */
+	if (aggressive && params->is_wraparound)
+	{
+		Assert(IsAutoVacuumWorkerProcess());
+		params->index_cleanup = VACOPT_TERNARY_DISABLED;
+	}
+
 	vacrelstats = (LVRelStats *) palloc0(sizeof(LVRelStats));
 
 	vacrelstats->relnamespace = get_namespace_name(RelationGetNamespace(onerel));
-- 
2.27.0

