PostgreSQL Source Code  git master
analyzejoins.c File Reference
#include "postgres.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/joininfo.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/planmain.h"
#include "optimizer/tlist.h"
#include "optimizer/var.h"
#include "utils/lsyscache.h"
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, Relids joinrelids)
 
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.

References bms_is_subset(), RestrictInfo::left_relids, RestrictInfo::outer_is_left, and RestrictInfo::right_relids.

Referenced by is_innerrel_unique_for(), and join_is_removable().

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 }
Relids left_relids
Definition: relation.h:1907
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:374
bool outer_is_left
Definition: relation.h:1935
Relids right_relids
Definition: relation.h:1908

◆ distinct_col_search()

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

Definition at line 931 of file analyzejoins.c.

References forboth, InvalidOid, lfirst_int, and lfirst_oid.

Referenced by query_is_distinct_for().

932 {
933  ListCell *lc1,
934  *lc2;
935 
936  forboth(lc1, colnos, lc2, opids)
937  {
938  if (colno == lfirst_int(lc1))
939  return lfirst_oid(lc2);
940  }
941  return InvalidOid;
942 }
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
#define lfirst_int(lc)
Definition: pg_list.h:107
#define InvalidOid
Definition: postgres_ext.h:36
#define lfirst_oid(lc)
Definition: pg_list.h:108

◆ 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 971 of file analyzejoins.c.

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

Referenced by add_paths_to_joinrel(), and reduce_unique_semijoins().

978 {
979  MemoryContext old_context;
980  ListCell *lc;
981 
982  /* Certainly can't prove uniqueness when there are no joinclauses */
983  if (restrictlist == NIL)
984  return false;
985 
986  /*
987  * Make a quick check to eliminate cases in which we will surely be unable
988  * to prove uniqueness of the innerrel.
989  */
990  if (!rel_supports_distinctness(root, innerrel))
991  return false;
992 
993  /*
994  * Query the cache to see if we've managed to prove that innerrel is
995  * unique for any subset of this outerrel. We don't need an exact match,
996  * as extra outerrels can't make the innerrel any less unique (or more
997  * formally, the restrictlist for a join to a superset outerrel must be a
998  * superset of the conditions we successfully used before).
999  */
1000  foreach(lc, innerrel->unique_for_rels)
1001  {
1002  Relids unique_for_rels = (Relids) lfirst(lc);
1003 
1004  if (bms_is_subset(unique_for_rels, outerrelids))
1005  return true; /* Success! */
1006  }
1007 
1008  /*
1009  * Conversely, we may have already determined that this outerrel, or some
1010  * superset thereof, cannot prove this innerrel to be unique.
1011  */
1012  foreach(lc, innerrel->non_unique_for_rels)
1013  {
1014  Relids unique_for_rels = (Relids) lfirst(lc);
1015 
1016  if (bms_is_subset(outerrelids, unique_for_rels))
1017  return false;
1018  }
1019 
1020  /* No cached information, so try to make the proof. */
1021  if (is_innerrel_unique_for(root, joinrelids, outerrelids, innerrel,
1022  jointype, restrictlist))
1023  {
1024  /*
1025  * Cache the positive result for future probes, being sure to keep it
1026  * in the planner_cxt even if we are working in GEQO.
1027  *
1028  * Note: one might consider trying to isolate the minimal subset of
1029  * the outerrels that proved the innerrel unique. But it's not worth
1030  * the trouble, because the planner builds up joinrels incrementally
1031  * and so we'll see the minimally sufficient outerrels before any
1032  * supersets of them anyway.
1033  */
1034  old_context = MemoryContextSwitchTo(root->planner_cxt);
1035  innerrel->unique_for_rels = lappend(innerrel->unique_for_rels,
1036  bms_copy(outerrelids));
1037  MemoryContextSwitchTo(old_context);
1038 
1039  return true; /* Success! */
1040  }
1041  else
1042  {
1043  /*
1044  * None of the join conditions for outerrel proved innerrel unique, so
1045  * we can safely reject this outerrel or any subset of it in future
1046  * checks.
1047  *
1048  * However, in normal planning mode, caching this knowledge is totally
1049  * pointless; it won't be queried again, because we build up joinrels
1050  * from smaller to larger. It is useful in GEQO mode, where the
1051  * knowledge can be carried across successive planning attempts; and
1052  * it's likely to be useful when using join-search plugins, too. Hence
1053  * cache when join_search_private is non-NULL. (Yeah, that's a hack,
1054  * but it seems reasonable.)
1055  *
1056  * Also, allow callers to override that heuristic and force caching;
1057  * that's useful for reduce_unique_semijoins, which calls here before
1058  * the normal join search starts.
1059  */
1060  if (force_cache || root->join_search_private)
1061  {
1062  old_context = MemoryContextSwitchTo(root->planner_cxt);
1063  innerrel->non_unique_for_rels =
1064  lappend(innerrel->non_unique_for_rels,
1065  bms_copy(outerrelids));
1066  MemoryContextSwitchTo(old_context);
1067  }
1068 
1069  return false;
1070  }
1071 }
static bool is_innerrel_unique_for(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist)
#define NIL
Definition: pg_list.h:69
List * unique_for_rels
Definition: relation.h:667
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:133
void * join_search_private
Definition: relation.h:332
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:374
List * non_unique_for_rels
Definition: relation.h:669
Bitmapset * Relids
Definition: relation.h:29
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
MemoryContext planner_cxt
Definition: relation.h:302
static bool rel_supports_distinctness(PlannerInfo *root, RelOptInfo *rel)
Definition: analyzejoins.c:592

