PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
analyzejoins.c File Reference
Include dependency graph for analyzejoins.c:

Go to the source code of this file.

Data Structures

struct  SelfJoinCandidate
 

Functions

static bool join_is_removable (PlannerInfo *root, SpecialJoinInfo *sjinfo)
 
static void remove_leftjoinrel_from_query (PlannerInfo *root, int relid, SpecialJoinInfo *sjinfo)
 
static void remove_rel_from_restrictinfo (RestrictInfo *rinfo, int relid, int ojrelid)
 
static void remove_rel_from_eclass (EquivalenceClass *ec, SpecialJoinInfo *sjinfo, int relid, int subst)
 
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, List **extra_clauses)
 
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, List **extra_clauses)
 
static int self_join_candidates_cmp (const void *a, const void *b)
 
Listremove_useless_joins (PlannerInfo *root, List *joinlist)
 
static void remove_rel_from_query (PlannerInfo *root, RelOptInfo *rel, int subst, SpecialJoinInfo *sjinfo, Relids joinrelids)
 
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)
 
bool innerrel_is_unique_ext (PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache, List **extra_clauses)
 
static void update_eclasses (EquivalenceClass *ec, int from, int to)
 
static bool restrict_infos_logically_equal (RestrictInfo *a, RestrictInfo *b)
 
static void add_non_redundant_clauses (PlannerInfo *root, List *rinfo_candidates, List **keep_rinfo_list, Index removed_relid)
 
static void remove_self_join_rel (PlannerInfo *root, PlanRowMark *kmark, PlanRowMark *rmark, RelOptInfo *toKeep, RelOptInfo *toRemove, List *restrictlist)
 
static void split_selfjoin_quals (PlannerInfo *root, List *joinquals, List **selfjoinquals, List **otherjoinquals, int from, int to)
 
static bool match_unique_clauses (PlannerInfo *root, RelOptInfo *outer, List *uclauses, Index relid)
 
static Relids remove_self_joins_one_group (PlannerInfo *root, Relids relids)
 
static Relids remove_self_joins_recurse (PlannerInfo *root, List *joinlist, Relids toRemove)
 
Listremove_useless_self_joins (PlannerInfo *root, List *joinlist)
 

Variables

bool enable_self_join_elimination
 

Function Documentation

◆ add_non_redundant_clauses()

static void add_non_redundant_clauses ( PlannerInfo root,
List rinfo_candidates,
List **  keep_rinfo_list,
Index  removed_relid 
)
static

Definition at line 1638 of file analyzejoins.c.

1642{
1643 foreach_node(RestrictInfo, rinfo, rinfo_candidates)
1644 {
1645 bool is_redundant = false;
1646
1647 Assert(!bms_is_member(removed_relid, rinfo->required_relids));
1648
1649 foreach_node(RestrictInfo, src, (*keep_rinfo_list))
1650 {
1651 if (!bms_equal(src->clause_relids, rinfo->clause_relids))
1652 /* Can't compare trivially different clauses */
1653 continue;
1654
1655 if (src == rinfo ||
1656 (rinfo->parent_ec != NULL &&
1657 src->parent_ec == rinfo->parent_ec) ||
1659 {
1660 is_redundant = true;
1661 break;
1662 }
1663 }
1664 if (!is_redundant)
1666 }
1667}
static bool restrict_infos_logically_equal(RestrictInfo *a, RestrictInfo *b)
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:142
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
Assert(PointerIsAligned(start, uint64))
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:3227
#define foreach_node(type, var, lst)
Definition: pg_list.h:496
tree ctl root
Definition: radixtree.h:1857

References Assert(), bms_equal(), bms_is_member(), distribute_restrictinfo_to_rels(), foreach_node, restrict_infos_logically_equal(), and root.

Referenced by remove_self_join_rel().

◆ distinct_col_search()

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

Definition at line 1248 of file analyzejoins.c.

1249{
1250 ListCell *lc1,
1251 *lc2;
1252
1253 forboth(lc1, colnos, lc2, opids)
1254 {
1255 if (colno == lfirst_int(lc1))
1256 return lfirst_oid(lc2);
1257 }
1258 return InvalidOid;
1259}
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
#define lfirst_int(lc)
Definition: pg_list.h:173
#define lfirst_oid(lc)
Definition: pg_list.h:174
#define InvalidOid
Definition: postgres_ext.h:35

References forboth, InvalidOid, lfirst_int, and lfirst_oid.

Referenced by query_is_distinct_for().

◆ innerrel_is_unique()

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

Definition at line 1288 of file analyzejoins.c.

1295{
1296 return innerrel_is_unique_ext(root, joinrelids, outerrelids, innerrel,
1297 jointype, restrictlist, force_cache, NULL);
1298}
bool innerrel_is_unique_ext(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache, List **extra_clauses)

References innerrel_is_unique_ext(), and root.

Referenced by add_paths_to_joinrel(), and reduce_unique_semijoins().

◆ innerrel_is_unique_ext()

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

Definition at line 1310 of file analyzejoins.c.

1318{
1319 MemoryContext old_context;
1320 ListCell *lc;
1321 UniqueRelInfo *uniqueRelInfo;
1322 List *outer_exprs = NIL;
1323 bool self_join = (extra_clauses != NULL);
1324
1325 /* Certainly can't prove uniqueness when there are no joinclauses */
1326 if (restrictlist == NIL)
1327 return false;
1328
1329 /*
1330 * Make a quick check to eliminate cases in which we will surely be unable
1331 * to prove uniqueness of the innerrel.
1332 */
1333 if (!rel_supports_distinctness(root, innerrel))
1334 return false;
1335
1336 /*
1337 * Query the cache to see if we've managed to prove that innerrel is
1338 * unique for any subset of this outerrel. For non-self-join search, we
1339 * don't need an exact match, as extra outerrels can't make the innerrel
1340 * any less unique (or more formally, the restrictlist for a join to a
1341 * superset outerrel must be a superset of the conditions we successfully
1342 * used before). For self-join search, we require an exact match of
1343 * outerrels because we need extra clauses to be valid for our case. Also,
1344 * for self-join checking we've filtered the clauses list. Thus, we can
1345 * match only the result cached for a self-join search for another
1346 * self-join check.
1347 */
1348 foreach(lc, innerrel->unique_for_rels)
1349 {
1350 uniqueRelInfo = (UniqueRelInfo *) lfirst(lc);
1351
1352 if ((!self_join && bms_is_subset(uniqueRelInfo->outerrelids, outerrelids)) ||
1353 (self_join && bms_equal(uniqueRelInfo->outerrelids, outerrelids) &&
1354 uniqueRelInfo->self_join))
1355 {
1356 if (extra_clauses)
1357 *extra_clauses = uniqueRelInfo->extra_clauses;
1358 return true; /* Success! */
1359 }
1360 }
1361
1362 /*
1363 * Conversely, we may have already determined that this outerrel, or some
1364 * superset thereof, cannot prove this innerrel to be unique.
1365 */
1366 foreach(lc, innerrel->non_unique_for_rels)
1367 {
1368 Relids unique_for_rels = (Relids) lfirst(lc);
1369
1370 if (bms_is_subset(outerrelids, unique_for_rels))
1371 return false;
1372 }
1373
1374 /* No cached information, so try to make the proof. */
1375 if (is_innerrel_unique_for(root, joinrelids, outerrelids, innerrel,
1376 jointype, restrictlist,
1377 self_join ? &outer_exprs : NULL))
1378 {
1379 /*
1380 * Cache the positive result for future probes, being sure to keep it
1381 * in the planner_cxt even if we are working in GEQO.
1382 *
1383 * Note: one might consider trying to isolate the minimal subset of
1384 * the outerrels that proved the innerrel unique. But it's not worth
1385 * the trouble, because the planner builds up joinrels incrementally
1386 * and so we'll see the minimally sufficient outerrels before any
1387 * supersets of them anyway.
1388 */
1389 old_context = MemoryContextSwitchTo(root->planner_cxt);
1390 uniqueRelInfo = makeNode(UniqueRelInfo);
1391 uniqueRelInfo->outerrelids = bms_copy(outerrelids);
1392 uniqueRelInfo->self_join = self_join;
1393 uniqueRelInfo->extra_clauses = outer_exprs;
1394 innerrel->unique_for_rels = lappend(innerrel->unique_for_rels,
1395 uniqueRelInfo);
1396 MemoryContextSwitchTo(old_context);
1397
1398 if (extra_clauses)
1399 *extra_clauses = outer_exprs;
1400 return true; /* Success! */
1401 }
1402 else
1403 {
1404 /*
1405 * None of the join conditions for outerrel proved innerrel unique, so
1406 * we can safely reject this outerrel or any subset of it in future
1407 * checks.
1408 *
1409 * However, in normal planning mode, caching this knowledge is totally
1410 * pointless; it won't be queried again, because we build up joinrels
1411 * from smaller to larger. It is useful in GEQO mode, where the
1412 * knowledge can be carried across successive planning attempts; and
1413 * it's likely to be useful when using join-search plugins, too. Hence
1414 * cache when join_search_private is non-NULL. (Yeah, that's a hack,
1415 * but it seems reasonable.)
1416 *
1417 * Also, allow callers to override that heuristic and force caching;
1418 * that's useful for reduce_unique_semijoins, which calls here before
1419 * the normal join search starts.
1420 */
1421 if (force_cache || root->join_search_private)
1422 {
1423 old_context = MemoryContextSwitchTo(root->planner_cxt);
1424 innerrel->non_unique_for_rels =
1425 lappend(innerrel->non_unique_for_rels,
1426 bms_copy(outerrelids));
1427 MemoryContextSwitchTo(old_context);
1428 }
1429
1430 return false;
1431 }
1432}
static bool is_innerrel_unique_for(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, List **extra_clauses)
static bool rel_supports_distinctness(PlannerInfo *root, RelOptInfo *rel)
Definition: analyzejoins.c:902
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:412
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:122
List * lappend(List *list, void *datum)
Definition: list.c:339
#define makeNode(_type_)
Definition: nodes.h:161
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
Bitmapset * Relids
Definition: pathnodes.h:30
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
Definition: pg_list.h:54
List * unique_for_rels
Definition: pathnodes.h:1004
List * non_unique_for_rels
Definition: pathnodes.h:1006
Relids outerrelids
Definition: pathnodes.h:3586
List * extra_clauses
Definition: pathnodes.h:3600

References bms_copy(), bms_equal(), bms_is_subset(), UniqueRelInfo::extra_clauses, is_innerrel_unique_for(), lappend(), lfirst, makeNode, MemoryContextSwitchTo(), NIL, RelOptInfo::non_unique_for_rels, UniqueRelInfo::outerrelids, rel_supports_distinctness(), root, UniqueRelInfo::self_join, and RelOptInfo::unique_for_rels.

Referenced by innerrel_is_unique(), and remove_self_joins_one_group().

◆ is_innerrel_unique_for()

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

Definition at line 1440 of file analyzejoins.c.

1447{
1448 List *clause_list = NIL;
1449 ListCell *lc;
1450
1451 /*
1452 * Search for mergejoinable clauses that constrain the inner rel against
1453 * the outer rel. If an operator is mergejoinable then it behaves like
1454 * equality for some btree opclass, so it's what we want. The
1455 * mergejoinability test also eliminates clauses containing volatile
1456 * functions, which we couldn't depend on.
1457 */
1458 foreach(lc, restrictlist)
1459 {
1460 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1461
1462 /*
1463 * As noted above, if it's a pushed-down clause and we're at an outer
1464 * join, we can't use it.
1465 */
1466 if (IS_OUTER_JOIN(jointype) &&
1467 RINFO_IS_PUSHED_DOWN(restrictinfo, joinrelids))
1468 continue;
1469
1470 /* Ignore if it's not a mergejoinable clause */
1471 if (!restrictinfo->can_join ||
1472 restrictinfo->mergeopfamilies == NIL)
1473 continue; /* not mergejoinable */
1474
1475 /*
1476 * Check if the clause has the form "outer op inner" or "inner op
1477 * outer", and if so mark which side is inner.
1478 */
1479 if (!clause_sides_match_join(restrictinfo, outerrelids,
1480 innerrel->relids))
1481 continue; /* no good for these input relations */
1482
1483 /* OK, add to the list */
1484 clause_list = lappend(clause_list, restrictinfo);
1485 }
1486
1487 /* Let rel_is_distinct_for() do the hard work */
1488 return rel_is_distinct_for(root, innerrel, clause_list, extra_clauses);
1489}
static bool rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel, List *clause_list, List **extra_clauses)
Definition: analyzejoins.c:962
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:344
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: pathnodes.h:2857
static bool clause_sides_match_join(RestrictInfo *rinfo, Relids outerrelids, Relids innerrelids)
Definition: restrictinfo.h:73
Relids relids
Definition: pathnodes.h:898

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

