PostgreSQL Source Code  git master
restrictinfo.c File Reference
#include "postgres.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/optimizer.h"
#include "optimizer/restrictinfo.h"
Include dependency graph for restrictinfo.c:

Go to the source code of this file.

Functions

static RestrictInfomake_restrictinfo_internal (PlannerInfo *root, Expr *clause, Expr *orclause, bool is_pushed_down, bool outerjoin_delayed, bool pseudoconstant, Index security_level, Relids required_relids, Relids outer_relids, Relids nullable_relids)
 
static Exprmake_sub_restrictinfos (PlannerInfo *root, Expr *clause, bool is_pushed_down, bool outerjoin_delayed, bool pseudoconstant, Index security_level, Relids required_relids, Relids outer_relids, Relids nullable_relids)
 
RestrictInfomake_restrictinfo (PlannerInfo *root, Expr *clause, bool is_pushed_down, bool outerjoin_delayed, bool pseudoconstant, Index security_level, Relids required_relids, Relids outer_relids, Relids nullable_relids)
 
RestrictInfocommute_restrictinfo (RestrictInfo *rinfo, Oid comm_op)
 
bool restriction_is_or_clause (RestrictInfo *restrictinfo)
 
bool restriction_is_securely_promotable (RestrictInfo *restrictinfo, RelOptInfo *rel)
 
Listget_actual_clauses (List *restrictinfo_list)
 
Listextract_actual_clauses (List *restrictinfo_list, bool pseudoconstant)
 
void extract_actual_join_clauses (List *restrictinfo_list, Relids joinrelids, List **joinquals, List **otherquals)
 
bool join_clause_is_movable_to (RestrictInfo *rinfo, RelOptInfo *baserel)
 
bool join_clause_is_movable_into (RestrictInfo *rinfo, Relids currentrelids, Relids current_and_outer)
 

Function Documentation

◆ commute_restrictinfo()

RestrictInfo* commute_restrictinfo ( RestrictInfo rinfo,
Oid  comm_op 
)

Definition at line 327 of file restrictinfo.c.

328 {
329  RestrictInfo *result;
330  OpExpr *newclause;
331  OpExpr *clause = castNode(OpExpr, rinfo->clause);
332 
333  Assert(list_length(clause->args) == 2);
334 
335  /* flat-copy all the fields of clause ... */
336  newclause = makeNode(OpExpr);
337  memcpy(newclause, clause, sizeof(OpExpr));
338 
339  /* ... and adjust those we need to change to commute it */
340  newclause->opno = comm_op;
341  newclause->opfuncid = InvalidOid;
342  newclause->args = list_make2(lsecond(clause->args),
343  linitial(clause->args));
344 
345  /* likewise, flat-copy all the fields of rinfo ... */
346  result = makeNode(RestrictInfo);
347  memcpy(result, rinfo, sizeof(RestrictInfo));
348 
349  /*
350  * ... and adjust those we need to change. Note in particular that we can
351  * preserve any cached selectivity or cost estimates, since those ought to
352  * be the same for the new clause. Likewise we can keep the source's
353  * parent_ec.
354  */
355  result->clause = (Expr *) newclause;
356  result->left_relids = rinfo->right_relids;
357  result->right_relids = rinfo->left_relids;
358  Assert(result->orclause == NULL);
359  result->left_ec = rinfo->right_ec;
360  result->right_ec = rinfo->left_ec;
361  result->left_em = rinfo->right_em;
362  result->right_em = rinfo->left_em;
363  result->scansel_cache = NIL; /* not worth updating this */
364  if (rinfo->hashjoinoperator == clause->opno)
365  result->hashjoinoperator = comm_op;
366  else
367  result->hashjoinoperator = InvalidOid;
368  result->left_bucketsize = rinfo->right_bucketsize;
369  result->right_bucketsize = rinfo->left_bucketsize;
370  result->left_mcvfreq = rinfo->right_mcvfreq;
371  result->right_mcvfreq = rinfo->left_mcvfreq;
372  result->left_hasheqoperator = InvalidOid;
373  result->right_hasheqoperator = InvalidOid;
374 
375  return result;
376 }
Assert(fmt[strlen(fmt) - 1] !='\n')
#define makeNode(_type_)
Definition: nodes.h:165
#define castNode(_type_, nodeptr)
Definition: nodes.h:186
static int list_length(const List *l)
Definition: pg_list.h:150
#define NIL
Definition: pg_list.h:66
#define linitial(l)
Definition: pg_list.h:176
#define lsecond(l)
Definition: pg_list.h:181
#define list_make2(x1, x2)
Definition: pg_list.h:212
#define InvalidOid
Definition: postgres_ext.h:36
Oid opno
Definition: primnodes.h:648
List * args
Definition: primnodes.h:666
Expr * clause
Definition: pathnodes.h:2432

