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

Go to the source code of this file.

Functions

static bool is_safe_restriction_clause_for (RestrictInfo *rinfo, RelOptInfo *rel)
 
static Exprextract_or_clause (RestrictInfo *or_rinfo, RelOptInfo *rel)
 
static void consider_new_or_clause (PlannerInfo *root, RelOptInfo *rel, Expr *orclause, RestrictInfo *join_or_rinfo)
 
void extract_restriction_or_clauses (PlannerInfo *root)
 

Function Documentation

◆ consider_new_or_clause()

static void consider_new_or_clause ( PlannerInfo root,
RelOptInfo rel,
Expr orclause,
RestrictInfo join_or_rinfo 
)
static

Definition at line 255 of file orclauses.c.

257 {
258  RestrictInfo *or_rinfo;
259  Selectivity or_selec,
260  orig_selec;
261 
262  /*
263  * Build a RestrictInfo from the new OR clause. We can assume it's valid
264  * as a base restriction clause.
265  */
266  or_rinfo = make_restrictinfo(root,
267  orclause,
268  true,
269  false,
270  false,
271  false,
272  join_or_rinfo->security_level,
273  NULL,
274  NULL,
275  NULL);
276 
277  /*
278  * Estimate its selectivity. (We could have done this earlier, but doing
279  * it on the RestrictInfo representation allows the result to get cached,
280  * saving work later.)
281  */
282  or_selec = clause_selectivity(root, (Node *) or_rinfo,
283  0, JOIN_INNER, NULL);
284 
285  /*
286  * The clause is only worth adding to the query if it rejects a useful
287  * fraction of the base relation's rows; otherwise, it's just going to
288  * cause duplicate computation (since we will still have to check the
289  * original OR clause when the join is formed). Somewhat arbitrarily, we
290  * set the selectivity threshold at 0.9.
291  */
292  if (or_selec > 0.9)
293  return; /* forget it */
294 
295  /*
296  * OK, add it to the rel's restriction-clause list.
297  */
298  rel->baserestrictinfo = lappend(rel->baserestrictinfo, or_rinfo);
300  or_rinfo->security_level);
301 
302  /*
303  * Adjust the original join OR clause's cached selectivity to compensate
304  * for the selectivity of the added (but redundant) lower-level qual. This
305  * should result in the join rel getting approximately the same rows
306  * estimate as it would have gotten without all these shenanigans.
307  *
308  * XXX major hack alert: this depends on the assumption that the
309  * selectivity will stay cached.
310  *
311  * XXX another major hack: we adjust only norm_selec, the cached
312  * selectivity for JOIN_INNER semantics, even though the join clause
313  * might've been an outer-join clause. This is partly because we can't
314  * easily identify the relevant SpecialJoinInfo here, and partly because
315  * the linearity assumption we're making would fail anyway. (If it is an
316  * outer-join clause, "rel" must be on the nullable side, else we'd not
317  * have gotten here. So the computation of the join size is going to be
318  * quite nonlinear with respect to the size of "rel", so it's not clear
319  * how we ought to adjust outer_selec even if we could compute its
320  * original value correctly.)
321  */
322  if (or_selec > 0)
323  {
324  SpecialJoinInfo sjinfo;
325 
326  /*
327  * Make up a SpecialJoinInfo for JOIN_INNER semantics. (Compare
328  * approx_tuple_count() in costsize.c.)
329  */
330  sjinfo.type = T_SpecialJoinInfo;
331  sjinfo.min_lefthand = bms_difference(join_or_rinfo->clause_relids,
332  rel->relids);
333  sjinfo.min_righthand = rel->relids;
334  sjinfo.syn_lefthand = sjinfo.min_lefthand;
335  sjinfo.syn_righthand = sjinfo.min_righthand;
336  sjinfo.jointype = JOIN_INNER;
337  sjinfo.ojrelid = 0;
338  sjinfo.commute_above_l = NULL;
339  sjinfo.commute_above_r = NULL;
340  sjinfo.commute_below_l = NULL;
341  sjinfo.commute_below_r = NULL;
342  /* we don't bother trying to make the remaining fields valid */
343  sjinfo.lhs_strict = false;
344  sjinfo.semi_can_btree = false;
345  sjinfo.semi_can_hash = false;
346  sjinfo.semi_operators = NIL;
347  sjinfo.semi_rhs_exprs = NIL;
348 
349  /* Compute inner-join size */
350  orig_selec = clause_selectivity(root, (Node *) join_or_rinfo,
351  0, JOIN_INNER, &sjinfo);
352 
353  /* And hack cached selectivity so join size remains the same */
354  join_or_rinfo->norm_selec = orig_selec / or_selec;
355  /* ensure result stays in sane range */
356  if (join_or_rinfo->norm_selec > 1)
357  join_or_rinfo->norm_selec = 1;
358  /* as explained above, we don't touch outer_selec */
359  }
360 }
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:297
#define Min(x, y)
Definition: c.h:993
Selectivity clause_selectivity(PlannerInfo *root, Node *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:669
List * lappend(List *list, void *datum)
Definition: list.c:338
double Selectivity
Definition: nodes.h:261
@ JOIN_INNER
Definition: nodes.h:304
#define NIL
Definition: pg_list.h:68
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
Definition: nodes.h:129
List * baserestrictinfo
Definition: pathnodes.h:964
Relids relids
Definition: pathnodes.h:856
Index baserestrict_min_security
Definition: pathnodes.h:968
Index security_level
Definition: pathnodes.h:2551
Relids commute_above_r
Definition: pathnodes.h:2860
Relids syn_lefthand
Definition: pathnodes.h:2855
Relids min_righthand
Definition: pathnodes.h:2854
List * semi_rhs_exprs
Definition: pathnodes.h:2868
Relids commute_above_l
Definition: pathnodes.h:2859
JoinType jointype
Definition: pathnodes.h:2857
Relids commute_below_l
Definition: pathnodes.h:2861
Relids min_lefthand
Definition: pathnodes.h:2853
Relids syn_righthand
Definition: pathnodes.h:2856
Relids commute_below_r
Definition: pathnodes.h:2862
List * semi_operators
Definition: pathnodes.h:2867

References RelOptInfo::baserestrict_min_security, RelOptInfo::baserestrictinfo, bms_difference(), clause_selectivity(), SpecialJoinInfo::commute_above_l, SpecialJoinInfo::commute_above_r, SpecialJoinInfo::commute_below_l, SpecialJoinInfo::commute_below_r, JOIN_INNER, SpecialJoinInfo::jointype, lappend(), SpecialJoinInfo::lhs_strict, make_restrictinfo(), Min, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, NIL, SpecialJoinInfo::ojrelid, RelOptInfo::relids, RestrictInfo::security_level, SpecialJoinInfo::semi_can_btree, SpecialJoinInfo::semi_can_hash, SpecialJoinInfo::semi_operators, SpecialJoinInfo::semi_rhs_exprs, SpecialJoinInfo::syn_lefthand, and SpecialJoinInfo::syn_righthand.

Referenced by extract_restriction_or_clauses().

◆ extract_or_clause()

static Expr * extract_or_clause ( RestrictInfo or_rinfo,
RelOptInfo rel 
)
static

Definition at line 157 of file orclauses.c.

158 {
159  List *clauselist = NIL;
160  ListCell *lc;
161 
162  /*
163  * Scan each arm of the input OR clause. Notice we descend into
164  * or_rinfo->orclause, which has RestrictInfo nodes embedded below the
165  * toplevel OR/AND structure. This is useful because we can use the info
166  * in those nodes to make is_safe_restriction_clause_for()'s checks
167  * cheaper. We'll strip those nodes from the returned tree, though,
168  * meaning that fresh ones will be built if the clause is accepted as a
169  * restriction clause. This might seem wasteful --- couldn't we re-use
170  * the existing RestrictInfos? But that'd require assuming that
171  * selectivity and other cached data is computed exactly the same way for
172  * a restriction clause as for a join clause, which seems undesirable.
173  */
174  Assert(is_orclause(or_rinfo->orclause));
175  foreach(lc, ((BoolExpr *) or_rinfo->orclause)->args)
176  {
177  Node *orarg = (Node *) lfirst(lc);
178  List *subclauses = NIL;
179  Node *subclause;
180 
181  /* OR arguments should be ANDs or sub-RestrictInfos */
182  if (is_andclause(orarg))
183  {
184  List *andargs = ((BoolExpr *) orarg)->args;
185  ListCell *lc2;
186 
187  foreach(lc2, andargs)
188  {
189  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc2);
190 
191  if (restriction_is_or_clause(rinfo))
192  {
193  /*
194  * Recurse to deal with nested OR. Note we *must* recurse
195  * here, this isn't just overly-tense optimization: we
196  * have to descend far enough to find and strip all
197  * RestrictInfos in the expression.
198  */
199  Expr *suborclause;
200 
201  suborclause = extract_or_clause(rinfo, rel);
202  if (suborclause)
203  subclauses = lappend(subclauses, suborclause);
204  }
205  else if (is_safe_restriction_clause_for(rinfo, rel))
206  subclauses = lappend(subclauses, rinfo->clause);
207  }
208  }
209  else
210  {
211  RestrictInfo *rinfo = castNode(RestrictInfo, orarg);
212 
214  if (is_safe_restriction_clause_for(rinfo, rel))
215  subclauses = lappend(subclauses, rinfo->clause);
216  }
217 
218  /*
219  * If nothing could be extracted from this arm, we can't do anything
220  * with this OR clause.
221  */
222  if (subclauses == NIL)
223  return NULL;
224 
225  /*
226  * OK, add subclause(s) to the result OR. If we found more than one,
227  * we need an AND node. But if we found only one, and it is itself an
228  * OR node, add its subclauses to the result instead; this is needed
229  * to preserve AND/OR flatness (ie, no OR directly underneath OR).
230  */
231  subclause = (Node *) make_ands_explicit(subclauses);
232  if (is_orclause(subclause))
233  clauselist = list_concat(clauselist,
234  ((BoolExpr *) subclause)->args);
235  else
236  clauselist = lappend(clauselist, subclause);
237  }
238 
239  /*
240  * If we got a restriction clause from every arm, wrap them up in an OR
241  * node. (In theory the OR node might be unnecessary, if there was only
242  * one arm --- but then the input OR node was also redundant.)
243  */
244  if (clauselist != NIL)
245  return make_orclause(clauselist);
246  return NULL;
247 }
Assert(fmt[strlen(fmt) - 1] !='\n')
List * list_concat(List *list1, const List *list2)
Definition: list.c:560
Expr * make_ands_explicit(List *andclauses)
Definition: makefuncs.c:711
Expr * make_orclause(List *orclauses)
Definition: makefuncs.c:655
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:105
static bool is_orclause(const void *clause)
Definition: nodeFuncs.h:114
#define castNode(_type_, nodeptr)
Definition: nodes.h:197
static Expr * extract_or_clause(RestrictInfo *or_rinfo, RelOptInfo *rel)
Definition: orclauses.c:157
static bool is_safe_restriction_clause_for(RestrictInfo *rinfo, RelOptInfo *rel)
Definition: orclauses.c:127
#define lfirst(lc)
Definition: pg_list.h:172
#define lfirst_node(type, lc)
Definition: pg_list.h:176
bool restriction_is_or_clause(RestrictInfo *restrictinfo)
Definition: restrictinfo.c:416
Definition: pg_list.h:54
Expr * clause
Definition: pathnodes.h:2529