Referenced by innerrel_is_unique_ext().

◆ join_is_removable()

static bool join_is_removable ( PlannerInfo root,
SpecialJoinInfo sjinfo 
)
static

Definition at line 153 of file analyzejoins.c.

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

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

Referenced by remove_useless_joins().

◆ match_unique_clauses()

static bool match_unique_clauses ( PlannerInfo root,
RelOptInfo outer,
List uclauses,
Index  relid 
)
static

Definition at line 1931 of file analyzejoins.c.

1933{
1934 foreach_node(RestrictInfo, rinfo, uclauses)
1935 {
1936 Expr *clause;
1937 Node *iclause;
1938 Node *c1;
1939 bool matched = false;
1940
1941 Assert(outer->relid > 0 && relid > 0);
1942
1943 /* Only filters like f(R.x1,...,R.xN) == expr we should consider. */
1944 Assert(bms_is_empty(rinfo->left_relids) ^
1945 bms_is_empty(rinfo->right_relids));
1946
1947 clause = (Expr *) copyObject(rinfo->clause);
1948 ChangeVarNodes((Node *) clause, relid, outer->relid, 0);
1949
1950 iclause = bms_is_empty(rinfo->left_relids) ? get_rightop(clause) :
1951 get_leftop(clause);
1952 c1 = bms_is_empty(rinfo->left_relids) ? get_leftop(clause) :
1953 get_rightop(clause);
1954
1955 /*
1956 * Compare these left and right sides with the corresponding sides of
1957 * the outer's filters. If no one is detected - return immediately.
1958 */
1960 {
1961 Node *oclause;
1962 Node *c2;
1963
1964 if (orinfo->mergeopfamilies == NIL)
1965 /* Don't consider clauses that aren't similar to 'F(X)=G(Y)' */
1966 continue;
1967
1968 Assert(is_opclause(orinfo->clause));
1969
1970 oclause = bms_is_empty(orinfo->left_relids) ?
1971 get_rightop(orinfo->clause) : get_leftop(orinfo->clause);
1972 c2 = (bms_is_empty(orinfo->left_relids) ?
1973 get_leftop(orinfo->clause) : get_rightop(orinfo->clause));
1974
1975 if (equal(iclause, oclause) && equal(c1, c2))
1976 {
1977 matched = true;
1978 break;
1979 }
1980 }
1981
1982 if (!matched)
1983 return false;
1984 }
1985
1986 return true;
1987}
#define bms_is_empty(a)
Definition: bitmapset.h:118
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:95
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:76
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:83
#define copyObject(obj)
Definition: nodes.h:230
void ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up)
Definition: rewriteManip.c:793
List * baserestrictinfo
Definition: pathnodes.h:1012
Index relid
Definition: pathnodes.h:945

References Assert(), RelOptInfo::baserestrictinfo, bms_is_empty, ChangeVarNodes(), copyObject, equal(), foreach_node, get_leftop(), get_rightop(), is_opclause(), NIL, and RelOptInfo::relid.

Referenced by remove_self_joins_one_group().

◆ query_is_distinct_for()

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

Definition at line 1099 of file analyzejoins.c.

1100{
1101 ListCell *l;
1102 Oid opid;
1103
1104 Assert(list_length(colnos) == list_length(opids));
1105
1106 /*
1107 * DISTINCT (including DISTINCT ON) guarantees uniqueness if all the
1108 * columns in the DISTINCT clause appear in colnos and operator semantics
1109 * match. This is true even if there are SRFs in the DISTINCT columns or
1110 * elsewhere in the tlist.
1111 */
1112 if (query->distinctClause)
1113 {
1114 foreach(l, query->distinctClause)
1115 {
1118 query->targetList);
1119
1120 opid = distinct_col_search(tle->resno, colnos, opids);
1121 if (!OidIsValid(opid) ||
1122 !equality_ops_are_compatible(opid, sgc->eqop))
1123 break; /* exit early if no match */
1124 }
1125 if (l == NULL) /* had matches for all? */
1126 return true;
1127 }
1128
1129 /*
1130 * Otherwise, a set-returning function in the query's targetlist can
1131 * result in returning duplicate rows, despite any grouping that might
1132 * occur before tlist evaluation. (If all tlist SRFs are within GROUP BY
1133 * columns, it would be safe because they'd be expanded before grouping.
1134 * But it doesn't currently seem worth the effort to check for that.)
1135 */
1136 if (query->hasTargetSRFs)
1137 return false;
1138
1139 /*
1140 * Similarly, GROUP BY without GROUPING SETS guarantees uniqueness if all
1141 * the grouped columns appear in colnos and operator semantics match.
1142 */
1143 if (query->groupClause && !query->groupingSets)
1144 {
1145 foreach(l, query->groupClause)
1146 {
1149 query->targetList);
1150
1151 opid = distinct_col_search(tle->resno, colnos, opids);
1152 if (!OidIsValid(opid) ||
1153 !equality_ops_are_compatible(opid, sgc->eqop))
1154 break; /* exit early if no match */
1155 }
1156 if (l == NULL) /* had matches for all? */
1157 return true;
1158 }
1159 else if (query->groupingSets)
1160 {
1161 /*
1162 * If we have grouping sets with expressions, we probably don't have
1163 * uniqueness and analysis would be hard. Punt.
1164 */
1165 if (query->groupClause)
1166 return false;
1167
1168 /*
1169 * If we have no groupClause (therefore no grouping expressions), we
1170 * might have one or many empty grouping sets. If there's just one,
1171 * then we're returning only one row and are certainly unique. But
1172 * otherwise, we know we're certainly not unique.
1173 */
1174 if (list_length(query->groupingSets) == 1 &&
1175 ((GroupingSet *) linitial(query->groupingSets))->kind == GROUPING_SET_EMPTY)
1176 return true;
1177 else
1178 return false;
1179 }
1180 else
1181 {
1182 /*
1183 * If we have no GROUP BY, but do have aggregates or HAVING, then the
1184 * result is at most one row so it's surely unique, for any operators.
1185 */
1186 if (query->hasAggs || query->havingQual)
1187 return true;
1188 }
1189
1190 /*
1191 * UNION, INTERSECT, EXCEPT guarantee uniqueness of the whole output row,
1192 * except with ALL.
1193 */
1194 if (query->setOperations)
1195 {
1197
1198 Assert(topop->op != SETOP_NONE);
1199
1200 if (!topop->all)
1201 {
1202 ListCell *lg;
1203
1204 /* We're good if all the nonjunk output columns are in colnos */
1205 lg = list_head(topop->groupClauses);
1206 foreach(l, query->targetList)
1207 {
1208 TargetEntry *tle = (TargetEntry *) lfirst(l);
1209 SortGroupClause *sgc;
1210
1211 if (tle->resjunk)
1212 continue; /* ignore resjunk columns */
1213
1214 /* non-resjunk columns should have grouping clauses */
1215 Assert(lg != NULL);
1216 sgc = (SortGroupClause *) lfirst(lg);
1217 lg = lnext(topop->groupClauses, lg);
1218
1219 opid = distinct_col_search(tle->resno, colnos, opids);
1220 if (!OidIsValid(opid) ||
1221 !equality_ops_are_compatible(opid, sgc->eqop))
1222 break; /* exit early if no match */
1223 }
1224 if (l == NULL) /* had matches for all? */
1225 return true;
1226 }
1227 }
1228
1229 /*
1230 * XXX Are there any other cases in which we can easily see the result
1231 * must be distinct?
1232 *
1233 * If you do add more smarts to this function, be sure to update
1234 * query_supports_distinctness() to match.
1235 */
1236
1237 return false;
1238}
static Oid distinct_col_search(int colno, List *colnos, List *opids)
#define OidIsValid(objectId)
Definition: c.h:746
bool equality_ops_are_compatible(Oid opno1, Oid opno2)
Definition: lsyscache.c:779
#define castNode(_type_, nodeptr)
Definition: nodes.h:182
@ GROUPING_SET_EMPTY
Definition: parsenodes.h:1513
@ SETOP_NONE
Definition: parsenodes.h:2167
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial(l)
Definition: pg_list.h:178
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
unsigned int Oid
Definition: postgres_ext.h:30
Node * setOperations
Definition: parsenodes.h:230
List * groupClause
Definition: parsenodes.h:211
Node * havingQual
Definition: parsenodes.h:216
List * targetList
Definition: parsenodes.h:193
List * groupingSets
Definition: parsenodes.h:214
List * distinctClause
Definition: parsenodes.h:220
SetOperation op
Definition: parsenodes.h:2247
AttrNumber resno
Definition: primnodes.h:2221
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:367

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

