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/prep/prepunion.c | 36 ++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'src/backend/optimizer/prep/prepunion.c') diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c index 2e22d09b4a..0efd150f6b 100644 --- a/src/backend/optimizer/prep/prepunion.c +++ b/src/backend/optimizer/prep/prepunion.c @@ -22,7 +22,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.158 2008/10/07 19:27:04 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.159 2008/10/21 20:42:53 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1584,7 +1584,39 @@ adjust_appendrel_attrs_mutator(Node *node, AppendRelInfo *context) context->child_relid); return (Node *) fslink; } - /* Shouldn't need to handle SpecialJoinInfo or AppendRelInfo here */ + if (IsA(node, PlaceHolderVar)) + { + /* Copy the PlaceHolderVar node with correct mutation of subnodes */ + PlaceHolderVar *phv; + + phv = (PlaceHolderVar *) expression_tree_mutator(node, + adjust_appendrel_attrs_mutator, + (void *) context); + /* now fix PlaceHolderVar's relid sets */ + if (phv->phlevelsup == 0) + phv->phrels = adjust_relid_set(phv->phrels, + context->parent_relid, + context->child_relid); + return (Node *) phv; + } + if (IsA(node, PlaceHolderInfo)) + { + /* Copy the PlaceHolderInfo node with correct mutation of subnodes */ + PlaceHolderInfo *phinfo; + + phinfo = (PlaceHolderInfo *) expression_tree_mutator(node, + adjust_appendrel_attrs_mutator, + (void *) context); + /* now fix PlaceHolderInfo's relid sets */ + phinfo->ph_eval_at = adjust_relid_set(phinfo->ph_eval_at, + context->parent_relid, + context->child_relid); + phinfo->ph_needed = adjust_relid_set(phinfo->ph_needed, + context->parent_relid, + context->child_relid); + return (Node *) phinfo; + } + /* Shouldn't need to handle other planner auxiliary nodes here */ Assert(!IsA(node, SpecialJoinInfo)); Assert(!IsA(node, AppendRelInfo)); -- cgit v1.2.1