◆ 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 1079 of file analyzejoins.c.

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

Referenced by innerrel_is_unique().

1085 {
1086  List *clause_list = NIL;
1087  ListCell *lc;
1088 
1089  /*
1090  * Search for mergejoinable clauses that constrain the inner rel against
1091  * the outer rel. If an operator is mergejoinable then it behaves like
1092  * equality for some btree opclass, so it's what we want. The
1093  * mergejoinability test also eliminates clauses containing volatile
1094  * functions, which we couldn't depend on.
1095  */
1096  foreach(lc, restrictlist)
1097  {
1098  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1099 
1100  /*
1101  * As noted above, if it's a pushed-down clause and we're at an outer
1102  * join, we can't use it.
1103  */
1104  if (IS_OUTER_JOIN(jointype) &&
1105  RINFO_IS_PUSHED_DOWN(restrictinfo, joinrelids))
1106  continue;
1107 
1108  /* Ignore if it's not a mergejoinable clause */
1109  if (!restrictinfo->can_join ||
1110  restrictinfo->mergeopfamilies == NIL)
1111  continue; /* not mergejoinable */
1112 
1113  /*
1114  * Check if clause has the form "outer op inner" or "inner op outer",
1115  * and if so mark which side is inner.
1116  */
1117  if (!clause_sides_match_join(restrictinfo, outerrelids,
1118  innerrel->relids))
1119  continue; /* no good for these input relations */
1120 
1121  /* OK, add to list */
1122  clause_list = lappend(clause_list, restrictinfo);
1123  }
1124 
1125  /* Let rel_is_distinct_for() do the hard work */
1126  return rel_is_distinct_for(root, innerrel, clause_list);
1127 }
#define NIL
Definition: pg_list.h:69
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:730
List * mergeopfamilies
Definition: relation.h:1925
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: relation.h:1957
static bool rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel, List *clause_list)
Definition: analyzejoins.c:648
bool can_join
Definition: relation.h:1886
static bool clause_sides_match_join(RestrictInfo *rinfo, Relids outerrelids, Relids innerrelids)
Definition: analyzejoins.c:128
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

◆ join_is_removable()

static bool join_is_removable ( PlannerInfo root,
SpecialJoinInfo sjinfo 
)
static

Definition at line 160 of file analyzejoins.c.

References RelOptInfo::attr_needed, bms_get_singleton_member(), bms_is_member(), bms_is_subset(), bms_overlap(), bms_union(), RestrictInfo::can_join, RestrictInfo::clause_relids, clause_sides_match_join(), SpecialJoinInfo::delay_upper_joins, find_base_rel(), JOIN_LEFT, RelOptInfo::joininfo, SpecialJoinInfo::jointype, lappend(), lfirst, RelOptInfo::max_attr, RestrictInfo::mergeopfamilies, RelOptInfo::min_attr, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, NIL, PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_lateral, PlaceHolderInfo::ph_needed, PlaceHolderInfo::ph_var, PlaceHolderVar::phexpr, PlannerInfo::placeholder_list, pull_varnos(), rel_is_distinct_for(), rel_supports_distinctness(), RelOptInfo::relids, and RINFO_IS_PUSHED_DOWN.

Referenced by remove_useless_joins().

