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 "rewrite/rewriteManip.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, JoinDomain *jdomain, EquivalenceMember *parent, 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, OuterJoinClauseInfo *ojcinfo, bool outer_on_left)
 
static bool reconsider_full_join_clause (PlannerInfo *root, OuterJoinClauseInfo *ojcinfo)
 
static JoinDomainfind_join_domain (PlannerInfo *root, Relids relids)
 
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, JoinDomain *jdomain)
 
Exprcanonicalize_ec_expression (Expr *expr, Oid req_type, Oid req_collation)
 
EquivalenceClassget_eclass_for_sort_expr (PlannerInfo *root, Expr *expr, 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)
 
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, SpecialJoinInfo *sjinfo)
 
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 2743 of file equivclass.c.

2747 {
2748  Relids top_parent_relids = child_joinrel->top_parent_relids;
2749  Relids child_relids = child_joinrel->relids;
2750  Bitmapset *matching_ecs;
2751  MemoryContext oldcontext;
2752  int i;
2753 
2754  Assert(IS_JOIN_REL(child_joinrel) && IS_JOIN_REL(parent_joinrel));
2755 
2756  /* We need consider only ECs that mention the parent joinrel */
2757  matching_ecs = get_eclass_indexes_for_relids(root, top_parent_relids);
2758 
2759  /*
2760  * If we're being called during GEQO join planning, we still have to
2761  * create any new EC members in the main planner context, to avoid having
2762  * a corrupt EC data structure after the GEQO context is reset. This is
2763  * problematic since we'll leak memory across repeated GEQO cycles. For
2764  * now, though, bloat is better than crash. If it becomes a real issue
2765  * we'll have to do something to avoid generating duplicate EC members.
2766  */
2767  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
2768 
2769  i = -1;
2770  while ((i = bms_next_member(matching_ecs, i)) >= 0)
2771  {
2772  EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2773  int num_members;
2774 
2775  /*
2776  * If this EC contains a volatile expression, then generating child
2777  * EMs would be downright dangerous, so skip it. We rely on a
2778  * volatile EC having only one EM.
2779  */
2780  if (cur_ec->ec_has_volatile)
2781  continue;
2782 
2783  /* Sanity check on get_eclass_indexes_for_relids result */
2784  Assert(bms_overlap(top_parent_relids, cur_ec->ec_relids));
2785 
2786  /*
2787  * We don't use foreach() here because there's no point in scanning
2788  * newly-added child members, so we can stop after the last
2789  * pre-existing EC member.
2790  */
2791  num_members = list_length(cur_ec->ec_members);
2792  for (int pos = 0; pos < num_members; pos++)
2793  {
2794  EquivalenceMember *cur_em = (EquivalenceMember *) list_nth(cur_ec->ec_members, pos);
2795 
2796  if (cur_em->em_is_const)
2797  continue; /* ignore consts here */
2798 
2799  /*
2800  * We consider only original EC members here, not
2801  * already-transformed child members.
2802  */
2803  if (cur_em->em_is_child)
2804  continue; /* ignore children here */
2805 
2806  /*
2807  * We may ignore expressions that reference a single baserel,
2808  * because add_child_rel_equivalences should have handled them.
2809  */
2810  if (bms_membership(cur_em->em_relids) != BMS_MULTIPLE)
2811  continue;
2812 
2813  /* Does this member reference child's topmost parent rel? */
2814  if (bms_overlap(cur_em->em_relids, top_parent_relids))
2815  {
2816  /* Yes, generate transformed child version */
2817  Expr *child_expr;
2818  Relids new_relids;
2819 
2820  if (parent_joinrel->reloptkind == RELOPT_JOINREL)
2821  {
2822  /* Simple single-level transformation */
2823  child_expr = (Expr *)
2825  (Node *) cur_em->em_expr,
2826  nappinfos, appinfos);
2827  }
2828  else
2829  {
2830  /* Must do multi-level transformation */
2831  Assert(parent_joinrel->reloptkind == RELOPT_OTHER_JOINREL);
2832  child_expr = (Expr *)
2834  (Node *) cur_em->em_expr,
2835  child_joinrel,
2836  child_joinrel->top_parent);
2837  }
2838 
2839  /*
2840  * Transform em_relids to match. Note we do *not* do
2841  * pull_varnos(child_expr) here, as for example the
2842  * transformation might have substituted a constant, but we
2843  * don't want the child member to be marked as constant.
2844  */
2845  new_relids = bms_difference(cur_em->em_relids,
2846  top_parent_relids);
2847  new_relids = bms_add_members(new_relids, child_relids);
2848 
2849  (void) add_eq_member(cur_ec, child_expr, new_relids,
2850  cur_em->em_jdomain,
2851  cur_em, cur_em->em_datatype);
2852  }
2853  }
2854  }
2855 
2856  MemoryContextSwitchTo(oldcontext);
2857 }
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:196
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, RelOptInfo *childrel, RelOptInfo *parentrel)
Definition: appendinfo.c:521
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1106
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:297
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:835
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:712
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:527
@ BMS_MULTIPLE
Definition: bitmapset.h:73
static EquivalenceMember * add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids, JoinDomain *jdomain, EquivalenceMember *parent, Oid datatype)
Definition: equivclass.c:517
static Bitmapset * get_eclass_indexes_for_relids(PlannerInfo *root, Relids relids)
Definition: equivclass.c:3257
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:829
@ RELOPT_JOINREL
Definition: pathnodes.h:813
@ RELOPT_OTHER_JOINREL
Definition: pathnodes.h:815
static int list_length(const List *l)
Definition: pg_list.h:152
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
JoinDomain * em_jdomain
Definition: pathnodes.h:1424
Definition: nodes.h:129
List * eq_classes
Definition: pathnodes.h:311
Relids relids
Definition: pathnodes.h:856
Relids top_parent_relids
Definition: pathnodes.h:988
RelOptKind reloptkind
Definition: pathnodes.h:850

References add_eq_member(), adjust_appendrel_attrs(), adjust_appendrel_attrs_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_jdomain, EquivalenceMember::em_relids, PlannerInfo::eq_classes, get_eclass_indexes_for_relids(), i, IS_JOIN_REL, list_length(), list_nth(), MemoryContextSwitchTo(), 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 2621 of file equivclass.c.

2625 {
2626  Relids top_parent_relids = child_rel->top_parent_relids;
2627  Relids child_relids = child_rel->relids;
2628  int i;
2629 
2630  /*
2631  * EC merging should be complete already, so we can use the parent rel's
2632  * eclass_indexes to avoid searching all of root->eq_classes.
2633  */
2634  Assert(root->ec_merging_done);
2635  Assert(IS_SIMPLE_REL(parent_rel));
2636 
2637  i = -1;
2638  while ((i = bms_next_member(parent_rel->eclass_indexes, i)) >= 0)
2639  {
2640  EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2641  int num_members;
2642 
2643  /*
2644  * If this EC contains a volatile expression, then generating child
2645  * EMs would be downright dangerous, so skip it. We rely on a
2646  * volatile EC having only one EM.
2647  */
2648  if (cur_ec->ec_has_volatile)
2649  continue;
2650 
2651  /* Sanity check eclass_indexes only contain ECs for parent_rel */
2652  Assert(bms_is_subset(top_parent_relids, cur_ec->ec_relids));
2653 
2654  /*
2655  * We don't use foreach() here because there's no point in scanning
2656  * newly-added child members, so we can stop after the last
2657  * pre-existing EC member.
2658  */
2659  num_members = list_length(cur_ec->ec_members);
2660  for (int pos = 0; pos < num_members; pos++)
2661  {
2662  EquivalenceMember *cur_em = (EquivalenceMember *) list_nth(cur_ec->ec_members, pos);
2663 
2664  if (cur_em->em_is_const)
2665  continue; /* ignore consts here */
2666 
2667  /*
2668  * We consider only original EC members here, not
2669  * already-transformed child members. Otherwise, if some original
2670  * member expression references more than one appendrel, we'd get
2671  * an O(N^2) explosion of useless derived expressions for
2672  * combinations of children. (But add_child_join_rel_equivalences
2673  * may add targeted combinations for partitionwise-join purposes.)
2674  */
2675  if (cur_em->em_is_child)
2676  continue; /* ignore children here */
2677 
2678  /*
2679  * Consider only members that reference and can be computed at
2680  * child's topmost parent rel. In particular we want to exclude
2681  * parent-rel Vars that have nonempty varnullingrels. Translating
2682  * those might fail, if the transformed expression wouldn't be a
2683  * simple Var; and in any case it wouldn't produce a member that
2684  * has any use in creating plans for the child rel.
2685  */
2686  if (bms_is_subset(cur_em->em_relids, top_parent_relids) &&
2687  !bms_is_empty(cur_em->em_relids))
2688  {
2689  /* OK, generate transformed child version */
2690  Expr *child_expr;
2691  Relids new_relids;
2692 
2693  if (parent_rel->reloptkind == RELOPT_BASEREL)
2694  {
2695  /* Simple single-level transformation */
2696  child_expr = (Expr *)
2698  (Node *) cur_em->em_expr,
2699  1, &appinfo);
2700  }
2701  else
2702  {
2703  /* Must do multi-level transformation */
2704  child_expr = (Expr *)
2706  (Node *) cur_em->em_expr,
2707  child_rel,
2708  child_rel->top_parent);
2709  }
2710 
2711  /*
2712  * Transform em_relids to match. Note we do *not* do
2713  * pull_varnos(child_expr) here, as for example the
2714  * transformation might have substituted a constant, but we
2715  * don't want the child member to be marked as constant.
2716  */
2717  new_relids = bms_difference(cur_em->em_relids,
2718  top_parent_relids);
2719  new_relids = bms_add_members(new_relids, child_relids);
2720 
2721  (void) add_eq_member(cur_ec, child_expr, new_relids,
2722  cur_em->em_jdomain,
2723  cur_em, cur_em->em_datatype);
2724 
2725  /* Record this EC index for the child rel */
2726  child_rel->eclass_indexes = bms_add_member(child_rel->eclass_indexes, i);
2727  }
2728  }
2729  }
2730 }
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:363
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:753
#define bms_is_empty(a)
Definition: bitmapset.h:105
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:824
@ RELOPT_BASEREL
Definition: pathnodes.h:812
bool ec_merging_done
Definition: pathnodes.h:314
Bitmapset * eclass_indexes
Definition: pathnodes.h:931

References add_eq_member(), adjust_appendrel_attrs(), adjust_appendrel_attrs_multilevel(), Assert(), bms_add_member(), bms_add_members(), bms_difference(), bms_is_empty, bms_is_subset(), bms_next_member(), 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_jdomain, 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,
JoinDomain jdomain,
EquivalenceMember parent,
Oid  datatype 
)
static

