mirror of
https://github.com/postgres/postgres.git
synced 2025-10-28 11:55:03 +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:
@@ -54,7 +54,7 @@
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/path/costsize.c,v 1.199 2008/10/17 20:27:24 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/path/costsize.c,v 1.200 2008/10/21 20:42:52 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -69,6 +69,7 @@
|
||||
#include "optimizer/clauses.h"
|
||||
#include "optimizer/cost.h"
|
||||
#include "optimizer/pathnode.h"
|
||||
#include "optimizer/placeholder.h"
|
||||
#include "optimizer/planmain.h"
|
||||
#include "parser/parsetree.h"
|
||||
#include "utils/lsyscache.h"
|
||||
@@ -2610,55 +2611,66 @@ set_rel_width(PlannerInfo *root, RelOptInfo *rel)
|
||||
{
|
||||
Oid reloid = planner_rt_fetch(rel->relid, root)->relid;
|
||||
int32 tuple_width = 0;
|
||||
ListCell *tllist;
|
||||
ListCell *lc;
|
||||
|
||||
foreach(tllist, rel->reltargetlist)
|
||||
foreach(lc, rel->reltargetlist)
|
||||
{
|
||||
Var *var = (Var *) lfirst(tllist);
|
||||
int ndx;
|
||||
int32 item_width;
|
||||
Node *node = (Node *) lfirst(lc);
|
||||
|
||||
/* For now, punt on whole-row child Vars */
|
||||
if (!IsA(var, Var))
|
||||
if (IsA(node, Var))
|
||||
{
|
||||
tuple_width += 32; /* arbitrary */
|
||||
continue;
|
||||
}
|
||||
Var *var = (Var *) node;
|
||||
int ndx;
|
||||
int32 item_width;
|
||||
|
||||
Assert(var->varno == rel->relid);
|
||||
Assert(var->varattno >= rel->min_attr);
|
||||
Assert(var->varattno <= rel->max_attr);
|
||||
Assert(var->varno == rel->relid);
|
||||
Assert(var->varattno >= rel->min_attr);
|
||||
Assert(var->varattno <= rel->max_attr);
|
||||
|
||||
ndx = var->varattno - rel->min_attr;
|
||||
ndx = var->varattno - rel->min_attr;
|
||||
|
||||
/*
|
||||
* The width probably hasn't been cached yet, but may as well check
|
||||
*/
|
||||
if (rel->attr_widths[ndx] > 0)
|
||||
{
|
||||
tuple_width += rel->attr_widths[ndx];
|
||||
continue;
|
||||
}
|
||||
|
||||
if (reloid != InvalidOid)
|
||||
{
|
||||
item_width = get_attavgwidth(reloid, var->varattno);
|
||||
if (item_width > 0)
|
||||
/*
|
||||
* The width probably hasn't been cached yet, but may as well check
|
||||
*/
|
||||
if (rel->attr_widths[ndx] > 0)
|
||||
{
|
||||
rel->attr_widths[ndx] = item_width;
|
||||
tuple_width += item_width;
|
||||
tuple_width += rel->attr_widths[ndx];
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Not a plain relation, or can't find statistics for it. Estimate
|
||||
* using just the type info.
|
||||
*/
|
||||
item_width = get_typavgwidth(var->vartype, var->vartypmod);
|
||||
Assert(item_width > 0);
|
||||
rel->attr_widths[ndx] = item_width;
|
||||
tuple_width += item_width;
|
||||
/* Try to get column width from statistics */
|
||||
if (reloid != InvalidOid)
|
||||
{
|
||||
item_width = get_attavgwidth(reloid, var->varattno);
|
||||
if (item_width > 0)
|
||||
{
|
||||
rel->attr_widths[ndx] = item_width;
|
||||
tuple_width += item_width;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Not a plain relation, or can't find statistics for it. Estimate
|
||||
* using just the type info.
|
||||
*/
|
||||
item_width = get_typavgwidth(var->vartype, var->vartypmod);
|
||||
Assert(item_width > 0);
|
||||
rel->attr_widths[ndx] = item_width;
|
||||
tuple_width += item_width;
|
||||
}
|
||||
else if (IsA(node, PlaceHolderVar))
|
||||
{
|
||||
PlaceHolderVar *phv = (PlaceHolderVar *) node;
|
||||
PlaceHolderInfo *phinfo = find_placeholder_info(root, phv);
|
||||
|
||||
tuple_width += phinfo->ph_width;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* For now, punt on whole-row child Vars */
|
||||
tuple_width += 32; /* arbitrary */
|
||||
}
|
||||
}
|
||||
Assert(tuple_width >= 0);
|
||||
rel->width = tuple_width;
|
||||
|
||||
Reference in New Issue
Block a user