161 {
162  int innerrelid;
163  RelOptInfo *innerrel;
164  Relids joinrelids;
165  List *clause_list = NIL;
166  ListCell *l;
167  int attroff;
168 
169  /*
170  * Must be a non-delaying left join to a single baserel, else we aren't
171  * going to be able to do anything with it.
172  */
173  if (sjinfo->jointype != JOIN_LEFT ||
174  sjinfo->delay_upper_joins)
175  return false;
176 
177  if (!bms_get_singleton_member(sjinfo->min_righthand, &innerrelid))
178  return false;
179 
180  innerrel = find_base_rel(root, innerrelid);
181 
182  /*
183  * Before we go to the effort of checking whether any innerrel variables
184  * are needed above the join, make a quick check to eliminate cases in
185  * which we will surely be unable to prove uniqueness of the innerrel.
186  */
187  if (!rel_supports_distinctness(root, innerrel))
188  return false;
189 
190  /* Compute the relid set for the join we are considering */
191  joinrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
192 
193  /*
194  * We can't remove the join if any inner-rel attributes are used above the
195  * join.
196  *
197  * Note that this test only detects use of inner-rel attributes in higher
198  * join conditions and the target list. There might be such attributes in
199  * pushed-down conditions at this join, too. We check that case below.
200  *
201  * As a micro-optimization, it seems better to start with max_attr and
202  * count down rather than starting with min_attr and counting up, on the
203  * theory that the system attributes are somewhat less likely to be wanted
204  * and should be tested last.
205  */
206  for (attroff = innerrel->max_attr - innerrel->min_attr;
207  attroff >= 0;
208  attroff--)
209  {
210  if (!bms_is_subset(innerrel->attr_needed[attroff], joinrelids))
211  return false;
212  }
213 
214  /*
215  * Similarly check that the inner rel isn't needed by any PlaceHolderVars
216  * that will be used above the join. We only need to fail if such a PHV
217  * actually references some inner-rel attributes; but the correct check
218  * for that is relatively expensive, so we first check against ph_eval_at,
219  * which must mention the inner rel if the PHV uses any inner-rel attrs as
220  * non-lateral references. Note that if the PHV's syntactic scope is just
221  * the inner rel, we can't drop the rel even if the PHV is variable-free.
222  */
223  foreach(l, root->placeholder_list)
224  {
225  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
226 
227  if (bms_overlap(phinfo->ph_lateral, innerrel->relids))
228  return false; /* it references innerrel laterally */
229  if (bms_is_subset(phinfo->ph_needed, joinrelids))
230  continue; /* PHV is not used above the join */
231  if (!bms_overlap(phinfo->ph_eval_at, innerrel->relids))
232  continue; /* it definitely doesn't reference innerrel */
233  if (bms_is_subset(phinfo->ph_eval_at, innerrel->relids))
234  return false; /* there isn't any other place to eval PHV */
235  if (bms_overlap(pull_varnos((Node *) phinfo->ph_var->phexpr),
236  innerrel->relids))
237  return false; /* it does reference innerrel */
238  }
239 
240  /*
241  * Search for mergejoinable clauses that constrain the inner rel against
242  * either the outer rel or a pseudoconstant. If an operator is
243  * mergejoinable then it behaves like equality for some btree opclass, so
244  * it's what we want. The mergejoinability test also eliminates clauses
245  * containing volatile functions, which we couldn't depend on.
246  */
247  foreach(l, innerrel->joininfo)
248  {
249  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
250 
251  /*
252  * If it's not a join clause for this outer join, we can't use it.
253  * Note that if the clause is pushed-down, then it is logically from
254  * above the outer join, even if it references no other rels (it might
255  * be from WHERE, for example).
256  */
257  if (RINFO_IS_PUSHED_DOWN(restrictinfo, joinrelids))
258  {
259  /*
260  * If such a clause actually references the inner rel then join
261  * removal has to be disallowed. We have to check this despite
262  * the previous attr_needed checks because of the possibility of
263  * pushed-down clauses referencing the rel.
264  */
265  if (bms_is_member(innerrelid, restrictinfo->clause_relids))
266  return false;
267  continue; /* else, ignore; not useful here */
268  }
269 
270  /* Ignore if it's not a mergejoinable clause */
271  if (!restrictinfo->can_join ||
272  restrictinfo->mergeopfamilies == NIL)
273  continue; /* not mergejoinable */
274 
275  /*
276  * Check if clause has the form "outer op inner" or "inner op outer",
277  * and if so mark which side is inner.
278  */
279  if (!clause_sides_match_join(restrictinfo, sjinfo->min_lefthand,
280  innerrel->relids))
281  continue; /* no good for these input relations */
282 
283  /* OK, add to list */
284  clause_list = lappend(clause_list, restrictinfo);
285  }
286 
287  /*
288  * Now that we have the relevant equality join clauses, try to prove the
289  * innerrel distinct.
290  */
291  if (rel_is_distinct_for(root, innerrel, clause_list))
292  return true;
293 
294  /*
295  * Some day it would be nice to check for other methods of establishing
296  * distinctness.
297  */
298  return false;
299 }
#define NIL
Definition: pg_list.h:69
Relids ph_needed
Definition: relation.h:2196
Relids ph_eval_at
Definition: relation.h:2194
PlaceHolderVar * ph_var
Definition: relation.h:2193
Relids * attr_needed
Definition: relation.h:645
Relids min_righthand
Definition: relation.h:2067
Relids clause_relids
Definition: relation.h:1895
Definition: nodes.h:517
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:635
List * mergeopfamilies
Definition: relation.h:1925
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: relation.h:1957
Expr * phexpr
Definition: relation.h:1999
static bool rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel, List *clause_list)
Definition: analyzejoins.c:648
bool can_join
Definition: relation.h:1886
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:374
List * joininfo
Definition: relation.h:676
static bool clause_sides_match_join(RestrictInfo *rinfo, Relids outerrelids, Relids innerrelids)
Definition: analyzejoins.c:128
Relids relids
Definition: relation.h:612
Relids pull_varnos(Node *node)
Definition: var.c:95
List * lappend(List *list, void *datum)
Definition: list.c:128
bool delay_upper_joins
Definition: relation.h:2072
Relids ph_lateral
Definition: relation.h:2195
#define lfirst(lc)
Definition: pg_list.h:106
JoinType jointype
Definition: relation.h:2070
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:284
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:509
AttrNumber max_attr
Definition: relation.h:644
List * placeholder_list
Definition: relation.h:270
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:279
static bool rel_supports_distinctness(PlannerInfo *root, RelOptInfo *rel)
Definition: analyzejoins.c:592
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:486
Relids min_lefthand
Definition: relation.h:2066
AttrNumber min_attr
Definition: relation.h:643

◆ query_is_distinct_for()

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

Definition at line 782 of file analyzejoins.c.

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

Referenced by create_unique_path(), and rel_is_distinct_for().

