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 2070 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().

2074 {
2075  ListCell *lc1;
2076 
2077  foreach(lc1, root->eq_classes)
2078  {
2079  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
2080  ListCell *lc2;
2081 
2082  /*
2083  * If this EC contains a volatile expression, then generating child
2084  * EMs would be downright dangerous, so skip it. We rely on a
2085  * volatile EC having only one EM.
2086  */
2087  if (cur_ec->ec_has_volatile)
2088  continue;
2089 
2090  /*
2091  * No point in searching if parent rel not mentioned in eclass; but we
2092  * can't tell that for sure if parent rel is itself a child.
2093  */
2094  if (parent_rel->reloptkind == RELOPT_BASEREL &&
2095  !bms_is_subset(parent_rel->relids, cur_ec->ec_relids))
2096  continue;
2097 
2098  foreach(lc2, cur_ec->ec_members)
2099  {
2100  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2101 
2102  if (cur_em->em_is_const)
2103  continue; /* ignore consts here */
2104 
2105  /* Does it reference parent_rel? */
2106  if (bms_overlap(cur_em->em_relids, parent_rel->relids))
2107  {
2108  /* Yes, generate transformed child version */
2109  Expr *child_expr;
2110  Relids new_relids;
2111  Relids new_nullable_relids;
2112 
2113  child_expr = (Expr *)
2115  (Node *) cur_em->em_expr,
2116  1, &appinfo);
2117 
2118  /*
2119  * Transform em_relids to match. Note we do *not* do
2120  * pull_varnos(child_expr) here, as for example the
2121  * transformation might have substituted a constant, but we
2122  * don't want the child member to be marked as constant.
2123  */
2124  new_relids = bms_difference(cur_em->em_relids,
2125  parent_rel->relids);
2126  new_relids = bms_add_members(new_relids, child_rel->relids);
2127 
2128  /*
2129  * And likewise for nullable_relids. Note this code assumes
2130  * parent and child relids are singletons.
2131  */
2132  new_nullable_relids = cur_em->em_nullable_relids;
2133  if (bms_overlap(new_nullable_relids, parent_rel->relids))
2134  {
2135  new_nullable_relids = bms_difference(new_nullable_relids,
2136  parent_rel->relids);
2137  new_nullable_relids = bms_add_members(new_nullable_relids,
2138  child_rel->relids);
2139  }
2140 
2141  (void) add_eq_member(cur_ec, child_expr,
2142  new_relids, new_nullable_relids,
2143  true, cur_em->em_datatype);
2144  }
2145  }
2146  }
2147 }
RelOptKind reloptkind
Definition: relation.h:568
Relids em_nullable_relids
Definition: relation.h:878
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:284
Definition: nodes.h:509
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
Relids ec_relids
Definition: relation.h:831
Relids relids
Definition: relation.h:571
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: prepunion.c:1943
Relids em_relids
Definition: relation.h:877
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:235
bool ec_has_volatile
Definition: relation.h:834
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:828
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:878
Relids ec_relids
Definition: relation.h:831
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:877
#define makeNode(_type_)
Definition: nodes.h:557
#define Assert(condition)
Definition: c.h:664
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:755
List * ec_members
Definition: relation.h:828
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:560
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
Definition: nodes.h:509
unsigned int Oid
Definition: postgres_ext.h:31
#define IsPolymorphicType(typid)
Definition: pg_type.h:745
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:720
void * arg
static RestrictInfo * create_join_clause ( PlannerInfo root,
EquivalenceClass ec,
Oid  opno,
EquivalenceMember leftem,
EquivalenceMember rightem,
EquivalenceClass parent_ec 
)
static

Definition at line 1381 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().

