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

Go to the source code of this file.

Functions

static EquivalenceMemberadd_eq_member (EquivalenceClass *ec, Expr *expr, Relids relids, Relids nullable_relids, bool is_child, Oid datatype)
 
static void generate_base_implied_equalities_const (PlannerInfo *root, EquivalenceClass *ec)
 
static void generate_base_implied_equalities_no_const (PlannerInfo *root, EquivalenceClass *ec)
 
static void generate_base_implied_equalities_broken (PlannerInfo *root, EquivalenceClass *ec)
 
static Listgenerate_join_implied_equalities_normal (PlannerInfo *root, EquivalenceClass *ec, Relids join_relids, Relids outer_relids, Relids inner_relids)
 
static Listgenerate_join_implied_equalities_broken (PlannerInfo *root, EquivalenceClass *ec, Relids nominal_join_relids, Relids outer_relids, Relids nominal_inner_relids, RelOptInfo *inner_rel)
 
static Oid select_equality_operator (EquivalenceClass *ec, Oid lefttype, Oid righttype)
 
static RestrictInfocreate_join_clause (PlannerInfo *root, EquivalenceClass *ec, Oid opno, EquivalenceMember *leftem, EquivalenceMember *rightem, EquivalenceClass *parent_ec)
 
static bool reconsider_outer_join_clause (PlannerInfo *root, RestrictInfo *rinfo, bool outer_on_left)
 
static bool reconsider_full_join_clause (PlannerInfo *root, RestrictInfo *rinfo)
 
static Bitmapsetget_eclass_indexes_for_relids (PlannerInfo *root, Relids relids)
 
static Bitmapsetget_common_eclass_indexes (PlannerInfo *root, Relids relids1, Relids relids2)
 
bool process_equivalence (PlannerInfo *root, RestrictInfo **p_restrictinfo, bool below_outer_join)
 
Exprcanonicalize_ec_expression (Expr *expr, Oid req_type, Oid req_collation)
 
EquivalenceClassget_eclass_for_sort_expr (PlannerInfo *root, Expr *expr, Relids nullable_relids, List *opfamilies, Oid opcintype, Oid collation, Index sortref, Relids rel, bool create_it)
 
Exprfind_em_expr_for_rel (EquivalenceClass *ec, RelOptInfo *rel)
 
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 2451 of file equivclass.c.

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

2455 {
2456  Relids top_parent_relids = child_joinrel->top_parent_relids;
2457  Relids child_relids = child_joinrel->relids;
2458  Bitmapset *matching_ecs;
2459  MemoryContext oldcontext;
2460  int i;
2461 
2462  Assert(IS_JOIN_REL(child_joinrel) && IS_JOIN_REL(parent_joinrel));
2463 
2464  /* We need consider only ECs that mention the parent joinrel */
2465  matching_ecs = get_eclass_indexes_for_relids(root, top_parent_relids);
2466 
2467  /*
2468  * If we're being called during GEQO join planning, we still have to
2469  * create any new EC members in the main planner context, to avoid having
2470  * a corrupt EC data structure after the GEQO context is reset. This is
2471  * problematic since we'll leak memory across repeated GEQO cycles. For
2472  * now, though, bloat is better than crash. If it becomes a real issue
2473  * we'll have to do something to avoid generating duplicate EC members.
2474  */
2475  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
2476 
2477  i = -1;
2478  while ((i = bms_next_member(matching_ecs, i)) >= 0)
2479  {
2480  EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2481  int num_members;
2482 
2483  /*
2484  * If this EC contains a volatile expression, then generating child
2485  * EMs would be downright dangerous, so skip it. We rely on a
2486  * volatile EC having only one EM.
2487  */
2488  if (cur_ec->ec_has_volatile)
2489  continue;
2490 
2491  /* Sanity check on get_eclass_indexes_for_relids result */
2492  Assert(bms_overlap(top_parent_relids, cur_ec->ec_relids));
2493 
2494  /*
2495  * We don't use foreach() here because there's no point in scanning
2496  * newly-added child members, so we can stop after the last
2497  * pre-existing EC member.
2498  */
2499  num_members = list_length(cur_ec->ec_members);
2500  for (int pos = 0; pos < num_members; pos++)
2501  {
2502  EquivalenceMember *cur_em = (EquivalenceMember *) list_nth(cur_ec->ec_members, pos);
2503 
2504  if (cur_em->em_is_const)
2505  continue; /* ignore consts here */
2506 
2507  /*
2508  * We consider only original EC members here, not
2509  * already-transformed child members.
2510  */
2511  if (cur_em->em_is_child)
2512  continue; /* ignore children here */
2513 
2514  /*
2515  * We may ignore expressions that reference a single baserel,
2516  * because add_child_rel_equivalences should have handled them.
2517  */
2518  if (bms_membership(cur_em->em_relids) != BMS_MULTIPLE)
2519  continue;
2520 
2521  /* Does this member reference child's topmost parent rel? */
2522  if (bms_overlap(cur_em->em_relids, top_parent_relids))
2523  {
2524  /* Yes, generate transformed child version */
2525  Expr *child_expr;
2526  Relids new_relids;
2527  Relids new_nullable_relids;
2528 
2529  if (parent_joinrel->reloptkind == RELOPT_JOINREL)
2530  {
2531  /* Simple single-level transformation */
2532  child_expr = (Expr *)
2534  (Node *) cur_em->em_expr,
2535  nappinfos, appinfos);
2536  }
2537  else
2538  {
2539  /* Must do multi-level transformation */
2540  Assert(parent_joinrel->reloptkind == RELOPT_OTHER_JOINREL);
2541  child_expr = (Expr *)
2543  (Node *) cur_em->em_expr,
2544  child_relids,
2545  top_parent_relids);
2546  }
2547 
2548  /*
2549  * Transform em_relids to match. Note we do *not* do
2550  * pull_varnos(child_expr) here, as for example the
2551  * transformation might have substituted a constant, but we
2552  * don't want the child member to be marked as constant.
2553  */
2554  new_relids = bms_difference(cur_em->em_relids,
2555  top_parent_relids);
2556  new_relids = bms_add_members(new_relids, child_relids);
2557 
2558  /*
2559  * For nullable_relids, we must selectively replace parent
2560  * nullable relids with child ones.
2561  */
2562  new_nullable_relids = cur_em->em_nullable_relids;
2563  if (bms_overlap(new_nullable_relids, top_parent_relids))
2564  new_nullable_relids =
2566  new_nullable_relids,
2567  child_relids,
2568  top_parent_relids);
2569 
2570  (void) add_eq_member(cur_ec, child_expr,
2571  new_relids, new_nullable_relids,
2572  true, cur_em->em_datatype);
2573  }
2574  }
2575  }
2576 
2577  MemoryContextSwitchTo(oldcontext);
2578 }
RelOptKind reloptkind
Definition: pathnodes.h:661
Relids em_nullable_relids
Definition: pathnodes.h:1016
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:291
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:528
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:642
Relids adjust_child_relids_multilevel(PlannerInfo *root, Relids relids, Relids child_relids, Relids top_parent_relids)
Definition: appendinfo.c:576
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, Relids child_relids, Relids top_parent_relids)
Definition: appendinfo.c:502
static void * list_nth(const List *list, int n)
Definition: pg_list.h:266
static Bitmapset * get_eclass_indexes_for_relids(PlannerInfo *root, Relids relids)
Definition: equivclass.c:2967
Relids ec_relids
Definition: pathnodes.h:969
Relids relids
Definition: pathnodes.h:664
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
#define Assert(condition)
Definition: c.h:746
List * eq_classes
Definition: pathnodes.h:264
static int list_length(const List *l)
Definition: pg_list.h:149
bool ec_has_volatile
Definition: pathnodes.h:972
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
int i
static EquivalenceMember * add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids, Relids nullable_relids, bool is_child, Oid datatype)
Definition: equivclass.c:543
MemoryContext planner_cxt
Definition: pathnodes.h:329
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793
List * ec_members
Definition: pathnodes.h:966
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:194
Relids top_parent_relids
Definition: pathnodes.h:737

◆ add_child_rel_equivalences()

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

Definition at line 2323 of file equivclass.c.

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

Referenced by set_append_rel_size().

2327 {
2328  Relids top_parent_relids = child_rel->top_parent_relids;
2329  Relids child_relids = child_rel->relids;
2330  int i;
2331 
2332  /*
2333  * EC merging should be complete already, so we can use the parent rel's
2334  * eclass_indexes to avoid searching all of root->eq_classes.
2335  */
2336  Assert(root->ec_merging_done);
2337  Assert(IS_SIMPLE_REL(parent_rel));
2338 
2339  i = -1;
2340  while ((i = bms_next_member(parent_rel->eclass_indexes, i)) >= 0)
2341  {
2342  EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2343  int num_members;
2344 
2345  /*
2346  * If this EC contains a volatile expression, then generating child
2347  * EMs would be downright dangerous, so skip it. We rely on a
2348  * volatile EC having only one EM.
2349  */
2350  if (cur_ec->ec_has_volatile)
2351  continue;
2352 
2353  /* Sanity check eclass_indexes only contain ECs for parent_rel */
2354  Assert(bms_is_subset(top_parent_relids, cur_ec->ec_relids));
2355 
2356  /*
2357  * We don't use foreach() here because there's no point in scanning
2358  * newly-added child members, so we can stop after the last
2359  * pre-existing EC member.
2360  */
2361  num_members = list_length(cur_ec->ec_members);
2362  for (int pos = 0; pos < num_members; pos++)
2363  {
2364  EquivalenceMember *cur_em = (EquivalenceMember *) list_nth(cur_ec->ec_members, pos);
2365 
2366  if (cur_em->em_is_const)
2367  continue; /* ignore consts here */
2368 
2369  /*
2370  * We consider only original EC members here, not
2371  * already-transformed child members. Otherwise, if some original
2372  * member expression references more than one appendrel, we'd get
2373  * an O(N^2) explosion of useless derived expressions for
2374  * combinations of children. (But add_child_join_rel_equivalences
2375  * may add targeted combinations for partitionwise-join purposes.)
2376  */
2377  if (cur_em->em_is_child)
2378  continue; /* ignore children here */
2379 
2380  /* Does this member reference child's topmost parent rel? */
2381  if (bms_overlap(cur_em->em_relids, top_parent_relids))
2382  {
2383  /* Yes, generate transformed child version */
2384  Expr *child_expr;
2385  Relids new_relids;
2386  Relids new_nullable_relids;
2387 
2388  if (parent_rel->reloptkind == RELOPT_BASEREL)
2389  {
2390  /* Simple single-level transformation */
2391  child_expr = (Expr *)
2393  (Node *) cur_em->em_expr,
2394  1, &appinfo);
2395  }
2396  else
2397  {
2398  /* Must do multi-level transformation */
2399  child_expr = (Expr *)
2401  (Node *) cur_em->em_expr,
2402  child_relids,
2403  top_parent_relids);
2404  }
2405 
2406  /*
2407  * Transform em_relids to match. Note we do *not* do
2408  * pull_varnos(child_expr) here, as for example the
2409  * transformation might have substituted a constant, but we
2410  * don't want the child member to be marked as constant.
2411  */
2412  new_relids = bms_difference(cur_em->em_relids,
2413  top_parent_relids);
2414  new_relids = bms_add_members(new_relids, child_relids);
2415 
2416  /*
2417  * And likewise for nullable_relids. Note this code assumes
2418  * parent and child relids are singletons.
2419  */
2420  new_nullable_relids = cur_em->em_nullable_relids;
2421  if (bms_overlap(new_nullable_relids, top_parent_relids))
2422  {
2423  new_nullable_relids = bms_difference(new_nullable_relids,
2424  top_parent_relids);
2425  new_nullable_relids = bms_add_members(new_nullable_relids,
2426  child_relids);
2427  }
2428 
2429  (void) add_eq_member(cur_ec, child_expr,
2430  new_relids, new_nullable_relids,
2431  true, cur_em->em_datatype);
2432 
2433  /* Record this EC index for the child rel */
2434  child_rel->eclass_indexes = bms_add_member(child_rel->eclass_indexes, i);
2435  }
2436  }
2437  }
2438 }
RelOptKind reloptkind
Definition: pathnodes.h:661
bool ec_merging_done
Definition: pathnodes.h:266
Relids em_nullable_relids
Definition: pathnodes.h:1016
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:291
Definition: nodes.h:528
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:637
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, Relids child_relids, Relids top_parent_relids)
Definition: appendinfo.c:502
static void * list_nth(const List *list, int n)
Definition: pg_list.h:266
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids ec_relids
Definition: pathnodes.h:969
Relids relids
Definition: pathnodes.h:664
#define Assert(condition)
Definition: c.h:746
List * eq_classes
Definition: pathnodes.h:264
static int list_length(const List *l)
Definition: pg_list.h:149
bool ec_has_volatile
Definition: pathnodes.h:972
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
int i
static EquivalenceMember * add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids, Relids nullable_relids, bool is_child, Oid datatype)
Definition: equivclass.c:543
Bitmapset * eclass_indexes
Definition: pathnodes.h:706
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793
List * ec_members
Definition: pathnodes.h:966
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:194
Relids top_parent_relids
Definition: pathnodes.h:737

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

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

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

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

