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/optimizer.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/planmain.h"
#include "optimizer/tlist.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 127 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().

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

◆ distinct_col_search()

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

Definition at line 924 of file analyzejoins.c.

References forboth, InvalidOid, lfirst_int, and lfirst_oid.

Referenced by query_is_distinct_for().

925 {
926  ListCell *lc1,
927  *lc2;
928 
929  forboth(lc1, colnos, lc2, opids)
930  {
931  if (colno == lfirst_int(lc1))
932  return lfirst_oid(lc2);
933  }
934  return InvalidOid;
935 }
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:419
#define lfirst_int(lc)
Definition: pg_list.h:191
#define InvalidOid
Definition: postgres_ext.h:36
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ 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 964 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().

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

◆ 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 1072 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().

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

◆ join_is_removable()

static bool join_is_removable ( PlannerInfo root,
SpecialJoinInfo sjinfo 
)
static

Definition at line 159 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().

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

◆ query_is_distinct_for()

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

Definition at line 775 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().

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

739 {
740  /* SRFs break distinctness except with DISTINCT, see below */
741  if (query->hasTargetSRFs && query->distinctClause == NIL)
742  return false;
743 
744  /* check for features we can prove distinctness with */
745  if (query->distinctClause != NIL ||
746  query->groupClause != NIL ||
747  query->groupingSets != NIL ||
748  query->hasAggs ||
749  query->havingQual ||
750  query->setOperations)
751  return true;
752 
753  return false;
754 }
#define NIL
Definition: pg_list.h:65
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 508 of file analyzejoins.c.

References bms_get_singleton_member(), bms_union(), SpecialJoinInfo::delay_upper_joins, 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, and rel_supports_distinctness().

Referenced by query_planner().

509 {
510  ListCell *lc;
511 
512  /*
513  * Scan the join_info_list to find semijoins.
514  */
515  foreach(lc, root->join_info_list)
516  {
517  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
518  int innerrelid;
519  RelOptInfo *innerrel;
520  Relids joinrelids;
521  List *restrictlist;
522 
523  /*
524  * Must be a non-delaying semijoin to a single baserel, else we aren't
525  * going to be able to do anything with it. (It's probably not
526  * possible for delay_upper_joins to be set on a semijoin, but we
527  * might as well check.)
528  */
529  if (sjinfo->jointype != JOIN_SEMI ||
530  sjinfo->delay_upper_joins)
531  continue;
532 
533  if (!bms_get_singleton_member(sjinfo->min_righthand, &innerrelid))
534  continue;
535 
536  innerrel = find_base_rel(root, innerrelid);
537 
538  /*
539  * Before we trouble to run generate_join_implied_equalities, make a
540  * quick check to eliminate cases in which we will surely be unable to
541  * prove uniqueness of the innerrel.
542  */
543  if (!rel_supports_distinctness(root, innerrel))
544  continue;
545 
546  /* Compute the relid set for the join we are considering */
547  joinrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
548 
549  /*
550  * Since we're only considering a single-rel RHS, any join clauses it
551  * has must be clauses linking it to the semijoin's min_lefthand. We
552  * can also consider EC-derived join clauses.
553  */
554  restrictlist =
556  joinrelids,
557  sjinfo->min_lefthand,
558  innerrel),
559  innerrel->joininfo);
560 
561  /* Test whether the innerrel is unique for those clauses. */
562  if (!innerrel_is_unique(root,
563  joinrelids, sjinfo->min_lefthand, innerrel,
564  JOIN_SEMI, restrictlist, true))
565  continue;
566 
567  /* OK, remove the SpecialJoinInfo from the list. */
569  }
570 }
List * join_info_list
Definition: pathnodes.h:281
Relids min_righthand
Definition: pathnodes.h:2134
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:615
bool innerrel_is_unique(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache)
Definition: analyzejoins.c:964
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:368
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1126
List * joininfo
Definition: pathnodes.h:707
bool delay_upper_joins
Definition: pathnodes.h:2139
#define lfirst(lc)
Definition: pg_list.h:190
JoinType jointype
Definition: pathnodes.h:2137
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:363
static bool rel_supports_distinctness(PlannerInfo *root, RelOptInfo *rel)
Definition: analyzejoins.c:585
Definition: pg_list.h:50
Relids min_lefthand
Definition: pathnodes.h:2133

