From 410fcd0106c5ba9a4718b79d2a758fa40c246afc Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Thu, 6 Feb 2020 10:27:05 -0600
Subject: [PATCH v17 2/2] Functions to initialize errcontext

---
 src/backend/access/heap/vacuumlazy.c | 68 +++++++++++++++++-------------------
 1 file changed, 32 insertions(+), 36 deletions(-)

diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index 9358ab4..361e595 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -370,6 +370,8 @@ static void end_parallel_vacuum(Relation *Irel, IndexBulkDeleteResult **stats,
 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 init_error_context_heap(ErrorContextCallback *errcallback, vacuum_error_callback_arg *errcbarg, Relation onerel, int phase);
+static void init_error_context_index(ErrorContextCallback *errcallback, vacuum_error_callback_arg *errcbarg, Relation indrel, int phase);
 
 
 /*
@@ -882,15 +884,7 @@ lazy_scan_heap(Relation onerel, VacuumParams *params, LVRelStats *vacrelstats,
 		skipping_blocks = false;
 
 	/* Setup error traceback support for ereport() */
-	errcbarg.relnamespace = get_namespace_name(RelationGetNamespace(onerel));
-	errcbarg.relname = relname;
-	errcbarg.blkno = InvalidBlockNumber; /* Not known yet */
-	errcbarg.phase = PROGRESS_VACUUM_PHASE_SCAN_HEAP;
-
-	errcallback.callback = vacuum_error_callback;
-	errcallback.arg = (void *) &errcbarg;
-	errcallback.previous = error_context_stack;
-	error_context_stack = &errcallback;
+	init_error_context_heap(&errcallback, &errcbarg, onerel, PROGRESS_VACUUM_PHASE_SCAN_HEAP);
 
 	for (blkno = 0; blkno < nblocks; blkno++)
 	{
@@ -1815,15 +1809,7 @@ lazy_vacuum_heap(Relation onerel, LVRelStats *vacrelstats)
 	/*
 	 * Setup error traceback support for ereport()
 	 */
-	errcbarg.relnamespace = get_namespace_name(RelationGetNamespace(onerel));
-	errcbarg.relname = RelationGetRelationName(onerel);
-	errcbarg.blkno = InvalidBlockNumber; /* Not known yet */
-	errcbarg.phase = PROGRESS_VACUUM_PHASE_VACUUM_HEAP;
-
-	errcallback.callback = vacuum_error_callback;
-	errcallback.arg = (void *) &errcbarg;
-	errcallback.previous = error_context_stack;
-	error_context_stack = &errcallback;
+	init_error_context_heap(&errcallback, &errcbarg, onerel, PROGRESS_VACUUM_PHASE_VACUUM_HEAP);
 
 	pg_rusage_init(&ru0);
 	npages = 0;
@@ -2384,15 +2370,7 @@ lazy_vacuum_index(Relation indrel, IndexBulkDeleteResult **stats,
 	ivinfo.strategy = vac_strategy;
 
 	/* Setup error traceback support for ereport() */
-	errcbarg.relnamespace = get_namespace_name(RelationGetNamespace(indrel));
-	errcbarg.indname = RelationGetRelationName(indrel);
-	errcbarg.relname = get_rel_name(indrel->rd_index->indexrelid);
-	errcbarg.phase = PROGRESS_VACUUM_PHASE_VACUUM_INDEX;
-
-	errcallback.callback = vacuum_error_callback;
-	errcallback.arg = (void *) &errcbarg;
-	errcallback.previous = error_context_stack;
-	error_context_stack = &errcallback;
+	init_error_context_index(&errcallback, &errcbarg, indrel, PROGRESS_VACUUM_PHASE_VACUUM_INDEX);
 
 	/* Do bulk deletion */
 	*stats = index_bulk_delete(&ivinfo, *stats,
@@ -2442,15 +2420,7 @@ lazy_cleanup_index(Relation indrel,
 	ivinfo.strategy = vac_strategy;
 
 	/* Setup error traceback support for ereport() */
-	errcbarg.relnamespace = get_namespace_name(RelationGetNamespace(indrel));
-	errcbarg.indname = RelationGetRelationName(indrel);
-	errcbarg.relname = get_rel_name(indrel->rd_index->indexrelid);
-	errcbarg.phase = PROGRESS_VACUUM_PHASE_INDEX_CLEANUP;
-
-	errcallback.previous = error_context_stack;
-	errcallback.callback = vacuum_error_callback;
-	errcallback.arg = (void *) &errcbarg;
-	error_context_stack = &errcallback;
+	init_error_context_index(&errcallback, &errcbarg, indrel, PROGRESS_VACUUM_PHASE_INDEX_CLEANUP);
 
 	*stats = index_vacuum_cleanup(&ivinfo, *stats);
 
@@ -3492,3 +3462,29 @@ vacuum_error_callback(void *arg)
 			break;
 	}
 }
+
+/* Initialize error context for heap operations */
+static void
+init_error_context_heap(ErrorContextCallback *errcallback, vacuum_error_callback_arg *errcbarg, Relation onerel, int phase)
+{
+	errcbarg->relnamespace = get_namespace_name(RelationGetNamespace(onerel));
+	errcbarg->relname = RelationGetRelationName(onerel);
+	errcbarg->indname = NULL; /* Not used for heap */
+	errcbarg->blkno = InvalidBlockNumber; /* Not known yet */
+	errcbarg->phase = phase;
+
+	errcallback->callback = vacuum_error_callback;
+	errcallback->arg = errcbarg;
+	errcallback->previous = error_context_stack;
+	error_context_stack = errcallback;
+}
+
+/* Initialize error context for index operations */
+static void
+init_error_context_index(ErrorContextCallback *errcallback, vacuum_error_callback_arg *errcbarg, Relation indrel, int phase)
+{
+	/* Piggyback on the heap function but specially set relation names.  Note, blkno is not used for index. */
+	init_error_context_heap(errcallback, errcbarg, indrel, phase);
+	errcbarg->indname = RelationGetRelationName(indrel);
+	errcbarg->relname = get_rel_name(indrel->rd_index->indexrelid);
+}
-- 
2.7.4

