From 2d83e7c73ccf2fb2a7b81e7deb1603647a637760 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Tue, 9 Dec 2003 01:56:20 +0000
Subject: [PATCH] query_tree_mutator should copy RangeTblEntry nodes even when
 it's not planning to modify them itself.  Otherwise we end up with shared RTE
 substructure, which breaks inheritance_planner because the rte->inh flag
 needs to be independent in each copied subquery.  Per bug report from Chris
 Piker.

---
 src/backend/optimizer/util/clauses.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c
index 9b992c42721..697799a494e 100644
--- a/src/backend/optimizer/util/clauses.c
+++ b/src/backend/optimizer/util/clauses.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.155 2003/11/29 19:51:51 pgsql Exp $
+ *	  $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.156 2003/12/09 01:56:20 tgl Exp $
  *
  * HISTORY
  *	  AUTHOR			DATE			MAJOR EVENT
@@ -2960,36 +2960,31 @@ query_tree_mutator(Query *query,
 		RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
 		RangeTblEntry *newrte;
 
+		FLATCOPY(newrte, rte, RangeTblEntry);
 		switch (rte->rtekind)
 		{
 			case RTE_RELATION:
 			case RTE_SPECIAL:
-				/* nothing to do, don't bother to make a copy */
+				/* we don't bother to copy eref, aliases, etc; OK? */
 				break;
 			case RTE_SUBQUERY:
 				if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
 				{
-					FLATCOPY(newrte, rte, RangeTblEntry);
 					CHECKFLATCOPY(newrte->subquery, rte->subquery, Query);
 					MUTATE(newrte->subquery, newrte->subquery, Query *);
-					rte = newrte;
 				}
 				break;
 			case RTE_JOIN:
 				if (!(flags & QTW_IGNORE_JOINALIASES))
 				{
-					FLATCOPY(newrte, rte, RangeTblEntry);
 					MUTATE(newrte->joinaliasvars, rte->joinaliasvars, List *);
-					rte = newrte;
 				}
 				break;
 			case RTE_FUNCTION:
-				FLATCOPY(newrte, rte, RangeTblEntry);
 				MUTATE(newrte->funcexpr, rte->funcexpr, Node *);
-				rte = newrte;
 				break;
 		}
-		FastAppend(&newrt, rte);
+		FastAppend(&newrt, newrte);
 	}
 	query->rtable = FastListValue(&newrt);
 	return query;
-- 
GitLab