PostgreSQL Source Code git master
Loading...
Searching...
No Matches
initsplan.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * initsplan.c
4 * Target list, group by, qualification, joininfo initialization routines
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/optimizer/plan/initsplan.c
12 *
13 *-------------------------------------------------------------------------
14 */
15#include "postgres.h"
16
17#include "access/nbtree.h"
18#include "access/sysattr.h"
20#include "catalog/pg_type.h"
21#include "nodes/makefuncs.h"
22#include "nodes/nodeFuncs.h"
23#include "optimizer/clauses.h"
24#include "optimizer/cost.h"
25#include "optimizer/inherit.h"
26#include "optimizer/joininfo.h"
27#include "optimizer/optimizer.h"
28#include "optimizer/pathnode.h"
29#include "optimizer/paths.h"
31#include "optimizer/planmain.h"
32#include "optimizer/planner.h"
34#include "parser/analyze.h"
36#include "utils/lsyscache.h"
37#include "utils/rel.h"
38#include "utils/typcache.h"
39
40/* These parameters are set by GUC */
43
44
45/*
46 * deconstruct_jointree requires multiple passes over the join tree, because we
47 * need to finish computing JoinDomains before we start distributing quals.
48 * As long as we have to do that, other information such as the relevant
49 * qualscopes might as well be computed in the first pass too.
50 *
51 * deconstruct_recurse recursively examines the join tree and builds a List
52 * (in depth-first traversal order) of JoinTreeItem structs, which are then
53 * processed iteratively by deconstruct_distribute. If there are outer
54 * joins, non-degenerate outer join clauses are processed in a third pass
55 * deconstruct_distribute_oj_quals.
56 *
57 * The JoinTreeItem structs themselves can be freed at the end of
58 * deconstruct_jointree, but do not modify or free their substructure,
59 * as the relid sets may also be pointed to by RestrictInfo and
60 * SpecialJoinInfo nodes.
61 */
62typedef struct JoinTreeItem
63{
64 /* Fields filled during deconstruct_recurse: */
65 Node *jtnode; /* jointree node to examine */
66 JoinDomain *jdomain; /* join domain for its ON/WHERE clauses */
67 struct JoinTreeItem *jti_parent; /* JoinTreeItem for this node's
68 * parent, or NULL if it's the top */
69 Relids qualscope; /* base+OJ Relids syntactically included in
70 * this jointree node */
71 Relids inner_join_rels; /* base+OJ Relids syntactically included
72 * in inner joins appearing at or below
73 * this jointree node */
74 Relids left_rels; /* if join node, Relids of the left side */
75 Relids right_rels; /* if join node, Relids of the right side */
76 Relids nonnullable_rels; /* if outer join, Relids of the
77 * non-nullable side */
78 /* Fields filled during deconstruct_distribute: */
79 SpecialJoinInfo *sjinfo; /* if outer join, its SpecialJoinInfo */
80 List *oj_joinclauses; /* outer join quals not yet distributed */
81 List *lateral_clauses; /* quals postponed from children due to
82 * lateral references */
84
85/*
86 * Compatibility info for one GROUP BY item, precomputed for use by
87 * remove_useless_groupby_columns() when matching unique-index columns against
88 * GROUP BY items.
89 */
90typedef struct GroupByColInfo
91{
92 AttrNumber attno; /* var->varattno */
93 List *eq_opfamilies; /* mergejoin opfamilies of sgc->eqop */
94 Oid coll; /* var->varcollid */
96
97
103 Expr *expr);
105 Index rtindex);
109 List **item_list);
112 int rti, JoinTreeItem *jtitem);
113static void mark_rels_nulled_by_join(PlannerInfo *root, Index ojrelid,
116 Relids left_rels, Relids right_rels,
117 Relids inner_join_rels,
118 JoinType jointype, Index ojrelid,
119 List *clause);
121 List *clause);
123 List *jtitems,
125static void distribute_quals_to_rels(PlannerInfo *root, List *clauses,
127 SpecialJoinInfo *sjinfo,
128 Index security_level,
129 Relids qualscope,
132 Relids incompatible_relids,
134 bool has_clone,
135 bool is_clone,
137static void distribute_qual_to_rels(PlannerInfo *root, Node *clause,
139 SpecialJoinInfo *sjinfo,
140 Index security_level,
141 Relids qualscope,
144 Relids incompatible_relids,
146 bool has_clone,
147 bool is_clone,
154
155
156/*****************************************************************************
157 *
158 * JOIN TREES
159 *
160 *****************************************************************************/
161
162/*
163 * add_base_rels_to_query
164 *
165 * Scan the query's jointree and create baserel RelOptInfos for all
166 * the base relations (e.g., table, subquery, and function RTEs)
167 * appearing in the jointree.
168 *
169 * The initial invocation must pass root->parse->jointree as the value of
170 * jtnode. Internally, the function recurses through the jointree.
171 *
172 * At the end of this process, there should be one baserel RelOptInfo for
173 * every non-join RTE that is used in the query. Some of the baserels
174 * may be appendrel parents, which will require additional "otherrel"
175 * RelOptInfos for their member rels, but those are added later.
176 */
177void
179{
180 if (jtnode == NULL)
181 return;
182 if (IsA(jtnode, RangeTblRef))
183 {
184 int varno = ((RangeTblRef *) jtnode)->rtindex;
185
186 (void) build_simple_rel(root, varno, NULL);
187 }
188 else if (IsA(jtnode, FromExpr))
189 {
190 FromExpr *f = (FromExpr *) jtnode;
191 ListCell *l;
192
193 foreach(l, f->fromlist)
195 }
196 else if (IsA(jtnode, JoinExpr))
197 {
198 JoinExpr *j = (JoinExpr *) jtnode;
199
202 }
203 else
204 elog(ERROR, "unrecognized node type: %d",
205 (int) nodeTag(jtnode));
206}
207
208/*
209 * add_other_rels_to_query
210 * create "otherrel" RelOptInfos for the children of appendrel baserels
211 *
212 * At the end of this process, there should be RelOptInfos for all relations
213 * that will be scanned by the query.
214 */
215void
217{
218 int rti;
219
220 for (rti = 1; rti < root->simple_rel_array_size; rti++)
221 {
222 RelOptInfo *rel = root->simple_rel_array[rti];
223 RangeTblEntry *rte = root->simple_rte_array[rti];
224
225 /* there may be empty slots corresponding to non-baserel RTEs */
226 if (rel == NULL)
227 continue;
228
229 /* Ignore any "otherrels" that were already added. */
230 if (rel->reloptkind != RELOPT_BASEREL)
231 continue;
232
233 /* If it's marked as inheritable, look for children. */
234 if (rte->inh)
236 }
237}
238
239
240/*****************************************************************************
241 *
242 * TARGET LISTS
243 *
244 *****************************************************************************/
245
246/*
247 * build_base_rel_tlists
248 * Add targetlist entries for each var needed in the query's final tlist
249 * (and HAVING clause, if any) to the appropriate base relations.
250 *
251 * We mark such vars as needed by "relation 0" to ensure that they will
252 * propagate up through all join plan steps.
253 */
254void
256{
257 List *tlist_vars = pull_var_clause((Node *) final_tlist,
261
262 if (tlist_vars != NIL)
263 {
265 list_free(tlist_vars);
266 }
267
268 /*
269 * If there's a HAVING clause, we'll need the Vars it uses, too. Note
270 * that HAVING can contain Aggrefs but not WindowFuncs.
271 */
272 if (root->parse->havingQual)
273 {
274 List *having_vars = pull_var_clause(root->parse->havingQual,
277
278 if (having_vars != NIL)
279 {
283 }
284 }
285}
286
287/*
288 * add_vars_to_targetlist
289 * For each variable appearing in the list, add it to the owning
290 * relation's targetlist if not already present, and mark the variable
291 * as being needed for the indicated join (or for final output if
292 * where_needed includes "relation 0").
293 *
294 * The list may also contain PlaceHolderVars. These don't necessarily
295 * have a single owning relation; we keep their attr_needed info in
296 * root->placeholder_list instead. Find or create the associated
297 * PlaceHolderInfo entry, and update its ph_needed.
298 *
299 * See also add_vars_to_attr_needed.
300 */
301void
304{
305 ListCell *temp;
306
308
309 foreach(temp, vars)
310 {
311 Node *node = (Node *) lfirst(temp);
312
313 if (IsA(node, Var))
314 {
315 Var *var = (Var *) node;
316 RelOptInfo *rel = find_base_rel(root, var->varno);
317 int attno = var->varattno;
318
320 continue;
321 Assert(attno >= rel->min_attr && attno <= rel->max_attr);
322 attno -= rel->min_attr;
323 if (rel->attr_needed[attno] == NULL)
324 {
325 /*
326 * Variable not yet requested, so add to rel's targetlist.
327 *
328 * The value available at the rel's scan level has not been
329 * nulled by any outer join, so drop its varnullingrels.
330 * (We'll put those back as we climb up the join tree.)
331 */
332 var = copyObject(var);
333 var->varnullingrels = NULL;
334 rel->reltarget->exprs = lappend(rel->reltarget->exprs, var);
335 /* reltarget cost and width will be computed later */
336 }
337 rel->attr_needed[attno] = bms_add_members(rel->attr_needed[attno],
339 }
340 else if (IsA(node, PlaceHolderVar))
341 {
344
345 phinfo->ph_needed = bms_add_members(phinfo->ph_needed,
347 }
348 else
349 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
350 }
351}
352
353/*
354 * add_vars_to_attr_needed
355 * This does a subset of what add_vars_to_targetlist does: it just
356 * updates attr_needed for Vars and ph_needed for PlaceHolderVars.
357 * We assume the Vars are already in their relations' targetlists.
358 *
359 * This is used to rebuild attr_needed/ph_needed sets after removal
360 * of a useless outer join. The removed join clause might have been
361 * the only upper-level use of some other relation's Var, in which
362 * case we can reduce that Var's attr_needed and thereby possibly
363 * open the door to further join removals. But we can't tell that
364 * without tedious reconstruction of the attr_needed data.
365 *
366 * Note that if a Var's attr_needed is successfully reduced to empty,
367 * it will still be in the relation's targetlist even though we do
368 * not really need the scan plan node to emit it. The extra plan
369 * inefficiency seems tiny enough to not be worth spending planner
370 * cycles to get rid of it.
371 */
372void
375{
376 ListCell *temp;
377
379
380 foreach(temp, vars)
381 {
382 Node *node = (Node *) lfirst(temp);
383
384 if (IsA(node, Var))
385 {
386 Var *var = (Var *) node;
387 RelOptInfo *rel = find_base_rel(root, var->varno);
388 int attno = var->varattno;
389
391 continue;
392 Assert(attno >= rel->min_attr && attno <= rel->max_attr);
393 attno -= rel->min_attr;
394 rel->attr_needed[attno] = bms_add_members(rel->attr_needed[attno],
396 }
397 else if (IsA(node, PlaceHolderVar))
398 {
401
402 phinfo->ph_needed = bms_add_members(phinfo->ph_needed,
404 }
405 else
406 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
407 }
408}
409
410/*****************************************************************************
411 *
412 * GROUP BY
413 *
414 *****************************************************************************/
415
416/*
417 * remove_useless_groupby_columns
418 * Remove any columns in the GROUP BY clause that are redundant due to
419 * being functionally dependent on other GROUP BY columns.
420 *
421 * Since some other DBMSes do not allow references to ungrouped columns, it's
422 * not unusual to find all columns listed in GROUP BY even though listing the
423 * primary-key columns, or columns of a unique constraint would be sufficient.
424 * Deleting such excess columns avoids redundant sorting or hashing work, so
425 * it's worth doing.
426 *
427 * Relcache invalidations will ensure that cached plans become invalidated
428 * when the underlying supporting indexes are dropped or if a column's NOT
429 * NULL attribute is removed.
430 */
431void
433{
434 Query *parse = root->parse;
438 bool tryremove = false;
439 ListCell *lc;
440 int relid;
441
442 /* No chance to do anything if there are less than two GROUP BY items */
443 if (list_length(root->processed_groupClause) < 2)
444 return;
445
446 /* Don't fiddle with the GROUP BY clause if the query has grouping sets */
447 if (parse->groupingSets)
448 return;
449
450 /*
451 * Scan the GROUP BY clause to find GROUP BY items that are simple Vars.
452 * Fill groupbyattnos[k] with a bitmapset of the column attnos of RTE k
453 * that are GROUP BY items, and groupbycols[k] with a parallel list of
454 * GroupByColInfo records. We need the latter so that, when checking a
455 * unique index against this rel's GROUP BY items, we can verify that the
456 * index's notion of equality agrees with at least one GROUP BY item per
457 * index column.
458 */
461 foreach(lc, root->processed_groupClause)
462 {
465 Var *var = (Var *) tle->expr;
466 GroupByColInfo *info;
467
468 /*
469 * Ignore non-Vars and Vars from other query levels.
470 *
471 * XXX in principle, stable expressions containing Vars could also be
472 * removed, if all the Vars are functionally dependent on other GROUP
473 * BY items. But it's not clear that such cases occur often enough to
474 * be worth troubling over.
475 */
476 if (!IsA(var, Var) ||
477 var->varlevelsup > 0)
478 continue;
479
480 /* OK, remember we have this Var */
481 relid = var->varno;
482 Assert(relid <= list_length(parse->rtable));
483
484 /*
485 * If this isn't the first column for this relation then we now have
486 * multiple columns. That means there might be some that can be
487 * removed.
488 */
492
493 info = palloc(sizeof(GroupByColInfo));
494 info->attno = var->varattno;
496 info->coll = var->varcollid;
497 groupbycols[relid] = lappend(groupbycols[relid], info);
498 }
499
500 /*
501 * No Vars or didn't find multiple Vars for any relation in the GROUP BY?
502 * If so, nothing can be removed, so don't waste more effort trying.
503 */
504 if (!tryremove)
505 return;
506
507 /*
508 * Consider each relation and see if it is possible to remove some of its
509 * Vars from GROUP BY. For simplicity and speed, we do the actual removal
510 * in a separate pass. Here, we just fill surplusvars[k] with a bitmapset
511 * of the column attnos of RTE k that are removable GROUP BY items.
512 */
513 surplusvars = NULL; /* don't allocate array unless required */
514 relid = 0;
515 foreach(lc, parse->rtable)
516 {
518 RelOptInfo *rel;
522
523 relid++;
524
525 /* Only plain relations could have primary-key constraints */
526 if (rte->rtekind != RTE_RELATION)
527 continue;
528
529 /*
530 * We must skip inheritance parent tables as some of the child rels
531 * may cause duplicate rows. This cannot happen with partitioned
532 * tables, however.
533 */
534 if (rte->inh && rte->relkind != RELKIND_PARTITIONED_TABLE)
535 continue;
536
537 /* Nothing to do unless this rel has multiple Vars in GROUP BY */
538 relattnos = groupbyattnos[relid];
540 continue;
541
542 rel = root->simple_rel_array[relid];
543
544 /*
545 * Now check each index for this relation to see if there are any with
546 * columns which are a proper subset of the grouping columns for this
547 * relation.
548 */
550 {
552 bool index_check_ok;
553
554 /*
555 * Skip any non-unique and deferrable indexes. Predicate indexes
556 * have not been checked yet, so we must skip those too as the
557 * predOK check that's done later might fail.
558 */
559 if (!index->unique || !index->immediate || index->indpred != NIL)
560 continue;
561
562 /* For simplicity, we currently don't support expression indexes */
563 if (index->indexprs != NIL)
564 continue;
565
567 index_check_ok = true;
568 for (int i = 0; i < index->nkeycolumns; i++)
569 {
570 AttrNumber indkey_attno = index->indexkeys[i];
571 Oid indkey_opfamily = index->opfamily[i];
572 Oid indkey_coll = index->indexcollations[i];
573 ListCell *lc2;
574
575 /*
576 * We must insist that the index columns are all defined NOT
577 * NULL otherwise duplicate NULLs could exist. However, we
578 * can relax this check when the index is defined with NULLS
579 * NOT DISTINCT as there can only be 1 NULL row, therefore
580 * functional dependency on the unique columns is maintained,
581 * despite the NULL.
582 */
583 if (!index->nullsnotdistinct &&
585 {
586 index_check_ok = false;
587 break;
588 }
589
590 /*
591 * The index proves uniqueness only under its own opfamily and
592 * collation. Require some GROUP BY item on this column to
593 * use a compatible eqop and collation, the same check
594 * relation_has_unique_index_for() applies to join clauses.
595 */
596 foreach(lc2, groupbycols[relid])
597 {
599
600 if (info->attno != indkey_attno)
601 continue;
604 break;
605 }
606 if (lc2 == NULL)
607 {
608 index_check_ok = false;
609 break;
610 }
611
612 ind_attnos =
616 }
617
618 if (!index_check_ok)
619 continue;
620
621 /*
622 * Skip any indexes where the indexed columns aren't a proper
623 * subset of the GROUP BY.
624 */
626 continue;
627
628 /*
629 * Record the attribute numbers from the index with the fewest
630 * columns. This allows the largest number of columns to be
631 * removed from the GROUP BY clause. In the future, we may wish
632 * to consider using the narrowest set of columns and looking at
633 * pg_statistic.stawidth as it might be better to use an index
634 * with, say two INT4s, rather than, say, one long varlena column.
635 */
636 if (index->nkeycolumns < best_nkeycolumns)
637 {
639 best_nkeycolumns = index->nkeycolumns;
640 }
641 }
642
643 /* Did we find a suitable index? */
645 {
646 /*
647 * To easily remember whether we've found anything to do, we don't
648 * allocate the surplusvars[] array until we find something.
649 */
650 if (surplusvars == NULL)
652
653 /* Remember the attnos of the removable columns */
655 }
656 }
657
658 /*
659 * If we found any surplus Vars, build a new GROUP BY clause without them.
660 * (Note: this may leave some TLEs with unreferenced ressortgroupref
661 * markings, but that's harmless.)
662 */
663 if (surplusvars != NULL)
664 {
666
667 foreach(lc, root->processed_groupClause)
668 {
671 Var *var = (Var *) tle->expr;
672
673 /*
674 * New list must include non-Vars, outer Vars, and anything not
675 * marked as surplus.
676 */
677 if (!IsA(var, Var) ||
678 var->varlevelsup > 0 ||
680 surplusvars[var->varno]))
682 }
683
684 root->processed_groupClause = new_groupby;
685 }
686}
687
688/*
689 * setup_eager_aggregation
690 * Check if eager aggregation is applicable, and if so collect suitable
691 * aggregate expressions and grouping expressions in the query.
692 */
693void
695{
696 /*
697 * Don't apply eager aggregation if disabled by user.
698 */
700 return;
701
702 /*
703 * Don't apply eager aggregation if there are no available GROUP BY
704 * clauses.
705 */
706 if (!root->processed_groupClause)
707 return;
708
709 /*
710 * For now we don't try to support grouping sets.
711 */
712 if (root->parse->groupingSets)
713 return;
714
715 /*
716 * For now we don't try to support DISTINCT or ORDER BY aggregates.
717 */
718 if (root->numOrderedAggs > 0)
719 return;
720
721 /*
722 * If there are any aggregates that do not support partial mode, or any
723 * partial aggregates that are non-serializable, do not apply eager
724 * aggregation.
725 */
726 if (root->hasNonPartialAggs || root->hasNonSerialAggs)
727 return;
728
729 /*
730 * We don't try to apply eager aggregation if there are set-returning
731 * functions in targetlist.
732 */
733 if (root->parse->hasTargetSRFs)
734 return;
735
736 /*
737 * Eager aggregation only makes sense if there are multiple base rels in
738 * the query.
739 */
740 if (bms_membership(root->all_baserels) != BMS_MULTIPLE)
741 return;
742
743 /*
744 * Don't apply eager aggregation if any aggregate poses a risk of
745 * excessive memory usage during partial aggregation.
746 */
748 return;
749
750 /*
751 * Collect aggregate expressions and plain Vars that appear in the
752 * targetlist and havingQual.
753 */
755
756 /*
757 * If there are no suitable aggregate expressions, we cannot apply eager
758 * aggregation.
759 */
760 if (root->agg_clause_list == NIL)
761 return;
762
763 /*
764 * Collect grouping expressions that appear in grouping clauses.
765 */
767}
768
769/*
770 * is_partial_agg_memory_risky
771 * Check if any aggregate poses a risk of excessive memory usage during
772 * partial aggregation.
773 *
774 * We check if any aggregate has a negative aggtransspace value, which
775 * indicates that its transition state data can grow unboundedly in size.
776 * Applying eager aggregation in such cases risks high memory usage since
777 * partial aggregation results might be stored in join hash tables or
778 * materialized nodes.
779 */
780static bool
782{
783 ListCell *lc;
784
785 foreach(lc, root->aggtransinfos)
786 {
788
789 if (transinfo->aggtransspace < 0)
790 return true;
791 }
792
793 return false;
794}
795
796/*
797 * create_agg_clause_infos
798 * Search the targetlist and havingQual for Aggrefs and plain Vars, and
799 * create an AggClauseInfo for each Aggref node.
800 */
801static void
803{
805 List *agg_clause_list = NIL;
806 List *tlist_vars = NIL;
808 bool eager_agg_applicable = true;
809 ListCell *lc;
810
811 Assert(root->agg_clause_list == NIL);
812 Assert(root->tlist_vars == NIL);
813
814 tlist_exprs = pull_var_clause((Node *) root->processed_tlist,
818
819 /*
820 * Aggregates within the HAVING clause need to be processed in the same
821 * way as those in the targetlist. Note that HAVING can contain Aggrefs
822 * but not WindowFuncs.
823 */
824 if (root->parse->havingQual != NULL)
825 {
827
828 having_exprs = pull_var_clause((Node *) root->parse->havingQual,
831 if (having_exprs != NIL)
832 {
835 }
836 }
837
838 foreach(lc, tlist_exprs)
839 {
840 Expr *expr = (Expr *) lfirst(lc);
841 Aggref *aggref;
842 Relids agg_eval_at;
844
845 /* For now we don't try to support GROUPING() expressions */
846 if (IsA(expr, GroupingFunc))
847 {
848 eager_agg_applicable = false;
849 break;
850 }
851
852 /* Collect plain Vars for future reference */
853 if (IsA(expr, Var))
854 {
855 tlist_vars = list_append_unique(tlist_vars, expr);
856 continue;
857 }
858
859 aggref = castNode(Aggref, expr);
860
861 Assert(aggref->aggorder == NIL);
862 Assert(aggref->aggdistinct == NIL);
863
864 /*
865 * We cannot push down aggregates that contain volatile functions.
866 * Doing so would change the number of times the function is
867 * evaluated.
868 */
869 if (contain_volatile_functions((Node *) aggref))
870 {
871 eager_agg_applicable = false;
872 break;
873 }
874
875 /*
876 * If there are any securityQuals, do not try to apply eager
877 * aggregation if any non-leakproof aggregate functions are present.
878 * This is overly strict, but for now...
879 */
880 if (root->qual_security_level > 0 &&
882 {
883 eager_agg_applicable = false;
884 break;
885 }
886
887 agg_eval_at = pull_varnos(root, (Node *) aggref);
888
889 /*
890 * If all base relations in the query are referenced by aggregate
891 * functions, then eager aggregation is not applicable.
892 */
894 if (bms_is_subset(root->all_baserels, aggregate_relids))
895 {
896 eager_agg_applicable = false;
897 break;
898 }
899
900 /* OK, create the AggClauseInfo node */
902 ac_info->aggref = aggref;
903 ac_info->agg_eval_at = agg_eval_at;
904
905 /* ... and add it to the list */
906 agg_clause_list = list_append_unique(agg_clause_list, ac_info);
907 }
908
910
912 {
913 root->agg_clause_list = agg_clause_list;
914 root->tlist_vars = tlist_vars;
915 }
916 else
917 {
918 list_free_deep(agg_clause_list);
919 list_free(tlist_vars);
920 }
921}
922
923/*
924 * create_grouping_expr_infos
925 * Create a GroupingExprInfo for each expression usable as grouping key.
926 *
927 * If any grouping expression is not suitable, we will just return with
928 * root->group_expr_list being NIL.
929 */
930static void
932{
933 List *exprs = NIL;
935 List *ecs = NIL;
936 ListCell *lc,
937 *lc1,
938 *lc2,
939 *lc3;
940
941 Assert(root->group_expr_list == NIL);
942
943 foreach(lc, root->processed_groupClause)
944 {
946 TargetEntry *tle = get_sortgroupclause_tle(sgc, root->processed_tlist);
949
950 Assert(tle->ressortgroupref > 0);
951
952 /*
953 * For now we only support plain Vars as grouping expressions.
954 */
955 if (!IsA(tle->expr, Var))
956 return;
957
958 /*
959 * Eager aggregation is only possible if equality implies image
960 * equality for each grouping key. Otherwise, placing keys with
961 * different byte images into the same group may result in the loss of
962 * information that could be necessary to evaluate upper qual clauses.
963 *
964 * For instance, the NUMERIC data type is not supported, as values
965 * that are considered equal by the equality operator (e.g., 0 and
966 * 0.0) can have different scales.
967 */
970 if (!OidIsValid(tce->btree_opf) ||
971 !OidIsValid(tce->btree_opintype))
972 return;
973
975 tce->btree_opintype,
976 tce->btree_opintype,
978
979 /*
980 * If there is no BTEQUALIMAGE_PROC, eager aggregation is assumed to
981 * be unsafe. Otherwise, we call the procedure to check. We must be
982 * careful to pass the expression's actual collation, rather than the
983 * data type's default collation, to ensure that non-deterministic
984 * collations are correctly handled.
985 */
988 exprCollation((Node *) tle->expr),
989 ObjectIdGetDatum(tce->btree_opintype))))
990 return;
991
992 exprs = lappend(exprs, tle->expr);
993 sortgrouprefs = lappend_int(sortgrouprefs, tle->ressortgroupref);
995 }
996
997 /*
998 * Construct a GroupingExprInfo for each expression.
999 */
1000 forthree(lc1, exprs, lc2, sortgrouprefs, lc3, ecs)
1001 {
1002 Expr *expr = (Expr *) lfirst(lc1);
1003 int sortgroupref = lfirst_int(lc2);
1006
1008 ge_info->expr = (Expr *) copyObject(expr);
1009 ge_info->sortgroupref = sortgroupref;
1010 ge_info->ec = ec;
1011
1012 root->group_expr_list = lappend(root->group_expr_list, ge_info);
1013 }
1014}
1015
1016/*
1017 * get_eclass_for_sortgroupclause
1018 * Given a group clause and an expression, find an existing equivalence
1019 * class that the expression is a member of; return NULL if none.
1020 */
1021static EquivalenceClass *
1023 Expr *expr)
1024{
1025 Oid opfamily,
1026 opcintype,
1027 collation;
1028 CompareType cmptype;
1031
1032 /* Punt if the group clause is not sortable */
1033 if (!OidIsValid(sgc->sortop))
1034 return NULL;
1035
1036 /* Find the operator in pg_amop --- failure shouldn't happen */
1037 if (!get_ordering_op_properties(sgc->sortop,
1038 &opfamily, &opcintype, &cmptype))
1039 elog(ERROR, "operator %u is not a valid ordering operator",
1040 sgc->sortop);
1041
1042 /* Because SortGroupClause doesn't carry collation, consult the expr */
1043 collation = exprCollation((Node *) expr);
1044
1045 /*
1046 * EquivalenceClasses need to contain opfamily lists based on the family
1047 * membership of mergejoinable equality operators, which could belong to
1048 * more than one opfamily. So we have to look up the opfamily's equality
1049 * operator and get its membership.
1050 */
1052 opcintype,
1053 opcintype,
1054 COMPARE_EQ);
1055 if (!OidIsValid(equality_op)) /* shouldn't happen */
1056 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
1057 COMPARE_EQ, opcintype, opcintype, opfamily);
1059 if (!opfamilies) /* certainly should find some */
1060 elog(ERROR, "could not find opfamilies for equality operator %u",
1061 equality_op);
1062
1063 /* Now find a matching EquivalenceClass */
1064 return get_eclass_for_sort_expr(root, expr, opfamilies, opcintype,
1065 collation, sgc->tleSortGroupRef,
1066 NULL, false);
1067}
1068
1069/*****************************************************************************
1070 *
1071 * LATERAL REFERENCES
1072 *
1073 *****************************************************************************/
1074
1075/*
1076 * find_lateral_references
1077 * For each LATERAL subquery, extract all its references to Vars and
1078 * PlaceHolderVars of the current query level, and make sure those values
1079 * will be available for evaluation of the subquery.
1080 *
1081 * While later planning steps ensure that the Var/PHV source rels are on the
1082 * outside of nestloops relative to the LATERAL subquery, we also need to
1083 * ensure that the Vars/PHVs propagate up to the nestloop join level; this
1084 * means setting suitable where_needed values for them.
1085 *
1086 * Note that this only deals with lateral references in unflattened LATERAL
1087 * subqueries. When we flatten a LATERAL subquery, its lateral references
1088 * become plain Vars in the parent query, but they may have to be wrapped in
1089 * PlaceHolderVars if they need to be forced NULL by outer joins that don't
1090 * also null the LATERAL subquery. That's all handled elsewhere.
1091 *
1092 * This has to run before deconstruct_jointree, since it might result in
1093 * creation of PlaceHolderInfos.
1094 */
1095void
1097{
1098 Index rti;
1099
1100 /* We need do nothing if the query contains no LATERAL RTEs */
1101 if (!root->hasLateralRTEs)
1102 return;
1103
1104 /*
1105 * Examine all baserels (the rel array has been set up by now).
1106 */
1107 for (rti = 1; rti < root->simple_rel_array_size; rti++)
1108 {
1109 RelOptInfo *brel = root->simple_rel_array[rti];
1110
1111 /* there may be empty slots corresponding to non-baserel RTEs */
1112 if (brel == NULL)
1113 continue;
1114
1115 Assert(brel->relid == rti); /* sanity check on array */
1116
1117 /*
1118 * This bit is less obvious than it might look. We ignore appendrel
1119 * otherrels and consider only their parent baserels. In a case where
1120 * a LATERAL-containing UNION ALL subquery was pulled up, it is the
1121 * otherrel that is actually going to be in the plan. However, we
1122 * want to mark all its lateral references as needed by the parent,
1123 * because it is the parent's relid that will be used for join
1124 * planning purposes. And the parent's RTE will contain all the
1125 * lateral references we need to know, since the pulled-up member is
1126 * nothing but a copy of parts of the original RTE's subquery. We
1127 * could visit the parent's children instead and transform their
1128 * references back to the parent's relid, but it would be much more
1129 * complicated for no real gain. (Important here is that the child
1130 * members have not yet received any processing beyond being pulled
1131 * up.) Similarly, in appendrels created by inheritance expansion,
1132 * it's sufficient to look at the parent relation.
1133 */
1134
1135 /* ignore RTEs that are "other rels" */
1136 if (brel->reloptkind != RELOPT_BASEREL)
1137 continue;
1138
1140 }
1141}
1142
1143static void
1145{
1146 RangeTblEntry *rte = root->simple_rte_array[rtindex];
1147 List *vars;
1148 List *newvars;
1150 ListCell *lc;
1151
1152 /* No cross-references are possible if it's not LATERAL */
1153 if (!rte->lateral)
1154 return;
1155
1156 /* Fetch the appropriate variables */
1157 if (rte->rtekind == RTE_RELATION)
1158 vars = pull_vars_of_level((Node *) rte->tablesample, 0);
1159 else if (rte->rtekind == RTE_SUBQUERY)
1160 vars = pull_vars_of_level((Node *) rte->subquery, 1);
1161 else if (rte->rtekind == RTE_FUNCTION)
1162 vars = pull_vars_of_level((Node *) rte->functions, 0);
1163 else if (rte->rtekind == RTE_TABLEFUNC)
1164 vars = pull_vars_of_level((Node *) rte->tablefunc, 0);
1165 else if (rte->rtekind == RTE_VALUES)
1166 vars = pull_vars_of_level((Node *) rte->values_lists, 0);
1167 else
1168 {
1169 Assert(false);
1170 return; /* keep compiler quiet */
1171 }
1172
1173 if (vars == NIL)
1174 return; /* nothing to do */
1175
1176 /* Copy each Var (or PlaceHolderVar) and adjust it to match our level */
1177 newvars = NIL;
1178 foreach(lc, vars)
1179 {
1180 Node *node = (Node *) lfirst(lc);
1181
1182 node = copyObject(node);
1183 if (IsA(node, Var))
1184 {
1185 Var *var = (Var *) node;
1186
1187 /* Adjustment is easy since it's just one node */
1188 var->varlevelsup = 0;
1189 }
1190 else if (IsA(node, PlaceHolderVar))
1191 {
1192 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1193 int levelsup = phv->phlevelsup;
1194
1195 /* Have to work harder to adjust the contained expression too */
1196 if (levelsup != 0)
1197 IncrementVarSublevelsUp(node, -levelsup, 0);
1198
1199 /*
1200 * If we pulled the PHV out of a subquery RTE, its expression
1201 * needs to be preprocessed. subquery_planner() already did this
1202 * for level-zero PHVs in function and values RTEs, though.
1203 */
1204 if (levelsup > 0)
1205 phv->phexpr = preprocess_phv_expression(root, phv->phexpr);
1206 }
1207 else
1208 Assert(false);
1209 newvars = lappend(newvars, node);
1210 }
1211
1212 list_free(vars);
1213
1214 /*
1215 * We mark the Vars as being "needed" at the LATERAL RTE. This is a bit
1216 * of a cheat: a more formal approach would be to mark each one as needed
1217 * at the join of the LATERAL RTE with its source RTE. But it will work,
1218 * and it's much less tedious than computing a separate where_needed for
1219 * each Var.
1220 */
1222
1223 /*
1224 * Push Vars into their source relations' targetlists, and PHVs into
1225 * root->placeholder_list.
1226 */
1228
1229 /*
1230 * Remember the lateral references for rebuild_lateral_attr_needed and
1231 * create_lateral_join_info.
1232 */
1233 brel->lateral_vars = newvars;
1234}
1235
1236/*
1237 * rebuild_lateral_attr_needed
1238 * Put back attr_needed bits for Vars/PHVs needed for lateral references.
1239 *
1240 * This is used to rebuild attr_needed/ph_needed sets after removal of a
1241 * useless outer join. It should match what find_lateral_references did,
1242 * except that we call add_vars_to_attr_needed not add_vars_to_targetlist.
1243 */
1244void
1246{
1247 Index rti;
1248
1249 /* We need do nothing if the query contains no LATERAL RTEs */
1250 if (!root->hasLateralRTEs)
1251 return;
1252
1253 /* Examine the same baserels that find_lateral_references did */
1254 for (rti = 1; rti < root->simple_rel_array_size; rti++)
1255 {
1256 RelOptInfo *brel = root->simple_rel_array[rti];
1258
1259 if (brel == NULL)
1260 continue;
1261 if (brel->reloptkind != RELOPT_BASEREL)
1262 continue;
1263
1264 /*
1265 * We don't need to repeat all of extract_lateral_references, since it
1266 * kindly saved the extracted Vars/PHVs in lateral_vars.
1267 */
1268 if (brel->lateral_vars == NIL)
1269 continue;
1270
1272
1274 }
1275}
1276
1277/*
1278 * create_lateral_join_info
1279 * Fill in the per-base-relation direct_lateral_relids, lateral_relids
1280 * and lateral_referencers sets.
1281 */
1282void
1284{
1285 bool found_laterals = false;
1286 Index rti;
1287 ListCell *lc;
1288
1289 /* We need do nothing if the query contains no LATERAL RTEs */
1290 if (!root->hasLateralRTEs)
1291 return;
1292
1293 /* We'll need to have the ph_eval_at values for PlaceHolderVars */
1294 Assert(root->placeholdersFrozen);
1295
1296 /*
1297 * Examine all baserels (the rel array has been set up by now).
1298 */
1299 for (rti = 1; rti < root->simple_rel_array_size; rti++)
1300 {
1301 RelOptInfo *brel = root->simple_rel_array[rti];
1302 Relids lateral_relids;
1303
1304 /* there may be empty slots corresponding to non-baserel RTEs */
1305 if (brel == NULL)
1306 continue;
1307
1308 Assert(brel->relid == rti); /* sanity check on array */
1309
1310 /* ignore RTEs that are "other rels" */
1311 if (brel->reloptkind != RELOPT_BASEREL)
1312 continue;
1313
1314 lateral_relids = NULL;
1315
1316 /* consider each laterally-referenced Var or PHV */
1317 foreach(lc, brel->lateral_vars)
1318 {
1319 Node *node = (Node *) lfirst(lc);
1320
1321 if (IsA(node, Var))
1322 {
1323 Var *var = (Var *) node;
1324
1325 found_laterals = true;
1326 lateral_relids = bms_add_member(lateral_relids,
1327 var->varno);
1328 }
1329 else if (IsA(node, PlaceHolderVar))
1330 {
1331 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1333
1334 found_laterals = true;
1335 lateral_relids = bms_add_members(lateral_relids,
1336 phinfo->ph_eval_at);
1337 }
1338 else
1339 Assert(false);
1340 }
1341
1342 /* We now have all the simple lateral refs from this rel */
1343 brel->direct_lateral_relids = lateral_relids;
1344 brel->lateral_relids = bms_copy(lateral_relids);
1345 }
1346
1347 /*
1348 * Now check for lateral references within PlaceHolderVars, and mark their
1349 * eval_at rels as having lateral references to the source rels.
1350 *
1351 * For a PHV that is due to be evaluated at a baserel, mark its source(s)
1352 * as direct lateral dependencies of the baserel (adding onto the ones
1353 * recorded above). If it's due to be evaluated at a join, mark its
1354 * source(s) as indirect lateral dependencies of each baserel in the join,
1355 * ie put them into lateral_relids but not direct_lateral_relids. This is
1356 * appropriate because we can't put any such baserel on the outside of a
1357 * join to one of the PHV's lateral dependencies, but on the other hand we
1358 * also can't yet join it directly to the dependency.
1359 */
1360 foreach(lc, root->placeholder_list)
1361 {
1363 Relids eval_at = phinfo->ph_eval_at;
1365 int varno;
1366
1367 if (phinfo->ph_lateral == NULL)
1368 continue; /* PHV is uninteresting if no lateral refs */
1369
1370 found_laterals = true;
1371
1372 /*
1373 * Include only baserels not outer joins in the evaluation sites'
1374 * lateral relids. This avoids problems when outer join order gets
1375 * rearranged, and it should still ensure that the lateral values are
1376 * available when needed.
1377 */
1378 lateral_refs = bms_intersect(phinfo->ph_lateral, root->all_baserels);
1380
1381 if (bms_get_singleton_member(eval_at, &varno))
1382 {
1383 /* Evaluation site is a baserel */
1384 RelOptInfo *brel = find_base_rel(root, varno);
1385
1386 brel->direct_lateral_relids =
1387 bms_add_members(brel->direct_lateral_relids,
1388 lateral_refs);
1389 brel->lateral_relids =
1390 bms_add_members(brel->lateral_relids,
1391 lateral_refs);
1392 }
1393 else
1394 {
1395 /* Evaluation site is a join */
1396 varno = -1;
1397 while ((varno = bms_next_member(eval_at, varno)) >= 0)
1398 {
1400
1401 if (brel == NULL)
1402 continue; /* ignore outer joins in eval_at */
1403 brel->lateral_relids = bms_add_members(brel->lateral_relids,
1404 lateral_refs);
1405 }
1406 }
1407 }
1408
1409 /*
1410 * If we found no actual lateral references, we're done; but reset the
1411 * hasLateralRTEs flag to avoid useless work later.
1412 */
1413 if (!found_laterals)
1414 {
1415 root->hasLateralRTEs = false;
1416 return;
1417 }
1418
1419 /*
1420 * Calculate the transitive closure of the lateral_relids sets, so that
1421 * they describe both direct and indirect lateral references. If relation
1422 * X references Y laterally, and Y references Z laterally, then we will
1423 * have to scan X on the inside of a nestloop with Z, so for all intents
1424 * and purposes X is laterally dependent on Z too.
1425 *
1426 * This code is essentially Warshall's algorithm for transitive closure.
1427 * The outer loop considers each baserel, and propagates its lateral
1428 * dependencies to those baserels that have a lateral dependency on it.
1429 */
1430 for (rti = 1; rti < root->simple_rel_array_size; rti++)
1431 {
1432 RelOptInfo *brel = root->simple_rel_array[rti];
1434 Index rti2;
1435
1436 if (brel == NULL || brel->reloptkind != RELOPT_BASEREL)
1437 continue;
1438
1439 /* need not consider baserel further if it has no lateral refs */
1440 outer_lateral_relids = brel->lateral_relids;
1442 continue;
1443
1444 /* else scan all baserels */
1445 for (rti2 = 1; rti2 < root->simple_rel_array_size; rti2++)
1446 {
1447 RelOptInfo *brel2 = root->simple_rel_array[rti2];
1448
1449 if (brel2 == NULL || brel2->reloptkind != RELOPT_BASEREL)
1450 continue;
1451
1452 /* if brel2 has lateral ref to brel, propagate brel's refs */
1453 if (bms_is_member(rti, brel2->lateral_relids))
1454 brel2->lateral_relids = bms_add_members(brel2->lateral_relids,
1456 }
1457 }
1458
1459 /*
1460 * Now that we've identified all lateral references, mark each baserel
1461 * with the set of relids of rels that reference it laterally (possibly
1462 * indirectly) --- that is, the inverse mapping of lateral_relids.
1463 */
1464 for (rti = 1; rti < root->simple_rel_array_size; rti++)
1465 {
1466 RelOptInfo *brel = root->simple_rel_array[rti];
1467 Relids lateral_relids;
1468 int rti2;
1469
1470 if (brel == NULL || brel->reloptkind != RELOPT_BASEREL)
1471 continue;
1472
1473 /* Nothing to do at rels with no lateral refs */
1474 lateral_relids = brel->lateral_relids;
1475 if (bms_is_empty(lateral_relids))
1476 continue;
1477
1478 /* No rel should have a lateral dependency on itself */
1479 Assert(!bms_is_member(rti, lateral_relids));
1480
1481 /* Mark this rel's referencees */
1482 rti2 = -1;
1483 while ((rti2 = bms_next_member(lateral_relids, rti2)) >= 0)
1484 {
1485 RelOptInfo *brel2 = root->simple_rel_array[rti2];
1486
1487 if (brel2 == NULL)
1488 continue; /* must be an OJ */
1489
1490 Assert(brel2->reloptkind == RELOPT_BASEREL);
1491 brel2->lateral_referencers =
1492 bms_add_member(brel2->lateral_referencers, rti);
1493 }
1494 }
1495}
1496
1497
1498/*****************************************************************************
1499 *
1500 * JOIN TREE PROCESSING
1501 *
1502 *****************************************************************************/
1503
1504/*
1505 * deconstruct_jointree
1506 * Recursively scan the query's join tree for WHERE and JOIN/ON qual
1507 * clauses, and add these to the appropriate restrictinfo and joininfo
1508 * lists belonging to base RelOptInfos. Also, add SpecialJoinInfo nodes
1509 * to root->join_info_list for any outer joins appearing in the query tree.
1510 * Return a "joinlist" data structure showing the join order decisions
1511 * that need to be made by make_one_rel().
1512 *
1513 * The "joinlist" result is a list of items that are either RangeTblRef
1514 * jointree nodes or sub-joinlists. All the items at the same level of
1515 * joinlist must be joined in an order to be determined by make_one_rel()
1516 * (note that legal orders may be constrained by SpecialJoinInfo nodes).
1517 * A sub-joinlist represents a subproblem to be planned separately. Currently
1518 * sub-joinlists arise only from FULL OUTER JOIN or when collapsing of
1519 * subproblems is stopped by join_collapse_limit or from_collapse_limit.
1520 */
1521List *
1523{
1524 List *result;
1526 List *item_list = NIL;
1527 ListCell *lc;
1528
1529 /*
1530 * After this point, no more PlaceHolderInfos may be made, because
1531 * make_outerjoininfo requires all active placeholders to be present in
1532 * root->placeholder_list while we crawl up the join tree.
1533 */
1534 root->placeholdersFrozen = true;
1535
1536 /* Fetch the already-created top-level join domain for the query */
1537 top_jdomain = linitial_node(JoinDomain, root->join_domains);
1538 top_jdomain->jd_relids = NULL; /* filled during deconstruct_recurse */
1539
1540 /* Start recursion at top of jointree */
1541 Assert(root->parse->jointree != NULL &&
1542 IsA(root->parse->jointree, FromExpr));
1543
1544 /* These are filled as we scan the jointree */
1545 root->all_baserels = NULL;
1546 root->outer_join_rels = NULL;
1547
1548 /* Perform the initial scan of the jointree */
1549 result = deconstruct_recurse(root, (Node *) root->parse->jointree,
1551 &item_list);
1552
1553 /* Now we can form the value of all_query_rels, too */
1554 root->all_query_rels = bms_union(root->all_baserels, root->outer_join_rels);
1555
1556 /* ... which should match what we computed for the top join domain */
1557 Assert(bms_equal(root->all_query_rels, top_jdomain->jd_relids));
1558
1559 /* Now scan all the jointree nodes again, and distribute quals */
1560 foreach(lc, item_list)
1561 {
1563
1565 }
1566
1567 /*
1568 * If there were any special joins then we may have some postponed LEFT
1569 * JOIN clauses to deal with.
1570 */
1571 if (root->join_info_list)
1572 {
1573 foreach(lc, item_list)
1574 {
1576
1577 if (jtitem->oj_joinclauses != NIL)
1579 }
1580 }
1581
1582 /* Don't need the JoinTreeItems any more */
1584
1585 return result;
1586}
1587
1588/*
1589 * deconstruct_recurse
1590 * One recursion level of deconstruct_jointree's initial jointree scan.
1591 *
1592 * jtnode is the jointree node to examine, and parent_domain is the
1593 * enclosing join domain. (We must add all base+OJ relids appearing
1594 * here or below to parent_domain.) parent_jtitem is the JoinTreeItem
1595 * for the parent jointree node, or NULL at the top of the recursion.
1596 *
1597 * item_list is an in/out parameter: we add a JoinTreeItem struct to
1598 * that list for each jointree node, in depth-first traversal order.
1599 * (Hence, after each call, the last list item corresponds to its jtnode.)
1600 *
1601 * Return value is the appropriate joinlist for this jointree node.
1602 */
1603static List *
1607 List **item_list)
1608{
1609 List *joinlist;
1611
1612 Assert(jtnode != NULL);
1613
1614 /* Make the new JoinTreeItem, but don't add it to item_list yet */
1616 jtitem->jtnode = jtnode;
1617 jtitem->jti_parent = parent_jtitem;
1618
1619 if (IsA(jtnode, RangeTblRef))
1620 {
1621 int varno = ((RangeTblRef *) jtnode)->rtindex;
1622
1623 /* Fill all_baserels as we encounter baserel jointree nodes */
1624 root->all_baserels = bms_add_member(root->all_baserels, varno);
1625 /* This node belongs to parent_domain */
1626 jtitem->jdomain = parent_domain;
1627 parent_domain->jd_relids = bms_add_member(parent_domain->jd_relids,
1628 varno);
1629 /* qualscope is just the one RTE */
1630 jtitem->qualscope = bms_make_singleton(varno);
1631 /* A single baserel does not create an inner join */
1632 jtitem->inner_join_rels = NULL;
1633 joinlist = list_make1(jtnode);
1634 }
1635 else if (IsA(jtnode, FromExpr))
1636 {
1637 FromExpr *f = (FromExpr *) jtnode;
1638 int remaining;
1639 ListCell *l;
1640
1641 /* This node belongs to parent_domain, as do its children */
1642 jtitem->jdomain = parent_domain;
1643
1644 /*
1645 * Recurse to handle child nodes, and compute output joinlist. We
1646 * collapse subproblems into a single joinlist whenever the resulting
1647 * joinlist wouldn't exceed from_collapse_limit members. Also, always
1648 * collapse one-element subproblems, since that won't lengthen the
1649 * joinlist anyway.
1650 */
1651 jtitem->qualscope = NULL;
1652 jtitem->inner_join_rels = NULL;
1653 joinlist = NIL;
1655 foreach(l, f->fromlist)
1656 {
1659 int sub_members;
1660
1663 jtitem,
1664 item_list);
1666 jtitem->qualscope = bms_add_members(jtitem->qualscope,
1667 sub_item->qualscope);
1668 jtitem->inner_join_rels = sub_item->inner_join_rels;
1670 remaining--;
1671 if (sub_members <= 1 ||
1674 else
1676 }
1677
1678 /*
1679 * A FROM with more than one list element is an inner join subsuming
1680 * all below it, so we should report inner_join_rels = qualscope. If
1681 * there was exactly one element, we should (and already did) report
1682 * whatever its inner_join_rels were. If there were no elements (is
1683 * that still possible?) the initialization before the loop fixed it.
1684 */
1685 if (list_length(f->fromlist) > 1)
1686 jtitem->inner_join_rels = jtitem->qualscope;
1687 }
1688 else if (IsA(jtnode, JoinExpr))
1689 {
1690 JoinExpr *j = (JoinExpr *) jtnode;
1692 *fj_domain;
1694 *right_item;
1697
1698 switch (j->jointype)
1699 {
1700 case JOIN_INNER:
1701 /* This node belongs to parent_domain, as do its children */
1702 jtitem->jdomain = parent_domain;
1703 /* Recurse */
1706 jtitem,
1707 item_list);
1711 jtitem,
1712 item_list);
1714 /* Compute qualscope etc */
1715 jtitem->qualscope = bms_union(left_item->qualscope,
1716 right_item->qualscope);
1717 jtitem->inner_join_rels = jtitem->qualscope;
1718 jtitem->left_rels = left_item->qualscope;
1719 jtitem->right_rels = right_item->qualscope;
1720 /* Inner join adds no restrictions for quals */
1721 jtitem->nonnullable_rels = NULL;
1722 break;
1723 case JOIN_LEFT:
1724 case JOIN_ANTI:
1725 /* Make new join domain for my quals and the RHS */
1727 child_domain->jd_relids = NULL; /* filled by recursion */
1728 root->join_domains = lappend(root->join_domains, child_domain);
1729 jtitem->jdomain = child_domain;
1730 /* Recurse */
1733 jtitem,
1734 item_list);
1738 jtitem,
1739 item_list);
1741 /* Compute join domain contents, qualscope etc */
1742 parent_domain->jd_relids =
1743 bms_add_members(parent_domain->jd_relids,
1744 child_domain->jd_relids);
1745 jtitem->qualscope = bms_union(left_item->qualscope,
1746 right_item->qualscope);
1747 /* caution: ANTI join derived from SEMI will lack rtindex */
1748 if (j->rtindex != 0)
1749 {
1750 parent_domain->jd_relids =
1751 bms_add_member(parent_domain->jd_relids,
1752 j->rtindex);
1753 jtitem->qualscope = bms_add_member(jtitem->qualscope,
1754 j->rtindex);
1755 root->outer_join_rels = bms_add_member(root->outer_join_rels,
1756 j->rtindex);
1758 right_item->qualscope);
1759 }
1760 jtitem->inner_join_rels = bms_union(left_item->inner_join_rels,
1761 right_item->inner_join_rels);
1762 jtitem->left_rels = left_item->qualscope;
1763 jtitem->right_rels = right_item->qualscope;
1764 jtitem->nonnullable_rels = left_item->qualscope;
1765 break;
1766 case JOIN_SEMI:
1767 /* This node belongs to parent_domain, as do its children */
1768 jtitem->jdomain = parent_domain;
1769 /* Recurse */
1772 jtitem,
1773 item_list);
1777 jtitem,
1778 item_list);
1780 /* Compute qualscope etc */
1781 jtitem->qualscope = bms_union(left_item->qualscope,
1782 right_item->qualscope);
1783 /* SEMI join never has rtindex, so don't add to anything */
1784 Assert(j->rtindex == 0);
1785 jtitem->inner_join_rels = bms_union(left_item->inner_join_rels,
1786 right_item->inner_join_rels);
1787 jtitem->left_rels = left_item->qualscope;
1788 jtitem->right_rels = right_item->qualscope;
1789 /* Semi join adds no restrictions for quals */
1790 jtitem->nonnullable_rels = NULL;
1791 break;
1792 case JOIN_FULL:
1793 /* The FULL JOIN's quals need their very own domain */
1795 root->join_domains = lappend(root->join_domains, fj_domain);
1796 jtitem->jdomain = fj_domain;
1797 /* Recurse, giving each side its own join domain */
1799 child_domain->jd_relids = NULL; /* filled by recursion */
1800 root->join_domains = lappend(root->join_domains, child_domain);
1803 jtitem,
1804 item_list);
1806 fj_domain->jd_relids = bms_copy(child_domain->jd_relids);
1808 child_domain->jd_relids = NULL; /* filled by recursion */
1809 root->join_domains = lappend(root->join_domains, child_domain);
1812 jtitem,
1813 item_list);
1815 /* Compute qualscope etc */
1816 fj_domain->jd_relids = bms_add_members(fj_domain->jd_relids,
1817 child_domain->jd_relids);
1818 parent_domain->jd_relids = bms_add_members(parent_domain->jd_relids,
1819 fj_domain->jd_relids);
1820 jtitem->qualscope = bms_union(left_item->qualscope,
1821 right_item->qualscope);
1822 Assert(j->rtindex != 0);
1823 parent_domain->jd_relids = bms_add_member(parent_domain->jd_relids,
1824 j->rtindex);
1825 jtitem->qualscope = bms_add_member(jtitem->qualscope,
1826 j->rtindex);
1827 root->outer_join_rels = bms_add_member(root->outer_join_rels,
1828 j->rtindex);
1830 left_item->qualscope);
1832 right_item->qualscope);
1833 jtitem->inner_join_rels = bms_union(left_item->inner_join_rels,
1834 right_item->inner_join_rels);
1835 jtitem->left_rels = left_item->qualscope;
1836 jtitem->right_rels = right_item->qualscope;
1837 /* each side is both outer and inner */
1838 jtitem->nonnullable_rels = jtitem->qualscope;
1839 break;
1840 default:
1841 /* JOIN_RIGHT was eliminated during reduce_outer_joins() */
1842 elog(ERROR, "unrecognized join type: %d",
1843 (int) j->jointype);
1844 leftjoinlist = rightjoinlist = NIL; /* keep compiler quiet */
1845 break;
1846 }
1847
1848 /*
1849 * Compute the output joinlist. We fold subproblems together except
1850 * at a FULL JOIN or where join_collapse_limit would be exceeded.
1851 */
1852 if (j->jointype == JOIN_FULL)
1853 {
1854 /* force the join order exactly at this node */
1856 }
1859 {
1860 /* OK to combine subproblems */
1862 }
1863 else
1864 {
1865 /* can't combine, but needn't force join order above here */
1866 Node *leftpart,
1867 *rightpart;
1868
1869 /* avoid creating useless 1-element sublists */
1870 if (list_length(leftjoinlist) == 1)
1872 else
1874 if (list_length(rightjoinlist) == 1)
1876 else
1879 }
1880 }
1881 else
1882 {
1883 elog(ERROR, "unrecognized node type: %d",
1884 (int) nodeTag(jtnode));
1885 joinlist = NIL; /* keep compiler quiet */
1886 }
1887
1888 /* Finally, we can add the new JoinTreeItem to item_list */
1890
1891 return joinlist;
1892}
1893
1894/*
1895 * deconstruct_distribute
1896 * Process one jointree node in phase 2 of deconstruct_jointree processing.
1897 *
1898 * Distribute quals of the node to appropriate restriction and join lists.
1899 * In addition, entries will be added to root->join_info_list for outer joins.
1900 */
1901static void
1903{
1904 Node *jtnode = jtitem->jtnode;
1905
1906 if (IsA(jtnode, RangeTblRef))
1907 {
1908 int varno = ((RangeTblRef *) jtnode)->rtindex;
1909
1910 /* Deal with any securityQuals attached to the RTE */
1911 if (root->qual_security_level > 0)
1913 varno,
1914 jtitem);
1915 }
1916 else if (IsA(jtnode, FromExpr))
1917 {
1918 FromExpr *f = (FromExpr *) jtnode;
1919
1920 /*
1921 * Process any lateral-referencing quals that were postponed to this
1922 * level by children.
1923 */
1924 distribute_quals_to_rels(root, jtitem->lateral_clauses,
1925 jtitem,
1926 NULL,
1927 root->qual_security_level,
1928 jtitem->qualscope,
1929 NULL, NULL, NULL,
1930 true, false, false,
1931 NULL);
1932
1933 /*
1934 * Now process the top-level quals.
1935 */
1937 jtitem,
1938 NULL,
1939 root->qual_security_level,
1940 jtitem->qualscope,
1941 NULL, NULL, NULL,
1942 true, false, false,
1943 NULL);
1944 }
1945 else if (IsA(jtnode, JoinExpr))
1946 {
1947 JoinExpr *j = (JoinExpr *) jtnode;
1949 List *my_quals;
1950 SpecialJoinInfo *sjinfo;
1952
1953 /*
1954 * Include lateral-referencing quals postponed from children in
1955 * my_quals, so that they'll be handled properly in
1956 * make_outerjoininfo. (This is destructive to
1957 * jtitem->lateral_clauses, but we won't use that again.)
1958 */
1959 my_quals = list_concat(jtitem->lateral_clauses,
1960 (List *) j->quals);
1961
1962 /*
1963 * For an OJ, form the SpecialJoinInfo now, so that we can pass it to
1964 * distribute_qual_to_rels. We must compute its ojscope too.
1965 *
1966 * Semijoins are a bit of a hybrid: we build a SpecialJoinInfo, but we
1967 * want ojscope = NULL for distribute_qual_to_rels.
1968 */
1969 if (j->jointype != JOIN_INNER)
1970 {
1971 sjinfo = make_outerjoininfo(root,
1972 jtitem->left_rels,
1973 jtitem->right_rels,
1974 jtitem->inner_join_rels,
1975 j->jointype,
1976 j->rtindex,
1977 my_quals);
1978 jtitem->sjinfo = sjinfo;
1979 if (j->jointype == JOIN_SEMI)
1980 ojscope = NULL;
1981 else
1982 ojscope = bms_union(sjinfo->min_lefthand,
1983 sjinfo->min_righthand);
1984 }
1985 else
1986 {
1987 sjinfo = NULL;
1988 ojscope = NULL;
1989 }
1990
1991 /*
1992 * If it's a left join with a join clause that is strict for the LHS,
1993 * then we need to postpone handling of any non-degenerate join
1994 * clauses, in case the join is able to commute with another left join
1995 * per identity 3. (Degenerate clauses need not be postponed, since
1996 * they will drop down below this join anyway.)
1997 */
1998 if (j->jointype == JOIN_LEFT && sjinfo->lhs_strict)
1999 {
2000 postponed_oj_qual_list = &jtitem->oj_joinclauses;
2001
2002 /*
2003 * Add back any commutable lower OJ relids that were removed from
2004 * min_lefthand or min_righthand, else the ojscope cross-check in
2005 * distribute_qual_to_rels will complain. Since we are postponing
2006 * processing of non-degenerate clauses, this addition doesn't
2007 * affect anything except that cross-check. Real clause
2008 * positioning decisions will be made later, when we revisit the
2009 * postponed clauses.
2010 */
2013 }
2014 else
2016
2017 /* Process the JOIN's qual clauses */
2019 jtitem,
2020 sjinfo,
2021 root->qual_security_level,
2022 jtitem->qualscope,
2023 ojscope, jtitem->nonnullable_rels,
2024 NULL, /* incompatible_relids */
2025 true, /* allow_equivalence */
2026 false, false, /* not clones */
2028
2029 /* And add the SpecialJoinInfo to join_info_list */
2030 if (sjinfo)
2031 root->join_info_list = lappend(root->join_info_list, sjinfo);
2032 }
2033 else
2034 {
2035 elog(ERROR, "unrecognized node type: %d",
2036 (int) nodeTag(jtnode));
2037 }
2038}
2039
2040/*
2041 * process_security_barrier_quals
2042 * Transfer security-barrier quals into relation's baserestrictinfo list.
2043 *
2044 * The rewriter put any relevant security-barrier conditions into the RTE's
2045 * securityQuals field, but it's now time to copy them into the rel's
2046 * baserestrictinfo.
2047 *
2048 * In inheritance cases, we only consider quals attached to the parent rel
2049 * here; they will be valid for all children too, so it's okay to consider
2050 * them for purposes like equivalence class creation. Quals attached to
2051 * individual child rels will be dealt with during path creation.
2052 */
2053static void
2055 int rti, JoinTreeItem *jtitem)
2056{
2057 RangeTblEntry *rte = root->simple_rte_array[rti];
2058 Index security_level = 0;
2059 ListCell *lc;
2060
2061 /*
2062 * Each element of the securityQuals list has been preprocessed into an
2063 * implicitly-ANDed list of clauses. All the clauses in a given sublist
2064 * should get the same security level, but successive sublists get higher
2065 * levels.
2066 */
2067 foreach(lc, rte->securityQuals)
2068 {
2069 List *qualset = (List *) lfirst(lc);
2070
2071 /*
2072 * We cheat to the extent of passing ojscope = qualscope rather than
2073 * its more logical value of NULL. The only effect this has is to
2074 * force a Var-free qual to be evaluated at the rel rather than being
2075 * pushed up to top of tree, which we don't want.
2076 */
2078 jtitem,
2079 NULL,
2080 security_level,
2081 jtitem->qualscope,
2082 jtitem->qualscope,
2083 NULL,
2084 NULL,
2085 true,
2086 false, false, /* not clones */
2087 NULL);
2088 security_level++;
2089 }
2090
2091 /* Assert that qual_security_level is higher than anything we just used */
2092 Assert(security_level <= root->qual_security_level);
2093}
2094
2095/*
2096 * mark_rels_nulled_by_join
2097 * Fill RelOptInfo.nulling_relids of baserels nulled by this outer join
2098 *
2099 * Inputs:
2100 * ojrelid: RT index of the join RTE (must not be 0)
2101 * lower_rels: the base+OJ Relids syntactically below nullable side of join
2102 */
2103static void
2106{
2107 int relid = -1;
2108
2109 while ((relid = bms_next_member(lower_rels, relid)) > 0)
2110 {
2111 RelOptInfo *rel = root->simple_rel_array[relid];
2112
2113 /* ignore the RTE_GROUP RTE */
2114 if (relid == root->group_rtindex)
2115 continue;
2116
2117 if (rel == NULL) /* must be an outer join */
2118 {
2119 Assert(bms_is_member(relid, root->outer_join_rels));
2120 continue;
2121 }
2122 rel->nulling_relids = bms_add_member(rel->nulling_relids, ojrelid);
2123 }
2124}
2125
2126/*
2127 * make_outerjoininfo
2128 * Build a SpecialJoinInfo for the current outer join
2129 *
2130 * Inputs:
2131 * left_rels: the base+OJ Relids syntactically on outer side of join
2132 * right_rels: the base+OJ Relids syntactically on inner side of join
2133 * inner_join_rels: base+OJ Relids participating in inner joins below this one
2134 * jointype: what it says (must always be LEFT, FULL, SEMI, or ANTI)
2135 * ojrelid: RT index of the join RTE (0 for SEMI, which isn't in the RT list)
2136 * clause: the outer join's join condition (in implicit-AND format)
2137 *
2138 * The node should eventually be appended to root->join_info_list, but we
2139 * do not do that here.
2140 *
2141 * Note: we assume that this function is invoked bottom-up, so that
2142 * root->join_info_list already contains entries for all outer joins that are
2143 * syntactically below this one.
2144 */
2145static SpecialJoinInfo *
2147 Relids left_rels, Relids right_rels,
2148 Relids inner_join_rels,
2149 JoinType jointype, Index ojrelid,
2150 List *clause)
2151{
2155 Relids min_lefthand;
2156 Relids min_righthand;
2157 Relids commute_below_l;
2158 Relids commute_below_r;
2159 ListCell *l;
2160
2161 /*
2162 * We should not see RIGHT JOIN here because left/right were switched
2163 * earlier
2164 */
2165 Assert(jointype != JOIN_INNER);
2166 Assert(jointype != JOIN_RIGHT);
2167
2168 /*
2169 * Presently the executor cannot support FOR [KEY] UPDATE/SHARE marking of
2170 * rels appearing on the nullable side of an outer join. (It's somewhat
2171 * unclear what that would mean, anyway: what should we mark when a result
2172 * row is generated from no element of the nullable relation?) So,
2173 * complain if any nullable rel is FOR [KEY] UPDATE/SHARE.
2174 *
2175 * You might be wondering why this test isn't made far upstream in the
2176 * parser. It's because the parser hasn't got enough info --- consider
2177 * FOR UPDATE applied to a view. Only after rewriting and flattening do
2178 * we know whether the view contains an outer join.
2179 *
2180 * We use the original RowMarkClause list here; the PlanRowMark list would
2181 * list everything.
2182 */
2183 foreach(l, root->parse->rowMarks)
2184 {
2185 RowMarkClause *rc = (RowMarkClause *) lfirst(l);
2186
2187 if (bms_is_member(rc->rti, right_rels) ||
2188 (jointype == JOIN_FULL && bms_is_member(rc->rti, left_rels)))
2189 ereport(ERROR,
2191 /*------
2192 translator: %s is a SQL row locking clause such as FOR UPDATE */
2193 errmsg("%s cannot be applied to the nullable side of an outer join",
2194 LCS_asString(rc->strength))));
2195 }
2196
2197 sjinfo->syn_lefthand = left_rels;
2198 sjinfo->syn_righthand = right_rels;
2199 sjinfo->jointype = jointype;
2200 sjinfo->ojrelid = ojrelid;
2201 /* these fields may get added to later: */
2202 sjinfo->commute_above_l = NULL;
2203 sjinfo->commute_above_r = NULL;
2204 sjinfo->commute_below_l = NULL;
2205 sjinfo->commute_below_r = NULL;
2206
2207 compute_semijoin_info(root, sjinfo, clause);
2208
2209 /* If it's a full join, no need to be very smart */
2210 if (jointype == JOIN_FULL)
2211 {
2212 sjinfo->min_lefthand = bms_copy(left_rels);
2213 sjinfo->min_righthand = bms_copy(right_rels);
2214 sjinfo->lhs_strict = false; /* don't care about this */
2215 return sjinfo;
2216 }
2217
2218 /*
2219 * Retrieve all relids mentioned within the join clause.
2220 */
2221 clause_relids = pull_varnos(root, (Node *) clause);
2222
2223 /*
2224 * For which relids is the clause strict, ie, it cannot succeed if the
2225 * rel's columns are all NULL?
2226 */
2228
2229 /* Remember whether the clause is strict for any LHS relations */
2230 sjinfo->lhs_strict = bms_overlap(strict_relids, left_rels);
2231
2232 /*
2233 * Required LHS always includes the LHS rels mentioned in the clause. We
2234 * may have to add more rels based on lower outer joins; see below.
2235 */
2236 min_lefthand = bms_intersect(clause_relids, left_rels);
2237
2238 /*
2239 * Similarly for required RHS. But here, we must also include any lower
2240 * inner joins, to ensure we don't try to commute with any of them.
2241 */
2242 min_righthand = bms_int_members(bms_union(clause_relids, inner_join_rels),
2243 right_rels);
2244
2245 /*
2246 * Now check previous outer joins for ordering restrictions.
2247 *
2248 * commute_below_l and commute_below_r accumulate the relids of lower
2249 * outer joins that we think this one can commute with. These decisions
2250 * are just tentative within this loop, since we might find an
2251 * intermediate outer join that prevents commutation. Surviving relids
2252 * will get merged into the SpecialJoinInfo structs afterwards.
2253 */
2254 commute_below_l = commute_below_r = NULL;
2255 foreach(l, root->join_info_list)
2256 {
2258 bool have_unsafe_phvs;
2259
2260 /*
2261 * A full join is an optimization barrier: we can't associate into or
2262 * out of it. Hence, if it overlaps either LHS or RHS of the current
2263 * rel, expand that side's min relset to cover the whole full join.
2264 */
2265 if (otherinfo->jointype == JOIN_FULL)
2266 {
2267 Assert(otherinfo->ojrelid != 0);
2268 if (bms_overlap(left_rels, otherinfo->syn_lefthand) ||
2269 bms_overlap(left_rels, otherinfo->syn_righthand))
2270 {
2271 min_lefthand = bms_add_members(min_lefthand,
2272 otherinfo->syn_lefthand);
2273 min_lefthand = bms_add_members(min_lefthand,
2274 otherinfo->syn_righthand);
2275 min_lefthand = bms_add_member(min_lefthand,
2276 otherinfo->ojrelid);
2277 }
2278 if (bms_overlap(right_rels, otherinfo->syn_lefthand) ||
2279 bms_overlap(right_rels, otherinfo->syn_righthand))
2280 {
2281 min_righthand = bms_add_members(min_righthand,
2282 otherinfo->syn_lefthand);
2283 min_righthand = bms_add_members(min_righthand,
2284 otherinfo->syn_righthand);
2285 min_righthand = bms_add_member(min_righthand,
2286 otherinfo->ojrelid);
2287 }
2288 /* Needn't do anything else with the full join */
2289 continue;
2290 }
2291
2292 /*
2293 * If our join condition contains any PlaceHolderVars that need to be
2294 * evaluated above the lower OJ, then we can't commute with it.
2295 */
2296 if (otherinfo->ojrelid != 0)
2299 (Node *) clause,
2300 otherinfo->ojrelid);
2301 else
2302 have_unsafe_phvs = false;
2303
2304 /*
2305 * For a lower OJ in our LHS, if our join condition uses the lower
2306 * join's RHS and is not strict for that rel, we must preserve the
2307 * ordering of the two OJs, so add lower OJ's full syntactic relset to
2308 * min_lefthand. (We must use its full syntactic relset, not just its
2309 * min_lefthand + min_righthand. This is because there might be other
2310 * OJs below this one that this one can commute with, but we cannot
2311 * commute with them if we don't with this one.) Also, if we have
2312 * unsafe PHVs or the current join is a semijoin or antijoin, we must
2313 * preserve ordering regardless of strictness.
2314 *
2315 * Note: I believe we have to insist on being strict for at least one
2316 * rel in the lower OJ's min_righthand, not its whole syn_righthand.
2317 *
2318 * When we don't need to preserve ordering, check to see if outer join
2319 * identity 3 applies, and if so, remove the lower OJ's ojrelid from
2320 * our min_lefthand so that commutation is allowed.
2321 */
2322 if (bms_overlap(left_rels, otherinfo->syn_righthand))
2323 {
2324 if (bms_overlap(clause_relids, otherinfo->syn_righthand) &&
2326 jointype == JOIN_SEMI || jointype == JOIN_ANTI ||
2327 !bms_overlap(strict_relids, otherinfo->min_righthand)))
2328 {
2329 /* Preserve ordering */
2330 min_lefthand = bms_add_members(min_lefthand,
2331 otherinfo->syn_lefthand);
2332 min_lefthand = bms_add_members(min_lefthand,
2333 otherinfo->syn_righthand);
2334 if (otherinfo->ojrelid != 0)
2335 min_lefthand = bms_add_member(min_lefthand,
2336 otherinfo->ojrelid);
2337 }
2338 else if (jointype == JOIN_LEFT &&
2339 otherinfo->jointype == JOIN_LEFT &&
2340 bms_overlap(strict_relids, otherinfo->min_righthand) &&
2341 !bms_overlap(clause_relids, otherinfo->syn_lefthand))
2342 {
2343 /* Identity 3 applies, so remove the ordering restriction */
2344 min_lefthand = bms_del_member(min_lefthand, otherinfo->ojrelid);
2345 /* Record the (still tentative) commutability relationship */
2346 commute_below_l =
2347 bms_add_member(commute_below_l, otherinfo->ojrelid);
2348 }
2349 }
2350
2351 /*
2352 * For a lower OJ in our RHS, if our join condition does not use the
2353 * lower join's RHS and the lower OJ's join condition is strict, we
2354 * can interchange the ordering of the two OJs; otherwise we must add
2355 * the lower OJ's full syntactic relset to min_righthand.
2356 *
2357 * Also, if our join condition does not use the lower join's LHS
2358 * either, force the ordering to be preserved. Otherwise we can end
2359 * up with SpecialJoinInfos with identical min_righthands, which can
2360 * confuse join_is_legal (see discussion in backend/optimizer/README).
2361 *
2362 * Also, we must preserve ordering anyway if we have unsafe PHVs, or
2363 * if either this join or the lower OJ is a semijoin or antijoin.
2364 *
2365 * When we don't need to preserve ordering, check to see if outer join
2366 * identity 3 applies, and if so, remove the lower OJ's ojrelid from
2367 * our min_righthand so that commutation is allowed.
2368 */
2369 if (bms_overlap(right_rels, otherinfo->syn_righthand))
2370 {
2371 if (bms_overlap(clause_relids, otherinfo->syn_righthand) ||
2372 !bms_overlap(clause_relids, otherinfo->min_lefthand) ||
2374 jointype == JOIN_SEMI ||
2375 jointype == JOIN_ANTI ||
2376 otherinfo->jointype == JOIN_SEMI ||
2377 otherinfo->jointype == JOIN_ANTI ||
2378 !otherinfo->lhs_strict)
2379 {
2380 /* Preserve ordering */
2381 min_righthand = bms_add_members(min_righthand,
2382 otherinfo->syn_lefthand);
2383 min_righthand = bms_add_members(min_righthand,
2384 otherinfo->syn_righthand);
2385 if (otherinfo->ojrelid != 0)
2386 min_righthand = bms_add_member(min_righthand,
2387 otherinfo->ojrelid);
2388 }
2389 else if (jointype == JOIN_LEFT &&
2390 otherinfo->jointype == JOIN_LEFT &&
2391 otherinfo->lhs_strict)
2392 {
2393 /* Identity 3 applies, so remove the ordering restriction */
2394 min_righthand = bms_del_member(min_righthand,
2395 otherinfo->ojrelid);
2396 /* Record the (still tentative) commutability relationship */
2397 commute_below_r =
2398 bms_add_member(commute_below_r, otherinfo->ojrelid);
2399 }
2400 }
2401 }
2402
2403 /*
2404 * Examine PlaceHolderVars. If a PHV is supposed to be evaluated within
2405 * this join's nullable side, then ensure that min_righthand contains the
2406 * full eval_at set of the PHV. This ensures that the PHV actually can be
2407 * evaluated within the RHS. Note that this works only because we should
2408 * already have determined the final eval_at level for any PHV
2409 * syntactically within this join.
2410 */
2411 foreach(l, root->placeholder_list)
2412 {
2414 Relids ph_syn_level = phinfo->ph_var->phrels;
2415
2416 /* Ignore placeholder if it didn't syntactically come from RHS */
2417 if (!bms_is_subset(ph_syn_level, right_rels))
2418 continue;
2419
2420 /* Else, prevent join from being formed before we eval the PHV */
2421 min_righthand = bms_add_members(min_righthand, phinfo->ph_eval_at);
2422 }
2423
2424 /*
2425 * If we found nothing to put in min_lefthand, punt and make it the full
2426 * LHS, to avoid having an empty min_lefthand which will confuse later
2427 * processing. (We don't try to be smart about such cases, just correct.)
2428 * Likewise for min_righthand.
2429 */
2430 if (bms_is_empty(min_lefthand))
2431 min_lefthand = bms_copy(left_rels);
2432 if (bms_is_empty(min_righthand))
2433 min_righthand = bms_copy(right_rels);
2434
2435 /* Now they'd better be nonempty */
2436 Assert(!bms_is_empty(min_lefthand));
2437 Assert(!bms_is_empty(min_righthand));
2438 /* Shouldn't overlap either */
2439 Assert(!bms_overlap(min_lefthand, min_righthand));
2440
2441 sjinfo->min_lefthand = min_lefthand;
2442 sjinfo->min_righthand = min_righthand;
2443
2444 /*
2445 * Now that we've identified the correct min_lefthand and min_righthand,
2446 * any commute_below_l or commute_below_r relids that have not gotten
2447 * added back into those sets (due to intervening outer joins) are indeed
2448 * commutable with this one.
2449 *
2450 * First, delete any subsequently-added-back relids (this is easier than
2451 * maintaining commute_below_l/r precisely through all the above).
2452 */
2453 commute_below_l = bms_del_members(commute_below_l, min_lefthand);
2454 commute_below_r = bms_del_members(commute_below_r, min_righthand);
2455
2456 /* Anything left? */
2457 if (commute_below_l || commute_below_r)
2458 {
2459 /* Yup, so we must update the derived data in the SpecialJoinInfos */
2460 sjinfo->commute_below_l = commute_below_l;
2461 sjinfo->commute_below_r = commute_below_r;
2462 foreach(l, root->join_info_list)
2463 {
2465
2466 if (bms_is_member(otherinfo->ojrelid, commute_below_l))
2468 bms_add_member(otherinfo->commute_above_l, ojrelid);
2469 else if (bms_is_member(otherinfo->ojrelid, commute_below_r))
2470 otherinfo->commute_above_r =
2471 bms_add_member(otherinfo->commute_above_r, ojrelid);
2472 }
2473 }
2474
2475 return sjinfo;
2476}
2477
2478/*
2479 * compute_semijoin_info
2480 * Fill semijoin-related fields of a new SpecialJoinInfo
2481 *
2482 * Note: this relies on only the jointype and syn_righthand fields of the
2483 * SpecialJoinInfo; the rest may not be set yet.
2484 */
2485static void
2487{
2488 List *semi_operators;
2489 List *semi_rhs_exprs;
2490 bool all_btree;
2491 bool all_hash;
2492 ListCell *lc;
2493
2494 /* Initialize semijoin-related fields in case we can't unique-ify */
2495 sjinfo->semi_can_btree = false;
2496 sjinfo->semi_can_hash = false;
2497 sjinfo->semi_operators = NIL;
2498 sjinfo->semi_rhs_exprs = NIL;
2499
2500 /* Nothing more to do if it's not a semijoin */
2501 if (sjinfo->jointype != JOIN_SEMI)
2502 return;
2503
2504 /*
2505 * Look to see whether the semijoin's join quals consist of AND'ed
2506 * equality operators, with (only) RHS variables on only one side of each
2507 * one. If so, we can figure out how to enforce uniqueness for the RHS.
2508 *
2509 * Note that the input clause list is the list of quals that are
2510 * *syntactically* associated with the semijoin, which in practice means
2511 * the synthesized comparison list for an IN or the WHERE of an EXISTS.
2512 * Particularly in the latter case, it might contain clauses that aren't
2513 * *semantically* associated with the join, but refer to just one side or
2514 * the other. We can ignore such clauses here, as they will just drop
2515 * down to be processed within one side or the other. (It is okay to
2516 * consider only the syntactically-associated clauses here because for a
2517 * semijoin, no higher-level quals could refer to the RHS, and so there
2518 * can be no other quals that are semantically associated with this join.
2519 * We do things this way because it is useful to have the set of potential
2520 * unique-ification expressions before we can extract the list of quals
2521 * that are actually semantically associated with the particular join.)
2522 *
2523 * Note that the semi_operators list consists of the joinqual operators
2524 * themselves (but commuted if needed to put the RHS value on the right).
2525 * These could be cross-type operators, in which case the operator
2526 * actually needed for uniqueness is a related single-type operator. We
2527 * assume here that that operator will be available from the btree or hash
2528 * opclass when the time comes ... if not, create_unique_plan() will fail.
2529 */
2530 semi_operators = NIL;
2531 semi_rhs_exprs = NIL;
2532 all_btree = true;
2533 all_hash = enable_hashagg; /* don't consider hash if not enabled */
2534 foreach(lc, clause)
2535 {
2536 OpExpr *op = (OpExpr *) lfirst(lc);
2537 Oid opno;
2538 Node *left_expr;
2544
2545 /* Is it a binary opclause? */
2546 if (!IsA(op, OpExpr) ||
2547 list_length(op->args) != 2)
2548 {
2549 /* No, but does it reference both sides? */
2550 all_varnos = pull_varnos(root, (Node *) op);
2551 if (!bms_overlap(all_varnos, sjinfo->syn_righthand) ||
2553 {
2554 /*
2555 * Clause refers to only one rel, so ignore it --- unless it
2556 * contains volatile functions, in which case we'd better
2557 * punt.
2558 */
2559 if (contain_volatile_functions((Node *) op))
2560 return;
2561 continue;
2562 }
2563 /* Non-operator clause referencing both sides, must punt */
2564 return;
2565 }
2566
2567 /* Extract data from binary opclause */
2568 opno = op->opno;
2569 left_expr = linitial(op->args);
2570 right_expr = lsecond(op->args);
2575
2576 /* Does it reference both sides? */
2577 if (!bms_overlap(all_varnos, sjinfo->syn_righthand) ||
2579 {
2580 /*
2581 * Clause refers to only one rel, so ignore it --- unless it
2582 * contains volatile functions, in which case we'd better punt.
2583 */
2584 if (contain_volatile_functions((Node *) op))
2585 return;
2586 continue;
2587 }
2588
2589 /* check rel membership of arguments */
2590 if (!bms_is_empty(right_varnos) &&
2593 {
2594 /* typical case, right_expr is RHS variable */
2595 }
2596 else if (!bms_is_empty(left_varnos) &&
2599 {
2600 /* flipped case, left_expr is RHS variable */
2601 opno = get_commutator(opno);
2602 if (!OidIsValid(opno))
2603 return;
2605 }
2606 else
2607 {
2608 /* mixed membership of args, punt */
2609 return;
2610 }
2611
2612 /* all operators must be btree equality or hash equality */
2613 if (all_btree)
2614 {
2615 /* oprcanmerge is considered a hint... */
2616 if (!op_mergejoinable(opno, opinputtype) ||
2618 all_btree = false;
2619 }
2620 if (all_hash)
2621 {
2622 /* ... but oprcanhash had better be correct */
2623 if (!op_hashjoinable(opno, opinputtype))
2624 all_hash = false;
2625 }
2626 if (!(all_btree || all_hash))
2627 return;
2628
2629 /* so far so good, keep building lists */
2630 semi_operators = lappend_oid(semi_operators, opno);
2631 semi_rhs_exprs = lappend(semi_rhs_exprs, copyObject(right_expr));
2632 }
2633
2634 /* Punt if we didn't find at least one column to unique-ify */
2635 if (semi_rhs_exprs == NIL)
2636 return;
2637
2638 /*
2639 * The expressions we'd need to unique-ify mustn't be volatile.
2640 */
2641 if (contain_volatile_functions((Node *) semi_rhs_exprs))
2642 return;
2643
2644 /*
2645 * If we get here, we can unique-ify the semijoin's RHS using at least one
2646 * of sorting and hashing. Save the information about how to do that.
2647 */
2648 sjinfo->semi_can_btree = all_btree;
2649 sjinfo->semi_can_hash = all_hash;
2650 sjinfo->semi_operators = semi_operators;
2651 sjinfo->semi_rhs_exprs = semi_rhs_exprs;
2652}
2653
2654/*
2655 * deconstruct_distribute_oj_quals
2656 * Adjust LEFT JOIN quals to be suitable for commuted-left-join cases,
2657 * then push them into the joinqual lists and EquivalenceClass structures.
2658 *
2659 * This runs immediately after we've completed the deconstruct_distribute scan.
2660 * jtitems contains all the JoinTreeItems (in depth-first order), and jtitem
2661 * is one that has postponed oj_joinclauses to deal with.
2662 */
2663static void
2665 List *jtitems,
2667{
2668 SpecialJoinInfo *sjinfo = jtitem->sjinfo;
2669 Relids qualscope,
2670 ojscope,
2671 nonnullable_rels;
2672
2673 /* Recompute syntactic and semantic scopes of this left join */
2674 qualscope = bms_union(sjinfo->syn_lefthand, sjinfo->syn_righthand);
2675 qualscope = bms_add_member(qualscope, sjinfo->ojrelid);
2676 ojscope = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
2677 nonnullable_rels = sjinfo->syn_lefthand;
2678
2679 /*
2680 * If this join can commute with any other ones per outer-join identity 3,
2681 * and it is the one providing the join clause with flexible semantics,
2682 * then we have to generate variants of the join clause with different
2683 * nullingrels labeling. Otherwise, just push out the postponed clause
2684 * as-is.
2685 */
2686 Assert(sjinfo->lhs_strict); /* else we shouldn't be here */
2687 if (sjinfo->commute_above_r || sjinfo->commute_below_l)
2688 {
2693 List *quals;
2695 ListCell *lc;
2696
2697 /* Identify the outer joins this one commutes with */
2698 joins_above = sjinfo->commute_above_r;
2699 joins_below = sjinfo->commute_below_l;
2700
2701 /*
2702 * Generate qual variants with different sets of nullingrels bits.
2703 *
2704 * We only need bit-sets that correspond to the successively less
2705 * deeply syntactically-nested subsets of this join and its
2706 * commutators. That's true first because obviously only those forms
2707 * of the Vars and PHVs could appear elsewhere in the query, and
2708 * second because the outer join identities do not provide a way to
2709 * re-order such joins in a way that would require different marking.
2710 * (That is, while the current join may commute with several others,
2711 * none of those others can commute with each other.) To visit the
2712 * interesting joins in syntactic nesting order, we rely on the
2713 * jtitems list to be ordered that way.
2714 *
2715 * We first strip out all the nullingrels bits corresponding to
2716 * commuting joins below this one, and then successively put them back
2717 * as we crawl up the join stack.
2718 */
2719 quals = jtitem->oj_joinclauses;
2721 quals = (List *) remove_nulling_relids((Node *) quals,
2723 NULL);
2724
2725 /*
2726 * We'll need to mark the lower versions of the quals as not safe to
2727 * apply above not-yet-processed joins of the stack. This prevents
2728 * possibly applying a cloned qual at the wrong join level.
2729 */
2732 sjinfo->ojrelid);
2733
2734 /*
2735 * Each time we produce RestrictInfo(s) from these quals, reset the
2736 * last_rinfo_serial counter, so that the RestrictInfos for the "same"
2737 * qual condition get identical serial numbers. (This relies on the
2738 * fact that we're not changing the qual list in any way that'd affect
2739 * the number of RestrictInfos built from it.) This'll allow us to
2740 * detect duplicative qual usage later.
2741 */
2742 save_last_rinfo_serial = root->last_rinfo_serial;
2743
2745 foreach(lc, jtitems)
2746 {
2749 bool below_sjinfo = false;
2750 bool above_sjinfo = false;
2753 bool allow_equivalence,
2754 has_clone,
2755 is_clone;
2756
2757 if (othersj == NULL)
2758 continue; /* not an outer-join item, ignore */
2759
2760 if (bms_is_member(othersj->ojrelid, joins_below))
2761 {
2762 /* othersj commutes with sjinfo from below left */
2763 below_sjinfo = true;
2764 }
2765 else if (othersj == sjinfo)
2766 {
2767 /* found our join in syntactic order */
2769 }
2770 else if (bms_is_member(othersj->ojrelid, joins_above))
2771 {
2772 /* othersj commutes with sjinfo from above */
2773 above_sjinfo = true;
2774 }
2775 else
2776 {
2777 /* othersj is not relevant, ignore */
2778 continue;
2779 }
2780
2781 /* Reset serial counter for this version of the quals */
2782 root->last_rinfo_serial = save_last_rinfo_serial;
2783
2784 /*
2785 * When we are looking at joins above sjinfo, we are envisioning
2786 * pushing sjinfo to above othersj, so add othersj's nulling bit
2787 * before distributing the quals. We should add it to Vars coming
2788 * from the current join's LHS: we want to transform the second
2789 * form of OJ identity 3 to the first form, in which Vars of
2790 * relation B will appear nulled by the syntactically-upper OJ
2791 * within the Pbc clause, but those of relation C will not. (In
2792 * the notation used by optimizer/README, we're converting a qual
2793 * of the form Pbc to Pb*c.) Of course, we must also remove that
2794 * bit from the incompatible_joins value, else we'll make a qual
2795 * that can't be placed anywhere.
2796 */
2797 if (above_sjinfo)
2798 {
2799 quals = (List *)
2800 add_nulling_relids((Node *) quals,
2801 sjinfo->syn_lefthand,
2802 bms_make_singleton(othersj->ojrelid));
2804 othersj->ojrelid);
2805 }
2806
2807 /* Compute qualscope and ojscope for this join level */
2810 if (above_sjinfo)
2811 {
2812 /* othersj is not yet in joins_so_far, but we need it */
2814 othersj->ojrelid);
2816 othersj->ojrelid);
2817 /* sjinfo is in joins_so_far, and we don't want it */
2819 sjinfo->ojrelid);
2820 }
2821
2822 /*
2823 * We generate EquivalenceClasses only from the first form of the
2824 * quals, with the fewest nullingrels bits set. An EC made from
2825 * this version of the quals can be useful below the outer-join
2826 * nest, whereas versions with some nullingrels bits set would not
2827 * be. We cannot generate ECs from more than one version, or
2828 * we'll make nonsensical conclusions that Vars with nullingrels
2829 * bits set are equal to their versions without. Fortunately,
2830 * such ECs wouldn't be very useful anyway, because they'd equate
2831 * values not observable outside the join nest. (See
2832 * optimizer/README.)
2833 *
2834 * The first form of the quals is also the only one marked as
2835 * has_clone rather than is_clone.
2836 */
2838 has_clone = allow_equivalence;
2839 is_clone = !has_clone;
2840
2843 sjinfo,
2844 root->qual_security_level,
2846 this_ojscope, nonnullable_rels,
2849 has_clone,
2850 is_clone,
2851 NULL); /* no more postponement */
2852
2853 /*
2854 * Adjust qual nulling bits for next level up, if needed. We
2855 * don't want to put sjinfo's own bit in at all, and if we're
2856 * above sjinfo then we did it already. Here, we should mark all
2857 * Vars coming from the lower join's RHS. (Again, we are
2858 * converting a qual of the form Pbc to Pb*c, but now we are
2859 * putting back bits that were there in the parser output and were
2860 * temporarily stripped above.) Update incompatible_joins too.
2861 */
2862 if (below_sjinfo)
2863 {
2864 quals = (List *)
2865 add_nulling_relids((Node *) quals,
2866 othersj->syn_righthand,
2867 bms_make_singleton(othersj->ojrelid));
2869 othersj->ojrelid);
2870 }
2871
2872 /* ... and track joins processed so far */
2874 }
2875 }
2876 else
2877 {
2878 /* No commutation possible, just process the postponed clauses */
2879 distribute_quals_to_rels(root, jtitem->oj_joinclauses,
2880 jtitem,
2881 sjinfo,
2882 root->qual_security_level,
2883 qualscope,
2884 ojscope, nonnullable_rels,
2885 NULL, /* incompatible_relids */
2886 true, /* allow_equivalence */
2887 false, false, /* not clones */
2888 NULL); /* no more postponement */
2889 }
2890}
2891
2892
2893/*****************************************************************************
2894 *
2895 * QUALIFICATIONS
2896 *
2897 *****************************************************************************/
2898
2899/*
2900 * distribute_quals_to_rels
2901 * Convenience routine to apply distribute_qual_to_rels to each element
2902 * of an AND'ed list of clauses.
2903 */
2904static void
2907 SpecialJoinInfo *sjinfo,
2908 Index security_level,
2909 Relids qualscope,
2912 Relids incompatible_relids,
2913 bool allow_equivalence,
2914 bool has_clone,
2915 bool is_clone,
2917{
2918 ListCell *lc;
2919
2920 foreach(lc, clauses)
2921 {
2922 Node *clause = (Node *) lfirst(lc);
2923
2925 jtitem,
2926 sjinfo,
2927 security_level,
2928 qualscope,
2929 ojscope,
2931 incompatible_relids,
2933 has_clone,
2934 is_clone,
2936 }
2937}
2938
2939/*
2940 * distribute_qual_to_rels
2941 * Add clause information to either the baserestrictinfo or joininfo list
2942 * (depending on whether the clause is a join) of each base relation
2943 * mentioned in the clause. A RestrictInfo node is created and added to
2944 * the appropriate list for each rel. Alternatively, if the clause uses a
2945 * mergejoinable operator, enter its left- and right-side expressions into
2946 * the query's EquivalenceClasses.
2947 *
2948 * In some cases, quals will be added to parent jtitems' lateral_clauses
2949 * or to postponed_oj_qual_list instead of being processed right away.
2950 * These will be dealt with in later calls of deconstruct_distribute.
2951 *
2952 * 'clause': the qual clause to be distributed
2953 * 'jtitem': the JoinTreeItem for the containing jointree node
2954 * 'sjinfo': join's SpecialJoinInfo (NULL for an inner join or WHERE clause)
2955 * 'security_level': security_level to assign to the qual
2956 * 'qualscope': set of base+OJ rels the qual's syntactic scope covers
2957 * 'ojscope': NULL if not an outer-join qual, else the minimum set of base+OJ
2958 * rels needed to form this join
2959 * 'outerjoin_nonnullable': NULL if not an outer-join qual, else the set of
2960 * base+OJ rels appearing on the outer (nonnullable) side of the join
2961 * (for FULL JOIN this includes both sides of the join, and must in fact
2962 * equal qualscope)
2963 * 'incompatible_relids': the set of outer-join relid(s) that must not be
2964 * computed below this qual. We only bother to compute this for
2965 * "clone" quals, otherwise it can be left NULL.
2966 * 'allow_equivalence': true if it's okay to convert clause into an
2967 * EquivalenceClass
2968 * 'has_clone': has_clone property to assign to the qual
2969 * 'is_clone': is_clone property to assign to the qual
2970 * 'postponed_oj_qual_list': if not NULL, non-degenerate outer join clauses
2971 * should be added to this list instead of being processed (list entries
2972 * are just the bare clauses)
2973 *
2974 * 'qualscope' identifies what level of JOIN the qual came from syntactically.
2975 * 'ojscope' is needed if we decide to force the qual up to the outer-join
2976 * level, which will be ojscope not necessarily qualscope.
2977 *
2978 * At the time this is called, root->join_info_list must contain entries for
2979 * at least those special joins that are syntactically below this qual.
2980 * (We now need that only for detection of redundant IS NULL quals.)
2981 */
2982static void
2985 SpecialJoinInfo *sjinfo,
2986 Index security_level,
2987 Relids qualscope,
2990 Relids incompatible_relids,
2991 bool allow_equivalence,
2992 bool has_clone,
2993 bool is_clone,
2995{
2996 Relids relids;
2997 bool is_pushed_down;
2998 bool pseudoconstant = false;
2999 bool maybe_equivalence;
3000 bool maybe_outer_join;
3002
3003 /*
3004 * Retrieve all relids mentioned within the clause.
3005 */
3006 relids = pull_varnos(root, clause);
3007
3008 /*
3009 * In ordinary SQL, a WHERE or JOIN/ON clause can't reference any rels
3010 * that aren't within its syntactic scope; however, if we pulled up a
3011 * LATERAL subquery then we might find such references in quals that have
3012 * been pulled up. We need to treat such quals as belonging to the join
3013 * level that includes every rel they reference. Although we could make
3014 * pull_up_subqueries() place such quals correctly to begin with, it's
3015 * easier to handle it here. When we find a clause that contains Vars
3016 * outside its syntactic scope, locate the nearest parent join level that
3017 * includes all the required rels and add the clause to that level's
3018 * lateral_clauses list. We'll process it when we reach that join level.
3019 */
3020 if (!bms_is_subset(relids, qualscope))
3021 {
3023
3024 Assert(root->hasLateralRTEs); /* shouldn't happen otherwise */
3025 Assert(sjinfo == NULL); /* mustn't postpone past outer join */
3026 for (pitem = jtitem->jti_parent; pitem; pitem = pitem->jti_parent)
3027 {
3028 if (bms_is_subset(relids, pitem->qualscope))
3029 {
3030 pitem->lateral_clauses = lappend(pitem->lateral_clauses,
3031 clause);
3032 return;
3033 }
3034
3035 /*
3036 * We should not be postponing any quals past an outer join. If
3037 * this Assert fires, pull_up_subqueries() messed up.
3038 */
3039 Assert(pitem->sjinfo == NULL);
3040 }
3041 elog(ERROR, "failed to postpone qual containing lateral reference");
3042 }
3043
3044 /*
3045 * If it's an outer-join clause, also check that relids is a subset of
3046 * ojscope. (This should not fail if the syntactic scope check passed.)
3047 */
3048 if (ojscope && !bms_is_subset(relids, ojscope))
3049 elog(ERROR, "JOIN qualification cannot refer to other relations");
3050
3051 /*
3052 * If the clause is variable-free, our normal heuristic for pushing it
3053 * down to just the mentioned rels doesn't work, because there are none.
3054 *
3055 * If the clause is an outer-join clause, we must force it to the OJ's
3056 * semantic level to preserve semantics.
3057 *
3058 * Otherwise, when the clause contains volatile functions, we force it to
3059 * be evaluated at its original syntactic level. This preserves the
3060 * expected semantics.
3061 *
3062 * When the clause contains no volatile functions either, it is actually a
3063 * pseudoconstant clause that will not change value during any one
3064 * execution of the plan, and hence can be used as a one-time qual in a
3065 * gating Result plan node. We put such a clause into the regular
3066 * RestrictInfo lists for the moment, but eventually createplan.c will
3067 * pull it out and make a gating Result node immediately above whatever
3068 * plan node the pseudoconstant clause is assigned to. It's usually best
3069 * to put a gating node as high in the plan tree as possible.
3070 */
3071 if (bms_is_empty(relids))
3072 {
3073 if (ojscope)
3074 {
3075 /* clause is attached to outer join, eval it there */
3076 relids = bms_copy(ojscope);
3077 /* mustn't use as gating qual, so don't mark pseudoconstant */
3078 }
3079 else if (contain_volatile_functions(clause))
3080 {
3081 /* eval at original syntactic level */
3082 relids = bms_copy(qualscope);
3083 /* again, can't mark pseudoconstant */
3084 }
3085 else
3086 {
3087 /*
3088 * If we are in the top-level join domain, we can push the qual to
3089 * the top of the plan tree. Otherwise, be conservative and eval
3090 * it at original syntactic level. (Ideally we'd push it to the
3091 * top of the current join domain in all cases, but that causes
3092 * problems if we later rearrange outer-join evaluation order.
3093 * Pseudoconstant quals below the top level are a pretty odd case,
3094 * so it's not clear that it's worth working hard on.)
3095 */
3096 if (jtitem->jdomain == (JoinDomain *) linitial(root->join_domains))
3097 relids = bms_copy(jtitem->jdomain->jd_relids);
3098 else
3099 relids = bms_copy(qualscope);
3100 /* mark as gating qual */
3101 pseudoconstant = true;
3102 /* tell createplan.c to check for gating quals */
3103 root->hasPseudoConstantQuals = true;
3104 }
3105 }
3106
3107 /*----------
3108 * Check to see if clause application must be delayed by outer-join
3109 * considerations.
3110 *
3111 * A word about is_pushed_down: we mark the qual as "pushed down" if
3112 * it is (potentially) applicable at a level different from its original
3113 * syntactic level. This flag is used to distinguish OUTER JOIN ON quals
3114 * from other quals pushed down to the same joinrel. The rules are:
3115 * WHERE quals and INNER JOIN quals: is_pushed_down = true.
3116 * Non-degenerate OUTER JOIN quals: is_pushed_down = false.
3117 * Degenerate OUTER JOIN quals: is_pushed_down = true.
3118 * A "degenerate" OUTER JOIN qual is one that doesn't mention the
3119 * non-nullable side, and hence can be pushed down into the nullable side
3120 * without changing the join result. It is correct to treat it as a
3121 * regular filter condition at the level where it is evaluated.
3122 *
3123 * Note: it is not immediately obvious that a simple boolean is enough
3124 * for this: if for some reason we were to attach a degenerate qual to
3125 * its original join level, it would need to be treated as an outer join
3126 * qual there. However, this cannot happen, because all the rels the
3127 * clause mentions must be in the outer join's min_righthand, therefore
3128 * the join it needs must be formed before the outer join; and we always
3129 * attach quals to the lowest level where they can be evaluated. But
3130 * if we were ever to re-introduce a mechanism for delaying evaluation
3131 * of "expensive" quals, this area would need work.
3132 *
3133 * Note: generally, use of is_pushed_down has to go through the macro
3134 * RINFO_IS_PUSHED_DOWN, because that flag alone is not always sufficient
3135 * to tell whether a clause must be treated as pushed-down in context.
3136 * This seems like another reason why it should perhaps be rethought.
3137 *----------
3138 */
3140 {
3141 /*
3142 * The qual is attached to an outer join and mentions (some of the)
3143 * rels on the nonnullable side, so it's not degenerate. If the
3144 * caller wants to postpone handling such clauses, just add it to
3145 * postponed_oj_qual_list and return. (The work we've done up to here
3146 * will have to be redone later, but there's not much of it.)
3147 */
3149 {
3151 return;
3152 }
3153
3154 /*
3155 * We can't use such a clause to deduce equivalence (the left and
3156 * right sides might be unequal above the join because one of them has
3157 * gone to NULL) ... but we might be able to use it for more limited
3158 * deductions, if it is mergejoinable. So consider adding it to the
3159 * lists of set-aside outer-join clauses.
3160 */
3161 is_pushed_down = false;
3162 maybe_equivalence = false;
3163 maybe_outer_join = true;
3164
3165 /*
3166 * Now force the qual to be evaluated exactly at the level of joining
3167 * corresponding to the outer join. We cannot let it get pushed down
3168 * into the nonnullable side, since then we'd produce no output rows,
3169 * rather than the intended single null-extended row, for any
3170 * nonnullable-side rows failing the qual.
3171 */
3172 Assert(ojscope);
3173 relids = ojscope;
3175 }
3176 else
3177 {
3178 /*
3179 * Normal qual clause or degenerate outer-join clause. Either way, we
3180 * can mark it as pushed-down.
3181 */
3182 is_pushed_down = true;
3183
3184 /*
3185 * It's possible that this is an IS NULL clause that's redundant with
3186 * a lower antijoin; if so we can just discard it. We need not test
3187 * in any of the other cases, because this will only be possible for
3188 * pushed-down clauses.
3189 */
3191 return;
3192
3193 /* Feed qual to the equivalence machinery, if allowed by caller */
3195
3196 /*
3197 * Since it doesn't mention the LHS, it's certainly not useful as a
3198 * set-aside OJ clause, even if it's in an OJ.
3199 */
3200 maybe_outer_join = false;
3201 }
3202
3203 /*
3204 * Build the RestrictInfo node itself.
3205 */
3207 (Expr *) clause,
3208 is_pushed_down,
3209 has_clone,
3210 is_clone,
3212 security_level,
3213 relids,
3214 incompatible_relids,
3216
3217 /*
3218 * If it's a join clause, add vars used in the clause to targetlists of
3219 * their relations, so that they will be emitted by the plan nodes that
3220 * scan those relations (else they won't be available at the join node!).
3221 *
3222 * Normally we mark the vars as needed at the join identified by "relids".
3223 * However, if this is a clone clause then ignore the outer-join relids in
3224 * that set. Otherwise, vars appearing in a cloned clause would end up
3225 * marked as having to propagate to the highest one of the commuting
3226 * joins, which would often be an overestimate. For such clauses, correct
3227 * var propagation is ensured by making ojscope include input rels from
3228 * both sides of the join.
3229 *
3230 * See also rebuild_joinclause_attr_needed, which has to partially repeat
3231 * this work after removal of an outer join.
3232 *
3233 * Note: if the clause gets absorbed into an EquivalenceClass then this
3234 * may be unnecessary, but for now we have to do it to cover the case
3235 * where the EC becomes ec_broken and we end up reinserting the original
3236 * clauses into the plan.
3237 */
3238 if (bms_membership(relids) == BMS_MULTIPLE)
3239 {
3240 List *vars = pull_var_clause(clause,
3245
3246 if (is_clone)
3247 where_needed = bms_intersect(relids, root->all_baserels);
3248 else
3249 where_needed = relids;
3251 list_free(vars);
3252 }
3253
3254 /*
3255 * We check "mergejoinability" of every clause, not only join clauses,
3256 * because we want to know about equivalences between vars of the same
3257 * relation, or between vars and consts.
3258 */
3260
3261 /*
3262 * If it is a true equivalence clause, send it to the EquivalenceClass
3263 * machinery. We do *not* attach it directly to any restriction or join
3264 * lists. The EC code will propagate it to the appropriate places later.
3265 *
3266 * If the clause has a mergejoinable operator, yet isn't an equivalence
3267 * because it is an outer-join clause, the EC code may still be able to do
3268 * something with it. We add it to appropriate lists for further
3269 * consideration later. Specifically:
3270 *
3271 * If it is a left or right outer-join qualification that relates the two
3272 * sides of the outer join (no funny business like leftvar1 = leftvar2 +
3273 * rightvar), we add it to root->left_join_clauses or
3274 * root->right_join_clauses according to which side the nonnullable
3275 * variable appears on.
3276 *
3277 * If it is a full outer-join qualification, we add it to
3278 * root->full_join_clauses. (Ideally we'd discard cases that aren't
3279 * leftvar = rightvar, as we do for left/right joins, but this routine
3280 * doesn't have the info needed to do that; and the current usage of the
3281 * full_join_clauses list doesn't require that, so it's not currently
3282 * worth complicating this routine's API to make it possible.)
3283 *
3284 * If none of the above hold, pass it off to
3285 * distribute_restrictinfo_to_rels().
3286 *
3287 * In all cases, it's important to initialize the left_ec and right_ec
3288 * fields of a mergejoinable clause, so that all possibly mergejoinable
3289 * expressions have representations in EquivalenceClasses. If
3290 * process_equivalence is successful, it will take care of that;
3291 * otherwise, we have to call initialize_mergeclause_eclasses to do it.
3292 */
3293 if (restrictinfo->mergeopfamilies)
3294 {
3296 {
3298 return;
3299 /* EC rejected it, so set left_ec/right_ec the hard way ... */
3300 if (restrictinfo->mergeopfamilies) /* EC might have changed this */
3302 /* ... and fall through to distribute_restrictinfo_to_rels */
3303 }
3304 else if (maybe_outer_join && restrictinfo->can_join)
3305 {
3306 /* we need to set up left_ec/right_ec the hard way */
3308 /* now see if it should go to any outer-join lists */
3309 Assert(sjinfo != NULL);
3310 if (bms_is_subset(restrictinfo->left_relids,
3312 !bms_overlap(restrictinfo->right_relids,
3314 {
3315 /* we have outervar = innervar */
3317
3318 ojcinfo->rinfo = restrictinfo;
3319 ojcinfo->sjinfo = sjinfo;
3320 root->left_join_clauses = lappend(root->left_join_clauses,
3321 ojcinfo);
3322 return;
3323 }
3324 if (bms_is_subset(restrictinfo->right_relids,
3326 !bms_overlap(restrictinfo->left_relids,
3328 {
3329 /* we have innervar = outervar */
3331
3332 ojcinfo->rinfo = restrictinfo;
3333 ojcinfo->sjinfo = sjinfo;
3334 root->right_join_clauses = lappend(root->right_join_clauses,
3335 ojcinfo);
3336 return;
3337 }
3338 if (sjinfo->jointype == JOIN_FULL)
3339 {
3340 /* FULL JOIN (above tests cannot match in this case) */
3342
3343 ojcinfo->rinfo = restrictinfo;
3344 ojcinfo->sjinfo = sjinfo;
3345 root->full_join_clauses = lappend(root->full_join_clauses,
3346 ojcinfo);
3347 return;
3348 }
3349 /* nope, so fall through to distribute_restrictinfo_to_rels */
3350 }
3351 else
3352 {
3353 /* we still need to set up left_ec/right_ec */
3355 }
3356 }
3357
3358 /* No EC special case applies, so push it into the clause lists */
3360}
3361
3362/*
3363 * check_redundant_nullability_qual
3364 * Check to see if the qual is an IS NULL qual that is redundant with
3365 * a lower JOIN_ANTI join.
3366 *
3367 * We want to suppress redundant IS NULL quals, not so much to save cycles
3368 * as to avoid generating bogus selectivity estimates for them. So if
3369 * redundancy is detected here, distribute_qual_to_rels() just throws away
3370 * the qual.
3371 */
3372static bool
3374{
3376 ListCell *lc;
3377
3378 /* Check for IS NULL, and identify the Var forced to NULL */
3380 if (forced_null_var == NULL)
3381 return false;
3382
3383 /*
3384 * If the Var comes from the nullable side of a lower antijoin, the IS
3385 * NULL condition is necessarily true. If it's not nulled by anything,
3386 * there is no point in searching the join_info_list. Otherwise, we need
3387 * to find out whether the nulling rel is an antijoin.
3388 */
3389 if (forced_null_var->varnullingrels == NULL)
3390 return false;
3391
3392 foreach(lc, root->join_info_list)
3393 {
3395
3396 /*
3397 * This test will not succeed if sjinfo->ojrelid is zero, which is
3398 * possible for an antijoin that was converted from a semijoin; but in
3399 * such a case the Var couldn't have come from its nullable side.
3400 */
3401 if (sjinfo->jointype == JOIN_ANTI && sjinfo->ojrelid != 0 &&
3402 bms_is_member(sjinfo->ojrelid, forced_null_var->varnullingrels))
3403 return true;
3404 }
3405
3406 return false;
3407}
3408
3409/*
3410 * add_base_clause_to_rel
3411 * Add 'restrictinfo' as a baserestrictinfo to the base relation denoted
3412 * by 'relid'. We offer some simple prechecks to try to determine if the
3413 * qual is always true, in which case we ignore it rather than add it.
3414 * If we detect the qual is always false, we replace it with
3415 * constant-FALSE.
3416 */
3417static void
3420{
3421 RelOptInfo *rel = find_base_rel(root, relid);
3422 RangeTblEntry *rte = root->simple_rte_array[relid];
3423
3424 Assert(bms_membership(restrictinfo->required_relids) == BMS_SINGLETON);
3425
3426 /*
3427 * For inheritance parent tables, we must always record the RestrictInfo
3428 * in baserestrictinfo as is. If we were to transform or skip adding it,
3429 * then the original wouldn't be available in apply_child_basequals. Since
3430 * there are two RangeTblEntries for inheritance parents, one with
3431 * inh==true and the other with inh==false, we're still able to apply this
3432 * optimization to the inh==false one. The inh==true one is what
3433 * apply_child_basequals() sees, whereas the inh==false one is what's used
3434 * for the scan node in the final plan.
3435 *
3436 * We make an exception to this for partitioned tables. For these, we
3437 * always apply the constant-TRUE and constant-FALSE transformations. A
3438 * qual which is either of these for a partitioned table must also be that
3439 * for all of its child partitions.
3440 */
3441 if (!rte->inh || rte->relkind == RELKIND_PARTITIONED_TABLE)
3442 {
3443 /* Don't add the clause if it is always true */
3445 return;
3446
3447 /*
3448 * Substitute the origin qual with constant-FALSE if it is provably
3449 * always false.
3450 *
3451 * Note that we need to keep the same rinfo_serial, since it is in
3452 * practice the same condition. We also need to reset the
3453 * last_rinfo_serial counter, which is essential to ensure that the
3454 * RestrictInfos for the "same" qual condition get identical serial
3455 * numbers (see deconstruct_distribute_oj_quals).
3456 */
3458 {
3459 int save_rinfo_serial = restrictinfo->rinfo_serial;
3460 int save_last_rinfo_serial = root->last_rinfo_serial;
3461
3463 (Expr *) makeBoolConst(false, false),
3464 restrictinfo->is_pushed_down,
3465 restrictinfo->has_clone,
3466 restrictinfo->is_clone,
3467 restrictinfo->pseudoconstant,
3468 0, /* security_level */
3469 restrictinfo->required_relids,
3470 restrictinfo->incompatible_relids,
3471 restrictinfo->outer_relids);
3472 restrictinfo->rinfo_serial = save_rinfo_serial;
3473 root->last_rinfo_serial = save_last_rinfo_serial;
3474 }
3475 }
3476
3477 /* Add clause to rel's restriction list */
3479
3480 /* Update security level info */
3482 restrictinfo->security_level);
3483}
3484
3485/*
3486 * restriction_is_always_true
3487 * Check to see if the RestrictInfo is always true.
3488 *
3489 * Currently we only check for NullTest quals and OR clauses that include
3490 * NullTest quals. We may extend it in the future.
3491 */
3492bool
3495{
3496 /*
3497 * For a clone clause, we don't have a reliable way to determine if the
3498 * input expression of a NullTest is non-nullable: nullingrel bits in
3499 * clone clauses may not reflect reality, so we dare not draw conclusions
3500 * from clones about whether Vars are guaranteed not-null.
3501 */
3502 if (restrictinfo->has_clone || restrictinfo->is_clone)
3503 return false;
3504
3505 /* Check for NullTest qual */
3506 if (IsA(restrictinfo->clause, NullTest))
3507 {
3508 NullTest *nulltest = (NullTest *) restrictinfo->clause;
3509
3510 /* is this NullTest an IS_NOT_NULL qual? */
3511 if (nulltest->nulltesttype != IS_NOT_NULL)
3512 return false;
3513
3514 /*
3515 * Empty rows can appear NULL in some contexts and NOT NULL in others,
3516 * so avoid this optimization for row expressions.
3517 */
3518 if (nulltest->argisrow)
3519 return false;
3520
3522 }
3523
3524 /* If it's an OR, check its sub-clauses */
3526 {
3527 ListCell *lc;
3528
3529 Assert(is_orclause(restrictinfo->orclause));
3530
3531 /*
3532 * if any of the given OR branches is provably always true then the
3533 * entire condition is true.
3534 */
3535 foreach(lc, ((BoolExpr *) restrictinfo->orclause)->args)
3536 {
3537 Node *orarg = (Node *) lfirst(lc);
3538
3539 if (!IsA(orarg, RestrictInfo))
3540 continue;
3541
3543 return true;
3544 }
3545 }
3546
3547 return false;
3548}
3549
3550/*
3551 * restriction_is_always_false
3552 * Check to see if the RestrictInfo is always false.
3553 *
3554 * Currently we only check for NullTest quals and OR clauses that include
3555 * NullTest quals. We may extend it in the future.
3556 */
3557bool
3560{
3561 /*
3562 * For a clone clause, we don't have a reliable way to determine if the
3563 * input expression of a NullTest is non-nullable: nullingrel bits in
3564 * clone clauses may not reflect reality, so we dare not draw conclusions
3565 * from clones about whether Vars are guaranteed not-null.
3566 */
3567 if (restrictinfo->has_clone || restrictinfo->is_clone)
3568 return false;
3569
3570 /* Check for NullTest qual */
3571 if (IsA(restrictinfo->clause, NullTest))
3572 {
3573 NullTest *nulltest = (NullTest *) restrictinfo->clause;
3574
3575 /* is this NullTest an IS_NULL qual? */
3576 if (nulltest->nulltesttype != IS_NULL)
3577 return false;
3578
3579 /*
3580 * Empty rows can appear NULL in some contexts and NOT NULL in others,
3581 * so avoid this optimization for row expressions.
3582 */
3583 if (nulltest->argisrow)
3584 return false;
3585
3587 }
3588
3589 /* If it's an OR, check its sub-clauses */
3591 {
3592 ListCell *lc;
3593
3594 Assert(is_orclause(restrictinfo->orclause));
3595
3596 /*
3597 * Currently, when processing OR expressions, we only return true when
3598 * all of the OR branches are always false. This could perhaps be
3599 * expanded to remove OR branches that are provably false. This may
3600 * be a useful thing to do as it could result in the OR being left
3601 * with a single arg. That's useful as it would allow the OR
3602 * condition to be replaced with its single argument which may allow
3603 * use of an index for faster filtering on the remaining condition.
3604 */
3605 foreach(lc, ((BoolExpr *) restrictinfo->orclause)->args)
3606 {
3607 Node *orarg = (Node *) lfirst(lc);
3608
3609 if (!IsA(orarg, RestrictInfo) ||
3611 return false;
3612 }
3613 return true;
3614 }
3615
3616 return false;
3617}
3618
3619/*
3620 * distribute_restrictinfo_to_rels
3621 * Push a completed RestrictInfo into the proper restriction or join
3622 * clause list(s).
3623 *
3624 * This is the last step of distribute_qual_to_rels() for ordinary qual
3625 * clauses. Clauses that are interesting for equivalence-class processing
3626 * are diverted to the EC machinery, but may ultimately get fed back here.
3627 */
3628void
3631{
3632 Relids relids = restrictinfo->required_relids;
3633
3634 if (!bms_is_empty(relids))
3635 {
3636 int relid;
3637
3638 if (bms_get_singleton_member(relids, &relid))
3639 {
3640 /*
3641 * There is only one relation participating in the clause, so it
3642 * is a restriction clause for that relation.
3643 */
3645 }
3646 else
3647 {
3648 /*
3649 * The clause is a join clause, since there is more than one rel
3650 * in its relid set.
3651 */
3652
3653 /*
3654 * Check for hashjoinable operators. (We don't bother setting the
3655 * hashjoin info except in true join clauses.)
3656 */
3658
3659 /*
3660 * Likewise, check if the clause is suitable to be used with a
3661 * Memoize node to cache inner tuples during a parameterized
3662 * nested loop.
3663 */
3665
3666 /*
3667 * Add clause to the join lists of all the relevant relations.
3668 */
3670 }
3671 }
3672 else
3673 {
3674 /*
3675 * clause references no rels, and therefore we have no place to attach
3676 * it. Shouldn't get here if callers are working properly.
3677 */
3678 elog(ERROR, "cannot cope with variable-free clause");
3679 }
3680}
3681
3682/*
3683 * process_implied_equality
3684 * Create a restrictinfo item that says "item1 op item2", and push it
3685 * into the appropriate lists. (In practice opno is always a btree
3686 * equality operator.)
3687 *
3688 * "qualscope" is the nominal syntactic level to impute to the restrictinfo.
3689 * This must contain at least all the rels used in the expressions, but it
3690 * is used only to set the qual application level when both exprs are
3691 * variable-free. (Hence, it should usually match the join domain in which
3692 * the clause applies.) Otherwise the qual is applied at the lowest join
3693 * level that provides all its variables.
3694 *
3695 * "security_level" is the security level to assign to the new restrictinfo.
3696 *
3697 * "both_const" indicates whether both items are known pseudo-constant;
3698 * in this case it is worth applying eval_const_expressions() in case we
3699 * can produce constant TRUE or constant FALSE. (Otherwise it's not,
3700 * because the expressions went through eval_const_expressions already.)
3701 *
3702 * Returns the generated RestrictInfo, if any. The result will be NULL
3703 * if both_const is true and we successfully reduced the clause to
3704 * constant TRUE.
3705 *
3706 * Note: this function will copy item1 and item2, but it is caller's
3707 * responsibility to make sure that the Relids parameters are fresh copies
3708 * not shared with other uses.
3709 *
3710 * Note: we do not do initialize_mergeclause_eclasses() here. It is
3711 * caller's responsibility that left_ec/right_ec be set as necessary.
3712 */
3715 Oid opno,
3716 Oid collation,
3717 Expr *item1,
3718 Expr *item2,
3719 Relids qualscope,
3720 Index security_level,
3721 bool both_const)
3722{
3724 Node *clause;
3725 Relids relids;
3726 bool pseudoconstant = false;
3727
3728 /*
3729 * Build the new clause. Copy to ensure it shares no substructure with
3730 * original (this is necessary in case there are subselects in there...)
3731 */
3732 clause = (Node *) make_opclause(opno,
3733 BOOLOID, /* opresulttype */
3734 false, /* opretset */
3737 InvalidOid,
3738 collation);
3739
3740 /* If both constant, try to reduce to a boolean constant. */
3741 if (both_const)
3742 {
3743 clause = eval_const_expressions(root, clause);
3744
3745 /* If we produced const TRUE, just drop the clause */
3746 if (clause && IsA(clause, Const))
3747 {
3748 Const *cclause = (Const *) clause;
3749
3750 Assert(cclause->consttype == BOOLOID);
3751 if (!cclause->constisnull && DatumGetBool(cclause->constvalue))
3752 return NULL;
3753 }
3754 }
3755
3756 /*
3757 * The rest of this is a very cut-down version of distribute_qual_to_rels.
3758 * We can skip most of the work therein, but there are a couple of special
3759 * cases we still have to handle.
3760 *
3761 * Retrieve all relids mentioned within the possibly-simplified clause.
3762 */
3763 relids = pull_varnos(root, clause);
3764 Assert(bms_is_subset(relids, qualscope));
3765
3766 /*
3767 * If the clause is variable-free, our normal heuristic for pushing it
3768 * down to just the mentioned rels doesn't work, because there are none.
3769 * Apply it as a gating qual at the appropriate level (see comments for
3770 * get_join_domain_min_rels).
3771 */
3772 if (bms_is_empty(relids))
3773 {
3774 /* eval at join domain's safe level */
3775 relids = get_join_domain_min_rels(root, qualscope);
3776 /* mark as gating qual */
3777 pseudoconstant = true;
3778 /* tell createplan.c to check for gating quals */
3779 root->hasPseudoConstantQuals = true;
3780 }
3781
3782 /*
3783 * Build the RestrictInfo node itself.
3784 */
3786 (Expr *) clause,
3787 true, /* is_pushed_down */
3788 false, /* !has_clone */
3789 false, /* !is_clone */
3791 security_level,
3792 relids,
3793 NULL, /* incompatible_relids */
3794 NULL); /* outer_relids */
3795
3796 /*
3797 * If it's a join clause, add vars used in the clause to targetlists of
3798 * their relations, so that they will be emitted by the plan nodes that
3799 * scan those relations (else they won't be available at the join node!).
3800 *
3801 * Typically, we'd have already done this when the component expressions
3802 * were first seen by distribute_qual_to_rels; but it is possible that
3803 * some of the Vars could have missed having that done because they only
3804 * appeared in single-relation clauses originally. So do it here for
3805 * safety.
3806 *
3807 * See also rebuild_joinclause_attr_needed, which has to partially repeat
3808 * this work after removal of an outer join. (Since we will put this
3809 * clause into the joininfo lists, that function needn't do any extra work
3810 * to find it.)
3811 */
3812 if (bms_membership(relids) == BMS_MULTIPLE)
3813 {
3814 List *vars = pull_var_clause(clause,
3818
3820 list_free(vars);
3821 }
3822
3823 /*
3824 * Check mergejoinability. This will usually succeed, since the op came
3825 * from an EquivalenceClass; but we could have reduced the original clause
3826 * to a constant.
3827 */
3829
3830 /*
3831 * Note we don't do initialize_mergeclause_eclasses(); the caller can
3832 * handle that much more cheaply than we can. It's okay to call
3833 * distribute_restrictinfo_to_rels() before that happens.
3834 */
3835
3836 /*
3837 * Push the new clause into all the appropriate restrictinfo lists.
3838 */
3840
3841 return restrictinfo;
3842}
3843
3844/*
3845 * build_implied_join_equality --- build a RestrictInfo for a derived equality
3846 *
3847 * This overlaps the functionality of process_implied_equality(), but we
3848 * must not push the RestrictInfo into the joininfo tree.
3849 *
3850 * Note: this function will copy item1 and item2, but it is caller's
3851 * responsibility to make sure that the Relids parameters are fresh copies
3852 * not shared with other uses.
3853 *
3854 * Note: we do not do initialize_mergeclause_eclasses() here. It is
3855 * caller's responsibility that left_ec/right_ec be set as necessary.
3856 */
3859 Oid opno,
3860 Oid collation,
3861 Expr *item1,
3862 Expr *item2,
3863 Relids qualscope,
3864 Index security_level)
3865{
3867 Expr *clause;
3868
3869 /*
3870 * Build the new clause. Copy to ensure it shares no substructure with
3871 * original (this is necessary in case there are subselects in there...)
3872 */
3873 clause = make_opclause(opno,
3874 BOOLOID, /* opresulttype */
3875 false, /* opretset */
3878 InvalidOid,
3879 collation);
3880
3881 /*
3882 * Build the RestrictInfo node itself.
3883 */
3885 clause,
3886 true, /* is_pushed_down */
3887 false, /* !has_clone */
3888 false, /* !is_clone */
3889 false, /* pseudoconstant */
3890 security_level, /* security_level */
3891 qualscope, /* required_relids */
3892 NULL, /* incompatible_relids */
3893 NULL); /* outer_relids */
3894
3895 /* Set mergejoinability/hashjoinability flags */
3899
3900 return restrictinfo;
3901}
3902
3903/*
3904 * get_join_domain_min_rels
3905 * Identify the appropriate join level for derived quals belonging
3906 * to the join domain with the given relids.
3907 *
3908 * When we derive a pseudoconstant (Var-free) clause from an EquivalenceClass,
3909 * we'd ideally apply the clause at the top level of the EC's join domain.
3910 * However, if there are any outer joins inside that domain that get commuted
3911 * with joins outside it, that leads to not finding a correct place to apply
3912 * the clause. Instead, remove any lower outer joins from the relid set,
3913 * and apply the clause to just the remaining rels. This still results in a
3914 * correct answer, since if the clause produces FALSE then the LHS of these
3915 * joins will be empty leading to an empty join result.
3916 *
3917 * However, there's no need to remove outer joins if this is the top-level
3918 * join domain of the query, since then there's nothing else to commute with.
3919 *
3920 * Note: it's tempting to use this in distribute_qual_to_rels where it's
3921 * dealing with pseudoconstant quals; but we can't because the necessary
3922 * SpecialJoinInfos aren't all formed at that point.
3923 *
3924 * The result is always freshly palloc'd; we do not modify domain_relids.
3925 */
3926static Relids
3928{
3930 ListCell *lc;
3931
3932 /* Top-level join domain? */
3933 if (bms_equal(result, root->all_query_rels))
3934 return result;
3935
3936 /* Nope, look for lower outer joins that could potentially commute out */
3937 foreach(lc, root->join_info_list)
3938 {
3940
3941 if (sjinfo->jointype == JOIN_LEFT &&
3942 bms_is_member(sjinfo->ojrelid, result))
3943 {
3944 result = bms_del_member(result, sjinfo->ojrelid);
3946 }
3947 }
3948 return result;
3949}
3950
3951
3952/*
3953 * rebuild_joinclause_attr_needed
3954 * Put back attr_needed bits for Vars/PHVs needed for join clauses.
3955 *
3956 * This is used to rebuild attr_needed/ph_needed sets after removal of a
3957 * useless outer join. It should match what distribute_qual_to_rels did,
3958 * except that we call add_vars_to_attr_needed not add_vars_to_targetlist.
3959 */
3960void
3962{
3963 /*
3964 * We must examine all join clauses, but there's no value in processing
3965 * any join clause more than once. So it's slightly annoying that we have
3966 * to find them via the per-base-relation joininfo lists. Avoid duplicate
3967 * processing by tracking the rinfo_serial numbers of join clauses we've
3968 * already seen. (This doesn't work for is_clone clauses, so we must
3969 * waste effort on them.)
3970 */
3972 Index rti;
3973
3974 /* Scan all baserels for join clauses */
3975 for (rti = 1; rti < root->simple_rel_array_size; rti++)
3976 {
3977 RelOptInfo *brel = root->simple_rel_array[rti];
3978 ListCell *lc;
3979
3980 if (brel == NULL)
3981 continue;
3982 if (brel->reloptkind != RELOPT_BASEREL)
3983 continue;
3984
3985 foreach(lc, brel->joininfo)
3986 {
3987 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3988 Relids relids = rinfo->required_relids;
3989
3990 if (!rinfo->is_clone) /* else serial number is not unique */
3991 {
3993 continue; /* saw it already */
3995 rinfo->rinfo_serial);
3996 }
3997
3998 if (bms_membership(relids) == BMS_MULTIPLE)
3999 {
4000 List *vars = pull_var_clause((Node *) rinfo->clause,
4005
4006 if (rinfo->is_clone)
4007 where_needed = bms_intersect(relids, root->all_baserels);
4008 else
4009 where_needed = relids;
4011 list_free(vars);
4012 }
4013 }
4014 }
4015}
4016
4017
4018/*
4019 * match_foreign_keys_to_quals
4020 * Match foreign-key constraints to equivalence classes and join quals
4021 *
4022 * The idea here is to see which query join conditions match equality
4023 * constraints of a foreign-key relationship. For such join conditions,
4024 * we can use the FK semantics to make selectivity estimates that are more
4025 * reliable than estimating from statistics, especially for multiple-column
4026 * FKs, where the normal assumption of independent conditions tends to fail.
4027 *
4028 * In this function we annotate the ForeignKeyOptInfos in root->fkey_list
4029 * with info about which eclasses and join qual clauses they match, and
4030 * discard any ForeignKeyOptInfos that are irrelevant for the query.
4031 */
4032void
4034{
4035 List *newlist = NIL;
4036 ListCell *lc;
4037
4038 foreach(lc, root->fkey_list)
4039 {
4043 int colno;
4044
4045 /*
4046 * Either relid might identify a rel that is in the query's rtable but
4047 * isn't referenced by the jointree, or has been removed by join
4048 * removal, so that it won't have a RelOptInfo. Hence don't use
4049 * find_base_rel() here. We can ignore such FKs.
4050 */
4051 if (fkinfo->con_relid >= root->simple_rel_array_size ||
4052 fkinfo->ref_relid >= root->simple_rel_array_size)
4053 continue; /* just paranoia */
4054 con_rel = root->simple_rel_array[fkinfo->con_relid];
4055 if (con_rel == NULL)
4056 continue;
4057 ref_rel = root->simple_rel_array[fkinfo->ref_relid];
4058 if (ref_rel == NULL)
4059 continue;
4060
4061 /*
4062 * Ignore FK unless both rels are baserels. This gets rid of FKs that
4063 * link to inheritance child rels (otherrels).
4064 */
4065 if (con_rel->reloptkind != RELOPT_BASEREL ||
4066 ref_rel->reloptkind != RELOPT_BASEREL)
4067 continue;
4068
4069 /*
4070 * Scan the columns and try to match them to eclasses and quals.
4071 *
4072 * Note: for simple inner joins, any match should be in an eclass.
4073 * "Loose" quals that syntactically match an FK equality must have
4074 * been rejected for EC status because they are outer-join quals or
4075 * similar. We can still consider them to match the FK.
4076 */
4077 for (colno = 0; colno < fkinfo->nkeys; colno++)
4078 {
4079 EquivalenceClass *ec;
4081 ref_attno;
4082 Oid fpeqop;
4083 ListCell *lc2;
4084
4086 /* Don't bother looking for loose quals if we got an EC match */
4087 if (ec != NULL)
4088 {
4089 fkinfo->nmatched_ec++;
4090 if (ec->ec_has_const)
4091 fkinfo->nconst_ec++;
4092 continue;
4093 }
4094
4095 /*
4096 * Scan joininfo list for relevant clauses. Either rel's joininfo
4097 * list would do equally well; we use con_rel's.
4098 */
4099 con_attno = fkinfo->conkey[colno];
4100 ref_attno = fkinfo->confkey[colno];
4101 fpeqop = InvalidOid; /* we'll look this up only if needed */
4102
4103 foreach(lc2, con_rel->joininfo)
4104 {
4105 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc2);
4106 OpExpr *clause = (OpExpr *) rinfo->clause;
4107 Var *leftvar;
4108 Var *rightvar;
4109
4110 /* Only binary OpExprs are useful for consideration */
4111 if (!IsA(clause, OpExpr) ||
4112 list_length(clause->args) != 2)
4113 continue;
4114 leftvar = (Var *) get_leftop((Expr *) clause);
4115 rightvar = (Var *) get_rightop((Expr *) clause);
4116
4117 /* Operands must be Vars, possibly with RelabelType */
4118 while (leftvar && IsA(leftvar, RelabelType))
4119 leftvar = (Var *) ((RelabelType *) leftvar)->arg;
4120 if (!(leftvar && IsA(leftvar, Var)))
4121 continue;
4122 while (rightvar && IsA(rightvar, RelabelType))
4123 rightvar = (Var *) ((RelabelType *) rightvar)->arg;
4124 if (!(rightvar && IsA(rightvar, Var)))
4125 continue;
4126
4127 /* Now try to match the vars to the current foreign key cols */
4128 if (fkinfo->ref_relid == leftvar->varno &&
4129 ref_attno == leftvar->varattno &&
4130 fkinfo->con_relid == rightvar->varno &&
4131 con_attno == rightvar->varattno)
4132 {
4133 /* Vars match, but is it the right operator? */
4134 if (clause->opno == fkinfo->conpfeqop[colno])
4135 {
4136 fkinfo->rinfos[colno] = lappend(fkinfo->rinfos[colno],
4137 rinfo);
4138 fkinfo->nmatched_ri++;
4139 }
4140 }
4141 else if (fkinfo->ref_relid == rightvar->varno &&
4142 ref_attno == rightvar->varattno &&
4143 fkinfo->con_relid == leftvar->varno &&
4144 con_attno == leftvar->varattno)
4145 {
4146 /*
4147 * Reverse match, must check commutator operator. Look it
4148 * up if we didn't already. (In the worst case we might
4149 * do multiple lookups here, but that would require an FK
4150 * equality operator without commutator, which is
4151 * unlikely.)
4152 */
4153 if (!OidIsValid(fpeqop))
4154 fpeqop = get_commutator(fkinfo->conpfeqop[colno]);
4155 if (clause->opno == fpeqop)
4156 {
4157 fkinfo->rinfos[colno] = lappend(fkinfo->rinfos[colno],
4158 rinfo);
4159 fkinfo->nmatched_ri++;
4160 }
4161 }
4162 }
4163 /* If we found any matching loose quals, count col as matched */
4164 if (fkinfo->rinfos[colno])
4165 fkinfo->nmatched_rcols++;
4166 }
4167
4168 /*
4169 * Currently, we drop multicolumn FKs that aren't fully matched to the
4170 * query. Later we might figure out how to derive some sort of
4171 * estimate from them, in which case this test should be weakened to
4172 * "if ((fkinfo->nmatched_ec + fkinfo->nmatched_rcols) > 0)".
4173 */
4174 if ((fkinfo->nmatched_ec + fkinfo->nmatched_rcols) == fkinfo->nkeys)
4176 }
4177 /* Replace fkey_list, thereby discarding any useless entries */
4178 root->fkey_list = newlist;
4179}
4180
4181
4182/*****************************************************************************
4183 *
4184 * CHECKS FOR MERGEJOINABLE AND HASHJOINABLE CLAUSES
4185 *
4186 *****************************************************************************/
4187
4188/*
4189 * check_mergejoinable
4190 * If the restrictinfo's clause is mergejoinable, set the mergejoin
4191 * info fields in the restrictinfo.
4192 *
4193 * Currently, we support mergejoin for binary opclauses where
4194 * the operator is a mergejoinable operator. The arguments can be
4195 * anything --- as long as there are no volatile functions in them.
4196 */
4197static void
4199{
4200 Expr *clause = restrictinfo->clause;
4201 Oid opno;
4202 Node *leftarg;
4203
4204 if (restrictinfo->pseudoconstant)
4205 return;
4206 if (!is_opclause(clause))
4207 return;
4208 if (list_length(((OpExpr *) clause)->args) != 2)
4209 return;
4210
4211 opno = ((OpExpr *) clause)->opno;
4212 leftarg = linitial(((OpExpr *) clause)->args);
4213
4214 if (op_mergejoinable(opno, exprType(leftarg)) &&
4216 restrictinfo->mergeopfamilies = get_mergejoin_opfamilies(opno);
4217
4218 /*
4219 * Note: op_mergejoinable is just a hint; if we fail to find the operator
4220 * in any btree opfamilies, mergeopfamilies remains NIL and so the clause
4221 * is not treated as mergejoinable.
4222 */
4223}
4224
4225/*
4226 * check_hashjoinable
4227 * If the restrictinfo's clause is hashjoinable, set the hashjoin
4228 * info fields in the restrictinfo.
4229 *
4230 * Currently, we support hashjoin for binary opclauses where
4231 * the operator is a hashjoinable operator. The arguments can be
4232 * anything --- as long as there are no volatile functions in them.
4233 */
4234static void
4236{
4237 Expr *clause = restrictinfo->clause;
4238 Oid opno;
4239 Node *leftarg;
4240
4241 if (restrictinfo->pseudoconstant)
4242 return;
4243 if (!is_opclause(clause))
4244 return;
4245 if (list_length(((OpExpr *) clause)->args) != 2)
4246 return;
4247
4248 opno = ((OpExpr *) clause)->opno;
4249 leftarg = linitial(((OpExpr *) clause)->args);
4250
4251 if (op_hashjoinable(opno, exprType(leftarg)) &&
4253 restrictinfo->hashjoinoperator = opno;
4254}
4255
4256/*
4257 * check_memoizable
4258 * If the restrictinfo's clause is suitable to be used for a Memoize node,
4259 * set the left_hasheqoperator and right_hasheqoperator to the hash equality
4260 * operator that will be needed during caching.
4261 */
4262static void
4264{
4265 TypeCacheEntry *typentry;
4266 Expr *clause = restrictinfo->clause;
4267 Oid lefttype;
4268 Oid righttype;
4269
4270 if (restrictinfo->pseudoconstant)
4271 return;
4272 if (!is_opclause(clause))
4273 return;
4274 if (list_length(((OpExpr *) clause)->args) != 2)
4275 return;
4276
4277 lefttype = exprType(linitial(((OpExpr *) clause)->args));
4278
4279 typentry = lookup_type_cache(lefttype, TYPECACHE_HASH_PROC |
4281
4282 if (OidIsValid(typentry->hash_proc) && OidIsValid(typentry->eq_opr))
4283 restrictinfo->left_hasheqoperator = typentry->eq_opr;
4284
4285 righttype = exprType(lsecond(((OpExpr *) clause)->args));
4286
4287 /*
4288 * Lookup the right type, unless it's the same as the left type, in which
4289 * case typentry is already pointing to the required TypeCacheEntry.
4290 */
4291 if (lefttype != righttype)
4292 typentry = lookup_type_cache(righttype, TYPECACHE_HASH_PROC |
4294
4295 if (OidIsValid(typentry->hash_proc) && OidIsValid(typentry->eq_opr))
4296 restrictinfo->right_hasheqoperator = typentry->eq_opr;
4297}
bool enable_eager_aggregate
Definition allpaths.c:82
int16 AttrNumber
Definition attnum.h:21
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:346
Bitmapset * bms_make_singleton(int x)
Definition bitmapset.c:216
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:1093
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:292
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:142
BMS_Comparison bms_subset_compare(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:445
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:1145
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition bitmapset.c:852
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:412
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:901
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:251
BMS_Membership bms_membership(const Bitmapset *a)
Definition bitmapset.c:765
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:575
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition bitmapset.c:708
Bitmapset * bms_copy(const Bitmapset *a)
Definition bitmapset.c:122
#define bms_is_empty(a)
Definition bitmapset.h:118
@ BMS_SUBSET1
Definition bitmapset.h:63
@ BMS_SINGLETON
Definition bitmapset.h:72
@ BMS_MULTIPLE
Definition bitmapset.h:73
#define PG_INT32_MAX
Definition c.h:673
#define Min(x, y)
Definition c.h:1091
#define Assert(condition)
Definition c.h:943
int32_t int32
Definition c.h:620
unsigned int Index
Definition c.h:698
#define OidIsValid(objectId)
Definition c.h:858
uint32 result
Var * find_forced_null_var(Node *node)
Definition clauses.c:1997
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition clauses.c:2500
bool expr_is_nonnullable(PlannerInfo *root, Expr *expr, NotNullSource source)
Definition clauses.c:4785
Relids find_nonnullable_rels(Node *clause)
Definition clauses.c:1476
bool contain_volatile_functions(Node *clause)
Definition clauses.c:551
CompareType
Definition cmptype.h:32
@ COMPARE_EQ
Definition cmptype.h:36
bool enable_hashagg
Definition costsize.c:153
int errcode(int sqlerrcode)
Definition elog.c:875
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
EquivalenceClass * get_eclass_for_sort_expr(PlannerInfo *root, Expr *expr, List *opfamilies, Oid opcintype, Oid collation, Index sortref, Relids rel, bool create_it)
Definition equivclass.c:736
bool process_equivalence(PlannerInfo *root, RestrictInfo **p_restrictinfo, JoinDomain *jdomain)
Definition equivclass.c:179
EquivalenceClass * match_eclasses_to_foreign_key_col(PlannerInfo *root, ForeignKeyOptInfo *fkinfo, int colno)
#define palloc0_array(type, count)
Definition fe_memutils.h:77
#define palloc0_object(type)
Definition fe_memutils.h:75
Datum OidFunctionCall1Coll(Oid functionId, Oid collation, Datum arg1)
Definition fmgr.c:1413
void parse(int)
Definition parse.c:49
int remaining
Definition informix.c:692
void expand_inherited_rtentry(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte, Index rti)
Definition inherit.c:88
int join_collapse_limit
Definition initsplan.c:42
static void check_hashjoinable(RestrictInfo *restrictinfo)
Definition initsplan.c:4235
static void create_grouping_expr_infos(PlannerInfo *root)
Definition initsplan.c:931
static void extract_lateral_references(PlannerInfo *root, RelOptInfo *brel, Index rtindex)
Definition initsplan.c:1144
void rebuild_lateral_attr_needed(PlannerInfo *root)
Definition initsplan.c:1245
static void check_mergejoinable(RestrictInfo *restrictinfo)
Definition initsplan.c:4198
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition initsplan.c:3629
static List * deconstruct_recurse(PlannerInfo *root, Node *jtnode, JoinDomain *parent_domain, JoinTreeItem *parent_jtitem, List **item_list)
Definition initsplan.c:1604
static void compute_semijoin_info(PlannerInfo *root, SpecialJoinInfo *sjinfo, List *clause)
Definition initsplan.c:2486
void match_foreign_keys_to_quals(PlannerInfo *root)
Definition initsplan.c:4033
void find_lateral_references(PlannerInfo *root)
Definition initsplan.c:1096
void remove_useless_groupby_columns(PlannerInfo *root)
Definition initsplan.c:432
static void add_base_clause_to_rel(PlannerInfo *root, Index relid, RestrictInfo *restrictinfo)
Definition initsplan.c:3418
void add_base_rels_to_query(PlannerInfo *root, Node *jtnode)
Definition initsplan.c:178
void build_base_rel_tlists(PlannerInfo *root, List *final_tlist)
Definition initsplan.c:255
bool restriction_is_always_true(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition initsplan.c:3493
RestrictInfo * build_implied_join_equality(PlannerInfo *root, Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Index security_level)
Definition initsplan.c:3858
int from_collapse_limit
Definition initsplan.c:41
static void deconstruct_distribute_oj_quals(PlannerInfo *root, List *jtitems, JoinTreeItem *jtitem)
Definition initsplan.c:2664
static void distribute_quals_to_rels(PlannerInfo *root, List *clauses, JoinTreeItem *jtitem, SpecialJoinInfo *sjinfo, Index security_level, Relids qualscope, Relids ojscope, Relids outerjoin_nonnullable, Relids incompatible_relids, bool allow_equivalence, bool has_clone, bool is_clone, List **postponed_oj_qual_list)
Definition initsplan.c:2905
static SpecialJoinInfo * make_outerjoininfo(PlannerInfo *root, Relids left_rels, Relids right_rels, Relids inner_join_rels, JoinType jointype, Index ojrelid, List *clause)
Definition initsplan.c:2146
void add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed)
Definition initsplan.c:302
bool restriction_is_always_false(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition initsplan.c:3558
static void distribute_qual_to_rels(PlannerInfo *root, Node *clause, JoinTreeItem *jtitem, SpecialJoinInfo *sjinfo, Index security_level, Relids qualscope, Relids ojscope, Relids outerjoin_nonnullable, Relids incompatible_relids, bool allow_equivalence, bool has_clone, bool is_clone, List **postponed_oj_qual_list)
Definition initsplan.c:2983
List * deconstruct_jointree(PlannerInfo *root)
Definition initsplan.c:1522
static bool check_redundant_nullability_qual(PlannerInfo *root, Node *clause)
Definition initsplan.c:3373
void rebuild_joinclause_attr_needed(PlannerInfo *root)
Definition initsplan.c:3961
void setup_eager_aggregation(PlannerInfo *root)
Definition initsplan.c:694
static EquivalenceClass * get_eclass_for_sortgroupclause(PlannerInfo *root, SortGroupClause *sgc, Expr *expr)
Definition initsplan.c:1022
void add_other_rels_to_query(PlannerInfo *root)
Definition initsplan.c:216
static void mark_rels_nulled_by_join(PlannerInfo *root, Index ojrelid, Relids lower_rels)
Definition initsplan.c:2104
RestrictInfo * process_implied_equality(PlannerInfo *root, Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Index security_level, bool both_const)
Definition initsplan.c:3714
void create_lateral_join_info(PlannerInfo *root)
Definition initsplan.c:1283
static void process_security_barrier_quals(PlannerInfo *root, int rti, JoinTreeItem *jtitem)
Definition initsplan.c:2054
static bool is_partial_agg_memory_risky(PlannerInfo *root)
Definition initsplan.c:781
static void create_agg_clause_infos(PlannerInfo *root)
Definition initsplan.c:802
static void deconstruct_distribute(PlannerInfo *root, JoinTreeItem *jtitem)
Definition initsplan.c:1902
void add_vars_to_attr_needed(PlannerInfo *root, List *vars, Relids where_needed)
Definition initsplan.c:373
static void check_memoizable(RestrictInfo *restrictinfo)
Definition initsplan.c:4263
static Relids get_join_domain_min_rels(PlannerInfo *root, Relids domain_relids)
Definition initsplan.c:3927
int j
Definition isn.c:78
int i
Definition isn.c:77
void add_join_clause_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo, Relids join_relids)
Definition joininfo.c:98
List * lappend(List *list, void *datum)
Definition list.c:339
List * list_append_unique(List *list, void *datum)
Definition list.c:1343
List * list_concat(List *list1, const List *list2)
Definition list.c:561
List * lappend_int(List *list, int datum)
Definition list.c:357
List * lappend_oid(List *list, Oid datum)
Definition list.c:375
void list_free(List *list)
Definition list.c:1546
bool list_member_oid(const List *list, Oid datum)
Definition list.c:722
void list_free_deep(List *list)
Definition list.c:1560
bool collations_agree_on_equality(Oid coll1, Oid coll2)
Definition lsyscache.c:886
bool get_ordering_op_properties(Oid opno, Oid *opfamily, Oid *opcintype, CompareType *cmptype)
Definition lsyscache.c:261
Oid get_opfamily_member_for_cmptype(Oid opfamily, Oid lefttype, Oid righttype, CompareType cmptype)
Definition lsyscache.c:199
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition lsyscache.c:953
bool op_hashjoinable(Oid opno, Oid inputtype)
Definition lsyscache.c:1668
bool get_func_leakproof(Oid funcid)
Definition lsyscache.c:2068
bool op_mergejoinable(Oid opno, Oid inputtype)
Definition lsyscache.c:1617
List * get_mergejoin_opfamilies(Oid opno)
Definition lsyscache.c:430
Oid get_commutator(Oid opno)
Definition lsyscache.c:1740
Node * makeBoolConst(bool value, bool isnull)
Definition makefuncs.c:408
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition makefuncs.c:701
void * palloc(Size size)
Definition mcxt.c:1387
#define BTEQUALIMAGE_PROC
Definition nbtree.h:720
Oid exprType(const Node *expr)
Definition nodeFuncs.c:42
Oid exprCollation(const Node *expr)
Definition nodeFuncs.c:826
static bool is_orclause(const void *clause)
Definition nodeFuncs.h:116
static Node * get_rightop(const void *clause)
Definition nodeFuncs.h:95
static bool is_opclause(const void *clause)
Definition nodeFuncs.h:76
static Node * get_leftop(const void *clause)
Definition nodeFuncs.h:83
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define copyObject(obj)
Definition nodes.h:232
#define nodeTag(nodeptr)
Definition nodes.h:139
#define makeNode(_type_)
Definition nodes.h:161
#define castNode(_type_, nodeptr)
Definition nodes.h:182
JoinType
Definition nodes.h:298
@ JOIN_SEMI
Definition nodes.h:317
@ JOIN_FULL
Definition nodes.h:305
@ JOIN_INNER
Definition nodes.h:303
@ JOIN_RIGHT
Definition nodes.h:306
@ JOIN_LEFT
Definition nodes.h:304
@ JOIN_ANTI
Definition nodes.h:318
static char * errmsg
#define PVC_RECURSE_AGGREGATES
Definition optimizer.h:198
@ NOTNULL_SOURCE_RELOPT
Definition optimizer.h:136
#define PVC_RECURSE_PLACEHOLDERS
Definition optimizer.h:202
#define PVC_RECURSE_WINDOWFUNCS
Definition optimizer.h:200
#define PVC_INCLUDE_PLACEHOLDERS
Definition optimizer.h:201
#define PVC_INCLUDE_AGGREGATES
Definition optimizer.h:197
@ RTE_VALUES
@ RTE_SUBQUERY
@ RTE_FUNCTION
@ RTE_TABLEFUNC
@ RTE_RELATION
const char * LCS_asString(LockClauseStrength strength)
Definition analyze.c:3720
void initialize_mergeclause_eclasses(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition pathkeys.c:1463
@ RELOPT_BASEREL
Definition pathnodes.h:977
#define lfirst(lc)
Definition pg_list.h:172
#define llast(l)
Definition pg_list.h:198
#define lfirst_node(type, lc)
Definition pg_list.h:176
static int list_length(const List *l)
Definition pg_list.h:152
#define linitial_node(type, l)
Definition pg_list.h:181
#define NIL
Definition pg_list.h:68
#define lfirst_int(lc)
Definition pg_list.h:173
#define list_make1(x1)
Definition pg_list.h:244
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition pg_list.h:595
#define linitial(l)
Definition pg_list.h:178
#define lsecond(l)
Definition pg_list.h:183
#define foreach_node(type, var, lst)
Definition pg_list.h:528
#define list_make2(x1, x2)
Definition pg_list.h:246
bool contain_placeholder_references_to(PlannerInfo *root, Node *clause, int relid)
PlaceHolderInfo * find_placeholder_info(PlannerInfo *root, PlaceHolderVar *phv)
Definition placeholder.c:85
Expr * preprocess_phv_expression(PlannerInfo *root, Expr *expr)
Definition planner.c:1742
static bool DatumGetBool(Datum X)
Definition postgres.h:100
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
#define InvalidOid
unsigned int Oid
static int fb(int x)
@ IS_NULL
Definition primnodes.h:1992
@ IS_NOT_NULL
Definition primnodes.h:1992
tree ctl root
Definition radixtree.h:1857
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition relnode.c:544
RelOptInfo * build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
Definition relnode.c:212
RelOptInfo * find_base_rel_ignore_join(PlannerInfo *root, int relid)
Definition relnode.c:584
bool restriction_is_or_clause(RestrictInfo *restrictinfo)
RestrictInfo * make_restrictinfo(PlannerInfo *root, Expr *clause, bool is_pushed_down, bool has_clone, bool is_clone, bool pseudoconstant, Index security_level, Relids required_relids, Relids incompatible_relids, Relids outer_relids)
Node * add_nulling_relids(Node *node, const Bitmapset *target_relids, const Bitmapset *added_relids)
Node * remove_nulling_relids(Node *node, const Bitmapset *removable_relids, const Bitmapset *except_relids)
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Oid aggfnoid
Definition primnodes.h:464
List * aggdistinct
Definition primnodes.h:494
List * aggorder
Definition primnodes.h:491
Node * quals
Definition primnodes.h:2397
List * fromlist
Definition primnodes.h:2396
AttrNumber attno
Definition initsplan.c:92
List * eq_opfamilies
Definition initsplan.c:93
struct JoinTreeItem * jti_parent
Definition initsplan.c:67
SpecialJoinInfo * sjinfo
Definition initsplan.c:79
Relids inner_join_rels
Definition initsplan.c:71
Relids left_rels
Definition initsplan.c:74
Relids right_rels
Definition initsplan.c:75
Relids nonnullable_rels
Definition initsplan.c:76
Node * jtnode
Definition initsplan.c:65
JoinDomain * jdomain
Definition initsplan.c:66
List * oj_joinclauses
Definition initsplan.c:80
Relids qualscope
Definition initsplan.c:69
List * lateral_clauses
Definition initsplan.c:81
Definition pg_list.h:54
Definition nodes.h:135
Oid opno
Definition primnodes.h:851
List * args
Definition primnodes.h:869
List * exprs
Definition pathnodes.h:1878
List * baserestrictinfo
Definition pathnodes.h:1142
Bitmapset * notnullattnums
Definition pathnodes.h:1083
Relids relids
Definition pathnodes.h:1021
struct PathTarget * reltarget
Definition pathnodes.h:1045
Relids lateral_relids
Definition pathnodes.h:1064
RelOptKind reloptkind
Definition pathnodes.h:1015
List * indexlist
Definition pathnodes.h:1091
Relids nulling_relids
Definition pathnodes.h:1085
Index baserestrict_min_security
Definition pathnodes.h:1146
AttrNumber min_attr
Definition pathnodes.h:1075
Relids required_relids
Definition pathnodes.h:2932
Expr * clause
Definition pathnodes.h:2901
LockClauseStrength strength
Relids commute_above_r
Definition pathnodes.h:3233
Relids syn_lefthand
Definition pathnodes.h:3228
Relids min_righthand
Definition pathnodes.h:3227
List * semi_rhs_exprs
Definition pathnodes.h:3241
Relids commute_above_l
Definition pathnodes.h:3232
JoinType jointype
Definition pathnodes.h:3230
Relids commute_below_l
Definition pathnodes.h:3234
Relids min_lefthand
Definition pathnodes.h:3226
Relids syn_righthand
Definition pathnodes.h:3229
Relids commute_below_r
Definition pathnodes.h:3235
List * semi_operators
Definition pathnodes.h:3240
AttrNumber varattno
Definition primnodes.h:275
int varno
Definition primnodes.h:270
Index varlevelsup
Definition primnodes.h:295
Definition type.h:96
#define FirstLowInvalidHeapAttributeNumber
Definition sysattr.h:27
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition tlist.c:376
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition typcache.c:389
#define TYPECACHE_EQ_OPR
Definition typcache.h:138
#define TYPECACHE_BTREE_OPFAMILY
Definition typcache.h:147
#define TYPECACHE_HASH_PROC
Definition typcache.h:142
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition var.c:114
List * pull_vars_of_level(Node *node, int levelsup)
Definition var.c:339
List * pull_var_clause(Node *node, int flags)
Definition var.c:653