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)
 
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 2384 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(), RelOptInfo::relids, RELOPT_JOINREL, RELOPT_OTHER_JOINREL, RelOptInfo::reloptkind, and RelOptInfo::top_parent_relids.

Referenced by build_child_join_rel().

2388 {
2389  Relids top_parent_relids = child_joinrel->top_parent_relids;
2390  Relids child_relids = child_joinrel->relids;
2391  Bitmapset *matching_ecs;
2392  int i;
2393 
2394  Assert(IS_JOIN_REL(child_joinrel) && IS_JOIN_REL(parent_joinrel));
2395 
2396  /* We need consider only ECs that mention the parent joinrel */
2397  matching_ecs = get_eclass_indexes_for_relids(root, top_parent_relids);
2398 
2399  i = -1;
2400  while ((i = bms_next_member(matching_ecs, i)) >= 0)
2401  {
2402  EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2403  int num_members;
2404 
2405  /*
2406  * If this EC contains a volatile expression, then generating child
2407  * EMs would be downright dangerous, so skip it. We rely on a
2408  * volatile EC having only one EM.
2409  */
2410  if (cur_ec->ec_has_volatile)
2411  continue;
2412 
2413  /* Sanity check on get_eclass_indexes_for_relids result */
2414  Assert(bms_overlap(top_parent_relids, cur_ec->ec_relids));
2415 
2416  /*
2417  * We don't use foreach() here because there's no point in scanning
2418  * newly-added child members, so we can stop after the last
2419  * pre-existing EC member.
2420  */
2421  num_members = list_length(cur_ec->ec_members);
2422  for (int pos = 0; pos < num_members; pos++)
2423  {
2424  EquivalenceMember *cur_em = (EquivalenceMember *) list_nth(cur_ec->ec_members, pos);
2425 
2426  if (cur_em->em_is_const)
2427  continue; /* ignore consts here */
2428 
2429  /*
2430  * We consider only original EC members here, not
2431  * already-transformed child members.
2432  */
2433  if (cur_em->em_is_child)
2434  continue; /* ignore children here */
2435 
2436  /*
2437  * We may ignore expressions that reference a single baserel,
2438  * because add_child_rel_equivalences should have handled them.
2439  */
2440  if (bms_membership(cur_em->em_relids) != BMS_MULTIPLE)
2441  continue;
2442 
2443  /* Does this member reference child's topmost parent rel? */
2444  if (bms_overlap(cur_em->em_relids, top_parent_relids))
2445  {
2446  /* Yes, generate transformed child version */
2447  Expr *child_expr;
2448  Relids new_relids;
2449  Relids new_nullable_relids;
2450 
2451  if (parent_joinrel->reloptkind == RELOPT_JOINREL)
2452  {
2453  /* Simple single-level transformation */
2454  child_expr = (Expr *)
2456  (Node *) cur_em->em_expr,
2457  nappinfos, appinfos);
2458  }
2459  else
2460  {
2461  /* Must do multi-level transformation */
2462  Assert(parent_joinrel->reloptkind == RELOPT_OTHER_JOINREL);
2463  child_expr = (Expr *)
2465  (Node *) cur_em->em_expr,
2466  child_relids,
2467  top_parent_relids);
2468  }
2469 
2470  /*
2471  * Transform em_relids to match. Note we do *not* do
2472  * pull_varnos(child_expr) here, as for example the
2473  * transformation might have substituted a constant, but we
2474  * don't want the child member to be marked as constant.
2475  */
2476  new_relids = bms_difference(cur_em->em_relids,
2477  top_parent_relids);
2478  new_relids = bms_add_members(new_relids, child_relids);
2479 
2480  /*
2481  * For nullable_relids, we must selectively replace parent
2482  * nullable relids with child ones.
2483  */
2484  new_nullable_relids = cur_em->em_nullable_relids;
2485  if (bms_overlap(new_nullable_relids, top_parent_relids))
2486  new_nullable_relids =
2488  new_nullable_relids,
2489  child_relids,
2490  top_parent_relids);
2491 
2492  (void) add_eq_member(cur_ec, child_expr,
2493  new_relids, new_nullable_relids,
2494  true, cur_em->em_datatype);
2495  }
2496  }
2497  }
2498 }
RelOptKind reloptkind
Definition: pathnodes.h:662
Relids em_nullable_relids
Definition: pathnodes.h:1014
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:529
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:643
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:277
static Bitmapset * get_eclass_indexes_for_relids(PlannerInfo *root, Relids relids)
Definition: equivclass.c:2887
Relids ec_relids
Definition: pathnodes.h:967
Relids relids
Definition: pathnodes.h:665
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
#define Assert(condition)
Definition: c.h:738
List * eq_classes
Definition: pathnodes.h:266
static int list_length(const List *l)
Definition: pg_list.h:169
bool ec_has_volatile
Definition: pathnodes.h:970
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:549
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793
List * ec_members
Definition: pathnodes.h:964
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:194
Relids top_parent_relids
Definition: pathnodes.h:738

◆ add_child_rel_equivalences()

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

Definition at line 2256 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().

2260 {
2261  Relids top_parent_relids = child_rel->top_parent_relids;
2262  Relids child_relids = child_rel->relids;
2263  int i;
2264 
2265  /*
2266  * EC merging should be complete already, so we can use the parent rel's
2267  * eclass_indexes to avoid searching all of root->eq_classes.
2268  */
2269  Assert(root->ec_merging_done);
2270  Assert(IS_SIMPLE_REL(parent_rel));
2271 
2272  i = -1;
2273  while ((i = bms_next_member(parent_rel->eclass_indexes, i)) >= 0)
2274  {
2275  EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2276  int num_members;
2277 
2278  /*
2279  * If this EC contains a volatile expression, then generating child
2280  * EMs would be downright dangerous, so skip it. We rely on a
2281  * volatile EC having only one EM.
2282  */
2283  if (cur_ec->ec_has_volatile)
2284  continue;
2285 
2286  /* Sanity check eclass_indexes only contain ECs for parent_rel */
2287  Assert(bms_is_subset(top_parent_relids, cur_ec->ec_relids));
2288 
2289  /*
2290  * We don't use foreach() here because there's no point in scanning
2291  * newly-added child members, so we can stop after the last
2292  * pre-existing EC member.
2293  */
2294  num_members = list_length(cur_ec->ec_members);
2295  for (int pos = 0; pos < num_members; pos++)
2296  {
2297  EquivalenceMember *cur_em = (EquivalenceMember *) list_nth(cur_ec->ec_members, pos);
2298 
2299  if (cur_em->em_is_const)
2300  continue; /* ignore consts here */
2301 
2302  /*
2303  * We consider only original EC members here, not
2304  * already-transformed child members. Otherwise, if some original
2305  * member expression references more than one appendrel, we'd get
2306  * an O(N^2) explosion of useless derived expressions for
2307  * combinations of children. (But add_child_join_rel_equivalences
2308  * may add targeted combinations for partitionwise-join purposes.)
2309  */
2310  if (cur_em->em_is_child)
2311  continue; /* ignore children here */
2312 
2313  /* Does this member reference child's topmost parent rel? */
2314  if (bms_overlap(cur_em->em_relids, top_parent_relids))
2315  {
2316  /* Yes, generate transformed child version */
2317  Expr *child_expr;
2318  Relids new_relids;
2319  Relids new_nullable_relids;
2320 
2321  if (parent_rel->reloptkind == RELOPT_BASEREL)
2322  {
2323  /* Simple single-level transformation */
2324  child_expr = (Expr *)
2326  (Node *) cur_em->em_expr,
2327  1, &appinfo);
2328  }
2329  else
2330  {
2331  /* Must do multi-level transformation */
2332  child_expr = (Expr *)
2334  (Node *) cur_em->em_expr,
2335  child_relids,
2336  top_parent_relids);
2337  }
2338 
2339  /*
2340  * Transform em_relids to match. Note we do *not* do
2341  * pull_varnos(child_expr) here, as for example the
2342  * transformation might have substituted a constant, but we
2343  * don't want the child member to be marked as constant.
2344  */
2345  new_relids = bms_difference(cur_em->em_relids,
2346  top_parent_relids);
2347  new_relids = bms_add_members(new_relids, child_relids);
2348 
2349  /*
2350  * And likewise for nullable_relids. Note this code assumes
2351  * parent and child relids are singletons.
2352  */
2353  new_nullable_relids = cur_em->em_nullable_relids;
2354  if (bms_overlap(new_nullable_relids, top_parent_relids))
2355  {
2356  new_nullable_relids = bms_difference(new_nullable_relids,
2357  top_parent_relids);
2358  new_nullable_relids = bms_add_members(new_nullable_relids,
2359  child_relids);
2360  }
2361 
2362  (void) add_eq_member(cur_ec, child_expr,
2363  new_relids, new_nullable_relids,
2364  true, cur_em->em_datatype);
2365 
2366  /* Record this EC index for the child rel */
2367  child_rel->eclass_indexes = bms_add_member(child_rel->eclass_indexes, i);
2368  }
2369  }
2370  }
2371 }
RelOptKind reloptkind
Definition: pathnodes.h:662
bool ec_merging_done
Definition: pathnodes.h:268
Relids em_nullable_relids
Definition: pathnodes.h:1014
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:529
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:638
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:277
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids ec_relids
Definition: pathnodes.h:967
Relids relids
Definition: pathnodes.h:665
#define Assert(condition)
Definition: c.h:738
List * eq_classes
Definition: pathnodes.h:266
static int list_length(const List *l)
Definition: pg_list.h:169
bool ec_has_volatile
Definition: pathnodes.h:970
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:549
Bitmapset * eclass_indexes
Definition: pathnodes.h:707
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793
List * ec_members
Definition: pathnodes.h:964
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:194
Relids top_parent_relids
Definition: pathnodes.h:738

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

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

