PostgreSQL Source Code  git master
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/appendinfo.h"
#include "optimizer/clauses.h"
#include "optimizer/optimizer.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/planmain.h"
#include "optimizer/restrictinfo.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)
 
static Bitmapsetget_eclass_indexes_for_relids (PlannerInfo *root, Relids relids)
 
static Bitmapsetget_common_eclass_indexes (PlannerInfo *root, Relids relids1, Relids relids2)
 
bool process_equivalence (PlannerInfo *root, RestrictInfo **p_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)
 
bool is_redundant_with_indexclauses (RestrictInfo *rinfo, List *indexclauses)
 

Function Documentation

◆ add_child_rel_equivalences()

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

Definition at line 2222 of file equivclass.c.

References add_eq_member(), adjust_appendrel_attrs(), adjust_appendrel_attrs_multilevel(), Assert, bms_add_member(), bms_add_members(), bms_difference(), bms_is_subset(), bms_next_member(), bms_overlap(), EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_members, PlannerInfo::ec_merging_done, EquivalenceClass::ec_relids, RelOptInfo::eclass_indexes, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, EquivalenceMember::em_nullable_relids, EquivalenceMember::em_relids, PlannerInfo::eq_classes, i, IS_SIMPLE_REL, list_length(), list_nth(), RelOptInfo::relids, RELOPT_BASEREL, RelOptInfo::reloptkind, and RelOptInfo::top_parent_relids.

Referenced by set_append_rel_size().

2226 {
2227  int i;
2228 
2229  /*
2230  * EC merging should be complete already, so we can use the parent rel's
2231  * eclass_indexes to avoid searching all of root->eq_classes.
2232  */
2233  Assert(root->ec_merging_done);
2234  Assert(IS_SIMPLE_REL(parent_rel));
2235 
2236  i = -1;
2237  while ((i = bms_next_member(parent_rel->eclass_indexes, i)) >= 0)
2238  {
2239  EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2240  int num_members;
2241 
2242  /*
2243  * If this EC contains a volatile expression, then generating child
2244  * EMs would be downright dangerous, so skip it. We rely on a
2245  * volatile EC having only one EM.
2246  */
2247  if (cur_ec->ec_has_volatile)
2248  continue;
2249 
2250  /* Sanity check eclass_indexes only contain ECs for parent_rel */
2251  Assert(bms_is_subset(child_rel->top_parent_relids, cur_ec->ec_relids));
2252 
2253  /*
2254  * We don't use foreach() here because there's no point in scanning
2255  * newly-added child members, so we can stop after the last
2256  * pre-existing EC member.
2257  */
2258  num_members = list_length(cur_ec->ec_members);
2259  for (int pos = 0; pos < num_members; pos++)
2260  {
2261  EquivalenceMember *cur_em = (EquivalenceMember *) list_nth(cur_ec->ec_members, pos);
2262 
2263  if (cur_em->em_is_const)
2264  continue; /* ignore consts here */
2265 
2266  /*
2267  * We consider only original EC members here, not
2268  * already-transformed child members. Otherwise, if some original
2269  * member expression references more than one appendrel, we'd get
2270  * an O(N^2) explosion of useless derived expressions for
2271  * combinations of children.
2272  */
2273  if (cur_em->em_is_child)
2274  continue; /* ignore children here */
2275 
2276  /* Does this member reference child's topmost parent rel? */
2277  if (bms_overlap(cur_em->em_relids, child_rel->top_parent_relids))
2278  {
2279  /* Yes, generate transformed child version */
2280  Expr *child_expr;
2281  Relids new_relids;
2282  Relids new_nullable_relids;
2283 
2284  if (parent_rel->reloptkind == RELOPT_BASEREL)
2285  {
2286  /* Simple single-level transformation */
2287  child_expr = (Expr *)
2289  (Node *) cur_em->em_expr,
2290  1, &appinfo);
2291  }
2292  else
2293  {
2294  /* Must do multi-level transformation */
2295  child_expr = (Expr *)
2297  (Node *) cur_em->em_expr,
2298  child_rel->relids,
2299  child_rel->top_parent_relids);
2300  }
2301 
2302  /*
2303  * Transform em_relids to match. Note we do *not* do
2304  * pull_varnos(child_expr) here, as for example the
2305  * transformation might have substituted a constant, but we
2306  * don't want the child member to be marked as constant.
2307  */
2308  new_relids = bms_difference(cur_em->em_relids,
2309  child_rel->top_parent_relids);
2310  new_relids = bms_add_members(new_relids, child_rel->relids);
2311 
2312  /*
2313  * And likewise for nullable_relids. Note this code assumes
2314  * parent and child relids are singletons.
2315  */
2316  new_nullable_relids = cur_em->em_nullable_relids;
2317  if (bms_overlap(new_nullable_relids,
2318  child_rel->top_parent_relids))
2319  {
2320  new_nullable_relids = bms_difference(new_nullable_relids,
2321  child_rel->top_parent_relids);
2322  new_nullable_relids = bms_add_members(new_nullable_relids,
2323  child_rel->relids);
2324  }
2325 
2326  (void) add_eq_member(cur_ec, child_expr,
2327  new_relids, new_nullable_relids,
2328  true, cur_em->em_datatype);
2329 
2330  /* Record this EC index for the child rel */
2331  child_rel->eclass_indexes = bms_add_member(child_rel->eclass_indexes, i);
2332  }
2333  }
2334  }
2335 }
RelOptKind reloptkind
Definition: pathnodes.h:638
bool ec_merging_done
Definition: pathnodes.h:266
Relids em_nullable_relids
Definition: pathnodes.h:984
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:291
Definition: nodes.h:525
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:614
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, Relids child_relids, Relids top_parent_relids)
Definition: appendinfo.c:483
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids ec_relids
Definition: pathnodes.h:937
Relids relids
Definition: pathnodes.h:641
#define Assert(condition)
Definition: c.h:732
List * eq_classes
Definition: pathnodes.h:264
static int list_length(const List *l)
Definition: pg_list.h:169
bool ec_has_volatile
Definition: pathnodes.h:940
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
int i
static EquivalenceMember * add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids, Relids nullable_relids, bool is_child, Oid datatype)
Definition: equivclass.c:549
Bitmapset * eclass_indexes
Definition: pathnodes.h:683
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793
List * ec_members
Definition: pathnodes.h:934
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:180
Relids top_parent_relids
Definition: pathnodes.h:714

◆ add_eq_member()

static EquivalenceMember * add_eq_member ( EquivalenceClass ec,
Expr expr,
Relids  relids,
Relids  nullable_relids,
bool  is_child,
Oid  datatype 
)
static

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

551 {
553 
554  em->em_expr = expr;
555  em->em_relids = relids;
556  em->em_nullable_relids = nullable_relids;
557  em->em_is_const = false;
558  em->em_is_child = is_child;
559  em->em_datatype = datatype;
560 
561  if (bms_is_empty(relids))
562  {
563  /*
564  * No Vars, assume it's a pseudoconstant. This is correct for entries
565  * generated from process_equivalence(), because a WHERE clause can't
566  * contain aggregates or SRFs, and non-volatility was checked before
567  * process_equivalence() ever got called. But
568  * get_eclass_for_sort_expr() has to work harder. We put the tests
569  * there not here to save cycles in the equivalence case.
570  */
571  Assert(!is_child);
572  em->em_is_const = true;
573  ec->ec_has_const = true;
574  /* it can't affect ec_relids */
575  }
576  else if (!is_child) /* child members don't add to ec_relids */
577  {
578  ec->ec_relids = bms_add_members(ec->ec_relids, relids);
579  }
580  ec->ec_members = lappend(ec->ec_members, em);
581 
582  return em;
583 }
Relids em_nullable_relids
Definition: pathnodes.h:984
Relids ec_relids
Definition: pathnodes.h:937
List * lappend(List *list, void *datum)
Definition: list.c:322
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:732
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793
List * ec_members
Definition: pathnodes.h:934

◆ canonicalize_ec_expression()

Expr* canonicalize_ec_expression ( Expr expr,
Oid  req_type,
Oid  req_collation 
)

Definition at line 499 of file equivclass.c.

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

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

