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

Go to the source code of this file.

Functions

static bool join_is_removable (PlannerInfo *root, SpecialJoinInfo *sjinfo)
 
static void remove_rel_from_query (PlannerInfo *root, int relid, SpecialJoinInfo *sjinfo)
 
static void remove_rel_from_restrictinfo (RestrictInfo *rinfo, int relid, int ojrelid)
 
static Listremove_rel_from_joinlist (List *joinlist, int relid, int *nremoved)
 
static bool rel_supports_distinctness (PlannerInfo *root, RelOptInfo *rel)
 
static bool rel_is_distinct_for (PlannerInfo *root, RelOptInfo *rel, List *clause_list)
 
static Oid distinct_col_search (int colno, List *colnos, List *opids)
 
static bool is_innerrel_unique_for (PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist)
 
Listremove_useless_joins (PlannerInfo *root, List *joinlist)
 
static bool clause_sides_match_join (RestrictInfo *rinfo, Relids outerrelids, Relids innerrelids)
 
void reduce_unique_semijoins (PlannerInfo *root)
 
bool query_supports_distinctness (Query *query)
 
bool query_is_distinct_for (Query *query, List *colnos, List *opids)
 
bool innerrel_is_unique (PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache)
 

Function Documentation

◆ clause_sides_match_join()

static bool clause_sides_match_join ( RestrictInfo rinfo,
Relids  outerrelids,
Relids  innerrelids 
)
inlinestatic

Definition at line 128 of file analyzejoins.c.

130 {
131  if (bms_is_subset(rinfo->left_relids, outerrelids) &&
132  bms_is_subset(rinfo->right_relids, innerrelids))
133  {
134  /* lefthand side is outer */
135  rinfo->outer_is_left = true;
136  return true;
137  }
138  else if (bms_is_subset(rinfo->left_relids, innerrelids) &&
139  bms_is_subset(rinfo->right_relids, outerrelids))
140  {
141  /* righthand side is outer */
142  rinfo->outer_is_left = false;
143  return true;
144  }
145  return false; /* no good for these input relations */
146 }
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:332

References bms_is_subset().

Referenced by is_innerrel_unique_for(), and join_is_removable().

◆ distinct_col_search()

static Oid distinct_col_search ( int  colno,
List colnos,
List opids 
)
static

Definition at line 1064 of file analyzejoins.c.

1065 {
1066  ListCell *lc1,
1067  *lc2;
1068 
1069  forboth(lc1, colnos, lc2, opids)
1070  {
1071  if (colno == lfirst_int(lc1))
1072  return lfirst_oid(lc2);
1073  }
1074  return InvalidOid;
1075 }
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:467
#define lfirst_int(lc)
Definition: pg_list.h:173
#define lfirst_oid(lc)
Definition: pg_list.h:174
#define InvalidOid
Definition: postgres_ext.h:36

References forboth, InvalidOid, lfirst_int, and lfirst_oid.

Referenced by query_is_distinct_for().

◆ innerrel_is_unique()

bool innerrel_is_unique ( PlannerInfo root,
Relids  joinrelids,
Relids  outerrelids,
RelOptInfo innerrel,
JoinType  jointype,
List restrictlist,
bool  force_cache 
)

Definition at line 1104 of file analyzejoins.c.

1111 {
1112  MemoryContext old_context;
1113  ListCell *lc;
1114 
1115  /* Certainly can't prove uniqueness when there are no joinclauses */
1116  if (restrictlist == NIL)
1117  return false;
1118 
1119  /*
1120  * Make a quick check to eliminate cases in which we will surely be unable
1121  * to prove uniqueness of the innerrel.
1122  */
1123  if (!rel_supports_distinctness(root, innerrel))
1124  return false;
1125 
1126  /*
1127  * Query the cache to see if we've managed to prove that innerrel is
1128  * unique for any subset of this outerrel. We don't need an exact match,
1129  * as extra outerrels can't make the innerrel any less unique (or more
1130  * formally, the restrictlist for a join to a superset outerrel must be a
1131  * superset of the conditions we successfully used before).
1132  */
1133  foreach(lc, innerrel->unique_for_rels)
1134  {
1135  Relids unique_for_rels = (Relids) lfirst(lc);
1136 
1137  if (bms_is_subset(unique_for_rels, outerrelids))
1138  return true; /* Success! */
1139  }
1140 
1141  /*
1142  * Conversely, we may have already determined that this outerrel, or some
1143  * superset thereof, cannot prove this innerrel to be unique.
1144  */
1145  foreach(lc, innerrel->non_unique_for_rels)
1146  {
1147  Relids unique_for_rels = (Relids) lfirst(lc);
1148 
1149  if (bms_is_subset(outerrelids, unique_for_rels))
1150  return false;
1151  }
1152 
1153  /* No cached information, so try to make the proof. */
1154  if (is_innerrel_unique_for(root, joinrelids, outerrelids, innerrel,
1155  jointype, restrictlist))
1156  {
1157  /*
1158  * Cache the positive result for future probes, being sure to keep it
1159  * in the planner_cxt even if we are working in GEQO.
1160  *
1161  * Note: one might consider trying to isolate the minimal subset of
1162  * the outerrels that proved the innerrel unique. But it's not worth
1163  * the trouble, because the planner builds up joinrels incrementally
1164  * and so we'll see the minimally sufficient outerrels before any
1165  * supersets of them anyway.
1166  */
1167  old_context = MemoryContextSwitchTo(root->planner_cxt);
1168  innerrel->unique_for_rels = lappend(innerrel->unique_for_rels,
1169  bms_copy(outerrelids));
1170  MemoryContextSwitchTo(old_context);
1171 
1172  return true; /* Success! */
1173  }
1174  else
1175  {
1176  /*
1177  * None of the join conditions for outerrel proved innerrel unique, so
1178  * we can safely reject this outerrel or any subset of it in future
1179  * checks.
1180  *
1181  * However, in normal planning mode, caching this knowledge is totally
1182  * pointless; it won't be queried again, because we build up joinrels
1183  * from smaller to larger. It is useful in GEQO mode, where the
1184  * knowledge can be carried across successive planning attempts; and
1185  * it's likely to be useful when using join-search plugins, too. Hence
1186  * cache when join_search_private is non-NULL. (Yeah, that's a hack,
1187  * but it seems reasonable.)
1188  *
1189  * Also, allow callers to override that heuristic and force caching;
1190  * that's useful for reduce_unique_semijoins, which calls here before
1191  * the normal join search starts.
1192  */
1193  if (force_cache || root->join_search_private)
1194  {
1195  old_context = MemoryContextSwitchTo(root->planner_cxt);
1196  innerrel->non_unique_for_rels =
1197  lappend(innerrel->non_unique_for_rels,
1198  bms_copy(outerrelids));
1199  MemoryContextSwitchTo(old_context);
1200  }
1201 
1202  return false;
1203  }
1204 }
static bool is_innerrel_unique_for(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist)
static bool rel_supports_distinctness(PlannerInfo *root, RelOptInfo *rel)
Definition: analyzejoins.c:725
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
List * lappend(List *list, void *datum)
Definition: list.c:338
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
Bitmapset * Relids
Definition: pathnodes.h:30
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
List * unique_for_rels
Definition: pathnodes.h:956
List * non_unique_for_rels
Definition: pathnodes.h:958