References OpExpr::args, Assert(), castNode, RestrictInfo::clause, InvalidOid, linitial, list_length(), list_make2, lsecond, makeNode, NIL, and OpExpr::opno.

Referenced by match_opclause_to_indexcol().

◆ extract_actual_clauses()

List* extract_actual_clauses ( List restrictinfo_list,
bool  pseudoconstant 
)

◆ extract_actual_join_clauses()

void extract_actual_join_clauses ( List restrictinfo_list,
Relids  joinrelids,
List **  joinquals,
List **  otherquals 
)

Definition at line 472 of file restrictinfo.c.

476 {
477  ListCell *l;
478 
479  *joinquals = NIL;
480  *otherquals = NIL;
481 
482  foreach(l, restrictinfo_list)
483  {
485 
486  if (RINFO_IS_PUSHED_DOWN(rinfo, joinrelids))
487  {
488  if (!rinfo->pseudoconstant)
489  *otherquals = lappend(*otherquals, rinfo->clause);
490  }
491  else
492  {
493  /* joinquals shouldn't have been marked pseudoconstant */
494  Assert(!rinfo->pseudoconstant);
495  *joinquals = lappend(*joinquals, rinfo->clause);
496  }
497  }
498 }
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: pathnodes.h:2566

References Assert(), RestrictInfo::clause, lappend(), lfirst_node, NIL, and RINFO_IS_PUSHED_DOWN.

Referenced by create_hashjoin_plan(), create_mergejoin_plan(), and create_nestloop_plan().

◆ get_actual_clauses()

List* get_actual_clauses ( List restrictinfo_list)

Definition at line 422 of file restrictinfo.c.

423 {
424  List *result = NIL;
425  ListCell *l;
426 
427  foreach(l, restrictinfo_list)
428  {
430 
431  Assert(!rinfo->pseudoconstant);
432 
433  result = lappend(result, rinfo->clause);
434  }
435  return result;
436 }

References Assert(), RestrictInfo::clause, lappend(), lfirst_node, and NIL.

Referenced by create_bitmap_subplan(), create_hashjoin_plan(), create_join_plan(), and create_mergejoin_plan().

◆ join_clause_is_movable_into()

bool join_clause_is_movable_into ( RestrictInfo rinfo,
Relids  currentrelids,
Relids  current_and_outer 
)

Definition at line 600 of file restrictinfo.c.

603 {
604  /* Clause must be evaluable given available context */
605  if (!bms_is_subset(rinfo->clause_relids, current_and_outer))
606  return false;
607 
608  /* Clause must physically reference at least one target rel */
609  if (!bms_overlap(currentrelids, rinfo->clause_relids))
610  return false;
611 
612  /* Cannot move an outer-join clause into the join's outer side */
613  if (bms_overlap(currentrelids, rinfo->outer_relids))
614  return false;
615 
616  /*
617  * Target rel(s) must not be nullable below the clause. This is
618  * approximate, in the safe direction, because the current join might be
619  * above the join where the nulling would happen, in which case the clause
620  * would work correctly here. But we don't have enough info to be sure.
621  */
622  if (bms_overlap(currentrelids, rinfo->nullable_relids))
623  return false;
624 
625  return true;
626 }
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:316
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:495
Relids nullable_relids
Definition: pathnodes.h:2465
Relids outer_relids
Definition: pathnodes.h:2462

References bms_is_subset(), bms_overlap(), RestrictInfo::nullable_relids, and RestrictInfo::outer_relids.

Referenced by create_nestloop_path(), get_baserel_parampathinfo(), get_joinrel_parampathinfo(), and has_indexed_join_quals().

◆ join_clause_is_movable_to()

