From 7c76861addf213b3b417d783b04b12112fc468f1 Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Mon, 20 Jan 2020 15:26:39 -0600
Subject: [PATCH v14 3/3] Avoid extra calls like GetRelationName

---
 src/backend/access/heap/vacuumlazy.c | 75 ++++++++++++++++++------------------
 1 file changed, 37 insertions(+), 38 deletions(-)

diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index a62dc79..1841e7c 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -270,6 +270,9 @@ typedef struct LVParallelState
 
 typedef struct LVRelStats
 {
+	char		*relname;
+	char 		*relnamespace;
+
 	/* useindex = true means two-pass strategy; false means one-pass */
 	bool		useindex;
 	/* Overall statistics about rel */
@@ -292,8 +295,6 @@ typedef struct LVRelStats
 	bool		lock_waiter_detected;
 
 	/* Used by the error callback */
-	char		*relname;
-	char 		*relnamespace;
 	BlockNumber blkno;
 	char 		*indname;
 	int			stage;	/* 0: scan heap; 1: vacuum heap; 2: vacuum index */
@@ -603,8 +604,8 @@ heap_vacuum_rel(Relation onerel, VacuumParams *params,
 			}
 			appendStringInfo(&buf, msgfmt,
 							 get_database_name(MyDatabaseId),
-							 get_namespace_name(RelationGetNamespace(onerel)),
-							 RelationGetRelationName(onerel),
+							 vacrelstats.relnamespace,
+							 vacrelstats.relname,
 							 vacrelstats.num_index_scans);
 			appendStringInfo(&buf, _("pages: %u removed, %u remain, %u skipped due to pins, %u skipped frozen\n"),
 							 vacrelstats.pages_removed,
@@ -703,7 +704,6 @@ lazy_scan_heap(Relation onerel, VacuumParams *params,
 	BlockNumber nblocks,
 				blkno;
 	HeapTupleData tuple;
-	char	   *relname;
 	TransactionId relfrozenxid = onerel->rd_rel->relfrozenxid;
 	TransactionId relminmxid = onerel->rd_rel->relminmxid;
 	BlockNumber empty_pages,
@@ -732,18 +732,6 @@ lazy_scan_heap(Relation onerel, VacuumParams *params,
 
 	pg_rusage_init(&ru0);
 
-	relname = RelationGetRelationName(onerel);
-	if (aggressive)
-		ereport(elevel,
-				(errmsg("aggressively vacuuming \"%s.%s\"",
-						get_namespace_name(RelationGetNamespace(onerel)),
-						relname)));
-	else
-		ereport(elevel,
-				(errmsg("vacuuming \"%s.%s\"",
-						get_namespace_name(RelationGetNamespace(onerel)),
-						relname)));
-
 	empty_pages = vacuumed_pages = 0;
 	next_fsm_block_to_vacuum = (BlockNumber) 0;
 	num_tuples = live_tuples = tups_vacuumed = nkeep = nunused = 0;
@@ -758,6 +746,28 @@ lazy_scan_heap(Relation onerel, VacuumParams *params,
 	vacrelstats.nonempty_pages = 0;
 	vacrelstats.latestRemovedXid = InvalidTransactionId;
 
+	vacrelstats.relnamespace = get_namespace_name(RelationGetNamespace(onerel));
+	vacrelstats.relname = RelationGetRelationName(onerel);
+	vacrelstats.blkno = InvalidBlockNumber; /* Not known yet */
+	vacrelstats.stage = 0;
+
+	if (aggressive)
+		ereport(elevel,
+				(errmsg("aggressively vacuuming \"%s.%s\"",
+						vacrelstats.relnamespace,
+						vacrelstats.relname)));
+	else
+		ereport(elevel,
+				(errmsg("vacuuming \"%s.%s\"",
+						vacrelstats.relnamespace,
+						vacrelstats.relname)));
+
+	/* Setup error traceback support for ereport() */
+	errcallback.callback = vacuum_error_callback;
+	errcallback.arg = (void *) &vacrelstats;
+	errcallback.previous = error_context_stack;
+	error_context_stack = &errcallback;
+
 	/*
 	 * Initialize the state for a parallel vacuum.  As of now, only one worker
 	 * can be used for an index, so we invoke parallelism only if there are at
@@ -778,7 +788,7 @@ lazy_scan_heap(Relation onerel, VacuumParams *params,
 			if (params->nworkers > 0)
 				ereport(WARNING,
 						(errmsg("disabling parallel option of vacuum on \"%s\" --- cannot vacuum temporary tables in parallel",
-								RelationGetRelationName(onerel))));
+								vacrelstats.relname)));
 		}
 		else
 			lps = begin_parallel_vacuum(RelationGetRelid(onerel), Irel,
@@ -875,17 +885,6 @@ lazy_scan_heap(Relation onerel, VacuumParams *params,
 	else
 		skipping_blocks = false;
 
-	/* Setup error traceback support for ereport() */
-	vacrelstats.relnamespace = get_namespace_name(RelationGetNamespace(onerel));
-	vacrelstats.relname = relname;
-	vacrelstats.blkno = InvalidBlockNumber; /* Not known yet */
-	vacrelstats.stage = 0;
-
-	errcallback.callback = vacuum_error_callback;
-	errcallback.arg = (void *) &vacrelstats;
-	errcallback.previous = error_context_stack;
-	error_context_stack = &errcallback;
-
 	for (blkno = 0; blkno < nblocks; blkno++)
 	{
 		Buffer		buf;
@@ -1557,7 +1556,7 @@ lazy_scan_heap(Relation onerel, VacuumParams *params,
 				 && VM_ALL_VISIBLE(onerel, blkno, &vmbuffer))
 		{
 			elog(WARNING, "page is not marked all-visible but visibility map bit is set in relation \"%s\" page %u",
-				 relname, blkno);
+				 vacrelstats.relname, blkno);
 			visibilitymap_clear(onerel, blkno, vmbuffer,
 								VISIBILITYMAP_VALID_BITS);
 		}
@@ -1578,7 +1577,7 @@ lazy_scan_heap(Relation onerel, VacuumParams *params,
 		else if (PageIsAllVisible(page) && has_dead_tuples)
 		{
 			elog(WARNING, "page containing dead tuples is marked as all-visible in relation \"%s\" page %u",
-				 relname, blkno);
+				 vacrelstats.relname, blkno);
 			PageClearAllVisible(page);
 			MarkBufferDirty(buf);
 			visibilitymap_clear(onerel, blkno, vmbuffer,
@@ -1691,7 +1690,7 @@ lazy_scan_heap(Relation onerel, VacuumParams *params,
 	if (vacuumed_pages)
 		ereport(elevel,
 				(errmsg("\"%s\": removed %.0f row versions in %u pages",
-						RelationGetRelationName(onerel),
+						vacrelstats.relname,
 						tups_vacuumed, vacuumed_pages)));
 
 	/*
@@ -1720,7 +1719,7 @@ lazy_scan_heap(Relation onerel, VacuumParams *params,
 
 	ereport(elevel,
 			(errmsg("\"%s\": found %.0f removable, %.0f nonremovable row versions in %u out of %u pages",
-					RelationGetRelationName(onerel),
+					vacrelstats.relname,
 					tups_vacuumed, num_tuples,
 					vacrelstats.scanned_pages, nblocks),
 			 errdetail_internal("%s", buf.data)));
@@ -1862,7 +1861,7 @@ lazy_vacuum_heap(Relation onerel)
 
 	ereport(elevel,
 			(errmsg("\"%s\": removed %d row versions in %d pages",
-					RelationGetRelationName(onerel),
+					vacrelstats.relname,
 					tupindex, npages),
 			 errdetail_internal("%s", pg_rusage_show(&ru0))));
 }
@@ -2399,7 +2398,7 @@ lazy_vacuum_index(Relation indrel, IndexBulkDeleteResult **stats,
 
 	ereport(elevel,
 			(errmsg(msg,
-					RelationGetRelationName(indrel),
+					vacrelstats.relname,
 					dead_tuples->num_tuples),
 			 errdetail_internal("%s", pg_rusage_show(&ru0))));
 }
@@ -2542,7 +2541,7 @@ lazy_truncate_heap(Relation onerel)
 				vacrelstats.lock_waiter_detected = true;
 				ereport(elevel,
 						(errmsg("\"%s\": stopping truncate due to conflicting lock request",
-								RelationGetRelationName(onerel))));
+								vacrelstats.relname)));
 				return;
 			}
 
@@ -2607,7 +2606,7 @@ lazy_truncate_heap(Relation onerel)
 
 		ereport(elevel,
 				(errmsg("\"%s\": truncated %u to %u pages",
-						RelationGetRelationName(onerel),
+						vacrelstats.relname,
 						old_rel_pages, new_rel_pages),
 				 errdetail_internal("%s",
 									pg_rusage_show(&ru0))));
@@ -2672,7 +2671,7 @@ count_nondeletable_pages(Relation onerel)
 				{
 					ereport(elevel,
 							(errmsg("\"%s\": suspending truncate due to conflicting lock request",
-									RelationGetRelationName(onerel))));
+									vacrelstats.relname)));
 
 					vacrelstats.lock_waiter_detected = true;
 					return blkno;
-- 
2.7.4