Referenced by create_unique_path(), and rel_is_distinct_for().

◆ query_supports_distinctness()

bool query_supports_distinctness ( Query query)

Definition at line 1061 of file analyzejoins.c.

1062{
1063 /* SRFs break distinctness except with DISTINCT, see below */
1064 if (query->hasTargetSRFs && query->distinctClause == NIL)
1065 return false;
1066
1067 /* check for features we can prove distinctness with */
1068 if (query->distinctClause != NIL ||
1069 query->groupClause != NIL ||
1070 query->groupingSets != NIL ||
1071 query->hasAggs ||
1072 query->havingQual ||
1073 query->setOperations)
1074 return true;
1075
1076 return false;
1077}

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

Referenced by create_unique_path(), and rel_supports_distinctness().

◆ reduce_unique_semijoins()

void reduce_unique_semijoins ( PlannerInfo root)

Definition at line 826 of file analyzejoins.c.

827{
828 ListCell *lc;
829
830 /*
831 * Scan the join_info_list to find semijoins.
832 */
833 foreach(lc, root->join_info_list)
834 {
835 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
836 int innerrelid;
837 RelOptInfo *innerrel;
838 Relids joinrelids;
839 List *restrictlist;
840
841 /*
842 * Must be a semijoin to a single baserel, else we aren't going to be
843 * able to do anything with it.
844 */
845 if (sjinfo->jointype != JOIN_SEMI)
846 continue;
847
848 if (!bms_get_singleton_member(sjinfo->min_righthand, &innerrelid))
849 continue;
850
851 innerrel = find_base_rel(root, innerrelid);
852
853 /*
854 * Before we trouble to run generate_join_implied_equalities, make a
855 * quick check to eliminate cases in which we will surely be unable to
856 * prove uniqueness of the innerrel.
857 */
858 if (!rel_supports_distinctness(root, innerrel))
859 continue;
860
861 /* Compute the relid set for the join we are considering */
862 joinrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
863 Assert(sjinfo->ojrelid == 0); /* SEMI joins don't have RT indexes */
864
865 /*
866 * Since we're only considering a single-rel RHS, any join clauses it
867 * has must be clauses linking it to the semijoin's min_lefthand. We
868 * can also consider EC-derived join clauses.
869 */
870 restrictlist =
872 joinrelids,
873 sjinfo->min_lefthand,
874 innerrel,
875 NULL),
876 innerrel->joininfo);
877
878 /* Test whether the innerrel is unique for those clauses. */
880 joinrelids, sjinfo->min_lefthand, innerrel,
881 JOIN_SEMI, restrictlist, true))
882 continue;
883
884 /* OK, remove the SpecialJoinInfo from the list. */
885 root->join_info_list = foreach_delete_current(root->join_info_list, lc);
886 }
887}
bool innerrel_is_unique(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache)
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
Definition: equivclass.c:1550
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
@ JOIN_SEMI
Definition: nodes.h:313
#define foreach_delete_current(lst, var_or_cell)
Definition: pg_list.h:391

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

Referenced by query_planner().

◆ rel_is_distinct_for()

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

Definition at line 962 of file analyzejoins.c.

964{
965 /*
966 * We could skip a couple of tests here if we assume all callers checked
967 * rel_supports_distinctness first, but it doesn't seem worth taking any
968 * risk for.
969 */
970 if (rel->reloptkind != RELOPT_BASEREL)
971 return false;
972 if (rel->rtekind == RTE_RELATION)
973 {
974 /*
975 * Examine the indexes to see if we have a matching unique index.
976 * relation_has_unique_index_ext automatically adds any usable
977 * restriction clauses for the rel, so we needn't do that here.
978 */
979 if (relation_has_unique_index_ext(root, rel, clause_list, NIL, NIL,
980 extra_clauses))
981 return true;
982 }
983 else if (rel->rtekind == RTE_SUBQUERY)
984 {
985 Index relid = rel->relid;
986 Query *subquery = root->simple_rte_array[relid]->subquery;
987 List *colnos = NIL;
988 List *opids = NIL;
989 ListCell *l;
990
991 /*
992 * Build the argument lists for query_is_distinct_for: a list of
993 * output column numbers that the query needs to be distinct over, and
994 * a list of equality operators that the output columns need to be
995 * distinct according to.
996 *
997 * (XXX we are not considering restriction clauses attached to the
998 * subquery; is that worth doing?)
999 */
1000 foreach(l, clause_list)
1001 {
1003 Oid op;
1004 Var *var;
1005
1006 /*
1007 * Get the equality operator we need uniqueness according to.
1008 * (This might be a cross-type operator and thus not exactly the
1009 * same operator the subquery would consider; that's all right
1010 * since query_is_distinct_for can resolve such cases.) The
1011 * caller's mergejoinability test should have selected only
1012 * OpExprs.
1013 */
1014 op = castNode(OpExpr, rinfo->clause)->opno;
1015
1016 /* caller identified the inner side for us */
1017 if (rinfo->outer_is_left)
1018 var = (Var *) get_rightop(rinfo->clause);
1019 else
1020 var = (Var *) get_leftop(rinfo->clause);
1021
1022 /*
1023 * We may ignore any RelabelType node above the operand. (There
1024 * won't be more than one, since eval_const_expressions() has been
1025 * applied already.)
1026 */
1027 if (var && IsA(var, RelabelType))
1028 var = (Var *) ((RelabelType *) var)->arg;
1029
1030 /*
1031 * If inner side isn't a Var referencing a subquery output column,
1032 * this clause doesn't help us.
1033 */
1034 if (!var || !IsA(var, Var) ||
1035 var->varno != relid || var->varlevelsup != 0)
1036 continue;
1037
1038 colnos = lappend_int(colnos, var->varattno);
1039 opids = lappend_oid(opids, op);
1040 }
1041
1042 if (query_is_distinct_for(subquery, colnos, opids))
1043 return true;
1044 }
1045 return false;
1046}
bool query_is_distinct_for(Query *query, List *colnos, List *opids)
unsigned int Index
Definition: c.h:585
bool relation_has_unique_index_ext(PlannerInfo *root, RelOptInfo *rel, List *restrictlist, List *exprlist, List *oprlist, List **extra_clauses)
Definition: indxpath.c:4177
List * lappend_int(List *list, int datum)
Definition: list.c:357
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
@ RTE_SUBQUERY
Definition: parsenodes.h:1027
@ RTE_RELATION
Definition: parsenodes.h:1026
@ RELOPT_BASEREL
Definition: pathnodes.h:854
#define lfirst_node(type, lc)
Definition: pg_list.h:176
RelOptKind reloptkind
Definition: pathnodes.h:892
RTEKind rtekind
Definition: pathnodes.h:949
Expr * clause
Definition: pathnodes.h:2700
Definition: primnodes.h:262
AttrNumber varattno
Definition: primnodes.h:274
int varno
Definition: primnodes.h:269
Index varlevelsup
Definition: primnodes.h:294

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

Referenced by is_innerrel_unique_for(), and join_is_removable().

◆ rel_supports_distinctness()

static bool rel_supports_distinctness ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 902 of file analyzejoins.c.

903{
904 /* We only know about baserels ... */
905 if (rel->reloptkind != RELOPT_BASEREL)
906 return false;
907 if (rel->rtekind == RTE_RELATION)
908 {
909 /*
910 * For a plain relation, we only know how to prove uniqueness by
911 * reference to unique indexes. Make sure there's at least one
912 * suitable unique index. It must be immediately enforced, and not a
913 * partial index. (Keep these conditions in sync with
914 * relation_has_unique_index_for!)
915 */
916 ListCell *lc;
917
918 foreach(lc, rel->indexlist)
919 {
921
922 if (ind->unique && ind->immediate && ind->indpred == NIL)
923 return true;
924 }
925 }
926 else if (rel->rtekind == RTE_SUBQUERY)
927 {
928 Query *subquery = root->simple_rte_array[rel->relid]->subquery;
929
930 /* Check if the subquery has any qualities that support distinctness */
931 if (query_supports_distinctness(subquery))
932 return true;
933 }
934 /* We have no proof rules for any other rtekinds. */
935 return false;
936}
bool query_supports_distinctness(Query *query)
List * indexlist
Definition: pathnodes.h:971

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

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

◆ remove_leftjoinrel_from_query()

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

Definition at line 528 of file analyzejoins.c.

