mirror of
https://github.com/postgres/postgres.git
synced 2025-07-31 22:04:40 +03:00
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.
This commit is contained in:
@ -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
|
||||
|
@ -22,7 +22,7 @@
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.333 2008/10/06 17:39:26 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.334 2008/10/21 20:42:52 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -763,6 +763,27 @@ _equalFlattenedSubLink(FlattenedSubLink *a, FlattenedSubLink *b)
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
_equalPlaceHolderVar(PlaceHolderVar *a, PlaceHolderVar *b)
|
||||
{
|
||||
/*
|
||||
* We intentionally do not compare phexpr. Two PlaceHolderVars with the
|
||||
* same ID and levelsup should be considered equal even if the contained
|
||||
* expressions have managed to mutate to different states. One way in
|
||||
* which that can happen is that initplan sublinks would get replaced by
|
||||
* differently-numbered Params when sublink folding is done. (The end
|
||||
* result of such a situation would be some unreferenced initplans, which
|
||||
* is annoying but not really a problem.)
|
||||
*
|
||||
* COMPARE_NODE_FIELD(phexpr);
|
||||
*/
|
||||
COMPARE_BITMAPSET_FIELD(phrels);
|
||||
COMPARE_SCALAR_FIELD(phid);
|
||||
COMPARE_SCALAR_FIELD(phlevelsup);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
_equalSpecialJoinInfo(SpecialJoinInfo *a, SpecialJoinInfo *b)
|
||||
{
|
||||
@ -792,6 +813,18 @@ _equalAppendRelInfo(AppendRelInfo *a, AppendRelInfo *b)
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
_equalPlaceHolderInfo(PlaceHolderInfo *a, PlaceHolderInfo *b)
|
||||
{
|
||||
COMPARE_SCALAR_FIELD(phid);
|
||||
COMPARE_NODE_FIELD(ph_var);
|
||||
COMPARE_BITMAPSET_FIELD(ph_eval_at);
|
||||
COMPARE_BITMAPSET_FIELD(ph_needed);
|
||||
COMPARE_SCALAR_FIELD(ph_width);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Stuff from parsenodes.h
|
||||
@ -2289,12 +2322,19 @@ equal(void *a, void *b)
|
||||
case T_FlattenedSubLink:
|
||||
retval = _equalFlattenedSubLink(a, b);
|
||||
break;
|
||||
case T_PlaceHolderVar:
|
||||
retval = _equalPlaceHolderVar(a, b);
|
||||
break;
|
||||
case T_SpecialJoinInfo:
|
||||
retval = _equalSpecialJoinInfo(a, b);
|
||||
break;
|
||||
case T_AppendRelInfo:
|
||||
retval = _equalAppendRelInfo(a, b);
|
||||
break;
|
||||
case T_PlaceHolderInfo:
|
||||
retval = _equalPlaceHolderInfo(a, b);
|
||||
break;
|
||||
|
||||
case T_List:
|
||||
case T_IntList:
|
||||
case T_OidList:
|
||||
|
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/nodes/nodeFuncs.c,v 1.34 2008/10/06 17:39:26 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/nodes/nodeFuncs.c,v 1.35 2008/10/21 20:42:52 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -220,6 +220,9 @@ exprType(Node *expr)
|
||||
case T_CurrentOfExpr:
|
||||
type = BOOLOID;
|
||||
break;
|
||||
case T_PlaceHolderVar:
|
||||
type = exprType((Node *) ((PlaceHolderVar *) expr)->phexpr);
|
||||
break;
|
||||
default:
|
||||
elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
|
||||
type = InvalidOid; /* keep compiler quiet */
|
||||
@ -420,6 +423,8 @@ exprTypmod(Node *expr)
|
||||
return ((CoerceToDomainValue *) expr)->typeMod;
|
||||
case T_SetToDefault:
|
||||
return ((SetToDefault *) expr)->typeMod;
|
||||
case T_PlaceHolderVar:
|
||||
return exprTypmod((Node *) ((PlaceHolderVar *) expr)->phexpr);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -876,6 +881,10 @@ exprLocation(Node *expr)
|
||||
case T_CommonTableExpr:
|
||||
loc = ((CommonTableExpr *) expr)->location;
|
||||
break;
|
||||
case T_PlaceHolderVar:
|
||||
/* just use argument's location */
|
||||
loc = exprLocation((Node *) ((PlaceHolderVar *) expr)->phexpr);
|
||||
break;
|
||||
default:
|
||||
/* for any other node type it's just unknown... */
|
||||
loc = -1;
|
||||
@ -1272,11 +1281,12 @@ expression_tree_walker(Node *node,
|
||||
{
|
||||
FlattenedSubLink *fslink = (FlattenedSubLink *) node;
|
||||
|
||||
if (expression_tree_walker((Node *) fslink->quals,
|
||||
walker, context))
|
||||
if (walker(fslink->quals, context))
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case T_PlaceHolderVar:
|
||||
return walker(((PlaceHolderVar *) node)->phexpr, context);
|
||||
case T_AppendRelInfo:
|
||||
{
|
||||
AppendRelInfo *appinfo = (AppendRelInfo *) node;
|
||||
@ -1286,6 +1296,8 @@ expression_tree_walker(Node *node,
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case T_PlaceHolderInfo:
|
||||
return walker(((PlaceHolderInfo *) node)->ph_var, context);
|
||||
default:
|
||||
elog(ERROR, "unrecognized node type: %d",
|
||||
(int) nodeTag(node));
|
||||
@ -1918,6 +1930,17 @@ expression_tree_mutator(Node *node,
|
||||
return (Node *) newnode;
|
||||
}
|
||||
break;
|
||||
case T_PlaceHolderVar:
|
||||
{
|
||||
PlaceHolderVar *phv = (PlaceHolderVar *) node;
|
||||
PlaceHolderVar *newnode;
|
||||
|
||||
FLATCOPY(newnode, phv, PlaceHolderVar);
|
||||
MUTATE(newnode->phexpr, phv->phexpr, Expr *);
|
||||
/* Assume we need not copy the relids bitmapset */
|
||||
return (Node *) newnode;
|
||||
}
|
||||
break;
|
||||
case T_AppendRelInfo:
|
||||
{
|
||||
AppendRelInfo *appinfo = (AppendRelInfo *) node;
|
||||
@ -1928,6 +1951,17 @@ expression_tree_mutator(Node *node,
|
||||
return (Node *) newnode;
|
||||
}
|
||||
break;
|
||||
case T_PlaceHolderInfo:
|
||||
{
|
||||
PlaceHolderInfo *phinfo = (PlaceHolderInfo *) node;
|
||||
PlaceHolderInfo *newnode;
|
||||
|
||||
FLATCOPY(newnode, phinfo, PlaceHolderInfo);
|
||||
MUTATE(newnode->ph_var, phinfo->ph_var, PlaceHolderVar *);
|
||||
/* Assume we need not copy the relids bitmapsets */
|
||||
return (Node *) newnode;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
elog(ERROR, "unrecognized node type: %d",
|
||||
(int) nodeTag(node));
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user