References bms_copy(), bms_is_subset(), is_innerrel_unique_for(), lappend(), lfirst, MemoryContextSwitchTo(), NIL, RelOptInfo::non_unique_for_rels, rel_supports_distinctness(), and RelOptInfo::unique_for_rels.

Referenced by add_paths_to_joinrel(), and reduce_unique_semijoins().

◆ is_innerrel_unique_for()

static bool is_innerrel_unique_for ( PlannerInfo root,
Relids  joinrelids,
Relids  outerrelids,
RelOptInfo innerrel,
JoinType  jointype,
List restrictlist 
)
static

Definition at line 1212 of file analyzejoins.c.

1218 {
1219  List *clause_list = NIL;
1220  ListCell *lc;
1221 
1222  /*
1223  * Search for mergejoinable clauses that constrain the inner rel against
1224  * the outer rel. If an operator is mergejoinable then it behaves like
1225  * equality for some btree opclass, so it's what we want. The
1226  * mergejoinability test also eliminates clauses containing volatile
1227  * functions, which we couldn't depend on.
1228  */
1229  foreach(lc, restrictlist)
1230  {
1231  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1232 
1233  /*
1234  * As noted above, if it's a pushed-down clause and we're at an outer
1235  * join, we can't use it.
1236  */
1237  if (IS_OUTER_JOIN(jointype) &&
1238  RINFO_IS_PUSHED_DOWN(restrictinfo, joinrelids))
1239  continue;
1240 
1241  /* Ignore if it's not a mergejoinable clause */
1242  if (!restrictinfo->can_join ||
1243  restrictinfo->mergeopfamilies == NIL)
1244  continue; /* not mergejoinable */
1245 
1246  /*
1247  * Check if clause has the form "outer op inner" or "inner op outer",
1248  * and if so mark which side is inner.
1249  */
1250  if (!clause_sides_match_join(restrictinfo, outerrelids,
1251  innerrel->relids))
1252  continue; /* no good for these input relations */
1253 
1254  /* OK, add to list */
1255  clause_list = lappend(clause_list, restrictinfo);
1256  }
1257 
1258  /* Let rel_is_distinct_for() do the hard work */
1259  return rel_is_distinct_for(root, innerrel, clause_list);
1260 }
static bool clause_sides_match_join(RestrictInfo *rinfo, Relids outerrelids, Relids innerrelids)
Definition: analyzejoins.c:128
static bool rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel, List *clause_list)
Definition: analyzejoins.c:781
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:348
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: pathnodes.h:2670
Definition: pg_list.h:54
Relids relids
Definition: pathnodes.h:856

References clause_sides_match_join(), IS_OUTER_JOIN, lappend(), lfirst, NIL, rel_is_distinct_for(), RelOptInfo::relids, and RINFO_IS_PUSHED_DOWN.

Referenced by innerrel_is_unique().

◆ join_is_removable()

static bool join_is_removable ( PlannerInfo root,
SpecialJoinInfo sjinfo 
)
static

Definition at line 160 of file analyzejoins.c.

