From f155cc82ec3f9e4e75cade7348bfe19493950e68 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Tue, 27 Mar 2001 18:02:19 +0000
Subject: [PATCH] Quick hack to fix Oliver Elphick's problem with subselects in
 an inheritance query: make duplicate copies of subplans in
 adjust_inherited_attrs. When we redesign querytrees we really gotta do
 something about this issue of whether querytrees are read-only and can share
 substructure or not.

---
 src/backend/optimizer/prep/prepunion.c | 27 ++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)

diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c
index 968b9ded780..0b173466cf9 100644
--- a/src/backend/optimizer/prep/prepunion.c
+++ b/src/backend/optimizer/prep/prepunion.c
@@ -14,7 +14,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.61 2001/03/22 03:59:38 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.62 2001/03/27 18:02:19 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -679,7 +679,7 @@ adjust_inherited_attrs_mutator(Node *node,
 			adjust_inherited_attrs_mutator((Node *) oldinfo->clause, context);
 
 		newinfo->subclauseindices = NIL;
-		newinfo->eval_cost = -1;/* reset this too */
+		newinfo->eval_cost = -1;		/* reset this too */
 		newinfo->left_pathkey = NIL;	/* and these */
 		newinfo->right_pathkey = NIL;
 		newinfo->left_dispersion = -1;
@@ -692,6 +692,29 @@ adjust_inherited_attrs_mutator(Node *node,
 	 * NOTE: we do not need to recurse into sublinks, because they should
 	 * already have been converted to subplans before we see them.
 	 */
+
+	/*
+	 * BUT: although we don't need to recurse into subplans, we do need to
+	 * make sure that they are copied, not just referenced as
+	 * expression_tree_mutator will do by default.  Otherwise we'll have the
+	 * same subplan node referenced from each arm of the inheritance APPEND
+	 * plan, which will cause trouble in the executor.  This is a kluge
+	 * that should go away when we redesign querytrees.
+	 */
+	if (is_subplan(node))
+	{
+		SubPlan    *subplan;
+
+		/* Copy the node and process subplan args */
+		node = expression_tree_mutator(node, adjust_inherited_attrs_mutator,
+									   (void *) context);
+		/* Make sure we have separate copies of subplan and its rtable */
+		subplan = (SubPlan *) ((Expr *) node)->oper;
+		subplan->plan = copyObject(subplan->plan);
+		subplan->rtable = copyObject(subplan->rtable);
+		return node;
+	}
+
 	return expression_tree_mutator(node, adjust_inherited_attrs_mutator,
 								   (void *) context);
 }
-- 
GitLab