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/clauses.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/planmain.h"
#include "optimizer/prep.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/var.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)
 
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)
 
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)
 
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)
 

Function Documentation

◆ add_child_rel_equivalences()

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

Definition at line 2109 of file equivclass.c.

References add_eq_member(), adjust_appendrel_attrs(), bms_add_members(), bms_difference(), bms_is_subset(), bms_overlap(), EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_members, EquivalenceClass::ec_relids, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_const, EquivalenceMember::em_nullable_relids, EquivalenceMember::em_relids, PlannerInfo::eq_classes, lfirst, RelOptInfo::relids, RELOPT_BASEREL, and RelOptInfo::reloptkind.

Referenced by set_append_rel_size().

2113 {
2114  ListCell *lc1;
2115 
2116  foreach(lc1, root->eq_classes)
2117  {
2118  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
2119  ListCell *lc2;
2120 
2121  /*
2122  * If this EC contains a volatile expression, then generating child
2123  * EMs would be downright dangerous, so skip it. We rely on a
2124  * volatile EC having only one EM.
2125  */
2126  if (cur_ec->ec_has_volatile)
2127  continue;
2128 
2129  /*
2130  * No point in searching if parent rel not mentioned in eclass; but we
2131  * can't tell that for sure if parent rel is itself a child.
2132  */
2133  if (parent_rel->reloptkind == RELOPT_BASEREL &&
2134  !bms_is_subset(parent_rel->relids, cur_ec->ec_relids))
2135  continue;
2136 
2137  foreach(lc2, cur_ec->ec_members)
2138  {
2139  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2140 
2141  if (cur_em->em_is_const)
2142  continue; /* ignore consts here */
2143 
2144  /* Does it reference parent_rel? */
2145  if (bms_overlap(cur_em->em_relids, parent_rel->relids))
2146  {
2147  /* Yes, generate transformed child version */
2148  Expr *child_expr;
2149  Relids new_relids;
2150  Relids new_nullable_relids;
2151 
2152  child_expr = (Expr *)
2154  (Node *) cur_em->em_expr,
2155  1, &appinfo);
2156 
2157  /*
2158  * Transform em_relids to match. Note we do *not* do
2159  * pull_varnos(child_expr) here, as for example the
2160  * transformation might have substituted a constant, but we
2161  * don't want the child member to be marked as constant.
2162  */
2163  new_relids = bms_difference(cur_em->em_relids,
2164  parent_rel->relids);
2165  new_relids = bms_add_members(new_relids, child_rel->relids);
2166 
2167  /*
2168  * And likewise for nullable_relids. Note this code assumes
2169  * parent and child relids are singletons.
2170  */
2171  new_nullable_relids = cur_em->em_nullable_relids;
2172  if (bms_overlap(new_nullable_relids, parent_rel->relids))
2173  {
2174  new_nullable_relids = bms_difference(new_nullable_relids,
2175  parent_rel->relids);
2176  new_nullable_relids = bms_add_members(new_nullable_relids,
2177  child_rel->relids);
2178  }
2179 
2180  (void) add_eq_member(cur_ec, child_expr,
2181  new_relids, new_nullable_relids,
2182  true, cur_em->em_datatype);
2183  }
2184  }
2185  }
2186 }
RelOptKind reloptkind
Definition: relation.h:609
Relids em_nullable_relids
Definition: relation.h:948
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:350
Definition: nodes.h:517
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:374
Relids ec_relids
Definition: relation.h:901
Relids relids
Definition: relation.h:612
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: prepunion.c:2047
Relids em_relids
Definition: relation.h:947
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:249
bool ec_has_volatile
Definition: relation.h:904
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:509
static EquivalenceMember * add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids, Relids nullable_relids, bool is_child, Oid datatype)
Definition: equivclass.c:544
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:821
List * ec_members
Definition: relation.h:898

◆ 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 544 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_rel_equivalences(), get_eclass_for_sort_expr(), and process_equivalence().

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

◆ canonicalize_ec_expression()

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

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

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

