PostgreSQL Source Code  git master
joininfo.c File Reference
Include dependency graph for joininfo.c:

Go to the source code of this file.

Functions

bool have_relevant_joinclause (PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
 
void add_join_clause_to_rels (PlannerInfo *root, RestrictInfo *restrictinfo, Relids join_relids)
 
void remove_join_clause_from_rels (PlannerInfo *root, RestrictInfo *restrictinfo, Relids join_relids)
 

Function Documentation

◆ add_join_clause_to_rels()

void add_join_clause_to_rels ( PlannerInfo root,
RestrictInfo restrictinfo,
Relids  join_relids 
)

Definition at line 98 of file joininfo.c.

101 {
102  int cur_relid;
103 
104  /* Don't add the clause if it is always true */
105  if (restriction_is_always_true(root, restrictinfo))
106  return;
107 
108  /*
109  * Substitute constant-FALSE for the origin qual if it is always false.
110  * Note that we keep the same rinfo_serial.
111  */
112  if (restriction_is_always_false(root, restrictinfo))
113  {
114  int save_rinfo_serial = restrictinfo->rinfo_serial;
115 
116  restrictinfo = make_restrictinfo(root,
117  (Expr *) makeBoolConst(false, false),
118  restrictinfo->is_pushed_down,
119  restrictinfo->has_clone,
120  restrictinfo->is_clone,
121  restrictinfo->pseudoconstant,
122  0, /* security_level */
123  restrictinfo->required_relids,
124  restrictinfo->incompatible_relids,
125  restrictinfo->outer_relids);
126  restrictinfo->rinfo_serial = save_rinfo_serial;
127  }
128 
129  cur_relid = -1;
130  while ((cur_relid = bms_next_member(join_relids, cur_relid)) >= 0)
131  {
132  RelOptInfo *rel = find_base_rel_ignore_join(root, cur_relid);
133 
134  /* We only need to add the clause to baserels */
135  if (rel == NULL)
136  continue;
137  rel->joininfo = lappend(rel->joininfo, restrictinfo);
138  }
139 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
bool restriction_is_always_true(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2732
bool restriction_is_always_false(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2781
List * lappend(List *list, void *datum)
Definition: list.c:339
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:359
tree ctl root
Definition: radixtree.h:1884
RelOptInfo * find_base_rel_ignore_join(PlannerInfo *root, int relid)
Definition: relnode.c:454
RestrictInfo * make_restrictinfo(PlannerInfo *root, Expr *clause, bool is_pushed_down, bool has_clone, bool is_clone, bool pseudoconstant, Index security_level, Relids required_relids, Relids incompatible_relids, Relids outer_relids)
Definition: restrictinfo.c:63
List * joininfo
Definition: pathnodes.h:985
bool is_pushed_down
Definition: pathnodes.h:2567
Relids required_relids
Definition: pathnodes.h:2595
int rinfo_serial
Definition: pathnodes.h:2636
Relids outer_relids
Definition: pathnodes.h:2601
Relids incompatible_relids
Definition: pathnodes.h:2598
bool has_clone
Definition: pathnodes.h:2576

References bms_next_member(), find_base_rel_ignore_join(), RestrictInfo::has_clone, RestrictInfo::incompatible_relids, RestrictInfo::is_clone, RestrictInfo::is_pushed_down, RelOptInfo::joininfo, lappend(), make_restrictinfo(), makeBoolConst(), RestrictInfo::outer_relids, RestrictInfo::required_relids, restriction_is_always_false(), restriction_is_always_true(), RestrictInfo::rinfo_serial, and root.

Referenced by distribute_restrictinfo_to_rels().

◆ have_relevant_joinclause()

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

Definition at line 39 of file joininfo.c.

41 {
42  bool result = false;
43  List *joininfo;
44  Relids other_relids;
45  ListCell *l;
46 
47  /*
48  * We could scan either relation's joininfo list; may as well use the
49  * shorter one.
50  */
51  if (list_length(rel1->joininfo) <= list_length(rel2->joininfo))
52  {
53  joininfo = rel1->joininfo;
54  other_relids = rel2->relids;
55  }
56  else
57  {
58  joininfo = rel2->joininfo;
59  other_relids = rel1->relids;
60  }
61 
62  foreach(l, joininfo)
63  {
64  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
65 
66  if (bms_overlap(other_relids, rinfo->required_relids))
67  {
68  result = true;
69  break;
70  }
71  }
72 
73  /*
74  * We also need to check the EquivalenceClass data structure, which might
75  * contain relationships not emitted into the joininfo lists.
76  */
77  if (!result && rel1->has_eclass_joins && rel2->has_eclass_joins)
78  result = have_relevant_eclass_joinclause(root, rel1, rel2);
79 
80  return result;
81 }
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:582
bool have_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: equivclass.c:3092
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
Definition: pg_list.h:54
Relids relids
Definition: pathnodes.h:865
bool has_eclass_joins
Definition: pathnodes.h:987

References bms_overlap(), RelOptInfo::has_eclass_joins, have_relevant_eclass_joinclause(), RelOptInfo::joininfo, lfirst, list_length(), RelOptInfo::relids, RestrictInfo::required_relids, and root.

Referenced by desirable_join(), has_legal_joinclause(), join_search_one_level(), and make_rels_by_clause_joins().

◆ remove_join_clause_from_rels()

void remove_join_clause_from_rels ( PlannerInfo root,
RestrictInfo restrictinfo,
Relids  join_relids 
)

Definition at line 153 of file joininfo.c.

156 {
157  int cur_relid;
158 
159  cur_relid = -1;
160  while ((cur_relid = bms_next_member(join_relids, cur_relid)) >= 0)
161  {
162  RelOptInfo *rel = find_base_rel_ignore_join(root, cur_relid);
163 
164  /* We would only have added the clause to baserels */
165  if (rel == NULL)
166  continue;
167 
168  /*
169  * Remove the restrictinfo from the list. Pointer comparison is
170  * sufficient.
171  */
172  Assert(list_member_ptr(rel->joininfo, restrictinfo));
173  rel->joininfo = list_delete_ptr(rel->joininfo, restrictinfo);
174  }
175 }
#define Assert(condition)
Definition: c.h:858
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:872
bool list_member_ptr(const List *list, const void *datum)
Definition: list.c:682

References Assert, bms_next_member(), find_base_rel_ignore_join(), RelOptInfo::joininfo, list_delete_ptr(), list_member_ptr(), and root.

Referenced by remove_rel_from_query().