◆ canonicalize_ec_expression()

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

Definition at line 498 of file equivclass.c.

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

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

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

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

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

Referenced by generate_implied_equalities_for_column(), and generate_join_implied_equalities_normal().

1594 {
1595  RestrictInfo *rinfo;
1596  ListCell *lc;
1597  MemoryContext oldcontext;
1598 
1599  /*
1600  * Search to see if we already built a RestrictInfo for this pair of
1601  * EquivalenceMembers. We can use either original source clauses or
1602  * previously-derived clauses. The check on opno is probably redundant,
1603  * but be safe ...
1604  */
1605  foreach(lc, ec->ec_sources)
1606  {
1607  rinfo = (RestrictInfo *) lfirst(lc);
1608  if (rinfo->left_em == leftem &&
1609  rinfo->right_em == rightem &&
1610  rinfo->parent_ec == parent_ec &&
1611  opno == ((OpExpr *) rinfo->clause)->opno)
1612  return rinfo;
1613  }
1614 
1615  foreach(lc, ec->ec_derives)
1616  {
1617  rinfo = (RestrictInfo *) lfirst(lc);
1618  if (rinfo->left_em == leftem &&
1619  rinfo->right_em == rightem &&
1620  rinfo->parent_ec == parent_ec &&
1621  opno == ((OpExpr *) rinfo->clause)->opno)
1622  return rinfo;
1623  }
1624 
1625  /*
1626  * Not there, so build it, in planner context so we can re-use it. (Not
1627  * important in normal planning, but definitely so in GEQO.)
1628  */
1629  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
1630 
1631  rinfo = build_implied_join_equality(opno,
1632  ec->ec_collation,
1633  leftem->em_expr,
1634  rightem->em_expr,
1635  bms_union(leftem->em_relids,
1636  rightem->em_relids),
1637  bms_union(leftem->em_nullable_relids,
1638  rightem->em_nullable_relids),
1639  ec->ec_min_security);
1640 
1641  /* Mark the clause as redundant, or not */
1642  rinfo->parent_ec = parent_ec;
1643 
1644  /*
1645  * We know the correct values for left_ec/right_ec, ie this particular EC,
1646  * so we can just set them directly instead of forcing another lookup.
1647  */
1648  rinfo->left_ec = ec;
1649  rinfo->right_ec = ec;
1650 
1651  /* Mark it as usable with these EMs */
1652  rinfo->left_em = leftem;
1653  rinfo->right_em = rightem;
1654  /* and save it for possible re-use */
1655  ec->ec_derives = lappend(ec->ec_derives, rinfo);
1656 
1657  MemoryContextSwitchTo(oldcontext);
1658 
1659  return rinfo;
1660 }
Index ec_min_security
Definition: pathnodes.h:976
Relids em_nullable_relids
Definition: pathnodes.h:1016
List * ec_derives
Definition: pathnodes.h:968
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
EquivalenceClass * right_ec
Definition: pathnodes.h:2036
EquivalenceMember * left_em
Definition: pathnodes.h:2037
EquivalenceClass * parent_ec
Definition: pathnodes.h:2021
List * ec_sources
Definition: pathnodes.h:967
List * lappend(List *list, void *datum)
Definition: list.c:321
EquivalenceMember * right_em
Definition: pathnodes.h:2038
Expr * clause
Definition: pathnodes.h:1987
#define lfirst(lc)
Definition: pg_list.h:169
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
RestrictInfo * build_implied_join_equality(Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
Definition: initsplan.c:2410
EquivalenceClass * left_ec
Definition: pathnodes.h:2035
MemoryContext planner_cxt
Definition: pathnodes.h:329

◆ eclass_useful_for_merging()

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

Definition at line 2846 of file equivclass.c.

References Assert, bms_is_empty(), bms_is_subset(), bms_overlap(), EquivalenceClass::ec_has_const, EquivalenceClass::ec_members, EquivalenceClass::ec_merged, EquivalenceClass::ec_relids, EquivalenceMember::em_is_child, EquivalenceMember::em_relids, IS_OTHER_REL, lfirst, list_length(), RelOptInfo::relids, and RelOptInfo::top_parent_relids.

Referenced by get_useful_ecs_for_relation(), and pathkeys_useful_for_merging().

2849 {
2850  Relids relids;
2851  ListCell *lc;
2852 
2853  Assert(!eclass->ec_merged);
2854 
2855  /*
2856  * Won't generate joinclauses if const or single-member (the latter test
2857  * covers the volatile case too)
2858  */
2859  if (eclass->ec_has_const || list_length(eclass->ec_members) <= 1)
2860  return false;
2861 
2862  /*
2863  * Note we don't test ec_broken; if we did, we'd need a separate code path
2864  * to look through ec_sources. Checking the members anyway is OK as a
2865  * possibly-overoptimistic heuristic.
2866  */
2867 
2868  /* If specified rel is a child, we must consider the topmost parent rel */
2869  if (IS_OTHER_REL(rel))
2870  {
2872  relids = rel->top_parent_relids;
2873  }
2874  else
2875  relids = rel->relids;
2876 
2877  /* If rel already includes all members of eclass, no point in searching */
2878  if (bms_is_subset(eclass->ec_relids, relids))
2879  return false;
2880 
2881  /* To join, we need a member not in the given rel */
2882  foreach(lc, eclass->ec_members)
2883  {
2884  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
2885 
2886  if (cur_em->em_is_child)
2887  continue; /* ignore children here */
2888 
2889  if (!bms_overlap(cur_em->em_relids, relids))
2890  return true;
2891  }
2892 
2893  return false;
2894 }
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:652
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids ec_relids
Definition: pathnodes.h:969
Relids relids
Definition: pathnodes.h:664
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
struct EquivalenceClass * ec_merged
Definition: pathnodes.h:978
List * ec_members
Definition: pathnodes.h:966
Relids top_parent_relids
Definition: pathnodes.h:737

◆ exprs_known_equal()

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

Definition at line 2141 of file equivclass.c.

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

Referenced by add_unique_group_var().

2142 {
2143  ListCell *lc1;
2144 
2145  foreach(lc1, root->eq_classes)
2146  {
2147  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2148  bool item1member = false;
2149  bool item2member = false;
2150  ListCell *lc2;
2151 
2152  /* Never match to a volatile EC */
2153  if (ec->ec_has_volatile)
2154  continue;
2155 
2156  foreach(lc2, ec->ec_members)
2157  {
2159 
2160  if (em->em_is_child)
2161  continue; /* ignore children here */
2162  if (equal(item1, em->em_expr))
2163  item1member = true;
2164  else if (equal(item2, em->em_expr))
2165  item2member = true;
2166  /* Exit as soon as equality is proven */
2167  if (item1member && item2member)
2168  return true;
2169  }
2170  }
2171  return false;
2172 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3032
#define lfirst(lc)
Definition: pg_list.h:169
List * eq_classes
Definition: pathnodes.h:264
bool ec_has_volatile
Definition: pathnodes.h:972
List * ec_members
Definition: pathnodes.h:966

◆ find_derived_clause_for_ec_member()

RestrictInfo* find_derived_clause_for_ec_member ( EquivalenceClass ec,
EquivalenceMember em 
)

Definition at line 2283 of file equivclass.c.

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

2285 {
2286  ListCell *lc;
2287 
2288  Assert(ec->ec_has_const);
2289  Assert(!em->em_is_const);
2290  foreach(lc, ec->ec_derives)
2291  {
2292  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2293 
2294  /*
2295  * generate_base_implied_equalities_const will have put non-const
2296  * members on the left side of derived clauses.
2297  */
2298  if (rinfo->left_em == em)
2299  return rinfo;
2300  }
2301  return NULL;
2302 }
List * ec_derives
Definition: pathnodes.h:968
EquivalenceMember * left_em
Definition: pathnodes.h:2037
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169

◆ find_em_expr_for_rel()

Expr* find_em_expr_for_rel ( EquivalenceClass ec,
RelOptInfo rel 
)

Definition at line 776 of file equivclass.c.

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

Referenced by appendOrderByClause(), and get_useful_pathkeys_for_relation().

777 {
778  ListCell *lc_em;
779 
780  foreach(lc_em, ec->ec_members)
781  {
782  EquivalenceMember *em = lfirst(lc_em);
783 
784  if (bms_is_subset(em->em_relids, rel->relids) &&
785  !bms_is_empty(em->em_relids))
786  {
787  /*
788  * If there is more than one equivalence member whose Vars are
789  * taken entirely from this relation, we'll be content to choose
790  * any one of those.
791  */
792  return em->em_expr;
793  }
794  }
795 
796  /* We didn't find any suitable equivalence class expression */
797  return NULL;
798 }
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids relids
Definition: pathnodes.h:664
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
#define lfirst(lc)
Definition: pg_list.h:169
List * ec_members
Definition: pathnodes.h:966

◆ generate_base_implied_equalities()

void generate_base_implied_equalities ( PlannerInfo root)

Definition at line 847 of file equivclass.c.

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

Referenced by query_planner().

848 {
849  int ec_index;
850  ListCell *lc;
851 
852  /*
853  * At this point, we're done absorbing knowledge of equivalences in the
854  * query, so no further EC merging should happen, and ECs remaining in the
855  * eq_classes list can be considered canonical. (But note that it's still
856  * possible for new single-member ECs to be added through
857  * get_eclass_for_sort_expr().)
858  */
859  root->ec_merging_done = true;
860 
861  ec_index = 0;
862  foreach(lc, root->eq_classes)
863  {
865  bool can_generate_joinclause = false;
866  int i;
867 
868  Assert(ec->ec_merged == NULL); /* else shouldn't be in list */
869  Assert(!ec->ec_broken); /* not yet anyway... */
870 
871  /*
872  * Generate implied equalities that are restriction clauses.
873  * Single-member ECs won't generate any deductions, either here or at
874  * the join level.
875  */
876  if (list_length(ec->ec_members) > 1)
877  {
878  if (ec->ec_has_const)
880  else
882 
883  /* Recover if we failed to generate required derived clauses */
884  if (ec->ec_broken)
886 
887  /* Detect whether this EC might generate join clauses */
888  can_generate_joinclause =
890  }
891 
892  /*
893  * Mark the base rels cited in each eclass (which should all exist by
894  * now) with the eq_classes indexes of all eclasses mentioning them.
895  * This will let us avoid searching in subsequent lookups. While
896  * we're at it, we can mark base rels that have pending eclass joins;
897  * this is a cheap version of has_relevant_eclass_joinclause().
898  */
899  i = -1;
900  while ((i = bms_next_member(ec->ec_relids, i)) > 0)
901  {
902  RelOptInfo *rel = root->simple_rel_array[i];
903 
905 
907  ec_index);
908 
909  if (can_generate_joinclause)
910  rel->has_eclass_joins = true;
911  }
912 
913  ec_index++;
914  }
915 }
bool has_eclass_joins
Definition: pathnodes.h:732
static void generate_base_implied_equalities_no_const(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:1016
RelOptKind reloptkind
Definition: pathnodes.h:661
bool ec_merging_done
Definition: pathnodes.h:266
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
static void generate_base_implied_equalities_broken(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:1123
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:201
Relids ec_relids
Definition: pathnodes.h:969
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
List * eq_classes
Definition: pathnodes.h:264
static int list_length(const List *l)
Definition: pg_list.h:149
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
int i
static void generate_base_implied_equalities_const(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:921
Bitmapset * eclass_indexes
Definition: pathnodes.h:706
struct EquivalenceClass * ec_merged
Definition: pathnodes.h:978
List * ec_members
Definition: pathnodes.h:966

◆ generate_base_implied_equalities_broken()

static void generate_base_implied_equalities_broken ( PlannerInfo root,
EquivalenceClass ec 
)
static

Definition at line 1123 of file equivclass.c.

References bms_membership(), BMS_MULTIPLE, distribute_restrictinfo_to_rels(), EquivalenceClass::ec_has_const, EquivalenceClass::ec_sources, lfirst, and RestrictInfo::required_relids.

Referenced by generate_base_implied_equalities().

1125 {
1126  ListCell *lc;
1127 
1128  foreach(lc, ec->ec_sources)
1129  {
1130  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1131 
1132  if (ec->ec_has_const ||
1133  bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
1134  distribute_restrictinfo_to_rels(root, restrictinfo);
1135  }
1136 }
Relids required_relids
Definition: pathnodes.h:2005
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2173
List * ec_sources
Definition: pathnodes.h:967
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
#define lfirst(lc)
Definition: pg_list.h:169

◆ generate_base_implied_equalities_const()

static void generate_base_implied_equalities_const ( PlannerInfo root,
EquivalenceClass ec 
)
static

Definition at line 921 of file equivclass.c.

References Assert, bms_copy(), bms_membership(), BMS_MULTIPLE, bms_union(), distribute_restrictinfo_to_rels(), EquivalenceClass::ec_below_outer_join, EquivalenceClass::ec_broken, EquivalenceClass::ec_collation, EquivalenceClass::ec_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().

923 {
924  EquivalenceMember *const_em = NULL;
925  ListCell *lc;
926 
927  /*
928  * In the trivial case where we just had one "var = const" clause, push
929  * the original clause back into the main planner machinery. There is
930  * nothing to be gained by doing it differently, and we save the effort to
931  * re-build and re-analyze an equality clause that will be exactly
932  * equivalent to the old one.
933  */
934  if (list_length(ec->ec_members) == 2 &&
935  list_length(ec->ec_sources) == 1)
936  {
937  RestrictInfo *restrictinfo = (RestrictInfo *) linitial(ec->ec_sources);
938 
939  if (bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
940  {
941  distribute_restrictinfo_to_rels(root, restrictinfo);
942  return;
943  }
944  }
945 
946  /*
947  * Find the constant member to use. We prefer an actual constant to
948  * pseudo-constants (such as Params), because the constraint exclusion
949  * machinery might be able to exclude relations on the basis of generated
950  * "var = const" equalities, but "var = param" won't work for that.
951  */
952  foreach(lc, ec->ec_members)
953  {
954  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
955 
956  if (cur_em->em_is_const)
957  {
958  const_em = cur_em;
959  if (IsA(cur_em->em_expr, Const))
960  break;
961  }
962  }
963  Assert(const_em != NULL);
964 
965  /* Generate a derived equality against each other member */
966  foreach(lc, ec->ec_members)
967  {
968  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
969  Oid eq_op;
970  RestrictInfo *rinfo;
971 
972  Assert(!cur_em->em_is_child); /* no children yet */
973  if (cur_em == const_em)
974  continue;
975  eq_op = select_equality_operator(ec,
976  cur_em->em_datatype,
977  const_em->em_datatype);
978  if (!OidIsValid(eq_op))
979  {
980  /* failed... */
981  ec->ec_broken = true;
982  break;
983  }
984  rinfo = process_implied_equality(root, eq_op, ec->ec_collation,
985  cur_em->em_expr, const_em->em_expr,
986  bms_copy(ec->ec_relids),
988  const_em->em_nullable_relids),
989  ec->ec_min_security,
991  cur_em->em_is_const);
992 
993  /*
994  * If the clause didn't degenerate to a constant, fill in the correct
995  * markings for a mergejoinable clause, and save it in ec_derives. (We
996  * will not re-use such clauses directly, but selectivity estimation
997  * may consult the list later. Note that this use of ec_derives does
998  * not overlap with its use for join clauses, since we never generate
999  * join clauses from an ec_has_const eclass.)
1000  */
1001  if (rinfo && rinfo->mergeopfamilies)
1002  {
1003  /* it's not redundant, so don't set parent_ec */
1004  rinfo->left_ec = rinfo->right_ec = ec;
1005  rinfo->left_em = cur_em;
1006  rinfo->right_em = const_em;
1007  ec->ec_derives = lappend(ec->ec_derives, rinfo);
1008  }
1009  }
1010 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
Relids required_relids
Definition: pathnodes.h:2005
Index ec_min_security
Definition: pathnodes.h:976
Relids em_nullable_relids
Definition: pathnodes.h:1016
List * ec_derives
Definition: pathnodes.h:968
bool ec_below_outer_join
Definition: pathnodes.h:973
EquivalenceClass * right_ec
Definition: pathnodes.h:2036
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
List * mergeopfamilies
Definition: pathnodes.h:2032
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2173
#define linitial(l)
Definition: pg_list.h:174
EquivalenceMember * left_em
Definition: pathnodes.h:2037
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1554
List * ec_sources
Definition: pathnodes.h:967
Relids ec_relids
Definition: pathnodes.h:969
List * lappend(List *list, void *datum)
Definition: list.c:321
EquivalenceMember * right_em
Definition: pathnodes.h:2038
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
static int list_length(const List *l)
Definition: pg_list.h:149
EquivalenceClass * left_ec
Definition: pathnodes.h:2035
List * ec_members
Definition: pathnodes.h:966
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:2261

◆ generate_base_implied_equalities_no_const()

static void generate_base_implied_equalities_no_const ( PlannerInfo root,
EquivalenceClass ec 
)
static

Definition at line 1016 of file equivclass.c.

References add_vars_to_targetlist(), Assert, bms_copy(), bms_get_singleton_member(), bms_union(), EquivalenceClass::ec_below_outer_join, EquivalenceClass::ec_broken, EquivalenceClass::ec_collation, EquivalenceClass::ec_members, EquivalenceClass::ec_min_security, EquivalenceClass::ec_relids, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_nullable_relids, EquivalenceMember::em_relids, 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().

1018 {
1019  EquivalenceMember **prev_ems;
1020  ListCell *lc;
1021 
1022  /*
1023  * We scan the EC members once and track the last-seen member for each
1024  * base relation. When we see another member of the same base relation,
1025  * we generate "prev_em = cur_em". This results in the minimum number of
1026  * derived clauses, but it's possible that it will fail when a different
1027  * ordering would succeed. XXX FIXME: use a UNION-FIND algorithm similar
1028  * to the way we build merged ECs. (Use a list-of-lists for each rel.)
1029  */
1030  prev_ems = (EquivalenceMember **)
1031  palloc0(root->simple_rel_array_size * sizeof(EquivalenceMember *));
1032 
1033  foreach(lc, ec->ec_members)
1034  {
1035  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1036  int relid;
1037 
1038  Assert(!cur_em->em_is_child); /* no children yet */
1039  if (!bms_get_singleton_member(cur_em->em_relids, &relid))
1040  continue;
1041  Assert(relid < root->simple_rel_array_size);
1042 
1043  if (prev_ems[relid] != NULL)
1044  {
1045  EquivalenceMember *prev_em = prev_ems[relid];
1046  Oid eq_op;
1047  RestrictInfo *rinfo;
1048 
1049  eq_op = select_equality_operator(ec,
1050  prev_em->em_datatype,
1051  cur_em->em_datatype);
1052  if (!OidIsValid(eq_op))
1053  {
1054  /* failed... */
1055  ec->ec_broken = true;
1056  break;
1057  }
1058  rinfo = process_implied_equality(root, eq_op, ec->ec_collation,
1059  prev_em->em_expr, cur_em->em_expr,
1060  bms_copy(ec->ec_relids),
1061  bms_union(prev_em->em_nullable_relids,
1062  cur_em->em_nullable_relids),
1063  ec->ec_min_security,
1064  ec->ec_below_outer_join,
1065  false);
1066 
1067  /*
1068  * If the clause didn't degenerate to a constant, fill in the
1069  * correct markings for a mergejoinable clause. We don't put it
1070  * in ec_derives however; we don't currently need to re-find such
1071  * clauses, and we don't want to clutter that list with non-join
1072  * clauses.
1073  */
1074  if (rinfo && rinfo->mergeopfamilies)
1075  {
1076  /* it's not redundant, so don't set parent_ec */
1077  rinfo->left_ec = rinfo->right_ec = ec;
1078  rinfo->left_em = prev_em;
1079  rinfo->right_em = cur_em;
1080  }
1081  }
1082  prev_ems[relid] = cur_em;
1083  }
1084 
1085  pfree(prev_ems);
1086 
1087  /*
1088  * We also have to make sure that all the Vars used in the member clauses
1089  * will be available at any join node we might try to reference them at.
1090  * For the moment we force all the Vars to be available at all join nodes
1091  * for this eclass. Perhaps this could be improved by doing some
1092  * pre-analysis of which members we prefer to join, but it's no worse than
1093  * what happened in the pre-8.3 code.
1094  */
1095  foreach(lc, ec->ec_members)
1096  {
1097  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1098  List *vars = pull_var_clause((Node *) cur_em->em_expr,
1102 
1103  add_vars_to_targetlist(root, vars, ec->ec_relids, false);
1104  list_free(vars);
1105  }
1106 }
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
Index ec_min_security
Definition: pathnodes.h:976
Relids em_nullable_relids
Definition: pathnodes.h:1016
Definition: nodes.h:528
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:615
bool ec_below_outer_join
Definition: pathnodes.h:973
EquivalenceClass * right_ec
Definition: pathnodes.h:2036
List * pull_var_clause(Node *node, int flags)
Definition: var.c:535
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
List * mergeopfamilies
Definition: pathnodes.h:2032
void add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed, bool create_new_ph)
Definition: initsplan.c:227
void pfree(void *pointer)
Definition: mcxt.c:1057
EquivalenceMember * left_em
Definition: pathnodes.h:2037
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1554
Relids ec_relids
Definition: pathnodes.h:969
int simple_rel_array_size
Definition: pathnodes.h:202
EquivalenceMember * right_em
Definition: pathnodes.h:2038
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:175
void * palloc0(Size size)
Definition: mcxt.c:981
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
EquivalenceClass * left_ec
Definition: pathnodes.h:2035
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:174
void list_free(List *list)
Definition: list.c:1376
Definition: regcomp.c:224
Definition: pg_list.h:50
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:172
List * ec_members
Definition: pathnodes.h:966
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:2261

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

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

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

2610 {
2611  List *result = NIL;
2612  bool is_child_rel = (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
2613  Relids parent_relids;
2614  int i;
2615 
2616  /* Should be OK to rely on eclass_indexes */
2617  Assert(root->ec_merging_done);
2618 
2619  /* Indexes are available only on base or "other" member relations. */
2620  Assert(IS_SIMPLE_REL(rel));
2621 
2622  /* If it's a child rel, we'll need to know what its parent(s) are */
2623  if (is_child_rel)
2624  parent_relids = find_childrel_parents(root, rel);
2625  else
2626  parent_relids = NULL; /* not used, but keep compiler quiet */
2627 
2628  i = -1;
2629  while ((i = bms_next_member(rel->eclass_indexes, i)) >= 0)
2630  {
2631  EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2632  EquivalenceMember *cur_em;
2633  ListCell *lc2;
2634 
2635  /* Sanity check eclass_indexes only contain ECs for rel */
2636  Assert(is_child_rel || bms_is_subset(rel->relids, cur_ec->ec_relids));
2637 
2638  /*
2639  * Won't generate joinclauses if const or single-member (the latter
2640  * test covers the volatile case too)
2641  */
2642  if (cur_ec->ec_has_const || list_length(cur_ec->ec_members) <= 1)
2643  continue;
2644 
2645  /*
2646  * Scan members, looking for a match to the target column. Note that
2647  * child EC members are considered, but only when they belong to the
2648  * target relation. (Unlike regular members, the same expression
2649  * could be a child member of more than one EC. Therefore, it's
2650  * potentially order-dependent which EC a child relation's target
2651  * column gets matched to. This is annoying but it only happens in
2652  * corner cases, so for now we live with just reporting the first
2653  * match. See also get_eclass_for_sort_expr.)
2654  */
2655  cur_em = NULL;
2656  foreach(lc2, cur_ec->ec_members)
2657  {
2658  cur_em = (EquivalenceMember *) lfirst(lc2);
2659  if (bms_equal(cur_em->em_relids, rel->relids) &&
2660  callback(root, rel, cur_ec, cur_em, callback_arg))
2661  break;
2662  cur_em = NULL;
2663  }
2664 
2665  if (!cur_em)
2666  continue;
2667 
2668  /*
2669  * Found our match. Scan the other EC members and attempt to generate
2670  * joinclauses.
2671  */
2672  foreach(lc2, cur_ec->ec_members)
2673  {
2674  EquivalenceMember *other_em = (EquivalenceMember *) lfirst(lc2);
2675  Oid eq_op;
2676  RestrictInfo *rinfo;
2677 
2678  if (other_em->em_is_child)
2679  continue; /* ignore children here */
2680 
2681  /* Make sure it'll be a join to a different rel */
2682  if (other_em == cur_em ||
2683  bms_overlap(other_em->em_relids, rel->relids))
2684  continue;
2685 
2686  /* Forget it if caller doesn't want joins to this rel */
2687  if (bms_overlap(other_em->em_relids, prohibited_rels))
2688  continue;
2689 
2690  /*
2691  * Also, if this is a child rel, avoid generating a useless join
2692  * to its parent rel(s).
2693  */
2694  if (is_child_rel &&
2695  bms_overlap(parent_relids, other_em->em_relids))
2696  continue;
2697 
2698  eq_op = select_equality_operator(cur_ec,
2699  cur_em->em_datatype,
2700  other_em->em_datatype);
2701  if (!OidIsValid(eq_op))
2702  continue;
2703 
2704  /* set parent_ec to mark as redundant with other joinclauses */
2705  rinfo = create_join_clause(root, cur_ec, eq_op,
2706  cur_em, other_em,
2707  cur_ec);
2708 
2709  result = lappend(result, rinfo);
2710  }
2711 
2712  /*
2713  * If somehow we failed to create any join clauses, we might as well
2714  * keep scanning the ECs for another match. But if we did make any,
2715  * we're done, because we don't want to return non-redundant clauses.
2716  */
2717  if (result)
2718  break;
2719  }
2720 
2721  return result;
2722 }
#define NIL
Definition: pg_list.h:65
static RestrictInfo * create_join_clause(PlannerInfo *root, EquivalenceClass *ec, Oid opno, EquivalenceMember *leftem, EquivalenceMember *rightem, EquivalenceClass *parent_ec)
Definition: equivclass.c:1589
RelOptKind reloptkind
Definition: pathnodes.h:661
bool ec_merging_done
Definition: pathnodes.h:266
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:637
static void * list_nth(const List *list, int n)
Definition: pg_list.h:266
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1554
Relids ec_relids
Definition: pathnodes.h:969
Relids relids
Definition: pathnodes.h:664
List * lappend(List *list, void *datum)
Definition: list.c:321
Relids find_childrel_parents(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:1243
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
List * eq_classes
Definition: pathnodes.h:264
static int list_length(const List *l)
Definition: pg_list.h:149
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
int i
Definition: pg_list.h:50
Bitmapset * eclass_indexes
Definition: pathnodes.h:706
List * ec_members
Definition: pathnodes.h:966
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94

◆ generate_join_implied_equalities()

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

Definition at line 1180 of file equivclass.c.

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

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

1184 {
1185  List *result = NIL;
1186  Relids inner_relids = inner_rel->relids;
1187  Relids nominal_inner_relids;
1188  Relids nominal_join_relids;
1189  Bitmapset *matching_ecs;
1190  int i;
1191 
1192  /* If inner rel is a child, extra setup work is needed */
1193  if (IS_OTHER_REL(inner_rel))
1194  {
1195  Assert(!bms_is_empty(inner_rel->top_parent_relids));
1196 
1197  /* Fetch relid set for the topmost parent rel */
1198  nominal_inner_relids = inner_rel->top_parent_relids;
1199  /* ECs will be marked with the parent's relid, not the child's */
1200  nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1201  }
1202  else
1203  {
1204  nominal_inner_relids = inner_relids;
1205  nominal_join_relids = join_relids;
1206  }
1207 
1208  /*
1209  * Get all eclasses that mention both inner and outer sides of the join
1210  */
1211  matching_ecs = get_common_eclass_indexes(root, nominal_inner_relids,
1212  outer_relids);
1213 
1214  i = -1;
1215  while ((i = bms_next_member(matching_ecs, i)) >= 0)
1216  {
1218  List *sublist = NIL;
1219 
1220  /* ECs containing consts do not need any further enforcement */
1221  if (ec->ec_has_const)
1222  continue;
1223 
1224  /* Single-member ECs won't generate any deductions */
1225  if (list_length(ec->ec_members) <= 1)
1226  continue;
1227 
1228  /* Sanity check that this eclass overlaps the join */
1229  Assert(bms_overlap(ec->ec_relids, nominal_join_relids));
1230 
1231  if (!ec->ec_broken)
1233  ec,
1234  join_relids,
1235  outer_relids,
1236  inner_relids);
1237 
1238  /* Recover if we failed to generate required derived clauses */
1239  if (ec->ec_broken)
1241  ec,
1242  nominal_join_relids,
1243  outer_relids,
1244  nominal_inner_relids,
1245  inner_rel);
1246 
1247  result = list_concat(result, sublist);
1248  }
1249 
1250  return result;
1251 }
#define NIL
Definition: pg_list.h:65
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:652
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
static void * list_nth(const List *list, int n)
Definition: pg_list.h:266
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:1505
Relids ec_relids
Definition: pathnodes.h:969
Relids relids
Definition: pathnodes.h:664
static List * generate_join_implied_equalities_normal(PlannerInfo *root, EquivalenceClass *ec, Relids join_relids, Relids outer_relids, Relids inner_relids)
Definition: equivclass.c:1329
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
static Bitmapset * get_common_eclass_indexes(PlannerInfo *root, Relids relids1, Relids relids2)
Definition: equivclass.c:2991
#define Assert(condition)
Definition: c.h:746
List * eq_classes
Definition: pathnodes.h:264
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
static int list_length(const List *l)
Definition: pg_list.h:149
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
int i
Definition: pg_list.h:50
List * ec_members
Definition: pathnodes.h:966
Relids top_parent_relids
Definition: pathnodes.h:737

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

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

Referenced by generate_join_implied_equalities(), and generate_join_implied_equalities_for_ecs().

1511 {
1512  List *result = NIL;
1513  ListCell *lc;
1514 
1515  foreach(lc, ec->ec_sources)
1516  {
1517  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1518  Relids clause_relids = restrictinfo->required_relids;
1519 
1520  if (bms_is_subset(clause_relids, nominal_join_relids) &&
1521  !bms_is_subset(clause_relids, outer_relids) &&
1522  !bms_is_subset(clause_relids, nominal_inner_relids))
1523  result = lappend(result, restrictinfo);
1524  }
1525 
1526  /*
1527  * If we have to translate, just brute-force apply adjust_appendrel_attrs
1528  * to all the RestrictInfos at once. This will result in returning
1529  * RestrictInfos that are not listed in ec_derives, but there shouldn't be
1530  * any duplication, and it's a sufficiently narrow corner case that we
1531  * shouldn't sweat too much over it anyway.
1532  *
1533  * Since inner_rel might be an indirect descendant of the baserel
1534  * mentioned in the ec_sources clauses, we have to be prepared to apply
1535  * multiple levels of Var translation.
1536  */
1537  if (IS_OTHER_REL(inner_rel) && result != NIL)
1538  result = (List *) adjust_appendrel_attrs_multilevel(root,
1539  (Node *) result,
1540  inner_rel->relids,
1541  inner_rel->top_parent_relids);
1542 
1543  return result;
1544 }
#define NIL
Definition: pg_list.h:65
Relids required_relids
Definition: pathnodes.h:2005
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:652
Definition: nodes.h:528
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, Relids child_relids, Relids top_parent_relids)
Definition: appendinfo.c:502
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
List * ec_sources
Definition: pathnodes.h:967
Relids relids
Definition: pathnodes.h:664
List * lappend(List *list, void *datum)
Definition: list.c:321
#define lfirst(lc)
Definition: pg_list.h:169
Definition: pg_list.h:50
Relids top_parent_relids
Definition: pathnodes.h:737

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

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

Referenced by get_joinrel_parampathinfo().

1263 {
1264  List *result = NIL;
1265  Relids inner_relids = inner_rel->relids;
1266  Relids nominal_inner_relids;
1267  Relids nominal_join_relids;
1268  ListCell *lc;
1269 
1270  /* If inner rel is a child, extra setup work is needed */
1271  if (IS_OTHER_REL(inner_rel))
1272  {
1273  Assert(!bms_is_empty(inner_rel->top_parent_relids));
1274 
1275  /* Fetch relid set for the topmost parent rel */
1276  nominal_inner_relids = inner_rel->top_parent_relids;
1277  /* ECs will be marked with the parent's relid, not the child's */
1278  nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1279  }
1280  else
1281  {
1282  nominal_inner_relids = inner_relids;
1283  nominal_join_relids = join_relids;
1284  }
1285 
1286  foreach(lc, eclasses)
1287  {
1289  List *sublist = NIL;
1290 
1291  /* ECs containing consts do not need any further enforcement */
1292  if (ec->ec_has_const)
1293  continue;
1294 
1295  /* Single-member ECs won't generate any deductions */
1296  if (list_length(ec->ec_members) <= 1)
1297  continue;
1298 
1299  /* We can quickly ignore any that don't overlap the join, too */
1300  if (!bms_overlap(ec->ec_relids, nominal_join_relids))
1301  continue;
1302 
1303  if (!ec->ec_broken)
1305  ec,
1306  join_relids,
1307  outer_relids,
1308  inner_relids);
1309 
1310  /* Recover if we failed to generate required derived clauses */
1311  if (ec->ec_broken)
1313  ec,
1314  nominal_join_relids,
1315  outer_relids,
1316  nominal_inner_relids,
1317  inner_rel);
1318 
1319  result = list_concat(result, sublist);
1320  }
1321 
1322  return result;
1323 }
#define NIL
Definition: pg_list.h:65
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:652
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
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:1505
Relids ec_relids
Definition: pathnodes.h:969
Relids relids
Definition: pathnodes.h:664
static List * generate_join_implied_equalities_normal(PlannerInfo *root, EquivalenceClass *ec, Relids join_relids, Relids outer_relids, Relids inner_relids)
Definition: equivclass.c:1329
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
static int list_length(const List *l)
Definition: pg_list.h:149
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
Definition: pg_list.h:50
List * ec_members
Definition: pathnodes.h:966
Relids top_parent_relids
Definition: pathnodes.h:737

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

References bms_is_subset(), create_join_clause(), EquivalenceClass::ec_broken, EquivalenceClass::ec_members, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_relids, exprType(), InvalidOid, IsA, lappend(), lfirst, linitial, list_concat(), NIL, OidIsValid, op_hashjoinable(), and select_equality_operator().

Referenced by generate_join_implied_equalities(), and generate_join_implied_equalities_for_ecs().

1334 {
1335  List *result = NIL;
1336  List *new_members = NIL;
1337  List *outer_members = NIL;
1338  List *inner_members = NIL;
1339  ListCell *lc1;
1340 
1341  /*
1342  * First, scan the EC to identify member values that are computable at the
1343  * outer rel, at the inner rel, or at this relation but not in either
1344  * input rel. The outer-rel members should already be enforced equal,
1345  * likewise for the inner-rel members. We'll need to create clauses to
1346  * enforce that any newly computable members are all equal to each other
1347  * as well as to at least one input member, plus enforce at least one
1348  * outer-rel member equal to at least one inner-rel member.
1349  */
1350  foreach(lc1, ec->ec_members)
1351  {
1352  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
1353 
1354  /*
1355  * We don't need to check explicitly for child EC members. This test
1356  * against join_relids will cause them to be ignored except when
1357  * considering a child inner rel, which is what we want.
1358  */
1359  if (!bms_is_subset(cur_em->em_relids, join_relids))
1360  continue; /* not computable yet, or wrong child */
1361 
1362  if (bms_is_subset(cur_em->em_relids, outer_relids))
1363  outer_members = lappend(outer_members, cur_em);
1364  else if (bms_is_subset(cur_em->em_relids, inner_relids))
1365  inner_members = lappend(inner_members, cur_em);
1366  else
1367  new_members = lappend(new_members, cur_em);
1368  }
1369 
1370  /*
1371  * First, select the joinclause if needed. We can equate any one outer
1372  * member to any one inner member, but we have to find a datatype
1373  * combination for which an opfamily member operator exists. If we have
1374  * choices, we prefer simple Var members (possibly with RelabelType) since
1375  * these are (a) cheapest to compute at runtime and (b) most likely to
1376  * have useful statistics. Also, prefer operators that are also
1377  * hashjoinable.
1378  */
1379  if (outer_members && inner_members)
1380  {
1381  EquivalenceMember *best_outer_em = NULL;
1382  EquivalenceMember *best_inner_em = NULL;
1383  Oid best_eq_op = InvalidOid;
1384  int best_score = -1;
1385  RestrictInfo *rinfo;
1386 
1387  foreach(lc1, outer_members)
1388  {
1389  EquivalenceMember *outer_em = (EquivalenceMember *) lfirst(lc1);
1390  ListCell *lc2;
1391 
1392  foreach(lc2, inner_members)
1393  {
1394  EquivalenceMember *inner_em = (EquivalenceMember *) lfirst(lc2);
1395  Oid eq_op;
1396  int score;
1397 
1398  eq_op = select_equality_operator(ec,
1399  outer_em->em_datatype,
1400  inner_em->em_datatype);
1401  if (!OidIsValid(eq_op))
1402  continue;
1403  score = 0;
1404  if (IsA(outer_em->em_expr, Var) ||
1405  (IsA(outer_em->em_expr, RelabelType) &&
1406  IsA(((RelabelType *) outer_em->em_expr)->arg, Var)))
1407  score++;
1408  if (IsA(inner_em->em_expr, Var) ||
1409  (IsA(inner_em->em_expr, RelabelType) &&
1410  IsA(((RelabelType *) inner_em->em_expr)->arg, Var)))
1411  score++;
1412  if (op_hashjoinable(eq_op,
1413  exprType((Node *) outer_em->em_expr)))
1414  score++;
1415  if (score > best_score)
1416  {
1417  best_outer_em = outer_em;
1418  best_inner_em = inner_em;
1419  best_eq_op = eq_op;
1420  best_score = score;
1421  if (best_score == 3)
1422  break; /* no need to look further */
1423  }
1424  }
1425  if (best_score == 3)
1426  break; /* no need to look further */
1427  }
1428  if (best_score < 0)
1429  {
1430  /* failed... */
1431  ec->ec_broken = true;
1432  return NIL;
1433  }
1434 
1435  /*
1436  * Create clause, setting parent_ec to mark it as redundant with other
1437  * joinclauses
1438  */
1439  rinfo = create_join_clause(root, ec, best_eq_op,
1440  best_outer_em, best_inner_em,
1441  ec);
1442 
1443  result = lappend(result, rinfo);
1444  }
1445 
1446  /*
1447  * Now deal with building restrictions for any expressions that involve
1448  * Vars from both sides of the join. We have to equate all of these to
1449  * each other as well as to at least one old member (if any).
1450  *
1451  * XXX as in generate_base_implied_equalities_no_const, we could be a lot
1452  * smarter here to avoid unnecessary failures in cross-type situations.
1453  * For now, use the same left-to-right method used there.
1454  */
1455  if (new_members)
1456  {
1457  List *old_members = list_concat(outer_members, inner_members);
1458  EquivalenceMember *prev_em = NULL;
1459  RestrictInfo *rinfo;
1460 
1461  /* For now, arbitrarily take the first old_member as the one to use */
1462  if (old_members)
1463  new_members = lappend(new_members, linitial(old_members));
1464 
1465  foreach(lc1, new_members)
1466  {
1467  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
1468 
1469  if (prev_em != NULL)
1470  {
1471  Oid eq_op;
1472 
1473  eq_op = select_equality_operator(ec,
1474  prev_em->em_datatype,
1475  cur_em->em_datatype);
1476  if (!OidIsValid(eq_op))
1477  {
1478  /* failed... */
1479  ec->ec_broken = true;
1480  return NIL;
1481  }
1482  /* do NOT set parent_ec, this qual is not redundant! */
1483  rinfo = create_join_clause(root, ec, eq_op,
1484  prev_em, cur_em,
1485  NULL);
1486 
1487  result = lappend(result, rinfo);
1488  }
1489  prev_em = cur_em;
1490  }
1491  }
1492 
1493  return result;
1494 }
#define NIL
Definition: pg_list.h:65
static RestrictInfo * create_join_clause(PlannerInfo *root, EquivalenceClass *ec, Oid opno, EquivalenceMember *leftem, EquivalenceMember *rightem, EquivalenceClass *parent_ec)
Definition: equivclass.c:1589
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
Definition: nodes.h:528
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:181
#define OidIsValid(objectId)
Definition: c.h:652
bool op_hashjoinable(Oid opno, Oid inputtype)
Definition: lsyscache.c:1354
#define linitial(l)
Definition: pg_list.h:174
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1554
List * lappend(List *list, void *datum)
Definition: list.c:321
#define InvalidOid
Definition: postgres_ext.h:36
#define lfirst(lc)
Definition: pg_list.h:169
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
Definition: pg_list.h:50
List * ec_members
Definition: pathnodes.h:966

◆ get_common_eclass_indexes()

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

Definition at line 2991 of file equivclass.c.

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

Referenced by generate_join_implied_equalities(), and have_relevant_eclass_joinclause().

2992 {
2993  Bitmapset *rel1ecs;
2994  Bitmapset *rel2ecs;
2995  int relid;
2996 
2997  rel1ecs = get_eclass_indexes_for_relids(root, relids1);
2998 
2999  /*
3000  * We can get away with just using the relation's eclass_indexes directly
3001  * when relids2 is a singleton set.
3002  */
3003  if (bms_get_singleton_member(relids2, &relid))
3004  rel2ecs = root->simple_rel_array[relid]->eclass_indexes;
3005  else
3006  rel2ecs = get_eclass_indexes_for_relids(root, relids2);
3007 
3008  /* Calculate and return the common EC indexes, recycling the left input. */
3009  return bms_int_members(rel1ecs, rel2ecs);
3010 }
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:615
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:201
static Bitmapset * get_eclass_indexes_for_relids(PlannerInfo *root, Relids relids)
Definition: equivclass.c:2967
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:902
Bitmapset * eclass_indexes
Definition: pathnodes.h:706

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

References add_eq_member(), Assert, bms_add_member(), bms_equal(), bms_intersect(), bms_next_member(), canonicalize_ec_expression(), contain_agg_clause(), contain_volatile_functions(), contain_window_function(), copyObject, EquivalenceClass::ec_below_outer_join, EquivalenceClass::ec_broken, EquivalenceClass::ec_collation, EquivalenceClass::ec_derives, EquivalenceClass::ec_has_const, EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_max_security, EquivalenceClass::ec_members, EquivalenceClass::ec_merged, PlannerInfo::ec_merging_done, EquivalenceClass::ec_min_security, EquivalenceClass::ec_opfamilies, EquivalenceClass::ec_relids, EquivalenceClass::ec_sortref, EquivalenceClass::ec_sources, RelOptInfo::eclass_indexes, elog, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, EquivalenceMember::em_relids, PlannerInfo::eq_classes, equal(), ERROR, expression_returns_set(), i, lappend(), lfirst, list_copy(), list_length(), makeNode, MemoryContextSwitchTo(), NIL, PlannerInfo::planner_cxt, pull_varnos(), RELOPT_BASEREL, RELOPT_DEADREL, RelOptInfo::reloptkind, and PlannerInfo::simple_rel_array.

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

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

◆ get_eclass_indexes_for_relids()

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

Definition at line 2967 of file equivclass.c.

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

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

2968 {
2969  Bitmapset *ec_indexes = NULL;
2970  int i = -1;
2971 
2972  /* Should be OK to rely on eclass_indexes */
2973  Assert(root->ec_merging_done);
2974 
2975  while ((i = bms_next_member(relids, i)) > 0)
2976  {
2977  RelOptInfo *rel = root->simple_rel_array[i];
2978 
2979  ec_indexes = bms_add_members(ec_indexes, rel->eclass_indexes);
2980  }
2981  return ec_indexes;
2982 }
bool ec_merging_done
Definition: pathnodes.h:266
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:201
#define Assert(condition)
Definition: c.h:746
int i
Bitmapset * eclass_indexes
Definition: pathnodes.h:706
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793

◆ has_relevant_eclass_joinclause()

bool has_relevant_eclass_joinclause ( PlannerInfo root,
RelOptInfo rel1 
)

Definition at line 2802 of file equivclass.c.

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

Referenced by build_join_rel().

2803 {
2804  Bitmapset *matched_ecs;
2805  int i;
2806 
2807  /* Examine only eclasses mentioning rel1 */
2808  matched_ecs = get_eclass_indexes_for_relids(root, rel1->relids);
2809 
2810  i = -1;
2811  while ((i = bms_next_member(matched_ecs, i)) >= 0)
2812  {
2814  i);
2815 
2816  /*
2817  * Won't generate joinclauses if single-member (this test covers the
2818  * volatile case too)
2819  */
2820  if (list_length(ec->ec_members) <= 1)
2821  continue;
2822 
2823  /*
2824  * Per the comment in have_relevant_eclass_joinclause, it's sufficient
2825  * to find an EC that mentions both this rel and some other rel.
2826  */
2827  if (!bms_is_subset(ec->ec_relids, rel1->relids))
2828  return true;
2829  }
2830 
2831  return false;
2832 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
static void * list_nth(const List *list, int n)
Definition: pg_list.h:266
static Bitmapset * get_eclass_indexes_for_relids(PlannerInfo *root, Relids relids)
Definition: equivclass.c:2967
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids ec_relids
Definition: pathnodes.h:969
Relids relids
Definition: pathnodes.h:664
List * eq_classes
Definition: pathnodes.h:264
static int list_length(const List *l)
Definition: pg_list.h:149
int i
List * ec_members
Definition: pathnodes.h:966

◆ have_relevant_eclass_joinclause()

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

Definition at line 2735 of file equivclass.c.

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

Referenced by have_relevant_joinclause().

2737 {
2738  Bitmapset *matching_ecs;
2739  int i;
2740 
2741  /* Examine only eclasses mentioning both rel1 and rel2 */
2742  matching_ecs = get_common_eclass_indexes(root, rel1->relids,
2743  rel2->relids);
2744 
2745  i = -1;
2746  while ((i = bms_next_member(matching_ecs, i)) >= 0)
2747  {
2749  i);
2750 
2751  /*
2752  * Sanity check that get_common_eclass_indexes gave only ECs
2753  * containing both rels.
2754  */
2755  Assert(bms_overlap(rel1->relids, ec->ec_relids));
2756  Assert(bms_overlap(rel2->relids, ec->ec_relids));
2757 
2758  /*
2759  * Won't generate joinclauses if single-member (this test covers the
2760  * volatile case too)
2761  */
2762  if (list_length(ec->ec_members) <= 1)
2763  continue;
2764 
2765  /*
2766  * We do not need to examine the individual members of the EC, because
2767  * all that we care about is whether each rel overlaps the relids of
2768  * at least one member, and get_common_eclass_indexes() and the single
2769  * member check above are sufficient to prove that. (As with
2770  * have_relevant_joinclause(), it is not necessary that the EC be able
2771  * to form a joinclause relating exactly the two given rels, only that
2772  * it be able to form a joinclause mentioning both, and this will
2773  * surely be true if both of them overlap ec_relids.)
2774  *
2775  * Note we don't test ec_broken; if we did, we'd need a separate code
2776  * path to look through ec_sources. Checking the membership anyway is
2777  * OK as a possibly-overoptimistic heuristic.
2778  *
2779  * We don't test ec_has_const either, even though a const eclass won't
2780  * generate real join clauses. This is because if we had "WHERE a.x =
2781  * b.y and a.x = 42", it is worth considering a join between a and b,
2782  * since the join result is likely to be small even though it'll end
2783  * up being an unqualified nestloop.
2784  */
2785 
2786  return true;
2787  }
2788 
2789  return false;
2790 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
static void * list_nth(const List *list, int n)
Definition: pg_list.h:266
Relids ec_relids
Definition: pathnodes.h:969
Relids relids
Definition: pathnodes.h:664
static Bitmapset * get_common_eclass_indexes(PlannerInfo *root, Relids relids1, Relids relids2)
Definition: equivclass.c:2991
#define Assert(condition)
Definition: c.h:746
List * eq_classes
Definition: pathnodes.h:264
static int list_length(const List *l)
Definition: pg_list.h:149
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
int i
List * ec_members
Definition: pathnodes.h:966

◆ is_redundant_derived_clause()

bool is_redundant_derived_clause ( RestrictInfo rinfo,
List clauselist 
)

Definition at line 2904 of file equivclass.c.

References lfirst, and RestrictInfo::parent_ec.

Referenced by create_tidscan_plan().

2905 {
2906  EquivalenceClass *parent_ec = rinfo->parent_ec;
2907  ListCell *lc;
2908 
2909  /* Fail if it's not a potentially-redundant clause from some EC */
2910  if (parent_ec == NULL)
2911  return false;
2912 
2913  foreach(lc, clauselist)
2914  {
2915  RestrictInfo *otherrinfo = (RestrictInfo *) lfirst(lc);
2916 
2917  if (otherrinfo->parent_ec == parent_ec)
2918  return true;
2919  }
2920 
2921  return false;
2922 }
EquivalenceClass * parent_ec
Definition: pathnodes.h:2021
#define lfirst(lc)
Definition: pg_list.h:169

◆ is_redundant_with_indexclauses()

bool is_redundant_with_indexclauses ( RestrictInfo rinfo,
List indexclauses 
)

Definition at line 2931 of file equivclass.c.

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

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

2932 {
2933  EquivalenceClass *parent_ec = rinfo->parent_ec;
2934  ListCell *lc;
2935 
2936  foreach(lc, indexclauses)
2937  {
2938  IndexClause *iclause = lfirst_node(IndexClause, lc);
2939  RestrictInfo *otherrinfo = iclause->rinfo;
2940 
2941  /* If indexclause is lossy, it won't enforce the condition exactly */
2942  if (iclause->lossy)
2943  continue;
2944 
2945  /* Match if it's same clause (pointer equality should be enough) */
2946  if (rinfo == otherrinfo)
2947  return true;
2948  /* Match if derived from same EC */
2949  if (parent_ec && otherrinfo->parent_ec == parent_ec)
2950  return true;
2951 
2952  /*
2953  * No need to look at the derived clauses in iclause->indexquals; they
2954  * couldn't match if the parent clause didn't.
2955  */
2956  }
2957 
2958  return false;
2959 }
EquivalenceClass * parent_ec
Definition: pathnodes.h:2021
#define lfirst_node(type, lc)
Definition: pg_list.h:172
struct RestrictInfo * rinfo
Definition: pathnodes.h:1255

◆ match_eclasses_to_foreign_key_col()

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

Definition at line 2192 of file equivclass.c.

References Assert, bms_intersect(), bms_next_member(), ForeignKeyOptInfo::con_relid, ForeignKeyOptInfo::confkey, ForeignKeyOptInfo::conkey, ForeignKeyOptInfo::conpfeqop, EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_members, PlannerInfo::ec_merging_done, EquivalenceClass::ec_opfamilies, 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, RangeQueryClause::var, Var::varattno, and Var::varno.

Referenced by match_foreign_keys_to_quals().

2195 {
2196  Index var1varno = fkinfo->con_relid;
2197  AttrNumber var1attno = fkinfo->conkey[colno];
2198  Index var2varno = fkinfo->ref_relid;
2199  AttrNumber var2attno = fkinfo->confkey[colno];
2200  Oid eqop = fkinfo->conpfeqop[colno];
2201  RelOptInfo *rel1 = root->simple_rel_array[var1varno];
2202  RelOptInfo *rel2 = root->simple_rel_array[var2varno];
2203  List *opfamilies = NIL; /* compute only if needed */
2204  Bitmapset *matching_ecs;
2205  int i;
2206 
2207  /* Consider only eclasses mentioning both relations */
2208  Assert(root->ec_merging_done);
2209  Assert(IS_SIMPLE_REL(rel1));
2210  Assert(IS_SIMPLE_REL(rel2));
2211  matching_ecs = bms_intersect(rel1->eclass_indexes,
2212  rel2->eclass_indexes);
2213 
2214  i = -1;
2215  while ((i = bms_next_member(matching_ecs, i)) >= 0)
2216  {
2218  i);
2219  EquivalenceMember *item1_em = NULL;
2220  EquivalenceMember *item2_em = NULL;
2221  ListCell *lc2;
2222 
2223  /* Never match to a volatile EC */
2224  if (ec->ec_has_volatile)
2225  continue;
2226  /* Note: it seems okay to match to "broken" eclasses here */
2227 
2228  foreach(lc2, ec->ec_members)
2229  {
2231  Var *var;
2232 
2233  if (em->em_is_child)
2234  continue; /* ignore children here */
2235 
2236  /* EM must be a Var, possibly with RelabelType */
2237  var = (Var *) em->em_expr;
2238  while (var && IsA(var, RelabelType))
2239  var = (Var *) ((RelabelType *) var)->arg;
2240  if (!(var && IsA(var, Var)))
2241  continue;
2242 
2243  /* Match? */
2244  if (var->varno == var1varno && var->varattno == var1attno)
2245  item1_em = em;
2246  else if (var->varno == var2varno && var->varattno == var2attno)
2247  item2_em = em;
2248 
2249  /* Have we found both PK and FK column in this EC? */
2250  if (item1_em && item2_em)
2251  {
2252  /*
2253  * Succeed if eqop matches EC's opfamilies. We could test
2254  * this before scanning the members, but it's probably cheaper
2255  * to test for member matches first.
2256  */
2257  if (opfamilies == NIL) /* compute if we didn't already */
2258  opfamilies = get_mergejoin_opfamilies(eqop);
2259  if (equal(opfamilies, ec->ec_opfamilies))
2260  {
2261  fkinfo->eclass[colno] = ec;
2262  fkinfo->fk_eclass_member[colno] = item2_em;
2263  return ec;
2264  }
2265  /* Otherwise, done with this EC, move on to the next */
2266  break;
2267  }
2268  }
2269  }
2270  return NULL;
2271 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
bool ec_merging_done
Definition: pathnodes.h:266
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3032
List * get_mergejoin_opfamilies(Oid opno)
Definition: lsyscache.c:364
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
AttrNumber varattno
Definition: primnodes.h:186
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:181
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:637
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:201
static void * list_nth(const List *list, int n)
Definition: pg_list.h:266
Oid conpfeqop[INDEX_MAX_KEYS]
Definition: pathnodes.h:888
struct EquivalenceClass * eclass[INDEX_MAX_KEYS]
Definition: pathnodes.h:896
Index varno
Definition: primnodes.h:184
List * ec_opfamilies
Definition: pathnodes.h:964
struct EquivalenceMember * fk_eclass_member[INDEX_MAX_KEYS]
Definition: pathnodes.h:898
AttrNumber conkey[INDEX_MAX_KEYS]
Definition: pathnodes.h:886
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:259
unsigned int Index
Definition: c.h:483
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
List * eq_classes
Definition: pathnodes.h:264
bool ec_has_volatile
Definition: pathnodes.h:972
int i
Definition: pg_list.h:50
AttrNumber confkey[INDEX_MAX_KEYS]
Definition: pathnodes.h:887
int16 AttrNumber
Definition: attnum.h:21
Bitmapset * eclass_indexes
Definition: pathnodes.h:706
List * ec_members
Definition: pathnodes.h:966

◆ process_equivalence()

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

Definition at line 118 of file equivclass.c.

References add_eq_member(), NullTest::arg, NullTest::argisrow, Assert, bms_intersect(), bms_is_empty(), bms_join(), canonicalize_ec_expression(), RestrictInfo::clause, contain_nonstrict_functions(), EquivalenceClass::ec_below_outer_join, EquivalenceClass::ec_broken, EquivalenceClass::ec_collation, EquivalenceClass::ec_derives, EquivalenceClass::ec_has_const, EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_max_security, EquivalenceClass::ec_members, EquivalenceClass::ec_merged, PlannerInfo::ec_merging_done, EquivalenceClass::ec_min_security, EquivalenceClass::ec_opfamilies, EquivalenceClass::ec_relids, EquivalenceClass::ec_sortref, EquivalenceClass::ec_sources, elog, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, PlannerInfo::eq_classes, equal(), ERROR, exprType(), 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().

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

◆ reconsider_full_join_clause()

static bool reconsider_full_join_clause ( PlannerInfo root,
RestrictInfo rinfo 
)
static

Definition at line 1962 of file equivclass.c.

References CoalesceExpr::args, Assert, bms_copy(), bms_intersect(), build_implied_join_equality(), RestrictInfo::clause, EquivalenceClass::ec_collation, EquivalenceClass::ec_has_const, EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_members, EquivalenceClass::ec_min_security, EquivalenceClass::ec_opfamilies, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, PlannerInfo::eq_classes, equal(), foreach_current_index, get_leftop(), get_rightop(), 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().

1963 {
1964  Expr *leftvar;
1965  Expr *rightvar;
1966  Oid opno,
1967  collation,
1968  left_type,
1969  right_type;
1970  Relids left_relids,
1971  right_relids,
1972  left_nullable_relids,
1973  right_nullable_relids;
1974  ListCell *lc1;
1975 
1976  /* Can't use an outerjoin_delayed clause here */
1977  if (rinfo->outerjoin_delayed)
1978  return false;
1979 
1980  /* Extract needed info from the clause */
1981  Assert(is_opclause(rinfo->clause));
1982  opno = ((OpExpr *) rinfo->clause)->opno;
1983  collation = ((OpExpr *) rinfo->clause)->inputcollid;
1984  op_input_types(opno, &left_type, &right_type);
1985  leftvar = (Expr *) get_leftop(rinfo->clause);
1986  rightvar = (Expr *) get_rightop(rinfo->clause);
1987  left_relids = rinfo->left_relids;
1988  right_relids = rinfo->right_relids;
1989  left_nullable_relids = bms_intersect(left_relids,
1990  rinfo->nullable_relids);
1991  right_nullable_relids = bms_intersect(right_relids,
1992  rinfo->nullable_relids);
1993 
1994  foreach(lc1, root->eq_classes)
1995  {
1996  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
1997  EquivalenceMember *coal_em = NULL;
1998  bool match;
1999  bool matchleft;
2000  bool matchright;
2001  ListCell *lc2;
2002  int coal_idx = -1;
2003 
2004  /* Ignore EC unless it contains pseudoconstants */
2005  if (!cur_ec->ec_has_const)
2006  continue;
2007  /* Never match to a volatile EC */
2008  if (cur_ec->ec_has_volatile)
2009  continue;
2010  /* It has to match the outer-join clause as to semantics, too */
2011  if (collation != cur_ec->ec_collation)
2012  continue;
2013  if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
2014  continue;
2015 
2016  /*
2017  * Does it contain a COALESCE(leftvar, rightvar) construct?
2018  *
2019  * We can assume the COALESCE() inputs are in the same order as the
2020  * join clause, since both were automatically generated in the cases
2021  * we care about.
2022  *
2023  * XXX currently this may fail to match in cross-type cases because
2024  * the COALESCE will contain typecast operations while the join clause
2025  * may not (if there is a cross-type mergejoin operator available for
2026  * the two column types). Is it OK to strip implicit coercions from
2027  * the COALESCE arguments?
2028  */
2029  match = false;
2030  foreach(lc2, cur_ec->ec_members)
2031  {
2032  coal_em = (EquivalenceMember *) lfirst(lc2);
2033  Assert(!coal_em->em_is_child); /* no children yet */
2034  if (IsA(coal_em->em_expr, CoalesceExpr))
2035  {
2036  CoalesceExpr *cexpr = (CoalesceExpr *) coal_em->em_expr;
2037  Node *cfirst;
2038  Node *csecond;
2039 
2040  if (list_length(cexpr->args) != 2)
2041  continue;
2042  cfirst = (Node *) linitial(cexpr->args);
2043  csecond = (Node *) lsecond(cexpr->args);
2044 
2045  if (equal(leftvar, cfirst) && equal(rightvar, csecond))
2046  {
2047  coal_idx = foreach_current_index(lc2);
2048  match = true;
2049  break;
2050  }
2051  }
2052  }
2053  if (!match)
2054  continue; /* no match, so ignore this EC */
2055 
2056  /*
2057  * Yes it does! Try to generate clauses LEFTVAR = CONSTANT and
2058  * RIGHTVAR = CONSTANT for each CONSTANT in the EC. Note that we must
2059  * succeed with at least one constant for each var before we can
2060  * decide to throw away the outer-join clause.
2061  */
2062  matchleft = matchright = false;
2063  foreach(lc2, cur_ec->ec_members)
2064  {
2065  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2066  Oid eq_op;
2067  RestrictInfo *newrinfo;
2068 
2069  if (!cur_em->em_is_const)
2070  continue; /* ignore non-const members */
2071  eq_op = select_equality_operator(cur_ec,
2072  left_type,
2073  cur_em->em_datatype);
2074  if (OidIsValid(eq_op))
2075  {
2076  newrinfo = build_implied_join_equality(eq_op,
2077  cur_ec->ec_collation,
2078  leftvar,
2079  cur_em->em_expr,
2080  bms_copy(left_relids),
2081  bms_copy(left_nullable_relids),
2082  cur_ec->ec_min_security);
2083  if (process_equivalence(root, &newrinfo, true))
2084  matchleft = true;
2085  }
2086  eq_op = select_equality_operator(cur_ec,
2087  right_type,
2088  cur_em->em_datatype);
2089  if (OidIsValid(eq_op))
2090  {
2091  newrinfo = build_implied_join_equality(eq_op,
2092  cur_ec->ec_collation,
2093  rightvar,
2094  cur_em->em_expr,
2095  bms_copy(right_relids),
2096  bms_copy(right_nullable_relids),
2097  cur_ec->ec_min_security);
2098  if (process_equivalence(root, &newrinfo, true))
2099  matchright = true;
2100  }
2101  }
2102 
2103  /*
2104  * If we were able to equate both vars to constants, we're done, and
2105  * we can throw away the full-join clause as redundant. Moreover, we
2106  * can remove the COALESCE entry from the EC, since the added
2107  * restrictions ensure it will always have the expected value. (We
2108  * don't bother trying to update ec_relids or ec_sources.)
2109  */
2110  if (matchleft && matchright)
2111  {
2112  cur_ec->ec_members = list_delete_nth_cell(cur_ec->ec_members, coal_idx);
2113  return true;
2114  }
2115 
2116  /*
2117  * Otherwise, fall out of the search loop, since we know the COALESCE
2118  * appears in at most one EC (XXX might stop being true if we allow
2119  * stripping of coercions above?)
2120  */
2121  break;
2122  }
2123 
2124  return false; /* failed to make any deduction */
2125 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3032
Index ec_min_security
Definition: pathnodes.h:976
Definition: nodes.h:528
Relids left_relids
Definition: pathnodes.h:2014
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
List * mergeopfamilies
Definition: pathnodes.h:2032
#define lsecond(l)
Definition: pg_list.h:179
#define linitial(l)
Definition: pg_list.h:174
bool process_equivalence(PlannerInfo *root, RestrictInfo **p_restrictinfo, bool below_outer_join)
Definition: equivclass.c:118
List * args
Definition: primnodes.h:1089
bool outerjoin_delayed
Definition: pathnodes.h:1991
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1554
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1275
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:73
Expr * clause
Definition: pathnodes.h:1987
HashCompareFunc match
Definition: dynahash.c:223
List * ec_opfamilies
Definition: pathnodes.h:964
Relids nullable_relids
Definition: pathnodes.h:2011
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:259
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:85
Relids right_relids
Definition: pathnodes.h:2015
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
List * eq_classes
Definition: pathnodes.h:264
static int list_length(const List *l)
Definition: pg_list.h:149
bool ec_has_volatile
Definition: pathnodes.h:972
RestrictInfo * build_implied_join_equality(Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
Definition: initsplan.c:2410
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:66
List * list_delete_nth_cell(List *list, int n)
Definition: list.c:696
#define foreach_current_index(cell)
Definition: pg_list.h:370
List * ec_members
Definition: pathnodes.h:966

◆ reconsider_outer_join_clause()

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

Definition at line 1837 of file equivclass.c.

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

Referenced by reconsider_outer_join_clauses().

1839 {
1840  Expr *outervar,
1841  *innervar;
1842  Oid opno,
1843  collation,
1844  left_type,
1845  right_type,
1846  inner_datatype;
1847  Relids inner_relids,
1848  inner_nullable_relids;
1849  ListCell *lc1;
1850 
1851  Assert(is_opclause(rinfo->clause));
1852  opno = ((OpExpr *) rinfo->clause)->opno;
1853  collation = ((OpExpr *) rinfo->clause)->inputcollid;
1854 
1855  /* If clause is outerjoin_delayed, operator must be strict */
1856  if (rinfo->outerjoin_delayed && !op_strict(opno))
1857  return false;
1858 
1859  /* Extract needed info from the clause */
1860  op_input_types(opno, &left_type, &right_type);
1861  if (outer_on_left)
1862  {
1863  outervar = (Expr *) get_leftop(rinfo->clause);
1864  innervar = (Expr *) get_rightop(rinfo->clause);
1865  inner_datatype = right_type;
1866  inner_relids = rinfo->right_relids;
1867  }
1868  else
1869  {
1870  outervar = (Expr *) get_rightop(rinfo->clause);
1871  innervar = (Expr *) get_leftop(rinfo->clause);
1872  inner_datatype = left_type;
1873  inner_relids = rinfo->left_relids;
1874  }
1875  inner_nullable_relids = bms_intersect(inner_relids,
1876  rinfo->nullable_relids);
1877 
1878  /* Scan EquivalenceClasses for a match to outervar */
1879  foreach(lc1, root->eq_classes)
1880  {
1881  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
1882  bool match;
1883  ListCell *lc2;
1884 
1885  /* Ignore EC unless it contains pseudoconstants */
1886  if (!cur_ec->ec_has_const)
1887  continue;
1888  /* Never match to a volatile EC */
1889  if (cur_ec->ec_has_volatile)
1890  continue;
1891  /* It has to match the outer-join clause as to semantics, too */
1892  if (collation != cur_ec->ec_collation)
1893  continue;
1894  if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
1895  continue;
1896  /* Does it contain a match to outervar? */
1897  match = false;
1898  foreach(lc2, cur_ec->ec_members)
1899  {
1900  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
1901 
1902  Assert(!cur_em->em_is_child); /* no children yet */
1903  if (equal(outervar, cur_em->em_expr))
1904  {
1905  match = true;
1906  break;
1907  }
1908  }
1909  if (!match)
1910  continue; /* no match, so ignore this EC */
1911 
1912  /*
1913  * Yes it does! Try to generate a clause INNERVAR = CONSTANT for each
1914  * CONSTANT in the EC. Note that we must succeed with at least one
1915  * constant before we can decide to throw away the outer-join clause.
1916  */
1917  match = false;
1918  foreach(lc2, cur_ec->ec_members)
1919  {
1920  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
1921  Oid eq_op;
1922  RestrictInfo *newrinfo;
1923 
1924  if (!cur_em->em_is_const)
1925  continue; /* ignore non-const members */
1926  eq_op = select_equality_operator(cur_ec,
1927  inner_datatype,
1928  cur_em->em_datatype);
1929  if (!OidIsValid(eq_op))
1930  continue; /* can't generate equality */
1931  newrinfo = build_implied_join_equality(eq_op,
1932  cur_ec->ec_collation,
1933  innervar,
1934  cur_em->em_expr,
1935  bms_copy(inner_relids),
1936  bms_copy(inner_nullable_relids),
1937  cur_ec->ec_min_security);
1938  if (process_equivalence(root, &newrinfo, true))
1939  match = true;
1940  }
1941 
1942  /*
1943  * If we were able to equate INNERVAR to any constant, report success.
1944  * Otherwise, fall out of the search loop, since we know the OUTERVAR
1945  * appears in at most one EC.
1946  */
1947  if (match)
1948  return true;
1949  else
1950  break;
1951  }
1952 
1953  return false; /* failed to make any deduction */
1954 }
bool op_strict(Oid opno)
Definition: lsyscache.c:1389
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3032
Index ec_min_security
Definition: pathnodes.h:976
Relids left_relids
Definition: pathnodes.h:2014
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
List * mergeopfamilies
Definition: pathnodes.h:2032
bool process_equivalence(PlannerInfo *root, RestrictInfo **p_restrictinfo, bool below_outer_join)
Definition: equivclass.c:118
bool outerjoin_delayed
Definition: pathnodes.h:1991
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1554
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1275
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:73
Expr * clause
Definition: pathnodes.h:1987
HashCompareFunc match
Definition: dynahash.c:223
List * ec_opfamilies
Definition: pathnodes.h:964
Relids nullable_relids
Definition: pathnodes.h:2011
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:259
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:85
Relids right_relids
Definition: pathnodes.h:2015
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
List * eq_classes
Definition: pathnodes.h:264
bool ec_has_volatile
Definition: pathnodes.h:972
RestrictInfo * build_implied_join_equality(Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
Definition: initsplan.c:2410
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:66
List * ec_members
Definition: pathnodes.h:966

◆ reconsider_outer_join_clauses()

void reconsider_outer_join_clauses ( PlannerInfo root)

Definition at line 1742 of file equivclass.c.

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

Referenced by query_planner().

1743 {
1744  bool found;
1745  ListCell *cell;
1746 
1747  /* Outer loop repeats until we find no more deductions */
1748  do
1749  {
1750  found = false;
1751 
1752  /* Process the LEFT JOIN clauses */
1753  foreach(cell, root->left_join_clauses)
1754  {
1755  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1756 
1757  if (reconsider_outer_join_clause(root, rinfo, true))
1758  {
1759  found = true;
1760  /* remove it from the list */
1761  root->left_join_clauses =
1763  /* we throw it back anyway (see notes above) */
1764  /* but the thrown-back clause has no extra selectivity */
1765  rinfo->norm_selec = 2.0;
1766  rinfo->outer_selec = 1.0;
1767  distribute_restrictinfo_to_rels(root, rinfo);
1768  }
1769  }
1770 
1771  /* Process the RIGHT JOIN clauses */
1772  foreach(cell, root->right_join_clauses)
1773  {
1774  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1775 
1776  if (reconsider_outer_join_clause(root, rinfo, false))
1777  {
1778  found = true;
1779  /* remove it from the list */
1780  root->right_join_clauses =
1782  /* we throw it back anyway (see notes above) */
1783  /* but the thrown-back clause has no extra selectivity */
1784  rinfo->norm_selec = 2.0;
1785  rinfo->outer_selec = 1.0;
1786  distribute_restrictinfo_to_rels(root, rinfo);
1787  }
1788  }
1789 
1790  /* Process the FULL JOIN clauses */
1791  foreach(cell, root->full_join_clauses)
1792  {
1793  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1794 
1795  if (reconsider_full_join_clause(root, rinfo))
1796  {
1797  found = true;
1798  /* remove it from the list */
1799  root->full_join_clauses =
1801  /* we throw it back anyway (see notes above) */
1802  /* but the thrown-back clause has no extra selectivity */
1803  rinfo->norm_selec = 2.0;
1804  rinfo->outer_selec = 1.0;
1805  distribute_restrictinfo_to_rels(root, rinfo);
1806  }
1807  }
1808  } while (found);
1809 
1810  /* Now, any remaining clauses have to be thrown back */
1811  foreach(cell, root->left_join_clauses)
1812  {
1813  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1814 
1815  distribute_restrictinfo_to_rels(root, rinfo);
1816  }
1817  foreach(cell, root->right_join_clauses)
1818  {
1819  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1820 
1821  distribute_restrictinfo_to_rels(root, rinfo);
1822  }
1823  foreach(cell, root->full_join_clauses)
1824  {
1825  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1826 
1827  distribute_restrictinfo_to_rels(root, rinfo);
1828  }
1829 }
static bool reconsider_full_join_clause(PlannerInfo *root, RestrictInfo *rinfo)
Definition: equivclass.c:1962
static bool reconsider_outer_join_clause(PlannerInfo *root, RestrictInfo *rinfo, bool outer_on_left)
Definition: equivclass.c:1837
Selectivity norm_selec
Definition: pathnodes.h:2025
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:357
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2173
List * left_join_clauses
Definition: pathnodes.h:270
List * full_join_clauses
Definition: pathnodes.h:278
Selectivity outer_selec
Definition: pathnodes.h:2028
#define lfirst(lc)
Definition: pg_list.h:169
List * right_join_clauses
Definition: pathnodes.h:274

◆ select_equality_operator()

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

Definition at line 1554 of file equivclass.c.

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

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

1555 {
1556  ListCell *lc;
1557 
1558  foreach(lc, ec->ec_opfamilies)
1559  {
1560  Oid opfamily = lfirst_oid(lc);
1561  Oid opno;
1562 
1563  opno = get_opfamily_member(opfamily, lefttype, righttype,
1565  if (!OidIsValid(opno))
1566  continue;
1567  /* If no barrier quals in query, don't worry about leaky operators */
1568  if (ec->ec_max_security == 0)
1569  return opno;
1570  /* Otherwise, insist that selected operators be leakproof */
1571  if (get_func_leakproof(get_opcode(opno)))
1572  return opno;
1573  }
1574  return InvalidOid;
1575 }
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:1749
Index ec_max_security
Definition: pathnodes.h:977
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:164
List * ec_opfamilies
Definition: pathnodes.h:964
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1202
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define lfirst_oid(lc)
Definition: pg_list.h:171