161 {
162  int innerrelid;
163  RelOptInfo *innerrel;
164  Relids inputrelids;
165  Relids joinrelids;
166  List *clause_list = NIL;
167  ListCell *l;
168  int attroff;
169 
170  /*
171  * Must be a left join to a single baserel, else we aren't going to be
172  * able to do anything with it.
173  */
174  if (sjinfo->jointype != JOIN_LEFT)
175  return false;
176 
177  if (!bms_get_singleton_member(sjinfo->min_righthand, &innerrelid))
178  return false;
179 
180  /*
181  * Never try to eliminate a left join to the query result rel. Although
182  * the case is syntactically impossible in standard SQL, MERGE will build
183  * a join tree that looks exactly like that.
184  */
185  if (innerrelid == root->parse->resultRelation)
186  return false;
187 
188  innerrel = find_base_rel(root, innerrelid);
189 
190  /*
191  * Before we go to the effort of checking whether any innerrel variables
192  * are needed above the join, make a quick check to eliminate cases in
193  * which we will surely be unable to prove uniqueness of the innerrel.
194  */
195  if (!rel_supports_distinctness(root, innerrel))
196  return false;
197 
198  /* Compute the relid set for the join we are considering */
199  inputrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
200  Assert(sjinfo->ojrelid != 0);
201  joinrelids = bms_copy(inputrelids);
202  joinrelids = bms_add_member(joinrelids, sjinfo->ojrelid);
203 
204  /*
205  * We can't remove the join if any inner-rel attributes are used above the
206  * join. Here, "above" the join includes pushed-down conditions, so we
207  * should reject if attr_needed includes the OJ's own relid; therefore,
208  * compare to inputrelids not joinrelids.
209  *
210  * As a micro-optimization, it seems better to start with max_attr and
211  * count down rather than starting with min_attr and counting up, on the
212  * theory that the system attributes are somewhat less likely to be wanted
213  * and should be tested last.
214  */
215  for (attroff = innerrel->max_attr - innerrel->min_attr;
216  attroff >= 0;
217  attroff--)
218  {
219  if (!bms_is_subset(innerrel->attr_needed[attroff], inputrelids))
220  return false;
221  }
222 
223  /*
224  * Similarly check that the inner rel isn't needed by any PlaceHolderVars
225  * that will be used above the join. The PHV case is a little bit more
226  * complicated, because PHVs may have been assigned a ph_eval_at location
227  * that includes the innerrel, yet their contained expression might not
228  * actually reference the innerrel (it could be just a constant, for
229  * instance). If such a PHV is due to be evaluated above the join then it
230  * needn't prevent join removal.
231  */
232  foreach(l, root->placeholder_list)
233  {
234  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
235 
236  if (bms_overlap(phinfo->ph_lateral, innerrel->relids))
237  return false; /* it references innerrel laterally */
238  if (!bms_overlap(phinfo->ph_eval_at, innerrel->relids))
239  continue; /* it definitely doesn't reference innerrel */
240  if (bms_is_subset(phinfo->ph_needed, inputrelids))
241  continue; /* PHV is not used above the join */
242  if (!bms_is_member(sjinfo->ojrelid, phinfo->ph_eval_at))
243  return false; /* it has to be evaluated below the join */
244 
245  /*
246  * We need to be sure there will still be a place to evaluate the PHV
247  * if we remove the join, ie that ph_eval_at wouldn't become empty.
248  */
249  if (!bms_overlap(sjinfo->min_lefthand, phinfo->ph_eval_at))
250  return false; /* there isn't any other place to eval PHV */
251  /* Check contained expression last, since this is a bit expensive */
252  if (bms_overlap(pull_varnos(root, (Node *) phinfo->ph_var->phexpr),
253  innerrel->relids))
254  return false; /* contained expression references innerrel */
255  }
256 
257  /*
258  * Search for mergejoinable clauses that constrain the inner rel against
259  * either the outer rel or a pseudoconstant. If an operator is
260  * mergejoinable then it behaves like equality for some btree opclass, so
261  * it's what we want. The mergejoinability test also eliminates clauses
262  * containing volatile functions, which we couldn't depend on.
263  */
264  foreach(l, innerrel->joininfo)
265  {
266  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
267 
268  /*
269  * If the current join commutes with some other outer join(s) via
270  * outer join identity 3, there will be multiple clones of its join
271  * clauses in the joininfo list. We want to consider only the
272  * has_clone form of such clauses. Processing more than one form
273  * would be wasteful, and also some of the others would confuse the
274  * RINFO_IS_PUSHED_DOWN test below.
275  */
276  if (restrictinfo->is_clone)
277  continue; /* ignore it */
278 
279  /*
280  * If it's not a join clause for this outer join, we can't use it.
281  * Note that if the clause is pushed-down, then it is logically from
282  * above the outer join, even if it references no other rels (it might
283  * be from WHERE, for example).
284  */
285  if (RINFO_IS_PUSHED_DOWN(restrictinfo, joinrelids))
286  continue; /* ignore; not useful here */
287 
288  /* Ignore if it's not a mergejoinable clause */
289  if (!restrictinfo->can_join ||
290  restrictinfo->mergeopfamilies == NIL)
291  continue; /* not mergejoinable */
292 
293  /*
294  * Check if clause has the form "outer op inner" or "inner op outer",
295  * and if so mark which side is inner.
296  */
297  if (!clause_sides_match_join(restrictinfo, sjinfo->min_lefthand,
298  innerrel->relids))
299  continue; /* no good for these input relations */
300 
301  /* OK, add to list */
302  clause_list = lappend(clause_list, restrictinfo);
303  }
304 
305  /*
306  * Now that we have the relevant equality join clauses, try to prove the
307  * innerrel distinct.
308  */
309  if (rel_is_distinct_for(root, innerrel, clause_list))
310  return true;
311 
312  /*
313  * Some day it would be nice to check for other methods of establishing
314  * distinctness.
315  */
316  return false;
317 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:444
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:755
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:226
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:511
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:634
Assert(fmt[strlen(fmt) - 1] !='\n')
@ JOIN_LEFT
Definition: nodes.h:305
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:405
Definition: nodes.h:129
Relids ph_lateral
Definition: pathnodes.h:3037
Relids ph_needed
Definition: pathnodes.h:3040
Relids ph_eval_at
Definition: pathnodes.h:3034
PlaceHolderVar * ph_var
Definition: pathnodes.h:3031
List * placeholder_list
Definition: pathnodes.h:371
Query * parse
Definition: pathnodes.h:199
List * joininfo
Definition: pathnodes.h:970
AttrNumber max_attr
Definition: pathnodes.h:911
AttrNumber min_attr
Definition: pathnodes.h:909
Relids min_righthand
Definition: pathnodes.h:2841
JoinType jointype
Definition: pathnodes.h:2844
Relids min_lefthand
Definition: pathnodes.h:2840
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:108

References Assert(), bms_add_member(), bms_copy(), bms_get_singleton_member(), bms_is_member(), bms_is_subset(), bms_overlap(), bms_union(), clause_sides_match_join(), find_base_rel(), RestrictInfo::is_clone, JOIN_LEFT, RelOptInfo::joininfo, SpecialJoinInfo::jointype, lappend(), lfirst, RelOptInfo::max_attr, RelOptInfo::min_attr, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, NIL, SpecialJoinInfo::ojrelid, PlannerInfo::parse, PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_lateral, PlaceHolderInfo::ph_needed, PlaceHolderInfo::ph_var, PlannerInfo::placeholder_list, pull_varnos(), rel_is_distinct_for(), rel_supports_distinctness(), RelOptInfo::relids, and RINFO_IS_PUSHED_DOWN.

Referenced by remove_useless_joins().

◆ query_is_distinct_for()

bool query_is_distinct_for ( Query query,
List colnos,
List opids 
)

Definition at line 915 of file analyzejoins.c.

916 {
917  ListCell *l;
918  Oid opid;
919 
920  Assert(list_length(colnos) == list_length(opids));
921 
922  /*
923  * DISTINCT (including DISTINCT ON) guarantees uniqueness if all the
924  * columns in the DISTINCT clause appear in colnos and operator semantics
925  * match. This is true even if there are SRFs in the DISTINCT columns or
926  * elsewhere in the tlist.
927  */
928  if (query->distinctClause)
929  {
930  foreach(l, query->distinctClause)
931  {
932  SortGroupClause *sgc = (SortGroupClause *) lfirst(l);
934  query->targetList);
935 
936  opid = distinct_col_search(tle->resno, colnos, opids);
937  if (!OidIsValid(opid) ||
938  !equality_ops_are_compatible(opid, sgc->eqop))
939  break; /* exit early if no match */
940  }
941  if (l == NULL) /* had matches for all? */
942  return true;
943  }
944 
945  /*
946  * Otherwise, a set-returning function in the query's targetlist can
947  * result in returning duplicate rows, despite any grouping that might
948  * occur before tlist evaluation. (If all tlist SRFs are within GROUP BY
949  * columns, it would be safe because they'd be expanded before grouping.
950  * But it doesn't currently seem worth the effort to check for that.)
951  */
952  if (query->hasTargetSRFs)
953  return false;
954 
955  /*
956  * Similarly, GROUP BY without GROUPING SETS guarantees uniqueness if all
957  * the grouped columns appear in colnos and operator semantics match.
958  */
959  if (query->groupClause && !query->groupingSets)
960  {
961  foreach(l, query->groupClause)
962  {
963  SortGroupClause *sgc = (SortGroupClause *) lfirst(l);
965  query->targetList);
966 
967  opid = distinct_col_search(tle->resno, colnos, opids);
968  if (!OidIsValid(opid) ||
969  !equality_ops_are_compatible(opid, sgc->eqop))
970  break; /* exit early if no match */
971  }
972  if (l == NULL) /* had matches for all? */
973  return true;
974  }
975  else if (query->groupingSets)
976  {
977  /*
978  * If we have grouping sets with expressions, we probably don't have
979  * uniqueness and analysis would be hard. Punt.
980  */
981  if (query->groupClause)
982  return false;
983 
984  /*
985  * If we have no groupClause (therefore no grouping expressions), we
986  * might have one or many empty grouping sets. If there's just one,
987  * then we're returning only one row and are certainly unique. But
988  * otherwise, we know we're certainly not unique.
989  */
990  if (list_length(query->groupingSets) == 1 &&
991  ((GroupingSet *) linitial(query->groupingSets))->kind == GROUPING_SET_EMPTY)
992  return true;
993  else
994  return false;
995  }
996  else
997  {
998  /*
999  * If we have no GROUP BY, but do have aggregates or HAVING, then the
1000  * result is at most one row so it's surely unique, for any operators.
1001  */
1002  if (query->hasAggs || query->havingQual)
1003  return true;
1004  }
1005 
1006  /*
1007  * UNION, INTERSECT, EXCEPT guarantee uniqueness of the whole output row,
1008  * except with ALL.
1009  */
1010  if (query->setOperations)
1011  {
1013 
1014  Assert(topop->op != SETOP_NONE);
1015 
1016  if (!topop->all)
1017  {
1018  ListCell *lg;
1019 
1020  /* We're good if all the nonjunk output columns are in colnos */
1021  lg = list_head(topop->groupClauses);
1022  foreach(l, query->targetList)
1023  {
1024  TargetEntry *tle = (TargetEntry *) lfirst(l);
1025  SortGroupClause *sgc;
1026 
1027  if (tle->resjunk)
1028  continue; /* ignore resjunk columns */
1029 
1030  /* non-resjunk columns should have grouping clauses */
1031  Assert(lg != NULL);
1032  sgc = (SortGroupClause *) lfirst(lg);
1033  lg = lnext(topop->groupClauses, lg);
1034 
1035  opid = distinct_col_search(tle->resno, colnos, opids);
1036  if (!OidIsValid(opid) ||
1037  !equality_ops_are_compatible(opid, sgc->eqop))
1038  break; /* exit early if no match */
1039  }
1040  if (l == NULL) /* had matches for all? */
1041  return true;
1042  }
1043  }
1044 
1045  /*
1046  * XXX Are there any other cases in which we can easily see the result
1047  * must be distinct?
1048  *
1049  * If you do add more smarts to this function, be sure to update
1050  * query_supports_distinctness() to match.
1051  */
1052 
1053  return false;
1054 }
static Oid distinct_col_search(int colno, List *colnos, List *opids)
#define OidIsValid(objectId)
Definition: c.h:759
bool equality_ops_are_compatible(Oid opno1, Oid opno2)
Definition: lsyscache.c:697
#define castNode(_type_, nodeptr)
Definition: nodes.h:197
@ GROUPING_SET_EMPTY
Definition: parsenodes.h:1453
@ SETOP_NONE
Definition: parsenodes.h:1934
static int list_length(const List *l)
Definition: pg_list.h:152
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
#define linitial(l)
Definition: pg_list.h:178
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
unsigned int Oid
Definition: postgres_ext.h:31
Node * setOperations
Definition: parsenodes.h:217
List * groupClause
Definition: parsenodes.h:198
Node * havingQual
Definition: parsenodes.h:203
List * targetList
Definition: parsenodes.h:189
List * groupingSets
Definition: parsenodes.h:201
List * distinctClause
Definition: parsenodes.h:207
SetOperation op
Definition: parsenodes.h:2012
AttrNumber resno
Definition: primnodes.h:1888
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:367