530{
531 RelOptInfo *rel = find_base_rel(root, relid);
532 int ojrelid = sjinfo->ojrelid;
533 Relids joinrelids;
534 Relids join_plus_commute;
535 List *joininfos;
536 ListCell *l;
537
538 /* Compute the relid set for the join we are considering */
539 joinrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
540 Assert(ojrelid != 0);
541 joinrelids = bms_add_member(joinrelids, ojrelid);
542
543 remove_rel_from_query(root, rel, -1, sjinfo, joinrelids);
544
545 /*
546 * Remove any joinquals referencing the rel from the joininfo lists.
547 *
548 * In some cases, a joinqual has to be put back after deleting its
549 * reference to the target rel. This can occur for pseudoconstant and
550 * outerjoin-delayed quals, which can get marked as requiring the rel in
551 * order to force them to be evaluated at or above the join. We can't
552 * just discard them, though. Only quals that logically belonged to the
553 * outer join being discarded should be removed from the query.
554 *
555 * We might encounter a qual that is a clone of a deletable qual with some
556 * outer-join relids added (see deconstruct_distribute_oj_quals). To
557 * ensure we get rid of such clones as well, add the relids of all OJs
558 * commutable with this one to the set we test against for
559 * pushed-down-ness.
560 */
561 join_plus_commute = bms_union(joinrelids,
562 sjinfo->commute_above_r);
563 join_plus_commute = bms_add_members(join_plus_commute,
564 sjinfo->commute_below_l);
565
566 /*
567 * We must make a copy of the rel's old joininfo list before starting the
568 * loop, because otherwise remove_join_clause_from_rels would destroy the
569 * list while we're scanning it.
570 */
571 joininfos = list_copy(rel->joininfo);
572 foreach(l, joininfos)
573 {
574 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
575
577
578 if (RINFO_IS_PUSHED_DOWN(rinfo, join_plus_commute))
579 {
580 /*
581 * There might be references to relid or ojrelid in the
582 * RestrictInfo's relid sets, as a consequence of PHVs having had
583 * ph_eval_at sets that include those. We already checked above
584 * that any such PHV is safe (and updated its ph_eval_at), so we
585 * can just drop those references.
586 */
587 remove_rel_from_restrictinfo(rinfo, relid, ojrelid);
588
589 /*
590 * Cross-check that the clause itself does not reference the
591 * target rel or join.
592 */
593#ifdef USE_ASSERT_CHECKING
594 {
595 Relids clause_varnos = pull_varnos(root,
596 (Node *) rinfo->clause);
597
598 Assert(!bms_is_member(relid, clause_varnos));
599 Assert(!bms_is_member(ojrelid, clause_varnos));
600 }
601#endif
602 /* Now throw it back into the joininfo lists */
604 }
605 }
606
607 /*
608 * There may be references to the rel in root->fkey_list, but if so,
609 * match_foreign_keys_to_quals() will get rid of them.
610 */
611
612 /*
613 * Now remove the rel from the baserel array to prevent it from being
614 * referenced again. (We can't do this earlier because
615 * remove_join_clause_from_rels will touch it.)
616 */
617 root->simple_rel_array[relid] = NULL;
618
619 /* And nuke the RelOptInfo, just in case there's another access path */
620 pfree(rel);
621
622 /*
623 * Now repeat construction of attr_needed bits coming from all other
624 * sources.
625 */
630}
static void remove_rel_from_query(PlannerInfo *root, RelOptInfo *rel, int subst, SpecialJoinInfo *sjinfo, Relids joinrelids)
Definition: analyzejoins.c:323
static void remove_rel_from_restrictinfo(RestrictInfo *rinfo, int relid, int ojrelid)
Definition: analyzejoins.c:641
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:917
void rebuild_eclass_attr_needed(PlannerInfo *root)
Definition: equivclass.c:2574
void rebuild_lateral_attr_needed(PlannerInfo *root)
Definition: initsplan.c:807
void rebuild_joinclause_attr_needed(PlannerInfo *root)
Definition: initsplan.c:3559
void remove_join_clause_from_rels(PlannerInfo *root, RestrictInfo *restrictinfo, Relids join_relids)
Definition: joininfo.c:161
List * list_copy(const List *oldlist)
Definition: list.c:1573
void pfree(void *pointer)
Definition: mcxt.c:2146
void rebuild_placeholder_attr_needed(PlannerInfo *root)
Definition: placeholder.c:327
Relids required_relids
Definition: pathnodes.h:2731
Relids commute_above_r
Definition: pathnodes.h:3037
Relids commute_below_l
Definition: pathnodes.h:3038

References Assert(), bms_add_member(), bms_add_members(), bms_is_member(), bms_union(), RestrictInfo::clause, SpecialJoinInfo::commute_above_r, SpecialJoinInfo::commute_below_l, distribute_restrictinfo_to_rels(), find_base_rel(), RelOptInfo::joininfo, lfirst, list_copy(), SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, SpecialJoinInfo::ojrelid, pfree(), pull_varnos(), rebuild_eclass_attr_needed(), rebuild_joinclause_attr_needed(), rebuild_lateral_attr_needed(), rebuild_placeholder_attr_needed(), remove_join_clause_from_rels(), remove_rel_from_query(), remove_rel_from_restrictinfo(), RestrictInfo::required_relids, RINFO_IS_PUSHED_DOWN, and root.

Referenced by remove_useless_joins().

◆ remove_rel_from_eclass()

static void remove_rel_from_eclass ( EquivalenceClass ec,
SpecialJoinInfo sjinfo,
int  relid,
int  subst 
)
static

Definition at line 702 of file analyzejoins.c.

704{
705 ListCell *lc;
706
707 /* Fix up the EC's overall relids */
708 ec->ec_relids = adjust_relid_set(ec->ec_relids, relid, subst);
709 if (sjinfo != NULL)
711 sjinfo->ojrelid, subst);
712
713 /*
714 * We don't expect any EC child members to exist at this point. Ensure
715 * that's the case, otherwise, we might be getting asked to do something
716 * this function hasn't been coded for.
717 */
718 Assert(ec->ec_childmembers == NULL);
719
720 /*
721 * Fix up the member expressions. Any non-const member that ends with
722 * empty em_relids must be a Var or PHV of the removed relation. We don't
723 * need it anymore, so we can drop it.
724 */
725 foreach(lc, ec->ec_members)
726 {
728
729 if (bms_is_member(relid, cur_em->em_relids) ||
730 (sjinfo != NULL && bms_is_member(sjinfo->ojrelid,
731 cur_em->em_relids)))
732 {
733 Assert(!cur_em->em_is_const);
734 cur_em->em_relids = adjust_relid_set(cur_em->em_relids, relid, subst);
735 if (sjinfo != NULL)
736 cur_em->em_relids = adjust_relid_set(cur_em->em_relids,
737 sjinfo->ojrelid, subst);
738 if (bms_is_empty(cur_em->em_relids))
740 }
741 }
742
743 /* Fix up the source clauses, in case we can re-use them later */
744 foreach(lc, ec->ec_sources)
745 {
746 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
747
748 if (sjinfo == NULL)
749 ChangeVarNodes((Node *) rinfo, relid, subst, 0);
750 else
751 remove_rel_from_restrictinfo(rinfo, relid, sjinfo->ojrelid);
752 }
753
754 /*
755 * Rather than expend code on fixing up any already-derived clauses, just
756 * drop them. (At this point, any such clauses would be base restriction
757 * clauses, which we'd not need anymore anyway.)
758 */
760}
void ec_clear_derived_clauses(EquivalenceClass *ec)
Definition: equivclass.c:3831
Relids adjust_relid_set(Relids relids, int oldrelid, int newrelid)
Definition: rewriteManip.c:808
List ** ec_childmembers
Definition: pathnodes.h:1454

References adjust_relid_set(), Assert(), bms_is_empty, bms_is_member(), ChangeVarNodes(), EquivalenceClass::ec_childmembers, ec_clear_derived_clauses(), EquivalenceClass::ec_members, EquivalenceClass::ec_relids, EquivalenceClass::ec_sources, EquivalenceMember::em_is_const, EquivalenceMember::em_relids, foreach_delete_current, lfirst, SpecialJoinInfo::ojrelid, and remove_rel_from_restrictinfo().

Referenced by remove_rel_from_query().

◆ remove_rel_from_joinlist()

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

Definition at line 772 of file analyzejoins.c.

773{
774 List *result = NIL;
775 ListCell *jl;
776
777 foreach(jl, joinlist)
778 {
779 Node *jlnode = (Node *) lfirst(jl);
780
781 if (IsA(jlnode, RangeTblRef))
782 {
783 int varno = ((RangeTblRef *) jlnode)->rtindex;
784
785 if (varno == relid)
786 (*nremoved)++;
787 else
788 result = lappend(result, jlnode);
789 }
790 else if (IsA(jlnode, List))
791 {
792 /* Recurse to handle subproblem */
793 List *sublist;
794
795 sublist = remove_rel_from_joinlist((List *) jlnode,
796 relid, nremoved);
797 /* Avoid including empty sub-lists in the result */
798 if (sublist)
799 result = lappend(result, sublist);
800 }
801 else
802 {
803 elog(ERROR, "unrecognized joinlist node type: %d",
804 (int) nodeTag(jlnode));
805 }
806 }
807
808 return result;
809}
static List * remove_rel_from_joinlist(List *joinlist, int relid, int *nremoved)
Definition: analyzejoins.c:772
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define nodeTag(nodeptr)
Definition: nodes.h:139

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

Referenced by remove_rel_from_joinlist(), remove_useless_joins(), and remove_useless_self_joins().

◆ remove_rel_from_query()

static void remove_rel_from_query ( PlannerInfo root,
RelOptInfo rel,
int  subst,
SpecialJoinInfo sjinfo,
Relids  joinrelids 
)
static

Definition at line 323 of file analyzejoins.c.

