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 bool is_exprlist_member (Expr *node, List *exprs)
 
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)
 
EquivalenceMemberfind_ec_member_matching_expr (EquivalenceClass *ec, Expr *expr, Relids relids)
 
EquivalenceMemberfind_computable_ec_member (PlannerInfo *root, EquivalenceClass *ec, List *exprs, Relids relids, bool require_parallel_safe)
 
Exprfind_em_expr_for_rel (EquivalenceClass *ec, RelOptInfo *rel)
 
bool relation_can_be_sorted_early (PlannerInfo *root, RelOptInfo *rel, EquivalenceClass *ec, bool require_parallel_safe)
 
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)
 
RestrictInfofind_derived_clause_for_ec_member (EquivalenceClass *ec, EquivalenceMember *em)
 
void add_child_rel_equivalences (PlannerInfo *root, AppendRelInfo *appinfo, RelOptInfo *parent_rel, RelOptInfo *child_rel)
 
void add_child_join_rel_equivalences (PlannerInfo *root, int nappinfos, AppendRelInfo **appinfos, RelOptInfo *parent_joinrel, RelOptInfo *child_joinrel)
 
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_join_rel_equivalences()

void add_child_join_rel_equivalences ( PlannerInfo root,
int  nappinfos,
AppendRelInfo **  appinfos,
RelOptInfo parent_joinrel,
RelOptInfo child_joinrel 
)

Definition at line 2696 of file equivclass.c.

2700 {
2701  Relids top_parent_relids = child_joinrel->top_parent_relids;
2702  Relids child_relids = child_joinrel->relids;
2703  Bitmapset *matching_ecs;
2704  MemoryContext oldcontext;
2705  int i;
2706 
2707  Assert(IS_JOIN_REL(child_joinrel) && IS_JOIN_REL(parent_joinrel));
2708 
2709  /* We need consider only ECs that mention the parent joinrel */
2710  matching_ecs = get_eclass_indexes_for_relids(root, top_parent_relids);
2711 
2712  /*
2713  * If we're being called during GEQO join planning, we still have to
2714  * create any new EC members in the main planner context, to avoid having
2715  * a corrupt EC data structure after the GEQO context is reset. This is
2716  * problematic since we'll leak memory across repeated GEQO cycles. For
2717  * now, though, bloat is better than crash. If it becomes a real issue
2718  * we'll have to do something to avoid generating duplicate EC members.
2719  */
2720  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
2721 
2722  i = -1;
2723  while ((i = bms_next_member(matching_ecs, i)) >= 0)
2724  {
2725  EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2726  int num_members;
2727 
2728  /*
2729  * If this EC contains a volatile expression, then generating child
2730  * EMs would be downright dangerous, so skip it. We rely on a
2731  * volatile EC having only one EM.
2732  */
2733  if (cur_ec->ec_has_volatile)
2734  continue;
2735 
2736  /* Sanity check on get_eclass_indexes_for_relids result */
2737  Assert(bms_overlap(top_parent_relids, cur_ec->ec_relids));
2738 
2739  /*
2740  * We don't use foreach() here because there's no point in scanning
2741  * newly-added child members, so we can stop after the last
2742  * pre-existing EC member.
2743  */
2744  num_members = list_length(cur_ec->ec_members);
2745  for (int pos = 0; pos < num_members; pos++)
2746  {
2747  EquivalenceMember *cur_em = (EquivalenceMember *) list_nth(cur_ec->ec_members, pos);
2748 
2749  if (cur_em->em_is_const)
2750  continue; /* ignore consts here */
2751 
2752  /*
2753  * We consider only original EC members here, not
2754  * already-transformed child members.
2755  */
2756  if (cur_em->em_is_child)
2757  continue; /* ignore children here */
2758 
2759  /*
2760  * We may ignore expressions that reference a single baserel,
2761  * because add_child_rel_equivalences should have handled them.
2762  */
2763  if (bms_membership(cur_em->em_relids) != BMS_MULTIPLE)
2764  continue;
2765 
2766  /* Does this member reference child's topmost parent rel? */
2767  if (bms_overlap(cur_em->em_relids, top_parent_relids))
2768  {
2769  /* Yes, generate transformed child version */
2770  Expr *child_expr;
2771  Relids new_relids;
2772  Relids new_nullable_relids;
2773 
2774  if (parent_joinrel->reloptkind == RELOPT_JOINREL)
2775  {
2776  /* Simple single-level transformation */
2777  child_expr = (Expr *)
2779  (Node *) cur_em->em_expr,
2780  nappinfos, appinfos);
2781  }
2782  else
2783  {
2784  /* Must do multi-level transformation */
2785  Assert(parent_joinrel->reloptkind == RELOPT_OTHER_JOINREL);
2786  child_expr = (Expr *)
2788  (Node *) cur_em->em_expr,
2789  child_relids,
2790  top_parent_relids);
2791  }
2792 
2793  /*
2794  * Transform em_relids to match. Note we do *not* do
2795  * pull_varnos(child_expr) here, as for example the
2796  * transformation might have substituted a constant, but we
2797  * don't want the child member to be marked as constant.
2798  */
2799  new_relids = bms_difference(cur_em->em_relids,
2800  top_parent_relids);
2801  new_relids = bms_add_members(new_relids, child_relids);
2802 
2803  /*
2804  * For nullable_relids, we must selectively replace parent
2805  * nullable relids with child ones.
2806  */
2807  new_nullable_relids = cur_em->em_nullable_relids;
2808  if (bms_overlap(new_nullable_relids, top_parent_relids))
2809  new_nullable_relids =
2811  new_nullable_relids,
2812  child_relids,
2813  top_parent_relids);
2814 
2815  (void) add_eq_member(cur_ec, child_expr,
2816  new_relids, new_nullable_relids,
2817  true, cur_em->em_datatype);
2818  }
2819  }
2820  }
2821 
2822  MemoryContextSwitchTo(oldcontext);
2823 }
Relids adjust_child_relids_multilevel(PlannerInfo *root, Relids relids, Relids child_relids, Relids top_parent_relids)
Definition: appendinfo.c:562
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:195
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, Relids child_relids, Relids top_parent_relids)
Definition: appendinfo.c:488
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1045
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:291
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:795
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:674
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
@ BMS_MULTIPLE
Definition: bitmapset.h:70
static EquivalenceMember * add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids, Relids nullable_relids, bool is_child, Oid datatype)
Definition: equivclass.c:545
static Bitmapset * get_eclass_indexes_for_relids(PlannerInfo *root, Relids relids)
Definition: equivclass.c:3212
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:659
@ RELOPT_JOINREL
Definition: pathnodes.h:642
@ RELOPT_OTHER_JOINREL
Definition: pathnodes.h:644
static int list_length(const List *l)
Definition: pg_list.h:149
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
Relids ec_relids
Definition: pathnodes.h:992
List * ec_members
Definition: pathnodes.h:989
bool ec_has_volatile
Definition: pathnodes.h:995
Relids em_nullable_relids
Definition: pathnodes.h:1039
Definition: nodes.h:539
MemoryContext planner_cxt
Definition: pathnodes.h:335
List * eq_classes
Definition: pathnodes.h:249
Relids relids
Definition: pathnodes.h:681
Relids top_parent_relids
Definition: pathnodes.h:756
RelOptKind reloptkind
Definition: pathnodes.h:678

References add_eq_member(), adjust_appendrel_attrs(), adjust_appendrel_attrs_multilevel(), adjust_child_relids_multilevel(), Assert(), bms_add_members(), bms_difference(), bms_membership(), BMS_MULTIPLE, bms_next_member(), bms_overlap(), EquivalenceClass::ec_has_volatile, 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, PlannerInfo::eq_classes, get_eclass_indexes_for_relids(), i, IS_JOIN_REL, list_length(), list_nth(), MemoryContextSwitchTo(), PlannerInfo::planner_cxt, RelOptInfo::relids, RELOPT_JOINREL, RELOPT_OTHER_JOINREL, RelOptInfo::reloptkind, and RelOptInfo::top_parent_relids.

Referenced by build_child_join_rel().

◆ add_child_rel_equivalences()

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

Definition at line 2568 of file equivclass.c.

2572 {
2573  Relids top_parent_relids = child_rel->top_parent_relids;
2574  Relids child_relids = child_rel->relids;
2575  int i;
2576 
2577  /*
2578  * EC merging should be complete already, so we can use the parent rel's
2579  * eclass_indexes to avoid searching all of root->eq_classes.
2580  */
2581  Assert(root->ec_merging_done);
2582  Assert(IS_SIMPLE_REL(parent_rel));
2583 
2584  i = -1;
2585  while ((i = bms_next_member(parent_rel->eclass_indexes, i)) >= 0)
2586  {
2587  EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2588  int num_members;
2589 
2590  /*
2591  * If this EC contains a volatile expression, then generating child
2592  * EMs would be downright dangerous, so skip it. We rely on a
2593  * volatile EC having only one EM.
2594  */
2595  if (cur_ec->ec_has_volatile)
2596  continue;
2597 
2598  /* Sanity check eclass_indexes only contain ECs for parent_rel */
2599  Assert(bms_is_subset(top_parent_relids, cur_ec->ec_relids));
2600 
2601  /*
2602  * We don't use foreach() here because there's no point in scanning
2603  * newly-added child members, so we can stop after the last
2604  * pre-existing EC member.
2605  */
2606  num_members = list_length(cur_ec->ec_members);
2607  for (int pos = 0; pos < num_members; pos++)
2608  {
2609  EquivalenceMember *cur_em = (EquivalenceMember *) list_nth(cur_ec->ec_members, pos);
2610 
2611  if (cur_em->em_is_const)
2612  continue; /* ignore consts here */
2613 
2614  /*
2615  * We consider only original EC members here, not
2616  * already-transformed child members. Otherwise, if some original
2617  * member expression references more than one appendrel, we'd get
2618  * an O(N^2) explosion of useless derived expressions for
2619  * combinations of children. (But add_child_join_rel_equivalences
2620  * may add targeted combinations for partitionwise-join purposes.)
2621  */
2622  if (cur_em->em_is_child)
2623  continue; /* ignore children here */
2624 
2625  /* Does this member reference child's topmost parent rel? */
2626  if (bms_overlap(cur_em->em_relids, top_parent_relids))
2627  {
2628  /* Yes, generate transformed child version */
2629  Expr *child_expr;
2630  Relids new_relids;
2631  Relids new_nullable_relids;
2632 
2633  if (parent_rel->reloptkind == RELOPT_BASEREL)
2634  {
2635  /* Simple single-level transformation */
2636  child_expr = (Expr *)
2638  (Node *) cur_em->em_expr,
2639  1, &appinfo);
2640  }
2641  else
2642  {
2643  /* Must do multi-level transformation */
2644  child_expr = (Expr *)
2646  (Node *) cur_em->em_expr,
2647  child_relids,
2648  top_parent_relids);
2649  }
2650 
2651  /*
2652  * Transform em_relids to match. Note we do *not* do
2653  * pull_varnos(child_expr) here, as for example the
2654  * transformation might have substituted a constant, but we
2655  * don't want the child member to be marked as constant.
2656  */
2657  new_relids = bms_difference(cur_em->em_relids,
2658  top_parent_relids);
2659  new_relids = bms_add_members(new_relids, child_relids);
2660 
2661  /*
2662  * And likewise for nullable_relids. Note this code assumes
2663  * parent and child relids are singletons.
2664  */
2665  new_nullable_relids = cur_em->em_nullable_relids;
2666  if (bms_overlap(new_nullable_relids, top_parent_relids))
2667  {
2668  new_nullable_relids = bms_difference(new_nullable_relids,
2669  top_parent_relids);
2670  new_nullable_relids = bms_add_members(new_nullable_relids,
2671  child_relids);
2672  }
2673 
2674  (void) add_eq_member(cur_ec, child_expr,
2675  new_relids, new_nullable_relids,
2676  true, cur_em->em_datatype);
2677 
2678  /* Record this EC index for the child rel */
2679  child_rel->eclass_indexes = bms_add_member(child_rel->eclass_indexes, i);
2680  }
2681  }
2682  }
2683 }
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:738
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:654
@ RELOPT_BASEREL
Definition: pathnodes.h:641
bool ec_merging_done
Definition: pathnodes.h:251
Bitmapset * eclass_indexes
Definition: pathnodes.h:723

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

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

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

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_join_rel_equivalences(), add_child_rel_equivalences(), get_eclass_for_sort_expr(), and process_equivalence().