1386 {
1387  RestrictInfo *rinfo;
1388  ListCell *lc;
1389  MemoryContext oldcontext;
1390 
1391  /*
1392  * Search to see if we already built a RestrictInfo for this pair of
1393  * EquivalenceMembers. We can use either original source clauses or
1394  * previously-derived clauses. The check on opno is probably redundant,
1395  * but be safe ...
1396  */
1397  foreach(lc, ec->ec_sources)
1398  {
1399  rinfo = (RestrictInfo *) lfirst(lc);
1400  if (rinfo->left_em == leftem &&
1401  rinfo->right_em == rightem &&
1402  rinfo->parent_ec == parent_ec &&
1403  opno == ((OpExpr *) rinfo->clause)->opno)
1404  return rinfo;
1405  }
1406 
1407  foreach(lc, ec->ec_derives)
1408  {
1409  rinfo = (RestrictInfo *) lfirst(lc);
1410  if (rinfo->left_em == leftem &&
1411  rinfo->right_em == rightem &&
1412  rinfo->parent_ec == parent_ec &&
1413  opno == ((OpExpr *) rinfo->clause)->opno)
1414  return rinfo;
1415  }
1416 
1417  /*
1418  * Not there, so build it, in planner context so we can re-use it. (Not
1419  * important in normal planning, but definitely so in GEQO.)
1420  */
1421  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
1422 
1423  rinfo = build_implied_join_equality(opno,
1424  ec->ec_collation,
1425  leftem->em_expr,
1426  rightem->em_expr,
1427  bms_union(leftem->em_relids,
1428  rightem->em_relids),
1429  bms_union(leftem->em_nullable_relids,
1430  rightem->em_nullable_relids),
1431  ec->ec_min_security);
1432 
1433  /* Mark the clause as redundant, or not */
1434  rinfo->parent_ec = parent_ec;
1435 
1436  /*
1437  * We know the correct values for left_ec/right_ec, ie this particular EC,
1438  * so we can just set them directly instead of forcing another lookup.
1439  */
1440  rinfo->left_ec = ec;
1441  rinfo->right_ec = ec;
1442 
1443  /* Mark it as usable with these EMs */
1444  rinfo->left_em = leftem;
1445  rinfo->right_em = rightem;
1446  /* and save it for possible re-use */
1447  ec->ec_derives = lappend(ec->ec_derives, rinfo);
1448 
1449  MemoryContextSwitchTo(oldcontext);
1450 
1451  return rinfo;
1452 }
Index ec_min_security
Definition: relation.h:838
Relids em_nullable_relids
Definition: relation.h:878
List * ec_derives
Definition: relation.h:830
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
EquivalenceClass * right_ec
Definition: relation.h:1850
EquivalenceMember * left_em
Definition: relation.h:1851
EquivalenceClass * parent_ec
Definition: relation.h:1835
List * ec_sources
Definition: relation.h:829
List * lappend(List *list, void *datum)
Definition: list.c:128
EquivalenceMember * right_em
Definition: relation.h:1852
Expr * clause
Definition: relation.h:1801
Relids em_relids
Definition: relation.h:877
#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:2375
EquivalenceClass * left_ec
Definition: relation.h:1849
MemoryContext planner_cxt
Definition: relation.h:290
bool eclass_useful_for_merging ( PlannerInfo root,
EquivalenceClass eclass,
RelOptInfo rel 
)

Definition at line 2398 of file equivclass.c.

References Assert, bms_is_empty(), 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, IS_OTHER_REL, lfirst, list_length(), RelOptInfo::relids, and RelOptInfo::top_parent_relids.

Referenced by get_useful_ecs_for_relation(), and pathkeys_useful_for_merging().

2401 {
2402  Relids relids;
2403  ListCell *lc;
2404 
2405  Assert(!eclass->ec_merged);
2406 
2407  /*
2408  * Won't generate joinclauses if const or single-member (the latter test
2409  * covers the volatile case too)
2410  */
2411  if (eclass->ec_has_const || list_length(eclass->ec_members) <= 1)
2412  return false;
2413 
2414  /*
2415  * Note we don't test ec_broken; if we did, we'd need a separate code path
2416  * to look through ec_sources. Checking the members anyway is OK as a
2417  * possibly-overoptimistic heuristic.
2418  */
2419 
2420  /* If specified rel is a child, we must consider the topmost parent rel */
2421  if (IS_OTHER_REL(rel))
2422  {
2424  relids = rel->top_parent_relids;
2425  }
2426  else
2427  relids = rel->relids;
2428 
2429  /* If rel already includes all members of eclass, no point in searching */
2430  if (bms_is_subset(eclass->ec_relids, relids))
2431  return false;
2432 
2433  /* To join, we need a member not in the given rel */
2434  foreach(lc, eclass->ec_members)
2435  {
2436  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
2437 
2438  if (cur_em->em_is_child)
2439  continue; /* ignore children here */
2440 
2441  if (!bms_overlap(cur_em->em_relids, relids))
2442  return true;
2443  }
2444 
2445  return false;
2446 }
#define IS_OTHER_REL(rel)
Definition: relation.h:562
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
Relids ec_relids
Definition: relation.h:831
Relids relids
Definition: relation.h:571
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
Relids em_relids
Definition: relation.h:877
#define Assert(condition)
Definition: c.h:664
#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:840
List * ec_members
Definition: relation.h:828
Relids top_parent_relids
Definition: relation.h:640
bool exprs_known_equal ( PlannerInfo root,
Node item1,
Node item2 
)

