From afd4916d190d7da48b535432f8edea65615d7fe8 Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Mon, 22 Jun 2020 18:51:35 -0500
Subject: [PATCH v2 2/5] Specially save the index name in
 lazy_{vacuum,cleanup}_index..

..at expense of symmetry, move handling of index outside of
update_vacuum_error_info, since it's more clear this way that use of indname is
limited and safe.
---
 src/backend/access/heap/vacuumlazy.c | 49 ++++++++++++----------------
 1 file changed, 20 insertions(+), 29 deletions(-)

diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index a6a5d906c0..43a3c093fd 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -389,7 +389,7 @@ static LVSharedIndStats *get_indstats(LVShared *lvshared, int n);
 static bool skip_parallel_vacuum_index(Relation indrel, LVShared *lvshared);
 static void vacuum_error_callback(void *arg);
 static void update_vacuum_error_info(LVRelStats *errinfo, int phase,
-									 BlockNumber blkno, char *indname);
+									 BlockNumber blkno);
 
 
 /*
@@ -477,7 +477,6 @@ heap_vacuum_rel(Relation onerel, VacuumParams *params,
 
 	vacrelstats->relnamespace = get_namespace_name(RelationGetNamespace(onerel));
 	vacrelstats->relname = pstrdup(RelationGetRelationName(onerel));
-	vacrelstats->indname = NULL;
 	vacrelstats->phase = VACUUM_ERRCB_PHASE_UNKNOWN;
 	vacrelstats->old_rel_pages = onerel->rd_rel->relpages;
 	vacrelstats->old_live_tuples = onerel->rd_rel->reltuples;
@@ -539,7 +538,7 @@ heap_vacuum_rel(Relation onerel, VacuumParams *params,
 		 * revert to the previous phase.
 		 */
 		update_vacuum_error_info(vacrelstats, VACUUM_ERRCB_PHASE_TRUNCATE,
-								 vacrelstats->nonempty_pages, NULL);
+								 vacrelstats->nonempty_pages);
 		lazy_truncate_heap(onerel, vacrelstats);
 	}
 
@@ -949,7 +948,7 @@ lazy_scan_heap(Relation onerel, VacuumParams *params, LVRelStats *vacrelstats,
 		pgstat_progress_update_param(PROGRESS_VACUUM_HEAP_BLKS_SCANNED, blkno);
 
 		update_vacuum_error_info(vacrelstats, VACUUM_ERRCB_PHASE_SCAN_HEAP,
-								 blkno, NULL);
+								 blkno);
 
 		if (blkno == next_unskippable_block)
 		{
@@ -1829,7 +1828,7 @@ lazy_vacuum_heap(Relation onerel, LVRelStats *vacrelstats)
 	/* Update error traceback information */
 	olderrinfo = *vacrelstats;
 	update_vacuum_error_info(vacrelstats, VACUUM_ERRCB_PHASE_VACUUM_HEAP,
-							 InvalidBlockNumber, NULL);
+							 InvalidBlockNumber);
 
 	pg_rusage_init(&ru0);
 	npages = 0;
@@ -1881,8 +1880,7 @@ lazy_vacuum_heap(Relation onerel, LVRelStats *vacrelstats)
 	/* Revert to the previous phase information for error traceback */
 	update_vacuum_error_info(vacrelstats,
 							 olderrinfo.phase,
-							 olderrinfo.blkno,
-							 olderrinfo.indname);
+							 olderrinfo.blkno);
 }
 
 /*
@@ -1912,7 +1910,7 @@ lazy_vacuum_page(Relation onerel, BlockNumber blkno, Buffer buffer,
 	/* Update error traceback information */
 	olderrinfo = *vacrelstats;
 	update_vacuum_error_info(vacrelstats, VACUUM_ERRCB_PHASE_VACUUM_HEAP,
-							 blkno, NULL);
+							 blkno);
 
 	START_CRIT_SECTION();
 