◆ canonicalize_ec_expression()

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

Definition at line 500 of file equivclass.c.

501 {
502  Oid expr_type = exprType((Node *) expr);
503 
504  /*
505  * For a polymorphic-input-type opclass, just keep the same exposed type.
506  * RECORD opclasses work like polymorphic-type ones for this purpose.
507  */
508  if (IsPolymorphicType(req_type) || req_type == RECORDOID)
509  req_type = expr_type;
510 
511  /*
512  * No work if the expression exposes the right type/collation already.
513  */
514  if (expr_type != req_type ||
515  exprCollation((Node *) expr) != req_collation)
516  {
517  /*
518  * If we have to change the type of the expression, set typmod to -1,
519  * since the new type may not have the same typmod interpretation.
520  * When we only have to change collation, preserve the exposed typmod.
521  */
522  int32 req_typmod;
523 
524  if (expr_type != req_type)
525  req_typmod = -1;
526  else
527  req_typmod = exprTypmod((Node *) expr);
528 
529  /*
530  * Use applyRelabelType so that we preserve const-flatness. This is
531  * important since eval_const_expressions has already been applied.
532  */
533  expr = (Expr *) applyRelabelType((Node *) expr,
534  req_type, req_typmod, req_collation,
535  COERCE_IMPLICIT_CAST, -1, false);
536  }
537 
538  return expr;
539 }
signed int int32
Definition: c.h:429
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
Node * applyRelabelType(Node *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat, int rlocation, bool overwrite_ok)
Definition: nodeFuncs.c:582
unsigned int Oid
Definition: postgres_ext.h:31
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:485

References applyRelabelType(), COERCE_IMPLICIT_CAST, exprCollation(), exprType(), and exprTypmod().

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

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