◆ rel_is_distinct_for()

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

Definition at line 641 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().

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

◆ rel_supports_distinctness()

static bool rel_supports_distinctness ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 585 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().

586 {
587  /* We only know about baserels ... */
588  if (rel->reloptkind != RELOPT_BASEREL)
589  return false;
590  if (rel->rtekind == RTE_RELATION)
591  {
592  /*
593  * For a plain relation, we only know how to prove uniqueness by
594  * reference to unique indexes. Make sure there's at least one
595  * suitable unique index. It must be immediately enforced, and if
596  * it's a partial index, it must match the query. (Keep these
597  * conditions in sync with relation_has_unique_index_for!)
598  */
599  ListCell *lc;
600 
601  foreach(lc, rel->indexlist)
602  {
603  IndexOptInfo *ind = (IndexOptInfo *) lfirst(lc);
604 
605  if (ind->unique && ind->immediate &&
606  (ind->indpred == NIL || ind->predOK))
607  return true;
608  }
609  }
610  else if (rel->rtekind == RTE_SUBQUERY)
611  {
612  Query *subquery = root->simple_rte_array[rel->relid]->subquery;
613 
614  /* Check if the subquery has any qualities that support distinctness */
615  if (query_supports_distinctness(subquery))
616  return true;
617  }
618  /* We have no proof rules for any other rtekinds. */
619  return false;
620 }
#define NIL
Definition: pg_list.h:65
RelOptKind reloptkind
Definition: pathnodes.h:638
bool immediate
Definition: pathnodes.h:826
Index relid
Definition: pathnodes.h:669
RangeTblEntry ** simple_rte_array
Definition: pathnodes.h:209
RTEKind rtekind
Definition: pathnodes.h:671
List * indexlist
Definition: pathnodes.h:678
#define lfirst(lc)
Definition: pg_list.h:190
bool query_supports_distinctness(Query *query)
Definition: analyzejoins.c:738
Query * subquery
Definition: parsenodes.h:1009
List * indpred
Definition: pathnodes.h:814

◆ remove_rel_from_joinlist()

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

Definition at line 454 of file analyzejoins.c.

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

Referenced by remove_useless_joins().

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

◆ remove_rel_from_query()

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

Definition at line 312 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(), foreach_delete_current, PlannerInfo::join_info_list, RelOptInfo::joininfo, lfirst, list_copy(), 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().

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

◆ 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_cell(), 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. (Since we'll restart the foreach loop
100  * immediately, we don't bother with foreach_delete_current.)
101  */
103 
104  /*
105  * Restart the scan. This is necessary to ensure we find all
106  * removable joins independently of ordering of the join_info_list
107  * (note that removal of attr_needed bits may make a join appear
108  * removable that did not before).
109  */
110  goto restart;
111  }
112 
113  return joinlist;
114 }
List * join_info_list
Definition: pathnodes.h:281
Relids min_righthand
Definition: pathnodes.h:2134
static void remove_rel_from_query(PlannerInfo *root, int relid, Relids joinrelids)
Definition: analyzejoins.c:312
#define ERROR
Definition: elog.h:43
static bool join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
Definition: analyzejoins.c:159
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:577
List * list_delete_cell(List *list, ListCell *cell)
Definition: list.c:768
#define lfirst(lc)
Definition: pg_list.h:190
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
#define elog(elevel,...)
Definition: elog.h:226
static List * remove_rel_from_joinlist(List *joinlist, int relid, int *nremoved)
Definition: analyzejoins.c:454
Relids min_lefthand
Definition: pathnodes.h:2133