Definition at line 1945 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().

1946 {
1947  ListCell *lc1;
1948 
1949  foreach(lc1, root->eq_classes)
1950  {
1951  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
1952  bool item1member = false;
1953  bool item2member = false;
1954  ListCell *lc2;
1955 
1956  /* Never match to a volatile EC */
1957  if (ec->ec_has_volatile)
1958  continue;
1959 
1960  foreach(lc2, ec->ec_members)
1961  {
1963 
1964  if (em->em_is_child)
1965  continue; /* ignore children here */
1966  if (equal(item1, em->em_expr))
1967  item1member = true;
1968  else if (equal(item2, em->em_expr))
1969  item2member = true;
1970  /* Exit as soon as equality is proven */
1971  if (item1member && item2member)
1972  return true;
1973  }
1974  }
1975  return false;
1976 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2964
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:235
bool ec_has_volatile
Definition: relation.h:834
List * ec_members
Definition: relation.h:828
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(), 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:637
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:179
bool has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
Definition: equivclass.c:2359
int simple_rel_array_size
Definition: relation.h:180
unsigned int Index
Definition: c.h:359
#define Assert(condition)
Definition: c.h:664
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:235
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:840
List * ec_members
Definition: relation.h:828
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:1819
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2226
List * ec_sources
Definition: relation.h:829
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(), 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:560
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:111
Relids required_relids
Definition: relation.h:1819
Index ec_min_security
Definition: relation.h:838
Relids em_nullable_relids
Definition: relation.h:878
bool ec_below_outer_join
Definition: relation.h:835
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:532
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:2310
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2226
#define linitial(l)
Definition: pg_list.h:111
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1346
List * ec_sources
Definition: relation.h:829
Relids ec_relids
Definition: relation.h:831
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:634
#define Assert(condition)
Definition: c.h:664
#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:828
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(), 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:838
Relids em_nullable_relids
Definition: relation.h:878
Definition: nodes.h:509
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:569
bool ec_below_outer_join
Definition: relation.h:835
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:532
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:2310
void add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed, bool create_new_ph)
Definition: initsplan.c:198
#define PVC_INCLUDE_PLACEHOLDERS
Definition: var.h:24
void pfree(void *pointer)
Definition: mcxt.c:949
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1346
Relids ec_relids
Definition: relation.h:831
#define PVC_RECURSE_WINDOWFUNCS
Definition: var.h:23
int simple_rel_array_size
Definition: relation.h:180
void * palloc0(Size size)
Definition: mcxt.c:877
Relids em_relids
Definition: relation.h:877
#define Assert(condition)
Definition: c.h:664
#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:828
List* generate_implied_equalities_for_column ( PlannerInfo root,
RelOptInfo rel,
ec_matches_callback_type  callback,
void *  callback_arg,
Relids  prohibited_rels 
)

Definition at line 2174 of file equivclass.c.

References Assert, 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(), IS_SIMPLE_REL, lappend(), lfirst, list_length(), NIL, OidIsValid, RelOptInfo::relids, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, result, and select_equality_operator().

Referenced by match_eclass_clauses_to_index(), and postgresGetForeignPaths().

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

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:235
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 1297 of file equivclass.c.