783 {
784  ListCell *l;
785  Oid opid;
786 
787  Assert(list_length(colnos) == list_length(opids));
788 
789  /*
790  * DISTINCT (including DISTINCT ON) guarantees uniqueness if all the
791  * columns in the DISTINCT clause appear in colnos and operator semantics
792  * match. This is true even if there are SRFs in the DISTINCT columns or
793  * elsewhere in the tlist.
794  */
795  if (query->distinctClause)
796  {
797  foreach(l, query->distinctClause)
798  {
799  SortGroupClause *sgc = (SortGroupClause *) lfirst(l);
801  query->targetList);
802 
803  opid = distinct_col_search(tle->resno, colnos, opids);
804  if (!OidIsValid(opid) ||
805  !equality_ops_are_compatible(opid, sgc->eqop))
806  break; /* exit early if no match */
807  }
808  if (l == NULL) /* had matches for all? */
809  return true;
810  }
811 
812  /*
813  * Otherwise, a set-returning function in the query's targetlist can
814  * result in returning duplicate rows, despite any grouping that might
815  * occur before tlist evaluation. (If all tlist SRFs are within GROUP BY
816  * columns, it would be safe because they'd be expanded before grouping.
817  * But it doesn't currently seem worth the effort to check for that.)
818  */
819  if (query->hasTargetSRFs)
820  return false;
821 
822  /*
823  * Similarly, GROUP BY without GROUPING SETS guarantees uniqueness if all
824  * the grouped columns appear in colnos and operator semantics match.
825  */
826  if (query->groupClause && !query->groupingSets)
827  {
828  foreach(l, query->groupClause)
829  {
830  SortGroupClause *sgc = (SortGroupClause *) lfirst(l);
832  query->targetList);
833 
834  opid = distinct_col_search(tle->resno, colnos, opids);
835  if (!OidIsValid(opid) ||
836  !equality_ops_are_compatible(opid, sgc->eqop))
837  break; /* exit early if no match */
838  }
839  if (l == NULL) /* had matches for all? */
840  return true;
841  }
842  else if (query->groupingSets)
843  {
844  /*
845  * If we have grouping sets with expressions, we probably don't have
846  * uniqueness and analysis would be hard. Punt.
847  */
848  if (query->groupClause)
849  return false;
850 
851  /*
852  * If we have no groupClause (therefore no grouping expressions), we
853  * might have one or many empty grouping sets. If there's just one,
854  * then we're returning only one row and are certainly unique. But
855  * otherwise, we know we're certainly not unique.
856  */
857  if (list_length(query->groupingSets) == 1 &&
858  ((GroupingSet *) linitial(query->groupingSets))->kind == GROUPING_SET_EMPTY)
859  return true;
860  else
861  return false;
862  }
863  else
864  {
865  /*
866  * If we have no GROUP BY, but do have aggregates or HAVING, then the
867  * result is at most one row so it's surely unique, for any operators.
868  */
869  if (query->hasAggs || query->havingQual)
870  return true;
871  }
872 
873  /*
874  * UNION, INTERSECT, EXCEPT guarantee uniqueness of the whole output row,
875  * except with ALL.
876  */
877  if (query->setOperations)
878  {
880 
881  Assert(topop->op != SETOP_NONE);
882 
883  if (!topop->all)
884  {
885  ListCell *lg;
886 
887  /* We're good if all the nonjunk output columns are in colnos */
888  lg = list_head(topop->groupClauses);
889  foreach(l, query->targetList)
890  {
891  TargetEntry *tle = (TargetEntry *) lfirst(l);
892  SortGroupClause *sgc;
893 
894  if (tle->resjunk)
895  continue; /* ignore resjunk columns */
896 
897  /* non-resjunk columns should have grouping clauses */
898  Assert(lg != NULL);
899  sgc = (SortGroupClause *) lfirst(lg);
900  lg = lnext(lg);
901 
902  opid = distinct_col_search(tle->resno, colnos, opids);
903  if (!OidIsValid(opid) ||
904  !equality_ops_are_compatible(opid, sgc->eqop))
905  break; /* exit early if no match */
906  }
907  if (l == NULL) /* had matches for all? */
908  return true;
909  }
910  }
911 
912  /*
913  * XXX Are there any other cases in which we can easily see the result
914  * must be distinct?
915  *
916  * If you do add more smarts to this function, be sure to update
917  * query_supports_distinctness() to match.
918  */
919 
920  return false;
921 }
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:370
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
static Oid distinct_col_search(int colno, List *colnos, List *opids)
Definition: analyzejoins.c:931
bool hasAggs
Definition: parsenodes.h:125
List * groupingSets
Definition: parsenodes.h:150
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
List * targetList
Definition: parsenodes.h:140
bool resjunk
Definition: primnodes.h:1383
#define linitial(l)
Definition: pg_list.h:111
List * distinctClause
Definition: parsenodes.h:156
AttrNumber resno
Definition: primnodes.h:1377
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
#define lnext(lc)
Definition: pg_list.h:105
bool hasTargetSRFs
Definition: parsenodes.h:127
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
bool equality_ops_are_compatible(Oid opno1, Oid opno2)
Definition: lsyscache.c:695
static int list_length(const List *l)
Definition: pg_list.h:89
SetOperation op
Definition: parsenodes.h:1599
Node * setOperations
Definition: parsenodes.h:165
List * groupClause
Definition: parsenodes.h:148
Node * havingQual
Definition: parsenodes.h:152

◆ query_supports_distinctness()

bool query_supports_distinctness ( Query query)

Definition at line 745 of file analyzejoins.c.

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

Referenced by create_unique_path(), and rel_supports_distinctness().

746 {
747  /* SRFs break distinctness except with DISTINCT, see below */
748  if (query->hasTargetSRFs && query->distinctClause == NIL)
749  return false;
750 
751  /* check for features we can prove distinctness with */
752  if (query->distinctClause != NIL ||
753  query->groupClause != NIL ||
754  query->groupingSets != NIL ||
755  query->hasAggs ||
756  query->havingQual ||
757  query->setOperations)
758  return true;
759 
760  return false;
761 }
#define NIL
Definition: pg_list.h:69
bool hasAggs
Definition: parsenodes.h:125
List * groupingSets
Definition: parsenodes.h:150
List * distinctClause
Definition: parsenodes.h:156
bool hasTargetSRFs
Definition: parsenodes.h:127
Node * setOperations
Definition: parsenodes.h:165
List * groupClause
Definition: parsenodes.h:148
Node * havingQual
Definition: parsenodes.h:152

