From f9ad1b8e857c088a3eab5aef88a9016e18fedc6c Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Mon, 27 Jan 2020 08:30:03 -0600
Subject: [PATCH v15 2/2] Include name of table in callback for index vacuum

---
 src/backend/access/heap/vacuumlazy.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index d11c7af..a57190b 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -177,6 +177,7 @@ typedef struct LVShared
 	 * the lazy vacuum.
 	 */
 	Oid			relid;
+	char		relname[NAMEDATALEN];	/* tablename, used for error callback */
 	int			elevel;
 
 	/*
@@ -270,6 +271,7 @@ typedef struct LVParallelState
 
 typedef struct LVRelStats
 {
+	char		*relname;	/* tablename, used for error callback */
 	/* useindex = true means two-pass strategy; false means one-pass */
 	bool		useindex;
 	/* Overall statistics about rel */
@@ -296,6 +298,7 @@ typedef struct
 {
 	char 		*relnamespace;
 	char		*relname;
+	char 		*indname; /* If vacuuming index */
 	BlockNumber blkno;
 	int			stage;	/* 0: scan heap; 1: vacuum heap; 2: vacuum index */
 } vacuum_error_callback_arg;
@@ -321,7 +324,7 @@ 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);
+							  LVDeadTuples *dead_tuples, double reltuples, char *relname);
 static void lazy_cleanup_index(Relation indrel,
 							   IndexBulkDeleteResult **stats,
 							   double reltuples, bool estimated_count);
@@ -757,6 +760,7 @@ lazy_scan_heap(Relation onerel, VacuumParams *params, LVRelStats *vacrelstats,
 		palloc0(nindexes * sizeof(IndexBulkDeleteResult *));
 
 	nblocks = RelationGetNumberOfBlocks(onerel);
+	vacrelstats->relname = relname;
 	vacrelstats->rel_pages = nblocks;
 	vacrelstats->scanned_pages = 0;
 	vacrelstats->tupcount_pages = 0;
@@ -1775,7 +1779,7 @@ 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->old_live_tuples, vacrelstats->relname);
 	}
 
 	/* Increase and report the number of index scans */
@@ -2272,7 +2276,7 @@ vacuum_one_index(Relation indrel, IndexBulkDeleteResult **stats,
 						   lvshared->estimated_count);
 	else
 		lazy_vacuum_index(indrel, stats, dead_tuples,
-						  lvshared->reltuples);
+						  lvshared->reltuples, lvshared->relname);
 
 	/*
 	 * Copy the index bulk-deletion result returned from ambulkdelete and
@@ -2359,10 +2363,11 @@ lazy_cleanup_all_indexes(Relation *Irel, IndexBulkDeleteResult **stats,
  *
  *		reltuples is the number of heap tuples to be passed to the
  *		bulkdelete callback.
+ *		relname is the name of the table to be passed to the error callback.
  */
 static void
 lazy_vacuum_index(Relation indrel, IndexBulkDeleteResult **stats,
-				  LVDeadTuples *dead_tuples, double reltuples)
+				  LVDeadTuples *dead_tuples, double reltuples, char *relname)
 {
 	IndexVacuumInfo ivinfo;
 	const char *msg;
@@ -2382,7 +2387,8 @@ lazy_vacuum_index(Relation indrel, IndexBulkDeleteResult **stats,
 
 	/* Setup error traceback support for ereport() */
 	errcbarg.relnamespace = get_namespace_name(RelationGetNamespace(indrel));
-	errcbarg.relname = RelationGetRelationName(indrel);
+	errcbarg.indname = RelationGetRelationName(indrel);
+	errcbarg.relname = relname;
 	errcbarg.stage = 2;
 
 	errcallback.callback = vacuum_error_callback;
@@ -3229,6 +3235,7 @@ begin_parallel_vacuum(Oid relid, Relation *Irel, LVRelStats *vacrelstats,
 	MemSet(shared, 0, est_shared);
 	shared->relid = relid;
 	shared->elevel = elevel;
+	strcpy(shared->relname, vacrelstats->relname);
 	shared->maintenance_work_mem_worker =
 		(nindexes_mwm > 0) ?
 		maintenance_work_mem / Min(parallel_workers, nindexes_mwm) :
@@ -3455,6 +3462,6 @@ vacuum_error_callback(void *arg)
 		errcontext(_("while vacuuming block %u of relation \"%s.%s\""),
 				cbarg->blkno, cbarg->relnamespace, cbarg->relname);
 	else if (cbarg->stage == 2)
-		errcontext(_("while vacuuming index \"%s.%s\""),
-				cbarg->relnamespace, cbarg->relname);
+		errcontext(_("while vacuuming index \"%s.%s\" of table \"%s\""),
+				cbarg->relnamespace, cbarg->indname, cbarg->relname);
 }
-- 
2.7.4