References SetOperationStmt::all, Assert(), castNode, distinct_col_search(), Query::distinctClause, SortGroupClause::eqop, equality_ops_are_compatible(), get_sortgroupclause_tle(), Query::groupClause, GROUPING_SET_EMPTY, Query::groupingSets, Query::havingQual, lfirst, linitial, list_head(), list_length(), lnext(), OidIsValid, SetOperationStmt::op, TargetEntry::resno, SETOP_NONE, Query::setOperations, and Query::targetList.

Referenced by create_unique_path(), and rel_is_distinct_for().

◆ query_supports_distinctness()

bool query_supports_distinctness ( Query query)

Definition at line 878 of file analyzejoins.c.

879 {
880  /* SRFs break distinctness except with DISTINCT, see below */
881  if (query->hasTargetSRFs && query->distinctClause == NIL)
882  return false;
883 
884  /* check for features we can prove distinctness with */
885  if (query->distinctClause != NIL ||
886  query->groupClause != NIL ||
887  query->groupingSets != NIL ||
888  query->hasAggs ||
889  query->havingQual ||
890  query->setOperations)
891  return true;
892 
893  return false;
894 }

References Query::distinctClause, Query::groupClause, Query::groupingSets, Query::havingQual, NIL, and Query::setOperations.

Referenced by create_unique_path(), and rel_supports_distinctness().

◆ reduce_unique_semijoins()

void reduce_unique_semijoins ( PlannerInfo root)

Definition at line 649 of file analyzejoins.c.

