aboutsummaryrefslogtreecommitdiff
path: root/src/backend/optimizer/plan
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2000-04-04 01:21:48 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2000-04-04 01:21:48 +0000
commit1c72a8a37a446eebb09bcfb5eb6bad7ddd1655c8 (patch)
treea886d5b14699e0a3a271cc2b2ccd5a1e5dff8b2a /src/backend/optimizer/plan
parent8cdabf074118ab0c86423891cc0b85c9c94939e7 (diff)
downloadpostgresql-1c72a8a37a446eebb09bcfb5eb6bad7ddd1655c8.tar.gz
postgresql-1c72a8a37a446eebb09bcfb5eb6bad7ddd1655c8.zip
Fix extremely nasty little bug observed when a sub-SELECT appears in
WHERE in a place where it can be part of a nestloop inner indexqual. As the code stood, it put the same physical sub-Plan node into both indxqual and indxqualorig of the IndexScan plan node. That confused later processing in the optimizer (which expected that tracing the subPlan list would visit each subplan node exactly once), and would probably have blown up in the executor if the planner hadn't choked first. Fix by making the 'fixed' indexqual be a complete deep copy of the original indexqual, rather than trying to share nodes below the topmost operator node. This had further ramifications though, because we were making the aforesaid list of sub-Plan nodes during SS_process_sublinks which is run before construction of the 'fixed' indexqual, meaning that the copy of the sub-Plan didn't show up in that list. Fix by rearranging logic so that the sub-Plan list is built by the final set_plan_references pass, not in SS_process_sublinks. This may sound like a mess, but it's actually a good deal cleaner now than it was before, because we are no longer dependent on the assumption that planning will never make a copy of a sub-Plan node.
Diffstat (limited to 'src/backend/optimizer/plan')
-rw-r--r--src/backend/optimizer/plan/createplan.c22
-rw-r--r--src/backend/optimizer/plan/setrefs.c83
-rw-r--r--src/backend/optimizer/plan/subselect.c41
3 files changed, 54 insertions, 92 deletions
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index 8e20ae302e1..3fab7f08b87 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -10,7 +10,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.87 2000/03/22 22:08:34 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.88 2000/04/04 01:21:47 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -724,7 +724,9 @@ create_hashjoin_node(HashPath *best_path,
* track of which index applies to each subgroup of index qual clauses...
*
* Returns a modified copy of the indexqual list --- the original is not
- * changed.
+ * changed. Note also that the copy shares no substructure with the
+ * original; this is needed in case there is a subplan in it (we need
+ * two separate copies of the subplan tree, or things will go awry).
*/
static List *
@@ -808,11 +810,13 @@ fix_indxqual_sublist(List *indexqual, int baserelid, Oid relam,
get_relattval((Node *) clause, baserelid,
&relid, &attno, &constval, &flag);
- /* Copy enough structure to allow commuting and replacing an operand
- * without changing original clause.
+ /* Make a copy that will become the fixed clause.
+ *
+ * We used to try to do a shallow copy here, but that fails if there
+ * is a subplan in the arguments of the opclause. So just do a
+ * full copy.
*/
- newclause = make_clause(clause->opType, clause->oper,
- listCopy(clause->args));
+ newclause = (Expr *) copyObject((Node *) clause);
/* If the indexkey is on the right, commute the clause. */
if ((flag & SEL_RIGHT) == 0)
@@ -834,11 +838,7 @@ fix_indxqual_sublist(List *indexqual, int baserelid, Oid relam,
newopno = indexable_operator(newclause, opclass, relam, true);
if (newopno == InvalidOid)
elog(ERROR, "fix_indxqual_sublist: failed to find substitute op");
- if (newopno != ((Oper *) newclause->oper)->opno)
- {
- newclause->oper = (Node *) copyObject(newclause->oper);
- ((Oper *) newclause->oper)->opno = newopno;
- }
+ ((Oper *) newclause->oper)->opno = newopno;
fixed_qual = lappend(fixed_qual, newclause);
}
diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.c
index b44aa6408b8..756333e0059 100644
--- a/src/backend/optimizer/plan/setrefs.c
+++ b/src/backend/optimizer/plan/setrefs.c
@@ -9,7 +9,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.60 2000/01/26 05:56:38 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.61 2000/04/04 01:21:47 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -59,6 +59,8 @@ static bool fix_opids_walker(Node *node, void *context);
* for the convenience of the executor. We update Vars in upper plan nodes
* to refer to the outputs of their subplans, and we compute regproc OIDs
* for operators (ie, we look up the function that implements each op).
+ * We must also build lists of all the subplan nodes present in each
+ * plan node's expression trees.
*
* set_plan_references recursively traverses the whole plan tree.
*
@@ -72,6 +74,11 @@ set_plan_references(Plan *plan)
if (plan == NULL)
return;
+ /* We must rebuild the plan's list of subplan nodes, since we are
+ * copying/mutating its expression trees.
+ */
+ plan->subPlan = NIL;
+
/*
* Plan-type-specific fixes
*/
@@ -83,6 +90,12 @@ set_plan_references(Plan *plan)
case T_IndexScan:
fix_opids((Node *) ((IndexScan *) plan)->indxqual);
fix_opids((Node *) ((IndexScan *) plan)->indxqualorig);
+ plan->subPlan =
+ nconc(plan->subPlan,
+ pull_subplans((Node *) ((IndexScan *) plan)->indxqual));
+ plan->subPlan =
+ nconc(plan->subPlan,
+ pull_subplans((Node *) ((IndexScan *) plan)->indxqualorig));
break;
case T_NestLoop:
set_join_references((Join *) plan);
@@ -90,10 +103,16 @@ set_plan_references(Plan *plan)
case T_MergeJoin:
set_join_references((Join *) plan);
fix_opids((Node *) ((MergeJoin *) plan)->mergeclauses);
+ plan->subPlan =
+ nconc(plan->subPlan,
+ pull_subplans((Node *) ((MergeJoin *) plan)->mergeclauses));
break;
case T_HashJoin:
set_join_references((Join *) plan);
fix_opids((Node *) ((HashJoin *) plan)->hashclauses);
+ plan->subPlan =
+ nconc(plan->subPlan,
+ pull_subplans((Node *) ((HashJoin *) plan)->hashclauses));
break;
case T_Material:
case T_Sort:
@@ -119,6 +138,9 @@ set_plan_references(Plan *plan)
if (plan->lefttree != NULL)
set_uppernode_references(plan, (Index) OUTER);
fix_opids(((Result *) plan)->resconstantqual);
+ plan->subPlan =
+ nconc(plan->subPlan,
+ pull_subplans(((Result *) plan)->resconstantqual));
break;
case T_Append:
foreach(pl, ((Append *) plan)->appendplans)
@@ -136,10 +158,17 @@ set_plan_references(Plan *plan)
}
/*
- * For all plan types, fix operators in targetlist and qual expressions
+ * For all plan types, fix operators in targetlist and qual expressions,
+ * and find subplans therein.
*/
fix_opids((Node *) plan->targetlist);
fix_opids((Node *) plan->qual);
+ plan->subPlan =
+ nconc(plan->subPlan,
+ pull_subplans((Node *) plan->targetlist));
+ plan->subPlan =
+ nconc(plan->subPlan,
+ pull_subplans((Node *) plan->qual));
/*
* Now recurse into subplans, if any
@@ -302,31 +331,7 @@ join_references_mutator(Node *node,
*/
if (var->varno != context->acceptable_rel)
elog(ERROR, "join_references: variable not in subplan target lists");
- return (Node *) newvar; /* copy is probably not necessary here... */
- }
- /*
- * expression_tree_mutator will copy SubPlan nodes if given a chance.
- * We do not want to do that here, because subselect.c has already
- * constructed the initPlan and subPlan lists of the current plan node
- * and we mustn't leave those dangling (ie, pointing to different
- * copies of the nodes than what's in the targetlist & quals...)
- * Instead, alter the SubPlan in-place. Grotty --- is there a better way?
- */
- if (is_subplan(node))
- {
- Expr *expr = (Expr *) node;
- SubLink *sublink = ((SubPlan *) expr->oper)->sublink;
-
- /* transform args list (params to be passed to subplan) */
- expr->args = (List *)
- join_references_mutator((Node *) expr->args,
- context);
- /* transform sublink's oper list as well */
- sublink->oper = (List *)
- join_references_mutator((Node *) sublink->oper,
- context);
-
- return (Node *) expr;
+ return (Node *) newvar;
}
return expression_tree_mutator(node,
join_references_mutator,
@@ -383,30 +388,6 @@ replace_vars_with_subplan_refs_mutator(Node *node,
newvar->varattno = resdom->resno;
return (Node *) newvar;
}
- /*
- * expression_tree_mutator will copy SubPlan nodes if given a chance.
- * We do not want to do that here, because subselect.c has already
- * constructed the initPlan and subPlan lists of the current plan node
- * and we mustn't leave those dangling (ie, pointing to different
- * copies of the nodes than what's in the targetlist & quals...)
- * Instead, alter the SubPlan in-place. Grotty --- is there a better way?
- */
- if (is_subplan(node))
- {
- Expr *expr = (Expr *) node;
- SubLink *sublink = ((SubPlan *) expr->oper)->sublink;
-
- /* transform args list (params to be passed to subplan) */
- expr->args = (List *)
- replace_vars_with_subplan_refs_mutator((Node *) expr->args,
- context);
- /* transform sublink's oper list as well */
- sublink->oper = (List *)
- replace_vars_with_subplan_refs_mutator((Node *) sublink->oper,
- context);
-
- return (Node *) expr;
- }
return expression_tree_mutator(node,
replace_vars_with_subplan_refs_mutator,
(void *) context);
diff --git a/src/backend/optimizer/plan/subselect.c b/src/backend/optimizer/plan/subselect.c
index 3928f578360..b77d8b586f6 100644
--- a/src/backend/optimizer/plan/subselect.c
+++ b/src/backend/optimizer/plan/subselect.c
@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/subselect.c,v 1.33 2000/03/21 05:12:02 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/subselect.c,v 1.34 2000/04/04 01:21:47 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -442,6 +442,12 @@ set_unioni(List *l1, List *l2)
* finalize_primnode: build lists of subplans and params appearing
* in the given expression tree. NOTE: items are added to lists passed in,
* so caller must initialize lists to NIL before first call!
+ *
+ * Note: the subplan list that is constructed here and assigned to the
+ * plan's subPlan field will be replaced with an up-to-date list in
+ * set_plan_references(). We could almost dispense with building this
+ * subplan list at all; I believe the only place that uses it is the
+ * check in make_subplan to see whether a subselect has any subselects.
*/
typedef struct finalize_primnode_results {
@@ -604,6 +610,10 @@ SS_finalize_plan(Plan *plan)
case T_IndexScan:
finalize_primnode((Node *) ((IndexScan *) plan)->indxqual,
&results);
+ /* we need not look at indxqualorig, since it will have the
+ * same param references as indxqual, and we aren't really
+ * concerned yet about having a complete subplan list.
+ */
break;
case T_MergeJoin:
@@ -670,32 +680,3 @@ SS_finalize_plan(Plan *plan)
return results.paramids;
}
-
-/*
- * Construct a list of all subplans found within the given node tree.
- */
-
-static bool SS_pull_subplan_walker(Node *node, List **listptr);
-
-List *
-SS_pull_subplan(Node *expr)
-{
- List *result = NIL;
-
- SS_pull_subplan_walker(expr, &result);
- return result;
-}
-
-static bool
-SS_pull_subplan_walker(Node *node, List **listptr)
-{
- if (node == NULL)
- return false;
- if (is_subplan(node))
- {
- *listptr = lappend(*listptr, ((Expr *) node)->oper);
- /* fall through to check args to subplan */
- }
- return expression_tree_walker(node, SS_pull_subplan_walker,
- (void *) listptr);
-}