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

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

References add_eq_member(), adjust_appendrel_attrs(), adjust_appendrel_attrs_multilevel(), adjust_child_relids_multilevel(), Assert(), bms_add_members(), bms_difference(), bms_membership(), BMS_MULTIPLE, bms_next_member(), bms_overlap(), EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_members, EquivalenceClass::ec_relids, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, EquivalenceMember::em_nullable_relids, EquivalenceMember::em_relids, PlannerInfo::eq_classes, get_eclass_indexes_for_relids(), i, IS_JOIN_REL, list_length(), list_nth(), MemoryContextSwitchTo(), PlannerInfo::planner_cxt, RelOptInfo::relids, RELOPT_JOINREL, RELOPT_OTHER_JOINREL, RelOptInfo::reloptkind, and RelOptInfo::top_parent_relids.

Referenced by build_child_join_rel().

◆ add_child_rel_equivalences()

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

Definition at line 2550 of file equivclass.c.

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

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

Referenced by set_append_rel_size().

◆ add_eq_member()

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

Definition at line 545 of file equivclass.c.

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

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

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

◆ canonicalize_ec_expression()

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

Definition at line 500 of file equivclass.c.

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

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

1817 {
1818  RestrictInfo *rinfo;
1819  ListCell *lc;
1820  MemoryContext oldcontext;
1821 
1822  /*
1823  * Search to see if we already built a RestrictInfo for this pair of
1824  * EquivalenceMembers. We can use either original source clauses or
1825  * previously-derived clauses. The check on opno is probably redundant,
1826  * but be safe ...
1827  */
1828  foreach(lc, ec->ec_sources)
1829  {
1830  rinfo = (RestrictInfo *) lfirst(lc);
1831  if (rinfo->left_em == leftem &&
1832  rinfo->right_em == rightem &&
1833  rinfo->parent_ec == parent_ec &&
1834  opno == ((OpExpr *) rinfo->clause)->opno)
1835  return rinfo;
1836  }
1837 
1838  foreach(lc, ec->ec_derives)
1839  {
1840  rinfo = (RestrictInfo *) lfirst(lc);
1841  if (rinfo->left_em == leftem &&
1842  rinfo->right_em == rightem &&
1843  rinfo->parent_ec == parent_ec &&
1844  opno == ((OpExpr *) rinfo->clause)->opno)
1845  return rinfo;
1846  }
1847 
1848  /*
1849  * Not there, so build it, in planner context so we can re-use it. (Not
1850  * important in normal planning, but definitely so in GEQO.)
1851  */
1852  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
1853 
1854  rinfo = build_implied_join_equality(root,
1855  opno,
1856  ec->ec_collation,
1857  leftem->em_expr,
1858  rightem->em_expr,
1859  bms_union(leftem->em_relids,
1860  rightem->em_relids),
1861  bms_union(leftem->em_nullable_relids,
1862  rightem->em_nullable_relids),
1863  ec->ec_min_security);
1864 
1865  /* Mark the clause as redundant, or not */
1866  rinfo->parent_ec = parent_ec;
1867 
1868  /*
1869  * We know the correct values for left_ec/right_ec, ie this particular EC,
1870  * so we can just set them directly instead of forcing another lookup.
1871  */
1872  rinfo->left_ec = ec;
1873  rinfo->right_ec = ec;
1874 
1875  /* Mark it as usable with these EMs */
1876  rinfo->left_em = leftem;
1877  rinfo->right_em = rightem;
1878  /* and save it for possible re-use */
1879  ec->ec_derives = lappend(ec->ec_derives, rinfo);
1880 
1881  MemoryContextSwitchTo(oldcontext);
1882 
1883  return rinfo;
1884 }
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
RestrictInfo * build_implied_join_equality(PlannerInfo *root, Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
Definition: initsplan.c:2422
#define lfirst(lc)
Definition: pg_list.h:169
Index ec_min_security
Definition: pathnodes.h:1001
List * ec_derives
Definition: pathnodes.h:993
List * ec_sources
Definition: pathnodes.h:992
EquivalenceClass * left_ec
Definition: pathnodes.h:2126
EquivalenceMember * left_em
Definition: pathnodes.h:2128
Expr * clause
Definition: pathnodes.h:2075
EquivalenceClass * parent_ec
Definition: pathnodes.h:2112
EquivalenceClass * right_ec
Definition: pathnodes.h:2127
EquivalenceMember * right_em
Definition: pathnodes.h:2129

References bms_union(), build_implied_join_equality(), RestrictInfo::clause, EquivalenceClass::ec_collation, EquivalenceClass::ec_derives, EquivalenceClass::ec_min_security, EquivalenceClass::ec_sources, EquivalenceMember::em_expr, EquivalenceMember::em_nullable_relids, EquivalenceMember::em_relids, lappend(), RestrictInfo::left_ec, RestrictInfo::left_em, lfirst, MemoryContextSwitchTo(), RestrictInfo::parent_ec, PlannerInfo::planner_cxt, RestrictInfo::right_ec, and RestrictInfo::right_em.

Referenced by generate_implied_equalities_for_column(), and generate_join_implied_equalities_normal().

◆ eclass_useful_for_merging()

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

Definition at line 3073 of file equivclass.c.

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

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

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

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

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

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

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

Referenced by get_foreign_key_join_selectivity().

◆ find_ec_member_matching_expr()

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

Definition at line 797 of file equivclass.c.

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

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

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

Referenced by query_planner().

◆ generate_base_implied_equalities_broken()

static void generate_base_implied_equalities_broken ( PlannerInfo root,
EquivalenceClass ec 
)
static

Definition at line 1346 of file equivclass.c.

1348 {
1349  ListCell *lc;
1350 
1351  foreach(lc, ec->ec_sources)
1352  {
1353  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1354 
1355  if (ec->ec_has_const ||
1356  bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
1357  distribute_restrictinfo_to_rels(root, restrictinfo);
1358  }
1359 }
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2177
Relids required_relids
Definition: pathnodes.h:2096

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

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

References Assert(), bms_copy(), bms_membership(), BMS_MULTIPLE, bms_union(), distribute_restrictinfo_to_rels(), EquivalenceClass::ec_below_outer_join, EquivalenceClass::ec_broken, EquivalenceClass::ec_collation, EquivalenceClass::ec_derives, EquivalenceClass::ec_members, EquivalenceClass::ec_min_security, EquivalenceClass::ec_relids, EquivalenceClass::ec_sources, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, EquivalenceMember::em_nullable_relids, IsA, lappend(), RestrictInfo::left_ec, RestrictInfo::left_em, lfirst, linitial, list_length(), RestrictInfo::mergeopfamilies, OidIsValid, process_implied_equality(), RestrictInfo::required_relids, RestrictInfo::right_ec, RestrictInfo::right_em, and select_equality_operator().

Referenced by generate_base_implied_equalities().

◆ generate_base_implied_equalities_no_const()

static void generate_base_implied_equalities_no_const ( PlannerInfo root,
EquivalenceClass ec 
)
static

Definition at line 1239 of file equivclass.c.

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

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

Referenced by generate_base_implied_equalities().

◆ generate_implied_equalities_for_column()

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

Definition at line 2832 of file equivclass.c.

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

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

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

◆ generate_join_implied_equalities()

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

Definition at line 1403 of file equivclass.c.

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

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

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

◆ generate_join_implied_equalities_broken()

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

Definition at line 1728 of file equivclass.c.

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

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

Referenced by generate_join_implied_equalities(), and generate_join_implied_equalities_for_ecs().

◆ generate_join_implied_equalities_for_ecs()

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

Definition at line 1481 of file equivclass.c.

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

References Assert(), bms_is_empty(), bms_overlap(), bms_union(), EquivalenceClass::ec_broken, EquivalenceClass::ec_has_const, EquivalenceClass::ec_members, EquivalenceClass::ec_relids, generate_join_implied_equalities_broken(), generate_join_implied_equalities_normal(), IS_OTHER_REL, lfirst, list_concat(), list_length(), NIL, RelOptInfo::relids, and RelOptInfo::top_parent_relids.

Referenced by get_joinrel_parampathinfo().

◆ generate_join_implied_equalities_normal()

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

Definition at line 1552 of file equivclass.c.

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

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

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

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

Referenced by generate_join_implied_equalities(), and have_relevant_eclass_joinclause().

◆ get_eclass_for_sort_expr()

EquivalenceClass* get_eclass_for_sort_expr ( PlannerInfo root,
Expr expr,
Relids  nullable_relids,
List opfamilies,
Oid  opcintype,
Oid  collation,
Index  sortref,
Relids  rel,
bool  create_it 
)

Definition at line 621 of file equivclass.c.

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

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

◆ get_eclass_indexes_for_relids()

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

Definition at line 3194 of file equivclass.c.

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

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

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

◆ has_relevant_eclass_joinclause()

bool has_relevant_eclass_joinclause ( PlannerInfo root,
RelOptInfo rel1 
)

Definition at line 3029 of file equivclass.c.

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

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

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

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

929 {
930  ListCell *lc;
931 
932  foreach(lc, exprs)
933  {
934  Expr *expr = (Expr *) lfirst(lc);
935 
936  if (expr && IsA(expr, TargetEntry))
937  expr = ((TargetEntry *) expr)->expr;
938 
939  if (equal(node, expr))
940  return true;
941  }
942  return false;
943 }

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

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

References lfirst, and RestrictInfo::parent_ec.

Referenced by create_tidscan_plan().

◆ is_redundant_with_indexclauses()

bool is_redundant_with_indexclauses ( RestrictInfo rinfo,
List indexclauses 
)

Definition at line 3158 of file equivclass.c.

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

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

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

◆ match_eclasses_to_foreign_key_col()

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

Definition at line 2419 of file equivclass.c.

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

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

Referenced by match_foreign_keys_to_quals().

◆ process_equivalence()

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

Definition at line 119 of file equivclass.c.

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

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

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

◆ reconsider_full_join_clause()

static bool reconsider_full_join_clause ( PlannerInfo root,
RestrictInfo rinfo 
)
static

Definition at line 2187 of file equivclass.c.

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

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

Referenced by reconsider_outer_join_clauses().

◆ reconsider_outer_join_clause()

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

Definition at line 2061 of file equivclass.c.

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

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

Referenced by reconsider_outer_join_clauses().

◆ reconsider_outer_join_clauses()

void reconsider_outer_join_clauses ( PlannerInfo root)

Definition at line 1966 of file equivclass.c.

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

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

Referenced by query_planner().

◆ relation_can_be_sorted_early()

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

Definition at line 959 of file equivclass.c.

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

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

Referenced by get_useful_pathkeys_for_relation().

◆ select_equality_operator()

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

Definition at line 1777 of file equivclass.c.

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

References BTEqualStrategyNumber, EquivalenceClass::ec_max_security, EquivalenceClass::ec_opfamilies, get_func_leakproof(), get_opcode(), get_opfamily_member(), InvalidOid, lfirst_oid, and OidIsValid.

Referenced by generate_base_implied_equalities_const(), generate_base_implied_equalities_no_const(), generate_implied_equalities_for_column(), generate_join_implied_equalities_normal(), reconsider_full_join_clause(), and reconsider_outer_join_clause().