1425 {
1426  RestrictInfo *rinfo;
1427  ListCell *lc;
1428  MemoryContext oldcontext;
1429 
1430  /*
1431  * Search to see if we already built a RestrictInfo for this pair of
1432  * EquivalenceMembers. We can use either original source clauses or
1433  * previously-derived clauses. The check on opno is probably redundant,
1434  * but be safe ...
1435  */
1436  foreach(lc, ec->ec_sources)
1437  {
1438  rinfo = (RestrictInfo *) lfirst(lc);
1439  if (rinfo->left_em == leftem &&
1440  rinfo->right_em == rightem &&
1441  rinfo->parent_ec == parent_ec &&
1442  opno == ((OpExpr *) rinfo->clause)->opno)
1443  return rinfo;
1444  }
1445 
1446  foreach(lc, ec->ec_derives)
1447  {
1448  rinfo = (RestrictInfo *) lfirst(lc);
1449  if (rinfo->left_em == leftem &&
1450  rinfo->right_em == rightem &&
1451  rinfo->parent_ec == parent_ec &&
1452  opno == ((OpExpr *) rinfo->clause)->opno)
1453  return rinfo;
1454  }
1455 
1456  /*
1457  * Not there, so build it, in planner context so we can re-use it. (Not
1458  * important in normal planning, but definitely so in GEQO.)
1459  */
1460  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
1461 
1462  rinfo = build_implied_join_equality(opno,
1463  ec->ec_collation,
1464  leftem->em_expr,
1465  rightem->em_expr,
1466  bms_union(leftem->em_relids,
1467  rightem->em_relids),
1468  bms_union(leftem->em_nullable_relids,
1469  rightem->em_nullable_relids),
1470  ec->ec_min_security);
1471 
1472  /* Mark the clause as redundant, or not */
1473  rinfo->parent_ec = parent_ec;
1474 
1475  /*
1476  * We know the correct values for left_ec/right_ec, ie this particular EC,
1477  * so we can just set them directly instead of forcing another lookup.
1478  */
1479  rinfo->left_ec = ec;
1480  rinfo->right_ec = ec;
1481 
1482  /* Mark it as usable with these EMs */
1483  rinfo->left_em = leftem;
1484  rinfo->right_em = rightem;
1485  /* and save it for possible re-use */
1486  ec->ec_derives = lappend(ec->ec_derives, rinfo);
1487 
1488  MemoryContextSwitchTo(oldcontext);
1489 
1490  return rinfo;
1491 }
Index ec_min_security
Definition: relation.h:908
Relids em_nullable_relids
Definition: relation.h:948
List * ec_derives
Definition: relation.h:900
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
EquivalenceClass * right_ec
Definition: relation.h:1929
EquivalenceMember * left_em
Definition: relation.h:1930
EquivalenceClass * parent_ec
Definition: relation.h:1914
List * ec_sources
Definition: relation.h:899
List * lappend(List *list, void *datum)
Definition: list.c:128
EquivalenceMember * right_em
Definition: relation.h:1931
Expr * clause
Definition: relation.h:1880
Relids em_relids
Definition: relation.h:947
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:284
RestrictInfo * build_implied_join_equality(Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
Definition: initsplan.c:2381
EquivalenceClass * left_ec
Definition: relation.h:1928
MemoryContext planner_cxt
Definition: relation.h:302

◆ eclass_useful_for_merging()

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

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

2440 {
2441  Relids relids;
2442  ListCell *lc;
2443 
2444  Assert(!eclass->ec_merged);
2445 
2446  /*
2447  * Won't generate joinclauses if const or single-member (the latter test
2448  * covers the volatile case too)
2449  */
2450  if (eclass->ec_has_const || list_length(eclass->ec_members) <= 1)
2451  return false;
2452 
2453  /*
2454  * Note we don't test ec_broken; if we did, we'd need a separate code path
2455  * to look through ec_sources. Checking the members anyway is OK as a
2456  * possibly-overoptimistic heuristic.
2457  */
2458 
2459  /* If specified rel is a child, we must consider the topmost parent rel */
2460  if (IS_OTHER_REL(rel))
2461  {
2463  relids = rel->top_parent_relids;
2464  }
2465  else
2466  relids = rel->relids;
2467 
2468  /* If rel already includes all members of eclass, no point in searching */
2469  if (bms_is_subset(eclass->ec_relids, relids))
2470  return false;
2471 
2472  /* To join, we need a member not in the given rel */
2473  foreach(lc, eclass->ec_members)
2474  {
2475  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
2476 
2477  if (cur_em->em_is_child)
2478  continue; /* ignore children here */
2479 
2480  if (!bms_overlap(cur_em->em_relids, relids))
2481  return true;
2482  }
2483 
2484  return false;
2485 }
#define IS_OTHER_REL(rel)
Definition: relation.h:600
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:374
Relids ec_relids
Definition: relation.h:901
Relids relids
Definition: relation.h:612
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:729
Relids em_relids
Definition: relation.h:947
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:509
struct EquivalenceClass * ec_merged
Definition: relation.h:910
List * ec_members
Definition: relation.h:898
Relids top_parent_relids
Definition: relation.h:681

◆ exprs_known_equal()

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

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

1985 {
1986  ListCell *lc1;
1987 
1988  foreach(lc1, root->eq_classes)
1989  {
1990  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
1991  bool item1member = false;
1992  bool item2member = false;
1993  ListCell *lc2;
1994 
1995  /* Never match to a volatile EC */
1996  if (ec->ec_has_volatile)
1997  continue;
1998 
1999  foreach(lc2, ec->ec_members)
2000  {
2002 
2003  if (em->em_is_child)
2004  continue; /* ignore children here */
2005  if (equal(item1, em->em_expr))
2006  item1member = true;
2007  else if (equal(item2, em->em_expr))
2008  item2member = true;
2009  /* Exit as soon as equality is proven */
2010  if (item1member && item2member)
2011  return true;
2012  }
2013  }
2014  return false;
2015 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2986
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:249
bool ec_has_volatile
Definition: relation.h:904
List * ec_members
Definition: relation.h:898

◆ generate_base_implied_equalities()

void generate_base_implied_equalities ( PlannerInfo root)

Definition at line 801 of file equivclass.c.

References Assert, EquivalenceClass::ec_broken, EquivalenceClass::ec_has_const, EquivalenceClass::ec_members, EquivalenceClass::ec_merged, PlannerInfo::eq_classes, generate_base_implied_equalities_broken(), generate_base_implied_equalities_const(), generate_base_implied_equalities_no_const(), RelOptInfo::has_eclass_joins, has_relevant_eclass_joinclause(), lfirst, list_length(), PlannerInfo::simple_rel_array, and PlannerInfo::simple_rel_array_size.

Referenced by query_planner().

802 {
803  ListCell *lc;
804  Index rti;
805 
806  foreach(lc, root->eq_classes)
807  {
809 
810  Assert(ec->ec_merged == NULL); /* else shouldn't be in list */
811  Assert(!ec->ec_broken); /* not yet anyway... */
812 
813  /* Single-member ECs won't generate any deductions */
814  if (list_length(ec->ec_members) <= 1)
815  continue;
816 
817  if (ec->ec_has_const)
819  else
821 
822  /* Recover if we failed to generate required derived clauses */
823  if (ec->ec_broken)
825  }
826 
827  /*
828  * This is also a handy place to mark base rels (which should all exist by
829  * now) with flags showing whether they have pending eclass joins.
830  */
831  for (rti = 1; rti < root->simple_rel_array_size; rti++)
832  {
833  RelOptInfo *brel = root->simple_rel_array[rti];
834 
835  if (brel == NULL)
836  continue;
837 
839  }
840 }
bool has_eclass_joins
Definition: relation.h:678
static void generate_base_implied_equalities_no_const(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:923
static void generate_base_implied_equalities_broken(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:1015
struct RelOptInfo ** simple_rel_array
Definition: relation.h:193
bool has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
Definition: equivclass.c:2398
int simple_rel_array_size
Definition: relation.h:194
unsigned int Index
Definition: c.h:442
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:249
static int list_length(const List *l)
Definition: pg_list.h:89
static void generate_base_implied_equalities_const(PlannerInfo *root, EquivalenceClass *ec)
Definition: equivclass.c:846
struct EquivalenceClass * ec_merged
Definition: relation.h:910
List * ec_members
Definition: relation.h:898

◆ generate_base_implied_equalities_broken()

static void generate_base_implied_equalities_broken ( PlannerInfo root,
EquivalenceClass ec 
)
static

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

1017 {
1018  ListCell *lc;
1019 
1020  foreach(lc, ec->ec_sources)
1021  {
1022  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1023 
1024  if (ec->ec_has_const ||
1025  bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
1026  distribute_restrictinfo_to_rels(root, restrictinfo);
1027  }
1028 }
Relids required_relids
Definition: relation.h:1898
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2232
List * ec_sources
Definition: relation.h:899
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:700
#define lfirst(lc)
Definition: pg_list.h:106

◆ generate_base_implied_equalities_const()

static void generate_base_implied_equalities_const ( PlannerInfo root,
EquivalenceClass ec 
)
static

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

848 {
849  EquivalenceMember *const_em = NULL;
850  ListCell *lc;
851 
852  /*
853  * In the trivial case where we just had one "var = const" clause, push
854  * the original clause back into the main planner machinery. There is
855  * nothing to be gained by doing it differently, and we save the effort to
856  * re-build and re-analyze an equality clause that will be exactly
857  * equivalent to the old one.
858  */
859  if (list_length(ec->ec_members) == 2 &&
860  list_length(ec->ec_sources) == 1)
861  {
862  RestrictInfo *restrictinfo = (RestrictInfo *) linitial(ec->ec_sources);
863 
864  if (bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
865  {
866  distribute_restrictinfo_to_rels(root, restrictinfo);
867  return;
868  }
869  }
870 
871  /*
872  * Find the constant member to use. We prefer an actual constant to
873  * pseudo-constants (such as Params), because the constraint exclusion
874  * machinery might be able to exclude relations on the basis of generated
875  * "var = const" equalities, but "var = param" won't work for that.
876  */
877  foreach(lc, ec->ec_members)
878  {
879  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
880 
881  if (cur_em->em_is_const)
882  {
883  const_em = cur_em;
884  if (IsA(cur_em->em_expr, Const))
885  break;
886  }
887  }
888  Assert(const_em != NULL);
889 
890  /* Generate a derived equality against each other member */
891  foreach(lc, ec->ec_members)
892  {
893  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
894  Oid eq_op;
895 
896  Assert(!cur_em->em_is_child); /* no children yet */
897  if (cur_em == const_em)
898  continue;
899  eq_op = select_equality_operator(ec,
900  cur_em->em_datatype,
901  const_em->em_datatype);
902  if (!OidIsValid(eq_op))
903  {
904  /* failed... */
905  ec->ec_broken = true;
906  break;
907  }
908  process_implied_equality(root, eq_op, ec->ec_collation,
909  cur_em->em_expr, const_em->em_expr,
910  bms_copy(ec->ec_relids),
912  const_em->em_nullable_relids),
913  ec->ec_min_security,
915  cur_em->em_is_const);
916  }
917 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:133
Relids required_relids
Definition: relation.h:1898
Index ec_min_security
Definition: relation.h:908
Relids em_nullable_relids
Definition: relation.h:948
bool ec_below_outer_join
Definition: relation.h:905
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
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:2316
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2232
#define linitial(l)
Definition: pg_list.h:111
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1385
List * ec_sources
Definition: relation.h:899
Relids ec_relids
Definition: relation.h:901
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:700
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:284
static int list_length(const List *l)
Definition: pg_list.h:89
List * ec_members
Definition: relation.h:898

◆ generate_base_implied_equalities_no_const()

static void generate_base_implied_equalities_no_const ( PlannerInfo root,
EquivalenceClass ec 
)
static

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

925 {
926  EquivalenceMember **prev_ems;
927  ListCell *lc;
928 
929  /*
930  * We scan the EC members once and track the last-seen member for each
931  * base relation. When we see another member of the same base relation,
932  * we generate "prev_mem = cur_mem". This results in the minimum number
933  * of derived clauses, but it's possible that it will fail when a
934  * different ordering would succeed. XXX FIXME: use a UNION-FIND
935  * algorithm similar to the way we build merged ECs. (Use a list-of-lists
936  * for each rel.)
937  */
938  prev_ems = (EquivalenceMember **)
940 
941  foreach(lc, ec->ec_members)
942  {
943  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
944  int relid;
945 
946  Assert(!cur_em->em_is_child); /* no children yet */
947  if (!bms_get_singleton_member(cur_em->em_relids, &relid))
948  continue;
949  Assert(relid < root->simple_rel_array_size);
950 
951  if (prev_ems[relid] != NULL)
952  {
953  EquivalenceMember *prev_em = prev_ems[relid];
954  Oid eq_op;
955 
956  eq_op = select_equality_operator(ec,
957  prev_em->em_datatype,
958  cur_em->em_datatype);
959  if (!OidIsValid(eq_op))
960  {
961  /* failed... */
962  ec->ec_broken = true;
963  break;
964  }
965  process_implied_equality(root, eq_op, ec->ec_collation,
966  prev_em->em_expr, cur_em->em_expr,
967  bms_copy(ec->ec_relids),
968  bms_union(prev_em->em_nullable_relids,
969  cur_em->em_nullable_relids),
970  ec->ec_min_security,
972  false);
973  }
974  prev_ems[relid] = cur_em;
975  }
976 
977  pfree(prev_ems);
978 
979  /*
980  * We also have to make sure that all the Vars used in the member clauses
981  * will be available at any join node we might try to reference them at.
982  * For the moment we force all the Vars to be available at all join nodes
983  * for this eclass. Perhaps this could be improved by doing some
984  * pre-analysis of which members we prefer to join, but it's no worse than
985  * what happened in the pre-8.3 code.
986  */
987  foreach(lc, ec->ec_members)
988  {
989  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
990  List *vars = pull_var_clause((Node *) cur_em->em_expr,
994 
995  add_vars_to_targetlist(root, vars, ec->ec_relids, false);
996  list_free(vars);
997  }
998 }
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:133
#define PVC_RECURSE_AGGREGATES
Definition: var.h:21
Index ec_min_security
Definition: relation.h:908
Relids em_nullable_relids
Definition: relation.h:948
Definition: nodes.h:517
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:635
bool ec_below_outer_join
Definition: relation.h:905
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:605
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:2316
void add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed, bool create_new_ph)
Definition: initsplan.c:198
#define PVC_INCLUDE_PLACEHOLDERS
Definition: var.h:24
void pfree(void *pointer)
Definition: mcxt.c:1031
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1385
Relids ec_relids
Definition: relation.h:901
#define PVC_RECURSE_WINDOWFUNCS
Definition: var.h:23
int simple_rel_array_size
Definition: relation.h:194
void * palloc0(Size size)
Definition: mcxt.c:955
Relids em_relids
Definition: relation.h:947
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:284
void list_free(List *list)
Definition: list.c:1133
Definition: regcomp.c:224
Definition: pg_list.h:45
List * ec_members
Definition: relation.h:898

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

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

Referenced by match_eclass_clauses_to_index(), and postgresGetForeignPaths().

2218 {
2219  List *result = NIL;
2220  bool is_child_rel = (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
2221  Relids parent_relids;
2222  ListCell *lc1;
2223 
2224  /* Indexes are available only on base or "other" member relations. */
2225  Assert(IS_SIMPLE_REL(rel));
2226 
2227  /* If it's a child rel, we'll need to know what its parent(s) are */
2228  if (is_child_rel)
2229  parent_relids = find_childrel_parents(root, rel);
2230  else
2231  parent_relids = NULL; /* not used, but keep compiler quiet */
2232 
2233  foreach(lc1, root->eq_classes)
2234  {
2235  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
2236  EquivalenceMember *cur_em;
2237  ListCell *lc2;
2238 
2239  /*
2240  * Won't generate joinclauses if const or single-member (the latter
2241  * test covers the volatile case too)
2242  */
2243  if (cur_ec->ec_has_const || list_length(cur_ec->ec_members) <= 1)
2244  continue;
2245 
2246  /*
2247  * No point in searching if rel not mentioned in eclass (but we can't
2248  * tell that for a child rel).
2249  */
2250  if (!is_child_rel &&
2251  !bms_is_subset(rel->relids, cur_ec->ec_relids))
2252  continue;
2253 
2254  /*
2255  * Scan members, looking for a match to the target column. Note that
2256  * child EC members are considered, but only when they belong to the
2257  * target relation. (Unlike regular members, the same expression
2258  * could be a child member of more than one EC. Therefore, it's
2259  * potentially order-dependent which EC a child relation's target
2260  * column gets matched to. This is annoying but it only happens in
2261  * corner cases, so for now we live with just reporting the first
2262  * match. See also get_eclass_for_sort_expr.)
2263  */
2264  cur_em = NULL;
2265  foreach(lc2, cur_ec->ec_members)
2266  {
2267  cur_em = (EquivalenceMember *) lfirst(lc2);
2268  if (bms_equal(cur_em->em_relids, rel->relids) &&
2269  callback(root, rel, cur_ec, cur_em, callback_arg))
2270  break;
2271  cur_em = NULL;
2272  }
2273 
2274  if (!cur_em)
2275  continue;
2276 
2277  /*
2278  * Found our match. Scan the other EC members and attempt to generate
2279  * joinclauses.
2280  */
2281  foreach(lc2, cur_ec->ec_members)
2282  {
2283  EquivalenceMember *other_em = (EquivalenceMember *) lfirst(lc2);
2284  Oid eq_op;
2285  RestrictInfo *rinfo;
2286 
2287  if (other_em->em_is_child)
2288  continue; /* ignore children here */
2289 
2290  /* Make sure it'll be a join to a different rel */
2291  if (other_em == cur_em ||
2292  bms_overlap(other_em->em_relids, rel->relids))
2293  continue;
2294 
2295  /* Forget it if caller doesn't want joins to this rel */
2296  if (bms_overlap(other_em->em_relids, prohibited_rels))
2297  continue;
2298 
2299  /*
2300  * Also, if this is a child rel, avoid generating a useless join
2301  * to its parent rel(s).
2302  */
2303  if (is_child_rel &&
2304  bms_overlap(parent_relids, other_em->em_relids))
2305  continue;
2306 
2307  eq_op = select_equality_operator(cur_ec,
2308  cur_em->em_datatype,
2309  other_em->em_datatype);
2310  if (!OidIsValid(eq_op))
2311  continue;
2312 
2313  /* set parent_ec to mark as redundant with other joinclauses */
2314  rinfo = create_join_clause(root, cur_ec, eq_op,
2315  cur_em, other_em,
2316  cur_ec);
2317 
2318  result = lappend(result, rinfo);
2319  }
2320 
2321  /*
2322  * If somehow we failed to create any join clauses, we might as well
2323  * keep scanning the ECs for another match. But if we did make any,
2324  * we're done, because we don't want to return non-redundant clauses.
2325  */
2326  if (result)
2327  break;
2328  }
2329 
2330  return result;
2331 }
#define NIL
Definition: pg_list.h:69
static RestrictInfo * create_join_clause(PlannerInfo *root, EquivalenceClass *ec, Oid opno, EquivalenceMember *leftem, EquivalenceMember *rightem, EquivalenceClass *parent_ec)
Definition: equivclass.c:1420
RelOptKind reloptkind
Definition: relation.h:609
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
#define IS_SIMPLE_REL(rel)
Definition: relation.h:585
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:374
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1385
Relids ec_relids
Definition: relation.h:901
Relids relids
Definition: relation.h:612
List * lappend(List *list, void *datum)
Definition: list.c:128
Relids em_relids
Definition: relation.h:947
Relids find_childrel_parents(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:1226
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:249
static int list_length(const List *l)
Definition: pg_list.h:89
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:509
Definition: pg_list.h:45
List * ec_members
Definition: relation.h:898
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:153

◆ generate_join_implied_equalities()

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

Definition at line 1072 of file equivclass.c.

References PlannerInfo::eq_classes, and generate_join_implied_equalities_for_ecs().

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

1076 {
1078  root->eq_classes,
1079  join_relids,
1080  outer_relids,
1081  inner_rel);
1082 }
List * generate_join_implied_equalities_for_ecs(PlannerInfo *root, List *eclasses, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1089
List * eq_classes
Definition: relation.h:249

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

1342 {
1343  List *result = NIL;
1344  ListCell *lc;
1345 
1346  foreach(lc, ec->ec_sources)
1347  {
1348  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1349  Relids clause_relids = restrictinfo->required_relids;
1350 
1351  if (bms_is_subset(clause_relids, nominal_join_relids) &&
1352  !bms_is_subset(clause_relids, outer_relids) &&
1353  !bms_is_subset(clause_relids, nominal_inner_relids))
1354  result = lappend(result, restrictinfo);
1355  }
1356 
1357  /*
1358  * If we have to translate, just brute-force apply adjust_appendrel_attrs
1359  * to all the RestrictInfos at once. This will result in returning
1360  * RestrictInfos that are not listed in ec_derives, but there shouldn't be
1361  * any duplication, and it's a sufficiently narrow corner case that we
1362  * shouldn't sweat too much over it anyway.
1363  *
1364  * Since inner_rel might be an indirect descendant of the baserel
1365  * mentioned in the ec_sources clauses, we have to be prepared to apply
1366  * multiple levels of Var translation.
1367  */
1368  if (IS_OTHER_REL(inner_rel) && result != NIL)
1369  result = (List *) adjust_appendrel_attrs_multilevel(root,
1370  (Node *) result,
1371  inner_rel->relids,
1372  inner_rel->top_parent_relids);
1373 
1374  return result;
1375 }
#define NIL
Definition: pg_list.h:69
Relids required_relids
Definition: relation.h:1898
Definition: nodes.h:517
#define IS_OTHER_REL(rel)
Definition: relation.h:600
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, Relids child_relids, Relids top_parent_relids)
Definition: prepunion.c:2534
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:374
List * ec_sources
Definition: relation.h:899
Relids relids
Definition: relation.h:612
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
Definition: pg_list.h:45
Relids top_parent_relids
Definition: relation.h:681

◆ 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 1089 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 generate_join_implied_equalities(), and get_joinrel_parampathinfo().

1094 {
1095  List *result = NIL;
1096  Relids inner_relids = inner_rel->relids;
1097  Relids nominal_inner_relids;
1098  Relids nominal_join_relids;
1099  ListCell *lc;
1100 
1101  /* If inner rel is a child, extra setup work is needed */
1102  if (IS_OTHER_REL(inner_rel))
1103  {
1104  Assert(!bms_is_empty(inner_rel->top_parent_relids));
1105 
1106  /* Fetch relid set for the topmost parent rel */
1107  nominal_inner_relids = inner_rel->top_parent_relids;
1108  /* ECs will be marked with the parent's relid, not the child's */
1109  nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1110  }
1111  else
1112  {
1113  nominal_inner_relids = inner_relids;
1114  nominal_join_relids = join_relids;
1115  }
1116 
1117  foreach(lc, eclasses)
1118  {
1120  List *sublist = NIL;
1121 
1122  /* ECs containing consts do not need any further enforcement */
1123  if (ec->ec_has_const)
1124  continue;
1125 
1126  /* Single-member ECs won't generate any deductions */
1127  if (list_length(ec->ec_members) <= 1)
1128  continue;
1129 
1130  /* We can quickly ignore any that don't overlap the join, too */
1131  if (!bms_overlap(ec->ec_relids, nominal_join_relids))
1132  continue;
1133 
1134  if (!ec->ec_broken)
1136  ec,
1137  join_relids,
1138  outer_relids,
1139  inner_relids);
1140 
1141  /* Recover if we failed to generate required derived clauses */
1142  if (ec->ec_broken)
1144  ec,
1145  nominal_join_relids,
1146  outer_relids,
1147  nominal_inner_relids,
1148  inner_rel);
1149 
1150  result = list_concat(result, sublist);
1151  }
1152 
1153  return result;
1154 }
#define NIL
Definition: pg_list.h:69
#define IS_OTHER_REL(rel)
Definition: relation.h:600
List * list_concat(List *list1, List *list2)
Definition: list.c:321
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:1336
Relids ec_relids
Definition: relation.h:901
Relids relids
Definition: relation.h:612
static List * generate_join_implied_equalities_normal(PlannerInfo *root, EquivalenceClass *ec, Relids join_relids, Relids outer_relids, Relids inner_relids)
Definition: equivclass.c:1160
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:729
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:284
static int list_length(const List *l)
Definition: pg_list.h:89
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:509
Definition: pg_list.h:45
List * ec_members
Definition: relation.h:898
Relids top_parent_relids
Definition: relation.h:681

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

1165 {
1166  List *result = NIL;
1167  List *new_members = NIL;
1168  List *outer_members = NIL;
1169  List *inner_members = NIL;
1170  ListCell *lc1;
1171 
1172  /*
1173  * First, scan the EC to identify member values that are computable at the
1174  * outer rel, at the inner rel, or at this relation but not in either
1175  * input rel. The outer-rel members should already be enforced equal,
1176  * likewise for the inner-rel members. We'll need to create clauses to
1177  * enforce that any newly computable members are all equal to each other
1178  * as well as to at least one input member, plus enforce at least one
1179  * outer-rel member equal to at least one inner-rel member.
1180  */
1181  foreach(lc1, ec->ec_members)
1182  {
1183  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
1184 
1185  /*
1186  * We don't need to check explicitly for child EC members. This test
1187  * against join_relids will cause them to be ignored except when
1188  * considering a child inner rel, which is what we want.
1189  */
1190  if (!bms_is_subset(cur_em->em_relids, join_relids))
1191  continue; /* not computable yet, or wrong child */
1192 
1193  if (bms_is_subset(cur_em->em_relids, outer_relids))
1194  outer_members = lappend(outer_members, cur_em);
1195  else if (bms_is_subset(cur_em->em_relids, inner_relids))
1196  inner_members = lappend(inner_members, cur_em);
1197  else
1198  new_members = lappend(new_members, cur_em);
1199  }
1200 
1201  /*
1202  * First, select the joinclause if needed. We can equate any one outer
1203  * member to any one inner member, but we have to find a datatype
1204  * combination for which an opfamily member operator exists. If we have
1205  * choices, we prefer simple Var members (possibly with RelabelType) since
1206  * these are (a) cheapest to compute at runtime and (b) most likely to
1207  * have useful statistics. Also, prefer operators that are also
1208  * hashjoinable.
1209  */
1210  if (outer_members && inner_members)
1211  {
1212  EquivalenceMember *best_outer_em = NULL;
1213  EquivalenceMember *best_inner_em = NULL;
1214  Oid best_eq_op = InvalidOid;
1215  int best_score = -1;
1216  RestrictInfo *rinfo;
1217 
1218  foreach(lc1, outer_members)
1219  {
1220  EquivalenceMember *outer_em = (EquivalenceMember *) lfirst(lc1);
1221  ListCell *lc2;
1222 
1223  foreach(lc2, inner_members)
1224  {
1225  EquivalenceMember *inner_em = (EquivalenceMember *) lfirst(lc2);
1226  Oid eq_op;
1227  int score;
1228 
1229  eq_op = select_equality_operator(ec,
1230  outer_em->em_datatype,
1231  inner_em->em_datatype);
1232  if (!OidIsValid(eq_op))
1233  continue;
1234  score = 0;
1235  if (IsA(outer_em->em_expr, Var) ||
1236  (IsA(outer_em->em_expr, RelabelType) &&
1237  IsA(((RelabelType *) outer_em->em_expr)->arg, Var)))
1238  score++;
1239  if (IsA(inner_em->em_expr, Var) ||
1240  (IsA(inner_em->em_expr, RelabelType) &&
1241  IsA(((RelabelType *) inner_em->em_expr)->arg, Var)))
1242  score++;
1243  if (op_hashjoinable(eq_op,
1244  exprType((Node *) outer_em->em_expr)))
1245  score++;
1246  if (score > best_score)
1247  {
1248  best_outer_em = outer_em;
1249  best_inner_em = inner_em;
1250  best_eq_op = eq_op;
1251  best_score = score;
1252  if (best_score == 3)
1253  break; /* no need to look further */
1254  }
1255  }
1256  if (best_score == 3)
1257  break; /* no need to look further */
1258  }
1259  if (best_score < 0)
1260  {
1261  /* failed... */
1262  ec->ec_broken = true;
1263  return NIL;
1264  }
1265 
1266  /*
1267  * Create clause, setting parent_ec to mark it as redundant with other
1268  * joinclauses
1269  */
1270  rinfo = create_join_clause(root, ec, best_eq_op,
1271  best_outer_em, best_inner_em,
1272  ec);
1273 
1274  result = lappend(result, rinfo);
1275  }
1276 
1277  /*
1278  * Now deal with building restrictions for any expressions that involve
1279  * Vars from both sides of the join. We have to equate all of these to
1280  * each other as well as to at least one old member (if any).
1281  *
1282  * XXX as in generate_base_implied_equalities_no_const, we could be a lot
1283  * smarter here to avoid unnecessary failures in cross-type situations.
1284  * For now, use the same left-to-right method used there.
1285  */
1286  if (new_members)
1287  {
1288  List *old_members = list_concat(outer_members, inner_members);
1289  EquivalenceMember *prev_em = NULL;
1290  RestrictInfo *rinfo;
1291 
1292  /* For now, arbitrarily take the first old_member as the one to use */
1293  if (old_members)
1294  new_members = lappend(new_members, linitial(old_members));
1295 
1296  foreach(lc1, new_members)
1297  {
1298  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc1);
1299 
1300  if (prev_em != NULL)
1301  {
1302  Oid eq_op;
1303 
1304  eq_op = select_equality_operator(ec,
1305  prev_em->em_datatype,
1306  cur_em->em_datatype);
1307  if (!OidIsValid(eq_op))
1308  {
1309  /* failed... */
1310  ec->ec_broken = true;
1311  return NIL;
1312  }
1313  /* do NOT set parent_ec, this qual is not redundant! */
1314  rinfo = create_join_clause(root, ec, eq_op,
1315  prev_em, cur_em,
1316  NULL);
1317 
1318  result = lappend(result, rinfo);
1319  }
1320  prev_em = cur_em;
1321  }
1322  }
1323 
1324  return result;
1325 }
#define NIL
Definition: pg_list.h:69
static RestrictInfo * create_join_clause(PlannerInfo *root, EquivalenceClass *ec, Oid opno, EquivalenceMember *leftem, EquivalenceMember *rightem, EquivalenceClass *parent_ec)
Definition: equivclass.c:1420
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Definition: nodes.h:517
List * list_concat(List *list1, List *list2)
Definition: list.c:321
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:164
#define OidIsValid(objectId)
Definition: c.h:605
bool op_hashjoinable(Oid opno, Oid inputtype)
Definition: lsyscache.c:1231
#define linitial(l)
Definition: pg_list.h:111
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:374
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1385
List * lappend(List *list, void *datum)
Definition: list.c:128
Relids em_relids
Definition: relation.h:947
#define InvalidOid
Definition: postgres_ext.h:36
#define lfirst(lc)
Definition: pg_list.h:106
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
Definition: pg_list.h:45
List * ec_members
Definition: relation.h:898

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

References add_eq_member(), bms_equal(), bms_intersect(), 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, EquivalenceClass::ec_min_security, EquivalenceClass::ec_opfamilies, EquivalenceClass::ec_relids, EquivalenceClass::ec_sortref, EquivalenceClass::ec_sources, elog, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, EquivalenceMember::em_is_const, EquivalenceMember::em_relids, PlannerInfo::eq_classes, equal(), ERROR, expression_returns_set(), lappend(), lfirst, list_copy(), makeNode, MemoryContextSwitchTo(), NIL, PlannerInfo::planner_cxt, and pull_varnos().

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

629 {
630  Relids expr_relids;
631  EquivalenceClass *newec;
632  EquivalenceMember *newem;
633  ListCell *lc1;
634  MemoryContext oldcontext;
635 
636  /*
637  * Ensure the expression exposes the correct type and collation.
638  */
639  expr = canonicalize_ec_expression(expr, opcintype, collation);
640 
641  /*
642  * Get the precise set of nullable relids appearing in the expression.
643  */
644  expr_relids = pull_varnos((Node *) expr);
645  nullable_relids = bms_intersect(nullable_relids, expr_relids);
646 
647  /*
648  * Scan through the existing EquivalenceClasses for a match
649  */
650  foreach(lc1, root->eq_classes)
651  {
652  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
653  ListCell *lc2;
654 
655  /*
656  * Never match to a volatile EC, except when we are looking at another
657  * reference to the same volatile SortGroupClause.
658  */
659  if (cur_ec->ec_has_volatile &&
660  (sortref == 0 || sortref != cur_ec->ec_sortref))
661  continue;
662 
663  if (collation != cur_ec->ec_collation)
664  continue;
665  if (!equal(opfamilies, cur_ec->ec_opfamilies))
666  continue;
667 
668  foreach(lc2, cur_ec->ec_members)
669  {
670  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
671 
672  /*
673  * Ignore child members unless they match the request.
674  */
675  if (cur_em->em_is_child &&
676  !bms_equal(cur_em->em_relids, rel))
677  continue;
678 
679  /*
680  * If below an outer join, don't match constants: they're not as
681  * constant as they look.
682  */
683  if (cur_ec->ec_below_outer_join &&
684  cur_em->em_is_const)
685  continue;
686 
687  if (opcintype == cur_em->em_datatype &&
688  equal(expr, cur_em->em_expr))
689  return cur_ec; /* Match! */
690  }
691  }
692 
693  /* No match; does caller want a NULL result? */
694  if (!create_it)
695  return NULL;
696 
697  /*
698  * OK, build a new single-member EC
699  *
700  * Here, we must be sure that we construct the EC in the right context.
701  */
702  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
703 
704  newec = makeNode(EquivalenceClass);
705  newec->ec_opfamilies = list_copy(opfamilies);
706  newec->ec_collation = collation;
707  newec->ec_members = NIL;
708  newec->ec_sources = NIL;
709  newec->ec_derives = NIL;
710  newec->ec_relids = NULL;
711  newec->ec_has_const = false;
713  newec->ec_below_outer_join = false;
714  newec->ec_broken = false;
715  newec->ec_sortref = sortref;
716  newec->ec_min_security = UINT_MAX;
717  newec->ec_max_security = 0;
718  newec->ec_merged = NULL;
719 
720  if (newec->ec_has_volatile && sortref == 0) /* should not happen */
721  elog(ERROR, "volatile EquivalenceClass has no sortref");
722 
723  newem = add_eq_member(newec, copyObject(expr), expr_relids,
724  nullable_relids, false, opcintype);
725 
726  /*
727  * add_eq_member doesn't check for volatile functions, set-returning
728  * functions, aggregates, or window functions, but such could appear in
729  * sort expressions; so we have to check whether its const-marking was
730  * correct.
731  */
732  if (newec->ec_has_const)
733  {
734  if (newec->ec_has_volatile ||
735  expression_returns_set((Node *) expr) ||
736  contain_agg_clause((Node *) expr) ||
737  contain_window_function((Node *) expr))
738  {
739  newec->ec_has_const = false;
740  newem->em_is_const = false;
741  }
742  }
743 
744  root->eq_classes = lappend(root->eq_classes, newec);
745 
746  MemoryContextSwitchTo(oldcontext);
747 
748  return newec;
749 }
#define NIL
Definition: pg_list.h:69
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2986
Index ec_min_security
Definition: relation.h:908
List * ec_derives
Definition: relation.h:900
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:670
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
List * list_copy(const List *oldlist)
Definition: list.c:1160
Index ec_sortref
Definition: relation.h:907
Definition: nodes.h:517
bool ec_below_outer_join
Definition: relation.h:905
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:958
Index ec_max_security
Definition: relation.h:909
#define ERROR
Definition: elog.h:43
Expr * canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
Definition: equivclass.c:494
List * ec_sources
Definition: relation.h:899
Relids ec_relids
Definition: relation.h:901
Relids pull_varnos(Node *node)
Definition: var.c:95
bool contain_window_function(Node *clause)
Definition: clauses.c:728
List * lappend(List *list, void *datum)
Definition: list.c:128
List * ec_opfamilies
Definition: relation.h:896
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:318
Relids em_relids
Definition: relation.h:947
#define makeNode(_type_)
Definition: nodes.h:565
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:249
bool ec_has_volatile
Definition: relation.h:904
bool contain_agg_clause(Node *clause)
Definition: clauses.c:418
static EquivalenceMember * add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids, Relids nullable_relids, bool is_child, Oid datatype)
Definition: equivclass.c:544
MemoryContext planner_cxt
Definition: relation.h:302
#define elog
Definition: elog.h:219
#define copyObject(obj)
Definition: nodes.h:630
struct EquivalenceClass * ec_merged
Definition: relation.h:910
List * ec_members
Definition: relation.h:898
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:153

◆ has_relevant_eclass_joinclause()

bool has_relevant_eclass_joinclause ( PlannerInfo root,
RelOptInfo rel1 
)

Definition at line 2398 of file equivclass.c.

References bms_is_subset(), bms_overlap(), EquivalenceClass::ec_members, EquivalenceClass::ec_relids, PlannerInfo::eq_classes, lfirst, list_length(), and RelOptInfo::relids.

Referenced by build_join_rel(), and generate_base_implied_equalities().

2399 {
2400  ListCell *lc1;
2401 
2402  foreach(lc1, root->eq_classes)
2403  {
2404  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2405 
2406  /*
2407  * Won't generate joinclauses if single-member (this test covers the
2408  * volatile case too)
2409  */
2410  if (list_length(ec->ec_members) <= 1)
2411  continue;
2412 
2413  /*
2414  * Per the comment in have_relevant_eclass_joinclause, it's sufficient
2415  * to find an EC that mentions both this rel and some other rel.
2416  */
2417  if (bms_overlap(rel1->relids, ec->ec_relids) &&
2418  !bms_is_subset(ec->ec_relids, rel1->relids))
2419  return true;
2420  }
2421 
2422  return false;
2423 }
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:374
Relids ec_relids
Definition: relation.h:901
Relids relids
Definition: relation.h:612
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:249
static int list_length(const List *l)
Definition: pg_list.h:89
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:509
List * ec_members
Definition: relation.h:898

◆ have_relevant_eclass_joinclause()

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

Definition at line 2344 of file equivclass.c.

References bms_overlap(), EquivalenceClass::ec_members, EquivalenceClass::ec_relids, PlannerInfo::eq_classes, lfirst, list_length(), and RelOptInfo::relids.

Referenced by have_relevant_joinclause().

2346 {
2347  ListCell *lc1;
2348 
2349  foreach(lc1, root->eq_classes)
2350  {
2351  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2352 
2353  /*
2354  * Won't generate joinclauses if single-member (this test covers the
2355  * volatile case too)
2356  */
2357  if (list_length(ec->ec_members) <= 1)
2358  continue;
2359 
2360  /*
2361  * We do not need to examine the individual members of the EC, because
2362  * all that we care about is whether each rel overlaps the relids of
2363  * at least one member, and a test on ec_relids is sufficient to prove
2364  * that. (As with have_relevant_joinclause(), it is not necessary
2365  * that the EC be able to form a joinclause relating exactly the two
2366  * given rels, only that it be able to form a joinclause mentioning
2367  * both, and this will surely be true if both of them overlap
2368  * ec_relids.)
2369  *
2370  * Note we don't test ec_broken; if we did, we'd need a separate code
2371  * path to look through ec_sources. Checking the membership anyway is
2372  * OK as a possibly-overoptimistic heuristic.
2373  *
2374  * We don't test ec_has_const either, even though a const eclass won't
2375  * generate real join clauses. This is because if we had "WHERE a.x =
2376  * b.y and a.x = 42", it is worth considering a join between a and b,
2377  * since the join result is likely to be small even though it'll end
2378  * up being an unqualified nestloop.
2379  */
2380  if (bms_overlap(rel1->relids, ec->ec_relids) &&
2381  bms_overlap(rel2->relids, ec->ec_relids))
2382  return true;
2383  }
2384 
2385  return false;
2386 }
Relids ec_relids
Definition: relation.h:901
Relids relids
Definition: relation.h:612
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:249
static int list_length(const List *l)
Definition: pg_list.h:89
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:509
List * ec_members
Definition: relation.h:898

◆ is_redundant_derived_clause()

bool is_redundant_derived_clause ( RestrictInfo rinfo,
List clauselist 
)

Definition at line 2495 of file equivclass.c.

References lfirst, and RestrictInfo::parent_ec.

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

2496 {
2497  EquivalenceClass *parent_ec = rinfo->parent_ec;
2498  ListCell *lc;
2499 
2500  /* Fail if it's not a potentially-redundant clause from some EC */
2501  if (parent_ec == NULL)
2502  return false;
2503 
2504  foreach(lc, clauselist)
2505  {
2506  RestrictInfo *otherrinfo = (RestrictInfo *) lfirst(lc);
2507 
2508  if (otherrinfo->parent_ec == parent_ec)
2509  return true;
2510  }
2511 
2512  return false;
2513 }
EquivalenceClass * parent_ec
Definition: relation.h:1914
#define lfirst(lc)
Definition: pg_list.h:106

◆ match_eclasses_to_foreign_key_col()

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

Definition at line 2031 of file equivclass.c.

References ForeignKeyOptInfo::con_relid, ForeignKeyOptInfo::confkey, ForeignKeyOptInfo::conkey, ForeignKeyOptInfo::conpfeqop, EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_members, EquivalenceClass::ec_opfamilies, EquivalenceMember::em_expr, EquivalenceMember::em_is_child, PlannerInfo::eq_classes, equal(), get_mergejoin_opfamilies(), IsA, lfirst, NIL, ForeignKeyOptInfo::ref_relid, RangeQueryClause::var, Var::varattno, and Var::varno.

Referenced by match_foreign_keys_to_quals().

2034 {
2035  Index var1varno = fkinfo->con_relid;
2036  AttrNumber var1attno = fkinfo->conkey[colno];
2037  Index var2varno = fkinfo->ref_relid;
2038  AttrNumber var2attno = fkinfo->confkey[colno];
2039  Oid eqop = fkinfo->conpfeqop[colno];
2040  List *opfamilies = NIL; /* compute only if needed */
2041  ListCell *lc1;
2042 
2043  foreach(lc1, root->eq_classes)
2044  {
2045  EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2046  bool item1member = false;
2047  bool item2member = false;
2048  ListCell *lc2;
2049 
2050  /* Never match to a volatile EC */
2051  if (ec->ec_has_volatile)
2052  continue;
2053  /* Note: it seems okay to match to "broken" eclasses here */
2054 
2055  foreach(lc2, ec->ec_members)
2056  {
2058  Var *var;
2059 
2060  if (em->em_is_child)
2061  continue; /* ignore children here */
2062 
2063  /* EM must be a Var, possibly with RelabelType */
2064  var = (Var *) em->em_expr;
2065  while (var && IsA(var, RelabelType))
2066  var = (Var *) ((RelabelType *) var)->arg;
2067  if (!(var && IsA(var, Var)))
2068  continue;
2069 
2070  /* Match? */
2071  if (var->varno == var1varno && var->varattno == var1attno)
2072  item1member = true;
2073  else if (var->varno == var2varno && var->varattno == var2attno)
2074  item2member = true;
2075 
2076  /* Have we found both PK and FK column in this EC? */
2077  if (item1member && item2member)
2078  {
2079  /*
2080  * Succeed if eqop matches EC's opfamilies. We could test
2081  * this before scanning the members, but it's probably cheaper
2082  * to test for member matches first.
2083  */
2084  if (opfamilies == NIL) /* compute if we didn't already */
2085  opfamilies = get_mergejoin_opfamilies(eqop);
2086  if (equal(opfamilies, ec->ec_opfamilies))
2087  return ec;
2088  /* Otherwise, done with this EC, move on to the next */
2089  break;
2090  }
2091  }
2092  }
2093  return NULL;
2094 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2986
List * get_mergejoin_opfamilies(Oid opno)
Definition: lsyscache.c:363
AttrNumber varattno
Definition: primnodes.h:169
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:164
Oid conpfeqop[INDEX_MAX_KEYS]
Definition: relation.h:823
Index varno
Definition: primnodes.h:167
List * ec_opfamilies
Definition: relation.h:896
AttrNumber conkey[INDEX_MAX_KEYS]
Definition: relation.h:821
unsigned int Index
Definition: c.h:442
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:249
bool ec_has_volatile
Definition: relation.h:904
Definition: pg_list.h:45
AttrNumber confkey[INDEX_MAX_KEYS]
Definition: relation.h:822
int16 AttrNumber
Definition: attnum.h:21
List * ec_members
Definition: relation.h:898

◆ process_equivalence()

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

Definition at line 114 of file equivclass.c.

References add_eq_member(), NullTest::arg, NullTest::argisrow, Assert, bms_intersect(), bms_is_empty(), bms_join(), PlannerInfo::canon_pathkeys, 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, 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().

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

◆ reconsider_full_join_clause()

static bool reconsider_full_join_clause ( PlannerInfo root,
RestrictInfo rinfo 
)
static

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

1808 {
1809  Expr *leftvar;
1810  Expr *rightvar;
1811  Oid opno,
1812  collation,
1813  left_type,
1814  right_type;
1815  Relids left_relids,
1816  right_relids,
1817  left_nullable_relids,
1818  right_nullable_relids;
1819  ListCell *lc1;
1820 
1821  /* Can't use an outerjoin_delayed clause here */
1822  if (rinfo->outerjoin_delayed)
1823  return false;
1824 
1825  /* Extract needed info from the clause */
1826  Assert(is_opclause(rinfo->clause));
1827  opno = ((OpExpr *) rinfo->clause)->opno;
1828  collation = ((OpExpr *) rinfo->clause)->inputcollid;
1829  op_input_types(opno, &left_type, &right_type);
1830  leftvar = (Expr *) get_leftop(rinfo->clause);
1831  rightvar = (Expr *) get_rightop(rinfo->clause);
1832  left_relids = rinfo->left_relids;
1833  right_relids = rinfo->right_relids;
1834  left_nullable_relids = bms_intersect(left_relids,
1835  rinfo->nullable_relids);
1836  right_nullable_relids = bms_intersect(right_relids,
1837  rinfo->nullable_relids);
1838 
1839  foreach(lc1, root->eq_classes)
1840  {
1841  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
1842  EquivalenceMember *coal_em = NULL;
1843  bool match;
1844  bool matchleft;
1845  bool matchright;
1846  ListCell *lc2;
1847 
1848  /* Ignore EC unless it contains pseudoconstants */
1849  if (!cur_ec->ec_has_const)
1850  continue;
1851  /* Never match to a volatile EC */
1852  if (cur_ec->ec_has_volatile)
1853  continue;
1854  /* It has to match the outer-join clause as to semantics, too */
1855  if (collation != cur_ec->ec_collation)
1856  continue;
1857  if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
1858  continue;
1859 
1860  /*
1861  * Does it contain a COALESCE(leftvar, rightvar) construct?
1862  *
1863  * We can assume the COALESCE() inputs are in the same order as the
1864  * join clause, since both were automatically generated in the cases
1865  * we care about.
1866  *
1867  * XXX currently this may fail to match in cross-type cases because
1868  * the COALESCE will contain typecast operations while the join clause
1869  * may not (if there is a cross-type mergejoin operator available for
1870  * the two column types). Is it OK to strip implicit coercions from
1871  * the COALESCE arguments?
1872  */
1873  match = false;
1874  foreach(lc2, cur_ec->ec_members)
1875  {
1876  coal_em = (EquivalenceMember *) lfirst(lc2);
1877  Assert(!coal_em->em_is_child); /* no children yet */
1878  if (IsA(coal_em->em_expr, CoalesceExpr))
1879  {
1880  CoalesceExpr *cexpr = (CoalesceExpr *) coal_em->em_expr;
1881  Node *cfirst;
1882  Node *csecond;
1883 
1884  if (list_length(cexpr->args) != 2)
1885  continue;
1886  cfirst = (Node *) linitial(cexpr->args);
1887  csecond = (Node *) lsecond(cexpr->args);
1888 
1889  if (equal(leftvar, cfirst) && equal(rightvar, csecond))
1890  {
1891  match = true;
1892  break;
1893  }
1894  }
1895  }
1896  if (!match)
1897  continue; /* no match, so ignore this EC */
1898 
1899  /*
1900  * Yes it does! Try to generate clauses LEFTVAR = CONSTANT and
1901  * RIGHTVAR = CONSTANT for each CONSTANT in the EC. Note that we must
1902  * succeed with at least one constant for each var before we can
1903  * decide to throw away the outer-join clause.
1904  */
1905  matchleft = matchright = false;
1906  foreach(lc2, cur_ec->ec_members)
1907  {
1908  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
1909  Oid eq_op;
1910  RestrictInfo *newrinfo;
1911 
1912  if (!cur_em->em_is_const)
1913  continue; /* ignore non-const members */
1914  eq_op = select_equality_operator(cur_ec,
1915  left_type,
1916  cur_em->em_datatype);
1917  if (OidIsValid(eq_op))
1918  {
1919  newrinfo = build_implied_join_equality(eq_op,
1920  cur_ec->ec_collation,
1921  leftvar,
1922  cur_em->em_expr,
1923  bms_copy(left_relids),
1924  bms_copy(left_nullable_relids),
1925  cur_ec->ec_min_security);
1926  if (process_equivalence(root, &newrinfo, true))
1927  matchleft = true;
1928  }
1929  eq_op = select_equality_operator(cur_ec,
1930  right_type,
1931  cur_em->em_datatype);
1932  if (OidIsValid(eq_op))
1933  {
1934  newrinfo = build_implied_join_equality(eq_op,
1935  cur_ec->ec_collation,
1936  rightvar,
1937  cur_em->em_expr,
1938  bms_copy(right_relids),
1939  bms_copy(right_nullable_relids),
1940  cur_ec->ec_min_security);
1941  if (process_equivalence(root, &newrinfo, true))
1942  matchright = true;
1943  }
1944  }
1945 
1946  /*
1947  * If we were able to equate both vars to constants, we're done, and
1948  * we can throw away the full-join clause as redundant. Moreover, we
1949  * can remove the COALESCE entry from the EC, since the added
1950  * restrictions ensure it will always have the expected value. (We
1951  * don't bother trying to update ec_relids or ec_sources.)
1952  */
1953  if (matchleft && matchright)
1954  {
1955  cur_ec->ec_members = list_delete_ptr(cur_ec->ec_members, coal_em);
1956  return true;
1957  }
1958 
1959  /*
1960  * Otherwise, fall out of the search loop, since we know the COALESCE
1961  * appears in at most one EC (XXX might stop being true if we allow
1962  * stripping of coercions above?)
1963  */
1964  break;
1965  }
1966 
1967  return false; /* failed to make any deduction */
1968 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:133
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2986
Index ec_min_security
Definition: relation.h:908
Definition: nodes.h:517
Relids left_relids
Definition: relation.h:1907
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:590
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
List * mergeopfamilies
Definition: relation.h:1925
#define lsecond(l)
Definition: pg_list.h:116
#define linitial(l)
Definition: pg_list.h:111
bool process_equivalence(PlannerInfo *root, RestrictInfo **p_restrictinfo, bool below_outer_join)
Definition: equivclass.c:114
#define is_opclause(clause)
Definition: clauses.h:20
List * args
Definition: primnodes.h:1055
Node * get_leftop(const Expr *clause)
Definition: clauses.c:200
bool outerjoin_delayed
Definition: relation.h:1884
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1385
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1152
Expr * clause
Definition: relation.h:1880
HashCompareFunc match
Definition: dynahash.c:213
List * ec_opfamilies
Definition: relation.h:896
Relids nullable_relids
Definition: relation.h:1904
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:318
Relids right_relids
Definition: relation.h:1908
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:249
static int list_length(const List *l)
Definition: pg_list.h:89
bool ec_has_volatile
Definition: relation.h:904
RestrictInfo * build_implied_join_equality(Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
Definition: initsplan.c:2381
Node * get_rightop(const Expr *clause)
Definition: clauses.c:217
List * ec_members
Definition: relation.h:898

◆ reconsider_outer_join_clause()

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

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

1684 {
1685  Expr *outervar,
1686  *innervar;
1687  Oid opno,
1688  collation,
1689  left_type,
1690  right_type,
1691  inner_datatype;
1692  Relids inner_relids,
1693  inner_nullable_relids;
1694  ListCell *lc1;
1695 
1696  Assert(is_opclause(rinfo->clause));
1697  opno = ((OpExpr *) rinfo->clause)->opno;
1698  collation = ((OpExpr *) rinfo->clause)->inputcollid;
1699 
1700  /* If clause is outerjoin_delayed, operator must be strict */
1701  if (rinfo->outerjoin_delayed && !op_strict(opno))
1702  return false;
1703 
1704  /* Extract needed info from the clause */
1705  op_input_types(opno, &left_type, &right_type);
1706  if (outer_on_left)
1707  {
1708  outervar = (Expr *) get_leftop(rinfo->clause);
1709  innervar = (Expr *) get_rightop(rinfo->clause);
1710  inner_datatype = right_type;
1711  inner_relids = rinfo->right_relids;
1712  }
1713  else
1714  {
1715  outervar = (Expr *) get_rightop(rinfo->clause);
1716  innervar = (Expr *) get_leftop(rinfo->clause);
1717  inner_datatype = left_type;
1718  inner_relids = rinfo->left_relids;
1719  }
1720  inner_nullable_relids = bms_intersect(inner_relids,
1721  rinfo->nullable_relids);
1722 
1723  /* Scan EquivalenceClasses for a match to outervar */
1724  foreach(lc1, root->eq_classes)
1725  {
1726  EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
1727  bool match;
1728  ListCell *lc2;
1729 
1730  /* Ignore EC unless it contains pseudoconstants */
1731  if (!cur_ec->ec_has_const)
1732  continue;
1733  /* Never match to a volatile EC */
1734  if (cur_ec->ec_has_volatile)
1735  continue;
1736  /* It has to match the outer-join clause as to semantics, too */
1737  if (collation != cur_ec->ec_collation)
1738  continue;
1739  if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
1740  continue;
1741  /* Does it contain a match to outervar? */
1742  match = false;
1743  foreach(lc2, cur_ec->ec_members)
1744  {
1745  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
1746 
1747  Assert(!cur_em->em_is_child); /* no children yet */
1748  if (equal(outervar, cur_em->em_expr))
1749  {
1750  match = true;
1751  break;
1752  }
1753  }
1754  if (!match)
1755  continue; /* no match, so ignore this EC */
1756 
1757  /*
1758  * Yes it does! Try to generate a clause INNERVAR = CONSTANT for each
1759  * CONSTANT in the EC. Note that we must succeed with at least one
1760  * constant before we can decide to throw away the outer-join clause.
1761  */
1762  match = false;
1763  foreach(lc2, cur_ec->ec_members)
1764  {
1765  EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
1766  Oid eq_op;
1767  RestrictInfo *newrinfo;
1768 
1769  if (!cur_em->em_is_const)
1770  continue; /* ignore non-const members */
1771  eq_op = select_equality_operator(cur_ec,
1772  inner_datatype,
1773  cur_em->em_datatype);
1774  if (!OidIsValid(eq_op))
1775  continue; /* can't generate equality */
1776  newrinfo = build_implied_join_equality(eq_op,
1777  cur_ec->ec_collation,
1778  innervar,
1779  cur_em->em_expr,
1780  bms_copy(inner_relids),
1781  bms_copy(inner_nullable_relids),
1782  cur_ec->ec_min_security);
1783  if (process_equivalence(root, &newrinfo, true))
1784  match = true;
1785  }
1786 
1787  /*
1788  * If we were able to equate INNERVAR to any constant, report success.
1789  * Otherwise, fall out of the search loop, since we know the OUTERVAR
1790  * appears in at most one EC.
1791  */
1792  if (match)
1793  return true;
1794  else
1795  break;
1796  }
1797 
1798  return false; /* failed to make any deduction */
1799 }
bool op_strict(Oid opno)
Definition: lsyscache.c:1266
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:133
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2986
Index ec_min_security
Definition: relation.h:908
Relids left_relids
Definition: relation.h:1907
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
List * mergeopfamilies
Definition: relation.h:1925
bool process_equivalence(PlannerInfo *root, RestrictInfo **p_restrictinfo, bool below_outer_join)
Definition: equivclass.c:114
#define is_opclause(clause)
Definition: clauses.h:20
Node * get_leftop(const Expr *clause)
Definition: clauses.c:200
bool outerjoin_delayed
Definition: relation.h:1884
static Oid select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
Definition: equivclass.c:1385
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1152
Expr * clause
Definition: relation.h:1880
HashCompareFunc match
Definition: dynahash.c:213
List * ec_opfamilies
Definition: relation.h:896
Relids nullable_relids
Definition: relation.h:1904
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:318
Relids right_relids
Definition: relation.h:1908
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:249
bool ec_has_volatile
Definition: relation.h:904
RestrictInfo * build_implied_join_equality(Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
Definition: initsplan.c:2381
Node * get_rightop(const Expr *clause)
Definition: clauses.c:217
List * ec_members
Definition: relation.h:898

◆ reconsider_outer_join_clauses()

void reconsider_outer_join_clauses ( PlannerInfo root)

Definition at line 1573 of file equivclass.c.

References distribute_restrictinfo_to_rels(), PlannerInfo::full_join_clauses, PlannerInfo::left_join_clauses, lfirst, list_delete_cell(), list_head(), lnext, next, RestrictInfo::norm_selec, RestrictInfo::outer_selec, reconsider_full_join_clause(), reconsider_outer_join_clause(), and PlannerInfo::right_join_clauses.

Referenced by query_planner().

1574 {
1575  bool found;
1576  ListCell *cell;
1577  ListCell *prev;
1578  ListCell *next;
1579 
1580  /* Outer loop repeats until we find no more deductions */
1581  do
1582  {
1583  found = false;
1584 
1585  /* Process the LEFT JOIN clauses */
1586  prev = NULL;
1587  for (cell = list_head(root->left_join_clauses); cell; cell = next)
1588  {
1589  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1590 
1591  next = lnext(cell);
1592  if (reconsider_outer_join_clause(root, rinfo, true))
1593  {
1594  found = true;
1595  /* remove it from the list */
1596  root->left_join_clauses =
1597  list_delete_cell(root->left_join_clauses, cell, prev);
1598  /* we throw it back anyway (see notes above) */
1599  /* but the thrown-back clause has no extra selectivity */
1600  rinfo->norm_selec = 2.0;
1601  rinfo->outer_selec = 1.0;
1602  distribute_restrictinfo_to_rels(root, rinfo);
1603  }
1604  else
1605  prev = cell;
1606  }
1607 
1608  /* Process the RIGHT JOIN clauses */
1609  prev = NULL;
1610  for (cell = list_head(root->right_join_clauses); cell; cell = next)
1611  {
1612  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1613 
1614  next = lnext(cell);
1615  if (reconsider_outer_join_clause(root, rinfo, false))
1616  {
1617  found = true;
1618  /* remove it from the list */
1619  root->right_join_clauses =
1620  list_delete_cell(root->right_join_clauses, cell, prev);
1621  /* we throw it back anyway (see notes above) */
1622  /* but the thrown-back clause has no extra selectivity */
1623  rinfo->norm_selec = 2.0;
1624  rinfo->outer_selec = 1.0;
1625  distribute_restrictinfo_to_rels(root, rinfo);
1626  }
1627  else
1628  prev = cell;
1629  }
1630 
1631  /* Process the FULL JOIN clauses */
1632  prev = NULL;
1633  for (cell = list_head(root->full_join_clauses); cell; cell = next)
1634  {
1635  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1636 
1637  next = lnext(cell);
1638  if (reconsider_full_join_clause(root, rinfo))
1639  {
1640  found = true;
1641  /* remove it from the list */
1642  root->full_join_clauses =
1643  list_delete_cell(root->full_join_clauses, cell, prev);
1644  /* we throw it back anyway (see notes above) */
1645  /* but the thrown-back clause has no extra selectivity */
1646  rinfo->norm_selec = 2.0;
1647  rinfo->outer_selec = 1.0;
1648  distribute_restrictinfo_to_rels(root, rinfo);
1649  }
1650  else
1651  prev = cell;
1652  }
1653  } while (found);
1654 
1655  /* Now, any remaining clauses have to be thrown back */
1656  foreach(cell, root->left_join_clauses)
1657  {
1658  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1659 
1660  distribute_restrictinfo_to_rels(root, rinfo);
1661  }
1662  foreach(cell, root->right_join_clauses)
1663  {
1664  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1665 
1666  distribute_restrictinfo_to_rels(root, rinfo);
1667  }
1668  foreach(cell, root->full_join_clauses)
1669  {
1670  RestrictInfo *rinfo = (RestrictInfo *) lfirst(cell);
1671 
1672  distribute_restrictinfo_to_rels(root, rinfo);
1673  }
1674 }
static bool reconsider_full_join_clause(PlannerInfo *root, RestrictInfo *rinfo)
Definition: equivclass.c:1807
static int32 next
Definition: blutils.c:211
static bool reconsider_outer_join_clause(PlannerInfo *root, RestrictInfo *rinfo, bool outer_on_left)
Definition: equivclass.c:1682
Selectivity norm_selec
Definition: relation.h:1918
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2232
List * left_join_clauses
Definition: relation.h:253
List * full_join_clauses
Definition: relation.h:261
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
Selectivity outer_selec
Definition: relation.h:1921
#define lnext(lc)
Definition: pg_list.h:105
List * list_delete_cell(List *list, ListCell *cell, ListCell *prev)
Definition: list.c:528
#define lfirst(lc)
Definition: pg_list.h:106
List * right_join_clauses
Definition: relation.h:257

◆ select_equality_operator()

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

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

1386 {
1387  ListCell *lc;
1388 
1389  foreach(lc, ec->ec_opfamilies)
1390  {
1391  Oid opfamily = lfirst_oid(lc);
1392  Oid opno;
1393 
1394  opno = get_opfamily_member(opfamily, lefttype, righttype,
1396  if (!OidIsValid(opno))
1397  continue;
1398  /* If no barrier quals in query, don't worry about leaky operators */
1399  if (ec->ec_max_security == 0)
1400  return opno;
1401  /* Otherwise, insist that selected operators be leakproof */
1402  if (get_func_leakproof(get_opcode(opno)))
1403  return opno;
1404  }
1405  return InvalidOid;
1406 }
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:1626
Index ec_max_security
Definition: relation.h:909
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
List * ec_opfamilies
Definition: relation.h:896
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1079
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define lfirst_oid(lc)
Definition: pg_list.h:108