◆ canonicalize_ec_expression()

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

Definition at line 499 of file equivclass.c.

References arg, COERCE_IMPLICIT_CAST, exprCollation(), exprType(), exprTypmod(), IsA, and makeRelabelType().

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

500 {
501  Oid expr_type = exprType((Node *) expr);
502 
503  /*
504  * For a polymorphic-input-type opclass, just keep the same exposed type.
505  * RECORD opclasses work like polymorphic-type ones for this purpose.
506  */
507  if (IsPolymorphicType(req_type) || req_type == RECORDOID)
508  req_type = expr_type;
509 
510  /*
511  * No work if the expression exposes the right type/collation already.
512  */
513  if (expr_type != req_type ||
514  exprCollation((Node *) expr) != req_collation)
515  {
516  /*
517  * Strip any existing RelabelType, then add a new one if needed. This
518  * is to preserve the invariant of no redundant RelabelTypes.
519  *
520  * If we have to change the exposed type of the stripped expression,
521  * set typmod to -1 (since the new type may not have the same typmod
522  * interpretation). If we only have to change collation, preserve the
523  * exposed typmod.
524  */
525  while (expr && IsA(expr, RelabelType))
526  expr = (Expr *) ((RelabelType *) expr)->arg;
527 
528  if (exprType((Node *) expr) != req_type)
529  expr = (Expr *) makeRelabelType(expr,
530  req_type,
531  -1,
532  req_collation,
534  else if (exprCollation((Node *) expr) != req_collation)
535  expr = (Expr *) makeRelabelType(expr,
536  req_type,
537  exprTypmod((Node *) expr),
538  req_collation,
540  }
541 
542  return expr;
543 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:275
Definition: nodes.h:529
unsigned int Oid
Definition: postgres_ext.h:31
RelabelType * makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat)
Definition: makefuncs.c:402
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:719
void * arg

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

1568 {
1569  RestrictInfo *rinfo;
1570  ListCell *lc;
1571  MemoryContext oldcontext;
1572 
1573  /*
1574  * Search to see if we already built a RestrictInfo for this pair of
1575  * EquivalenceMembers. We can use either original source clauses or
1576  * previously-derived clauses. The check on opno is probably redundant,
1577  * but be safe ...
1578  */
1579  foreach(lc, ec->ec_sources)
1580  {
1581  rinfo = (RestrictInfo *) lfirst(lc);
1582  if (rinfo->left_em == leftem &&
1583  rinfo->right_em == rightem &&
1584  rinfo->parent_ec == parent_ec &&
1585  opno == ((OpExpr *) rinfo->clause)->opno)
1586  return rinfo;
1587  }
1588 
1589  foreach(lc, ec->ec_derives)
1590  {
1591  rinfo = (RestrictInfo *) lfirst(lc);
1592  if (rinfo->left_em == leftem &&
1593  rinfo->right_em == rightem &&
1594  rinfo->parent_ec == parent_ec &&
1595  opno == ((OpExpr *) rinfo->clause)->opno)
1596  return rinfo;
1597  }
1598 
1599  /*
1600  * Not there, so build it, in planner context so we can re-use it. (Not
1601  * important in normal planning, but definitely so in GEQO.)
1602  */
1603  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
1604 
1605  rinfo = build_implied_join_equality(opno,
1606  ec->ec_collation,
1607  leftem->em_expr,
1608  rightem->em_expr,
1609  bms_union(leftem->em_relids,
1610  rightem->em_relids),
1611  bms_union(leftem->em_nullable_relids,
1612  rightem->em_nullable_relids),
1613  ec->ec_min_security);
1614 
1615  /* Mark the clause as redundant, or not */
1616  rinfo->parent_ec = parent_ec;
1617 
1618  /*
1619  * We know the correct values for left_ec/right_ec, ie this particular EC,
1620  * so we can just set them directly instead of forcing another lookup.
1621  */
1622  rinfo->left_ec = ec;
1623  rinfo->right_ec = ec;
1624 
1625  /* Mark it as usable with these EMs */
1626  rinfo->left_em = leftem;
1627  rinfo->right_em = rightem;
1628  /* and save it for possible re-use */
1629  ec->ec_derives = lappend(ec->ec_derives, rinfo);
1630 
1631  MemoryContextSwitchTo(oldcontext);
1632 
1633  return rinfo;
1634 }
Index ec_min_security
Definition: pathnodes.h:974
Relids em_nullable_relids
Definition: pathnodes.h:1014
List * ec_derives
Definition: pathnodes.h:966
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
EquivalenceClass * right_ec
Definition: pathnodes.h:2034
EquivalenceMember * left_em
Definition: pathnodes.h:2035
EquivalenceClass * parent_ec
Definition: pathnodes.h:2019
List * ec_sources
Definition: pathnodes.h:965
List * lappend(List *list, void *datum)
Definition: list.c:321
EquivalenceMember * right_em
Definition: pathnodes.h:2036
Expr * clause
Definition: pathnodes.h:1985
#define lfirst(lc)
Definition: pg_list.h:190
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:2353
EquivalenceClass * left_ec
Definition: pathnodes.h:2033
MemoryContext planner_cxt
Definition: pathnodes.h:331

◆ eclass_useful_for_merging()

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

Definition at line 2766 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().

2769 {
2770  Relids relids;
2771  ListCell *lc;
2772 
2773  Assert(!eclass->ec_merged);
2774 
2775  /*
2776  * Won't generate joinclauses if const or single-member (the latter test
2777  * covers the volatile case too)
2778  */
2779  if (eclass->ec_has_const || list_length(eclass->ec_members) <= 1)
2780  return false;
2781 
2782  /*
2783  * Note we don't test ec_broken; if we did, we'd need a separate code path
2784  * to look through ec_sources. Checking the members anyway is OK as a
2785  * possibly-overoptimistic heuristic.
2786  */
2787 
2788  /* If specified rel is a child, we must consider the topmost parent rel */
2789  if (IS_OTHER_REL(rel))
2790  {
2792  relids = rel->top_parent_relids;
2793  }
2794  else
2795  relids = rel->relids;
2796 
2797  /* If rel already includes all members of eclass, no point in searching */
2798  if (bms_is_subset(eclass->ec_relids, relids))
2799  return false;
2800 
2801  /* To join, we need a member not in the given rel */
2802  foreach(lc, eclass->ec_members)
2803  {
2804  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
2805 
2806  if (cur_em->em_is_child)
2807  continue; /* ignore children here */
2808 
2809  if (!bms_overlap(cur_em->em_relids, relids))
2810  return true;
2811  }
2812 
2813  return false;
2814 }
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:653
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids ec_relids
Definition: pathnodes.h:967
Relids relids
Definition: pathnodes.h:665
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
struct EquivalenceClass * ec_merged
Definition: pathnodes.h:976
List * ec_members
Definition: pathnodes.h:964
Relids top_parent_relids
Definition: pathnodes.h:738

◆ exprs_known_equal()

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

Definition at line 2113 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().

2114 {
2115  ListCell *lc1;
2116 
2117  foreach(lc1, root->eq_classes)
2118  {
2119  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2120  bool item1member = false;
2121  bool item2member = false;
2122  ListCell *lc2;
2123 
2124  /* Never match to a volatile EC */
2125  if (ec->ec_has_volatile)
2126  continue;
2127 
2128  foreach(lc2, ec->ec_members)
2129  {
2131 
2132  if (em->em_is_child)
2133  continue; /* ignore children here */
2134  if (equal(item1, em->em_expr))
2135  item1member = true;
2136  else if (equal(item2, em->em_expr))
2137  item2member = true;
2138  /* Exit as soon as equality is proven */
2139  if (item1member && item2member)
2140  return true;
2141  }
2142  }
2143  return false;
2144 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3033
#define lfirst(lc)
Definition: pg_list.h:190
List * eq_classes
Definition: pathnodes.h:266
bool ec_has_volatile
Definition: pathnodes.h:970
List * ec_members
Definition: pathnodes.h:964

◆ find_em_expr_for_rel()

Expr* find_em_expr_for_rel ( EquivalenceClass ec,
RelOptInfo rel 
)

Definition at line 782 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().

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

◆ generate_base_implied_equalities()

void generate_base_implied_equalities ( PlannerInfo root)

Definition at line 855 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().

856 {
857  int ec_index;
858  ListCell *lc;
859 
860  /*
861  * At this point, we're done absorbing knowledge of equivalences in the
862  * query, so no further EC merging should happen, and ECs remaining in the
863  * eq_classes list can be considered canonical. (But note that it's still
864  * possible for new single-member ECs to be added through
865  * get_eclass_for_sort_expr().)
866  */
867  root->ec_merging_done = true;
868 
869  ec_index = 0;
870  foreach(lc, root->eq_classes)
871  {
873  bool can_generate_joinclause = false;
874  int i;
875 
876  Assert(ec->ec_merged == NULL); /* else shouldn't be in list */
877  Assert(!ec->ec_broken); /* not yet anyway... */
878 
879  /*
880  * Generate implied equalities that are restriction clauses.
881  * Single-member ECs won't generate any deductions, either here or at
882  * the join level.
883  */
884  if (list_length(ec->ec_members) > 1)
885  {
886  if (ec->ec_has_const)
888  else
890 
891  /* Recover if we failed to generate required derived clauses */
892  if (ec->ec_broken)
894 
895  /* Detect whether this EC might generate join clauses */
896  can_generate_joinclause =
898  }
899 
900  /*
901  * Mark the base rels cited in each eclass (which should all exist by
902  * now) with the eq_classes indexes of all eclasses mentioning them.
903  * This will let us avoid searching in subsequent lookups. While
904  * we're at it, we can mark base rels that have pending eclass joins;
905  * this is a cheap version of has_relevant_eclass_joinclause().
906  */
907  i = -1;
908  while ((i = bms_next_member(ec->ec_relids, i)) > 0)
909  {
910  RelOptInfo *rel = root->simple_rel_array[i];
911 
913 
915  ec_index);
916 
917  if (can_generate_joinclause)
918  rel->has_eclass_joins = true;
919  }
920 
921  ec_index++;
922  }
923 }
bool has_eclass_joins
Definition: pathnodes.h:733
static void generate_base_implied_equalities_no_const(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:1006
RelOptKind reloptkind
Definition: pathnodes.h:662
bool ec_merging_done
Definition: pathnodes.h:268
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:1097
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:203
Relids ec_relids
Definition: pathnodes.h:967
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
List * eq_classes
Definition: pathnodes.h:266
static int list_length(const List *l)
Definition: pg_list.h:169
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:929
Bitmapset * eclass_indexes
Definition: pathnodes.h:707
struct EquivalenceClass * ec_merged
Definition: pathnodes.h:976
List * ec_members
Definition: pathnodes.h:964

◆ generate_base_implied_equalities_broken()

static void generate_base_implied_equalities_broken ( PlannerInfo root,
EquivalenceClass ec 
)
static

Definition at line 1097 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().

1099 {
1100  ListCell *lc;
1101 
1102  foreach(lc, ec->ec_sources)
1103  {
1104  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1105 
1106  if (ec->ec_has_const ||
1107  bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
1108  distribute_restrictinfo_to_rels(root, restrictinfo);
1109  }
1110 }
Relids required_relids
Definition: pathnodes.h:2003
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2204
List * ec_sources
Definition: pathnodes.h:965
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
#define lfirst(lc)
Definition: pg_list.h:190

◆ generate_base_implied_equalities_const()

static void generate_base_implied_equalities_const ( PlannerInfo root,
EquivalenceClass ec 
)
static

Definition at line 929 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_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, lfirst, linitial, list_length(), OidIsValid, process_implied_equality(), RestrictInfo::required_relids, and select_equality_operator().

Referenced by generate_base_implied_equalities().

931 {
932  EquivalenceMember *const_em = NULL;
933  ListCell *lc;
934 
935  /*
936  * In the trivial case where we just had one "var = const" clause, push
937  * the original clause back into the main planner machinery. There is
938  * nothing to be gained by doing it differently, and we save the effort to
939  * re-build and re-analyze an equality clause that will be exactly
940  * equivalent to the old one.
941  */
942  if (list_length(ec->ec_members) == 2 &&
943  list_length(ec->ec_sources) == 1)
944  {
945  RestrictInfo *restrictinfo = (RestrictInfo *) linitial(ec->ec_sources);
946 
947  if (bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
948  {
949  distribute_restrictinfo_to_rels(root, restrictinfo);
950  return;
951  }
952  }
953 
954  /*
955  * Find the constant member to use. We prefer an actual constant to
956  * pseudo-constants (such as Params), because the constraint exclusion
957  * machinery might be able to exclude relations on the basis of generated
958  * "var = const" equalities, but "var = param" won't work for that.
959  */
960  foreach(lc, ec->ec_members)
961  {
962  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
963 
964  if (cur_em->em_is_const)
965  {
966  const_em = cur_em;
967  if (IsA(cur_em->em_expr, Const))
968  break;
969  }
970  }
971  Assert(const_em != NULL);
972 
973  /* Generate a derived equality against each other member */
974  foreach(lc, ec->ec_members)
975  {
976  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
977  Oid eq_op;
978 
979  Assert(!cur_em->em_is_child); /* no children yet */
980  if (cur_em == const_em)
981  continue;
982  eq_op = select_equality_operator(ec,
983  cur_em->em_datatype,
984  const_em->em_datatype);
985  if (!OidIsValid(eq_op))
986  {
987  /* failed... */
988  ec->ec_broken = true;
989  break;
990  }
991  process_implied_equality(root, eq_op, ec->ec_collation,
992  cur_em->em_expr, const_em->em_expr,
993  bms_copy(ec->ec_relids),
995  const_em->em_nullable_relids),
996  ec->ec_min_security,
998  cur_em->em_is_const);
999  }
1000 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
Relids required_relids
Definition: pathnodes.h:2003
Index ec_min_security
Definition: pathnodes.h:974
Relids em_nullable_relids
Definition: pathnodes.h:1014
bool ec_below_outer_join
Definition: pathnodes.h:971
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:644
void 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:2288
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2204
#define linitial(l)
Definition: pg_list.h:195
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1528
List * ec_sources
Definition: pathnodes.h:965
Relids ec_relids
Definition: pathnodes.h:967
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
static int list_length(const List *l)
Definition: pg_list.h:169
List * ec_members
Definition: pathnodes.h:964

◆ generate_base_implied_equalities_no_const()

static void generate_base_implied_equalities_no_const ( PlannerInfo root,
EquivalenceClass ec 
)
static

Definition at line 1006 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, lfirst, list_free(), OidIsValid, palloc0(), pfree(), process_implied_equality(), pull_var_clause(), PVC_INCLUDE_PLACEHOLDERS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_WINDOWFUNCS, select_equality_operator(), and PlannerInfo::simple_rel_array_size.

Referenced by generate_base_implied_equalities().

1008 {
1009  EquivalenceMember **prev_ems;
1010  ListCell *lc;
1011 
1012  /*
1013  * We scan the EC members once and track the last-seen member for each
1014  * base relation. When we see another member of the same base relation,
1015  * we generate "prev_em = cur_em". This results in the minimum number of
1016  * derived clauses, but it's possible that it will fail when a different
1017  * ordering would succeed. XXX FIXME: use a UNION-FIND algorithm similar
1018  * to the way we build merged ECs. (Use a list-of-lists for each rel.)
1019  */
1020  prev_ems = (EquivalenceMember **)
1021  palloc0(root->simple_rel_array_size * sizeof(EquivalenceMember *));
1022 
1023  foreach(lc, ec->ec_members)
1024  {
1025  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1026  int relid;
1027 
1028  Assert(!cur_em->em_is_child); /* no children yet */
1029  if (!bms_get_singleton_member(cur_em->em_relids, &relid))
1030  continue;
1031  Assert(relid < root->simple_rel_array_size);
1032 
1033  if (prev_ems[relid] != NULL)
1034  {
1035  EquivalenceMember *prev_em = prev_ems[relid];
1036  Oid eq_op;
1037 
1038  eq_op = select_equality_operator(ec,
1039  prev_em->em_datatype,
1040  cur_em->em_datatype);
1041  if (!OidIsValid(eq_op))
1042  {
1043  /* failed... */
1044  ec->ec_broken = true;
1045  break;
1046  }
1047  process_implied_equality(root, eq_op, ec->ec_collation,
1048  prev_em->em_expr, cur_em->em_expr,
1049  bms_copy(ec->ec_relids),
1050  bms_union(prev_em->em_nullable_relids,
1051  cur_em->em_nullable_relids),
1052  ec->ec_min_security,
1053  ec->ec_below_outer_join,
1054  false);
1055  }
1056  prev_ems[relid] = cur_em;
1057  }
1058 
1059  pfree(prev_ems);
1060 
1061  /*
1062  * We also have to make sure that all the Vars used in the member clauses
1063  * will be available at any join node we might try to reference them at.
1064  * For the moment we force all the Vars to be available at all join nodes
1065  * for this eclass. Perhaps this could be improved by doing some
1066  * pre-analysis of which members we prefer to join, but it's no worse than
1067  * what happened in the pre-8.3 code.
1068  */
1069  foreach(lc, ec->ec_members)
1070  {
1071  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1072  List *vars = pull_var_clause((Node *) cur_em->em_expr,
1076 
1077  add_vars_to_targetlist(root, vars, ec->ec_relids, false);
1078  list_free(vars);
1079  }
1080 }
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
Index ec_min_security
Definition: pathnodes.h:974
Relids em_nullable_relids
Definition: pathnodes.h:1014
Definition: nodes.h:529
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:615
bool ec_below_outer_join
Definition: pathnodes.h:971
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:644
void 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:2288
void add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed, bool create_new_ph)
Definition: initsplan.c:229
void pfree(void *pointer)
Definition: mcxt.c:1056
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1528
Relids ec_relids
Definition: pathnodes.h:967
int simple_rel_array_size
Definition: pathnodes.h:204
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:175
void * palloc0(Size size)
Definition: mcxt.c:980
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
#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:964

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

2530 {
2531  List *result = NIL;
2532  bool is_child_rel = (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
2533  Relids parent_relids;
2534  int i;
2535 
2536  /* Should be OK to rely on eclass_indexes */
2537  Assert(root->ec_merging_done);
2538 
2539  /* Indexes are available only on base or "other" member relations. */
2540  Assert(IS_SIMPLE_REL(rel));
2541 
2542  /* If it's a child rel, we'll need to know what its parent(s) are */
2543  if (is_child_rel)
2544  parent_relids = find_childrel_parents(root, rel);
2545  else
2546  parent_relids = NULL; /* not used, but keep compiler quiet */
2547 
2548  i = -1;
2549  while ((i = bms_next_member(rel->eclass_indexes, i)) >= 0)
2550  {
2551  EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2552  EquivalenceMember *cur_em;
2553  ListCell *lc2;
2554 
2555  /* Sanity check eclass_indexes only contain ECs for rel */
2556  Assert(is_child_rel || bms_is_subset(rel->relids, cur_ec->ec_relids));
2557 
2558  /*
2559  * Won't generate joinclauses if const or single-member (the latter
2560  * test covers the volatile case too)
2561  */
2562  if (cur_ec->ec_has_const || list_length(cur_ec->ec_members) <= 1)
2563  continue;
2564 
2565  /*
2566  * Scan members, looking for a match to the target column. Note that
2567  * child EC members are considered, but only when they belong to the
2568  * target relation. (Unlike regular members, the same expression
2569  * could be a child member of more than one EC. Therefore, it's
2570  * potentially order-dependent which EC a child relation's target
2571  * column gets matched to. This is annoying but it only happens in
2572  * corner cases, so for now we live with just reporting the first
2573  * match. See also get_eclass_for_sort_expr.)
2574  */
2575  cur_em = NULL;
2576  foreach(lc2, cur_ec->ec_members)
2577  {
2578  cur_em = (EquivalenceMember *) lfirst(lc2);
2579  if (bms_equal(cur_em->em_relids, rel->relids) &&
2580  callback(root, rel, cur_ec, cur_em, callback_arg))
2581  break;
2582  cur_em = NULL;
2583  }
2584 
2585  if (!cur_em)
2586  continue;
2587 
2588  /*
2589  * Found our match. Scan the other EC members and attempt to generate
2590  * joinclauses.
2591  */
2592  foreach(lc2, cur_ec->ec_members)
2593  {
2594  EquivalenceMember *other_em = (EquivalenceMember *) lfirst(lc2);
2595  Oid eq_op;
2596  RestrictInfo *rinfo;
2597 
2598  if (other_em->em_is_child)
2599  continue; /* ignore children here */
2600 
2601  /* Make sure it'll be a join to a different rel */
2602  if (other_em == cur_em ||
2603  bms_overlap(other_em->em_relids, rel->relids))
2604  continue;
2605 
2606  /* Forget it if caller doesn't want joins to this rel */
2607  if (bms_overlap(other_em->em_relids, prohibited_rels))
2608  continue;
2609 
2610  /*
2611  * Also, if this is a child rel, avoid generating a useless join
2612  * to its parent rel(s).
2613  */
2614  if (is_child_rel &&
2615  bms_overlap(parent_relids, other_em->em_relids))
2616  continue;
2617 
2618  eq_op = select_equality_operator(cur_ec,
2619  cur_em->em_datatype,
2620  other_em->em_datatype);
2621  if (!OidIsValid(eq_op))
2622  continue;
2623 
2624  /* set parent_ec to mark as redundant with other joinclauses */
2625  rinfo = create_join_clause(root, cur_ec, eq_op,
2626  cur_em, other_em,
2627  cur_ec);
2628 
2629  result = lappend(result, rinfo);
2630  }
2631 
2632  /*
2633  * If somehow we failed to create any join clauses, we might as well
2634  * keep scanning the ECs for another match. But if we did make any,
2635  * we're done, because we don't want to return non-redundant clauses.
2636  */
2637  if (result)
2638  break;
2639  }
2640 
2641  return result;
2642 }
#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:1563
RelOptKind reloptkind
Definition: pathnodes.h:662
bool ec_merging_done
Definition: pathnodes.h:268
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:644
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:638
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
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:1528
Relids ec_relids
Definition: pathnodes.h:967
Relids relids
Definition: pathnodes.h:665
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:738
#define lfirst(lc)
Definition: pg_list.h:190
List * eq_classes
Definition: pathnodes.h:266
static int list_length(const List *l)
Definition: pg_list.h:169
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:707
List * ec_members
Definition: pathnodes.h:964
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 1154 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().

1158 {
1159  List *result = NIL;
1160  Relids inner_relids = inner_rel->relids;
1161  Relids nominal_inner_relids;
1162  Relids nominal_join_relids;
1163  Bitmapset *matching_ecs;
1164  int i;
1165 
1166  /* If inner rel is a child, extra setup work is needed */
1167  if (IS_OTHER_REL(inner_rel))
1168  {
1169  Assert(!bms_is_empty(inner_rel->top_parent_relids));
1170 
1171  /* Fetch relid set for the topmost parent rel */
1172  nominal_inner_relids = inner_rel->top_parent_relids;
1173  /* ECs will be marked with the parent's relid, not the child's */
1174  nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1175  }
1176  else
1177  {
1178  nominal_inner_relids = inner_relids;
1179  nominal_join_relids = join_relids;
1180  }
1181 
1182  /*
1183  * Get all eclasses in common between inner_rel's relids and outer_relids
1184  */
1185  matching_ecs = get_common_eclass_indexes(root, inner_rel->relids,
1186  outer_relids);
1187 
1188  i = -1;
1189  while ((i = bms_next_member(matching_ecs, i)) >= 0)
1190  {
1192  List *sublist = NIL;
1193 
1194  /* ECs containing consts do not need any further enforcement */
1195  if (ec->ec_has_const)
1196  continue;
1197 
1198  /* Single-member ECs won't generate any deductions */
1199  if (list_length(ec->ec_members) <= 1)
1200  continue;
1201 
1202  /* Sanity check that this eclass overlaps the join */
1203  Assert(bms_overlap(ec->ec_relids, nominal_join_relids));
1204 
1205  if (!ec->ec_broken)
1207  ec,
1208  join_relids,
1209  outer_relids,
1210  inner_relids);
1211 
1212  /* Recover if we failed to generate required derived clauses */
1213  if (ec->ec_broken)
1215  ec,
1216  nominal_join_relids,
1217  outer_relids,
1218  nominal_inner_relids,
1219  inner_rel);
1220 
1221  result = list_concat(result, sublist);
1222  }
1223 
1224  return result;
1225 }
#define NIL
Definition: pg_list.h:65
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:653
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:277
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:1479
Relids ec_relids
Definition: pathnodes.h:967
Relids relids
Definition: pathnodes.h:665
static List * generate_join_implied_equalities_normal(PlannerInfo *root, EquivalenceClass *ec, Relids join_relids, Relids outer_relids, Relids inner_relids)
Definition: equivclass.c:1303
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:2911
#define Assert(condition)
Definition: c.h:738
List * eq_classes
Definition: pathnodes.h:266
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
static int list_length(const List *l)
Definition: pg_list.h:169
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:964
Relids top_parent_relids
Definition: pathnodes.h:738

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

1485 {
1486  List *result = NIL;
1487  ListCell *lc;
1488 
1489  foreach(lc, ec->ec_sources)
1490  {
1491  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1492  Relids clause_relids = restrictinfo->required_relids;
1493 
1494  if (bms_is_subset(clause_relids, nominal_join_relids) &&
1495  !bms_is_subset(clause_relids, outer_relids) &&
1496  !bms_is_subset(clause_relids, nominal_inner_relids))
1497  result = lappend(result, restrictinfo);
1498  }
1499 
1500  /*
1501  * If we have to translate, just brute-force apply adjust_appendrel_attrs
1502  * to all the RestrictInfos at once. This will result in returning
1503  * RestrictInfos that are not listed in ec_derives, but there shouldn't be
1504  * any duplication, and it's a sufficiently narrow corner case that we
1505  * shouldn't sweat too much over it anyway.
1506  *
1507  * Since inner_rel might be an indirect descendant of the baserel
1508  * mentioned in the ec_sources clauses, we have to be prepared to apply
1509  * multiple levels of Var translation.
1510  */
1511  if (IS_OTHER_REL(inner_rel) && result != NIL)
1512  result = (List *) adjust_appendrel_attrs_multilevel(root,
1513  (Node *) result,
1514  inner_rel->relids,
1515  inner_rel->top_parent_relids);
1516 
1517  return result;
1518 }
#define NIL
Definition: pg_list.h:65
Relids required_relids
Definition: pathnodes.h:2003
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:653
Definition: nodes.h:529
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:965
Relids relids
Definition: pathnodes.h:665
List * lappend(List *list, void *datum)
Definition: list.c:321
#define lfirst(lc)
Definition: pg_list.h:190
Definition: pg_list.h:50
Relids top_parent_relids
Definition: pathnodes.h:738

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

1237 {
1238  List *result = NIL;
1239  Relids inner_relids = inner_rel->relids;
1240  Relids nominal_inner_relids;
1241  Relids nominal_join_relids;
1242  ListCell *lc;
1243 
1244  /* If inner rel is a child, extra setup work is needed */
1245  if (IS_OTHER_REL(inner_rel))
1246  {
1247  Assert(!bms_is_empty(inner_rel->top_parent_relids));
1248 
1249  /* Fetch relid set for the topmost parent rel */
1250  nominal_inner_relids = inner_rel->top_parent_relids;
1251  /* ECs will be marked with the parent's relid, not the child's */
1252  nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1253  }
1254  else
1255  {
1256  nominal_inner_relids = inner_relids;
1257  nominal_join_relids = join_relids;
1258  }
1259 
1260  foreach(lc, eclasses)
1261  {
1263  List *sublist = NIL;
1264 
1265  /* ECs containing consts do not need any further enforcement */
1266  if (ec->ec_has_const)
1267  continue;
1268 
1269  /* Single-member ECs won't generate any deductions */
1270  if (list_length(ec->ec_members) <= 1)
1271  continue;
1272 
1273  /* We can quickly ignore any that don't overlap the join, too */
1274  if (!bms_overlap(ec->ec_relids, nominal_join_relids))
1275  continue;
1276 
1277  if (!ec->ec_broken)
1279  ec,
1280  join_relids,
1281  outer_relids,
1282  inner_relids);
1283 
1284  /* Recover if we failed to generate required derived clauses */
1285  if (ec->ec_broken)
1287  ec,
1288  nominal_join_relids,
1289  outer_relids,
1290  nominal_inner_relids,
1291  inner_rel);
1292 
1293  result = list_concat(result, sublist);
1294  }
1295 
1296  return result;
1297 }
#define NIL
Definition: pg_list.h:65
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:653
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:1479
Relids ec_relids
Definition: pathnodes.h:967
Relids relids
Definition: pathnodes.h:665
static List * generate_join_implied_equalities_normal(PlannerInfo *root, EquivalenceClass *ec, Relids join_relids, Relids outer_relids, Relids inner_relids)
Definition: equivclass.c:1303
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
static int list_length(const List *l)
Definition: pg_list.h:169
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
Definition: pg_list.h:50
List * ec_members
Definition: pathnodes.h:964
Relids top_parent_relids
Definition: pathnodes.h:738

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

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

◆ get_common_eclass_indexes()

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

Definition at line 2911 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().

2912 {
2913  Bitmapset *rel1ecs;
2914  Bitmapset *rel2ecs;
2915  int relid;
2916 
2917  rel1ecs = get_eclass_indexes_for_relids(root, relids1);
2918 
2919  /*
2920  * We can get away with just using the relation's eclass_indexes directly
2921  * when relids2 is a singleton set.
2922  */
2923  if (bms_get_singleton_member(relids2, &relid))
2924  rel2ecs = root->simple_rel_array[relid]->eclass_indexes;
2925  else
2926  rel2ecs = get_eclass_indexes_for_relids(root, relids2);
2927 
2928  /* Calculate and return the common EC indexes, recycling the left input. */
2929  return bms_int_members(rel1ecs, rel2ecs);
2930 }
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:615
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:203
static Bitmapset * get_eclass_indexes_for_relids(PlannerInfo *root, Relids relids)
Definition: equivclass.c:2887
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:902
Bitmapset * eclass_indexes
Definition: pathnodes.h:707

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

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

2888 {
2889  Bitmapset *ec_indexes = NULL;
2890  int i = -1;
2891 
2892  /* Should be OK to rely on eclass_indexes */
2893  Assert(root->ec_merging_done);
2894 
2895  while ((i = bms_next_member(relids, i)) > 0)
2896  {
2897  RelOptInfo *rel = root->simple_rel_array[i];
2898 
2899  ec_indexes = bms_add_members(ec_indexes, rel->eclass_indexes);
2900  }
2901  return ec_indexes;
2902 }
bool ec_merging_done
Definition: pathnodes.h:268
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:203
#define Assert(condition)
Definition: c.h:738
int i
Bitmapset * eclass_indexes
Definition: pathnodes.h:707
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 2722 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().

2723 {
2724  Bitmapset *matched_ecs;
2725  int i;
2726 
2727  /* Examine only eclasses mentioning rel1 */
2728  matched_ecs = get_eclass_indexes_for_relids(root, rel1->relids);
2729 
2730  i = -1;
2731  while ((i = bms_next_member(matched_ecs, i)) >= 0)
2732  {
2734  i);
2735 
2736  /*
2737  * Won't generate joinclauses if single-member (this test covers the
2738  * volatile case too)
2739  */
2740  if (list_length(ec->ec_members) <= 1)
2741  continue;
2742 
2743  /*
2744  * Per the comment in have_relevant_eclass_joinclause, it's sufficient
2745  * to find an EC that mentions both this rel and some other rel.
2746  */
2747  if (!bms_is_subset(ec->ec_relids, rel1->relids))
2748  return true;
2749  }
2750 
2751  return false;
2752 }
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:277
static Bitmapset * get_eclass_indexes_for_relids(PlannerInfo *root, Relids relids)
Definition: equivclass.c:2887
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids ec_relids
Definition: pathnodes.h:967
Relids relids
Definition: pathnodes.h:665
List * eq_classes
Definition: pathnodes.h:266
static int list_length(const List *l)
Definition: pg_list.h:169
int i
List * ec_members
Definition: pathnodes.h:964

◆ have_relevant_eclass_joinclause()

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

Definition at line 2655 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().

2657 {
2658  Bitmapset *matching_ecs;
2659  int i;
2660 
2661  /* Examine only eclasses mentioning both rel1 and rel2 */
2662  matching_ecs = get_common_eclass_indexes(root, rel1->relids,
2663  rel2->relids);
2664 
2665  i = -1;
2666  while ((i = bms_next_member(matching_ecs, i)) >= 0)
2667  {
2669  i);
2670 
2671  /*
2672  * Sanity check that get_common_eclass_indexes gave only ECs
2673  * containing both rels.
2674  */
2675  Assert(bms_overlap(rel1->relids, ec->ec_relids));
2676  Assert(bms_overlap(rel2->relids, ec->ec_relids));
2677 
2678  /*
2679  * Won't generate joinclauses if single-member (this test covers the
2680  * volatile case too)
2681  */
2682  if (list_length(ec->ec_members) <= 1)
2683  continue;
2684 
2685  /*
2686  * We do not need to examine the individual members of the EC, because
2687  * all that we care about is whether each rel overlaps the relids of
2688  * at least one member, and get_common_eclass_indexes() and the single
2689  * member check above are sufficient to prove that. (As with
2690  * have_relevant_joinclause(), it is not necessary that the EC be able
2691  * to form a joinclause relating exactly the two given rels, only that
2692  * it be able to form a joinclause mentioning both, and this will
2693  * surely be true if both of them overlap ec_relids.)
2694  *
2695  * Note we don't test ec_broken; if we did, we'd need a separate code
2696  * path to look through ec_sources. Checking the membership anyway is
2697  * OK as a possibly-overoptimistic heuristic.
2698  *
2699  * We don't test ec_has_const either, even though a const eclass won't
2700  * generate real join clauses. This is because if we had "WHERE a.x =
2701  * b.y and a.x = 42", it is worth considering a join between a and b,
2702  * since the join result is likely to be small even though it'll end
2703  * up being an unqualified nestloop.
2704  */
2705 
2706  return true;
2707  }
2708 
2709  return false;
2710 }
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:277
Relids ec_relids
Definition: pathnodes.h:967
Relids relids
Definition: pathnodes.h:665
static Bitmapset * get_common_eclass_indexes(PlannerInfo *root, Relids relids1, Relids relids2)
Definition: equivclass.c:2911
#define Assert(condition)
Definition: c.h:738
List * eq_classes
Definition: pathnodes.h:266
static int list_length(const List *l)
Definition: pg_list.h:169
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
int i
List * ec_members
Definition: pathnodes.h:964