326{
327 int relid = rel->relid;
328 Index rti;
329 ListCell *l;
330
331 /*
332 * Update all_baserels and related relid sets.
333 */
334 root->all_baserels = adjust_relid_set(root->all_baserels, relid, subst);
335 root->all_query_rels = adjust_relid_set(root->all_query_rels, relid, subst);
336
337 if (sjinfo != NULL)
338 {
339 root->outer_join_rels = bms_del_member(root->outer_join_rels,
340 sjinfo->ojrelid);
341 root->all_query_rels = bms_del_member(root->all_query_rels,
342 sjinfo->ojrelid);
343 }
344
345 /*
346 * Likewise remove references from SpecialJoinInfo data structures.
347 *
348 * This is relevant in case the outer join we're deleting is nested inside
349 * other outer joins: the upper joins' relid sets have to be adjusted. The
350 * RHS of the target outer join will be made empty here, but that's OK
351 * since caller will delete that SpecialJoinInfo entirely.
352 */
353 foreach(l, root->join_info_list)
354 {
355 SpecialJoinInfo *sjinf = (SpecialJoinInfo *) lfirst(l);
356
357 /*
358 * initsplan.c is fairly cavalier about allowing SpecialJoinInfos'
359 * lefthand/righthand relid sets to be shared with other data
360 * structures. Ensure that we don't modify the original relid sets.
361 * (The commute_xxx sets are always per-SpecialJoinInfo though.)
362 */
363 sjinf->min_lefthand = bms_copy(sjinf->min_lefthand);
364 sjinf->min_righthand = bms_copy(sjinf->min_righthand);
365 sjinf->syn_lefthand = bms_copy(sjinf->syn_lefthand);
366 sjinf->syn_righthand = bms_copy(sjinf->syn_righthand);
367 /* Now remove relid from the sets: */
368 sjinf->min_lefthand = adjust_relid_set(sjinf->min_lefthand, relid, subst);
369 sjinf->min_righthand = adjust_relid_set(sjinf->min_righthand, relid, subst);
370 sjinf->syn_lefthand = adjust_relid_set(sjinf->syn_lefthand, relid, subst);
371 sjinf->syn_righthand = adjust_relid_set(sjinf->syn_righthand, relid, subst);
372
373 if (sjinfo != NULL)
374 {
375 Assert(subst <= 0);
376
377 /* Remove sjinfo->ojrelid bits from the sets: */
379 sjinfo->ojrelid);
381 sjinfo->ojrelid);
383 sjinfo->ojrelid);
385 sjinfo->ojrelid);
386 /* relid cannot appear in these fields, but ojrelid can: */
388 sjinfo->ojrelid);
390 sjinfo->ojrelid);
392 sjinfo->ojrelid);
394 sjinfo->ojrelid);
395 }
396 else
397 {
398 Assert(subst > 0);
399
400 ChangeVarNodes((Node *) sjinf->semi_rhs_exprs, relid, subst, 0);
401 }
402 }
403
404 /*
405 * Likewise remove references from PlaceHolderVar data structures,
406 * removing any no-longer-needed placeholders entirely.
407 *
408 * Removal is a bit trickier than it might seem: we can remove PHVs that
409 * are used at the target rel and/or in the join qual, but not those that
410 * are used at join partner rels or above the join. It's not that easy to
411 * distinguish PHVs used at partner rels from those used in the join qual,
412 * since they will both have ph_needed sets that are subsets of
413 * joinrelids. However, a PHV used at a partner rel could not have the
414 * target rel in ph_eval_at, so we check that while deciding whether to
415 * remove or just update the PHV. There is no corresponding test in
416 * join_is_removable because it doesn't need to distinguish those cases.
417 */
418 foreach(l, root->placeholder_list)
419 {
420 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
421
422 Assert(sjinfo == NULL || !bms_is_member(relid, phinfo->ph_lateral));
423 if (bms_is_subset(phinfo->ph_needed, joinrelids) &&
424 bms_is_member(relid, phinfo->ph_eval_at) &&
425 (sjinfo == NULL || !bms_is_member(sjinfo->ojrelid, phinfo->ph_eval_at)))
426 {
427 root->placeholder_list = foreach_delete_current(root->placeholder_list,
428 l);
429 root->placeholder_array[phinfo->phid] = NULL;
430 }
431 else
432 {
433 PlaceHolderVar *phv = phinfo->ph_var;
434
435 phinfo->ph_eval_at = adjust_relid_set(phinfo->ph_eval_at, relid, subst);
436 if (sjinfo != NULL)
437 phinfo->ph_eval_at = adjust_relid_set(phinfo->ph_eval_at,
438 sjinfo->ojrelid, subst);
439 Assert(!bms_is_empty(phinfo->ph_eval_at)); /* checked previously */
440 /* Reduce ph_needed to contain only "relation 0"; see below */
441 if (bms_is_member(0, phinfo->ph_needed))
442 phinfo->ph_needed = bms_make_singleton(0);
443 else
444 phinfo->ph_needed = NULL;
445
446 phinfo->ph_lateral = adjust_relid_set(phinfo->ph_lateral, relid, subst);
447
448 /*
449 * ph_lateral might contain rels mentioned in ph_eval_at after the
450 * replacement, remove them.
451 */
452 phinfo->ph_lateral = bms_difference(phinfo->ph_lateral, phinfo->ph_eval_at);
453 /* ph_lateral might or might not be empty */
454
455 phv->phrels = adjust_relid_set(phv->phrels, relid, subst);
456 if (sjinfo != NULL)
457 phv->phrels = adjust_relid_set(phv->phrels,
458 sjinfo->ojrelid, subst);
459 Assert(!bms_is_empty(phv->phrels));
460
461 ChangeVarNodes((Node *) phv->phexpr, relid, subst, 0);
462
463 Assert(phv->phnullingrels == NULL); /* no need to adjust */
464 }
465 }
466
467 /*
468 * Likewise remove references from EquivalenceClasses.
469 */
470 foreach(l, root->eq_classes)
471 {
473
474 if (bms_is_member(relid, ec->ec_relids) ||
475 (sjinfo == NULL || bms_is_member(sjinfo->ojrelid, ec->ec_relids)))
476 remove_rel_from_eclass(ec, sjinfo, relid, subst);
477 }
478
479 /*
480 * Finally, we must recompute per-Var attr_needed and per-PlaceHolderVar
481 * ph_needed relid sets. These have to be known accurately, else we may
482 * fail to remove other now-removable outer joins. And our removal of the
483 * join clause(s) for this outer join may mean that Vars that were
484 * formerly needed no longer are. So we have to do this honestly by
485 * repeating the construction of those relid sets. We can cheat to one
486 * small extent: we can avoid re-examining the targetlist and HAVING qual
487 * by preserving "relation 0" bits from the existing relid sets. This is
488 * safe because we'd never remove such references.
489 *
490 * So, start by removing all other bits from attr_needed sets and
491 * lateral_vars lists. (We already did this above for ph_needed.)
492 */
493 for (rti = 1; rti < root->simple_rel_array_size; rti++)
494 {
495 RelOptInfo *otherrel = root->simple_rel_array[rti];
496 int attroff;
497
498 /* there may be empty slots corresponding to non-baserel RTEs */
499 if (otherrel == NULL)
500 continue;
501
502 Assert(otherrel->relid == rti); /* sanity check on array */
503
504 for (attroff = otherrel->max_attr - otherrel->min_attr;
505 attroff >= 0;
506 attroff--)
507 {
508 if (bms_is_member(0, otherrel->attr_needed[attroff]))
509 otherrel->attr_needed[attroff] = bms_make_singleton(0);
510 else
511 otherrel->attr_needed[attroff] = NULL;
512 }
513
514 if (subst > 0)
515 ChangeVarNodes((Node *) otherrel->lateral_vars, relid, subst, 0);
516 }
517}
static void remove_rel_from_eclass(EquivalenceClass *ec, SpecialJoinInfo *sjinfo, int relid, int subst)
Definition: analyzejoins.c:702
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:346
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:216
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:868
Relids phnullingrels
Definition: pathnodes.h:2927
List * lateral_vars
Definition: pathnodes.h:967
Relids syn_lefthand
Definition: pathnodes.h:3032
List * semi_rhs_exprs
Definition: pathnodes.h:3045
Relids commute_above_l
Definition: pathnodes.h:3036
Relids syn_righthand
Definition: pathnodes.h:3033
Relids commute_below_r
Definition: pathnodes.h:3039

References adjust_relid_set(), Assert(), bms_copy(), bms_del_member(), bms_difference(), bms_is_empty, bms_is_member(), bms_is_subset(), bms_make_singleton(), ChangeVarNodes(), SpecialJoinInfo::commute_above_l, SpecialJoinInfo::commute_above_r, SpecialJoinInfo::commute_below_l, SpecialJoinInfo::commute_below_r, EquivalenceClass::ec_relids, foreach_delete_current, RelOptInfo::lateral_vars, lfirst, RelOptInfo::max_attr, RelOptInfo::min_attr, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, SpecialJoinInfo::ojrelid, PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_lateral, PlaceHolderInfo::ph_needed, PlaceHolderInfo::ph_var, PlaceHolderInfo::phid, PlaceHolderVar::phnullingrels, RelOptInfo::relid, remove_rel_from_eclass(), root, SpecialJoinInfo::semi_rhs_exprs, SpecialJoinInfo::syn_lefthand, and SpecialJoinInfo::syn_righthand.

Referenced by remove_leftjoinrel_from_query(), and remove_self_join_rel().

◆ remove_rel_from_restrictinfo()

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

Definition at line 641 of file analyzejoins.c.

642{
643 /*
644 * initsplan.c is fairly cavalier about allowing RestrictInfos to share
645 * relid sets with other RestrictInfos, and SpecialJoinInfos too. Make
646 * sure this RestrictInfo has its own relid sets before we modify them.
647 * (In present usage, clause_relids is probably not shared, but
648 * required_relids could be; let's not assume anything.)
649 */
650 rinfo->clause_relids = bms_copy(rinfo->clause_relids);
651 rinfo->clause_relids = bms_del_member(rinfo->clause_relids, relid);
652 rinfo->clause_relids = bms_del_member(rinfo->clause_relids, ojrelid);
653 /* Likewise for required_relids */
655 rinfo->required_relids = bms_del_member(rinfo->required_relids, relid);
656 rinfo->required_relids = bms_del_member(rinfo->required_relids, ojrelid);
657
658 /* If it's an OR, recurse to clean up sub-clauses */
659 if (restriction_is_or_clause(rinfo))
660 {
661 ListCell *lc;
662
663 Assert(is_orclause(rinfo->orclause));
664 foreach(lc, ((BoolExpr *) rinfo->orclause)->args)
665 {
666 Node *orarg = (Node *) lfirst(lc);
667
668 /* OR arguments should be ANDs or sub-RestrictInfos */
669 if (is_andclause(orarg))
670 {
671 List *andargs = ((BoolExpr *) orarg)->args;
672 ListCell *lc2;
673
674 foreach(lc2, andargs)
675 {
676 RestrictInfo *rinfo2 = lfirst_node(RestrictInfo, lc2);
677
678 remove_rel_from_restrictinfo(rinfo2, relid, ojrelid);
679 }
680 }
681 else
682 {
683 RestrictInfo *rinfo2 = castNode(RestrictInfo, orarg);
684
685 remove_rel_from_restrictinfo(rinfo2, relid, ojrelid);
686 }
687 }
688 }
689}
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:107
static bool is_orclause(const void *clause)
Definition: nodeFuncs.h:116
bool restriction_is_or_clause(RestrictInfo *restrictinfo)
Definition: restrictinfo.c:407

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

Referenced by remove_leftjoinrel_from_query(), remove_rel_from_eclass(), and remove_rel_from_restrictinfo().

◆ remove_self_join_rel()

static void remove_self_join_rel ( PlannerInfo root,
PlanRowMark kmark,
PlanRowMark rmark,
RelOptInfo toKeep,
RelOptInfo toRemove,
List restrictlist 
)
static

Definition at line 1694 of file analyzejoins.c.