◆ reduce_unique_semijoins()

void reduce_unique_semijoins ( PlannerInfo root)

Definition at line 511 of file analyzejoins.c.

References bms_get_singleton_member(), bms_union(), SpecialJoinInfo::delay_upper_joins, find_base_rel(), generate_join_implied_equalities(), innerrel_is_unique(), PlannerInfo::join_info_list, JOIN_SEMI, RelOptInfo::joininfo, SpecialJoinInfo::jointype, lfirst, list_concat(), list_delete_ptr(), list_head(), lnext, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, next, and rel_supports_distinctness().

Referenced by query_planner().

512 {
513  ListCell *lc;
514  ListCell *next;
515 
516  /*
517  * Scan the join_info_list to find semijoins. We can't use foreach
518  * because we may delete the current cell.
519  */
520  for (lc = list_head(root->join_info_list); lc != NULL; lc = next)
521  {
522  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
523  int innerrelid;
524  RelOptInfo *innerrel;
525  Relids joinrelids;
526  List *restrictlist;
527 
528  next = lnext(lc);
529 
530  /*
531  * Must be a non-delaying semijoin to a single baserel, else we aren't
532  * going to be able to do anything with it. (It's probably not
533  * possible for delay_upper_joins to be set on a semijoin, but we
534  * might as well check.)
535  */
536  if (sjinfo->jointype != JOIN_SEMI ||
537  sjinfo->delay_upper_joins)
538  continue;
539 
540  if (!bms_get_singleton_member(sjinfo->min_righthand, &innerrelid))
541  continue;
542 
543  innerrel = find_base_rel(root, innerrelid);
544 
545  /*
546  * Before we trouble to run generate_join_implied_equalities, make a
547  * quick check to eliminate cases in which we will surely be unable to
548  * prove uniqueness of the innerrel.
549  */
550  if (!rel_supports_distinctness(root, innerrel))
551  continue;
552 
553  /* Compute the relid set for the join we are considering */
554  joinrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
555 
556  /*
557  * Since we're only considering a single-rel RHS, any join clauses it
558  * has must be clauses linking it to the semijoin's min_lefthand. We
559  * can also consider EC-derived join clauses.
560  */
561  restrictlist =
563  joinrelids,
564  sjinfo->min_lefthand,
565  innerrel),
566  innerrel->joininfo);
567 
568  /* Test whether the innerrel is unique for those clauses. */
569  if (!innerrel_is_unique(root,
570  joinrelids, sjinfo->min_lefthand, innerrel,
571  JOIN_SEMI, restrictlist, true))
572  continue;
573 
574  /* OK, remove the SpecialJoinInfo from the list. */
575  root->join_info_list = list_delete_ptr(root->join_info_list, sjinfo);
576  }
577 }
static int32 next
Definition: blutils.c:211
List * join_info_list
Definition: relation.h:264
Relids min_righthand
Definition: relation.h:2067
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:635
List * list_concat(List *list1, List *list2)
Definition: list.c:321
bool innerrel_is_unique(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache)
Definition: analyzejoins.c:971
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:590
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1071
List * joininfo
Definition: relation.h:676
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
#define lnext(lc)
Definition: pg_list.h:105
bool delay_upper_joins
Definition: relation.h:2072
#define lfirst(lc)
Definition: pg_list.h:106
JoinType jointype
Definition: relation.h:2070
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:284
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:279
static bool rel_supports_distinctness(PlannerInfo *root, RelOptInfo *rel)
Definition: analyzejoins.c:592
Definition: pg_list.h:45
Relids min_lefthand
Definition: relation.h:2066

◆ rel_is_distinct_for()

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

Definition at line 648 of file analyzejoins.c.

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

Referenced by is_innerrel_unique_for(), and join_is_removable().

