PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
equivclass.c File Reference
#include "postgres.h"
#include <limits.h>
#include "access/stratnum.h"
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/planmain.h"
#include "optimizer/prep.h"
#include "optimizer/var.h"
#include "utils/lsyscache.h"
Include dependency graph for equivclass.c:

Go to the source code of this file.

Functions

static EquivalenceMemberadd_eq_member (EquivalenceClass *ec, Expr *expr, Relids relids, Relids nullable_relids, bool is_child, Oid datatype)
 
static void generate_base_implied_equalities_const (PlannerInfo *root, EquivalenceClass *ec)
 
static void generate_base_implied_equalities_no_const (PlannerInfo *root, EquivalenceClass *ec)
 
static void generate_base_implied_equalities_broken (PlannerInfo *root, EquivalenceClass *ec)
 
static Listgenerate_join_implied_equalities_normal (PlannerInfo *root, EquivalenceClass *ec, Relids join_relids, Relids outer_relids, Relids inner_relids)
 
static Listgenerate_join_implied_equalities_broken (PlannerInfo *root, EquivalenceClass *ec, Relids nominal_join_relids, Relids outer_relids, Relids nominal_inner_relids, RelOptInfo *inner_rel)
 
static Oid select_equality_operator (EquivalenceClass *ec, Oid lefttype, Oid righttype)
 
static RestrictInfocreate_join_clause (PlannerInfo *root, EquivalenceClass *ec, Oid opno, EquivalenceMember *leftem, EquivalenceMember *rightem, EquivalenceClass *parent_ec)
 
static bool reconsider_outer_join_clause (PlannerInfo *root, RestrictInfo *rinfo, bool outer_on_left)
 
static bool reconsider_full_join_clause (PlannerInfo *root, RestrictInfo *rinfo)
 
bool process_equivalence (PlannerInfo *root, RestrictInfo *restrictinfo, bool below_outer_join)
 
Exprcanonicalize_ec_expression (Expr *expr, Oid req_type, Oid req_collation)
 
EquivalenceClassget_eclass_for_sort_expr (PlannerInfo *root, Expr *expr, Relids nullable_relids, List *opfamilies, Oid opcintype, Oid collation, Index sortref, Relids rel, bool create_it)
 
void generate_base_implied_equalities (PlannerInfo *root)
 
Listgenerate_join_implied_equalities (PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
 
Listgenerate_join_implied_equalities_for_ecs (PlannerInfo *root, List *eclasses, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
 
void reconsider_outer_join_clauses (PlannerInfo *root)
 
bool exprs_known_equal (PlannerInfo *root, Node *item1, Node *item2)
 
EquivalenceClassmatch_eclasses_to_foreign_key_col (PlannerInfo *root, ForeignKeyOptInfo *fkinfo, int colno)
 
void add_child_rel_equivalences (PlannerInfo *root, AppendRelInfo *appinfo, RelOptInfo *parent_rel, RelOptInfo *child_rel)
 
Listgenerate_implied_equalities_for_column (PlannerInfo *root, RelOptInfo *rel, ec_matches_callback_type callback, void *callback_arg, Relids prohibited_rels)
 
bool have_relevant_eclass_joinclause (PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
 
bool has_relevant_eclass_joinclause (PlannerInfo *root, RelOptInfo *rel1)
 
bool eclass_useful_for_merging (PlannerInfo *root, EquivalenceClass *eclass, RelOptInfo *rel)
 
bool is_redundant_derived_clause (RestrictInfo *rinfo, List *clauselist)
 

Function Documentation

void add_child_rel_equivalences ( PlannerInfo root,
AppendRelInfo appinfo,
RelOptInfo parent_rel,
RelOptInfo child_rel 
)

Definition at line 2068 of file equivclass.c.

References add_eq_member(), adjust_appendrel_attrs(), bms_add_members(), bms_difference(), bms_is_subset(), bms_overlap(), EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_members, EquivalenceClass::ec_relids, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_const, EquivalenceMember::em_nullable_relids, EquivalenceMember::em_relids, PlannerInfo::eq_classes, lfirst, RelOptInfo::relids, RELOPT_BASEREL, and RelOptInfo::reloptkind.

Referenced by set_append_rel_size().

2072 {
2073  ListCell *lc1;
2074 
2075  foreach(lc1, root->eq_classes)
2076  {
2077  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
2078  ListCell *lc2;
2079 
2080  /*
2081  * If this EC contains a volatile expression, then generating child
2082  * EMs would be downright dangerous, so skip it. We rely on a
2083  * volatile EC having only one EM.
2084  */
2085  if (cur_ec->ec_has_volatile)
2086  continue;
2087 
2088  /*
2089  * No point in searching if parent rel not mentioned in eclass; but we
2090  * can't tell that for sure if parent rel is itself a child.
2091  */
2092  if (parent_rel->reloptkind == RELOPT_BASEREL &&
2093  !bms_is_subset(parent_rel->relids, cur_ec->ec_relids))
2094  continue;
2095 
2096  foreach(lc2, cur_ec->ec_members)
2097  {
2098  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2099 
2100  if (cur_em->em_is_const)
2101  continue; /* ignore consts here */
2102 
2103  /* Does it reference parent_rel? */
2104  if (bms_overlap(cur_em->em_relids, parent_rel->relids))
2105  {
2106  /* Yes, generate transformed child version */
2107  Expr *child_expr;
2108  Relids new_relids;
2109  Relids new_nullable_relids;
2110 
2111  child_expr = (Expr *)
2113  (Node *) cur_em->em_expr,
2114  appinfo);
2115 
2116  /*
2117  * Transform em_relids to match. Note we do *not* do
2118  * pull_varnos(child_expr) here, as for example the
2119  * transformation might have substituted a constant, but we
2120  * don't want the child member to be marked as constant.
2121  */
2122  new_relids = bms_difference(cur_em->em_relids,
2123  parent_rel->relids);
2124  new_relids = bms_add_members(new_relids, child_rel->relids);
2125 
2126  /*
2127  * And likewise for nullable_relids. Note this code assumes
2128  * parent and child relids are singletons.
2129  */
2130  new_nullable_relids = cur_em->em_nullable_relids;
2131  if (bms_overlap(new_nullable_relids, parent_rel->relids))
2132  {
2133  new_nullable_relids = bms_difference(new_nullable_relids,
2134  parent_rel->relids);
2135  new_nullable_relids = bms_add_members(new_nullable_relids,
2136  child_rel->relids);
2137  }
2138 
2139  (void) add_eq_member(cur_ec, child_expr,
2140  new_relids, new_nullable_relids,
2141  true, cur_em->em_datatype);
2142  }
2143  }
2144  }
2145 }
RelOptKind reloptkind
Definition: relation.h:491
Relids em_nullable_relids
Definition: relation.h:787
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:284
Definition: nodes.h:506
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, AppendRelInfo *appinfo)
Definition: prepunion.c:1772
Relids ec_relids
Definition: relation.h:740
Relids relids
Definition: relation.h:494
Relids em_relids
Definition: relation.h:786
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:234
bool ec_has_volatile
Definition: relation.h:743
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
static EquivalenceMember * add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids, Relids nullable_relids, bool is_child, Oid datatype)
Definition: equivclass.c:505
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:755
List * ec_members
Definition: relation.h:737
static EquivalenceMember * add_eq_member ( EquivalenceClass ec,
Expr expr,
Relids  relids,
Relids  nullable_relids,
bool  is_child,
Oid  datatype 
)
static

Definition at line 505 of file equivclass.c.

References Assert, bms_add_members(), bms_is_empty(), EquivalenceClass::ec_has_const, EquivalenceClass::ec_members, EquivalenceClass::ec_relids, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, EquivalenceMember::em_nullable_relids, EquivalenceMember::em_relids, lappend(), and makeNode.

Referenced by add_child_rel_equivalences(), get_eclass_for_sort_expr(), and process_equivalence().

507 {
509 
510  em->em_expr = expr;
511  em->em_relids = relids;
512  em->em_nullable_relids = nullable_relids;
513  em->em_is_const = false;
514  em->em_is_child = is_child;
515  em->em_datatype = datatype;
516 
517  if (bms_is_empty(relids))
518  {
519  /*
520  * No Vars, assume it's a pseudoconstant. This is correct for entries
521  * generated from process_equivalence(), because a WHERE clause can't
522  * contain aggregates or SRFs, and non-volatility was checked before
523  * process_equivalence() ever got called. But
524  * get_eclass_for_sort_expr() has to work harder. We put the tests
525  * there not here to save cycles in the equivalence case.
526  */
527  Assert(!is_child);
528  em->em_is_const = true;
529  ec->ec_has_const = true;
530  /* it can't affect ec_relids */
531  }
532  else if (!is_child) /* child members don't add to ec_relids */
533  {
534  ec->ec_relids = bms_add_members(ec->ec_relids, relids);
535  }
536  ec->ec_members = lappend(ec->ec_members, em);
537 
538  return em;
539 }
Relids em_nullable_relids
Definition: relation.h:787
Relids ec_relids
Definition: relation.h:740
List * lappend(List *list, void *datum)
Definition: list.c:128
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
Relids em_relids
Definition: relation.h:786
#define makeNode(_type_)
Definition: nodes.h:554
#define Assert(condition)
Definition: c.h:675
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:755
List * ec_members
Definition: relation.h:737
Expr* canonicalize_ec_expression ( Expr expr,
Oid  req_type,
Oid  req_collation 
)

Definition at line 456 of file equivclass.c.

References arg, COERCE_IMPLICIT_CAST, exprCollation(), exprType(), exprTypmod(), IsA, IsPolymorphicType, and makeRelabelType().

Referenced by convert_subquery_pathkeys(), get_eclass_for_sort_expr(), and process_equivalence().

