PostgreSQL Source Code  git master
equivclass.c File Reference
#include "postgres.h"
#include <limits.h>
#include "access/stratnum.h"
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/appendinfo.h"
#include "optimizer/clauses.h"
#include "optimizer/optimizer.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/planmain.h"
#include "optimizer/restrictinfo.h"
#include "utils/lsyscache.h"
Include dependency graph for equivclass.c:

Go to the source code of this file.

Functions

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

Function Documentation

◆ add_child_join_rel_equivalences()

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

Definition at line 2679 of file equivclass.c.

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

References add_eq_member(), adjust_appendrel_attrs(), adjust_appendrel_attrs_multilevel(), adjust_child_relids_multilevel(), Assert(), bms_add_members(), bms_difference(), bms_membership(), BMS_MULTIPLE, bms_next_member(), bms_overlap(), EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_members, EquivalenceClass::ec_relids, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, EquivalenceMember::em_nullable_relids, EquivalenceMember::em_relids, PlannerInfo::eq_classes, get_eclass_indexes_for_relids(), i, IS_JOIN_REL, list_length(), list_nth(), MemoryContextSwitchTo(), 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 2551 of file equivclass.c.

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

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

Referenced by set_append_rel_size().

◆ add_eq_member()

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

Definition at line 545 of file equivclass.c.

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

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

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

◆ canonicalize_ec_expression()

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

Definition at line 500 of file equivclass.c.

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

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