649 {
650  /*
651  * We could skip a couple of tests here if we assume all callers checked
652  * rel_supports_distinctness first, but it doesn't seem worth taking any
653  * risk for.
654  */
655  if (rel->reloptkind != RELOPT_BASEREL)
656  return false;
657  if (rel->rtekind == RTE_RELATION)
658  {
659  /*
660  * Examine the indexes to see if we have a matching unique index.
661  * relation_has_unique_index_for automatically adds any usable
662  * restriction clauses for the rel, so we needn't do that here.
663  */
664  if (relation_has_unique_index_for(root, rel, clause_list, NIL, NIL))
665  return true;
666  }
667  else if (rel->rtekind == RTE_SUBQUERY)
668  {
669  Index relid = rel->relid;
670  Query *subquery = root->simple_rte_array[relid]->subquery;
671  List *colnos = NIL;
672  List *opids = NIL;
673  ListCell *l;
674 
675  /*
676  * Build the argument lists for query_is_distinct_for: a list of
677  * output column numbers that the query needs to be distinct over, and
678  * a list of equality operators that the output columns need to be
679  * distinct according to.
680  *
681  * (XXX we are not considering restriction clauses attached to the
682  * subquery; is that worth doing?)
683  */
684  foreach(l, clause_list)
685  {
687  Oid op;
688  Var *var;
689 
690  /*
691  * Get the equality operator we need uniqueness according to.
692  * (This might be a cross-type operator and thus not exactly the
693  * same operator the subquery would consider; that's all right
694  * since query_is_distinct_for can resolve such cases.) The
695  * caller's mergejoinability test should have selected only
696  * OpExprs.
697  */
698  op = castNode(OpExpr, rinfo->clause)->opno;
699 
700  /* caller identified the inner side for us */
701  if (rinfo->outer_is_left)
702  var = (Var *) get_rightop(rinfo->clause);
703  else
704  var = (Var *) get_leftop(rinfo->clause);
705 
706  /*
707  * We may ignore any RelabelType node above the operand. (There
708  * won't be more than one, since eval_const_expressions() has been
709  * applied already.)
710  */
711  if (var && IsA(var, RelabelType))
712  var = (Var *) ((RelabelType *) var)->arg;
713 
714  /*
715  * If inner side isn't a Var referencing a subquery output column,
716  * this clause doesn't help us.
717  */
718  if (!var || !IsA(var, Var) ||
719  var->varno != relid || var->varlevelsup != 0)
720  continue;
721 
722  colnos = lappend_int(colnos, var->varattno);
723  opids = lappend_oid(opids, op);
724  }
725 
726  if (query_is_distinct_for(subquery, colnos, opids))
727  return true;
728  }
729  return false;
730 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
bool query_is_distinct_for(Query *query, List *colnos, List *opids)
Definition: analyzejoins.c:782
Index varlevelsup
Definition: primnodes.h:174
RelOptKind reloptkind
Definition: relation.h:609
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
AttrNumber varattno
Definition: primnodes.h:169
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:164
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
Node * get_leftop(const Expr *clause)
Definition: clauses.c:200
#define lfirst_node(type, lc)
Definition: pg_list.h:109
bool outer_is_left
Definition: relation.h:1935
List * lappend_int(List *list, int datum)
Definition: list.c:146
Index relid
Definition: relation.h:640
RangeTblEntry ** simple_rte_array
Definition: relation.h:202
Expr * clause
Definition: relation.h:1880
Index varno
Definition: primnodes.h:167
unsigned int Index
Definition: c.h:442
RTEKind rtekind
Definition: relation.h:642
Node * get_rightop(const Expr *clause)
Definition: clauses.c:217
Query * subquery
Definition: parsenodes.h:985
bool relation_has_unique_index_for(PlannerInfo *root, RelOptInfo *rel, List *restrictlist, List *exprlist, List *oprlist)
Definition: indxpath.c:2985
Definition: pg_list.h:45

◆ rel_supports_distinctness()

static bool rel_supports_distinctness ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 592 of file analyzejoins.c.

References IndexOptInfo::immediate, RelOptInfo::indexlist, IndexOptInfo::indpred, lfirst, NIL, IndexOptInfo::predOK, query_supports_distinctness(), RelOptInfo::relid, RELOPT_BASEREL, RelOptInfo::reloptkind, RTE_RELATION, RTE_SUBQUERY, RelOptInfo::rtekind, PlannerInfo::simple_rte_array, RangeTblEntry::subquery, and IndexOptInfo::unique.

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

593 {
594  /* We only know about baserels ... */
595  if (rel->reloptkind != RELOPT_BASEREL)
596  return false;
597  if (rel->rtekind == RTE_RELATION)
598  {
599  /*
600  * For a plain relation, we only know how to prove uniqueness by
601  * reference to unique indexes. Make sure there's at least one
602  * suitable unique index. It must be immediately enforced, and if
603  * it's a partial index, it must match the query. (Keep these
604  * conditions in sync with relation_has_unique_index_for!)
605  */
606  ListCell *lc;
607 
608  foreach(lc, rel->indexlist)
609  {
610  IndexOptInfo *ind = (IndexOptInfo *) lfirst(lc);
611 
612  if (ind->unique && ind->immediate &&
613  (ind->indpred == NIL || ind->predOK))
614  return true;
615  }
616  }
617  else if (rel->rtekind == RTE_SUBQUERY)
618  {
619  Query *subquery = root->simple_rte_array[rel->relid]->subquery;
620 
621  /* Check if the subquery has any qualities that support distinctness */
622  if (query_supports_distinctness(subquery))
623  return true;
624  }
625  /* We have no proof rules for any other rtekinds. */
626  return false;
627 }
#define NIL
Definition: pg_list.h:69
bool predOK
Definition: relation.h:788
RelOptKind reloptkind
Definition: relation.h:609
bool immediate
Definition: relation.h:790
bool unique
Definition: relation.h:789
Index relid
Definition: relation.h:640
RangeTblEntry ** simple_rte_array
Definition: relation.h:202
RTEKind rtekind
Definition: relation.h:642
List * indexlist
Definition: relation.h:649
#define lfirst(lc)
Definition: pg_list.h:106
bool query_supports_distinctness(Query *query)
Definition: analyzejoins.c:745
Query * subquery
Definition: parsenodes.h:985
List * indpred
Definition: relation.h:778

◆ remove_rel_from_joinlist()

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

Definition at line 457 of file analyzejoins.c.

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

Referenced by remove_useless_joins().

458 {
459  List *result = NIL;
460  ListCell *jl;
461 
462  foreach(jl, joinlist)
463  {
464  Node *jlnode = (Node *) lfirst(jl);
465 
466  if (IsA(jlnode, RangeTblRef))
467  {
468  int varno = ((RangeTblRef *) jlnode)->rtindex;
469 
470  if (varno == relid)
471  (*nremoved)++;
472  else
473  result = lappend(result, jlnode);
474  }
475  else if (IsA(jlnode, List))
476  {
477  /* Recurse to handle subproblem */
478  List *sublist;
479 
480  sublist = remove_rel_from_joinlist((List *) jlnode,
481  relid, nremoved);
482  /* Avoid including empty sub-lists in the result */
483  if (sublist)
484  result = lappend(result, sublist);
485  }
486  else
487  {
488  elog(ERROR, "unrecognized joinlist node type: %d",
489  (int) nodeTag(jlnode));
490  }
491  }
492 
493  return result;
494 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Definition: nodes.h:517
#define ERROR
Definition: elog.h:43
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
#define nodeTag(nodeptr)
Definition: nodes.h:522
static List * remove_rel_from_joinlist(List *joinlist, int relid, int *nremoved)
Definition: analyzejoins.c:457
#define elog
Definition: elog.h:219
Definition: pg_list.h:45

◆ remove_rel_from_query()

static void remove_rel_from_query ( PlannerInfo root,
int  relid,
Relids  joinrelids 
)
static

Definition at line 313 of file analyzejoins.c.

References Assert, RelOptInfo::attr_needed, bms_copy(), bms_del_member(), bms_is_empty(), bms_is_member(), bms_is_subset(), RestrictInfo::clause_relids, distribute_restrictinfo_to_rels(), find_base_rel(), PlannerInfo::join_info_list, RelOptInfo::joininfo, lfirst, list_copy(), list_delete_ptr(), list_head(), lnext, RelOptInfo::max_attr, RelOptInfo::min_attr, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_lateral, PlaceHolderInfo::ph_needed, PlannerInfo::placeholder_list, RelOptInfo::relid, RELOPT_DEADREL, RelOptInfo::reloptkind, remove_join_clause_from_rels(), RestrictInfo::required_relids, RINFO_IS_PUSHED_DOWN, PlannerInfo::simple_rel_array, PlannerInfo::simple_rel_array_size, SpecialJoinInfo::syn_lefthand, and SpecialJoinInfo::syn_righthand.

Referenced by remove_useless_joins().

314 {
315  RelOptInfo *rel = find_base_rel(root, relid);
316  List *joininfos;
317  Index rti;
318  ListCell *l;
319  ListCell *nextl;
320 
321  /*
322  * Mark the rel as "dead" to show it is no longer part of the join tree.
323  * (Removing it from the baserel array altogether seems too risky.)
324  */
325  rel->reloptkind = RELOPT_DEADREL;
326 
327  /*
328  * Remove references to the rel from other baserels' attr_needed arrays.
329  */
330  for (rti = 1; rti < root->simple_rel_array_size; rti++)
331  {
332  RelOptInfo *otherrel = root->simple_rel_array[rti];
333  int attroff;
334 
335  /* there may be empty slots corresponding to non-baserel RTEs */
336  if (otherrel == NULL)
337  continue;
338 
339  Assert(otherrel->relid == rti); /* sanity check on array */
340 
341  /* no point in processing target rel itself */
342  if (otherrel == rel)
343  continue;
344 
345  for (attroff = otherrel->max_attr - otherrel->min_attr;
346  attroff >= 0;
347  attroff--)
348  {
349  otherrel->attr_needed[attroff] =
350  bms_del_member(otherrel->attr_needed[attroff], relid);
351  }
352  }
353 
354  /*
355  * Likewise remove references from SpecialJoinInfo data structures.
356  *
357  * This is relevant in case the outer join we're deleting is nested inside
358  * other outer joins: the upper joins' relid sets have to be adjusted. The
359  * RHS of the target outer join will be made empty here, but that's OK
360  * since caller will delete that SpecialJoinInfo entirely.
361  */
362  foreach(l, root->join_info_list)
363  {
364  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
365 
366  sjinfo->min_lefthand = bms_del_member(sjinfo->min_lefthand, relid);
367  sjinfo->min_righthand = bms_del_member(sjinfo->min_righthand, relid);
368  sjinfo->syn_lefthand = bms_del_member(sjinfo->syn_lefthand, relid);
369  sjinfo->syn_righthand = bms_del_member(sjinfo->syn_righthand, relid);
370  }
371 
372  /*
373  * Likewise remove references from PlaceHolderVar data structures,
374  * removing any no-longer-needed placeholders entirely.
375  *
376  * Removal is a bit tricker than it might seem: we can remove PHVs that
377  * are used at the target rel and/or in the join qual, but not those that
378  * are used at join partner rels or above the join. It's not that easy to
379  * distinguish PHVs used at partner rels from those used in the join qual,
380  * since they will both have ph_needed sets that are subsets of
381  * joinrelids. However, a PHV used at a partner rel could not have the
382  * target rel in ph_eval_at, so we check that while deciding whether to
383  * remove or just update the PHV. There is no corresponding test in
384  * join_is_removable because it doesn't need to distinguish those cases.
385  */
386  for (l = list_head(root->placeholder_list); l != NULL; l = nextl)
387  {
388  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
389 
390  nextl = lnext(l);
391  Assert(!bms_is_member(relid, phinfo->ph_lateral));
392  if (bms_is_subset(phinfo->ph_needed, joinrelids) &&
393  bms_is_member(relid, phinfo->ph_eval_at))
395  phinfo);
396  else
397  {
398  phinfo->ph_eval_at = bms_del_member(phinfo->ph_eval_at, relid);
399  Assert(!bms_is_empty(phinfo->ph_eval_at));
400  phinfo->ph_needed = bms_del_member(phinfo->ph_needed, relid);
401  }
402  }
403 
404  /*
405  * Remove any joinquals referencing the rel from the joininfo lists.
406  *
407  * In some cases, a joinqual has to be put back after deleting its
408  * reference to the target rel. This can occur for pseudoconstant and
409  * outerjoin-delayed quals, which can get marked as requiring the rel in
410  * order to force them to be evaluated at or above the join. We can't
411  * just discard them, though. Only quals that logically belonged to the
412  * outer join being discarded should be removed from the query.
413  *
414  * We must make a copy of the rel's old joininfo list before starting the
415  * loop, because otherwise remove_join_clause_from_rels would destroy the
416  * list while we're scanning it.
417  */
418  joininfos = list_copy(rel->joininfo);
419  foreach(l, joininfos)
420  {
421  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
422 
423  remove_join_clause_from_rels(root, rinfo, rinfo->required_relids);
424 
425  if (RINFO_IS_PUSHED_DOWN(rinfo, joinrelids))
426  {
427  /* Recheck that qual doesn't actually reference the target rel */
428  Assert(!bms_is_member(relid, rinfo->clause_relids));
429 
430  /*
431  * The required_relids probably aren't shared with anything else,
432  * but let's copy them just to be sure.
433  */
434  rinfo->required_relids = bms_copy(rinfo->required_relids);
436  relid);
437  distribute_restrictinfo_to_rels(root, rinfo);
438  }
439  }
440 
441  /*
442  * There may be references to the rel in root->fkey_list, but if so,
443  * match_foreign_keys_to_quals() will get rid of them.
444  */
445 }
Relids ph_needed
Definition: relation.h:2196
void remove_join_clause_from_rels(PlannerInfo *root, RestrictInfo *restrictinfo, Relids join_relids)
Definition: joininfo.c:122
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:133
Relids ph_eval_at
Definition: relation.h:2194
RelOptKind reloptkind
Definition: relation.h:609
Relids * attr_needed
Definition: relation.h:645
List * join_info_list
Definition: relation.h:264
Relids required_relids
Definition: relation.h:1898
Relids min_righthand
Definition: relation.h:2067
Relids clause_relids
Definition: relation.h:1895
List * list_copy(const List *oldlist)
Definition: list.c:1160
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:590
Relids syn_lefthand
Definition: relation.h:2068
struct RelOptInfo ** simple_rel_array
Definition: relation.h:193
Relids syn_righthand
Definition: relation.h:2069
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2232
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: relation.h:1957
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:374
List * joininfo
Definition: relation.h:676
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
int simple_rel_array_size
Definition: relation.h:194
#define lnext(lc)
Definition: pg_list.h:105
Index relid
Definition: relation.h:640
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:729
Relids ph_lateral
Definition: relation.h:2195
unsigned int Index
Definition: c.h:442
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
AttrNumber max_attr
Definition: relation.h:644
List * placeholder_list
Definition: relation.h:270
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:279
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:801
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:486
Relids min_lefthand
Definition: relation.h:2066
AttrNumber min_attr
Definition: relation.h:643