650 {
651  ListCell *lc;
652 
653  /*
654  * Scan the join_info_list to find semijoins.
655  */
656  foreach(lc, root->join_info_list)
657  {
658  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
659  int innerrelid;
660  RelOptInfo *innerrel;
661  Relids joinrelids;
662  List *restrictlist;
663 
664  /*
665  * Must be a semijoin to a single baserel, else we aren't going to be
666  * able to do anything with it.
667  */
668  if (sjinfo->jointype != JOIN_SEMI)
669  continue;
670 
671  if (!bms_get_singleton_member(sjinfo->min_righthand, &innerrelid))
672  continue;
673 
674  innerrel = find_base_rel(root, innerrelid);
675 
676  /*
677  * Before we trouble to run generate_join_implied_equalities, make a
678  * quick check to eliminate cases in which we will surely be unable to
679  * prove uniqueness of the innerrel.
680  */
681  if (!rel_supports_distinctness(root, innerrel))
682  continue;
683 
684  /* Compute the relid set for the join we are considering */
685  joinrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
686  Assert(sjinfo->ojrelid == 0); /* SEMI joins don't have RT indexes */
687 
688  /*
689  * Since we're only considering a single-rel RHS, any join clauses it
690  * has must be clauses linking it to the semijoin's min_lefthand. We
691  * can also consider EC-derived join clauses.
692  */
693  restrictlist =
695  joinrelids,
696  sjinfo->min_lefthand,
697  innerrel,
698  NULL),
699  innerrel->joininfo);
700 
701  /* Test whether the innerrel is unique for those clauses. */
702  if (!innerrel_is_unique(root,
703  joinrelids, sjinfo->min_lefthand, innerrel,
704  JOIN_SEMI, restrictlist, true))
705  continue;
706 
707  /* OK, remove the SpecialJoinInfo from the list. */
709  }
710 }
bool innerrel_is_unique(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache)
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
Definition: equivclass.c:1381
List * list_concat(List *list1, const List *list2)
Definition: list.c:560
@ JOIN_SEMI
Definition: nodes.h:318
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:390
List * join_info_list
Definition: pathnodes.h:337

References Assert(), bms_get_singleton_member(), bms_union(), find_base_rel(), foreach_delete_current, generate_join_implied_equalities(), innerrel_is_unique(), PlannerInfo::join_info_list, JOIN_SEMI, RelOptInfo::joininfo, SpecialJoinInfo::jointype, lfirst, list_concat(), SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, SpecialJoinInfo::ojrelid, and rel_supports_distinctness().

Referenced by query_planner().

◆ rel_is_distinct_for()

static bool rel_is_distinct_for ( PlannerInfo root,
RelOptInfo rel,
List clause_list 
)
static

Definition at line 781 of file analyzejoins.c.

782 {
783  /*
784  * We could skip a couple of tests here if we assume all callers checked
785  * rel_supports_distinctness first, but it doesn't seem worth taking any
786  * risk for.
787  */
788  if (rel->reloptkind != RELOPT_BASEREL)
789  return false;
790  if (rel->rtekind == RTE_RELATION)
791  {
792  /*
793  * Examine the indexes to see if we have a matching unique index.
794  * relation_has_unique_index_for automatically adds any usable
795  * restriction clauses for the rel, so we needn't do that here.
796  */
797  if (relation_has_unique_index_for(root, rel, clause_list, NIL, NIL))
798  return true;
799  }
800  else if (rel->rtekind == RTE_SUBQUERY)
801  {
802  Index relid = rel->relid;
803  Query *subquery = root->simple_rte_array[relid]->subquery;
804  List *colnos = NIL;
805  List *opids = NIL;
806  ListCell *l;
807 
808  /*
809  * Build the argument lists for query_is_distinct_for: a list of
810  * output column numbers that the query needs to be distinct over, and
811  * a list of equality operators that the output columns need to be
812  * distinct according to.
813  *
814  * (XXX we are not considering restriction clauses attached to the
815  * subquery; is that worth doing?)
816  */
817  foreach(l, clause_list)
818  {
820  Oid op;
821  Var *var;
822 
823  /*
824  * Get the equality operator we need uniqueness according to.
825  * (This might be a cross-type operator and thus not exactly the
826  * same operator the subquery would consider; that's all right
827  * since query_is_distinct_for can resolve such cases.) The
828  * caller's mergejoinability test should have selected only
829  * OpExprs.
830  */
831  op = castNode(OpExpr, rinfo->clause)->opno;
832 
833  /* caller identified the inner side for us */
834  if (rinfo->outer_is_left)
835  var = (Var *) get_rightop(rinfo->clause);
836  else
837  var = (Var *) get_leftop(rinfo->clause);
838 
839  /*
840  * We may ignore any RelabelType node above the operand. (There
841  * won't be more than one, since eval_const_expressions() has been
842  * applied already.)
843  */
844  if (var && IsA(var, RelabelType))
845  var = (Var *) ((RelabelType *) var)->arg;
846 
847  /*
848  * If inner side isn't a Var referencing a subquery output column,
849  * this clause doesn't help us.
850  */
851  if (!var || !IsA(var, Var) ||
852  var->varno != relid || var->varlevelsup != 0)
853  continue;
854 
855  colnos = lappend_int(colnos, var->varattno);
856  opids = lappend_oid(opids, op);
857  }
858 
859  if (query_is_distinct_for(subquery, colnos, opids))
860  return true;
861  }
862  return false;
863 }
bool query_is_distinct_for(Query *query, List *colnos, List *opids)
Definition: analyzejoins.c:915
unsigned int Index
Definition: c.h:598
bool relation_has_unique_index_for(PlannerInfo *root, RelOptInfo *rel, List *restrictlist, List *exprlist, List *oprlist)
Definition: indxpath.c:3491
List * lappend_int(List *list, int datum)
Definition: list.c:356
List * lappend_oid(List *list, Oid datum)
Definition: list.c:374
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:93
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:81
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
@ RTE_SUBQUERY
Definition: parsenodes.h:1015
@ RTE_RELATION
Definition: parsenodes.h:1014
@ RELOPT_BASEREL
Definition: pathnodes.h:812
#define lfirst_node(type, lc)
Definition: pg_list.h:176
Index relid
Definition: pathnodes.h:903
RelOptKind reloptkind
Definition: pathnodes.h:850
RTEKind rtekind
Definition: pathnodes.h:907
Expr * clause
Definition: pathnodes.h:2516
Definition: primnodes.h:226
AttrNumber varattno
Definition: primnodes.h:238
int varno
Definition: primnodes.h:233
Index varlevelsup
Definition: primnodes.h:258

References castNode, RestrictInfo::clause, get_leftop(), get_rightop(), IsA, lappend_int(), lappend_oid(), lfirst_node, NIL, query_is_distinct_for(), relation_has_unique_index_for(), RelOptInfo::relid, RELOPT_BASEREL, RelOptInfo::reloptkind, RTE_RELATION, RTE_SUBQUERY, RelOptInfo::rtekind, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by is_innerrel_unique_for(), and join_is_removable().

