Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * equivclass.c
4 : : * Routines for managing EquivalenceClasses
5 : : *
6 : : * See src/backend/optimizer/README for discussion of EquivalenceClasses.
7 : : *
8 : : *
9 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
10 : : * Portions Copyright (c) 1994, Regents of the University of California
11 : : *
12 : : * IDENTIFICATION
13 : : * src/backend/optimizer/path/equivclass.c
14 : : *
15 : : *-------------------------------------------------------------------------
16 : : */
17 : : #include "postgres.h"
18 : :
19 : : #include <limits.h>
20 : :
21 : : #include "access/stratnum.h"
22 : : #include "catalog/pg_type.h"
23 : : #include "common/hashfn.h"
24 : : #include "nodes/makefuncs.h"
25 : : #include "nodes/nodeFuncs.h"
26 : : #include "optimizer/appendinfo.h"
27 : : #include "optimizer/clauses.h"
28 : : #include "optimizer/optimizer.h"
29 : : #include "optimizer/pathnode.h"
30 : : #include "optimizer/paths.h"
31 : : #include "optimizer/planmain.h"
32 : : #include "optimizer/restrictinfo.h"
33 : : #include "rewrite/rewriteManip.h"
34 : : #include "utils/lsyscache.h"
35 : :
36 : :
37 : : static EquivalenceMember *make_eq_member(EquivalenceClass *ec,
38 : : Expr *expr, Relids relids,
39 : : JoinDomain *jdomain,
40 : : EquivalenceMember *parent,
41 : : Oid datatype);
42 : : static EquivalenceMember *add_eq_member(EquivalenceClass *ec,
43 : : Expr *expr, Relids relids,
44 : : JoinDomain *jdomain,
45 : : Oid datatype);
46 : : static EquivalenceMember *add_child_eq_member(PlannerInfo *root,
47 : : EquivalenceClass *ec,
48 : : int ec_index, Expr *expr,
49 : : Relids relids,
50 : : JoinDomain *jdomain,
51 : : EquivalenceMember *parent_em,
52 : : Oid datatype,
53 : : Index child_relid);
54 : : static void generate_base_implied_equalities_const(PlannerInfo *root,
55 : : EquivalenceClass *ec);
56 : : static void generate_base_implied_equalities_no_const(PlannerInfo *root,
57 : : EquivalenceClass *ec);
58 : : static void generate_base_implied_equalities_broken(PlannerInfo *root,
59 : : EquivalenceClass *ec);
60 : : static List *generate_join_implied_equalities_normal(PlannerInfo *root,
61 : : EquivalenceClass *ec,
62 : : Relids join_relids,
63 : : Relids outer_relids,
64 : : Relids inner_relids);
65 : : static List *generate_join_implied_equalities_broken(PlannerInfo *root,
66 : : EquivalenceClass *ec,
67 : : Relids nominal_join_relids,
68 : : Relids outer_relids,
69 : : Relids nominal_inner_relids,
70 : : RelOptInfo *inner_rel);
71 : : static Oid select_equality_operator(EquivalenceClass *ec,
72 : : Oid lefttype, Oid righttype);
73 : : static RestrictInfo *create_join_clause(PlannerInfo *root,
74 : : EquivalenceClass *ec, Oid opno,
75 : : EquivalenceMember *leftem,
76 : : EquivalenceMember *rightem,
77 : : EquivalenceClass *parent_ec);
78 : : static bool reconsider_outer_join_clause(PlannerInfo *root,
79 : : OuterJoinClauseInfo *ojcinfo,
80 : : bool outer_on_left);
81 : : static bool reconsider_full_join_clause(PlannerInfo *root,
82 : : OuterJoinClauseInfo *ojcinfo);
83 : : static JoinDomain *find_join_domain(PlannerInfo *root, Relids relids);
84 : : static Bitmapset *get_eclass_indexes_for_relids(PlannerInfo *root,
85 : : Relids relids);
86 : : static Bitmapset *get_common_eclass_indexes(PlannerInfo *root, Relids relids1,
87 : : Relids relids2);
88 : : static void ec_build_derives_hash(PlannerInfo *root, EquivalenceClass *ec);
89 : : static void ec_add_derived_clauses(EquivalenceClass *ec, List *clauses);
90 : : static void ec_add_derived_clause(EquivalenceClass *ec, RestrictInfo *clause);
91 : : static void ec_add_clause_to_derives_hash(EquivalenceClass *ec, RestrictInfo *rinfo);
92 : : static RestrictInfo *ec_search_clause_for_ems(PlannerInfo *root, EquivalenceClass *ec,
93 : : EquivalenceMember *leftem,
94 : : EquivalenceMember *rightem,
95 : : EquivalenceClass *parent_ec);
96 : : static RestrictInfo *ec_search_derived_clause_for_ems(PlannerInfo *root,
97 : : EquivalenceClass *ec,
98 : : EquivalenceMember *leftem,
99 : : EquivalenceMember *rightem,
100 : : EquivalenceClass *parent_ec);
101 : :
102 : : /*
103 : : * Hash key identifying a derived clause.
104 : : *
105 : : * This structure should not be filled manually. Use fill_ec_derives_key() to
106 : : * set it up in canonical form.
107 : : */
108 : : typedef struct
109 : : {
110 : : EquivalenceMember *em1;
111 : : EquivalenceMember *em2;
112 : : EquivalenceClass *parent_ec;
113 : : } ECDerivesKey;
114 : :
115 : : /* Hash table entry in ec_derives_hash. */
116 : : typedef struct
117 : : {
118 : : uint32 status;
119 : : ECDerivesKey key;
120 : : RestrictInfo *rinfo;
121 : : } ECDerivesEntry;
122 : :
123 : : /* Threshold for switching from list to hash table */
124 : : #define EC_DERIVES_HASH_THRESHOLD 32
125 : :
126 : : #define SH_PREFIX derives
127 : : #define SH_ELEMENT_TYPE ECDerivesEntry
128 : : #define SH_KEY_TYPE ECDerivesKey
129 : : #define SH_KEY key
130 : : #define SH_HASH_KEY(tb, key) \
131 : : hash_bytes((const unsigned char *) &(key), sizeof(ECDerivesKey))
132 : : #define SH_EQUAL(tb, a, b) \
133 : : ((a).em1 == (b).em1 && (a).em2 == (b).em2 && (a).parent_ec == (b).parent_ec)
134 : : #define SH_SCOPE static inline
135 : : #define SH_DECLARE
136 : : #define SH_DEFINE
137 : : #include "lib/simplehash.h"
138 : :
139 : : /*
140 : : * process_equivalence
141 : : * The given clause has a mergejoinable operator and is not an outer-join
142 : : * qualification, so its two sides can be considered equal
143 : : * anywhere they are both computable; moreover that equality can be
144 : : * extended transitively. Record this knowledge in the EquivalenceClass
145 : : * data structure, if applicable. Returns true if successful, false if not
146 : : * (in which case caller should treat the clause as ordinary, not an
147 : : * equivalence).
148 : : *
149 : : * In some cases, although we cannot convert a clause into EquivalenceClass
150 : : * knowledge, we can still modify it to a more useful form than the original.
151 : : * Then, *p_restrictinfo will be replaced by a new RestrictInfo, which is what
152 : : * the caller should use for further processing.
153 : : *
154 : : * jdomain is the join domain within which the given clause was found.
155 : : * This limits the applicability of deductions from the EquivalenceClass,
156 : : * as described in optimizer/README.
157 : : *
158 : : * We reject proposed equivalence clauses if they contain leaky functions
159 : : * and have security_level above zero. The EC evaluation rules require us to
160 : : * apply certain tests at certain joining levels, and we can't tolerate
161 : : * delaying any test on security_level grounds. By rejecting candidate clauses
162 : : * that might require security delays, we ensure it's safe to apply an EC
163 : : * clause as soon as it's supposed to be applied.
164 : : *
165 : : * On success return, we have also initialized the clause's left_ec/right_ec
166 : : * fields to point to the EquivalenceClass representing it. This saves lookup
167 : : * effort later.
168 : : *
169 : : * Note: constructing merged EquivalenceClasses is a standard UNION-FIND
170 : : * problem, for which there exist better data structures than simple lists.
171 : : * If this code ever proves to be a bottleneck then it could be sped up ---
172 : : * but for now, simple is beautiful.
173 : : *
174 : : * Note: this is only called during planner startup, not during GEQO
175 : : * exploration, so we need not worry about whether we're in the right
176 : : * memory context.
177 : : */
178 : : bool
3245 tgl@sss.pgh.pa.us 179 :CBC 236355 : process_equivalence(PlannerInfo *root,
180 : : RestrictInfo **p_restrictinfo,
181 : : JoinDomain *jdomain)
182 : : {
183 : 236355 : RestrictInfo *restrictinfo = *p_restrictinfo;
7159 184 : 236355 : Expr *clause = restrictinfo->clause;
185 : : Oid opno,
186 : : collation,
187 : : item1_type,
188 : : item2_type;
189 : : Expr *item1;
190 : : Expr *item2;
191 : : Relids item1_relids,
192 : : item2_relids;
193 : : List *opfamilies;
194 : : EquivalenceClass *ec1,
195 : : *ec2;
196 : : EquivalenceMember *em1,
197 : : *em2;
198 : : ListCell *lc1;
199 : : int ec2_idx;
200 : :
201 : : /* Should not already be marked as having generated an eclass */
5781 202 [ - + ]: 236355 : Assert(restrictinfo->left_ec == NULL);
203 [ - + ]: 236355 : Assert(restrictinfo->right_ec == NULL);
204 : :
205 : : /* Reject if it is potentially postponable by security considerations */
3508 206 [ + + + + ]: 236355 : if (restrictinfo->security_level > 0 && !restrictinfo->leakproof)
207 : 172 : return false;
208 : :
209 : : /* Extract info from given clause */
7159 210 [ - + ]: 236183 : Assert(is_opclause(clause));
211 : 236183 : opno = ((OpExpr *) clause)->opno;
5640 212 : 236183 : collation = ((OpExpr *) clause)->inputcollid;
7159 213 : 236183 : item1 = (Expr *) get_leftop(clause);
214 : 236183 : item2 = (Expr *) get_rightop(clause);
215 : 236183 : item1_relids = restrictinfo->left_relids;
216 : 236183 : item2_relids = restrictinfo->right_relids;
217 : :
218 : : /*
219 : : * Ensure both input expressions expose the desired collation (their types
220 : : * should be OK already); see comments for canonicalize_ec_expression.
221 : : */
5640 222 : 236183 : item1 = canonicalize_ec_expression(item1,
223 : : exprType((Node *) item1),
224 : : collation);
225 : 236183 : item2 = canonicalize_ec_expression(item2,
226 : : exprType((Node *) item2),
227 : : collation);
228 : :
229 : : /*
230 : : * Clauses of the form X=X cannot be translated into EquivalenceClasses.
231 : : * We'd either end up with a single-entry EC, losing the knowledge that
232 : : * the clause was present at all, or else make an EC with duplicate
233 : : * entries, causing other issues.
234 : : */
6176 235 [ + + ]: 236183 : if (equal(item1, item2))
236 : : {
237 : : /*
238 : : * If the operator is strict, then the clause can be treated as just
239 : : * "X IS NOT NULL". (Since we know we are considering a top-level
240 : : * qual, we can ignore the difference between FALSE and NULL results.)
241 : : * It's worth making the conversion because we'll typically get a much
242 : : * better selectivity estimate than we would for X=X.
243 : : *
244 : : * If the operator is not strict, we can't be sure what it will do
245 : : * with NULLs, so don't attempt to optimize it.
246 : : */
3245 247 : 45 : set_opfuncid((OpExpr *) clause);
248 [ + - ]: 45 : if (func_strict(((OpExpr *) clause)->opfuncid))
249 : : {
250 : 45 : NullTest *ntest = makeNode(NullTest);
251 : :
252 : 45 : ntest->arg = item1;
253 : 45 : ntest->nulltesttype = IS_NOT_NULL;
254 : 45 : ntest->argisrow = false; /* correct even if composite arg */
255 : 45 : ntest->location = -1;
256 : :
257 : 45 : *p_restrictinfo =
2044 258 : 45 : make_restrictinfo(root,
259 : : (Expr *) ntest,
3245 260 : 45 : restrictinfo->is_pushed_down,
1190 261 : 45 : restrictinfo->has_clone,
262 : 45 : restrictinfo->is_clone,
3245 263 : 45 : restrictinfo->pseudoconstant,
264 : : restrictinfo->security_level,
265 : : NULL,
266 : : restrictinfo->incompatible_relids,
267 : : restrictinfo->outer_relids);
268 : : }
269 : 45 : return false;
270 : : }
271 : :
272 : : /*
273 : : * We use the declared input types of the operator, not exprType() of the
274 : : * inputs, as the nominal datatypes for opfamily lookup. This presumes
275 : : * that btree operators are always registered with amoplefttype and
276 : : * amoprighttype equal to their declared input types. We will need this
277 : : * info anyway to build EquivalenceMember nodes, and by extracting it now
278 : : * we can use type comparisons to short-circuit some equal() tests.
279 : : */
7159 280 : 236138 : op_input_types(opno, &item1_type, &item2_type);
281 : :
282 : 236138 : opfamilies = restrictinfo->mergeopfamilies;
283 : :
284 : : /*
285 : : * Sweep through the existing EquivalenceClasses looking for matches to
286 : : * item1 and item2. These are the possible outcomes:
287 : : *
288 : : * 1. We find both in the same EC. The equivalence is already known, so
289 : : * there's nothing to do.
290 : : *
291 : : * 2. We find both in different ECs. Merge the two ECs together.
292 : : *
293 : : * 3. We find just one. Add the other to its EC.
294 : : *
295 : : * 4. We find neither. Make a new, two-entry EC.
296 : : *
297 : : * Note: since all ECs are built through this process or the similar
298 : : * search in get_eclass_for_sort_expr(), it's impossible that we'd match
299 : : * an item in more than one existing nonvolatile EC. So it's okay to stop
300 : : * at the first match.
301 : : */
302 : 236138 : ec1 = ec2 = NULL;
7157 303 : 236138 : em1 = em2 = NULL;
2135 drowley@postgresql.o 304 : 236138 : ec2_idx = -1;
7159 tgl@sss.pgh.pa.us 305 [ + + + + : 404469 : foreach(lc1, root->eq_classes)
+ + ]
306 : : {
307 : 168399 : EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
308 : : ListCell *lc2;
309 : :
310 : : /* Never match to a volatile EC */
311 [ - + ]: 168399 : if (cur_ec->ec_has_volatile)
7159 tgl@sss.pgh.pa.us 312 :UBC 0 : continue;
313 : :
314 : : /*
315 : : * The collation has to match; check this first since it's cheaper
316 : : * than the opfamily comparison.
317 : : */
5640 tgl@sss.pgh.pa.us 318 [ + + ]:CBC 168399 : if (collation != cur_ec->ec_collation)
319 : 14653 : continue;
320 : :
321 : : /*
322 : : * A "match" requires matching sets of btree opfamilies. Use of
323 : : * equal() for this test has implications discussed in the comments
324 : : * for get_mergejoin_opfamilies().
325 : : */
7159 326 [ + + ]: 153746 : if (!equal(opfamilies, cur_ec->ec_opfamilies))
327 : 41170 : continue;
328 : :
329 : : /* We don't expect any children yet */
506 drowley@postgresql.o 330 [ - + ]: 112576 : Assert(cur_ec->ec_childmembers == NULL);
331 : :
7159 tgl@sss.pgh.pa.us 332 [ + - + + : 335660 : foreach(lc2, cur_ec->ec_members)
+ + ]
333 : : {
334 : 223152 : EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
335 : :
336 : : /* Child members should not exist in ec_members */
506 drowley@postgresql.o 337 [ - + ]: 223152 : Assert(!cur_em->em_is_child);
338 : :
339 : : /*
340 : : * Match constants only within the same JoinDomain (see
341 : : * optimizer/README).
342 : : */
1305 tgl@sss.pgh.pa.us 343 [ + + + + ]: 223152 : if (cur_em->em_is_const && cur_em->em_jdomain != jdomain)
7159 344 : 2995 : continue;
345 : :
346 [ + + ]: 220157 : if (!ec1 &&
347 [ + + + + ]: 420722 : item1_type == cur_em->em_datatype &&
348 : 210208 : equal(item1, cur_em->em_expr))
349 : : {
350 : 13590 : ec1 = cur_ec;
7157 351 : 13590 : em1 = cur_em;
7159 352 [ + + ]: 13590 : if (ec2)
353 : 25 : break;
354 : : }
355 : :
356 [ + + ]: 220132 : if (!ec2 &&
357 [ + + + + ]: 436285 : item2_type == cur_em->em_datatype &&
358 : 217877 : equal(item2, cur_em->em_expr))
359 : : {
360 : 2808 : ec2 = cur_ec;
2135 drowley@postgresql.o 361 : 2808 : ec2_idx = foreach_current_index(lc1);
7157 tgl@sss.pgh.pa.us 362 : 2808 : em2 = cur_em;
7159 363 [ + + ]: 2808 : if (ec1)
364 : 43 : break;
365 : : }
366 : : }
367 : :
368 [ + + + + ]: 112576 : if (ec1 && ec2)
369 : 68 : break;
370 : : }
371 : :
372 : : /* Sweep finished, what did we find? */
373 : :
374 [ + + + + ]: 236138 : if (ec1 && ec2)
375 : : {
376 : : /* If case 1, nothing to do, except add to sources */
377 [ + + ]: 68 : if (ec1 == ec2)
378 : : {
379 : 35 : ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
3508 380 : 35 : ec1->ec_min_security = Min(ec1->ec_min_security,
381 : : restrictinfo->security_level);
382 : 35 : ec1->ec_max_security = Max(ec1->ec_max_security,
383 : : restrictinfo->security_level);
384 : : /* mark the RI as associated with this eclass */
5781 385 : 35 : restrictinfo->left_ec = ec1;
386 : 35 : restrictinfo->right_ec = ec1;
387 : : /* mark the RI as usable with this pair of EMs */
7157 388 : 35 : restrictinfo->left_em = em1;
389 : 35 : restrictinfo->right_em = em2;
7159 390 : 35 : return true;
391 : : }
392 : :
393 : : /*
394 : : * Case 2: need to merge ec1 and ec2. This should never happen after
395 : : * the ECs have reached canonical state; otherwise, pathkeys could be
396 : : * rendered non-canonical by the merge, and relation eclass indexes
397 : : * would get broken by removal of an eq_classes list entry.
398 : : */
2594 drowley@postgresql.o 399 [ - + ]: 33 : if (root->ec_merging_done)
4868 tgl@sss.pgh.pa.us 400 [ # # ]:UBC 0 : elog(ERROR, "too late to merge equivalence classes");
401 : :
402 : : /*
403 : : * We add ec2's items to ec1, then set ec2's ec_merged link to point
404 : : * to ec1 and remove ec2 from the eq_classes list. We cannot simply
405 : : * delete ec2 because that could leave dangling pointers in existing
406 : : * PathKeys. We leave it behind with a link so that the merged EC can
407 : : * be found.
408 : : */
7159 tgl@sss.pgh.pa.us 409 :CBC 33 : ec1->ec_members = list_concat(ec1->ec_members, ec2->ec_members);
410 : 33 : ec1->ec_sources = list_concat(ec1->ec_sources, ec2->ec_sources);
411 : :
412 : : /*
413 : : * Appends ec2's derived clauses to ec1->ec_derives_list and adds them
414 : : * to ec1->ec_derives_hash if present.
415 : : */
510 amitlan@postgresql.o 416 : 33 : ec_add_derived_clauses(ec1, ec2->ec_derives_list);
7159 tgl@sss.pgh.pa.us 417 : 33 : ec1->ec_relids = bms_join(ec1->ec_relids, ec2->ec_relids);
418 : 33 : ec1->ec_has_const |= ec2->ec_has_const;
419 : : /* can't need to set has_volatile */
3508 420 : 33 : ec1->ec_min_security = Min(ec1->ec_min_security,
421 : : ec2->ec_min_security);
422 : 33 : ec1->ec_max_security = Max(ec1->ec_max_security,
423 : : ec2->ec_max_security);
7159 424 : 33 : ec2->ec_merged = ec1;
2135 drowley@postgresql.o 425 : 33 : root->eq_classes = list_delete_nth_cell(root->eq_classes, ec2_idx);
426 : : /* just to avoid debugging confusion w/ dangling pointers: */
7159 tgl@sss.pgh.pa.us 427 : 33 : ec2->ec_members = NIL;
428 : 33 : ec2->ec_sources = NIL;
510 amitlan@postgresql.o 429 : 33 : ec_clear_derived_clauses(ec2);
7159 tgl@sss.pgh.pa.us 430 : 33 : ec2->ec_relids = NULL;
431 : 33 : ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
3508 432 : 33 : ec1->ec_min_security = Min(ec1->ec_min_security,
433 : : restrictinfo->security_level);
434 : 33 : ec1->ec_max_security = Max(ec1->ec_max_security,
435 : : restrictinfo->security_level);
436 : : /* mark the RI as associated with this eclass */
5781 437 : 33 : restrictinfo->left_ec = ec1;
438 : 33 : restrictinfo->right_ec = ec1;
439 : : /* mark the RI as usable with this pair of EMs */
7157 440 : 33 : restrictinfo->left_em = em1;
441 : 33 : restrictinfo->right_em = em2;
442 : : }
7159 443 [ + + ]: 236070 : else if (ec1)
444 : : {
445 : : /* Case 3: add item2 to ec1 */
1305 446 : 13522 : em2 = add_eq_member(ec1, item2, item2_relids,
447 : : jdomain, item2_type);
7159 448 : 13522 : ec1->ec_sources = lappend(ec1->ec_sources, restrictinfo);
3508 449 : 13522 : ec1->ec_min_security = Min(ec1->ec_min_security,
450 : : restrictinfo->security_level);
451 : 13522 : ec1->ec_max_security = Max(ec1->ec_max_security,
452 : : restrictinfo->security_level);
453 : : /* mark the RI as associated with this eclass */
5781 454 : 13522 : restrictinfo->left_ec = ec1;
455 : 13522 : restrictinfo->right_ec = ec1;
456 : : /* mark the RI as usable with this pair of EMs */
7157 457 : 13522 : restrictinfo->left_em = em1;
458 : 13522 : restrictinfo->right_em = em2;
459 : : }
7159 460 [ + + ]: 222548 : else if (ec2)
461 : : {
462 : : /* Case 3: add item1 to ec2 */
1305 463 : 2740 : em1 = add_eq_member(ec2, item1, item1_relids,
464 : : jdomain, item1_type);
7159 465 : 2740 : ec2->ec_sources = lappend(ec2->ec_sources, restrictinfo);
3508 466 : 2740 : ec2->ec_min_security = Min(ec2->ec_min_security,
467 : : restrictinfo->security_level);
468 : 2740 : ec2->ec_max_security = Max(ec2->ec_max_security,
469 : : restrictinfo->security_level);
470 : : /* mark the RI as associated with this eclass */
5781 471 : 2740 : restrictinfo->left_ec = ec2;
472 : 2740 : restrictinfo->right_ec = ec2;
473 : : /* mark the RI as usable with this pair of EMs */
7157 474 : 2740 : restrictinfo->left_em = em1;
475 : 2740 : restrictinfo->right_em = em2;
476 : : }
477 : : else
478 : : {
479 : : /* Case 4: make a new, two-entry EC */
7159 480 : 219808 : EquivalenceClass *ec = makeNode(EquivalenceClass);
481 : :
482 : 219808 : ec->ec_opfamilies = opfamilies;
5640 483 : 219808 : ec->ec_collation = collation;
506 drowley@postgresql.o 484 : 219808 : ec->ec_childmembers_size = 0;
7159 tgl@sss.pgh.pa.us 485 : 219808 : ec->ec_members = NIL;
506 drowley@postgresql.o 486 : 219808 : ec->ec_childmembers = NULL;
7159 tgl@sss.pgh.pa.us 487 : 219808 : ec->ec_sources = list_make1(restrictinfo);
510 amitlan@postgresql.o 488 : 219808 : ec->ec_derives_list = NIL;
489 : 219808 : ec->ec_derives_hash = NULL;
7159 tgl@sss.pgh.pa.us 490 : 219808 : ec->ec_relids = NULL;
491 : 219808 : ec->ec_has_const = false;
492 : 219808 : ec->ec_has_volatile = false;
493 : 219808 : ec->ec_broken = false;
6867 494 : 219808 : ec->ec_sortref = 0;
3508 495 : 219808 : ec->ec_min_security = restrictinfo->security_level;
496 : 219808 : ec->ec_max_security = restrictinfo->security_level;
7159 497 : 219808 : ec->ec_merged = NULL;
1305 498 : 219808 : em1 = add_eq_member(ec, item1, item1_relids,
499 : : jdomain, item1_type);
500 : 219808 : em2 = add_eq_member(ec, item2, item2_relids,
501 : : jdomain, item2_type);
502 : :
7159 503 : 219808 : root->eq_classes = lappend(root->eq_classes, ec);
504 : :
505 : : /* mark the RI as associated with this eclass */
5781 506 : 219808 : restrictinfo->left_ec = ec;
507 : 219808 : restrictinfo->right_ec = ec;
508 : : /* mark the RI as usable with this pair of EMs */
7157 509 : 219808 : restrictinfo->left_em = em1;
510 : 219808 : restrictinfo->right_em = em2;
511 : : }
512 : :
7159 513 : 236103 : return true;
514 : : }
515 : :
516 : : /*
517 : : * canonicalize_ec_expression
518 : : *
519 : : * This function ensures that the expression exposes the expected type and
520 : : * collation, so that it will be equal() to other equivalence-class expressions
521 : : * that it ought to be equal() to.
522 : : *
523 : : * The rule for datatypes is that the exposed type should match what it would
524 : : * be for an input to an operator of the EC's opfamilies; which is usually
525 : : * the declared input type of the operator, but in the case of polymorphic
526 : : * operators no relabeling is wanted (compare the behavior of parse_coerce.c).
527 : : * Expressions coming in from quals will generally have the right type
528 : : * already, but expressions coming from indexkeys may not (because they are
529 : : * represented without any explicit relabel in pg_index), and the same problem
530 : : * occurs for sort expressions (because the parser is likewise cavalier about
531 : : * putting relabels on them). Such cases will be binary-compatible with the
532 : : * real operators, so adding a RelabelType is sufficient.
533 : : *
534 : : * Also, the expression's exposed collation must match the EC's collation.
535 : : * This is important because in comparisons like "foo < bar COLLATE baz",
536 : : * only one of the expressions has the correct exposed collation as we receive
537 : : * it from the parser. Forcing both of them to have it ensures that all
538 : : * variant spellings of such a construct behave the same. Again, we can
539 : : * stick on a RelabelType to force the right exposed collation. (It might
540 : : * work to not label the collation at all in EC members, but this is risky
541 : : * since some parts of the system expect exprCollation() to deliver the
542 : : * right answer for a sort key.)
543 : : */
544 : : Expr *
5640 545 : 2346367 : canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
546 : : {
547 : 2346367 : Oid expr_type = exprType((Node *) expr);
548 : :
549 : : /*
550 : : * For a polymorphic-input-type opclass, just keep the same exposed type.
551 : : * RECORD opclasses work like polymorphic-type ones for this purpose.
552 : : */
3025 553 [ + - + + : 2346367 : if (IsPolymorphicType(req_type) || req_type == RECORDOID)
+ - + + +
+ + + + -
+ - + - +
- + - +
+ ]
5640 554 : 9433 : req_type = expr_type;
555 : :
556 : : /*
557 : : * No work if the expression exposes the right type/collation already.
558 : : */
559 [ + + + + ]: 4630189 : if (expr_type != req_type ||
560 : 2283822 : exprCollation((Node *) expr) != req_collation)
561 : : {
562 : : /*
563 : : * If we have to change the type of the expression, set typmod to -1,
564 : : * since the new type may not have the same typmod interpretation.
565 : : * When we only have to change collation, preserve the exposed typmod.
566 : : */
567 : : int32 req_typmod;
568 : :
2199 569 [ + + ]: 64316 : if (expr_type != req_type)
570 : 62545 : req_typmod = -1;
571 : : else
572 : 1771 : req_typmod = exprTypmod((Node *) expr);
573 : :
574 : : /*
575 : : * Use applyRelabelType so that we preserve const-flatness. This is
576 : : * important since eval_const_expressions has already been applied.
577 : : */
578 : 64316 : expr = (Expr *) applyRelabelType((Node *) expr,
579 : : req_type, req_typmod, req_collation,
580 : : COERCE_IMPLICIT_CAST, -1, false);
581 : : }
582 : :
5640 583 : 2346367 : return expr;
584 : : }
585 : :
586 : : /*
587 : : * make_eq_member
588 : : * Build a new EquivalenceMember without adding it to an EC. If 'parent'
589 : : * is NULL, the result will be a parent member, otherwise a child member.
590 : : */
591 : : static EquivalenceMember *
506 drowley@postgresql.o 592 : 754683 : make_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids,
593 : : JoinDomain *jdomain, EquivalenceMember *parent, Oid datatype)
594 : : {
7159 tgl@sss.pgh.pa.us 595 : 754683 : EquivalenceMember *em = makeNode(EquivalenceMember);
596 : :
597 : 754683 : em->em_expr = expr;
598 : 754683 : em->em_relids = relids;
599 : 754683 : em->em_is_const = false;
1305 600 : 754683 : em->em_is_child = (parent != NULL);
7159 601 : 754683 : em->em_datatype = datatype;
1305 602 : 754683 : em->em_jdomain = jdomain;
603 : 754683 : em->em_parent = parent;
604 : :
7159 605 [ + + ]: 754683 : if (bms_is_empty(relids))
606 : : {
607 : : /*
608 : : * No Vars, assume it's a pseudoconstant. This is correct for entries
609 : : * generated from process_equivalence(), because a WHERE clause can't
610 : : * contain aggregates or SRFs, and non-volatility was checked before
611 : : * process_equivalence() ever got called. But
612 : : * get_eclass_for_sort_expr() has to work harder. We put the tests
613 : : * there not here to save cycles in the equivalence case.
614 : : */
1305 615 [ - + ]: 167832 : Assert(!parent);
7159 616 : 167832 : em->em_is_const = true;
617 : 167832 : ec->ec_has_const = true;
618 : : /* it can't affect ec_relids */
619 : : }
620 : :
506 drowley@postgresql.o 621 : 754683 : return em;
622 : : }
623 : :
624 : : /*
625 : : * add_eq_member - build a new non-child EquivalenceMember and add it to 'ec'.
626 : : */
627 : : static EquivalenceMember *
628 : 670573 : add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids,
629 : : JoinDomain *jdomain, Oid datatype)
630 : : {
631 : 670573 : EquivalenceMember *em = make_eq_member(ec, expr, relids, jdomain,
632 : : NULL, datatype);
633 : :
634 : : /* add to the members list */
635 : 670573 : ec->ec_members = lappend(ec->ec_members, em);
636 : :
637 : : /* record the relids for parent members */
638 : 670573 : ec->ec_relids = bms_add_members(ec->ec_relids, relids);
639 : :
640 : 670573 : return em;
641 : : }
642 : :
643 : : /*
644 : : * add_child_eq_member
645 : : * Create an em_is_child=true EquivalenceMember and add it to 'ec'.
646 : : *
647 : : * 'root' is the PlannerInfo that 'ec' belongs to.
648 : : * 'ec' is the EquivalenceClass to add the child member to.
649 : : * 'ec_index' the index of 'ec' within root->eq_classes, or -1 if maintaining
650 : : * the RelOptInfo.eclass_indexes isn't needed.
651 : : * 'expr' is the em_expr for the new member.
652 : : * 'relids' is the 'em_relids' for the new member.
653 : : * 'jdomain' is the 'em_jdomain' for the new member.
654 : : * 'parent_em' is the parent member of the child to create.
655 : : * 'datatype' is the em_datatype of the new member.
656 : : * 'child_relid' defines which element of ec_childmembers to add this member
657 : : * to. This is generally a RELOPT_OTHER_MEMBER_REL, but for set operations
658 : : * can be a RELOPT_BASEREL representing the set-op children.
659 : : */
660 : : static EquivalenceMember *
661 : 84110 : add_child_eq_member(PlannerInfo *root, EquivalenceClass *ec, int ec_index,
662 : : Expr *expr, Relids relids, JoinDomain *jdomain,
663 : : EquivalenceMember *parent_em, Oid datatype,
664 : : Index child_relid)
665 : : {
666 : : EquivalenceMember *em;
667 : :
668 [ - + ]: 84110 : Assert(parent_em != NULL);
669 : :
670 : : /*
671 : : * Allocate the array to store child members; an array of Lists indexed by
672 : : * relid, or expand the existing one, if necessary.
673 : : */
674 [ + + ]: 84110 : if (unlikely(ec->ec_childmembers_size < root->simple_rel_array_size))
675 : : {
676 [ + - ]: 23784 : if (ec->ec_childmembers == NULL)
677 : 23784 : ec->ec_childmembers = palloc0_array(List *, root->simple_rel_array_size);
678 : : else
506 drowley@postgresql.o 679 :UBC 0 : ec->ec_childmembers = repalloc0_array(ec->ec_childmembers, List *,
680 : : ec->ec_childmembers_size,
681 : : root->simple_rel_array_size);
682 : :
506 drowley@postgresql.o 683 :CBC 23784 : ec->ec_childmembers_size = root->simple_rel_array_size;
684 : : }
685 : :
686 : 84110 : em = make_eq_member(ec, expr, relids, jdomain, parent_em, datatype);
687 : :
688 : : /* add member to the ec_childmembers List for the given child_relid */
689 : 84110 : ec->ec_childmembers[child_relid] = lappend(ec->ec_childmembers[child_relid], em);
690 : :
691 : : /* Record this EC index for the child rel */
692 [ + + ]: 84110 : if (ec_index >= 0)
693 : : {
694 : 51224 : RelOptInfo *child_rel = root->simple_rel_array[child_relid];
695 : :
696 : 51224 : child_rel->eclass_indexes =
697 : 51224 : bms_add_member(child_rel->eclass_indexes, ec_index);
698 : : }
699 : :
7157 tgl@sss.pgh.pa.us 700 : 84110 : return em;
701 : : }
702 : :
703 : :
704 : : /*
705 : : * get_eclass_for_sort_expr
706 : : * Given an expression and opfamily/collation info, find an existing
707 : : * equivalence class it is a member of; if none, optionally build a new
708 : : * single-member EquivalenceClass for it.
709 : : *
710 : : * sortref is the SortGroupRef of the originating SortGroupClause, if any,
711 : : * or zero if not. (It should never be zero if the expression is volatile!)
712 : : *
713 : : * If rel is not NULL, it identifies a specific relation we're considering
714 : : * a path for, and indicates that child EC members for that relation can be
715 : : * considered. Otherwise child members are ignored. (Note: since child EC
716 : : * members aren't guaranteed unique, a non-NULL value means that there could
717 : : * be more than one EC that matches the expression; if so it's order-dependent
718 : : * which one you get. This is annoying but it only happens in corner cases,
719 : : * so for now we live with just reporting the first match. See also
720 : : * generate_implied_equalities_for_column and match_pathkeys_to_index.)
721 : : *
722 : : * If create_it is true, we'll build a new EquivalenceClass when there is no
723 : : * match. If create_it is false, we just return NULL when no match.
724 : : *
725 : : * This can be used safely both before and after EquivalenceClass merging;
726 : : * since it never causes merging it does not invalidate any existing ECs
727 : : * or PathKeys. However, ECs added after path generation has begun are
728 : : * of limited usefulness, so usually it's best to create them beforehand.
729 : : *
730 : : * Note: opfamilies must be chosen consistently with the way
731 : : * process_equivalence() would do; that is, generated from a mergejoinable
732 : : * equality operator. Else we might fail to detect valid equivalences,
733 : : * generating poor (but not incorrect) plans.
734 : : */
735 : : EquivalenceClass *
7159 736 : 1745664 : get_eclass_for_sort_expr(PlannerInfo *root,
737 : : Expr *expr,
738 : : List *opfamilies,
739 : : Oid opcintype,
740 : : Oid collation,
741 : : Index sortref,
742 : : Relids rel,
743 : : bool create_it)
744 : : {
745 : : JoinDomain *jdomain;
746 : : Relids expr_relids;
747 : : EquivalenceClass *newec;
748 : : EquivalenceMember *newem;
749 : : ListCell *lc1;
750 : : MemoryContext oldcontext;
751 : :
752 : : /*
753 : : * Ensure the expression exposes the correct type and collation.
754 : : */
5640 755 : 1745664 : expr = canonicalize_ec_expression(expr, opcintype, collation);
756 : :
757 : : /*
758 : : * Since SortGroupClause nodes are top-level expressions (GROUP BY, ORDER
759 : : * BY, etc), they can be presumed to belong to the top JoinDomain.
760 : : */
1305 761 : 1745664 : jdomain = linitial_node(JoinDomain, root->join_domains);
762 : :
763 : : /*
764 : : * Scan through the existing EquivalenceClasses for a match
765 : : */
7159 766 [ + + + + : 5792437 : foreach(lc1, root->eq_classes)
+ + ]
767 : : {
768 : 5070586 : EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
769 : : EquivalenceMemberIterator it;
770 : : EquivalenceMember *cur_em;
771 : :
772 : : /*
773 : : * Never match to a volatile EC, except when we are looking at another
774 : : * reference to the same volatile SortGroupClause.
775 : : */
6193 776 [ + + + + ]: 5070586 : if (cur_ec->ec_has_volatile &&
777 [ + + ]: 28 : (sortref == 0 || sortref != cur_ec->ec_sortref))
6867 778 : 2147775 : continue;
779 : :
5640 780 [ + + ]: 5070076 : if (collation != cur_ec->ec_collation)
781 : 1434759 : continue;
7159 782 [ + + ]: 3635317 : if (!equal(opfamilies, cur_ec->ec_opfamilies))
783 : 712506 : continue;
784 : :
506 drowley@postgresql.o 785 : 2922811 : setup_eclass_member_iterator(&it, cur_ec, rel);
786 [ + + ]: 6453083 : while ((cur_em = eclass_member_iterator_next(&it)) != NULL)
787 : : {
788 : : /*
789 : : * Ignore child members unless they match the request.
790 : : */
5277 tgl@sss.pgh.pa.us 791 [ + + ]: 4554085 : if (cur_em->em_is_child &&
792 [ - + ]: 84940 : !bms_equal(cur_em->em_relids, rel))
5277 tgl@sss.pgh.pa.us 793 :UBC 0 : continue;
794 : :
795 : : /*
796 : : * Match constants only within the same JoinDomain (see
797 : : * optimizer/README).
798 : : */
1305 tgl@sss.pgh.pa.us 799 [ + + + + ]:CBC 4554085 : if (cur_em->em_is_const && cur_em->em_jdomain != jdomain)
7159 800 : 64105 : continue;
801 : :
5640 802 [ + + + + ]: 8946290 : if (opcintype == cur_em->em_datatype &&
7159 803 : 4456310 : equal(expr, cur_em->em_expr))
812 akorotkov@postgresql 804 : 1023813 : return cur_ec; /* Match! */
805 : : }
806 : : }
807 : :
808 : : /* No match; does caller want a NULL result? */
5781 tgl@sss.pgh.pa.us 809 [ + + ]: 721851 : if (!create_it)
810 : 507156 : return NULL;
811 : :
812 : : /*
813 : : * OK, build a new single-member EC
814 : : *
815 : : * Here, we must be sure that we construct the EC in the right context.
816 : : */
7159 817 : 214695 : oldcontext = MemoryContextSwitchTo(root->planner_cxt);
818 : :
819 : 214695 : newec = makeNode(EquivalenceClass);
820 : 214695 : newec->ec_opfamilies = list_copy(opfamilies);
5640 821 : 214695 : newec->ec_collation = collation;
506 drowley@postgresql.o 822 : 214695 : newec->ec_childmembers_size = 0;
7159 tgl@sss.pgh.pa.us 823 : 214695 : newec->ec_members = NIL;
506 drowley@postgresql.o 824 : 214695 : newec->ec_childmembers = NULL;
7159 tgl@sss.pgh.pa.us 825 : 214695 : newec->ec_sources = NIL;
510 amitlan@postgresql.o 826 : 214695 : newec->ec_derives_list = NIL;
827 : 214695 : newec->ec_derives_hash = NULL;
7159 tgl@sss.pgh.pa.us 828 : 214695 : newec->ec_relids = NULL;
829 : 214695 : newec->ec_has_const = false;
830 : 214695 : newec->ec_has_volatile = contain_volatile_functions((Node *) expr);
831 : 214695 : newec->ec_broken = false;
6867 832 : 214695 : newec->ec_sortref = sortref;
3508 833 : 214695 : newec->ec_min_security = UINT_MAX;
834 : 214695 : newec->ec_max_security = 0;
7159 835 : 214695 : newec->ec_merged = NULL;
836 : :
6026 bruce@momjian.us 837 [ + + - + ]: 214695 : if (newec->ec_has_volatile && sortref == 0) /* should not happen */
6193 tgl@sss.pgh.pa.us 838 [ # # ]:UBC 0 : elog(ERROR, "volatile EquivalenceClass has no sortref");
839 : :
840 : : /*
841 : : * Get the precise set of relids appearing in the expression.
842 : : */
2044 tgl@sss.pgh.pa.us 843 :CBC 214695 : expr_relids = pull_varnos(root, (Node *) expr);
844 : :
4668 845 : 214695 : newem = add_eq_member(newec, copyObject(expr), expr_relids,
846 : : jdomain, opcintype);
847 : :
848 : : /*
849 : : * add_eq_member doesn't check for volatile functions, set-returning
850 : : * functions, aggregates, or window functions, but such could appear in
851 : : * sort expressions; so we have to check whether its const-marking was
852 : : * correct.
853 : : */
7159 854 [ + + ]: 214695 : if (newec->ec_has_const)
855 : : {
6991 856 [ + + + + ]: 15172 : if (newec->ec_has_volatile ||
857 [ + + ]: 14948 : expression_returns_set((Node *) expr) ||
6451 858 [ + + ]: 14677 : contain_agg_clause((Node *) expr) ||
859 : 7263 : contain_window_function((Node *) expr))
860 : : {
7159 861 : 380 : newec->ec_has_const = false;
7157 862 : 380 : newem->em_is_const = false;
863 : : }
864 : : }
865 : :
7159 866 : 214695 : root->eq_classes = lappend(root->eq_classes, newec);
867 : :
868 : : /*
869 : : * If EC merging is already complete, we have to mop up by adding the new
870 : : * EC to the eclass_indexes of the relation(s) mentioned in it.
871 : : */
2594 drowley@postgresql.o 872 [ + + ]: 214695 : if (root->ec_merging_done)
873 : : {
874 : 126566 : int ec_index = list_length(root->eq_classes) - 1;
875 : 126566 : int i = -1;
876 : :
877 [ + + ]: 243915 : while ((i = bms_next_member(newec->ec_relids, i)) > 0)
878 : : {
879 : 117349 : RelOptInfo *rel = root->simple_rel_array[i];
880 : :
881 : : /* ignore the RTE_GROUP RTE */
716 rguo@postgresql.org 882 [ + + ]: 117349 : if (i == root->group_rtindex)
883 : 584 : continue;
884 : :
1305 tgl@sss.pgh.pa.us 885 [ + + ]: 116765 : if (rel == NULL) /* must be an outer join */
886 : : {
887 [ - + ]: 5351 : Assert(bms_is_member(i, root->outer_join_rels));
888 : 5351 : continue;
889 : : }
890 : :
1291 891 [ - + ]: 111414 : Assert(rel->reloptkind == RELOPT_BASEREL);
892 : :
2594 drowley@postgresql.o 893 : 111414 : rel->eclass_indexes = bms_add_member(rel->eclass_indexes,
894 : : ec_index);
895 : : }
896 : : }
897 : :
7159 tgl@sss.pgh.pa.us 898 : 214695 : MemoryContextSwitchTo(oldcontext);
899 : :
900 : 214695 : return newec;
901 : : }
902 : :
903 : : /*
904 : : * find_ec_member_matching_expr
905 : : * Locate an EquivalenceClass member matching the given expr, if any;
906 : : * return NULL if no match.
907 : : *
908 : : * "Matching" is defined as "equal after stripping RelabelTypes".
909 : : * This is used for identifying sort expressions, and we need to allow
910 : : * binary-compatible relabeling for some cases involving binary-compatible
911 : : * sort operators.
912 : : *
913 : : * Child EC members are ignored unless they belong to given 'relids'.
914 : : */
915 : : EquivalenceMember *
1955 916 : 275252 : find_ec_member_matching_expr(EquivalenceClass *ec,
917 : : Expr *expr,
918 : : Relids relids)
919 : : {
920 : : EquivalenceMemberIterator it;
921 : : EquivalenceMember *em;
922 : :
923 : : /* We ignore binary-compatible relabeling on both ends */
924 [ + - + + ]: 297602 : while (expr && IsA(expr, RelabelType))
925 : 22350 : expr = ((RelabelType *) expr)->arg;
926 : :
506 drowley@postgresql.o 927 : 275252 : setup_eclass_member_iterator(&it, ec, relids);
928 [ + + ]: 463973 : while ((em = eclass_member_iterator_next(&it)) != NULL)
929 : : {
930 : : Expr *emexpr;
931 : :
932 : : /*
933 : : * We shouldn't be trying to sort by an equivalence class that
934 : : * contains a constant, so no need to consider such cases any further.
935 : : */
1955 tgl@sss.pgh.pa.us 936 [ - + ]: 307061 : if (em->em_is_const)
1955 tgl@sss.pgh.pa.us 937 :UBC 0 : continue;
938 : :
939 : : /*
940 : : * Ignore child members unless they belong to the requested rel.
941 : : */
1955 tgl@sss.pgh.pa.us 942 [ + + ]:CBC 307061 : if (em->em_is_child &&
943 [ + + ]: 10050 : !bms_is_subset(em->em_relids, relids))
944 : 3396 : continue;
945 : :
946 : : /*
947 : : * Match if same expression (after stripping relabel).
948 : : */
949 : 303665 : emexpr = em->em_expr;
950 [ + - + + ]: 310483 : while (emexpr && IsA(emexpr, RelabelType))
951 : 6818 : emexpr = ((RelabelType *) emexpr)->arg;
952 : :
953 [ + + ]: 303665 : if (equal(emexpr, expr))
954 : 118340 : return em;
955 : : }
956 : :
957 : 156912 : return NULL;
958 : : }
959 : :
960 : : /*
961 : : * find_computable_ec_member
962 : : * Locate an EquivalenceClass member that can be computed from the
963 : : * expressions appearing in "exprs"; return NULL if no match.
964 : : *
965 : : * "exprs" can be either a list of bare expression trees, or a list of
966 : : * TargetEntry nodes. Typically it will contain Vars and possibly Aggrefs
967 : : * and WindowFuncs; however, when considering an appendrel member the list
968 : : * could contain arbitrary expressions. We consider an EC member to be
969 : : * computable if all the Vars, PlaceHolderVars, Aggrefs, and WindowFuncs
970 : : * it needs are present in "exprs".
971 : : *
972 : : * There is some subtlety in that definition: for example, if an EC member is
973 : : * Var_A + 1 while what is in "exprs" is Var_A + 2, it's still computable.
974 : : * This works because in the final plan tree, the EC member's expression will
975 : : * be computed as part of the same plan node targetlist that is currently
976 : : * represented by "exprs". So if we have Var_A available for the existing
977 : : * tlist member, it must be OK to use it in the EC expression too.
978 : : *
979 : : * Unlike find_ec_member_matching_expr, there's no special provision here
980 : : * for binary-compatible relabeling. This is intentional: if we have to
981 : : * compute an expression in this way, setrefs.c is going to insist on exact
982 : : * matches of Vars to the source tlist.
983 : : *
984 : : * Child EC members are ignored unless they belong to given 'relids'.
985 : : * Also, non-parallel-safe expressions are ignored if 'require_parallel_safe'.
986 : : *
987 : : * Note: some callers pass root == NULL for notational reasons. This is OK
988 : : * when require_parallel_safe is false.
989 : : */
990 : : EquivalenceMember *
991 : 7325 : find_computable_ec_member(PlannerInfo *root,
992 : : EquivalenceClass *ec,
993 : : List *exprs,
994 : : Relids relids,
995 : : bool require_parallel_safe)
996 : : {
997 : : List *exprvars;
998 : : EquivalenceMemberIterator it;
999 : : EquivalenceMember *em;
1000 : :
1001 : : /*
1002 : : * Pull out the Vars and quasi-Vars present in "exprs". In the typical
1003 : : * non-appendrel case, this is just another representation of the same
1004 : : * list. However, it does remove the distinction between the case of a
1005 : : * list of plain expressions and a list of TargetEntrys.
1006 : : */
684 1007 : 7325 : exprvars = pull_var_clause((Node *) exprs,
1008 : : PVC_INCLUDE_AGGREGATES |
1009 : : PVC_INCLUDE_WINDOWFUNCS |
1010 : : PVC_INCLUDE_PLACEHOLDERS);
1011 : :
506 drowley@postgresql.o 1012 : 7325 : setup_eclass_member_iterator(&it, ec, relids);
1013 [ + + ]: 14647 : while ((em = eclass_member_iterator_next(&it)) != NULL)
1014 : : {
1015 : : List *emvars;
1016 : : ListCell *lc2;
1017 : :
1018 : : /*
1019 : : * We shouldn't be trying to sort by an equivalence class that
1020 : : * contains a constant, so no need to consider such cases any further.
1021 : : */
1955 tgl@sss.pgh.pa.us 1022 [ - + ]: 7793 : if (em->em_is_const)
1955 tgl@sss.pgh.pa.us 1023 :UBC 0 : continue;
1024 : :
1025 : : /*
1026 : : * Ignore child members unless they belong to the requested rel.
1027 : : */
1955 tgl@sss.pgh.pa.us 1028 [ + + ]:CBC 7793 : if (em->em_is_child &&
1029 [ + + ]: 270 : !bms_is_subset(em->em_relids, relids))
1030 : 110 : continue;
1031 : :
1032 : : /*
1033 : : * Match if all Vars and quasi-Vars are present in "exprs".
1034 : : */
684 1035 : 7683 : emvars = pull_var_clause((Node *) em->em_expr,
1036 : : PVC_INCLUDE_AGGREGATES |
1037 : : PVC_INCLUDE_WINDOWFUNCS |
1038 : : PVC_INCLUDE_PLACEHOLDERS);
1039 [ + + + + : 8566 : foreach(lc2, emvars)
+ + ]
1040 : : {
1041 [ + + ]: 7979 : if (!list_member(exprvars, lfirst(lc2)))
1955 1042 : 7096 : break;
1043 : : }
684 1044 : 7683 : list_free(emvars);
1955 1045 [ + + ]: 7683 : if (lc2)
1046 : 7096 : continue; /* we hit a non-available Var */
1047 : :
1048 : : /*
1049 : : * If requested, reject expressions that are not parallel-safe. We
1050 : : * check this last because it's a rather expensive test.
1051 : : */
1052 [ + + ]: 587 : if (require_parallel_safe &&
1053 [ + + ]: 277 : !is_parallel_safe(root, (Node *) em->em_expr))
1054 : 116 : continue;
1055 : :
1056 : 471 : return em; /* found usable expression */
1057 : : }
1058 : :
1059 : 6854 : return NULL;
1060 : : }
1061 : :
1062 : : /*
1063 : : * relation_can_be_sorted_early
1064 : : * Can this relation be sorted on this EC before the final output step?
1065 : : *
1066 : : * To succeed, we must find an EC member that prepare_sort_from_pathkeys knows
1067 : : * how to sort on, given the rel's reltarget as input. There are also a few
1068 : : * additional constraints based on the fact that the desired sort will be done
1069 : : * "early", within the scan/join part of the plan. Also, non-parallel-safe
1070 : : * expressions are ignored if 'require_parallel_safe'.
1071 : : *
1072 : : * At some point we might want to return the identified EquivalenceMember,
1073 : : * but for now, callers only want to know if there is one.
1074 : : */
1075 : : bool
1076 : 16609 : relation_can_be_sorted_early(PlannerInfo *root, RelOptInfo *rel,
1077 : : EquivalenceClass *ec, bool require_parallel_safe)
1078 : : {
1079 : 16609 : PathTarget *target = rel->reltarget;
1080 : : EquivalenceMember *em;
1081 : : ListCell *lc;
1082 : :
1083 : : /*
1084 : : * Reject volatile ECs immediately; such sorts must always be postponed.
1085 : : */
1086 [ + + ]: 16609 : if (ec->ec_has_volatile)
1087 : 60 : return false;
1088 : :
1089 : : /*
1090 : : * Try to find an EM directly matching some reltarget member.
1091 : : */
1092 [ + + + + : 38282 : foreach(lc, target->exprs)
+ + ]
1093 : : {
1094 : 31267 : Expr *targetexpr = (Expr *) lfirst(lc);
1095 : :
1096 : 31267 : em = find_ec_member_matching_expr(ec, targetexpr, rel->relids);
1097 [ + + ]: 31267 : if (!em)
2123 tomas.vondra@postgre 1098 : 21733 : continue;
1099 : :
1100 : : /*
1101 : : * Reject expressions involving set-returning functions, as those
1102 : : * can't be computed early either. (Note: this test and the following
1103 : : * one are effectively checking properties of targetexpr, so there's
1104 : : * no point in asking whether some other EC member would be better.)
1105 : : */
1485 tgl@sss.pgh.pa.us 1106 [ - + ]: 9534 : if (expression_returns_set((Node *) em->em_expr))
2075 tomas.vondra@postgre 1107 :UBC 0 : continue;
1108 : :
1109 : : /*
1110 : : * If requested, reject expressions that are not parallel-safe. We
1111 : : * check this last because it's a rather expensive test.
1112 : : */
1955 tgl@sss.pgh.pa.us 1113 [ + - ]:CBC 9534 : if (require_parallel_safe &&
1114 [ - + ]: 9534 : !is_parallel_safe(root, (Node *) em->em_expr))
2075 tomas.vondra@postgre 1115 :UBC 0 : continue;
1116 : :
1955 tgl@sss.pgh.pa.us 1117 :CBC 9534 : return true;
1118 : : }
1119 : :
1120 : : /*
1121 : : * Try to find an expression computable from the reltarget.
1122 : : */
1123 : 7015 : em = find_computable_ec_member(root, ec, target->exprs, rel->relids,
1124 : : require_parallel_safe);
1125 [ + + ]: 7015 : if (!em)
1126 : 6854 : return false;
1127 : :
1128 : : /*
1129 : : * Reject expressions involving set-returning functions, as those can't be
1130 : : * computed early either. (There's no point in looking for another EC
1131 : : * member in this case; since SRFs can't appear in WHERE, they cannot
1132 : : * belong to multi-member ECs.)
1133 : : */
1485 1134 [ + + ]: 161 : if (expression_returns_set((Node *) em->em_expr))
1955 1135 : 10 : return false;
1136 : :
1137 : 151 : return true;
1138 : : }
1139 : :
1140 : : /*
1141 : : * generate_base_implied_equalities
1142 : : * Generate any restriction clauses that we can deduce from equivalence
1143 : : * classes.
1144 : : *
1145 : : * When an EC contains pseudoconstants, our strategy is to generate
1146 : : * "member = const1" clauses where const1 is the first constant member, for
1147 : : * every other member (including other constants). If we are able to do this
1148 : : * then we don't need any "var = var" comparisons because we've successfully
1149 : : * constrained all the vars at their points of creation. If we fail to
1150 : : * generate any of these clauses due to lack of cross-type operators, we fall
1151 : : * back to the "ec_broken" strategy described below. (XXX if there are
1152 : : * multiple constants of different types, it's possible that we might succeed
1153 : : * in forming all the required clauses if we started from a different const
1154 : : * member; but this seems a sufficiently hokey corner case to not be worth
1155 : : * spending lots of cycles on.)
1156 : : *
1157 : : * For ECs that contain no pseudoconstants, we generate derived clauses
1158 : : * "member1 = member2" for each pair of members belonging to the same base
1159 : : * relation (actually, if there are more than two for the same base relation,
1160 : : * we only need enough clauses to link each to each other). This provides
1161 : : * the base case for the recursion: each row emitted by a base relation scan
1162 : : * will constrain all computable members of the EC to be equal. As each
1163 : : * join path is formed, we'll add additional derived clauses on-the-fly
1164 : : * to maintain this invariant (see generate_join_implied_equalities).
1165 : : *
1166 : : * If the opfamilies used by the EC do not provide complete sets of cross-type
1167 : : * equality operators, it is possible that we will fail to generate a clause
1168 : : * that must be generated to maintain the invariant. (An example: given
1169 : : * "WHERE a.x = b.y AND b.y = a.z", the scheme breaks down if we cannot
1170 : : * generate "a.x = a.z" as a restriction clause for A.) In this case we mark
1171 : : * the EC "ec_broken" and fall back to regurgitating its original source
1172 : : * RestrictInfos at appropriate times. We do not try to retract any derived
1173 : : * clauses already generated from the broken EC, so the resulting plan could
1174 : : * be poor due to bad selectivity estimates caused by redundant clauses. But
1175 : : * the correct solution to that is to fix the opfamilies ...
1176 : : *
1177 : : * Equality clauses derived by this function are passed off to
1178 : : * process_implied_equality (in plan/initsplan.c) to be inserted into the
1179 : : * restrictinfo datastructures. Note that this must be called after initial
1180 : : * scanning of the quals and before Path construction begins.
1181 : : *
1182 : : * We make no attempt to avoid generating duplicate RestrictInfos here: we
1183 : : * don't search existing source or derived clauses in the EC for matches. It
1184 : : * doesn't really seem worth the trouble to do so.
1185 : : */
1186 : : void
7159 1187 : 247878 : generate_base_implied_equalities(PlannerInfo *root)
1188 : : {
1189 : : int ec_index;
1190 : : ListCell *lc;
1191 : :
1192 : : /*
1193 : : * At this point, we're done absorbing knowledge of equivalences in the
1194 : : * query, so no further EC merging should happen, and ECs remaining in the
1195 : : * eq_classes list can be considered canonical. (But note that it's still
1196 : : * possible for new single-member ECs to be added through
1197 : : * get_eclass_for_sort_expr().)
1198 : : */
2594 drowley@postgresql.o 1199 : 247878 : root->ec_merging_done = true;
1200 : :
1201 : 247878 : ec_index = 0;
7159 tgl@sss.pgh.pa.us 1202 [ + + + + : 555782 : foreach(lc, root->eq_classes)
+ + ]
1203 : : {
1204 : 307904 : EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc);
2594 drowley@postgresql.o 1205 : 307904 : bool can_generate_joinclause = false;
1206 : : int i;
1207 : :
6860 bruce@momjian.us 1208 [ - + ]: 307904 : Assert(ec->ec_merged == NULL); /* else shouldn't be in list */
1209 [ - + ]: 307904 : Assert(!ec->ec_broken); /* not yet anyway... */
1210 : :
1211 : : /*
1212 : : * Generate implied equalities that are restriction clauses.
1213 : : * Single-member ECs won't generate any deductions, either here or at
1214 : : * the join level.
1215 : : */
2594 drowley@postgresql.o 1216 [ + + ]: 307904 : if (list_length(ec->ec_members) > 1)
1217 : : {
1218 [ + + ]: 221446 : if (ec->ec_has_const)
1219 : 160079 : generate_base_implied_equalities_const(root, ec);
1220 : : else
1221 : 61367 : generate_base_implied_equalities_no_const(root, ec);
1222 : :
1223 : : /* Recover if we failed to generate required derived clauses */
1224 [ + + ]: 221446 : if (ec->ec_broken)
1225 : 25 : generate_base_implied_equalities_broken(root, ec);
1226 : :
1227 : : /* Detect whether this EC might generate join clauses */
1228 : 221446 : can_generate_joinclause =
1229 : 221446 : (bms_membership(ec->ec_relids) == BMS_MULTIPLE);
1230 : : }
1231 : :
1232 : : /*
1233 : : * Mark the base rels cited in each eclass (which should all exist by
1234 : : * now) with the eq_classes indexes of all eclasses mentioning them.
1235 : : * This will let us avoid searching in subsequent lookups. While
1236 : : * we're at it, we can mark base rels that have pending eclass joins;
1237 : : * this is a cheap version of has_relevant_eclass_joinclause().
1238 : : */
1239 : 307904 : i = -1;
1240 [ + + ]: 693117 : while ((i = bms_next_member(ec->ec_relids, i)) > 0)
1241 : : {
1242 : 385213 : RelOptInfo *rel = root->simple_rel_array[i];
1243 : :
1244 : : /* ignore the RTE_GROUP RTE */
716 rguo@postgresql.org 1245 [ - + ]: 385213 : if (i == root->group_rtindex)
716 rguo@postgresql.org 1246 :UBC 0 : continue;
1247 : :
1305 tgl@sss.pgh.pa.us 1248 [ + + ]:CBC 385213 : if (rel == NULL) /* must be an outer join */
1249 : : {
1250 [ - + ]: 3317 : Assert(bms_is_member(i, root->outer_join_rels));
1251 : 3317 : continue;
1252 : : }
1253 : :
2594 drowley@postgresql.o 1254 [ - + ]: 381896 : Assert(rel->reloptkind == RELOPT_BASEREL);
1255 : :
1256 : 381896 : rel->eclass_indexes = bms_add_member(rel->eclass_indexes,
1257 : : ec_index);
1258 : :
1259 [ + + ]: 381896 : if (can_generate_joinclause)
1260 : 146670 : rel->has_eclass_joins = true;
1261 : : }
1262 : :
1263 : 307904 : ec_index++;
1264 : : }
7159 tgl@sss.pgh.pa.us 1265 : 247878 : }
1266 : :
1267 : : /*
1268 : : * generate_base_implied_equalities when EC contains pseudoconstant(s)
1269 : : */
1270 : : static void
1271 : 160079 : generate_base_implied_equalities_const(PlannerInfo *root,
1272 : : EquivalenceClass *ec)
1273 : : {
1274 : 160079 : EquivalenceMember *const_em = NULL;
1275 : : ListCell *lc;
1276 : :
1277 : : /*
1278 : : * In the trivial case where we just had one "var = const" clause, push
1279 : : * the original clause back into the main planner machinery. There is
1280 : : * nothing to be gained by doing it differently, and we save the effort to
1281 : : * re-build and re-analyze an equality clause that will be exactly
1282 : : * equivalent to the old one.
1283 : : */
6852 1284 [ + + + - ]: 308007 : if (list_length(ec->ec_members) == 2 &&
1285 : 147928 : list_length(ec->ec_sources) == 1)
1286 : : {
1287 : 147928 : RestrictInfo *restrictinfo = (RestrictInfo *) linitial(ec->ec_sources);
1288 : :
1305 1289 : 147928 : distribute_restrictinfo_to_rels(root, restrictinfo);
1290 : 147928 : return;
1291 : : }
1292 : :
1293 : : /* We don't expect any children yet */
506 drowley@postgresql.o 1294 [ - + ]: 12151 : Assert(ec->ec_childmembers == NULL);
1295 : :
1296 : : /*
1297 : : * Find the constant member to use. We prefer an actual constant to
1298 : : * pseudo-constants (such as Params), because the constraint exclusion
1299 : : * machinery might be able to exclude relations on the basis of generated
1300 : : * "var = const" equalities, but "var = param" won't work for that.
1301 : : */
7159 tgl@sss.pgh.pa.us 1302 [ + - + + : 29281 : foreach(lc, ec->ec_members)
+ + ]
1303 : : {
1304 : 29207 : EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1305 : :
1306 [ + + ]: 29207 : if (cur_em->em_is_const)
1307 : : {
1308 : 12156 : const_em = cur_em;
5053 1309 [ + + ]: 12156 : if (IsA(cur_em->em_expr, Const))
1310 : 12077 : break;
1311 : : }
1312 : : }
7159 1313 [ - + ]: 12151 : Assert(const_em != NULL);
1314 : :
1315 : : /* Generate a derived equality against each other member */
1316 [ + - + + : 48710 : foreach(lc, ec->ec_members)
+ + ]
1317 : : {
1318 : 36584 : EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1319 : : Oid eq_op;
1320 : : RestrictInfo *rinfo;
1321 : :
1322 : : /* Child members should not exist in ec_members */
506 drowley@postgresql.o 1323 [ - + ]: 36584 : Assert(!cur_em->em_is_child);
7159 tgl@sss.pgh.pa.us 1324 [ + + ]: 36584 : if (cur_em == const_em)
1325 : 12131 : continue;
1326 : 24453 : eq_op = select_equality_operator(ec,
1327 : : cur_em->em_datatype,
1328 : : const_em->em_datatype);
1329 [ + + ]: 24453 : if (!OidIsValid(eq_op))
1330 : : {
1331 : : /* failed... */
1332 : 25 : ec->ec_broken = true;
1333 : 25 : break;
1334 : : }
1335 : :
1336 : : /*
1337 : : * We use the constant's em_jdomain as qualscope, so that if the
1338 : : * generated clause is variable-free (i.e, both EMs are consts) it
1339 : : * will be enforced at the join domain level.
1340 : : */
2129 1341 : 24428 : rinfo = process_implied_equality(root, eq_op, ec->ec_collation,
1342 : : cur_em->em_expr, const_em->em_expr,
1305 1343 : 24428 : const_em->em_jdomain->jd_relids,
1344 : : ec->ec_min_security,
2129 1345 : 24428 : cur_em->em_is_const);
1346 : :
1347 : : /*
1348 : : * If the clause didn't degenerate to a constant, fill in the correct
1349 : : * markings for a mergejoinable clause, and save it as a derived
1350 : : * clause. (We will not re-use such clauses directly, but selectivity
1351 : : * estimation may consult those later. Note that this use of derived
1352 : : * clauses does not overlap with its use for join clauses, since we
1353 : : * never generate join clauses from an ec_has_const eclass.)
1354 : : */
1355 [ + - + + ]: 24428 : if (rinfo && rinfo->mergeopfamilies)
1356 : : {
1357 : : /* it's not redundant, so don't set parent_ec */
1358 : 24318 : rinfo->left_ec = rinfo->right_ec = ec;
1359 : 24318 : rinfo->left_em = cur_em;
1360 : 24318 : rinfo->right_em = const_em;
510 amitlan@postgresql.o 1361 : 24318 : ec_add_derived_clause(ec, rinfo);
1362 : : }
1363 : : }
1364 : : }
1365 : :
1366 : : /*
1367 : : * generate_base_implied_equalities when EC contains no pseudoconstants
1368 : : */
1369 : : static void
7159 tgl@sss.pgh.pa.us 1370 : 61367 : generate_base_implied_equalities_no_const(PlannerInfo *root,
1371 : : EquivalenceClass *ec)
1372 : : {
1373 : : EquivalenceMember **prev_ems;
1374 : : ListCell *lc;
1375 : :
1376 : : /*
1377 : : * We scan the EC members once and track the last-seen member for each
1378 : : * base relation. When we see another member of the same base relation,
1379 : : * we generate "prev_em = cur_em". This results in the minimum number of
1380 : : * derived clauses, but it's possible that it will fail when a different
1381 : : * ordering would succeed. XXX FIXME: use a UNION-FIND algorithm similar
1382 : : * to the way we build merged ECs. (Use a list-of-lists for each rel.)
1383 : : */
10 michael@paquier.xyz 1384 :GNC 61367 : prev_ems = palloc0_array(EquivalenceMember *, root->simple_rel_array_size);
1385 : :
1386 : : /* We don't expect any children yet */
506 drowley@postgresql.o 1387 [ - + ]:CBC 61367 : Assert(ec->ec_childmembers == NULL);
1388 : :
7159 tgl@sss.pgh.pa.us 1389 [ + - + + : 186441 : foreach(lc, ec->ec_members)
+ + ]
1390 : : {
1391 : 125074 : EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
1392 : : int relid;
1393 : :
1394 : : /* Child members should not exist in ec_members */
506 drowley@postgresql.o 1395 [ - + ]: 125074 : Assert(!cur_em->em_is_child);
1396 : :
4290 tgl@sss.pgh.pa.us 1397 [ + + ]: 125074 : if (!bms_get_singleton_member(cur_em->em_relids, &relid))
7159 1398 : 165 : continue;
1399 [ - + ]: 124909 : Assert(relid < root->simple_rel_array_size);
1400 : :
1401 [ + + ]: 124909 : if (prev_ems[relid] != NULL)
1402 : : {
1403 : 404 : EquivalenceMember *prev_em = prev_ems[relid];
1404 : : Oid eq_op;
1405 : : RestrictInfo *rinfo;
1406 : :
1407 : 404 : eq_op = select_equality_operator(ec,
1408 : : prev_em->em_datatype,
1409 : : cur_em->em_datatype);
1410 [ - + ]: 404 : if (!OidIsValid(eq_op))
1411 : : {
1412 : : /* failed... */
7159 tgl@sss.pgh.pa.us 1413 :UBC 0 : ec->ec_broken = true;
1414 : 0 : break;
1415 : : }
1416 : :
1417 : : /*
1418 : : * The expressions aren't constants, so the passed qualscope will
1419 : : * never be used to place the generated clause. We just need to
1420 : : * be sure it covers both expressions, which em_relids should do.
1421 : : */
2129 tgl@sss.pgh.pa.us 1422 :CBC 404 : rinfo = process_implied_equality(root, eq_op, ec->ec_collation,
1423 : : prev_em->em_expr, cur_em->em_expr,
1424 : : cur_em->em_relids,
1425 : : ec->ec_min_security,
1426 : : false);
1427 : :
1428 : : /*
1429 : : * If the clause didn't degenerate to a constant, fill in the
1430 : : * correct markings for a mergejoinable clause. We don't record
1431 : : * it as a derived clause, since we don't currently need to
1432 : : * re-find such clauses, and don't want to clutter the
1433 : : * derived-clause set with non-join clauses.
1434 : : */
1435 [ + - + - ]: 404 : if (rinfo && rinfo->mergeopfamilies)
1436 : : {
1437 : : /* it's not redundant, so don't set parent_ec */
1438 : 404 : rinfo->left_ec = rinfo->right_ec = ec;
1439 : 404 : rinfo->left_em = prev_em;
1440 : 404 : rinfo->right_em = cur_em;
1441 : : }
1442 : : }
7159 1443 : 124909 : prev_ems[relid] = cur_em;
1444 : : }
1445 : :
1446 : 61367 : pfree(prev_ems);
1447 : :
1448 : : /*
1449 : : * We also have to make sure that all the Vars used in the member clauses
1450 : : * will be available at any join node we might try to reference them at.
1451 : : * For the moment we force all the Vars to be available at all join nodes
1452 : : * for this eclass. Perhaps this could be improved by doing some
1453 : : * pre-analysis of which members we prefer to join, but it's no worse than
1454 : : * what happened in the pre-8.3 code. (Note: rebuild_eclass_attr_needed
1455 : : * needs to match this code.)
1456 : : */
1457 [ + - + + : 186441 : foreach(lc, ec->ec_members)
+ + ]
1458 : : {
1459 : 125074 : EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
6339 1460 : 125074 : List *vars = pull_var_clause((Node *) cur_em->em_expr,
1461 : : PVC_RECURSE_AGGREGATES |
1462 : : PVC_RECURSE_WINDOWFUNCS |
1463 : : PVC_INCLUDE_PLACEHOLDERS);
1464 : :
1471 1465 : 125074 : add_vars_to_targetlist(root, vars, ec->ec_relids);
7159 1466 : 125074 : list_free(vars);
1467 : : }
1468 : 61367 : }
1469 : :
1470 : : /*
1471 : : * generate_base_implied_equalities cleanup after failure
1472 : : *
1473 : : * What we must do here is push any zero- or one-relation source RestrictInfos
1474 : : * of the EC back into the main restrictinfo datastructures. Multi-relation
1475 : : * clauses will be regurgitated later by generate_join_implied_equalities().
1476 : : * (We do it this way to maintain continuity with the case that ec_broken
1477 : : * becomes set only after we've gone up a join level or two.) However, for
1478 : : * an EC that contains constants, we can adopt a simpler strategy and just
1479 : : * throw back all the source RestrictInfos immediately; that works because
1480 : : * we know that such an EC can't become broken later. (This rule justifies
1481 : : * ignoring ec_has_const ECs in generate_join_implied_equalities, even when
1482 : : * they are broken.)
1483 : : */
1484 : : static void
1485 : 25 : generate_base_implied_equalities_broken(PlannerInfo *root,
1486 : : EquivalenceClass *ec)
1487 : : {
1488 : : ListCell *lc;
1489 : :
1490 [ + - + + : 80 : foreach(lc, ec->ec_sources)
+ + ]
1491 : : {
1492 : 55 : RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
1493 : :
5243 1494 [ - + - - ]: 55 : if (ec->ec_has_const ||
5243 tgl@sss.pgh.pa.us 1495 :UBC 0 : bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
7159 tgl@sss.pgh.pa.us 1496 :CBC 55 : distribute_restrictinfo_to_rels(root, restrictinfo);
1497 : : }
1498 : 25 : }
1499 : :
1500 : :
1501 : : /*
1502 : : * generate_join_implied_equalities
1503 : : * Generate any join clauses that we can deduce from equivalence classes.
1504 : : *
1505 : : * At a join node, we must enforce restriction clauses sufficient to ensure
1506 : : * that all equivalence-class members computable at that node are equal.
1507 : : * Since the set of clauses to enforce can vary depending on which subset
1508 : : * relations are the inputs, we have to compute this afresh for each join
1509 : : * relation pair. Hence a fresh List of RestrictInfo nodes is built and
1510 : : * passed back on each call.
1511 : : *
1512 : : * In addition to its use at join nodes, this can be applied to generate
1513 : : * eclass-based join clauses for use in a parameterized scan of a base rel.
1514 : : * The reason for the asymmetry of specifying the inner rel as a RelOptInfo
1515 : : * and the outer rel by Relids is that this usage occurs before we have
1516 : : * built any join RelOptInfos.
1517 : : *
1518 : : * An annoying special case for parameterized scans is that the inner rel can
1519 : : * be an appendrel child (an "other rel"). In this case we must generate
1520 : : * appropriate clauses using child EC members. add_child_rel_equivalences
1521 : : * must already have been done for the child rel.
1522 : : *
1523 : : * The results are sufficient for use in merge, hash, and plain nestloop join
1524 : : * methods. We do not worry here about selecting clauses that are optimal
1525 : : * for use in a parameterized indexscan. indxpath.c makes its own selections
1526 : : * of clauses to use, and if the ones we pick here are redundant with those,
1527 : : * the extras will be eliminated at createplan time, using the parent_ec
1528 : : * markers that we provide (see is_redundant_derived_clause()).
1529 : : *
1530 : : * Because the same join clauses are likely to be needed multiple times as
1531 : : * we consider different join paths, we avoid generating multiple copies:
1532 : : * whenever we select a particular pair of EquivalenceMembers to join,
1533 : : * we check to see if the pair matches any original clause (in ec_sources)
1534 : : * or previously-built derived clause. This saves memory and allows
1535 : : * re-use of information cached in RestrictInfos. We also avoid generating
1536 : : * commutative duplicates, i.e. if the algorithm selects "a.x = b.y" but
1537 : : * we already have "b.y = a.x", we return the existing clause.
1538 : : *
1539 : : * If we are considering an outer join, sjinfo is the associated OJ info,
1540 : : * otherwise it can be NULL.
1541 : : *
1542 : : * join_relids should always equal bms_union(outer_relids, inner_rel->relids)
1543 : : * plus whatever add_outer_joins_to_relids() would add. We could simplify
1544 : : * this function's API by computing it internally, but most callers have the
1545 : : * value at hand anyway.
1546 : : */
1547 : : List *
1548 : 451724 : generate_join_implied_equalities(PlannerInfo *root,
1549 : : Relids join_relids,
1550 : : Relids outer_relids,
1551 : : RelOptInfo *inner_rel,
1552 : : SpecialJoinInfo *sjinfo)
1553 : : {
2594 drowley@postgresql.o 1554 : 451724 : List *result = NIL;
1555 : 451724 : Relids inner_relids = inner_rel->relids;
1556 : : Relids nominal_inner_relids;
1557 : : Relids nominal_join_relids;
1558 : : Bitmapset *matching_ecs;
1559 : : int i;
1560 : :
1561 : : /* If inner rel is a child, extra setup work is needed */
1562 [ + + + + : 451724 : if (IS_OTHER_REL(inner_rel))
- + ]
1563 : : {
1564 [ - + ]: 6332 : Assert(!bms_is_empty(inner_rel->top_parent_relids));
1565 : :
1566 : : /* Fetch relid set for the topmost parent rel */
1567 : 6332 : nominal_inner_relids = inner_rel->top_parent_relids;
1568 : : /* ECs will be marked with the parent's relid, not the child's */
1569 : 6332 : nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1198 tgl@sss.pgh.pa.us 1570 : 6332 : nominal_join_relids = add_outer_joins_to_relids(root,
1571 : : nominal_join_relids,
1572 : : sjinfo,
1573 : : NULL);
1574 : : }
1575 : : else
1576 : : {
2594 drowley@postgresql.o 1577 : 445392 : nominal_inner_relids = inner_relids;
1578 : 445392 : nominal_join_relids = join_relids;
1579 : : }
1580 : :
1581 : : /*
1582 : : * Examine all potentially-relevant eclasses.
1583 : : *
1584 : : * If we are considering an outer join, we must include "join" clauses
1585 : : * that mention either input rel plus the outer join's relid; these
1586 : : * represent post-join filter clauses that have to be applied at this
1587 : : * join. We don't have infrastructure that would let us identify such
1588 : : * eclasses cheaply, so just fall back to considering all eclasses
1589 : : * mentioning anything in nominal_join_relids.
1590 : : *
1591 : : * At inner joins, we can be smarter: only consider eclasses mentioning
1592 : : * both input rels.
1593 : : */
1198 tgl@sss.pgh.pa.us 1594 [ + + + + ]: 451724 : if (sjinfo && sjinfo->ojrelid != 0)
1281 1595 : 62902 : matching_ecs = get_eclass_indexes_for_relids(root, nominal_join_relids);
1596 : : else
1597 : 388822 : matching_ecs = get_common_eclass_indexes(root, nominal_inner_relids,
1598 : : outer_relids);
1599 : :
2594 drowley@postgresql.o 1600 : 451724 : i = -1;
1601 [ + + ]: 1201696 : while ((i = bms_next_member(matching_ecs, i)) >= 0)
1602 : : {
1603 : 749972 : EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
1604 : 749972 : List *sublist = NIL;
1605 : :
1606 : : /* ECs containing consts do not need any further enforcement */
1607 [ + + ]: 749972 : if (ec->ec_has_const)
1608 : 87838 : continue;
1609 : :
1610 : : /* Single-member ECs won't generate any deductions */
1611 [ + + ]: 662134 : if (list_length(ec->ec_members) <= 1)
1612 : 304572 : continue;
1613 : :
1614 : : /* Sanity check that this eclass overlaps the join */
1615 [ - + ]: 357562 : Assert(bms_overlap(ec->ec_relids, nominal_join_relids));
1616 : :
1617 [ + + ]: 357562 : if (!ec->ec_broken)
1618 : 357292 : sublist = generate_join_implied_equalities_normal(root,
1619 : : ec,
1620 : : join_relids,
1621 : : outer_relids,
1622 : : inner_relids);
1623 : :
1624 : : /* Recover if we failed to generate required derived clauses */
1625 [ + + ]: 357562 : if (ec->ec_broken)
1626 : 300 : sublist = generate_join_implied_equalities_broken(root,
1627 : : ec,
1628 : : nominal_join_relids,
1629 : : outer_relids,
1630 : : nominal_inner_relids,
1631 : : inner_rel);
1632 : :
1633 : 357562 : result = list_concat(result, sublist);
1634 : : }
1635 : :
1636 : 451724 : return result;
1637 : : }
1638 : :
1639 : : /*
1640 : : * generate_join_implied_equalities_for_ecs
1641 : : * As above, but consider only the listed ECs.
1642 : : *
1643 : : * For the sole current caller, we can assume sjinfo == NULL, that is we are
1644 : : * not interested in outer-join filter clauses. This might need to change
1645 : : * in future.
1646 : : */
1647 : : List *
3772 tgl@sss.pgh.pa.us 1648 : 7028 : generate_join_implied_equalities_for_ecs(PlannerInfo *root,
1649 : : List *eclasses,
1650 : : Relids join_relids,
1651 : : Relids outer_relids,
1652 : : RelOptInfo *inner_rel)
1653 : : {
7159 1654 : 7028 : List *result = NIL;
5243 1655 : 7028 : Relids inner_relids = inner_rel->relids;
1656 : : Relids nominal_inner_relids;
1657 : : Relids nominal_join_relids;
1658 : : ListCell *lc;
1659 : :
1660 : : /* If inner rel is a child, extra setup work is needed */
3433 rhaas@postgresql.org 1661 [ + + + - : 7028 : if (IS_OTHER_REL(inner_rel))
- + ]
1662 : : {
1663 [ - + ]: 20 : Assert(!bms_is_empty(inner_rel->top_parent_relids));
1664 : :
1665 : : /* Fetch relid set for the topmost parent rel */
1666 : 20 : nominal_inner_relids = inner_rel->top_parent_relids;
1667 : : /* ECs will be marked with the parent's relid, not the child's */
5243 tgl@sss.pgh.pa.us 1668 : 20 : nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
1669 : : }
1670 : : else
1671 : : {
1672 : 7008 : nominal_inner_relids = inner_relids;
1673 : 7008 : nominal_join_relids = join_relids;
1674 : : }
1675 : :
3772 1676 [ + - + + : 14471 : foreach(lc, eclasses)
+ + ]
1677 : : {
7159 1678 : 7443 : EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc);
6860 bruce@momjian.us 1679 : 7443 : List *sublist = NIL;
1680 : :
1681 : : /* ECs containing consts do not need any further enforcement */
7159 tgl@sss.pgh.pa.us 1682 [ - + ]: 7443 : if (ec->ec_has_const)
7159 tgl@sss.pgh.pa.us 1683 :UBC 0 : continue;
1684 : :
1685 : : /* Single-member ECs won't generate any deductions */
7159 tgl@sss.pgh.pa.us 1686 [ - + ]:CBC 7443 : if (list_length(ec->ec_members) <= 1)
7159 tgl@sss.pgh.pa.us 1687 :UBC 0 : continue;
1688 : :
1689 : : /* We can quickly ignore any that don't overlap the join, too */
5243 tgl@sss.pgh.pa.us 1690 [ - + ]:CBC 7443 : if (!bms_overlap(ec->ec_relids, nominal_join_relids))
7159 tgl@sss.pgh.pa.us 1691 :UBC 0 : continue;
1692 : :
7159 tgl@sss.pgh.pa.us 1693 [ + - ]:CBC 7443 : if (!ec->ec_broken)
1694 : 7443 : sublist = generate_join_implied_equalities_normal(root,
1695 : : ec,
1696 : : join_relids,
1697 : : outer_relids,
1698 : : inner_relids);
1699 : :
1700 : : /* Recover if we failed to generate required derived clauses */
1701 [ - + ]: 7443 : if (ec->ec_broken)
7159 tgl@sss.pgh.pa.us 1702 :UBC 0 : sublist = generate_join_implied_equalities_broken(root,
1703 : : ec,
1704 : : nominal_join_relids,
1705 : : outer_relids,
1706 : : nominal_inner_relids,
1707 : : inner_rel);
1708 : :
7159 tgl@sss.pgh.pa.us 1709 :CBC 7443 : result = list_concat(result, sublist);
1710 : : }
1711 : :
1712 : 7028 : return result;
1713 : : }
1714 : :
1715 : : /*
1716 : : * generate_join_implied_equalities for a still-valid EC
1717 : : */
1718 : : static List *
1719 : 364735 : generate_join_implied_equalities_normal(PlannerInfo *root,
1720 : : EquivalenceClass *ec,
1721 : : Relids join_relids,
1722 : : Relids outer_relids,
1723 : : Relids inner_relids)
1724 : : {
1725 : 364735 : List *result = NIL;
1726 : 364735 : List *new_members = NIL;
1727 : 364735 : List *outer_members = NIL;
1728 : 364735 : List *inner_members = NIL;
1729 : : EquivalenceMemberIterator it;
1730 : : EquivalenceMember *cur_em;
1731 : :
1732 : : /*
1733 : : * First, scan the EC to identify member values that are computable at the
1734 : : * outer rel, at the inner rel, or at this relation but not in either
1735 : : * input rel. The outer-rel members should already be enforced equal,
1736 : : * likewise for the inner-rel members. We'll need to create clauses to
1737 : : * enforce that any newly computable members are all equal to each other
1738 : : * as well as to at least one input member, plus enforce at least one
1739 : : * outer-rel member equal to at least one inner-rel member.
1740 : : */
506 drowley@postgresql.o 1741 : 364735 : setup_eclass_member_iterator(&it, ec, join_relids);
1742 [ + + ]: 1166352 : while ((cur_em = eclass_member_iterator_next(&it)) != NULL)
1743 : : {
1744 : : /*
1745 : : * We don't need to check explicitly for child EC members. This test
1746 : : * against join_relids will cause them to be ignored except when
1747 : : * considering a child inner rel, which is what we want.
1748 : : */
5243 tgl@sss.pgh.pa.us 1749 [ + + ]: 801617 : if (!bms_is_subset(cur_em->em_relids, join_relids))
1750 : 67845 : continue; /* not computable yet, or wrong child */
1751 : :
1752 [ + + ]: 733772 : if (bms_is_subset(cur_em->em_relids, outer_relids))
7159 1753 : 410648 : outer_members = lappend(outer_members, cur_em);
5243 1754 [ + + ]: 323124 : else if (bms_is_subset(cur_em->em_relids, inner_relids))
7159 1755 : 321294 : inner_members = lappend(inner_members, cur_em);
1756 : : else
1757 : 1830 : new_members = lappend(new_members, cur_em);
1758 : : }
1759 : :
1760 : : /*
1761 : : * First, select the joinclause if needed. We can equate any one outer
1762 : : * member to any one inner member, but we have to find a datatype
1763 : : * combination for which an opfamily member operator exists. If we have
1764 : : * choices, we prefer simple Var members (possibly with RelabelType) since
1765 : : * these are (a) cheapest to compute at runtime and (b) most likely to
1766 : : * have useful statistics. Also, prefer operators that are also
1767 : : * hashjoinable.
1768 : : */
1769 [ + + + + ]: 364735 : if (outer_members && inner_members)
1770 : : {
1771 : 308992 : EquivalenceMember *best_outer_em = NULL;
1772 : 308992 : EquivalenceMember *best_inner_em = NULL;
6860 bruce@momjian.us 1773 : 308992 : Oid best_eq_op = InvalidOid;
1774 : 308992 : int best_score = -1;
1775 : : RestrictInfo *rinfo;
1776 : : ListCell *lc1;
1777 : :
7159 tgl@sss.pgh.pa.us 1778 [ + - + + : 321836 : foreach(lc1, outer_members)
+ + ]
1779 : : {
1780 : 309057 : EquivalenceMember *outer_em = (EquivalenceMember *) lfirst(lc1);
1781 : : ListCell *lc2;
1782 : :
1783 [ + - + + : 321921 : foreach(lc2, inner_members)
+ + ]
1784 : : {
1785 : 309077 : EquivalenceMember *inner_em = (EquivalenceMember *) lfirst(lc2);
1786 : : Oid eq_op;
1787 : : int score;
1788 : :
1789 : 309077 : eq_op = select_equality_operator(ec,
1790 : : outer_em->em_datatype,
1791 : : inner_em->em_datatype);
1792 [ + + ]: 309077 : if (!OidIsValid(eq_op))
1793 : 30 : continue;
1794 : 309047 : score = 0;
1795 [ + + ]: 309047 : if (IsA(outer_em->em_expr, Var) ||
1796 [ + + ]: 17129 : (IsA(outer_em->em_expr, RelabelType) &&
1797 [ + + ]: 6328 : IsA(((RelabelType *) outer_em->em_expr)->arg, Var)))
1798 : 298125 : score++;
1799 [ + + ]: 309047 : if (IsA(inner_em->em_expr, Var) ||
1800 [ + + ]: 12402 : (IsA(inner_em->em_expr, RelabelType) &&
1801 [ + + ]: 10223 : IsA(((RelabelType *) inner_em->em_expr)->arg, Var)))
1802 : 306792 : score++;
5719 1803 [ + + ]: 309047 : if (op_hashjoinable(eq_op,
5780 1804 : 309047 : exprType((Node *) outer_em->em_expr)))
7159 1805 : 308988 : score++;
1806 [ + + ]: 309047 : if (score > best_score)
1807 : : {
1808 : 308962 : best_outer_em = outer_em;
1809 : 308962 : best_inner_em = inner_em;
1810 : 308962 : best_eq_op = eq_op;
1811 : 308962 : best_score = score;
1812 [ + + ]: 308962 : if (best_score == 3)
6860 bruce@momjian.us 1813 : 296213 : break; /* no need to look further */
1814 : : }
1815 : : }
7159 tgl@sss.pgh.pa.us 1816 [ + + ]: 309057 : if (best_score == 3)
6860 bruce@momjian.us 1817 : 296213 : break; /* no need to look further */
1818 : : }
7159 tgl@sss.pgh.pa.us 1819 [ + + ]: 308992 : if (best_score < 0)
1820 : : {
1821 : : /* failed... */
1822 : 30 : ec->ec_broken = true;
1823 : 30 : return NIL;
1824 : : }
1825 : :
1826 : : /*
1827 : : * Create clause, setting parent_ec to mark it as redundant with other
1828 : : * joinclauses
1829 : : */
7157 1830 : 308962 : rinfo = create_join_clause(root, ec, best_eq_op,
1831 : : best_outer_em, best_inner_em,
1832 : : ec);
1833 : :
7159 1834 : 308962 : result = lappend(result, rinfo);
1835 : : }
1836 : :
1837 : : /*
1838 : : * Now deal with building restrictions for any expressions that involve
1839 : : * Vars from both sides of the join. We have to equate all of these to
1840 : : * each other as well as to at least one old member (if any).
1841 : : *
1842 : : * XXX as in generate_base_implied_equalities_no_const, we could be a lot
1843 : : * smarter here to avoid unnecessary failures in cross-type situations.
1844 : : * For now, use the same left-to-right method used there.
1845 : : */
1846 [ + + ]: 364705 : if (new_members)
1847 : : {
1848 : 1800 : List *old_members = list_concat(outer_members, inner_members);
1849 : 1800 : EquivalenceMember *prev_em = NULL;
1850 : : RestrictInfo *rinfo;
1851 : : ListCell *lc1;
1852 : :
1853 : : /* For now, arbitrarily take the first old_member as the one to use */
1854 [ + + ]: 1800 : if (old_members)
1855 : 1515 : new_members = lappend(new_members, linitial(old_members));
1856 : :
1857 [ + - + + : 5145 : foreach(lc1, new_members)
+ + ]
1858 : : {
506 drowley@postgresql.o 1859 : 3345 : cur_em = (EquivalenceMember *) lfirst(lc1);
1860 : :
7159 tgl@sss.pgh.pa.us 1861 [ + + ]: 3345 : if (prev_em != NULL)
1862 : : {
1863 : : Oid eq_op;
1864 : :
1865 : 1545 : eq_op = select_equality_operator(ec,
1866 : : prev_em->em_datatype,
1867 : : cur_em->em_datatype);
1868 [ - + ]: 1545 : if (!OidIsValid(eq_op))
1869 : : {
1870 : : /* failed... */
7159 tgl@sss.pgh.pa.us 1871 :UBC 0 : ec->ec_broken = true;
1872 : 0 : return NIL;
1873 : : }
1874 : : /* do NOT set parent_ec, this qual is not redundant! */
7157 tgl@sss.pgh.pa.us 1875 :CBC 1545 : rinfo = create_join_clause(root, ec, eq_op,
1876 : : prev_em, cur_em,
1877 : : NULL);
1878 : :
7159 1879 : 1545 : result = lappend(result, rinfo);
1880 : : }
1881 : 3345 : prev_em = cur_em;
1882 : : }
1883 : : }
1884 : :
1885 : 364705 : return result;
1886 : : }
1887 : :
1888 : : /*
1889 : : * generate_join_implied_equalities cleanup after failure
1890 : : *
1891 : : * Return any original RestrictInfos that are enforceable at this join.
1892 : : *
1893 : : * In the case of a child inner relation, we have to translate the
1894 : : * original RestrictInfos from parent to child Vars.
1895 : : */
1896 : : static List *
1897 : 300 : generate_join_implied_equalities_broken(PlannerInfo *root,
1898 : : EquivalenceClass *ec,
1899 : : Relids nominal_join_relids,
1900 : : Relids outer_relids,
1901 : : Relids nominal_inner_relids,
1902 : : RelOptInfo *inner_rel)
1903 : : {
1904 : 300 : List *result = NIL;
1905 : : ListCell *lc;
1906 : :
1907 [ + - + + : 820 : foreach(lc, ec->ec_sources)
+ + ]
1908 : : {
1909 : 520 : RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
5243 1910 : 520 : Relids clause_relids = restrictinfo->required_relids;
1911 : :
1912 [ + + ]: 520 : if (bms_is_subset(clause_relids, nominal_join_relids) &&
1913 [ + + ]: 280 : !bms_is_subset(clause_relids, outer_relids) &&
1914 [ + - ]: 260 : !bms_is_subset(clause_relids, nominal_inner_relids))
7159 1915 : 260 : result = lappend(result, restrictinfo);
1916 : : }
1917 : :
1918 : : /*
1919 : : * If we have to translate, just brute-force apply adjust_appendrel_attrs
1920 : : * to all the RestrictInfos at once. This will result in returning
1921 : : * RestrictInfos that are not included in EC's derived clauses, but there
1922 : : * shouldn't be any duplication, and it's a sufficiently narrow corner
1923 : : * case that we shouldn't sweat too much over it anyway.
1924 : : *
1925 : : * Since inner_rel might be an indirect descendant of the baserel
1926 : : * mentioned in the ec_sources clauses, we have to be prepared to apply
1927 : : * multiple levels of Var translation.
1928 : : */
3433 rhaas@postgresql.org 1929 [ + + + - : 300 : if (IS_OTHER_REL(inner_rel) && result != NIL)
- + + + ]
4348 tgl@sss.pgh.pa.us 1930 : 135 : result = (List *) adjust_appendrel_attrs_multilevel(root,
1931 : : (Node *) result,
1932 : : inner_rel,
1470 1933 : 135 : inner_rel->top_parent);
1934 : :
7159 1935 : 300 : return result;
1936 : : }
1937 : :
1938 : :
1939 : : /*
1940 : : * select_equality_operator
1941 : : * Select a suitable equality operator for comparing two EC members
1942 : : *
1943 : : * Returns InvalidOid if no operator can be found for this datatype combination
1944 : : */
1945 : : static Oid
6860 bruce@momjian.us 1946 : 438808 : select_equality_operator(EquivalenceClass *ec, Oid lefttype, Oid righttype)
1947 : : {
1948 : : ListCell *lc;
1949 : :
7159 tgl@sss.pgh.pa.us 1950 [ + - + + : 438863 : foreach(lc, ec->ec_opfamilies)
+ + ]
1951 : : {
6860 bruce@momjian.us 1952 : 438808 : Oid opfamily = lfirst_oid(lc);
1953 : : Oid opno;
1954 : :
508 peter@eisentraut.org 1955 : 438808 : opno = get_opfamily_member_for_cmptype(opfamily, lefttype, righttype, COMPARE_EQ);
3508 tgl@sss.pgh.pa.us 1956 [ + + ]: 438808 : if (!OidIsValid(opno))
1957 : 55 : continue;
1958 : : /* If no barrier quals in query, don't worry about leaky operators */
1959 [ + + ]: 438753 : if (ec->ec_max_security == 0)
1960 : 438753 : return opno;
1961 : : /* Otherwise, insist that selected operators be leakproof */
1962 [ + - ]: 839 : if (get_func_leakproof(get_opcode(opno)))
7159 1963 : 839 : return opno;
1964 : : }
1965 : 55 : return InvalidOid;
1966 : : }
1967 : :
1968 : :
1969 : : /*
1970 : : * create_join_clause
1971 : : * Find or make a RestrictInfo comparing the two given EC members
1972 : : * with the given operator (or, possibly, its commutator, because
1973 : : * the ordering of the operands in the result is not guaranteed).
1974 : : *
1975 : : * parent_ec is either equal to ec (if the clause is a potentially-redundant
1976 : : * join clause) or NULL (if not). We have to treat this as part of the
1977 : : * match requirements --- it's possible that a clause comparing the same two
1978 : : * EMs is a join clause in one join path and a restriction clause in another.
1979 : : */
1980 : : static RestrictInfo *
7157 1981 : 415814 : create_join_clause(PlannerInfo *root,
1982 : : EquivalenceClass *ec, Oid opno,
1983 : : EquivalenceMember *leftem,
1984 : : EquivalenceMember *rightem,
1985 : : EquivalenceClass *parent_ec)
1986 : : {
1987 : : RestrictInfo *rinfo;
1305 1988 : 415814 : RestrictInfo *parent_rinfo = NULL;
1989 : : MemoryContext oldcontext;
1990 : :
510 amitlan@postgresql.o 1991 : 415814 : rinfo = ec_search_clause_for_ems(root, ec, leftem, rightem, parent_ec);
1992 [ + + ]: 415814 : if (rinfo)
1993 : 346576 : return rinfo;
1994 : :
1995 : : /*
1996 : : * Not there, so build it, in planner context so we can re-use it. (Not
1997 : : * important in normal planning, but definitely so in GEQO.)
1998 : : */
7157 tgl@sss.pgh.pa.us 1999 : 69238 : oldcontext = MemoryContextSwitchTo(root->planner_cxt);
2000 : :
2001 : : /*
2002 : : * If either EM is a child, recursively create the corresponding
2003 : : * parent-to-parent clause, so that we can duplicate its rinfo_serial.
2004 : : */
1305 2005 [ + + + + ]: 69238 : if (leftem->em_is_child || rightem->em_is_child)
2006 : : {
2007 [ + + ]: 3679 : EquivalenceMember *leftp = leftem->em_parent ? leftem->em_parent : leftem;
2008 [ + + ]: 3679 : EquivalenceMember *rightp = rightem->em_parent ? rightem->em_parent : rightem;
2009 : :
2010 : 3679 : parent_rinfo = create_join_clause(root, ec, opno,
2011 : : leftp, rightp,
2012 : : parent_ec);
2013 : : }
2014 : :
2044 2015 : 69238 : rinfo = build_implied_join_equality(root,
2016 : : opno,
2017 : : ec->ec_collation,
2018 : : leftem->em_expr,
2019 : : rightem->em_expr,
6852 2020 : 69238 : bms_union(leftem->em_relids,
5061 2021 : 69238 : rightem->em_relids),
2022 : : ec->ec_min_security);
2023 : :
2024 : : /*
2025 : : * If either EM is a child, force the clause's clause_relids to include
2026 : : * the relid(s) of the child rel. In normal cases it would already, but
2027 : : * not if we are considering appendrel child relations with pseudoconstant
2028 : : * translated variables (i.e., UNION ALL sub-selects with constant output
2029 : : * items). We must do this so that join_clause_is_movable_into() will
2030 : : * think that the clause should be evaluated at the correct place.
2031 : : */
863 2032 [ + + ]: 69238 : if (leftem->em_is_child)
2033 : 3208 : rinfo->clause_relids = bms_add_members(rinfo->clause_relids,
2034 : 3208 : leftem->em_relids);
2035 [ + + ]: 69238 : if (rightem->em_is_child)
2036 : 471 : rinfo->clause_relids = bms_add_members(rinfo->clause_relids,
2037 : 471 : rightem->em_relids);
2038 : :
2039 : : /* If it's a child clause, copy the parent's rinfo_serial */
1305 2040 [ + + ]: 69238 : if (parent_rinfo)
2041 : 3679 : rinfo->rinfo_serial = parent_rinfo->rinfo_serial;
2042 : :
2043 : : /* Mark the clause as redundant, or not */
7157 2044 : 69238 : rinfo->parent_ec = parent_ec;
2045 : :
2046 : : /*
2047 : : * We know the correct values for left_ec/right_ec, ie this particular EC,
2048 : : * so we can just set them directly instead of forcing another lookup.
2049 : : */
2050 : 69238 : rinfo->left_ec = ec;
2051 : 69238 : rinfo->right_ec = ec;
2052 : :
2053 : : /* Mark it as usable with these EMs */
2054 : 69238 : rinfo->left_em = leftem;
2055 : 69238 : rinfo->right_em = rightem;
2056 : : /* and save it for possible re-use */
510 amitlan@postgresql.o 2057 : 69238 : ec_add_derived_clause(ec, rinfo);
2058 : :
7157 tgl@sss.pgh.pa.us 2059 : 69238 : MemoryContextSwitchTo(oldcontext);
2060 : :
2061 : 69238 : return rinfo;
2062 : : }
2063 : :
2064 : :
2065 : : /*
2066 : : * reconsider_outer_join_clauses
2067 : : * Re-examine any outer-join clauses that were set aside by
2068 : : * distribute_qual_to_rels(), and see if we can derive any
2069 : : * EquivalenceClasses from them. Then, if they were not made
2070 : : * redundant, push them out into the regular join-clause lists.
2071 : : *
2072 : : * When we have mergejoinable clauses A = B that are outer-join clauses,
2073 : : * we can't blindly combine them with other clauses A = C to deduce B = C,
2074 : : * since in fact the "equality" A = B won't necessarily hold above the
2075 : : * outer join (one of the variables might be NULL instead). Nonetheless
2076 : : * there are cases where we can add qual clauses using transitivity.
2077 : : *
2078 : : * One case that we look for here is an outer-join clause OUTERVAR = INNERVAR
2079 : : * for which there is also an equivalence clause OUTERVAR = CONSTANT.
2080 : : * It is safe and useful to push a clause INNERVAR = CONSTANT into the
2081 : : * evaluation of the inner (nullable) relation, because any inner rows not
2082 : : * meeting this condition will not contribute to the outer-join result anyway.
2083 : : * (Any outer rows they could join to will be eliminated by the pushed-down
2084 : : * equivalence clause.)
2085 : : *
2086 : : * Note that the above rule does not work for full outer joins; nor is it
2087 : : * very interesting to consider cases where the generated equivalence clause
2088 : : * would involve relations outside the outer join, since such clauses couldn't
2089 : : * be pushed into the inner side's scan anyway. So the restriction to
2090 : : * outervar = pseudoconstant is not really giving up anything.
2091 : : *
2092 : : * For full-join cases, we can only do something useful if it's a FULL JOIN
2093 : : * USING and a merged column has an equivalence MERGEDVAR = CONSTANT.
2094 : : * By the time it gets here, the merged column will look like
2095 : : * COALESCE(LEFTVAR, RIGHTVAR)
2096 : : * and we will have a full-join clause LEFTVAR = RIGHTVAR that we can match
2097 : : * the COALESCE expression to. In this situation we can push LEFTVAR = CONSTANT
2098 : : * and RIGHTVAR = CONSTANT into the input relations, since any rows not
2099 : : * meeting these conditions cannot contribute to the join result.
2100 : : *
2101 : : * Again, there isn't any traction to be gained by trying to deal with
2102 : : * clauses comparing a mergedvar to a non-pseudoconstant. So we can make
2103 : : * use of the EquivalenceClasses to search for matching variables that were
2104 : : * equivalenced to constants. The interesting outer-join clauses were
2105 : : * accumulated for us by distribute_qual_to_rels.
2106 : : *
2107 : : * When we find one of these cases, we implement the changes we want by
2108 : : * generating a new equivalence clause INNERVAR = CONSTANT (or LEFTVAR, etc)
2109 : : * and pushing it into the EquivalenceClass structures. This is because we
2110 : : * may already know that INNERVAR is equivalenced to some other var(s), and
2111 : : * we'd like the constant to propagate to them too. Note that it would be
2112 : : * unsafe to merge any existing EC for INNERVAR with the OUTERVAR's EC ---
2113 : : * that could result in propagating constant restrictions from
2114 : : * INNERVAR to OUTERVAR, which would be very wrong.
2115 : : *
2116 : : * It's possible that the INNERVAR is also an OUTERVAR for some other
2117 : : * outer-join clause, in which case the process can be repeated. So we repeat
2118 : : * looping over the lists of clauses until no further deductions can be made.
2119 : : * Whenever we do make a deduction, we remove the generating clause from the
2120 : : * lists, since we don't want to make the same deduction twice.
2121 : : *
2122 : : * If we don't find any match for a set-aside outer join clause, we must
2123 : : * throw it back into the regular joinclause processing by passing it to
2124 : : * distribute_restrictinfo_to_rels(). If we do generate a derived clause,
2125 : : * however, the outer-join clause is redundant. We must still put some
2126 : : * clause into the regular processing, because otherwise the join will be
2127 : : * seen as a clauseless join and avoided during join order searching.
2128 : : * We handle this by generating a constant-TRUE clause that is marked with
2129 : : * the same required_relids etc as the removed outer-join clause, thus
2130 : : * making it a join clause between the correct relations.
2131 : : */
2132 : : void
7159 2133 : 247878 : reconsider_outer_join_clauses(PlannerInfo *root)
2134 : : {
2135 : : bool found;
2136 : : ListCell *cell;
2137 : :
2138 : : /* Outer loop repeats until we find no more deductions */
2139 : : do
2140 : : {
6805 2141 : 249529 : found = false;
2142 : :
2143 : : /* Process the LEFT JOIN clauses */
2600 2144 [ + + + + : 269689 : foreach(cell, root->left_join_clauses)
+ + ]
2145 : : {
1305 2146 : 20160 : OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
2147 : :
2148 [ + + ]: 20160 : if (reconsider_outer_join_clause(root, ojcinfo, true))
2149 : : {
2150 : 516 : RestrictInfo *rinfo = ojcinfo->rinfo;
2151 : :
6805 2152 : 516 : found = true;
2153 : : /* remove it from the list */
2154 : 516 : root->left_join_clauses =
2600 2155 : 516 : foreach_delete_current(root->left_join_clauses, cell);
2156 : : /* throw back a dummy replacement clause (see notes above) */
1305 2157 : 516 : rinfo = make_restrictinfo(root,
2158 : 516 : (Expr *) makeBoolConst(true, false),
1190 2159 : 516 : rinfo->is_pushed_down,
2160 : 516 : rinfo->has_clone,
2161 : 516 : rinfo->is_clone,
2162 : : false, /* pseudoconstant */
2163 : : 0, /* security_level */
2164 : : rinfo->required_relids,
2165 : : rinfo->incompatible_relids,
2166 : : rinfo->outer_relids);
6805 2167 : 516 : distribute_restrictinfo_to_rels(root, rinfo);
2168 : : }
2169 : : }
2170 : :
2171 : : /* Process the RIGHT JOIN clauses */
2600 2172 [ + + + + : 278369 : foreach(cell, root->right_join_clauses)
+ + ]
2173 : : {
1305 2174 : 28840 : OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
2175 : :
2176 [ + + ]: 28840 : if (reconsider_outer_join_clause(root, ojcinfo, false))
2177 : : {
2178 : 1140 : RestrictInfo *rinfo = ojcinfo->rinfo;
2179 : :
6805 2180 : 1140 : found = true;
2181 : : /* remove it from the list */
2182 : 1140 : root->right_join_clauses =
2600 2183 : 1140 : foreach_delete_current(root->right_join_clauses, cell);
2184 : : /* throw back a dummy replacement clause (see notes above) */
1305 2185 : 1140 : rinfo = make_restrictinfo(root,
2186 : 1140 : (Expr *) makeBoolConst(true, false),
1190 2187 : 1140 : rinfo->is_pushed_down,
2188 : 1140 : rinfo->has_clone,
2189 : 1140 : rinfo->is_clone,
2190 : : false, /* pseudoconstant */
2191 : : 0, /* security_level */
2192 : : rinfo->required_relids,
2193 : : rinfo->incompatible_relids,
2194 : : rinfo->outer_relids);
6805 2195 : 1140 : distribute_restrictinfo_to_rels(root, rinfo);
2196 : : }
2197 : : }
2198 : :
2199 : : /* Process the FULL JOIN clauses */
2600 2200 [ + + + + : 250564 : foreach(cell, root->full_join_clauses)
+ + ]
2201 : : {
1305 2202 : 1035 : OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
2203 : :
2204 [ + + ]: 1035 : if (reconsider_full_join_clause(root, ojcinfo))
2205 : : {
2206 : 5 : RestrictInfo *rinfo = ojcinfo->rinfo;
2207 : :
6805 2208 : 5 : found = true;
2209 : : /* remove it from the list */
2210 : 5 : root->full_join_clauses =
2600 2211 : 5 : foreach_delete_current(root->full_join_clauses, cell);
2212 : : /* throw back a dummy replacement clause (see notes above) */
1305 2213 : 5 : rinfo = make_restrictinfo(root,
2214 : 5 : (Expr *) makeBoolConst(true, false),
1190 2215 : 5 : rinfo->is_pushed_down,
2216 : 5 : rinfo->has_clone,
2217 : 5 : rinfo->is_clone,
2218 : : false, /* pseudoconstant */
2219 : : 0, /* security_level */
2220 : : rinfo->required_relids,
2221 : : rinfo->incompatible_relids,
2222 : : rinfo->outer_relids);
6805 2223 : 5 : distribute_restrictinfo_to_rels(root, rinfo);
2224 : : }
2225 : : }
2226 [ + + ]: 249529 : } while (found);
2227 : :
2228 : : /* Now, any remaining clauses have to be thrown back */
2229 [ + + + + : 267206 : foreach(cell, root->left_join_clauses)
+ + ]
2230 : : {
1305 2231 : 19328 : OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
2232 : :
2233 : 19328 : distribute_restrictinfo_to_rels(root, ojcinfo->rinfo);
2234 : : }
6805 2235 [ + + + + : 274391 : foreach(cell, root->right_join_clauses)
+ + ]
2236 : : {
1305 2237 : 26513 : OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
2238 : :
2239 : 26513 : distribute_restrictinfo_to_rels(root, ojcinfo->rinfo);
2240 : : }
6805 2241 [ + + + + : 248908 : foreach(cell, root->full_join_clauses)
+ + ]
2242 : : {
1305 2243 : 1030 : OuterJoinClauseInfo *ojcinfo = (OuterJoinClauseInfo *) lfirst(cell);
2244 : :
2245 : 1030 : distribute_restrictinfo_to_rels(root, ojcinfo->rinfo);
2246 : : }
7159 2247 : 247878 : }
2248 : :
2249 : : /*
2250 : : * reconsider_outer_join_clauses for a single LEFT/RIGHT JOIN clause
2251 : : *
2252 : : * Returns true if we were able to propagate a constant through the clause.
2253 : : */
2254 : : static bool
1305 2255 : 49000 : reconsider_outer_join_clause(PlannerInfo *root, OuterJoinClauseInfo *ojcinfo,
2256 : : bool outer_on_left)
2257 : : {
2258 : 49000 : RestrictInfo *rinfo = ojcinfo->rinfo;
2259 : 49000 : SpecialJoinInfo *sjinfo = ojcinfo->sjinfo;
2260 : : Expr *outervar,
2261 : : *innervar;
2262 : : Oid opno,
2263 : : collation,
2264 : : left_type,
2265 : : right_type,
2266 : : inner_datatype;
2267 : : Relids inner_relids;
2268 : : ListCell *lc1;
2269 : :
7159 2270 [ - + ]: 49000 : Assert(is_opclause(rinfo->clause));
6805 2271 : 49000 : opno = ((OpExpr *) rinfo->clause)->opno;
5640 2272 : 49000 : collation = ((OpExpr *) rinfo->clause)->inputcollid;
2273 : :
2274 : : /* Extract needed info from the clause */
6805 2275 : 49000 : op_input_types(opno, &left_type, &right_type);
7159 2276 [ + + ]: 49000 : if (outer_on_left)
2277 : : {
2278 : 20160 : outervar = (Expr *) get_leftop(rinfo->clause);
2279 : 20160 : innervar = (Expr *) get_rightop(rinfo->clause);
2280 : 20160 : inner_datatype = right_type;
6852 2281 : 20160 : inner_relids = rinfo->right_relids;
2282 : : }
2283 : : else
2284 : : {
7159 2285 : 28840 : outervar = (Expr *) get_rightop(rinfo->clause);
2286 : 28840 : innervar = (Expr *) get_leftop(rinfo->clause);
2287 : 28840 : inner_datatype = left_type;
6852 2288 : 28840 : inner_relids = rinfo->left_relids;
2289 : : }
2290 : :
2291 : : /* Scan EquivalenceClasses for a match to outervar */
7159 2292 [ + - + + : 298020 : foreach(lc1, root->eq_classes)
+ + ]
2293 : : {
2294 : 250676 : EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
2295 : : bool match;
2296 : : ListCell *lc2;
2297 : :
2298 : : /* We don't expect any children yet */
506 drowley@postgresql.o 2299 [ - + ]: 250676 : Assert(cur_ec->ec_childmembers == NULL);
2300 : :
2301 : : /* Ignore EC unless it contains pseudoconstants */
7159 tgl@sss.pgh.pa.us 2302 [ + + ]: 250676 : if (!cur_ec->ec_has_const)
2303 : 194840 : continue;
2304 : : /* Never match to a volatile EC */
2305 [ - + ]: 55836 : if (cur_ec->ec_has_volatile)
7159 tgl@sss.pgh.pa.us 2306 :UBC 0 : continue;
2307 : : /* It has to match the outer-join clause as to semantics, too */
5640 tgl@sss.pgh.pa.us 2308 [ + + ]:CBC 55836 : if (collation != cur_ec->ec_collation)
2309 : 3651 : continue;
7159 2310 [ + + ]: 52185 : if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
2311 : 9301 : continue;
2312 : : /* Does it contain a match to outervar? */
2313 : 42884 : match = false;
2314 [ + - + + : 136671 : foreach(lc2, cur_ec->ec_members)
+ + ]
2315 : : {
2316 : 95443 : EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2317 : :
2318 : : /* Child members should not exist in ec_members */
506 drowley@postgresql.o 2319 [ - + ]: 95443 : Assert(!cur_em->em_is_child);
7159 tgl@sss.pgh.pa.us 2320 [ + + ]: 95443 : if (equal(outervar, cur_em->em_expr))
2321 : : {
2322 : 1656 : match = true;
2323 : 1656 : break;
2324 : : }
2325 : : }
2326 [ + + ]: 42884 : if (!match)
2327 : 41228 : continue; /* no match, so ignore this EC */
2328 : :
2329 : : /*
2330 : : * Yes it does! Try to generate a clause INNERVAR = CONSTANT for each
2331 : : * CONSTANT in the EC. Note that we must succeed with at least one
2332 : : * constant before we can decide to throw away the outer-join clause.
2333 : : */
2334 : 1656 : match = false;
2335 [ + - + + : 5861 : foreach(lc2, cur_ec->ec_members)
+ + ]
2336 : : {
2337 : 4205 : EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2338 : : Oid eq_op;
2339 : : RestrictInfo *newrinfo;
2340 : : JoinDomain *jdomain;
2341 : :
2342 [ + + ]: 4205 : if (!cur_em->em_is_const)
2343 : 2514 : continue; /* ignore non-const members */
2344 : 1691 : eq_op = select_equality_operator(cur_ec,
2345 : : inner_datatype,
2346 : : cur_em->em_datatype);
2347 [ - + ]: 1691 : if (!OidIsValid(eq_op))
7159 tgl@sss.pgh.pa.us 2348 :UBC 0 : continue; /* can't generate equality */
2044 tgl@sss.pgh.pa.us 2349 :CBC 1691 : newrinfo = build_implied_join_equality(root,
2350 : : eq_op,
2351 : : cur_ec->ec_collation,
2352 : : innervar,
2353 : : cur_em->em_expr,
2354 : : bms_copy(inner_relids),
2355 : : cur_ec->ec_min_security);
2356 : : /* This equality holds within the OJ's child JoinDomain */
1305 2357 : 1691 : jdomain = find_join_domain(root, sjinfo->syn_righthand);
2358 [ + - ]: 1691 : if (process_equivalence(root, &newrinfo, jdomain))
7159 2359 : 1691 : match = true;
2360 : : }
2361 : :
2362 : : /*
2363 : : * If we were able to equate INNERVAR to any constant, report success.
2364 : : * Otherwise, fall out of the search loop, since we know the OUTERVAR
2365 : : * appears in at most one EC.
2366 : : */
2367 [ + - ]: 1656 : if (match)
6805 2368 : 1656 : return true;
2369 : : else
7159 tgl@sss.pgh.pa.us 2370 :UBC 0 : break;
2371 : : }
2372 : :
6805 tgl@sss.pgh.pa.us 2373 :CBC 47344 : return false; /* failed to make any deduction */
2374 : : }
2375 : :
2376 : : /*
2377 : : * reconsider_outer_join_clauses for a single FULL JOIN clause
2378 : : *
2379 : : * Returns true if we were able to propagate a constant through the clause.
2380 : : */
2381 : : static bool
1305 2382 : 1035 : reconsider_full_join_clause(PlannerInfo *root, OuterJoinClauseInfo *ojcinfo)
2383 : : {
2384 : 1035 : RestrictInfo *rinfo = ojcinfo->rinfo;
2385 : 1035 : SpecialJoinInfo *sjinfo = ojcinfo->sjinfo;
2386 : 1035 : Relids fjrelids = bms_make_singleton(sjinfo->ojrelid);
2387 : : Expr *leftvar;
2388 : : Expr *rightvar;
2389 : : Oid opno,
2390 : : collation,
2391 : : left_type,
2392 : : right_type;
2393 : : Relids left_relids,
2394 : : right_relids;
2395 : : ListCell *lc1;
2396 : :
2397 : : /* Extract needed info from the clause */
7159 2398 [ - + ]: 1035 : Assert(is_opclause(rinfo->clause));
6805 2399 : 1035 : opno = ((OpExpr *) rinfo->clause)->opno;
5640 2400 : 1035 : collation = ((OpExpr *) rinfo->clause)->inputcollid;
6805 2401 : 1035 : op_input_types(opno, &left_type, &right_type);
7159 2402 : 1035 : leftvar = (Expr *) get_leftop(rinfo->clause);
2403 : 1035 : rightvar = (Expr *) get_rightop(rinfo->clause);
6852 2404 : 1035 : left_relids = rinfo->left_relids;
2405 : 1035 : right_relids = rinfo->right_relids;
2406 : :
7159 2407 [ + - + + : 5275 : foreach(lc1, root->eq_classes)
+ + ]
2408 : : {
2409 : 4245 : EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
2410 : 4245 : EquivalenceMember *coal_em = NULL;
2411 : : bool match;
2412 : : bool matchleft;
2413 : : bool matchright;
2414 : : ListCell *lc2;
2135 drowley@postgresql.o 2415 : 4245 : int coal_idx = -1;
2416 : :
2417 : : /* We don't expect any children yet */
506 2418 [ - + ]: 4245 : Assert(cur_ec->ec_childmembers == NULL);
2419 : :
2420 : : /* Ignore EC unless it contains pseudoconstants */
7159 tgl@sss.pgh.pa.us 2421 [ + + ]: 4245 : if (!cur_ec->ec_has_const)
2422 : 3995 : continue;
2423 : : /* Never match to a volatile EC */
2424 [ - + ]: 250 : if (cur_ec->ec_has_volatile)
7159 tgl@sss.pgh.pa.us 2425 :UBC 0 : continue;
2426 : : /* It has to match the outer-join clause as to semantics, too */
5640 tgl@sss.pgh.pa.us 2427 [ + + ]:CBC 250 : if (collation != cur_ec->ec_collation)
2428 : 30 : continue;
7159 2429 [ - + ]: 220 : if (!equal(rinfo->mergeopfamilies, cur_ec->ec_opfamilies))
7159 tgl@sss.pgh.pa.us 2430 :UBC 0 : continue;
2431 : :
2432 : : /*
2433 : : * Does it contain a COALESCE(leftvar, rightvar) construct?
2434 : : *
2435 : : * We can assume the COALESCE() inputs are in the same order as the
2436 : : * join clause, since both were automatically generated in the cases
2437 : : * we care about.
2438 : : *
2439 : : * XXX currently this may fail to match in cross-type cases because
2440 : : * the COALESCE will contain typecast operations while the join clause
2441 : : * may not (if there is a cross-type mergejoin operator available for
2442 : : * the two column types). Is it OK to strip implicit coercions from
2443 : : * the COALESCE arguments?
2444 : : */
7159 tgl@sss.pgh.pa.us 2445 :CBC 220 : match = false;
2446 [ + - + + : 645 : foreach(lc2, cur_ec->ec_members)
+ + ]
2447 : : {
2448 : 430 : coal_em = (EquivalenceMember *) lfirst(lc2);
2449 : :
2450 : : /* Child members should not exist in ec_members */
506 drowley@postgresql.o 2451 [ - + ]: 430 : Assert(!coal_em->em_is_child);
7159 tgl@sss.pgh.pa.us 2452 [ + + ]: 430 : if (IsA(coal_em->em_expr, CoalesceExpr))
2453 : : {
2454 : 15 : CoalesceExpr *cexpr = (CoalesceExpr *) coal_em->em_expr;
2455 : : Node *cfirst;
2456 : : Node *csecond;
2457 : :
2458 [ - + ]: 15 : if (list_length(cexpr->args) != 2)
7159 tgl@sss.pgh.pa.us 2459 :UBC 0 : continue;
7159 tgl@sss.pgh.pa.us 2460 :CBC 15 : cfirst = (Node *) linitial(cexpr->args);
2461 : 15 : csecond = (Node *) lsecond(cexpr->args);
2462 : :
2463 : : /*
2464 : : * The COALESCE arguments will be marked as possibly nulled by
2465 : : * the full join, while we wish to generate clauses that apply
2466 : : * to the join's inputs. So we must strip the join from the
2467 : : * nullingrels fields of cfirst/csecond before comparing them
2468 : : * to leftvar/rightvar. (Perhaps with a less hokey
2469 : : * representation for FULL JOIN USING output columns, this
2470 : : * wouldn't be needed?)
2471 : : */
1305 2472 : 15 : cfirst = remove_nulling_relids(cfirst, fjrelids, NULL);
2473 : 15 : csecond = remove_nulling_relids(csecond, fjrelids, NULL);
2474 : :
7159 2475 [ + + + - ]: 15 : if (equal(leftvar, cfirst) && equal(rightvar, csecond))
2476 : : {
2135 drowley@postgresql.o 2477 : 5 : coal_idx = foreach_current_index(lc2);
7159 tgl@sss.pgh.pa.us 2478 : 5 : match = true;
2479 : 5 : break;
2480 : : }
2481 : : }
2482 : : }
2483 [ + + ]: 220 : if (!match)
2484 : 215 : continue; /* no match, so ignore this EC */
2485 : :
2486 : : /*
2487 : : * Yes it does! Try to generate clauses LEFTVAR = CONSTANT and
2488 : : * RIGHTVAR = CONSTANT for each CONSTANT in the EC. Note that we must
2489 : : * succeed with at least one constant for each var before we can
2490 : : * decide to throw away the outer-join clause.
2491 : : */
2492 : 5 : matchleft = matchright = false;
2493 [ + - + + : 15 : foreach(lc2, cur_ec->ec_members)
+ + ]
2494 : : {
2495 : 10 : EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2496 : : Oid eq_op;
2497 : : RestrictInfo *newrinfo;
2498 : : JoinDomain *jdomain;
2499 : :
2500 [ + + ]: 10 : if (!cur_em->em_is_const)
2501 : 5 : continue; /* ignore non-const members */
2502 : 5 : eq_op = select_equality_operator(cur_ec,
2503 : : left_type,
2504 : : cur_em->em_datatype);
2505 [ + - ]: 5 : if (OidIsValid(eq_op))
2506 : : {
2044 2507 : 5 : newrinfo = build_implied_join_equality(root,
2508 : : eq_op,
2509 : : cur_ec->ec_collation,
2510 : : leftvar,
2511 : : cur_em->em_expr,
2512 : : bms_copy(left_relids),
2513 : : cur_ec->ec_min_security);
2514 : : /* This equality holds within the lefthand child JoinDomain */
1305 2515 : 5 : jdomain = find_join_domain(root, sjinfo->syn_lefthand);
2516 [ + - ]: 5 : if (process_equivalence(root, &newrinfo, jdomain))
7159 2517 : 5 : matchleft = true;
2518 : : }
2519 : 5 : eq_op = select_equality_operator(cur_ec,
2520 : : right_type,
2521 : : cur_em->em_datatype);
2522 [ + - ]: 5 : if (OidIsValid(eq_op))
2523 : : {
2044 2524 : 5 : newrinfo = build_implied_join_equality(root,
2525 : : eq_op,
2526 : : cur_ec->ec_collation,
2527 : : rightvar,
2528 : : cur_em->em_expr,
2529 : : bms_copy(right_relids),
2530 : : cur_ec->ec_min_security);
2531 : : /* This equality holds within the righthand child JoinDomain */
1305 2532 : 5 : jdomain = find_join_domain(root, sjinfo->syn_righthand);
2533 [ + - ]: 5 : if (process_equivalence(root, &newrinfo, jdomain))
7159 2534 : 5 : matchright = true;
2535 : : }
2536 : : }
2537 : :
2538 : : /*
2539 : : * If we were able to equate both vars to constants, we're done, and
2540 : : * we can throw away the full-join clause as redundant. Moreover, we
2541 : : * can remove the COALESCE entry from the EC, since the added
2542 : : * restrictions ensure it will always have the expected value. (We
2543 : : * don't bother trying to update ec_relids or ec_sources.)
2544 : : */
2545 [ + - + - ]: 5 : if (matchleft && matchright)
2546 : : {
2135 drowley@postgresql.o 2547 : 5 : cur_ec->ec_members = list_delete_nth_cell(cur_ec->ec_members, coal_idx);
6805 tgl@sss.pgh.pa.us 2548 : 5 : return true;
2549 : : }
2550 : :
2551 : : /*
2552 : : * Otherwise, fall out of the search loop, since we know the COALESCE
2553 : : * appears in at most one EC (XXX might stop being true if we allow
2554 : : * stripping of coercions above?)
2555 : : */
7159 tgl@sss.pgh.pa.us 2556 :UBC 0 : break;
2557 : : }
2558 : :
6805 tgl@sss.pgh.pa.us 2559 :CBC 1030 : return false; /* failed to make any deduction */
2560 : : }
2561 : :
2562 : : /*
2563 : : * rebuild_eclass_attr_needed
2564 : : * Put back attr_needed bits for Vars/PHVs needed for join eclasses.
2565 : : *
2566 : : * This is used to rebuild attr_needed/ph_needed sets after removal of a
2567 : : * useless outer join. It should match what
2568 : : * generate_base_implied_equalities_no_const did, except that we call
2569 : : * add_vars_to_attr_needed not add_vars_to_targetlist.
2570 : : */
2571 : : void
699 2572 : 9139 : rebuild_eclass_attr_needed(PlannerInfo *root)
2573 : : {
2574 : : ListCell *lc;
2575 : :
2576 [ + - + + : 51455 : foreach(lc, root->eq_classes)
+ + ]
2577 : : {
2578 : 42316 : EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc);
2579 : :
2580 : : /*
2581 : : * We don't expect any EC child members to exist at this point. Ensure
2582 : : * that's the case, otherwise, we might be getting asked to do
2583 : : * something this function hasn't been coded for.
2584 : : */
506 drowley@postgresql.o 2585 [ - + ]: 42316 : Assert(ec->ec_childmembers == NULL);
2586 : :
2587 : : /* Need do anything only for a multi-member, no-const EC. */
699 tgl@sss.pgh.pa.us 2588 [ + + + + ]: 42316 : if (list_length(ec->ec_members) > 1 && !ec->ec_has_const)
2589 : : {
2590 : : ListCell *lc2;
2591 : :
2592 [ + - + + : 12697 : foreach(lc2, ec->ec_members)
+ + ]
2593 : : {
2594 : 8490 : EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
2595 : 8490 : List *vars = pull_var_clause((Node *) cur_em->em_expr,
2596 : : PVC_RECURSE_AGGREGATES |
2597 : : PVC_RECURSE_WINDOWFUNCS |
2598 : : PVC_INCLUDE_PLACEHOLDERS);
2599 : :
2600 : 8490 : add_vars_to_attr_needed(root, vars, ec->ec_relids);
2601 : 8490 : list_free(vars);
2602 : : }
2603 : : }
2604 : : }
2605 : 9139 : }
2606 : :
2607 : : /*
2608 : : * find_join_domain
2609 : : * Find the highest JoinDomain enclosed within the given relid set.
2610 : : *
2611 : : * (We could avoid this search at the cost of complicating APIs elsewhere,
2612 : : * which doesn't seem worth it.)
2613 : : */
2614 : : static JoinDomain *
1305 2615 : 1701 : find_join_domain(PlannerInfo *root, Relids relids)
2616 : : {
2617 : : ListCell *lc;
2618 : :
2619 [ + - + - : 3489 : foreach(lc, root->join_domains)
+ - ]
2620 : : {
2621 : 3489 : JoinDomain *jdomain = (JoinDomain *) lfirst(lc);
2622 : :
2623 [ + + ]: 3489 : if (bms_is_subset(jdomain->jd_relids, relids))
2624 : 1701 : return jdomain;
2625 : : }
1305 tgl@sss.pgh.pa.us 2626 [ # # ]:UBC 0 : elog(ERROR, "failed to find appropriate JoinDomain");
2627 : : return NULL; /* keep compiler quiet */
2628 : : }
2629 : :
2630 : :
2631 : : /*
2632 : : * exprs_known_equal
2633 : : * Detect whether two expressions are known equal due to equivalence
2634 : : * relationships.
2635 : : *
2636 : : * If opfamily is given, the expressions must be known equal per the semantics
2637 : : * of that opfamily (note it has to be a btree opfamily, since those are the
2638 : : * only opfamilies equivclass.c deals with). If opfamily is InvalidOid, we'll
2639 : : * return true if they're equal according to any opfamily, which is fuzzy but
2640 : : * OK for estimation purposes.
2641 : : *
2642 : : * Note: does not bother to check for "equal(item1, item2)"; caller must
2643 : : * check that case if it's possible to pass identical items.
2644 : : */
2645 : : bool
758 rguo@postgresql.org 2646 :CBC 25946 : exprs_known_equal(PlannerInfo *root, Node *item1, Node *item2, Oid opfamily)
2647 : : {
2648 : : ListCell *lc1;
2649 : :
7159 tgl@sss.pgh.pa.us 2650 [ + + + + : 200349 : foreach(lc1, root->eq_classes)
+ + ]
2651 : : {
2652 : 177239 : EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc1);
2653 : 177239 : bool item1member = false;
2654 : 177239 : bool item2member = false;
2655 : : ListCell *lc2;
2656 : :
2657 : : /* Never match to a volatile EC */
2658 [ - + ]: 177239 : if (ec->ec_has_volatile)
7159 tgl@sss.pgh.pa.us 2659 :UBC 0 : continue;
2660 : :
2661 : : /*
2662 : : * It's okay to consider ec_broken ECs here. Brokenness just means we
2663 : : * couldn't derive all the implied clauses we'd have liked to; it does
2664 : : * not invalidate our knowledge that the members are equal.
2665 : : */
2666 : :
2667 : : /* Ignore if this EC doesn't use specified opfamily */
758 rguo@postgresql.org 2668 [ + + ]:CBC 177239 : if (OidIsValid(opfamily) &&
2669 [ + + ]: 550 : !list_member_oid(ec->ec_opfamilies, opfamily))
2670 : 190 : continue;
2671 : :
2672 : : /* Ignore children here */
7159 tgl@sss.pgh.pa.us 2673 [ + + + + : 423816 : foreach(lc2, ec->ec_members)
+ + ]
2674 : : {
2675 : 249603 : EquivalenceMember *em = (EquivalenceMember *) lfirst(lc2);
2676 : :
2677 : : /* Child members should not exist in ec_members */
506 drowley@postgresql.o 2678 [ - + ]: 249603 : Assert(!em->em_is_child);
7159 tgl@sss.pgh.pa.us 2679 [ + + ]: 249603 : if (equal(item1, em->em_expr))
2680 : 13534 : item1member = true;
2681 [ + + ]: 236069 : else if (equal(item2, em->em_expr))
2682 : 21516 : item2member = true;
2683 : : /* Exit as soon as equality is proven */
2684 [ + + + + ]: 249603 : if (item1member && item2member)
2685 : 2836 : return true;
2686 : : }
2687 : : }
2688 : 23110 : return false;
2689 : : }
2690 : :
2691 : :
2692 : : /*
2693 : : * match_eclasses_to_foreign_key_col
2694 : : * See whether a foreign key column match is proven by any eclass.
2695 : : *
2696 : : * If the referenced and referencing Vars of the fkey's colno'th column are
2697 : : * known equal due to any eclass, return that eclass; otherwise return NULL.
2698 : : * (In principle there might be more than one matching eclass if multiple
2699 : : * collations are involved, but since collation doesn't matter for equality,
2700 : : * we ignore that fine point here.) This is much like exprs_known_equal,
2701 : : * except for the format of the input.
2702 : : *
2703 : : * On success, we also set fkinfo->eclass[colno] to the matching eclass,
2704 : : * and set fkinfo->fk_eclass_member[colno] to the eclass member for the
2705 : : * referencing Var.
2706 : : */
2707 : : EquivalenceClass *
3722 2708 : 2183 : match_eclasses_to_foreign_key_col(PlannerInfo *root,
2709 : : ForeignKeyOptInfo *fkinfo,
2710 : : int colno)
2711 : : {
2712 : 2183 : Index var1varno = fkinfo->con_relid;
2713 : 2183 : AttrNumber var1attno = fkinfo->conkey[colno];
2714 : 2183 : Index var2varno = fkinfo->ref_relid;
2715 : 2183 : AttrNumber var2attno = fkinfo->confkey[colno];
2716 : 2183 : Oid eqop = fkinfo->conpfeqop[colno];
2594 drowley@postgresql.o 2717 : 2183 : RelOptInfo *rel1 = root->simple_rel_array[var1varno];
2718 : 2183 : RelOptInfo *rel2 = root->simple_rel_array[var2varno];
3354 tgl@sss.pgh.pa.us 2719 : 2183 : List *opfamilies = NIL; /* compute only if needed */
2720 : : Bitmapset *matching_ecs;
2721 : : int i;
2722 : :
2723 : : /* Consider only eclasses mentioning both relations */
2594 drowley@postgresql.o 2724 [ - + ]: 2183 : Assert(root->ec_merging_done);
2725 [ - + - - ]: 2183 : Assert(IS_SIMPLE_REL(rel1));
2726 [ - + - - ]: 2183 : Assert(IS_SIMPLE_REL(rel2));
2727 : 2183 : matching_ecs = bms_intersect(rel1->eclass_indexes,
2728 : 2183 : rel2->eclass_indexes);
2729 : :
2730 : 2183 : i = -1;
2731 [ + + ]: 2263 : while ((i = bms_next_member(matching_ecs, i)) >= 0)
2732 : : {
2733 : 645 : EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes,
2734 : : i);
2129 tgl@sss.pgh.pa.us 2735 : 645 : EquivalenceMember *item1_em = NULL;
2736 : 645 : EquivalenceMember *item2_em = NULL;
2737 : : ListCell *lc2;
2738 : :
2739 : : /* Never match to a volatile EC */
3722 2740 [ - + ]: 645 : if (ec->ec_has_volatile)
3722 tgl@sss.pgh.pa.us 2741 :UBC 0 : continue;
2742 : :
2743 : : /*
2744 : : * It's okay to consider "broken" ECs here, see exprs_known_equal.
2745 : : * Ignore children here.
2746 : : */
3722 tgl@sss.pgh.pa.us 2747 [ + - + + :CBC 1495 : foreach(lc2, ec->ec_members)
+ + ]
2748 : : {
2749 : 1415 : EquivalenceMember *em = (EquivalenceMember *) lfirst(lc2);
2750 : : Var *var;
2751 : :
2752 : : /* Child members should not exist in ec_members */
506 drowley@postgresql.o 2753 [ - + ]: 1415 : Assert(!em->em_is_child);
2754 : :
2755 : : /* EM must be a Var, possibly with RelabelType */
3722 tgl@sss.pgh.pa.us 2756 : 1415 : var = (Var *) em->em_expr;
2757 [ + - - + ]: 1415 : while (var && IsA(var, RelabelType))
3722 tgl@sss.pgh.pa.us 2758 :UBC 0 : var = (Var *) ((RelabelType *) var)->arg;
3722 tgl@sss.pgh.pa.us 2759 [ + - + + ]:CBC 1415 : if (!(var && IsA(var, Var)))
2760 : 15 : continue;
2761 : :
2762 : : /* Match? */
2763 [ + + + + ]: 1400 : if (var->varno == var1varno && var->varattno == var1attno)
2129 2764 : 565 : item1_em = em;
3722 2765 [ + + + + ]: 835 : else if (var->varno == var2varno && var->varattno == var2attno)
2129 2766 : 565 : item2_em = em;
2767 : :
2768 : : /* Have we found both PK and FK column in this EC? */
2769 [ + + + + ]: 1400 : if (item1_em && item2_em)
2770 : : {
2771 : : /*
2772 : : * Succeed if eqop matches EC's opfamilies. We could test
2773 : : * this before scanning the members, but it's probably cheaper
2774 : : * to test for member matches first.
2775 : : */
3722 2776 [ + - ]: 565 : if (opfamilies == NIL) /* compute if we didn't already */
2777 : 565 : opfamilies = get_mergejoin_opfamilies(eqop);
2778 [ + - ]: 565 : if (equal(opfamilies, ec->ec_opfamilies))
2779 : : {
2129 2780 : 565 : fkinfo->eclass[colno] = ec;
2781 : 565 : fkinfo->fk_eclass_member[colno] = item2_em;
3722 2782 : 565 : return ec;
2783 : : }
2784 : : /* Otherwise, done with this EC, move on to the next */
3722 tgl@sss.pgh.pa.us 2785 :UBC 0 : break;
2786 : : }
2787 : : }
2788 : : }
3722 tgl@sss.pgh.pa.us 2789 :CBC 1618 : return NULL;
2790 : : }
2791 : :
2792 : : /*
2793 : : * find_derived_clause_for_ec_member
2794 : : * Search for a previously-derived clause mentioning the given EM.
2795 : : *
2796 : : * The eclass should be an ec_has_const EC, of which the EM is a non-const
2797 : : * member. This should ensure there is just one derived clause mentioning
2798 : : * the EM (and equating it to a constant).
2799 : : * Returns NULL if no such clause can be found.
2800 : : */
2801 : : RestrictInfo *
510 amitlan@postgresql.o 2802 : 5 : find_derived_clause_for_ec_member(PlannerInfo *root,
2803 : : EquivalenceClass *ec,
2804 : : EquivalenceMember *em)
2805 : : {
2129 tgl@sss.pgh.pa.us 2806 [ - + ]: 5 : Assert(ec->ec_has_const);
2807 [ - + ]: 5 : Assert(!em->em_is_const);
2808 : :
510 amitlan@postgresql.o 2809 : 5 : return ec_search_derived_clause_for_ems(root, ec, em, NULL, NULL);
2810 : : }
2811 : :
2812 : :
2813 : : /*
2814 : : * add_child_rel_equivalences
2815 : : * Search for EC members that reference the root parent of child_rel, and
2816 : : * add transformed members referencing the child_rel.
2817 : : *
2818 : : * Note that this function won't be called at all unless we have at least some
2819 : : * reason to believe that the EC members it generates will be useful.
2820 : : *
2821 : : * parent_rel and child_rel could be derived from appinfo, but since the
2822 : : * caller has already computed them, we might as well just pass them in.
2823 : : *
2824 : : * The passed-in AppendRelInfo is not used when the parent_rel is not a
2825 : : * top-level baserel, since it shows the mapping from the parent_rel but
2826 : : * we need to translate EC expressions that refer to the top-level parent.
2827 : : * Using it is faster than using adjust_appendrel_attrs_multilevel(), though,
2828 : : * so we prefer it when we can.
2829 : : */
2830 : : void
7159 tgl@sss.pgh.pa.us 2831 : 28725 : add_child_rel_equivalences(PlannerInfo *root,
2832 : : AppendRelInfo *appinfo,
2833 : : RelOptInfo *parent_rel,
2834 : : RelOptInfo *child_rel)
2835 : : {
2487 2836 : 28725 : Relids top_parent_relids = child_rel->top_parent_relids;
2837 : 28725 : Relids child_relids = child_rel->relids;
2838 : : int i;
2839 : :
2840 : : /*
2841 : : * EC merging should be complete already, so we can use the parent rel's
2842 : : * eclass_indexes to avoid searching all of root->eq_classes.
2843 : : */
2594 drowley@postgresql.o 2844 [ - + ]: 28725 : Assert(root->ec_merging_done);
2845 [ + + - + ]: 28725 : Assert(IS_SIMPLE_REL(parent_rel));
2846 : :
2847 : 28725 : i = -1;
2848 [ + + ]: 81858 : while ((i = bms_next_member(parent_rel->eclass_indexes, i)) >= 0)
2849 : : {
2850 : 53133 : EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2851 : :
2852 : : /*
2853 : : * If this EC contains a volatile expression, then generating child
2854 : : * EMs would be downright dangerous, so skip it. We rely on a
2855 : : * volatile EC having only one EM.
2856 : : */
5291 tgl@sss.pgh.pa.us 2857 [ - + ]: 53133 : if (cur_ec->ec_has_volatile)
7159 tgl@sss.pgh.pa.us 2858 :UBC 0 : continue;
2859 : :
2860 : : /* Sanity check eclass_indexes only contain ECs for parent_rel */
2487 tgl@sss.pgh.pa.us 2861 [ - + ]:CBC 53133 : Assert(bms_is_subset(top_parent_relids, cur_ec->ec_relids));
2862 : :
506 drowley@postgresql.o 2863 [ + - + + : 181975 : foreach_node(EquivalenceMember, cur_em, cur_ec->ec_members)
+ + ]
2864 : : {
4535 tgl@sss.pgh.pa.us 2865 [ + + ]: 75709 : if (cur_em->em_is_const)
2866 : 2762 : continue; /* ignore consts here */
2867 : :
2868 : : /* Child members should not exist in ec_members */
506 drowley@postgresql.o 2869 [ - + ]: 72947 : Assert(!cur_em->em_is_child);
2870 : :
2871 : : /*
2872 : : * Consider only members that reference and can be computed at
2873 : : * child's topmost parent rel. In particular we want to exclude
2874 : : * parent-rel Vars that have nonempty varnullingrels. Translating
2875 : : * those might fail, if the transformed expression wouldn't be a
2876 : : * simple Var; and in any case it wouldn't produce a member that
2877 : : * has any use in creating plans for the child rel.
2878 : : */
1305 tgl@sss.pgh.pa.us 2879 [ + + ]: 72947 : if (bms_is_subset(cur_em->em_relids, top_parent_relids) &&
2880 [ + - ]: 51224 : !bms_is_empty(cur_em->em_relids))
2881 : : {
2882 : : /* OK, generate transformed child version */
2883 : : Expr *child_expr;
2884 : : Relids new_relids;
2885 : :
2632 2886 [ + + ]: 51224 : if (parent_rel->reloptkind == RELOPT_BASEREL)
2887 : : {
2888 : : /* Simple single-level transformation */
2889 : : child_expr = (Expr *)
2890 : 44027 : adjust_appendrel_attrs(root,
2891 : 44027 : (Node *) cur_em->em_expr,
2892 : : 1, &appinfo);
2893 : : }
2894 : : else
2895 : : {
2896 : : /* Must do multi-level transformation */
2897 : : child_expr = (Expr *)
2898 : 7197 : adjust_appendrel_attrs_multilevel(root,
2899 : 7197 : (Node *) cur_em->em_expr,
2900 : : child_rel,
1470 2901 : 7197 : child_rel->top_parent);
2902 : : }
2903 : :
2904 : : /*
2905 : : * Transform em_relids to match. Note we do *not* do
2906 : : * pull_varnos(child_expr) here, as for example the
2907 : : * transformation might have substituted a constant, but we
2908 : : * don't want the child member to be marked as constant.
2909 : : */
5243 2910 : 51224 : new_relids = bms_difference(cur_em->em_relids,
2911 : : top_parent_relids);
2487 2912 : 51224 : new_relids = bms_add_members(new_relids, child_relids);
2913 : :
506 drowley@postgresql.o 2914 : 51224 : add_child_eq_member(root,
2915 : : cur_ec,
2916 : : i,
2917 : : child_expr,
2918 : : new_relids,
2919 : : cur_em->em_jdomain,
2920 : : cur_em,
2921 : : cur_em->em_datatype,
2922 : : child_rel->relid);
2923 : : }
2924 : : }
2925 : : }
7159 tgl@sss.pgh.pa.us 2926 : 28725 : }
2927 : :
2928 : : /*
2929 : : * add_child_join_rel_equivalences
2930 : : * Like add_child_rel_equivalences(), but for joinrels
2931 : : *
2932 : : * Here we find the ECs relevant to the top parent joinrel and add transformed
2933 : : * member expressions that refer to this child joinrel.
2934 : : *
2935 : : * Note that this function won't be called at all unless we have at least some
2936 : : * reason to believe that the EC members it generates will be useful.
2937 : : */
2938 : : void
2487 2939 : 14885 : add_child_join_rel_equivalences(PlannerInfo *root,
2940 : : int nappinfos, AppendRelInfo **appinfos,
2941 : : RelOptInfo *parent_joinrel,
2942 : : RelOptInfo *child_joinrel)
2943 : : {
2944 : 14885 : Relids top_parent_relids = child_joinrel->top_parent_relids;
2945 : 14885 : Relids child_relids = child_joinrel->relids;
2946 : : Bitmapset *matching_ecs;
2947 : : MemoryContext oldcontext;
2948 : : int i;
2949 : :
2950 [ + - + - : 14885 : Assert(IS_JOIN_REL(child_joinrel) && IS_JOIN_REL(parent_joinrel));
+ + - + ]
2951 : :
2952 : : /* We need consider only ECs that mention the parent joinrel */
2953 : 14885 : matching_ecs = get_eclass_indexes_for_relids(root, top_parent_relids);
2954 : :
2955 : : /*
2956 : : * If we're being called during GEQO join planning, we still have to
2957 : : * create any new EC members in the main planner context, to avoid having
2958 : : * a corrupt EC data structure after the GEQO context is reset. This is
2959 : : * problematic since we'll leak memory across repeated GEQO cycles. For
2960 : : * now, though, bloat is better than crash. If it becomes a real issue
2961 : : * we'll have to do something to avoid generating duplicate EC members.
2962 : : */
2151 2963 : 14885 : oldcontext = MemoryContextSwitchTo(root->planner_cxt);
2964 : :
2487 2965 : 14885 : i = -1;
2966 [ + + ]: 39801 : while ((i = bms_next_member(matching_ecs, i)) >= 0)
2967 : : {
2968 : 24916 : EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
2969 : :
2970 : : /*
2971 : : * If this EC contains a volatile expression, then generating child
2972 : : * EMs would be downright dangerous, so skip it. We rely on a
2973 : : * volatile EC having only one EM.
2974 : : */
2975 [ - + ]: 24916 : if (cur_ec->ec_has_volatile)
2487 tgl@sss.pgh.pa.us 2976 :UBC 0 : continue;
2977 : :
2978 : : /* Sanity check on get_eclass_indexes_for_relids result */
2487 tgl@sss.pgh.pa.us 2979 [ - + ]:CBC 24916 : Assert(bms_overlap(top_parent_relids, cur_ec->ec_relids));
2980 : :
506 drowley@postgresql.o 2981 [ + - + + : 92622 : foreach_node(EquivalenceMember, cur_em, cur_ec->ec_members)
+ + ]
2982 : : {
2487 tgl@sss.pgh.pa.us 2983 [ + + ]: 42790 : if (cur_em->em_is_const)
2984 : 1898 : continue; /* ignore consts here */
2985 : :
2986 : : /* Child members should not exist in ec_members */
506 drowley@postgresql.o 2987 [ - + ]: 40892 : Assert(!cur_em->em_is_child);
2988 : :
2989 : : /*
2990 : : * We may ignore expressions that reference a single baserel,
2991 : : * because add_child_rel_equivalences should have handled them.
2992 : : */
2487 tgl@sss.pgh.pa.us 2993 [ + + ]: 40892 : if (bms_membership(cur_em->em_relids) != BMS_MULTIPLE)
2994 : 38715 : continue;
2995 : :
2996 : : /* Does this member reference child's topmost parent rel? */
2997 [ + - ]: 2177 : if (bms_overlap(cur_em->em_relids, top_parent_relids))
2998 : : {
2999 : : /* Yes, generate transformed child version */
3000 : : Expr *child_expr;
3001 : : Relids new_relids;
3002 : :
3003 [ + + ]: 2177 : if (parent_joinrel->reloptkind == RELOPT_JOINREL)
3004 : : {
3005 : : /* Simple single-level transformation */
3006 : : child_expr = (Expr *)
3007 : 2097 : adjust_appendrel_attrs(root,
3008 : 2097 : (Node *) cur_em->em_expr,
3009 : : nappinfos, appinfos);
3010 : : }
3011 : : else
3012 : : {
3013 : : /* Must do multi-level transformation */
3014 [ - + ]: 80 : Assert(parent_joinrel->reloptkind == RELOPT_OTHER_JOINREL);
3015 : : child_expr = (Expr *)
3016 : 80 : adjust_appendrel_attrs_multilevel(root,
3017 : 80 : (Node *) cur_em->em_expr,
3018 : : child_joinrel,
1470 3019 : 80 : child_joinrel->top_parent);
3020 : : }
3021 : :
3022 : : /*
3023 : : * Transform em_relids to match. Note we do *not* do
3024 : : * pull_varnos(child_expr) here, as for example the
3025 : : * transformation might have substituted a constant, but we
3026 : : * don't want the child member to be marked as constant.
3027 : : */
2487 3028 : 2177 : new_relids = bms_difference(cur_em->em_relids,
3029 : : top_parent_relids);
3030 : 2177 : new_relids = bms_add_members(new_relids, child_relids);
3031 : :
3032 : : /*
3033 : : * Add new child member to the EquivalenceClass. Because this
3034 : : * is a RELOPT_OTHER_JOINREL which has multiple component
3035 : : * relids, there is no ideal place to store these members in
3036 : : * the class. Ordinarily, child members are stored in the
3037 : : * ec_childmembers[] array element corresponding to their
3038 : : * relid, however, here we have multiple component relids, so
3039 : : * there's no single ec_childmembers[] array element to store
3040 : : * this member. So that we still correctly find this member
3041 : : * in loops iterating over an EquivalenceMemberIterator, we
3042 : : * opt to store the member in the ec_childmembers array in
3043 : : * only the first component relid slot of the array. This
3044 : : * allows the member to be found, providing callers of
3045 : : * setup_eclass_member_iterator() specify all the component
3046 : : * relids for the RELOPT_OTHER_JOINREL, which they do. If we
3047 : : * opted to store the member in each ec_childmembers[] element
3048 : : * for all the component relids, then that would just result
3049 : : * in eclass_member_iterator_next() finding the member
3050 : : * multiple times, which is a waste of effort.
3051 : : */
506 drowley@postgresql.o 3052 : 2177 : add_child_eq_member(root,
3053 : : cur_ec,
3054 : : -1,
3055 : : child_expr,
3056 : : new_relids,
3057 : : cur_em->em_jdomain,
3058 : : cur_em,
3059 : : cur_em->em_datatype,
3060 : 2177 : bms_next_member(child_joinrel->relids, -1));
3061 : : }
3062 : : }
3063 : : }
3064 : :
2151 tgl@sss.pgh.pa.us 3065 : 14885 : MemoryContextSwitchTo(oldcontext);
2487 3066 : 14885 : }
3067 : :
3068 : : /*
3069 : : * add_setop_child_rel_equivalences
3070 : : * Add equivalence members for each non-resjunk target in 'child_tlist'
3071 : : * to the EquivalenceClass in the corresponding setop_pathkey's pk_eclass.
3072 : : *
3073 : : * 'root' is the PlannerInfo belonging to the top-level set operation.
3074 : : * 'child_rel' is the RelOptInfo of the child relation we're adding
3075 : : * EquivalenceMembers for.
3076 : : * 'child_tlist' is the target list for the setop child relation. The target
3077 : : * list expressions are what we add as EquivalenceMembers.
3078 : : * 'setop_pathkeys' is a list of PathKeys which must contain an entry for each
3079 : : * non-resjunk target in 'child_tlist'.
3080 : : */
3081 : : void
828 rhaas@postgresql.org 3082 : 10323 : add_setop_child_rel_equivalences(PlannerInfo *root, RelOptInfo *child_rel,
3083 : : List *child_tlist, List *setop_pathkeys)
3084 : : {
3085 : : ListCell *lc;
3086 : 10323 : ListCell *lc2 = list_head(setop_pathkeys);
3087 : :
3088 [ + - + + : 41032 : foreach(lc, child_tlist)
+ + ]
3089 : : {
3090 : 30709 : TargetEntry *tle = lfirst_node(TargetEntry, lc);
3091 : : EquivalenceMember *parent_em;
3092 : : PathKey *pk;
3093 : :
3094 [ - + ]: 30709 : if (tle->resjunk)
828 rhaas@postgresql.org 3095 :UBC 0 : continue;
3096 : :
828 rhaas@postgresql.org 3097 [ - + ]:CBC 30709 : if (lc2 == NULL)
828 rhaas@postgresql.org 3098 [ # # ]:UBC 0 : elog(ERROR, "too few pathkeys for set operation");
3099 : :
828 rhaas@postgresql.org 3100 :CBC 30709 : pk = lfirst_node(PathKey, lc2);
3101 : 30709 : parent_em = linitial(pk->pk_eclass->ec_members);
3102 : :
3103 : : /*
3104 : : * We can safely pass the parent member as the first member in the
3105 : : * ec_members list as this is added first in generate_union_paths,
3106 : : * likewise, the JoinDomain can be that of the initial member of the
3107 : : * Pathkey's EquivalenceClass. We pass -1 for ec_index since we
3108 : : * maintain the eclass_indexes for the child_rel after the loop.
3109 : : */
506 drowley@postgresql.o 3110 : 30709 : add_child_eq_member(root,
3111 : : pk->pk_eclass,
3112 : : -1,
3113 : : tle->expr,
3114 : : child_rel->relids,
3115 : : parent_em->em_jdomain,
3116 : : parent_em,
3117 : 30709 : exprType((Node *) tle->expr),
3118 : : child_rel->relid);
3119 : :
828 rhaas@postgresql.org 3120 : 30709 : lc2 = lnext(setop_pathkeys, lc2);
3121 : : }
3122 : :
3123 : : /*
3124 : : * transformSetOperationStmt() ensures that the targetlist never contains
3125 : : * any resjunk columns, so all eclasses that exist in 'root' must have
3126 : : * received a new member in the loop above. Add them to the child_rel's
3127 : : * eclass_indexes.
3128 : : */
3129 : 10323 : child_rel->eclass_indexes = bms_add_range(child_rel->eclass_indexes, 0,
3130 : 10323 : list_length(root->eq_classes) - 1);
3131 : 10323 : }
3132 : :
3133 : : /*
3134 : : * setup_eclass_member_iterator
3135 : : * Setup an EquivalenceMemberIterator 'it' to iterate over all parent
3136 : : * EquivalenceMembers and child members belonging to the given 'ec'.
3137 : : *
3138 : : * This iterator returns:
3139 : : * - All parent members stored directly in ec_members for 'ec', and;
3140 : : * - Any child member added to the given ec by add_child_eq_member() where
3141 : : * the child_relid specified in the add_child_eq_member() call is a member
3142 : : * of the 'child_relids' parameter.
3143 : : *
3144 : : * Note:
3145 : : * The given 'child_relids' must remain allocated and not be changed for the
3146 : : * lifetime of the iterator.
3147 : : *
3148 : : * Parameters:
3149 : : * 'it' is a pointer to the iterator to set up. Normally stack allocated.
3150 : : * 'ec' is the EquivalenceClass from which to iterate members for.
3151 : : * 'child_relids' is the relids to return child members for.
3152 : : */
3153 : : void
506 drowley@postgresql.o 3154 : 4036999 : setup_eclass_member_iterator(EquivalenceMemberIterator *it,
3155 : : EquivalenceClass *ec, Relids child_relids)
3156 : : {
3157 : 4036999 : it->ec = ec;
3158 : : /* no need to set this if the class has no child members array set */
3159 [ + + ]: 4036999 : it->child_relids = ec->ec_childmembers != NULL ? child_relids : NULL;
3160 : 4036999 : it->current_relid = -1;
3161 : 4036999 : it->current_list = ec->ec_members;
3162 : 4036999 : it->current_cell = list_head(it->current_list);
3163 : 4036999 : }
3164 : :
3165 : : /*
3166 : : * eclass_member_iterator_next
3167 : : * Get the next EquivalenceMember from the EquivalenceMemberIterator 'it',
3168 : : * as setup by setup_eclass_member_iterator(). NULL is returned if there
3169 : : * are no members left, after which callers must not call
3170 : : * eclass_member_iterator_next() again for the given iterator.
3171 : : */
3172 : : EquivalenceMember *
3173 : 9381844 : eclass_member_iterator_next(EquivalenceMemberIterator *it)
3174 : : {
3175 [ + + ]: 9381844 : while (it->current_list != NULL)
3176 : : {
3177 [ + + ]: 9362976 : while (it->current_cell != NULL)
3178 : : {
3179 : : EquivalenceMember *em;
3180 : :
3181 : 6477551 : nextcell:
3182 : 6586247 : em = lfirst_node(EquivalenceMember, it->current_cell);
3183 : 6586247 : it->current_cell = lnext(it->current_list, it->current_cell);
3184 : 6586247 : return em;
3185 : : }
3186 : :
3187 : : /* Search for the next list to return members from */
3188 [ + + ]: 3001398 : while ((it->current_relid = bms_next_member(it->child_relids, it->current_relid)) > 0)
3189 : : {
3190 : : /*
3191 : : * Be paranoid in case we're given relids above what we've sized
3192 : : * the ec_childmembers array to.
3193 : : */
3194 [ - + ]: 224669 : if (it->current_relid >= it->ec->ec_childmembers_size)
506 drowley@postgresql.o 3195 :UBC 0 : return NULL;
3196 : :
506 drowley@postgresql.o 3197 :CBC 224669 : it->current_list = it->ec->ec_childmembers[it->current_relid];
3198 : :
3199 : : /* If there are members in this list, use it. */
3200 [ + + ]: 224669 : if (it->current_list != NIL)
3201 : : {
3202 : : /* point current_cell to the head of this list */
3203 : 108696 : it->current_cell = list_head(it->current_list);
3204 : 108696 : goto nextcell;
3205 : : }
3206 : : }
3207 : 2776729 : return NULL;
3208 : : }
3209 : :
3210 : 18868 : return NULL;
3211 : : }
3212 : :
3213 : : /*
3214 : : * generate_implied_equalities_for_column
3215 : : * Create EC-derived joinclauses usable with a specific column.
3216 : : *
3217 : : * This is used by indxpath.c to extract potentially indexable joinclauses
3218 : : * from ECs, and can be used by foreign data wrappers for similar purposes.
3219 : : * We assume that only expressions in Vars of a single table are of interest,
3220 : : * but the caller provides a callback function to identify exactly which
3221 : : * such expressions it would like to know about.
3222 : : *
3223 : : * We assume that any given table/index column could appear in only one EC.
3224 : : * (This should be true in all but the most pathological cases, and if it
3225 : : * isn't, we stop on the first match anyway.) Therefore, what we return
3226 : : * is a redundant list of clauses equating the table/index column to each of
3227 : : * the other-relation values it is known to be equal to. Any one of
3228 : : * these clauses can be used to create a parameterized path, and there
3229 : : * is no value in using more than one. (But it *is* worthwhile to create
3230 : : * a separate parameterized path for each one, since that leads to different
3231 : : * join orders.)
3232 : : *
3233 : : * The caller can pass a Relids set of rels we aren't interested in joining
3234 : : * to, so as to save the work of creating useless clauses.
3235 : : */
3236 : : List *
4907 tgl@sss.pgh.pa.us 3237 : 470020 : generate_implied_equalities_for_column(PlannerInfo *root,
3238 : : RelOptInfo *rel,
3239 : : ec_matches_callback_type callback,
3240 : : void *callback_arg,
3241 : : Relids prohibited_rels)
3242 : : {
7159 3243 : 470020 : List *result = NIL;
3244 : 470020 : bool is_child_rel = (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
3245 : : Relids parent_relids;
3246 : : int i;
3247 : :
3248 : : /* Should be OK to rely on eclass_indexes */
2594 drowley@postgresql.o 3249 [ - + ]: 470020 : Assert(root->ec_merging_done);
3250 : :
3251 : : /* Indexes are available only on base or "other" member relations. */
3433 rhaas@postgresql.org 3252 [ + + - + ]: 470020 : Assert(IS_SIMPLE_REL(rel));
3253 : :
3254 : : /* If it's a child rel, we'll need to know what its parent(s) are */
5326 tgl@sss.pgh.pa.us 3255 [ + + ]: 470020 : if (is_child_rel)
4348 3256 : 10172 : parent_relids = find_childrel_parents(root, rel);
3257 : : else
3258 : 459848 : parent_relids = NULL; /* not used, but keep compiler quiet */
3259 : :
2594 drowley@postgresql.o 3260 : 470020 : i = -1;
3261 [ + + ]: 1356069 : while ((i = bms_next_member(rel->eclass_indexes, i)) >= 0)
3262 : : {
3263 : 982485 : EquivalenceClass *cur_ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
3264 : : EquivalenceMemberIterator it;
3265 : : EquivalenceMember *cur_em;
3266 : : ListCell *lc2;
3267 : :
3268 : : /* Sanity check eclass_indexes only contain ECs for rel */
3269 [ + + - + ]: 982485 : Assert(is_child_rel || bms_is_subset(rel->relids, cur_ec->ec_relids));
3270 : :
3271 : : /*
3272 : : * Won't generate joinclauses if const or single-member (the latter
3273 : : * test covers the volatile case too)
3274 : : */
7159 tgl@sss.pgh.pa.us 3275 [ + + + + ]: 982485 : if (cur_ec->ec_has_const || list_length(cur_ec->ec_members) <= 1)
3276 : 885662 : continue;
3277 : :
3278 : : /*
3279 : : * Scan members, looking for a match to the target column. Note that
3280 : : * child EC members are considered, but only when they belong to the
3281 : : * target relation. (Unlike regular members, the same expression
3282 : : * could be a child member of more than one EC. Therefore, it's
3283 : : * potentially order-dependent which EC a child relation's target
3284 : : * column gets matched to. This is annoying but it only happens in
3285 : : * corner cases, so for now we live with just reporting the first
3286 : : * match. See also get_eclass_for_sort_expr.)
3287 : : */
506 drowley@postgresql.o 3288 : 463687 : setup_eclass_member_iterator(&it, cur_ec, rel->relids);
3289 [ + + ]: 1742042 : while ((cur_em = eclass_member_iterator_next(&it)) != NULL)
3290 : : {
5326 tgl@sss.pgh.pa.us 3291 [ + + + + ]: 1376153 : if (bms_equal(cur_em->em_relids, rel->relids) &&
4907 3292 : 464662 : callback(root, rel, cur_ec, cur_em, callback_arg))
5326 3293 : 96823 : break;
3294 : : }
3295 : :
3296 [ + + ]: 463687 : if (!cur_em)
7159 3297 : 366864 : continue;
3298 : :
3299 : : /*
3300 : : * Found our match. Scan the other EC members and attempt to generate
3301 : : * joinclauses. Ignore children here.
3302 : : */
3303 [ + - + + : 295821 : foreach(lc2, cur_ec->ec_members)
+ + ]
3304 : : {
5326 3305 : 198998 : EquivalenceMember *other_em = (EquivalenceMember *) lfirst(lc2);
3306 : : Oid eq_op;
3307 : : RestrictInfo *rinfo;
3308 : :
3309 : : /* Child members should not exist in ec_members */
506 drowley@postgresql.o 3310 [ - + ]: 198998 : Assert(!other_em->em_is_child);
3311 : :
3312 : : /* Make sure it'll be a join to a different rel */
5326 tgl@sss.pgh.pa.us 3313 [ + + + + ]: 303900 : if (other_em == cur_em ||
3314 : 104902 : bms_overlap(other_em->em_relids, rel->relids))
7159 3315 : 94443 : continue;
3316 : :
3317 : : /* Forget it if caller doesn't want joins to this rel */
5110 3318 [ + + ]: 104555 : if (bms_overlap(other_em->em_relids, prohibited_rels))
3319 : 130 : continue;
3320 : :
3321 : : /*
3322 : : * Also, if this is a child rel, avoid generating a useless join
3323 : : * to its parent rel(s).
3324 : : */
5326 3325 [ + + + + ]: 110496 : if (is_child_rel &&
4348 3326 : 6071 : bms_overlap(parent_relids, other_em->em_relids))
5326 3327 : 2797 : continue;
3328 : :
3329 : 101628 : eq_op = select_equality_operator(cur_ec,
3330 : : cur_em->em_datatype,
3331 : : other_em->em_datatype);
3332 [ - + ]: 101628 : if (!OidIsValid(eq_op))
5326 tgl@sss.pgh.pa.us 3333 :UBC 0 : continue;
3334 : :
3335 : : /* set parent_ec to mark as redundant with other joinclauses */
5326 tgl@sss.pgh.pa.us 3336 :CBC 101628 : rinfo = create_join_clause(root, cur_ec, eq_op,
3337 : : cur_em, other_em,
3338 : : cur_ec);
3339 : :
3340 : 101628 : result = lappend(result, rinfo);
3341 : : }
3342 : :
3343 : : /*
3344 : : * If somehow we failed to create any join clauses, we might as well
3345 : : * keep scanning the ECs for another match. But if we did make any,
3346 : : * we're done, because we don't want to return non-redundant clauses.
3347 : : */
3348 [ + + ]: 96823 : if (result)
3349 : 96436 : break;
3350 : : }
3351 : :
7159 3352 : 470020 : return result;
3353 : : }
3354 : :
3355 : : /*
3356 : : * have_relevant_eclass_joinclause
3357 : : * Detect whether there is an EquivalenceClass that could produce
3358 : : * a joinclause involving the two given relations.
3359 : : *
3360 : : * This is essentially a very cut-down version of
3361 : : * generate_join_implied_equalities(). Note it's OK to occasionally say "yes"
3362 : : * incorrectly. Hence we don't bother with details like whether the lack of a
3363 : : * cross-type operator might prevent the clause from actually being generated.
3364 : : * False negatives are not always fatal either: they will discourage, but not
3365 : : * completely prevent, investigation of particular join pathways.
3366 : : */
3367 : : bool
3368 : 170166 : have_relevant_eclass_joinclause(PlannerInfo *root,
3369 : : RelOptInfo *rel1, RelOptInfo *rel2)
3370 : : {
3371 : : Bitmapset *matching_ecs;
3372 : : int i;
3373 : :
3374 : : /*
3375 : : * Examine only eclasses mentioning both rel1 and rel2.
3376 : : *
3377 : : * Note that we do not consider the possibility of an eclass generating
3378 : : * "join" clauses that mention just one of the rels plus an outer join
3379 : : * that could be formed from them. Although such clauses must be
3380 : : * correctly enforced when we form the outer join, they don't seem like
3381 : : * sufficient reason to prioritize this join over other ones. The join
3382 : : * ordering rules will force the join to be made when necessary.
3383 : : */
2594 drowley@postgresql.o 3384 : 170166 : matching_ecs = get_common_eclass_indexes(root, rel1->relids,
3385 : : rel2->relids);
3386 : :
3387 : 170166 : i = -1;
3388 [ + + ]: 170256 : while ((i = bms_next_member(matching_ecs, i)) >= 0)
3389 : : {
3390 : 145249 : EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes,
3391 : : i);
3392 : :
3393 : : /*
3394 : : * Sanity check that get_common_eclass_indexes gave only ECs
3395 : : * containing both rels.
3396 : : */
3397 [ - + ]: 145249 : Assert(bms_overlap(rel1->relids, ec->ec_relids));
3398 [ - + ]: 145249 : Assert(bms_overlap(rel2->relids, ec->ec_relids));
3399 : :
3400 : : /*
3401 : : * Won't generate joinclauses if single-member (this test covers the
3402 : : * volatile case too)
3403 : : */
6805 tgl@sss.pgh.pa.us 3404 [ + + ]: 145249 : if (list_length(ec->ec_members) <= 1)
7159 3405 : 90 : continue;
3406 : :
3407 : : /*
3408 : : * We do not need to examine the individual members of the EC, because
3409 : : * all that we care about is whether each rel overlaps the relids of
3410 : : * at least one member, and get_common_eclass_indexes() and the single
3411 : : * member check above are sufficient to prove that. (As with
3412 : : * have_relevant_joinclause(), it is not necessary that the EC be able
3413 : : * to form a joinclause relating exactly the two given rels, only that
3414 : : * it be able to form a joinclause mentioning both, and this will
3415 : : * surely be true if both of them overlap ec_relids.)
3416 : : *
3417 : : * Note we don't test ec_broken; if we did, we'd need a separate code
3418 : : * path to look through ec_sources. Checking the membership anyway is
3419 : : * OK as a possibly-overoptimistic heuristic.
3420 : : *
3421 : : * We don't test ec_has_const either, even though a const eclass won't
3422 : : * generate real join clauses. This is because if we had "WHERE a.x =
3423 : : * b.y and a.x = 42", it is worth considering a join between a and b,
3424 : : * since the join result is likely to be small even though it'll end
3425 : : * up being an unqualified nestloop.
3426 : : */
3427 : :
2594 drowley@postgresql.o 3428 : 145159 : return true;
3429 : : }
3430 : :
7159 tgl@sss.pgh.pa.us 3431 : 25007 : return false;
3432 : : }
3433 : :
3434 : :
3435 : : /*
3436 : : * has_relevant_eclass_joinclause
3437 : : * Detect whether there is an EquivalenceClass that could produce
3438 : : * a joinclause involving the given relation and anything else.
3439 : : *
3440 : : * This is the same as have_relevant_eclass_joinclause with the other rel
3441 : : * implicitly defined as "everything else in the query".
3442 : : */
3443 : : bool
3444 : 179397 : has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
3445 : : {
3446 : : Bitmapset *matched_ecs;
3447 : : int i;
3448 : :
3449 : : /* Examine only eclasses mentioning rel1 */
2594 drowley@postgresql.o 3450 : 179397 : matched_ecs = get_eclass_indexes_for_relids(root, rel1->relids);
3451 : :
3452 : 179397 : i = -1;
3453 [ + + ]: 613265 : while ((i = bms_next_member(matched_ecs, i)) >= 0)
3454 : : {
3455 : 505224 : EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes,
3456 : : i);
3457 : :
3458 : : /*
3459 : : * Won't generate joinclauses if single-member (this test covers the
3460 : : * volatile case too)
3461 : : */
6805 tgl@sss.pgh.pa.us 3462 [ + + ]: 505224 : if (list_length(ec->ec_members) <= 1)
7159 3463 : 222925 : continue;
3464 : :
3465 : : /*
3466 : : * Per the comment in have_relevant_eclass_joinclause, it's sufficient
3467 : : * to find an EC that mentions both this rel and some other rel.
3468 : : */
2594 drowley@postgresql.o 3469 [ + + ]: 282299 : if (!bms_is_subset(ec->ec_relids, rel1->relids))
7159 tgl@sss.pgh.pa.us 3470 : 71356 : return true;
3471 : : }
3472 : :
3473 : 108041 : return false;
3474 : : }
3475 : :
3476 : :
3477 : : /*
3478 : : * eclass_useful_for_merging
3479 : : * Detect whether the EC could produce any mergejoinable join clauses
3480 : : * against the specified relation.
3481 : : *
3482 : : * This is just a heuristic test and doesn't have to be exact; it's better
3483 : : * to say "yes" incorrectly than "no". Hence we don't bother with details
3484 : : * like whether the lack of a cross-type operator might prevent the clause
3485 : : * from actually being generated.
3486 : : */
3487 : : bool
4039 3488 : 630458 : eclass_useful_for_merging(PlannerInfo *root,
3489 : : EquivalenceClass *eclass,
3490 : : RelOptInfo *rel)
3491 : : {
3492 : : Relids relids;
3493 : : ListCell *lc;
3494 : :
7159 3495 [ - + ]: 630458 : Assert(!eclass->ec_merged);
3496 : :
3497 : : /*
3498 : : * Won't generate joinclauses if const or single-member (the latter test
3499 : : * covers the volatile case too)
3500 : : */
3501 [ + + + + ]: 630458 : if (eclass->ec_has_const || list_length(eclass->ec_members) <= 1)
3502 : 31310 : return false;
3503 : :
3504 : : /*
3505 : : * Note we don't test ec_broken; if we did, we'd need a separate code path
3506 : : * to look through ec_sources. Checking the members anyway is OK as a
3507 : : * possibly-overoptimistic heuristic.
3508 : : */
3509 : :
3510 : : /* If specified rel is a child, we must consider the topmost parent rel */
3433 rhaas@postgresql.org 3511 [ + + + + : 599148 : if (IS_OTHER_REL(rel))
- + ]
3512 : : {
3513 [ - + ]: 4692 : Assert(!bms_is_empty(rel->top_parent_relids));
3514 : 4692 : relids = rel->top_parent_relids;
3515 : : }
3516 : : else
4039 tgl@sss.pgh.pa.us 3517 : 594456 : relids = rel->relids;
3518 : :
3519 : : /* If rel already includes all members of eclass, no point in searching */
3520 [ + + ]: 599148 : if (bms_is_subset(eclass->ec_relids, relids))
7159 3521 : 227010 : return false;
3522 : :
3523 : : /*
3524 : : * To join, we need a member not in the given rel. Ignore children here.
3525 : : */
3526 [ + - + + : 586913 : foreach(lc, eclass->ec_members)
+ + ]
3527 : : {
3528 : 586388 : EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
3529 : :
3530 : : /* Child members should not exist in ec_members */
506 drowley@postgresql.o 3531 [ - + ]: 586388 : Assert(!cur_em->em_is_child);
3532 : :
4039 tgl@sss.pgh.pa.us 3533 [ + + ]: 586388 : if (!bms_overlap(cur_em->em_relids, relids))
7159 3534 : 371613 : return true;
3535 : : }
3536 : :
3537 : 525 : return false;
3538 : : }
3539 : :
3540 : :
3541 : : /*
3542 : : * is_redundant_derived_clause
3543 : : * Test whether rinfo is derived from same EC as any clause in clauselist;
3544 : : * if so, it can be presumed to represent a condition that's redundant
3545 : : * with that member of the list.
3546 : : */
3547 : : bool
5243 3548 : 70 : is_redundant_derived_clause(RestrictInfo *rinfo, List *clauselist)
3549 : : {
3550 : 70 : EquivalenceClass *parent_ec = rinfo->parent_ec;
3551 : : ListCell *lc;
3552 : :
3553 : : /* Fail if it's not a potentially-redundant clause from some EC */
3554 [ + - ]: 70 : if (parent_ec == NULL)
3555 : 70 : return false;
3556 : :
5243 tgl@sss.pgh.pa.us 3557 [ # # # # :UBC 0 : foreach(lc, clauselist)
# # ]
3558 : : {
3559 : 0 : RestrictInfo *otherrinfo = (RestrictInfo *) lfirst(lc);
3560 : :
3561 [ # # ]: 0 : if (otherrinfo->parent_ec == parent_ec)
3562 : 0 : return true;
3563 : : }
3564 : :
3565 : 0 : return false;
3566 : : }
3567 : :
3568 : : /*
3569 : : * is_redundant_with_indexclauses
3570 : : * Test whether rinfo is redundant with any clause in the IndexClause
3571 : : * list. Here, for convenience, we test both simple identity and
3572 : : * whether it is derived from the same EC as any member of the list.
3573 : : */
3574 : : bool
2756 tgl@sss.pgh.pa.us 3575 :CBC 1153442 : is_redundant_with_indexclauses(RestrictInfo *rinfo, List *indexclauses)
3576 : : {
3577 : 1153442 : EquivalenceClass *parent_ec = rinfo->parent_ec;
3578 : : ListCell *lc;
3579 : :
3580 [ + + + + : 1605390 : foreach(lc, indexclauses)
+ + ]
3581 : : {
3582 : 1169272 : IndexClause *iclause = lfirst_node(IndexClause, lc);
3583 : 1169272 : RestrictInfo *otherrinfo = iclause->rinfo;
3584 : :
3585 : : /* If indexclause is lossy, it won't enforce the condition exactly */
3586 [ + + ]: 1169272 : if (iclause->lossy)
3587 : 33283 : continue;
3588 : :
3589 : : /* Match if it's same clause (pointer equality should be enough) */
3590 [ + + ]: 1135989 : if (rinfo == otherrinfo)
3591 : 717324 : return true;
3592 : : /* Match if derived from same EC */
3593 [ + + + + ]: 418992 : if (parent_ec && otherrinfo->parent_ec == parent_ec)
3594 : 327 : return true;
3595 : :
3596 : : /*
3597 : : * No need to look at the derived clauses in iclause->indexquals; they
3598 : : * couldn't match if the parent clause didn't.
3599 : : */
3600 : : }
3601 : :
3602 : 436118 : return false;
3603 : : }
3604 : :
3605 : : /*
3606 : : * get_eclass_indexes_for_relids
3607 : : * Build and return a Bitmapset containing the indexes into root's
3608 : : * eq_classes list for all eclasses that mention any of these relids
3609 : : */
3610 : : static Bitmapset *
2594 drowley@postgresql.o 3611 : 951393 : get_eclass_indexes_for_relids(PlannerInfo *root, Relids relids)
3612 : : {
3613 : 951393 : Bitmapset *ec_indexes = NULL;
3614 : 951393 : int i = -1;
3615 : :
3616 : : /* Should be OK to rely on eclass_indexes */
3617 [ - + ]: 951393 : Assert(root->ec_merging_done);
3618 : :
3619 [ + + ]: 2933584 : while ((i = bms_next_member(relids, i)) > 0)
3620 : : {
3621 : 1982191 : RelOptInfo *rel = root->simple_rel_array[i];
3622 : :
3623 : : /* ignore the RTE_GROUP RTE */
716 rguo@postgresql.org 3624 [ - + ]: 1982191 : if (i == root->group_rtindex)
716 rguo@postgresql.org 3625 :UBC 0 : continue;
3626 : :
1305 tgl@sss.pgh.pa.us 3627 [ + + ]:CBC 1982191 : if (rel == NULL) /* must be an outer join */
3628 : : {
3629 [ - + ]: 215252 : Assert(bms_is_member(i, root->outer_join_rels));
3630 : 215252 : continue;
3631 : : }
3632 : :
2594 drowley@postgresql.o 3633 : 1766939 : ec_indexes = bms_add_members(ec_indexes, rel->eclass_indexes);
3634 : : }
3635 : 951393 : return ec_indexes;
3636 : : }
3637 : :
3638 : : /*
3639 : : * get_common_eclass_indexes
3640 : : * Build and return a Bitmapset containing the indexes into root's
3641 : : * eq_classes list for all eclasses that mention rels in both
3642 : : * relids1 and relids2.
3643 : : */
3644 : : static Bitmapset *
3645 : 558988 : get_common_eclass_indexes(PlannerInfo *root, Relids relids1, Relids relids2)
3646 : : {
3647 : : Bitmapset *rel1ecs;
3648 : : Bitmapset *rel2ecs;
3649 : : int relid;
3650 : :
3651 : 558988 : rel1ecs = get_eclass_indexes_for_relids(root, relids1);
3652 : :
3653 : : /*
3654 : : * We can get away with just using the relation's eclass_indexes directly
3655 : : * when relids2 is a singleton set.
3656 : : */
3657 [ + + ]: 558988 : if (bms_get_singleton_member(relids2, &relid))
3658 : 423767 : rel2ecs = root->simple_rel_array[relid]->eclass_indexes;
3659 : : else
3660 : 135221 : rel2ecs = get_eclass_indexes_for_relids(root, relids2);
3661 : :
3662 : : /* Calculate and return the common EC indexes, recycling the left input. */
3663 : 558988 : return bms_int_members(rel1ecs, rel2ecs);
3664 : : }
3665 : :
3666 : : /*
3667 : : * ec_build_derives_hash
3668 : : * Construct the auxiliary hash table for derived clause lookups.
3669 : : */
3670 : : static void
510 amitlan@postgresql.o 3671 :UBC 0 : ec_build_derives_hash(PlannerInfo *root, EquivalenceClass *ec)
3672 : : {
3673 [ # # ]: 0 : Assert(!ec->ec_derives_hash);
3674 : :
3675 : : /*
3676 : : * Create the hash table.
3677 : : *
3678 : : * We pass list_length(ec->ec_derives_list) as the initial size.
3679 : : * Simplehash will divide this by the fillfactor (typically 0.9) and round
3680 : : * up to the next power of two, so this will usually give us at least 64
3681 : : * buckets around the threshold. That avoids immediate resizing without
3682 : : * hardcoding a specific size.
3683 : : */
3684 : 0 : ec->ec_derives_hash = derives_create(root->planner_cxt,
3685 : 0 : list_length(ec->ec_derives_list),
3686 : : NULL);
3687 : :
3688 [ # # # # : 0 : foreach_node(RestrictInfo, rinfo, ec->ec_derives_list)
# # ]
3689 : 0 : ec_add_clause_to_derives_hash(ec, rinfo);
3690 : 0 : }
3691 : :
3692 : : /*
3693 : : * ec_add_derived_clause
3694 : : * Add a clause to the set of derived clauses for the given
3695 : : * EquivalenceClass. Always appends to ec_derives_list; also adds
3696 : : * to ec_derives_hash if it exists.
3697 : : *
3698 : : * Also asserts expected invariants of derived clauses.
3699 : : */
3700 : : static void
510 amitlan@postgresql.o 3701 :CBC 93556 : ec_add_derived_clause(EquivalenceClass *ec, RestrictInfo *clause)
3702 : : {
3703 : : /*
3704 : : * Constant, if present, is always placed on the RHS; see
3705 : : * generate_base_implied_equalities_const(). LHS is never a constant.
3706 : : */
3707 [ - + ]: 93556 : Assert(!clause->left_em->em_is_const);
3708 : :
3709 : : /*
3710 : : * Clauses containing a constant are never considered redundant, so
3711 : : * parent_ec is not set.
3712 : : */
3713 [ + + - + ]: 93556 : Assert(!clause->parent_ec || !clause->right_em->em_is_const);
3714 : :
3715 : 93556 : ec->ec_derives_list = lappend(ec->ec_derives_list, clause);
3716 [ - + ]: 93556 : if (ec->ec_derives_hash)
510 amitlan@postgresql.o 3717 :UBC 0 : ec_add_clause_to_derives_hash(ec, clause);
510 amitlan@postgresql.o 3718 :CBC 93556 : }
3719 : :
3720 : : /*
3721 : : * ec_add_derived_clauses
3722 : : * Add a list of clauses to the set of clauses derived from the given
3723 : : * EquivalenceClass; adding to the list and hash table if needed.
3724 : : *
3725 : : * This function is similar to ec_add_derived_clause() but optimized for adding
3726 : : * multiple clauses at a time to the ec_derives_list. The assertions from
3727 : : * ec_add_derived_clause() are not repeated here, as the input clauses are
3728 : : * assumed to have already been validated.
3729 : : */
3730 : : static void
3731 : 33 : ec_add_derived_clauses(EquivalenceClass *ec, List *clauses)
3732 : : {
3733 : 33 : ec->ec_derives_list = list_concat(ec->ec_derives_list, clauses);
3734 [ - + ]: 33 : if (ec->ec_derives_hash)
510 amitlan@postgresql.o 3735 [ # # # # :UBC 0 : foreach_node(RestrictInfo, rinfo, clauses)
# # ]
3736 : 0 : ec_add_clause_to_derives_hash(ec, rinfo);
510 amitlan@postgresql.o 3737 :CBC 33 : }
3738 : :
3739 : : /*
3740 : : * fill_ec_derives_key
3741 : : * Compute a canonical key for ec_derives_hash lookup or insertion.
3742 : : *
3743 : : * Derived clauses are looked up using a pair of EquivalenceMembers and a
3744 : : * parent EquivalenceClass. To avoid storing or searching for both EM orderings,
3745 : : * we canonicalize the key:
3746 : : *
3747 : : * - For clauses involving two non-constant EMs, em1 is set to the EM with lower
3748 : : * memory address and em2 is set to the other one.
3749 : : * - For clauses involving a constant EM, the caller must pass the non-constant
3750 : : * EM as leftem and NULL as rightem; we then set em1 = NULL and em2 = leftem.
3751 : : */
3752 : : static inline void
510 amitlan@postgresql.o 3753 :UBC 0 : fill_ec_derives_key(ECDerivesKey *key,
3754 : : EquivalenceMember *leftem,
3755 : : EquivalenceMember *rightem,
3756 : : EquivalenceClass *parent_ec)
3757 : : {
3758 [ # # ]: 0 : Assert(leftem); /* Always required for lookup or insertion */
3759 : :
3760 [ # # ]: 0 : if (rightem == NULL)
3761 : : {
3762 : 0 : key->em1 = NULL;
3763 : 0 : key->em2 = leftem;
3764 : : }
3765 [ # # ]: 0 : else if (leftem < rightem)
3766 : : {
3767 : 0 : key->em1 = leftem;
3768 : 0 : key->em2 = rightem;
3769 : : }
3770 : : else
3771 : : {
3772 : 0 : key->em1 = rightem;
3773 : 0 : key->em2 = leftem;
3774 : : }
3775 : 0 : key->parent_ec = parent_ec;
3776 : 0 : }
3777 : :
3778 : : /*
3779 : : * ec_add_clause_to_derives_hash
3780 : : * Add a derived clause to ec_derives_hash in the given EquivalenceClass.
3781 : : *
3782 : : * Each clause is associated with a canonicalized key. For constant-containing
3783 : : * clauses, only the non-constant EM is used for lookup; see comments in
3784 : : * fill_ec_derives_key().
3785 : : */
3786 : : static void
3787 : 0 : ec_add_clause_to_derives_hash(EquivalenceClass *ec, RestrictInfo *rinfo)
3788 : : {
3789 : : ECDerivesKey key;
3790 : : ECDerivesEntry *entry;
3791 : : bool found;
3792 : :
3793 : : /*
3794 : : * Constants are always placed on the RHS; see
3795 : : * generate_base_implied_equalities_const().
3796 : : */
3797 [ # # ]: 0 : Assert(!rinfo->left_em->em_is_const);
3798 : :
3799 : : /*
3800 : : * Clauses containing a constant are never considered redundant, so
3801 : : * parent_ec is not set.
3802 : : */
3803 [ # # # # ]: 0 : Assert(!rinfo->parent_ec || !rinfo->right_em->em_is_const);
3804 : :
3805 : : /*
3806 : : * See fill_ec_derives_key() for details: we use a canonicalized key to
3807 : : * avoid storing both EM orderings. For constant EMs, only the
3808 : : * non-constant EM is included in the key.
3809 : : */
3810 : 0 : fill_ec_derives_key(&key,
3811 : : rinfo->left_em,
3812 [ # # ]: 0 : rinfo->right_em->em_is_const ? NULL : rinfo->right_em,
3813 : : rinfo->parent_ec);
3814 : 0 : entry = derives_insert(ec->ec_derives_hash, key, &found);
3815 [ # # ]: 0 : Assert(!found);
3816 : 0 : entry->rinfo = rinfo;
3817 : 0 : }
3818 : :
3819 : : /*
3820 : : * ec_clear_derived_clauses
3821 : : * Reset ec_derives_list and ec_derives_hash.
3822 : : *
3823 : : * We destroy the hash table explicitly, since it may consume significant
3824 : : * space. The list holds the same set of entries and can become equally large
3825 : : * when thousands of partitions are involved, so we free it as well -- even
3826 : : * though we do not typically free lists.
3827 : : */
3828 : : void
510 amitlan@postgresql.o 3829 :CBC 12790 : ec_clear_derived_clauses(EquivalenceClass *ec)
3830 : : {
3831 : 12790 : list_free(ec->ec_derives_list);
3832 : 12790 : ec->ec_derives_list = NIL;
3833 : :
3834 [ - + ]: 12790 : if (ec->ec_derives_hash)
3835 : : {
510 amitlan@postgresql.o 3836 :UBC 0 : derives_destroy(ec->ec_derives_hash);
3837 : 0 : ec->ec_derives_hash = NULL;
3838 : : }
510 amitlan@postgresql.o 3839 :CBC 12790 : }
3840 : :
3841 : : /*
3842 : : * ec_search_clause_for_ems
3843 : : * Search for an existing RestrictInfo that equates the given pair
3844 : : * of EquivalenceMembers, either from ec_sources or ec_derives.
3845 : : *
3846 : : * Returns a clause with matching operands in either given order or commuted
3847 : : * order. We used to require matching operator OIDs, but dropped that since any
3848 : : * semantically different operator here would indicate a broken operator family.
3849 : : *
3850 : : * Returns NULL if no matching clause is found.
3851 : : */
3852 : : static RestrictInfo *
3853 : 415814 : ec_search_clause_for_ems(PlannerInfo *root, EquivalenceClass *ec,
3854 : : EquivalenceMember *leftem, EquivalenceMember *rightem,
3855 : : EquivalenceClass *parent_ec)
3856 : : {
3857 : : /* Check original source clauses */
3858 [ + - + + : 1317256 : foreach_node(RestrictInfo, rinfo, ec->ec_sources)
+ + ]
3859 : : {
3860 [ + + ]: 488210 : if (rinfo->left_em == leftem &&
3861 [ + + ]: 232380 : rinfo->right_em == rightem &&
3862 [ + + ]: 205883 : rinfo->parent_ec == parent_ec)
3863 : 1291 : return rinfo;
3864 [ + + ]: 488120 : if (rinfo->left_em == rightem &&
3865 [ + + ]: 209878 : rinfo->right_em == leftem &&
3866 [ + + ]: 187128 : rinfo->parent_ec == parent_ec)
3867 : 1201 : return rinfo;
3868 : : }
3869 : :
3870 : : /* Not found in ec_sources; search derived clauses */
3871 : 414523 : return ec_search_derived_clause_for_ems(root, ec, leftem, rightem,
3872 : : parent_ec);
3873 : : }
3874 : :
3875 : : /*
3876 : : * ec_search_derived_clause_for_ems
3877 : : * Search for an existing derived clause between two EquivalenceMembers.
3878 : : *
3879 : : * If the number of derived clauses exceeds a threshold, switch to hash table
3880 : : * lookup; otherwise, scan ec_derives_list linearly.
3881 : : *
3882 : : * Clauses involving constants are looked up by passing the non-constant EM
3883 : : * as leftem and setting rightem to NULL. In that case, we expect to find a
3884 : : * clause with a constant on the RHS.
3885 : : *
3886 : : * While searching the list, we compare each given EM with both sides of each
3887 : : * clause. But for hash table lookups, we construct a canonicalized key and
3888 : : * perform a single lookup.
3889 : : */
3890 : : static RestrictInfo *
3891 : 414528 : ec_search_derived_clause_for_ems(PlannerInfo *root, EquivalenceClass *ec,
3892 : : EquivalenceMember *leftem,
3893 : : EquivalenceMember *rightem,
3894 : : EquivalenceClass *parent_ec)
3895 : : {
3896 : : /* Switch to using hash lookup when list grows "too long". */
3897 [ + - - + ]: 829056 : if (!ec->ec_derives_hash &&
3898 : 414528 : list_length(ec->ec_derives_list) >= EC_DERIVES_HASH_THRESHOLD)
510 amitlan@postgresql.o 3899 :UBC 0 : ec_build_derives_hash(root, ec);
3900 : :
3901 : : /* Perform hash table lookup if available */
510 amitlan@postgresql.o 3902 [ - + ]:CBC 414528 : if (ec->ec_derives_hash)
3903 : : {
3904 : : ECDerivesKey key;
3905 : : RestrictInfo *rinfo;
3906 : : ECDerivesEntry *entry;
3907 : :
510 amitlan@postgresql.o 3908 :UBC 0 : fill_ec_derives_key(&key, leftem, rightem, parent_ec);
3909 : 0 : entry = derives_lookup(ec->ec_derives_hash, key);
3910 [ # # ]: 0 : if (entry)
3911 : : {
3912 : 0 : rinfo = entry->rinfo;
3913 [ # # ]: 0 : Assert(rinfo);
3914 [ # # # # ]: 0 : Assert(rightem || rinfo->right_em->em_is_const);
3915 : 0 : return rinfo;
3916 : : }
3917 : : }
3918 : : else
3919 : : {
3920 : : /* Fallback to linear search over ec_derives_list */
510 amitlan@postgresql.o 3921 [ + + + + :CBC 591465 : foreach_node(RestrictInfo, rinfo, ec->ec_derives_list)
+ + ]
3922 : : {
3923 : : /* Handle special case: lookup by non-const EM alone */
3924 [ + + ]: 452989 : if (!rightem &&
3925 [ + - ]: 5 : rinfo->left_em == leftem)
3926 : : {
3927 [ - + ]: 5 : Assert(rinfo->right_em->em_is_const);
3928 : 345290 : return rinfo;
3929 : : }
3930 [ + + ]: 452984 : if (rinfo->left_em == leftem &&
3931 [ + + ]: 185660 : rinfo->right_em == rightem &&
3932 [ + + ]: 168339 : rinfo->parent_ec == parent_ec)
3933 : 168329 : return rinfo;
3934 [ + + ]: 284655 : if (rinfo->left_em == rightem &&
3935 [ + + ]: 187158 : rinfo->right_em == leftem &&
3936 [ + - ]: 176956 : rinfo->parent_ec == parent_ec)
3937 : 176956 : return rinfo;
3938 : : }
3939 : : }
3940 : :
3941 : 69238 : return NULL;
3942 : : }
|