500 {
501  Oid expr_type = exprType((Node *) expr);
502 
503  /*
504  * For a polymorphic-input-type opclass, just keep the same exposed type.
505  * RECORD opclasses work like polymorphic-type ones for this purpose.
506  */
507  if (IsPolymorphicType(req_type) || req_type == RECORDOID)
508  req_type = expr_type;
509 
510  /*
511  * No work if the expression exposes the right type/collation already.
512  */
513  if (expr_type != req_type ||
514  exprCollation((Node *) expr) != req_collation)
515  {
516  /*
517  * Strip any existing RelabelType, then add a new one if needed. This
518  * is to preserve the invariant of no redundant RelabelTypes.
519  *
520  * If we have to change the exposed type of the stripped expression,
521  * set typmod to -1 (since the new type may not have the same typmod
522  * interpretation). If we only have to change collation, preserve the
523  * exposed typmod.
524  */
525  while (expr && IsA(expr, RelabelType))
526  expr = (Expr *) ((RelabelType *) expr)->arg;
527 
528  if (exprType((Node *) expr) != req_type)
529  expr = (Expr *) makeRelabelType(expr,
530  req_type,
531  -1,
532  req_collation,
534  else if (exprCollation((Node *) expr) != req_collation)
535  expr = (Expr *) makeRelabelType(expr,
536  req_type,
537  exprTypmod((Node *) expr),
538  req_collation,
540  }
541 
542  return expr;
543 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
Definition: nodes.h:525
unsigned int Oid
Definition: postgres_ext.h:31
RelabelType * makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat)
Definition: makefuncs.c:400
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
void * arg

◆ create_join_clause()

static RestrictInfo * create_join_clause ( PlannerInfo root,
EquivalenceClass ec,
Oid  opno,
EquivalenceMember leftem,
EquivalenceMember rightem,
EquivalenceClass parent_ec 
)
static

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

1540 {
1541  RestrictInfo *rinfo;
1542  ListCell *lc;
1543  MemoryContext oldcontext;
1544 
1545  /*
1546  * Search to see if we already built a RestrictInfo for this pair of
1547  * EquivalenceMembers. We can use either original source clauses or
1548  * previously-derived clauses. The check on opno is probably redundant,
1549  * but be safe ...
1550  */
1551  foreach(lc, ec->ec_sources)
1552  {
1553  rinfo = (RestrictInfo *) lfirst(lc);
1554  if (rinfo->left_em == leftem &&
1555  rinfo->right_em == rightem &&
1556  rinfo->parent_ec == parent_ec &&
1557  opno == ((OpExpr *) rinfo->clause)->opno)
1558  return rinfo;
1559  }
1560 
1561  foreach(lc, ec->ec_derives)
1562  {
1563  rinfo = (RestrictInfo *) lfirst(lc);
1564  if (rinfo->left_em == leftem &&
1565  rinfo->right_em == rightem &&
1566  rinfo->parent_ec == parent_ec &&
1567  opno == ((OpExpr *) rinfo->clause)->opno)
1568  return rinfo;
1569  }
1570 
1571  /*
1572  * Not there, so build it, in planner context so we can re-use it. (Not
1573  * important in normal planning, but definitely so in GEQO.)
1574  */
1575  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
1576 
1577  rinfo = build_implied_join_equality(opno,
1578  ec->ec_collation,
1579  leftem->em_expr,
1580  rightem->em_expr,
1581  bms_union(leftem->em_relids,
1582  rightem->em_relids),
1583  bms_union(leftem->em_nullable_relids,
1584  rightem->em_nullable_relids),
1585  ec->ec_min_security);
1586 
1587  /* Mark the clause as redundant, or not */
1588  rinfo->parent_ec = parent_ec;
1589 
1590  /*
1591  * We know the correct values for left_ec/right_ec, ie this particular EC,
1592  * so we can just set them directly instead of forcing another lookup.
1593  */
1594  rinfo->left_ec = ec;
1595  rinfo->right_ec = ec;
1596 
1597  /* Mark it as usable with these EMs */
1598  rinfo->left_em = leftem;
1599  rinfo->right_em = rightem;
1600  /* and save it for possible re-use */
1601  ec->ec_derives = lappend(ec->ec_derives, rinfo);
1602 
1603  MemoryContextSwitchTo(oldcontext);
1604 
1605  return rinfo;
1606 }
Index ec_min_security
Definition: pathnodes.h:944
Relids em_nullable_relids
Definition: pathnodes.h:984
List * ec_derives
Definition: pathnodes.h:936
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
EquivalenceClass * right_ec
Definition: pathnodes.h:1992
EquivalenceMember * left_em
Definition: pathnodes.h:1993
EquivalenceClass * parent_ec
Definition: pathnodes.h:1977
List * ec_sources
Definition: pathnodes.h:935
List * lappend(List *list, void *datum)
Definition: list.c:322
EquivalenceMember * right_em
Definition: pathnodes.h:1994
Expr * clause
Definition: pathnodes.h:1943
#define lfirst(lc)
Definition: pg_list.h:190
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
RestrictInfo * build_implied_join_equality(Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
Definition: initsplan.c:2354
EquivalenceClass * left_ec
Definition: pathnodes.h:1991
MemoryContext planner_cxt
Definition: pathnodes.h:329

◆ eclass_useful_for_merging()

bool eclass_useful_for_merging ( PlannerInfo root,
EquivalenceClass eclass,
RelOptInfo rel 
)

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

2606 {
2607  Relids relids;
2608  ListCell *lc;
2609 
2610  Assert(!eclass->ec_merged);
2611 
2612  /*
2613  * Won't generate joinclauses if const or single-member (the latter test
2614  * covers the volatile case too)
2615  */
2616  if (eclass->ec_has_const || list_length(eclass->ec_members) <= 1)
2617  return false;
2618 
2619  /*
2620  * Note we don't test ec_broken; if we did, we'd need a separate code path
2621  * to look through ec_sources. Checking the members anyway is OK as a
2622  * possibly-overoptimistic heuristic.
2623  */
2624 
2625  /* If specified rel is a child, we must consider the topmost parent rel */
2626  if (IS_OTHER_REL(rel))
2627  {
2629  relids = rel->top_parent_relids;
2630  }
2631  else
2632  relids = rel->relids;
2633 
2634  /* If rel already includes all members of eclass, no point in searching */
2635  if (bms_is_subset(eclass->ec_relids, relids))
2636  return false;
2637 
2638  /* To join, we need a member not in the given rel */
2639  foreach(lc, eclass->ec_members)
2640  {
2641  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
2642 
2643  if (cur_em->em_is_child)
2644  continue; /* ignore children here */
2645 
2646  if (!bms_overlap(cur_em->em_relids, relids))
2647  return true;
2648  }
2649 
2650  return false;
2651 }
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:629
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids ec_relids
Definition: pathnodes.h:937
Relids relids
Definition: pathnodes.h:641
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
struct EquivalenceClass * ec_merged
Definition: pathnodes.h:946
List * ec_members
Definition: pathnodes.h:934
Relids top_parent_relids
Definition: pathnodes.h:714

◆ exprs_known_equal()

bool exprs_known_equal ( PlannerInfo root,
Node item1,
Node item2 
)

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

2086 {
2087  ListCell *lc1;
2088 
2089  foreach(lc1, root->eq_classes)
2090  {
2091  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2092  bool item1member = false;
2093  bool item2member = false;
2094  ListCell *lc2;
2095 
2096  /* Never match to a volatile EC */
2097  if (ec->ec_has_volatile)
2098  continue;
2099 
2100  foreach(lc2, ec->ec_members)
2101  {
2103 
2104  if (em->em_is_child)
2105  continue; /* ignore children here */
2106  if (equal(item1, em->em_expr))
2107  item1member = true;
2108  else if (equal(item2, em->em_expr))
2109  item2member = true;
2110  /* Exit as soon as equality is proven */
2111  if (item1member && item2member)
2112  return true;
2113  }
2114  }
2115  return false;
2116 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3008
#define lfirst(lc)
Definition: pg_list.h:190
List * eq_classes
Definition: pathnodes.h:264
bool ec_has_volatile
Definition: pathnodes.h:940
List * ec_members
Definition: pathnodes.h:934

◆ generate_base_implied_equalities()

void generate_base_implied_equalities ( PlannerInfo root)

Definition at line 827 of file equivclass.c.

References Assert, bms_add_member(), bms_membership(), BMS_MULTIPLE, bms_next_member(), EquivalenceClass::ec_broken, EquivalenceClass::ec_has_const, EquivalenceClass::ec_members, EquivalenceClass::ec_merged, PlannerInfo::ec_merging_done, EquivalenceClass::ec_relids, RelOptInfo::eclass_indexes, PlannerInfo::eq_classes, generate_base_implied_equalities_broken(), generate_base_implied_equalities_const(), generate_base_implied_equalities_no_const(), RelOptInfo::has_eclass_joins, i, lfirst, list_length(), RELOPT_BASEREL, RelOptInfo::reloptkind, and PlannerInfo::simple_rel_array.

Referenced by query_planner().

828 {
829  int ec_index;
830  ListCell *lc;
831 
832  /*
833  * At this point, we're done absorbing knowledge of equivalences in the
834  * query, so no further EC merging should happen, and ECs remaining in the
835  * eq_classes list can be considered canonical. (But note that it's still
836  * possible for new single-member ECs to be added through
837  * get_eclass_for_sort_expr().)
838  */
839  root->ec_merging_done = true;
840 
841  ec_index = 0;
842  foreach(lc, root->eq_classes)
843  {
845  bool can_generate_joinclause = false;
846  int i;
847 
848  Assert(ec->ec_merged == NULL); /* else shouldn't be in list */
849  Assert(!ec->ec_broken); /* not yet anyway... */
850 
851  /*
852  * Generate implied equalities that are restriction clauses.
853  * Single-member ECs won't generate any deductions, either here or at
854  * the join level.
855  */
856  if (list_length(ec->ec_members) > 1)
857  {
858  if (ec->ec_has_const)
860  else
862 
863  /* Recover if we failed to generate required derived clauses */
864  if (ec->ec_broken)
866 
867  /* Detect whether this EC might generate join clauses */
868  can_generate_joinclause =
870  }
871 
872  /*
873  * Mark the base rels cited in each eclass (which should all exist by
874  * now) with the eq_classes indexes of all eclasses mentioning them.
875  * This will let us avoid searching in subsequent lookups. While
876  * we're at it, we can mark base rels that have pending eclass joins;
877  * this is a cheap version of has_relevant_eclass_joinclause().
878  */
879  i = -1;
880  while ((i = bms_next_member(ec->ec_relids, i)) > 0)
881  {
882  RelOptInfo *rel = root->simple_rel_array[i];
883 
885 
887  ec_index);
888 
889  if (can_generate_joinclause)
890  rel->has_eclass_joins = true;
891  }
892 
893  ec_index++;
894  }
895 }
bool has_eclass_joins
Definition: pathnodes.h:709
static void generate_base_implied_equalities_no_const(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:978
RelOptKind reloptkind
Definition: pathnodes.h:638
bool ec_merging_done
Definition: pathnodes.h:266
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
static void generate_base_implied_equalities_broken(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:1069
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:201
Relids ec_relids
Definition: pathnodes.h:937
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
List * eq_classes
Definition: pathnodes.h:264
static int list_length(const List *l)
Definition: pg_list.h:169
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
int i
static void generate_base_implied_equalities_const(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:901
Bitmapset * eclass_indexes
Definition: pathnodes.h:683
struct EquivalenceClass * ec_merged
Definition: pathnodes.h:946
List * ec_members
Definition: pathnodes.h:934

◆ generate_base_implied_equalities_broken()

static void generate_base_implied_equalities_broken ( PlannerInfo root,
EquivalenceClass ec 
)
static

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

1071 {
1072  ListCell *lc;
1073 
1074  foreach(lc, ec->ec_sources)
1075  {
1076  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1077 
1078  if (ec->ec_has_const ||
1079  bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
1080  distribute_restrictinfo_to_rels(root, restrictinfo);
1081  }
1082 }
Relids required_relids
Definition: pathnodes.h:1961
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2205
List * ec_sources
Definition: pathnodes.h:935
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
#define lfirst(lc)
Definition: pg_list.h:190

◆ generate_base_implied_equalities_const()

static void generate_base_implied_equalities_const ( PlannerInfo root,
EquivalenceClass ec 
)
static

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

903 {
904  EquivalenceMember *const_em = NULL;
905  ListCell *lc;
906 
907  /*
908  * In the trivial case where we just had one "var = const" clause, push
909  * the original clause back into the main planner machinery. There is
910  * nothing to be gained by doing it differently, and we save the effort to
911  * re-build and re-analyze an equality clause that will be exactly
912  * equivalent to the old one.
913  */
914  if (list_length(ec->ec_members) == 2 &&
915  list_length(ec->ec_sources) == 1)
916  {
917  RestrictInfo *restrictinfo = (RestrictInfo *) linitial(ec->ec_sources);
918 
919  if (bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
920  {
921  distribute_restrictinfo_to_rels(root, restrictinfo);
922  return;
923  }
924  }
925 
926  /*
927  * Find the constant member to use. We prefer an actual constant to
928  * pseudo-constants (such as Params), because the constraint exclusion
929  * machinery might be able to exclude relations on the basis of generated
930  * "var = const" equalities, but "var = param" won't work for that.
931  */
932  foreach(lc, ec->ec_members)
933  {
934  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
935 
936  if (cur_em->em_is_const)
937  {
938  const_em = cur_em;
939  if (IsA(cur_em->em_expr, Const))
940  break;
941  }
942  }
943  Assert(const_em != NULL);
944 
945  /* Generate a derived equality against each other member */
946  foreach(lc, ec->ec_members)
947  {
948  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
949  Oid eq_op;
950 
951  Assert(!cur_em->em_is_child); /* no children yet */
952  if (cur_em == const_em)
953  continue;
954  eq_op = select_equality_operator(ec,
955  cur_em->em_datatype,
956  const_em->em_datatype);
957  if (!OidIsValid(eq_op))
958  {
959  /* failed... */
960  ec->ec_broken = true;
961  break;
962  }
963  process_implied_equality(root, eq_op, ec->ec_collation,
964  cur_em->em_expr, const_em->em_expr,
965  bms_copy(ec->ec_relids),
967  const_em->em_nullable_relids),
968  ec->ec_min_security,
970  cur_em->em_is_const);
971  }
972 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
Relids required_relids
Definition: pathnodes.h:1961
Index ec_min_security
Definition: pathnodes.h:944
Relids em_nullable_relids
Definition: pathnodes.h:984
bool ec_below_outer_join
Definition: pathnodes.h:941
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
void process_implied_equality(PlannerInfo *root, Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level, bool below_outer_join, bool both_const)
Definition: initsplan.c:2289
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2205
#define linitial(l)
Definition: pg_list.h:195
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1500
List * ec_sources
Definition: pathnodes.h:935
Relids ec_relids
Definition: pathnodes.h:937
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
static int list_length(const List *l)
Definition: pg_list.h:169
List * ec_members
Definition: pathnodes.h:934

◆ generate_base_implied_equalities_no_const()

static void generate_base_implied_equalities_no_const ( PlannerInfo root,
EquivalenceClass ec 
)
static

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

980 {
981  EquivalenceMember **prev_ems;
982  ListCell *lc;
983 
984  /*
985  * We scan the EC members once and track the last-seen member for each
986  * base relation. When we see another member of the same base relation,
987  * we generate "prev_em = cur_em". This results in the minimum number of
988  * derived clauses, but it's possible that it will fail when a different
989  * ordering would succeed. XXX FIXME: use a UNION-FIND algorithm similar
990  * to the way we build merged ECs. (Use a list-of-lists for each rel.)
991  */
992  prev_ems = (EquivalenceMember **)
994 
995  foreach(lc, ec->ec_members)
996  {
997  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
998  int relid;
999 
1000  Assert(!cur_em->em_is_child); /* no children yet */
1001  if (!bms_get_singleton_member(cur_em->em_relids, &relid))
1002  continue;
1003  Assert(relid < root->simple_rel_array_size);
1004 
1005  if (prev_ems[relid] != NULL)
1006  {
1007  EquivalenceMember *prev_em = prev_ems[relid];
1008  Oid eq_op;
1009 
1010  eq_op = select_equality_operator(ec,
1011  prev_em->em_datatype,
1012  cur_em->em_datatype);
1013  if (!OidIsValid(eq_op))
1014  {
1015  /* failed... */
1016  ec->ec_broken = true;
1017  break;
1018  }
1019  process_implied_equality(root, eq_op, ec->ec_collation,
1020  prev_em->em_expr, cur_em->em_expr,
1021  bms_copy(ec->ec_relids),
1022  bms_union(prev_em->em_nullable_relids,
1023  cur_em->em_nullable_relids),
1024  ec->ec_min_security,
1025  ec->ec_below_outer_join,
1026  false);
1027  }
1028  prev_ems[relid] = cur_em;
1029  }
1030 
1031  pfree(prev_ems);
1032 
1033  /*
1034  * We also have to make sure that all the Vars used in the member clauses
1035  * will be available at any join node we might try to reference them at.
1036  * For the moment we force all the Vars to be available at all join nodes
1037  * for this eclass. Perhaps this could be improved by doing some
1038  * pre-analysis of which members we prefer to join, but it's no worse than
1039  * what happened in the pre-8.3 code.
1040  */
1041  foreach(lc, ec->ec_members)
1042  {
1043  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1044  List *vars = pull_var_clause((Node *) cur_em->em_expr,
1048 
1049  add_vars_to_targetlist(root, vars, ec->ec_relids, false);
1050  list_free(vars);
1051  }
1052 }
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
Index ec_min_security
Definition: pathnodes.h:944
Relids em_nullable_relids
Definition: pathnodes.h:984
Definition: nodes.h:525
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:615
bool ec_below_outer_join
Definition: pathnodes.h:941
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:638
void process_implied_equality(PlannerInfo *root, Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level, bool below_outer_join, bool both_const)
Definition: initsplan.c:2289
void add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed, bool create_new_ph)
Definition: initsplan.c:230
void pfree(void *pointer)
Definition: mcxt.c:1056
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1500
Relids ec_relids
Definition: pathnodes.h:937
int simple_rel_array_size
Definition: pathnodes.h:202
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:174
void * palloc0(Size size)
Definition: mcxt.c:980
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:173
void list_free(List *list)
Definition: list.c:1377
Definition: regcomp.c:224
Definition: pg_list.h:50
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:171
List * ec_members
Definition: pathnodes.h:934

◆ generate_implied_equalities_for_column()

List* generate_implied_equalities_for_column ( PlannerInfo root,
RelOptInfo rel,
ec_matches_callback_type  callback,
void *  callback_arg,
Relids  prohibited_rels 
)

Definition at line 2362 of file equivclass.c.

References Assert, bms_equal(), bms_is_subset(), bms_next_member(), bms_overlap(), callback(), create_join_clause(), EquivalenceClass::ec_has_const, EquivalenceClass::ec_members, PlannerInfo::ec_merging_done, EquivalenceClass::ec_relids, RelOptInfo::eclass_indexes, EquivalenceMember::em_datatype, EquivalenceMember::em_is_child, EquivalenceMember::em_relids, PlannerInfo::eq_classes, find_childrel_parents(), i, IS_SIMPLE_REL, lappend(), lfirst, list_length(), list_nth(), NIL, OidIsValid, RelOptInfo::relids, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, and select_equality_operator().

Referenced by create_tidscan_paths(), match_eclass_clauses_to_index(), and postgresGetForeignPaths().

2367 {
2368  List *result = NIL;
2369  bool is_child_rel = (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
2370  Relids parent_relids;
2371  int i;
2372 
2373  /* Should be OK to rely on eclass_indexes */
2374  Assert(root->ec_merging_done);
2375 
2376  /* Indexes are available only on base or "other" member relations. */
2377  Assert(IS_SIMPLE_REL(rel));
2378 
2379  /* If it's a child rel, we'll need to know what its parent(s) are */
2380  if (is_child_rel)
2381  parent_relids = find_childrel_parents(root, rel);
2382  else
2383  parent_relids = NULL; /* not used, but keep compiler quiet */
2384 
2385  i = -1;
2386  while ((i = bms_next_member(rel->eclass_indexes, i)) >= 0)
2387  {
2388  EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2389  EquivalenceMember *cur_em;
2390  ListCell *lc2;
2391 
2392  /* Sanity check eclass_indexes only contain ECs for rel */
2393  Assert(is_child_rel || bms_is_subset(rel->relids, cur_ec->ec_relids));
2394 
2395  /*
2396  * Won't generate joinclauses if const or single-member (the latter
2397  * test covers the volatile case too)
2398  */
2399  if (cur_ec->ec_has_const || list_length(cur_ec->ec_members) <= 1)
2400  continue;
2401 
2402  /*
2403  * Scan members, looking for a match to the target column. Note that
2404  * child EC members are considered, but only when they belong to the
2405  * target relation. (Unlike regular members, the same expression
2406  * could be a child member of more than one EC. Therefore, it's
2407  * potentially order-dependent which EC a child relation's target
2408  * column gets matched to. This is annoying but it only happens in
2409  * corner cases, so for now we live with just reporting the first
2410  * match. See also get_eclass_for_sort_expr.)
2411  */
2412  cur_em = NULL;
2413  foreach(lc2, cur_ec->ec_members)
2414  {
2415  cur_em = (EquivalenceMember *) lfirst(lc2);
2416  if (bms_equal(cur_em->em_relids, rel->relids) &&
2417  callback(root, rel, cur_ec, cur_em, callback_arg))
2418  break;
2419  cur_em = NULL;
2420  }
2421 
2422  if (!cur_em)
2423  continue;
2424 
2425  /*
2426  * Found our match. Scan the other EC members and attempt to generate
2427  * joinclauses.
2428  */
2429  foreach(lc2, cur_ec->ec_members)
2430  {
2431  EquivalenceMember *other_em = (EquivalenceMember *) lfirst(lc2);
2432  Oid eq_op;
2433  RestrictInfo *rinfo;
2434 
2435  if (other_em->em_is_child)
2436  continue; /* ignore children here */
2437 
2438  /* Make sure it'll be a join to a different rel */
2439  if (other_em == cur_em ||
2440  bms_overlap(other_em->em_relids, rel->relids))
2441  continue;
2442 
2443  /* Forget it if caller doesn't want joins to this rel */
2444  if (bms_overlap(other_em->em_relids, prohibited_rels))
2445  continue;
2446 
2447  /*
2448  * Also, if this is a child rel, avoid generating a useless join
2449  * to its parent rel(s).
2450  */
2451  if (is_child_rel &&
2452  bms_overlap(parent_relids, other_em->em_relids))
2453  continue;
2454 
2455  eq_op = select_equality_operator(cur_ec,
2456  cur_em->em_datatype,
2457  other_em->em_datatype);
2458  if (!OidIsValid(eq_op))
2459  continue;
2460 
2461  /* set parent_ec to mark as redundant with other joinclauses */
2462  rinfo = create_join_clause(root, cur_ec, eq_op,
2463  cur_em, other_em,
2464  cur_ec);
2465 
2466  result = lappend(result, rinfo);
2467  }
2468 
2469  /*
2470  * If somehow we failed to create any join clauses, we might as well
2471  * keep scanning the ECs for another match. But if we did make any,
2472  * we're done, because we don't want to return non-redundant clauses.
2473  */
2474  if (result)
2475  break;
2476  }
2477 
2478  return result;
2479 }
#define NIL
Definition: pg_list.h:65
static RestrictInfo * create_join_clause(PlannerInfo *root, EquivalenceClass *ec, Oid opno, EquivalenceMember *leftem, EquivalenceMember *rightem, EquivalenceClass *parent_ec)
Definition: equivclass.c:1535
RelOptKind reloptkind
Definition: pathnodes.h:638
bool ec_merging_done
Definition: pathnodes.h:266
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:614
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
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:315
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1500
Relids ec_relids
Definition: pathnodes.h:937
Relids relids
Definition: pathnodes.h:641
List * lappend(List *list, void *datum)
Definition: list.c:322
Relids find_childrel_parents(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:1215
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
List * eq_classes
Definition: pathnodes.h:264
static int list_length(const List *l)
Definition: pg_list.h:169
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
int i
Definition: pg_list.h:50
Bitmapset * eclass_indexes
Definition: pathnodes.h:683
List * ec_members
Definition: pathnodes.h:934
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94

◆ generate_join_implied_equalities()

List* generate_join_implied_equalities ( PlannerInfo root,
Relids  join_relids,
Relids  outer_relids,
RelOptInfo inner_rel 
)

Definition at line 1126 of file equivclass.c.

References Assert, bms_is_empty(), bms_next_member(), bms_overlap(), bms_union(), EquivalenceClass::ec_broken, EquivalenceClass::ec_has_const, EquivalenceClass::ec_members, EquivalenceClass::ec_relids, PlannerInfo::eq_classes, generate_join_implied_equalities_broken(), generate_join_implied_equalities_normal(), get_common_eclass_indexes(), i, IS_OTHER_REL, list_concat(), list_length(), list_nth(), NIL, RelOptInfo::relids, and RelOptInfo::top_parent_relids.

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

1130 {
1131  List *result = NIL;
1132  Relids inner_relids = inner_rel->relids;
1133  Relids nominal_inner_relids;
1134  Relids nominal_join_relids;
1135  Bitmapset * matching_ecs;
1136  int i;
1137 
1138  /* If inner rel is a child, extra setup work is needed */
1139  if (IS_OTHER_REL(inner_rel))
1140  {
1141  Assert(!bms_is_empty(inner_rel->top_parent_relids));
1142 
1143  /* Fetch relid set for the topmost parent rel */
1144  nominal_inner_relids = inner_rel->top_parent_relids;
1145  /* ECs will be marked with the parent's relid, not the child's */
1146  nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1147  }
1148  else
1149  {
1150  nominal_inner_relids = inner_relids;
1151  nominal_join_relids = join_relids;
1152  }
1153 
1154  /*
1155  * Get all eclasses in common between inner_rel's relids and outer_relids
1156  */
1157  matching_ecs = get_common_eclass_indexes(root, inner_rel->relids,
1158  outer_relids);
1159 
1160  i = -1;
1161  while ((i = bms_next_member(matching_ecs, i)) >= 0)
1162  {
1164  List *sublist = NIL;
1165 
1166  /* ECs containing consts do not need any further enforcement */
1167  if (ec->ec_has_const)
1168  continue;
1169 
1170  /* Single-member ECs won't generate any deductions */
1171  if (list_length(ec->ec_members) <= 1)
1172  continue;
1173 
1174  /* Sanity check that this eclass overlaps the join */
1175  Assert(bms_overlap(ec->ec_relids, nominal_join_relids));
1176 
1177  if (!ec->ec_broken)
1179  ec,
1180  join_relids,
1181  outer_relids,
1182  inner_relids);
1183 
1184  /* Recover if we failed to generate required derived clauses */
1185  if (ec->ec_broken)
1187  ec,
1188  nominal_join_relids,
1189  outer_relids,
1190  nominal_inner_relids,
1191  inner_rel);
1192 
1193  result = list_concat(result, sublist);
1194  }
1195 
1196  return result;
1197 }
#define NIL
Definition: pg_list.h:65
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:629
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
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:1451
Relids ec_relids
Definition: pathnodes.h:937
Relids relids
Definition: pathnodes.h:641
static List * generate_join_implied_equalities_normal(PlannerInfo *root, EquivalenceClass *ec, Relids join_relids, Relids outer_relids, Relids inner_relids)
Definition: equivclass.c:1275
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
static Bitmapset * get_common_eclass_indexes(PlannerInfo *root, Relids relids1, Relids relids2)
Definition: equivclass.c:2748
#define Assert(condition)
Definition: c.h:732
List * eq_classes
Definition: pathnodes.h:264
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
static int list_length(const List *l)
Definition: pg_list.h:169
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
int i
Definition: pg_list.h:50
List * ec_members
Definition: pathnodes.h:934
Relids top_parent_relids
Definition: pathnodes.h:714

◆ generate_join_implied_equalities_broken()

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 1451 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, and RelOptInfo::top_parent_relids.

Referenced by generate_join_implied_equalities(), and generate_join_implied_equalities_for_ecs().

1457 {
1458  List *result = NIL;
1459  ListCell *lc;
1460 
1461  foreach(lc, ec->ec_sources)
1462  {
1463  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1464  Relids clause_relids = restrictinfo->required_relids;
1465 
1466  if (bms_is_subset(clause_relids, nominal_join_relids) &&
1467  !bms_is_subset(clause_relids, outer_relids) &&
1468  !bms_is_subset(clause_relids, nominal_inner_relids))
1469  result = lappend(result, restrictinfo);
1470  }
1471 
1472  /*
1473  * If we have to translate, just brute-force apply adjust_appendrel_attrs
1474  * to all the RestrictInfos at once. This will result in returning
1475  * RestrictInfos that are not listed in ec_derives, but there shouldn't be
1476  * any duplication, and it's a sufficiently narrow corner case that we
1477  * shouldn't sweat too much over it anyway.
1478  *
1479  * Since inner_rel might be an indirect descendant of the baserel
1480  * mentioned in the ec_sources clauses, we have to be prepared to apply
1481  * multiple levels of Var translation.
1482  */
1483  if (IS_OTHER_REL(inner_rel) && result != NIL)
1484  result = (List *) adjust_appendrel_attrs_multilevel(root,
1485  (Node *) result,
1486  inner_rel->relids,
1487  inner_rel->top_parent_relids);
1488 
1489  return result;
1490 }
#define NIL
Definition: pg_list.h:65
Relids required_relids
Definition: pathnodes.h:1961
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:629
Definition: nodes.h:525
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, Relids child_relids, Relids top_parent_relids)
Definition: appendinfo.c:483
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
List * ec_sources
Definition: pathnodes.h:935
Relids relids
Definition: pathnodes.h:641
List * lappend(List *list, void *datum)
Definition: list.c:322
#define lfirst(lc)
Definition: pg_list.h:190
Definition: pg_list.h:50
Relids top_parent_relids
Definition: pathnodes.h:714

◆ generate_join_implied_equalities_for_ecs()

List* generate_join_implied_equalities_for_ecs ( PlannerInfo root,
List eclasses,
Relids  join_relids,
Relids  outer_relids,
RelOptInfo inner_rel 
)

Definition at line 1204 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, and RelOptInfo::top_parent_relids.

Referenced by get_joinrel_parampathinfo().

1209 {
1210  List *result = NIL;
1211  Relids inner_relids = inner_rel->relids;
1212  Relids nominal_inner_relids;
1213  Relids nominal_join_relids;
1214  ListCell *lc;
1215 
1216  /* If inner rel is a child, extra setup work is needed */
1217  if (IS_OTHER_REL(inner_rel))
1218  {
1219  Assert(!bms_is_empty(inner_rel->top_parent_relids));
1220 
1221  /* Fetch relid set for the topmost parent rel */
1222  nominal_inner_relids = inner_rel->top_parent_relids;
1223  /* ECs will be marked with the parent's relid, not the child's */
1224  nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1225  }
1226  else
1227  {
1228  nominal_inner_relids = inner_relids;
1229  nominal_join_relids = join_relids;
1230  }
1231 
1232  foreach(lc, eclasses)
1233  {
1235  List *sublist = NIL;
1236 
1237  /* ECs containing consts do not need any further enforcement */
1238  if (ec->ec_has_const)
1239  continue;
1240 
1241  /* Single-member ECs won't generate any deductions */
1242  if (list_length(ec->ec_members) <= 1)
1243  continue;
1244 
1245  /* We can quickly ignore any that don't overlap the join, too */
1246  if (!bms_overlap(ec->ec_relids, nominal_join_relids))
1247  continue;
1248 
1249  if (!ec->ec_broken)
1251  ec,
1252  join_relids,
1253  outer_relids,
1254  inner_relids);
1255 
1256  /* Recover if we failed to generate required derived clauses */
1257  if (ec->ec_broken)
1259  ec,
1260  nominal_join_relids,
1261  outer_relids,
1262  nominal_inner_relids,
1263  inner_rel);
1264 
1265  result = list_concat(result, sublist);
1266  }
1267 
1268  return result;
1269 }
#define NIL
Definition: pg_list.h:65
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:629
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
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:1451
Relids ec_relids
Definition: pathnodes.h:937
Relids relids
Definition: pathnodes.h:641
static List * generate_join_implied_equalities_normal(PlannerInfo *root, EquivalenceClass *ec, Relids join_relids, Relids outer_relids, Relids inner_relids)
Definition: equivclass.c:1275
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
static int list_length(const List *l)
Definition: pg_list.h:169
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
Definition: pg_list.h:50
List * ec_members
Definition: pathnodes.h:934
Relids top_parent_relids
Definition: pathnodes.h:714

◆ generate_join_implied_equalities_normal()

static List * generate_join_implied_equalities_normal ( PlannerInfo root,
EquivalenceClass ec,
Relids  join_relids,
Relids  outer_relids,
Relids  inner_relids 
)
static

Definition at line 1275 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(), and select_equality_operator().

Referenced by generate_join_implied_equalities(), and generate_join_implied_equalities_for_ecs().

1280 {
1281  List *result = NIL;
1282  List *new_members = NIL;
1283  List *outer_members = NIL;
1284  List *inner_members = NIL;
1285  ListCell *lc1;
1286 
1287  /*
1288  * First, scan the EC to identify member values that are computable at the
1289  * outer rel, at the inner rel, or at this relation but not in either
1290  * input rel. The outer-rel members should already be enforced equal,
1291  * likewise for the inner-rel members. We'll need to create clauses to
1292  * enforce that any newly computable members are all equal to each other
1293  * as well as to at least one input member, plus enforce at least one
1294  * outer-rel member equal to at least one inner-rel member.
1295  */
1296  foreach(lc1, ec->ec_members)
1297  {
1298  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
1299 
1300  /*
1301  * We don't need to check explicitly for child EC members. This test
1302  * against join_relids will cause them to be ignored except when
1303  * considering a child inner rel, which is what we want.
1304  */
1305  if (!bms_is_subset(cur_em->em_relids, join_relids))
1306  continue; /* not computable yet, or wrong child */
1307 
1308  if (bms_is_subset(cur_em->em_relids, outer_relids))
1309  outer_members = lappend(outer_members, cur_em);
1310  else if (bms_is_subset(cur_em->em_relids, inner_relids))
1311  inner_members = lappend(inner_members, cur_em);
1312  else
1313  new_members = lappend(new_members, cur_em);
1314  }
1315 
1316  /*
1317  * First, select the joinclause if needed. We can equate any one outer
1318  * member to any one inner member, but we have to find a datatype
1319  * combination for which an opfamily member operator exists. If we have
1320  * choices, we prefer simple Var members (possibly with RelabelType) since
1321  * these are (a) cheapest to compute at runtime and (b) most likely to
1322  * have useful statistics. Also, prefer operators that are also
1323  * hashjoinable.
1324  */
1325  if (outer_members && inner_members)
1326  {
1327  EquivalenceMember *best_outer_em = NULL;
1328  EquivalenceMember *best_inner_em = NULL;
1329  Oid best_eq_op = InvalidOid;
1330  int best_score = -1;
1331  RestrictInfo *rinfo;
1332 
1333  foreach(lc1, outer_members)
1334  {
1335  EquivalenceMember *outer_em = (EquivalenceMember *) lfirst(lc1);
1336  ListCell *lc2;
1337 
1338  foreach(lc2, inner_members)
1339  {
1340  EquivalenceMember *inner_em = (EquivalenceMember *) lfirst(lc2);
1341  Oid eq_op;
1342  int score;
1343 
1344  eq_op = select_equality_operator(ec,
1345  outer_em->em_datatype,
1346  inner_em->em_datatype);
1347  if (!OidIsValid(eq_op))
1348  continue;
1349  score = 0;
1350  if (IsA(outer_em->em_expr, Var) ||
1351  (IsA(outer_em->em_expr, RelabelType) &&
1352  IsA(((RelabelType *) outer_em->em_expr)->arg, Var)))
1353  score++;
1354  if (IsA(inner_em->em_expr, Var) ||
1355  (IsA(inner_em->em_expr, RelabelType) &&
1356  IsA(((RelabelType *) inner_em->em_expr)->arg, Var)))
1357  score++;
1358  if (op_hashjoinable(eq_op,
1359  exprType((Node *) outer_em->em_expr)))
1360  score++;
1361  if (score > best_score)
1362  {
1363  best_outer_em = outer_em;
1364  best_inner_em = inner_em;
1365  best_eq_op = eq_op;
1366  best_score = score;
1367  if (best_score == 3)
1368  break; /* no need to look further */
1369  }
1370  }
1371  if (best_score == 3)
1372  break; /* no need to look further */
1373  }
1374  if (best_score < 0)
1375  {
1376  /* failed... */
1377  ec->ec_broken = true;
1378  return NIL;
1379  }
1380 
1381  /*
1382  * Create clause, setting parent_ec to mark it as redundant with other
1383  * joinclauses
1384  */
1385  rinfo = create_join_clause(root, ec, best_eq_op,
1386  best_outer_em, best_inner_em,
1387  ec);
1388 
1389  result = lappend(result, rinfo);
1390  }
1391 
1392  /*
1393  * Now deal with building restrictions for any expressions that involve
1394  * Vars from both sides of the join. We have to equate all of these to
1395  * each other as well as to at least one old member (if any).
1396  *
1397  * XXX as in generate_base_implied_equalities_no_const, we could be a lot
1398  * smarter here to avoid unnecessary failures in cross-type situations.
1399  * For now, use the same left-to-right method used there.
1400  */
1401  if (new_members)
1402  {
1403  List *old_members = list_concat(outer_members, inner_members);
1404  EquivalenceMember *prev_em = NULL;
1405  RestrictInfo *rinfo;
1406 
1407  /* For now, arbitrarily take the first old_member as the one to use */
1408  if (old_members)
1409  new_members = lappend(new_members, linitial(old_members));
1410 
1411  foreach(lc1, new_members)
1412  {
1413  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
1414 
1415  if (prev_em != NULL)
1416  {
1417  Oid eq_op;
1418 
1419  eq_op = select_equality_operator(ec,
1420  prev_em->em_datatype,
1421  cur_em->em_datatype);
1422  if (!OidIsValid(eq_op))
1423  {
1424  /* failed... */
1425  ec->ec_broken = true;
1426  return NIL;
1427  }
1428  /* do NOT set parent_ec, this qual is not redundant! */
1429  rinfo = create_join_clause(root, ec, eq_op,
1430  prev_em, cur_em,
1431  NULL);
1432 
1433  result = lappend(result, rinfo);
1434  }
1435  prev_em = cur_em;
1436  }
1437  }
1438 
1439  return result;
1440 }
#define NIL
Definition: pg_list.h:65
static RestrictInfo * create_join_clause(PlannerInfo *root, EquivalenceClass *ec, Oid opno, EquivalenceMember *leftem, EquivalenceMember *rightem, EquivalenceClass *parent_ec)
Definition: equivclass.c:1535
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Definition: nodes.h:525
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:167
#define OidIsValid(objectId)
Definition: c.h:638
bool op_hashjoinable(Oid opno, Oid inputtype)
Definition: lsyscache.c:1244
#define linitial(l)
Definition: pg_list.h:195
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1500
List * lappend(List *list, void *datum)
Definition: list.c:322
#define InvalidOid
Definition: postgres_ext.h:36
#define lfirst(lc)
Definition: pg_list.h:190
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
Definition: pg_list.h:50
List * ec_members
Definition: pathnodes.h:934

◆ get_common_eclass_indexes()

static Bitmapset * get_common_eclass_indexes ( PlannerInfo root,
Relids  relids1,
Relids  relids2 
)
static

Definition at line 2748 of file equivclass.c.

References bms_get_singleton_member(), bms_int_members(), RelOptInfo::eclass_indexes, get_eclass_indexes_for_relids(), and PlannerInfo::simple_rel_array.

Referenced by generate_join_implied_equalities(), and have_relevant_eclass_joinclause().

2749 {
2750  Bitmapset *rel1ecs;
2751  Bitmapset *rel2ecs;
2752  int relid;
2753 
2754  rel1ecs = get_eclass_indexes_for_relids(root, relids1);
2755 
2756  /*
2757  * We can get away with just using the relation's eclass_indexes directly
2758  * when relids2 is a singleton set.
2759  */
2760  if (bms_get_singleton_member(relids2, &relid))
2761  rel2ecs = root->simple_rel_array[relid]->eclass_indexes;
2762  else
2763  rel2ecs = get_eclass_indexes_for_relids(root, relids2);
2764 
2765  /* Calculate and return the common EC indexes, recycling the left input. */
2766  return bms_int_members(rel1ecs, rel2ecs);
2767 }
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:615
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:201
static Bitmapset * get_eclass_indexes_for_relids(PlannerInfo *root, Relids relids)
Definition: equivclass.c:2724
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:902
Bitmapset * eclass_indexes
Definition: pathnodes.h:683

◆ get_eclass_for_sort_expr()

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 625 of file equivclass.c.

References add_eq_member(), Assert, bms_add_member(), bms_equal(), bms_intersect(), bms_next_member(), 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, PlannerInfo::ec_merging_done, EquivalenceClass::ec_min_security, EquivalenceClass::ec_opfamilies, EquivalenceClass::ec_relids, EquivalenceClass::ec_sortref, EquivalenceClass::ec_sources, RelOptInfo::eclass_indexes, 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(), i, lappend(), lfirst, list_copy(), list_length(), makeNode, MemoryContextSwitchTo(), NIL, PlannerInfo::planner_cxt, pull_varnos(), RELOPT_BASEREL, RELOPT_DEADREL, RelOptInfo::reloptkind, and PlannerInfo::simple_rel_array.

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

634 {
635  Relids expr_relids;
636  EquivalenceClass *newec;
637  EquivalenceMember *newem;
638  ListCell *lc1;
639  MemoryContext oldcontext;
640 
641  /*
642  * Ensure the expression exposes the correct type and collation.
643  */
644  expr = canonicalize_ec_expression(expr, opcintype, collation);
645 
646  /*
647  * Get the precise set of nullable relids appearing in the expression.
648  */
649  expr_relids = pull_varnos((Node *) expr);
650  nullable_relids = bms_intersect(nullable_relids, expr_relids);
651 
652  /*
653  * Scan through the existing EquivalenceClasses for a match
654  */
655  foreach(lc1, root->eq_classes)
656  {
657  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
658  ListCell *lc2;
659 
660  /*
661  * Never match to a volatile EC, except when we are looking at another
662  * reference to the same volatile SortGroupClause.
663  */
664  if (cur_ec->ec_has_volatile &&
665  (sortref == 0 || sortref != cur_ec->ec_sortref))
666  continue;
667 
668  if (collation != cur_ec->ec_collation)
669  continue;
670  if (!equal(opfamilies, cur_ec->ec_opfamilies))
671  continue;
672 
673  foreach(lc2, cur_ec->ec_members)
674  {
675  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
676 
677  /*
678  * Ignore child members unless they match the request.
679  */
680  if (cur_em->em_is_child &&
681  !bms_equal(cur_em->em_relids, rel))
682  continue;
683 
684  /*
685  * If below an outer join, don't match constants: they're not as
686  * constant as they look.
687  */
688  if (cur_ec->ec_below_outer_join &&
689  cur_em->em_is_const)
690  continue;
691 
692  if (opcintype == cur_em->em_datatype &&
693  equal(expr, cur_em->em_expr))
694  return cur_ec; /* Match! */
695  }
696  }
697 
698  /* No match; does caller want a NULL result? */
699  if (!create_it)
700  return NULL;
701 
702  /*
703  * OK, build a new single-member EC
704  *
705  * Here, we must be sure that we construct the EC in the right context.
706  */
707  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
708 
709  newec = makeNode(EquivalenceClass);
710  newec->ec_opfamilies = list_copy(opfamilies);
711  newec->ec_collation = collation;
712  newec->ec_members = NIL;
713  newec->ec_sources = NIL;
714  newec->ec_derives = NIL;
715  newec->ec_relids = NULL;
716  newec->ec_has_const = false;
718  newec->ec_below_outer_join = false;
719  newec->ec_broken = false;
720  newec->ec_sortref = sortref;
721  newec->ec_min_security = UINT_MAX;
722  newec->ec_max_security = 0;
723  newec->ec_merged = NULL;
724 
725  if (newec->ec_has_volatile && sortref == 0) /* should not happen */
726  elog(ERROR, "volatile EquivalenceClass has no sortref");
727 
728  newem = add_eq_member(newec, copyObject(expr), expr_relids,
729  nullable_relids, false, opcintype);
730 
731  /*
732  * add_eq_member doesn't check for volatile functions, set-returning
733  * functions, aggregates, or window functions, but such could appear in
734  * sort expressions; so we have to check whether its const-marking was
735  * correct.
736  */
737  if (newec->ec_has_const)
738  {
739  if (newec->ec_has_volatile ||
740  expression_returns_set((Node *) expr) ||
741  contain_agg_clause((Node *) expr) ||
742  contain_window_function((Node *) expr))
743  {
744  newec->ec_has_const = false;
745  newem->em_is_const = false;
746  }
747  }
748 
749  root->eq_classes = lappend(root->eq_classes, newec);
750 
751  /*
752  * If EC merging is already complete, we have to mop up by adding the new
753  * EC to the eclass_indexes of the relation(s) mentioned in it.
754  */
755  if (root->ec_merging_done)
756  {
757  int ec_index = list_length(root->eq_classes) - 1;
758  int i = -1;
759 
760  while ((i = bms_next_member(newec->ec_relids, i)) > 0)
761  {
762  RelOptInfo *rel = root->simple_rel_array[i];
763 
764  Assert(rel->reloptkind == RELOPT_BASEREL ||
765  rel->reloptkind == RELOPT_DEADREL);
766 
768  ec_index);
769  }
770  }
771 
772  MemoryContextSwitchTo(oldcontext);
773 
774  return newec;
775 }
#define NIL
Definition: pg_list.h:65
RelOptKind reloptkind
Definition: pathnodes.h:638
bool ec_merging_done
Definition: pathnodes.h:266
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3008
Index ec_min_security
Definition: pathnodes.h:944
List * ec_derives
Definition: pathnodes.h:936
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
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:1404
Definition: nodes.h:525
bool ec_below_outer_join
Definition: pathnodes.h:941
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724
Index ec_max_security
Definition: pathnodes.h:945
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:201
#define ERROR
Definition: elog.h:43
Expr * canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
Definition: equivclass.c:499
List * ec_sources
Definition: pathnodes.h:935
Relids ec_relids
Definition: pathnodes.h:937
Relids pull_varnos(Node *node)
Definition: var.c:95
bool contain_window_function(Node *clause)
Definition: clauses.c:494
List * lappend(List *list, void *datum)
Definition: list.c:322
List * ec_opfamilies
Definition: pathnodes.h:932
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:259
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
List * eq_classes
Definition: pathnodes.h:264
static int list_length(const List *l)
Definition: pg_list.h:169
bool ec_has_volatile
Definition: pathnodes.h:940
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
bool contain_agg_clause(Node *clause)
Definition: clauses.c:179
#define elog(elevel,...)
Definition: elog.h:226
int i
static EquivalenceMember * add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids, Relids nullable_relids, bool is_child, Oid datatype)
Definition: equivclass.c:549
MemoryContext planner_cxt
Definition: pathnodes.h:329
#define copyObject(obj)
Definition: nodes.h:641
Bitmapset * eclass_indexes
Definition: pathnodes.h:683
struct EquivalenceClass * ec_merged
Definition: pathnodes.h:946
List * ec_members
Definition: pathnodes.h:934
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94

◆ get_eclass_indexes_for_relids()

static Bitmapset * get_eclass_indexes_for_relids ( PlannerInfo root,
Relids  relids 
)
static

Definition at line 2724 of file equivclass.c.

References Assert, bms_add_members(), bms_next_member(), PlannerInfo::ec_merging_done, RelOptInfo::eclass_indexes, i, and PlannerInfo::simple_rel_array.

Referenced by get_common_eclass_indexes(), and has_relevant_eclass_joinclause().

2725 {
2726  Bitmapset *ec_indexes = NULL;
2727  int i = -1;
2728 
2729  /* Should be OK to rely on eclass_indexes */
2730  Assert(root->ec_merging_done);
2731 
2732  while ((i = bms_next_member(relids, i)) > 0)
2733  {
2734  RelOptInfo *rel = root->simple_rel_array[i];
2735 
2736  ec_indexes = bms_add_members(ec_indexes, rel->eclass_indexes);
2737  }
2738  return ec_indexes;
2739 }
bool ec_merging_done
Definition: pathnodes.h:266
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:201
#define Assert(condition)
Definition: c.h:732
int i
Bitmapset * eclass_indexes
Definition: pathnodes.h:683
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793

◆ has_relevant_eclass_joinclause()

bool has_relevant_eclass_joinclause ( PlannerInfo root,
RelOptInfo rel1 
)

Definition at line 2559 of file equivclass.c.

References bms_is_subset(), bms_next_member(), EquivalenceClass::ec_members, EquivalenceClass::ec_relids, PlannerInfo::eq_classes, get_eclass_indexes_for_relids(), i, list_length(), list_nth(), and RelOptInfo::relids.

Referenced by build_join_rel().

2560 {
2561  Bitmapset *matched_ecs;
2562  int i;
2563 
2564  /* Examine only eclasses mentioning rel1 */
2565  matched_ecs = get_eclass_indexes_for_relids(root, rel1->relids);
2566 
2567  i = -1;
2568  while ((i = bms_next_member(matched_ecs, i)) >= 0)
2569  {
2571  i);
2572 
2573  /*
2574  * Won't generate joinclauses if single-member (this test covers the
2575  * volatile case too)
2576  */
2577  if (list_length(ec->ec_members) <= 1)
2578  continue;
2579 
2580  /*
2581  * Per the comment in have_relevant_eclass_joinclause, it's sufficient
2582  * to find an EC that mentions both this rel and some other rel.
2583  */
2584  if (!bms_is_subset(ec->ec_relids, rel1->relids))
2585  return true;
2586  }
2587 
2588  return false;
2589 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
static Bitmapset * get_eclass_indexes_for_relids(PlannerInfo *root, Relids relids)
Definition: equivclass.c:2724
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids ec_relids
Definition: pathnodes.h:937
Relids relids
Definition: pathnodes.h:641
List * eq_classes
Definition: pathnodes.h:264
static int list_length(const List *l)
Definition: pg_list.h:169
int i
List * ec_members
Definition: pathnodes.h:934

◆ have_relevant_eclass_joinclause()

bool have_relevant_eclass_joinclause ( PlannerInfo root,
RelOptInfo rel1,
RelOptInfo rel2 
)

Definition at line 2492 of file equivclass.c.

References Assert, bms_next_member(), bms_overlap(), EquivalenceClass::ec_members, EquivalenceClass::ec_relids, PlannerInfo::eq_classes, get_common_eclass_indexes(), i, list_length(), list_nth(), and RelOptInfo::relids.

Referenced by have_relevant_joinclause().

2494 {
2495  Bitmapset *matching_ecs;
2496  int i;
2497 
2498  /* Examine only eclasses mentioning both rel1 and rel2 */
2499  matching_ecs = get_common_eclass_indexes(root, rel1->relids,
2500  rel2->relids);
2501 
2502  i = -1;
2503  while ((i = bms_next_member(matching_ecs, i)) >= 0)
2504  {
2506  i);
2507 
2508  /*
2509  * Sanity check that get_common_eclass_indexes gave only ECs
2510  * containing both rels.
2511  */
2512  Assert(bms_overlap(rel1->relids, ec->ec_relids));
2513  Assert(bms_overlap(rel2->relids, ec->ec_relids));
2514 
2515  /*
2516  * Won't generate joinclauses if single-member (this test covers the
2517  * volatile case too)
2518  */
2519  if (list_length(ec->ec_members) <= 1)
2520  continue;
2521 
2522  /*
2523  * We do not need to examine the individual members of the EC, because
2524  * all that we care about is whether each rel overlaps the relids of
2525  * at least one member, and get_common_eclass_indexes() and the single
2526  * member check above are sufficient to prove that. (As with
2527  * have_relevant_joinclause(), it is not necessary that the EC be able
2528  * to form a joinclause relating exactly the two given rels, only that
2529  * it be able to form a joinclause mentioning both, and this will
2530  * surely be true if both of them overlap ec_relids.)
2531  *
2532  * Note we don't test ec_broken; if we did, we'd need a separate code
2533  * path to look through ec_sources. Checking the membership anyway is
2534  * OK as a possibly-overoptimistic heuristic.
2535  *
2536  * We don't test ec_has_const either, even though a const eclass won't
2537  * generate real join clauses. This is because if we had "WHERE a.x =
2538  * b.y and a.x = 42", it is worth considering a join between a and b,
2539  * since the join result is likely to be small even though it'll end
2540  * up being an unqualified nestloop.
2541  */
2542 
2543  return true;
2544  }
2545 
2546  return false;
2547 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
Relids ec_relids
Definition: pathnodes.h:937
Relids relids
Definition: pathnodes.h:641
static Bitmapset * get_common_eclass_indexes(PlannerInfo *root, Relids relids1, Relids relids2)
Definition: equivclass.c:2748
#define Assert(condition)
Definition: c.h:732
List * eq_classes
Definition: pathnodes.h:264
static int list_length(const List *l)
Definition: pg_list.h:169
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
int i
List * ec_members
Definition: pathnodes.h:934

◆ is_redundant_derived_clause()

bool is_redundant_derived_clause ( RestrictInfo rinfo,
List clauselist 
)

Definition at line 2661 of file equivclass.c.

References lfirst, and RestrictInfo::parent_ec.

Referenced by create_tidscan_plan().

2662 {
2663  EquivalenceClass *parent_ec = rinfo->parent_ec;
2664  ListCell *lc;
2665 
2666  /* Fail if it's not a potentially-redundant clause from some EC */
2667  if (parent_ec == NULL)
2668  return false;
2669 
2670  foreach(lc, clauselist)
2671  {
2672  RestrictInfo *otherrinfo = (RestrictInfo *) lfirst(lc);
2673 
2674  if (otherrinfo->parent_ec == parent_ec)
2675  return true;
2676  }
2677 
2678  return false;
2679 }
EquivalenceClass * parent_ec
Definition: pathnodes.h:1977
#define lfirst(lc)
Definition: pg_list.h:190

◆ is_redundant_with_indexclauses()

bool is_redundant_with_indexclauses ( RestrictInfo rinfo,
List indexclauses 
)

Definition at line 2688 of file equivclass.c.

References lfirst_node, IndexClause::lossy, RestrictInfo::parent_ec, and IndexClause::rinfo.

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

2689 {
2690  EquivalenceClass *parent_ec = rinfo->parent_ec;
2691  ListCell *lc;
2692 
2693  foreach(lc, indexclauses)
2694  {
2695  IndexClause *iclause = lfirst_node(IndexClause, lc);
2696  RestrictInfo *otherrinfo = iclause->rinfo;
2697 
2698  /* If indexclause is lossy, it won't enforce the condition exactly */
2699  if (iclause->lossy)
2700  continue;
2701 
2702  /* Match if it's same clause (pointer equality should be enough) */
2703  if (rinfo == otherrinfo)
2704  return true;
2705  /* Match if derived from same EC */
2706  if (parent_ec && otherrinfo->parent_ec == parent_ec)
2707  return true;
2708 
2709  /*
2710  * No need to look at the derived clauses in iclause->indexquals; they
2711  * couldn't match if the parent clause didn't.
2712  */
2713  }
2714 
2715  return false;
2716 }
EquivalenceClass * parent_ec
Definition: pathnodes.h:1977
#define lfirst_node(type, lc)
Definition: pg_list.h:193
struct RestrictInfo * rinfo
Definition: pathnodes.h:1223

◆ match_eclasses_to_foreign_key_col()

EquivalenceClass* match_eclasses_to_foreign_key_col ( PlannerInfo root,
ForeignKeyOptInfo fkinfo,
int  colno 
)

Definition at line 2132 of file equivclass.c.

References Assert, bms_intersect(), bms_next_member(), ForeignKeyOptInfo::con_relid, ForeignKeyOptInfo::confkey, ForeignKeyOptInfo::conkey, ForeignKeyOptInfo::conpfeqop, EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_members, PlannerInfo::ec_merging_done, EquivalenceClass::ec_opfamilies, RelOptInfo::eclass_indexes, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, PlannerInfo::eq_classes, equal(), get_mergejoin_opfamilies(), i, IS_SIMPLE_REL, IsA, lfirst, list_nth(), NIL, ForeignKeyOptInfo::ref_relid, PlannerInfo::simple_rel_array, RangeQueryClause::var, Var::varattno, and Var::varno.

Referenced by match_foreign_keys_to_quals().

2135 {
2136  Index var1varno = fkinfo->con_relid;
2137  AttrNumber var1attno = fkinfo->conkey[colno];
2138  Index var2varno = fkinfo->ref_relid;
2139  AttrNumber var2attno = fkinfo->confkey[colno];
2140  Oid eqop = fkinfo->conpfeqop[colno];
2141  RelOptInfo *rel1 = root->simple_rel_array[var1varno];
2142  RelOptInfo *rel2 = root->simple_rel_array[var2varno];
2143  List *opfamilies = NIL; /* compute only if needed */
2144  Bitmapset *matching_ecs;
2145  int i;
2146 
2147  /* Consider only eclasses mentioning both relations */
2148  Assert(root->ec_merging_done);
2149  Assert(IS_SIMPLE_REL(rel1));
2150  Assert(IS_SIMPLE_REL(rel2));
2151  matching_ecs = bms_intersect(rel1->eclass_indexes,
2152  rel2->eclass_indexes);
2153 
2154  i = -1;
2155  while ((i = bms_next_member(matching_ecs, i)) >= 0)
2156  {
2158  i);
2159  bool item1member = false;
2160  bool item2member = false;
2161  ListCell *lc2;
2162 
2163  /* Never match to a volatile EC */
2164  if (ec->ec_has_volatile)
2165  continue;
2166  /* Note: it seems okay to match to "broken" eclasses here */
2167 
2168  foreach(lc2, ec->ec_members)
2169  {
2171  Var *var;
2172 
2173  if (em->em_is_child)
2174  continue; /* ignore children here */
2175 
2176  /* EM must be a Var, possibly with RelabelType */
2177  var = (Var *) em->em_expr;
2178  while (var && IsA(var, RelabelType))
2179  var = (Var *) ((RelabelType *) var)->arg;
2180  if (!(var && IsA(var, Var)))
2181  continue;
2182 
2183  /* Match? */
2184  if (var->varno == var1varno && var->varattno == var1attno)
2185  item1member = true;
2186  else if (var->varno == var2varno && var->varattno == var2attno)
2187  item2member = true;
2188 
2189  /* Have we found both PK and FK column in this EC? */
2190  if (item1member && item2member)
2191  {
2192  /*
2193  * Succeed if eqop matches EC's opfamilies. We could test
2194  * this before scanning the members, but it's probably cheaper
2195  * to test for member matches first.
2196  */
2197  if (opfamilies == NIL) /* compute if we didn't already */
2198  opfamilies = get_mergejoin_opfamilies(eqop);
2199  if (equal(opfamilies, ec->ec_opfamilies))
2200  return ec;
2201  /* Otherwise, done with this EC, move on to the next */
2202  break;
2203  }
2204  }
2205  }
2206  return NULL;
2207 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
bool ec_merging_done
Definition: pathnodes.h:266
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3008
List * get_mergejoin_opfamilies(Oid opno)
Definition: lsyscache.c:363
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
AttrNumber varattno
Definition: primnodes.h:172
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:167
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:614
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:201
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
Oid conpfeqop[INDEX_MAX_KEYS]
Definition: pathnodes.h:859
Index varno
Definition: primnodes.h:170
List * ec_opfamilies
Definition: pathnodes.h:932
AttrNumber conkey[INDEX_MAX_KEYS]
Definition: pathnodes.h:857
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:259
unsigned int Index
Definition: c.h:475
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
List * eq_classes
Definition: pathnodes.h:264
bool ec_has_volatile
Definition: pathnodes.h:940
int i
Definition: pg_list.h:50
AttrNumber confkey[INDEX_MAX_KEYS]
Definition: pathnodes.h:858
int16 AttrNumber
Definition: attnum.h:21
Bitmapset * eclass_indexes
Definition: pathnodes.h:683
List * ec_members
Definition: pathnodes.h:934

◆ process_equivalence()

bool process_equivalence ( PlannerInfo root,
RestrictInfo **  p_restrictinfo,
bool  below_outer_join 
)

Definition at line 118 of file equivclass.c.

References add_eq_member(), NullTest::arg, NullTest::argisrow, Assert, bms_intersect(), bms_is_empty(), bms_join(), 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, PlannerInfo::ec_merging_done, 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(), func_strict(), get_leftop(), get_rightop(), IS_NOT_NULL, is_opclause(), RestrictInfo::is_pushed_down, lappend(), RestrictInfo::leakproof, RestrictInfo::left_ec, RestrictInfo::left_em, RestrictInfo::left_relids, lfirst, list_concat(), list_delete_ptr(), list_make1, NullTest::location, make_restrictinfo(), makeNode, Max, RestrictInfo::mergeopfamilies, Min, NIL, RestrictInfo::nullable_relids, NullTest::nulltesttype, op_input_types(), RestrictInfo::outer_relids, RestrictInfo::outerjoin_delayed, RestrictInfo::pseudoconstant, RestrictInfo::right_ec, RestrictInfo::right_em, RestrictInfo::right_relids, RestrictInfo::security_level, and set_opfuncid().

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

121 {
122  RestrictInfo *restrictinfo = *p_restrictinfo;
123  Expr *clause = restrictinfo->clause;
124  Oid opno,
125  collation,
126  item1_type,
127  item2_type;
128  Expr *item1;
129  Expr *item2;
130  Relids item1_relids,
131  item2_relids,
132  item1_nullable_relids,
133  item2_nullable_relids;
134  List *opfamilies;
135  EquivalenceClass *ec1,
136  *ec2;
137  EquivalenceMember *em1,
138  *em2;
139  ListCell *lc1;
140 
141  /* Should not already be marked as having generated an eclass */
142  Assert(restrictinfo->left_ec == NULL);
143  Assert(restrictinfo->right_ec == NULL);
144 
145  /* Reject if it is potentially postponable by security considerations */
146  if (restrictinfo->security_level > 0 && !restrictinfo->leakproof)
147  return false;
148 
149  /* Extract info from given clause */
150  Assert(is_opclause(clause));
151  opno = ((OpExpr *) clause)->opno;
152  collation = ((OpExpr *) clause)->inputcollid;
153  item1 = (Expr *) get_leftop(clause);
154  item2 = (Expr *) get_rightop(clause);
155  item1_relids = restrictinfo->left_relids;
156  item2_relids = restrictinfo->right_relids;
157 
158  /*
159  * Ensure both input expressions expose the desired collation (their types
160  * should be OK already); see comments for canonicalize_ec_expression.
161  */
162  item1 = canonicalize_ec_expression(item1,
163  exprType((Node *) item1),
164  collation);
165  item2 = canonicalize_ec_expression(item2,
166  exprType((Node *) item2),
167  collation);
168 
169  /*
170  * Clauses of the form X=X cannot be translated into EquivalenceClasses.
171  * We'd either end up with a single-entry EC, losing the knowledge that
172  * the clause was present at all, or else make an EC with duplicate
173  * entries, causing other issues.
174  */
175  if (equal(item1, item2))
176  {
177  /*
178  * If the operator is strict, then the clause can be treated as just
179  * "X IS NOT NULL". (Since we know we are considering a top-level
180  * qual, we can ignore the difference between FALSE and NULL results.)
181  * It's worth making the conversion because we'll typically get a much
182  * better selectivity estimate than we would for X=X.
183  *
184  * If the operator is not strict, we can't be sure what it will do
185  * with NULLs, so don't attempt to optimize it.
186  */
187  set_opfuncid((OpExpr *) clause);
188  if (func_strict(((OpExpr *) clause)->opfuncid))
189  {
190  NullTest *ntest = makeNode(NullTest);
191 
192  ntest->arg = item1;
193  ntest->nulltesttype = IS_NOT_NULL;
194  ntest->argisrow = false; /* correct even if composite arg */
195  ntest->location = -1;
196 
197  *p_restrictinfo =
198  make_restrictinfo((Expr *) ntest,
199  restrictinfo->is_pushed_down,
200  restrictinfo->outerjoin_delayed,
201  restrictinfo->pseudoconstant,
202  restrictinfo->security_level,
203  NULL,
204  restrictinfo->outer_relids,
205  restrictinfo->nullable_relids);
206  }
207  return false;
208  }
209 
210  /*
211  * If below outer join, check for strictness, else reject.
212  */
213  if (below_outer_join)
214  {
215  if (!bms_is_empty(item1_relids) &&
217  return false; /* LHS is non-strict but not constant */
218  if (!bms_is_empty(item2_relids) &&
220  return false; /* RHS is non-strict but not constant */
221  }
222 
223  /* Calculate nullable-relid sets for each side of the clause */
224  item1_nullable_relids = bms_intersect(item1_relids,
225  restrictinfo->nullable_relids);
226  item2_nullable_relids = bms_intersect(item2_relids,
227  restrictinfo->nullable_relids);
228 
229  /*
230  * We use the declared input types of the operator, not exprType() of the
231  * inputs, as the nominal datatypes for opfamily lookup. This presumes
232  * that btree operators are always registered with amoplefttype and
233  * amoprighttype equal to their declared input types. We will need this
234  * info anyway to build EquivalenceMember nodes, and by extracting it now
235  * we can use type comparisons to short-circuit some equal() tests.
236  */
237  op_input_types(opno, &item1_type, &item2_type);
238 
239  opfamilies = restrictinfo->mergeopfamilies;
240 
241  /*
242  * Sweep through the existing EquivalenceClasses looking for matches to
243  * item1 and item2. These are the possible outcomes:
244  *
245  * 1. We find both in the same EC. The equivalence is already known, so
246  * there's nothing to do.
247  *
248  * 2. We find both in different ECs. Merge the two ECs together.
249  *
250  * 3. We find just one. Add the other to its EC.
251  *
252  * 4. We find neither. Make a new, two-entry EC.
253  *
254  * Note: since all ECs are built through this process or the similar
255  * search in get_eclass_for_sort_expr(), it's impossible that we'd match
256  * an item in more than one existing nonvolatile EC. So it's okay to stop
257  * at the first match.
258  */
259  ec1 = ec2 = NULL;
260  em1 = em2 = NULL;
261  foreach(lc1, root->eq_classes)
262  {
263  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
264  ListCell *lc2;
265 
266  /* Never match to a volatile EC */
267  if (cur_ec->ec_has_volatile)
268  continue;
269 
270  /*
271  * The collation has to match; check this first since it's cheaper
272  * than the opfamily comparison.
273  */
274  if (collation != cur_ec->ec_collation)
275  continue;
276 
277  /*
278  * A "match" requires matching sets of btree opfamilies. Use of
279  * equal() for this test has implications discussed in the comments
280  * for get_mergejoin_opfamilies().
281  */
282  if (!equal(opfamilies, cur_ec->ec_opfamilies))
283  continue;
284 
285  foreach(lc2, cur_ec->ec_members)
286  {
287  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
288 
289  Assert(!cur_em->em_is_child); /* no children yet */
290 
291  /*
292  * If below an outer join, don't match constants: they're not as
293  * constant as they look.
294  */
295  if ((below_outer_join || cur_ec->ec_below_outer_join) &&
296  cur_em->em_is_const)
297  continue;
298 
299  if (!ec1 &&
300  item1_type == cur_em->em_datatype &&
301  equal(item1, cur_em->em_expr))
302  {
303  ec1 = cur_ec;
304  em1 = cur_em;
305  if (ec2)
306  break;
307  }
308 
309  if (!ec2 &&
310  item2_type == cur_em->em_datatype &&
311  equal(item2, cur_em->em_expr))
312  {
313  ec2 = cur_ec;
314  em2 = cur_em;
315  if (ec1)
316  break;
317  }
318  }
319 
320  if (ec1 && ec2)
321  break;
322  }
323 
324  /* Sweep finished, what did we find? */
325 
326  if (ec1 && ec2)
327  {
328  /* If case 1, nothing to do, except add to sources */
329  if (ec1 == ec2)
330  {
331  ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
332  ec1->ec_below_outer_join |= below_outer_join;
333  ec1->ec_min_security = Min(ec1->ec_min_security,
334  restrictinfo->security_level);
335  ec1->ec_max_security = Max(ec1->ec_max_security,
336  restrictinfo->security_level);
337  /* mark the RI as associated with this eclass */
338  restrictinfo->left_ec = ec1;
339  restrictinfo->right_ec = ec1;
340  /* mark the RI as usable with this pair of EMs */
341  restrictinfo->left_em = em1;
342  restrictinfo->right_em = em2;
343  return true;
344  }
345 
346  /*
347  * Case 2: need to merge ec1 and ec2. This should never happen after
348  * the ECs have reached canonical state; otherwise, pathkeys could be
349  * rendered non-canonical by the merge, and relation eclass indexes
350  * would get broken by removal of an eq_classes list entry.
351  */
352  if (root->ec_merging_done)
353  elog(ERROR, "too late to merge equivalence classes");
354 
355  /*
356  * We add ec2's items to ec1, then set ec2's ec_merged link to point
357  * to ec1 and remove ec2 from the eq_classes list. We cannot simply
358  * delete ec2 because that could leave dangling pointers in existing
359  * PathKeys. We leave it behind with a link so that the merged EC can
360  * be found.
361  */
362  ec1->ec_members = list_concat(ec1->ec_members, ec2->ec_members);
363  ec1->ec_sources = list_concat(ec1->ec_sources, ec2->ec_sources);
364  ec1->ec_derives = list_concat(ec1->ec_derives, ec2->ec_derives);
365  ec1->ec_relids = bms_join(ec1->ec_relids, ec2->ec_relids);
366  ec1->ec_has_const |= ec2->ec_has_const;
367  /* can't need to set has_volatile */
369  ec1->ec_min_security = Min(ec1->ec_min_security,
370  ec2->ec_min_security);
371  ec1->ec_max_security = Max(ec1->ec_max_security,
372  ec2->ec_max_security);
373  ec2->ec_merged = ec1;
374  root->eq_classes = list_delete_ptr(root->eq_classes, ec2);
375  /* just to avoid debugging confusion w/ dangling pointers: */
376  ec2->ec_members = NIL;
377  ec2->ec_sources = NIL;
378  ec2->ec_derives = NIL;
379  ec2->ec_relids = NULL;
380  ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
381  ec1->ec_below_outer_join |= below_outer_join;
382  ec1->ec_min_security = Min(ec1->ec_min_security,
383  restrictinfo->security_level);
384  ec1->ec_max_security = Max(ec1->ec_max_security,
385  restrictinfo->security_level);
386  /* mark the RI as associated with this eclass */
387  restrictinfo->left_ec = ec1;
388  restrictinfo->right_ec = ec1;
389  /* mark the RI as usable with this pair of EMs */
390  restrictinfo->left_em = em1;
391  restrictinfo->right_em = em2;
392  }
393  else if (ec1)
394  {
395  /* Case 3: add item2 to ec1 */
396  em2 = add_eq_member(ec1, item2, item2_relids, item2_nullable_relids,
397  false, item2_type);
398  ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
399  ec1->ec_below_outer_join |= below_outer_join;
400  ec1->ec_min_security = Min(ec1->ec_min_security,
401  restrictinfo->security_level);
402  ec1->ec_max_security = Max(ec1->ec_max_security,
403  restrictinfo->security_level);
404  /* mark the RI as associated with this eclass */
405  restrictinfo->left_ec = ec1;
406  restrictinfo->right_ec = ec1;
407  /* mark the RI as usable with this pair of EMs */
408  restrictinfo->left_em = em1;
409  restrictinfo->right_em = em2;
410  }
411  else if (ec2)
412  {
413  /* Case 3: add item1 to ec2 */
414  em1 = add_eq_member(ec2, item1, item1_relids, item1_nullable_relids,
415  false, item1_type);
416  ec2->ec_sources = lappend(ec2->ec_sources, restrictinfo);
417  ec2->ec_below_outer_join |= below_outer_join;
418  ec2->ec_min_security = Min(ec2->ec_min_security,
419  restrictinfo->security_level);
420  ec2->ec_max_security = Max(ec2->ec_max_security,
421  restrictinfo->security_level);
422  /* mark the RI as associated with this eclass */
423  restrictinfo->left_ec = ec2;
424  restrictinfo->right_ec = ec2;
425  /* mark the RI as usable with this pair of EMs */
426  restrictinfo->left_em = em1;
427  restrictinfo->right_em = em2;
428  }
429  else
430  {
431  /* Case 4: make a new, two-entry EC */
433 
434  ec->ec_opfamilies = opfamilies;
435  ec->ec_collation = collation;
436  ec->ec_members = NIL;
437  ec->ec_sources = list_make1(restrictinfo);
438  ec->ec_derives = NIL;
439  ec->ec_relids = NULL;
440  ec->ec_has_const = false;
441  ec->ec_has_volatile = false;
442  ec->ec_below_outer_join = below_outer_join;
443  ec->ec_broken = false;
444  ec->ec_sortref = 0;
445  ec->ec_min_security = restrictinfo->security_level;
446  ec->ec_max_security = restrictinfo->security_level;
447  ec->ec_merged = NULL;
448  em1 = add_eq_member(ec, item1, item1_relids, item1_nullable_relids,
449  false, item1_type);
450  em2 = add_eq_member(ec, item2, item2_relids, item2_nullable_relids,
451  false, item2_type);
452 
453  root->eq_classes = lappend(root->eq_classes, ec);
454 
455  /* mark the RI as associated with this eclass */
456  restrictinfo->left_ec = ec;
457  restrictinfo->right_ec = ec;
458  /* mark the RI as usable with this pair of EMs */
459  restrictinfo->left_em = em1;
460  restrictinfo->right_em = em2;
461  }
462 
463  return true;
464 }
#define NIL
Definition: pg_list.h:65
Index security_level
Definition: pathnodes.h:1955
RestrictInfo * make_restrictinfo(Expr *clause, bool is_pushed_down, bool outerjoin_delayed, bool pseudoconstant, Index security_level, Relids required_relids, Relids outer_relids, Relids nullable_relids)
Definition: restrictinfo.c:59
bool ec_merging_done
Definition: pathnodes.h:266
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3008
bool leakproof
Definition: pathnodes.h:1953
Index ec_min_security
Definition: pathnodes.h:944
List * ec_derives
Definition: pathnodes.h:936
#define Min(x, y)
Definition: c.h:904
bool pseudoconstant
Definition: pathnodes.h:1951
Definition: nodes.h:525
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
Relids left_relids
Definition: pathnodes.h:1970
bool ec_below_outer_join
Definition: pathnodes.h:941
EquivalenceClass * right_ec
Definition: pathnodes.h:1992
Index ec_max_security
Definition: pathnodes.h:945
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:797
unsigned int Oid
Definition: postgres_ext.h:31
List * mergeopfamilies
Definition: pathnodes.h:1988
Relids outer_relids
Definition: pathnodes.h:1964
#define list_make1(x1)
Definition: pg_list.h:227
#define ERROR
Definition: elog.h:43
EquivalenceMember * left_em
Definition: pathnodes.h:1993
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:949
Expr * arg
Definition: primnodes.h:1205
Expr * canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
Definition: equivclass.c:499
bool outerjoin_delayed
Definition: pathnodes.h:1947
List * ec_sources
Definition: pathnodes.h:935
Relids ec_relids
Definition: pathnodes.h:937
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1165
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:70
List * lappend(List *list, void *datum)
Definition: list.c:322
EquivalenceMember * right_em
Definition: pathnodes.h:1994
Expr * clause
Definition: pathnodes.h:1943
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
List * ec_opfamilies
Definition: pathnodes.h:932
Relids nullable_relids
Definition: pathnodes.h:1967
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:259
NullTestType nulltesttype
Definition: primnodes.h:1206
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:82
bool is_pushed_down
Definition: pathnodes.h:1945
#define Max(x, y)
Definition: c.h:898
#define makeNode(_type_)
Definition: nodes.h:573
Relids right_relids
Definition: pathnodes.h:1971
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
List * eq_classes
Definition: pathnodes.h:264
int location
Definition: primnodes.h:1208
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
bool ec_has_volatile
Definition: pathnodes.h:940
bool func_strict(Oid funcid)
Definition: lsyscache.c:1563
EquivalenceClass * left_ec
Definition: pathnodes.h:1991
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1619
#define elog(elevel,...)
Definition: elog.h:226
static EquivalenceMember * add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids, Relids nullable_relids, bool is_child, Oid datatype)
Definition: equivclass.c:549
bool argisrow
Definition: primnodes.h:1207
bool contain_nonstrict_functions(Node *clause)
Definition: clauses.c:1094
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:63
Definition: pg_list.h:50
struct EquivalenceClass * ec_merged
Definition: pathnodes.h:946
List * ec_members
Definition: pathnodes.h:934

◆ reconsider_full_join_clause()

static bool reconsider_full_join_clause ( PlannerInfo root,
RestrictInfo rinfo 
)
static

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

1909 {
1910  Expr *leftvar;
1911  Expr *rightvar;
1912  Oid opno,
1913  collation,
1914  left_type,
1915  right_type;
1916  Relids left_relids,
1917  right_relids,
1918  left_nullable_relids,
1919  right_nullable_relids;
1920  ListCell *lc1;
1921 
1922  /* Can't use an outerjoin_delayed clause here */
1923  if (rinfo->outerjoin_delayed)
1924  return false;
1925 
1926  /* Extract needed info from the clause */
1927  Assert(is_opclause(rinfo->clause));
1928  opno = ((OpExpr *) rinfo->clause)->opno;
1929  collation = ((OpExpr *) rinfo->clause)->inputcollid;
1930  op_input_types(opno, &left_type, &right_type);
1931  leftvar = (Expr *) get_leftop(rinfo->clause);
1932  rightvar = (Expr *) get_rightop(rinfo->clause);
1933  left_relids = rinfo->left_relids;
1934  right_relids = rinfo->right_relids;
1935  left_nullable_relids = bms_intersect(left_relids,
1936  rinfo->nullable_relids);
1937  right_nullable_relids = bms_intersect(right_relids,
1938  rinfo->nullable_relids);
1939 
1940  foreach(lc1, root->eq_classes)
1941  {
1942  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
1943  EquivalenceMember *coal_em = NULL;
1944  bool match;
1945  bool matchleft;
1946  bool matchright;
1947  ListCell *lc2;
1948 
1949  /* Ignore EC unless it contains pseudoconstants */
1950  if (!cur_ec->ec_has_const)
1951  continue;
1952  /* Never match to a volatile EC */
1953  if (cur_ec->ec_has_volatile)
1954  continue;
1955  /* It has to match the outer-join clause as to semantics, too */
1956  if (collation != cur_ec->ec_collation)
1957  continue;
1958  if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
1959  continue;
1960 
1961  /*
1962  * Does it contain a COALESCE(leftvar, rightvar) construct?
1963  *
1964  * We can assume the COALESCE() inputs are in the same order as the
1965  * join clause, since both were automatically generated in the cases
1966  * we care about.
1967  *
1968  * XXX currently this may fail to match in cross-type cases because
1969  * the COALESCE will contain typecast operations while the join clause
1970  * may not (if there is a cross-type mergejoin operator available for
1971  * the two column types). Is it OK to strip implicit coercions from
1972  * the COALESCE arguments?
1973  */
1974  match = false;
1975  foreach(lc2, cur_ec->ec_members)
1976  {
1977  coal_em = (EquivalenceMember *) lfirst(lc2);
1978  Assert(!coal_em->em_is_child); /* no children yet */
1979  if (IsA(coal_em->em_expr, CoalesceExpr))
1980  {
1981  CoalesceExpr *cexpr = (CoalesceExpr *) coal_em->em_expr;
1982  Node *cfirst;
1983  Node *csecond;
1984 
1985  if (list_length(cexpr->args) != 2)
1986  continue;
1987  cfirst = (Node *) linitial(cexpr->args);
1988  csecond = (Node *) lsecond(cexpr->args);
1989 
1990  if (equal(leftvar, cfirst) && equal(rightvar, csecond))
1991  {
1992  match = true;
1993  break;
1994  }
1995  }
1996  }
1997  if (!match)
1998  continue; /* no match, so ignore this EC */
1999 
2000  /*
2001  * Yes it does! Try to generate clauses LEFTVAR = CONSTANT and
2002  * RIGHTVAR = CONSTANT for each CONSTANT in the EC. Note that we must
2003  * succeed with at least one constant for each var before we can
2004  * decide to throw away the outer-join clause.
2005  */
2006  matchleft = matchright = false;
2007  foreach(lc2, cur_ec->ec_members)
2008  {
2009  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2010  Oid eq_op;
2011  RestrictInfo *newrinfo;
2012 
2013  if (!cur_em->em_is_const)
2014  continue; /* ignore non-const members */
2015  eq_op = select_equality_operator(cur_ec,
2016  left_type,
2017  cur_em->em_datatype);
2018  if (OidIsValid(eq_op))
2019  {
2020  newrinfo = build_implied_join_equality(eq_op,
2021  cur_ec->ec_collation,
2022  leftvar,
2023  cur_em->em_expr,
2024  bms_copy(left_relids),
2025  bms_copy(left_nullable_relids),
2026  cur_ec->ec_min_security);
2027  if (process_equivalence(root, &newrinfo, true))
2028  matchleft = true;
2029  }
2030  eq_op = select_equality_operator(cur_ec,
2031  right_type,
2032  cur_em->em_datatype);
2033  if (OidIsValid(eq_op))
2034  {
2035  newrinfo = build_implied_join_equality(eq_op,
2036  cur_ec->ec_collation,
2037  rightvar,
2038  cur_em->em_expr,
2039  bms_copy(right_relids),
2040  bms_copy(right_nullable_relids),
2041  cur_ec->ec_min_security);
2042  if (process_equivalence(root, &newrinfo, true))
2043  matchright = true;
2044  }
2045  }
2046 
2047  /*
2048  * If we were able to equate both vars to constants, we're done, and
2049  * we can throw away the full-join clause as redundant. Moreover, we
2050  * can remove the COALESCE entry from the EC, since the added
2051  * restrictions ensure it will always have the expected value. (We
2052  * don't bother trying to update ec_relids or ec_sources.)
2053  */
2054  if (matchleft && matchright)
2055  {
2056  cur_ec->ec_members = list_delete_ptr(cur_ec->ec_members, coal_em);
2057  return true;
2058  }
2059 
2060  /*
2061  * Otherwise, fall out of the search loop, since we know the COALESCE
2062  * appears in at most one EC (XXX might stop being true if we allow
2063  * stripping of coercions above?)
2064  */
2065  break;
2066  }
2067 
2068  return false; /* failed to make any deduction */
2069 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3008
Index ec_min_security
Definition: pathnodes.h:944
Definition: nodes.h:525
Relids left_relids
Definition: pathnodes.h:1970
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:797
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
List * mergeopfamilies
Definition: pathnodes.h:1988
#define lsecond(l)
Definition: pg_list.h:200
#define linitial(l)
Definition: pg_list.h:195
bool process_equivalence(PlannerInfo *root, RestrictInfo **p_restrictinfo, bool below_outer_join)
Definition: equivclass.c:118
List * args
Definition: primnodes.h:1072
bool outerjoin_delayed
Definition: pathnodes.h:1947
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1500
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1165
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:70
Expr * clause
Definition: pathnodes.h:1943
HashCompareFunc match
Definition: dynahash.c:213
List * ec_opfamilies
Definition: pathnodes.h:932
Relids nullable_relids
Definition: pathnodes.h:1967
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:259
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:82
Relids right_relids
Definition: pathnodes.h:1971
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
List * eq_classes
Definition: pathnodes.h:264
static int list_length(const List *l)
Definition: pg_list.h:169
bool ec_has_volatile
Definition: pathnodes.h:940
RestrictInfo * build_implied_join_equality(Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
Definition: initsplan.c:2354
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:63
List * ec_members
Definition: pathnodes.h:934

◆ reconsider_outer_join_clause()

static bool reconsider_outer_join_clause ( PlannerInfo root,
RestrictInfo rinfo,
bool  outer_on_left 
)
static

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

1785 {
1786  Expr *outervar,
1787  *innervar;
1788  Oid opno,
1789  collation,
1790  left_type,
1791  right_type,
1792  inner_datatype;
1793  Relids inner_relids,
1794  inner_nullable_relids;
1795  ListCell *lc1;
1796 
1797  Assert(is_opclause(rinfo->clause));
1798  opno = ((OpExpr *) rinfo->clause)->opno;
1799  collation = ((OpExpr *) rinfo->clause)->inputcollid;
1800 
1801  /* If clause is outerjoin_delayed, operator must be strict */
1802  if (rinfo->outerjoin_delayed && !op_strict(opno))
1803  return false;
1804 
1805  /* Extract needed info from the clause */
1806  op_input_types(opno, &left_type, &right_type);
1807  if (outer_on_left)
1808  {
1809  outervar = (Expr *) get_leftop(rinfo->clause);
1810  innervar = (Expr *) get_rightop(rinfo->clause);
1811  inner_datatype = right_type;
1812  inner_relids = rinfo->right_relids;
1813  }
1814  else
1815  {
1816  outervar = (Expr *) get_rightop(rinfo->clause);
1817  innervar = (Expr *) get_leftop(rinfo->clause);
1818  inner_datatype = left_type;
1819  inner_relids = rinfo->left_relids;
1820  }
1821  inner_nullable_relids = bms_intersect(inner_relids,
1822  rinfo->nullable_relids);
1823 
1824  /* Scan EquivalenceClasses for a match to outervar */
1825  foreach(lc1, root->eq_classes)
1826  {
1827  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
1828  bool match;
1829  ListCell *lc2;
1830 
1831  /* Ignore EC unless it contains pseudoconstants */
1832  if (!cur_ec->ec_has_const)
1833  continue;
1834  /* Never match to a volatile EC */
1835  if (cur_ec->ec_has_volatile)
1836  continue;
1837  /* It has to match the outer-join clause as to semantics, too */
1838  if (collation != cur_ec->ec_collation)
1839  continue;
1840  if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
1841  continue;
1842  /* Does it contain a match to outervar? */
1843  match = false;
1844  foreach(lc2, cur_ec->ec_members)
1845  {
1846  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
1847 
1848  Assert(!cur_em->em_is_child); /* no children yet */
1849  if (equal(outervar, cur_em->em_expr))
1850  {
1851  match = true;
1852  break;
1853  }
1854  }
1855  if (!match)
1856  continue; /* no match, so ignore this EC */
1857 
1858  /*
1859  * Yes it does! Try to generate a clause INNERVAR = CONSTANT for each
1860  * CONSTANT in the EC. Note that we must succeed with at least one
1861  * constant before we can decide to throw away the outer-join clause.
1862  */
1863  match = false;
1864  foreach(lc2, cur_ec->ec_members)
1865  {
1866  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
1867  Oid eq_op;
1868  RestrictInfo *newrinfo;
1869 
1870  if (!cur_em->em_is_const)
1871  continue; /* ignore non-const members */
1872  eq_op = select_equality_operator(cur_ec,
1873  inner_datatype,
1874  cur_em->em_datatype);
1875  if (!OidIsValid(eq_op))
1876  continue; /* can't generate equality */
1877  newrinfo = build_implied_join_equality(eq_op,
1878  cur_ec->ec_collation,
1879  innervar,
1880  cur_em->em_expr,
1881  bms_copy(inner_relids),
1882  bms_copy(inner_nullable_relids),
1883  cur_ec->ec_min_security);
1884  if (process_equivalence(root, &newrinfo, true))
1885  match = true;
1886  }
1887 
1888  /*
1889  * If we were able to equate INNERVAR to any constant, report success.
1890  * Otherwise, fall out of the search loop, since we know the OUTERVAR
1891  * appears in at most one EC.
1892  */
1893  if (match)
1894  return true;
1895  else
1896  break;
1897  }
1898 
1899  return false; /* failed to make any deduction */
1900 }
bool op_strict(Oid opno)
Definition: lsyscache.c:1279
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3008
Index ec_min_security
Definition: pathnodes.h:944
Relids left_relids
Definition: pathnodes.h:1970
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
List * mergeopfamilies
Definition: pathnodes.h:1988
bool process_equivalence(PlannerInfo *root, RestrictInfo **p_restrictinfo, bool below_outer_join)
Definition: equivclass.c:118
bool outerjoin_delayed
Definition: pathnodes.h:1947
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1500
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1165
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:70
Expr * clause
Definition: pathnodes.h:1943
HashCompareFunc match
Definition: dynahash.c:213
List * ec_opfamilies
Definition: pathnodes.h:932
Relids nullable_relids
Definition: pathnodes.h:1967
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:259
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:82
Relids right_relids
Definition: pathnodes.h:1971
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
List * eq_classes
Definition: pathnodes.h:264
bool ec_has_volatile
Definition: pathnodes.h:940
RestrictInfo * build_implied_join_equality(Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
Definition: initsplan.c:2354
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:63
List * ec_members
Definition: pathnodes.h:934

◆ reconsider_outer_join_clauses()

void reconsider_outer_join_clauses ( PlannerInfo root)

Definition at line 1688 of file equivclass.c.

References distribute_restrictinfo_to_rels(), foreach_delete_current, PlannerInfo::full_join_clauses, PlannerInfo::left_join_clauses, lfirst, RestrictInfo::norm_selec, RestrictInfo::outer_selec, reconsider_full_join_clause(), reconsider_outer_join_clause(), and PlannerInfo::right_join_clauses.

Referenced by query_planner().

1689 {
1690  bool found;
1691  ListCell *cell;
1692 
1693  /* Outer loop repeats until we find no more deductions */
1694  do
1695  {
1696  found = false;
1697 
1698  /* Process the LEFT JOIN clauses */
1699  foreach(cell, root->left_join_clauses)
1700  {
1701  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1702 
1703  if (reconsider_outer_join_clause(root, rinfo, true))
1704  {
1705  found = true;
1706  /* remove it from the list */
1707  root->left_join_clauses =
1709  /* we throw it back anyway (see notes above) */
1710  /* but the thrown-back clause has no extra selectivity */
1711  rinfo->norm_selec = 2.0;
1712  rinfo->outer_selec = 1.0;
1713  distribute_restrictinfo_to_rels(root, rinfo);
1714  }
1715  }
1716 
1717  /* Process the RIGHT JOIN clauses */
1718  foreach(cell, root->right_join_clauses)
1719  {
1720  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1721 
1722  if (reconsider_outer_join_clause(root, rinfo, false))
1723  {
1724  found = true;
1725  /* remove it from the list */
1726  root->right_join_clauses =
1728  /* we throw it back anyway (see notes above) */
1729  /* but the thrown-back clause has no extra selectivity */
1730  rinfo->norm_selec = 2.0;
1731  rinfo->outer_selec = 1.0;
1732  distribute_restrictinfo_to_rels(root, rinfo);
1733  }
1734  }
1735 
1736  /* Process the FULL JOIN clauses */
1737  foreach(cell, root->full_join_clauses)
1738  {
1739  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1740 
1741  if (reconsider_full_join_clause(root, rinfo))
1742  {
1743  found = true;
1744  /* remove it from the list */
1745  root->full_join_clauses =
1747  /* we throw it back anyway (see notes above) */
1748  /* but the thrown-back clause has no extra selectivity */
1749  rinfo->norm_selec = 2.0;
1750  rinfo->outer_selec = 1.0;
1751  distribute_restrictinfo_to_rels(root, rinfo);
1752  }
1753  }
1754  } while (found);
1755 
1756  /* Now, any remaining clauses have to be thrown back */
1757  foreach(cell, root->left_join_clauses)
1758  {
1759  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1760 
1761  distribute_restrictinfo_to_rels(root, rinfo);
1762  }
1763  foreach(cell, root->right_join_clauses)
1764  {
1765  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1766 
1767  distribute_restrictinfo_to_rels(root, rinfo);
1768  }
1769  foreach(cell, root->full_join_clauses)
1770  {
1771  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1772 
1773  distribute_restrictinfo_to_rels(root, rinfo);
1774  }
1775 }
static bool reconsider_full_join_clause(PlannerInfo *root, RestrictInfo *rinfo)
Definition: equivclass.c:1908
static bool reconsider_outer_join_clause(PlannerInfo *root, RestrictInfo *rinfo, bool outer_on_left)
Definition: equivclass.c:1783
Selectivity norm_selec
Definition: pathnodes.h:1981
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:368
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2205
List * left_join_clauses
Definition: pathnodes.h:270
List * full_join_clauses
Definition: pathnodes.h:278
Selectivity outer_selec
Definition: pathnodes.h:1984
#define lfirst(lc)
Definition: pg_list.h:190
List * right_join_clauses
Definition: pathnodes.h:274

◆ select_equality_operator()

static Oid select_equality_operator ( EquivalenceClass ec,
Oid  lefttype,
Oid  righttype 
)
static

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

1501 {
1502  ListCell *lc;
1503 
1504  foreach(lc, ec->ec_opfamilies)
1505  {
1506  Oid opfamily = lfirst_oid(lc);
1507  Oid opno;
1508 
1509  opno = get_opfamily_member(opfamily, lefttype, righttype,
1511  if (!OidIsValid(opno))
1512  continue;
1513  /* If no barrier quals in query, don't worry about leaky operators */
1514  if (ec->ec_max_security == 0)
1515  return opno;
1516  /* Otherwise, insist that selected operators be leakproof */
1517  if (get_func_leakproof(get_opcode(opno)))
1518  return opno;
1519  }
1520  return InvalidOid;
1521 }
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:1639
Index ec_max_security
Definition: pathnodes.h:945
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
List * ec_opfamilies
Definition: pathnodes.h:932
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1092
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define lfirst_oid(lc)
Definition: pg_list.h:192