◆ rel_supports_distinctness()

static bool rel_supports_distinctness ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 725 of file analyzejoins.c.

726 {
727  /* We only know about baserels ... */
728  if (rel->reloptkind != RELOPT_BASEREL)
729  return false;
730  if (rel->rtekind == RTE_RELATION)
731  {
732  /*
733  * For a plain relation, we only know how to prove uniqueness by
734  * reference to unique indexes. Make sure there's at least one
735  * suitable unique index. It must be immediately enforced, and if
736  * it's a partial index, it must match the query. (Keep these
737  * conditions in sync with relation_has_unique_index_for!)
738  */
739  ListCell *lc;
740 
741  foreach(lc, rel->indexlist)
742  {
743  IndexOptInfo *ind = (IndexOptInfo *) lfirst(lc);
744 
745  if (ind->unique && ind->immediate &&
746  (ind->indpred == NIL || ind->predOK))
747  return true;
748  }
749  }
750  else if (rel->rtekind == RTE_SUBQUERY)
751  {
752  Query *subquery = root->simple_rte_array[rel->relid]->subquery;
753 
754  /* Check if the subquery has any qualities that support distinctness */
755  if (query_supports_distinctness(subquery))
756  return true;
757  }
758  /* We have no proof rules for any other rtekinds. */
759  return false;
760 }
bool query_supports_distinctness(Query *query)
Definition: analyzejoins.c:878
List * indexlist
Definition: pathnodes.h:923

References RelOptInfo::indexlist, lfirst, NIL, query_supports_distinctness(), RelOptInfo::relid, RELOPT_BASEREL, RelOptInfo::reloptkind, RTE_RELATION, RTE_SUBQUERY, and RelOptInfo::rtekind.

Referenced by innerrel_is_unique(), join_is_removable(), and reduce_unique_semijoins().

◆ remove_rel_from_joinlist()

static List * remove_rel_from_joinlist ( List joinlist,
int  relid,
int *  nremoved 
)
static

Definition at line 595 of file analyzejoins.c.

596 {
597  List *result = NIL;
598  ListCell *jl;
599 
600  foreach(jl, joinlist)
601  {
602  Node *jlnode = (Node *) lfirst(jl);
603 
604  if (IsA(jlnode, RangeTblRef))
605  {
606  int varno = ((RangeTblRef *) jlnode)->rtindex;
607 
608  if (varno == relid)
609  (*nremoved)++;
610  else
611  result = lappend(result, jlnode);
612  }
613  else if (IsA(jlnode, List))
614  {
615  /* Recurse to handle subproblem */
616  List *sublist;
617 
618  sublist = remove_rel_from_joinlist((List *) jlnode,
619  relid, nremoved);
620  /* Avoid including empty sub-lists in the result */
621  if (sublist)
622  result = lappend(result, sublist);
623  }
624  else
625  {
626  elog(ERROR, "unrecognized joinlist node type: %d",
627  (int) nodeTag(jlnode));
628  }
629  }
630 
631  return result;
632 }
static List * remove_rel_from_joinlist(List *joinlist, int relid, int *nremoved)
Definition: analyzejoins.c:595
#define ERROR
Definition: elog.h:39
#define nodeTag(nodeptr)
Definition: nodes.h:133

References elog(), ERROR, IsA, lappend(), lfirst, NIL, and nodeTag.

Referenced by remove_useless_joins().

◆ remove_rel_from_query()

static void remove_rel_from_query ( PlannerInfo root,
int  relid,
SpecialJoinInfo sjinfo 
)
static

Definition at line 329 of file analyzejoins.c.