Definition at line 517 of file equivclass.c.

519 {
521 
522  em->em_expr = expr;
523  em->em_relids = relids;
524  em->em_is_const = false;
525  em->em_is_child = (parent != NULL);
526  em->em_datatype = datatype;
527  em->em_jdomain = jdomain;
528  em->em_parent = parent;
529 
530  if (bms_is_empty(relids))
531  {
532  /*
533  * No Vars, assume it's a pseudoconstant. This is correct for entries
534  * generated from process_equivalence(), because a WHERE clause can't
535  * contain aggregates or SRFs, and non-volatility was checked before
536  * process_equivalence() ever got called. But
537  * get_eclass_for_sort_expr() has to work harder. We put the tests
538  * there not here to save cycles in the equivalence case.
539  */
540  Assert(!parent);
541  em->em_is_const = true;
542  ec->ec_has_const = true;
543  /* it can't affect ec_relids */
544  }
545  else if (!parent) /* child members don't add to ec_relids */
546  {
547  ec->ec_relids = bms_add_members(ec->ec_relids, relids);
548  }
549  ec->ec_members = lappend(ec->ec_members, em);
550 
551  return em;
552 }
List * lappend(List *list, void *datum)
Definition: list.c:338
#define makeNode(_type_)
Definition: nodes.h:176

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_jdomain, 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 472 of file equivclass.c.

473 {
474  Oid expr_type = exprType((Node *) expr);
475 
476  /*
477  * For a polymorphic-input-type opclass, just keep the same exposed type.
478  * RECORD opclasses work like polymorphic-type ones for this purpose.
479  */
480  if (IsPolymorphicType(req_type) || req_type == RECORDOID)
481  req_type = expr_type;
482 
483  /*
484  * No work if the expression exposes the right type/collation already.
485  */
486  if (expr_type != req_type ||
487  exprCollation((Node *) expr) != req_collation)
488  {
489  /*
490  * If we have to change the type of the expression, set typmod to -1,
491  * since the new type may not have the same typmod interpretation.
492  * When we only have to change collation, preserve the exposed typmod.
493  */
494  int32 req_typmod;
495 
496  if (expr_type != req_type)
497  req_typmod = -1;
498  else
499  req_typmod = exprTypmod((Node *) expr);
500 
501  /*
502  * Use applyRelabelType so that we preserve const-flatness. This is
503  * important since eval_const_expressions has already been applied.
504  */
505  expr = (Expr *) applyRelabelType((Node *) expr,
506  req_type, req_typmod, req_collation,
507  COERCE_IMPLICIT_CAST, -1, false);
508  }
509 
510  return expr;
511 }
signed int int32
Definition: c.h:483
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:43
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:282
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:786
Node * applyRelabelType(Node *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat, int rlocation, bool overwrite_ok)
Definition: nodeFuncs.c:601
unsigned int Oid
Definition: postgres_ext.h:31
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:663

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

1818 {
1819  RestrictInfo *rinfo;
1820  RestrictInfo *parent_rinfo = NULL;
1821  ListCell *lc;
1822  MemoryContext oldcontext;
1823 
1824  /*
1825  * Search to see if we already built a RestrictInfo for this pair of
1826  * EquivalenceMembers. We can use either original source clauses or
1827  * previously-derived clauses, and a commutator clause is acceptable.
1828  *
1829  * We used to verify that opno matches, but that seems redundant: even if
1830  * it's not identical, it'd better have the same effects, or the operator
1831  * families we're using are broken.
1832  */
1833  foreach(lc, ec->ec_sources)
1834  {
1835  rinfo = (RestrictInfo *) lfirst(lc);
1836  if (rinfo->left_em == leftem &&
1837  rinfo->right_em == rightem &&
1838  rinfo->parent_ec == parent_ec)
1839  return rinfo;
1840  if (rinfo->left_em == rightem &&
1841  rinfo->right_em == leftem &&
1842  rinfo->parent_ec == parent_ec)
1843  return rinfo;
1844  }
1845 
1846  foreach(lc, ec->ec_derives)
1847  {
1848  rinfo = (RestrictInfo *) lfirst(lc);
1849  if (rinfo->left_em == leftem &&
1850  rinfo->right_em == rightem &&
1851  rinfo->parent_ec == parent_ec)
1852  return rinfo;
1853  if (rinfo->left_em == rightem &&
1854  rinfo->right_em == leftem &&
1855  rinfo->parent_ec == parent_ec)
1856  return rinfo;
1857  }
1858 
1859  /*
1860  * Not there, so build it, in planner context so we can re-use it. (Not
1861  * important in normal planning, but definitely so in GEQO.)
1862  */
1863  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
1864 
1865  /*
1866  * If either EM is a child, recursively create the corresponding
1867  * parent-to-parent clause, so that we can duplicate its rinfo_serial.
1868  */
1869  if (leftem->em_is_child || rightem->em_is_child)
1870  {
1871  EquivalenceMember *leftp = leftem->em_parent ? leftem->em_parent : leftem;
1872  EquivalenceMember *rightp = rightem->em_parent ? rightem->em_parent : rightem;
1873 
1874  parent_rinfo = create_join_clause(root, ec, opno,
1875  leftp, rightp,
1876  parent_ec);
1877  }
1878 
1879  rinfo = build_implied_join_equality(root,
1880  opno,
1881  ec->ec_collation,
1882  leftem->em_expr,
1883  rightem->em_expr,
1884  bms_union(leftem->em_relids,
1885  rightem->em_relids),
1886  ec->ec_min_security);
1887 
1888  /* If it's a child clause, copy the parent's rinfo_serial */
1889  if (parent_rinfo)
1890  rinfo->rinfo_serial = parent_rinfo->rinfo_serial;
1891 
1892  /* Mark the clause as redundant, or not */
1893  rinfo->parent_ec = parent_ec;
1894 
1895  /*
1896  * We know the correct values for left_ec/right_ec, ie this particular EC,
1897  * so we can just set them directly instead of forcing another lookup.
1898  */
1899  rinfo->left_ec = ec;
1900  rinfo->right_ec = ec;
1901 
1902  /* Mark it as usable with these EMs */
1903  rinfo->left_em = leftem;
1904  rinfo->right_em = rightem;
1905  /* and save it for possible re-use */
1906  ec->ec_derives = lappend(ec->ec_derives, rinfo);
1907 
1908  MemoryContextSwitchTo(oldcontext);
1909 
1910  return rinfo;
1911 }
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:211
static RestrictInfo * create_join_clause(PlannerInfo *root, EquivalenceClass *ec, Oid opno, EquivalenceMember *leftem, EquivalenceMember *rightem, EquivalenceClass *parent_ec)
Definition: equivclass.c:1813
RestrictInfo * build_implied_join_equality(PlannerInfo *root, Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Index security_level)
Definition: initsplan.c:2861
#define lfirst(lc)
Definition: pg_list.h:172
Index ec_min_security
Definition: pathnodes.h:1379
int rinfo_serial
Definition: pathnodes.h:2598

References bms_union(), build_implied_join_equality(), EquivalenceClass::ec_collation, EquivalenceClass::ec_derives, EquivalenceClass::ec_min_security, EquivalenceClass::ec_sources, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_relids, lappend(), lfirst, MemoryContextSwitchTo(), and RestrictInfo::rinfo_serial.

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

3139 {
3140  Relids relids;
3141  ListCell *lc;
3142 
3143  Assert(!eclass->ec_merged);
3144 
3145  /*
3146  * Won't generate joinclauses if const or single-member (the latter test
3147  * covers the volatile case too)
3148  */
3149  if (eclass->ec_has_const || list_length(eclass->ec_members) <= 1)
3150  return false;
3151 
3152  /*
3153  * Note we don't test ec_broken; if we did, we'd need a separate code path
3154  * to look through ec_sources. Checking the members anyway is OK as a
3155  * possibly-overoptimistic heuristic.
3156  */
3157 
3158  /* If specified rel is a child, we must consider the topmost parent rel */
3159  if (IS_OTHER_REL(rel))
3160  {
3162  relids = rel->top_parent_relids;
3163  }
3164  else
3165  relids = rel->relids;
3166 
3167  /* If rel already includes all members of eclass, no point in searching */
3168  if (bms_is_subset(eclass->ec_relids, relids))
3169  return false;
3170 
3171  /* To join, we need a member not in the given rel */
3172  foreach(lc, eclass->ec_members)
3173  {
3174  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
3175 
3176  if (cur_em->em_is_child)
3177  continue; /* ignore children here */
3178 
3179  if (!bms_overlap(cur_em->em_relids, relids))
3180  return true;
3181  }
3182 
3183  return false;
3184 }
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:839
static struct cvec * eclass(struct vars *v, chr c, int cases)
Definition: regc_locale.c:500

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