◆ is_redundant_derived_clause()

bool is_redundant_derived_clause ( RestrictInfo rinfo,
List clauselist 
)

Definition at line 2824 of file equivclass.c.

References lfirst, and RestrictInfo::parent_ec.

Referenced by create_tidscan_plan().

2825 {
2826  EquivalenceClass *parent_ec = rinfo->parent_ec;
2827  ListCell *lc;
2828 
2829  /* Fail if it's not a potentially-redundant clause from some EC */
2830  if (parent_ec == NULL)
2831  return false;
2832 
2833  foreach(lc, clauselist)
2834  {
2835  RestrictInfo *otherrinfo = (RestrictInfo *) lfirst(lc);
2836 
2837  if (otherrinfo->parent_ec == parent_ec)
2838  return true;
2839  }
2840 
2841  return false;
2842 }
EquivalenceClass * parent_ec
Definition: pathnodes.h:2019
#define lfirst(lc)
Definition: pg_list.h:190

◆ is_redundant_with_indexclauses()

bool is_redundant_with_indexclauses ( RestrictInfo rinfo,
List indexclauses 
)

Definition at line 2851 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().

2852 {
2853  EquivalenceClass *parent_ec = rinfo->parent_ec;
2854  ListCell *lc;
2855 
2856  foreach(lc, indexclauses)
2857  {
2858  IndexClause *iclause = lfirst_node(IndexClause, lc);
2859  RestrictInfo *otherrinfo = iclause->rinfo;
2860 
2861  /* If indexclause is lossy, it won't enforce the condition exactly */
2862  if (iclause->lossy)
2863  continue;
2864 
2865  /* Match if it's same clause (pointer equality should be enough) */
2866  if (rinfo == otherrinfo)
2867  return true;
2868  /* Match if derived from same EC */
2869  if (parent_ec && otherrinfo->parent_ec == parent_ec)
2870  return true;
2871 
2872  /*
2873  * No need to look at the derived clauses in iclause->indexquals; they
2874  * couldn't match if the parent clause didn't.
2875  */
2876  }
2877 
2878  return false;
2879 }
EquivalenceClass * parent_ec
Definition: pathnodes.h:2019
#define lfirst_node(type, lc)
Definition: pg_list.h:193
struct RestrictInfo * rinfo
Definition: pathnodes.h:1253

