1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* relnode.c
|
2000-02-07 05:41:04 +01:00
|
|
|
* Relation-node lookup/construction routines
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2002-06-20 22:29:54 +02:00
|
|
|
* Portions Copyright (c) 1996-2002, 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
|
2003-01-12 23:35:29 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/relnode.c,v 1.42 2003/01/12 22:35:29 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
#include "optimizer/cost.h"
|
|
|
|
#include "optimizer/joininfo.h"
|
1999-07-16 05:14:30 +02:00
|
|
|
#include "optimizer/pathnode.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "optimizer/plancat.h"
|
2002-11-24 22:52:15 +01:00
|
|
|
#include "optimizer/restrictinfo.h"
|
2000-02-07 05:41:04 +01:00
|
|
|
#include "optimizer/tlist.h"
|
2000-09-29 20:21:41 +02:00
|
|
|
#include "parser/parsetree.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
2001-05-20 22:28:20 +02:00
|
|
|
static RelOptInfo *make_base_rel(Query *root, int relid);
|
2000-02-07 05:41:04 +01:00
|
|
|
static List *new_join_tlist(List *tlist, int first_resdomno);
|
2001-10-18 18:11:42 +02:00
|
|
|
static List *build_joinrel_restrictlist(Query *root,
|
2001-10-25 07:50:21 +02:00
|
|
|
RelOptInfo *joinrel,
|
|
|
|
RelOptInfo *outer_rel,
|
2002-10-13 00:24:49 +02:00
|
|
|
RelOptInfo *inner_rel,
|
|
|
|
JoinType jointype);
|
2000-02-07 05:41:04 +01:00
|
|
|
static void build_joinrel_joinlist(RelOptInfo *joinrel,
|
2000-04-12 19:17:23 +02:00
|
|
|
RelOptInfo *outer_rel,
|
|
|
|
RelOptInfo *inner_rel);
|
2000-02-07 05:41:04 +01:00
|
|
|
static List *subbuild_joinrel_restrictlist(RelOptInfo *joinrel,
|
2000-04-12 19:17:23 +02:00
|
|
|
List *joininfo_list);
|
2000-02-07 05:41:04 +01:00
|
|
|
static void subbuild_joinrel_joinlist(RelOptInfo *joinrel,
|
2000-04-12 19:17:23 +02:00
|
|
|
List *joininfo_list);
|
2000-02-07 05:41:04 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2001-05-20 22:28:20 +02:00
|
|
|
* build_base_rel
|
2002-03-12 01:52:10 +01:00
|
|
|
* Construct a new base relation RelOptInfo, and put it in the query's
|
|
|
|
* base_rel_list.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2002-03-12 01:52:10 +01:00
|
|
|
void
|
2001-05-20 22:28:20 +02:00
|
|
|
build_base_rel(Query *root, int relid)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-05-20 22:28:20 +02:00
|
|
|
List *rels;
|
1998-09-01 06:40:42 +02:00
|
|
|
RelOptInfo *rel;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-03-12 01:52:10 +01:00
|
|
|
/* Rel should not exist already */
|
2001-05-20 22:28:20 +02:00
|
|
|
foreach(rels, root->base_rel_list)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-05-20 22:28:20 +02:00
|
|
|
rel = (RelOptInfo *) lfirst(rels);
|
2000-02-07 05:41:04 +01:00
|
|
|
|
2001-05-20 22:28:20 +02:00
|
|
|
/* length(rel->relids) == 1 for all members of base_rel_list */
|
2000-02-07 05:41:04 +01:00
|
|
|
if (lfirsti(rel->relids) == relid)
|
2002-03-12 01:52:10 +01:00
|
|
|
elog(ERROR, "build_base_rel: rel already exists");
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2000-02-07 05:41:04 +01:00
|
|
|
|
2002-03-12 01:52:10 +01:00
|
|
|
/* It should not exist as an "other" rel, either */
|
2001-05-20 22:28:20 +02:00
|
|
|
foreach(rels, root->other_rel_list)
|
|
|
|
{
|
|
|
|
rel = (RelOptInfo *) lfirst(rels);
|
|
|
|
|
|
|
|
if (lfirsti(rel->relids) == relid)
|
|
|
|
elog(ERROR, "build_base_rel: rel already exists as 'other' rel");
|
|
|
|
}
|
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
/* No existing RelOptInfo for this base rel, so make a new one */
|
2000-11-12 01:37:02 +01:00
|
|
|
rel = make_base_rel(root, relid);
|
|
|
|
|
|
|
|
/* and add it to the list */
|
|
|
|
root->base_rel_list = lcons(rel, root->base_rel_list);
|
|
|
|
}
|
|
|
|
|
2001-05-20 22:28:20 +02:00
|
|
|
/*
|
|
|
|
* build_other_rel
|
|
|
|
* Returns relation entry corresponding to 'relid', creating a new one
|
2002-03-12 01:52:10 +01:00
|
|
|
* if necessary. This is for 'other' relations, which are much like
|
2001-05-20 22:28:20 +02:00
|
|
|
* base relations except that they live in a different list.
|
|
|
|
*/
|
|
|
|
RelOptInfo *
|
|
|
|
build_other_rel(Query *root, int relid)
|
|
|
|
{
|
|
|
|
List *rels;
|
|
|
|
RelOptInfo *rel;
|
|
|
|
|
|
|
|
/* Already made? */
|
|
|
|
foreach(rels, root->other_rel_list)
|
|
|
|
{
|
|
|
|
rel = (RelOptInfo *) lfirst(rels);
|
|
|
|
|
|
|
|
/* length(rel->relids) == 1 for all members of other_rel_list */
|
|
|
|
if (lfirsti(rel->relids) == relid)
|
|
|
|
return rel;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It should not exist as a base rel */
|
|
|
|
foreach(rels, root->base_rel_list)
|
|
|
|
{
|
|
|
|
rel = (RelOptInfo *) lfirst(rels);
|
|
|
|
|
|
|
|
if (lfirsti(rel->relids) == relid)
|
|
|
|
elog(ERROR, "build_other_rel: rel already exists as base rel");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No existing RelOptInfo for this other rel, so make a new one */
|
|
|
|
rel = make_base_rel(root, relid);
|
|
|
|
|
2002-03-12 01:52:10 +01:00
|
|
|
/* if it's not a join rel, must be a child rel */
|
|
|
|
if (rel->reloptkind == RELOPT_BASEREL)
|
|
|
|
rel->reloptkind = RELOPT_OTHER_CHILD_REL;
|
|
|
|
|
2001-05-20 22:28:20 +02:00
|
|
|
/* and add it to the list */
|
|
|
|
root->other_rel_list = lcons(rel, root->other_rel_list);
|
|
|
|
|
|
|
|
return rel;
|
|
|
|
}
|
|
|
|
|
2000-11-12 01:37:02 +01:00
|
|
|
/*
|
|
|
|
* make_base_rel
|
|
|
|
* Construct a base-relation RelOptInfo for the specified rangetable index.
|
|
|
|
*
|
2001-05-20 22:28:20 +02:00
|
|
|
* Common code for build_base_rel and build_other_rel.
|
2000-11-12 01:37:02 +01:00
|
|
|
*/
|
2001-05-20 22:28:20 +02:00
|
|
|
static RelOptInfo *
|
2000-11-12 01:37:02 +01:00
|
|
|
make_base_rel(Query *root, int relid)
|
|
|
|
{
|
|
|
|
RelOptInfo *rel = makeNode(RelOptInfo);
|
2002-03-12 01:52:10 +01:00
|
|
|
RangeTblEntry *rte = rt_fetch(relid, root->rtable);
|
2000-11-12 01:37:02 +01:00
|
|
|
|
2002-03-12 01:52:10 +01:00
|
|
|
rel->reloptkind = RELOPT_BASEREL;
|
2000-09-29 20:21:41 +02:00
|
|
|
rel->relids = makeListi1(relid);
|
2000-02-07 05:41:04 +01:00
|
|
|
rel->rows = 0;
|
|
|
|
rel->width = 0;
|
|
|
|
rel->targetlist = NIL;
|
|
|
|
rel->pathlist = NIL;
|
2000-02-15 21:49:31 +01:00
|
|
|
rel->cheapest_startup_path = NULL;
|
|
|
|
rel->cheapest_total_path = NULL;
|
2000-02-07 05:41:04 +01:00
|
|
|
rel->pruneable = true;
|
2002-05-12 22:10:05 +02:00
|
|
|
rel->rtekind = rte->rtekind;
|
2001-05-20 22:28:20 +02:00
|
|
|
rel->indexlist = NIL;
|
2000-02-07 05:41:04 +01:00
|
|
|
rel->pages = 0;
|
|
|
|
rel->tuples = 0;
|
2000-09-29 20:21:41 +02:00
|
|
|
rel->subplan = NULL;
|
2002-03-12 01:52:10 +01:00
|
|
|
rel->joinrti = 0;
|
|
|
|
rel->joinrteids = NIL;
|
2000-02-07 05:41:04 +01:00
|
|
|
rel->baserestrictinfo = NIL;
|
2003-01-12 23:35:29 +01:00
|
|
|
rel->baserestrictcost.startup = 0;
|
|
|
|
rel->baserestrictcost.per_tuple = 0;
|
2000-09-12 23:07:18 +02:00
|
|
|
rel->outerjoinset = NIL;
|
2000-02-07 05:41:04 +01:00
|
|
|
rel->joininfo = NIL;
|
2002-11-24 22:52:15 +01:00
|
|
|
rel->index_outer_relids = NIL;
|
|
|
|
rel->index_inner_paths = NIL;
|
2000-02-07 05:41:04 +01:00
|
|
|
|
2002-03-12 01:52:10 +01:00
|
|
|
/* Check type of rtable entry */
|
|
|
|
switch (rte->rtekind)
|
2000-09-29 20:21:41 +02:00
|
|
|
{
|
2002-03-12 01:52:10 +01:00
|
|
|
case RTE_RELATION:
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
|
|
|
/* Table --- retrieve statistics from the system catalogs */
|
|
|
|
bool indexed;
|
|
|
|
|
|
|
|
get_relation_info(rte->relid,
|
|
|
|
&indexed, &rel->pages, &rel->tuples);
|
|
|
|
if (indexed)
|
|
|
|
rel->indexlist = find_secondary_indexes(rte->relid);
|
|
|
|
break;
|
|
|
|
}
|
2002-03-12 01:52:10 +01:00
|
|
|
case RTE_SUBQUERY:
|
2002-05-12 22:10:05 +02:00
|
|
|
case RTE_FUNCTION:
|
|
|
|
/* Subquery or function --- nothing to do here */
|
2002-03-12 01:52:10 +01:00
|
|
|
break;
|
|
|
|
case RTE_JOIN:
|
|
|
|
/* Join --- must be an otherrel */
|
|
|
|
rel->reloptkind = RELOPT_OTHER_JOIN_REL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(ERROR, "make_base_rel: unsupported RTE kind %d",
|
|
|
|
(int) rte->rtekind);
|
|
|
|
break;
|
2000-09-29 20:21:41 +02:00
|
|
|
}
|
2000-02-07 05:41:04 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return rel;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2001-05-20 22:28:20 +02:00
|
|
|
/*
|
|
|
|
* find_base_rel
|
|
|
|
* Find a base or other relation entry, which must already exist
|
|
|
|
* (since we'd have no idea which list to add it to).
|
|
|
|
*/
|
|
|
|
RelOptInfo *
|
|
|
|
find_base_rel(Query *root, int relid)
|
|
|
|
{
|
|
|
|
List *rels;
|
|
|
|
RelOptInfo *rel;
|
|
|
|
|
|
|
|
foreach(rels, root->base_rel_list)
|
|
|
|
{
|
|
|
|
rel = (RelOptInfo *) lfirst(rels);
|
|
|
|
|
|
|
|
/* length(rel->relids) == 1 for all members of base_rel_list */
|
|
|
|
if (lfirsti(rel->relids) == relid)
|
|
|
|
return rel;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(rels, root->other_rel_list)
|
|
|
|
{
|
|
|
|
rel = (RelOptInfo *) lfirst(rels);
|
|
|
|
|
|
|
|
if (lfirsti(rel->relids) == relid)
|
|
|
|
return rel;
|
|
|
|
}
|
|
|
|
|
|
|
|
elog(ERROR, "find_base_rel: no relation entry for relid %d", relid);
|
|
|
|
|
|
|
|
return NULL; /* keep compiler quiet */
|
|
|
|
}
|
|
|
|
|
2002-03-12 01:52:10 +01:00
|
|
|
/*
|
|
|
|
* find_other_rel
|
|
|
|
* Find an otherrel entry, if one exists for the given relid.
|
|
|
|
* Return NULL if no entry.
|
|
|
|
*/
|
|
|
|
RelOptInfo *
|
|
|
|
find_other_rel(Query *root, int relid)
|
|
|
|
{
|
|
|
|
List *rels;
|
|
|
|
|
|
|
|
foreach(rels, root->other_rel_list)
|
|
|
|
{
|
|
|
|
RelOptInfo *rel = (RelOptInfo *) lfirst(rels);
|
|
|
|
|
|
|
|
if (lfirsti(rel->relids) == relid)
|
|
|
|
return rel;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find_other_rel_for_join
|
|
|
|
* Look for an otherrel for a join RTE matching the given baserel set.
|
|
|
|
* Return NULL if no entry.
|
|
|
|
*/
|
|
|
|
RelOptInfo *
|
|
|
|
find_other_rel_for_join(Query *root, List *relids)
|
|
|
|
{
|
|
|
|
List *rels;
|
|
|
|
|
|
|
|
foreach(rels, root->other_rel_list)
|
|
|
|
{
|
|
|
|
RelOptInfo *rel = (RelOptInfo *) lfirst(rels);
|
|
|
|
|
|
|
|
if (rel->reloptkind == RELOPT_OTHER_JOIN_REL
|
|
|
|
&& sameseti(relids, rel->outerjoinset))
|
|
|
|
return rel;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
/*
|
|
|
|
* find_join_rel
|
|
|
|
* Returns relation entry corresponding to 'relids' (a list of RT indexes),
|
|
|
|
* or NULL if none exists. This is for join relations.
|
|
|
|
*
|
|
|
|
* Note: there is probably no good reason for this to be called from
|
2001-05-20 22:28:20 +02:00
|
|
|
* anywhere except build_join_rel, but keep it as a separate routine
|
2000-02-07 05:41:04 +01:00
|
|
|
* just in case.
|
|
|
|
*/
|
|
|
|
static RelOptInfo *
|
|
|
|
find_join_rel(Query *root, Relids relids)
|
|
|
|
{
|
|
|
|
List *joinrels;
|
|
|
|
|
|
|
|
foreach(joinrels, root->join_rel_list)
|
|
|
|
{
|
|
|
|
RelOptInfo *rel = (RelOptInfo *) lfirst(joinrels);
|
|
|
|
|
|
|
|
if (sameseti(rel->relids, relids))
|
|
|
|
return rel;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2001-05-20 22:28:20 +02:00
|
|
|
* build_join_rel
|
2000-02-07 05:41:04 +01:00
|
|
|
* Returns relation entry corresponding to the union of two given rels,
|
|
|
|
* creating a new relation entry if none already exists.
|
|
|
|
*
|
|
|
|
* 'outer_rel' and 'inner_rel' are relation nodes for the relations to be
|
|
|
|
* joined
|
2001-02-16 01:03:08 +01:00
|
|
|
* 'jointype': type of join (inner/outer)
|
2000-02-07 05:41:04 +01:00
|
|
|
* 'restrictlist_ptr': result variable. If not NULL, *restrictlist_ptr
|
|
|
|
* receives the list of RestrictInfo nodes that apply to this
|
|
|
|
* particular pair of joinable relations.
|
|
|
|
*
|
|
|
|
* restrictlist_ptr makes the routine's API a little grotty, but it saves
|
|
|
|
* duplicated calculation of the restrictlist...
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-07-18 06:22:52 +02:00
|
|
|
RelOptInfo *
|
2001-05-20 22:28:20 +02:00
|
|
|
build_join_rel(Query *root,
|
|
|
|
RelOptInfo *outer_rel,
|
|
|
|
RelOptInfo *inner_rel,
|
|
|
|
JoinType jointype,
|
|
|
|
List **restrictlist_ptr)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-02-07 05:41:04 +01:00
|
|
|
List *joinrelids;
|
|
|
|
RelOptInfo *joinrel;
|
2002-03-12 01:52:10 +01:00
|
|
|
RelOptInfo *joinrterel;
|
2000-02-07 05:41:04 +01:00
|
|
|
List *restrictlist;
|
|
|
|
List *new_outer_tlist;
|
|
|
|
List *new_inner_tlist;
|
|
|
|
|
|
|
|
/* We should never try to join two overlapping sets of rels. */
|
|
|
|
Assert(nonoverlap_setsi(outer_rel->relids, inner_rel->relids));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if we already have a joinrel for this set of base rels.
|
|
|
|
*
|
|
|
|
* nconc(listCopy(x), y) is an idiom for making a new list without
|
|
|
|
* changing either input list.
|
|
|
|
*/
|
|
|
|
joinrelids = nconc(listCopy(outer_rel->relids), inner_rel->relids);
|
|
|
|
joinrel = find_join_rel(root, joinrelids);
|
|
|
|
|
|
|
|
if (joinrel)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Yes, so we only need to figure the restrictlist for this
|
|
|
|
* particular pair of component relations.
|
|
|
|
*/
|
|
|
|
if (restrictlist_ptr)
|
2001-10-18 18:11:42 +02:00
|
|
|
*restrictlist_ptr = build_joinrel_restrictlist(root,
|
|
|
|
joinrel,
|
2000-02-07 05:41:04 +01:00
|
|
|
outer_rel,
|
2002-10-13 00:24:49 +02:00
|
|
|
inner_rel,
|
|
|
|
jointype);
|
2000-02-07 05:41:04 +01:00
|
|
|
return joinrel;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Nope, so make one.
|
|
|
|
*/
|
|
|
|
joinrel = makeNode(RelOptInfo);
|
2002-03-12 01:52:10 +01:00
|
|
|
joinrel->reloptkind = RELOPT_JOINREL;
|
2000-02-07 05:41:04 +01:00
|
|
|
joinrel->relids = joinrelids;
|
|
|
|
joinrel->rows = 0;
|
|
|
|
joinrel->width = 0;
|
|
|
|
joinrel->targetlist = NIL;
|
|
|
|
joinrel->pathlist = NIL;
|
2000-02-15 21:49:31 +01:00
|
|
|
joinrel->cheapest_startup_path = NULL;
|
|
|
|
joinrel->cheapest_total_path = NULL;
|
2000-02-07 05:41:04 +01:00
|
|
|
joinrel->pruneable = true;
|
2002-05-12 22:10:05 +02:00
|
|
|
joinrel->rtekind = RTE_JOIN;
|
2001-05-20 22:28:20 +02:00
|
|
|
joinrel->indexlist = NIL;
|
2000-02-07 05:41:04 +01:00
|
|
|
joinrel->pages = 0;
|
|
|
|
joinrel->tuples = 0;
|
2000-09-29 20:21:41 +02:00
|
|
|
joinrel->subplan = NULL;
|
2002-03-12 01:52:10 +01:00
|
|
|
joinrel->joinrti = 0;
|
|
|
|
joinrel->joinrteids = nconc(listCopy(outer_rel->joinrteids),
|
|
|
|
inner_rel->joinrteids);
|
2000-02-07 05:41:04 +01:00
|
|
|
joinrel->baserestrictinfo = NIL;
|
2003-01-12 23:35:29 +01:00
|
|
|
joinrel->baserestrictcost.startup = 0;
|
|
|
|
joinrel->baserestrictcost.per_tuple = 0;
|
2000-09-12 23:07:18 +02:00
|
|
|
joinrel->outerjoinset = NIL;
|
2000-02-07 05:41:04 +01:00
|
|
|
joinrel->joininfo = NIL;
|
2002-11-24 22:52:15 +01:00
|
|
|
joinrel->index_outer_relids = NIL;
|
|
|
|
joinrel->index_inner_paths = NIL;
|
2000-02-07 05:41:04 +01:00
|
|
|
|
2002-03-12 01:52:10 +01:00
|
|
|
/* Is there a join RTE matching this join? */
|
|
|
|
joinrterel = find_other_rel_for_join(root, joinrelids);
|
|
|
|
if (joinrterel)
|
|
|
|
{
|
|
|
|
/* Yes, remember its RT index */
|
|
|
|
joinrel->joinrti = lfirsti(joinrterel->relids);
|
|
|
|
joinrel->joinrteids = lconsi(joinrel->joinrti, joinrel->joinrteids);
|
|
|
|
}
|
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
/*
|
|
|
|
* Create a new tlist by removing irrelevant elements from both tlists
|
|
|
|
* of the outer and inner join relations and then merging the results
|
|
|
|
* together.
|
|
|
|
*
|
2002-09-04 22:31:48 +02:00
|
|
|
* XXX right now we don't remove any irrelevant elements, we just append
|
|
|
|
* the two tlists together. Someday consider pruning vars from the
|
2002-03-12 01:52:10 +01:00
|
|
|
* join's targetlist if they are needed only to evaluate restriction
|
2002-09-04 22:31:48 +02:00
|
|
|
* clauses of this join, and will never be accessed at higher levels
|
|
|
|
* of the plantree.
|
2002-03-12 01:52:10 +01:00
|
|
|
*
|
2000-04-12 19:17:23 +02:00
|
|
|
* NOTE: the tlist order for a join rel will depend on which pair of
|
|
|
|
* outer and inner rels we first try to build it from. But the
|
2000-02-07 05:41:04 +01:00
|
|
|
* contents should be the same regardless.
|
|
|
|
*/
|
|
|
|
new_outer_tlist = new_join_tlist(outer_rel->targetlist, 1);
|
|
|
|
new_inner_tlist = new_join_tlist(inner_rel->targetlist,
|
|
|
|
length(new_outer_tlist) + 1);
|
|
|
|
joinrel->targetlist = nconc(new_outer_tlist, new_inner_tlist);
|
|
|
|
|
2002-03-12 01:52:10 +01:00
|
|
|
/*
|
|
|
|
* If there are any alias variables attached to the matching join RTE,
|
2002-09-04 22:31:48 +02:00
|
|
|
* attach them to the tlist too, so that they will be evaluated for
|
|
|
|
* use at higher plan levels.
|
2002-03-12 01:52:10 +01:00
|
|
|
*/
|
|
|
|
if (joinrterel)
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
List *jrtetl;
|
2002-03-12 01:52:10 +01:00
|
|
|
|
|
|
|
foreach(jrtetl, joinrterel->targetlist)
|
|
|
|
{
|
|
|
|
TargetEntry *jrtete = lfirst(jrtetl);
|
|
|
|
|
|
|
|
add_var_to_tlist(joinrel, (Var *) jrtete->expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* Construct restrict and join clause lists for the new joinrel. (The
|
|
|
|
* caller might or might not need the restrictlist, but I need it
|
|
|
|
* anyway for set_joinrel_size_estimates().)
|
2000-02-07 05:41:04 +01:00
|
|
|
*/
|
2001-10-18 18:11:42 +02:00
|
|
|
restrictlist = build_joinrel_restrictlist(root,
|
|
|
|
joinrel,
|
|
|
|
outer_rel,
|
2002-10-13 00:24:49 +02:00
|
|
|
inner_rel,
|
|
|
|
jointype);
|
2000-02-07 05:41:04 +01:00
|
|
|
if (restrictlist_ptr)
|
|
|
|
*restrictlist_ptr = restrictlist;
|
|
|
|
build_joinrel_joinlist(joinrel, outer_rel, inner_rel);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set estimates of the joinrel's size.
|
|
|
|
*/
|
|
|
|
set_joinrel_size_estimates(root, joinrel, outer_rel, inner_rel,
|
2001-02-16 01:03:08 +01:00
|
|
|
jointype, restrictlist);
|
2000-02-07 05:41:04 +01:00
|
|
|
|
|
|
|
/*
|
2000-09-12 23:07:18 +02:00
|
|
|
* Add the joinrel to the query's joinrel list.
|
2000-02-07 05:41:04 +01:00
|
|
|
*/
|
|
|
|
root->join_rel_list = lcons(joinrel, root->join_rel_list);
|
|
|
|
|
|
|
|
return joinrel;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2000-02-07 05:41:04 +01:00
|
|
|
* new_join_tlist
|
|
|
|
* Builds a join relation's target list by keeping those elements that
|
|
|
|
* will be in the final target list and any other elements that are still
|
|
|
|
* needed for future joins. For a target list entry to still be needed
|
|
|
|
* for future joins, its 'joinlist' field must not be empty after removal
|
|
|
|
* of all relids in 'other_relids'.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2000-02-07 05:41:04 +01:00
|
|
|
* XXX the above comment refers to code that is long dead and gone;
|
|
|
|
* we don't keep track of joinlists for individual targetlist entries
|
2000-04-12 19:17:23 +02:00
|
|
|
* anymore. For now, all vars present in either input tlist will be
|
2000-02-07 05:41:04 +01:00
|
|
|
* emitted in the join's tlist.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2000-02-07 05:41:04 +01:00
|
|
|
* 'tlist' is the target list of one of the join relations
|
|
|
|
* 'first_resdomno' is the resdom number to use for the first created
|
|
|
|
* target list entry
|
|
|
|
*
|
|
|
|
* Returns the new target list.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-02-07 05:41:04 +01:00
|
|
|
static List *
|
|
|
|
new_join_tlist(List *tlist,
|
|
|
|
int first_resdomno)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-02-07 05:41:04 +01:00
|
|
|
int resdomno = first_resdomno - 1;
|
|
|
|
List *t_list = NIL;
|
|
|
|
List *i;
|
1999-08-16 04:17:58 +02:00
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
foreach(i, tlist)
|
2000-02-06 04:27:35 +01:00
|
|
|
{
|
2000-02-07 05:41:04 +01:00
|
|
|
TargetEntry *xtl = lfirst(i);
|
1999-08-16 04:17:58 +02:00
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
resdomno += 1;
|
|
|
|
t_list = lappend(t_list,
|
|
|
|
create_tl_element(get_expr(xtl), resdomno));
|
|
|
|
}
|
|
|
|
|
|
|
|
return t_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* build_joinrel_restrictlist
|
|
|
|
* build_joinrel_joinlist
|
|
|
|
* These routines build lists of restriction and join clauses for a
|
|
|
|
* join relation from the joininfo lists of the relations it joins.
|
|
|
|
*
|
|
|
|
* These routines are separate because the restriction list must be
|
|
|
|
* built afresh for each pair of input sub-relations we consider, whereas
|
|
|
|
* the join lists need only be computed once for any join RelOptInfo.
|
|
|
|
* The join lists are fully determined by the set of rels making up the
|
|
|
|
* joinrel, so we should get the same results (up to ordering) from any
|
2000-04-12 19:17:23 +02:00
|
|
|
* candidate pair of sub-relations. But the restriction list is whatever
|
2000-02-07 05:41:04 +01:00
|
|
|
* is not handled in the sub-relations, so it depends on which
|
|
|
|
* sub-relations are considered.
|
|
|
|
*
|
|
|
|
* If a join clause from an input relation refers to base rels still not
|
|
|
|
* present in the joinrel, then it is still a join clause for the joinrel;
|
2000-04-12 19:17:23 +02:00
|
|
|
* we put it into an appropriate JoinInfo list for the joinrel. Otherwise,
|
2000-02-07 05:41:04 +01:00
|
|
|
* the clause is now a restrict clause for the joined relation, and we
|
|
|
|
* return it to the caller of build_joinrel_restrictlist() to be stored in
|
2000-04-12 19:17:23 +02:00
|
|
|
* join paths made from this pair of sub-relations. (It will not need to
|
2000-02-07 05:41:04 +01:00
|
|
|
* be considered further up the join tree.)
|
|
|
|
*
|
2001-10-18 18:11:42 +02:00
|
|
|
* When building a restriction list, we eliminate redundant clauses.
|
|
|
|
* We don't try to do that for join clause lists, since the join clauses
|
|
|
|
* aren't really doing anything, just waiting to become part of higher
|
|
|
|
* levels' restriction lists.
|
|
|
|
*
|
2000-02-07 05:41:04 +01:00
|
|
|
* 'joinrel' is a join relation node
|
|
|
|
* 'outer_rel' and 'inner_rel' are a pair of relations that can be joined
|
|
|
|
* to form joinrel.
|
2002-10-13 00:24:49 +02:00
|
|
|
* 'jointype' is the type of join used.
|
2000-02-07 05:41:04 +01:00
|
|
|
*
|
|
|
|
* build_joinrel_restrictlist() returns a list of relevant restrictinfos,
|
|
|
|
* whereas build_joinrel_joinlist() stores its results in the joinrel's
|
2000-04-12 19:17:23 +02:00
|
|
|
* joininfo lists. One or the other must accept each given clause!
|
2000-02-07 05:41:04 +01:00
|
|
|
*
|
|
|
|
* NB: Formerly, we made deep(!) copies of each input RestrictInfo to pass
|
|
|
|
* up to the join relation. I believe this is no longer necessary, because
|
2000-04-12 19:17:23 +02:00
|
|
|
* RestrictInfo nodes are no longer context-dependent. Instead, just include
|
2000-02-07 05:41:04 +01:00
|
|
|
* the original nodes in the lists made for the join relation.
|
|
|
|
*/
|
|
|
|
static List *
|
2001-10-18 18:11:42 +02:00
|
|
|
build_joinrel_restrictlist(Query *root,
|
|
|
|
RelOptInfo *joinrel,
|
2000-02-07 05:41:04 +01:00
|
|
|
RelOptInfo *outer_rel,
|
2002-10-13 00:24:49 +02:00
|
|
|
RelOptInfo *inner_rel,
|
|
|
|
JoinType jointype)
|
2000-02-07 05:41:04 +01:00
|
|
|
{
|
2002-11-24 22:52:15 +01:00
|
|
|
List *result;
|
2001-10-25 07:50:21 +02:00
|
|
|
List *rlist;
|
2001-10-18 18:11:42 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Collect all the clauses that syntactically belong at this level.
|
|
|
|
*/
|
|
|
|
rlist = nconc(subbuild_joinrel_restrictlist(joinrel,
|
|
|
|
outer_rel->joininfo),
|
|
|
|
subbuild_joinrel_restrictlist(joinrel,
|
|
|
|
inner_rel->joininfo));
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
/*
|
2001-10-18 18:11:42 +02:00
|
|
|
* Eliminate duplicate and redundant clauses.
|
|
|
|
*
|
|
|
|
* We must eliminate duplicates, since we will see many of the same
|
2001-10-25 07:50:21 +02:00
|
|
|
* clauses arriving from both input relations. Also, if a clause is a
|
|
|
|
* mergejoinable clause, it's possible that it is redundant with
|
|
|
|
* previous clauses (see optimizer/README for discussion). We detect
|
2001-10-18 18:11:42 +02:00
|
|
|
* that case and omit the redundant clause from the result list.
|
2000-02-07 05:41:04 +01:00
|
|
|
*/
|
2002-11-24 22:52:15 +01:00
|
|
|
result = remove_redundant_join_clauses(root, rlist, jointype);
|
2001-10-18 18:11:42 +02:00
|
|
|
|
|
|
|
freeList(rlist);
|
|
|
|
|
|
|
|
return result;
|
2000-02-07 05:41:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
build_joinrel_joinlist(RelOptInfo *joinrel,
|
|
|
|
RelOptInfo *outer_rel,
|
|
|
|
RelOptInfo *inner_rel)
|
|
|
|
{
|
|
|
|
subbuild_joinrel_joinlist(joinrel, outer_rel->joininfo);
|
|
|
|
subbuild_joinrel_joinlist(joinrel, inner_rel->joininfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static List *
|
|
|
|
subbuild_joinrel_restrictlist(RelOptInfo *joinrel,
|
|
|
|
List *joininfo_list)
|
|
|
|
{
|
|
|
|
List *restrictlist = NIL;
|
|
|
|
List *xjoininfo;
|
|
|
|
|
|
|
|
foreach(xjoininfo, joininfo_list)
|
|
|
|
{
|
|
|
|
JoinInfo *joininfo = (JoinInfo *) lfirst(xjoininfo);
|
|
|
|
|
2000-02-19 00:47:31 +01:00
|
|
|
if (is_subseti(joininfo->unjoined_relids, joinrel->relids))
|
2000-02-07 05:41:04 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Clauses in this JoinInfo list become restriction clauses
|
|
|
|
* for the joinrel, since they refer to no outside rels.
|
|
|
|
*
|
|
|
|
* We must copy the list to avoid disturbing the input relation,
|
|
|
|
* but we can use a shallow copy.
|
|
|
|
*/
|
|
|
|
restrictlist = nconc(restrictlist,
|
|
|
|
listCopy(joininfo->jinfo_restrictinfo));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* These clauses are still join clauses at this level, so we
|
|
|
|
* ignore them in this routine.
|
2000-02-07 05:41:04 +01:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return restrictlist;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
subbuild_joinrel_joinlist(RelOptInfo *joinrel,
|
|
|
|
List *joininfo_list)
|
|
|
|
{
|
|
|
|
List *xjoininfo;
|
|
|
|
|
|
|
|
foreach(xjoininfo, joininfo_list)
|
|
|
|
{
|
|
|
|
JoinInfo *joininfo = (JoinInfo *) lfirst(xjoininfo);
|
|
|
|
Relids new_unjoined_relids;
|
|
|
|
|
|
|
|
new_unjoined_relids = set_differencei(joininfo->unjoined_relids,
|
|
|
|
joinrel->relids);
|
|
|
|
if (new_unjoined_relids == NIL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Clauses in this JoinInfo list become restriction clauses
|
2000-04-12 19:17:23 +02:00
|
|
|
* for the joinrel, since they refer to no outside rels. So we
|
|
|
|
* can ignore them in this routine.
|
2000-02-07 05:41:04 +01:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* These clauses are still join clauses at this level, so find
|
|
|
|
* or make the appropriate JoinInfo item for the joinrel, and
|
|
|
|
* add the clauses to it (eliminating duplicates).
|
2000-02-07 05:41:04 +01:00
|
|
|
*/
|
|
|
|
JoinInfo *new_joininfo;
|
|
|
|
|
|
|
|
new_joininfo = find_joininfo_node(joinrel, new_unjoined_relids);
|
|
|
|
new_joininfo->jinfo_restrictinfo =
|
2000-09-29 20:21:41 +02:00
|
|
|
set_union(new_joininfo->jinfo_restrictinfo,
|
2000-02-07 05:41:04 +01:00
|
|
|
joininfo->jinfo_restrictinfo);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|