2440 {
2441  ListCell *lc1;
2442 
2443  foreach(lc1, root->eq_classes)
2444  {
2445  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2446  bool item1member = false;
2447  bool item2member = false;
2448  ListCell *lc2;
2449 
2450  /* Never match to a volatile EC */
2451  if (ec->ec_has_volatile)
2452  continue;
2453 
2454  foreach(lc2, ec->ec_members)
2455  {
2457 
2458  if (em->em_is_child)
2459  continue; /* ignore children here */
2460  if (equal(item1, em->em_expr))
2461  item1member = true;
2462  else if (equal(item2, em->em_expr))
2463  item2member = true;
2464  /* Exit as soon as equality is proven */
2465  if (item1member && item2member)
2466  return true;
2467  }
2468  }
2469  return false;
2470 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223

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

830 {
831  ListCell *lc;
832 
833  foreach(lc, ec->ec_members)
834  {
836  List *exprvars;
837  ListCell *lc2;
838 
839  /*
840  * We shouldn't be trying to sort by an equivalence class that
841  * contains a constant, so no need to consider such cases any further.
842  */
843  if (em->em_is_const)
844  continue;
845 
846  /*
847  * Ignore child members unless they belong to the requested rel.
848  */
849  if (em->em_is_child &&
850  !bms_is_subset(em->em_relids, relids))
851  continue;
852 
853  /*
854  * Match if all Vars and quasi-Vars are available in "exprs".
855  */
856  exprvars = pull_var_clause((Node *) em->em_expr,
860  foreach(lc2, exprvars)
861  {
862  if (!is_exprlist_member(lfirst(lc2), exprs))
863  break;
864  }
865  list_free(exprvars);
866  if (lc2)
867  continue; /* we hit a non-available Var */
868 
869  /*
870  * If requested, reject expressions that are not parallel-safe. We
871  * check this last because it's a rather expensive test.
872  */
873  if (require_parallel_safe &&
874  !is_parallel_safe(root, (Node *) em->em_expr))
875  continue;
876 
877  return em; /* found usable expression */
878  }
879 
880  return NULL;
881 }
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:670
static bool is_exprlist_member(Expr *node, List *exprs)
Definition: equivclass.c:891
void list_free(List *list)
Definition: list.c:1545
#define PVC_INCLUDE_WINDOWFUNCS
Definition: optimizer.h:185
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:187
#define PVC_INCLUDE_AGGREGATES
Definition: optimizer.h:183
Definition: pg_list.h:54
List * pull_var_clause(Node *node, int flags)
Definition: var.c:607

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

2583 {
2584  ListCell *lc;
2585 
2586  Assert(ec->ec_has_const);
2587  Assert(!em->em_is_const);
2588  foreach(lc, ec->ec_derives)
2589  {
2590  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2591 
2592  /*
2593  * generate_base_implied_equalities_const will have put non-const
2594  * members on the left side of derived clauses.
2595  */
2596  if (rinfo->left_em == em)
2597  return rinfo;
2598  }
2599  return NULL;
2600 }

References Assert(), EquivalenceClass::ec_derives, EquivalenceClass::ec_has_const, EquivalenceMember::em_is_const, 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 760 of file equivclass.c.

763 {
764  ListCell *lc;
765 
766  /* We ignore binary-compatible relabeling on both ends */
767  while (expr && IsA(expr, RelabelType))
768  expr = ((RelabelType *) expr)->arg;
769 
770  foreach(lc, ec->ec_members)
771  {
773  Expr *emexpr;
774 
775  /*
776  * We shouldn't be trying to sort by an equivalence class that
777  * contains a constant, so no need to consider such cases any further.
778  */
779  if (em->em_is_const)
780  continue;
781 
782  /*
783  * Ignore child members unless they belong to the requested rel.
784  */
785  if (em->em_is_child &&
786  !bms_is_subset(em->em_relids, relids))
787  continue;
788 
789  /*
790  * Match if same expression (after stripping relabel).
791  */
792  emexpr = em->em_expr;
793  while (emexpr && IsA(emexpr, RelabelType))
794  emexpr = ((RelabelType *) emexpr)->arg;
795 
796  if (equal(emexpr, expr))
797  return em;
798  }
799 
800  return NULL;
801 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
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_join_domain()

static JoinDomain * find_join_domain ( PlannerInfo root,
Relids  relids 
)
static

Definition at line 2410 of file equivclass.c.

2411 {
2412  ListCell *lc;
2413 
2414  foreach(lc, root->join_domains)
2415  {
2416  JoinDomain *jdomain = (JoinDomain *) lfirst(lc);
2417 
2418  if (bms_is_subset(jdomain->jd_relids, relids))
2419  return jdomain;
2420  }
2421  elog(ERROR, "failed to find appropriate JoinDomain");
2422  return NULL; /* keep compiler quiet */
2423 }
#define ERROR
Definition: elog.h:39
Relids jd_relids
Definition: pathnodes.h:1306
List * join_domains
Definition: pathnodes.h:308

References bms_is_subset(), elog(), ERROR, JoinDomain::jd_relids, PlannerInfo::join_domains, and lfirst.

Referenced by reconsider_full_join_clause(), and reconsider_outer_join_clause().

◆ generate_base_implied_equalities()

void generate_base_implied_equalities ( PlannerInfo root)

Definition at line 1033 of file equivclass.c.

1034 {
1035  int ec_index;
1036  ListCell *lc;
1037 
1038  /*
1039  * At this point, we're done absorbing knowledge of equivalences in the
1040  * query, so no further EC merging should happen, and ECs remaining in the
1041  * eq_classes list can be considered canonical. (But note that it's still
1042  * possible for new single-member ECs to be added through
1043  * get_eclass_for_sort_expr().)
1044  */
1045  root->ec_merging_done = true;
1046 
1047  ec_index = 0;
1048  foreach(lc, root->eq_classes)
1049  {
1051  bool can_generate_joinclause = false;
1052  int i;
1053 
1054  Assert(ec->ec_merged == NULL); /* else shouldn't be in list */
1055  Assert(!ec->ec_broken); /* not yet anyway... */
1056 
1057  /*
1058  * Generate implied equalities that are restriction clauses.
1059  * Single-member ECs won't generate any deductions, either here or at
1060  * the join level.
1061  */
1062  if (list_length(ec->ec_members) > 1)
1063  {
1064  if (ec->ec_has_const)
1066  else
1068 
1069  /* Recover if we failed to generate required derived clauses */
1070  if (ec->ec_broken)
1072 
1073  /* Detect whether this EC might generate join clauses */
1074  can_generate_joinclause =
1076  }
1077 
1078  /*
1079  * Mark the base rels cited in each eclass (which should all exist by
1080  * now) with the eq_classes indexes of all eclasses mentioning them.
1081  * This will let us avoid searching in subsequent lookups. While
1082  * we're at it, we can mark base rels that have pending eclass joins;
1083  * this is a cheap version of has_relevant_eclass_joinclause().
1084  */
1085  i = -1;
1086  while ((i = bms_next_member(ec->ec_relids, i)) > 0)
1087  {
1088  RelOptInfo *rel = root->simple_rel_array[i];
1089 
1090  if (rel == NULL) /* must be an outer join */
1091  {
1093  continue;
1094  }
1095 
1096  Assert(rel->reloptkind == RELOPT_BASEREL);
1097 
1099  ec_index);
1100 
1101  if (can_generate_joinclause)
1102  rel->has_eclass_joins = true;
1103  }
1104 
1105  ec_index++;
1106  }
1107 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:460
static void generate_base_implied_equalities_broken(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:1318
static void generate_base_implied_equalities_no_const(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:1208
static void generate_base_implied_equalities_const(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:1113
struct EquivalenceClass * ec_merged
Definition: pathnodes.h:1381
Relids outer_join_rels
Definition: pathnodes.h:258
bool has_eclass_joins
Definition: pathnodes.h:972

References Assert(), bms_add_member(), bms_is_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(), PlannerInfo::outer_join_rels, RELOPT_BASEREL, and RelOptInfo::reloptkind.

Referenced by query_planner().

◆ generate_base_implied_equalities_broken()

static void generate_base_implied_equalities_broken ( PlannerInfo root,
EquivalenceClass ec 
)
static

Definition at line 1318 of file equivclass.c.

1320 {
1321  ListCell *lc;
1322 
1323  foreach(lc, ec->ec_sources)
1324  {
1325  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1326 
1327  if (ec->ec_has_const ||
1328  bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
1329  distribute_restrictinfo_to_rels(root, restrictinfo);
1330  }
1331 }
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2631
Relids required_relids
Definition: pathnodes.h:2560

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

1115 {
1116  EquivalenceMember *const_em = NULL;
1117  ListCell *lc;
1118 
1119  /*
1120  * In the trivial case where we just had one "var = const" clause, push
1121  * the original clause back into the main planner machinery. There is
1122  * nothing to be gained by doing it differently, and we save the effort to
1123  * re-build and re-analyze an equality clause that will be exactly
1124  * equivalent to the old one.
1125  */
1126  if (list_length(ec->ec_members) == 2 &&
1127  list_length(ec->ec_sources) == 1)
1128  {
1129  RestrictInfo *restrictinfo = (RestrictInfo *) linitial(ec->ec_sources);
1130 
1131  distribute_restrictinfo_to_rels(root, restrictinfo);
1132  return;
1133  }
1134 
1135  /*
1136  * Find the constant member to use. We prefer an actual constant to
1137  * pseudo-constants (such as Params), because the constraint exclusion
1138  * machinery might be able to exclude relations on the basis of generated
1139  * "var = const" equalities, but "var = param" won't work for that.
1140  */
1141  foreach(lc, ec->ec_members)
1142  {
1143  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1144 
1145  if (cur_em->em_is_const)
1146  {
1147  const_em = cur_em;
1148  if (IsA(cur_em->em_expr, Const))
1149  break;
1150  }
1151  }
1152  Assert(const_em != NULL);
1153 
1154  /* Generate a derived equality against each other member */
1155  foreach(lc, ec->ec_members)
1156  {
1157  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1158  Oid eq_op;
1159  RestrictInfo *rinfo;
1160 
1161  Assert(!cur_em->em_is_child); /* no children yet */
1162  if (cur_em == const_em)
1163  continue;
1164  eq_op = select_equality_operator(ec,
1165  cur_em->em_datatype,
1166  const_em->em_datatype);
1167  if (!OidIsValid(eq_op))
1168  {
1169  /* failed... */
1170  ec->ec_broken = true;
1171  break;
1172  }
1173 
1174  /*
1175  * We use the constant's em_jdomain as qualscope, so that if the
1176  * generated clause is variable-free (i.e, both EMs are consts) it
1177  * will be enforced at the join domain level.
1178  */
1179  rinfo = process_implied_equality(root, eq_op, ec->ec_collation,
1180  cur_em->em_expr, const_em->em_expr,
1181  const_em->em_jdomain->jd_relids,
1182  ec->ec_min_security,
1183  cur_em->em_is_const);
1184 
1185  /*
1186  * If the clause didn't degenerate to a constant, fill in the correct
1187  * markings for a mergejoinable clause, and save it in ec_derives. (We
1188  * will not re-use such clauses directly, but selectivity estimation
1189  * may consult the list later. Note that this use of ec_derives does
1190  * not overlap with its use for join clauses, since we never generate
1191  * join clauses from an ec_has_const eclass.)
1192  */
1193  if (rinfo && rinfo->mergeopfamilies)
1194  {
1195  /* it's not redundant, so don't set parent_ec */
1196  rinfo->left_ec = rinfo->right_ec = ec;
1197  rinfo->left_em = cur_em;
1198  rinfo->right_em = const_em;
1199  ec->ec_derives = lappend(ec->ec_derives, rinfo);
1200  }
1201  }
1202 }
#define OidIsValid(objectId)
Definition: c.h:764
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1777
RestrictInfo * process_implied_equality(PlannerInfo *root, Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Index security_level, bool both_const)
Definition: initsplan.c:2722
#define linitial(l)
Definition: pg_list.h:178

References Assert(), distribute_restrictinfo_to_rels(), EquivalenceClass::ec_broken, EquivalenceClass::ec_collation, EquivalenceClass::ec_derives, EquivalenceClass::ec_members, EquivalenceClass::ec_min_security, EquivalenceClass::ec_sources, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, EquivalenceMember::em_jdomain, IsA, JoinDomain::jd_relids, lappend(), lfirst, linitial, list_length(), OidIsValid, process_implied_equality(), 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 1208 of file equivclass.c.

1210 {
1211  EquivalenceMember **prev_ems;
1212  ListCell *lc;
1213 
1214  /*
1215  * We scan the EC members once and track the last-seen member for each
1216  * base relation. When we see another member of the same base relation,
1217  * we generate "prev_em = cur_em". This results in the minimum number of
1218  * derived clauses, but it's possible that it will fail when a different
1219  * ordering would succeed. XXX FIXME: use a UNION-FIND algorithm similar
1220  * to the way we build merged ECs. (Use a list-of-lists for each rel.)
1221  */
1222  prev_ems = (EquivalenceMember **)
1223  palloc0(root->simple_rel_array_size * sizeof(EquivalenceMember *));
1224 
1225  foreach(lc, ec->ec_members)
1226  {
1227  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1228  int relid;
1229 
1230  Assert(!cur_em->em_is_child); /* no children yet */
1231  if (!bms_get_singleton_member(cur_em->em_relids, &relid))
1232  continue;
1233  Assert(relid < root->simple_rel_array_size);
1234 
1235  if (prev_ems[relid] != NULL)
1236  {
1237  EquivalenceMember *prev_em = prev_ems[relid];
1238  Oid eq_op;
1239  RestrictInfo *rinfo;
1240 
1241  eq_op = select_equality_operator(ec,
1242  prev_em->em_datatype,
1243  cur_em->em_datatype);
1244  if (!OidIsValid(eq_op))
1245  {
1246  /* failed... */
1247  ec->ec_broken = true;
1248  break;
1249  }
1250 
1251  /*
1252  * The expressions aren't constants, so the passed qualscope will
1253  * never be used to place the generated clause. We just need to
1254  * be sure it covers both expressions, which em_relids should do.
1255  */
1256  rinfo = process_implied_equality(root, eq_op, ec->ec_collation,
1257  prev_em->em_expr, cur_em->em_expr,
1258  cur_em->em_relids,
1259  ec->ec_min_security,
1260  false);
1261 
1262  /*
1263  * If the clause didn't degenerate to a constant, fill in the
1264  * correct markings for a mergejoinable clause. We don't put it
1265  * in ec_derives however; we don't currently need to re-find such
1266  * clauses, and we don't want to clutter that list with non-join
1267  * clauses.
1268  */
1269  if (rinfo && rinfo->mergeopfamilies)
1270  {
1271  /* it's not redundant, so don't set parent_ec */
1272  rinfo->left_ec = rinfo->right_ec = ec;
1273  rinfo->left_em = prev_em;
1274  rinfo->right_em = cur_em;
1275  }
1276  }
1277  prev_ems[relid] = cur_em;
1278  }
1279 
1280  pfree(prev_ems);
1281 
1282  /*
1283  * We also have to make sure that all the Vars used in the member clauses
1284  * will be available at any join node we might try to reference them at.
1285  * For the moment we force all the Vars to be available at all join nodes
1286  * for this eclass. Perhaps this could be improved by doing some
1287  * pre-analysis of which members we prefer to join, but it's no worse than
1288  * what happened in the pre-8.3 code.
1289  */
1290  foreach(lc, ec->ec_members)
1291  {
1292  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1293  List *vars = pull_var_clause((Node *) cur_em->em_expr,
1297 
1299  list_free(vars);
1300  }
1301 }
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:652
void add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed)
Definition: initsplan.c:280
void pfree(void *pointer)
Definition: mcxt.c:1456
void * palloc0(Size size)
Definition: mcxt.c:1257
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:184
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:186
int simple_rel_array_size
Definition: pathnodes.h:229
Definition: regcomp.c:281

References add_vars_to_targetlist(), Assert(), bms_get_singleton_member(), 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_relids, lfirst, list_free(), OidIsValid, palloc0(), pfree(), process_implied_equality(), pull_var_clause(), PVC_INCLUDE_PLACEHOLDERS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_WINDOWFUNCS, select_equality_operator(), and PlannerInfo::simple_rel_array_size.

Referenced by generate_base_implied_equalities().

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

2889 {
2890  List *result = NIL;
2891  bool is_child_rel = (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
2892  Relids parent_relids;
2893  int i;
2894 
2895  /* Should be OK to rely on eclass_indexes */
2896  Assert(root->ec_merging_done);
2897 
2898  /* Indexes are available only on base or "other" member relations. */
2899  Assert(IS_SIMPLE_REL(rel));
2900 
2901  /* If it's a child rel, we'll need to know what its parent(s) are */
2902  if (is_child_rel)
2903  parent_relids = find_childrel_parents(root, rel);
2904  else
2905  parent_relids = NULL; /* not used, but keep compiler quiet */
2906 
2907  i = -1;
2908  while ((i = bms_next_member(rel->eclass_indexes, i)) >= 0)
2909  {
2910  EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2911  EquivalenceMember *cur_em;
2912  ListCell *lc2;
2913 
2914  /* Sanity check eclass_indexes only contain ECs for rel */
2915  Assert(is_child_rel || bms_is_subset(rel->relids, cur_ec->ec_relids));
2916 
2917  /*
2918  * Won't generate joinclauses if const or single-member (the latter
2919  * test covers the volatile case too)
2920  */
2921  if (cur_ec->ec_has_const || list_length(cur_ec->ec_members) <= 1)
2922  continue;
2923 
2924  /*
2925  * Scan members, looking for a match to the target column. Note that
2926  * child EC members are considered, but only when they belong to the
2927  * target relation. (Unlike regular members, the same expression
2928  * could be a child member of more than one EC. Therefore, it's
2929  * potentially order-dependent which EC a child relation's target
2930  * column gets matched to. This is annoying but it only happens in
2931  * corner cases, so for now we live with just reporting the first
2932  * match. See also get_eclass_for_sort_expr.)
2933  */
2934  cur_em = NULL;
2935  foreach(lc2, cur_ec->ec_members)
2936  {
2937  cur_em = (EquivalenceMember *) lfirst(lc2);
2938  if (bms_equal(cur_em->em_relids, rel->relids) &&
2939  callback(root, rel, cur_ec, cur_em, callback_arg))
2940  break;
2941  cur_em = NULL;
2942  }
2943 
2944  if (!cur_em)
2945  continue;
2946 
2947  /*
2948  * Found our match. Scan the other EC members and attempt to generate
2949  * joinclauses.
2950  */
2951  foreach(lc2, cur_ec->ec_members)
2952  {
2953  EquivalenceMember *other_em = (EquivalenceMember *) lfirst(lc2);
2954  Oid eq_op;
2955  RestrictInfo *rinfo;
2956 
2957  if (other_em->em_is_child)
2958  continue; /* ignore children here */
2959 
2960  /* Make sure it'll be a join to a different rel */
2961  if (other_em == cur_em ||
2962  bms_overlap(other_em->em_relids, rel->relids))
2963  continue;
2964 
2965  /* Forget it if caller doesn't want joins to this rel */
2966  if (bms_overlap(other_em->em_relids, prohibited_rels))
2967  continue;
2968 
2969  /*
2970  * Also, if this is a child rel, avoid generating a useless join
2971  * to its parent rel(s).
2972  */
2973  if (is_child_rel &&
2974  bms_overlap(parent_relids, other_em->em_relids))
2975  continue;
2976 
2977  eq_op = select_equality_operator(cur_ec,
2978  cur_em->em_datatype,
2979  other_em->em_datatype);
2980  if (!OidIsValid(eq_op))
2981  continue;
2982 
2983  /* set parent_ec to mark as redundant with other joinclauses */
2984  rinfo = create_join_clause(root, cur_ec, eq_op,
2985  cur_em, other_em,
2986  cur_ec);
2987 
2988  result = lappend(result, rinfo);
2989  }
2990 
2991  /*
2992  * If somehow we failed to create any join clauses, we might as well
2993  * keep scanning the ECs for another match. But if we did make any,
2994  * we're done, because we don't want to return non-redundant clauses.
2995  */
2996  if (result)
2997  break;
2998  }
2999 
3000  return result;
3001 }
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:97
@ RELOPT_OTHER_MEMBER_REL
Definition: pathnodes.h:814
#define NIL
Definition: pg_list.h:68
Relids find_childrel_parents(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:1494
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:46

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,
SpecialJoinInfo sjinfo 
)

Definition at line 1381 of file equivclass.c.

1386 {
1387  List *result = NIL;
1388  Relids inner_relids = inner_rel->relids;
1389  Relids nominal_inner_relids;
1390  Relids nominal_join_relids;
1391  Bitmapset *matching_ecs;
1392  int i;
1393 
1394  /* If inner rel is a child, extra setup work is needed */
1395  if (IS_OTHER_REL(inner_rel))
1396  {
1397  Assert(!bms_is_empty(inner_rel->top_parent_relids));
1398 
1399  /* Fetch relid set for the topmost parent rel */
1400  nominal_inner_relids = inner_rel->top_parent_relids;
1401  /* ECs will be marked with the parent's relid, not the child's */
1402  nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1403  nominal_join_relids = add_outer_joins_to_relids(root,
1404  nominal_join_relids,
1405  sjinfo,
1406  NULL);
1407  }
1408  else
1409  {
1410  nominal_inner_relids = inner_relids;
1411  nominal_join_relids = join_relids;
1412  }
1413 
1414  /*
1415  * Examine all potentially-relevant eclasses.
1416  *
1417  * If we are considering an outer join, we must include "join" clauses
1418  * that mention either input rel plus the outer join's relid; these
1419  * represent post-join filter clauses that have to be applied at this
1420  * join. We don't have infrastructure that would let us identify such
1421  * eclasses cheaply, so just fall back to considering all eclasses
1422  * mentioning anything in nominal_join_relids.
1423  *
1424  * At inner joins, we can be smarter: only consider eclasses mentioning
1425  * both input rels.
1426  */
1427  if (sjinfo && sjinfo->ojrelid != 0)
1428  matching_ecs = get_eclass_indexes_for_relids(root, nominal_join_relids);
1429  else
1430  matching_ecs = get_common_eclass_indexes(root, nominal_inner_relids,
1431  outer_relids);
1432 
1433  i = -1;
1434  while ((i = bms_next_member(matching_ecs, i)) >= 0)
1435  {
1437  List *sublist = NIL;
1438 
1439  /* ECs containing consts do not need any further enforcement */
1440  if (ec->ec_has_const)
1441  continue;
1442 
1443  /* Single-member ECs won't generate any deductions */
1444  if (list_length(ec->ec_members) <= 1)
1445  continue;
1446 
1447  /* Sanity check that this eclass overlaps the join */
1448  Assert(bms_overlap(ec->ec_relids, nominal_join_relids));
1449 
1450  if (!ec->ec_broken)
1452  ec,
1453  join_relids,
1454  outer_relids,
1455  inner_relids);
1456 
1457  /* Recover if we failed to generate required derived clauses */
1458  if (ec->ec_broken)
1460  ec,
1461  nominal_join_relids,
1462  outer_relids,
1463  nominal_inner_relids,
1464  inner_rel);
1465 
1466  result = list_concat(result, sublist);
1467  }
1468 
1469  return result;
1470 }
static List * generate_join_implied_equalities_normal(PlannerInfo *root, EquivalenceClass *ec, Relids join_relids, Relids outer_relids, Relids inner_relids)
Definition: equivclass.c:1552
static Bitmapset * get_common_eclass_indexes(PlannerInfo *root, Relids relids1, Relids relids2)
Definition: equivclass.c:3287
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:1728
Relids add_outer_joins_to_relids(PlannerInfo *root, Relids input_relids, SpecialJoinInfo *sjinfo, List **pushed_down_joins)
Definition: joinrels.c:783
List * list_concat(List *list1, const List *list2)
Definition: list.c:560

References add_outer_joins_to_relids(), 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(), get_eclass_indexes_for_relids(), i, IS_OTHER_REL, list_concat(), list_length(), list_nth(), NIL, SpecialJoinInfo::ojrelid, 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 1728 of file equivclass.c.

1734 {
1735  List *result = NIL;
1736  ListCell *lc;
1737 
1738  foreach(lc, ec->ec_sources)
1739  {
1740  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1741  Relids clause_relids = restrictinfo->required_relids;
1742 
1743  if (bms_is_subset(clause_relids, nominal_join_relids) &&
1744  !bms_is_subset(clause_relids, outer_relids) &&
1745  !bms_is_subset(clause_relids, nominal_inner_relids))
1746  result = lappend(result, restrictinfo);
1747  }
1748 
1749  /*
1750  * If we have to translate, just brute-force apply adjust_appendrel_attrs
1751  * to all the RestrictInfos at once. This will result in returning
1752  * RestrictInfos that are not listed in ec_derives, but there shouldn't be
1753  * any duplication, and it's a sufficiently narrow corner case that we
1754  * shouldn't sweat too much over it anyway.
1755  *
1756  * Since inner_rel might be an indirect descendant of the baserel
1757  * mentioned in the ec_sources clauses, we have to be prepared to apply
1758  * multiple levels of Var translation.
1759  */
1760  if (IS_OTHER_REL(inner_rel) && result != NIL)
1761  result = (List *) adjust_appendrel_attrs_multilevel(root,
1762  (Node *) result,
1763  inner_rel,
1764  inner_rel->top_parent);
1765 
1766  return result;
1767 }

References adjust_appendrel_attrs_multilevel(), bms_is_subset(), EquivalenceClass::ec_sources, IS_OTHER_REL, lappend(), lfirst, NIL, and RestrictInfo::required_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 1481 of file equivclass.c.

1486 {
1487  List *result = NIL;
1488  Relids inner_relids = inner_rel->relids;
1489  Relids nominal_inner_relids;
1490  Relids nominal_join_relids;
1491  ListCell *lc;
1492 
1493  /* If inner rel is a child, extra setup work is needed */
1494  if (IS_OTHER_REL(inner_rel))
1495  {
1496  Assert(!bms_is_empty(inner_rel->top_parent_relids));
1497 
1498  /* Fetch relid set for the topmost parent rel */
1499  nominal_inner_relids = inner_rel->top_parent_relids;
1500  /* ECs will be marked with the parent's relid, not the child's */
1501  nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1502  }
1503  else
1504  {
1505  nominal_inner_relids = inner_relids;
1506  nominal_join_relids = join_relids;
1507  }
1508 
1509  foreach(lc, eclasses)
1510  {
1512  List *sublist = NIL;
1513 
1514  /* ECs containing consts do not need any further enforcement */
1515  if (ec->ec_has_const)
1516  continue;
1517 
1518  /* Single-member ECs won't generate any deductions */
1519  if (list_length(ec->ec_members) <= 1)
1520  continue;
1521 
1522  /* We can quickly ignore any that don't overlap the join, too */
1523  if (!bms_overlap(ec->ec_relids, nominal_join_relids))
1524  continue;
1525 
1526  if (!ec->ec_broken)
1528  ec,
1529  join_relids,
1530  outer_relids,
1531  inner_relids);
1532 
1533  /* Recover if we failed to generate required derived clauses */
1534  if (ec->ec_broken)
1536  ec,
1537  nominal_join_relids,
1538  outer_relids,
1539  nominal_inner_relids,
1540  inner_rel);
1541 
1542  result = list_concat(result, sublist);
1543  }
1544 
1545  return result;
1546 }

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

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

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

3288 {
3289  Bitmapset *rel1ecs;
3290  Bitmapset *rel2ecs;
3291  int relid;
3292 
3293  rel1ecs = get_eclass_indexes_for_relids(root, relids1);
3294 
3295  /*
3296  * We can get away with just using the relation's eclass_indexes directly
3297  * when relids2 is a singleton set.
3298  */
3299  if (bms_get_singleton_member(relids2, &relid))
3300  rel2ecs = root->simple_rel_array[relid]->eclass_indexes;
3301  else
3302  rel2ecs = get_eclass_indexes_for_relids(root, relids2);
3303 
3304  /* Calculate and return the common EC indexes, recycling the left input. */
3305  return bms_int_members(rel1ecs, rel2ecs);
3306 }
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:951

References bms_get_singleton_member(), bms_int_members(), and get_eclass_indexes_for_relids().

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,
List opfamilies,
Oid  opcintype,
Oid  collation,
Index  sortref,
Relids  rel,
bool  create_it 
)

Definition at line 587 of file equivclass.c.

595 {
596  JoinDomain *jdomain;
597  Relids expr_relids;
598  EquivalenceClass *newec;
599  EquivalenceMember *newem;
600  ListCell *lc1;
601  MemoryContext oldcontext;
602 
603  /*
604  * Ensure the expression exposes the correct type and collation.
605  */
606  expr = canonicalize_ec_expression(expr, opcintype, collation);
607 
608  /*
609  * Since SortGroupClause nodes are top-level expressions (GROUP BY, ORDER
610  * BY, etc), they can be presumed to belong to the top JoinDomain.
611  */
612  jdomain = linitial_node(JoinDomain, root->join_domains);
613 
614  /*
615  * Scan through the existing EquivalenceClasses for a match
616  */
617  foreach(lc1, root->eq_classes)
618  {
619  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
620  ListCell *lc2;
621 
622  /*
623  * Never match to a volatile EC, except when we are looking at another
624  * reference to the same volatile SortGroupClause.
625  */
626  if (cur_ec->ec_has_volatile &&
627  (sortref == 0 || sortref != cur_ec->ec_sortref))
628  continue;
629 
630  if (collation != cur_ec->ec_collation)
631  continue;
632  if (!equal(opfamilies, cur_ec->ec_opfamilies))
633  continue;
634 
635  foreach(lc2, cur_ec->ec_members)
636  {
637  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
638 
639  /*
640  * Ignore child members unless they match the request.
641  */
642  if (cur_em->em_is_child &&
643  !bms_equal(cur_em->em_relids, rel))
644  continue;
645 
646  /*
647  * Match constants only within the same JoinDomain (see
648  * optimizer/README).
649  */
650  if (cur_em->em_is_const && cur_em->em_jdomain != jdomain)
651  continue;
652 
653  if (opcintype == cur_em->em_datatype &&
654  equal(expr, cur_em->em_expr))
655  return cur_ec; /* Match! */
656  }
657  }
658 
659  /* No match; does caller want a NULL result? */
660  if (!create_it)
661  return NULL;
662 
663  /*
664  * OK, build a new single-member EC
665  *
666  * Here, we must be sure that we construct the EC in the right context.
667  */
668  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
669 
670  newec = makeNode(EquivalenceClass);
671  newec->ec_opfamilies = list_copy(opfamilies);
672  newec->ec_collation = collation;
673  newec->ec_members = NIL;
674  newec->ec_sources = NIL;
675  newec->ec_derives = NIL;
676  newec->ec_relids = NULL;
677  newec->ec_has_const = false;
679  newec->ec_broken = false;
680  newec->ec_sortref = sortref;
681  newec->ec_min_security = UINT_MAX;
682  newec->ec_max_security = 0;
683  newec->ec_merged = NULL;
684 
685  if (newec->ec_has_volatile && sortref == 0) /* should not happen */
686  elog(ERROR, "volatile EquivalenceClass has no sortref");
687 
688  /*
689  * Get the precise set of relids appearing in the expression.
690  */
691  expr_relids = pull_varnos(root, (Node *) expr);
692 
693  newem = add_eq_member(newec, copyObject(expr), expr_relids,
694  jdomain, NULL, opcintype);
695 
696  /*
697  * add_eq_member doesn't check for volatile functions, set-returning
698  * functions, aggregates, or window functions, but such could appear in
699  * sort expressions; so we have to check whether its const-marking was
700  * correct.
701  */
702  if (newec->ec_has_const)
703  {
704  if (newec->ec_has_volatile ||
705  expression_returns_set((Node *) expr) ||
706  contain_agg_clause((Node *) expr) ||
707  contain_window_function((Node *) expr))
708  {
709  newec->ec_has_const = false;
710  newem->em_is_const = false;
711  }
712  }
713 
714  root->eq_classes = lappend(root->eq_classes, newec);
715 
716  /*
717  * If EC merging is already complete, we have to mop up by adding the new
718  * EC to the eclass_indexes of the relation(s) mentioned in it.
719  */
720  if (root->ec_merging_done)
721  {
722  int ec_index = list_length(root->eq_classes) - 1;
723  int i = -1;
724 
725  while ((i = bms_next_member(newec->ec_relids, i)) > 0)
726  {
727  RelOptInfo *rel = root->simple_rel_array[i];
728 
729  if (rel == NULL) /* must be an outer join */
730  {
732  continue;
733  }
734 
736 
738  ec_index);
739  }
740  }
741 
742  MemoryContextSwitchTo(oldcontext);
743 
744  return newec;
745 }
bool contain_agg_clause(Node *clause)
Definition: clauses.c:179
bool contain_window_function(Node *clause)
Definition: clauses.c:216
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:483
Expr * canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
Definition: equivclass.c:472
List * list_copy(const List *oldlist)
Definition: list.c:1572
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:728
#define copyObject(obj)
Definition: nodes.h:244
#define linitial_node(type, l)
Definition: pg_list.h:181
List * ec_opfamilies
Definition: pathnodes.h:1368
Index ec_max_security
Definition: pathnodes.h:1380
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:108

References add_eq_member(), Assert(), bms_add_member(), bms_equal(), bms_is_member(), bms_next_member(), canonicalize_ec_expression(), contain_agg_clause(), contain_volatile_functions(), contain_window_function(), copyObject, 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_jdomain, EquivalenceMember::em_relids, PlannerInfo::eq_classes, equal(), ERROR, expression_returns_set(), i, PlannerInfo::join_domains, lappend(), lfirst, linitial_node, list_copy(), list_length(), makeNode, MemoryContextSwitchTo(), NIL, PlannerInfo::outer_join_rels, pull_varnos(), RELOPT_BASEREL, and RelOptInfo::reloptkind.

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

3258 {
3259  Bitmapset *ec_indexes = NULL;
3260  int i = -1;
3261 
3262  /* Should be OK to rely on eclass_indexes */
3263  Assert(root->ec_merging_done);
3264 
3265  while ((i = bms_next_member(relids, i)) > 0)
3266  {
3267  RelOptInfo *rel = root->simple_rel_array[i];
3268 
3269  if (rel == NULL) /* must be an outer join */
3270  {
3272  continue;
3273  }
3274 
3275  ec_indexes = bms_add_members(ec_indexes, rel->eclass_indexes);
3276  }
3277  return ec_indexes;
3278 }

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

Referenced by add_child_join_rel_equivalences(), generate_join_implied_equalities(), 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 3092 of file equivclass.c.

3093 {
3094  Bitmapset *matched_ecs;
3095  int i;
3096 
3097  /* Examine only eclasses mentioning rel1 */
3098  matched_ecs = get_eclass_indexes_for_relids(root, rel1->relids);
3099 
3100  i = -1;
3101  while ((i = bms_next_member(matched_ecs, i)) >= 0)
3102  {
3104  i);
3105 
3106  /*
3107  * Won't generate joinclauses if single-member (this test covers the
3108  * volatile case too)
3109  */
3110  if (list_length(ec->ec_members) <= 1)
3111  continue;
3112 
3113  /*
3114  * Per the comment in have_relevant_eclass_joinclause, it's sufficient
3115  * to find an EC that mentions both this rel and some other rel.
3116  */
3117  if (!bms_is_subset(ec->ec_relids, rel1->relids))
3118  return true;
3119  }
3120 
3121  return false;
3122 }

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

3018 {
3019  Bitmapset *matching_ecs;
3020  int i;
3021 
3022  /*
3023  * Examine only eclasses mentioning both rel1 and rel2.
3024  *
3025  * Note that we do not consider the possibility of an eclass generating
3026  * "join" clauses that mention just one of the rels plus an outer join
3027  * that could be formed from them. Although such clauses must be
3028  * correctly enforced when we form the outer join, they don't seem like
3029  * sufficient reason to prioritize this join over other ones. The join
3030  * ordering rules will force the join to be made when necessary.
3031  */
3032  matching_ecs = get_common_eclass_indexes(root, rel1->relids,
3033  rel2->relids);
3034 
3035  i = -1;
3036  while ((i = bms_next_member(matching_ecs, i)) >= 0)
3037  {
3039  i);
3040 
3041  /*
3042  * Sanity check that get_common_eclass_indexes gave only ECs
3043  * containing both rels.
3044  */
3045  Assert(bms_overlap(rel1->relids, ec->ec_relids));
3046  Assert(bms_overlap(rel2->relids, ec->ec_relids));
3047 
3048  /*
3049  * Won't generate joinclauses if single-member (this test covers the
3050  * volatile case too)
3051  */
3052  if (list_length(ec->ec_members) <= 1)
3053  continue;
3054 
3055  /*
3056  * We do not need to examine the individual members of the EC, because
3057  * all that we care about is whether each rel overlaps the relids of
3058  * at least one member, and get_common_eclass_indexes() and the single
3059  * member check above are sufficient to prove that. (As with
3060  * have_relevant_joinclause(), it is not necessary that the EC be able
3061  * to form a joinclause relating exactly the two given rels, only that
3062  * it be able to form a joinclause mentioning both, and this will
3063  * surely be true if both of them overlap ec_relids.)
3064  *
3065  * Note we don't test ec_broken; if we did, we'd need a separate code
3066  * path to look through ec_sources. Checking the membership anyway is
3067  * OK as a possibly-overoptimistic heuristic.
3068  *
3069  * We don't test ec_has_const either, even though a const eclass won't
3070  * generate real join clauses. This is because if we had "WHERE a.x =
3071  * b.y and a.x = 42", it is worth considering a join between a and b,
3072  * since the join result is likely to be small even though it'll end
3073  * up being an unqualified nestloop.
3074  */
3075 
3076  return true;
3077  }
3078 
3079  return false;
3080 }

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

