From e6ae3b5dbf2c07bceb737c5a0ff199b1156051d1 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 21 Oct 2008 20:42:53 +0000 Subject: Add a concept of "placeholder" variables to the planner. These are variables that represent some expression that we desire to compute below the top level of the plan, and then let that value "bubble up" as though it were a plain Var (ie, a column value). The immediate application is to allow sub-selects to be flattened even when they are below an outer join and have non-nullable output expressions. Formerly we couldn't flatten because such an expression wouldn't properly go to NULL when evaluated above the outer join. Now, we wrap it in a PlaceHolderVar and arrange for the actual evaluation to occur below the outer join. When the resulting Var bubbles up through the join, it will be set to NULL if necessary, yielding the correct results. This fixes a planner limitation that's existed since 7.1. In future we might want to use this mechanism to re-introduce some form of Hellerstein's "expensive functions" optimization, ie place the evaluation of an expensive function at the most suitable point in the plan tree. --- src/backend/optimizer/util/clauses.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'src/backend/optimizer/util/clauses.c') diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c index 14b9313a9a..c826ecb2ad 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.269 2008/10/09 19:27:40 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.270 2008/10/21 20:42:53 tgl Exp $ * * HISTORY * AUTHOR DATE MAJOR EVENT @@ -1188,6 +1188,12 @@ find_nonnullable_rels_walker(Node *node, bool top_level) result = find_nonnullable_rels_walker((Node *) expr->quals, top_level); } + else if (IsA(node, PlaceHolderVar)) + { + PlaceHolderVar *phv = (PlaceHolderVar *) node; + + result = find_nonnullable_rels_walker((Node *) phv->phexpr, top_level); + } return result; } @@ -1393,6 +1399,12 @@ find_nonnullable_vars_walker(Node *node, bool top_level) result = find_nonnullable_vars_walker((Node *) expr->quals, top_level); } + else if (IsA(node, PlaceHolderVar)) + { + PlaceHolderVar *phv = (PlaceHolderVar *) node; + + result = find_nonnullable_vars_walker((Node *) phv->phexpr, top_level); + } return result; } @@ -1921,6 +1933,7 @@ eval_const_expressions(PlannerInfo *root, Node *node) * constant. This effectively means that we plan using the first supplied * value of the Param. * 2. Fold stable, as well as immutable, functions to constants. + * 3. Reduce PlaceHolderVar nodes to their contained expressions. *-------------------- */ Node * @@ -2823,6 +2836,20 @@ eval_const_expressions_mutator(Node *node, newfslink->quals = quals; return (Node *) newfslink; } + if (IsA(node, PlaceHolderVar) && context->estimate) + { + /* + * In estimation mode, just strip the PlaceHolderVar node altogether; + * this amounts to estimating that the contained value won't be forced + * to null by an outer join. In regular mode we just use the default + * behavior (ie, simplify the expression but leave the PlaceHolderVar + * node intact). + */ + PlaceHolderVar *phv = (PlaceHolderVar *) node; + + return eval_const_expressions_mutator((Node *) phv->phexpr, + context); + } /* * For any node type not handled above, we recurse using -- cgit v1.2.1