References adjust_appendrel_attrs_multilevel(), bms_is_subset(), EquivalenceClass::ec_sources, IS_OTHER_REL, lappend(), lfirst, NIL, RelOptInfo::relids, RestrictInfo::required_relids, result, and RelOptInfo::top_parent_relids.

Referenced by generate_join_implied_equalities_for_ecs().

1303 {
1304  List *result = NIL;
1305  ListCell *lc;
1306 
1307  foreach(lc, ec->ec_sources)
1308  {
1309  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1310  Relids clause_relids = restrictinfo->required_relids;
1311 
1312  if (bms_is_subset(clause_relids, nominal_join_relids) &&
1313  !bms_is_subset(clause_relids, outer_relids) &&
1314  !bms_is_subset(clause_relids, nominal_inner_relids))
1315  result = lappend(result, restrictinfo);
1316  }
1317 
1318  /*
1319  * If we have to translate, just brute-force apply adjust_appendrel_attrs
1320  * to all the RestrictInfos at once. This will result in returning
1321  * RestrictInfos that are not listed in ec_derives, but there shouldn't be
1322  * any duplication, and it's a sufficiently narrow corner case that we
1323  * shouldn't sweat too much over it anyway.
1324  *
1325  * Since inner_rel might be an indirect descendant of the baserel
1326  * mentioned in the ec_sources clauses, we have to be prepared to apply
1327  * multiple levels of Var translation.
1328  */
1329  if (IS_OTHER_REL(inner_rel) && result != NIL)
1330  result = (List *) adjust_appendrel_attrs_multilevel(root,
1331  (Node *) result,
1332  inner_rel->relids,
1333  inner_rel->top_parent_relids);
1334 
1335  return result;
1336 }
#define NIL
Definition: pg_list.h:69
Relids required_relids
Definition: relation.h:1819
Definition: nodes.h:509
#define IS_OTHER_REL(rel)
Definition: relation.h:562
return result
Definition: formatting.c:1633
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, Relids child_relids, Relids top_parent_relids)
Definition: prepunion.c:2377
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
List * ec_sources
Definition: relation.h:829
Relids relids
Definition: relation.h:571
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
Definition: pg_list.h:45
Relids top_parent_relids
Definition: relation.h:640
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 Assert, bms_is_empty(), bms_overlap(), bms_union(), EquivalenceClass::ec_broken, EquivalenceClass::ec_has_const, EquivalenceClass::ec_members, EquivalenceClass::ec_relids, generate_join_implied_equalities_broken(), generate_join_implied_equalities_normal(), IS_OTHER_REL, lfirst, list_concat(), list_length(), NIL, RelOptInfo::relids, result, and RelOptInfo::top_parent_relids.

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 (IS_OTHER_REL(inner_rel))
1064  {
1065  Assert(!bms_is_empty(inner_rel->top_parent_relids));
1066 
1067  /* Fetch relid set for the topmost parent rel */
1068  nominal_inner_relids = inner_rel->top_parent_relids;
1069  /* ECs will be marked with the parent's relid, not the child's */
1070  nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1071  }
1072  else
1073  {
1074  nominal_inner_relids = inner_relids;
1075  nominal_join_relids = join_relids;
1076  }
1077 
1078  foreach(lc, eclasses)
1079  {
1081  List *sublist = NIL;
1082 
1083  /* ECs containing consts do not need any further enforcement */
1084  if (ec->ec_has_const)
1085  continue;
1086 
1087  /* Single-member ECs won't generate any deductions */
1088  if (list_length(ec->ec_members) <= 1)
1089  continue;
1090 
1091  /* We can quickly ignore any that don't overlap the join, too */
1092  if (!bms_overlap(ec->ec_relids, nominal_join_relids))
1093  continue;
1094 
1095  if (!ec->ec_broken)
1097  ec,
1098  join_relids,
1099  outer_relids,
1100  inner_relids);
1101 
1102  /* Recover if we failed to generate required derived clauses */
1103  if (ec->ec_broken)
1105  ec,
1106  nominal_join_relids,
1107  outer_relids,
1108  nominal_inner_relids,
1109  inner_rel);
1110 
1111  result = list_concat(result, sublist);
1112  }
1113 
1114  return result;
1115 }
#define NIL
Definition: pg_list.h:69
#define IS_OTHER_REL(rel)
Definition: relation.h:562
List * list_concat(List *list1, List *list2)
Definition: list.c:321
return result
Definition: formatting.c:1633
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:1297
Relids ec_relids
Definition: relation.h:831
Relids relids
Definition: relation.h:571
static List * generate_join_implied_equalities_normal(PlannerInfo *root, EquivalenceClass *ec, Relids join_relids, Relids outer_relids, Relids inner_relids)
Definition: equivclass.c:1121
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
#define Assert(condition)
Definition: c.h:664
#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:828
Relids top_parent_relids
Definition: relation.h:640
static List * generate_join_implied_equalities_normal ( PlannerInfo root,
EquivalenceClass ec,
Relids  join_relids,
Relids  outer_relids,
Relids  inner_relids 
)
static