892 {
893  ListCell *lc;
894 
895  foreach(lc, exprs)
896  {
897  Expr *expr = (Expr *) lfirst(lc);
898 
899  if (expr && IsA(expr, TargetEntry))
900  expr = ((TargetEntry *) expr)->expr;
901 
902  if (equal(node, expr))
903  return true;
904  }
905  return false;
906 }

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

3195 {
3196  EquivalenceClass *parent_ec = rinfo->parent_ec;
3197  ListCell *lc;
3198 
3199  /* Fail if it's not a potentially-redundant clause from some EC */
3200  if (parent_ec == NULL)
3201  return false;
3202 
3203  foreach(lc, clauselist)
3204  {
3205  RestrictInfo *otherrinfo = (RestrictInfo *) lfirst(lc);
3206 
3207  if (otherrinfo->parent_ec == parent_ec)
3208  return true;
3209  }
3210 
3211  return false;
3212 }

References lfirst.

Referenced by create_tidscan_plan().

◆ is_redundant_with_indexclauses()

bool is_redundant_with_indexclauses ( RestrictInfo rinfo,
List indexclauses 
)

Definition at line 3221 of file equivclass.c.

3222 {
3223  EquivalenceClass *parent_ec = rinfo->parent_ec;
3224  ListCell *lc;
3225 
3226  foreach(lc, indexclauses)
3227  {
3228  IndexClause *iclause = lfirst_node(IndexClause, lc);
3229  RestrictInfo *otherrinfo = iclause->rinfo;
3230 
3231  /* If indexclause is lossy, it won't enforce the condition exactly */
3232  if (iclause->lossy)
3233  continue;
3234 
3235  /* Match if it's same clause (pointer equality should be enough) */
3236  if (rinfo == otherrinfo)
3237  return true;
3238  /* Match if derived from same EC */
3239  if (parent_ec && otherrinfo->parent_ec == parent_ec)
3240  return true;
3241 
3242  /*
3243  * No need to look at the derived clauses in iclause->indexquals; they
3244  * couldn't match if the parent clause didn't.
3245  */
3246  }
3247 
3248  return false;
3249 }
#define lfirst_node(type, lc)
Definition: pg_list.h:176
struct RestrictInfo * rinfo
Definition: pathnodes.h:1726