330 {
331  RelOptInfo *rel = find_base_rel(root, relid);
332  int ojrelid = sjinfo->ojrelid;
333  Relids joinrelids;
334  Relids join_plus_commute;
335  List *joininfos;
336  Index rti;
337  ListCell *l;
338 
339  /* Compute the relid set for the join we are considering */
340  joinrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
341  Assert(ojrelid != 0);
342  joinrelids = bms_add_member(joinrelids, ojrelid);
343 
344  /*
345  * Remove references to the rel from other baserels' attr_needed arrays.
346  */
347  for (rti = 1; rti < root->simple_rel_array_size; rti++)
348  {
349  RelOptInfo *otherrel = root->simple_rel_array[rti];
350  int attroff;
351 
352  /* there may be empty slots corresponding to non-baserel RTEs */
353  if (otherrel == NULL)
354  continue;
355 
356  Assert(otherrel->relid == rti); /* sanity check on array */
357 
358  /* no point in processing target rel itself */
359  if (otherrel == rel)
360  continue;
361 
362  for (attroff = otherrel->max_attr - otherrel->min_attr;
363  attroff >= 0;
364  attroff--)
365  {
366  otherrel->attr_needed[attroff] =
367  bms_del_member(otherrel->attr_needed[attroff], relid);
368  otherrel->attr_needed[attroff] =
369  bms_del_member(otherrel->attr_needed[attroff], ojrelid);
370  }
371  }
372 
373  /*
374  * Update all_baserels and related relid sets.
375  */
376  root->all_baserels = bms_del_member(root->all_baserels, relid);
377  root->outer_join_rels = bms_del_member(root->outer_join_rels, ojrelid);
378  root->all_query_rels = bms_del_member(root->all_query_rels, relid);
379  root->all_query_rels = bms_del_member(root->all_query_rels, ojrelid);
380 
381  /*
382  * Likewise remove references from SpecialJoinInfo data structures.
383  *
384  * This is relevant in case the outer join we're deleting is nested inside
385  * other outer joins: the upper joins' relid sets have to be adjusted. The
386  * RHS of the target outer join will be made empty here, but that's OK
387  * since caller will delete that SpecialJoinInfo entirely.
388  */
389  foreach(l, root->join_info_list)
390  {
391  SpecialJoinInfo *sjinf = (SpecialJoinInfo *) lfirst(l);
392 
393  sjinf->min_lefthand = bms_del_member(sjinf->min_lefthand, relid);
394  sjinf->min_righthand = bms_del_member(sjinf->min_righthand, relid);
395  sjinf->syn_lefthand = bms_del_member(sjinf->syn_lefthand, relid);
396  sjinf->syn_righthand = bms_del_member(sjinf->syn_righthand, relid);
397  sjinf->min_lefthand = bms_del_member(sjinf->min_lefthand, ojrelid);
398  sjinf->min_righthand = bms_del_member(sjinf->min_righthand, ojrelid);
399  sjinf->syn_lefthand = bms_del_member(sjinf->syn_lefthand, ojrelid);
400  sjinf->syn_righthand = bms_del_member(sjinf->syn_righthand, ojrelid);
401  /* relid cannot appear in these fields, but ojrelid can: */
402  sjinf->commute_above_l = bms_del_member(sjinf->commute_above_l, ojrelid);
403  sjinf->commute_above_r = bms_del_member(sjinf->commute_above_r, ojrelid);
404  sjinf->commute_below_l = bms_del_member(sjinf->commute_below_l, ojrelid);
405  sjinf->commute_below_r = bms_del_member(sjinf->commute_below_r, ojrelid);
406  }
407 
408  /*
409  * Likewise remove references from PlaceHolderVar data structures,
410  * removing any no-longer-needed placeholders entirely.
411  *
412  * Removal is a bit trickier than it might seem: we can remove PHVs that
413  * are used at the target rel and/or in the join qual, but not those that
414  * are used at join partner rels or above the join. It's not that easy to
415  * distinguish PHVs used at partner rels from those used in the join qual,
416  * since they will both have ph_needed sets that are subsets of
417  * joinrelids. However, a PHV used at a partner rel could not have the
418  * target rel in ph_eval_at, so we check that while deciding whether to
419  * remove or just update the PHV. There is no corresponding test in
420  * join_is_removable because it doesn't need to distinguish those cases.
421  */
422  foreach(l, root->placeholder_list)
423  {
424  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
425 
426  Assert(!bms_is_member(relid, phinfo->ph_lateral));
427  if (bms_is_subset(phinfo->ph_needed, joinrelids) &&
428  bms_is_member(relid, phinfo->ph_eval_at))
429  {
431  l);
432  root->placeholder_array[phinfo->phid] = NULL;
433  }
434  else
435  {
436  PlaceHolderVar *phv = phinfo->ph_var;
437 
438  phinfo->ph_eval_at = bms_del_member(phinfo->ph_eval_at, relid);
439  phinfo->ph_eval_at = bms_del_member(phinfo->ph_eval_at, ojrelid);
440  Assert(!bms_is_empty(phinfo->ph_eval_at)); /* checked previously */
441  phinfo->ph_needed = bms_del_member(phinfo->ph_needed, relid);
442  phinfo->ph_needed = bms_del_member(phinfo->ph_needed, ojrelid);
443  /* ph_needed might or might not become empty */
444  phv->phrels = bms_del_member(phv->phrels, relid);
445  phv->phrels = bms_del_member(phv->phrels, ojrelid);
446  Assert(!bms_is_empty(phv->phrels));
447  Assert(phv->phnullingrels == NULL); /* no need to adjust */
448  }
449  }
450 
451  /*
452  * Remove any joinquals referencing the rel from the joininfo lists.
453  *
454  * In some cases, a joinqual has to be put back after deleting its
455  * reference to the target rel. This can occur for pseudoconstant and
456  * outerjoin-delayed quals, which can get marked as requiring the rel in
457  * order to force them to be evaluated at or above the join. We can't
458  * just discard them, though. Only quals that logically belonged to the
459  * outer join being discarded should be removed from the query.
460  *
461  * We might encounter a qual that is a clone of a deletable qual with some
462  * outer-join relids added (see deconstruct_distribute_oj_quals). To
463  * ensure we get rid of such clones as well, add the relids of all OJs
464  * commutable with this one to the set we test against for
465  * pushed-down-ness.
466  */
467  join_plus_commute = bms_union(joinrelids,
468  sjinfo->commute_above_r);
469  join_plus_commute = bms_add_members(join_plus_commute,
470  sjinfo->commute_below_l);
471 
472  /*
473  * We must make a copy of the rel's old joininfo list before starting the
474  * loop, because otherwise remove_join_clause_from_rels would destroy the
475  * list while we're scanning it.
476  */
477  joininfos = list_copy(rel->joininfo);
478  foreach(l, joininfos)
479  {
480  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
481 
482  remove_join_clause_from_rels(root, rinfo, rinfo->required_relids);
483 
484  if (RINFO_IS_PUSHED_DOWN(rinfo, join_plus_commute))
485  {
486  /*
487  * There might be references to relid or ojrelid in the
488  * RestrictInfo's relid sets, as a consequence of PHVs having had
489  * ph_eval_at sets that include those. We already checked above
490  * that any such PHV is safe (and updated its ph_eval_at), so we
491  * can just drop those references.
492  */
493  remove_rel_from_restrictinfo(rinfo, relid, ojrelid);
494 
495  /*
496  * Cross-check that the clause itself does not reference the
497  * target rel or join.
498  */
499 #ifdef USE_ASSERT_CHECKING
500  {
501  Relids clause_varnos = pull_varnos(root,
502  (Node *) rinfo->clause);
503 
504  Assert(!bms_is_member(relid, clause_varnos));
505  Assert(!bms_is_member(ojrelid, clause_varnos));
506  }
507 #endif
508  /* Now throw it back into the joininfo lists */
509  distribute_restrictinfo_to_rels(root, rinfo);
510  }
511  }
512 
513  /*
514  * There may be references to the rel in root->fkey_list, but if so,
515  * match_foreign_keys_to_quals() will get rid of them.
516  */
517 
518  /*
519  * Finally, remove the rel from the baserel array to prevent it from being
520  * referenced again. (We can't do this earlier because
521  * remove_join_clause_from_rels will touch it.)
522  */
523  root->simple_rel_array[relid] = NULL;
524 
525  /* And nuke the RelOptInfo, just in case there's another access path */
526  pfree(rel);
527 }
static void remove_rel_from_restrictinfo(RestrictInfo *rinfo, int relid, int ojrelid)
Definition: analyzejoins.c:538
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:818
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:792
#define bms_is_empty(a)
Definition: bitmapset.h:105
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2621
void remove_join_clause_from_rels(PlannerInfo *root, RestrictInfo *restrictinfo, Relids join_relids)
Definition: joininfo.c:125
List * list_copy(const List *oldlist)
Definition: list.c:1572
void pfree(void *pointer)
Definition: mcxt.c:1456
Relids phnullingrels
Definition: pathnodes.h:2740
int simple_rel_array_size
Definition: pathnodes.h:229
Relids all_query_rels
Definition: pathnodes.h:266
Relids outer_join_rels
Definition: pathnodes.h:258
Relids all_baserels
Definition: pathnodes.h:252
Relids required_relids
Definition: pathnodes.h:2547
Relids commute_above_r
Definition: pathnodes.h:2847
Relids syn_lefthand
Definition: pathnodes.h:2842
Relids commute_above_l
Definition: pathnodes.h:2846
Relids commute_below_l
Definition: pathnodes.h:2848
Relids syn_righthand
Definition: pathnodes.h:2843
Relids commute_below_r
Definition: pathnodes.h:2849