◆ remove_useless_joins()

List* remove_useless_joins ( PlannerInfo root,
List joinlist 
)

Definition at line 61 of file analyzejoins.c.

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

Referenced by query_planner().

62 {
63  ListCell *lc;
64 
65  /*
66  * We are only interested in relations that are left-joined to, so we can
67  * scan the join_info_list to find them easily.
68  */
69 restart:
70  foreach(lc, root->join_info_list)
71  {
72  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
73  int innerrelid;
74  int nremoved;
75 
76  /* Skip if not removable */
77  if (!join_is_removable(root, sjinfo))
78  continue;
79 
80  /*
81  * Currently, join_is_removable can only succeed when the sjinfo's
82  * righthand is a single baserel. Remove that rel from the query and
83  * joinlist.
84  */
85  innerrelid = bms_singleton_member(sjinfo->min_righthand);
86 
87  remove_rel_from_query(root, innerrelid,
88  bms_union(sjinfo->min_lefthand,
89  sjinfo->min_righthand));
90 
91  /* We verify that exactly one reference gets removed from joinlist */
92  nremoved = 0;
93  joinlist = remove_rel_from_joinlist(joinlist, innerrelid, &nremoved);
94  if (nremoved != 1)
95  elog(ERROR, "failed to find relation %d in joinlist", innerrelid);
96 
97  /*
98  * We can delete this SpecialJoinInfo from the list too, since it's no
99  * longer of interest.
100  */
101  root->join_info_list = list_delete_ptr(root->join_info_list, sjinfo);
102 
103  /*
104  * Restart the scan. This is necessary to ensure we find all
105  * removable joins independently of ordering of the join_info_list
106  * (note that removal of attr_needed bits may make a join appear
107  * removable that did not before). Also, since we just deleted the
108  * current list cell, we'd have to have some kluge to continue the
109  * list scan anyway.
110  */
111  goto restart;
112  }
113 
114  return joinlist;
115 }
List * join_info_list
Definition: relation.h:264
Relids min_righthand
Definition: relation.h:2067
static void remove_rel_from_query(PlannerInfo *root, int relid, Relids joinrelids)
Definition: analyzejoins.c:313
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:590
#define ERROR
Definition: elog.h:43
static bool join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
Definition: analyzejoins.c:160
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:592
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:284
static List * remove_rel_from_joinlist(List *joinlist, int relid, int *nremoved)
Definition: analyzejoins.c:457
#define elog
Definition: elog.h:219
Relids min_lefthand
Definition: relation.h:2066