References lfirst_node, IndexClause::lossy, 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 2490 of file equivclass.c.

2493 {
2494  Index var1varno = fkinfo->con_relid;
2495  AttrNumber var1attno = fkinfo->conkey[colno];
2496  Index var2varno = fkinfo->ref_relid;
2497  AttrNumber var2attno = fkinfo->confkey[colno];
2498  Oid eqop = fkinfo->conpfeqop[colno];
2499  RelOptInfo *rel1 = root->simple_rel_array[var1varno];
2500  RelOptInfo *rel2 = root->simple_rel_array[var2varno];
2501  List *opfamilies = NIL; /* compute only if needed */
2502  Bitmapset *matching_ecs;
2503  int i;
2504 
2505  /* Consider only eclasses mentioning both relations */
2506  Assert(root->ec_merging_done);
2507  Assert(IS_SIMPLE_REL(rel1));
2508  Assert(IS_SIMPLE_REL(rel2));
2509  matching_ecs = bms_intersect(rel1->eclass_indexes,
2510  rel2->eclass_indexes);
2511 
2512  i = -1;
2513  while ((i = bms_next_member(matching_ecs, i)) >= 0)
2514  {
2516  i);
2517  EquivalenceMember *item1_em = NULL;
2518  EquivalenceMember *item2_em = NULL;
2519  ListCell *lc2;
2520 
2521  /* Never match to a volatile EC */
2522  if (ec->ec_has_volatile)
2523  continue;
2524  /* Note: it seems okay to match to "broken" eclasses here */
2525 
2526  foreach(lc2, ec->ec_members)
2527  {
2529  Var *var;
2530 
2531  if (em->em_is_child)
2532  continue; /* ignore children here */
2533 
2534  /* EM must be a Var, possibly with RelabelType */
2535  var = (Var *) em->em_expr;
2536  while (var && IsA(var, RelabelType))
2537  var = (Var *) ((RelabelType *) var)->arg;
2538  if (!(var && IsA(var, Var)))
2539  continue;
2540 
2541  /* Match? */
2542  if (var->varno == var1varno && var->varattno == var1attno)
2543  item1_em = em;
2544  else if (var->varno == var2varno && var->varattno == var2attno)
2545  item2_em = em;
2546 
2547  /* Have we found both PK and FK column in this EC? */
2548  if (item1_em && item2_em)
2549  {
2550  /*
2551  * Succeed if eqop matches EC's opfamilies. We could test
2552  * this before scanning the members, but it's probably cheaper
2553  * to test for member matches first.
2554  */
2555  if (opfamilies == NIL) /* compute if we didn't already */
2556  opfamilies = get_mergejoin_opfamilies(eqop);
2557  if (equal(opfamilies, ec->ec_opfamilies))
2558  {
2559  fkinfo->eclass[colno] = ec;
2560  fkinfo->fk_eclass_member[colno] = item2_em;
2561  return ec;
2562  }
2563  /* Otherwise, done with this EC, move on to the next */
2564  break;
2565  }
2566  }
2567  }
2568  return NULL;
2569 }
int16 AttrNumber
Definition: attnum.h:21
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:248
unsigned int Index
Definition: c.h:603
List * get_mergejoin_opfamilies(Oid opno)
Definition: lsyscache.c:365
while(p+4<=pend)
struct EquivalenceClass * eclass[INDEX_MAX_KEYS]
Definition: pathnodes.h:1235
struct EquivalenceMember * fk_eclass_member[INDEX_MAX_KEYS]
Definition: pathnodes.h:1237
AttrNumber varattno
Definition: primnodes.h:238
int varno
Definition: primnodes.h:233