bool join_clause_is_movable_to ( RestrictInfo rinfo,
RelOptInfo baserel 
)

Definition at line 527 of file restrictinfo.c.

528 {
529  /* Clause must physically reference target rel */
530  if (!bms_is_member(baserel->relid, rinfo->clause_relids))
531  return false;
532 
533  /* Cannot move an outer-join clause into the join's outer side */
534  if (bms_is_member(baserel->relid, rinfo->outer_relids))
535  return false;
536 
537  /* Target rel must not be nullable below the clause */
538  if (bms_is_member(baserel->relid, rinfo->nullable_relids))
539  return false;
540 
541  /* Clause must not use any rels with LATERAL references to this rel */
542  if (bms_overlap(baserel->lateral_referencers, rinfo->clause_relids))
543  return false;
544 
545  return true;
546 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:428
Index relid
Definition: pathnodes.h:868
Relids lateral_referencers
Definition: pathnodes.h:884

References bms_is_member(), bms_overlap(), RelOptInfo::lateral_referencers, RestrictInfo::nullable_relids, RestrictInfo::outer_relids, and RelOptInfo::relid.

Referenced by BuildParameterizedTidPaths(), check_index_predicates(), extract_restriction_or_clauses(), match_join_clauses_to_index(), and postgresGetForeignPaths().

◆ make_restrictinfo()

RestrictInfo* make_restrictinfo ( PlannerInfo root,
Expr clause,
bool  is_pushed_down,
bool  outerjoin_delayed,
bool  pseudoconstant,
Index  security_level,
Relids  required_relids,
Relids  outer_relids,
Relids  nullable_relids 
)

Definition at line 61 of file restrictinfo.c.

70 {
71  /*
72  * If it's an OR clause, build a modified copy with RestrictInfos inserted
73  * above each subclause of the top-level AND/OR structure.
74  */
75  if (is_orclause(clause))
76  return (RestrictInfo *) make_sub_restrictinfos(root,
77  clause,
78  is_pushed_down,
79  outerjoin_delayed,
80  pseudoconstant,
81  security_level,
82  required_relids,
83  outer_relids,
84  nullable_relids);
85 
86  /* Shouldn't be an AND clause, else AND/OR flattening messed up */
87  Assert(!is_andclause(clause));
88 
89  return make_restrictinfo_internal(root,
90  clause,
91  NULL,
92  is_pushed_down,
93  outerjoin_delayed,
94  pseudoconstant,
95  security_level,
96  required_relids,
97  outer_relids,
98  nullable_relids);
99 }
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:105
static bool is_orclause(const void *clause)
Definition: nodeFuncs.h:114
static RestrictInfo * make_restrictinfo_internal(PlannerInfo *root, Expr *clause, Expr *orclause, bool is_pushed_down, bool outerjoin_delayed, bool pseudoconstant, Index security_level, Relids required_relids, Relids outer_relids, Relids nullable_relids)
Definition: restrictinfo.c:107
static Expr * make_sub_restrictinfos(PlannerInfo *root, 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:244

References Assert(), is_andclause(), is_orclause(), make_restrictinfo_internal(), and make_sub_restrictinfos().

Referenced by apply_child_basequals(), build_implied_join_equality(), consider_new_or_clause(), distribute_qual_to_rels(), foreign_grouping_ok(), process_equivalence(), and process_implied_equality().

◆ make_restrictinfo_internal()

static RestrictInfo * make_restrictinfo_internal ( PlannerInfo root,
Expr clause,
Expr orclause,
bool  is_pushed_down,
bool  outerjoin_delayed,
bool  pseudoconstant,
Index  security_level,
Relids  required_relids,
Relids  outer_relids,
Relids  nullable_relids 
)
static

Definition at line 107 of file restrictinfo.c.

117 {
118  RestrictInfo *restrictinfo = makeNode(RestrictInfo);
119 
120  restrictinfo->clause = clause;
121  restrictinfo->orclause = orclause;
122  restrictinfo->is_pushed_down = is_pushed_down;
123  restrictinfo->outerjoin_delayed = outerjoin_delayed;
124  restrictinfo->pseudoconstant = pseudoconstant;
125  restrictinfo->can_join = false; /* may get set below */
126  restrictinfo->security_level = security_level;
127  restrictinfo->outer_relids = outer_relids;
128  restrictinfo->nullable_relids = nullable_relids;
129 
130  /*
131  * If it's potentially delayable by lower-level security quals, figure out
132  * whether it's leakproof. We can skip testing this for level-zero quals,
133  * since they would never get delayed on security grounds anyway.
134  */
135  if (security_level > 0)
136  restrictinfo->leakproof = !contain_leaked_vars((Node *) clause);
137  else
138  restrictinfo->leakproof = false; /* really, "don't know" */
139 
140  /*
141  * Mark volatility as unknown. The contain_volatile_functions function
142  * will determine if there are any volatile functions when called for the
143  * first time with this RestrictInfo.
144  */
145  restrictinfo->has_volatile = VOLATILITY_UNKNOWN;
146 
147  /*
148  * If it's a binary opclause, set up left/right relids info. In any case
149  * set up the total clause relids info.
150  */
151  if (is_opclause(clause) && list_length(((OpExpr *) clause)->args) == 2)
152  {
153  restrictinfo->left_relids = pull_varnos(root, get_leftop(clause));
154  restrictinfo->right_relids = pull_varnos(root, get_rightop(clause));
155 
156  restrictinfo->clause_relids = bms_union(restrictinfo->left_relids,
157  restrictinfo->right_relids);
158 
159  /*
160  * Does it look like a normal join clause, i.e., a binary operator
161  * relating expressions that come from distinct relations? If so we
162  * might be able to use it in a join algorithm. Note that this is a
163  * purely syntactic test that is made regardless of context.
164  */
165  if (!bms_is_empty(restrictinfo->left_relids) &&
166  !bms_is_empty(restrictinfo->right_relids) &&
167  !bms_overlap(restrictinfo->left_relids,
168  restrictinfo->right_relids))
169  {
170  restrictinfo->can_join = true;
171  /* pseudoconstant should certainly not be true */
172  Assert(!restrictinfo->pseudoconstant);
173  }
174  }
175  else
176  {
177  /* Not a binary opclause, so mark left/right relid sets as empty */
178  restrictinfo->left_relids = NULL;
179  restrictinfo->right_relids = NULL;
180  /* and get the total relid set the hard way */
181  restrictinfo->clause_relids = pull_varnos(root, (Node *) clause);
182  }
183 
184  /* required_relids defaults to clause_relids */
185  if (required_relids != NULL)
186  restrictinfo->required_relids = required_relids;
187  else
188  restrictinfo->required_relids = restrictinfo->clause_relids;
189 
190  /*
191  * Fill in all the cacheable fields with "not yet set" markers. None of
192  * these will be computed until/unless needed. Note in particular that we
193  * don't mark a binary opclause as mergejoinable or hashjoinable here;
194  * that happens only if it appears in the right context (top level of a
195  * joinclause list).
196  */
197  restrictinfo->parent_ec = NULL;
198 
199  restrictinfo->eval_cost.startup = -1;
200  restrictinfo->norm_selec = -1;
201  restrictinfo->outer_selec = -1;
202 
203  restrictinfo->mergeopfamilies = NIL;
204 
205  restrictinfo->left_ec = NULL;
206  restrictinfo->right_ec = NULL;
207  restrictinfo->left_em = NULL;
208  restrictinfo->right_em = NULL;
209  restrictinfo->scansel_cache = NIL;
210 
211  restrictinfo->outer_is_left = false;
212 
213  restrictinfo->hashjoinoperator = InvalidOid;
214 
215  restrictinfo->left_bucketsize = -1;
216  restrictinfo->right_bucketsize = -1;
217  restrictinfo->left_mcvfreq = -1;
218  restrictinfo->right_mcvfreq = -1;
219 
220  restrictinfo->left_hasheqoperator = InvalidOid;
221  restrictinfo->right_hasheqoperator = InvalidOid;
222 
223  return restrictinfo;
224 }
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:226
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:704
bool contain_leaked_vars(Node *clause)
Definition: clauses.c:1144
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:74
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:93
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:81
@ VOLATILITY_UNKNOWN
Definition: pathnodes.h:1389
Definition: nodes.h:118
bool is_pushed_down
Definition: pathnodes.h:2435
Index security_level
Definition: pathnodes.h:2453
Relids required_relids
Definition: pathnodes.h:2459
bool outerjoin_delayed
Definition: pathnodes.h:2438
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:100

References generate_unaccent_rules::args, Assert(), bms_is_empty(), bms_overlap(), bms_union(), RestrictInfo::clause, contain_leaked_vars(), get_leftop(), get_rightop(), InvalidOid, is_opclause(), RestrictInfo::is_pushed_down, list_length(), makeNode, NIL, RestrictInfo::nullable_relids, RestrictInfo::outer_relids, RestrictInfo::outerjoin_delayed, pull_varnos(), RestrictInfo::required_relids, RestrictInfo::security_level, and VOLATILITY_UNKNOWN.

Referenced by make_restrictinfo(), and make_sub_restrictinfos().

◆ make_sub_restrictinfos()

static Expr * make_sub_restrictinfos ( PlannerInfo root,
Expr clause,
bool  is_pushed_down,
bool  outerjoin_delayed,
bool  pseudoconstant,
Index  security_level,
Relids  required_relids,
Relids  outer_relids,
Relids  nullable_relids 
)
static

Definition at line 244 of file restrictinfo.c.

253 {
254  if (is_orclause(clause))
255  {
256  List *orlist = NIL;
257  ListCell *temp;
258 
259  foreach(temp, ((BoolExpr *) clause)->args)
260  orlist = lappend(orlist,
262  lfirst(temp),
263  is_pushed_down,
264  outerjoin_delayed,
265  pseudoconstant,
266  security_level,
267  NULL,
268  outer_relids,
269  nullable_relids));
270  return (Expr *) make_restrictinfo_internal(root,
271  clause,
272  make_orclause(orlist),
273  is_pushed_down,
274  outerjoin_delayed,
275  pseudoconstant,
276  security_level,
277  required_relids,
278  outer_relids,
279  nullable_relids);
280  }
281  else if (is_andclause(clause))
282  {
283  List *andlist = NIL;
284  ListCell *temp;
285 
286  foreach(temp, ((BoolExpr *) clause)->args)
287  andlist = lappend(andlist,
289  lfirst(temp),
290  is_pushed_down,
291  outerjoin_delayed,
292  pseudoconstant,
293  security_level,
294  required_relids,
295  outer_relids,
296  nullable_relids));
297  return make_andclause(andlist);
298  }
299  else
300  return (Expr *) make_restrictinfo_internal(root,
301  clause,
302  NULL,
303  is_pushed_down,
304  outerjoin_delayed,
305  pseudoconstant,
306  security_level,
307  required_relids,
308  outer_relids,
309  nullable_relids);
310 }
Expr * make_andclause(List *andclauses)
Definition: makefuncs.c:636
Expr * make_orclause(List *orclauses)
Definition: makefuncs.c:652
#define lfirst(lc)
Definition: pg_list.h:170