1809 {
1810  RestrictInfo *rinfo;
1811  ListCell *lc;
1812  MemoryContext oldcontext;
1813 
1814  /*
1815  * Search to see if we already built a RestrictInfo for this pair of
1816  * EquivalenceMembers. We can use either original source clauses or
1817  * previously-derived clauses, and a commutator clause is acceptable.
1818  *
1819  * We used to verify that opno matches, but that seems redundant: even if
1820  * it's not identical, it'd better have the same effects, or the operator
1821  * families we're using are broken.
1822  */
1823  foreach(lc, ec->ec_sources)
1824  {
1825  rinfo = (RestrictInfo *) lfirst(lc);
1826  if (rinfo->left_em == leftem &&
1827  rinfo->right_em == rightem &&
1828  rinfo->parent_ec == parent_ec)
1829  return rinfo;
1830  if (rinfo->left_em == rightem &&
1831  rinfo->right_em == leftem &&
1832  rinfo->parent_ec == parent_ec)
1833  return rinfo;
1834  }
1835 
1836  foreach(lc, ec->ec_derives)
1837  {
1838  rinfo = (RestrictInfo *) lfirst(lc);
1839  if (rinfo->left_em == leftem &&
1840  rinfo->right_em == rightem &&
1841  rinfo->parent_ec == parent_ec)
1842  return rinfo;
1843  if (rinfo->left_em == rightem &&
1844  rinfo->right_em == leftem &&
1845  rinfo->parent_ec == parent_ec)
1846  return rinfo;
1847  }
1848 
1849  /*
1850  * Not there, so build it, in planner context so we can re-use it. (Not
1851  * important in normal planning, but definitely so in GEQO.)
1852  */
1853  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
1854 
1855  rinfo = build_implied_join_equality(root,
1856  opno,
1857  ec->ec_collation,
1858  leftem->em_expr,
1859  rightem->em_expr,
1860  bms_union(leftem->em_relids,
1861  rightem->em_relids),
1862  bms_union(leftem->em_nullable_relids,
1863  rightem->em_nullable_relids),
1864  ec->ec_min_security);
1865 
1866  /* Mark the clause as redundant, or not */
1867  rinfo->parent_ec = parent_ec;
1868 
1869  /*
1870  * We know the correct values for left_ec/right_ec, ie this particular EC,
1871  * so we can just set them directly instead of forcing another lookup.
1872  */
1873  rinfo->left_ec = ec;
1874  rinfo->right_ec = ec;
1875 
1876  /* Mark it as usable with these EMs */
1877  rinfo->left_em = leftem;
1878  rinfo->right_em = rightem;
1879  /* and save it for possible re-use */
1880  ec->ec_derives = lappend(ec->ec_derives, rinfo);
1881 
1882  MemoryContextSwitchTo(oldcontext);
1883 
1884  return rinfo;
1885 }
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:226
RestrictInfo * build_implied_join_equality(PlannerInfo *root, Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
Definition: initsplan.c:2426
#define lfirst(lc)
Definition: pg_list.h:170
Index ec_min_security
Definition: pathnodes.h:1307

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_nullable_relids, EquivalenceMember::em_relids, lappend(), lfirst, and MemoryContextSwitchTo().

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

3077 {
3078  Relids relids;
3079  ListCell *lc;
3080 
3081  Assert(!eclass->ec_merged);
3082 
3083  /*
3084  * Won't generate joinclauses if const or single-member (the latter test
3085  * covers the volatile case too)
3086  */
3087  if (eclass->ec_has_const || list_length(eclass->ec_members) <= 1)
3088  return false;
3089 
3090  /*
3091  * Note we don't test ec_broken; if we did, we'd need a separate code path
3092  * to look through ec_sources. Checking the members anyway is OK as a
3093  * possibly-overoptimistic heuristic.
3094  */
3095 
3096  /* If specified rel is a child, we must consider the topmost parent rel */
3097  if (IS_OTHER_REL(rel))
3098  {
3100  relids = rel->top_parent_relids;
3101  }
3102  else
3103  relids = rel->relids;
3104 
3105  /* If rel already includes all members of eclass, no point in searching */
3106  if (bms_is_subset(eclass->ec_relids, relids))
3107  return false;
3108 
3109  /* To join, we need a member not in the given rel */
3110  foreach(lc, eclass->ec_members)
3111  {
3112  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
3113 
3114  if (cur_em->em_is_child)
3115  continue; /* ignore children here */
3116 
3117  if (!bms_overlap(cur_em->em_relids, relids))
3118  return true;
3119  }
3120 
3121  return false;
3122 }
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:807
static struct cvec * eclass(struct vars *v, chr c, int cases)
Definition: regc_locale.c:504

References Assert(), bms_is_empty(), bms_is_subset(), bms_overlap(), eclass(), EquivalenceMember::em_is_child, EquivalenceMember::em_relids, IS_OTHER_REL, lfirst, list_length(), RelOptInfo::relids, and RelOptInfo::top_parent_relids.

Referenced by get_useful_ecs_for_relation(), and pathkeys_useful_for_merging().

◆ exprs_known_equal()

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

Definition at line 2369 of file equivclass.c.

2370 {
2371  ListCell *lc1;
2372 
2373  foreach(lc1, root->eq_classes)
2374  {
2375  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2376  bool item1member = false;
2377  bool item2member = false;
2378  ListCell *lc2;
2379 
2380  /* Never match to a volatile EC */
2381  if (ec->ec_has_volatile)
2382  continue;
2383 
2384  foreach(lc2, ec->ec_members)
2385  {
2387 
2388  if (em->em_is_child)
2389  continue; /* ignore children here */
2390  if (equal(item1, em->em_expr))
2391  item1member = true;
2392  else if (equal(item2, em->em_expr))
2393  item2member = true;
2394  /* Exit as soon as equality is proven */
2395  if (item1member && item2member)
2396  return true;
2397  }
2398  }
2399  return false;
2400 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:225

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

Referenced by add_unique_group_var().

◆ find_computable_ec_member()

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

Definition at line 851 of file equivclass.c.

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

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

Referenced by prepare_sort_from_pathkeys(), and relation_can_be_sorted_early().

◆ find_derived_clause_for_ec_member()

RestrictInfo* find_derived_clause_for_ec_member ( EquivalenceClass ec,
EquivalenceMember em 
)

Definition at line 2511 of file equivclass.c.

2513 {
2514  ListCell *lc;
2515 
2516  Assert(ec->ec_has_const);
2517  Assert(!em->em_is_const);
2518  foreach(lc, ec->ec_derives)
2519  {
2520  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2521 
2522  /*
2523  * generate_base_implied_equalities_const will have put non-const
2524  * members on the left side of derived clauses.
2525  */
2526  if (rinfo->left_em == em)
2527  return rinfo;
2528  }
2529  return NULL;
2530 }

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

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

◆ generate_base_implied_equalities()

void generate_base_implied_equalities ( PlannerInfo root)

Definition at line 1059 of file equivclass.c.

1060 {
1061  int ec_index;
1062  ListCell *lc;
1063 
1064  /*
1065  * At this point, we're done absorbing knowledge of equivalences in the
1066  * query, so no further EC merging should happen, and ECs remaining in the
1067  * eq_classes list can be considered canonical. (But note that it's still
1068  * possible for new single-member ECs to be added through
1069  * get_eclass_for_sort_expr().)
1070  */
1071  root->ec_merging_done = true;
1072 
1073  ec_index = 0;
1074  foreach(lc, root->eq_classes)
1075  {
1077  bool can_generate_joinclause = false;
1078  int i;
1079 
1080  Assert(ec->ec_merged == NULL); /* else shouldn't be in list */
1081  Assert(!ec->ec_broken); /* not yet anyway... */
1082 
1083  /*
1084  * Generate implied equalities that are restriction clauses.
1085  * Single-member ECs won't generate any deductions, either here or at
1086  * the join level.
1087  */
1088  if (list_length(ec->ec_members) > 1)
1089  {
1090  if (ec->ec_has_const)
1092  else
1094 
1095  /* Recover if we failed to generate required derived clauses */
1096  if (ec->ec_broken)
1098 
1099  /* Detect whether this EC might generate join clauses */
1100  can_generate_joinclause =
1102  }
1103 
1104  /*
1105  * Mark the base rels cited in each eclass (which should all exist by
1106  * now) with the eq_classes indexes of all eclasses mentioning them.
1107  * This will let us avoid searching in subsequent lookups. While
1108  * we're at it, we can mark base rels that have pending eclass joins;
1109  * this is a cheap version of has_relevant_eclass_joinclause().
1110  */
1111  i = -1;
1112  while ((i = bms_next_member(ec->ec_relids, i)) > 0)
1113  {
1114  RelOptInfo *rel = root->simple_rel_array[i];
1115 
1116  Assert(rel->reloptkind == RELOPT_BASEREL);
1117 
1119  ec_index);
1120 
1121  if (can_generate_joinclause)
1122  rel->has_eclass_joins = true;
1123  }
1124 
1125  ec_index++;
1126  }
1127 }
static void generate_base_implied_equalities_broken(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:1335
static void generate_base_implied_equalities_no_const(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:1228
static void generate_base_implied_equalities_const(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:1133
struct EquivalenceClass * ec_merged
Definition: pathnodes.h:1309
bool has_eclass_joins
Definition: pathnodes.h:941

References Assert(), bms_add_member(), bms_membership(), BMS_MULTIPLE, bms_next_member(), EquivalenceClass::ec_broken, EquivalenceClass::ec_has_const, EquivalenceClass::ec_members, EquivalenceClass::ec_merged, PlannerInfo::ec_merging_done, EquivalenceClass::ec_relids, RelOptInfo::eclass_indexes, PlannerInfo::eq_classes, generate_base_implied_equalities_broken(), generate_base_implied_equalities_const(), generate_base_implied_equalities_no_const(), RelOptInfo::has_eclass_joins, i, lfirst, list_length(), RELOPT_BASEREL, 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 1335 of file equivclass.c.

1337 {
1338  ListCell *lc;
1339 
1340  foreach(lc, ec->ec_sources)
1341  {
1342  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1343 
1344  if (ec->ec_has_const ||
1345  bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
1346  distribute_restrictinfo_to_rels(root, restrictinfo);
1347  }
1348 }
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2181
Relids required_relids
Definition: pathnodes.h:2462

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

1135 {
1136  EquivalenceMember *const_em = NULL;
1137  ListCell *lc;
1138 
1139  /*
1140  * In the trivial case where we just had one "var = const" clause, push
1141  * the original clause back into the main planner machinery. There is
1142  * nothing to be gained by doing it differently, and we save the effort to
1143  * re-build and re-analyze an equality clause that will be exactly
1144  * equivalent to the old one.
1145  */
1146  if (list_length(ec->ec_members) == 2 &&
1147  list_length(ec->ec_sources) == 1)
1148  {
1149  RestrictInfo *restrictinfo = (RestrictInfo *) linitial(ec->ec_sources);
1150 
1151  if (bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
1152  {
1153  distribute_restrictinfo_to_rels(root, restrictinfo);
1154  return;
1155  }
1156  }
1157 
1158  /*
1159  * Find the constant member to use. We prefer an actual constant to
1160  * pseudo-constants (such as Params), because the constraint exclusion
1161  * machinery might be able to exclude relations on the basis of generated
1162  * "var = const" equalities, but "var = param" won't work for that.
1163  */
1164  foreach(lc, ec->ec_members)
1165  {
1166  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1167 
1168  if (cur_em->em_is_const)
1169  {
1170  const_em = cur_em;
1171  if (IsA(cur_em->em_expr, Const))
1172  break;
1173  }
1174  }
1175  Assert(const_em != NULL);
1176 
1177  /* Generate a derived equality against each other member */
1178  foreach(lc, ec->ec_members)
1179  {
1180  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1181  Oid eq_op;
1182  RestrictInfo *rinfo;
1183 
1184  Assert(!cur_em->em_is_child); /* no children yet */
1185  if (cur_em == const_em)
1186  continue;
1187  eq_op = select_equality_operator(ec,
1188  cur_em->em_datatype,
1189  const_em->em_datatype);
1190  if (!OidIsValid(eq_op))
1191  {
1192  /* failed... */
1193  ec->ec_broken = true;
1194  break;
1195  }
1196  rinfo = process_implied_equality(root, eq_op, ec->ec_collation,
1197  cur_em->em_expr, const_em->em_expr,
1198  bms_copy(ec->ec_relids),
1199  bms_union(cur_em->em_nullable_relids,
1200  const_em->em_nullable_relids),
1201  ec->ec_min_security,
1202  ec->ec_below_outer_join,
1203  cur_em->em_is_const);
1204 
1205  /*
1206  * If the clause didn't degenerate to a constant, fill in the correct
1207  * markings for a mergejoinable clause, and save it in ec_derives. (We
1208  * will not re-use such clauses directly, but selectivity estimation
1209  * may consult the list later. Note that this use of ec_derives does
1210  * not overlap with its use for join clauses, since we never generate
1211  * join clauses from an ec_has_const eclass.)
1212  */
1213  if (rinfo && rinfo->mergeopfamilies)
1214  {
1215  /* it's not redundant, so don't set parent_ec */
1216  rinfo->left_ec = rinfo->right_ec = ec;
1217  rinfo->left_em = cur_em;
1218  rinfo->right_em = const_em;
1219  ec->ec_derives = lappend(ec->ec_derives, rinfo);
1220  }
1221  }
1222 }
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
#define OidIsValid(objectId)
Definition: c.h:711
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1768
RestrictInfo * process_implied_equality(PlannerInfo *root, Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level, bool below_outer_join, bool both_const)
Definition: initsplan.c:2276
#define linitial(l)
Definition: pg_list.h:176
bool ec_below_outer_join
Definition: pathnodes.h:1304

References Assert(), bms_copy(), bms_membership(), BMS_MULTIPLE, bms_union(), distribute_restrictinfo_to_rels(), EquivalenceClass::ec_below_outer_join, EquivalenceClass::ec_broken, EquivalenceClass::ec_collation, EquivalenceClass::ec_derives, EquivalenceClass::ec_members, EquivalenceClass::ec_min_security, EquivalenceClass::ec_relids, EquivalenceClass::ec_sources, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, EquivalenceMember::em_nullable_relids, IsA, lappend(), lfirst, linitial, list_length(), OidIsValid, process_implied_equality(), RestrictInfo::required_relids, 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 1228 of file equivclass.c.

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

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

Referenced by generate_base_implied_equalities().

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

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

Definition at line 1394 of file equivclass.c.

1398 {
1399  List *result = NIL;
1400  Relids inner_relids = inner_rel->relids;
1401  Relids nominal_inner_relids;
1402  Relids nominal_join_relids;
1403  Bitmapset *matching_ecs;
1404  int i;
1405 
1406  /* If inner rel is a child, extra setup work is needed */
1407  if (IS_OTHER_REL(inner_rel))
1408  {
1409  Assert(!bms_is_empty(inner_rel->top_parent_relids));
1410 
1411  /* Fetch relid set for the topmost parent rel */
1412  nominal_inner_relids = inner_rel->top_parent_relids;
1413  /* ECs will be marked with the parent's relid, not the child's */
1414  nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1415  }
1416  else
1417  {
1418  nominal_inner_relids = inner_relids;
1419  nominal_join_relids = join_relids;
1420  }
1421 
1422  /*
1423  * Get all eclasses that mention both inner and outer sides of the join
1424  */
1425  matching_ecs = get_common_eclass_indexes(root, nominal_inner_relids,
1426  outer_relids);
1427 
1428  i = -1;
1429  while ((i = bms_next_member(matching_ecs, i)) >= 0)
1430  {
1432  List *sublist = NIL;
1433 
1434  /* ECs containing consts do not need any further enforcement */
1435  if (ec->ec_has_const)
1436  continue;
1437 
1438  /* Single-member ECs won't generate any deductions */
1439  if (list_length(ec->ec_members) <= 1)
1440  continue;
1441 
1442  /* Sanity check that this eclass overlaps the join */
1443  Assert(bms_overlap(ec->ec_relids, nominal_join_relids));
1444 
1445  if (!ec->ec_broken)
1447  ec,
1448  join_relids,
1449  outer_relids,
1450  inner_relids);
1451 
1452  /* Recover if we failed to generate required derived clauses */
1453  if (ec->ec_broken)
1455  ec,
1456  nominal_join_relids,
1457  outer_relids,
1458  nominal_inner_relids,
1459  inner_rel);
1460 
1461  result = list_concat(result, sublist);
1462  }
1463 
1464  return result;
1465 }
static List * generate_join_implied_equalities_normal(PlannerInfo *root, EquivalenceClass *ec, Relids join_relids, Relids outer_relids, Relids inner_relids)
Definition: equivclass.c:1543
static Bitmapset * get_common_eclass_indexes(PlannerInfo *root, Relids relids1, Relids relids2)
Definition: equivclass.c:3219
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:1719
List * list_concat(List *list1, const List *list2)
Definition: list.c:560

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

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

◆ generate_join_implied_equalities_broken()

static List * generate_join_implied_equalities_broken ( PlannerInfo root,
EquivalenceClass ec,
Relids  nominal_join_relids,
Relids  outer_relids,
Relids  nominal_inner_relids,
RelOptInfo inner_rel 
)
static

Definition at line 1719 of file equivclass.c.

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

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

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

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

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

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

3220 {
3221  Bitmapset *rel1ecs;
3222  Bitmapset *rel2ecs;
3223  int relid;
3224 
3225  rel1ecs = get_eclass_indexes_for_relids(root, relids1);
3226 
3227  /*
3228  * We can get away with just using the relation's eclass_indexes directly
3229  * when relids2 is a singleton set.
3230  */
3231  if (bms_get_singleton_member(relids2, &relid))
3232  rel2ecs = root->simple_rel_array[relid]->eclass_indexes;
3233  else
3234  rel2ecs = get_eclass_indexes_for_relids(root, relids2);
3235 
3236  /* Calculate and return the common EC indexes, recycling the left input. */
3237  return bms_int_members(rel1ecs, rel2ecs);
3238 }
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:906

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

Definition at line 621 of file equivclass.c.

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

References add_eq_member(), Assert(), bms_add_member(), bms_equal(), bms_intersect(), bms_next_member(), canonicalize_ec_expression(), contain_agg_clause(), contain_volatile_functions(), contain_window_function(), copyObject, EquivalenceClass::ec_below_outer_join, EquivalenceClass::ec_broken, EquivalenceClass::ec_collation, EquivalenceClass::ec_derives, EquivalenceClass::ec_has_const, EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_max_security, EquivalenceClass::ec_members, EquivalenceClass::ec_merged, PlannerInfo::ec_merging_done, EquivalenceClass::ec_min_security, EquivalenceClass::ec_opfamilies, EquivalenceClass::ec_relids, EquivalenceClass::ec_sortref, EquivalenceClass::ec_sources, RelOptInfo::eclass_indexes, elog(), EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, EquivalenceMember::em_relids, PlannerInfo::eq_classes, equal(), ERROR, expression_returns_set(), i, lappend(), lfirst, list_copy(), list_length(), makeNode, MemoryContextSwitchTo(), NIL, pull_varnos(), RELOPT_BASEREL, RELOPT_DEADREL, 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 3195 of file equivclass.c.

3196 {
3197  Bitmapset *ec_indexes = NULL;
3198  int i = -1;
3199 
3200  /* Should be OK to rely on eclass_indexes */
3201  Assert(root->ec_merging_done);
3202 
3203  while ((i = bms_next_member(relids, i)) > 0)
3204  {
3205  RelOptInfo *rel = root->simple_rel_array[i];
3206 
3207  ec_indexes = bms_add_members(ec_indexes, rel->eclass_indexes);
3208  }
3209  return ec_indexes;
3210 }

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

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

◆ has_relevant_eclass_joinclause()

bool has_relevant_eclass_joinclause ( PlannerInfo root,
RelOptInfo rel1 
)

Definition at line 3030 of file equivclass.c.

3031 {
3032  Bitmapset *matched_ecs;
3033  int i;
3034 
3035  /* Examine only eclasses mentioning rel1 */
3036  matched_ecs = get_eclass_indexes_for_relids(root, rel1->relids);
3037 
3038  i = -1;
3039  while ((i = bms_next_member(matched_ecs, i)) >= 0)
3040  {
3042  i);
3043 
3044  /*
3045  * Won't generate joinclauses if single-member (this test covers the
3046  * volatile case too)
3047  */
3048  if (list_length(ec->ec_members) <= 1)
3049  continue;
3050 
3051  /*
3052  * Per the comment in have_relevant_eclass_joinclause, it's sufficient
3053  * to find an EC that mentions both this rel and some other rel.
3054  */
3055  if (!bms_is_subset(ec->ec_relids, rel1->relids))
3056  return true;
3057  }
3058 
3059  return false;
3060 }

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

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

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

Referenced by have_relevant_joinclause().

◆ is_exprlist_member()

static bool is_exprlist_member ( Expr node,
List exprs 
)
static

Definition at line 917 of file equivclass.c.

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

References equal(), IsA, and lfirst.

Referenced by find_computable_ec_member().

◆ is_redundant_derived_clause()

bool is_redundant_derived_clause ( RestrictInfo rinfo,
List clauselist 
)

Definition at line 3132 of file equivclass.c.

3133 {
3134  EquivalenceClass *parent_ec = rinfo->parent_ec;
3135  ListCell *lc;
3136 
3137  /* Fail if it's not a potentially-redundant clause from some EC */
3138  if (parent_ec == NULL)
3139  return false;
3140 
3141  foreach(lc, clauselist)
3142  {
3143  RestrictInfo *otherrinfo = (RestrictInfo *) lfirst(lc);
3144 
3145  if (otherrinfo->parent_ec == parent_ec)
3146  return true;
3147  }
3148 
3149  return false;
3150 }

References lfirst.

Referenced by create_tidscan_plan().

◆ is_redundant_with_indexclauses()

bool is_redundant_with_indexclauses ( RestrictInfo rinfo,
List indexclauses 
)

Definition at line 3159 of file equivclass.c.

3160 {
3161  EquivalenceClass *parent_ec = rinfo->parent_ec;
3162  ListCell *lc;
3163 
3164  foreach(lc, indexclauses)
3165  {
3166  IndexClause *iclause = lfirst_node(IndexClause, lc);
3167  RestrictInfo *otherrinfo = iclause->rinfo;
3168 
3169  /* If indexclause is lossy, it won't enforce the condition exactly */
3170  if (iclause->lossy)
3171  continue;
3172 
3173  /* Match if it's same clause (pointer equality should be enough) */
3174  if (rinfo == otherrinfo)
3175  return true;
3176  /* Match if derived from same EC */
3177  if (parent_ec && otherrinfo->parent_ec == parent_ec)
3178  return true;
3179 
3180  /*
3181  * No need to look at the derived clauses in iclause->indexquals; they
3182  * couldn't match if the parent clause didn't.
3183  */
3184  }
3185 
3186  return false;
3187 }
#define lfirst_node(type, lc)
Definition: pg_list.h:174
struct RestrictInfo * rinfo
Definition: pathnodes.h:1648

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

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

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,
bool  below_outer_join 
)

Definition at line 119 of file equivclass.c.

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

References add_eq_member(), NullTest::arg, NullTest::argisrow, Assert(), bms_intersect(), bms_is_empty(), bms_join(), canonicalize_ec_expression(), RestrictInfo::clause, contain_nonstrict_functions(), EquivalenceClass::ec_below_outer_join, EquivalenceClass::ec_broken, EquivalenceClass::ec_collation, EquivalenceClass::ec_derives, EquivalenceClass::ec_has_const, EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_max_security, EquivalenceClass::ec_members, EquivalenceClass::ec_merged, PlannerInfo::ec_merging_done, EquivalenceClass::ec_min_security, EquivalenceClass::ec_opfamilies, EquivalenceClass::ec_relids, EquivalenceClass::ec_sortref, EquivalenceClass::ec_sources, elog(), EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, PlannerInfo::eq_classes, equal(), ERROR, exprType(), foreach_current_index, func_strict(), get_leftop(), get_rightop(), IS_NOT_NULL, is_opclause(), RestrictInfo::is_pushed_down, lappend(), lfirst, list_concat(), list_delete_nth_cell(), list_make1, NullTest::location, make_restrictinfo(), makeNode, Max, Min, NIL, RestrictInfo::nullable_relids, NullTest::nulltesttype, op_input_types(), RestrictInfo::outer_relids, RestrictInfo::outerjoin_delayed, RestrictInfo::security_level, and set_opfuncid().

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

◆ reconsider_full_join_clause()

static bool reconsider_full_join_clause ( PlannerInfo root,
RestrictInfo rinfo 
)
static

Definition at line 2188 of file equivclass.c.

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

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

Referenced by reconsider_outer_join_clauses().

◆ reconsider_outer_join_clause()

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

Definition at line 2062 of file equivclass.c.

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

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

Referenced by reconsider_outer_join_clauses().

◆ reconsider_outer_join_clauses()

void reconsider_outer_join_clauses ( PlannerInfo root)

Definition at line 1967 of file equivclass.c.

1968 {
1969  bool found;
1970  ListCell *cell;
1971 
1972  /* Outer loop repeats until we find no more deductions */
1973  do
1974  {
1975  found = false;
1976 
1977  /* Process the LEFT JOIN clauses */
1978  foreach(cell, root->left_join_clauses)
1979  {
1980  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1981 
1982  if (reconsider_outer_join_clause(root, rinfo, true))
1983  {
1984  found = true;
1985  /* remove it from the list */
1986  root->left_join_clauses =
1988  /* we throw it back anyway (see notes above) */
1989  /* but the thrown-back clause has no extra selectivity */
1990  rinfo->norm_selec = 2.0;
1991  rinfo->outer_selec = 1.0;
1992  distribute_restrictinfo_to_rels(root, rinfo);
1993  }
1994  }
1995 
1996  /* Process the RIGHT JOIN clauses */
1997  foreach(cell, root->right_join_clauses)
1998  {
1999  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
2000 
2001  if (reconsider_outer_join_clause(root, rinfo, false))
2002  {
2003  found = true;
2004  /* remove it from the list */
2005  root->right_join_clauses =
2007  /* we throw it back anyway (see notes above) */
2008  /* but the thrown-back clause has no extra selectivity */
2009  rinfo->norm_selec = 2.0;
2010  rinfo->outer_selec = 1.0;
2011  distribute_restrictinfo_to_rels(root, rinfo);
2012  }
2013  }
2014 
2015  /* Process the FULL JOIN clauses */
2016  foreach(cell, root->full_join_clauses)
2017  {
2018  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
2019 
2020  if (reconsider_full_join_clause(root, rinfo))
2021  {
2022  found = true;
2023  /* remove it from the list */
2024  root->full_join_clauses =
2026  /* we throw it back anyway (see notes above) */
2027  /* but the thrown-back clause has no extra selectivity */
2028  rinfo->norm_selec = 2.0;
2029  rinfo->outer_selec = 1.0;
2030  distribute_restrictinfo_to_rels(root, rinfo);
2031  }
2032  }
2033  } while (found);
2034 
2035  /* Now, any remaining clauses have to be thrown back */
2036  foreach(cell, root->left_join_clauses)
2037  {
2038  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
2039 
2040  distribute_restrictinfo_to_rels(root, rinfo);
2041  }
2042  foreach(cell, root->right_join_clauses)
2043  {
2044  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
2045 
2046  distribute_restrictinfo_to_rels(root, rinfo);
2047  }
2048  foreach(cell, root->full_join_clauses)
2049  {
2050  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
2051 
2052  distribute_restrictinfo_to_rels(root, rinfo);
2053  }
2054 }
static bool reconsider_full_join_clause(PlannerInfo *root, RestrictInfo *rinfo)
Definition: equivclass.c:2188
static bool reconsider_outer_join_clause(PlannerInfo *root, RestrictInfo *rinfo, bool outer_on_left)
Definition: equivclass.c:2062
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:388
List * left_join_clauses
Definition: pathnodes.h:319
List * full_join_clauses
Definition: pathnodes.h:330
List * right_join_clauses
Definition: pathnodes.h:325

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

Referenced by query_planner().

◆ relation_can_be_sorted_early()

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

Definition at line 948 of file equivclass.c.

950 {
951  PathTarget *target = rel->reltarget;
952  EquivalenceMember *em;
953  ListCell *lc;
954 
955  /*
956  * Reject volatile ECs immediately; such sorts must always be postponed.
957  */
958  if (ec->ec_has_volatile)
959  return false;
960 
961  /*
962  * Try to find an EM directly matching some reltarget member.
963  */
964  foreach(lc, target->exprs)
965  {
966  Expr *targetexpr = (Expr *) lfirst(lc);
967 
968  em = find_ec_member_matching_expr(ec, targetexpr, rel->relids);
969  if (!em)
970  continue;
971 
972  /*
973  * Reject expressions involving set-returning functions, as those
974  * can't be computed early either. (Note: this test and the following
975  * one are effectively checking properties of targetexpr, so there's
976  * no point in asking whether some other EC member would be better.)
977  */
978  if (expression_returns_set((Node *) em->em_expr))
979  continue;
980 
981  /*
982  * If requested, reject expressions that are not parallel-safe. We
983  * check this last because it's a rather expensive test.
984  */
985  if (require_parallel_safe &&
986  !is_parallel_safe(root, (Node *) em->em_expr))
987  continue;
988 
989  return true;
990  }
991 
992  /*
993  * Try to find an expression computable from the reltarget.
994  */
995  em = find_computable_ec_member(root, ec, target->exprs, rel->relids,
996  require_parallel_safe);
997  if (!em)
998  return false;
999 
1000  /*
1001  * Reject expressions involving set-returning functions, as those can't be
1002  * computed early either. (There's no point in looking for another EC
1003  * member in this case; since SRFs can't appear in WHERE, they cannot
1004  * belong to multi-member ECs.)
1005  */
1006  if (expression_returns_set((Node *) em->em_expr))
1007  return false;
1008 
1009  return true;
1010 }
EquivalenceMember * find_ec_member_matching_expr(EquivalenceClass *ec, Expr *expr, Relids relids)
Definition: equivclass.c:786
EquivalenceMember * find_computable_ec_member(PlannerInfo *root, EquivalenceClass *ec, List *exprs, Relids relids, bool require_parallel_safe)
Definition: equivclass.c:851
List * exprs
Definition: pathnodes.h:1427
struct PathTarget * reltarget
Definition: pathnodes.h:846

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

1769 {
1770  ListCell *lc;
1771 
1772  foreach(lc, ec->ec_opfamilies)
1773  {
1774  Oid opfamily = lfirst_oid(lc);
1775  Oid opno;
1776 
1777  opno = get_opfamily_member(opfamily, lefttype, righttype,
1779  if (!OidIsValid(opno))
1780  continue;
1781  /* If no barrier quals in query, don't worry about leaky operators */
1782  if (ec->ec_max_security == 0)
1783  return opno;
1784  /* Otherwise, insist that selected operators be leakproof */
1785  if (get_func_leakproof(get_opcode(opno)))
1786  return opno;
1787  }
1788  return InvalidOid;
1789 }
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1267
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:1819
#define lfirst_oid(lc)
Definition: pg_list.h:172
#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().