References PlannerInfo::all_baserels, PlannerInfo::all_query_rels, Assert(), bms_add_member(), bms_add_members(), bms_del_member(), bms_is_empty, bms_is_member(), bms_is_subset(), bms_union(), RestrictInfo::clause, SpecialJoinInfo::commute_above_l, SpecialJoinInfo::commute_above_r, SpecialJoinInfo::commute_below_l, SpecialJoinInfo::commute_below_r, distribute_restrictinfo_to_rels(), find_base_rel(), foreach_delete_current, PlannerInfo::join_info_list, RelOptInfo::joininfo, lfirst, list_copy(), RelOptInfo::max_attr, RelOptInfo::min_attr, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, SpecialJoinInfo::ojrelid, PlannerInfo::outer_join_rels, pfree(), PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_lateral, PlaceHolderInfo::ph_needed, PlaceHolderInfo::ph_var, PlaceHolderInfo::phid, PlaceHolderVar::phnullingrels, PlannerInfo::placeholder_list, pull_varnos(), RelOptInfo::relid, remove_join_clause_from_rels(), remove_rel_from_restrictinfo(), RestrictInfo::required_relids, RINFO_IS_PUSHED_DOWN, PlannerInfo::simple_rel_array_size, SpecialJoinInfo::syn_lefthand, and SpecialJoinInfo::syn_righthand.

Referenced by remove_useless_joins().

◆ remove_rel_from_restrictinfo()

static void remove_rel_from_restrictinfo ( RestrictInfo rinfo,
int  relid,
int  ojrelid 
)
static

Definition at line 538 of file analyzejoins.c.

539 {
540  /*
541  * The clause_relids probably aren't shared with anything else, but let's
542  * copy them just to be sure.
543  */
544  rinfo->clause_relids = bms_copy(rinfo->clause_relids);
545  rinfo->clause_relids = bms_del_member(rinfo->clause_relids, relid);
546  rinfo->clause_relids = bms_del_member(rinfo->clause_relids, ojrelid);
547  /* Likewise for required_relids */
548  rinfo->required_relids = bms_copy(rinfo->required_relids);
549  rinfo->required_relids = bms_del_member(rinfo->required_relids, relid);
550  rinfo->required_relids = bms_del_member(rinfo->required_relids, ojrelid);
551 
552  /* If it's an OR, recurse to clean up sub-clauses */
553  if (restriction_is_or_clause(rinfo))
554  {
555  ListCell *lc;
556 
557  Assert(is_orclause(rinfo->orclause));
558  foreach(lc, ((BoolExpr *) rinfo->orclause)->args)
559  {
560  Node *orarg = (Node *) lfirst(lc);
561 
562  /* OR arguments should be ANDs or sub-RestrictInfos */
563  if (is_andclause(orarg))
564  {
565  List *andargs = ((BoolExpr *) orarg)->args;
566  ListCell *lc2;
567 
568  foreach(lc2, andargs)
569  {
570  RestrictInfo *rinfo2 = lfirst_node(RestrictInfo, lc2);
571 
572  remove_rel_from_restrictinfo(rinfo2, relid, ojrelid);
573  }
574  }
575  else
576  {
577  RestrictInfo *rinfo2 = castNode(RestrictInfo, orarg);
578 
579  remove_rel_from_restrictinfo(rinfo2, relid, ojrelid);
580  }
581  }
582  }
583 }
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:105
static bool is_orclause(const void *clause)
Definition: nodeFuncs.h:114
bool restriction_is_or_clause(RestrictInfo *restrictinfo)
Definition: restrictinfo.c:416

References Assert(), bms_copy(), bms_del_member(), castNode, is_andclause(), is_orclause(), lfirst, lfirst_node, RestrictInfo::required_relids, and restriction_is_or_clause().

Referenced by remove_rel_from_query().

◆ remove_useless_joins()

List* remove_useless_joins ( PlannerInfo root,
List joinlist 
)

Definition at line 64 of file analyzejoins.c.

65 {
66  ListCell *lc;
67 
68  /*
69  * We are only interested in relations that are left-joined to, so we can
70  * scan the join_info_list to find them easily.
71  */
72 restart:
73  foreach(lc, root->join_info_list)
74  {
75  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
76  int innerrelid;
77  int nremoved;
78 
79  /* Skip if not removable */
80  if (!join_is_removable(root, sjinfo))
81  continue;
82 
83  /*
84  * Currently, join_is_removable can only succeed when the sjinfo's
85  * righthand is a single baserel. Remove that rel from the query and
86  * joinlist.
87  */
88  innerrelid = bms_singleton_member(sjinfo->min_righthand);
89 
90  remove_rel_from_query(root, innerrelid, sjinfo);
91 
92  /* We verify that exactly one reference gets removed from joinlist */
93  nremoved = 0;
94  joinlist = remove_rel_from_joinlist(joinlist, innerrelid, &nremoved);
95  if (nremoved != 1)
96  elog(ERROR, "failed to find relation %d in joinlist", innerrelid);
97 
98  /*
99  * We can delete this SpecialJoinInfo from the list too, since it's no
100  * longer of interest. (Since we'll restart the foreach loop
101  * immediately, we don't bother with foreach_delete_current.)
102  */
104 
105  /*
106  * Restart the scan. This is necessary to ensure we find all
107  * removable joins independently of ordering of the join_info_list
108  * (note that removal of attr_needed bits may make a join appear
109  * removable that did not before).
110  */
111  goto restart;
112  }
113 
114  return joinlist;
115 }
static void remove_rel_from_query(PlannerInfo *root, int relid, SpecialJoinInfo *sjinfo)
Definition: analyzejoins.c:329
static bool join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
Definition: analyzejoins.c:160
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:596
List * list_delete_cell(List *list, ListCell *cell)
Definition: list.c:840

References bms_singleton_member(), elog(), ERROR, PlannerInfo::join_info_list, join_is_removable(), lfirst, list_delete_cell(), SpecialJoinInfo::min_righthand, remove_rel_from_joinlist(), and remove_rel_from_query().

Referenced by query_planner().