457 {
458  Oid expr_type = exprType((Node *) expr);
459 
460  /*
461  * For a polymorphic-input-type opclass, just keep the same exposed type.
462  */
463  if (IsPolymorphicType(req_type))
464  req_type = expr_type;
465 
466  /*
467  * No work if the expression exposes the right type/collation already.
468  */
469  if (expr_type != req_type ||
470  exprCollation((Node *) expr) != req_collation)
471  {
472  /*
473  * Strip any existing RelabelType, then add a new one if needed. This
474  * is to preserve the invariant of no redundant RelabelTypes.
475  *
476  * If we have to change the exposed type of the stripped expression,
477  * set typmod to -1 (since the new type may not have the same typmod
478  * interpretation). If we only have to change collation, preserve the
479  * exposed typmod.
480  */
481  while (expr && IsA(expr, RelabelType))
482  expr = (Expr *) ((RelabelType *) expr)->arg;
483 
484  if (exprType((Node *) expr) != req_type)
485  expr = (Expr *) makeRelabelType(expr,
486  req_type,
487  -1,
488  req_collation,
490  else if (exprCollation((Node *) expr) != req_collation)
491  expr = (Expr *) makeRelabelType(expr,
492  req_type,
493  exprTypmod((Node *) expr),
494  req_collation,
496  }
497 
498  return expr;
499 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:557
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:273
Definition: nodes.h:506
unsigned int Oid
Definition: postgres_ext.h:31
#define IsPolymorphicType(typid)
Definition: pg_type.h:741
RelabelType * makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat)
Definition: makefuncs.c:399
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:745
void * arg
static RestrictInfo * create_join_clause ( PlannerInfo root,
EquivalenceClass ec,
Oid  opno,
EquivalenceMember leftem,
EquivalenceMember rightem,
EquivalenceClass parent_ec 
)
static

Definition at line 1379 of file equivclass.c.

References bms_union(), build_implied_join_equality(), RestrictInfo::clause, EquivalenceClass::ec_collation, EquivalenceClass::ec_derives, EquivalenceClass::ec_min_security, EquivalenceClass::ec_sources, EquivalenceMember::em_expr, EquivalenceMember::em_nullable_relids, EquivalenceMember::em_relids, lappend(), RestrictInfo::left_ec, RestrictInfo::left_em, lfirst, MemoryContextSwitchTo(), RestrictInfo::parent_ec, PlannerInfo::planner_cxt, RestrictInfo::right_ec, and RestrictInfo::right_em.

Referenced by generate_implied_equalities_for_column(), and generate_join_implied_equalities_normal().

1384 {
1385  RestrictInfo *rinfo;
1386  ListCell *lc;
1387  MemoryContext oldcontext;
1388 
1389  /*
1390  * Search to see if we already built a RestrictInfo for this pair of
1391  * EquivalenceMembers. We can use either original source clauses or
1392  * previously-derived clauses. The check on opno is probably redundant,
1393  * but be safe ...
1394  */
1395  foreach(lc, ec->ec_sources)
1396  {
1397  rinfo = (RestrictInfo *) lfirst(lc);
1398  if (rinfo->left_em == leftem &&
1399  rinfo->right_em == rightem &&
1400  rinfo->parent_ec == parent_ec &&
1401  opno == ((OpExpr *) rinfo->clause)->opno)
1402  return rinfo;
1403  }
1404 
1405  foreach(lc, ec->ec_derives)
1406  {
1407  rinfo = (RestrictInfo *) lfirst(lc);
1408  if (rinfo->left_em == leftem &&
1409  rinfo->right_em == rightem &&
1410  rinfo->parent_ec == parent_ec &&
1411  opno == ((OpExpr *) rinfo->clause)->opno)
1412  return rinfo;
1413  }
1414 
1415  /*
1416  * Not there, so build it, in planner context so we can re-use it. (Not
1417  * important in normal planning, but definitely so in GEQO.)
1418  */
1419  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
1420 
1421  rinfo = build_implied_join_equality(opno,
1422  ec->ec_collation,
1423  leftem->em_expr,
1424  rightem->em_expr,
1425  bms_union(leftem->em_relids,
1426  rightem->em_relids),
1427  bms_union(leftem->em_nullable_relids,
1428  rightem->em_nullable_relids),
1429  ec->ec_min_security);
1430 
1431  /* Mark the clause as redundant, or not */
1432  rinfo->parent_ec = parent_ec;
1433 
1434  /*
1435  * We know the correct values for left_ec/right_ec, ie this particular EC,
1436  * so we can just set them directly instead of forcing another lookup.
1437  */
1438  rinfo->left_ec = ec;
1439  rinfo->right_ec = ec;
1440 
1441  /* Mark it as usable with these EMs */
1442  rinfo->left_em = leftem;
1443  rinfo->right_em = rightem;
1444  /* and save it for possible re-use */
1445  ec->ec_derives = lappend(ec->ec_derives, rinfo);
1446 
1447  MemoryContextSwitchTo(oldcontext);
1448 
1449  return rinfo;
1450 }
Index ec_min_security
Definition: relation.h:747
Relids em_nullable_relids
Definition: relation.h:787
List * ec_derives
Definition: relation.h:739
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
EquivalenceClass * right_ec
Definition: relation.h:1748
EquivalenceMember * left_em
Definition: relation.h:1749
EquivalenceClass * parent_ec
Definition: relation.h:1733
List * ec_sources
Definition: relation.h:738
List * lappend(List *list, void *datum)
Definition: list.c:128
EquivalenceMember * right_em
Definition: relation.h:1750
Expr * clause
Definition: relation.h:1699
Relids em_relids
Definition: relation.h:786
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:218
RestrictInfo * build_implied_join_equality(Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
Definition: initsplan.c:2354
EquivalenceClass * left_ec
Definition: relation.h:1747
MemoryContext planner_cxt
Definition: relation.h:286
bool eclass_useful_for_merging ( PlannerInfo root,
EquivalenceClass eclass,
RelOptInfo rel 
)

Definition at line 2393 of file equivclass.c.

References Assert, bms_is_subset(), bms_overlap(), EquivalenceClass::ec_has_const, EquivalenceClass::ec_members, EquivalenceClass::ec_merged, EquivalenceClass::ec_relids, EquivalenceMember::em_is_child, EquivalenceMember::em_relids, find_childrel_top_parent(), lfirst, list_length(), RelOptInfo::relids, RELOPT_OTHER_MEMBER_REL, and RelOptInfo::reloptkind.

Referenced by get_useful_ecs_for_relation(), and pathkeys_useful_for_merging().

2396 {
2397  Relids relids;
2398  ListCell *lc;
2399 
2400  Assert(!eclass->ec_merged);
2401 
2402  /*
2403  * Won't generate joinclauses if const or single-member (the latter test
2404  * covers the volatile case too)
2405  */
2406  if (eclass->ec_has_const || list_length(eclass->ec_members) <= 1)
2407  return false;
2408 
2409  /*
2410  * Note we don't test ec_broken; if we did, we'd need a separate code path
2411  * to look through ec_sources. Checking the members anyway is OK as a
2412  * possibly-overoptimistic heuristic.
2413  */
2414 
2415  /* If specified rel is a child, we must consider the topmost parent rel */
2416  if (rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
2417  relids = find_childrel_top_parent(root, rel)->relids;
2418  else
2419  relids = rel->relids;
2420 
2421  /* If rel already includes all members of eclass, no point in searching */
2422  if (bms_is_subset(eclass->ec_relids, relids))
2423  return false;
2424 
2425  /* To join, we need a member not in the given rel */
2426  foreach(lc, eclass->ec_members)
2427  {
2428  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
2429 
2430  if (cur_em->em_is_child)
2431  continue; /* ignore children here */
2432 
2433  if (!bms_overlap(cur_em->em_relids, relids))
2434  return true;
2435  }
2436 
2437  return false;
2438 }
RelOptInfo * find_childrel_top_parent(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:976
RelOptKind reloptkind
Definition: relation.h:491
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
Relids ec_relids
Definition: relation.h:740
Relids relids
Definition: relation.h:494
Relids em_relids
Definition: relation.h:786
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
struct EquivalenceClass * ec_merged
Definition: relation.h:749
List * ec_members
Definition: relation.h:737
bool exprs_known_equal ( PlannerInfo root,
Node item1,
Node item2 
)

Definition at line 1943 of file equivclass.c.

References EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_members, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, PlannerInfo::eq_classes, equal(), and lfirst.

Referenced by add_unique_group_var().

1944 {
1945  ListCell *lc1;
1946 
1947  foreach(lc1, root->eq_classes)
1948  {
1949  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
1950  bool item1member = false;
1951  bool item2member = false;
1952  ListCell *lc2;
1953 
1954  /* Never match to a volatile EC */
1955  if (ec->ec_has_volatile)
1956  continue;
1957 
1958  foreach(lc2, ec->ec_members)
1959  {
1961 
1962  if (em->em_is_child)
1963  continue; /* ignore children here */
1964  if (equal(item1, em->em_expr))
1965  item1member = true;
1966  else if (equal(item2, em->em_expr))
1967  item2member = true;
1968  /* Exit as soon as equality is proven */
1969  if (item1member && item2member)
1970  return true;
1971  }
1972  }
1973  return false;
1974 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2946
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:234
bool ec_has_volatile
Definition: relation.h:743
List * ec_members
Definition: relation.h:737
void generate_base_implied_equalities ( PlannerInfo root)

Definition at line 762 of file equivclass.c.

References Assert, EquivalenceClass::ec_broken, EquivalenceClass::ec_has_const, EquivalenceClass::ec_members, EquivalenceClass::ec_merged, PlannerInfo::eq_classes, generate_base_implied_equalities_broken(), generate_base_implied_equalities_const(), generate_base_implied_equalities_no_const(), RelOptInfo::has_eclass_joins, has_relevant_eclass_joinclause(), lfirst, list_length(), NULL, PlannerInfo::simple_rel_array, and PlannerInfo::simple_rel_array_size.

Referenced by query_planner().

763 {
764  ListCell *lc;
765  Index rti;
766 
767  foreach(lc, root->eq_classes)
768  {
770 
771  Assert(ec->ec_merged == NULL); /* else shouldn't be in list */
772  Assert(!ec->ec_broken); /* not yet anyway... */
773 
774  /* Single-member ECs won't generate any deductions */
775  if (list_length(ec->ec_members) <= 1)
776  continue;
777 
778  if (ec->ec_has_const)
780  else
782 
783  /* Recover if we failed to generate required derived clauses */
784  if (ec->ec_broken)
786  }
787 
788  /*
789  * This is also a handy place to mark base rels (which should all exist by
790  * now) with flags showing whether they have pending eclass joins.
791  */
792  for (rti = 1; rti < root->simple_rel_array_size; rti++)
793  {
794  RelOptInfo *brel = root->simple_rel_array[rti];
795 
796  if (brel == NULL)
797  continue;
798 
800  }
801 }
bool has_eclass_joins
Definition: relation.h:556
static void generate_base_implied_equalities_no_const(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:884
static void generate_base_implied_equalities_broken(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:976
struct RelOptInfo ** simple_rel_array
Definition: relation.h:178
bool has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
Definition: equivclass.c:2354
int simple_rel_array_size
Definition: relation.h:179
unsigned int Index
Definition: c.h:365
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:234
static int list_length(const List *l)
Definition: pg_list.h:89
static void generate_base_implied_equalities_const(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:807
struct EquivalenceClass * ec_merged
Definition: relation.h:749
List * ec_members
Definition: relation.h:737
static void generate_base_implied_equalities_broken ( PlannerInfo root,
EquivalenceClass ec 
)
static

Definition at line 976 of file equivclass.c.

References bms_membership(), BMS_MULTIPLE, distribute_restrictinfo_to_rels(), EquivalenceClass::ec_has_const, EquivalenceClass::ec_sources, lfirst, and RestrictInfo::required_relids.

Referenced by generate_base_implied_equalities().

978 {
979  ListCell *lc;
980 
981  foreach(lc, ec->ec_sources)
982  {
983  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
984 
985  if (ec->ec_has_const ||
986  bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
987  distribute_restrictinfo_to_rels(root, restrictinfo);
988  }
989 }
Relids required_relids
Definition: relation.h:1717
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2205
List * ec_sources
Definition: relation.h:738
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:634
#define lfirst(lc)
Definition: pg_list.h:106
static void generate_base_implied_equalities_const ( PlannerInfo root,
EquivalenceClass ec 
)
static

Definition at line 807 of file equivclass.c.

References Assert, bms_copy(), bms_membership(), BMS_MULTIPLE, bms_union(), distribute_restrictinfo_to_rels(), EquivalenceClass::ec_below_outer_join, EquivalenceClass::ec_broken, EquivalenceClass::ec_collation, EquivalenceClass::ec_members, EquivalenceClass::ec_min_security, EquivalenceClass::ec_relids, EquivalenceClass::ec_sources, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, EquivalenceMember::em_nullable_relids, IsA, lfirst, linitial, list_length(), NULL, OidIsValid, process_implied_equality(), RestrictInfo::required_relids, and select_equality_operator().

Referenced by generate_base_implied_equalities().

809 {
810  EquivalenceMember *const_em = NULL;
811  ListCell *lc;
812 
813  /*
814  * In the trivial case where we just had one "var = const" clause, push
815  * the original clause back into the main planner machinery. There is
816  * nothing to be gained by doing it differently, and we save the effort to
817  * re-build and re-analyze an equality clause that will be exactly
818  * equivalent to the old one.
819  */
820  if (list_length(ec->ec_members) == 2 &&
821  list_length(ec->ec_sources) == 1)
822  {
823  RestrictInfo *restrictinfo = (RestrictInfo *) linitial(ec->ec_sources);
824 
825  if (bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
826  {
827  distribute_restrictinfo_to_rels(root, restrictinfo);
828  return;
829  }
830  }
831 
832  /*
833  * Find the constant member to use. We prefer an actual constant to
834  * pseudo-constants (such as Params), because the constraint exclusion
835  * machinery might be able to exclude relations on the basis of generated
836  * "var = const" equalities, but "var = param" won't work for that.
837  */
838  foreach(lc, ec->ec_members)
839  {
840  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
841 
842  if (cur_em->em_is_const)
843  {
844  const_em = cur_em;
845  if (IsA(cur_em->em_expr, Const))
846  break;
847  }
848  }
849  Assert(const_em != NULL);
850 
851  /* Generate a derived equality against each other member */
852  foreach(lc, ec->ec_members)
853  {
854  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
855  Oid eq_op;
856 
857  Assert(!cur_em->em_is_child); /* no children yet */
858  if (cur_em == const_em)
859  continue;
860  eq_op = select_equality_operator(ec,
861  cur_em->em_datatype,
862  const_em->em_datatype);
863  if (!OidIsValid(eq_op))
864  {
865  /* failed... */
866  ec->ec_broken = true;
867  break;
868  }
869  process_implied_equality(root, eq_op, ec->ec_collation,
870  cur_em->em_expr, const_em->em_expr,
871  bms_copy(ec->ec_relids),
873  const_em->em_nullable_relids),
874  ec->ec_min_security,
876  cur_em->em_is_const);
877  }
878 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:557
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:111
Relids required_relids
Definition: relation.h:1717
Index ec_min_security
Definition: relation.h:747
Relids em_nullable_relids
Definition: relation.h:787
bool ec_below_outer_join
Definition: relation.h:744
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
void process_implied_equality(PlannerInfo *root, Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level, bool below_outer_join, bool both_const)
Definition: initsplan.c:2289
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2205
#define linitial(l)
Definition: pg_list.h:110
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1344
List * ec_sources
Definition: relation.h:738
Relids ec_relids
Definition: relation.h:740
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:634
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:218
static int list_length(const List *l)
Definition: pg_list.h:89
List * ec_members
Definition: relation.h:737
static void generate_base_implied_equalities_no_const ( PlannerInfo root,
EquivalenceClass ec 
)
static

Definition at line 884 of file equivclass.c.

References add_vars_to_targetlist(), Assert, bms_copy(), bms_get_singleton_member(), bms_union(), EquivalenceClass::ec_below_outer_join, EquivalenceClass::ec_broken, EquivalenceClass::ec_collation, EquivalenceClass::ec_members, EquivalenceClass::ec_min_security, EquivalenceClass::ec_relids, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_nullable_relids, EquivalenceMember::em_relids, lfirst, list_free(), NULL, OidIsValid, palloc0(), pfree(), process_implied_equality(), pull_var_clause(), PVC_INCLUDE_PLACEHOLDERS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_WINDOWFUNCS, select_equality_operator(), and PlannerInfo::simple_rel_array_size.

Referenced by generate_base_implied_equalities().

886 {
887  EquivalenceMember **prev_ems;
888  ListCell *lc;
889 
890  /*
891  * We scan the EC members once and track the last-seen member for each
892  * base relation. When we see another member of the same base relation,
893  * we generate "prev_mem = cur_mem". This results in the minimum number
894  * of derived clauses, but it's possible that it will fail when a
895  * different ordering would succeed. XXX FIXME: use a UNION-FIND
896  * algorithm similar to the way we build merged ECs. (Use a list-of-lists
897  * for each rel.)
898  */
899  prev_ems = (EquivalenceMember **)
901 
902  foreach(lc, ec->ec_members)
903  {
904  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
905  int relid;
906 
907  Assert(!cur_em->em_is_child); /* no children yet */
908  if (!bms_get_singleton_member(cur_em->em_relids, &relid))
909  continue;
910  Assert(relid < root->simple_rel_array_size);
911 
912  if (prev_ems[relid] != NULL)
913  {
914  EquivalenceMember *prev_em = prev_ems[relid];
915  Oid eq_op;
916 
917  eq_op = select_equality_operator(ec,
918  prev_em->em_datatype,
919  cur_em->em_datatype);
920  if (!OidIsValid(eq_op))
921  {
922  /* failed... */
923  ec->ec_broken = true;
924  break;
925  }
926  process_implied_equality(root, eq_op, ec->ec_collation,
927  prev_em->em_expr, cur_em->em_expr,
928  bms_copy(ec->ec_relids),
929  bms_union(prev_em->em_nullable_relids,
930  cur_em->em_nullable_relids),
931  ec->ec_min_security,
933  false);
934  }
935  prev_ems[relid] = cur_em;
936  }
937 
938  pfree(prev_ems);
939 
940  /*
941  * We also have to make sure that all the Vars used in the member clauses
942  * will be available at any join node we might try to reference them at.
943  * For the moment we force all the Vars to be available at all join nodes
944  * for this eclass. Perhaps this could be improved by doing some
945  * pre-analysis of which members we prefer to join, but it's no worse than
946  * what happened in the pre-8.3 code.
947  */
948  foreach(lc, ec->ec_members)
949  {
950  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
951  List *vars = pull_var_clause((Node *) cur_em->em_expr,
955 
956  add_vars_to_targetlist(root, vars, ec->ec_relids, false);
957  list_free(vars);
958  }
959 }
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:111
#define PVC_RECURSE_AGGREGATES
Definition: var.h:21
Index ec_min_security
Definition: relation.h:747
Relids em_nullable_relids
Definition: relation.h:787
Definition: nodes.h:506
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:569
bool ec_below_outer_join
Definition: relation.h:744
List * pull_var_clause(Node *node, int flags)
Definition: var.c:535
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
void process_implied_equality(PlannerInfo *root, Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level, bool below_outer_join, bool both_const)
Definition: initsplan.c:2289
void add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed, bool create_new_ph)
Definition: initsplan.c:197
#define PVC_INCLUDE_PLACEHOLDERS
Definition: var.h:24
void pfree(void *pointer)
Definition: mcxt.c:950
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1344
Relids ec_relids
Definition: relation.h:740
#define PVC_RECURSE_WINDOWFUNCS
Definition: var.h:23
int simple_rel_array_size
Definition: relation.h:179
void * palloc0(Size size)
Definition: mcxt.c:878
Relids em_relids
Definition: relation.h:786
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:218
void list_free(List *list)
Definition: list.c:1133
Definition: regcomp.c:224
Definition: pg_list.h:45
List * ec_members
Definition: relation.h:737
List* generate_implied_equalities_for_column ( PlannerInfo root,
RelOptInfo rel,
ec_matches_callback_type  callback,
void *  callback_arg,
Relids  prohibited_rels 
)

Definition at line 2172 of file equivclass.c.

References bms_equal(), bms_is_subset(), bms_overlap(), callback(), create_join_clause(), EquivalenceClass::ec_has_const, EquivalenceClass::ec_members, EquivalenceClass::ec_relids, EquivalenceMember::em_datatype, EquivalenceMember::em_is_child, EquivalenceMember::em_relids, PlannerInfo::eq_classes, find_childrel_parents(), lappend(), lfirst, list_length(), NIL, NULL, OidIsValid, RelOptInfo::relids, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, result, and select_equality_operator().

Referenced by match_eclass_clauses_to_index(), and postgresGetForeignPaths().

2177 {
2178  List *result = NIL;
2179  bool is_child_rel = (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
2180  Relids parent_relids;
2181  ListCell *lc1;
2182 
2183  /* If it's a child rel, we'll need to know what its parent(s) are */
2184  if (is_child_rel)
2185  parent_relids = find_childrel_parents(root, rel);
2186  else
2187  parent_relids = NULL; /* not used, but keep compiler quiet */
2188 
2189  foreach(lc1, root->eq_classes)
2190  {
2191  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
2192  EquivalenceMember *cur_em;
2193  ListCell *lc2;
2194 
2195  /*
2196  * Won't generate joinclauses if const or single-member (the latter
2197  * test covers the volatile case too)
2198  */
2199  if (cur_ec->ec_has_const || list_length(cur_ec->ec_members) <= 1)
2200  continue;
2201 
2202  /*
2203  * No point in searching if rel not mentioned in eclass (but we can't
2204  * tell that for a child rel).
2205  */
2206  if (!is_child_rel &&
2207  !bms_is_subset(rel->relids, cur_ec->ec_relids))
2208  continue;
2209 
2210  /*
2211  * Scan members, looking for a match to the target column. Note that
2212  * child EC members are considered, but only when they belong to the
2213  * target relation. (Unlike regular members, the same expression
2214  * could be a child member of more than one EC. Therefore, it's
2215  * potentially order-dependent which EC a child relation's target
2216  * column gets matched to. This is annoying but it only happens in
2217  * corner cases, so for now we live with just reporting the first
2218  * match. See also get_eclass_for_sort_expr.)
2219  */
2220  cur_em = NULL;
2221  foreach(lc2, cur_ec->ec_members)
2222  {
2223  cur_em = (EquivalenceMember *) lfirst(lc2);
2224  if (bms_equal(cur_em->em_relids, rel->relids) &&
2225  callback(root, rel, cur_ec, cur_em, callback_arg))
2226  break;
2227  cur_em = NULL;
2228  }
2229 
2230  if (!cur_em)
2231  continue;
2232 
2233  /*
2234  * Found our match. Scan the other EC members and attempt to generate
2235  * joinclauses.
2236  */
2237  foreach(lc2, cur_ec->ec_members)
2238  {
2239  EquivalenceMember *other_em = (EquivalenceMember *) lfirst(lc2);
2240  Oid eq_op;
2241  RestrictInfo *rinfo;
2242 
2243  if (other_em->em_is_child)
2244  continue; /* ignore children here */
2245 
2246  /* Make sure it'll be a join to a different rel */
2247  if (other_em == cur_em ||
2248  bms_overlap(other_em->em_relids, rel->relids))
2249  continue;
2250 
2251  /* Forget it if caller doesn't want joins to this rel */
2252  if (bms_overlap(other_em->em_relids, prohibited_rels))
2253  continue;
2254 
2255  /*
2256  * Also, if this is a child rel, avoid generating a useless join
2257  * to its parent rel(s).
2258  */
2259  if (is_child_rel &&
2260  bms_overlap(parent_relids, other_em->em_relids))
2261  continue;
2262 
2263  eq_op = select_equality_operator(cur_ec,
2264  cur_em->em_datatype,
2265  other_em->em_datatype);
2266  if (!OidIsValid(eq_op))
2267  continue;
2268 
2269  /* set parent_ec to mark as redundant with other joinclauses */
2270  rinfo = create_join_clause(root, cur_ec, eq_op,
2271  cur_em, other_em,
2272  cur_ec);
2273 
2274  result = lappend(result, rinfo);
2275  }
2276 
2277  /*
2278  * If somehow we failed to create any join clauses, we might as well
2279  * keep scanning the ECs for another match. But if we did make any,
2280  * we're done, because we don't want to return non-redundant clauses.
2281  */
2282  if (result)
2283  break;
2284  }
2285 
2286  return result;
2287 }
#define NIL
Definition: pg_list.h:69
static RestrictInfo * create_join_clause(PlannerInfo *root, EquivalenceClass *ec, Oid opno, EquivalenceMember *leftem, EquivalenceMember *rightem, EquivalenceClass *parent_ec)
Definition: equivclass.c:1379
RelOptKind reloptkind
Definition: relation.h:491
return result
Definition: formatting.c:1618
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1344
Relids ec_relids
Definition: relation.h:740
Relids relids
Definition: relation.h:494
List * lappend(List *list, void *datum)
Definition: list.c:128
Relids em_relids
Definition: relation.h:786
Relids find_childrel_parents(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:1002
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:234
static int list_length(const List *l)
Definition: pg_list.h:89
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
Definition: pg_list.h:45
List * ec_members
Definition: relation.h:737
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:131
List* generate_join_implied_equalities ( PlannerInfo root,
Relids  join_relids,
Relids  outer_relids,
RelOptInfo inner_rel 
)

Definition at line 1033 of file equivclass.c.

References PlannerInfo::eq_classes, and generate_join_implied_equalities_for_ecs().

Referenced by build_joinrel_restrictlist(), check_index_predicates(), get_baserel_parampathinfo(), and get_joinrel_parampathinfo().

1037 {
1039  root->eq_classes,
1040  join_relids,
1041  outer_relids,
1042  inner_rel);
1043 }
List * generate_join_implied_equalities_for_ecs(PlannerInfo *root, List *eclasses, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1050
List * eq_classes
Definition: relation.h:234
static List * generate_join_implied_equalities_broken ( PlannerInfo root,
EquivalenceClass ec,
Relids  nominal_join_relids,
Relids  outer_relids,
Relids  nominal_inner_relids,
RelOptInfo inner_rel 
)
static

Definition at line 1295 of file equivclass.c.

References adjust_appendrel_attrs_multilevel(), bms_is_subset(), EquivalenceClass::ec_sources, lappend(), lfirst, NIL, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, RestrictInfo::required_relids, and result.

Referenced by generate_join_implied_equalities_for_ecs().

1301 {
1302  List *result = NIL;
1303  ListCell *lc;
1304 
1305  foreach(lc, ec->ec_sources)
1306  {
1307  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1308  Relids clause_relids = restrictinfo->required_relids;
1309 
1310  if (bms_is_subset(clause_relids, nominal_join_relids) &&
1311  !bms_is_subset(clause_relids, outer_relids) &&
1312  !bms_is_subset(clause_relids, nominal_inner_relids))
1313  result = lappend(result, restrictinfo);
1314  }
1315 
1316  /*
1317  * If we have to translate, just brute-force apply adjust_appendrel_attrs
1318  * to all the RestrictInfos at once. This will result in returning
1319  * RestrictInfos that are not listed in ec_derives, but there shouldn't be
1320  * any duplication, and it's a sufficiently narrow corner case that we
1321  * shouldn't sweat too much over it anyway.
1322  *
1323  * Since inner_rel might be an indirect descendant of the baserel
1324  * mentioned in the ec_sources clauses, we have to be prepared to apply
1325  * multiple levels of Var translation.
1326  */
1327  if (inner_rel->reloptkind == RELOPT_OTHER_MEMBER_REL &&
1328  result != NIL)
1329  result = (List *) adjust_appendrel_attrs_multilevel(root,
1330  (Node *) result,
1331  inner_rel);
1332 
1333  return result;
1334 }
#define NIL
Definition: pg_list.h:69
RelOptKind reloptkind
Definition: relation.h:491
Relids required_relids
Definition: relation.h:1717
Definition: nodes.h:506
return result
Definition: formatting.c:1618
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
List * ec_sources
Definition: relation.h:738
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, RelOptInfo *child_rel)
Definition: prepunion.c:2139
Definition: pg_list.h:45
List* generate_join_implied_equalities_for_ecs ( PlannerInfo root,
List eclasses,
Relids  join_relids,
Relids  outer_relids,
RelOptInfo inner_rel 
)

Definition at line 1050 of file equivclass.c.

References bms_overlap(), bms_union(), EquivalenceClass::ec_broken, EquivalenceClass::ec_has_const, EquivalenceClass::ec_members, EquivalenceClass::ec_relids, find_childrel_top_parent(), generate_join_implied_equalities_broken(), generate_join_implied_equalities_normal(), lfirst, list_concat(), list_length(), NIL, RelOptInfo::relids, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, and result.

Referenced by generate_join_implied_equalities(), and get_joinrel_parampathinfo().

1055 {
1056  List *result = NIL;
1057  Relids inner_relids = inner_rel->relids;
1058  Relids nominal_inner_relids;
1059  Relids nominal_join_relids;
1060  ListCell *lc;
1061 
1062  /* If inner rel is a child, extra setup work is needed */
1063  if (inner_rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
1064  {
1065  /* Fetch relid set for the topmost parent rel */
1066  nominal_inner_relids = find_childrel_top_parent(root, inner_rel)->relids;
1067  /* ECs will be marked with the parent's relid, not the child's */
1068  nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1069  }
1070  else
1071  {
1072  nominal_inner_relids = inner_relids;
1073  nominal_join_relids = join_relids;
1074  }
1075 
1076  foreach(lc, eclasses)
1077  {
1079  List *sublist = NIL;
1080 
1081  /* ECs containing consts do not need any further enforcement */
1082  if (ec->ec_has_const)
1083  continue;
1084 
1085  /* Single-member ECs won't generate any deductions */
1086  if (list_length(ec->ec_members) <= 1)
1087  continue;
1088 
1089  /* We can quickly ignore any that don't overlap the join, too */
1090  if (!bms_overlap(ec->ec_relids, nominal_join_relids))
1091  continue;
1092 
1093  if (!ec->ec_broken)
1095  ec,
1096  join_relids,
1097  outer_relids,
1098  inner_relids);
1099 
1100  /* Recover if we failed to generate required derived clauses */
1101  if (ec->ec_broken)
1103  ec,
1104  nominal_join_relids,
1105  outer_relids,
1106  nominal_inner_relids,
1107  inner_rel);
1108 
1109  result = list_concat(result, sublist);
1110  }
1111 
1112  return result;
1113 }
RelOptInfo * find_childrel_top_parent(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:976
#define NIL
Definition: pg_list.h:69
RelOptKind reloptkind
Definition: relation.h:491
List * list_concat(List *list1, List *list2)
Definition: list.c:321
return result
Definition: formatting.c:1618
static List * generate_join_implied_equalities_broken(PlannerInfo *root, EquivalenceClass *ec, Relids nominal_join_relids, Relids outer_relids, Relids nominal_inner_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1295
Relids ec_relids
Definition: relation.h:740
Relids relids
Definition: relation.h:494
static List * generate_join_implied_equalities_normal(PlannerInfo *root, EquivalenceClass *ec, Relids join_relids, Relids outer_relids, Relids inner_relids)
Definition: equivclass.c:1119
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:218
static int list_length(const List *l)
Definition: pg_list.h:89
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
Definition: pg_list.h:45
List * ec_members
Definition: relation.h:737
static List * generate_join_implied_equalities_normal ( PlannerInfo root,
EquivalenceClass ec,
Relids  join_relids,
Relids  outer_relids,
Relids  inner_relids 
)
static

Definition at line 1119 of file equivclass.c.

References bms_is_subset(), create_join_clause(), EquivalenceClass::ec_broken, EquivalenceClass::ec_members, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_relids, exprType(), InvalidOid, IsA, lappend(), lfirst, linitial, list_concat(), NIL, NULL, OidIsValid, op_hashjoinable(), result, and select_equality_operator().

Referenced by generate_join_implied_equalities_for_ecs().

1124 {
1125  List *result = NIL;
1126  List *new_members = NIL;
1127  List *outer_members = NIL;
1128  List *inner_members = NIL;
1129  ListCell *lc1;
1130 
1131  /*
1132  * First, scan the EC to identify member values that are computable at the
1133  * outer rel, at the inner rel, or at this relation but not in either
1134  * input rel. The outer-rel members should already be enforced equal,
1135  * likewise for the inner-rel members. We'll need to create clauses to
1136  * enforce that any newly computable members are all equal to each other
1137  * as well as to at least one input member, plus enforce at least one
1138  * outer-rel member equal to at least one inner-rel member.
1139  */
1140  foreach(lc1, ec->ec_members)
1141  {
1142  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
1143 
1144  /*
1145  * We don't need to check explicitly for child EC members. This test
1146  * against join_relids will cause them to be ignored except when
1147  * considering a child inner rel, which is what we want.
1148  */
1149  if (!bms_is_subset(cur_em->em_relids, join_relids))
1150  continue; /* not computable yet, or wrong child */
1151 
1152  if (bms_is_subset(cur_em->em_relids, outer_relids))
1153  outer_members = lappend(outer_members, cur_em);
1154  else if (bms_is_subset(cur_em->em_relids, inner_relids))
1155  inner_members = lappend(inner_members, cur_em);
1156  else
1157  new_members = lappend(new_members, cur_em);
1158  }
1159 
1160  /*
1161  * First, select the joinclause if needed. We can equate any one outer
1162  * member to any one inner member, but we have to find a datatype
1163  * combination for which an opfamily member operator exists. If we have
1164  * choices, we prefer simple Var members (possibly with RelabelType) since
1165  * these are (a) cheapest to compute at runtime and (b) most likely to
1166  * have useful statistics. Also, prefer operators that are also
1167  * hashjoinable.
1168  */
1169  if (outer_members && inner_members)
1170  {
1171  EquivalenceMember *best_outer_em = NULL;
1172  EquivalenceMember *best_inner_em = NULL;
1173  Oid best_eq_op = InvalidOid;
1174  int best_score = -1;
1175  RestrictInfo *rinfo;
1176 
1177  foreach(lc1, outer_members)
1178  {
1179  EquivalenceMember *outer_em = (EquivalenceMember *) lfirst(lc1);
1180  ListCell *lc2;
1181 
1182  foreach(lc2, inner_members)
1183  {
1184  EquivalenceMember *inner_em = (EquivalenceMember *) lfirst(lc2);
1185  Oid eq_op;
1186  int score;
1187 
1188  eq_op = select_equality_operator(ec,
1189  outer_em->em_datatype,
1190  inner_em->em_datatype);
1191  if (!OidIsValid(eq_op))
1192  continue;
1193  score = 0;
1194  if (IsA(outer_em->em_expr, Var) ||
1195  (IsA(outer_em->em_expr, RelabelType) &&
1196  IsA(((RelabelType *) outer_em->em_expr)->arg, Var)))
1197  score++;
1198  if (IsA(inner_em->em_expr, Var) ||
1199  (IsA(inner_em->em_expr, RelabelType) &&
1200  IsA(((RelabelType *) inner_em->em_expr)->arg, Var)))
1201  score++;
1202  if (op_hashjoinable(eq_op,
1203  exprType((Node *) outer_em->em_expr)))
1204  score++;
1205  if (score > best_score)
1206  {
1207  best_outer_em = outer_em;
1208  best_inner_em = inner_em;
1209  best_eq_op = eq_op;
1210  best_score = score;
1211  if (best_score == 3)
1212  break; /* no need to look further */
1213  }
1214  }
1215  if (best_score == 3)
1216  break; /* no need to look further */
1217  }
1218  if (best_score < 0)
1219  {
1220  /* failed... */
1221  ec->ec_broken = true;
1222  return NIL;
1223  }
1224 
1225  /*
1226  * Create clause, setting parent_ec to mark it as redundant with other
1227  * joinclauses
1228  */
1229  rinfo = create_join_clause(root, ec, best_eq_op,
1230  best_outer_em, best_inner_em,
1231  ec);
1232 
1233  result = lappend(result, rinfo);
1234  }
1235 
1236  /*
1237  * Now deal with building restrictions for any expressions that involve
1238  * Vars from both sides of the join. We have to equate all of these to
1239  * each other as well as to at least one old member (if any).
1240  *
1241  * XXX as in generate_base_implied_equalities_no_const, we could be a lot
1242  * smarter here to avoid unnecessary failures in cross-type situations.
1243  * For now, use the same left-to-right method used there.
1244  */
1245  if (new_members)
1246  {
1247  List *old_members = list_concat(outer_members, inner_members);
1248  EquivalenceMember *prev_em = NULL;
1249  RestrictInfo *rinfo;
1250 
1251  /* For now, arbitrarily take the first old_member as the one to use */
1252  if (old_members)
1253  new_members = lappend(new_members, linitial(old_members));
1254 
1255  foreach(lc1, new_members)
1256  {
1257  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
1258 
1259  if (prev_em != NULL)
1260  {
1261  Oid eq_op;
1262 
1263  eq_op = select_equality_operator(ec,
1264  prev_em->em_datatype,
1265  cur_em->em_datatype);
1266  if (!OidIsValid(eq_op))
1267  {
1268  /* failed... */
1269  ec->ec_broken = true;
1270  return NIL;
1271  }
1272  /* do NOT set parent_ec, this qual is not redundant! */
1273  rinfo = create_join_clause(root, ec, eq_op,
1274  prev_em, cur_em,
1275  NULL);
1276 
1277  result = lappend(result, rinfo);
1278  }
1279  prev_em = cur_em;
1280  }
1281  }
1282 
1283  return result;
1284 }
#define NIL
Definition: pg_list.h:69
static RestrictInfo * create_join_clause(PlannerInfo *root, EquivalenceClass *ec, Oid opno, EquivalenceMember *leftem, EquivalenceMember *rightem, EquivalenceClass *parent_ec)
Definition: equivclass.c:1379
#define IsA(nodeptr, _type_)
Definition: nodes.h:557
Definition: nodes.h:506
List * list_concat(List *list1, List *list2)
Definition: list.c:321
return result
Definition: formatting.c:1618
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:163
#define OidIsValid(objectId)
Definition: c.h:538
bool op_hashjoinable(Oid opno, Oid inputtype)
Definition: lsyscache.c:1214
#define linitial(l)
Definition: pg_list.h:110
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1344
List * lappend(List *list, void *datum)
Definition: list.c:128
Relids em_relids
Definition: relation.h:786
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
Definition: pg_list.h:45
List * ec_members
Definition: relation.h:737
EquivalenceClass* get_eclass_for_sort_expr ( PlannerInfo root,
Expr expr,
Relids  nullable_relids,
List opfamilies,
Oid  opcintype,
Oid  collation,
Index  sortref,
Relids  rel,
bool  create_it 
)

Definition at line 581 of file equivclass.c.

References add_eq_member(), bms_equal(), bms_intersect(), canonicalize_ec_expression(), contain_agg_clause(), contain_volatile_functions(), contain_window_function(), copyObject(), EquivalenceClass::ec_below_outer_join, EquivalenceClass::ec_broken, EquivalenceClass::ec_collation, EquivalenceClass::ec_derives, EquivalenceClass::ec_has_const, EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_max_security, EquivalenceClass::ec_members, EquivalenceClass::ec_merged, EquivalenceClass::ec_min_security, EquivalenceClass::ec_opfamilies, EquivalenceClass::ec_relids, EquivalenceClass::ec_sortref, EquivalenceClass::ec_sources, elog, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, EquivalenceMember::em_relids, PlannerInfo::eq_classes, equal(), ERROR, expression_returns_set(), lappend(), lfirst, list_copy(), makeNode, MemoryContextSwitchTo(), NIL, NULL, PlannerInfo::planner_cxt, and pull_varnos().

Referenced by convert_subquery_pathkeys(), initialize_mergeclause_eclasses(), and make_pathkey_from_sortinfo().

590 {
591  Relids expr_relids;
592  EquivalenceClass *newec;
593  EquivalenceMember *newem;
594  ListCell *lc1;
595  MemoryContext oldcontext;
596 
597  /*
598  * Ensure the expression exposes the correct type and collation.
599  */
600  expr = canonicalize_ec_expression(expr, opcintype, collation);
601 
602  /*
603  * Get the precise set of nullable relids appearing in the expression.
604  */
605  expr_relids = pull_varnos((Node *) expr);
606  nullable_relids = bms_intersect(nullable_relids, expr_relids);
607 
608  /*
609  * Scan through the existing EquivalenceClasses for a match
610  */
611  foreach(lc1, root->eq_classes)
612  {
613  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
614  ListCell *lc2;
615 
616  /*
617  * Never match to a volatile EC, except when we are looking at another
618  * reference to the same volatile SortGroupClause.
619  */
620  if (cur_ec->ec_has_volatile &&
621  (sortref == 0 || sortref != cur_ec->ec_sortref))
622  continue;
623 
624  if (collation != cur_ec->ec_collation)
625  continue;
626  if (!equal(opfamilies, cur_ec->ec_opfamilies))
627  continue;
628 
629  foreach(lc2, cur_ec->ec_members)
630  {
631  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
632 
633  /*
634  * Ignore child members unless they match the request.
635  */
636  if (cur_em->em_is_child &&
637  !bms_equal(cur_em->em_relids, rel))
638  continue;
639 
640  /*
641  * If below an outer join, don't match constants: they're not as
642  * constant as they look.
643  */
644  if (cur_ec->ec_below_outer_join &&
645  cur_em->em_is_const)
646  continue;
647 
648  if (opcintype == cur_em->em_datatype &&
649  equal(expr, cur_em->em_expr))
650  return cur_ec; /* Match! */
651  }
652  }
653 
654  /* No match; does caller want a NULL result? */
655  if (!create_it)
656  return NULL;
657 
658  /*
659  * OK, build a new single-member EC
660  *
661  * Here, we must be sure that we construct the EC in the right context.
662  */
663  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
664 
665  newec = makeNode(EquivalenceClass);
666  newec->ec_opfamilies = list_copy(opfamilies);
667  newec->ec_collation = collation;
668  newec->ec_members = NIL;
669  newec->ec_sources = NIL;
670  newec->ec_derives = NIL;
671  newec->ec_relids = NULL;
672  newec->ec_has_const = false;
674  newec->ec_below_outer_join = false;
675  newec->ec_broken = false;
676  newec->ec_sortref = sortref;
677  newec->ec_min_security = UINT_MAX;
678  newec->ec_max_security = 0;
679  newec->ec_merged = NULL;
680 
681  if (newec->ec_has_volatile && sortref == 0) /* should not happen */
682  elog(ERROR, "volatile EquivalenceClass has no sortref");
683 
684  newem = add_eq_member(newec, copyObject(expr), expr_relids,
685  nullable_relids, false, opcintype);
686 
687  /*
688  * add_eq_member doesn't check for volatile functions, set-returning
689  * functions, aggregates, or window functions, but such could appear in
690  * sort expressions; so we have to check whether its const-marking was
691  * correct.
692  */
693  if (newec->ec_has_const)
694  {
695  if (newec->ec_has_volatile ||
696  expression_returns_set((Node *) expr) ||
697  contain_agg_clause((Node *) expr) ||
698  contain_window_function((Node *) expr))
699  {
700  newec->ec_has_const = false;
701  newem->em_is_const = false;
702  }
703  }
704 
705  root->eq_classes = lappend(root->eq_classes, newec);
706 
707  MemoryContextSwitchTo(oldcontext);
708 
709  return newec;
710 }
#define NIL
Definition: pg_list.h:69
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2946
Index ec_min_security
Definition: relation.h:747
List * ec_derives
Definition: relation.h:739
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:667
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
List * list_copy(const List *oldlist)
Definition: list.c:1160
Index ec_sortref
Definition: relation.h:746
Definition: nodes.h:506
bool ec_below_outer_join
Definition: relation.h:744
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:950
Index ec_max_security
Definition: relation.h:748
void * copyObject(const void *from)
Definition: copyfuncs.c:4619
#define ERROR
Definition: elog.h:43
Expr * canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
Definition: equivclass.c:456
List * ec_sources
Definition: relation.h:738
Relids ec_relids
Definition: relation.h:740
Relids pull_varnos(Node *node)
Definition: var.c:95
bool contain_window_function(Node *clause)
Definition: clauses.c:726
List * lappend(List *list, void *datum)
Definition: list.c:128
List * ec_opfamilies
Definition: relation.h:735
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:252
Relids em_relids
Definition: relation.h:786
#define makeNode(_type_)
Definition: nodes.h:554
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:234
bool ec_has_volatile
Definition: relation.h:743
bool contain_agg_clause(Node *clause)
Definition: clauses.c:416
static EquivalenceMember * add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids, Relids nullable_relids, bool is_child, Oid datatype)
Definition: equivclass.c:505
MemoryContext planner_cxt
Definition: relation.h:286
#define elog
Definition: elog.h:219
struct EquivalenceClass * ec_merged
Definition: relation.h:749
List * ec_members
Definition: relation.h:737
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:131
bool has_relevant_eclass_joinclause ( PlannerInfo root,
RelOptInfo rel1 
)

Definition at line 2354 of file equivclass.c.

References bms_is_subset(), bms_overlap(), EquivalenceClass::ec_members, EquivalenceClass::ec_relids, PlannerInfo::eq_classes, lfirst, list_length(), and RelOptInfo::relids.

Referenced by build_join_rel(), and generate_base_implied_equalities().

2355 {
2356  ListCell *lc1;
2357 
2358  foreach(lc1, root->eq_classes)
2359  {
2360  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2361 
2362  /*
2363  * Won't generate joinclauses if single-member (this test covers the
2364  * volatile case too)
2365  */
2366  if (list_length(ec->ec_members) <= 1)
2367  continue;
2368 
2369  /*
2370  * Per the comment in have_relevant_eclass_joinclause, it's sufficient
2371  * to find an EC that mentions both this rel and some other rel.
2372  */
2373  if (bms_overlap(rel1->relids, ec->ec_relids) &&
2374  !bms_is_subset(ec->ec_relids, rel1->relids))
2375  return true;
2376  }
2377 
2378  return false;
2379 }
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
Relids ec_relids
Definition: relation.h:740
Relids relids
Definition: relation.h:494
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:234
static int list_length(const List *l)
Definition: pg_list.h:89
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
List * ec_members
Definition: relation.h:737
bool have_relevant_eclass_joinclause ( PlannerInfo root,
RelOptInfo rel1,
RelOptInfo rel2 
)

Definition at line 2300 of file equivclass.c.

References bms_overlap(), EquivalenceClass::ec_members, EquivalenceClass::ec_relids, PlannerInfo::eq_classes, lfirst, list_length(), and RelOptInfo::relids.

Referenced by have_relevant_joinclause().

2302 {
2303  ListCell *lc1;
2304 
2305  foreach(lc1, root->eq_classes)
2306  {
2307  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2308 
2309  /*
2310  * Won't generate joinclauses if single-member (this test covers the
2311  * volatile case too)
2312  */
2313  if (list_length(ec->ec_members) <= 1)
2314  continue;
2315 
2316  /*
2317  * We do not need to examine the individual members of the EC, because
2318  * all that we care about is whether each rel overlaps the relids of
2319  * at least one member, and a test on ec_relids is sufficient to prove
2320  * that. (As with have_relevant_joinclause(), it is not necessary
2321  * that the EC be able to form a joinclause relating exactly the two
2322  * given rels, only that it be able to form a joinclause mentioning
2323  * both, and this will surely be true if both of them overlap
2324  * ec_relids.)
2325  *
2326  * Note we don't test ec_broken; if we did, we'd need a separate code
2327  * path to look through ec_sources. Checking the membership anyway is
2328  * OK as a possibly-overoptimistic heuristic.
2329  *
2330  * We don't test ec_has_const either, even though a const eclass won't
2331  * generate real join clauses. This is because if we had "WHERE a.x =
2332  * b.y and a.x = 42", it is worth considering a join between a and b,
2333  * since the join result is likely to be small even though it'll end
2334  * up being an unqualified nestloop.
2335  */
2336  if (bms_overlap(rel1->relids, ec->ec_relids) &&
2337  bms_overlap(rel2->relids, ec->ec_relids))
2338  return true;
2339  }
2340 
2341  return false;
2342 }
Relids ec_relids
Definition: relation.h:740
Relids relids
Definition: relation.h:494
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:234
static int list_length(const List *l)
Definition: pg_list.h:89
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
List * ec_members
Definition: relation.h:737
bool is_redundant_derived_clause ( RestrictInfo rinfo,
List clauselist 
)

Definition at line 2448 of file equivclass.c.

References lfirst, NULL, and RestrictInfo::parent_ec.

Referenced by create_indexscan_plan(), extract_nonindex_conditions(), and has_indexed_join_quals().

2449 {
2450  EquivalenceClass *parent_ec = rinfo->parent_ec;
2451  ListCell *lc;
2452 
2453  /* Fail if it's not a potentially-redundant clause from some EC */
2454  if (parent_ec == NULL)
2455  return false;
2456 
2457  foreach(lc, clauselist)
2458  {
2459  RestrictInfo *otherrinfo = (RestrictInfo *) lfirst(lc);
2460 
2461  if (otherrinfo->parent_ec == parent_ec)
2462  return true;
2463  }
2464 
2465  return false;
2466 }
EquivalenceClass * parent_ec
Definition: relation.h:1733
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
EquivalenceClass* match_eclasses_to_foreign_key_col ( PlannerInfo root,
ForeignKeyOptInfo fkinfo,
int  colno 
)

Definition at line 1990 of file equivclass.c.

References ForeignKeyOptInfo::con_relid, ForeignKeyOptInfo::confkey, ForeignKeyOptInfo::conkey, ForeignKeyOptInfo::conpfeqop, EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_members, EquivalenceClass::ec_opfamilies, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, PlannerInfo::eq_classes, equal(), get_mergejoin_opfamilies(), IsA, lfirst, NIL, NULL, ForeignKeyOptInfo::ref_relid, RangeQueryClause::var, Var::varattno, and Var::varno.

Referenced by match_foreign_keys_to_quals().

1993 {
1994  Index var1varno = fkinfo->con_relid;
1995  AttrNumber var1attno = fkinfo->conkey[colno];
1996  Index var2varno = fkinfo->ref_relid;
1997  AttrNumber var2attno = fkinfo->confkey[colno];
1998  Oid eqop = fkinfo->conpfeqop[colno];
1999  List *opfamilies = NIL; /* compute only if needed */
2000  ListCell *lc1;
2001 
2002  foreach(lc1, root->eq_classes)
2003  {
2004  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2005  bool item1member = false;
2006  bool item2member = false;
2007  ListCell *lc2;
2008 
2009  /* Never match to a volatile EC */
2010  if (ec->ec_has_volatile)
2011  continue;
2012  /* Note: it seems okay to match to "broken" eclasses here */
2013 
2014  foreach(lc2, ec->ec_members)
2015  {
2017  Var *var;
2018 
2019  if (em->em_is_child)
2020  continue; /* ignore children here */
2021 
2022  /* EM must be a Var, possibly with RelabelType */
2023  var = (Var *) em->em_expr;
2024  while (var && IsA(var, RelabelType))
2025  var = (Var *) ((RelabelType *) var)->arg;
2026  if (!(var && IsA(var, Var)))
2027  continue;
2028 
2029  /* Match? */
2030  if (var->varno == var1varno && var->varattno == var1attno)
2031  item1member = true;
2032  else if (var->varno == var2varno && var->varattno == var2attno)
2033  item2member = true;
2034 
2035  /* Have we found both PK and FK column in this EC? */
2036  if (item1member && item2member)
2037  {
2038  /*
2039  * Succeed if eqop matches EC's opfamilies. We could test
2040  * this before scanning the members, but it's probably cheaper
2041  * to test for member matches first.
2042  */
2043  if (opfamilies == NIL) /* compute if we didn't already */
2044  opfamilies = get_mergejoin_opfamilies(eqop);
2045  if (equal(opfamilies, ec->ec_opfamilies))
2046  return ec;
2047  /* Otherwise, done with this EC, move on to the next */
2048  break;
2049  }
2050  }
2051  }
2052  return NULL;
2053 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:557
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2946
List * get_mergejoin_opfamilies(Oid opno)
Definition: lsyscache.c:363
AttrNumber varattno
Definition: primnodes.h:168
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:163
Oid conpfeqop[INDEX_MAX_KEYS]
Definition: relation.h:660
Index varno
Definition: primnodes.h:166
List * ec_opfamilies
Definition: relation.h:735
AttrNumber conkey[INDEX_MAX_KEYS]
Definition: relation.h:658
unsigned int Index
Definition: c.h:365
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:234
bool ec_has_volatile
Definition: relation.h:743
Definition: pg_list.h:45
AttrNumber confkey[INDEX_MAX_KEYS]
Definition: relation.h:659
int16 AttrNumber
Definition: attnum.h:21
List * ec_members
Definition: relation.h:737
bool process_equivalence ( PlannerInfo root,
RestrictInfo restrictinfo,
bool  below_outer_join 
)

Definition at line 107 of file equivclass.c.

References add_eq_member(), Assert, bms_intersect(), bms_is_empty(), bms_join(), PlannerInfo::canon_pathkeys, canonicalize_ec_expression(), RestrictInfo::clause, contain_nonstrict_functions(), EquivalenceClass::ec_below_outer_join, EquivalenceClass::ec_broken, EquivalenceClass::ec_collation, EquivalenceClass::ec_derives, EquivalenceClass::ec_has_const, EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_max_security, EquivalenceClass::ec_members, EquivalenceClass::ec_merged, EquivalenceClass::ec_min_security, EquivalenceClass::ec_opfamilies, EquivalenceClass::ec_relids, EquivalenceClass::ec_sortref, EquivalenceClass::ec_sources, elog, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, PlannerInfo::eq_classes, equal(), ERROR, exprType(), get_leftop(), get_rightop(), is_opclause, lappend(), RestrictInfo::leakproof, RestrictInfo::left_ec, RestrictInfo::left_em, RestrictInfo::left_relids, lfirst, list_concat(), list_delete_ptr(), list_make1, makeNode, Max, RestrictInfo::mergeopfamilies, Min, NIL, NULL, RestrictInfo::nullable_relids, op_input_types(), RestrictInfo::right_ec, RestrictInfo::right_em, RestrictInfo::right_relids, and RestrictInfo::security_level.

Referenced by distribute_qual_to_rels(), reconsider_full_join_clause(), and reconsider_outer_join_clause().

109 {
110  Expr *clause = restrictinfo->clause;
111  Oid opno,
112  collation,
113  item1_type,
114  item2_type;
115  Expr *item1;
116  Expr *item2;
117  Relids item1_relids,
118  item2_relids,
119  item1_nullable_relids,
120  item2_nullable_relids;
121  List *opfamilies;
122  EquivalenceClass *ec1,
123  *ec2;
124  EquivalenceMember *em1,
125  *em2;
126  ListCell *lc1;
127 
128  /* Should not already be marked as having generated an eclass */
129  Assert(restrictinfo->left_ec == NULL);
130  Assert(restrictinfo->right_ec == NULL);
131 
132  /* Reject if it is potentially postponable by security considerations */
133  if (restrictinfo->security_level > 0 && !restrictinfo->leakproof)
134  return false;
135 
136  /* Extract info from given clause */
137  Assert(is_opclause(clause));
138  opno = ((OpExpr *) clause)->opno;
139  collation = ((OpExpr *) clause)->inputcollid;
140  item1 = (Expr *) get_leftop(clause);
141  item2 = (Expr *) get_rightop(clause);
142  item1_relids = restrictinfo->left_relids;
143  item2_relids = restrictinfo->right_relids;
144 
145  /*
146  * Ensure both input expressions expose the desired collation (their types
147  * should be OK already); see comments for canonicalize_ec_expression.
148  */
149  item1 = canonicalize_ec_expression(item1,
150  exprType((Node *) item1),
151  collation);
152  item2 = canonicalize_ec_expression(item2,
153  exprType((Node *) item2),
154  collation);
155 
156  /*
157  * Reject clauses of the form X=X. These are not as redundant as they
158  * might seem at first glance: assuming the operator is strict, this is
159  * really an expensive way to write X IS NOT NULL. So we must not risk
160  * just losing the clause, which would be possible if there is already a
161  * single-element EquivalenceClass containing X. The case is not common
162  * enough to be worth contorting the EC machinery for, so just reject the
163  * clause and let it be processed as a normal restriction clause.
164  */
165  if (equal(item1, item2))
166  return false; /* X=X is not a useful equivalence */
167 
168  /*
169  * If below outer join, check for strictness, else reject.
170  */
171  if (below_outer_join)
172  {
173  if (!bms_is_empty(item1_relids) &&
175  return false; /* LHS is non-strict but not constant */
176  if (!bms_is_empty(item2_relids) &&
178  return false; /* RHS is non-strict but not constant */
179  }
180 
181  /* Calculate nullable-relid sets for each side of the clause */
182  item1_nullable_relids = bms_intersect(item1_relids,
183  restrictinfo->nullable_relids);
184  item2_nullable_relids = bms_intersect(item2_relids,
185  restrictinfo->nullable_relids);
186 
187  /*
188  * We use the declared input types of the operator, not exprType() of the
189  * inputs, as the nominal datatypes for opfamily lookup. This presumes
190  * that btree operators are always registered with amoplefttype and
191  * amoprighttype equal to their declared input types. We will need this
192  * info anyway to build EquivalenceMember nodes, and by extracting it now
193  * we can use type comparisons to short-circuit some equal() tests.
194  */
195  op_input_types(opno, &item1_type, &item2_type);
196 
197  opfamilies = restrictinfo->mergeopfamilies;
198 
199  /*
200  * Sweep through the existing EquivalenceClasses looking for matches to
201  * item1 and item2. These are the possible outcomes:
202  *
203  * 1. We find both in the same EC. The equivalence is already known, so
204  * there's nothing to do.
205  *
206  * 2. We find both in different ECs. Merge the two ECs together.
207  *
208  * 3. We find just one. Add the other to its EC.
209  *
210  * 4. We find neither. Make a new, two-entry EC.
211  *
212  * Note: since all ECs are built through this process or the similar
213  * search in get_eclass_for_sort_expr(), it's impossible that we'd match
214  * an item in more than one existing nonvolatile EC. So it's okay to stop
215  * at the first match.
216  */
217  ec1 = ec2 = NULL;
218  em1 = em2 = NULL;
219  foreach(lc1, root->eq_classes)
220  {
221  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
222  ListCell *lc2;
223 
224  /* Never match to a volatile EC */
225  if (cur_ec->ec_has_volatile)
226  continue;
227 
228  /*
229  * The collation has to match; check this first since it's cheaper
230  * than the opfamily comparison.
231  */
232  if (collation != cur_ec->ec_collation)
233  continue;
234 
235  /*
236  * A "match" requires matching sets of btree opfamilies. Use of
237  * equal() for this test has implications discussed in the comments
238  * for get_mergejoin_opfamilies().
239  */
240  if (!equal(opfamilies, cur_ec->ec_opfamilies))
241  continue;
242 
243  foreach(lc2, cur_ec->ec_members)
244  {
245  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
246 
247  Assert(!cur_em->em_is_child); /* no children yet */
248 
249  /*
250  * If below an outer join, don't match constants: they're not as
251  * constant as they look.
252  */
253  if ((below_outer_join || cur_ec->ec_below_outer_join) &&
254  cur_em->em_is_const)
255  continue;
256 
257  if (!ec1 &&
258  item1_type == cur_em->em_datatype &&
259  equal(item1, cur_em->em_expr))
260  {
261  ec1 = cur_ec;
262  em1 = cur_em;
263  if (ec2)
264  break;
265  }
266 
267  if (!ec2 &&
268  item2_type == cur_em->em_datatype &&
269  equal(item2, cur_em->em_expr))
270  {
271  ec2 = cur_ec;
272  em2 = cur_em;
273  if (ec1)
274  break;
275  }
276  }
277 
278  if (ec1 && ec2)
279  break;
280  }
281 
282  /* Sweep finished, what did we find? */
283 
284  if (ec1 && ec2)
285  {
286  /* If case 1, nothing to do, except add to sources */
287  if (ec1 == ec2)
288  {
289  ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
290  ec1->ec_below_outer_join |= below_outer_join;
291  ec1->ec_min_security = Min(ec1->ec_min_security,
292  restrictinfo->security_level);
293  ec1->ec_max_security = Max(ec1->ec_max_security,
294  restrictinfo->security_level);
295  /* mark the RI as associated with this eclass */
296  restrictinfo->left_ec = ec1;
297  restrictinfo->right_ec = ec1;
298  /* mark the RI as usable with this pair of EMs */
299  restrictinfo->left_em = em1;
300  restrictinfo->right_em = em2;
301  return true;
302  }
303 
304  /*
305  * Case 2: need to merge ec1 and ec2. This should never happen after
306  * we've built any canonical pathkeys; if it did, those pathkeys might
307  * be rendered non-canonical by the merge.
308  */
309  if (root->canon_pathkeys != NIL)
310  elog(ERROR, "too late to merge equivalence classes");
311 
312  /*
313  * We add ec2's items to ec1, then set ec2's ec_merged link to point
314  * to ec1 and remove ec2 from the eq_classes list. We cannot simply
315  * delete ec2 because that could leave dangling pointers in existing
316  * PathKeys. We leave it behind with a link so that the merged EC can
317  * be found.
318  */
319  ec1->ec_members = list_concat(ec1->ec_members, ec2->ec_members);
320  ec1->ec_sources = list_concat(ec1->ec_sources, ec2->ec_sources);
321  ec1->ec_derives = list_concat(ec1->ec_derives, ec2->ec_derives);
322  ec1->ec_relids = bms_join(ec1->ec_relids, ec2->ec_relids);
323  ec1->ec_has_const |= ec2->ec_has_const;
324  /* can't need to set has_volatile */
326  ec1->ec_min_security = Min(ec1->ec_min_security,
327  ec2->ec_min_security);
328  ec1->ec_max_security = Max(ec1->ec_max_security,
329  ec2->ec_max_security);
330  ec2->ec_merged = ec1;
331  root->eq_classes = list_delete_ptr(root->eq_classes, ec2);
332  /* just to avoid debugging confusion w/ dangling pointers: */
333  ec2->ec_members = NIL;
334  ec2->ec_sources = NIL;
335  ec2->ec_derives = NIL;
336  ec2->ec_relids = NULL;
337  ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
338  ec1->ec_below_outer_join |= below_outer_join;
339  ec1->ec_min_security = Min(ec1->ec_min_security,
340  restrictinfo->security_level);
341  ec1->ec_max_security = Max(ec1->ec_max_security,
342  restrictinfo->security_level);
343  /* mark the RI as associated with this eclass */
344  restrictinfo->left_ec = ec1;
345  restrictinfo->right_ec = ec1;
346  /* mark the RI as usable with this pair of EMs */
347  restrictinfo->left_em = em1;
348  restrictinfo->right_em = em2;
349  }
350  else if (ec1)
351  {
352  /* Case 3: add item2 to ec1 */
353  em2 = add_eq_member(ec1, item2, item2_relids, item2_nullable_relids,
354  false, item2_type);
355  ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
356  ec1->ec_below_outer_join |= below_outer_join;
357  ec1->ec_min_security = Min(ec1->ec_min_security,
358  restrictinfo->security_level);
359  ec1->ec_max_security = Max(ec1->ec_max_security,
360  restrictinfo->security_level);
361  /* mark the RI as associated with this eclass */
362  restrictinfo->left_ec = ec1;
363  restrictinfo->right_ec = ec1;
364  /* mark the RI as usable with this pair of EMs */
365  restrictinfo->left_em = em1;
366  restrictinfo->right_em = em2;
367  }
368  else if (ec2)
369  {
370  /* Case 3: add item1 to ec2 */
371  em1 = add_eq_member(ec2, item1, item1_relids, item1_nullable_relids,
372  false, item1_type);
373  ec2->ec_sources = lappend(ec2->ec_sources, restrictinfo);
374  ec2->ec_below_outer_join |= below_outer_join;
375  ec2->ec_min_security = Min(ec2->ec_min_security,
376  restrictinfo->security_level);
377  ec2->ec_max_security = Max(ec2->ec_max_security,
378  restrictinfo->security_level);
379  /* mark the RI as associated with this eclass */
380  restrictinfo->left_ec = ec2;
381  restrictinfo->right_ec = ec2;
382  /* mark the RI as usable with this pair of EMs */
383  restrictinfo->left_em = em1;
384  restrictinfo->right_em = em2;
385  }
386  else
387  {
388  /* Case 4: make a new, two-entry EC */
390 
391  ec->ec_opfamilies = opfamilies;
392  ec->ec_collation = collation;
393  ec->ec_members = NIL;
394  ec->ec_sources = list_make1(restrictinfo);
395  ec->ec_derives = NIL;
396  ec->ec_relids = NULL;
397  ec->ec_has_const = false;
398  ec->ec_has_volatile = false;
399  ec->ec_below_outer_join = below_outer_join;
400  ec->ec_broken = false;
401  ec->ec_sortref = 0;
402  ec->ec_min_security = restrictinfo->security_level;
403  ec->ec_max_security = restrictinfo->security_level;
404  ec->ec_merged = NULL;
405  em1 = add_eq_member(ec, item1, item1_relids, item1_nullable_relids,
406  false, item1_type);
407  em2 = add_eq_member(ec, item2, item2_relids, item2_nullable_relids,
408  false, item2_type);
409 
410  root->eq_classes = lappend(root->eq_classes, ec);
411 
412  /* mark the RI as associated with this eclass */
413  restrictinfo->left_ec = ec;
414  restrictinfo->right_ec = ec;
415  /* mark the RI as usable with this pair of EMs */
416  restrictinfo->left_em = em1;
417  restrictinfo->right_em = em2;
418  }
419 
420  return true;
421 }
#define NIL
Definition: pg_list.h:69
Index security_level
Definition: relation.h:1711
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2946
bool leakproof
Definition: relation.h:1709
Index ec_min_security
Definition: relation.h:747
List * ec_derives
Definition: relation.h:739
#define Min(x, y)
Definition: c.h:806
Index ec_sortref
Definition: relation.h:746
Definition: nodes.h:506
Relids left_relids
Definition: relation.h:1726
List * list_concat(List *list1, List *list2)
Definition: list.c:321
bool ec_below_outer_join
Definition: relation.h:744
EquivalenceClass * right_ec
Definition: relation.h:1748
Index ec_max_security
Definition: relation.h:748
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:590
unsigned int Oid
Definition: postgres_ext.h:31
List * mergeopfamilies
Definition: relation.h:1744
#define list_make1(x1)
Definition: pg_list.h:133
#define ERROR
Definition: elog.h:43
#define is_opclause(clause)
Definition: clauses.h:20
EquivalenceMember * left_em
Definition: relation.h:1749
Node * get_leftop(const Expr *clause)
Definition: clauses.c:198
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:838
Expr * canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
Definition: equivclass.c:456
List * ec_sources
Definition: relation.h:738
Relids ec_relids
Definition: relation.h:740
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1135
List * canon_pathkeys
Definition: relation.h:236
List * lappend(List *list, void *datum)
Definition: list.c:128
EquivalenceMember * right_em
Definition: relation.h:1750
Expr * clause
Definition: relation.h:1699
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
List * ec_opfamilies
Definition: relation.h:735
Relids nullable_relids
Definition: relation.h:1723
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:252
#define Max(x, y)
Definition: c.h:800
#define makeNode(_type_)
Definition: nodes.h:554
Relids right_relids
Definition: relation.h:1727
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:234
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
bool ec_has_volatile
Definition: relation.h:743
Node * get_rightop(const Expr *clause)
Definition: clauses.c:215
EquivalenceClass * left_ec
Definition: relation.h:1747
static EquivalenceMember * add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids, Relids nullable_relids, bool is_child, Oid datatype)
Definition: equivclass.c:505
#define elog
Definition: elog.h:219
bool contain_nonstrict_functions(Node *clause)
Definition: clauses.c:1234
Definition: pg_list.h:45
struct EquivalenceClass * ec_merged
Definition: relation.h:749
List * ec_members
Definition: relation.h:737
static bool reconsider_full_join_clause ( PlannerInfo root,
RestrictInfo rinfo 
)
static

Definition at line 1766 of file equivclass.c.

References CoalesceExpr::args, Assert, bms_copy(), bms_intersect(), build_implied_join_equality(), RestrictInfo::clause, EquivalenceClass::ec_collation, EquivalenceClass::ec_has_const, EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_members, EquivalenceClass::ec_min_security, EquivalenceClass::ec_opfamilies, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, PlannerInfo::eq_classes, equal(), get_leftop(), get_rightop(), is_opclause, IsA, RestrictInfo::left_relids, lfirst, linitial, list_delete_ptr(), list_length(), lsecond, RestrictInfo::mergeopfamilies, NULL, RestrictInfo::nullable_relids, OidIsValid, op_input_types(), RestrictInfo::outerjoin_delayed, process_equivalence(), RestrictInfo::right_relids, and select_equality_operator().

Referenced by reconsider_outer_join_clauses().

1767 {
1768  Expr *leftvar;
1769  Expr *rightvar;
1770  Oid opno,
1771  collation,
1772  left_type,
1773  right_type;
1774  Relids left_relids,
1775  right_relids,
1776  left_nullable_relids,
1777  right_nullable_relids;
1778  ListCell *lc1;
1779 
1780  /* Can't use an outerjoin_delayed clause here */
1781  if (rinfo->outerjoin_delayed)
1782  return false;
1783 
1784  /* Extract needed info from the clause */
1785  Assert(is_opclause(rinfo->clause));
1786  opno = ((OpExpr *) rinfo->clause)->opno;
1787  collation = ((OpExpr *) rinfo->clause)->inputcollid;
1788  op_input_types(opno, &left_type, &right_type);
1789  leftvar = (Expr *) get_leftop(rinfo->clause);
1790  rightvar = (Expr *) get_rightop(rinfo->clause);
1791  left_relids = rinfo->left_relids;
1792  right_relids = rinfo->right_relids;
1793  left_nullable_relids = bms_intersect(left_relids,
1794  rinfo->nullable_relids);
1795  right_nullable_relids = bms_intersect(right_relids,
1796  rinfo->nullable_relids);
1797 
1798  foreach(lc1, root->eq_classes)
1799  {
1800  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
1801  EquivalenceMember *coal_em = NULL;
1802  bool match;
1803  bool matchleft;
1804  bool matchright;
1805  ListCell *lc2;
1806 
1807  /* Ignore EC unless it contains pseudoconstants */
1808  if (!cur_ec->ec_has_const)
1809  continue;
1810  /* Never match to a volatile EC */
1811  if (cur_ec->ec_has_volatile)
1812  continue;
1813  /* It has to match the outer-join clause as to semantics, too */
1814  if (collation != cur_ec->ec_collation)
1815  continue;
1816  if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
1817  continue;
1818 
1819  /*
1820  * Does it contain a COALESCE(leftvar, rightvar) construct?
1821  *
1822  * We can assume the COALESCE() inputs are in the same order as the
1823  * join clause, since both were automatically generated in the cases
1824  * we care about.
1825  *
1826  * XXX currently this may fail to match in cross-type cases because
1827  * the COALESCE will contain typecast operations while the join clause
1828  * may not (if there is a cross-type mergejoin operator available for
1829  * the two column types). Is it OK to strip implicit coercions from
1830  * the COALESCE arguments?
1831  */
1832  match = false;
1833  foreach(lc2, cur_ec->ec_members)
1834  {
1835  coal_em = (EquivalenceMember *) lfirst(lc2);
1836  Assert(!coal_em->em_is_child); /* no children yet */
1837  if (IsA(coal_em->em_expr, CoalesceExpr))
1838  {
1839  CoalesceExpr *cexpr = (CoalesceExpr *) coal_em->em_expr;
1840  Node *cfirst;
1841  Node *csecond;
1842 
1843  if (list_length(cexpr->args) != 2)
1844  continue;
1845  cfirst = (Node *) linitial(cexpr->args);
1846  csecond = (Node *) lsecond(cexpr->args);
1847 
1848  if (equal(leftvar, cfirst) && equal(rightvar, csecond))
1849  {
1850  match = true;
1851  break;
1852  }
1853  }
1854  }
1855  if (!match)
1856  continue; /* no match, so ignore this EC */
1857 
1858  /*
1859  * Yes it does! Try to generate clauses LEFTVAR = CONSTANT and
1860  * RIGHTVAR = CONSTANT for each CONSTANT in the EC. Note that we must
1861  * succeed with at least one constant for each var before we can
1862  * decide to throw away the outer-join clause.
1863  */
1864  matchleft = matchright = false;
1865  foreach(lc2, cur_ec->ec_members)
1866  {
1867  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
1868  Oid eq_op;
1869  RestrictInfo *newrinfo;
1870 
1871  if (!cur_em->em_is_const)
1872  continue; /* ignore non-const members */
1873  eq_op = select_equality_operator(cur_ec,
1874  left_type,
1875  cur_em->em_datatype);
1876  if (OidIsValid(eq_op))
1877  {
1878  newrinfo = build_implied_join_equality(eq_op,
1879  cur_ec->ec_collation,
1880  leftvar,
1881  cur_em->em_expr,
1882  bms_copy(left_relids),
1883  bms_copy(left_nullable_relids),
1884  cur_ec->ec_min_security);
1885  if (process_equivalence(root, newrinfo, true))
1886  matchleft = true;
1887  }
1888  eq_op = select_equality_operator(cur_ec,
1889  right_type,
1890  cur_em->em_datatype);
1891  if (OidIsValid(eq_op))
1892  {
1893  newrinfo = build_implied_join_equality(eq_op,
1894  cur_ec->ec_collation,
1895  rightvar,
1896  cur_em->em_expr,
1897  bms_copy(right_relids),
1898  bms_copy(right_nullable_relids),
1899  cur_ec->ec_min_security);
1900  if (process_equivalence(root, newrinfo, true))
1901  matchright = true;
1902  }
1903  }
1904 
1905  /*
1906  * If we were able to equate both vars to constants, we're done, and
1907  * we can throw away the full-join clause as redundant. Moreover, we
1908  * can remove the COALESCE entry from the EC, since the added
1909  * restrictions ensure it will always have the expected value. (We
1910  * don't bother trying to update ec_relids or ec_sources.)
1911  */
1912  if (matchleft && matchright)
1913  {
1914  cur_ec->ec_members = list_delete_ptr(cur_ec->ec_members, coal_em);
1915  return true;
1916  }
1917 
1918  /*
1919  * Otherwise, fall out of the search loop, since we know the COALESCE
1920  * appears in at most one EC (XXX might stop being true if we allow
1921  * stripping of coercions above?)
1922  */
1923  break;
1924  }
1925 
1926  return false; /* failed to make any deduction */
1927 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:557
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:111
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2946
Index ec_min_security
Definition: relation.h:747
Definition: nodes.h:506
Relids left_relids
Definition: relation.h:1726
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:590
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
List * mergeopfamilies
Definition: relation.h:1744
#define lsecond(l)
Definition: pg_list.h:114
#define linitial(l)
Definition: pg_list.h:110
#define is_opclause(clause)
Definition: clauses.h:20
List * args
Definition: primnodes.h:1045
Node * get_leftop(const Expr *clause)
Definition: clauses.c:198
bool outerjoin_delayed
Definition: relation.h:1703
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1344
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1135
Expr * clause
Definition: relation.h:1699
HashCompareFunc match
Definition: dynahash.c:198
List * ec_opfamilies
Definition: relation.h:735
Relids nullable_relids
Definition: relation.h:1723
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:252
Relids right_relids
Definition: relation.h:1727
bool process_equivalence(PlannerInfo *root, RestrictInfo *restrictinfo, bool below_outer_join)
Definition: equivclass.c:107
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:234
static int list_length(const List *l)
Definition: pg_list.h:89
bool ec_has_volatile
Definition: relation.h:743
RestrictInfo * build_implied_join_equality(Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
Definition: initsplan.c:2354
Node * get_rightop(const Expr *clause)
Definition: clauses.c:215
List * ec_members
Definition: relation.h:737
static bool reconsider_outer_join_clause ( PlannerInfo root,
RestrictInfo rinfo,
bool  outer_on_left 
)
static

Definition at line 1641 of file equivclass.c.

References Assert, bms_copy(), bms_intersect(), build_implied_join_equality(), RestrictInfo::clause, EquivalenceClass::ec_collation, EquivalenceClass::ec_has_const, EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_members, EquivalenceClass::ec_min_security, EquivalenceClass::ec_opfamilies, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, PlannerInfo::eq_classes, equal(), get_leftop(), get_rightop(), is_opclause, RestrictInfo::left_relids, lfirst, RestrictInfo::mergeopfamilies, RestrictInfo::nullable_relids, OidIsValid, op_input_types(), op_strict(), RestrictInfo::outerjoin_delayed, process_equivalence(), RestrictInfo::right_relids, and select_equality_operator().

Referenced by reconsider_outer_join_clauses().

1643 {
1644  Expr *outervar,
1645  *innervar;
1646  Oid opno,
1647  collation,
1648  left_type,
1649  right_type,
1650  inner_datatype;
1651  Relids inner_relids,
1652  inner_nullable_relids;
1653  ListCell *lc1;
1654 
1655  Assert(is_opclause(rinfo->clause));
1656  opno = ((OpExpr *) rinfo->clause)->opno;
1657  collation = ((OpExpr *) rinfo->clause)->inputcollid;
1658 
1659  /* If clause is outerjoin_delayed, operator must be strict */
1660  if (rinfo->outerjoin_delayed && !op_strict(opno))
1661  return false;
1662 
1663  /* Extract needed info from the clause */
1664  op_input_types(opno, &left_type, &right_type);
1665  if (outer_on_left)
1666  {
1667  outervar = (Expr *) get_leftop(rinfo->clause);
1668  innervar = (Expr *) get_rightop(rinfo->clause);
1669  inner_datatype = right_type;
1670  inner_relids = rinfo->right_relids;
1671  }
1672  else
1673  {
1674  outervar = (Expr *) get_rightop(rinfo->clause);
1675  innervar = (Expr *) get_leftop(rinfo->clause);
1676  inner_datatype = left_type;
1677  inner_relids = rinfo->left_relids;
1678  }
1679  inner_nullable_relids = bms_intersect(inner_relids,
1680  rinfo->nullable_relids);
1681 
1682  /* Scan EquivalenceClasses for a match to outervar */
1683  foreach(lc1, root->eq_classes)
1684  {
1685  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
1686  bool match;
1687  ListCell *lc2;
1688 
1689  /* Ignore EC unless it contains pseudoconstants */
1690  if (!cur_ec->ec_has_const)
1691  continue;
1692  /* Never match to a volatile EC */
1693  if (cur_ec->ec_has_volatile)
1694  continue;
1695  /* It has to match the outer-join clause as to semantics, too */
1696  if (collation != cur_ec->ec_collation)
1697  continue;
1698  if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
1699  continue;
1700  /* Does it contain a match to outervar? */
1701  match = false;
1702  foreach(lc2, cur_ec->ec_members)
1703  {
1704  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
1705 
1706  Assert(!cur_em->em_is_child); /* no children yet */
1707  if (equal(outervar, cur_em->em_expr))
1708  {
1709  match = true;
1710  break;
1711  }
1712  }
1713  if (!match)
1714  continue; /* no match, so ignore this EC */
1715 
1716  /*
1717  * Yes it does! Try to generate a clause INNERVAR = CONSTANT for each
1718  * CONSTANT in the EC. Note that we must succeed with at least one
1719  * constant before we can decide to throw away the outer-join clause.
1720  */
1721  match = false;
1722  foreach(lc2, cur_ec->ec_members)
1723  {
1724  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
1725  Oid eq_op;
1726  RestrictInfo *newrinfo;
1727 
1728  if (!cur_em->em_is_const)
1729  continue; /* ignore non-const members */
1730  eq_op = select_equality_operator(cur_ec,
1731  inner_datatype,
1732  cur_em->em_datatype);
1733  if (!OidIsValid(eq_op))
1734  continue; /* can't generate equality */
1735  newrinfo = build_implied_join_equality(eq_op,
1736  cur_ec->ec_collation,
1737  innervar,
1738  cur_em->em_expr,
1739  bms_copy(inner_relids),
1740  bms_copy(inner_nullable_relids),
1741  cur_ec->ec_min_security);
1742  if (process_equivalence(root, newrinfo, true))
1743  match = true;
1744  }
1745 
1746  /*
1747  * If we were able to equate INNERVAR to any constant, report success.
1748  * Otherwise, fall out of the search loop, since we know the OUTERVAR
1749  * appears in at most one EC.
1750  */
1751  if (match)
1752  return true;
1753  else
1754  break;
1755  }
1756 
1757  return false; /* failed to make any deduction */
1758 }
bool op_strict(Oid opno)
Definition: lsyscache.c:1249
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:111
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2946
Index ec_min_security
Definition: relation.h:747
Relids left_relids
Definition: relation.h:1726
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
List * mergeopfamilies
Definition: relation.h:1744
#define is_opclause(clause)
Definition: clauses.h:20
Node * get_leftop(const Expr *clause)
Definition: clauses.c:198
bool outerjoin_delayed
Definition: relation.h:1703
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1344
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1135
Expr * clause
Definition: relation.h:1699
HashCompareFunc match
Definition: dynahash.c:198
List * ec_opfamilies
Definition: relation.h:735
Relids nullable_relids
Definition: relation.h:1723
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:252
Relids right_relids
Definition: relation.h:1727
bool process_equivalence(PlannerInfo *root, RestrictInfo *restrictinfo, bool below_outer_join)
Definition: equivclass.c:107
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:234
bool ec_has_volatile
Definition: relation.h:743
RestrictInfo * build_implied_join_equality(Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
Definition: initsplan.c:2354
Node * get_rightop(const Expr *clause)
Definition: clauses.c:215
List * ec_members
Definition: relation.h:737
void reconsider_outer_join_clauses ( PlannerInfo root)

Definition at line 1532 of file equivclass.c.

References distribute_restrictinfo_to_rels(), PlannerInfo::full_join_clauses, PlannerInfo::left_join_clauses, lfirst, list_delete_cell(), list_head(), lnext, next, RestrictInfo::norm_selec, NULL, RestrictInfo::outer_selec, reconsider_full_join_clause(), reconsider_outer_join_clause(), and PlannerInfo::right_join_clauses.

Referenced by query_planner().

1533 {
1534  bool found;
1535  ListCell *cell;
1536  ListCell *prev;
1537  ListCell *next;
1538 
1539  /* Outer loop repeats until we find no more deductions */
1540  do
1541  {
1542  found = false;
1543 
1544  /* Process the LEFT JOIN clauses */
1545  prev = NULL;
1546  for (cell = list_head(root->left_join_clauses); cell; cell = next)
1547  {
1548  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1549 
1550  next = lnext(cell);
1551  if (reconsider_outer_join_clause(root, rinfo, true))
1552  {
1553  found = true;
1554  /* remove it from the list */
1555  root->left_join_clauses =
1556  list_delete_cell(root->left_join_clauses, cell, prev);
1557  /* we throw it back anyway (see notes above) */
1558  /* but the thrown-back clause has no extra selectivity */
1559  rinfo->norm_selec = 2.0;
1560  rinfo->outer_selec = 1.0;
1561  distribute_restrictinfo_to_rels(root, rinfo);
1562  }
1563  else
1564  prev = cell;
1565  }
1566 
1567  /* Process the RIGHT JOIN clauses */
1568  prev = NULL;
1569  for (cell = list_head(root->right_join_clauses); cell; cell = next)
1570  {
1571  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1572 
1573  next = lnext(cell);
1574  if (reconsider_outer_join_clause(root, rinfo, false))
1575  {
1576  found = true;
1577  /* remove it from the list */
1578  root->right_join_clauses =
1579  list_delete_cell(root->right_join_clauses, cell, prev);
1580  /* we throw it back anyway (see notes above) */
1581  /* but the thrown-back clause has no extra selectivity */
1582  rinfo->norm_selec = 2.0;
1583  rinfo->outer_selec = 1.0;
1584  distribute_restrictinfo_to_rels(root, rinfo);
1585  }
1586  else
1587  prev = cell;
1588  }
1589 
1590  /* Process the FULL JOIN clauses */
1591  prev = NULL;
1592  for (cell = list_head(root->full_join_clauses); cell; cell = next)
1593  {
1594  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1595 
1596  next = lnext(cell);
1597  if (reconsider_full_join_clause(root, rinfo))
1598  {
1599  found = true;
1600  /* remove it from the list */
1601  root->full_join_clauses =
1602  list_delete_cell(root->full_join_clauses, cell, prev);
1603  /* we throw it back anyway (see notes above) */
1604  /* but the thrown-back clause has no extra selectivity */
1605  rinfo->norm_selec = 2.0;
1606  rinfo->outer_selec = 1.0;
1607  distribute_restrictinfo_to_rels(root, rinfo);
1608  }
1609  else
1610  prev = cell;
1611  }
1612  } while (found);
1613 
1614  /* Now, any remaining clauses have to be thrown back */
1615  foreach(cell, root->left_join_clauses)
1616  {
1617  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1618 
1619  distribute_restrictinfo_to_rels(root, rinfo);
1620  }
1621  foreach(cell, root->right_join_clauses)
1622  {
1623  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1624 
1625  distribute_restrictinfo_to_rels(root, rinfo);
1626  }
1627  foreach(cell, root->full_join_clauses)
1628  {
1629  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1630 
1631  distribute_restrictinfo_to_rels(root, rinfo);
1632  }
1633 }
static bool reconsider_full_join_clause(PlannerInfo *root, RestrictInfo *rinfo)
Definition: equivclass.c:1766
static int32 next
Definition: blutils.c:210
static bool reconsider_outer_join_clause(PlannerInfo *root, RestrictInfo *rinfo, bool outer_on_left)
Definition: equivclass.c:1641
Selectivity norm_selec
Definition: relation.h:1737
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2205
List * left_join_clauses
Definition: relation.h:238
List * full_join_clauses
Definition: relation.h:246
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
Selectivity outer_selec
Definition: relation.h:1740
#define lnext(lc)
Definition: pg_list.h:105
List * list_delete_cell(List *list, ListCell *cell, ListCell *prev)
Definition: list.c:528
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
List * right_join_clauses
Definition: relation.h:242
static Oid select_equality_operator ( EquivalenceClass ec,
Oid  lefttype,
Oid  righttype 
)
static

Definition at line 1344 of file equivclass.c.

References BTEqualStrategyNumber, EquivalenceClass::ec_max_security, EquivalenceClass::ec_opfamilies, get_func_leakproof(), get_opcode(), get_opfamily_member(), InvalidOid, lfirst_oid, and OidIsValid.

Referenced by generate_base_implied_equalities_const(), generate_base_implied_equalities_no_const(), generate_implied_equalities_for_column(), generate_join_implied_equalities_normal(), reconsider_full_join_clause(), and reconsider_outer_join_clause().

1345 {
1346  ListCell *lc;
1347 
1348  foreach(lc, ec->ec_opfamilies)
1349  {
1350  Oid opfamily = lfirst_oid(lc);
1351  Oid opno;
1352 
1353  opno = get_opfamily_member(opfamily, lefttype, righttype,
1355  if (!OidIsValid(opno))
1356  continue;
1357  /* If no barrier quals in query, don't worry about leaky operators */
1358  if (ec->ec_max_security == 0)
1359  return opno;
1360  /* Otherwise, insist that selected operators be leakproof */
1361  if (get_func_leakproof(get_opcode(opno)))
1362  return opno;
1363  }
1364  return InvalidOid;
1365 }
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:1590
Index ec_max_security
Definition: relation.h:748
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
List * ec_opfamilies
Definition: relation.h:735
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1062
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define lfirst_oid(lc)
Definition: pg_list.h:108