From 68fb8b46e3bafe6f176499e088af9930272a2f2e Mon Sep 17 00:00:00 2001
From: Sami Imseih <simseih@amazon.com>
Date: Mon, 16 Dec 2024 22:28:43 -0600
Subject: [PATCH 1/1] skip join RTE in deparse_context_for_plan_tree

---
 src/backend/utils/adt/ruleutils.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 2194ab3dfa..33811f327d 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -167,6 +167,7 @@ typedef struct
 	List	   *subplans;		/* List of Plan trees for SubPlans */
 	List	   *ctes;			/* List of CommonTableExpr nodes */
 	AppendRelInfo **appendrels; /* Array of AppendRelInfo nodes, or NULL */
+	bool		deparse_for_plan; /* deparsing for explain plan? */
 	/* Workspace for column alias assignment: */
 	bool		unique_using;	/* Are we making USING names globally unique */
 	List	   *using_names;	/* List of assigned names for USING columns */
@@ -3751,6 +3752,7 @@ deparse_context_for_plan_tree(PlannedStmt *pstmt, List *rtable_names)
 	dpns->rtable_names = rtable_names;
 	dpns->subplans = pstmt->subplans;
 	dpns->ctes = NIL;
+	dpns->deparse_for_plan = true;
 	if (pstmt->appendRelations)
 	{
 		/* Set up the array, indexed by child relid */
@@ -3772,11 +3774,6 @@ deparse_context_for_plan_tree(PlannedStmt *pstmt, List *rtable_names)
 	else
 		dpns->appendrels = NULL;	/* don't need it */
 
-	/*
-	 * Set up column name aliases.  We will get rather bogus results for join
-	 * RTEs, but that doesn't matter because plan trees don't contain any join
-	 * alias Vars.
-	 */
 	set_simple_column_names(dpns);
 
 	/* Return a one-deep namespace stack */
@@ -4088,6 +4085,13 @@ set_simple_column_names(deparse_namespace *dpns)
 		RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
 		deparse_columns *colinfo = (deparse_columns *) lfirst(lc2);
 
+		/*
+		 * plan trees don't contain any join alias Vars, so we can skip
+		 * setting column names in this case.
+		 */
+		if (dpns->deparse_for_plan && rte->rtekind == RTE_JOIN)
+			continue;
+
 		set_relation_column_names(dpns, rte, colinfo);
 	}
 }
-- 
2.39.5 (Apple Git-154)