Definition at line 1121 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, OidIsValid, op_hashjoinable(), result, and select_equality_operator().

Referenced by generate_join_implied_equalities_for_ecs().

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

2360 {
2361  ListCell *lc1;
2362 
2363  foreach(lc1, root->eq_classes)
2364  {
2365  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2366 
2367  /*
2368  * Won't generate joinclauses if single-member (this test covers the
2369  * volatile case too)
2370  */
2371  if (list_length(ec->ec_members) <= 1)
2372  continue;
2373 
2374  /*
2375  * Per the comment in have_relevant_eclass_joinclause, it's sufficient
2376  * to find an EC that mentions both this rel and some other rel.
2377  */
2378  if (bms_overlap(rel1->relids, ec->ec_relids) &&
2379  !bms_is_subset(ec->ec_relids, rel1->relids))
2380  return true;
2381  }
2382 
2383  return false;
2384 }
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
Relids ec_relids
Definition: relation.h:831
Relids relids
Definition: relation.h:571
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:235
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:828
bool have_relevant_eclass_joinclause ( PlannerInfo root,
RelOptInfo rel1,
RelOptInfo rel2 
)

Definition at line 2305 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().

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

Definition at line 2456 of file equivclass.c.

References lfirst, and RestrictInfo::parent_ec.

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

2457 {
2458  EquivalenceClass *parent_ec = rinfo->parent_ec;
2459  ListCell *lc;
2460 
2461  /* Fail if it's not a potentially-redundant clause from some EC */
2462  if (parent_ec == NULL)
2463  return false;
2464 
2465  foreach(lc, clauselist)
2466  {
2467  RestrictInfo *otherrinfo = (RestrictInfo *) lfirst(lc);
2468 
2469  if (otherrinfo->parent_ec == parent_ec)
2470  return true;
2471  }
2472 
2473  return false;
2474 }
EquivalenceClass * parent_ec
Definition: relation.h:1835
#define lfirst(lc)
Definition: pg_list.h:106
EquivalenceClass* match_eclasses_to_foreign_key_col ( PlannerInfo root,
ForeignKeyOptInfo fkinfo,
int  colno 
)

Definition at line 1992 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, ForeignKeyOptInfo::ref_relid, RangeQueryClause::var, Var::varattno, and Var::varno.

Referenced by match_foreign_keys_to_quals().