References Assert(), bms_intersect(), bms_next_member(), ForeignKeyOptInfo::con_relid, 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, Var::varattno, Var::varno, and while().

Referenced by match_foreign_keys_to_quals().

◆ process_equivalence()

bool process_equivalence ( PlannerInfo root,
RestrictInfo **  p_restrictinfo,
JoinDomain jdomain 
)

Definition at line 118 of file equivclass.c.

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

References add_eq_member(), NullTest::arg, Assert(), bms_join(), canonicalize_ec_expression(), RestrictInfo::clause, 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, EquivalenceMember::em_jdomain, PlannerInfo::eq_classes, equal(), ERROR, exprType(), foreach_current_index, func_strict(), get_leftop(), get_rightop(), RestrictInfo::has_clone, RestrictInfo::incompatible_relids, RestrictInfo::is_clone, IS_NOT_NULL, is_opclause(), RestrictInfo::is_pushed_down, lappend(), lfirst, list_concat(), list_delete_nth_cell(), list_make1, NullTest::location, make_restrictinfo(), makeNode, Max, Min, NIL, NullTest::nulltesttype, op_input_types(), RestrictInfo::outer_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,
OuterJoinClauseInfo ojcinfo 
)
static

Definition at line 2227 of file equivclass.c.

2228 {
2229  RestrictInfo *rinfo = ojcinfo->rinfo;
2230  SpecialJoinInfo *sjinfo = ojcinfo->sjinfo;
2231  Relids fjrelids = bms_make_singleton(sjinfo->ojrelid);
2232  Expr *leftvar;
2233  Expr *rightvar;
2234  Oid opno,
2235  collation,
2236  left_type,
2237  right_type;
2238  Relids left_relids,
2239  right_relids;
2240  ListCell *lc1;
2241 
2242  /* Extract needed info from the clause */
2243  Assert(is_opclause(rinfo->clause));
2244  opno = ((OpExpr *) rinfo->clause)->opno;
2245  collation = ((OpExpr *) rinfo->clause)->inputcollid;
2246  op_input_types(opno, &left_type, &right_type);
2247  leftvar = (Expr *) get_leftop(rinfo->clause);
2248  rightvar = (Expr *) get_rightop(rinfo->clause);
2249  left_relids = rinfo->left_relids;
2250  right_relids = rinfo->right_relids;
2251 
2252  foreach(lc1, root->eq_classes)
2253  {
2254  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
2255  EquivalenceMember *coal_em = NULL;
2256  bool match;
2257  bool matchleft;
2258  bool matchright;
2259  ListCell *lc2;
2260  int coal_idx = -1;
2261 
2262  /* Ignore EC unless it contains pseudoconstants */
2263  if (!cur_ec->ec_has_const)
2264  continue;
2265  /* Never match to a volatile EC */
2266  if (cur_ec->ec_has_volatile)
2267  continue;
2268  /* It has to match the outer-join clause as to semantics, too */
2269  if (collation != cur_ec->ec_collation)
2270  continue;
2271  if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
2272  continue;
2273 
2274  /*
2275  * Does it contain a COALESCE(leftvar, rightvar) construct?
2276  *
2277  * We can assume the COALESCE() inputs are in the same order as the
2278  * join clause, since both were automatically generated in the cases
2279  * we care about.
2280  *
2281  * XXX currently this may fail to match in cross-type cases because
2282  * the COALESCE will contain typecast operations while the join clause
2283  * may not (if there is a cross-type mergejoin operator available for
2284  * the two column types). Is it OK to strip implicit coercions from
2285  * the COALESCE arguments?
2286  */
2287  match = false;
2288  foreach(lc2, cur_ec->ec_members)
2289  {
2290  coal_em = (EquivalenceMember *) lfirst(lc2);
2291  Assert(!coal_em->em_is_child); /* no children yet */
2292  if (IsA(coal_em->em_expr, CoalesceExpr))
2293  {
2294  CoalesceExpr *cexpr = (CoalesceExpr *) coal_em->em_expr;
2295  Node *cfirst;
2296  Node *csecond;
2297 
2298  if (list_length(cexpr->args) != 2)
2299  continue;
2300  cfirst = (Node *) linitial(cexpr->args);
2301  csecond = (Node *) lsecond(cexpr->args);
2302 
2303  /*
2304  * The COALESCE arguments will be marked as possibly nulled by
2305  * the full join, while we wish to generate clauses that apply
2306  * to the join's inputs. So we must strip the join from the
2307  * nullingrels fields of cfirst/csecond before comparing them
2308  * to leftvar/rightvar. (Perhaps with a less hokey
2309  * representation for FULL JOIN USING output columns, this
2310  * wouldn't be needed?)
2311  */
2312  cfirst = remove_nulling_relids(cfirst, fjrelids, NULL);
2313  csecond = remove_nulling_relids(csecond, fjrelids, NULL);
2314 
2315  if (equal(leftvar, cfirst) && equal(rightvar, csecond))
2316  {
2317  coal_idx = foreach_current_index(lc2);
2318  match = true;
2319  break;
2320  }
2321  }
2322  }
2323  if (!match)
2324  continue; /* no match, so ignore this EC */
2325 
2326  /*
2327  * Yes it does! Try to generate clauses LEFTVAR = CONSTANT and
2328  * RIGHTVAR = CONSTANT for each CONSTANT in the EC. Note that we must
2329  * succeed with at least one constant for each var before we can
2330  * decide to throw away the outer-join clause.
2331  */
2332  matchleft = matchright = false;
2333  foreach(lc2, cur_ec->ec_members)
2334  {
2335  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2336  Oid eq_op;
2337  RestrictInfo *newrinfo;
2338  JoinDomain *jdomain;
2339 
2340  if (!cur_em->em_is_const)
2341  continue; /* ignore non-const members */
2342  eq_op = select_equality_operator(cur_ec,
2343  left_type,
2344  cur_em->em_datatype);
2345  if (OidIsValid(eq_op))
2346  {
2347  newrinfo = build_implied_join_equality(root,
2348  eq_op,
2349  cur_ec->ec_collation,
2350  leftvar,
2351  cur_em->em_expr,
2352  bms_copy(left_relids),
2353  cur_ec->ec_min_security);
2354  /* This equality holds within the lefthand child JoinDomain */
2355  jdomain = find_join_domain(root, sjinfo->syn_lefthand);
2356  if (process_equivalence(root, &newrinfo, jdomain))
2357  matchleft = true;
2358  }
2359  eq_op = select_equality_operator(cur_ec,
2360  right_type,
2361  cur_em->em_datatype);
2362  if (OidIsValid(eq_op))
2363  {
2364  newrinfo = build_implied_join_equality(root,
2365  eq_op,
2366  cur_ec->ec_collation,
2367  rightvar,
2368  cur_em->em_expr,
2369  bms_copy(right_relids),
2370  cur_ec->ec_min_security);
2371  /* This equality holds within the righthand child JoinDomain */
2372  jdomain = find_join_domain(root, sjinfo->syn_righthand);
2373  if (process_equivalence(root, &newrinfo, jdomain))
2374  matchright = true;
2375  }
2376  }
2377 
2378  /*
2379  * If we were able to equate both vars to constants, we're done, and
2380  * we can throw away the full-join clause as redundant. Moreover, we
2381  * can remove the COALESCE entry from the EC, since the added
2382  * restrictions ensure it will always have the expected value. (We
2383  * don't bother trying to update ec_relids or ec_sources.)
2384  */
2385  if (matchleft && matchright)
2386  {
2387  cur_ec->ec_members = list_delete_nth_cell(cur_ec->ec_members, coal_idx);
2388  return true;
2389  }
2390 
2391  /*
2392  * Otherwise, fall out of the search loop, since we know the COALESCE
2393  * appears in at most one EC (XXX might stop being true if we allow
2394  * stripping of coercions above?)
2395  */
2396  break;
2397  }
2398 
2399  return false; /* failed to make any deduction */
2400 }
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:171
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:80
static JoinDomain * find_join_domain(PlannerInfo *root, Relids relids)
Definition: equivclass.c:2410
bool process_equivalence(PlannerInfo *root, RestrictInfo **p_restrictinfo, JoinDomain *jdomain)
Definition: equivclass.c:118
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
#define lsecond(l)
Definition: pg_list.h:183
Node * remove_nulling_relids(Node *node, const Bitmapset *removable_relids, const Bitmapset *except_relids)
List * args
Definition: primnodes.h:1419
RestrictInfo * rinfo
Definition: pathnodes.h:2882
SpecialJoinInfo * sjinfo
Definition: pathnodes.h:2883
Relids syn_lefthand
Definition: pathnodes.h:2855
Relids syn_righthand
Definition: pathnodes.h:2856

