From f92198c6bae4260bee53c6c683198d874c7253a8 Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Mon, 20 Jan 2020 14:44:06 -0600
Subject: [PATCH v12 4/5] errcontext for lazy_vacuum_index

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

diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index c980d96..fcec582 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -292,7 +292,7 @@ typedef struct LVRelStats
 	char		*relname;
 	char 		*relnamespace;
 	BlockNumber blkno;
-	int			stage;	/* 0: scan heap; 1: vacuum heap */
+	int			stage;	/* 0: scan heap; 1: vacuum heap; 2: vacuum index */
 } LVRelStats;
 
 /* A few variables that don't seem worth passing around as parameters */
@@ -2356,6 +2356,8 @@ lazy_vacuum_index(Relation indrel, IndexBulkDeleteResult **stats,
 	IndexVacuumInfo ivinfo;
 	const char *msg;
 	PGRUsage	ru0;
+	ErrorContextCallback errcallback;
+	LVRelStats	vacrelstats; /* Used for error callback, only */
 
 	pg_rusage_init(&ru0);
 
@@ -2367,10 +2369,24 @@ lazy_vacuum_index(Relation indrel, IndexBulkDeleteResult **stats,
 	ivinfo.num_heap_tuples = reltuples;
 	ivinfo.strategy = vac_strategy;
 
+	/* Setup error traceback support for ereport() */
+	vacrelstats.relnamespace = get_namespace_name(RelationGetNamespace(indrel));
+	vacrelstats.relname = RelationGetRelationName(indrel);
+	vacrelstats.blkno = InvalidBlockNumber; /* Not used */
+	vacrelstats.stage = 2;
+
+	errcallback.callback = vacuum_error_callback;
+	errcallback.arg = (void *) &vacrelstats;
+	errcallback.previous = error_context_stack;
+	error_context_stack = &errcallback;
+
 	/* Do bulk deletion */
 	*stats = index_bulk_delete(&ivinfo, *stats,
 							   lazy_tid_reaped, (void *) dead_tuples);
 
+	/* Pop the error context stack */
+	error_context_stack = errcallback.previous;
+
 	if (IsParallelWorker())
 		msg = gettext_noop("scanned index \"%s\" to remove %d row versions by parallel vacuum worker");
 	else
@@ -3428,3 +3444,7 @@ vacuum_error_callback(void *arg)
 	else if (cbarg->stage == 1)
 		errcontext(_("while vacuuming block %u of relation \"%s.%s\""),
 				cbarg->blkno, cbarg->relnamespace, cbarg->relname);
+	else if (cbarg->stage == 2)
+		errcontext(_("while vacuuming relation \"%s.%s\""),
+				cbarg->relnamespace, cbarg->relname);
+}
-- 
2.7.4