1697{
1698 List *joininfos;
1699 ListCell *lc;
1700 int i;
1701 List *jinfo_candidates = NIL;
1702 List *binfo_candidates = NIL;
1703
1704 Assert(toKeep->relid > 0);
1705 Assert(toRemove->relid > 0);
1706
1707 /*
1708 * Replace the index of the removing table with the keeping one. The
1709 * technique of removing/distributing restrictinfo is used here to attach
1710 * just appeared (for keeping relation) join clauses and avoid adding
1711 * duplicates of those that already exist in the joininfo list.
1712 */
1713 joininfos = list_copy(toRemove->joininfo);
1714 foreach_node(RestrictInfo, rinfo, joininfos)
1715 {
1716 remove_join_clause_from_rels(root, rinfo, rinfo->required_relids);
1717 ChangeVarNodes((Node *) rinfo, toRemove->relid, toKeep->relid, 0);
1718
1719 if (bms_membership(rinfo->required_relids) == BMS_MULTIPLE)
1720 jinfo_candidates = lappend(jinfo_candidates, rinfo);
1721 else
1722 binfo_candidates = lappend(binfo_candidates, rinfo);
1723 }
1724
1725 /*
1726 * Concatenate restrictlist to the list of base restrictions of the
1727 * removing table just to simplify the replacement procedure: all of them
1728 * weren't connected to any keeping relations and need to be added to some
1729 * rels.
1730 */
1731 toRemove->baserestrictinfo = list_concat(toRemove->baserestrictinfo,
1732 restrictlist);
1733 foreach_node(RestrictInfo, rinfo, toRemove->baserestrictinfo)
1734 {
1735 ChangeVarNodes((Node *) rinfo, toRemove->relid, toKeep->relid, 0);
1736
1737 if (bms_membership(rinfo->required_relids) == BMS_MULTIPLE)
1738 jinfo_candidates = lappend(jinfo_candidates, rinfo);
1739 else
1740 binfo_candidates = lappend(binfo_candidates, rinfo);
1741 }
1742
1743 /*
1744 * Now, add all non-redundant clauses to the keeping relation.
1745 */
1746 add_non_redundant_clauses(root, binfo_candidates,
1747 &toKeep->baserestrictinfo, toRemove->relid);
1748 add_non_redundant_clauses(root, jinfo_candidates,
1749 &toKeep->joininfo, toRemove->relid);
1750
1751 list_free(binfo_candidates);
1752 list_free(jinfo_candidates);
1753
1754 /*
1755 * Arrange equivalence classes, mentioned removing a table, with the
1756 * keeping one: varno of removing table should be replaced in members and
1757 * sources lists. Also, remove duplicated elements if this replacement
1758 * procedure created them.
1759 */
1760 i = -1;
1761 while ((i = bms_next_member(toRemove->eclass_indexes, i)) >= 0)
1762 {
1763 EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
1764
1765 update_eclasses(ec, toRemove->relid, toKeep->relid);
1766 toKeep->eclass_indexes = bms_add_member(toKeep->eclass_indexes, i);
1767 }
1768
1769 /*
1770 * Transfer the targetlist and attr_needed flags.
1771 */
1772
1773 foreach(lc, toRemove->reltarget->exprs)
1774 {
1775 Node *node = lfirst(lc);
1776
1777 ChangeVarNodes(node, toRemove->relid, toKeep->relid, 0);
1778 if (!list_member(toKeep->reltarget->exprs, node))
1779 toKeep->reltarget->exprs = lappend(toKeep->reltarget->exprs, node);
1780 }
1781
1782 for (i = toKeep->min_attr; i <= toKeep->max_attr; i++)
1783 {
1784 int attno = i - toKeep->min_attr;
1785
1786 toRemove->attr_needed[attno] = adjust_relid_set(toRemove->attr_needed[attno],
1787 toRemove->relid, toKeep->relid);
1788 toKeep->attr_needed[attno] = bms_add_members(toKeep->attr_needed[attno],
1789 toRemove->attr_needed[attno]);
1790 }
1791
1792 /*
1793 * If the removed relation has a row mark, transfer it to the remaining
1794 * one.
1795 *
1796 * If both rels have row marks, just keep the one corresponding to the
1797 * remaining relation because we verified earlier that they have the same
1798 * strength.
1799 */
1800 if (rmark)
1801 {
1802 if (kmark)
1803 {
1804 Assert(kmark->markType == rmark->markType);
1805
1806 root->rowMarks = list_delete_ptr(root->rowMarks, rmark);
1807 }
1808 else
1809 {
1810 /* Shouldn't have inheritance children here. */
1811 Assert(rmark->rti == rmark->prti);
1812
1813 rmark->rti = rmark->prti = toKeep->relid;
1814 }
1815 }
1816
1817 /*
1818 * Replace varno in all the query structures, except nodes RangeTblRef
1819 * otherwise later remove_rel_from_joinlist will yield errors.
1820 */
1821 ChangeVarNodesExtended((Node *) root->parse, toRemove->relid, toKeep->relid, 0, false);
1822
1823 /* Replace links in the planner info */
1824 remove_rel_from_query(root, toRemove, toKeep->relid, NULL, NULL);
1825
1826 /* At last, replace varno in root targetlist and HAVING clause */
1827 ChangeVarNodes((Node *) root->processed_tlist, toRemove->relid, toKeep->relid, 0);
1828 ChangeVarNodes((Node *) root->processed_groupClause, toRemove->relid, toKeep->relid, 0);
1829
1830 adjust_relid_set(root->all_result_relids, toRemove->relid, toKeep->relid);
1831 adjust_relid_set(root->leaf_result_relids, toRemove->relid, toKeep->relid);
1832
1833 /*
1834 * There may be references to the rel in root->fkey_list, but if so,
1835 * match_foreign_keys_to_quals() will get rid of them.
1836 */
1837
1838 /*
1839 * Finally, remove the rel from the baserel array to prevent it from being
1840 * referenced again. (We can't do this earlier because
1841 * remove_join_clause_from_rels will touch it.)
1842 */
1843 root->simple_rel_array[toRemove->relid] = NULL;
1844
1845 /* And nuke the RelOptInfo, just in case there's another access path. */
1846 pfree(toRemove);
1847
1848 /*
1849 * Now repeat construction of attr_needed bits coming from all other
1850 * sources.
1851 */
1856}
static void add_non_redundant_clauses(PlannerInfo *root, List *rinfo_candidates, List **keep_rinfo_list, Index removed_relid)
static void update_eclasses(EquivalenceClass *ec, int from, int to)
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:781
@ BMS_MULTIPLE
Definition: bitmapset.h:73
int i
Definition: isn.c:77
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:872
void list_free(List *list)
Definition: list.c:1546
bool list_member(const List *list, const void *datum)
Definition: list.c:661
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
void ChangeVarNodesExtended(Node *node, int rt_index, int new_index, int sublevels_up, bool change_RangeTblRef)
Definition: rewriteManip.c:751
List * exprs
Definition: pathnodes.h:1669
Index prti
Definition: plannodes.h:1542
RowMarkType markType
Definition: plannodes.h:1546
struct PathTarget * reltarget
Definition: pathnodes.h:920
Bitmapset * eclass_indexes
Definition: pathnodes.h:979

References add_non_redundant_clauses(), adjust_relid_set(), Assert(), RelOptInfo::baserestrictinfo, bms_add_member(), bms_add_members(), bms_membership(), BMS_MULTIPLE, bms_next_member(), ChangeVarNodes(), ChangeVarNodesExtended(), RelOptInfo::eclass_indexes, PathTarget::exprs, foreach_node, i, RelOptInfo::joininfo, lappend(), lfirst, list_concat(), list_copy(), list_delete_ptr(), list_free(), list_member(), list_nth(), PlanRowMark::markType, RelOptInfo::min_attr, NIL, pfree(), PlanRowMark::prti, rebuild_eclass_attr_needed(), rebuild_joinclause_attr_needed(), rebuild_lateral_attr_needed(), rebuild_placeholder_attr_needed(), RelOptInfo::relid, RelOptInfo::reltarget, remove_join_clause_from_rels(), remove_rel_from_query(), root, PlanRowMark::rti, and update_eclasses().

Referenced by remove_self_joins_one_group().

◆ remove_self_joins_one_group()

static Relids remove_self_joins_one_group ( PlannerInfo root,
Relids  relids 
)
static

Definition at line 1996 of file analyzejoins.c.

