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/nodes/outfuncs.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'src/backend/nodes/outfuncs.c') diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index 75a04ce2fa..d1130760f9 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/nodes/outfuncs.c,v 1.342 2008/10/07 19:27:04 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/nodes/outfuncs.c,v 1.343 2008/10/21 20:42:52 tgl Exp $ * * NOTES * Every node type that can appear in stored rules' parsetrees *must* @@ -1412,6 +1412,8 @@ _outPlannerGlobal(StringInfo str, PlannerGlobal *node) WRITE_NODE_FIELD(finalrtable); WRITE_NODE_FIELD(relationOids); WRITE_NODE_FIELD(invalItems); + WRITE_UINT_FIELD(lastPHId); + WRITE_BOOL_FIELD(transientPlan); } static void @@ -1435,6 +1437,7 @@ _outPlannerInfo(StringInfo str, PlannerInfo *node) WRITE_NODE_FIELD(full_join_clauses); WRITE_NODE_FIELD(join_info_list); WRITE_NODE_FIELD(append_rel_list); + WRITE_NODE_FIELD(placeholder_list); WRITE_NODE_FIELD(query_pathkeys); WRITE_NODE_FIELD(group_pathkeys); WRITE_NODE_FIELD(distinct_pathkeys); @@ -1590,6 +1593,17 @@ _outFlattenedSubLink(StringInfo str, FlattenedSubLink *node) WRITE_NODE_FIELD(quals); } +static void +_outPlaceHolderVar(StringInfo str, PlaceHolderVar *node) +{ + WRITE_NODE_TYPE("PLACEHOLDERVAR"); + + WRITE_NODE_FIELD(phexpr); + WRITE_BITMAPSET_FIELD(phrels); + WRITE_UINT_FIELD(phid); + WRITE_UINT_FIELD(phlevelsup); +} + static void _outSpecialJoinInfo(StringInfo str, SpecialJoinInfo *node) { @@ -1619,6 +1633,18 @@ _outAppendRelInfo(StringInfo str, AppendRelInfo *node) WRITE_OID_FIELD(parent_reloid); } +static void +_outPlaceHolderInfo(StringInfo str, PlaceHolderInfo *node) +{ + WRITE_NODE_TYPE("PLACEHOLDERINFO"); + + WRITE_UINT_FIELD(phid); + WRITE_NODE_FIELD(ph_var); + WRITE_BITMAPSET_FIELD(ph_eval_at); + WRITE_BITMAPSET_FIELD(ph_needed); + WRITE_INT_FIELD(ph_width); +} + static void _outPlannerParamItem(StringInfo str, PlannerParamItem *node) { @@ -2539,12 +2565,18 @@ _outNode(StringInfo str, void *obj) case T_FlattenedSubLink: _outFlattenedSubLink(str, obj); break; + case T_PlaceHolderVar: + _outPlaceHolderVar(str, obj); + break; case T_SpecialJoinInfo: _outSpecialJoinInfo(str, obj); break; case T_AppendRelInfo: _outAppendRelInfo(str, obj); break; + case T_PlaceHolderInfo: + _outPlaceHolderInfo(str, obj); + break; case T_PlannerParamItem: _outPlannerParamItem(str, obj); break; -- cgit v1.2.1