References generate_unaccent_rules::args, is_andclause(), is_orclause(), lappend(), lfirst, make_andclause(), make_orclause(), make_restrictinfo_internal(), and NIL.

Referenced by make_restrictinfo().

◆ restriction_is_or_clause()

bool restriction_is_or_clause ( RestrictInfo restrictinfo)

Definition at line 384 of file restrictinfo.c.

385 {
386  if (restrictinfo->orclause != NULL)
387  return true;
388  else
389  return false;
390 }

Referenced by extract_or_clause(), extract_restriction_or_clauses(), generate_bitmap_or_paths(), match_join_clauses_to_index(), and TidQualFromRestrictInfoList().

◆ restriction_is_securely_promotable()

bool restriction_is_securely_promotable ( RestrictInfo restrictinfo,
RelOptInfo rel 
)

Definition at line 399 of file restrictinfo.c.

401 {
402  /*
403  * It's okay if there are no baserestrictinfo clauses for the rel that
404  * would need to go before this one, *or* if this one is leakproof.
405  */
406  if (restrictinfo->security_level <= rel->baserestrict_min_security ||
407  restrictinfo->leakproof)
408  return true;
409  else
410  return false;
411 }
Index baserestrict_min_security
Definition: pathnodes.h:934

References RelOptInfo::baserestrict_min_security, and RestrictInfo::security_level.

Referenced by BuildParameterizedTidPaths(), match_clause_to_index(), and TidQualFromRestrictInfo().