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/copyfuncs.c | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'src/backend/nodes/copyfuncs.c') diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c index f0a49155024..2dce7bab18f 100644 --- a/src/backend/nodes/copyfuncs.c +++ b/src/backend/nodes/copyfuncs.c @@ -15,7 +15,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.408 2008/10/07 19:27:04 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.409 2008/10/21 20:42:52 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1592,6 +1592,22 @@ _copyFlattenedSubLink(FlattenedSubLink *from) return newnode; } +/* + * _copyPlaceHolderVar + */ +static PlaceHolderVar * +_copyPlaceHolderVar(PlaceHolderVar *from) +{ + PlaceHolderVar *newnode = makeNode(PlaceHolderVar); + + COPY_NODE_FIELD(phexpr); + COPY_BITMAPSET_FIELD(phrels); + COPY_SCALAR_FIELD(phid); + COPY_SCALAR_FIELD(phlevelsup); + + return newnode; +} + /* * _copySpecialJoinInfo */ @@ -1631,6 +1647,23 @@ _copyAppendRelInfo(AppendRelInfo *from) return newnode; } +/* + * _copyPlaceHolderInfo + */ +static PlaceHolderInfo * +_copyPlaceHolderInfo(PlaceHolderInfo *from) +{ + PlaceHolderInfo *newnode = makeNode(PlaceHolderInfo); + + COPY_SCALAR_FIELD(phid); + COPY_NODE_FIELD(ph_var); + COPY_BITMAPSET_FIELD(ph_eval_at); + COPY_BITMAPSET_FIELD(ph_needed); + COPY_SCALAR_FIELD(ph_width); + + return newnode; +} + /* **************************************************************** * parsenodes.h copy functions * **************************************************************** @@ -3438,12 +3471,18 @@ copyObject(void *from) case T_FlattenedSubLink: retval = _copyFlattenedSubLink(from); break; + case T_PlaceHolderVar: + retval = _copyPlaceHolderVar(from); + break; case T_SpecialJoinInfo: retval = _copySpecialJoinInfo(from); break; case T_AppendRelInfo: retval = _copyAppendRelInfo(from); break; + case T_PlaceHolderInfo: + retval = _copyPlaceHolderInfo(from); + break; /* * VALUE NODES -- cgit v1.2.3