1997{
1998 Relids result = NULL;
1999 int k; /* Index of kept relation */
2000 int r = -1; /* Index of removed relation */
2001
2002 while ((r = bms_next_member(relids, r)) > 0)
2003 {
2004 RelOptInfo *inner = root->simple_rel_array[r];
2005
2006 k = r;
2007
2008 while ((k = bms_next_member(relids, k)) > 0)
2009 {
2010 Relids joinrelids = NULL;
2011 RelOptInfo *outer = root->simple_rel_array[k];
2012 List *restrictlist;
2013 List *selfjoinquals;
2014 List *otherjoinquals;
2015 ListCell *lc;
2016 bool jinfo_check = true;
2017 PlanRowMark *omark = NULL;
2018 PlanRowMark *imark = NULL;
2019 List *uclauses = NIL;
2020
2021 /* A sanity check: the relations have the same Oid. */
2022 Assert(root->simple_rte_array[k]->relid ==
2023 root->simple_rte_array[r]->relid);
2024
2025 /*
2026 * It is impossible to eliminate the join of two relations if they
2027 * belong to different rules of order. Otherwise, the planner
2028 * can't find any variants of the correct query plan.
2029 */
2030 foreach(lc, root->join_info_list)
2031 {
2032 SpecialJoinInfo *info = (SpecialJoinInfo *) lfirst(lc);
2033
2034 if ((bms_is_member(k, info->syn_lefthand) ^
2035 bms_is_member(r, info->syn_lefthand)) ||
2036 (bms_is_member(k, info->syn_righthand) ^
2037 bms_is_member(r, info->syn_righthand)))
2038 {
2039 jinfo_check = false;
2040 break;
2041 }
2042 }
2043 if (!jinfo_check)
2044 continue;
2045
2046 /*
2047 * Check Row Marks equivalence. We can't remove the join if the
2048 * relations have row marks of different strength (e.g., one is
2049 * locked FOR UPDATE, and another just has ROW_MARK_REFERENCE for
2050 * EvalPlanQual rechecking).
2051 */
2052 foreach(lc, root->rowMarks)
2053 {
2054 PlanRowMark *rowMark = (PlanRowMark *) lfirst(lc);
2055
2056 if (rowMark->rti == k)
2057 {
2058 Assert(imark == NULL);
2059 imark = rowMark;
2060 }
2061 else if (rowMark->rti == r)
2062 {
2063 Assert(omark == NULL);
2064 omark = rowMark;
2065 }
2066
2067 if (omark && imark)
2068 break;
2069 }
2070 if (omark && imark && omark->markType != imark->markType)
2071 continue;
2072
2073 /*
2074 * We only deal with base rels here, so their relids bitset
2075 * contains only one member -- their relid.
2076 */
2077 joinrelids = bms_add_member(joinrelids, r);
2078 joinrelids = bms_add_member(joinrelids, k);
2079
2080 /*
2081 * PHVs should not impose any constraints on removing self-joins.
2082 */
2083
2084 /*
2085 * At this stage, joininfo lists of inner and outer can contain
2086 * only clauses required for a superior outer join that can't
2087 * influence this optimization. So, we can avoid to call the
2088 * build_joinrel_restrictlist() routine.
2089 */
2090 restrictlist = generate_join_implied_equalities(root, joinrelids,
2091 inner->relids,
2092 outer, NULL);
2093 if (restrictlist == NIL)
2094 continue;
2095
2096 /*
2097 * Process restrictlist to separate the self-join quals from the
2098 * other quals. e.g., "x = x" goes to selfjoinquals and "a = b" to
2099 * otherjoinquals.
2100 */
2101 split_selfjoin_quals(root, restrictlist, &selfjoinquals,
2102 &otherjoinquals, inner->relid, outer->relid);
2103
2104 Assert(list_length(restrictlist) ==
2105 (list_length(selfjoinquals) + list_length(otherjoinquals)));
2106
2107 /*
2108 * To enable SJE for the only degenerate case without any self
2109 * join clauses at all, add baserestrictinfo to this list. The
2110 * degenerate case works only if both sides have the same clause.
2111 * So doesn't matter which side to add.
2112 */
2113 selfjoinquals = list_concat(selfjoinquals, outer->baserestrictinfo);
2114
2115 /*
2116 * Determine if the inner table can duplicate outer rows. We must
2117 * bypass the unique rel cache here since we're possibly using a
2118 * subset of join quals. We can use 'force_cache' == true when all
2119 * join quals are self-join quals. Otherwise, we could end up
2120 * putting false negatives in the cache.
2121 */
2122 if (!innerrel_is_unique_ext(root, joinrelids, inner->relids,
2123 outer, JOIN_INNER, selfjoinquals,
2124 list_length(otherjoinquals) == 0,
2125 &uclauses))
2126 continue;
2127
2128 /*
2129 * 'uclauses' is the copy of outer->baserestrictinfo that are
2130 * associated with an index. We proved by matching selfjoinquals
2131 * to a unique index that the outer relation has at most one
2132 * matching row for each inner row. Sometimes that is not enough.
2133 * e.g. "WHERE s1.b = s2.b AND s1.a = 1 AND s2.a = 2" when the
2134 * unique index is (a,b). Having non-empty uclauses, we must
2135 * validate that the inner baserestrictinfo contains the same
2136 * expressions, or we won't match the same row on each side of the
2137 * join.
2138 */
2139 if (!match_unique_clauses(root, inner, uclauses, outer->relid))
2140 continue;
2141
2142 /*
2143 * We can remove either relation, so remove the inner one in order
2144 * to simplify this loop.
2145 */
2146 remove_self_join_rel(root, omark, imark, outer, inner, restrictlist);
2147
2148 result = bms_add_member(result, r);
2149
2150 /* We have removed the outer relation, try the next one. */
2151 break;
2152 }
2153 }
2154
2155 return result;
2156}
static bool match_unique_clauses(PlannerInfo *root, RelOptInfo *outer, List *uclauses, Index relid)
static void split_selfjoin_quals(PlannerInfo *root, List *joinquals, List **selfjoinquals, List **otherjoinquals, int from, int to)
static void remove_self_join_rel(PlannerInfo *root, PlanRowMark *kmark, PlanRowMark *rmark, RelOptInfo *toKeep, RelOptInfo *toRemove, List *restrictlist)
@ JOIN_INNER
Definition: nodes.h:299

References Assert(), RelOptInfo::baserestrictinfo, bms_add_member(), bms_is_member(), bms_next_member(), generate_join_implied_equalities(), innerrel_is_unique_ext(), JOIN_INNER, lfirst, list_concat(), list_length(), PlanRowMark::markType, match_unique_clauses(), NIL, RelOptInfo::relid, RelOptInfo::relids, remove_self_join_rel(), root, PlanRowMark::rti, split_selfjoin_quals(), SpecialJoinInfo::syn_lefthand, and SpecialJoinInfo::syn_righthand.

Referenced by remove_self_joins_recurse().

◆ remove_self_joins_recurse()

static Relids remove_self_joins_recurse ( PlannerInfo root,
List joinlist,
Relids  toRemove 
)
static

Definition at line 2163 of file analyzejoins.c.