@@ -1993,8 +1991,7 @@ lazy_vacuum_page(Relation onerel, BlockNumber blkno, Buffer buffer,
 	/* Revert to the previous phase information for error traceback */
 	update_vacuum_error_info(vacrelstats,
 							 olderrinfo.phase,
-							 olderrinfo.blkno,
-							 olderrinfo.indname);
+							 olderrinfo.blkno);
 	return tupindex;
 }
 
@@ -2418,10 +2415,11 @@ lazy_vacuum_index(Relation indrel, IndexBulkDeleteResult **stats,
 
 	/* Update error traceback information */
 	olderrinfo = *vacrelstats;
+	/* The index name is also saved during this phase */
+	vacrelstats->indname = pstrdup(RelationGetRelationName(indrel));
 	update_vacuum_error_info(vacrelstats,
 							 VACUUM_ERRCB_PHASE_VACUUM_INDEX,
-							 InvalidBlockNumber,
-							 RelationGetRelationName(indrel));
+							 InvalidBlockNumber);
 
 	/* Do bulk deletion */
 	*stats = index_bulk_delete(&ivinfo, *stats,
@@ -2441,8 +2439,8 @@ lazy_vacuum_index(Relation indrel, IndexBulkDeleteResult **stats,
 	/* Revert to the previous phase information for error traceback */
 	update_vacuum_error_info(vacrelstats,
 							 olderrinfo.phase,
-							 olderrinfo.blkno,
-							 olderrinfo.indname);
+							 olderrinfo.blkno);
+	pfree(vacrelstats->indname);
 }
 
 /*
@@ -2474,18 +2472,20 @@ lazy_cleanup_index(Relation indrel,
 
 	/* Update error traceback information */
 	olderrinfo = *vacrelstats;
+	/* The index name is also saved during this phase */
+	vacrelstats->indname = pstrdup(RelationGetRelationName(indrel));
 	update_vacuum_error_info(vacrelstats,
 							 VACUUM_ERRCB_PHASE_INDEX_CLEANUP,
-							 InvalidBlockNumber,
-							 RelationGetRelationName(indrel));
+							 InvalidBlockNumber);
 
 	*stats = index_vacuum_cleanup(&ivinfo, *stats);
 
 	/* Revert back to the old phase information for error traceback */
 	update_vacuum_error_info(vacrelstats,
 							 olderrinfo.phase,
-							 olderrinfo.blkno,
-							 olderrinfo.indname);
+							 olderrinfo.blkno);
+	pfree(vacrelstats->indname);
+
 	if (!(*stats))
 		return;
 
@@ -3523,7 +3523,6 @@ parallel_vacuum_main(dsm_segment *seg, shm_toc *toc)
 	 */
 	vacrelstats.relnamespace = get_namespace_name(RelationGetNamespace(onerel));
 	vacrelstats.relname = pstrdup(RelationGetRelationName(onerel));
-	vacrelstats.indname = NULL;
 	vacrelstats.phase = VACUUM_ERRCB_PHASE_UNKNOWN; /* Not yet processing */
 
 	/* Setup error traceback support for ereport() */
@@ -3598,18 +3597,10 @@ vacuum_error_callback(void *arg)
 	}
 }
 
-/* Update vacuum error callback for the current phase, block, and index. */
+/* Update vacuum error callback for the current phase and block. */
 static void
-update_vacuum_error_info(LVRelStats *errinfo, int phase, BlockNumber blkno,
-						 char *indname)
+update_vacuum_error_info(LVRelStats *errinfo, int phase, BlockNumber blkno)
 {
 	errinfo->blkno = blkno;
 	errinfo->phase = phase;
-
-	/* Free index name from any previous phase */
-	if (errinfo->indname)
-		pfree(errinfo->indname);
-
-	/* For index phases, save the name of the current index for the callback */
-	errinfo->indname = indname ? pstrdup(indname) : NULL;
 }
-- 
2.17.0