◆ match_eclasses_to_foreign_key_col()

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

Definition at line 2160 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, RelOptInfo::eclass_indexes, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, PlannerInfo::eq_classes, equal(), 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().

2163 {
2164  Index var1varno = fkinfo->con_relid;
2165  AttrNumber var1attno = fkinfo->conkey[colno];
2166  Index var2varno = fkinfo->ref_relid;
2167  AttrNumber var2attno = fkinfo->confkey[colno];
2168  Oid eqop = fkinfo->conpfeqop[colno];
2169  RelOptInfo *rel1 = root->simple_rel_array[var1varno];
2170  RelOptInfo *rel2 = root->simple_rel_array[var2varno];
2171  List *opfamilies = NIL; /* compute only if needed */
2172  Bitmapset *matching_ecs;
2173  int i;
2174 
2175  /* Consider only eclasses mentioning both relations */
2176  Assert(root->ec_merging_done);
2177  Assert(IS_SIMPLE_REL(rel1));
2178  Assert(IS_SIMPLE_REL(rel2));
2179  matching_ecs = bms_intersect(rel1->eclass_indexes,
2180  rel2->eclass_indexes);
2181 
2182  i = -1;
2183  while ((i = bms_next_member(matching_ecs, i)) >= 0)
2184  {
2186  i);
2187  bool item1member = false;
2188  bool item2member = false;
2189  ListCell *lc2;
2190 
2191  /* Never match to a volatile EC */
2192  if (ec->ec_has_volatile)
2193  continue;
2194  /* Note: it seems okay to match to "broken" eclasses here */
2195 
2196  foreach(lc2, ec->ec_members)
2197  {
2199  Var *var;
2200 
2201  if (em->em_is_child)
2202  continue; /* ignore children here */
2203 
2204  /* EM must be a Var, possibly with RelabelType */
2205  var = (Var *) em->em_expr;
2206  while (var && IsA(var, RelabelType))
2207  var = (Var *) ((RelabelType *) var)->arg;
2208  if (!(var && IsA(var, Var)))
2209  continue;
2210 
2211  /* Match? */
2212  if (var->varno == var1varno && var->varattno == var1attno)
2213  item1member = true;
2214  else if (var->varno == var2varno && var->varattno == var2attno)
2215  item2member = true;
2216 
2217  /* Have we found both PK and FK column in this EC? */
2218  if (item1member && item2member)
2219  {
2220  /*
2221  * Succeed if eqop matches EC's opfamilies. We could test
2222  * this before scanning the members, but it's probably cheaper
2223  * to test for member matches first.
2224  */
2225  if (opfamilies == NIL) /* compute if we didn't already */
2226  opfamilies = get_mergejoin_opfamilies(eqop);
2227  if (equal(opfamilies, ec->ec_opfamilies))
2228  return ec;
2229  /* Otherwise, done with this EC, move on to the next */
2230  break;
2231  }
2232  }
2233  }
2234  return NULL;
2235 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
bool ec_merging_done
Definition: pathnodes.h:268
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3033
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:638
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:203
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
Oid conpfeqop[INDEX_MAX_KEYS]
Definition: pathnodes.h:889
Index varno
Definition: primnodes.h:184
List * ec_opfamilies
Definition: pathnodes.h:962
AttrNumber conkey[INDEX_MAX_KEYS]
Definition: pathnodes.h:887
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:259
unsigned int Index
Definition: c.h:475
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
List * eq_classes
Definition: pathnodes.h:266
bool ec_has_volatile
Definition: pathnodes.h:970
int i
Definition: pg_list.h:50
AttrNumber confkey[INDEX_MAX_KEYS]
Definition: pathnodes.h:888
int16 AttrNumber
Definition: attnum.h:21
Bitmapset * eclass_indexes
Definition: pathnodes.h:707
List * ec_members
Definition: pathnodes.h:964

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

