diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index b379b67..f051097 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -6592,6 +6592,24 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' WITH csv;
+
+ vacuum_cleanup_index_scale_factor (floating point)
+
+ vacuum_cleanup_indexscale_factor> configuration parameter
+
+
+
+
+ Specifies a fraction of the table pages that need to be vacuumed
+ prior to vacuum needed to trigger a cleaning up indexes in one table.
+ If the number of pages containg the page that need to be vacuumed is lower
+ than this value and VACUUM> doesn't perform an aggressive scan,
+ we can skip to clean up index. The default is 0.01. This parameter can be
+ set anywhere.
+
+
+
+
bytea_output (enum)
diff --git a/src/backend/commands/vacuumlazy.c b/src/backend/commands/vacuumlazy.c
index b74e493..1a72f65 100644
--- a/src/backend/commands/vacuumlazy.c
+++ b/src/backend/commands/vacuumlazy.c
@@ -132,6 +132,10 @@ typedef struct LVRelStats
bool lock_waiter_detected;
} LVRelStats;
+/*
+ * GUC parameter
+ */
+double vacuum_cleanup_index_scale;
/* A few variables that don't seem worth passing around as parameters */
static int elevel = -1;
@@ -483,6 +487,7 @@ lazy_scan_heap(Relation onerel, int options, LVRelStats *vacrelstats,
PROGRESS_VACUUM_MAX_DEAD_TUPLES
};
int64 initprog_val[3];
+ float4 cleanupidx_thresh;
pg_rusage_init(&ru0);
@@ -1320,9 +1325,24 @@ lazy_scan_heap(Relation onerel, int options, LVRelStats *vacrelstats,
pgstat_progress_update_param(PROGRESS_VACUUM_PHASE,
PROGRESS_VACUUM_PHASE_INDEX_CLEANUP);
- /* Do post-vacuum cleanup and statistics update for each index */
- for (i = 0; i < nindexes; i++)
- lazy_cleanup_index(Irel[i], indstats[i], vacrelstats);
+ /*
+ * Do post-vacuum cleanup and statistics update for each index if
+ * the number of vacuumed page exceeds threshold and we're not
+ * performing an aggressive scan. Deleting index item could stash
+ * an XID and it could cause the data corruption. Let's do that on
+ * the safe side.
+ */
+ cleanupidx_thresh = (float4) nblocks * vacuum_cleanup_index_scale;
+
+ elog(DEBUG3, "%s: scanned: %d (threshold %0.f)",
+ RelationGetRelationName(onerel), vacrelstats->scanned_pages,
+ cleanupidx_thresh);
+ if (vacrelstats->scanned_pages >= cleanupidx_thresh &&
+ !aggressive)
+ {
+ for (i = 0; i < nindexes; i++)
+ lazy_cleanup_index(Irel[i], indstats[i], vacrelstats);
+ }
/* If no indexes, make log report that lazy_vacuum_heap would've made */
if (vacuumed_pages)
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 4feb26a..7ddc43c 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -3004,6 +3004,16 @@ static struct config_real ConfigureNamesReal[] =
},
{
+ {"vacuum_cleanup_index_scale_factor", PGC_USERSET, CLIENT_CONN_STATEMENT,
+ gettext_noop("Number of pages containing dead tuple prior to vacuum as a fraction of relpages."),
+ NULL
+ },
+ &vacuum_cleanup_index_scale,
+ 0.01, 0.0, 100.0,
+ NULL, NULL, NULL
+ },
+
+ {
{"checkpoint_completion_target", PGC_SIGHUP, WAL_CHECKPOINTS,
gettext_noop("Time spent flushing dirty buffers during checkpoint, as fraction of checkpoint interval."),
NULL
diff --git a/src/include/commands/vacuum.h b/src/include/commands/vacuum.h
index 541c2fa..3e9a149 100644
--- a/src/include/commands/vacuum.h
+++ b/src/include/commands/vacuum.h
@@ -154,6 +154,7 @@ extern int vacuum_freeze_min_age;
extern int vacuum_freeze_table_age;
extern int vacuum_multixact_freeze_min_age;
extern int vacuum_multixact_freeze_table_age;
+extern double vacuum_cleanup_index_scale;
/* in commands/vacuum.c */