1835 {
1836  RestrictInfo *rinfo;
1837  ListCell *lc;
1838  MemoryContext oldcontext;
1839 
1840  /*
1841  * Search to see if we already built a RestrictInfo for this pair of
1842  * EquivalenceMembers. We can use either original source clauses or
1843  * previously-derived clauses. The check on opno is probably redundant,
1844  * but be safe ...
1845  */
1846  foreach(lc, ec->ec_sources)
1847  {
1848  rinfo = (RestrictInfo *) lfirst(lc);
1849  if (rinfo->left_em == leftem &&
1850  rinfo->right_em == rightem &&
1851  rinfo->parent_ec == parent_ec &&
1852  opno == ((OpExpr *) rinfo->clause)->opno)
1853  return rinfo;
1854  }
1855 
1856  foreach(lc, ec->ec_derives)
1857  {
1858  rinfo = (RestrictInfo *) lfirst(lc);
1859  if (rinfo->left_em == leftem &&
1860  rinfo->right_em == rightem &&
1861  rinfo->parent_ec == parent_ec &&
1862  opno == ((OpExpr *) rinfo->clause)->opno)
1863  return rinfo;
1864  }
1865 
1866  /*
1867  * Not there, so build it, in planner context so we can re-use it. (Not
1868  * important in normal planning, but definitely so in GEQO.)
1869  */
1870  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
1871 
1872  rinfo = build_implied_join_equality(root,
1873  opno,
1874  ec->ec_collation,
1875  leftem->em_expr,
1876  rightem->em_expr,
1877  bms_union(leftem->em_relids,
1878  rightem->em_relids),
1879  bms_union(leftem->em_nullable_relids,
1880  rightem->em_nullable_relids),
1881  ec->ec_min_security);
1882 
1883  /* Mark the clause as redundant, or not */
1884  rinfo->parent_ec = parent_ec;
1885 
1886  /*
1887  * We know the correct values for left_ec/right_ec, ie this particular EC,
1888  * so we can just set them directly instead of forcing another lookup.
1889  */
1890  rinfo->left_ec = ec;
1891  rinfo->right_ec = ec;
1892 
1893  /* Mark it as usable with these EMs */
1894  rinfo->left_em = leftem;
1895  rinfo->right_em = rightem;
1896  /* and save it for possible re-use */
1897  ec->ec_derives = lappend(ec->ec_derives, rinfo);
1898 
1899  MemoryContextSwitchTo(oldcontext);
1900 
1901  return rinfo;
1902 }
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
RestrictInfo * build_implied_join_equality(PlannerInfo *root, Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
Definition: initsplan.c:2422
#define lfirst(lc)
Definition: pg_list.h:169
Index ec_min_security
Definition: pathnodes.h:999
List * ec_derives
Definition: pathnodes.h:991
List * ec_sources
Definition: pathnodes.h:990
EquivalenceClass * left_ec
Definition: pathnodes.h:2109
EquivalenceMember * left_em
Definition: pathnodes.h:2111
Expr * clause
Definition: pathnodes.h:2058
EquivalenceClass * parent_ec
Definition: pathnodes.h:2095
EquivalenceClass * right_ec
Definition: pathnodes.h:2110
EquivalenceMember * right_em
Definition: pathnodes.h:2112

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

◆ eclass_useful_for_merging()

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

Definition at line 3091 of file equivclass.c.

3094 {
3095  Relids relids;
3096  ListCell *lc;
3097 
3098  Assert(!eclass->ec_merged);
3099 
3100  /*
3101  * Won't generate joinclauses if const or single-member (the latter test
3102  * covers the volatile case too)
3103  */
3104  if (eclass->ec_has_const || list_length(eclass->ec_members) <= 1)
3105  return false;
3106 
3107  /*
3108  * Note we don't test ec_broken; if we did, we'd need a separate code path
3109  * to look through ec_sources. Checking the members anyway is OK as a
3110  * possibly-overoptimistic heuristic.
3111  */
3112 
3113  /* If specified rel is a child, we must consider the topmost parent rel */
3114  if (IS_OTHER_REL(rel))
3115  {
3117  relids = rel->top_parent_relids;
3118  }
3119  else
3120  relids = rel->relids;
3121 
3122  /* If rel already includes all members of eclass, no point in searching */
3123  if (bms_is_subset(eclass->ec_relids, relids))
3124  return false;
3125 
3126  /* To join, we need a member not in the given rel */
3127  foreach(lc, eclass->ec_members)
3128  {
3129  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
3130 
3131  if (cur_em->em_is_child)
3132  continue; /* ignore children here */
3133 
3134  if (!bms_overlap(cur_em->em_relids, relids))
3135  return true;
3136  }
3137 
3138  return false;
3139 }
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:669
static struct cvec * eclass(struct vars *v, chr c, int cases)
Definition: regc_locale.c:504

References Assert(), bms_is_empty(), bms_is_subset(), bms_overlap(), eclass(), 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().

◆ exprs_known_equal()

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

Definition at line 2386 of file equivclass.c.

2387 {
2388  ListCell *lc1;
2389 
2390  foreach(lc1, root->eq_classes)
2391  {
2392  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2393  bool item1member = false;
2394  bool item2member = false;
2395  ListCell *lc2;
2396 
2397  /* Never match to a volatile EC */
2398  if (ec->ec_has_volatile)
2399  continue;
2400 
2401  foreach(lc2, ec->ec_members)
2402  {
2404 
2405  if (em->em_is_child)
2406  continue; /* ignore children here */
2407  if (equal(item1, em->em_expr))
2408  item1member = true;
2409  else if (equal(item2, em->em_expr))
2410  item2member = true;
2411  /* Exit as soon as equality is proven */
2412  if (item1member && item2member)
2413  return true;
2414  }
2415  }
2416  return false;
2417 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3161

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

◆ find_computable_ec_member()

EquivalenceMember* find_computable_ec_member ( PlannerInfo root,
EquivalenceClass ec,
List exprs,
Relids  relids,
bool  require_parallel_safe 
)

Definition at line 851 of file equivclass.c.

856 {
857  ListCell *lc;
858 
859  foreach(lc, ec->ec_members)
860  {
862  List *exprvars;
863  ListCell *lc2;
864 
865  /*
866  * We shouldn't be trying to sort by an equivalence class that
867  * contains a constant, so no need to consider such cases any further.
868  */
869  if (em->em_is_const)
870  continue;
871 
872  /*
873  * Ignore child members unless they belong to the requested rel.
874  */
875  if (em->em_is_child &&
876  !bms_is_subset(em->em_relids, relids))
877  continue;
878 
879  /*
880  * Match if all Vars and quasi-Vars are available in "exprs".
881  */
882  exprvars = pull_var_clause((Node *) em->em_expr,
886  foreach(lc2, exprvars)
887  {
888  if (!is_exprlist_member(lfirst(lc2), exprs))
889  break;
890  }
891  list_free(exprvars);
892  if (lc2)
893  continue; /* we hit a non-available Var */
894 
895  /*
896  * If requested, reject expressions that are not parallel-safe. We
897  * check this last because it's a rather expensive test.
898  */
899  if (require_parallel_safe &&
900  !is_parallel_safe(root, (Node *) em->em_expr))
901  continue;
902 
903  return em; /* found usable expression */
904  }
905 
906  return NULL;
907 }
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:640
static bool is_exprlist_member(Expr *node, List *exprs)
Definition: equivclass.c:917
void list_free(List *list)
Definition: list.c:1505
#define PVC_INCLUDE_WINDOWFUNCS
Definition: optimizer.h:188
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:190
#define PVC_INCLUDE_AGGREGATES
Definition: optimizer.h:186
Definition: pg_list.h:51
List * pull_var_clause(Node *node, int flags)
Definition: var.c:597

References bms_is_subset(), EquivalenceClass::ec_members, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, EquivalenceMember::em_relids, is_exprlist_member(), is_parallel_safe(), lfirst, list_free(), pull_var_clause(), PVC_INCLUDE_AGGREGATES, PVC_INCLUDE_PLACEHOLDERS, and PVC_INCLUDE_WINDOWFUNCS.

Referenced by prepare_sort_from_pathkeys(), and relation_can_be_sorted_early().

◆ find_derived_clause_for_ec_member()

RestrictInfo* find_derived_clause_for_ec_member ( EquivalenceClass ec,
EquivalenceMember em 
)

Definition at line 2528 of file equivclass.c.

2530 {
2531  ListCell *lc;
2532 
2533  Assert(ec->ec_has_const);
2534  Assert(!em->em_is_const);
2535  foreach(lc, ec->ec_derives)
2536  {
2537  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2538 
2539  /*
2540  * generate_base_implied_equalities_const will have put non-const
2541  * members on the left side of derived clauses.
2542  */
2543  if (rinfo->left_em == em)
2544  return rinfo;
2545  }
2546  return NULL;
2547 }

References Assert(), EquivalenceClass::ec_derives, EquivalenceClass::ec_has_const, EquivalenceMember::em_is_const, RestrictInfo::left_em, and lfirst.

Referenced by get_foreign_key_join_selectivity().

◆ find_ec_member_matching_expr()

EquivalenceMember* find_ec_member_matching_expr ( EquivalenceClass ec,
Expr expr,
Relids  relids 
)

Definition at line 786 of file equivclass.c.

789 {
790  ListCell *lc;
791 
792  /* We ignore binary-compatible relabeling on both ends */
793  while (expr && IsA(expr, RelabelType))
794  expr = ((RelabelType *) expr)->arg;
795 
796  foreach(lc, ec->ec_members)
797  {
799  Expr *emexpr;
800 
801  /*
802  * We shouldn't be trying to sort by an equivalence class that
803  * contains a constant, so no need to consider such cases any further.
804  */
805  if (em->em_is_const)
806  continue;
807 
808  /*
809  * Ignore child members unless they belong to the requested rel.
810  */
811  if (em->em_is_child &&
812  !bms_is_subset(em->em_relids, relids))
813  continue;
814 
815  /*
816  * Match if same expression (after stripping relabel).
817  */
818  emexpr = em->em_expr;
819  while (emexpr && IsA(emexpr, RelabelType))
820  emexpr = ((RelabelType *) emexpr)->arg;
821 
822  if (equal(emexpr, expr))
823  return em;
824  }
825 
826  return NULL;
827 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:589
void * arg

References arg, bms_is_subset(), EquivalenceClass::ec_members, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, EquivalenceMember::em_relids, equal(), IsA, and lfirst.

Referenced by make_unique_from_pathkeys(), prepare_sort_from_pathkeys(), and relation_can_be_sorted_early().

◆ find_em_expr_for_rel()

Expr* find_em_expr_for_rel ( EquivalenceClass ec,
RelOptInfo rel 
)

Definition at line 939 of file equivclass.c.

940 {
941  ListCell *lc_em;
942 
943  foreach(lc_em, ec->ec_members)
944  {
945  EquivalenceMember *em = lfirst(lc_em);
946 
947  if (bms_is_subset(em->em_relids, rel->relids) &&
948  !bms_is_empty(em->em_relids))
949  {
950  /*
951  * If there is more than one equivalence member whose Vars are
952  * taken entirely from this relation, we'll be content to choose
953  * any one of those.
954  */
955  return em->em_expr;
956  }
957  }
958 
959  /* We didn't find any suitable equivalence class expression */
960  return NULL;
961 }

References bms_is_empty(), bms_is_subset(), EquivalenceClass::ec_members, EquivalenceMember::em_expr, EquivalenceMember::em_relids, lfirst, and RelOptInfo::relids.

Referenced by appendOrderByClause(), and get_useful_pathkeys_for_relation().

◆ generate_base_implied_equalities()

void generate_base_implied_equalities ( PlannerInfo root)

Definition at line 1088 of file equivclass.c.

1089 {
1090  int ec_index;
1091  ListCell *lc;
1092 
1093  /*
1094  * At this point, we're done absorbing knowledge of equivalences in the
1095  * query, so no further EC merging should happen, and ECs remaining in the
1096  * eq_classes list can be considered canonical. (But note that it's still
1097  * possible for new single-member ECs to be added through
1098  * get_eclass_for_sort_expr().)
1099  */
1100  root->ec_merging_done = true;
1101 
1102  ec_index = 0;
1103  foreach(lc, root->eq_classes)
1104  {
1106  bool can_generate_joinclause = false;
1107  int i;
1108 
1109  Assert(ec->ec_merged == NULL); /* else shouldn't be in list */
1110  Assert(!ec->ec_broken); /* not yet anyway... */
1111 
1112  /*
1113  * Generate implied equalities that are restriction clauses.
1114  * Single-member ECs won't generate any deductions, either here or at
1115  * the join level.
1116  */
1117  if (list_length(ec->ec_members) > 1)
1118  {
1119  if (ec->ec_has_const)
1121  else
1123 
1124  /* Recover if we failed to generate required derived clauses */
1125  if (ec->ec_broken)
1127 
1128  /* Detect whether this EC might generate join clauses */
1129  can_generate_joinclause =
1131  }
1132 
1133  /*
1134  * Mark the base rels cited in each eclass (which should all exist by
1135  * now) with the eq_classes indexes of all eclasses mentioning them.
1136  * This will let us avoid searching in subsequent lookups. While
1137  * we're at it, we can mark base rels that have pending eclass joins;
1138  * this is a cheap version of has_relevant_eclass_joinclause().
1139  */
1140  i = -1;
1141  while ((i = bms_next_member(ec->ec_relids, i)) > 0)
1142  {
1143  RelOptInfo *rel = root->simple_rel_array[i];
1144 
1145  Assert(rel->reloptkind == RELOPT_BASEREL);
1146 
1148  ec_index);
1149 
1150  if (can_generate_joinclause)
1151  rel->has_eclass_joins = true;
1152  }
1153 
1154  ec_index++;
1155  }
1156 }
static void generate_base_implied_equalities_broken(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:1364
static void generate_base_implied_equalities_no_const(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:1257
static void generate_base_implied_equalities_const(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:1162
struct EquivalenceClass * ec_merged
Definition: pathnodes.h:1001
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:186
bool has_eclass_joins
Definition: pathnodes.h:751

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

◆ generate_base_implied_equalities_broken()

static void generate_base_implied_equalities_broken ( PlannerInfo root,
EquivalenceClass ec 
)
static

Definition at line 1364 of file equivclass.c.

1366 {
1367  ListCell *lc;
1368 
1369  foreach(lc, ec->ec_sources)
1370  {
1371  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1372 
1373  if (ec->ec_has_const ||
1374  bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
1375  distribute_restrictinfo_to_rels(root, restrictinfo);
1376  }
1377 }
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2177
Relids required_relids
Definition: pathnodes.h:2079

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

◆ generate_base_implied_equalities_const()

static void generate_base_implied_equalities_const ( PlannerInfo root,
EquivalenceClass ec 
)
static

Definition at line 1162 of file equivclass.c.

1164 {
1165  EquivalenceMember *const_em = NULL;
1166  ListCell *lc;
1167 
1168  /*
1169  * In the trivial case where we just had one "var = const" clause, push
1170  * the original clause back into the main planner machinery. There is
1171  * nothing to be gained by doing it differently, and we save the effort to
1172  * re-build and re-analyze an equality clause that will be exactly
1173  * equivalent to the old one.
1174  */
1175  if (list_length(ec->ec_members) == 2 &&
1176  list_length(ec->ec_sources) == 1)
1177  {
1178  RestrictInfo *restrictinfo = (RestrictInfo *) linitial(ec->ec_sources);
1179 
1180  if (bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
1181  {
1182  distribute_restrictinfo_to_rels(root, restrictinfo);
1183  return;
1184  }
1185  }
1186 
1187  /*
1188  * Find the constant member to use. We prefer an actual constant to
1189  * pseudo-constants (such as Params), because the constraint exclusion
1190  * machinery might be able to exclude relations on the basis of generated
1191  * "var = const" equalities, but "var = param" won't work for that.
1192  */
1193  foreach(lc, ec->ec_members)
1194  {
1195  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1196 
1197  if (cur_em->em_is_const)
1198  {
1199  const_em = cur_em;
1200  if (IsA(cur_em->em_expr, Const))
1201  break;
1202  }
1203  }
1204  Assert(const_em != NULL);
1205 
1206  /* Generate a derived equality against each other member */
1207  foreach(lc, ec->ec_members)
1208  {
1209  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1210  Oid eq_op;
1211  RestrictInfo *rinfo;
1212 
1213  Assert(!cur_em->em_is_child); /* no children yet */
1214  if (cur_em == const_em)
1215  continue;
1216  eq_op = select_equality_operator(ec,
1217  cur_em->em_datatype,
1218  const_em->em_datatype);
1219  if (!OidIsValid(eq_op))
1220  {
1221  /* failed... */
1222  ec->ec_broken = true;
1223  break;
1224  }
1225  rinfo = process_implied_equality(root, eq_op, ec->ec_collation,
1226  cur_em->em_expr, const_em->em_expr,
1227  bms_copy(ec->ec_relids),
1228  bms_union(cur_em->em_nullable_relids,
1229  const_em->em_nullable_relids),
1230  ec->ec_min_security,
1231  ec->ec_below_outer_join,
1232  cur_em->em_is_const);
1233 
1234  /*
1235  * If the clause didn't degenerate to a constant, fill in the correct
1236  * markings for a mergejoinable clause, and save it in ec_derives. (We
1237  * will not re-use such clauses directly, but selectivity estimation
1238  * may consult the list later. Note that this use of ec_derives does
1239  * not overlap with its use for join clauses, since we never generate
1240  * join clauses from an ec_has_const eclass.)
1241  */
1242  if (rinfo && rinfo->mergeopfamilies)
1243  {
1244  /* it's not redundant, so don't set parent_ec */
1245  rinfo->left_ec = rinfo->right_ec = ec;
1246  rinfo->left_em = cur_em;
1247  rinfo->right_em = const_em;
1248  ec->ec_derives = lappend(ec->ec_derives, rinfo);
1249  }
1250  }
1251 }
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
#define OidIsValid(objectId)
Definition: c.h:710
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1795
RestrictInfo * 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:2272
#define linitial(l)
Definition: pg_list.h:174
bool ec_below_outer_join
Definition: pathnodes.h:996
List * mergeopfamilies
Definition: pathnodes.h:2106

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_derives, 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, lappend(), RestrictInfo::left_ec, RestrictInfo::left_em, lfirst, linitial, list_length(), RestrictInfo::mergeopfamilies, OidIsValid, process_implied_equality(), RestrictInfo::required_relids, RestrictInfo::right_ec, RestrictInfo::right_em, and select_equality_operator().

Referenced by generate_base_implied_equalities().

◆ generate_base_implied_equalities_no_const()

static void generate_base_implied_equalities_no_const ( PlannerInfo root,
EquivalenceClass ec 
)
static

Definition at line 1257 of file equivclass.c.

1259 {
1260  EquivalenceMember **prev_ems;
1261  ListCell *lc;
1262 
1263  /*
1264  * We scan the EC members once and track the last-seen member for each
1265  * base relation. When we see another member of the same base relation,
1266  * we generate "prev_em = cur_em". This results in the minimum number of
1267  * derived clauses, but it's possible that it will fail when a different
1268  * ordering would succeed. XXX FIXME: use a UNION-FIND algorithm similar
1269  * to the way we build merged ECs. (Use a list-of-lists for each rel.)
1270  */
1271  prev_ems = (EquivalenceMember **)
1272  palloc0(root->simple_rel_array_size * sizeof(EquivalenceMember *));
1273 
1274  foreach(lc, ec->ec_members)
1275  {
1276  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1277  int relid;
1278 
1279  Assert(!cur_em->em_is_child); /* no children yet */
1280  if (!bms_get_singleton_member(cur_em->em_relids, &relid))
1281  continue;
1282  Assert(relid < root->simple_rel_array_size);
1283 
1284  if (prev_ems[relid] != NULL)
1285  {
1286  EquivalenceMember *prev_em = prev_ems[relid];
1287  Oid eq_op;
1288  RestrictInfo *rinfo;
1289 
1290  eq_op = select_equality_operator(ec,
1291  prev_em->em_datatype,
1292  cur_em->em_datatype);
1293  if (!OidIsValid(eq_op))
1294  {
1295  /* failed... */
1296  ec->ec_broken = true;
1297  break;
1298  }
1299  rinfo = process_implied_equality(root, eq_op, ec->ec_collation,
1300  prev_em->em_expr, cur_em->em_expr,
1301  bms_copy(ec->ec_relids),
1302  bms_union(prev_em->em_nullable_relids,
1303  cur_em->em_nullable_relids),
1304  ec->ec_min_security,
1305  ec->ec_below_outer_join,
1306  false);
1307 
1308  /*
1309  * If the clause didn't degenerate to a constant, fill in the
1310  * correct markings for a mergejoinable clause. We don't put it
1311  * in ec_derives however; we don't currently need to re-find such
1312  * clauses, and we don't want to clutter that list with non-join
1313  * clauses.
1314  */
1315  if (rinfo && rinfo->mergeopfamilies)
1316  {
1317  /* it's not redundant, so don't set parent_ec */
1318  rinfo->left_ec = rinfo->right_ec = ec;
1319  rinfo->left_em = prev_em;
1320  rinfo->right_em = cur_em;
1321  }
1322  }
1323  prev_ems[relid] = cur_em;
1324  }
1325 
1326  pfree(prev_ems);
1327 
1328  /*
1329  * We also have to make sure that all the Vars used in the member clauses
1330  * will be available at any join node we might try to reference them at.
1331  * For the moment we force all the Vars to be available at all join nodes
1332  * for this eclass. Perhaps this could be improved by doing some
1333  * pre-analysis of which members we prefer to join, but it's no worse than
1334  * what happened in the pre-8.3 code.
1335  */
1336  foreach(lc, ec->ec_members)
1337  {
1338  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1339  List *vars = pull_var_clause((Node *) cur_em->em_expr,
1343 
1344  add_vars_to_targetlist(root, vars, ec->ec_relids, false);
1345  list_free(vars);
1346  }
1347 }
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:617
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:1169
void * palloc0(Size size)
Definition: mcxt.c:1093
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:187
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:189
int simple_rel_array_size
Definition: pathnodes.h:187
Definition: regcomp.c:238

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, RestrictInfo::left_ec, RestrictInfo::left_em, lfirst, list_free(), RestrictInfo::mergeopfamilies, OidIsValid, palloc0(), pfree(), process_implied_equality(), pull_var_clause(), PVC_INCLUDE_PLACEHOLDERS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_WINDOWFUNCS, RestrictInfo::right_ec, RestrictInfo::right_em, select_equality_operator(), and PlannerInfo::simple_rel_array_size.

Referenced by generate_base_implied_equalities().

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

2855 {
2856  List *result = NIL;
2857  bool is_child_rel = (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
2858  Relids parent_relids;
2859  int i;
2860 
2861  /* Should be OK to rely on eclass_indexes */
2862  Assert(root->ec_merging_done);
2863 
2864  /* Indexes are available only on base or "other" member relations. */
2865  Assert(IS_SIMPLE_REL(rel));
2866 
2867  /* If it's a child rel, we'll need to know what its parent(s) are */
2868  if (is_child_rel)
2869  parent_relids = find_childrel_parents(root, rel);
2870  else
2871  parent_relids = NULL; /* not used, but keep compiler quiet */
2872 
2873  i = -1;
2874  while ((i = bms_next_member(rel->eclass_indexes, i)) >= 0)
2875  {
2876  EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2877  EquivalenceMember *cur_em;
2878  ListCell *lc2;
2879 
2880  /* Sanity check eclass_indexes only contain ECs for rel */
2881  Assert(is_child_rel || bms_is_subset(rel->relids, cur_ec->ec_relids));
2882 
2883  /*
2884  * Won't generate joinclauses if const or single-member (the latter
2885  * test covers the volatile case too)
2886  */
2887  if (cur_ec->ec_has_const || list_length(cur_ec->ec_members) <= 1)
2888  continue;
2889 
2890  /*
2891  * Scan members, looking for a match to the target column. Note that
2892  * child EC members are considered, but only when they belong to the
2893  * target relation. (Unlike regular members, the same expression
2894  * could be a child member of more than one EC. Therefore, it's
2895  * potentially order-dependent which EC a child relation's target
2896  * column gets matched to. This is annoying but it only happens in
2897  * corner cases, so for now we live with just reporting the first
2898  * match. See also get_eclass_for_sort_expr.)
2899  */
2900  cur_em = NULL;
2901  foreach(lc2, cur_ec->ec_members)
2902  {
2903  cur_em = (EquivalenceMember *) lfirst(lc2);
2904  if (bms_equal(cur_em->em_relids, rel->relids) &&
2905  callback(root, rel, cur_ec, cur_em, callback_arg))
2906  break;
2907  cur_em = NULL;
2908  }
2909 
2910  if (!cur_em)
2911  continue;
2912 
2913  /*
2914  * Found our match. Scan the other EC members and attempt to generate
2915  * joinclauses.
2916  */
2917  foreach(lc2, cur_ec->ec_members)
2918  {
2919  EquivalenceMember *other_em = (EquivalenceMember *) lfirst(lc2);
2920  Oid eq_op;
2921  RestrictInfo *rinfo;
2922 
2923  if (other_em->em_is_child)
2924  continue; /* ignore children here */
2925 
2926  /* Make sure it'll be a join to a different rel */
2927  if (other_em == cur_em ||
2928  bms_overlap(other_em->em_relids, rel->relids))
2929  continue;
2930 
2931  /* Forget it if caller doesn't want joins to this rel */
2932  if (bms_overlap(other_em->em_relids, prohibited_rels))
2933  continue;
2934 
2935  /*
2936  * Also, if this is a child rel, avoid generating a useless join
2937  * to its parent rel(s).
2938  */
2939  if (is_child_rel &&
2940  bms_overlap(parent_relids, other_em->em_relids))
2941  continue;
2942 
2943  eq_op = select_equality_operator(cur_ec,
2944  cur_em->em_datatype,
2945  other_em->em_datatype);
2946  if (!OidIsValid(eq_op))
2947  continue;
2948 
2949  /* set parent_ec to mark as redundant with other joinclauses */
2950  rinfo = create_join_clause(root, cur_ec, eq_op,
2951  cur_em, other_em,
2952  cur_ec);
2953 
2954  result = lappend(result, rinfo);
2955  }
2956 
2957  /*
2958  * If somehow we failed to create any join clauses, we might as well
2959  * keep scanning the ECs for another match. But if we did make any,
2960  * we're done, because we don't want to return non-redundant clauses.
2961  */
2962  if (result)
2963  break;
2964  }
2965 
2966  return result;
2967 }
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94
static RestrictInfo * create_join_clause(PlannerInfo *root, EquivalenceClass *ec, Oid opno, EquivalenceMember *leftem, EquivalenceMember *rightem, EquivalenceClass *parent_ec)
Definition: equivclass.c:1830
@ RELOPT_OTHER_MEMBER_REL
Definition: pathnodes.h:643
#define NIL
Definition: pg_list.h:65
Relids find_childrel_parents(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:1261
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48

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

◆ generate_join_implied_equalities()

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

Definition at line 1421 of file equivclass.c.

1425 {
1426  List *result = NIL;
1427  Relids inner_relids = inner_rel->relids;
1428  Relids nominal_inner_relids;
1429  Relids nominal_join_relids;
1430  Bitmapset *matching_ecs;
1431  int i;
1432 
1433  /* If inner rel is a child, extra setup work is needed */
1434  if (IS_OTHER_REL(inner_rel))
1435  {
1436  Assert(!bms_is_empty(inner_rel->top_parent_relids));
1437 
1438  /* Fetch relid set for the topmost parent rel */
1439  nominal_inner_relids = inner_rel->top_parent_relids;
1440  /* ECs will be marked with the parent's relid, not the child's */
1441  nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1442  }
1443  else
1444  {
1445  nominal_inner_relids = inner_relids;
1446  nominal_join_relids = join_relids;
1447  }
1448 
1449  /*
1450  * Get all eclasses that mention both inner and outer sides of the join
1451  */
1452  matching_ecs = get_common_eclass_indexes(root, nominal_inner_relids,
1453  outer_relids);
1454 
1455  i = -1;
1456  while ((i = bms_next_member(matching_ecs, i)) >= 0)
1457  {
1459  List *sublist = NIL;
1460 
1461  /* ECs containing consts do not need any further enforcement */
1462  if (ec->ec_has_const)
1463  continue;
1464 
1465  /* Single-member ECs won't generate any deductions */
1466  if (list_length(ec->ec_members) <= 1)
1467  continue;
1468 
1469  /* Sanity check that this eclass overlaps the join */
1470  Assert(bms_overlap(ec->ec_relids, nominal_join_relids));
1471 
1472  if (!ec->ec_broken)
1474  ec,
1475  join_relids,
1476  outer_relids,
1477  inner_relids);
1478 
1479  /* Recover if we failed to generate required derived clauses */
1480  if (ec->ec_broken)
1482  ec,
1483  nominal_join_relids,
1484  outer_relids,
1485  nominal_inner_relids,
1486  inner_rel);
1487 
1488  result = list_concat(result, sublist);
1489  }
1490 
1491  return result;
1492 }
static List * generate_join_implied_equalities_normal(PlannerInfo *root, EquivalenceClass *ec, Relids join_relids, Relids outer_relids, Relids inner_relids)
Definition: equivclass.c:1570
static Bitmapset * get_common_eclass_indexes(PlannerInfo *root, Relids relids1, Relids relids2)
Definition: equivclass.c:3236
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:1746
List * list_concat(List *list1, const List *list2)
Definition: list.c:540

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

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

1752 {
1753  List *result = NIL;
1754  ListCell *lc;
1755 
1756  foreach(lc, ec->ec_sources)
1757  {
1758  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1759  Relids clause_relids = restrictinfo->required_relids;
1760 
1761  if (bms_is_subset(clause_relids, nominal_join_relids) &&
1762  !bms_is_subset(clause_relids, outer_relids) &&
1763  !bms_is_subset(clause_relids, nominal_inner_relids))
1764  result = lappend(result, restrictinfo);
1765  }
1766 
1767  /*
1768  * If we have to translate, just brute-force apply adjust_appendrel_attrs
1769  * to all the RestrictInfos at once. This will result in returning
1770  * RestrictInfos that are not listed in ec_derives, but there shouldn't be
1771  * any duplication, and it's a sufficiently narrow corner case that we
1772  * shouldn't sweat too much over it anyway.
1773  *
1774  * Since inner_rel might be an indirect descendant of the baserel
1775  * mentioned in the ec_sources clauses, we have to be prepared to apply
1776  * multiple levels of Var translation.
1777  */
1778  if (IS_OTHER_REL(inner_rel) && result != NIL)
1779  result = (List *) adjust_appendrel_attrs_multilevel(root,
1780  (Node *) result,
1781  inner_rel->relids,
1782  inner_rel->top_parent_relids);
1783 
1784  return result;
1785 }

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

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

1504 {
1505  List *result = NIL;
1506  Relids inner_relids = inner_rel->relids;
1507  Relids nominal_inner_relids;
1508  Relids nominal_join_relids;
1509  ListCell *lc;
1510 
1511  /* If inner rel is a child, extra setup work is needed */
1512  if (IS_OTHER_REL(inner_rel))
1513  {
1514  Assert(!bms_is_empty(inner_rel->top_parent_relids));
1515 
1516  /* Fetch relid set for the topmost parent rel */
1517  nominal_inner_relids = inner_rel->top_parent_relids;
1518  /* ECs will be marked with the parent's relid, not the child's */
1519  nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1520  }
1521  else
1522  {
1523  nominal_inner_relids = inner_relids;
1524  nominal_join_relids = join_relids;
1525  }
1526 
1527  foreach(lc, eclasses)
1528  {
1530  List *sublist = NIL;
1531 
1532  /* ECs containing consts do not need any further enforcement */
1533  if (ec->ec_has_const)
1534  continue;
1535 
1536  /* Single-member ECs won't generate any deductions */
1537  if (list_length(ec->ec_members) <= 1)
1538  continue;
1539 
1540  /* We can quickly ignore any that don't overlap the join, too */
1541  if (!bms_overlap(ec->ec_relids, nominal_join_relids))
1542  continue;
1543 
1544  if (!ec->ec_broken)
1546  ec,
1547  join_relids,
1548  outer_relids,
1549  inner_relids);
1550 
1551  /* Recover if we failed to generate required derived clauses */
1552  if (ec->ec_broken)
1554  ec,
1555  nominal_join_relids,
1556  outer_relids,
1557  nominal_inner_relids,
1558  inner_rel);
1559 
1560  result = list_concat(result, sublist);
1561  }
1562 
1563  return result;
1564 }

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

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

1575 {
1576  List *result = NIL;
1577  List *new_members = NIL;
1578  List *outer_members = NIL;
1579  List *inner_members = NIL;
1580  ListCell *lc1;
1581 
1582  /*
1583  * First, scan the EC to identify member values that are computable at the
1584  * outer rel, at the inner rel, or at this relation but not in either
1585  * input rel. The outer-rel members should already be enforced equal,
1586  * likewise for the inner-rel members. We'll need to create clauses to
1587  * enforce that any newly computable members are all equal to each other
1588  * as well as to at least one input member, plus enforce at least one
1589  * outer-rel member equal to at least one inner-rel member.
1590  */
1591  foreach(lc1, ec->ec_members)
1592  {
1593  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
1594 
1595  /*
1596  * We don't need to check explicitly for child EC members. This test
1597  * against join_relids will cause them to be ignored except when
1598  * considering a child inner rel, which is what we want.
1599  */
1600  if (!bms_is_subset(cur_em->em_relids, join_relids))
1601  continue; /* not computable yet, or wrong child */
1602 
1603  if (bms_is_subset(cur_em->em_relids, outer_relids))
1604  outer_members = lappend(outer_members, cur_em);
1605  else if (bms_is_subset(cur_em->em_relids, inner_relids))
1606  inner_members = lappend(inner_members, cur_em);
1607  else
1608  new_members = lappend(new_members, cur_em);
1609  }
1610 
1611  /*
1612  * First, select the joinclause if needed. We can equate any one outer
1613  * member to any one inner member, but we have to find a datatype
1614  * combination for which an opfamily member operator exists. If we have
1615  * choices, we prefer simple Var members (possibly with RelabelType) since
1616  * these are (a) cheapest to compute at runtime and (b) most likely to
1617  * have useful statistics. Also, prefer operators that are also
1618  * hashjoinable.
1619  */
1620  if (outer_members && inner_members)
1621  {
1622  EquivalenceMember *best_outer_em = NULL;
1623  EquivalenceMember *best_inner_em = NULL;
1624  Oid best_eq_op = InvalidOid;
1625  int best_score = -1;
1626  RestrictInfo *rinfo;
1627 
1628  foreach(lc1, outer_members)
1629  {
1630  EquivalenceMember *outer_em = (EquivalenceMember *) lfirst(lc1);
1631  ListCell *lc2;
1632 
1633  foreach(lc2, inner_members)
1634  {
1635  EquivalenceMember *inner_em = (EquivalenceMember *) lfirst(lc2);
1636  Oid eq_op;
1637  int score;
1638 
1639  eq_op = select_equality_operator(ec,
1640  outer_em->em_datatype,
1641  inner_em->em_datatype);
1642  if (!OidIsValid(eq_op))
1643  continue;
1644  score = 0;
1645  if (IsA(outer_em->em_expr, Var) ||
1646  (IsA(outer_em->em_expr, RelabelType) &&
1647  IsA(((RelabelType *) outer_em->em_expr)->arg, Var)))
1648  score++;
1649  if (IsA(inner_em->em_expr, Var) ||
1650  (IsA(inner_em->em_expr, RelabelType) &&
1651  IsA(((RelabelType *) inner_em->em_expr)->arg, Var)))
1652  score++;
1653  if (op_hashjoinable(eq_op,
1654  exprType((Node *) outer_em->em_expr)))
1655  score++;
1656  if (score > best_score)
1657  {
1658  best_outer_em = outer_em;
1659  best_inner_em = inner_em;
1660  best_eq_op = eq_op;
1661  best_score = score;
1662  if (best_score == 3)
1663  break; /* no need to look further */
1664  }
1665  }
1666  if (best_score == 3)
1667  break; /* no need to look further */
1668  }
1669  if (best_score < 0)
1670  {
1671  /* failed... */
1672  ec->ec_broken = true;
1673  return NIL;
1674  }
1675 
1676  /*
1677  * Create clause, setting parent_ec to mark it as redundant with other
1678  * joinclauses
1679  */
1680  rinfo = create_join_clause(root, ec, best_eq_op,
1681  best_outer_em, best_inner_em,
1682  ec);
1683 
1684  result = lappend(result, rinfo);
1685  }
1686 
1687  /*
1688  * Now deal with building restrictions for any expressions that involve
1689  * Vars from both sides of the join. We have to equate all of these to
1690  * each other as well as to at least one old member (if any).
1691  *
1692  * XXX as in generate_base_implied_equalities_no_const, we could be a lot
1693  * smarter here to avoid unnecessary failures in cross-type situations.
1694  * For now, use the same left-to-right method used there.
1695  */
1696  if (new_members)
1697  {
1698  List *old_members = list_concat(outer_members, inner_members);
1699  EquivalenceMember *prev_em = NULL;
1700  RestrictInfo *rinfo;
1701 
1702  /* For now, arbitrarily take the first old_member as the one to use */
1703  if (old_members)
1704  new_members = lappend(new_members, linitial(old_members));
1705 
1706  foreach(lc1, new_members)
1707  {
1708  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
1709 
1710  if (prev_em != NULL)
1711  {
1712  Oid eq_op;
1713 
1714  eq_op = select_equality_operator(ec,
1715  prev_em->em_datatype,
1716  cur_em->em_datatype);
1717  if (!OidIsValid(eq_op))
1718  {
1719  /* failed... */
1720  ec->ec_broken = true;
1721  return NIL;
1722  }
1723  /* do NOT set parent_ec, this qual is not redundant! */
1724  rinfo = create_join_clause(root, ec, eq_op,
1725  prev_em, cur_em,
1726  NULL);
1727 
1728  result = lappend(result, rinfo);
1729  }
1730  prev_em = cur_em;
1731  }
1732  }
1733 
1734  return result;
1735 }
bool op_hashjoinable(Oid opno, Oid inputtype)
Definition: lsyscache.c:1408
#define InvalidOid
Definition: postgres_ext.h:36
Definition: primnodes.h:187

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

◆ get_common_eclass_indexes()

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

Definition at line 3236 of file equivclass.c.

3237 {
3238  Bitmapset *rel1ecs;
3239  Bitmapset *rel2ecs;
3240  int relid;
3241 
3242  rel1ecs = get_eclass_indexes_for_relids(root, relids1);
3243 
3244  /*
3245  * We can get away with just using the relation's eclass_indexes directly
3246  * when relids2 is a singleton set.
3247  */
3248  if (bms_get_singleton_member(relids2, &relid))
3249  rel2ecs = root->simple_rel_array[relid]->eclass_indexes;
3250  else
3251  rel2ecs = get_eclass_indexes_for_relids(root, relids2);
3252 
3253  /* Calculate and return the common EC indexes, recycling the left input. */
3254  return bms_int_members(rel1ecs, rel2ecs);
3255 }
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:904

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

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

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

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

◆ get_eclass_indexes_for_relids()

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

Definition at line 3212 of file equivclass.c.

3213 {
3214  Bitmapset *ec_indexes = NULL;
3215  int i = -1;
3216 
3217  /* Should be OK to rely on eclass_indexes */
3218  Assert(root->ec_merging_done);
3219 
3220  while ((i = bms_next_member(relids, i)) > 0)
3221  {
3222  RelOptInfo *rel = root->simple_rel_array[i];
3223 
3224  ec_indexes = bms_add_members(ec_indexes, rel->eclass_indexes);
3225  }
3226  return ec_indexes;
3227 }

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

Referenced by add_child_join_rel_equivalences(), get_common_eclass_indexes(), and has_relevant_eclass_joinclause().

◆ has_relevant_eclass_joinclause()

bool has_relevant_eclass_joinclause ( PlannerInfo root,
RelOptInfo rel1 
)

Definition at line 3047 of file equivclass.c.

3048 {
3049  Bitmapset *matched_ecs;
3050  int i;
3051 
3052  /* Examine only eclasses mentioning rel1 */
3053  matched_ecs = get_eclass_indexes_for_relids(root, rel1->relids);
3054 
3055  i = -1;
3056  while ((i = bms_next_member(matched_ecs, i)) >= 0)
3057  {
3059  i);
3060 
3061  /*
3062  * Won't generate joinclauses if single-member (this test covers the
3063  * volatile case too)
3064  */
3065  if (list_length(ec->ec_members) <= 1)
3066  continue;
3067 
3068  /*
3069  * Per the comment in have_relevant_eclass_joinclause, it's sufficient
3070  * to find an EC that mentions both this rel and some other rel.
3071  */
3072  if (!bms_is_subset(ec->ec_relids, rel1->relids))
3073  return true;
3074  }
3075 
3076  return false;
3077 }

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

◆ have_relevant_eclass_joinclause()

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

Definition at line 2980 of file equivclass.c.

2982 {
2983  Bitmapset *matching_ecs;
2984  int i;
2985 
2986  /* Examine only eclasses mentioning both rel1 and rel2 */
2987  matching_ecs = get_common_eclass_indexes(root, rel1->relids,
2988  rel2->relids);
2989 
2990  i = -1;
2991  while ((i = bms_next_member(matching_ecs, i)) >= 0)
2992  {
2994  i);
2995 
2996  /*
2997  * Sanity check that get_common_eclass_indexes gave only ECs
2998  * containing both rels.
2999  */
3000  Assert(bms_overlap(rel1->relids, ec->ec_relids));
3001  Assert(bms_overlap(rel2->relids, ec->ec_relids));
3002 
3003  /*
3004  * Won't generate joinclauses if single-member (this test covers the
3005  * volatile case too)
3006  */
3007  if (list_length(ec->ec_members) <= 1)
3008  continue;
3009 
3010  /*
3011  * We do not need to examine the individual members of the EC, because
3012  * all that we care about is whether each rel overlaps the relids of
3013  * at least one member, and get_common_eclass_indexes() and the single
3014  * member check above are sufficient to prove that. (As with
3015  * have_relevant_joinclause(), it is not necessary that the EC be able
3016  * to form a joinclause relating exactly the two given rels, only that
3017  * it be able to form a joinclause mentioning both, and this will
3018  * surely be true if both of them overlap ec_relids.)
3019  *
3020  * Note we don't test ec_broken; if we did, we'd need a separate code
3021  * path to look through ec_sources. Checking the membership anyway is
3022  * OK as a possibly-overoptimistic heuristic.
3023  *
3024  * We don't test ec_has_const either, even though a const eclass won't
3025  * generate real join clauses. This is because if we had "WHERE a.x =
3026  * b.y and a.x = 42", it is worth considering a join between a and b,
3027  * since the join result is likely to be small even though it'll end
3028  * up being an unqualified nestloop.
3029  */
3030 
3031  return true;
3032  }
3033 
3034  return false;
3035 }

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

◆ is_exprlist_member()

static bool is_exprlist_member ( Expr node,
List exprs 
)
static

Definition at line 917 of file equivclass.c.

918 {
919  ListCell *lc;
920 
921  foreach(lc, exprs)
922  {
923  Expr *expr = (Expr *) lfirst(lc);
924 
925  if (expr && IsA(expr, TargetEntry))
926  expr = ((TargetEntry *) expr)->expr;
927 
928  if (equal(node, expr))
929  return true;
930  }
931  return false;
932 }

References equal(), IsA, and lfirst.

Referenced by find_computable_ec_member().

◆ is_redundant_derived_clause()

bool is_redundant_derived_clause ( RestrictInfo rinfo,
List clauselist 
)

Definition at line 3149 of file equivclass.c.

3150 {
3151  EquivalenceClass *parent_ec = rinfo->parent_ec;
3152  ListCell *lc;
3153 
3154  /* Fail if it's not a potentially-redundant clause from some EC */
3155  if (parent_ec == NULL)
3156  return false;
3157 
3158  foreach(lc, clauselist)
3159  {
3160  RestrictInfo *otherrinfo = (RestrictInfo *) lfirst(lc);
3161 
3162  if (otherrinfo->parent_ec == parent_ec)
3163  return true;
3164  }
3165 
3166  return false;
3167 }

References lfirst, and RestrictInfo::parent_ec.

Referenced by create_tidscan_plan().

◆ is_redundant_with_indexclauses()

bool is_redundant_with_indexclauses ( RestrictInfo rinfo,
List indexclauses 
)

Definition at line 3176 of file equivclass.c.

3177 {
3178  EquivalenceClass *parent_ec = rinfo->parent_ec;
3179  ListCell *lc;
3180 
3181  foreach(lc, indexclauses)
3182  {
3183  IndexClause *iclause = lfirst_node(IndexClause, lc);
3184  RestrictInfo *otherrinfo = iclause->rinfo;
3185 
3186  /* If indexclause is lossy, it won't enforce the condition exactly */
3187  if (iclause->lossy)
3188  continue;
3189 
3190  /* Match if it's same clause (pointer equality should be enough) */
3191  if (rinfo == otherrinfo)
3192  return true;
3193  /* Match if derived from same EC */
3194  if (parent_ec && otherrinfo->parent_ec == parent_ec)
3195  return true;
3196 
3197  /*
3198  * No need to look at the derived clauses in iclause->indexquals; they
3199  * couldn't match if the parent clause didn't.
3200  */
3201  }
3202 
3203  return false;
3204 }
#define lfirst_node(type, lc)
Definition: pg_list.h:172
struct RestrictInfo * rinfo
Definition: pathnodes.h:1291

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

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

◆ match_eclasses_to_foreign_key_col()

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

Definition at line 2437 of file equivclass.c.

2440 {
2441  Index var1varno = fkinfo->con_relid;
2442  AttrNumber var1attno = fkinfo->conkey[colno];
2443  Index var2varno = fkinfo->ref_relid;
2444  AttrNumber var2attno = fkinfo->confkey[colno];
2445  Oid eqop = fkinfo->conpfeqop[colno];
2446  RelOptInfo *rel1 = root->simple_rel_array[var1varno];
2447  RelOptInfo *rel2 = root->simple_rel_array[var2varno];
2448  List *opfamilies = NIL; /* compute only if needed */
2449  Bitmapset *matching_ecs;
2450  int i;
2451 
2452  /* Consider only eclasses mentioning both relations */
2453  Assert(root->ec_merging_done);
2454  Assert(IS_SIMPLE_REL(rel1));
2455  Assert(IS_SIMPLE_REL(rel2));
2456  matching_ecs = bms_intersect(rel1->eclass_indexes,
2457  rel2->eclass_indexes);
2458 
2459  i = -1;
2460  while ((i = bms_next_member(matching_ecs, i)) >= 0)
2461  {
2463  i);
2464  EquivalenceMember *item1_em = NULL;
2465  EquivalenceMember *item2_em = NULL;
2466  ListCell *lc2;
2467 
2468  /* Never match to a volatile EC */
2469  if (ec->ec_has_volatile)
2470  continue;
2471  /* Note: it seems okay to match to "broken" eclasses here */
2472 
2473  foreach(lc2, ec->ec_members)
2474  {
2476  Var *var;
2477 
2478  if (em->em_is_child)
2479  continue; /* ignore children here */
2480 
2481  /* EM must be a Var, possibly with RelabelType */
2482  var = (Var *) em->em_expr;
2483  while (var && IsA(var, RelabelType))
2484  var = (Var *) ((RelabelType *) var)->arg;
2485  if (!(var && IsA(var, Var)))
2486  continue;
2487 
2488  /* Match? */
2489  if (var->varno == var1varno && var->varattno == var1attno)
2490  item1_em = em;
2491  else if (var->varno == var2varno && var->varattno == var2attno)
2492  item2_em = em;
2493 
2494  /* Have we found both PK and FK column in this EC? */
2495  if (item1_em && item2_em)
2496  {
2497  /*
2498  * Succeed if eqop matches EC's opfamilies. We could test
2499  * this before scanning the members, but it's probably cheaper
2500  * to test for member matches first.
2501  */
2502  if (opfamilies == NIL) /* compute if we didn't already */
2503  opfamilies = get_mergejoin_opfamilies(eqop);
2504  if (equal(opfamilies, ec->ec_opfamilies))
2505  {
2506  fkinfo->eclass[colno] = ec;
2507  fkinfo->fk_eclass_member[colno] = item2_em;
2508  return ec;
2509  }
2510  /* Otherwise, done with this EC, move on to the next */
2511  break;
2512  }
2513  }
2514  }
2515  return NULL;
2516 }
int16 AttrNumber
Definition: attnum.h:21
unsigned int Index
Definition: c.h:549
List * get_mergejoin_opfamilies(Oid opno)
Definition: lsyscache.c:364
while(p+4<=pend)
Oid conpfeqop[INDEX_MAX_KEYS]
Definition: pathnodes.h:910
AttrNumber confkey[INDEX_MAX_KEYS]
Definition: pathnodes.h:909
struct EquivalenceClass * eclass[INDEX_MAX_KEYS]
Definition: pathnodes.h:918
AttrNumber conkey[INDEX_MAX_KEYS]
Definition: pathnodes.h:908
struct EquivalenceMember * fk_eclass_member[INDEX_MAX_KEYS]
Definition: pathnodes.h:920
AttrNumber varattno
Definition: primnodes.h:191
int varno
Definition: primnodes.h:189

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, ForeignKeyOptInfo::eclass, RelOptInfo::eclass_indexes, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, PlannerInfo::eq_classes, equal(), ForeignKeyOptInfo::fk_eclass_member, get_mergejoin_opfamilies(), i, IS_SIMPLE_REL, IsA, lfirst, list_nth(), NIL, ForeignKeyOptInfo::ref_relid, PlannerInfo::simple_rel_array, Var::varattno, Var::varno, and while().

Referenced by match_foreign_keys_to_quals().

◆ process_equivalence()

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

Definition at line 119 of file equivclass.c.

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

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(), foreach_current_index, 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_nth_cell(), 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().

◆ reconsider_full_join_clause()

static bool reconsider_full_join_clause ( PlannerInfo root,
RestrictInfo rinfo 
)
static

Definition at line 2205 of file equivclass.c.

2206 {
2207  Expr *leftvar;
2208  Expr *rightvar;
2209  Oid opno,
2210  collation,
2211  left_type,
2212  right_type;
2213  Relids left_relids,
2214  right_relids,
2215  left_nullable_relids,
2216  right_nullable_relids;
2217  ListCell *lc1;
2218 
2219  /* Can't use an outerjoin_delayed clause here */
2220  if (rinfo->outerjoin_delayed)
2221  return false;
2222 
2223  /* Extract needed info from the clause */
2224  Assert(is_opclause(rinfo->clause));
2225  opno = ((OpExpr *) rinfo->clause)->opno;
2226  collation = ((OpExpr *) rinfo->clause)->inputcollid;
2227  op_input_types(opno, &left_type, &right_type);
2228  leftvar = (Expr *) get_leftop(rinfo->clause);
2229  rightvar = (Expr *) get_rightop(rinfo->clause);
2230  left_relids = rinfo->left_relids;
2231  right_relids = rinfo->right_relids;
2232  left_nullable_relids = bms_intersect(left_relids,
2233  rinfo->nullable_relids);
2234  right_nullable_relids = bms_intersect(right_relids,
2235  rinfo->nullable_relids);
2236 
2237  foreach(lc1, root->eq_classes)
2238  {
2239  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
2240  EquivalenceMember *coal_em = NULL;
2241  bool match;
2242  bool matchleft;
2243  bool matchright;
2244  ListCell *lc2;
2245  int coal_idx = -1;
2246 
2247  /* Ignore EC unless it contains pseudoconstants */
2248  if (!cur_ec->ec_has_const)
2249  continue;
2250  /* Never match to a volatile EC */
2251  if (cur_ec->ec_has_volatile)
2252  continue;
2253  /* It has to match the outer-join clause as to semantics, too */
2254  if (collation != cur_ec->ec_collation)
2255  continue;
2256  if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
2257  continue;
2258 
2259  /*
2260  * Does it contain a COALESCE(leftvar, rightvar) construct?
2261  *
2262  * We can assume the COALESCE() inputs are in the same order as the
2263  * join clause, since both were automatically generated in the cases
2264  * we care about.
2265  *
2266  * XXX currently this may fail to match in cross-type cases because
2267  * the COALESCE will contain typecast operations while the join clause
2268  * may not (if there is a cross-type mergejoin operator available for
2269  * the two column types). Is it OK to strip implicit coercions from
2270  * the COALESCE arguments?
2271  */
2272  match = false;
2273  foreach(lc2, cur_ec->ec_members)
2274  {
2275  coal_em = (EquivalenceMember *) lfirst(lc2);
2276  Assert(!coal_em->em_is_child); /* no children yet */
2277  if (IsA(coal_em->em_expr, CoalesceExpr))
2278  {
2279  CoalesceExpr *cexpr = (CoalesceExpr *) coal_em->em_expr;
2280  Node *cfirst;
2281  Node *csecond;
2282 
2283  if (list_length(cexpr->args) != 2)
2284  continue;
2285  cfirst = (Node *) linitial(cexpr->args);
2286  csecond = (Node *) lsecond(cexpr->args);
2287 
2288  if (equal(leftvar, cfirst) && equal(rightvar, csecond))
2289  {
2290  coal_idx = foreach_current_index(lc2);
2291  match = true;
2292  break;
2293  }
2294  }
2295  }
2296  if (!match)
2297  continue; /* no match, so ignore this EC */
2298 
2299  /*
2300  * Yes it does! Try to generate clauses LEFTVAR = CONSTANT and
2301  * RIGHTVAR = CONSTANT for each CONSTANT in the EC. Note that we must
2302  * succeed with at least one constant for each var before we can
2303  * decide to throw away the outer-join clause.
2304  */
2305  matchleft = matchright = false;
2306  foreach(lc2, cur_ec->ec_members)
2307  {
2308  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2309  Oid eq_op;
2310  RestrictInfo *newrinfo;
2311 
2312  if (!cur_em->em_is_const)
2313  continue; /* ignore non-const members */
2314  eq_op = select_equality_operator(cur_ec,
2315  left_type,
2316  cur_em->em_datatype);
2317  if (OidIsValid(eq_op))
2318  {
2319  newrinfo = build_implied_join_equality(root,
2320  eq_op,
2321  cur_ec->ec_collation,
2322  leftvar,
2323  cur_em->em_expr,
2324  bms_copy(left_relids),
2325  bms_copy(left_nullable_relids),
2326  cur_ec->ec_min_security);
2327  if (process_equivalence(root, &newrinfo, true))
2328  matchleft = true;
2329  }
2330  eq_op = select_equality_operator(cur_ec,
2331  right_type,
2332  cur_em->em_datatype);
2333  if (OidIsValid(eq_op))
2334  {
2335  newrinfo = build_implied_join_equality(root,
2336  eq_op,
2337  cur_ec->ec_collation,
2338  rightvar,
2339  cur_em->em_expr,
2340  bms_copy(right_relids),
2341  bms_copy(right_nullable_relids),
2342  cur_ec->ec_min_security);
2343  if (process_equivalence(root, &newrinfo, true))
2344  matchright = true;
2345  }
2346  }
2347 
2348  /*
2349  * If we were able to equate both vars to constants, we're done, and
2350  * we can throw away the full-join clause as redundant. Moreover, we
2351  * can remove the COALESCE entry from the EC, since the added
2352  * restrictions ensure it will always have the expected value. (We
2353  * don't bother trying to update ec_relids or ec_sources.)
2354  */
2355  if (matchleft && matchright)
2356  {
2357  cur_ec->ec_members = list_delete_nth_cell(cur_ec->ec_members, coal_idx);
2358  return true;
2359  }
2360 
2361  /*
2362  * Otherwise, fall out of the search loop, since we know the COALESCE
2363  * appears in at most one EC (XXX might stop being true if we allow
2364  * stripping of coercions above?)
2365  */
2366  break;
2367  }
2368 
2369  return false; /* failed to make any deduction */
2370 }
bool process_equivalence(PlannerInfo *root, RestrictInfo **p_restrictinfo, bool below_outer_join)
Definition: equivclass.c:119
#define lsecond(l)
Definition: pg_list.h:179
List * args
Definition: primnodes.h:1132

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(), foreach_current_index, get_leftop(), get_rightop(), if(), is_opclause(), IsA, RestrictInfo::left_relids, lfirst, linitial, list_delete_nth_cell(), 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().

◆ reconsider_outer_join_clause()

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

Definition at line 2079 of file equivclass.c.

2081 {
2082  Expr *outervar,
2083  *innervar;
2084  Oid opno,
2085  collation,
2086  left_type,
2087  right_type,
2088  inner_datatype;
2089  Relids inner_relids,
2090  inner_nullable_relids;
2091  ListCell *lc1;
2092 
2093  Assert(is_opclause(rinfo->clause));
2094  opno = ((OpExpr *) rinfo->clause)->opno;
2095  collation = ((OpExpr *) rinfo->clause)->inputcollid;
2096 
2097  /* If clause is outerjoin_delayed, operator must be strict */
2098  if (rinfo->outerjoin_delayed && !op_strict(opno))
2099  return false;
2100 
2101  /* Extract needed info from the clause */
2102  op_input_types(opno, &left_type, &right_type);
2103  if (outer_on_left)
2104  {
2105  outervar = (Expr *) get_leftop(rinfo->clause);
2106  innervar = (Expr *) get_rightop(rinfo->clause);
2107  inner_datatype = right_type;
2108  inner_relids = rinfo->right_relids;
2109  }
2110  else
2111  {
2112  outervar = (Expr *) get_rightop(rinfo->clause);
2113  innervar = (Expr *) get_leftop(rinfo->clause);
2114  inner_datatype = left_type;
2115  inner_relids = rinfo->left_relids;
2116  }
2117  inner_nullable_relids = bms_intersect(inner_relids,
2118  rinfo->nullable_relids);
2119 
2120  /* Scan EquivalenceClasses for a match to outervar */
2121  foreach(lc1, root->eq_classes)
2122  {
2123  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
2124  bool match;
2125  ListCell *lc2;
2126 
2127  /* Ignore EC unless it contains pseudoconstants */
2128  if (!cur_ec->ec_has_const)
2129  continue;
2130  /* Never match to a volatile EC */
2131  if (cur_ec->ec_has_volatile)
2132  continue;
2133  /* It has to match the outer-join clause as to semantics, too */
2134  if (collation != cur_ec->ec_collation)
2135  continue;
2136  if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
2137  continue;
2138  /* Does it contain a match to outervar? */
2139  match = false;
2140  foreach(lc2, cur_ec->ec_members)
2141  {
2142  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2143 
2144  Assert(!cur_em->em_is_child); /* no children yet */
2145  if (equal(outervar, cur_em->em_expr))
2146  {
2147  match = true;
2148  break;
2149  }
2150  }
2151  if (!match)
2152  continue; /* no match, so ignore this EC */
2153 
2154  /*
2155  * Yes it does! Try to generate a clause INNERVAR = CONSTANT for each
2156  * CONSTANT in the EC. Note that we must succeed with at least one
2157  * constant before we can decide to throw away the outer-join clause.
2158  */
2159  match = false;
2160  foreach(lc2, cur_ec->ec_members)
2161  {
2162  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2163  Oid eq_op;
2164  RestrictInfo *newrinfo;
2165 
2166  if (!cur_em->em_is_const)
2167  continue; /* ignore non-const members */
2168  eq_op = select_equality_operator(cur_ec,
2169  inner_datatype,
2170  cur_em->em_datatype);
2171  if (!OidIsValid(eq_op))
2172  continue; /* can't generate equality */
2173  newrinfo = build_implied_join_equality(root,
2174  eq_op,
2175  cur_ec->ec_collation,
2176  innervar,
2177  cur_em->em_expr,
2178  bms_copy(inner_relids),
2179  bms_copy(inner_nullable_relids),
2180  cur_ec->ec_min_security);
2181  if (process_equivalence(root, &newrinfo, true))
2182  match = true;
2183  }
2184 
2185  /*
2186  * If we were able to equate INNERVAR to any constant, report success.
2187  * Otherwise, fall out of the search loop, since we know the OUTERVAR
2188  * appears in at most one EC.
2189  */
2190  if (match)
2191  return true;
2192  else
2193  break;
2194  }
2195 
2196  return false; /* failed to make any deduction */
2197 }
bool op_strict(Oid opno)
Definition: lsyscache.c:1448

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

◆ reconsider_outer_join_clauses()

void reconsider_outer_join_clauses ( PlannerInfo root)

Definition at line 1984 of file equivclass.c.

1985 {
1986  bool found;
1987  ListCell *cell;
1988 
1989  /* Outer loop repeats until we find no more deductions */
1990  do
1991  {
1992  found = false;
1993 
1994  /* Process the LEFT JOIN clauses */
1995  foreach(cell, root->left_join_clauses)
1996  {
1997  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1998 
1999  if (reconsider_outer_join_clause(root, rinfo, true))
2000  {
2001  found = true;
2002  /* remove it from the list */
2003  root->left_join_clauses =
2005  /* we throw it back anyway (see notes above) */
2006  /* but the thrown-back clause has no extra selectivity */
2007  rinfo->norm_selec = 2.0;
2008  rinfo->outer_selec = 1.0;
2009  distribute_restrictinfo_to_rels(root, rinfo);
2010  }
2011  }
2012 
2013  /* Process the RIGHT JOIN clauses */
2014  foreach(cell, root->right_join_clauses)
2015  {
2016  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
2017 
2018  if (reconsider_outer_join_clause(root, rinfo, false))
2019  {
2020  found = true;
2021  /* remove it from the list */
2022  root->right_join_clauses =
2024  /* we throw it back anyway (see notes above) */
2025  /* but the thrown-back clause has no extra selectivity */
2026  rinfo->norm_selec = 2.0;
2027  rinfo->outer_selec = 1.0;
2028  distribute_restrictinfo_to_rels(root, rinfo);
2029  }
2030  }
2031 
2032  /* Process the FULL JOIN clauses */
2033  foreach(cell, root->full_join_clauses)
2034  {
2035  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
2036 
2037  if (reconsider_full_join_clause(root, rinfo))
2038  {
2039  found = true;
2040  /* remove it from the list */
2041  root->full_join_clauses =
2043  /* we throw it back anyway (see notes above) */
2044  /* but the thrown-back clause has no extra selectivity */
2045  rinfo->norm_selec = 2.0;
2046  rinfo->outer_selec = 1.0;
2047  distribute_restrictinfo_to_rels(root, rinfo);
2048  }
2049  }
2050  } while (found);
2051 
2052  /* Now, any remaining clauses have to be thrown back */
2053  foreach(cell, root->left_join_clauses)
2054  {
2055  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
2056 
2057  distribute_restrictinfo_to_rels(root, rinfo);
2058  }
2059  foreach(cell, root->right_join_clauses)
2060  {
2061  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
2062 
2063  distribute_restrictinfo_to_rels(root, rinfo);
2064  }
2065  foreach(cell, root->full_join_clauses)
2066  {
2067  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
2068 
2069  distribute_restrictinfo_to_rels(root, rinfo);
2070  }
2071 }
static bool reconsider_full_join_clause(PlannerInfo *root, RestrictInfo *rinfo)
Definition: equivclass.c:2205
static bool reconsider_outer_join_clause(PlannerInfo *root, RestrictInfo *rinfo, bool outer_on_left)
Definition: equivclass.c:2079
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:369
List * left_join_clauses
Definition: pathnodes.h:255
List * full_join_clauses
Definition: pathnodes.h:263
List * right_join_clauses
Definition: pathnodes.h:259
Selectivity outer_selec
Definition: pathnodes.h:2102
Selectivity norm_selec
Definition: pathnodes.h:2099

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

◆ relation_can_be_sorted_early()

bool relation_can_be_sorted_early ( PlannerInfo root,
RelOptInfo rel,
EquivalenceClass ec,
bool  require_parallel_safe 
)

Definition at line 977 of file equivclass.c.

979 {
980  PathTarget *target = rel->reltarget;
981  EquivalenceMember *em;
982  ListCell *lc;
983 
984  /*
985  * Reject volatile ECs immediately; such sorts must always be postponed.
986  */
987  if (ec->ec_has_volatile)
988  return false;
989 
990  /*
991  * Try to find an EM directly matching some reltarget member.
992  */
993  foreach(lc, target->exprs)
994  {
995  Expr *targetexpr = (Expr *) lfirst(lc);
996 
997  em = find_ec_member_matching_expr(ec, targetexpr, rel->relids);
998  if (!em)
999  continue;
1000 
1001  /*
1002  * Reject expressions involving set-returning functions, as those
1003  * can't be computed early either. (Note: this test and the following
1004  * one are effectively checking properties of targetexpr, so there's
1005  * no point in asking whether some other EC member would be better.)
1006  */
1007  if (IS_SRF_CALL((Node *) em->em_expr))
1008  continue;
1009 
1010  /*
1011  * If requested, reject expressions that are not parallel-safe. We
1012  * check this last because it's a rather expensive test.
1013  */
1014  if (require_parallel_safe &&
1015  !is_parallel_safe(root, (Node *) em->em_expr))
1016  continue;
1017 
1018  return true;
1019  }
1020 
1021  /*
1022  * Try to find a expression computable from the reltarget.
1023  */
1024  em = find_computable_ec_member(root, ec, target->exprs, rel->relids,
1025  require_parallel_safe);
1026  if (!em)
1027  return false;
1028 
1029  /*
1030  * Reject expressions involving set-returning functions, as those can't be
1031  * computed early either. (There's no point in looking for another EC
1032  * member in this case; since SRFs can't appear in WHERE, they cannot
1033  * belong to multi-member ECs.)
1034  */
1035  if (IS_SRF_CALL((Node *) em->em_expr))
1036  return false;
1037 
1038  return true;
1039 }
EquivalenceMember * find_ec_member_matching_expr(EquivalenceClass *ec, Expr *expr, Relids relids)
Definition: equivclass.c:786
EquivalenceMember * find_computable_ec_member(PlannerInfo *root, EquivalenceClass *ec, List *exprs, Relids relids, bool require_parallel_safe)
Definition: equivclass.c:851
#define IS_SRF_CALL(node)
Definition: optimizer.h:28
List * exprs
Definition: pathnodes.h:1110
struct PathTarget * reltarget
Definition: pathnodes.h:692

References EquivalenceClass::ec_has_volatile, EquivalenceMember::em_expr, PathTarget::exprs, find_computable_ec_member(), find_ec_member_matching_expr(), is_parallel_safe(), IS_SRF_CALL, lfirst, RelOptInfo::relids, and RelOptInfo::reltarget.

Referenced by get_useful_pathkeys_for_relation().

◆ select_equality_operator()

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

Definition at line 1795 of file equivclass.c.

1796 {
1797  ListCell *lc;
1798 
1799  foreach(lc, ec->ec_opfamilies)
1800  {
1801  Oid opfamily = lfirst_oid(lc);
1802  Oid opno;
1803 
1804  opno = get_opfamily_member(opfamily, lefttype, righttype,
1806  if (!OidIsValid(opno))
1807  continue;
1808  /* If no barrier quals in query, don't worry about leaky operators */
1809  if (ec->ec_max_security == 0)
1810  return opno;
1811  /* Otherwise, insist that selected operators be leakproof */
1812  if (get_func_leakproof(get_opcode(opno)))
1813  return opno;
1814  }
1815  return InvalidOid;
1816 }
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1256
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:164
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:1808
#define lfirst_oid(lc)
Definition: pg_list.h:171
#define BTEqualStrategyNumber
Definition: stratnum.h:31

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