References CoalesceExpr::args, Assert(), bms_copy(), bms_make_singleton(), 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(), find_join_domain(), foreach_current_index, get_leftop(), get_rightop(), if(), is_opclause(), IsA, lfirst, linitial, list_delete_nth_cell(), list_length(), lsecond, OidIsValid, SpecialJoinInfo::ojrelid, op_input_types(), process_equivalence(), remove_nulling_relids(), OuterJoinClauseInfo::rinfo, select_equality_operator(), OuterJoinClauseInfo::sjinfo, SpecialJoinInfo::syn_lefthand, and SpecialJoinInfo::syn_righthand.

Referenced by reconsider_outer_join_clauses().

◆ reconsider_outer_join_clause()

static bool reconsider_outer_join_clause ( PlannerInfo root,
OuterJoinClauseInfo ojcinfo,
bool  outer_on_left 
)
static

Definition at line 2104 of file equivclass.c.

2106 {
2107  RestrictInfo *rinfo = ojcinfo->rinfo;
2108  SpecialJoinInfo *sjinfo = ojcinfo->sjinfo;
2109  Expr *outervar,
2110  *innervar;
2111  Oid opno,
2112  collation,
2113  left_type,
2114  right_type,
2115  inner_datatype;
2116  Relids inner_relids;
2117  ListCell *lc1;
2118 
2119  Assert(is_opclause(rinfo->clause));
2120  opno = ((OpExpr *) rinfo->clause)->opno;
2121  collation = ((OpExpr *) rinfo->clause)->inputcollid;
2122 
2123  /* Extract needed info from the clause */
2124  op_input_types(opno, &left_type, &right_type);
2125  if (outer_on_left)
2126  {
2127  outervar = (Expr *) get_leftop(rinfo->clause);
2128  innervar = (Expr *) get_rightop(rinfo->clause);
2129  inner_datatype = right_type;
2130  inner_relids = rinfo->right_relids;
2131  }
2132  else
2133  {
2134  outervar = (Expr *) get_rightop(rinfo->clause);
2135  innervar = (Expr *) get_leftop(rinfo->clause);
2136  inner_datatype = left_type;
2137  inner_relids = rinfo->left_relids;
2138  }
2139 
2140  /* Scan EquivalenceClasses for a match to outervar */
2141  foreach(lc1, root->eq_classes)
2142  {
2143  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
2144  bool match;
2145  ListCell *lc2;
2146 
2147  /* Ignore EC unless it contains pseudoconstants */
2148  if (!cur_ec->ec_has_const)
2149  continue;
2150  /* Never match to a volatile EC */
2151  if (cur_ec->ec_has_volatile)
2152  continue;
2153  /* It has to match the outer-join clause as to semantics, too */
2154  if (collation != cur_ec->ec_collation)
2155  continue;
2156  if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
2157  continue;
2158  /* Does it contain a match to outervar? */
2159  match = false;
2160  foreach(lc2, cur_ec->ec_members)
2161  {
2162  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2163 
2164  Assert(!cur_em->em_is_child); /* no children yet */
2165  if (equal(outervar, cur_em->em_expr))
2166  {
2167  match = true;
2168  break;
2169  }
2170  }
2171  if (!match)
2172  continue; /* no match, so ignore this EC */
2173 
2174  /*
2175  * Yes it does! Try to generate a clause INNERVAR = CONSTANT for each
2176  * CONSTANT in the EC. Note that we must succeed with at least one
2177  * constant before we can decide to throw away the outer-join clause.
2178  */
2179  match = false;
2180  foreach(lc2, cur_ec->ec_members)
2181  {
2182  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2183  Oid eq_op;
2184  RestrictInfo *newrinfo;
2185  JoinDomain *jdomain;
2186 
2187  if (!cur_em->em_is_const)
2188  continue; /* ignore non-const members */
2189  eq_op = select_equality_operator(cur_ec,
2190  inner_datatype,
2191  cur_em->em_datatype);
2192  if (!OidIsValid(eq_op))
2193  continue; /* can't generate equality */
2194  newrinfo = build_implied_join_equality(root,
2195  eq_op,
2196  cur_ec->ec_collation,
2197  innervar,
2198  cur_em->em_expr,
2199  bms_copy(inner_relids),
2200  cur_ec->ec_min_security);
2201  /* This equality holds within the OJ's child JoinDomain */
2202  jdomain = find_join_domain(root, sjinfo->syn_righthand);
2203  if (process_equivalence(root, &newrinfo, jdomain))
2204  match = true;
2205  }
2206 
2207  /*
2208  * If we were able to equate INNERVAR to any constant, report success.
2209  * Otherwise, fall out of the search loop, since we know the OUTERVAR
2210  * appears in at most one EC.
2211  */
2212  if (match)
2213  return true;
2214  else
2215  break;
2216  }
2217 
2218  return false; /* failed to make any deduction */
2219 }