2164{
2165 ListCell *jl;
2166 Relids relids = NULL;
2167 SelfJoinCandidate *candidates = NULL;
2168 int i;
2169 int j;
2170 int numRels;
2171
2172 /* Collect indexes of base relations of the join tree */
2173 foreach(jl, joinlist)
2174 {
2175 Node *jlnode = (Node *) lfirst(jl);
2176
2177 if (IsA(jlnode, RangeTblRef))
2178 {
2179 int varno = ((RangeTblRef *) jlnode)->rtindex;
2180 RangeTblEntry *rte = root->simple_rte_array[varno];
2181
2182 /*
2183 * We only consider ordinary relations as candidates to be
2184 * removed, and these relations should not have TABLESAMPLE
2185 * clauses specified. Removing a relation with TABLESAMPLE clause
2186 * could potentially change the syntax of the query. Because of
2187 * UPDATE/DELETE EPQ mechanism, currently Query->resultRelation or
2188 * Query->mergeTargetRelation associated rel cannot be eliminated.
2189 */
2190 if (rte->rtekind == RTE_RELATION &&
2191 rte->relkind == RELKIND_RELATION &&
2192 rte->tablesample == NULL &&
2193 varno != root->parse->resultRelation &&
2194 varno != root->parse->mergeTargetRelation)
2195 {
2196 Assert(!bms_is_member(varno, relids));
2197 relids = bms_add_member(relids, varno);
2198 }
2199 }
2200 else if (IsA(jlnode, List))
2201 {
2202 /* Recursively go inside the sub-joinlist */
2203 toRemove = remove_self_joins_recurse(root, (List *) jlnode,
2204 toRemove);
2205 }
2206 else
2207 elog(ERROR, "unrecognized joinlist node type: %d",
2208 (int) nodeTag(jlnode));
2209 }
2210
2211 numRels = bms_num_members(relids);
2212
2213 /* Need at least two relations for the join */
2214 if (numRels < 2)
2215 return toRemove;
2216
2217 /*
2218 * In order to find relations with the same oid we first build an array of
2219 * candidates and then sort it by oid.
2220 */
2221 candidates = (SelfJoinCandidate *) palloc(sizeof(SelfJoinCandidate) *
2222 numRels);
2223 i = -1;
2224 j = 0;
2225 while ((i = bms_next_member(relids, i)) >= 0)
2226 {
2227 candidates[j].relid = i;
2228 candidates[j].reloid = root->simple_rte_array[i]->relid;
2229 j++;
2230 }
2231
2232 qsort(candidates, numRels, sizeof(SelfJoinCandidate),
2234
2235 /*
2236 * Iteratively form a group of relation indexes with the same oid and
2237 * launch the routine that detects self-joins in this group and removes
2238 * excessive range table entries.
2239 *
2240 * At the end of the iteration, exclude the group from the overall relids
2241 * list. So each next iteration of the cycle will involve less and less
2242 * value of relids.
2243 */
2244 i = 0;
2245 for (j = 1; j < numRels + 1; j++)
2246 {
2247 if (j == numRels || candidates[j].reloid != candidates[i].reloid)
2248 {
2249 if (j - i >= 2)
2250 {
2251 /* Create a group of relation indexes with the same oid */
2252 Relids group = NULL;
2253 Relids removed;
2254
2255 while (i < j)
2256 {
2257 group = bms_add_member(group, candidates[i].relid);
2258 i++;
2259 }
2260 relids = bms_del_members(relids, group);
2261
2262 /*
2263 * Try to remove self-joins from a group of identical entries.
2264 * Make the next attempt iteratively - if something is deleted
2265 * from a group, changes in clauses and equivalence classes
2266 * can give us a chance to find more candidates.
2267 */
2268 do
2269 {
2270 Assert(!bms_overlap(group, toRemove));
2271 removed = remove_self_joins_one_group(root, group);
2272 toRemove = bms_add_members(toRemove, removed);
2273 group = bms_del_members(group, removed);
2274 } while (!bms_is_empty(removed) &&
2275 bms_membership(group) == BMS_MULTIPLE);
2276 bms_free(removed);
2277 bms_free(group);
2278 }
2279 else
2280 {
2281 /* Single relation, just remove it from the set */
2282 relids = bms_del_member(relids, candidates[i].relid);
2283 i = j;
2284 }
2285 }
2286 }
2287
2288 Assert(bms_is_empty(relids));
2289
2290 return toRemove;
2291}
static int self_join_candidates_cmp(const void *a, const void *b)
static Relids remove_self_joins_one_group(PlannerInfo *root, Relids relids)
static Relids remove_self_joins_recurse(PlannerInfo *root, List *joinlist, Relids toRemove)
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:1161
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:751
int j
Definition: isn.c:78
void * palloc(Size size)
Definition: mcxt.c:1939
#define qsort(a, b, c, d)
Definition: port.h:479
struct TableSampleClause * tablesample
Definition: parsenodes.h:1112
RTEKind rtekind
Definition: parsenodes.h:1061

References Assert(), bms_add_member(), bms_add_members(), bms_del_member(), bms_del_members(), bms_free(), bms_is_empty, bms_is_member(), bms_membership(), BMS_MULTIPLE, bms_next_member(), bms_num_members(), bms_overlap(), elog, ERROR, i, IsA, j, lfirst, nodeTag, palloc(), qsort, SelfJoinCandidate::relid, SelfJoinCandidate::reloid, remove_self_joins_one_group(), remove_self_joins_recurse(), root, RTE_RELATION, RangeTblEntry::rtekind, self_join_candidates_cmp(), and RangeTblEntry::tablesample.

Referenced by remove_self_joins_recurse(), and remove_useless_self_joins().

◆ remove_useless_joins()

List * remove_useless_joins ( PlannerInfo root,
List joinlist 
)

Definition at line 88 of file analyzejoins.c.

89{
90 ListCell *lc;
91
92 /*
93 * We are only interested in relations that are left-joined to, so we can
94 * scan the join_info_list to find them easily.
95 */
96restart:
97 foreach(lc, root->join_info_list)
98 {
99 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
100 int innerrelid;
101 int nremoved;
102
103 /* Skip if not removable */
104 if (!join_is_removable(root, sjinfo))
105 continue;
106
107 /*
108 * Currently, join_is_removable can only succeed when the sjinfo's
109 * righthand is a single baserel. Remove that rel from the query and
110 * joinlist.
111 */
112 innerrelid = bms_singleton_member(sjinfo->min_righthand);
113
114 remove_leftjoinrel_from_query(root, innerrelid, sjinfo);
115
116 /* We verify that exactly one reference gets removed from joinlist */
117 nremoved = 0;
118 joinlist = remove_rel_from_joinlist(joinlist, innerrelid, &nremoved);
119 if (nremoved != 1)
120 elog(ERROR, "failed to find relation %d in joinlist", innerrelid);
121
122 /*
123 * We can delete this SpecialJoinInfo from the list too, since it's no
124 * longer of interest. (Since we'll restart the foreach loop
125 * immediately, we don't bother with foreach_delete_current.)
126 */
127 root->join_info_list = list_delete_cell(root->join_info_list, lc);
128
129 /*
130 * Restart the scan. This is necessary to ensure we find all
131 * removable joins independently of ordering of the join_info_list
132 * (note that removal of attr_needed bits may make a join appear
133 * removable that did not before).
134 */
135 goto restart;
136 }
137
138 return joinlist;
139}
static void remove_leftjoinrel_from_query(PlannerInfo *root, int relid, SpecialJoinInfo *sjinfo)
Definition: analyzejoins.c:528
static bool join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
Definition: analyzejoins.c:153
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:672
List * list_delete_cell(List *list, ListCell *cell)
Definition: list.c:841

References bms_singleton_member(), elog, ERROR, join_is_removable(), lfirst, list_delete_cell(), SpecialJoinInfo::min_righthand, remove_leftjoinrel_from_query(), remove_rel_from_joinlist(), and root.

Referenced by query_planner().

◆ remove_useless_self_joins()

List * remove_useless_self_joins ( PlannerInfo root,
List joinlist 
)

Definition at line 2344 of file analyzejoins.c.

2345{
2346 Relids toRemove = NULL;
2347 int relid = -1;
2348
2349 if (!enable_self_join_elimination || joinlist == NIL ||
2350 (list_length(joinlist) == 1 && !IsA(linitial(joinlist), List)))
2351 return joinlist;
2352
2353 /*
2354 * Merge pairs of relations participated in self-join. Remove unnecessary
2355 * range table entries.
2356 */
2357 toRemove = remove_self_joins_recurse(root, joinlist, toRemove);
2358
2359 if (unlikely(toRemove != NULL))
2360 {
2361 /* At the end, remove orphaned relation links */
2362 while ((relid = bms_next_member(toRemove, relid)) >= 0)
2363 {
2364 int nremoved = 0;
2365
2366 joinlist = remove_rel_from_joinlist(joinlist, relid, &nremoved);
2367 if (nremoved != 1)
2368 elog(ERROR, "failed to find relation %d in joinlist", relid);
2369 }
2370 }
2371
2372 return joinlist;
2373}
bool enable_self_join_elimination
Definition: analyzejoins.c:53
#define unlikely(x)
Definition: c.h:347

References bms_next_member(), elog, enable_self_join_elimination, ERROR, IsA, linitial, list_length(), NIL, remove_rel_from_joinlist(), remove_self_joins_recurse(), root, and unlikely.

Referenced by query_planner().

◆ restrict_infos_logically_equal()

static bool restrict_infos_logically_equal ( RestrictInfo a,
RestrictInfo b 
)
static

Definition at line 1612 of file analyzejoins.c.

1613{
1614 int saved_rinfo_serial = a->rinfo_serial;
1615 bool result;
1616
1617 a->rinfo_serial = b->rinfo_serial;
1618 result = equal(a, b);
1619 a->rinfo_serial = saved_rinfo_serial;
1620
1621 return result;
1622}
int b
Definition: isn.c:74
int a
Definition: isn.c:73

References a, b, and equal().

Referenced by add_non_redundant_clauses().

◆ self_join_candidates_cmp()

static int self_join_candidates_cmp ( const void *  a,
const void *  b 
)
static

Definition at line 2297 of file analyzejoins.c.

2298{
2299 const SelfJoinCandidate *ca = (const SelfJoinCandidate *) a;
2300 const SelfJoinCandidate *cb = (const SelfJoinCandidate *) b;
2301
2302 if (ca->reloid != cb->reloid)
2303 return (ca->reloid < cb->reloid ? -1 : 1);
2304 else
2305 return 0;
2306}

References a, b, and SelfJoinCandidate::reloid.

Referenced by remove_self_joins_recurse().

◆ split_selfjoin_quals()

static void split_selfjoin_quals ( PlannerInfo root,
List joinquals,
List **  selfjoinquals,
List **  otherjoinquals,
int  from,
int  to 
)
static

Definition at line 1868 of file analyzejoins.c.

1870{
1871 List *sjoinquals = NIL;
1872 List *ojoinquals = NIL;
1873
1874 foreach_node(RestrictInfo, rinfo, joinquals)
1875 {
1876 OpExpr *expr;
1877 Node *leftexpr;
1878 Node *rightexpr;
1879
1880 /* In general, clause looks like F(arg1) = G(arg2) */
1881 if (!rinfo->mergeopfamilies ||
1882 bms_num_members(rinfo->clause_relids) != 2 ||
1883 bms_membership(rinfo->left_relids) != BMS_SINGLETON ||
1884 bms_membership(rinfo->right_relids) != BMS_SINGLETON)
1885 {
1886 ojoinquals = lappend(ojoinquals, rinfo);
1887 continue;
1888 }
1889
1890 expr = (OpExpr *) rinfo->clause;
1891
1892 if (!IsA(expr, OpExpr) || list_length(expr->args) != 2)
1893 {
1894 ojoinquals = lappend(ojoinquals, rinfo);
1895 continue;
1896 }
1897
1898 leftexpr = get_leftop(rinfo->clause);
1899 rightexpr = copyObject(get_rightop(rinfo->clause));
1900
1901 if (leftexpr && IsA(leftexpr, RelabelType))
1902 leftexpr = (Node *) ((RelabelType *) leftexpr)->arg;
1903 if (rightexpr && IsA(rightexpr, RelabelType))
1904 rightexpr = (Node *) ((RelabelType *) rightexpr)->arg;
1905
1906 /*
1907 * Quite an expensive operation, narrowing the use case. For example,
1908 * when we have cast of the same var to different (but compatible)
1909 * types.
1910 */
1911 ChangeVarNodes(rightexpr, bms_singleton_member(rinfo->right_relids),
1912 bms_singleton_member(rinfo->left_relids), 0);
1913
1914 if (equal(leftexpr, rightexpr))
1915 sjoinquals = lappend(sjoinquals, rinfo);
1916 else
1917 ojoinquals = lappend(ojoinquals, rinfo);
1918 }
1919
1920 *selfjoinquals = sjoinquals;
1921 *otherjoinquals = ojoinquals;
1922}
@ BMS_SINGLETON
Definition: bitmapset.h:72
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
void * arg
List * args
Definition: primnodes.h:853

References arg, OpExpr::args, bms_membership(), bms_num_members(), BMS_SINGLETON, bms_singleton_member(), ChangeVarNodes(), copyObject, equal(), foreach_node, get_leftop(), get_rightop(), if(), IsA, lappend(), list_length(), and NIL.

Referenced by remove_self_joins_one_group().

◆ update_eclasses()

static void update_eclasses ( EquivalenceClass ec,
int  from,
int  to 
)
static

Definition at line 1514 of file analyzejoins.c.

1515{
1516 List *new_members = NIL;
1517 List *new_sources = NIL;
1518
1519 /*
1520 * We don't expect any EC child members to exist at this point. Ensure
1521 * that's the case, otherwise, we might be getting asked to do something
1522 * this function hasn't been coded for.
1523 */
1524 Assert(ec->ec_childmembers == NULL);
1525
1527 {
1528 bool is_redundant = false;
1529
1530 if (!bms_is_member(from, em->em_relids))
1531 {
1532 new_members = lappend(new_members, em);
1533 continue;
1534 }
1535
1536 em->em_relids = adjust_relid_set(em->em_relids, from, to);
1537 em->em_jdomain->jd_relids = adjust_relid_set(em->em_jdomain->jd_relids, from, to);
1538
1539 /* We only process inner joins */
1540 ChangeVarNodes((Node *) em->em_expr, from, to, 0);
1541
1542 foreach_node(EquivalenceMember, other, new_members)
1543 {
1544 if (!equal(em->em_relids, other->em_relids))
1545 continue;
1546
1547 if (equal(em->em_expr, other->em_expr))
1548 {
1549 is_redundant = true;
1550 break;
1551 }
1552 }
1553
1554 if (!is_redundant)
1555 new_members = lappend(new_members, em);
1556 }
1557
1558 list_free(ec->ec_members);
1559 ec->ec_members = new_members;
1560
1562
1563 /* Update EC source expressions */
1565 {
1566 bool is_redundant = false;
1567
1568 if (!bms_is_member(from, rinfo->required_relids))
1569 {
1570 new_sources = lappend(new_sources, rinfo);
1571 continue;
1572 }
1573
1574 ChangeVarNodes((Node *) rinfo, from, to, 0);
1575
1576 /*
1577 * After switching the clause to the remaining relation, check it for
1578 * redundancy with existing ones. We don't have to check for
1579 * redundancy with derived clauses, because we've just deleted them.
1580 */
1581 foreach_node(RestrictInfo, other, new_sources)
1582 {
1583 if (!equal(rinfo->clause_relids, other->clause_relids))
1584 continue;
1585
1586 if (equal(rinfo->clause, other->clause))
1587 {
1588 is_redundant = true;
1589 break;
1590 }
1591 }
1592
1593 if (!is_redundant)
1594 new_sources = lappend(new_sources, rinfo);
1595 }
1596
1597 list_free(ec->ec_sources);
1598 ec->ec_sources = new_sources;
1599 ec->ec_relids = adjust_relid_set(ec->ec_relids, from, to);
1600}

References adjust_relid_set(), Assert(), bms_is_member(), ChangeVarNodes(), EquivalenceClass::ec_childmembers, ec_clear_derived_clauses(), EquivalenceClass::ec_members, EquivalenceClass::ec_relids, EquivalenceClass::ec_sources, equal(), foreach_node, lappend(), list_free(), and NIL.

Referenced by remove_self_join_rel().

Variable Documentation

◆ enable_self_join_elimination

bool enable_self_join_elimination

Definition at line 53 of file analyzejoins.c.

Referenced by remove_useless_self_joins().