1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-08-16 04:17:58 +02:00
|
|
|
* pathkeys.c
|
|
|
|
* Utilities for matching and building path keys
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-07-24 05:11:01 +02:00
|
|
|
* See src/backend/optimizer/README for a great deal of information about
|
|
|
|
* the nature and use of path keys.
|
|
|
|
*
|
|
|
|
*
|
2001-01-24 20:43:33 +01:00
|
|
|
* Portions Copyright (c) 1996-2001, 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
|
2002-03-12 01:52:10 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/pathkeys.c,v 1.37 2002/03/12 00:51:44 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
#include "nodes/makefuncs.h"
|
|
|
|
#include "optimizer/clauses.h"
|
2000-02-15 21:49:31 +01:00
|
|
|
#include "optimizer/pathnode.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "optimizer/paths.h"
|
2000-07-24 05:11:01 +02:00
|
|
|
#include "optimizer/planmain.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "optimizer/tlist.h"
|
1999-08-16 04:17:58 +02:00
|
|
|
#include "parser/parsetree.h"
|
1999-08-21 05:49:17 +02:00
|
|
|
#include "parser/parse_func.h"
|
1999-08-16 04:17:58 +02:00
|
|
|
#include "utils/lsyscache.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-07-24 05:11:01 +02:00
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
static PathKeyItem *makePathKeyItem(Node *key, Oid sortop);
|
2000-02-15 21:49:31 +01:00
|
|
|
static List *make_canonical_pathkey(Query *root, PathKeyItem *item);
|
|
|
|
static Var *find_indexkey_var(Query *root, RelOptInfo *rel,
|
2001-03-22 05:01:46 +01:00
|
|
|
AttrNumber varattno);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* makePathKeyItem
|
|
|
|
* create a PathKeyItem node
|
|
|
|
*/
|
|
|
|
static PathKeyItem *
|
|
|
|
makePathKeyItem(Node *key, Oid sortop)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
PathKeyItem *item = makeNode(PathKeyItem);
|
1999-08-16 04:17:58 +02:00
|
|
|
|
|
|
|
item->key = key;
|
|
|
|
item->sortop = sortop;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
/*
|
|
|
|
* add_equijoined_keys
|
|
|
|
* The given clause has a mergejoinable operator, so its two sides
|
|
|
|
* can be considered equal after restriction clause application; in
|
|
|
|
* particular, any pathkey mentioning one side (with the correct sortop)
|
|
|
|
* can be expanded to include the other as well. Record the vars and
|
|
|
|
* associated sortops in the query's equi_key_list for future use.
|
|
|
|
*
|
|
|
|
* The query's equi_key_list field points to a list of sublists of PathKeyItem
|
|
|
|
* nodes, where each sublist is a set of two or more vars+sortops that have
|
|
|
|
* been identified as logically equivalent (and, therefore, we may consider
|
|
|
|
* any two in a set to be equal). As described above, we will subsequently
|
|
|
|
* use direct pointers to one of these sublists to represent any pathkey
|
|
|
|
* that involves an equijoined variable.
|
|
|
|
*
|
|
|
|
* This code would actually work fine with expressions more complex than
|
|
|
|
* a single Var, but currently it won't see any because check_mergejoinable
|
|
|
|
* won't accept such clauses as mergejoinable.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
add_equijoined_keys(Query *root, RestrictInfo *restrictinfo)
|
|
|
|
{
|
|
|
|
Expr *clause = restrictinfo->clause;
|
|
|
|
PathKeyItem *item1 = makePathKeyItem((Node *) get_leftop(clause),
|
|
|
|
restrictinfo->left_sortop);
|
|
|
|
PathKeyItem *item2 = makePathKeyItem((Node *) get_rightop(clause),
|
|
|
|
restrictinfo->right_sortop);
|
|
|
|
List *newset,
|
|
|
|
*cursetlink;
|
|
|
|
|
|
|
|
/* We might see a clause X=X; don't make a single-element list from it */
|
|
|
|
if (equal(item1, item2))
|
|
|
|
return;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* Our plan is to make a two-element set, then sweep through the
|
|
|
|
* existing equijoin sets looking for matches to item1 or item2. When
|
|
|
|
* we find one, we remove that set from equi_key_list and union it
|
|
|
|
* into our new set. When done, we add the new set to the front of
|
|
|
|
* equi_key_list.
|
2000-02-15 21:49:31 +01:00
|
|
|
*
|
2001-03-22 05:01:46 +01:00
|
|
|
* It may well be that the two items we're given are already known to be
|
|
|
|
* equijoin-equivalent, in which case we don't need to change our data
|
|
|
|
* structure. If we find both of them in the same equivalence set to
|
|
|
|
* start with, we can quit immediately.
|
2000-07-24 05:11:01 +02:00
|
|
|
*
|
2000-02-15 21:49:31 +01:00
|
|
|
* This is a standard UNION-FIND problem, for which there exist better
|
|
|
|
* data structures than simple lists. If this code ever proves to be
|
|
|
|
* a bottleneck then it could be sped up --- but for now, simple is
|
|
|
|
* beautiful.
|
|
|
|
*/
|
2000-07-24 05:11:01 +02:00
|
|
|
newset = NIL;
|
2000-02-15 21:49:31 +01:00
|
|
|
|
|
|
|
foreach(cursetlink, root->equi_key_list)
|
|
|
|
{
|
|
|
|
List *curset = lfirst(cursetlink);
|
2000-07-24 05:11:01 +02:00
|
|
|
bool item1here = member(item1, curset);
|
|
|
|
bool item2here = member(item2, curset);
|
2000-02-15 21:49:31 +01:00
|
|
|
|
2000-07-24 05:11:01 +02:00
|
|
|
if (item1here || item2here)
|
2000-02-15 21:49:31 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
/*
|
|
|
|
* If find both in same equivalence set, no need to do any
|
|
|
|
* more
|
|
|
|
*/
|
2000-07-24 05:11:01 +02:00
|
|
|
if (item1here && item2here)
|
|
|
|
{
|
|
|
|
/* Better not have seen only one in an earlier set... */
|
|
|
|
Assert(newset == NIL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Build the new set only when we know we must */
|
|
|
|
if (newset == NIL)
|
2001-01-17 18:26:45 +01:00
|
|
|
newset = makeList2(item1, item2);
|
2000-07-24 05:11:01 +02:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
/* Found a set to merge into our new set */
|
2000-09-29 20:21:41 +02:00
|
|
|
newset = set_union(newset, curset);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove old set from equi_key_list. NOTE this does not
|
2001-03-22 05:01:46 +01:00
|
|
|
* change lnext(cursetlink), so the foreach loop doesn't
|
|
|
|
* break.
|
2000-02-15 21:49:31 +01:00
|
|
|
*/
|
|
|
|
root->equi_key_list = lremove(curset, root->equi_key_list);
|
|
|
|
freeList(curset); /* might as well recycle old cons cells */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-24 05:11:01 +02:00
|
|
|
/* Build the new set only when we know we must */
|
|
|
|
if (newset == NIL)
|
2001-01-17 18:26:45 +01:00
|
|
|
newset = makeList2(item1, item2);
|
2000-07-24 05:11:01 +02:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
root->equi_key_list = lcons(newset, root->equi_key_list);
|
|
|
|
}
|
|
|
|
|
2000-07-24 05:11:01 +02:00
|
|
|
/*
|
|
|
|
* generate_implied_equalities
|
|
|
|
* Scan the completed equi_key_list for the query, and generate explicit
|
|
|
|
* qualifications (WHERE clauses) for all the pairwise equalities not
|
|
|
|
* already mentioned in the quals. This is useful because the additional
|
|
|
|
* clauses help the selectivity-estimation code, and in fact it's
|
|
|
|
* *necessary* to ensure that sort keys we think are equivalent really
|
|
|
|
* are (see src/backend/optimizer/README for more info).
|
|
|
|
*
|
|
|
|
* This routine just walks the equi_key_list to find all pairwise equalities.
|
|
|
|
* We call process_implied_equality (in plan/initsplan.c) to determine whether
|
|
|
|
* each is already known and add it to the proper restrictinfo list if not.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
generate_implied_equalities(Query *root)
|
|
|
|
{
|
|
|
|
List *cursetlink;
|
|
|
|
|
|
|
|
foreach(cursetlink, root->equi_key_list)
|
|
|
|
{
|
|
|
|
List *curset = lfirst(cursetlink);
|
|
|
|
List *ptr1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A set containing only two items cannot imply any equalities
|
|
|
|
* beyond the one that created the set, so we can skip it.
|
|
|
|
*/
|
|
|
|
if (length(curset) < 3)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* Match each item in the set with all that appear after it (it's
|
|
|
|
* sufficient to generate A=B, need not process B=A too).
|
2000-07-24 05:11:01 +02:00
|
|
|
*/
|
|
|
|
foreach(ptr1, curset)
|
|
|
|
{
|
|
|
|
PathKeyItem *item1 = (PathKeyItem *) lfirst(ptr1);
|
|
|
|
List *ptr2;
|
|
|
|
|
|
|
|
foreach(ptr2, lnext(ptr1))
|
|
|
|
{
|
|
|
|
PathKeyItem *item2 = (PathKeyItem *) lfirst(ptr2);
|
|
|
|
|
|
|
|
process_implied_equality(root, item1->key, item2->key,
|
|
|
|
item1->sortop, item2->sortop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
/*
|
|
|
|
* make_canonical_pathkey
|
|
|
|
* Given a PathKeyItem, find the equi_key_list subset it is a member of,
|
|
|
|
* if any. If so, return a pointer to that sublist, which is the
|
|
|
|
* canonical representation (for this query) of that PathKeyItem's
|
2000-12-14 23:30:45 +01:00
|
|
|
* equivalence set. If it is not found, add a singleton "equivalence set"
|
|
|
|
* to the equi_key_list and return that --- see compare_pathkeys.
|
2000-02-15 21:49:31 +01:00
|
|
|
*
|
|
|
|
* Note that this function must not be used until after we have completed
|
|
|
|
* scanning the WHERE clause for equijoin operators.
|
|
|
|
*/
|
|
|
|
static List *
|
|
|
|
make_canonical_pathkey(Query *root, PathKeyItem *item)
|
|
|
|
{
|
|
|
|
List *cursetlink;
|
2000-12-14 23:30:45 +01:00
|
|
|
List *newset;
|
2000-02-15 21:49:31 +01:00
|
|
|
|
|
|
|
foreach(cursetlink, root->equi_key_list)
|
|
|
|
{
|
|
|
|
List *curset = lfirst(cursetlink);
|
|
|
|
|
|
|
|
if (member(item, curset))
|
|
|
|
return curset;
|
|
|
|
}
|
2000-12-14 23:30:45 +01:00
|
|
|
newset = makeList1(item);
|
|
|
|
root->equi_key_list = lcons(newset, root->equi_key_list);
|
|
|
|
return newset;
|
2000-02-15 21:49:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* canonicalize_pathkeys
|
|
|
|
* Convert a not-necessarily-canonical pathkeys list to canonical form.
|
|
|
|
*
|
|
|
|
* Note that this function must not be used until after we have completed
|
|
|
|
* scanning the WHERE clause for equijoin operators.
|
|
|
|
*/
|
|
|
|
List *
|
|
|
|
canonicalize_pathkeys(Query *root, List *pathkeys)
|
|
|
|
{
|
|
|
|
List *new_pathkeys = NIL;
|
|
|
|
List *i;
|
|
|
|
|
|
|
|
foreach(i, pathkeys)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
List *pathkey = (List *) lfirst(i);
|
|
|
|
PathKeyItem *item;
|
2000-12-14 23:30:45 +01:00
|
|
|
List *cpathkey;
|
2000-02-15 21:49:31 +01:00
|
|
|
|
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* It's sufficient to look at the first entry in the sublist; if
|
|
|
|
* there are more entries, they're already part of an equivalence
|
|
|
|
* set by definition.
|
2000-02-15 21:49:31 +01:00
|
|
|
*/
|
|
|
|
Assert(pathkey != NIL);
|
|
|
|
item = (PathKeyItem *) lfirst(pathkey);
|
2000-12-14 23:30:45 +01:00
|
|
|
cpathkey = make_canonical_pathkey(root, item);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-12-14 23:30:45 +01:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* Eliminate redundant ordering requests --- ORDER BY A,A is the
|
|
|
|
* same as ORDER BY A. We want to check this only after we have
|
|
|
|
* canonicalized the keys, so that equivalent-key knowledge is
|
|
|
|
* used when deciding if an item is redundant.
|
2000-12-14 23:30:45 +01:00
|
|
|
*/
|
|
|
|
if (!ptrMember(cpathkey, new_pathkeys))
|
|
|
|
new_pathkeys = lappend(new_pathkeys, cpathkey);
|
2000-02-15 21:49:31 +01:00
|
|
|
}
|
|
|
|
return new_pathkeys;
|
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/****************************************************************************
|
1999-08-16 04:17:58 +02:00
|
|
|
* PATHKEY COMPARISONS
|
1996-07-09 08:22:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
/*
|
|
|
|
* compare_pathkeys
|
|
|
|
* Compare two pathkeys to see if they are equivalent, and if not whether
|
|
|
|
* one is "better" than the other.
|
|
|
|
*
|
2000-12-14 23:30:45 +01:00
|
|
|
* This function may only be applied to canonicalized pathkey lists.
|
|
|
|
* In the canonical representation, sublists can be checked for equality
|
|
|
|
* by simple pointer comparison.
|
1999-08-16 04:17:58 +02:00
|
|
|
*/
|
|
|
|
PathKeysComparison
|
|
|
|
compare_pathkeys(List *keys1, List *keys2)
|
|
|
|
{
|
|
|
|
List *key1,
|
|
|
|
*key2;
|
|
|
|
|
|
|
|
for (key1 = keys1, key2 = keys2;
|
|
|
|
key1 != NIL && key2 != NIL;
|
|
|
|
key1 = lnext(key1), key2 = lnext(key2))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-08-16 04:17:58 +02:00
|
|
|
List *subkey1 = lfirst(key1);
|
|
|
|
List *subkey2 = lfirst(key2);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-12-14 23:30:45 +01:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* XXX would like to check that we've been given canonicalized
|
|
|
|
* input, but query root not accessible here...
|
2000-12-14 23:30:45 +01:00
|
|
|
*/
|
|
|
|
#ifdef NOT_USED
|
|
|
|
Assert(ptrMember(subkey1, root->equi_key_list));
|
|
|
|
Assert(ptrMember(subkey2, root->equi_key_list));
|
|
|
|
#endif
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
|
|
|
* We will never have two subkeys where one is a subset of the
|
2001-03-22 05:01:46 +01:00
|
|
|
* other, because of the canonicalization process. Either they
|
2000-12-14 23:30:45 +01:00
|
|
|
* are equal or they ain't. Furthermore, we only need pointer
|
|
|
|
* comparison to detect equality.
|
1999-08-16 04:17:58 +02:00
|
|
|
*/
|
2000-12-14 23:30:45 +01:00
|
|
|
if (subkey1 != subkey2)
|
|
|
|
return PATHKEYS_DIFFERENT; /* no need to keep looking */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we reached the end of only one list, the other is longer and
|
|
|
|
* therefore not a subset. (We assume the additional sublist(s) of
|
|
|
|
* the other list are not NIL --- no pathkey list should ever have a
|
|
|
|
* NIL sublist.)
|
|
|
|
*/
|
|
|
|
if (key1 == NIL && key2 == NIL)
|
|
|
|
return PATHKEYS_EQUAL;
|
|
|
|
if (key1 != NIL)
|
2001-10-28 07:26:15 +01:00
|
|
|
return PATHKEYS_BETTER1; /* key1 is longer */
|
2000-12-14 23:30:45 +01:00
|
|
|
return PATHKEYS_BETTER2; /* key2 is longer */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* compare_noncanonical_pathkeys
|
|
|
|
* Compare two pathkeys to see if they are equivalent, and if not whether
|
|
|
|
* one is "better" than the other. This is used when we must compare
|
|
|
|
* non-canonicalized pathkeys.
|
|
|
|
*
|
|
|
|
* A pathkey can be considered better than another if it is a superset:
|
|
|
|
* it contains all the keys of the other plus more. For example, either
|
|
|
|
* ((A) (B)) or ((A B)) is better than ((A)).
|
|
|
|
*
|
|
|
|
* Currently, the only user of this routine is grouping_planner(),
|
|
|
|
* and it will only pass single-element sublists (from
|
|
|
|
* make_pathkeys_for_sortclauses). Therefore we don't have to do the
|
|
|
|
* full two-way-subset-inclusion test on each pair of sublists that is
|
|
|
|
* implied by the above statement. Instead we just verify they are
|
|
|
|
* singleton lists and then do an equal(). This could be improved if
|
|
|
|
* necessary.
|
|
|
|
*/
|
|
|
|
PathKeysComparison
|
|
|
|
compare_noncanonical_pathkeys(List *keys1, List *keys2)
|
|
|
|
{
|
|
|
|
List *key1,
|
|
|
|
*key2;
|
|
|
|
|
|
|
|
for (key1 = keys1, key2 = keys2;
|
|
|
|
key1 != NIL && key2 != NIL;
|
|
|
|
key1 = lnext(key1), key2 = lnext(key2))
|
|
|
|
{
|
|
|
|
List *subkey1 = lfirst(key1);
|
|
|
|
List *subkey2 = lfirst(key2);
|
|
|
|
|
|
|
|
Assert(length(subkey1) == 1);
|
|
|
|
Assert(length(subkey2) == 1);
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!equal(subkey1, subkey2))
|
|
|
|
return PATHKEYS_DIFFERENT; /* no need to keep looking */
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
|
|
|
* If we reached the end of only one list, the other is longer and
|
|
|
|
* therefore not a subset. (We assume the additional sublist(s) of
|
|
|
|
* the other list are not NIL --- no pathkey list should ever have a
|
|
|
|
* NIL sublist.)
|
1999-08-16 04:17:58 +02:00
|
|
|
*/
|
2000-02-15 21:49:31 +01:00
|
|
|
if (key1 == NIL && key2 == NIL)
|
1999-08-16 04:17:58 +02:00
|
|
|
return PATHKEYS_EQUAL;
|
2000-02-15 21:49:31 +01:00
|
|
|
if (key1 != NIL)
|
2001-10-28 07:26:15 +01:00
|
|
|
return PATHKEYS_BETTER1; /* key1 is longer */
|
2000-02-15 21:49:31 +01:00
|
|
|
return PATHKEYS_BETTER2; /* key2 is longer */
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-08-16 04:17:58 +02:00
|
|
|
* pathkeys_contained_in
|
|
|
|
* Common special case of compare_pathkeys: we just want to know
|
|
|
|
* if keys2 are at least as well sorted as keys1.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1999-08-16 04:17:58 +02:00
|
|
|
bool
|
|
|
|
pathkeys_contained_in(List *keys1, List *keys2)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-08-16 04:17:58 +02:00
|
|
|
switch (compare_pathkeys(keys1, keys2))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
case PATHKEYS_EQUAL:
|
|
|
|
case PATHKEYS_BETTER2:
|
1999-08-16 04:17:58 +02:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1999-08-16 04:17:58 +02:00
|
|
|
return false;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-12-14 23:30:45 +01:00
|
|
|
/*
|
|
|
|
* noncanonical_pathkeys_contained_in
|
|
|
|
* The same, when we don't have canonical pathkeys.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
noncanonical_pathkeys_contained_in(List *keys1, List *keys2)
|
|
|
|
{
|
|
|
|
switch (compare_noncanonical_pathkeys(keys1, keys2))
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
case PATHKEYS_EQUAL:
|
|
|
|
case PATHKEYS_BETTER2:
|
2000-12-14 23:30:45 +01:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-08-16 04:17:58 +02:00
|
|
|
* get_cheapest_path_for_pathkeys
|
2000-02-15 21:49:31 +01:00
|
|
|
* Find the cheapest path (according to the specified criterion) that
|
|
|
|
* satisfies the given pathkeys. Return NULL if no such path.
|
1999-08-16 04:17:58 +02:00
|
|
|
*
|
2000-02-15 21:49:31 +01:00
|
|
|
* 'paths' is a list of possible paths that all generate the same relation
|
|
|
|
* 'pathkeys' represents a required ordering (already canonicalized!)
|
|
|
|
* 'cost_criterion' is STARTUP_COST or TOTAL_COST
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
Path *
|
1999-08-21 05:49:17 +02:00
|
|
|
get_cheapest_path_for_pathkeys(List *paths, List *pathkeys,
|
2000-02-15 21:49:31 +01:00
|
|
|
CostSelector cost_criterion)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Path *matched_path = NULL;
|
1999-05-17 02:26:33 +02:00
|
|
|
List *i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(i, paths)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Path *path = (Path *) lfirst(i);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
/*
|
|
|
|
* Since cost comparison is a lot cheaper than pathkey comparison,
|
|
|
|
* do that first. (XXX is that still true?)
|
|
|
|
*/
|
|
|
|
if (matched_path != NULL &&
|
|
|
|
compare_path_costs(matched_path, path, cost_criterion) <= 0)
|
1999-08-21 05:49:17 +02:00
|
|
|
continue;
|
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
if (pathkeys_contained_in(pathkeys, path->pathkeys))
|
2000-02-15 21:49:31 +01:00
|
|
|
matched_path = path;
|
|
|
|
}
|
|
|
|
return matched_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_cheapest_fractional_path_for_pathkeys
|
|
|
|
* Find the cheapest path (for retrieving a specified fraction of all
|
|
|
|
* the tuples) that satisfies the given pathkeys.
|
|
|
|
* Return NULL if no such path.
|
|
|
|
*
|
|
|
|
* See compare_fractional_path_costs() for the interpretation of the fraction
|
|
|
|
* parameter.
|
|
|
|
*
|
|
|
|
* 'paths' is a list of possible paths that all generate the same relation
|
|
|
|
* 'pathkeys' represents a required ordering (already canonicalized!)
|
|
|
|
* 'fraction' is the fraction of the total tuples expected to be retrieved
|
|
|
|
*/
|
|
|
|
Path *
|
|
|
|
get_cheapest_fractional_path_for_pathkeys(List *paths,
|
|
|
|
List *pathkeys,
|
|
|
|
double fraction)
|
|
|
|
{
|
|
|
|
Path *matched_path = NULL;
|
|
|
|
List *i;
|
|
|
|
|
|
|
|
foreach(i, paths)
|
|
|
|
{
|
|
|
|
Path *path = (Path *) lfirst(i);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since cost comparison is a lot cheaper than pathkey comparison,
|
|
|
|
* do that first.
|
|
|
|
*/
|
|
|
|
if (matched_path != NULL &&
|
2000-04-12 19:17:23 +02:00
|
|
|
compare_fractional_path_costs(matched_path, path, fraction) <= 0)
|
2000-02-15 21:49:31 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (pathkeys_contained_in(pathkeys, path->pathkeys))
|
|
|
|
matched_path = path;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
return matched_path;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* NEW PATHKEY FORMATION
|
|
|
|
****************************************************************************/
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-08-16 04:17:58 +02:00
|
|
|
* build_index_pathkeys
|
|
|
|
* Build a pathkeys list that describes the ordering induced by an index
|
|
|
|
* scan using the given index. (Note that an unordered index doesn't
|
|
|
|
* induce any ordering; such an index will have no sortop OIDS in
|
2000-02-15 21:49:31 +01:00
|
|
|
* its "ordering" field, and we will return NIL.)
|
1999-08-16 04:17:58 +02:00
|
|
|
*
|
2000-02-15 21:49:31 +01:00
|
|
|
* If 'scandir' is BackwardScanDirection, attempt to build pathkeys
|
2000-04-12 19:17:23 +02:00
|
|
|
* representing a backwards scan of the index. Return NIL if can't do it.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
List *
|
2000-02-15 21:49:31 +01:00
|
|
|
build_index_pathkeys(Query *root,
|
|
|
|
RelOptInfo *rel,
|
|
|
|
IndexOptInfo *index,
|
|
|
|
ScanDirection scandir)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-08-16 04:17:58 +02:00
|
|
|
List *retval = NIL;
|
|
|
|
int *indexkeys = index->indexkeys;
|
|
|
|
Oid *ordering = index->ordering;
|
2000-02-15 21:49:31 +01:00
|
|
|
PathKeyItem *item;
|
|
|
|
Oid sortop;
|
1999-08-16 04:17:58 +02:00
|
|
|
|
|
|
|
if (!indexkeys || indexkeys[0] == 0 ||
|
|
|
|
!ordering || ordering[0] == InvalidOid)
|
|
|
|
return NIL; /* unordered index? */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
if (index->indproc)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-08-16 04:17:58 +02:00
|
|
|
/* Functional index: build a representation of the function call */
|
|
|
|
Func *funcnode = makeNode(Func);
|
|
|
|
List *funcargs = NIL;
|
|
|
|
|
|
|
|
funcnode->funcid = index->indproc;
|
|
|
|
funcnode->functype = get_func_rettype(index->indproc);
|
|
|
|
funcnode->func_fcache = NULL;
|
|
|
|
|
|
|
|
while (*indexkeys != 0)
|
|
|
|
{
|
|
|
|
funcargs = lappend(funcargs,
|
2000-02-15 21:49:31 +01:00
|
|
|
find_indexkey_var(root, rel, *indexkeys));
|
1999-08-16 04:17:58 +02:00
|
|
|
indexkeys++;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
sortop = *ordering;
|
|
|
|
if (ScanDirectionIsBackward(scandir))
|
|
|
|
{
|
|
|
|
sortop = get_commutator(sortop);
|
|
|
|
if (sortop == InvalidOid)
|
|
|
|
return NIL; /* oops, no reverse sort operator? */
|
|
|
|
}
|
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
/* Make a one-sublist pathkeys list for the function expression */
|
2000-02-15 21:49:31 +01:00
|
|
|
item = makePathKeyItem((Node *) make_funcclause(funcnode, funcargs),
|
|
|
|
sortop);
|
2001-01-17 18:26:45 +01:00
|
|
|
retval = makeList1(make_canonical_pathkey(root, item));
|
1999-08-16 04:17:58 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Normal non-functional index */
|
|
|
|
while (*indexkeys != 0 && *ordering != InvalidOid)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
Var *relvar = find_indexkey_var(root, rel, *indexkeys);
|
2000-12-14 23:30:45 +01:00
|
|
|
List *cpathkey;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
sortop = *ordering;
|
|
|
|
if (ScanDirectionIsBackward(scandir))
|
|
|
|
{
|
|
|
|
sortop = get_commutator(sortop);
|
|
|
|
if (sortop == InvalidOid)
|
|
|
|
break; /* oops, no reverse sort operator? */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* OK, make a sublist for this sort key */
|
|
|
|
item = makePathKeyItem((Node *) relvar, sortop);
|
2000-12-14 23:30:45 +01:00
|
|
|
cpathkey = make_canonical_pathkey(root, item);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-12-14 23:30:45 +01:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* Eliminate redundant ordering info; could happen if query is
|
|
|
|
* such that index keys are equijoined...
|
2000-12-14 23:30:45 +01:00
|
|
|
*/
|
|
|
|
if (!ptrMember(cpathkey, retval))
|
|
|
|
retval = lappend(retval, cpathkey);
|
1999-08-16 04:17:58 +02:00
|
|
|
indexkeys++;
|
|
|
|
ordering++;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
1999-08-16 04:17:58 +02:00
|
|
|
|
|
|
|
return retval;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
/*
|
2000-02-15 21:49:31 +01:00
|
|
|
* Find or make a Var node for the specified attribute of the rel.
|
|
|
|
*
|
|
|
|
* We first look for the var in the rel's target list, because that's
|
|
|
|
* easy and fast. But the var might not be there (this should normally
|
|
|
|
* only happen for vars that are used in WHERE restriction clauses,
|
|
|
|
* but not in join clauses or in the SELECT target list). In that case,
|
|
|
|
* gin up a Var node the hard way.
|
1999-08-16 04:17:58 +02:00
|
|
|
*/
|
|
|
|
static Var *
|
2000-02-15 21:49:31 +01:00
|
|
|
find_indexkey_var(Query *root, RelOptInfo *rel, AttrNumber varattno)
|
1999-08-16 04:17:58 +02:00
|
|
|
{
|
|
|
|
List *temp;
|
2000-02-15 21:49:31 +01:00
|
|
|
int relid;
|
|
|
|
Oid reloid,
|
|
|
|
vartypeid;
|
|
|
|
int32 type_mod;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
foreach(temp, rel->targetlist)
|
1999-08-16 04:17:58 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
Var *tle_var = get_expr(lfirst(temp));
|
1999-08-16 04:17:58 +02:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
if (IsA(tle_var, Var) &&tle_var->varattno == varattno)
|
1999-08-16 04:17:58 +02:00
|
|
|
return tle_var;
|
|
|
|
}
|
2000-02-15 21:49:31 +01:00
|
|
|
|
|
|
|
relid = lfirsti(rel->relids);
|
|
|
|
reloid = getrelid(relid, root->rtable);
|
|
|
|
vartypeid = get_atttype(reloid, varattno);
|
|
|
|
type_mod = get_atttypmod(reloid, varattno);
|
|
|
|
|
|
|
|
return makeVar(relid, varattno, vartypeid, type_mod, 0);
|
1999-08-16 04:17:58 +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
|
|
|
* build_join_pathkeys
|
1999-08-13 03:17:16 +02:00
|
|
|
* Build the path keys for a join relation constructed by mergejoin or
|
|
|
|
* nestloop join. These keys should include all the path key vars of the
|
|
|
|
* outer path (since the join will retain the ordering of the outer path)
|
2000-02-15 21:49:31 +01:00
|
|
|
* plus any vars of the inner path that are equijoined to the outer vars.
|
1999-08-13 03:17:16 +02:00
|
|
|
*
|
2000-12-14 23:30:45 +01:00
|
|
|
* Per the discussion in backend/optimizer/README, equijoined inner vars
|
1999-08-13 03:17:16 +02:00
|
|
|
* can be considered path keys of the result, just the same as the outer
|
2000-02-15 21:49:31 +01:00
|
|
|
* vars they were joined with; furthermore, it doesn't matter what kind
|
|
|
|
* of join algorithm is actually used.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2000-12-14 23:30:45 +01:00
|
|
|
* 'joinrel' is the join relation that paths are being formed for
|
|
|
|
* 'outer_pathkeys' is the list of the current outer path's path keys
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
|
|
|
* Returns the list of new path keys.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
List *
|
2000-12-14 23:30:45 +01:00
|
|
|
build_join_pathkeys(Query *root,
|
|
|
|
RelOptInfo *joinrel,
|
|
|
|
List *outer_pathkeys)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-02-15 21:49:31 +01:00
|
|
|
/*
|
|
|
|
* This used to be quite a complex bit of code, but now that all
|
2000-04-12 19:17:23 +02:00
|
|
|
* pathkey sublists start out life canonicalized, we don't have to do
|
|
|
|
* a darn thing here! The inner-rel vars we used to need to add are
|
|
|
|
* *already* part of the outer pathkey!
|
2000-02-15 21:49:31 +01:00
|
|
|
*
|
2000-12-14 23:30:45 +01:00
|
|
|
* We do, however, need to truncate the pathkeys list, since it may
|
|
|
|
* contain pathkeys that were useful for forming this joinrel but are
|
|
|
|
* uninteresting to higher levels.
|
2000-02-15 21:49:31 +01:00
|
|
|
*/
|
2000-12-14 23:30:45 +01:00
|
|
|
return truncate_useless_pathkeys(root, joinrel, outer_pathkeys);
|
1999-08-21 05:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* PATHKEYS AND SORT CLAUSES
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make_pathkeys_for_sortclauses
|
|
|
|
* Generate a pathkeys list that represents the sort order specified
|
|
|
|
* by a list of SortClauses (GroupClauses will work too!)
|
|
|
|
*
|
2000-02-15 21:49:31 +01:00
|
|
|
* NB: the result is NOT in canonical form, but must be passed through
|
|
|
|
* canonicalize_pathkeys() before it can be used for comparisons or
|
|
|
|
* labeling relation sort orders. (We do things this way because
|
2000-11-12 01:37:02 +01:00
|
|
|
* grouping_planner needs to be able to construct requested pathkeys
|
|
|
|
* before the pathkey equivalence sets have been created for the query.)
|
2000-02-15 21:49:31 +01:00
|
|
|
*
|
1999-08-21 05:49:17 +02:00
|
|
|
* 'sortclauses' is a list of SortClause or GroupClause nodes
|
|
|
|
* 'tlist' is the targetlist to find the referenced tlist entries in
|
|
|
|
*/
|
|
|
|
List *
|
2000-02-15 21:49:31 +01:00
|
|
|
make_pathkeys_for_sortclauses(List *sortclauses,
|
|
|
|
List *tlist)
|
1999-08-21 05:49:17 +02:00
|
|
|
{
|
|
|
|
List *pathkeys = NIL;
|
|
|
|
List *i;
|
|
|
|
|
|
|
|
foreach(i, sortclauses)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
SortClause *sortcl = (SortClause *) lfirst(i);
|
|
|
|
Node *sortkey;
|
|
|
|
PathKeyItem *pathkey;
|
1999-08-21 05:49:17 +02:00
|
|
|
|
|
|
|
sortkey = get_sortgroupclause_expr(sortcl, tlist);
|
|
|
|
pathkey = makePathKeyItem(sortkey, sortcl->sortop);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
/*
|
|
|
|
* The pathkey becomes a one-element sublist, for now;
|
2000-04-12 19:17:23 +02:00
|
|
|
* canonicalize_pathkeys() might replace it with a longer sublist
|
|
|
|
* later.
|
2000-02-15 21:49:31 +01:00
|
|
|
*/
|
2001-01-17 18:26:45 +01:00
|
|
|
pathkeys = lappend(pathkeys, makeList1(pathkey));
|
1999-08-21 05:49:17 +02:00
|
|
|
}
|
|
|
|
return pathkeys;
|
|
|
|
}
|
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* PATHKEYS AND MERGECLAUSES
|
|
|
|
****************************************************************************/
|
|
|
|
|
2000-12-14 23:30:45 +01:00
|
|
|
/*
|
|
|
|
* cache_mergeclause_pathkeys
|
|
|
|
* Make the cached pathkeys valid in a mergeclause restrictinfo.
|
|
|
|
*
|
|
|
|
* RestrictInfo contains fields in which we may cache the result
|
|
|
|
* of looking up the canonical pathkeys for the left and right sides
|
2001-03-22 05:01:46 +01:00
|
|
|
* of the mergeclause. (Note that in normal cases they will be the
|
2000-12-14 23:30:45 +01:00
|
|
|
* same, but not if the mergeclause appears above an OUTER JOIN.)
|
|
|
|
* This is a worthwhile savings because these routines will be invoked
|
|
|
|
* many times when dealing with a many-relation query.
|
|
|
|
*/
|
2001-10-18 18:11:42 +02:00
|
|
|
void
|
2000-12-14 23:30:45 +01:00
|
|
|
cache_mergeclause_pathkeys(Query *root, RestrictInfo *restrictinfo)
|
|
|
|
{
|
|
|
|
Node *key;
|
|
|
|
PathKeyItem *item;
|
|
|
|
|
|
|
|
Assert(restrictinfo->mergejoinoperator != InvalidOid);
|
|
|
|
|
|
|
|
if (restrictinfo->left_pathkey == NIL)
|
|
|
|
{
|
|
|
|
key = (Node *) get_leftop(restrictinfo->clause);
|
|
|
|
item = makePathKeyItem(key, restrictinfo->left_sortop);
|
|
|
|
restrictinfo->left_pathkey = make_canonical_pathkey(root, item);
|
|
|
|
}
|
|
|
|
if (restrictinfo->right_pathkey == NIL)
|
|
|
|
{
|
|
|
|
key = (Node *) get_rightop(restrictinfo->clause);
|
|
|
|
item = makePathKeyItem(key, restrictinfo->right_sortop);
|
|
|
|
restrictinfo->right_pathkey = make_canonical_pathkey(root, item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
/*
|
|
|
|
* find_mergeclauses_for_pathkeys
|
|
|
|
* This routine attempts to find a set of mergeclauses that can be
|
|
|
|
* used with a specified ordering for one of the input relations.
|
|
|
|
* If successful, it returns a list of mergeclauses.
|
|
|
|
*
|
|
|
|
* 'pathkeys' is a pathkeys list showing the ordering of an input path.
|
|
|
|
* It doesn't matter whether it is for the inner or outer path.
|
|
|
|
* 'restrictinfos' is a list of mergejoinable restriction clauses for the
|
|
|
|
* join relation being formed.
|
|
|
|
*
|
|
|
|
* The result is NIL if no merge can be done, else a maximal list of
|
|
|
|
* usable mergeclauses (represented as a list of their restrictinfo nodes).
|
|
|
|
*
|
|
|
|
* XXX Ideally we ought to be considering context, ie what path orderings
|
|
|
|
* are available on the other side of the join, rather than just making
|
2000-12-14 23:30:45 +01:00
|
|
|
* an arbitrary choice among the mergeclauses that will work for this side
|
|
|
|
* of the join.
|
1999-08-16 04:17:58 +02:00
|
|
|
*/
|
|
|
|
List *
|
2000-12-14 23:30:45 +01:00
|
|
|
find_mergeclauses_for_pathkeys(Query *root,
|
|
|
|
List *pathkeys,
|
|
|
|
List *restrictinfos)
|
1999-08-16 04:17:58 +02:00
|
|
|
{
|
|
|
|
List *mergeclauses = NIL;
|
|
|
|
List *i;
|
|
|
|
|
2001-11-11 21:33:53 +01:00
|
|
|
/* make sure we have pathkeys cached in the clauses */
|
|
|
|
foreach(i, restrictinfos)
|
|
|
|
{
|
|
|
|
RestrictInfo *restrictinfo = lfirst(i);
|
|
|
|
|
|
|
|
cache_mergeclause_pathkeys(root, restrictinfo);
|
|
|
|
}
|
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
foreach(i, pathkeys)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
List *pathkey = lfirst(i);
|
2001-11-11 21:33:53 +01:00
|
|
|
List *matched_restrictinfos = NIL;
|
2000-04-12 19:17:23 +02:00
|
|
|
List *j;
|
1999-08-16 04:17:58 +02:00
|
|
|
|
|
|
|
/*
|
2000-12-14 23:30:45 +01:00
|
|
|
* We can match a pathkey against either left or right side of any
|
2001-11-11 21:33:53 +01:00
|
|
|
* mergejoin clause. (We examine both sides since we aren't told if
|
|
|
|
* the given pathkeys are for inner or outer input path; no confusion
|
|
|
|
* is possible.) Furthermore, if there are multiple matching
|
|
|
|
* clauses, take them all. In plain inner-join scenarios we expect
|
|
|
|
* only one match, because redundant-mergeclause elimination will
|
|
|
|
* have removed any redundant mergeclauses from the input list.
|
|
|
|
* However, in outer-join scenarios there might be multiple matches.
|
|
|
|
* An example is
|
|
|
|
*
|
|
|
|
* select * from a full join b on
|
|
|
|
* a.v1 = b.v1 and a.v2 = b.v2 and a.v1 = b.v2;
|
|
|
|
*
|
|
|
|
* Given the pathkeys ((a.v1), (a.v2)) it is okay to return all
|
|
|
|
* three clauses (in the order a.v1=b.v1, a.v1=b.v2, a.v2=b.v2)
|
|
|
|
* and indeed we *must* do so or we will be unable to form a
|
|
|
|
* valid plan.
|
1999-08-16 04:17:58 +02:00
|
|
|
*/
|
2000-12-14 23:30:45 +01:00
|
|
|
foreach(j, restrictinfos)
|
1999-08-16 04:17:58 +02:00
|
|
|
{
|
2000-12-14 23:30:45 +01:00
|
|
|
RestrictInfo *restrictinfo = lfirst(j);
|
1999-08-16 04:17:58 +02:00
|
|
|
|
2000-12-14 23:30:45 +01:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* We can compare canonical pathkey sublists by simple pointer
|
|
|
|
* equality; see compare_pathkeys.
|
2000-12-14 23:30:45 +01:00
|
|
|
*/
|
|
|
|
if ((pathkey == restrictinfo->left_pathkey ||
|
|
|
|
pathkey == restrictinfo->right_pathkey) &&
|
|
|
|
!ptrMember(restrictinfo, mergeclauses))
|
1999-08-16 04:17:58 +02:00
|
|
|
{
|
2001-11-11 21:33:53 +01:00
|
|
|
matched_restrictinfos = lappend(matched_restrictinfos,
|
|
|
|
restrictinfo);
|
2000-12-14 23:30:45 +01:00
|
|
|
}
|
1999-08-16 04:17:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we didn't find a mergeclause, we're done --- any additional
|
2000-04-12 19:17:23 +02:00
|
|
|
* sort-key positions in the pathkeys are useless. (But we can
|
1999-08-16 04:17:58 +02:00
|
|
|
* still mergejoin if we found at least one mergeclause.)
|
|
|
|
*/
|
2001-11-11 21:33:53 +01:00
|
|
|
if (matched_restrictinfos == NIL)
|
1999-08-16 04:17:58 +02:00
|
|
|
break;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
/*
|
2001-11-11 21:33:53 +01:00
|
|
|
* If we did find usable mergeclause(s) for this sort-key position,
|
|
|
|
* add them to result list.
|
1999-08-16 04:17:58 +02:00
|
|
|
*/
|
2001-11-11 21:33:53 +01:00
|
|
|
mergeclauses = nconc(mergeclauses, matched_restrictinfos);
|
1999-08-16 04:17:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return mergeclauses;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make_pathkeys_for_mergeclauses
|
|
|
|
* Builds a pathkey list representing the explicit sort order that
|
|
|
|
* must be applied to a path in order to make it usable for the
|
|
|
|
* given mergeclauses.
|
|
|
|
*
|
|
|
|
* 'mergeclauses' is a list of RestrictInfos for mergejoin clauses
|
|
|
|
* that will be used in a merge join.
|
2000-09-12 23:07:18 +02:00
|
|
|
* 'rel' is the relation the pathkeys will apply to (ie, either the inner
|
|
|
|
* or outer side of the proposed join rel).
|
1999-08-16 04:17:58 +02:00
|
|
|
*
|
|
|
|
* Returns a pathkeys list that can be applied to the indicated relation.
|
|
|
|
*
|
|
|
|
* Note that it is not this routine's job to decide whether sorting is
|
|
|
|
* actually needed for a particular input path. Assume a sort is necessary;
|
|
|
|
* just make the keys, eh?
|
|
|
|
*/
|
|
|
|
List *
|
2000-02-15 21:49:31 +01:00
|
|
|
make_pathkeys_for_mergeclauses(Query *root,
|
|
|
|
List *mergeclauses,
|
2000-09-12 23:07:18 +02:00
|
|
|
RelOptInfo *rel)
|
1999-08-16 04:17:58 +02:00
|
|
|
{
|
|
|
|
List *pathkeys = NIL;
|
|
|
|
List *i;
|
|
|
|
|
|
|
|
foreach(i, mergeclauses)
|
|
|
|
{
|
|
|
|
RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(i);
|
1999-08-22 22:15:04 +02:00
|
|
|
Node *key;
|
2000-02-19 00:47:31 +01:00
|
|
|
List *pathkey;
|
1999-08-16 04:17:58 +02:00
|
|
|
|
2000-12-14 23:30:45 +01:00
|
|
|
cache_mergeclause_pathkeys(root, restrictinfo);
|
1999-08-22 22:15:04 +02:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
key = (Node *) get_leftop(restrictinfo->clause);
|
2002-03-12 01:52:10 +01:00
|
|
|
if (IsA(key, Var) &&
|
|
|
|
VARISRELMEMBER(((Var *) key)->varno, rel))
|
2000-12-14 23:30:45 +01:00
|
|
|
{
|
|
|
|
/* Rel is left side of mergeclause */
|
|
|
|
pathkey = restrictinfo->left_pathkey;
|
|
|
|
}
|
|
|
|
else
|
2000-09-12 23:07:18 +02:00
|
|
|
{
|
|
|
|
key = (Node *) get_rightop(restrictinfo->clause);
|
2002-03-12 01:52:10 +01:00
|
|
|
if (IsA(key, Var) &&
|
|
|
|
VARISRELMEMBER(((Var *) key)->varno, rel))
|
2000-12-14 23:30:45 +01:00
|
|
|
{
|
|
|
|
/* Rel is right side of mergeclause */
|
|
|
|
pathkey = restrictinfo->right_pathkey;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-09-12 23:07:18 +02:00
|
|
|
elog(ERROR, "make_pathkeys_for_mergeclauses: can't identify which side of mergeclause to use");
|
2000-12-14 23:30:45 +01:00
|
|
|
pathkey = NIL; /* keep compiler quiet */
|
|
|
|
}
|
2000-09-12 23:07:18 +02:00
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* When we are given multiple merge clauses, it's possible that
|
2001-03-22 07:16:21 +01:00
|
|
|
* some clauses refer to the same vars as earlier clauses. There's
|
|
|
|
* no reason for us to specify sort keys like (A,B,A) when (A,B)
|
|
|
|
* will do --- and adding redundant sort keys makes add_path think
|
|
|
|
* that this sort order is different from ones that are really the
|
|
|
|
* same, so don't do it. Since we now have a canonicalized
|
|
|
|
* pathkey, a simple ptrMember test is sufficient to detect
|
|
|
|
* redundant keys.
|
1999-08-16 04:17:58 +02:00
|
|
|
*/
|
2000-12-14 23:30:45 +01:00
|
|
|
if (!ptrMember(pathkey, pathkeys))
|
|
|
|
pathkeys = lappend(pathkeys, pathkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pathkeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* PATHKEY USEFULNESS CHECKS
|
|
|
|
*
|
|
|
|
* We only want to remember as many of the pathkeys of a path as have some
|
|
|
|
* potential use, either for subsequent mergejoins or for meeting the query's
|
|
|
|
* requested output ordering. This ensures that add_path() won't consider
|
|
|
|
* a path to have a usefully different ordering unless it really is useful.
|
|
|
|
* These routines check for usefulness of given pathkeys.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pathkeys_useful_for_merging
|
|
|
|
* Count the number of pathkeys that may be useful for mergejoins
|
|
|
|
* above the given relation (by looking at its joininfo lists).
|
|
|
|
*
|
|
|
|
* We consider a pathkey potentially useful if it corresponds to the merge
|
|
|
|
* ordering of either side of any joinclause for the rel. This might be
|
|
|
|
* overoptimistic, since joinclauses that appear in different join lists
|
|
|
|
* might never be usable at the same time, but trying to be exact is likely
|
|
|
|
* to be more trouble than it's worth.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pathkeys_useful_for_merging(Query *root, RelOptInfo *rel, List *pathkeys)
|
|
|
|
{
|
|
|
|
int useful = 0;
|
|
|
|
List *i;
|
|
|
|
|
|
|
|
foreach(i, pathkeys)
|
|
|
|
{
|
|
|
|
List *pathkey = lfirst(i);
|
|
|
|
bool matched = false;
|
|
|
|
List *j;
|
|
|
|
|
|
|
|
foreach(j, rel->joininfo)
|
|
|
|
{
|
|
|
|
JoinInfo *joininfo = (JoinInfo *) lfirst(j);
|
|
|
|
List *k;
|
|
|
|
|
|
|
|
foreach(k, joininfo->jinfo_restrictinfo)
|
|
|
|
{
|
|
|
|
RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(k);
|
|
|
|
|
|
|
|
if (restrictinfo->mergejoinoperator == InvalidOid)
|
|
|
|
continue;
|
|
|
|
cache_mergeclause_pathkeys(root, restrictinfo);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-12-14 23:30:45 +01:00
|
|
|
/*
|
|
|
|
* We can compare canonical pathkey sublists by simple
|
|
|
|
* pointer equality; see compare_pathkeys.
|
|
|
|
*/
|
|
|
|
if (pathkey == restrictinfo->left_pathkey ||
|
|
|
|
pathkey == restrictinfo->right_pathkey)
|
|
|
|
{
|
|
|
|
matched = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matched)
|
|
|
|
break;
|
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-02-19 00:47:31 +01:00
|
|
|
/*
|
2000-12-14 23:30:45 +01:00
|
|
|
* If we didn't find a mergeclause, we're done --- any additional
|
|
|
|
* sort-key positions in the pathkeys are useless. (But we can
|
|
|
|
* still mergejoin if we found at least one mergeclause.)
|
2000-02-19 00:47:31 +01:00
|
|
|
*/
|
2000-12-14 23:30:45 +01:00
|
|
|
if (matched)
|
|
|
|
useful++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2000-02-19 00:47:31 +01:00
|
|
|
|
2000-12-14 23:30:45 +01:00
|
|
|
return useful;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pathkeys_useful_for_ordering
|
|
|
|
* Count the number of pathkeys that are useful for meeting the
|
|
|
|
* query's requested output ordering.
|
|
|
|
*
|
|
|
|
* Unlike merge pathkeys, this is an all-or-nothing affair: it does us
|
|
|
|
* no good to order by just the first key(s) of the requested ordering.
|
|
|
|
* So the result is always either 0 or length(root->query_pathkeys).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pathkeys_useful_for_ordering(Query *root, List *pathkeys)
|
|
|
|
{
|
|
|
|
if (root->query_pathkeys == NIL)
|
|
|
|
return 0; /* no special ordering requested */
|
|
|
|
|
|
|
|
if (pathkeys == NIL)
|
|
|
|
return 0; /* unordered path */
|
|
|
|
|
|
|
|
if (pathkeys_contained_in(root->query_pathkeys, pathkeys))
|
|
|
|
{
|
|
|
|
/* It's useful ... or at least the first N keys are */
|
|
|
|
return length(root->query_pathkeys);
|
1999-08-16 04:17:58 +02:00
|
|
|
}
|
|
|
|
|
2000-12-14 23:30:45 +01:00
|
|
|
return 0; /* path ordering not useful */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* truncate_useless_pathkeys
|
|
|
|
* Shorten the given pathkey list to just the useful pathkeys.
|
|
|
|
*/
|
|
|
|
List *
|
|
|
|
truncate_useless_pathkeys(Query *root,
|
|
|
|
RelOptInfo *rel,
|
|
|
|
List *pathkeys)
|
|
|
|
{
|
|
|
|
int nuseful;
|
|
|
|
int nuseful2;
|
|
|
|
|
|
|
|
nuseful = pathkeys_useful_for_merging(root, rel, pathkeys);
|
|
|
|
nuseful2 = pathkeys_useful_for_ordering(root, pathkeys);
|
|
|
|
if (nuseful2 > nuseful)
|
|
|
|
nuseful = nuseful2;
|
2001-03-22 05:01:46 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: not safe to modify input list destructively, but we can avoid
|
2000-12-14 23:30:45 +01:00
|
|
|
* copying the list if we're not actually going to change it
|
|
|
|
*/
|
|
|
|
if (nuseful == length(pathkeys))
|
|
|
|
return pathkeys;
|
|
|
|
else
|
|
|
|
return ltruncate(nuseful, listCopy(pathkeys));
|
1999-08-16 04:17:58 +02:00
|
|
|
}
|