◆ reconsider_full_join_clause()

static bool reconsider_full_join_clause ( PlannerInfo root,
RestrictInfo rinfo 
)
static

Definition at line 1936 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(), get_leftop(), get_rightop(), is_opclause(), IsA, RestrictInfo::left_relids, lfirst, linitial, list_delete_ptr(), 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().

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

◆ reconsider_outer_join_clause()

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

Definition at line 1811 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().

1813 {
1814  Expr *outervar,
1815  *innervar;
1816  Oid opno,
1817  collation,
1818  left_type,
1819  right_type,
1820  inner_datatype;
1821  Relids inner_relids,
1822  inner_nullable_relids;
1823  ListCell *lc1;
1824 
1825  Assert(is_opclause(rinfo->clause));
1826  opno = ((OpExpr *) rinfo->clause)->opno;
1827  collation = ((OpExpr *) rinfo->clause)->inputcollid;
1828 
1829  /* If clause is outerjoin_delayed, operator must be strict */
1830  if (rinfo->outerjoin_delayed && !op_strict(opno))
1831  return false;
1832 
1833  /* Extract needed info from the clause */
1834  op_input_types(opno, &left_type, &right_type);
1835  if (outer_on_left)
1836  {
1837  outervar = (Expr *) get_leftop(rinfo->clause);
1838  innervar = (Expr *) get_rightop(rinfo->clause);
1839  inner_datatype = right_type;
1840  inner_relids = rinfo->right_relids;
1841  }
1842  else
1843  {
1844  outervar = (Expr *) get_rightop(rinfo->clause);
1845  innervar = (Expr *) get_leftop(rinfo->clause);
1846  inner_datatype = left_type;
1847  inner_relids = rinfo->left_relids;
1848  }
1849  inner_nullable_relids = bms_intersect(inner_relids,
1850  rinfo->nullable_relids);
1851 
1852  /* Scan EquivalenceClasses for a match to outervar */
1853  foreach(lc1, root->eq_classes)
1854  {
1855  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
1856  bool match;
1857  ListCell *lc2;
1858 
1859  /* Ignore EC unless it contains pseudoconstants */
1860  if (!cur_ec->ec_has_const)
1861  continue;
1862  /* Never match to a volatile EC */
1863  if (cur_ec->ec_has_volatile)
1864  continue;
1865  /* It has to match the outer-join clause as to semantics, too */
1866  if (collation != cur_ec->ec_collation)
1867  continue;
1868  if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
1869  continue;
1870  /* Does it contain a match to outervar? */
1871  match = false;
1872  foreach(lc2, cur_ec->ec_members)
1873  {
1874  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
1875 
1876  Assert(!cur_em->em_is_child); /* no children yet */
1877  if (equal(outervar, cur_em->em_expr))
1878  {
1879  match = true;
1880  break;
1881  }
1882  }
1883  if (!match)
1884  continue; /* no match, so ignore this EC */
1885 
1886  /*
1887  * Yes it does! Try to generate a clause INNERVAR = CONSTANT for each
1888  * CONSTANT in the EC. Note that we must succeed with at least one
1889  * constant before we can decide to throw away the outer-join clause.
1890  */
1891  match = false;
1892  foreach(lc2, cur_ec->ec_members)
1893  {
1894  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
1895  Oid eq_op;
1896  RestrictInfo *newrinfo;
1897 
1898  if (!cur_em->em_is_const)
1899  continue; /* ignore non-const members */
1900  eq_op = select_equality_operator(cur_ec,
1901  inner_datatype,
1902  cur_em->em_datatype);
1903  if (!OidIsValid(eq_op))
1904  continue; /* can't generate equality */
1905  newrinfo = build_implied_join_equality(eq_op,
1906  cur_ec->ec_collation,
1907  innervar,
1908  cur_em->em_expr,
1909  bms_copy(inner_relids),
1910  bms_copy(inner_nullable_relids),
1911  cur_ec->ec_min_security);
1912  if (process_equivalence(root, &newrinfo, true))
1913  match = true;
1914  }
1915 
1916  /*
1917  * If we were able to equate INNERVAR to any constant, report success.
1918  * Otherwise, fall out of the search loop, since we know the OUTERVAR
1919  * appears in at most one EC.
1920  */
1921  if (match)
1922  return true;
1923  else
1924  break;
1925  }
1926 
1927  return false; /* failed to make any deduction */
1928 }
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:3033
Index ec_min_security
Definition: pathnodes.h:974
Relids left_relids
Definition: pathnodes.h:2012
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:644
List * mergeopfamilies
Definition: pathnodes.h:2030
bool process_equivalence(PlannerInfo *root, RestrictInfo **p_restrictinfo, bool below_outer_join)
Definition: equivclass.c:118
bool outerjoin_delayed
Definition: pathnodes.h:1989
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1528
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1275
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:70
Expr * clause
Definition: pathnodes.h:1985
HashCompareFunc match
Definition: dynahash.c:215
List * ec_opfamilies
Definition: pathnodes.h:962
Relids nullable_relids
Definition: pathnodes.h:2009
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:259
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:82
Relids right_relids
Definition: pathnodes.h:2013
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
List * eq_classes
Definition: pathnodes.h:266
bool ec_has_volatile
Definition: pathnodes.h:970
RestrictInfo * build_implied_join_equality(Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
Definition: initsplan.c:2353
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:63
List * ec_members
Definition: pathnodes.h:964

◆ reconsider_outer_join_clauses()

void reconsider_outer_join_clauses ( PlannerInfo root)

Definition at line 1716 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().

1717 {
1718  bool found;
1719  ListCell *cell;
1720 
1721  /* Outer loop repeats until we find no more deductions */
1722  do
1723  {
1724  found = false;
1725 
1726  /* Process the LEFT JOIN clauses */
1727  foreach(cell, root->left_join_clauses)
1728  {
1729  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1730 
1731  if (reconsider_outer_join_clause(root, rinfo, true))
1732  {
1733  found = true;
1734  /* remove it from the list */
1735  root->left_join_clauses =
1737  /* we throw it back anyway (see notes above) */
1738  /* but the thrown-back clause has no extra selectivity */
1739  rinfo->norm_selec = 2.0;
1740  rinfo->outer_selec = 1.0;
1741  distribute_restrictinfo_to_rels(root, rinfo);
1742  }
1743  }
1744 
1745  /* Process the RIGHT JOIN clauses */
1746  foreach(cell, root->right_join_clauses)
1747  {
1748  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1749 
1750  if (reconsider_outer_join_clause(root, rinfo, false))
1751  {
1752  found = true;
1753  /* remove it from the list */
1754  root->right_join_clauses =
1756  /* we throw it back anyway (see notes above) */
1757  /* but the thrown-back clause has no extra selectivity */
1758  rinfo->norm_selec = 2.0;
1759  rinfo->outer_selec = 1.0;
1760  distribute_restrictinfo_to_rels(root, rinfo);
1761  }
1762  }
1763 
1764  /* Process the FULL JOIN clauses */
1765  foreach(cell, root->full_join_clauses)
1766  {
1767  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1768 
1769  if (reconsider_full_join_clause(root, rinfo))
1770  {
1771  found = true;
1772  /* remove it from the list */
1773  root->full_join_clauses =
1775  /* we throw it back anyway (see notes above) */
1776  /* but the thrown-back clause has no extra selectivity */
1777  rinfo->norm_selec = 2.0;
1778  rinfo->outer_selec = 1.0;
1779  distribute_restrictinfo_to_rels(root, rinfo);
1780  }
1781  }
1782  } while (found);
1783 
1784  /* Now, any remaining clauses have to be thrown back */
1785  foreach(cell, root->left_join_clauses)
1786  {
1787  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1788 
1789  distribute_restrictinfo_to_rels(root, rinfo);
1790  }
1791  foreach(cell, root->right_join_clauses)
1792  {
1793  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1794 
1795  distribute_restrictinfo_to_rels(root, rinfo);
1796  }
1797  foreach(cell, root->full_join_clauses)
1798  {
1799  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1800 
1801  distribute_restrictinfo_to_rels(root, rinfo);
1802  }
1803 }
static bool reconsider_full_join_clause(PlannerInfo *root, RestrictInfo *rinfo)
Definition: equivclass.c:1936
static bool reconsider_outer_join_clause(PlannerInfo *root, RestrictInfo *rinfo, bool outer_on_left)
Definition: equivclass.c:1811
Selectivity norm_selec
Definition: pathnodes.h:2023
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:368
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2204
List * left_join_clauses
Definition: pathnodes.h:272
List * full_join_clauses
Definition: pathnodes.h:280
Selectivity outer_selec
Definition: pathnodes.h:2026
#define lfirst(lc)
Definition: pg_list.h:190
List * right_join_clauses
Definition: pathnodes.h:276

◆ select_equality_operator()

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

Definition at line 1528 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().

1529 {
1530  ListCell *lc;
1531 
1532  foreach(lc, ec->ec_opfamilies)
1533  {
1534  Oid opfamily = lfirst_oid(lc);
1535  Oid opno;
1536 
1537  opno = get_opfamily_member(opfamily, lefttype, righttype,
1539  if (!OidIsValid(opno))
1540  continue;
1541  /* If no barrier quals in query, don't worry about leaky operators */
1542  if (ec->ec_max_security == 0)
1543  return opno;
1544  /* Otherwise, insist that selected operators be leakproof */
1545  if (get_func_leakproof(get_opcode(opno)))
1546  return opno;
1547  }
1548  return InvalidOid;
1549 }
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:1749
Index ec_max_security
Definition: pathnodes.h:975
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:644
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:164
List * ec_opfamilies
Definition: pathnodes.h:962
#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:192