References Assert(), bms_copy(), 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(), find_join_domain(), get_leftop(), get_rightop(), is_opclause(), lfirst, OidIsValid, op_input_types(), process_equivalence(), OuterJoinClauseInfo::rinfo, select_equality_operator(), OuterJoinClauseInfo::sjinfo, and SpecialJoinInfo::syn_righthand.

Referenced by reconsider_outer_join_clauses().

◆ reconsider_outer_join_clauses()

void reconsider_outer_join_clauses ( PlannerInfo root)

Definition at line 1982 of file equivclass.c.

1983 {
1984  bool found;
1985  ListCell *cell;
1986 
1987  /* Outer loop repeats until we find no more deductions */
1988  do
1989  {
1990  found = false;
1991 
1992  /* Process the LEFT JOIN clauses */
1993  foreach(cell, root->left_join_clauses)
1994  {
1995  OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
1996 
1997  if (reconsider_outer_join_clause(root, ojcinfo, true))
1998  {
1999  RestrictInfo *rinfo = ojcinfo->rinfo;
2000 
2001  found = true;
2002  /* remove it from the list */
2003  root->left_join_clauses =
2005  /* throw back a dummy replacement clause (see notes above) */
2006  rinfo = make_restrictinfo(root,
2007  (Expr *) makeBoolConst(true, false),
2008  rinfo->is_pushed_down,
2009  rinfo->has_clone,
2010  rinfo->is_clone,
2011  false, /* pseudoconstant */
2012  0, /* security_level */
2013  rinfo->required_relids,
2014  rinfo->incompatible_relids,
2015  rinfo->outer_relids);
2016  distribute_restrictinfo_to_rels(root, rinfo);
2017  }
2018  }
2019 
2020  /* Process the RIGHT JOIN clauses */
2021  foreach(cell, root->right_join_clauses)
2022  {
2023  OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
2024 
2025  if (reconsider_outer_join_clause(root, ojcinfo, false))
2026  {
2027  RestrictInfo *rinfo = ojcinfo->rinfo;
2028 
2029  found = true;
2030  /* remove it from the list */
2031  root->right_join_clauses =
2033  /* throw back a dummy replacement clause (see notes above) */
2034  rinfo = make_restrictinfo(root,
2035  (Expr *) makeBoolConst(true, false),
2036  rinfo->is_pushed_down,
2037  rinfo->has_clone,
2038  rinfo->is_clone,
2039  false, /* pseudoconstant */
2040  0, /* security_level */
2041  rinfo->required_relids,
2042  rinfo->incompatible_relids,
2043  rinfo->outer_relids);
2044  distribute_restrictinfo_to_rels(root, rinfo);
2045  }
2046  }
2047 
2048  /* Process the FULL JOIN clauses */
2049  foreach(cell, root->full_join_clauses)
2050  {
2051  OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
2052 
2053  if (reconsider_full_join_clause(root, ojcinfo))
2054  {
2055  RestrictInfo *rinfo = ojcinfo->rinfo;
2056 
2057  found = true;
2058  /* remove it from the list */
2059  root->full_join_clauses =
2061  /* throw back a dummy replacement clause (see notes above) */
2062  rinfo = make_restrictinfo(root,
2063  (Expr *) makeBoolConst(true, false),
2064  rinfo->is_pushed_down,
2065  rinfo->has_clone,
2066  rinfo->is_clone,
2067  false, /* pseudoconstant */
2068  0, /* security_level */
2069  rinfo->required_relids,
2070  rinfo->incompatible_relids,
2071  rinfo->outer_relids);
2072  distribute_restrictinfo_to_rels(root, rinfo);
2073  }
2074  }
2075  } while (found);
2076 
2077  /* Now, any remaining clauses have to be thrown back */
2078  foreach(cell, root->left_join_clauses)
2079  {
2080  OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
2081 
2082  distribute_restrictinfo_to_rels(root, ojcinfo->rinfo);
2083  }
2084  foreach(cell, root->right_join_clauses)
2085  {
2086  OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
2087 
2088  distribute_restrictinfo_to_rels(root, ojcinfo->rinfo);
2089  }
2090  foreach(cell, root->full_join_clauses)
2091  {
2092  OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
2093 
2094  distribute_restrictinfo_to_rels(root, ojcinfo->rinfo);
2095  }
2096 }
static bool reconsider_outer_join_clause(PlannerInfo *root, OuterJoinClauseInfo *ojcinfo, bool outer_on_left)
Definition: equivclass.c:2104
static bool reconsider_full_join_clause(PlannerInfo *root, OuterJoinClauseInfo *ojcinfo)
Definition: equivclass.c:2227
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:360
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:390
List * left_join_clauses
Definition: pathnodes.h:323
List * full_join_clauses
Definition: pathnodes.h:334
List * right_join_clauses
Definition: pathnodes.h:329

References distribute_restrictinfo_to_rels(), foreach_delete_current, PlannerInfo::full_join_clauses, RestrictInfo::has_clone, RestrictInfo::incompatible_relids, RestrictInfo::is_clone, RestrictInfo::is_pushed_down, PlannerInfo::left_join_clauses, lfirst, make_restrictinfo(), makeBoolConst(), RestrictInfo::outer_relids, reconsider_full_join_clause(), reconsider_outer_join_clause(), RestrictInfo::required_relids, PlannerInfo::right_join_clauses, and OuterJoinClauseInfo::rinfo.

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

924 {
925  PathTarget *target = rel->reltarget;
926  EquivalenceMember *em;
927  ListCell *lc;
928 
929  /*
930  * Reject volatile ECs immediately; such sorts must always be postponed.
931  */
932  if (ec->ec_has_volatile)
933  return false;
934 
935  /*
936  * Try to find an EM directly matching some reltarget member.
937  */
938  foreach(lc, target->exprs)
939  {
940  Expr *targetexpr = (Expr *) lfirst(lc);
941 
942  em = find_ec_member_matching_expr(ec, targetexpr, rel->relids);
943  if (!em)
944  continue;
945 
946  /*
947  * Reject expressions involving set-returning functions, as those
948  * can't be computed early either. (Note: this test and the following
949  * one are effectively checking properties of targetexpr, so there's
950  * no point in asking whether some other EC member would be better.)
951  */
952  if (expression_returns_set((Node *) em->em_expr))
953  continue;
954 
955  /*
956  * If requested, reject expressions that are not parallel-safe. We
957  * check this last because it's a rather expensive test.
958  */
959  if (require_parallel_safe &&
960  !is_parallel_safe(root, (Node *) em->em_expr))
961  continue;
962 
963  return true;
964  }
965 
966  /*
967  * Try to find an expression computable from the reltarget.
968  */
969  em = find_computable_ec_member(root, ec, target->exprs, rel->relids,
970  require_parallel_safe);
971  if (!em)
972  return false;
973 
974  /*
975  * Reject expressions involving set-returning functions, as those can't be
976  * computed early either. (There's no point in looking for another EC
977  * member in this case; since SRFs can't appear in WHERE, they cannot
978  * belong to multi-member ECs.)
979  */
980  if (expression_returns_set((Node *) em->em_expr))
981  return false;
982 
983  return true;
984 }
EquivalenceMember * find_ec_member_matching_expr(EquivalenceClass *ec, Expr *expr, Relids relids)
Definition: equivclass.c:760
EquivalenceMember * find_computable_ec_member(PlannerInfo *root, EquivalenceClass *ec, List *exprs, Relids relids, bool require_parallel_safe)
Definition: equivclass.c:825
List * exprs
Definition: pathnodes.h:1501
struct PathTarget * reltarget
Definition: pathnodes.h:878

References EquivalenceClass::ec_has_volatile, EquivalenceMember::em_expr, expression_returns_set(), PathTarget::exprs, find_computable_ec_member(), find_ec_member_matching_expr(), is_parallel_safe(), 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 1777 of file equivclass.c.

1778 {
1779  ListCell *lc;
1780 
1781  foreach(lc, ec->ec_opfamilies)
1782  {
1783  Oid opfamily = lfirst_oid(lc);
1784  Oid opno;
1785 
1786  opno = get_opfamily_member(opfamily, lefttype, righttype,
1788  if (!OidIsValid(opno))
1789  continue;
1790  /* If no barrier quals in query, don't worry about leaky operators */
1791  if (ec->ec_max_security == 0)
1792  return opno;
1793  /* Otherwise, insist that selected operators be leakproof */
1794  if (get_func_leakproof(get_opcode(opno)))
1795  return opno;
1796  }
1797  return InvalidOid;
1798 }
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1289
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:165
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:1841
#define lfirst_oid(lc)
Definition: pg_list.h:174
#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().