1995 {
1996  Index var1varno = fkinfo->con_relid;
1997  AttrNumber var1attno = fkinfo->conkey[colno];
1998  Index var2varno = fkinfo->ref_relid;
1999  AttrNumber var2attno = fkinfo->confkey[colno];
2000  Oid eqop = fkinfo->conpfeqop[colno];
2001  List *opfamilies = NIL; /* compute only if needed */
2002  ListCell *lc1;
2003 
2004  foreach(lc1, root->eq_classes)
2005  {
2006  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2007  bool item1member = false;
2008  bool item2member = false;
2009  ListCell *lc2;
2010 
2011  /* Never match to a volatile EC */
2012  if (ec->ec_has_volatile)
2013  continue;
2014  /* Note: it seems okay to match to "broken" eclasses here */
2015 
2016  foreach(lc2, ec->ec_members)
2017  {
2019  Var *var;
2020 
2021  if (em->em_is_child)
2022  continue; /* ignore children here */
2023 
2024  /* EM must be a Var, possibly with RelabelType */
2025  var = (Var *) em->em_expr;
2026  while (var && IsA(var, RelabelType))
2027  var = (Var *) ((RelabelType *) var)->arg;
2028  if (!(var && IsA(var, Var)))
2029  continue;
2030 
2031  /* Match? */
2032  if (var->varno == var1varno && var->varattno == var1attno)
2033  item1member = true;
2034  else if (var->varno == var2varno && var->varattno == var2attno)
2035  item2member = true;
2036 
2037  /* Have we found both PK and FK column in this EC? */
2038  if (item1member && item2member)
2039  {
2040  /*
2041  * Succeed if eqop matches EC's opfamilies. We could test
2042  * this before scanning the members, but it's probably cheaper
2043  * to test for member matches first.
2044  */
2045  if (opfamilies == NIL) /* compute if we didn't already */
2046  opfamilies = get_mergejoin_opfamilies(eqop);
2047  if (equal(opfamilies, ec->ec_opfamilies))
2048  return ec;
2049  /* Otherwise, done with this EC, move on to the next */
2050  break;
2051  }
2052  }
2053  }
2054  return NULL;
2055 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2964
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:753
Index varno
Definition: primnodes.h:166
List * ec_opfamilies
Definition: relation.h:826
AttrNumber conkey[INDEX_MAX_KEYS]
Definition: relation.h:751
unsigned int Index
Definition: c.h:359
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:235
bool ec_has_volatile
Definition: relation.h:834
Definition: pg_list.h:45
AttrNumber confkey[INDEX_MAX_KEYS]
Definition: relation.h:752
int16 AttrNumber
Definition: attnum.h:21
List * ec_members
Definition: relation.h:828
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, 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:1813
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2964
bool leakproof
Definition: relation.h:1811
Index ec_min_security
Definition: relation.h:838
List * ec_derives
Definition: relation.h:830
#define Min(x, y)
Definition: c.h:795
Index ec_sortref
Definition: relation.h:837
Definition: nodes.h:509
Relids left_relids
Definition: relation.h:1828
List * list_concat(List *list1, List *list2)
Definition: list.c:321
bool ec_below_outer_join
Definition: relation.h:835
EquivalenceClass * right_ec
Definition: relation.h:1850
Index ec_max_security
Definition: relation.h:839
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:1846
#define list_make1(x1)
Definition: pg_list.h:139
#define ERROR
Definition: elog.h:43
#define is_opclause(clause)
Definition: clauses.h:20
EquivalenceMember * left_em
Definition: relation.h:1851
Node * get_leftop(const Expr *clause)
Definition: clauses.c:199
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:829
Relids ec_relids
Definition: relation.h:831
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1167
List * canon_pathkeys
Definition: relation.h:237
List * lappend(List *list, void *datum)
Definition: list.c:128
EquivalenceMember * right_em
Definition: relation.h:1852
Expr * clause
Definition: relation.h:1801
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
List * ec_opfamilies
Definition: relation.h:826
Relids nullable_relids
Definition: relation.h:1825
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:252
#define Max(x, y)
Definition: c.h:789
#define makeNode(_type_)
Definition: nodes.h:557
Relids right_relids
Definition: relation.h:1829
#define Assert(condition)
Definition: c.h:664
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:235
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
bool ec_has_volatile
Definition: relation.h:834
Node * get_rightop(const Expr *clause)
Definition: clauses.c:216
EquivalenceClass * left_ec
Definition: relation.h:1849
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:1288
Definition: pg_list.h:45
struct EquivalenceClass * ec_merged
Definition: relation.h:840
List * ec_members
Definition: relation.h:828
static bool reconsider_full_join_clause ( PlannerInfo root,
RestrictInfo rinfo 
)
static

Definition at line 1768 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, 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().

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

Definition at line 1643 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().

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

Definition at line 1534 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, RestrictInfo::outer_selec, reconsider_full_join_clause(), reconsider_outer_join_clause(), and PlannerInfo::right_join_clauses.

Referenced by query_planner().

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

Definition at line 1346 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().

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