From 308faea3626e8c270eb75659bec3f7e15125a73a Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Mon, 22 Jun 2020 18:51:35 -0500
Subject: [PATCH 2/4] Add assert and document why indname is safe

---
 src/backend/access/heap/vacuumlazy.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index a6a5d906c0..40173480b8 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -3606,10 +3606,23 @@ 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)
+	{
+		/*
+		 * indname is only ever saved during lazy_vacuum_index(), which
+		 * during which the phase information is not not further
+		 * manipulated, until it's restored before returning from
+		 * lazy_vacuum_index().
+		 */
+		Assert(indname == NULL);
+
 		pfree(errinfo->indname);
+		errinfo->indname = NULL;
+	}
 
-	/* For index phases, save the name of the current index for the callback */
-	errinfo->indname = indname ? pstrdup(indname) : NULL;
+	if (indname)
+	{
+		/* For index phases, save the name of the current index for the callback */
+		errinfo->indname = pstrdup(indname);
+	}
 }
-- 
2.17.0

