1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* initsplan.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* Target list, qualification, joininfo initialization routines
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2006-03-05 16:59:11 +01:00
|
|
|
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2006-09-20 00:49:53 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/optimizer/plan/initsplan.c,v 1.122 2006/09/19 22:49:52 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2001-05-07 02:43:27 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2000-07-24 05:11:01 +02:00
|
|
|
#include "catalog/pg_operator.h"
|
1998-08-10 04:26:40 +02:00
|
|
|
#include "catalog/pg_type.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "optimizer/clauses.h"
|
|
|
|
#include "optimizer/cost.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "optimizer/joininfo.h"
|
|
|
|
#include "optimizer/pathnode.h"
|
2000-02-15 21:49:31 +01:00
|
|
|
#include "optimizer/paths.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "optimizer/planmain.h"
|
Revise the planner's handling of "pseudoconstant" WHERE clauses, that is
clauses containing no variables and no volatile functions. Such a clause
can be used as a one-time qual in a gating Result plan node, to suppress
plan execution entirely when it is false. Even when the clause is true,
putting it in a gating node wins by avoiding repeated evaluation of the
clause. In previous PG releases, query_planner() would do this for
pseudoconstant clauses appearing at the top level of the jointree, but
there was no ability to generate a gating Result deeper in the plan tree.
To fix it, get rid of the special case in query_planner(), and instead
process pseudoconstant clauses through the normal RestrictInfo qual
distribution mechanism. When a pseudoconstant clause is found attached to
a path node in create_plan(), pull it out and generate a gating Result at
that point. This requires special-casing pseudoconstants in selectivity
estimation and cost_qual_eval, but on the whole it's pretty clean.
It probably even makes the planner a bit faster than before for the normal
case of no pseudoconstants, since removing pull_constant_clauses saves one
useless traversal of the qual tree. Per gripe from Phil Frost.
2006-07-01 20:38:33 +02:00
|
|
|
#include "optimizer/prep.h"
|
2004-01-04 01:07:32 +01:00
|
|
|
#include "optimizer/restrictinfo.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "optimizer/var.h"
|
2000-07-24 05:11:01 +02:00
|
|
|
#include "parser/parse_expr.h"
|
|
|
|
#include "parser/parse_oper.h"
|
2002-05-18 00:35:13 +02:00
|
|
|
#include "utils/builtins.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "utils/lsyscache.h"
|
2000-11-16 23:30:52 +01:00
|
|
|
#include "utils/syscache.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
2005-12-20 03:30:36 +01:00
|
|
|
/* These parameters are set by GUC */
|
|
|
|
int from_collapse_limit;
|
|
|
|
int join_collapse_limit;
|
|
|
|
|
|
|
|
|
|
|
|
static void add_vars_to_targetlist(PlannerInfo *root, List *vars,
|
|
|
|
Relids where_needed);
|
|
|
|
static List *deconstruct_recurse(PlannerInfo *root, Node *jtnode,
|
|
|
|
bool below_outer_join, Relids *qualscope);
|
|
|
|
static OuterJoinInfo *make_outerjoininfo(PlannerInfo *root,
|
|
|
|
Relids left_rels, Relids right_rels,
|
|
|
|
bool is_full_join, Node *clause);
|
2005-06-06 00:32:58 +02:00
|
|
|
static void distribute_qual_to_rels(PlannerInfo *root, Node *clause,
|
2004-01-05 06:07:36 +01:00
|
|
|
bool is_pushed_down,
|
2005-09-28 23:17:02 +02:00
|
|
|
bool is_deduced,
|
|
|
|
bool below_outer_join,
|
2005-12-20 03:30:36 +01:00
|
|
|
Relids qualscope,
|
|
|
|
Relids ojscope,
|
|
|
|
Relids outerjoin_nonnullable);
|
2005-06-06 00:32:58 +02:00
|
|
|
static bool qual_is_redundant(PlannerInfo *root, RestrictInfo *restrictinfo,
|
2001-10-25 07:50:21 +02:00
|
|
|
List *restrictlist);
|
1999-08-16 04:17:58 +02:00
|
|
|
static void check_mergejoinable(RestrictInfo *restrictinfo);
|
|
|
|
static void check_hashjoinable(RestrictInfo *restrictinfo);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
2002-03-12 01:52:10 +01:00
|
|
|
* JOIN TREES
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2002-03-12 01:52:10 +01:00
|
|
|
* add_base_rels_to_query
|
|
|
|
*
|
|
|
|
* Scan the query's jointree and create baserel RelOptInfos for all
|
2003-01-15 20:35:48 +01:00
|
|
|
* the base relations (ie, table, subquery, and function RTEs)
|
|
|
|
* appearing in the jointree.
|
1999-10-07 06:23:24 +02:00
|
|
|
*
|
Revise the planner's handling of "pseudoconstant" WHERE clauses, that is
clauses containing no variables and no volatile functions. Such a clause
can be used as a one-time qual in a gating Result plan node, to suppress
plan execution entirely when it is false. Even when the clause is true,
putting it in a gating node wins by avoiding repeated evaluation of the
clause. In previous PG releases, query_planner() would do this for
pseudoconstant clauses appearing at the top level of the jointree, but
there was no ability to generate a gating Result deeper in the plan tree.
To fix it, get rid of the special case in query_planner(), and instead
process pseudoconstant clauses through the normal RestrictInfo qual
distribution mechanism. When a pseudoconstant clause is found attached to
a path node in create_plan(), pull it out and generate a gating Result at
that point. This requires special-casing pseudoconstants in selectivity
estimation and cost_qual_eval, but on the whole it's pretty clean.
It probably even makes the planner a bit faster than before for the normal
case of no pseudoconstants, since removing pull_constant_clauses saves one
useless traversal of the qual tree. Per gripe from Phil Frost.
2006-07-01 20:38:33 +02:00
|
|
|
* The initial invocation must pass root->parse->jointree as the value of
|
|
|
|
* jtnode. Internally, the function recurses through the jointree.
|
|
|
|
*
|
2002-03-12 01:52:10 +01:00
|
|
|
* At the end of this process, there should be one baserel RelOptInfo for
|
|
|
|
* every non-join RTE that is used in the query. Therefore, this routine
|
2006-01-31 22:39:25 +01:00
|
|
|
* is the only place that should call build_simple_rel with reloptkind
|
2006-09-20 00:49:53 +02:00
|
|
|
* RELOPT_BASEREL. (Note: build_simple_rel recurses internally to build
|
|
|
|
* "other rel" RelOptInfos for the members of any appendrels we find here.)
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2003-01-15 20:35:48 +01:00
|
|
|
void
|
2005-06-06 00:32:58 +02:00
|
|
|
add_base_rels_to_query(PlannerInfo *root, Node *jtnode)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-09-12 23:07:18 +02:00
|
|
|
if (jtnode == NULL)
|
2003-01-15 20:35:48 +01:00
|
|
|
return;
|
2000-09-29 20:21:41 +02:00
|
|
|
if (IsA(jtnode, RangeTblRef))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-09-29 20:21:41 +02:00
|
|
|
int varno = ((RangeTblRef *) jtnode)->rtindex;
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2006-01-31 22:39:25 +01:00
|
|
|
(void) build_simple_rel(root, varno, RELOPT_BASEREL);
|
2000-09-12 23:07:18 +02:00
|
|
|
}
|
2000-09-29 20:21:41 +02:00
|
|
|
else if (IsA(jtnode, FromExpr))
|
2000-09-12 23:07:18 +02:00
|
|
|
{
|
2000-09-29 20:21:41 +02:00
|
|
|
FromExpr *f = (FromExpr *) jtnode;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *l;
|
1999-08-10 05:00:15 +02:00
|
|
|
|
2000-09-29 20:21:41 +02:00
|
|
|
foreach(l, f->fromlist)
|
2003-01-15 20:35:48 +01:00
|
|
|
add_base_rels_to_query(root, lfirst(l));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2000-09-12 23:07:18 +02:00
|
|
|
else if (IsA(jtnode, JoinExpr))
|
|
|
|
{
|
|
|
|
JoinExpr *j = (JoinExpr *) jtnode;
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2003-01-15 20:35:48 +01:00
|
|
|
add_base_rels_to_query(root, j->larg);
|
|
|
|
add_base_rels_to_query(root, j->rarg);
|
2000-09-12 23:07:18 +02:00
|
|
|
}
|
|
|
|
else
|
2003-07-25 02:01:09 +02:00
|
|
|
elog(ERROR, "unrecognized node type: %d",
|
|
|
|
(int) nodeTag(jtnode));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-07-24 05:11:01 +02:00
|
|
|
|
2002-03-12 01:52:10 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* TARGET LISTS
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* build_base_rel_tlists
|
2003-06-30 01:05:05 +02:00
|
|
|
* Add targetlist entries for each var needed in the query's final tlist
|
|
|
|
* to the appropriate base relations.
|
|
|
|
*
|
|
|
|
* We mark such vars as needed by "relation 0" to ensure that they will
|
|
|
|
* propagate up through all join plan steps.
|
2002-03-12 01:52:10 +01:00
|
|
|
*/
|
|
|
|
void
|
2005-06-06 00:32:58 +02:00
|
|
|
build_base_rel_tlists(PlannerInfo *root, List *final_tlist)
|
2002-03-12 01:52:10 +01:00
|
|
|
{
|
2003-06-30 01:05:05 +02:00
|
|
|
List *tlist_vars = pull_var_clause((Node *) final_tlist, false);
|
2002-03-12 01:52:10 +01:00
|
|
|
|
2003-06-30 01:05:05 +02:00
|
|
|
if (tlist_vars != NIL)
|
|
|
|
{
|
|
|
|
add_vars_to_targetlist(root, tlist_vars, bms_make_singleton(0));
|
2004-05-31 01:40:41 +02:00
|
|
|
list_free(tlist_vars);
|
2003-06-30 01:05:05 +02:00
|
|
|
}
|
2002-03-12 01:52:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* add_vars_to_targetlist
|
|
|
|
* For each variable appearing in the list, add it to the owning
|
2003-06-30 01:05:05 +02:00
|
|
|
* relation's targetlist if not already present, and mark the variable
|
|
|
|
* as being needed for the indicated join (or for final output if
|
|
|
|
* where_needed includes "relation 0").
|
2002-03-12 01:52:10 +01:00
|
|
|
*/
|
|
|
|
static void
|
2005-06-06 00:32:58 +02:00
|
|
|
add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed)
|
2002-03-12 01:52:10 +01:00
|
|
|
{
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *temp;
|
2002-03-12 01:52:10 +01:00
|
|
|
|
2003-06-30 01:05:05 +02:00
|
|
|
Assert(!bms_is_empty(where_needed));
|
|
|
|
|
2002-03-12 01:52:10 +01:00
|
|
|
foreach(temp, vars)
|
|
|
|
{
|
|
|
|
Var *var = (Var *) lfirst(temp);
|
|
|
|
RelOptInfo *rel = find_base_rel(root, var->varno);
|
2003-06-30 01:05:05 +02:00
|
|
|
int attrno = var->varattno;
|
2002-03-12 01:52:10 +01:00
|
|
|
|
2003-06-30 01:05:05 +02:00
|
|
|
Assert(attrno >= rel->min_attr && attrno <= rel->max_attr);
|
|
|
|
attrno -= rel->min_attr;
|
|
|
|
if (bms_is_empty(rel->attr_needed[attrno]))
|
|
|
|
{
|
|
|
|
/* Variable not yet requested, so add to reltargetlist */
|
|
|
|
/* XXX is copyObject necessary here? */
|
2004-06-01 05:03:05 +02:00
|
|
|
rel->reltargetlist = lappend(rel->reltargetlist, copyObject(var));
|
2003-06-30 01:05:05 +02:00
|
|
|
}
|
|
|
|
rel->attr_needed[attrno] = bms_add_members(rel->attr_needed[attrno],
|
|
|
|
where_needed);
|
2002-03-12 01:52:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
2005-12-20 03:30:36 +01:00
|
|
|
* JOIN TREE PROCESSING
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2000-09-12 23:07:18 +02:00
|
|
|
/*
|
2005-12-20 03:30:36 +01:00
|
|
|
* deconstruct_jointree
|
2000-09-29 20:21:41 +02:00
|
|
|
* Recursively scan the query's join tree for WHERE and JOIN/ON qual
|
2005-06-09 06:19:00 +02:00
|
|
|
* clauses, and add these to the appropriate restrictinfo and joininfo
|
2005-12-20 03:30:36 +01:00
|
|
|
* lists belonging to base RelOptInfos. Also, add OuterJoinInfo nodes
|
|
|
|
* to root->oj_info_list for any outer joins appearing in the query tree.
|
|
|
|
* Return a "joinlist" data structure showing the join order decisions
|
|
|
|
* that need to be made by make_one_rel().
|
2000-09-12 23:07:18 +02:00
|
|
|
*
|
2005-12-20 03:30:36 +01:00
|
|
|
* The "joinlist" result is a list of items that are either RangeTblRef
|
|
|
|
* jointree nodes or sub-joinlists. All the items at the same level of
|
|
|
|
* joinlist must be joined in an order to be determined by make_one_rel()
|
|
|
|
* (note that legal orders may be constrained by OuterJoinInfo nodes).
|
|
|
|
* A sub-joinlist represents a subproblem to be planned separately. Currently
|
|
|
|
* sub-joinlists arise only from FULL OUTER JOIN or when collapsing of
|
|
|
|
* subproblems is stopped by join_collapse_limit or from_collapse_limit.
|
2005-09-28 23:17:02 +02:00
|
|
|
*
|
2000-09-12 23:07:18 +02:00
|
|
|
* NOTE: when dealing with inner joins, it is appropriate to let a qual clause
|
|
|
|
* be evaluated at the lowest level where all the variables it mentions are
|
2000-09-29 20:21:41 +02:00
|
|
|
* available. However, we cannot push a qual down into the nullable side(s)
|
|
|
|
* of an outer join since the qual might eliminate matching rows and cause a
|
2005-12-20 03:30:36 +01:00
|
|
|
* NULL row to be incorrectly emitted by the join. Therefore, we artificially
|
|
|
|
* OR the minimum-relids of such an outer join into the required_relids of
|
|
|
|
* clauses appearing above it. This forces those clauses to be delayed until
|
|
|
|
* application of the outer join (or maybe even higher in the join tree).
|
|
|
|
*/
|
|
|
|
List *
|
|
|
|
deconstruct_jointree(PlannerInfo *root)
|
|
|
|
{
|
|
|
|
Relids qualscope;
|
|
|
|
|
|
|
|
/* Start recursion at top of jointree */
|
|
|
|
Assert(root->parse->jointree != NULL &&
|
|
|
|
IsA(root->parse->jointree, FromExpr));
|
|
|
|
|
|
|
|
return deconstruct_recurse(root, (Node *) root->parse->jointree, false,
|
|
|
|
&qualscope);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* deconstruct_recurse
|
|
|
|
* One recursion level of deconstruct_jointree processing.
|
2000-09-12 23:07:18 +02:00
|
|
|
*
|
2005-12-20 03:30:36 +01:00
|
|
|
* Inputs:
|
|
|
|
* jtnode is the jointree node to examine
|
|
|
|
* below_outer_join is TRUE if this node is within the nullable side of a
|
|
|
|
* higher-level outer join
|
|
|
|
* Outputs:
|
|
|
|
* *qualscope gets the set of base Relids syntactically included in this
|
|
|
|
* jointree node (do not modify or free this, as it may also be pointed
|
|
|
|
* to by RestrictInfo nodes)
|
|
|
|
* Return value is the appropriate joinlist for this jointree node
|
|
|
|
*
|
|
|
|
* In addition, entries will be added to root->oj_info_list for outer joins.
|
2000-09-12 23:07:18 +02:00
|
|
|
*/
|
2005-12-20 03:30:36 +01:00
|
|
|
static List *
|
|
|
|
deconstruct_recurse(PlannerInfo *root, Node *jtnode, bool below_outer_join,
|
|
|
|
Relids *qualscope)
|
2000-09-12 23:07:18 +02:00
|
|
|
{
|
2005-12-20 03:30:36 +01:00
|
|
|
List *joinlist;
|
2000-09-12 23:07:18 +02:00
|
|
|
|
|
|
|
if (jtnode == NULL)
|
2005-12-20 03:30:36 +01:00
|
|
|
{
|
|
|
|
*qualscope = NULL;
|
|
|
|
return NIL;
|
|
|
|
}
|
2000-09-29 20:21:41 +02:00
|
|
|
if (IsA(jtnode, RangeTblRef))
|
2000-09-12 23:07:18 +02:00
|
|
|
{
|
2000-09-29 20:21:41 +02:00
|
|
|
int varno = ((RangeTblRef *) jtnode)->rtindex;
|
|
|
|
|
|
|
|
/* No quals to deal with, just return correct result */
|
2005-12-20 03:30:36 +01:00
|
|
|
*qualscope = bms_make_singleton(varno);
|
|
|
|
joinlist = list_make1(jtnode);
|
2000-09-29 20:21:41 +02:00
|
|
|
}
|
|
|
|
else if (IsA(jtnode, FromExpr))
|
|
|
|
{
|
|
|
|
FromExpr *f = (FromExpr *) jtnode;
|
2005-12-20 03:30:36 +01:00
|
|
|
int remaining;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *l;
|
2000-09-12 23:07:18 +02:00
|
|
|
|
|
|
|
/*
|
2005-12-20 03:30:36 +01:00
|
|
|
* First, recurse to handle child joins. We collapse subproblems
|
|
|
|
* into a single joinlist whenever the resulting joinlist wouldn't
|
|
|
|
* exceed from_collapse_limit members. Also, always collapse
|
|
|
|
* one-element subproblems, since that won't lengthen the joinlist
|
|
|
|
* anyway.
|
2000-09-12 23:07:18 +02:00
|
|
|
*/
|
2005-12-20 03:30:36 +01:00
|
|
|
*qualscope = NULL;
|
|
|
|
joinlist = NIL;
|
|
|
|
remaining = list_length(f->fromlist);
|
2000-09-29 20:21:41 +02:00
|
|
|
foreach(l, f->fromlist)
|
|
|
|
{
|
2005-12-20 03:30:36 +01:00
|
|
|
Relids sub_qualscope;
|
|
|
|
List *sub_joinlist;
|
|
|
|
int sub_members;
|
|
|
|
|
|
|
|
sub_joinlist = deconstruct_recurse(root, lfirst(l),
|
|
|
|
below_outer_join,
|
|
|
|
&sub_qualscope);
|
|
|
|
*qualscope = bms_add_members(*qualscope, sub_qualscope);
|
|
|
|
sub_members = list_length(sub_joinlist);
|
|
|
|
remaining--;
|
|
|
|
if (sub_members <= 1 ||
|
|
|
|
list_length(joinlist) + sub_members + remaining <= from_collapse_limit)
|
|
|
|
joinlist = list_concat(joinlist, sub_joinlist);
|
|
|
|
else
|
|
|
|
joinlist = lappend(joinlist, sub_joinlist);
|
2000-09-29 20:21:41 +02:00
|
|
|
}
|
2000-09-12 23:07:18 +02:00
|
|
|
|
2000-09-29 20:21:41 +02:00
|
|
|
/*
|
|
|
|
* Now process the top-level quals. These are always marked as
|
|
|
|
* "pushed down", since they clearly didn't come from a JOIN expr.
|
|
|
|
*/
|
2004-05-26 06:41:50 +02:00
|
|
|
foreach(l, (List *) f->quals)
|
|
|
|
distribute_qual_to_rels(root, (Node *) lfirst(l),
|
2005-09-28 23:17:02 +02:00
|
|
|
true, false, below_outer_join,
|
2005-12-20 03:30:36 +01:00
|
|
|
*qualscope, NULL, NULL);
|
2000-09-12 23:07:18 +02:00
|
|
|
}
|
|
|
|
else if (IsA(jtnode, JoinExpr))
|
|
|
|
{
|
|
|
|
JoinExpr *j = (JoinExpr *) jtnode;
|
|
|
|
Relids leftids,
|
2003-03-03 00:46:34 +01:00
|
|
|
rightids,
|
|
|
|
nonnullable_rels,
|
2005-12-20 03:30:36 +01:00
|
|
|
ojscope;
|
|
|
|
List *leftjoinlist,
|
|
|
|
*rightjoinlist;
|
|
|
|
OuterJoinInfo *ojinfo;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *qual;
|
2000-09-12 23:07:18 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Order of operations here is subtle and critical. First we recurse
|
|
|
|
* to handle sub-JOINs. Their join quals will be placed without
|
|
|
|
* regard for whether this level is an outer join, which is correct.
|
|
|
|
* Then we place our own join quals, which are restricted by lower
|
|
|
|
* outer joins in any case, and are forced to this level if this is an
|
|
|
|
* outer join and they mention the outer side. Finally, if this is an
|
2005-12-20 03:30:36 +01:00
|
|
|
* outer join, we create an oj_info_list entry for the join. This
|
|
|
|
* will prevent quals above us in the join tree that use those rels
|
|
|
|
* from being pushed down below this level. (It's okay for upper
|
|
|
|
* quals to be pushed down to the outer side, however.)
|
2000-09-12 23:07:18 +02:00
|
|
|
*/
|
|
|
|
switch (j->jointype)
|
|
|
|
{
|
|
|
|
case JOIN_INNER:
|
2005-12-20 03:30:36 +01:00
|
|
|
leftjoinlist = deconstruct_recurse(root, j->larg,
|
|
|
|
below_outer_join,
|
|
|
|
&leftids);
|
|
|
|
rightjoinlist = deconstruct_recurse(root, j->rarg,
|
|
|
|
below_outer_join,
|
|
|
|
&rightids);
|
|
|
|
*qualscope = bms_union(leftids, rightids);
|
2000-09-12 23:07:18 +02:00
|
|
|
/* Inner join adds no restrictions for quals */
|
2005-09-28 23:17:02 +02:00
|
|
|
nonnullable_rels = NULL;
|
2000-09-12 23:07:18 +02:00
|
|
|
break;
|
|
|
|
case JOIN_LEFT:
|
2005-12-20 03:30:36 +01:00
|
|
|
leftjoinlist = deconstruct_recurse(root, j->larg,
|
|
|
|
below_outer_join,
|
|
|
|
&leftids);
|
|
|
|
rightjoinlist = deconstruct_recurse(root, j->rarg,
|
|
|
|
true,
|
|
|
|
&rightids);
|
|
|
|
*qualscope = bms_union(leftids, rightids);
|
2003-03-03 00:46:34 +01:00
|
|
|
nonnullable_rels = leftids;
|
2000-09-12 23:07:18 +02:00
|
|
|
break;
|
|
|
|
case JOIN_FULL:
|
2005-12-20 03:30:36 +01:00
|
|
|
leftjoinlist = deconstruct_recurse(root, j->larg,
|
|
|
|
true,
|
|
|
|
&leftids);
|
|
|
|
rightjoinlist = deconstruct_recurse(root, j->rarg,
|
|
|
|
true,
|
|
|
|
&rightids);
|
|
|
|
*qualscope = bms_union(leftids, rightids);
|
2003-03-03 00:46:34 +01:00
|
|
|
/* each side is both outer and inner */
|
2005-12-20 03:30:36 +01:00
|
|
|
nonnullable_rels = *qualscope;
|
2000-09-12 23:07:18 +02:00
|
|
|
break;
|
|
|
|
case JOIN_RIGHT:
|
2005-12-20 03:30:36 +01:00
|
|
|
/* notice we switch leftids and rightids */
|
|
|
|
leftjoinlist = deconstruct_recurse(root, j->larg,
|
|
|
|
true,
|
|
|
|
&rightids);
|
|
|
|
rightjoinlist = deconstruct_recurse(root, j->rarg,
|
|
|
|
below_outer_join,
|
|
|
|
&leftids);
|
|
|
|
*qualscope = bms_union(leftids, rightids);
|
|
|
|
nonnullable_rels = leftids;
|
2000-09-12 23:07:18 +02:00
|
|
|
break;
|
|
|
|
default:
|
2003-07-25 02:01:09 +02:00
|
|
|
elog(ERROR, "unrecognized join type: %d",
|
2000-09-12 23:07:18 +02:00
|
|
|
(int) j->jointype);
|
2005-10-15 04:49:52 +02:00
|
|
|
nonnullable_rels = NULL; /* keep compiler quiet */
|
2005-12-20 03:30:36 +01:00
|
|
|
leftjoinlist = rightjoinlist = NIL;
|
2000-09-12 23:07:18 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-12-20 03:30:36 +01:00
|
|
|
/*
|
|
|
|
* For an OJ, form the OuterJoinInfo now, because we need the OJ's
|
|
|
|
* semantic scope (ojscope) to pass to distribute_qual_to_rels.
|
|
|
|
*/
|
|
|
|
if (j->jointype != JOIN_INNER)
|
|
|
|
{
|
|
|
|
ojinfo = make_outerjoininfo(root, leftids, rightids,
|
|
|
|
(j->jointype == JOIN_FULL), j->quals);
|
|
|
|
ojscope = bms_union(ojinfo->min_lefthand, ojinfo->min_righthand);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ojinfo = NULL;
|
|
|
|
ojscope = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process the qual clauses */
|
2000-09-12 23:07:18 +02:00
|
|
|
foreach(qual, (List *) j->quals)
|
2000-09-29 20:21:41 +02:00
|
|
|
distribute_qual_to_rels(root, (Node *) lfirst(qual),
|
2005-09-28 23:17:02 +02:00
|
|
|
false, false, below_outer_join,
|
2005-12-20 03:30:36 +01:00
|
|
|
*qualscope, ojscope, nonnullable_rels);
|
|
|
|
|
|
|
|
/* Now we can add the OuterJoinInfo to oj_info_list */
|
|
|
|
if (ojinfo)
|
|
|
|
root->oj_info_list = lappend(root->oj_info_list, ojinfo);
|
2003-03-03 00:46:34 +01:00
|
|
|
|
2005-12-20 03:30:36 +01:00
|
|
|
/*
|
|
|
|
* Finally, compute the output joinlist. We fold subproblems together
|
|
|
|
* except at a FULL JOIN or where join_collapse_limit would be
|
|
|
|
* exceeded.
|
|
|
|
*/
|
|
|
|
if (j->jointype != JOIN_FULL &&
|
|
|
|
(list_length(leftjoinlist) + list_length(rightjoinlist) <=
|
|
|
|
join_collapse_limit))
|
|
|
|
joinlist = list_concat(leftjoinlist, rightjoinlist);
|
|
|
|
else /* force the join order at this node */
|
|
|
|
joinlist = list_make1(list_make2(leftjoinlist, rightjoinlist));
|
2000-09-12 23:07:18 +02:00
|
|
|
}
|
|
|
|
else
|
2005-12-20 03:30:36 +01:00
|
|
|
{
|
2003-07-25 02:01:09 +02:00
|
|
|
elog(ERROR, "unrecognized node type: %d",
|
|
|
|
(int) nodeTag(jtnode));
|
2005-12-20 03:30:36 +01:00
|
|
|
joinlist = NIL; /* keep compiler quiet */
|
|
|
|
}
|
|
|
|
return joinlist;
|
2000-09-12 23:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-12-20 03:30:36 +01:00
|
|
|
* make_outerjoininfo
|
|
|
|
* Build an OuterJoinInfo for the current outer join
|
|
|
|
*
|
|
|
|
* Inputs:
|
|
|
|
* left_rels: the base Relids syntactically on outer side of join
|
|
|
|
* right_rels: the base Relids syntactically on inner side of join
|
|
|
|
* is_full_join: what it says
|
|
|
|
* clause: the outer join's join condition
|
|
|
|
*
|
|
|
|
* If the join is a RIGHT JOIN, left_rels and right_rels are switched by
|
|
|
|
* the caller, so that left_rels is always the nonnullable side. Hence
|
|
|
|
* we need only distinguish the LEFT and FULL cases.
|
|
|
|
*
|
|
|
|
* The node should eventually be put into root->oj_info_list, but we
|
|
|
|
* do not do that here.
|
2000-09-12 23:07:18 +02:00
|
|
|
*/
|
2005-12-20 03:30:36 +01:00
|
|
|
static OuterJoinInfo *
|
|
|
|
make_outerjoininfo(PlannerInfo *root,
|
|
|
|
Relids left_rels, Relids right_rels,
|
|
|
|
bool is_full_join, Node *clause)
|
2000-09-12 23:07:18 +02:00
|
|
|
{
|
2005-12-20 03:30:36 +01:00
|
|
|
OuterJoinInfo *ojinfo = makeNode(OuterJoinInfo);
|
|
|
|
Relids clause_relids;
|
|
|
|
Relids strict_relids;
|
|
|
|
ListCell *l;
|
|
|
|
|
2006-09-08 19:49:13 +02:00
|
|
|
/*
|
|
|
|
* Presently the executor cannot support FOR UPDATE/SHARE marking of rels
|
|
|
|
* appearing on the nullable side of an outer join. (It's somewhat unclear
|
|
|
|
* what that would mean, anyway: what should we mark when a result row is
|
|
|
|
* generated from no element of the nullable relation?) So, complain if
|
|
|
|
* any nullable rel is FOR UPDATE/SHARE.
|
|
|
|
*
|
|
|
|
* You might be wondering why this test isn't made far upstream in the
|
|
|
|
* parser. It's because the parser hasn't got enough info --- consider
|
|
|
|
* FOR UPDATE applied to a view. Only after rewriting and flattening
|
|
|
|
* do we know whether the view contains an outer join.
|
|
|
|
*/
|
|
|
|
foreach(l, root->parse->rowMarks)
|
|
|
|
{
|
|
|
|
RowMarkClause *rc = (RowMarkClause *) lfirst(l);
|
|
|
|
|
|
|
|
if (bms_is_member(rc->rti, right_rels) ||
|
|
|
|
(is_full_join && bms_is_member(rc->rti, left_rels)))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("SELECT FOR UPDATE/SHARE cannot be applied to the nullable side of an outer join")));
|
|
|
|
}
|
|
|
|
|
2005-12-20 03:30:36 +01:00
|
|
|
/* If it's a full join, no need to be very smart */
|
|
|
|
ojinfo->is_full_join = is_full_join;
|
|
|
|
if (is_full_join)
|
|
|
|
{
|
|
|
|
ojinfo->min_lefthand = left_rels;
|
|
|
|
ojinfo->min_righthand = right_rels;
|
|
|
|
ojinfo->lhs_strict = false; /* don't care about this */
|
|
|
|
return ojinfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieve all relids mentioned within the join clause.
|
|
|
|
*/
|
|
|
|
clause_relids = pull_varnos(clause);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For which relids is the clause strict, ie, it cannot succeed if the
|
|
|
|
* rel's columns are all NULL?
|
|
|
|
*/
|
|
|
|
strict_relids = find_nonnullable_rels(clause);
|
2000-09-12 23:07:18 +02:00
|
|
|
|
2005-12-20 03:30:36 +01:00
|
|
|
/* Remember whether the clause is strict for any LHS relations */
|
|
|
|
ojinfo->lhs_strict = bms_overlap(strict_relids, left_rels);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Required LHS is basically the LHS rels mentioned in the clause...
|
|
|
|
* but if there aren't any, punt and make it the full LHS, to avoid
|
|
|
|
* having an empty min_lefthand which will confuse later processing.
|
|
|
|
* (We don't try to be smart about such cases, just correct.)
|
|
|
|
* We may have to add more rels based on lower outer joins; see below.
|
|
|
|
*/
|
|
|
|
ojinfo->min_lefthand = bms_intersect(clause_relids, left_rels);
|
|
|
|
if (bms_is_empty(ojinfo->min_lefthand))
|
|
|
|
ojinfo->min_lefthand = bms_copy(left_rels);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Required RHS is normally the full set of RHS rels. Sometimes we
|
|
|
|
* can exclude some, see below.
|
|
|
|
*/
|
|
|
|
ojinfo->min_righthand = bms_copy(right_rels);
|
|
|
|
|
|
|
|
foreach(l, root->oj_info_list)
|
2000-09-12 23:07:18 +02:00
|
|
|
{
|
2005-12-20 03:30:36 +01:00
|
|
|
OuterJoinInfo *otherinfo = (OuterJoinInfo *) lfirst(l);
|
|
|
|
|
|
|
|
/* ignore full joins --- other mechanisms preserve their ordering */
|
|
|
|
if (otherinfo->is_full_join)
|
|
|
|
continue;
|
2000-09-12 23:07:18 +02:00
|
|
|
|
|
|
|
/*
|
2005-12-20 03:30:36 +01:00
|
|
|
* For a lower OJ in our LHS, if our join condition uses the lower
|
|
|
|
* join's RHS and is not strict for that rel, we must preserve the
|
|
|
|
* ordering of the two OJs, so add lower OJ's full required relset to
|
|
|
|
* min_lefthand.
|
2000-09-12 23:07:18 +02:00
|
|
|
*/
|
2005-12-20 03:30:36 +01:00
|
|
|
if (bms_overlap(ojinfo->min_lefthand, otherinfo->min_righthand) &&
|
|
|
|
!bms_overlap(strict_relids, otherinfo->min_righthand))
|
|
|
|
{
|
|
|
|
ojinfo->min_lefthand = bms_add_members(ojinfo->min_lefthand,
|
|
|
|
otherinfo->min_lefthand);
|
|
|
|
ojinfo->min_lefthand = bms_add_members(ojinfo->min_lefthand,
|
|
|
|
otherinfo->min_righthand);
|
|
|
|
}
|
2001-05-14 22:25:00 +02:00
|
|
|
/*
|
2005-12-20 03:30:36 +01:00
|
|
|
* For a lower OJ in our RHS, if our join condition does not use the
|
|
|
|
* lower join's RHS and the lower OJ's join condition is strict, we
|
|
|
|
* can interchange the ordering of the two OJs, so exclude the lower
|
|
|
|
* RHS from our min_righthand.
|
2001-05-14 22:25:00 +02:00
|
|
|
*/
|
2005-12-20 03:30:36 +01:00
|
|
|
if (bms_overlap(ojinfo->min_righthand, otherinfo->min_righthand) &&
|
|
|
|
!bms_overlap(clause_relids, otherinfo->min_righthand) &&
|
|
|
|
otherinfo->lhs_strict)
|
2001-05-14 22:25:00 +02:00
|
|
|
{
|
2005-12-20 03:30:36 +01:00
|
|
|
ojinfo->min_righthand = bms_del_members(ojinfo->min_righthand,
|
|
|
|
otherinfo->min_righthand);
|
2001-05-14 22:25:00 +02:00
|
|
|
}
|
2000-09-12 23:07:18 +02:00
|
|
|
}
|
2005-12-20 03:30:36 +01:00
|
|
|
|
|
|
|
/* Neither set should be empty, else we might get confused later */
|
|
|
|
Assert(!bms_is_empty(ojinfo->min_lefthand));
|
|
|
|
Assert(!bms_is_empty(ojinfo->min_righthand));
|
|
|
|
/* Shouldn't overlap either */
|
|
|
|
Assert(!bms_overlap(ojinfo->min_lefthand, ojinfo->min_righthand));
|
|
|
|
|
|
|
|
return ojinfo;
|
2000-09-12 23:07:18 +02:00
|
|
|
}
|
|
|
|
|
2005-12-20 03:30:36 +01:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* QUALIFICATIONS
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2000-09-29 20:21:41 +02:00
|
|
|
* distribute_qual_to_rels
|
2005-06-09 06:19:00 +02:00
|
|
|
* Add clause information to either the baserestrictinfo or joininfo list
|
1999-08-16 04:17:58 +02:00
|
|
|
* (depending on whether the clause is a join) of each base relation
|
2000-04-12 19:17:23 +02:00
|
|
|
* mentioned in the clause. A RestrictInfo node is created and added to
|
2000-02-15 21:49:31 +01:00
|
|
|
* the appropriate list for each rel. Also, if the clause uses a
|
2003-03-03 00:46:34 +01:00
|
|
|
* mergejoinable operator and is not delayed by outer-join rules, enter
|
|
|
|
* the left- and right-side expressions into the query's lists of
|
|
|
|
* equijoined vars.
|
2000-09-12 23:07:18 +02:00
|
|
|
*
|
2000-09-29 20:21:41 +02:00
|
|
|
* 'clause': the qual clause to be distributed
|
2004-01-05 06:07:36 +01:00
|
|
|
* 'is_pushed_down': if TRUE, force the clause to be marked 'is_pushed_down'
|
2000-09-29 20:21:41 +02:00
|
|
|
* (this indicates the clause came from a FromExpr, not a JoinExpr)
|
2005-09-28 23:17:02 +02:00
|
|
|
* 'is_deduced': TRUE if the qual came from implied-equality deduction
|
|
|
|
* 'below_outer_join': TRUE if the qual is from a JOIN/ON that is below the
|
|
|
|
* nullable side of a higher-level outer join.
|
2005-12-20 03:30:36 +01:00
|
|
|
* 'qualscope': set of baserels the qual's syntactic scope covers
|
|
|
|
* 'ojscope': NULL if not an outer-join qual, else the minimum set of baserels
|
|
|
|
* needed to form this join
|
2003-03-03 00:46:34 +01:00
|
|
|
* 'outerjoin_nonnullable': NULL if not an outer-join qual, else the set of
|
|
|
|
* baserels appearing on the outer (nonnullable) side of the join
|
2005-12-20 03:30:36 +01:00
|
|
|
* (for FULL JOIN this includes both sides of the join, and must in fact
|
|
|
|
* equal qualscope)
|
2000-09-29 20:21:41 +02:00
|
|
|
*
|
2005-12-20 03:30:36 +01:00
|
|
|
* 'qualscope' identifies what level of JOIN the qual came from syntactically.
|
|
|
|
* 'ojscope' is needed if we decide to force the qual up to the outer-join
|
|
|
|
* level, which will be ojscope not necessarily qualscope.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
static void
|
2005-06-06 00:32:58 +02:00
|
|
|
distribute_qual_to_rels(PlannerInfo *root, Node *clause,
|
2004-01-05 06:07:36 +01:00
|
|
|
bool is_pushed_down,
|
2005-09-28 23:17:02 +02:00
|
|
|
bool is_deduced,
|
|
|
|
bool below_outer_join,
|
2005-12-20 03:30:36 +01:00
|
|
|
Relids qualscope,
|
|
|
|
Relids ojscope,
|
|
|
|
Relids outerjoin_nonnullable)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-02-18 01:49:48 +01:00
|
|
|
Relids relids;
|
2005-11-15 00:54:23 +01:00
|
|
|
bool outerjoin_delayed;
|
Revise the planner's handling of "pseudoconstant" WHERE clauses, that is
clauses containing no variables and no volatile functions. Such a clause
can be used as a one-time qual in a gating Result plan node, to suppress
plan execution entirely when it is false. Even when the clause is true,
putting it in a gating node wins by avoiding repeated evaluation of the
clause. In previous PG releases, query_planner() would do this for
pseudoconstant clauses appearing at the top level of the jointree, but
there was no ability to generate a gating Result deeper in the plan tree.
To fix it, get rid of the special case in query_planner(), and instead
process pseudoconstant clauses through the normal RestrictInfo qual
distribution mechanism. When a pseudoconstant clause is found attached to
a path node in create_plan(), pull it out and generate a gating Result at
that point. This requires special-casing pseudoconstants in selectivity
estimation and cost_qual_eval, but on the whole it's pretty clean.
It probably even makes the planner a bit faster than before for the normal
case of no pseudoconstants, since removing pull_constant_clauses saves one
useless traversal of the qual tree. Per gripe from Phil Frost.
2006-07-01 20:38:33 +02:00
|
|
|
bool pseudoconstant = false;
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
2005-07-03 01:00:42 +02:00
|
|
|
bool maybe_equijoin;
|
|
|
|
bool maybe_outer_join;
|
2004-01-04 01:07:32 +01:00
|
|
|
RestrictInfo *restrictinfo;
|
|
|
|
RelOptInfo *rel;
|
2004-01-04 04:51:52 +01:00
|
|
|
List *vars;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-07-25 01:21:14 +02:00
|
|
|
/*
|
2004-01-04 04:51:52 +01:00
|
|
|
* Retrieve all relids mentioned within the clause.
|
1999-07-25 01:21:14 +02:00
|
|
|
*/
|
2004-01-04 04:51:52 +01:00
|
|
|
relids = pull_varnos(clause);
|
1999-07-25 01:21:14 +02:00
|
|
|
|
2000-09-12 23:07:18 +02:00
|
|
|
/*
|
2003-01-15 20:35:48 +01:00
|
|
|
* Cross-check: clause should contain no relids not within its scope.
|
|
|
|
* Otherwise the parser messed up.
|
2000-09-12 23:07:18 +02:00
|
|
|
*/
|
2003-02-08 21:20:55 +01:00
|
|
|
if (!bms_is_subset(relids, qualscope))
|
2003-01-15 20:35:48 +01:00
|
|
|
elog(ERROR, "JOIN qualification may not refer to other relations");
|
2005-12-20 03:30:36 +01:00
|
|
|
if (ojscope && !bms_is_subset(relids, ojscope))
|
|
|
|
elog(ERROR, "JOIN qualification may not refer to other relations");
|
2000-09-29 20:21:41 +02:00
|
|
|
|
|
|
|
/*
|
Revise the planner's handling of "pseudoconstant" WHERE clauses, that is
clauses containing no variables and no volatile functions. Such a clause
can be used as a one-time qual in a gating Result plan node, to suppress
plan execution entirely when it is false. Even when the clause is true,
putting it in a gating node wins by avoiding repeated evaluation of the
clause. In previous PG releases, query_planner() would do this for
pseudoconstant clauses appearing at the top level of the jointree, but
there was no ability to generate a gating Result deeper in the plan tree.
To fix it, get rid of the special case in query_planner(), and instead
process pseudoconstant clauses through the normal RestrictInfo qual
distribution mechanism. When a pseudoconstant clause is found attached to
a path node in create_plan(), pull it out and generate a gating Result at
that point. This requires special-casing pseudoconstants in selectivity
estimation and cost_qual_eval, but on the whole it's pretty clean.
It probably even makes the planner a bit faster than before for the normal
case of no pseudoconstants, since removing pull_constant_clauses saves one
useless traversal of the qual tree. Per gripe from Phil Frost.
2006-07-01 20:38:33 +02:00
|
|
|
* If the clause is variable-free, our normal heuristic for pushing it
|
|
|
|
* down to just the mentioned rels doesn't work, because there are none.
|
|
|
|
*
|
|
|
|
* If the clause is an outer-join clause, we must force it to the OJ's
|
|
|
|
* semantic level to preserve semantics.
|
|
|
|
*
|
|
|
|
* Otherwise, when the clause contains volatile functions, we force it
|
|
|
|
* to be evaluated at its original syntactic level. This preserves the
|
|
|
|
* expected semantics.
|
|
|
|
*
|
|
|
|
* When the clause contains no volatile functions either, it is actually
|
|
|
|
* a pseudoconstant clause that will not change value during any one
|
|
|
|
* execution of the plan, and hence can be used as a one-time qual in
|
|
|
|
* a gating Result plan node. We put such a clause into the regular
|
|
|
|
* RestrictInfo lists for the moment, but eventually createplan.c will
|
|
|
|
* pull it out and make a gating Result node immediately above whatever
|
|
|
|
* plan node the pseudoconstant clause is assigned to. It's usually
|
|
|
|
* best to put a gating node as high in the plan tree as possible.
|
|
|
|
* If we are not below an outer join, we can actually push the
|
|
|
|
* pseudoconstant qual all the way to the top of the tree. If we are
|
|
|
|
* below an outer join, we leave the qual at its original syntactic level
|
|
|
|
* (we could push it up to just below the outer join, but that seems more
|
|
|
|
* complex than it's worth).
|
2000-09-29 20:21:41 +02:00
|
|
|
*/
|
2003-02-08 21:20:55 +01:00
|
|
|
if (bms_is_empty(relids))
|
Revise the planner's handling of "pseudoconstant" WHERE clauses, that is
clauses containing no variables and no volatile functions. Such a clause
can be used as a one-time qual in a gating Result plan node, to suppress
plan execution entirely when it is false. Even when the clause is true,
putting it in a gating node wins by avoiding repeated evaluation of the
clause. In previous PG releases, query_planner() would do this for
pseudoconstant clauses appearing at the top level of the jointree, but
there was no ability to generate a gating Result deeper in the plan tree.
To fix it, get rid of the special case in query_planner(), and instead
process pseudoconstant clauses through the normal RestrictInfo qual
distribution mechanism. When a pseudoconstant clause is found attached to
a path node in create_plan(), pull it out and generate a gating Result at
that point. This requires special-casing pseudoconstants in selectivity
estimation and cost_qual_eval, but on the whole it's pretty clean.
It probably even makes the planner a bit faster than before for the normal
case of no pseudoconstants, since removing pull_constant_clauses saves one
useless traversal of the qual tree. Per gripe from Phil Frost.
2006-07-01 20:38:33 +02:00
|
|
|
{
|
|
|
|
if (ojscope)
|
|
|
|
{
|
|
|
|
/* clause is attached to outer join, eval it there */
|
|
|
|
relids = ojscope;
|
|
|
|
/* mustn't use as gating qual, so don't mark pseudoconstant */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* eval at original syntactic level */
|
|
|
|
relids = qualscope;
|
|
|
|
if (!contain_volatile_functions(clause))
|
|
|
|
{
|
|
|
|
/* mark as gating qual */
|
|
|
|
pseudoconstant = true;
|
|
|
|
/* tell createplan.c to check for gating quals */
|
|
|
|
root->hasPseudoConstantQuals = true;
|
|
|
|
/* if not below outer join, push it to top of tree */
|
|
|
|
if (!below_outer_join)
|
|
|
|
{
|
|
|
|
relids = get_relids_in_jointree((Node *) root->parse->jointree);
|
|
|
|
is_pushed_down = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-09-29 20:21:41 +02:00
|
|
|
|
|
|
|
/*
|
2003-03-03 00:46:34 +01:00
|
|
|
* Check to see if clause application must be delayed by outer-join
|
|
|
|
* considerations.
|
2000-09-29 20:21:41 +02:00
|
|
|
*/
|
2005-09-28 23:17:02 +02:00
|
|
|
if (is_deduced)
|
2001-10-18 18:11:42 +02:00
|
|
|
{
|
2003-03-03 00:46:34 +01:00
|
|
|
/*
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
2005-07-03 01:00:42 +02:00
|
|
|
* If the qual came from implied-equality deduction, we always
|
|
|
|
* evaluate the qual at its natural semantic level. It is the
|
2005-10-15 04:49:52 +02:00
|
|
|
* responsibility of the deducer not to create any quals that should
|
|
|
|
* be delayed by outer-join rules.
|
2003-03-03 00:46:34 +01:00
|
|
|
*/
|
2003-02-08 21:20:55 +01:00
|
|
|
Assert(bms_equal(relids, qualscope));
|
2005-12-20 03:30:36 +01:00
|
|
|
Assert(!ojscope);
|
Revise the planner's handling of "pseudoconstant" WHERE clauses, that is
clauses containing no variables and no volatile functions. Such a clause
can be used as a one-time qual in a gating Result plan node, to suppress
plan execution entirely when it is false. Even when the clause is true,
putting it in a gating node wins by avoiding repeated evaluation of the
clause. In previous PG releases, query_planner() would do this for
pseudoconstant clauses appearing at the top level of the jointree, but
there was no ability to generate a gating Result deeper in the plan tree.
To fix it, get rid of the special case in query_planner(), and instead
process pseudoconstant clauses through the normal RestrictInfo qual
distribution mechanism. When a pseudoconstant clause is found attached to
a path node in create_plan(), pull it out and generate a gating Result at
that point. This requires special-casing pseudoconstants in selectivity
estimation and cost_qual_eval, but on the whole it's pretty clean.
It probably even makes the planner a bit faster than before for the normal
case of no pseudoconstants, since removing pull_constant_clauses saves one
useless traversal of the qual tree. Per gripe from Phil Frost.
2006-07-01 20:38:33 +02:00
|
|
|
Assert(!pseudoconstant);
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
2005-07-03 01:00:42 +02:00
|
|
|
/* Needn't feed it back for more deductions */
|
2005-11-15 00:54:23 +01:00
|
|
|
outerjoin_delayed = false;
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
2005-07-03 01:00:42 +02:00
|
|
|
maybe_equijoin = false;
|
|
|
|
maybe_outer_join = false;
|
2001-10-18 18:11:42 +02:00
|
|
|
}
|
2003-03-03 00:46:34 +01:00
|
|
|
else if (bms_overlap(relids, outerjoin_nonnullable))
|
2000-09-12 23:07:18 +02:00
|
|
|
{
|
2003-03-03 00:46:34 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* The qual is attached to an outer join and mentions (some of the)
|
|
|
|
* rels on the nonnullable side. Force the qual to be evaluated
|
|
|
|
* exactly at the level of joining corresponding to the outer join. We
|
|
|
|
* cannot let it get pushed down into the nonnullable side, since then
|
|
|
|
* we'd produce no output rows, rather than the intended single
|
|
|
|
* null-extended row, for any nonnullable-side rows failing the qual.
|
2003-03-03 00:46:34 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Note: an outer-join qual that mentions only nullable-side rels can
|
|
|
|
* be pushed down into the nullable side without changing the join
|
2005-09-28 23:17:02 +02:00
|
|
|
* result, so we treat it the same as an ordinary inner-join qual,
|
|
|
|
* except for not setting maybe_equijoin (see below).
|
2003-03-03 00:46:34 +01:00
|
|
|
*/
|
2005-12-20 03:30:36 +01:00
|
|
|
Assert(ojscope);
|
|
|
|
relids = ojscope;
|
2005-11-15 00:54:23 +01:00
|
|
|
outerjoin_delayed = true;
|
Revise the planner's handling of "pseudoconstant" WHERE clauses, that is
clauses containing no variables and no volatile functions. Such a clause
can be used as a one-time qual in a gating Result plan node, to suppress
plan execution entirely when it is false. Even when the clause is true,
putting it in a gating node wins by avoiding repeated evaluation of the
clause. In previous PG releases, query_planner() would do this for
pseudoconstant clauses appearing at the top level of the jointree, but
there was no ability to generate a gating Result deeper in the plan tree.
To fix it, get rid of the special case in query_planner(), and instead
process pseudoconstant clauses through the normal RestrictInfo qual
distribution mechanism. When a pseudoconstant clause is found attached to
a path node in create_plan(), pull it out and generate a gating Result at
that point. This requires special-casing pseudoconstants in selectivity
estimation and cost_qual_eval, but on the whole it's pretty clean.
It probably even makes the planner a bit faster than before for the normal
case of no pseudoconstants, since removing pull_constant_clauses saves one
useless traversal of the qual tree. Per gripe from Phil Frost.
2006-07-01 20:38:33 +02:00
|
|
|
Assert(!pseudoconstant);
|
2005-10-15 04:49:52 +02:00
|
|
|
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
2005-07-03 01:00:42 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We can't use such a clause to deduce equijoin (the left and right
|
|
|
|
* sides might be unequal above the join because one of them has gone
|
|
|
|
* to NULL) ... but we might be able to use it for more limited
|
|
|
|
* purposes. Note: for the current uses of deductions from an
|
|
|
|
* outer-join clause, it seems safe to make the deductions even when
|
|
|
|
* the clause is below a higher-level outer join; so we do not check
|
|
|
|
* below_outer_join here.
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
2005-07-03 01:00:42 +02:00
|
|
|
*/
|
|
|
|
maybe_equijoin = false;
|
|
|
|
maybe_outer_join = true;
|
2000-09-12 23:07:18 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-03-03 00:46:34 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* For a non-outer-join qual, we can evaluate the qual as soon as (1)
|
|
|
|
* we have all the rels it mentions, and (2) we are at or above any
|
|
|
|
* outer joins that can null any of these rels and are below the
|
|
|
|
* syntactic location of the given qual. To enforce the latter, scan
|
2005-12-20 03:30:36 +01:00
|
|
|
* the oj_info_list and merge the required-relid sets of any such OJs
|
2005-10-15 04:49:52 +02:00
|
|
|
* into the clause's own reference list. At the time we are called,
|
2005-12-20 03:30:36 +01:00
|
|
|
* the oj_info_list contains only outer joins below this qual.
|
2003-03-03 00:46:34 +01:00
|
|
|
*/
|
|
|
|
Relids addrelids = NULL;
|
2005-12-20 03:30:36 +01:00
|
|
|
ListCell *l;
|
2000-09-12 23:07:18 +02:00
|
|
|
|
2005-11-15 00:54:23 +01:00
|
|
|
outerjoin_delayed = false;
|
2005-12-20 03:30:36 +01:00
|
|
|
foreach(l, root->oj_info_list)
|
2000-09-12 23:07:18 +02:00
|
|
|
{
|
2005-12-20 03:30:36 +01:00
|
|
|
OuterJoinInfo *ojinfo = (OuterJoinInfo *) lfirst(l);
|
2000-09-12 23:07:18 +02:00
|
|
|
|
2005-12-20 03:30:36 +01:00
|
|
|
if (bms_overlap(relids, ojinfo->min_righthand) ||
|
|
|
|
(ojinfo->is_full_join &&
|
|
|
|
bms_overlap(relids, ojinfo->min_lefthand)))
|
2005-11-15 00:54:23 +01:00
|
|
|
{
|
2005-12-20 03:30:36 +01:00
|
|
|
addrelids = bms_add_members(addrelids, ojinfo->min_lefthand);
|
|
|
|
addrelids = bms_add_members(addrelids, ojinfo->min_righthand);
|
2005-11-15 00:54:23 +01:00
|
|
|
outerjoin_delayed = true;
|
|
|
|
}
|
2000-09-12 23:07:18 +02:00
|
|
|
}
|
2003-03-03 00:46:34 +01:00
|
|
|
|
|
|
|
if (bms_is_subset(addrelids, relids))
|
|
|
|
{
|
2005-09-28 23:17:02 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Qual is not delayed by any lower outer-join restriction. If it
|
|
|
|
* is not itself below or within an outer join, we can consider it
|
|
|
|
* "valid everywhere", so consider feeding it to the equijoin
|
|
|
|
* machinery. (If it is within an outer join, we can't consider
|
|
|
|
* it "valid everywhere": once the contained variables have gone
|
|
|
|
* to NULL, we'd be asserting things like NULL = NULL, which is
|
|
|
|
* not true.)
|
2005-09-28 23:17:02 +02:00
|
|
|
*/
|
|
|
|
if (!below_outer_join && outerjoin_nonnullable == NULL)
|
|
|
|
maybe_equijoin = true;
|
|
|
|
else
|
|
|
|
maybe_equijoin = false;
|
2003-03-03 00:46:34 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
relids = bms_union(relids, addrelids);
|
|
|
|
/* Should still be a subset of current scope ... */
|
|
|
|
Assert(bms_is_subset(relids, qualscope));
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2003-03-03 00:46:34 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Because application of the qual will be delayed by outer join,
|
|
|
|
* we mustn't assume its vars are equal everywhere.
|
2003-03-03 00:46:34 +01:00
|
|
|
*/
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
2005-07-03 01:00:42 +02:00
|
|
|
maybe_equijoin = false;
|
2003-03-03 00:46:34 +01:00
|
|
|
}
|
|
|
|
bms_free(addrelids);
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
2005-07-03 01:00:42 +02:00
|
|
|
maybe_outer_join = false;
|
2000-09-12 23:07:18 +02:00
|
|
|
}
|
|
|
|
|
2000-09-29 20:21:41 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Mark the qual as "pushed down" if it can be applied at a level below
|
|
|
|
* its original syntactic level. This allows us to distinguish original
|
|
|
|
* JOIN/ON quals from higher-level quals pushed down to the same joinrel.
|
|
|
|
* A qual originating from WHERE is always considered "pushed down".
|
2005-12-20 03:30:36 +01:00
|
|
|
* Note that for an outer-join qual, we have to compare to ojscope not
|
|
|
|
* qualscope.
|
2000-09-29 20:21:41 +02:00
|
|
|
*/
|
2004-01-05 06:07:36 +01:00
|
|
|
if (!is_pushed_down)
|
2005-12-20 03:30:36 +01:00
|
|
|
is_pushed_down = !bms_equal(relids, ojscope ? ojscope : qualscope);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-12-31 00:53:15 +01:00
|
|
|
/*
|
2004-01-04 01:07:32 +01:00
|
|
|
* Build the RestrictInfo node itself.
|
2003-12-31 00:53:15 +01:00
|
|
|
*/
|
2004-01-05 06:07:36 +01:00
|
|
|
restrictinfo = make_restrictinfo((Expr *) clause,
|
|
|
|
is_pushed_down,
|
2005-11-15 00:54:23 +01:00
|
|
|
outerjoin_delayed,
|
Revise the planner's handling of "pseudoconstant" WHERE clauses, that is
clauses containing no variables and no volatile functions. Such a clause
can be used as a one-time qual in a gating Result plan node, to suppress
plan execution entirely when it is false. Even when the clause is true,
putting it in a gating node wins by avoiding repeated evaluation of the
clause. In previous PG releases, query_planner() would do this for
pseudoconstant clauses appearing at the top level of the jointree, but
there was no ability to generate a gating Result deeper in the plan tree.
To fix it, get rid of the special case in query_planner(), and instead
process pseudoconstant clauses through the normal RestrictInfo qual
distribution mechanism. When a pseudoconstant clause is found attached to
a path node in create_plan(), pull it out and generate a gating Result at
that point. This requires special-casing pseudoconstants in selectivity
estimation and cost_qual_eval, but on the whole it's pretty clean.
It probably even makes the planner a bit faster than before for the normal
case of no pseudoconstants, since removing pull_constant_clauses saves one
useless traversal of the qual tree. Per gripe from Phil Frost.
2006-07-01 20:38:33 +02:00
|
|
|
pseudoconstant,
|
2005-06-09 06:19:00 +02:00
|
|
|
relids);
|
2003-12-31 00:53:15 +01:00
|
|
|
|
2004-01-04 01:07:32 +01:00
|
|
|
/*
|
|
|
|
* Figure out where to attach it.
|
|
|
|
*/
|
2003-02-08 21:20:55 +01:00
|
|
|
switch (bms_membership(relids))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-02-08 21:20:55 +01:00
|
|
|
case BMS_SINGLETON:
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2003-02-08 21:20:55 +01:00
|
|
|
/*
|
|
|
|
* There is only one relation participating in 'clause', so
|
|
|
|
* 'clause' is a restriction clause for that relation.
|
|
|
|
*/
|
|
|
|
rel = find_base_rel(root, bms_singleton_member(relids));
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Check for a "mergejoinable" clause even though it's not a join
|
|
|
|
* clause. This is so that we can recognize that "a.x = a.y"
|
|
|
|
* makes x and y eligible to be considered equal, even when they
|
|
|
|
* belong to the same rel. Without this, we would not recognize
|
|
|
|
* that "a.x = a.y AND a.x = b.z AND a.y = c.q" allows us to
|
|
|
|
* consider z and q equal after their rels are joined.
|
2003-02-08 21:20:55 +01:00
|
|
|
*/
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
2005-07-03 01:00:42 +02:00
|
|
|
check_mergejoinable(restrictinfo);
|
2003-02-08 21:20:55 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If the clause was deduced from implied equality, check to see
|
|
|
|
* whether it is redundant with restriction clauses we already
|
|
|
|
* have for this rel. Note we cannot apply this check to
|
|
|
|
* user-written clauses, since we haven't found the canonical
|
|
|
|
* pathkey sets yet while processing user clauses. (NB: no
|
|
|
|
* comparable check is done in the join-clause case; redundancy
|
|
|
|
* will be detected when the join clause is moved into a join
|
|
|
|
* rel's restriction list.)
|
2003-02-08 21:20:55 +01:00
|
|
|
*/
|
2005-09-28 23:17:02 +02:00
|
|
|
if (!is_deduced ||
|
2004-01-04 01:07:32 +01:00
|
|
|
!qual_is_redundant(root, restrictinfo,
|
|
|
|
rel->baserestrictinfo))
|
2003-01-15 20:35:48 +01:00
|
|
|
{
|
2003-02-08 21:20:55 +01:00
|
|
|
/* Add clause to rel's restriction list */
|
|
|
|
rel->baserestrictinfo = lappend(rel->baserestrictinfo,
|
|
|
|
restrictinfo);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BMS_MULTIPLE:
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2003-02-08 21:20:55 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* 'clause' is a join clause, since there is more than one rel in
|
|
|
|
* the relid set.
|
2003-02-08 21:20:55 +01:00
|
|
|
*/
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2003-02-08 21:20:55 +01:00
|
|
|
/*
|
2004-01-04 01:07:32 +01:00
|
|
|
* Check for hash or mergejoinable operators.
|
2003-02-08 21:20:55 +01:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We don't bother setting the hashjoin info if we're not going to
|
|
|
|
* need it. We do want to know about mergejoinable ops in all
|
|
|
|
* cases, however, because we use mergejoinable ops for other
|
|
|
|
* purposes such as detecting redundant clauses.
|
2003-02-08 21:20:55 +01:00
|
|
|
*/
|
|
|
|
check_mergejoinable(restrictinfo);
|
|
|
|
if (enable_hashjoin)
|
|
|
|
check_hashjoinable(restrictinfo);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add clause to the join lists of all the relevant relations.
|
|
|
|
*/
|
|
|
|
add_join_clause_to_rels(root, restrictinfo, relids);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add vars used in the join clause to targetlists of their
|
2005-10-15 04:49:52 +02:00
|
|
|
* relations, so that they will be emitted by the plan nodes that
|
|
|
|
* scan those relations (else they won't be available at the join
|
|
|
|
* node!).
|
2003-02-08 21:20:55 +01:00
|
|
|
*/
|
2004-01-04 04:51:52 +01:00
|
|
|
vars = pull_var_clause(clause, false);
|
2003-06-30 01:05:05 +02:00
|
|
|
add_vars_to_targetlist(root, vars, relids);
|
2004-05-31 01:40:41 +02:00
|
|
|
list_free(vars);
|
2003-02-08 21:20:55 +01:00
|
|
|
break;
|
|
|
|
default:
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2003-02-08 21:20:55 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* 'clause' references no rels, and therefore we have no place to
|
|
|
|
* attach it. Shouldn't get here if callers are working properly.
|
2003-02-08 21:20:55 +01:00
|
|
|
*/
|
2003-07-25 02:01:09 +02:00
|
|
|
elog(ERROR, "cannot cope with variable-free clause");
|
2003-02-08 21:20:55 +01:00
|
|
|
break;
|
2000-08-13 04:50:35 +02:00
|
|
|
}
|
2000-02-15 21:49:31 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If the clause has a mergejoinable operator, we may be able to deduce
|
|
|
|
* more things from it under the principle of transitivity.
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
2005-07-03 01:00:42 +02:00
|
|
|
*
|
|
|
|
* If it is not an outer-join qualification nor bubbled up due to an outer
|
|
|
|
* join, then the two sides represent equivalent PathKeyItems for path
|
|
|
|
* keys: any path that is sorted by one side will also be sorted by the
|
|
|
|
* other (as soon as the two rels are joined, that is). Pass such clauses
|
|
|
|
* to add_equijoined_keys.
|
|
|
|
*
|
|
|
|
* If it is a left or right outer-join qualification that relates the two
|
|
|
|
* sides of the outer join (no funny business like leftvar1 = leftvar2 +
|
|
|
|
* rightvar), we add it to root->left_join_clauses or
|
|
|
|
* root->right_join_clauses according to which side the nonnullable
|
|
|
|
* variable appears on.
|
|
|
|
*
|
|
|
|
* If it is a full outer-join qualification, we add it to
|
|
|
|
* root->full_join_clauses. (Ideally we'd discard cases that aren't
|
|
|
|
* leftvar = rightvar, as we do for left/right joins, but this routine
|
|
|
|
* doesn't have the info needed to do that; and the current usage of the
|
|
|
|
* full_join_clauses list doesn't require that, so it's not currently
|
|
|
|
* worth complicating this routine's API to make it possible.)
|
2000-02-15 21:49:31 +01:00
|
|
|
*/
|
Teach planner about some cases where a restriction clause can be
propagated inside an outer join. In particular, given
LEFT JOIN ON (A = B) WHERE A = constant, we cannot conclude that
B = constant at the top level (B might be null instead), but we
can nonetheless put a restriction B = constant into the quals for
B's relation, since no inner-side rows not meeting that condition
can contribute to the final result. Similarly, given
FULL JOIN USING (J) WHERE J = constant, we can't directly conclude
that either input J variable = constant, but it's OK to push such
quals into each input rel. Per recent gripe from Kim Bisgaard.
Along the way, remove 'valid_everywhere' flag from RestrictInfo,
as on closer analysis it was not being used for anything, and was
defined backwards anyway.
2005-07-03 01:00:42 +02:00
|
|
|
if (restrictinfo->mergejoinoperator != InvalidOid)
|
|
|
|
{
|
|
|
|
if (maybe_equijoin)
|
|
|
|
add_equijoined_keys(root, restrictinfo);
|
|
|
|
else if (maybe_outer_join && restrictinfo->can_join)
|
|
|
|
{
|
|
|
|
if (bms_is_subset(restrictinfo->left_relids,
|
|
|
|
outerjoin_nonnullable) &&
|
|
|
|
!bms_overlap(restrictinfo->right_relids,
|
|
|
|
outerjoin_nonnullable))
|
|
|
|
{
|
|
|
|
/* we have outervar = innervar */
|
|
|
|
root->left_join_clauses = lappend(root->left_join_clauses,
|
|
|
|
restrictinfo);
|
|
|
|
}
|
|
|
|
else if (bms_is_subset(restrictinfo->right_relids,
|
|
|
|
outerjoin_nonnullable) &&
|
|
|
|
!bms_overlap(restrictinfo->left_relids,
|
|
|
|
outerjoin_nonnullable))
|
|
|
|
{
|
|
|
|
/* we have innervar = outervar */
|
|
|
|
root->right_join_clauses = lappend(root->right_join_clauses,
|
|
|
|
restrictinfo);
|
|
|
|
}
|
|
|
|
else if (bms_equal(outerjoin_nonnullable, qualscope))
|
|
|
|
{
|
|
|
|
/* FULL JOIN (above tests cannot match in this case) */
|
|
|
|
root->full_join_clauses = lappend(root->full_join_clauses,
|
|
|
|
restrictinfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-07-24 05:11:01 +02:00
|
|
|
/*
|
|
|
|
* process_implied_equality
|
|
|
|
* Check to see whether we already have a restrictinfo item that says
|
2003-01-24 04:58:44 +01:00
|
|
|
* item1 = item2, and create one if not; or if delete_it is true,
|
|
|
|
* remove any such restrictinfo item.
|
|
|
|
*
|
|
|
|
* This processing is a consequence of transitivity of mergejoin equality:
|
|
|
|
* if we have mergejoinable clauses A = B and B = C, we can deduce A = C
|
2003-08-04 02:43:34 +02:00
|
|
|
* (where = is an appropriate mergejoinable operator). See path/pathkeys.c
|
2003-01-24 04:58:44 +01:00
|
|
|
* for more details.
|
2000-07-24 05:11:01 +02:00
|
|
|
*/
|
|
|
|
void
|
2005-06-06 00:32:58 +02:00
|
|
|
process_implied_equality(PlannerInfo *root,
|
2003-01-24 04:58:44 +01:00
|
|
|
Node *item1, Node *item2,
|
|
|
|
Oid sortop1, Oid sortop2,
|
|
|
|
Relids item1_relids, Relids item2_relids,
|
|
|
|
bool delete_it)
|
2000-07-24 05:11:01 +02:00
|
|
|
{
|
2003-01-24 04:58:44 +01:00
|
|
|
Relids relids;
|
2003-02-08 21:20:55 +01:00
|
|
|
BMS_Membership membership;
|
2003-01-24 04:58:44 +01:00
|
|
|
RelOptInfo *rel1;
|
|
|
|
List *restrictlist;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *itm;
|
2000-07-24 05:11:01 +02:00
|
|
|
Oid ltype,
|
|
|
|
rtype;
|
|
|
|
Operator eq_operator;
|
|
|
|
Form_pg_operator pgopform;
|
|
|
|
Expr *clause;
|
|
|
|
|
2003-02-08 21:20:55 +01:00
|
|
|
/* Get set of relids referenced in the two expressions */
|
|
|
|
relids = bms_union(item1_relids, item2_relids);
|
|
|
|
membership = bms_membership(relids);
|
2003-01-24 04:58:44 +01:00
|
|
|
|
2000-07-24 05:11:01 +02:00
|
|
|
/*
|
2003-01-24 04:58:44 +01:00
|
|
|
* generate_implied_equalities() shouldn't call me on two constants.
|
|
|
|
*/
|
2003-02-08 21:20:55 +01:00
|
|
|
Assert(membership != BMS_EMPTY_SET);
|
2003-01-24 04:58:44 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the exprs involve a single rel, we need to look at that rel's
|
2005-10-15 04:49:52 +02:00
|
|
|
* baserestrictinfo list. If multiple rels, we can scan the joininfo list
|
|
|
|
* of any of 'em.
|
2000-07-24 05:11:01 +02:00
|
|
|
*/
|
2003-02-08 21:20:55 +01:00
|
|
|
if (membership == BMS_SINGLETON)
|
|
|
|
{
|
|
|
|
rel1 = find_base_rel(root, bms_singleton_member(relids));
|
2003-01-24 04:58:44 +01:00
|
|
|
restrictlist = rel1->baserestrictinfo;
|
2003-02-08 21:20:55 +01:00
|
|
|
}
|
2003-01-24 04:58:44 +01:00
|
|
|
else
|
|
|
|
{
|
2003-02-08 21:20:55 +01:00
|
|
|
Relids other_rels;
|
|
|
|
int first_rel;
|
|
|
|
|
|
|
|
/* Copy relids, find and remove one member */
|
|
|
|
other_rels = bms_copy(relids);
|
|
|
|
first_rel = bms_first_member(other_rels);
|
2005-06-09 06:19:00 +02:00
|
|
|
bms_free(other_rels);
|
2003-02-08 21:20:55 +01:00
|
|
|
|
|
|
|
rel1 = find_base_rel(root, first_rel);
|
2005-06-09 06:19:00 +02:00
|
|
|
restrictlist = rel1->joininfo;
|
2003-01-24 04:58:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Scan to see if equality is already known. If so, we're done in the add
|
|
|
|
* case, and done after removing it in the delete case.
|
2003-01-24 04:58:44 +01:00
|
|
|
*/
|
|
|
|
foreach(itm, restrictlist)
|
|
|
|
{
|
|
|
|
RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(itm);
|
|
|
|
Node *left,
|
|
|
|
*right;
|
|
|
|
|
|
|
|
if (restrictinfo->mergejoinoperator == InvalidOid)
|
|
|
|
continue; /* ignore non-mergejoinable clauses */
|
|
|
|
/* We now know the restrictinfo clause is a binary opclause */
|
|
|
|
left = get_leftop(restrictinfo->clause);
|
|
|
|
right = get_rightop(restrictinfo->clause);
|
|
|
|
if ((equal(item1, left) && equal(item2, right)) ||
|
|
|
|
(equal(item2, left) && equal(item1, right)))
|
|
|
|
{
|
|
|
|
/* found a matching clause */
|
|
|
|
if (delete_it)
|
|
|
|
{
|
2003-02-08 21:20:55 +01:00
|
|
|
if (membership == BMS_SINGLETON)
|
2003-01-24 04:58:44 +01:00
|
|
|
{
|
|
|
|
/* delete it from local restrictinfo list */
|
2004-05-31 01:40:41 +02:00
|
|
|
rel1->baserestrictinfo = list_delete_ptr(rel1->baserestrictinfo,
|
2005-10-15 04:49:52 +02:00
|
|
|
restrictinfo);
|
2003-01-24 04:58:44 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* let joininfo.c do it */
|
|
|
|
remove_join_clause_from_rels(root, restrictinfo, relids);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return; /* done */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Didn't find it. Done if deletion requested */
|
|
|
|
if (delete_it)
|
2003-01-15 20:35:48 +01:00
|
|
|
return;
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-07-24 05:11:01 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* This equality is new information, so construct a clause representing it
|
|
|
|
* to add to the query data structures.
|
2000-07-24 05:11:01 +02:00
|
|
|
*/
|
|
|
|
ltype = exprType(item1);
|
|
|
|
rtype = exprType(item2);
|
2006-03-14 23:48:25 +01:00
|
|
|
eq_operator = compatible_oper(NULL, list_make1(makeString("=")),
|
|
|
|
ltype, rtype,
|
|
|
|
true, -1);
|
2000-07-24 05:11:01 +02:00
|
|
|
if (!HeapTupleIsValid(eq_operator))
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Would it be safe to just not add the equality to the query if we
|
|
|
|
* have no suitable equality operator for the combination of
|
2000-07-24 05:11:01 +02:00
|
|
|
* datatypes? NO, because sortkey selection may screw up anyway.
|
|
|
|
*/
|
2003-07-25 02:01:09 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("could not identify an equality operator for types %s and %s",
|
|
|
|
format_type_be(ltype), format_type_be(rtype))));
|
2000-07-24 05:11:01 +02:00
|
|
|
}
|
|
|
|
pgopform = (Form_pg_operator) GETSTRUCT(eq_operator);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-07-24 05:11:01 +02:00
|
|
|
/*
|
|
|
|
* Let's just make sure this appears to be a compatible operator.
|
|
|
|
*/
|
|
|
|
if (pgopform->oprlsortop != sortop1 ||
|
|
|
|
pgopform->oprrsortop != sortop2 ||
|
|
|
|
pgopform->oprresult != BOOLOID)
|
2003-07-25 02:01:09 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("equality operator for types %s and %s should be merge-joinable, but isn't",
|
2003-07-25 02:01:09 +02:00
|
|
|
format_type_be(ltype), format_type_be(rtype))));
|
2000-07-24 05:11:01 +02:00
|
|
|
|
2004-02-27 22:42:00 +01:00
|
|
|
/*
|
|
|
|
* Now we can build the new clause. Copy to ensure it shares no
|
|
|
|
* substructure with original (this is necessary in case there are
|
|
|
|
* subselects in there...)
|
|
|
|
*/
|
2003-08-04 02:43:34 +02:00
|
|
|
clause = make_opclause(oprid(eq_operator), /* opno */
|
|
|
|
BOOLOID, /* opresulttype */
|
|
|
|
false, /* opretset */
|
2004-02-27 22:42:00 +01:00
|
|
|
(Expr *) copyObject(item1),
|
|
|
|
(Expr *) copyObject(item2));
|
2000-07-24 05:11:01 +02:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
ReleaseSysCache(eq_operator);
|
|
|
|
|
2000-09-29 20:21:41 +02:00
|
|
|
/*
|
2003-01-15 20:35:48 +01:00
|
|
|
* Push the new clause into all the appropriate restrictinfo lists.
|
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Note: we mark the qual "pushed down" to ensure that it can never be
|
|
|
|
* taken for an original JOIN/ON clause.
|
2000-09-29 20:21:41 +02:00
|
|
|
*/
|
|
|
|
distribute_qual_to_rels(root, (Node *) clause,
|
2005-12-20 03:30:36 +01:00
|
|
|
true, true, false, relids, NULL, NULL);
|
2002-11-20 00:22:00 +01:00
|
|
|
}
|
|
|
|
|
2001-10-18 18:11:42 +02:00
|
|
|
/*
|
|
|
|
* qual_is_redundant
|
|
|
|
* Detect whether an implied-equality qual that turns out to be a
|
|
|
|
* restriction clause for a single base relation is redundant with
|
|
|
|
* already-known restriction clauses for that rel. This occurs with,
|
|
|
|
* for example,
|
|
|
|
* SELECT * FROM tab WHERE f1 = f2 AND f2 = f3;
|
|
|
|
* We need to suppress the redundant condition to avoid computing
|
|
|
|
* too-small selectivity, not to mention wasting time at execution.
|
2003-01-24 04:58:44 +01:00
|
|
|
*
|
|
|
|
* Note: quals of the form "var = const" are never considered redundant,
|
2003-08-04 02:43:34 +02:00
|
|
|
* only those of the form "var = var". This is needed because when we
|
2003-01-24 04:58:44 +01:00
|
|
|
* have constants in an implied-equality set, we use a different strategy
|
2003-08-04 02:43:34 +02:00
|
|
|
* that suppresses all "var = var" deductions. We must therefore keep
|
2003-01-24 04:58:44 +01:00
|
|
|
* all the "var = const" quals.
|
2001-10-18 18:11:42 +02:00
|
|
|
*/
|
|
|
|
static bool
|
2005-06-06 00:32:58 +02:00
|
|
|
qual_is_redundant(PlannerInfo *root,
|
2001-10-18 18:11:42 +02:00
|
|
|
RestrictInfo *restrictinfo,
|
|
|
|
List *restrictlist)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
Node *newleft;
|
|
|
|
Node *newright;
|
2003-01-24 04:58:44 +01:00
|
|
|
List *oldquals;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *olditem;
|
2003-01-15 20:35:48 +01:00
|
|
|
List *equalexprs;
|
2001-10-25 07:50:21 +02:00
|
|
|
bool someadded;
|
2001-10-18 18:11:42 +02:00
|
|
|
|
2003-01-24 04:58:44 +01:00
|
|
|
/* Never redundant unless vars appear on both sides */
|
2003-12-31 00:53:15 +01:00
|
|
|
if (bms_is_empty(restrictinfo->left_relids) ||
|
|
|
|
bms_is_empty(restrictinfo->right_relids))
|
2003-01-24 04:58:44 +01:00
|
|
|
return false;
|
|
|
|
|
2003-12-31 00:53:15 +01:00
|
|
|
newleft = get_leftop(restrictinfo->clause);
|
|
|
|
newright = get_rightop(restrictinfo->clause);
|
|
|
|
|
2001-10-18 18:11:42 +02:00
|
|
|
/*
|
|
|
|
* Set cached pathkeys. NB: it is okay to do this now because this
|
|
|
|
* routine is only invoked while we are generating implied equalities.
|
|
|
|
* Therefore, the equi_key_list is already complete and so we can
|
|
|
|
* correctly determine canonical pathkeys.
|
|
|
|
*/
|
|
|
|
cache_mergeclause_pathkeys(root, restrictinfo);
|
|
|
|
/* If different, say "not redundant" (should never happen) */
|
|
|
|
if (restrictinfo->left_pathkey != restrictinfo->right_pathkey)
|
|
|
|
return false;
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2001-10-18 18:11:42 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Scan existing quals to find those referencing same pathkeys. Usually
|
|
|
|
* there will be few, if any, so build a list of just the interesting
|
|
|
|
* ones.
|
2001-10-18 18:11:42 +02:00
|
|
|
*/
|
|
|
|
oldquals = NIL;
|
|
|
|
foreach(olditem, restrictlist)
|
|
|
|
{
|
|
|
|
RestrictInfo *oldrinfo = (RestrictInfo *) lfirst(olditem);
|
|
|
|
|
|
|
|
if (oldrinfo->mergejoinoperator != InvalidOid)
|
|
|
|
{
|
|
|
|
cache_mergeclause_pathkeys(root, oldrinfo);
|
|
|
|
if (restrictinfo->left_pathkey == oldrinfo->left_pathkey &&
|
|
|
|
restrictinfo->right_pathkey == oldrinfo->right_pathkey)
|
|
|
|
oldquals = lcons(oldrinfo, oldquals);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (oldquals == NIL)
|
|
|
|
return false;
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2001-10-18 18:11:42 +02:00
|
|
|
/*
|
2003-01-15 20:35:48 +01:00
|
|
|
* Now, we want to develop a list of exprs that are known equal to the
|
2005-10-15 04:49:52 +02:00
|
|
|
* left side of the new qual. We traverse the old-quals list repeatedly
|
|
|
|
* to transitively expand the exprs list. If at any point we find we can
|
|
|
|
* reach the right-side expr of the new qual, we are done. We give up
|
|
|
|
* when we can't expand the equalexprs list any more.
|
2001-10-18 18:11:42 +02:00
|
|
|
*/
|
2004-05-31 01:40:41 +02:00
|
|
|
equalexprs = list_make1(newleft);
|
2001-10-25 07:50:21 +02:00
|
|
|
do
|
|
|
|
{
|
2001-10-18 18:11:42 +02:00
|
|
|
someadded = false;
|
2004-05-31 01:40:41 +02:00
|
|
|
/* cannot use foreach here because of possible list_delete */
|
2004-05-26 06:41:50 +02:00
|
|
|
olditem = list_head(oldquals);
|
2002-12-17 02:18:35 +01:00
|
|
|
while (olditem)
|
2001-10-18 18:11:42 +02:00
|
|
|
{
|
|
|
|
RestrictInfo *oldrinfo = (RestrictInfo *) lfirst(olditem);
|
2003-01-15 20:35:48 +01:00
|
|
|
Node *oldleft = get_leftop(oldrinfo->clause);
|
|
|
|
Node *oldright = get_rightop(oldrinfo->clause);
|
2001-10-25 07:50:21 +02:00
|
|
|
Node *newguy = NULL;
|
2001-10-18 18:11:42 +02:00
|
|
|
|
2004-05-31 01:40:41 +02:00
|
|
|
/* must advance olditem before list_delete possibly pfree's it */
|
2002-12-17 02:18:35 +01:00
|
|
|
olditem = lnext(olditem);
|
|
|
|
|
2004-05-31 01:40:41 +02:00
|
|
|
if (list_member(equalexprs, oldleft))
|
2001-10-18 18:11:42 +02:00
|
|
|
newguy = oldright;
|
2004-05-31 01:40:41 +02:00
|
|
|
else if (list_member(equalexprs, oldright))
|
2001-10-18 18:11:42 +02:00
|
|
|
newguy = oldleft;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
if (equal(newguy, newright))
|
|
|
|
return true; /* we proved new clause is redundant */
|
2003-01-15 20:35:48 +01:00
|
|
|
equalexprs = lcons(newguy, equalexprs);
|
2001-10-18 18:11:42 +02:00
|
|
|
someadded = true;
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2001-10-18 18:11:42 +02:00
|
|
|
/*
|
|
|
|
* Remove this qual from list, since we don't need it anymore.
|
|
|
|
*/
|
2004-05-31 01:40:41 +02:00
|
|
|
oldquals = list_delete_ptr(oldquals, oldrinfo);
|
2001-10-18 18:11:42 +02:00
|
|
|
}
|
|
|
|
} while (someadded);
|
|
|
|
|
|
|
|
return false; /* it's not redundant */
|
|
|
|
}
|
|
|
|
|
2000-07-24 05:11:01 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
2000-02-15 21:49:31 +01:00
|
|
|
* CHECKS FOR MERGEJOINABLE AND HASHJOINABLE CLAUSES
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-08-16 04:17:58 +02:00
|
|
|
* check_mergejoinable
|
|
|
|
* If the restrictinfo's clause is mergejoinable, set the mergejoin
|
|
|
|
* info fields in the restrictinfo.
|
|
|
|
*
|
|
|
|
* Currently, we support mergejoin for binary opclauses where
|
2003-01-15 20:35:48 +01:00
|
|
|
* the operator is a mergejoinable operator. The arguments can be
|
|
|
|
* anything --- as long as there are no volatile functions in them.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1999-08-16 04:17:58 +02:00
|
|
|
static void
|
|
|
|
check_mergejoinable(RestrictInfo *restrictinfo)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-08-16 04:17:58 +02:00
|
|
|
Expr *clause = restrictinfo->clause;
|
1999-02-15 02:06:59 +01:00
|
|
|
Oid opno,
|
|
|
|
leftOp,
|
1997-09-08 04:41:22 +02:00
|
|
|
rightOp;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
Revise the planner's handling of "pseudoconstant" WHERE clauses, that is
clauses containing no variables and no volatile functions. Such a clause
can be used as a one-time qual in a gating Result plan node, to suppress
plan execution entirely when it is false. Even when the clause is true,
putting it in a gating node wins by avoiding repeated evaluation of the
clause. In previous PG releases, query_planner() would do this for
pseudoconstant clauses appearing at the top level of the jointree, but
there was no ability to generate a gating Result deeper in the plan tree.
To fix it, get rid of the special case in query_planner(), and instead
process pseudoconstant clauses through the normal RestrictInfo qual
distribution mechanism. When a pseudoconstant clause is found attached to
a path node in create_plan(), pull it out and generate a gating Result at
that point. This requires special-casing pseudoconstants in selectivity
estimation and cost_qual_eval, but on the whole it's pretty clean.
It probably even makes the planner a bit faster than before for the normal
case of no pseudoconstants, since removing pull_constant_clauses saves one
useless traversal of the qual tree. Per gripe from Phil Frost.
2006-07-01 20:38:33 +02:00
|
|
|
if (restrictinfo->pseudoconstant)
|
|
|
|
return;
|
2002-12-12 16:49:42 +01:00
|
|
|
if (!is_opclause(clause))
|
1999-08-16 04:17:58 +02:00
|
|
|
return;
|
2004-05-31 01:40:41 +02:00
|
|
|
if (list_length(((OpExpr *) clause)->args) != 2)
|
1999-08-16 04:17:58 +02:00
|
|
|
return;
|
1999-02-15 02:06:59 +01:00
|
|
|
|
2002-12-12 16:49:42 +01:00
|
|
|
opno = ((OpExpr *) clause)->opno;
|
1999-02-15 02:06:59 +01:00
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
if (op_mergejoinable(opno,
|
|
|
|
&leftOp,
|
2003-01-15 20:35:48 +01:00
|
|
|
&rightOp) &&
|
|
|
|
!contain_volatile_functions((Node *) clause))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-08-16 04:17:58 +02:00
|
|
|
restrictinfo->mergejoinoperator = opno;
|
|
|
|
restrictinfo->left_sortop = leftOp;
|
|
|
|
restrictinfo->right_sortop = rightOp;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-08-16 04:17:58 +02:00
|
|
|
* check_hashjoinable
|
|
|
|
* If the restrictinfo's clause is hashjoinable, set the hashjoin
|
|
|
|
* info fields in the restrictinfo.
|
|
|
|
*
|
|
|
|
* Currently, we support hashjoin for binary opclauses where
|
2003-08-04 02:43:34 +02:00
|
|
|
* the operator is a hashjoinable operator. The arguments can be
|
2003-01-15 20:35:48 +01:00
|
|
|
* anything --- as long as there are no volatile functions in them.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1999-08-16 04:17:58 +02:00
|
|
|
static void
|
|
|
|
check_hashjoinable(RestrictInfo *restrictinfo)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-08-16 04:17:58 +02:00
|
|
|
Expr *clause = restrictinfo->clause;
|
|
|
|
Oid opno;
|
1999-02-15 02:06:59 +01:00
|
|
|
|
Revise the planner's handling of "pseudoconstant" WHERE clauses, that is
clauses containing no variables and no volatile functions. Such a clause
can be used as a one-time qual in a gating Result plan node, to suppress
plan execution entirely when it is false. Even when the clause is true,
putting it in a gating node wins by avoiding repeated evaluation of the
clause. In previous PG releases, query_planner() would do this for
pseudoconstant clauses appearing at the top level of the jointree, but
there was no ability to generate a gating Result deeper in the plan tree.
To fix it, get rid of the special case in query_planner(), and instead
process pseudoconstant clauses through the normal RestrictInfo qual
distribution mechanism. When a pseudoconstant clause is found attached to
a path node in create_plan(), pull it out and generate a gating Result at
that point. This requires special-casing pseudoconstants in selectivity
estimation and cost_qual_eval, but on the whole it's pretty clean.
It probably even makes the planner a bit faster than before for the normal
case of no pseudoconstants, since removing pull_constant_clauses saves one
useless traversal of the qual tree. Per gripe from Phil Frost.
2006-07-01 20:38:33 +02:00
|
|
|
if (restrictinfo->pseudoconstant)
|
|
|
|
return;
|
2002-12-12 16:49:42 +01:00
|
|
|
if (!is_opclause(clause))
|
1999-08-16 04:17:58 +02:00
|
|
|
return;
|
2004-05-31 01:40:41 +02:00
|
|
|
if (list_length(((OpExpr *) clause)->args) != 2)
|
1999-08-16 04:17:58 +02:00
|
|
|
return;
|
|
|
|
|
2002-12-12 16:49:42 +01:00
|
|
|
opno = ((OpExpr *) clause)->opno;
|
1999-02-15 02:06:59 +01:00
|
|
|
|
2003-01-15 20:35:48 +01:00
|
|
|
if (op_hashjoinable(opno) &&
|
|
|
|
!contain_volatile_functions((Node *) clause))
|
1999-08-16 04:17:58 +02:00
|
|
|
restrictinfo->hashjoinoperator = opno;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|