References Assert(), castNode, RestrictInfo::clause, is_andclause(), is_orclause(), is_safe_restriction_clause_for(), lappend(), lfirst, lfirst_node, list_concat(), make_ands_explicit(), make_orclause(), NIL, and restriction_is_or_clause().

Referenced by extract_restriction_or_clauses().

◆ extract_restriction_or_clauses()

void extract_restriction_or_clauses ( PlannerInfo root)

Definition at line 76 of file orclauses.c.

77 {
78  Index rti;
79 
80  /* Examine each baserel for potential join OR clauses */
81  for (rti = 1; rti < root->simple_rel_array_size; rti++)
82  {
83  RelOptInfo *rel = root->simple_rel_array[rti];
84  ListCell *lc;
85 
86  /* there may be empty slots corresponding to non-baserel RTEs */
87  if (rel == NULL)
88  continue;
89 
90  Assert(rel->relid == rti); /* sanity check on array */
91 
92  /* ignore RTEs that are "other rels" */
93  if (rel->reloptkind != RELOPT_BASEREL)
94  continue;
95 
96  /*
97  * Find potentially interesting OR joinclauses. We can use any
98  * joinclause that is considered safe to move to this rel by the
99  * parameterized-path machinery, even though what we are going to do
100  * with it is not exactly a parameterized path.
101  */
102  foreach(lc, rel->joininfo)
103  {
104  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
105 
106  if (restriction_is_or_clause(rinfo) &&
107  join_clause_is_movable_to(rinfo, rel))
108  {
109  /* Try to extract a qual for this rel only */
110  Expr *orclause = extract_or_clause(rinfo, rel);
111 
112  /*
113  * If successful, decide whether we want to use the clause,
114  * and insert it into the rel's restrictinfo list if so.
115  */
116  if (orclause)
117  consider_new_or_clause(root, rel, orclause, rinfo);
118  }
119  }
120  }
121 }
unsigned int Index
Definition: c.h:603
static void consider_new_or_clause(PlannerInfo *root, RelOptInfo *rel, Expr *orclause, RestrictInfo *join_or_rinfo)
Definition: orclauses.c:255
@ RELOPT_BASEREL
Definition: pathnodes.h:812
bool join_clause_is_movable_to(RestrictInfo *rinfo, RelOptInfo *baserel)
Definition: restrictinfo.c:584
int simple_rel_array_size
Definition: pathnodes.h:229
List * joininfo
Definition: pathnodes.h:970
Index relid
Definition: pathnodes.h:903
RelOptKind reloptkind
Definition: pathnodes.h:850

References Assert(), consider_new_or_clause(), extract_or_clause(), join_clause_is_movable_to(), RelOptInfo::joininfo, lfirst, RelOptInfo::relid, RELOPT_BASEREL, RelOptInfo::reloptkind, restriction_is_or_clause(), and PlannerInfo::simple_rel_array_size.

Referenced by query_planner().

◆ is_safe_restriction_clause_for()

static bool is_safe_restriction_clause_for ( RestrictInfo rinfo,
RelOptInfo rel 
)
static

Definition at line 127 of file orclauses.c.

128 {
129  /*
130  * We want clauses that mention the rel, and only the rel. So in
131  * particular pseudoconstant clauses can be rejected quickly. Then check
132  * the clause's Var membership.
133  */
134  if (rinfo->pseudoconstant)
135  return false;
136  if (!bms_equal(rinfo->clause_relids, rel->relids))
137  return false;
138 
139  /* We don't want extra evaluations of any volatile functions */
140  if (contain_volatile_functions((Node *) rinfo->clause))
141  return false;
142 
143  return true;
144 }
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:97
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:483

References bms_equal(), RestrictInfo::clause, contain_volatile_functions(), and RelOptInfo::relids.

Referenced by extract_or_clause().