PostgreSQL Source Code git master
Loading...
Searching...
No Matches
createplan.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * createplan.c
4 * Routines to create the desired plan for processing a query.
5 * Planning is complete, we just need to convert the selected
6 * Path into a Plan.
7 *
8 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
10 *
11 *
12 * IDENTIFICATION
13 * src/backend/optimizer/plan/createplan.c
14 *
15 *-------------------------------------------------------------------------
16 */
17#include "postgres.h"
18
19#include "access/sysattr.h"
20#include "access/transam.h"
21#include "catalog/pg_class.h"
22#include "foreign/fdwapi.h"
23#include "miscadmin.h"
24#include "nodes/extensible.h"
25#include "nodes/makefuncs.h"
26#include "nodes/nodeFuncs.h"
27#include "optimizer/clauses.h"
28#include "optimizer/cost.h"
29#include "optimizer/optimizer.h"
31#include "optimizer/pathnode.h"
32#include "optimizer/paths.h"
34#include "optimizer/plancat.h"
35#include "optimizer/planmain.h"
36#include "optimizer/prep.h"
38#include "optimizer/subselect.h"
39#include "optimizer/tlist.h"
40#include "parser/parse_clause.h"
41#include "parser/parsetree.h"
43#include "tcop/tcopprot.h"
44#include "utils/lsyscache.h"
45
46
47/*
48 * Flag bits that can appear in the flags argument of create_plan_recurse().
49 * These can be OR-ed together.
50 *
51 * CP_EXACT_TLIST specifies that the generated plan node must return exactly
52 * the tlist specified by the path's pathtarget (this overrides both
53 * CP_SMALL_TLIST and CP_LABEL_TLIST, if those are set). Otherwise, the
54 * plan node is allowed to return just the Vars and PlaceHolderVars needed
55 * to evaluate the pathtarget.
56 *
57 * CP_SMALL_TLIST specifies that a narrower tlist is preferred. This is
58 * passed down by parent nodes such as Sort and Hash, which will have to
59 * store the returned tuples.
60 *
61 * CP_LABEL_TLIST specifies that the plan node must return columns matching
62 * any sortgrouprefs specified in its pathtarget, with appropriate
63 * ressortgroupref labels. This is passed down by parent nodes such as Sort
64 * and Group, which need these values to be available in their inputs.
65 *
66 * CP_IGNORE_TLIST specifies that the caller plans to replace the targetlist,
67 * and therefore it doesn't matter a bit what target list gets generated.
68 */
69#define CP_EXACT_TLIST 0x0001 /* Plan must return specified tlist */
70#define CP_SMALL_TLIST 0x0002 /* Prefer narrower tlists */
71#define CP_LABEL_TLIST 0x0004 /* tlist must contain sortgrouprefs */
72#define CP_IGNORE_TLIST 0x0008 /* caller will replace tlist */
73
74
76 int flags);
78 int flags);
80static bool use_physical_tlist(PlannerInfo *root, Path *path, int flags);
81static List *get_gating_quals(PlannerInfo *root, List *quals);
85static bool mark_async_capable_plan(Plan *plan, Path *path);
87 int flags);
89 int flags);
94 int flags);
96 int flags);
100 int flags);
101static Plan *inject_projection_plan(Plan *subplan, List *tlist,
102 bool parallel_safe);
105 IncrementalSortPath *best_path, int flags);
113 int flags);
116 int flags);
119 int flags);
121 List *tlist, List *scan_clauses);
123 List *tlist, List *scan_clauses);
125 List *tlist, List *scan_clauses, bool indexonly);
128 List *tlist, List *scan_clauses);
129static Plan *create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
130 List **qual, List **indexqual, List **indexECs);
133 List *tlist, List *scan_clauses);
136 List *tlist,
140 List *tlist, List *scan_clauses);
142 List *tlist, List *scan_clauses);
144 List *tlist, List *scan_clauses);
146 List *tlist, List *scan_clauses);
148 List *tlist, List *scan_clauses);
150 Path *best_path, List *tlist, List *scan_clauses);
152 List *tlist, List *scan_clauses);
154 List *tlist, List *scan_clauses);
156 List *tlist, List *scan_clauses);
159 List *tlist, List *scan_clauses);
170 IndexOptInfo *index, int indexcol,
171 Node *clause, List *indexcolnos);
172static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol);
173static List *get_switched_clauses(List *clauses, Relids outerrelids);
174static List *order_qual_clauses(PlannerInfo *root, List *clauses);
175static void copy_generic_path_info(Plan *dest, Path *src);
176static void copy_plan_costsize(Plan *dest, Plan *src);
178 double limit_tuples);
180 List *pathkeys, double limit_tuples);
181static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid);
184static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
185 Oid indexid, List *indexqual, List *indexqualorig,
186 List *indexorderby, List *indexorderbyorig,
187 List *indexorderbyops,
188 ScanDirection indexscandir);
190 Index scanrelid, Oid indexid,
191 List *indexqual, List *recheckqual,
192 List *indexorderby,
193 List *indextlist,
194 ScanDirection indexscandir);
195static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
196 List *indexqual,
197 List *indexqualorig);
199 List *qpqual,
200 Plan *lefttree,
201 List *bitmapqualorig,
202 Index scanrelid);
203static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
204 List *tidquals);
206 Index scanrelid, List *tidrangequals);
208 List *qpqual,
209 Index scanrelid,
210 Plan *subplan);
212 Index scanrelid, List *functions, bool funcordinality);
214 Index scanrelid, List *values_lists);
216 Index scanrelid, TableFunc *tablefunc);
218 Index scanrelid, int ctePlanId, int cteParam);
220 Index scanrelid, char *enrname);
222 Index scanrelid, int wtParam);
224 Plan *lefttree,
225 Plan *righttree,
226 int wtParam,
227 List *distinctList,
228 Cardinality numGroups);
229static BitmapAnd *make_bitmap_and(List *bitmapplans);
230static BitmapOr *make_bitmap_or(List *bitmapplans);
231static NestLoop *make_nestloop(List *tlist,
232 List *joinclauses, List *otherclauses, List *nestParams,
233 Plan *lefttree, Plan *righttree,
234 JoinType jointype,
235 Relids ojrelids,
236 bool inner_unique);
237static HashJoin *make_hashjoin(List *tlist,
238 List *joinclauses, List *otherclauses,
239 List *hashclauses,
240 List *hashoperators, List *hashcollations,
241 List *hashkeys,
242 Plan *lefttree, Plan *righttree,
243 JoinType jointype,
244 Relids ojrelids,
245 bool inner_unique);
246static Hash *make_hash(Plan *lefttree,
247 List *hashkeys,
248 Oid skewTable,
249 AttrNumber skewColumn,
250 bool skewInherit);
251static MergeJoin *make_mergejoin(List *tlist,
252 List *joinclauses, List *otherclauses,
253 List *mergeclauses,
256 bool *mergereversals,
257 bool *mergenullsfirst,
258 Plan *lefttree, Plan *righttree,
259 JoinType jointype,
260 Relids ojrelids,
261 bool inner_unique,
262 bool skip_mark_restore);
263static Sort *make_sort(Plan *lefttree, int numCols,
264 AttrNumber *sortColIdx, Oid *sortOperators,
265 Oid *collations, bool *nullsFirst);
267 int numCols, int nPresortedCols,
268 AttrNumber *sortColIdx, Oid *sortOperators,
269 Oid *collations, bool *nullsFirst);
270static Plan *prepare_sort_from_pathkeys(Plan *lefttree, List *pathkeys,
271 Relids relids,
272 const AttrNumber *reqColIdx,
274 int *p_numsortkeys,
278 bool **p_nullsFirst);
279static Sort *make_sort_from_pathkeys(Plan *lefttree, List *pathkeys,
280 Relids relids);
282 List *pathkeys, Relids relids, int nPresortedCols);
285 Plan *lefttree);
286static Material *make_material(Plan *lefttree);
287static Memoize *make_memoize(Plan *lefttree, Oid *hashoperators,
288 Oid *collations, List *param_exprs,
289 bool singlerow, bool binary_mode,
290 uint32 est_entries, Bitmapset *keyparamids,
291 Cardinality est_calls,
292 Cardinality est_unique_keys,
293 double est_hit_ratio);
294static WindowAgg *make_windowagg(List *tlist, WindowClause *wc,
297 List *runCondition, List *qual, bool topWindow,
298 Plan *lefttree);
299static Group *make_group(List *tlist, List *qual, int numGroupCols,
301 Plan *lefttree);
302static Unique *make_unique_from_pathkeys(Plan *lefttree,
303 List *pathkeys, int numCols,
304 Relids relids);
306 int nworkers, int rescan_param, bool single_copy, Plan *subplan);
307static SetOp *make_setop(SetOpCmd cmd, SetOpStrategy strategy,
308 List *tlist, Plan *lefttree, Plan *righttree,
309 List *groupList, Cardinality numGroups);
310static LockRows *make_lockrows(Plan *lefttree, List *rowMarks, int epqParam);
311static Result *make_gating_result(List *tlist, Node *resconstantqual,
312 Plan *subplan);
313static Result *make_one_row_result(List *tlist, Node *resconstantqual,
314 RelOptInfo *rel);
315static ProjectSet *make_project_set(List *tlist, Plan *subplan);
317 CmdType operation, bool canSetTag,
318 Index nominalRelation, Index rootRelation,
319 List *resultRelations,
320 List *updateColnosLists,
321 List *withCheckOptionLists, List *returningLists,
322 List *rowMarks, OnConflictExpr *onconflict,
323 List *mergeActionLists, List *mergeJoinConditions,
324 ForPortionOfExpr *forPortionOf, int epqParam);
327
328
329/*
330 * create_plan
331 * Creates the access plan for a query by recursively processing the
332 * desired tree of pathnodes, starting at the node 'best_path'. For
333 * every pathnode found, we create a corresponding plan node containing
334 * appropriate id, target list, and qualification information.
335 *
336 * The tlists and quals in the plan tree are still in planner format,
337 * ie, Vars still correspond to the parser's numbering. This will be
338 * fixed later by setrefs.c.
339 *
340 * best_path is the best access path
341 *
342 * Returns a Plan tree.
343 */
344Plan *
346{
347 Plan *plan;
348
349 /* plan_params should not be in use in current query level */
350 Assert(root->plan_params == NIL);
351
352 /* Initialize this module's workspace in PlannerInfo */
353 root->curOuterRels = NULL;
354 root->curOuterParams = NIL;
355
356 /* Recursively process the path tree, demanding the correct tlist result */
358
359 /*
360 * Make sure the topmost plan node's targetlist exposes the original
361 * column names and other decorative info. Targetlists generated within
362 * the planner don't bother with that stuff, but we must have it on the
363 * top-level tlist seen at execution time. However, ModifyTable plan
364 * nodes don't have a tlist matching the querytree targetlist.
365 */
366 if (!IsA(plan, ModifyTable))
367 apply_tlist_labeling(plan->targetlist, root->processed_tlist);
368
369 /*
370 * Attach any initPlans created in this query level to the topmost plan
371 * node. (In principle the initplans could go in any plan node at or
372 * above where they're referenced, but there seems no reason to put them
373 * any lower than the topmost node for the query level. Also, see
374 * comments for SS_finalize_plan before you try to change this.)
375 */
377
378 /* Check we successfully assigned all NestLoopParams to plan nodes */
379 if (root->curOuterParams != NIL)
380 elog(ERROR, "failed to assign all NestLoopParams to plan nodes");
381
382 /*
383 * Reset plan_params to ensure param IDs used for nestloop params are not
384 * re-used later
385 */
386 root->plan_params = NIL;
387
388 return plan;
389}
390
391/*
392 * create_plan_recurse
393 * Recursive guts of create_plan().
394 */
395static Plan *
397{
398 Plan *plan;
399
400 /* Guard against stack overflow due to overly complex plans */
402
403 switch (best_path->pathtype)
404 {
405 case T_SeqScan:
406 case T_SampleScan:
407 case T_IndexScan:
408 case T_IndexOnlyScan:
409 case T_BitmapHeapScan:
410 case T_TidScan:
411 case T_TidRangeScan:
412 case T_SubqueryScan:
413 case T_FunctionScan:
414 case T_TableFuncScan:
415 case T_ValuesScan:
416 case T_CteScan:
417 case T_WorkTableScan:
419 case T_ForeignScan:
420 case T_CustomScan:
422 break;
423 case T_HashJoin:
424 case T_MergeJoin:
425 case T_NestLoop:
427 (JoinPath *) best_path);
428 break;
429 case T_Append:
432 flags);
433 break;
434 case T_MergeAppend:
437 flags);
438 break;
439 case T_Result:
441 {
444 flags);
445 }
446 else if (IsA(best_path, MinMaxAggPath))
447 {
450 }
451 else if (IsA(best_path, GroupResultPath))
452 {
455 }
456 else
457 {
458 /* Simple RTE_RESULT base relation */
461 }
462 break;
463 case T_ProjectSet:
466 break;
467 case T_Material:
470 flags);
471 break;
472 case T_Memoize:
475 flags);
476 break;
477 case T_Unique:
480 flags);
481 break;
482 case T_Gather:
485 break;
486 case T_Sort:
489 flags);
490 break;
494 flags);
495 break;
496 case T_Group:
498 (GroupPath *) best_path);
499 break;
500 case T_Agg:
504 else
505 {
508 (AggPath *) best_path);
509 }
510 break;
511 case T_WindowAgg:
514 break;
515 case T_SetOp:
518 flags);
519 break;
520 case T_RecursiveUnion:
523 break;
524 case T_LockRows:
527 flags);
528 break;
529 case T_ModifyTable:
532 break;
533 case T_Limit:
536 flags);
537 break;
538 case T_GatherMerge:
541 break;
542 default:
543 elog(ERROR, "unrecognized node type: %d",
544 (int) best_path->pathtype);
545 plan = NULL; /* keep compiler quiet */
546 break;
547 }
548
549 return plan;
550}
551
552/*
553 * create_scan_plan
554 * Create a scan plan for the parent relation of 'best_path'.
555 */
556static Plan *
558{
559 RelOptInfo *rel = best_path->parent;
562 List *tlist;
563 Plan *plan;
564
565 /*
566 * Extract the relevant restriction clauses from the parent relation. The
567 * executor must apply all these restrictions during the scan, except for
568 * pseudoconstants which we'll take care of below.
569 *
570 * If this is a plain indexscan or index-only scan, we need not consider
571 * restriction clauses that are implied by the index's predicate, so use
572 * indrestrictinfo not baserestrictinfo. Note that we can't do that for
573 * bitmap indexscans, since there's not necessarily a single index
574 * involved; but it doesn't matter since create_bitmap_scan_plan() will be
575 * able to get rid of such clauses anyway via predicate proof.
576 */
577 switch (best_path->pathtype)
578 {
579 case T_IndexScan:
580 case T_IndexOnlyScan:
581 scan_clauses = castNode(IndexPath, best_path)->indexinfo->indrestrictinfo;
582 break;
583 default:
585 break;
586 }
587
588 /*
589 * If this is a parameterized scan, we also need to enforce all the join
590 * clauses available from the outer relation(s).
591 *
592 * For paranoia's sake, don't modify the stored baserestrictinfo list.
593 */
594 if (best_path->param_info)
596 best_path->param_info->ppi_clauses);
597
598 /*
599 * Detect whether we have any pseudoconstant quals to deal with. Then, if
600 * we'll need a gating Result node, it will be able to project, so there
601 * are no requirements on the child's tlist.
602 *
603 * If this replaces a join, it must be a foreign scan or a custom scan,
604 * and the FDW or the custom scan provider would have stored in the best
605 * path the list of RestrictInfo nodes to apply to the join; check against
606 * that list in that case.
607 */
608 if (IS_JOIN_REL(rel))
609 {
611
612 Assert(best_path->pathtype == T_ForeignScan ||
613 best_path->pathtype == T_CustomScan);
614 if (best_path->pathtype == T_ForeignScan)
615 join_clauses = ((ForeignPath *) best_path)->fdw_restrictinfo;
616 else
617 join_clauses = ((CustomPath *) best_path)->custom_restrictinfo;
618
620 }
621 else
623 if (gating_clauses)
624 flags = 0;
625
626 /*
627 * For table scans, rather than using the relation targetlist (which is
628 * only those Vars actually needed by the query), we prefer to generate a
629 * tlist containing all Vars in order. This will allow the executor to
630 * optimize away projection of the table tuples, if possible.
631 *
632 * But if the caller is going to ignore our tlist anyway, then don't
633 * bother generating one at all. We use an exact equality test here, so
634 * that this only applies when CP_IGNORE_TLIST is the only flag set.
635 */
636 if (flags == CP_IGNORE_TLIST)
637 {
638 tlist = NULL;
639 }
640 else if (use_physical_tlist(root, best_path, flags))
641 {
642 if (best_path->pathtype == T_IndexOnlyScan)
643 {
644 /* For index-only scan, the preferred tlist is the index's */
645 tlist = copyObject(((IndexPath *) best_path)->indexinfo->indextlist);
646
647 /*
648 * Transfer sortgroupref data to the replacement tlist, if
649 * requested (use_physical_tlist checked that this will work).
650 */
651 if (flags & CP_LABEL_TLIST)
653 }
654 else
655 {
656 tlist = build_physical_tlist(root, rel);
657 if (tlist == NIL)
658 {
659 /* Failed because of dropped cols, so use regular method */
661 }
662 else
663 {
664 /* As above, transfer sortgroupref data to replacement tlist */
665 if (flags & CP_LABEL_TLIST)
667 }
668 }
669 }
670 else
671 {
673 }
674
675 switch (best_path->pathtype)
676 {
677 case T_SeqScan:
679 best_path,
680 tlist,
682 break;
683
684 case T_SampleScan:
686 best_path,
687 tlist,
689 break;
690
691 case T_IndexScan:
694 tlist,
696 false);
697 break;
698
699 case T_IndexOnlyScan:
702 tlist,
704 true);
705 break;
706
707 case T_BitmapHeapScan:
710 tlist,
712 break;
713
714 case T_TidScan:
716 (TidPath *) best_path,
717 tlist,
719 break;
720
721 case T_TidRangeScan:
724 tlist,
726 break;
727
728 case T_SubqueryScan:
731 tlist,
733 break;
734
735 case T_FunctionScan:
737 best_path,
738 tlist,
740 break;
741
742 case T_TableFuncScan:
744 best_path,
745 tlist,
747 break;
748
749 case T_ValuesScan:
751 best_path,
752 tlist,
754 break;
755
756 case T_CteScan:
758 best_path,
759 tlist,
761 break;
762
765 best_path,
766 tlist,
768 break;
769
770 case T_Result:
772 best_path,
773 tlist,
775 break;
776
777 case T_WorkTableScan:
779 best_path,
780 tlist,
782 break;
783
784 case T_ForeignScan:
787 tlist,
789 break;
790
791 case T_CustomScan:
794 tlist,
796 break;
797
798 default:
799 elog(ERROR, "unrecognized node type: %d",
800 (int) best_path->pathtype);
801 plan = NULL; /* keep compiler quiet */
802 break;
803 }
804
805 /*
806 * If there are any pseudoconstant clauses attached to this node, insert a
807 * gating Result node that evaluates the pseudoconstants as one-time
808 * quals.
809 */
810 if (gating_clauses)
812
813 return plan;
814}
815
816/*
817 * Build a target list (ie, a list of TargetEntry) for the Path's output.
818 *
819 * This is almost just make_tlist_from_pathtarget(), but we also have to
820 * deal with replacing nestloop params.
821 */
822static List *
824{
825 List *tlist = NIL;
826 Index *sortgrouprefs = path->pathtarget->sortgrouprefs;
827 int resno = 1;
828 ListCell *v;
829
830 foreach(v, path->pathtarget->exprs)
831 {
832 Node *node = (Node *) lfirst(v);
834
835 /*
836 * If it's a parameterized path, there might be lateral references in
837 * the tlist, which need to be replaced with Params. There's no need
838 * to remake the TargetEntry nodes, so apply this to each list item
839 * separately.
840 */
841 if (path->param_info)
842 node = replace_nestloop_params(root, node);
843
844 tle = makeTargetEntry((Expr *) node,
845 resno,
846 NULL,
847 false);
848 if (sortgrouprefs)
849 tle->ressortgroupref = sortgrouprefs[resno - 1];
850
851 tlist = lappend(tlist, tle);
852 resno++;
853 }
854 return tlist;
855}
856
857/*
858 * use_physical_tlist
859 * Decide whether to use a tlist matching relation structure,
860 * rather than only those Vars actually referenced.
861 */
862static bool
864{
865 RelOptInfo *rel = path->parent;
866 int i;
867 ListCell *lc;
868
869 /*
870 * Forget it if either exact tlist or small tlist is demanded.
871 */
872 if (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST))
873 return false;
874
875 /*
876 * We can do this for real relation scans, subquery scans, function scans,
877 * tablefunc scans, values scans, and CTE scans (but not for, eg, joins).
878 */
879 if (rel->rtekind != RTE_RELATION &&
880 rel->rtekind != RTE_SUBQUERY &&
881 rel->rtekind != RTE_FUNCTION &&
882 rel->rtekind != RTE_TABLEFUNC &&
883 rel->rtekind != RTE_VALUES &&
884 rel->rtekind != RTE_CTE)
885 return false;
886
887 /*
888 * Can't do it with inheritance cases either (mainly because Append
889 * doesn't project; this test may be unnecessary now that
890 * create_append_plan instructs its children to return an exact tlist).
891 */
892 if (rel->reloptkind != RELOPT_BASEREL)
893 return false;
894
895 /*
896 * Also, don't do it to a CustomPath; the premise that we're extracting
897 * columns from a simple physical tuple is unlikely to hold for those.
898 * (When it does make sense, the custom path creator can set up the path's
899 * pathtarget that way.)
900 */
901 if (IsA(path, CustomPath))
902 return false;
903
904 /*
905 * If a bitmap scan's tlist is empty, keep it as-is. This may allow the
906 * executor to skip heap page fetches, and in any case, the benefit of
907 * using a physical tlist instead would be minimal.
908 */
909 if (IsA(path, BitmapHeapPath) &&
910 path->pathtarget->exprs == NIL)
911 return false;
912
913 /*
914 * Can't do it if any system columns or whole-row Vars are requested.
915 * (This could possibly be fixed but would take some fragile assumptions
916 * in setrefs.c, I think.)
917 */
918 for (i = rel->min_attr; i <= 0; i++)
919 {
920 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
921 return false;
922 }
923
924 /*
925 * Can't do it if the rel is required to emit any placeholder expressions,
926 * either.
927 */
928 foreach(lc, root->placeholder_list)
929 {
931
932 if (bms_nonempty_difference(phinfo->ph_needed, rel->relids) &&
933 bms_is_subset(phinfo->ph_eval_at, rel->relids))
934 return false;
935 }
936
937 /*
938 * For an index-only scan, the "physical tlist" is the index's indextlist.
939 * We can only return that without a projection if all the index's columns
940 * are returnable.
941 */
942 if (path->pathtype == T_IndexOnlyScan)
943 {
944 IndexOptInfo *indexinfo = ((IndexPath *) path)->indexinfo;
945
946 for (i = 0; i < indexinfo->ncolumns; i++)
947 {
948 if (!indexinfo->canreturn[i])
949 return false;
950 }
951 }
952
953 /*
954 * Also, can't do it if CP_LABEL_TLIST is specified and path is requested
955 * to emit any sort/group columns that are not simple Vars. (If they are
956 * simple Vars, they should appear in the physical tlist, and
957 * apply_pathtarget_labeling_to_tlist will take care of getting them
958 * labeled again.) We also have to check that no two sort/group columns
959 * are the same Var, else that element of the physical tlist would need
960 * conflicting ressortgroupref labels.
961 */
962 if ((flags & CP_LABEL_TLIST) && path->pathtarget->sortgrouprefs)
963 {
965
966 i = 0;
967 foreach(lc, path->pathtarget->exprs)
968 {
969 Expr *expr = (Expr *) lfirst(lc);
970
971 if (path->pathtarget->sortgrouprefs[i])
972 {
973 if (expr && IsA(expr, Var))
974 {
975 int attno = ((Var *) expr)->varattno;
976
978 if (bms_is_member(attno, sortgroupatts))
979 return false;
981 }
982 else
983 return false;
984 }
985 i++;
986 }
987 }
988
989 return true;
990}
991
992/*
993 * get_gating_quals
994 * See if there are pseudoconstant quals in a node's quals list
995 *
996 * If the node's quals list includes any pseudoconstant quals,
997 * return just those quals.
998 */
999static List *
1001{
1002 /* No need to look if we know there are no pseudoconstants */
1003 if (!root->hasPseudoConstantQuals)
1004 return NIL;
1005
1006 /* Sort into desirable execution order while still in RestrictInfo form */
1007 quals = order_qual_clauses(root, quals);
1008
1009 /* Pull out any pseudoconstant quals from the RestrictInfo list */
1010 return extract_actual_clauses(quals, true);
1011}
1012
1013/*
1014 * create_gating_plan
1015 * Deal with pseudoconstant qual clauses
1016 *
1017 * Add a gating Result node atop the already-built plan.
1018 */
1019static Plan *
1022{
1023 Result *gplan;
1024
1026
1027 /*
1028 * Since we need a Result node anyway, always return the path's requested
1029 * tlist; that's never a wrong choice, even if the parent node didn't ask
1030 * for CP_EXACT_TLIST.
1031 */
1033 (Node *) gating_quals, plan);
1034
1035 /*
1036 * We might have had a trivial Result plan already. Stacking one Result
1037 * atop another is silly, so if that applies, just discard the input plan.
1038 * (We're assuming its targetlist is uninteresting; it should be either
1039 * the same as the result of build_path_tlist, or a simplified version.
1040 * However, we preserve the set of relids that it purports to scan and
1041 * attribute that to our replacement Result instead, and likewise for the
1042 * result_type.)
1043 */
1044 if (IsA(plan, Result))
1045 {
1046 Result *rplan = (Result *) plan;
1047
1048 gplan->plan.lefttree = NULL;
1049 gplan->relids = rplan->relids;
1050 gplan->result_type = rplan->result_type;
1051 }
1052
1053 /*
1054 * Notice that we don't change cost or size estimates when doing gating.
1055 * The costs of qual eval were already included in the subplan's cost.
1056 * Leaving the size alone amounts to assuming that the gating qual will
1057 * succeed, which is the conservative estimate for planning upper queries.
1058 * We certainly don't want to assume the output size is zero (unless the
1059 * gating qual is actually constant FALSE, and that case is dealt with in
1060 * clausesel.c). Interpolating between the two cases is silly, because it
1061 * doesn't reflect what will really happen at runtime, and besides which
1062 * in most cases we have only a very bad idea of the probability of the
1063 * gating qual being true.
1064 */
1065 copy_plan_costsize(&gplan->plan, plan);
1066
1067 /* Gating quals could be unsafe, so better use the Path's safety flag */
1068 gplan->plan.parallel_safe = path->parallel_safe;
1069
1070 return &gplan->plan;
1071}
1072
1073/*
1074 * create_join_plan
1075 * Create a join plan for 'best_path' and (recursively) plans for its
1076 * inner and outer paths.
1077 */
1078static Plan *
1080{
1081 Plan *plan;
1083
1084 switch (best_path->path.pathtype)
1085 {
1086 case T_MergeJoin:
1088 (MergePath *) best_path);
1089 break;
1090 case T_HashJoin:
1092 (HashPath *) best_path);
1093 break;
1094 case T_NestLoop:
1096 (NestPath *) best_path);
1097 break;
1098 default:
1099 elog(ERROR, "unrecognized node type: %d",
1100 (int) best_path->path.pathtype);
1101 plan = NULL; /* keep compiler quiet */
1102 break;
1103 }
1104
1105 /*
1106 * If there are any pseudoconstant clauses attached to this node, insert a
1107 * gating Result node that evaluates the pseudoconstants as one-time
1108 * quals.
1109 */
1110 gating_clauses = get_gating_quals(root, best_path->joinrestrictinfo);
1111 if (gating_clauses)
1114
1115#ifdef NOT_USED
1116
1117 /*
1118 * * Expensive function pullups may have pulled local predicates * into
1119 * this path node. Put them in the qpqual of the plan node. * JMH,
1120 * 6/15/92
1121 */
1126#endif
1127
1128 return plan;
1129}
1130
1131/*
1132 * mark_async_capable_plan
1133 * Check whether the Plan node created from a Path node is async-capable,
1134 * and if so, mark the Plan node as such and return true, otherwise
1135 * return false.
1136 */
1137static bool
1139{
1140 switch (nodeTag(path))
1141 {
1142 case T_SubqueryScanPath:
1143 {
1145
1146 /*
1147 * If the generated plan node includes a gating Result node,
1148 * we can't execute it asynchronously.
1149 */
1150 if (IsA(plan, Result))
1151 return false;
1152
1153 /*
1154 * If a SubqueryScan node atop of an async-capable plan node
1155 * is deletable, consider it as async-capable.
1156 */
1159 ((SubqueryScanPath *) path)->subpath))
1160 break;
1161 return false;
1162 }
1163 case T_ForeignPath:
1164 {
1165 FdwRoutine *fdwroutine = path->parent->fdwroutine;
1166
1167 /*
1168 * If the generated plan node includes a gating Result node,
1169 * we can't execute it asynchronously.
1170 */
1171 if (IsA(plan, Result))
1172 return false;
1173
1174 Assert(fdwroutine != NULL);
1175 if (fdwroutine->IsForeignPathAsyncCapable != NULL &&
1176 fdwroutine->IsForeignPathAsyncCapable((ForeignPath *) path))
1177 break;
1178 return false;
1179 }
1180 case T_ProjectionPath:
1181
1182 /*
1183 * If the generated plan node includes a Result node for the
1184 * projection, we can't execute it asynchronously.
1185 */
1186 if (IsA(plan, Result))
1187 return false;
1188
1189 /*
1190 * create_projection_plan() would have pulled up the subplan, so
1191 * check the capability using the subpath.
1192 */
1194 ((ProjectionPath *) path)->subpath))
1195 return true;
1196 return false;
1197 default:
1198 return false;
1199 }
1200
1201 plan->async_capable = true;
1202
1203 return true;
1204}
1205
1206/*
1207 * create_append_plan
1208 * Create an Append plan for 'best_path' and (recursively) plans
1209 * for its subpaths.
1210 *
1211 * Returns a Plan node.
1212 */
1213static Plan *
1215{
1216 Append *plan;
1217 List *tlist = build_path_tlist(root, &best_path->path);
1218 int orig_tlist_length = list_length(tlist);
1219 bool tlist_was_changed = false;
1220 List *pathkeys = best_path->path.pathkeys;
1221 List *subplans = NIL;
1222 ListCell *subpaths;
1223 int nasyncplans = 0;
1224 RelOptInfo *rel = best_path->path.parent;
1225 int nodenumsortkeys = 0;
1229 bool *nodeNullsFirst = NULL;
1230 bool consider_async = false;
1231
1232 /*
1233 * The subpaths list could be empty, if every child was proven empty by
1234 * constraint exclusion. In that case generate a dummy plan that returns
1235 * no rows.
1236 *
1237 * Note that an AppendPath with no members is also generated in certain
1238 * cases where there was no appending construct at all, but we know the
1239 * relation is empty (see set_dummy_rel_pathlist and mark_dummy_rel).
1240 */
1241 if (best_path->subpaths == NIL)
1242 {
1243 /* Generate a Result plan with constant-FALSE gating qual */
1244 Plan *plan;
1245
1246 plan = (Plan *) make_one_row_result(tlist,
1247 (Node *) list_make1(makeBoolConst(false,
1248 false)),
1249 best_path->path.parent);
1250
1252
1253 return plan;
1254 }
1255
1256 /*
1257 * Otherwise build an Append plan. Note that if there's just one child,
1258 * the Append is pretty useless; but we wait till setrefs.c to get rid of
1259 * it. Doing so here doesn't work because the varno of the child scan
1260 * plan won't match the parent-rel Vars it'll be asked to emit.
1261 *
1262 * We don't have the actual creation of the Append node split out into a
1263 * separate make_xxx function. This is because we want to run
1264 * prepare_sort_from_pathkeys on it before we do so on the individual
1265 * child plans, to make cross-checking the sort info easier.
1266 */
1267 plan = makeNode(Append);
1268 plan->plan.targetlist = tlist;
1269 plan->plan.qual = NIL;
1270 plan->plan.lefttree = NULL;
1271 plan->plan.righttree = NULL;
1272 plan->apprelids = rel->relids;
1273 plan->child_append_relid_sets = best_path->child_append_relid_sets;
1274
1275 if (pathkeys != NIL)
1276 {
1277 /*
1278 * Compute sort column info, and adjust the Append's tlist as needed.
1279 * Because we pass adjust_tlist_in_place = true, we may ignore the
1280 * function result; it must be the same plan node. However, we then
1281 * need to detect whether any tlist entries were added.
1282 */
1283 (void) prepare_sort_from_pathkeys((Plan *) plan, pathkeys,
1284 best_path->path.parent->relids,
1285 NULL,
1286 true,
1292 tlist_was_changed = (orig_tlist_length != list_length(plan->plan.targetlist));
1293 }
1294
1295 /* If appropriate, consider async append */
1296 consider_async = (enable_async_append && pathkeys == NIL &&
1297 !best_path->path.parallel_safe &&
1298 list_length(best_path->subpaths) > 1);
1299
1300 /* Build the plan for each child */
1301 foreach(subpaths, best_path->subpaths)
1302 {
1303 Path *subpath = (Path *) lfirst(subpaths);
1304 Plan *subplan;
1305
1306 /* Must insist that all children return the same tlist */
1308
1309 /*
1310 * For ordered Appends, we must insert a Sort node if subplan isn't
1311 * sufficiently ordered.
1312 */
1313 if (pathkeys != NIL)
1314 {
1315 int numsortkeys;
1316 AttrNumber *sortColIdx;
1317 Oid *sortOperators;
1318 Oid *collations;
1319 bool *nullsFirst;
1320 int presorted_keys;
1321
1322 /*
1323 * Compute sort column info, and adjust subplan's tlist as needed.
1324 * We must apply prepare_sort_from_pathkeys even to subplans that
1325 * don't need an explicit sort, to make sure they are returning
1326 * the same sort key columns the Append expects.
1327 */
1328 subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1329 subpath->parent->relids,
1331 false,
1332 &numsortkeys,
1333 &sortColIdx,
1334 &sortOperators,
1335 &collations,
1336 &nullsFirst);
1337
1338 /*
1339 * Check that we got the same sort key information. We just
1340 * Assert that the sortops match, since those depend only on the
1341 * pathkeys; but it seems like a good idea to check the sort
1342 * column numbers explicitly, to ensure the tlists match up.
1343 */
1345 if (memcmp(sortColIdx, nodeSortColIdx,
1346 numsortkeys * sizeof(AttrNumber)) != 0)
1347 elog(ERROR, "Append child's targetlist doesn't match Append");
1348 Assert(memcmp(sortOperators, nodeSortOperators,
1349 numsortkeys * sizeof(Oid)) == 0);
1350 Assert(memcmp(collations, nodeCollations,
1351 numsortkeys * sizeof(Oid)) == 0);
1353 numsortkeys * sizeof(bool)) == 0);
1354
1355 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1356 if (!pathkeys_count_contained_in(pathkeys, subpath->pathkeys,
1357 &presorted_keys))
1358 {
1359 Plan *sort_plan;
1360
1361 /*
1362 * We choose to use incremental sort if it is enabled and
1363 * there are presorted keys; otherwise we use full sort.
1364 */
1365 if (enable_incremental_sort && presorted_keys > 0)
1366 {
1367 sort_plan = (Plan *)
1368 make_incrementalsort(subplan, numsortkeys, presorted_keys,
1369 sortColIdx, sortOperators,
1370 collations, nullsFirst);
1371
1374 pathkeys,
1375 best_path->limit_tuples);
1376 }
1377 else
1378 {
1379 sort_plan = (Plan *) make_sort(subplan, numsortkeys,
1380 sortColIdx, sortOperators,
1381 collations, nullsFirst);
1382
1384 best_path->limit_tuples);
1385 }
1386
1387 subplan = sort_plan;
1388 }
1389 }
1390
1391 /* If needed, check to see if subplan can be executed asynchronously */
1393 {
1394 Assert(subplan->async_capable);
1395 ++nasyncplans;
1396 }
1397
1398 subplans = lappend(subplans, subplan);
1399 }
1400
1401 /* Set below if we find quals that we can use to run-time prune */
1402 plan->part_prune_index = -1;
1403
1404 /*
1405 * If any quals exist, they may be useful to perform further partition
1406 * pruning during execution. Gather information needed by the executor to
1407 * do partition pruning.
1408 */
1410 {
1411 List *prunequal;
1412
1414
1415 if (best_path->path.param_info)
1416 {
1417 List *prmquals = best_path->path.param_info->ppi_clauses;
1418
1421 (Node *) prmquals);
1422
1424 }
1425
1426 if (prunequal != NIL)
1427 plan->part_prune_index = make_partition_pruneinfo(root, rel,
1428 best_path->subpaths,
1429 prunequal);
1430 }
1431
1432 plan->appendplans = subplans;
1433 plan->nasyncplans = nasyncplans;
1434 plan->first_partial_plan = best_path->first_partial_path;
1435
1437
1438 /*
1439 * If prepare_sort_from_pathkeys added sort columns, but we were told to
1440 * produce either the exact tlist or a narrow tlist, we should get rid of
1441 * the sort columns again. We must inject a projection node to do so.
1442 */
1443 if (tlist_was_changed && (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST)))
1444 {
1445 tlist = list_copy_head(plan->plan.targetlist, orig_tlist_length);
1446 return inject_projection_plan((Plan *) plan, tlist,
1447 plan->plan.parallel_safe);
1448 }
1449 else
1450 return (Plan *) plan;
1451}
1452
1453/*
1454 * create_merge_append_plan
1455 * Create a MergeAppend plan for 'best_path' and (recursively) plans
1456 * for its subpaths.
1457 *
1458 * Returns a Plan node.
1459 */
1460static Plan *
1462 int flags)
1463{
1465 Plan *plan = &node->plan;
1466 List *tlist = build_path_tlist(root, &best_path->path);
1467 int orig_tlist_length = list_length(tlist);
1468 bool tlist_was_changed;
1469 List *pathkeys = best_path->path.pathkeys;
1470 List *subplans = NIL;
1471 ListCell *subpaths;
1472 RelOptInfo *rel = best_path->path.parent;
1473
1474 /*
1475 * We don't have the actual creation of the MergeAppend node split out
1476 * into a separate make_xxx function. This is because we want to run
1477 * prepare_sort_from_pathkeys on it before we do so on the individual
1478 * child plans, to make cross-checking the sort info easier.
1479 */
1481 plan->targetlist = tlist;
1482 plan->qual = NIL;
1483 plan->lefttree = NULL;
1484 plan->righttree = NULL;
1485 node->apprelids = rel->relids;
1486 node->child_append_relid_sets = best_path->child_append_relid_sets;
1487
1488 /*
1489 * Compute sort column info, and adjust MergeAppend's tlist as needed.
1490 * Because we pass adjust_tlist_in_place = true, we may ignore the
1491 * function result; it must be the same plan node. However, we then need
1492 * to detect whether any tlist entries were added.
1493 */
1495 best_path->path.parent->relids,
1496 NULL,
1497 true,
1498 &node->numCols,
1499 &node->sortColIdx,
1500 &node->sortOperators,
1501 &node->collations,
1502 &node->nullsFirst);
1504
1505 /*
1506 * Now prepare the child plans. We must apply prepare_sort_from_pathkeys
1507 * even to subplans that don't need an explicit sort, to make sure they
1508 * are returning the same sort key columns the MergeAppend expects.
1509 */
1510 foreach(subpaths, best_path->subpaths)
1511 {
1512 Path *subpath = (Path *) lfirst(subpaths);
1513 Plan *subplan;
1514 int numsortkeys;
1515 AttrNumber *sortColIdx;
1516 Oid *sortOperators;
1517 Oid *collations;
1518 bool *nullsFirst;
1519 int presorted_keys;
1520
1521 /* Build the child plan */
1522 /* Must insist that all children return the same tlist */
1524
1525 /* Compute sort column info, and adjust subplan's tlist as needed */
1526 subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1527 subpath->parent->relids,
1528 node->sortColIdx,
1529 false,
1530 &numsortkeys,
1531 &sortColIdx,
1532 &sortOperators,
1533 &collations,
1534 &nullsFirst);
1535
1536 /*
1537 * Check that we got the same sort key information. We just Assert
1538 * that the sortops match, since those depend only on the pathkeys;
1539 * but it seems like a good idea to check the sort column numbers
1540 * explicitly, to ensure the tlists really do match up.
1541 */
1542 Assert(numsortkeys == node->numCols);
1543 if (memcmp(sortColIdx, node->sortColIdx,
1544 numsortkeys * sizeof(AttrNumber)) != 0)
1545 elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
1546 Assert(memcmp(sortOperators, node->sortOperators,
1547 numsortkeys * sizeof(Oid)) == 0);
1548 Assert(memcmp(collations, node->collations,
1549 numsortkeys * sizeof(Oid)) == 0);
1550 Assert(memcmp(nullsFirst, node->nullsFirst,
1551 numsortkeys * sizeof(bool)) == 0);
1552
1553 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1554 if (!pathkeys_count_contained_in(pathkeys, subpath->pathkeys,
1555 &presorted_keys))
1556 {
1557 Plan *sort_plan;
1558
1559 /*
1560 * We choose to use incremental sort if it is enabled and there
1561 * are presorted keys; otherwise we use full sort.
1562 */
1563 if (enable_incremental_sort && presorted_keys > 0)
1564 {
1565 sort_plan = (Plan *)
1566 make_incrementalsort(subplan, numsortkeys, presorted_keys,
1567 sortColIdx, sortOperators,
1568 collations, nullsFirst);
1569
1572 pathkeys,
1573 best_path->limit_tuples);
1574 }
1575 else
1576 {
1577 sort_plan = (Plan *) make_sort(subplan, numsortkeys,
1578 sortColIdx, sortOperators,
1579 collations, nullsFirst);
1580
1582 best_path->limit_tuples);
1583 }
1584
1585 subplan = sort_plan;
1586 }
1587
1588 subplans = lappend(subplans, subplan);
1589 }
1590
1591 /* Set below if we find quals that we can use to run-time prune */
1592 node->part_prune_index = -1;
1593
1594 /*
1595 * If any quals exist, they may be useful to perform further partition
1596 * pruning during execution. Gather information needed by the executor to
1597 * do partition pruning.
1598 */
1600 {
1601 List *prunequal;
1602
1604
1605 /* We don't currently generate any parameterized MergeAppend paths */
1606 Assert(best_path->path.param_info == NULL);
1607
1608 if (prunequal != NIL)
1610 best_path->subpaths,
1611 prunequal);
1612 }
1613
1614 node->mergeplans = subplans;
1615
1616 /*
1617 * If prepare_sort_from_pathkeys added sort columns, but we were told to
1618 * produce either the exact tlist or a narrow tlist, we should get rid of
1619 * the sort columns again. We must inject a projection node to do so.
1620 */
1621 if (tlist_was_changed && (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST)))
1622 {
1623 tlist = list_copy_head(plan->targetlist, orig_tlist_length);
1624 return inject_projection_plan(plan, tlist, plan->parallel_safe);
1625 }
1626 else
1627 return plan;
1628}
1629
1630/*
1631 * create_group_result_plan
1632 * Create a Result plan for 'best_path'.
1633 * This is only used for degenerate grouping cases.
1634 *
1635 * Returns a Plan node.
1636 */
1637static Result *
1639{
1640 Result *plan;
1641 List *tlist;
1642 List *quals;
1643
1644 tlist = build_path_tlist(root, &best_path->path);
1645
1646 /* best_path->quals is just bare clauses */
1647 quals = order_qual_clauses(root, best_path->quals);
1648
1649 plan = make_one_row_result(tlist, (Node *) quals, best_path->path.parent);
1650
1652
1653 return plan;
1654}
1655
1656/*
1657 * create_project_set_plan
1658 * Create a ProjectSet plan for 'best_path'.
1659 *
1660 * Returns a Plan node.
1661 */
1662static ProjectSet *
1664{
1666 Plan *subplan;
1667 List *tlist;
1668
1669 /* Since we intend to project, we don't need to constrain child tlist */
1670 subplan = create_plan_recurse(root, best_path->subpath, 0);
1671
1672 tlist = build_path_tlist(root, &best_path->path);
1673
1674 plan = make_project_set(tlist, subplan);
1675
1677
1678 return plan;
1679}
1680
1681/*
1682 * create_material_plan
1683 * Create a Material plan for 'best_path' and (recursively) plans
1684 * for its subpaths.
1685 *
1686 * Returns a Plan node.
1687 */
1688static Material *
1690{
1691 Material *plan;
1692 Plan *subplan;
1693
1694 /*
1695 * We don't want any excess columns in the materialized tuples, so request
1696 * a smaller tlist. Otherwise, since Material doesn't project, tlist
1697 * requirements pass through.
1698 */
1699 subplan = create_plan_recurse(root, best_path->subpath,
1700 flags | CP_SMALL_TLIST);
1701
1702 plan = make_material(subplan);
1703
1705
1706 return plan;
1707}
1708
1709/*
1710 * create_memoize_plan
1711 * Create a Memoize plan for 'best_path' and (recursively) plans for its
1712 * subpaths.
1713 *
1714 * Returns a Plan node.
1715 */
1716static Memoize *
1718{
1719 Memoize *plan;
1720 Bitmapset *keyparamids;
1721 Plan *subplan;
1722 Oid *operators;
1723 Oid *collations;
1724 List *param_exprs = NIL;
1725 ListCell *lc;
1726 ListCell *lc2;
1727 int nkeys;
1728 int i;
1729
1730 subplan = create_plan_recurse(root, best_path->subpath,
1731 flags | CP_SMALL_TLIST);
1732
1733 param_exprs = (List *) replace_nestloop_params(root, (Node *)
1734 best_path->param_exprs);
1735
1736 nkeys = list_length(param_exprs);
1737 Assert(nkeys > 0);
1738 operators = palloc(nkeys * sizeof(Oid));
1739 collations = palloc(nkeys * sizeof(Oid));
1740
1741 i = 0;
1742 forboth(lc, param_exprs, lc2, best_path->hash_operators)
1743 {
1744 Expr *param_expr = (Expr *) lfirst(lc);
1745 Oid opno = lfirst_oid(lc2);
1746
1747 operators[i] = opno;
1748 collations[i] = exprCollation((Node *) param_expr);
1749 i++;
1750 }
1751
1752 keyparamids = pull_paramids((Expr *) param_exprs);
1753
1754 plan = make_memoize(subplan, operators, collations, param_exprs,
1755 best_path->singlerow, best_path->binary_mode,
1756 best_path->est_entries, keyparamids, best_path->est_calls,
1757 best_path->est_unique_keys, best_path->est_hit_ratio);
1758
1760
1761 return plan;
1762}
1763
1764/*
1765 * create_gather_plan
1766 *
1767 * Create a Gather plan for 'best_path' and (recursively) plans
1768 * for its subpaths.
1769 */
1770static Gather *
1772{
1774 Plan *subplan;
1775 List *tlist;
1776
1777 /*
1778 * Push projection down to the child node. That way, the projection work
1779 * is parallelized, and there can be no system columns in the result (they
1780 * can't travel through a tuple queue because it uses MinimalTuple
1781 * representation).
1782 */
1783 subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1784
1785 tlist = build_path_tlist(root, &best_path->path);
1786
1787 gather_plan = make_gather(tlist,
1788 NIL,
1789 best_path->num_workers,
1791 best_path->single_copy,
1792 subplan);
1793
1795
1796 /* use parallel mode for parallel plans. */
1797 root->glob->parallelModeNeeded = true;
1798
1799 return gather_plan;
1800}
1801
1802/*
1803 * create_gather_merge_plan
1804 *
1805 * Create a Gather Merge plan for 'best_path' and (recursively)
1806 * plans for its subpaths.
1807 */
1808static GatherMerge *
1810{
1812 Plan *subplan;
1813 List *pathkeys = best_path->path.pathkeys;
1814 List *tlist = build_path_tlist(root, &best_path->path);
1815
1816 /* As with Gather, project away columns in the workers. */
1817 subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1818
1819 /* Create a shell for a GatherMerge plan. */
1821 gm_plan->plan.targetlist = tlist;
1822 gm_plan->num_workers = best_path->num_workers;
1824
1825 /* Assign the rescan Param. */
1826 gm_plan->rescan_param = assign_special_exec_param(root);
1827
1828 /* Gather Merge is pointless with no pathkeys; use Gather instead. */
1829 Assert(pathkeys != NIL);
1830
1831 /* Compute sort column info, and adjust subplan's tlist as needed */
1832 subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1833 best_path->subpath->parent->relids,
1834 gm_plan->sortColIdx,
1835 false,
1836 &gm_plan->numCols,
1837 &gm_plan->sortColIdx,
1838 &gm_plan->sortOperators,
1839 &gm_plan->collations,
1840 &gm_plan->nullsFirst);
1841
1842 /*
1843 * All gather merge paths should have already guaranteed the necessary
1844 * sort order. See create_gather_merge_path.
1845 */
1846 Assert(pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys));
1847
1848 /* Now insert the subplan under GatherMerge. */
1849 gm_plan->plan.lefttree = subplan;
1850
1851 /* use parallel mode for parallel plans. */
1852 root->glob->parallelModeNeeded = true;
1853
1854 return gm_plan;
1855}
1856
1857/*
1858 * create_projection_plan
1859 *
1860 * Create a plan tree to do a projection step and (recursively) plans
1861 * for its subpaths. We may need a Result node for the projection,
1862 * but sometimes we can just let the subplan do the work.
1863 */
1864static Plan *
1866{
1867 Plan *plan;
1868 Plan *subplan;
1869 List *tlist;
1870 bool needs_result_node = false;
1871
1872 /*
1873 * Convert our subpath to a Plan and determine whether we need a Result
1874 * node.
1875 *
1876 * In most cases where we don't need to project, create_projection_path
1877 * will have set dummypp, but not always. First, some createplan.c
1878 * routines change the tlists of their nodes. (An example is that
1879 * create_merge_append_plan might add resjunk sort columns to a
1880 * MergeAppend.) Second, create_projection_path has no way of knowing
1881 * what path node will be placed on top of the projection path and
1882 * therefore can't predict whether it will require an exact tlist. For
1883 * both of these reasons, we have to recheck here.
1884 */
1885 if (use_physical_tlist(root, &best_path->path, flags))
1886 {
1887 /*
1888 * Our caller doesn't really care what tlist we return, so we don't
1889 * actually need to project. However, we may still need to ensure
1890 * proper sortgroupref labels, if the caller cares about those.
1891 */
1892 subplan = create_plan_recurse(root, best_path->subpath, 0);
1893 tlist = subplan->targetlist;
1894 if (flags & CP_LABEL_TLIST)
1896 best_path->path.pathtarget);
1897 }
1898 else if (is_projection_capable_path(best_path->subpath))
1899 {
1900 /*
1901 * Our caller requires that we return the exact tlist, but no separate
1902 * result node is needed because the subpath is projection-capable.
1903 * Tell create_plan_recurse that we're going to ignore the tlist it
1904 * produces.
1905 */
1906 subplan = create_plan_recurse(root, best_path->subpath,
1909 tlist = build_path_tlist(root, &best_path->path);
1910 }
1911 else
1912 {
1913 /*
1914 * It looks like we need a result node, unless by good fortune the
1915 * requested tlist is exactly the one the child wants to produce.
1916 */
1917 subplan = create_plan_recurse(root, best_path->subpath, 0);
1918 tlist = build_path_tlist(root, &best_path->path);
1919 needs_result_node = !tlist_same_exprs(tlist, subplan->targetlist);
1920 }
1921
1922 /*
1923 * If we make a different decision about whether to include a Result node
1924 * than create_projection_path did, we'll have made slightly wrong cost
1925 * estimates; but label the plan with the cost estimates we actually used,
1926 * not "corrected" ones. (XXX this could be cleaned up if we moved more
1927 * of the sortcolumn setup logic into Path creation, but that would add
1928 * expense to creating Paths we might end up not using.)
1929 */
1930 if (!needs_result_node)
1931 {
1932 /* Don't need a separate Result, just assign tlist to subplan */
1933 plan = subplan;
1934 plan->targetlist = tlist;
1935
1936 /* Label plan with the estimated costs we actually used */
1937 plan->startup_cost = best_path->path.startup_cost;
1938 plan->total_cost = best_path->path.total_cost;
1939 plan->plan_rows = best_path->path.rows;
1940 plan->plan_width = best_path->path.pathtarget->width;
1941 plan->parallel_safe = best_path->path.parallel_safe;
1942 /* ... but don't change subplan's parallel_aware flag */
1943 }
1944 else
1945 {
1946 plan = (Plan *) make_gating_result(tlist, NULL, subplan);
1947
1949 }
1950
1951 return plan;
1952}
1953
1954/*
1955 * inject_projection_plan
1956 * Insert a Result node to do a projection step.
1957 *
1958 * This is used in a few places where we decide on-the-fly that we need a
1959 * projection step as part of the tree generated for some Path node.
1960 * We should try to get rid of this in favor of doing it more honestly.
1961 *
1962 * One reason it's ugly is we have to be told the right parallel_safe marking
1963 * to apply (since the tlist might be unsafe even if the child plan is safe).
1964 */
1965static Plan *
1966inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
1967{
1968 Plan *plan;
1969
1970 plan = (Plan *) make_gating_result(tlist, NULL, subplan);
1971
1972 /*
1973 * In principle, we should charge tlist eval cost plus cpu_per_tuple per
1974 * row for the Result node. But the former has probably been factored in
1975 * already and the latter was not accounted for during Path construction,
1976 * so being formally correct might just make the EXPLAIN output look less
1977 * consistent not more so. Hence, just copy the subplan's cost.
1978 */
1979 copy_plan_costsize(plan, subplan);
1980 plan->parallel_safe = parallel_safe;
1981
1982 return plan;
1983}
1984
1985/*
1986 * change_plan_targetlist
1987 * Externally available wrapper for inject_projection_plan.
1988 *
1989 * This is meant for use by FDW plan-generation functions, which might
1990 * want to adjust the tlist computed by some subplan tree. In general,
1991 * a Result node is needed to compute the new tlist, but we can optimize
1992 * some cases.
1993 *
1994 * In most cases, tlist_parallel_safe can just be passed as the parallel_safe
1995 * flag of the FDW's own Path node.
1996 */
1997Plan *
1999{
2000 /*
2001 * If the top plan node can't do projections and its existing target list
2002 * isn't already what we need, we need to add a Result node to help it
2003 * along.
2004 */
2005 if (!is_projection_capable_plan(subplan) &&
2006 !tlist_same_exprs(tlist, subplan->targetlist))
2007 subplan = inject_projection_plan(subplan, tlist,
2008 subplan->parallel_safe &&
2010 else
2011 {
2012 /* Else we can just replace the plan node's tlist */
2013 subplan->targetlist = tlist;
2015 }
2016 return subplan;
2017}
2018
2019/*
2020 * create_sort_plan
2021 *
2022 * Create a Sort plan for 'best_path' and (recursively) plans
2023 * for its subpaths.
2024 */
2025static Sort *
2027{
2028 Sort *plan;
2029 Plan *subplan;
2030
2031 /*
2032 * We don't want any excess columns in the sorted tuples, so request a
2033 * smaller tlist. Otherwise, since Sort doesn't project, tlist
2034 * requirements pass through.
2035 */
2036 subplan = create_plan_recurse(root, best_path->subpath,
2037 flags | CP_SMALL_TLIST);
2038
2039 /*
2040 * make_sort_from_pathkeys indirectly calls find_ec_member_matching_expr,
2041 * which will ignore any child EC members that don't belong to the given
2042 * relids. Thus, if this sort path is based on a child relation, we must
2043 * pass its relids.
2044 */
2045 plan = make_sort_from_pathkeys(subplan, best_path->path.pathkeys,
2046 IS_OTHER_REL(best_path->subpath->parent) ?
2047 best_path->path.parent->relids : NULL);
2048
2050
2051 return plan;
2052}
2053
2054/*
2055 * create_incrementalsort_plan
2056 *
2057 * Do the same as create_sort_plan, but create IncrementalSort plan.
2058 */
2059static IncrementalSort *
2061 int flags)
2062{
2064 Plan *subplan;
2065
2066 /* See comments in create_sort_plan() above */
2067 subplan = create_plan_recurse(root, best_path->spath.subpath,
2068 flags | CP_SMALL_TLIST);
2070 best_path->spath.path.pathkeys,
2071 IS_OTHER_REL(best_path->spath.subpath->parent) ?
2072 best_path->spath.path.parent->relids : NULL,
2073 best_path->nPresortedCols);
2074
2075 copy_generic_path_info(&plan->sort.plan, (Path *) best_path);
2076
2077 return plan;
2078}
2079
2080/*
2081 * create_group_plan
2082 *
2083 * Create a Group plan for 'best_path' and (recursively) plans
2084 * for its subpaths.
2085 */
2086static Group *
2088{
2089 Group *plan;
2090 Plan *subplan;
2091 List *tlist;
2092 List *quals;
2093
2094 /*
2095 * Group can project, so no need to be terribly picky about child tlist,
2096 * but we do need grouping columns to be available
2097 */
2098 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2099
2100 tlist = build_path_tlist(root, &best_path->path);
2101
2102 quals = order_qual_clauses(root, best_path->qual);
2103
2104 plan = make_group(tlist,
2105 quals,
2106 list_length(best_path->groupClause),
2107 extract_grouping_cols(best_path->groupClause,
2108 subplan->targetlist),
2109 extract_grouping_ops(best_path->groupClause),
2111 subplan->targetlist),
2112 subplan);
2113
2115
2116 return plan;
2117}
2118
2119/*
2120 * create_unique_plan
2121 *
2122 * Create a Unique plan for 'best_path' and (recursively) plans
2123 * for its subpaths.
2124 */
2125static Unique *
2127{
2128 Unique *plan;
2129 Plan *subplan;
2130
2131 /*
2132 * Unique doesn't project, so tlist requirements pass through; moreover we
2133 * need grouping columns to be labeled.
2134 */
2135 subplan = create_plan_recurse(root, best_path->subpath,
2136 flags | CP_LABEL_TLIST);
2137
2138 /*
2139 * make_unique_from_pathkeys calls find_ec_member_matching_expr, which
2140 * will ignore any child EC members that don't belong to the given relids.
2141 * Thus, if this unique path is based on a child relation, we must pass
2142 * its relids.
2143 */
2145 best_path->path.pathkeys,
2146 best_path->numkeys,
2147 IS_OTHER_REL(best_path->path.parent) ?
2148 best_path->path.parent->relids : NULL);
2149
2151
2152 return plan;
2153}
2154
2155/*
2156 * create_agg_plan
2157 *
2158 * Create an Agg plan for 'best_path' and (recursively) plans
2159 * for its subpaths.
2160 */
2161static Agg *
2163{
2164 Agg *plan;
2165 Plan *subplan;
2166 List *tlist;
2167 List *quals;
2168
2169 /*
2170 * Agg can project, so no need to be terribly picky about child tlist, but
2171 * we do need grouping columns to be available
2172 */
2173 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2174
2175 tlist = build_path_tlist(root, &best_path->path);
2176
2177 quals = order_qual_clauses(root, best_path->qual);
2178
2179 plan = make_agg(tlist, quals,
2180 best_path->aggstrategy,
2181 best_path->aggsplit,
2182 list_length(best_path->groupClause),
2183 extract_grouping_cols(best_path->groupClause,
2184 subplan->targetlist),
2185 extract_grouping_ops(best_path->groupClause),
2187 subplan->targetlist),
2188 NIL,
2189 NIL,
2190 best_path->numGroups,
2191 best_path->transitionSpace,
2192 subplan);
2193
2195
2196 return plan;
2197}
2198
2199/*
2200 * Given a groupclause for a collection of grouping sets, produce the
2201 * corresponding groupColIdx.
2202 *
2203 * root->grouping_map maps the tleSortGroupRef to the actual column position in
2204 * the input tuple. So we get the ref from the entries in the groupclause and
2205 * look them up there.
2206 */
2207static AttrNumber *
2209{
2210 AttrNumber *grouping_map = root->grouping_map;
2212 ListCell *lc;
2213 int i;
2214
2216
2218
2219 i = 0;
2220 foreach(lc, groupClause)
2221 {
2222 SortGroupClause *clause = lfirst(lc);
2223
2225 }
2226
2227 return new_grpColIdx;
2228}
2229
2230/*
2231 * create_groupingsets_plan
2232 * Create a plan for 'best_path' and (recursively) plans
2233 * for its subpaths.
2234 *
2235 * What we emit is an Agg plan with some vestigial Agg and Sort nodes
2236 * hanging off the side. The top Agg implements the last grouping set
2237 * specified in the GroupingSetsPath, and any additional grouping sets
2238 * each give rise to a subsidiary Agg and Sort node in the top Agg's
2239 * "chain" list. These nodes don't participate in the plan directly,
2240 * but they are a convenient way to represent the required data for
2241 * the extra steps.
2242 *
2243 * Returns a Plan node.
2244 */
2245static Plan *
2247{
2248 Agg *plan;
2249 Plan *subplan;
2250 List *rollups = best_path->rollups;
2252 int maxref;
2253 List *chain;
2254 ListCell *lc;
2255
2256 /* Shouldn't get here without grouping sets */
2257 Assert(root->parse->groupingSets);
2258 Assert(rollups != NIL);
2259
2260 /*
2261 * Agg can project, so no need to be terribly picky about child tlist, but
2262 * we do need grouping columns to be available
2263 */
2264 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2265
2266 /*
2267 * Compute the mapping from tleSortGroupRef to column index in the child's
2268 * tlist. First, identify max SortGroupRef in groupClause, for array
2269 * sizing.
2270 */
2271 maxref = 0;
2272 foreach(lc, root->processed_groupClause)
2273 {
2275
2276 if (gc->tleSortGroupRef > maxref)
2278 }
2279
2280 grouping_map = (AttrNumber *) palloc0((maxref + 1) * sizeof(AttrNumber));
2281
2282 /* Now look up the column numbers in the child's tlist */
2283 foreach(lc, root->processed_groupClause)
2284 {
2287
2288 grouping_map[gc->tleSortGroupRef] = tle->resno;
2289 }
2290
2291 /*
2292 * During setrefs.c, we'll need the grouping_map to fix up the cols lists
2293 * in GroupingFunc nodes. Save it for setrefs.c to use.
2294 */
2295 Assert(root->grouping_map == NULL);
2296 root->grouping_map = grouping_map;
2297
2298 /*
2299 * Generate the side nodes that describe the other sort and group
2300 * operations besides the top one. Note that we don't worry about putting
2301 * accurate cost estimates in the side nodes; only the topmost Agg node's
2302 * costs will be shown by EXPLAIN.
2303 */
2304 chain = NIL;
2305 if (list_length(rollups) > 1)
2306 {
2307 bool is_first_sort = ((RollupData *) linitial(rollups))->is_hashed;
2308
2309 for_each_from(lc, rollups, 1)
2310 {
2313 Plan *sort_plan = NULL;
2314 Plan *agg_plan;
2316
2317 new_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2318
2319 if (!rollup->is_hashed && !is_first_sort)
2320 {
2321 sort_plan = (Plan *)
2322 make_sort_from_groupcols(rollup->groupClause,
2324 subplan);
2325 }
2326
2327 if (!rollup->is_hashed)
2328 is_first_sort = false;
2329
2330 if (rollup->is_hashed)
2331 strat = AGG_HASHED;
2332 else if (linitial(rollup->gsets) == NIL)
2333 strat = AGG_PLAIN;
2334 else
2335 strat = AGG_SORTED;
2336
2337 agg_plan = (Plan *) make_agg(NIL,
2338 NIL,
2339 strat,
2341 list_length((List *) linitial(rollup->gsets)),
2343 extract_grouping_ops(rollup->groupClause),
2344 extract_grouping_collations(rollup->groupClause, subplan->targetlist),
2345 rollup->gsets,
2346 NIL,
2347 rollup->numGroups,
2348 best_path->transitionSpace,
2349 sort_plan);
2350
2351 /*
2352 * Remove stuff we don't need to avoid bloating debug output.
2353 */
2354 if (sort_plan)
2355 {
2356 sort_plan->targetlist = NIL;
2357 sort_plan->lefttree = NULL;
2358 }
2359
2360 chain = lappend(chain, agg_plan);
2361 }
2362 }
2363
2364 /*
2365 * Now make the real Agg node
2366 */
2367 {
2368 RollupData *rollup = linitial(rollups);
2370 int numGroupCols;
2371
2372 top_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2373
2375
2377 best_path->qual,
2378 best_path->aggstrategy,
2382 extract_grouping_ops(rollup->groupClause),
2383 extract_grouping_collations(rollup->groupClause, subplan->targetlist),
2384 rollup->gsets,
2385 chain,
2386 rollup->numGroups,
2387 best_path->transitionSpace,
2388 subplan);
2389
2390 /* Copy cost data from Path to Plan */
2391 copy_generic_path_info(&plan->plan, &best_path->path);
2392 }
2393
2394 return (Plan *) plan;
2395}
2396
2397/*
2398 * create_minmaxagg_plan
2399 *
2400 * Create a Result plan for 'best_path' and (recursively) plans
2401 * for its subpaths.
2402 */
2403static Result *
2405{
2406 Result *plan;
2407 List *tlist;
2408 ListCell *lc;
2409
2410 /* Prepare an InitPlan for each aggregate's subquery. */
2411 foreach(lc, best_path->mmaggregates)
2412 {
2414 PlannerInfo *subroot = mminfo->subroot;
2415 Query *subparse = subroot->parse;
2416 Plan *plan;
2417
2418 /*
2419 * Generate the plan for the subquery. We already have a Path, but we
2420 * have to convert it to a Plan and attach a LIMIT node above it.
2421 * Since we are entering a different planner context (subroot),
2422 * recurse to create_plan not create_plan_recurse.
2423 */
2424 plan = create_plan(subroot, mminfo->path);
2425
2426 plan = (Plan *) make_limit(plan,
2427 subparse->limitOffset,
2428 subparse->limitCount,
2429 subparse->limitOption,
2430 0, NULL, NULL, NULL);
2431
2432 /* Must apply correct cost/width data to Limit node */
2433 plan->disabled_nodes = mminfo->path->disabled_nodes;
2434 plan->startup_cost = mminfo->path->startup_cost;
2435 plan->total_cost = mminfo->pathcost;
2436 plan->plan_rows = 1;
2437 plan->plan_width = mminfo->path->pathtarget->width;
2438 plan->parallel_aware = false;
2439 plan->parallel_safe = mminfo->path->parallel_safe;
2440
2441 /* Convert the plan into an InitPlan in the outer query. */
2442 SS_make_initplan_from_plan(root, subroot, plan, mminfo->param);
2443 }
2444
2445 /* Generate the output plan --- basically just a Result */
2446 tlist = build_path_tlist(root, &best_path->path);
2447
2448 plan = make_one_row_result(tlist, (Node *) best_path->quals,
2449 best_path->path.parent);
2450 plan->result_type = RESULT_TYPE_MINMAX;
2451
2453
2454 /*
2455 * During setrefs.c, we'll need to replace references to the Agg nodes
2456 * with InitPlan output params. (We can't just do that locally in the
2457 * MinMaxAgg node, because path nodes above here may have Agg references
2458 * as well.) Save the mmaggregates list to tell setrefs.c to do that.
2459 */
2460 Assert(root->minmax_aggs == NIL);
2461 root->minmax_aggs = best_path->mmaggregates;
2462
2463 return plan;
2464}
2465
2466/*
2467 * create_windowagg_plan
2468 *
2469 * Create a WindowAgg plan for 'best_path' and (recursively) plans
2470 * for its subpaths.
2471 */
2472static WindowAgg *
2474{
2475 WindowAgg *plan;
2476 WindowClause *wc = best_path->winclause;
2478 int numOrder = list_length(wc->orderClause);
2479 Plan *subplan;
2480 List *tlist;
2481 int partNumCols;
2485 int ordNumCols;
2489 ListCell *lc;
2490
2491 /*
2492 * Choice of tlist here is motivated by the fact that WindowAgg will be
2493 * storing the input rows of window frames in a tuplestore; it therefore
2494 * behooves us to request a small tlist to avoid wasting space. We do of
2495 * course need grouping columns to be available.
2496 */
2497 subplan = create_plan_recurse(root, best_path->subpath,
2499
2500 tlist = build_path_tlist(root, &best_path->path);
2501
2502 /*
2503 * Convert SortGroupClause lists into arrays of attr indexes and equality
2504 * operators, as wanted by executor.
2505 */
2509
2510 partNumCols = 0;
2511 foreach(lc, wc->partitionClause)
2512 {
2515
2516 Assert(OidIsValid(sgc->eqop));
2517 partColIdx[partNumCols] = tle->resno;
2518 partOperators[partNumCols] = sgc->eqop;
2519 partCollations[partNumCols] = exprCollation((Node *) tle->expr);
2520 partNumCols++;
2521 }
2522
2526
2527 ordNumCols = 0;
2528 foreach(lc, wc->orderClause)
2529 {
2532
2533 Assert(OidIsValid(sgc->eqop));
2534 ordColIdx[ordNumCols] = tle->resno;
2535 ordOperators[ordNumCols] = sgc->eqop;
2536 ordCollations[ordNumCols] = exprCollation((Node *) tle->expr);
2537 ordNumCols++;
2538 }
2539
2540 /* And finally we can make the WindowAgg node */
2541 plan = make_windowagg(tlist,
2542 wc,
2543 partNumCols,
2544 partColIdx,
2547 ordNumCols,
2548 ordColIdx,
2551 best_path->runCondition,
2552 best_path->qual,
2553 best_path->topwindow,
2554 subplan);
2555
2557
2558 return plan;
2559}
2560
2561/*
2562 * create_setop_plan
2563 *
2564 * Create a SetOp plan for 'best_path' and (recursively) plans
2565 * for its subpaths.
2566 */
2567static SetOp *
2569{
2570 SetOp *plan;
2571 List *tlist = build_path_tlist(root, &best_path->path);
2572 Plan *leftplan;
2573 Plan *rightplan;
2574
2575 /*
2576 * SetOp doesn't project, so tlist requirements pass through; moreover we
2577 * need grouping columns to be labeled.
2578 */
2580 flags | CP_LABEL_TLIST);
2582 flags | CP_LABEL_TLIST);
2583
2584 plan = make_setop(best_path->cmd,
2585 best_path->strategy,
2586 tlist,
2587 leftplan,
2588 rightplan,
2589 best_path->groupList,
2590 best_path->numGroups);
2591
2593
2594 return plan;
2595}
2596
2597/*
2598 * create_recursiveunion_plan
2599 *
2600 * Create a RecursiveUnion plan for 'best_path' and (recursively) plans
2601 * for its subpaths.
2602 */
2603static RecursiveUnion *
2605{
2607 Plan *leftplan;
2608 Plan *rightplan;
2609 List *tlist;
2610
2611 /* Need both children to produce same tlist, so force it */
2614
2615 tlist = build_path_tlist(root, &best_path->path);
2616
2617 plan = make_recursive_union(tlist,
2618 leftplan,
2619 rightplan,
2620 best_path->wtParam,
2621 best_path->distinctList,
2622 best_path->numGroups);
2623
2625
2626 return plan;
2627}
2628
2629/*
2630 * create_lockrows_plan
2631 *
2632 * Create a LockRows plan for 'best_path' and (recursively) plans
2633 * for its subpaths.
2634 */
2635static LockRows *
2637 int flags)
2638{
2639 LockRows *plan;
2640 Plan *subplan;
2641
2642 /* LockRows doesn't project, so tlist requirements pass through */
2643 subplan = create_plan_recurse(root, best_path->subpath, flags);
2644
2645 plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
2646
2648
2649 return plan;
2650}
2651
2652/*
2653 * create_modifytable_plan
2654 * Create a ModifyTable plan for 'best_path'.
2655 *
2656 * Returns a Plan node.
2657 */
2658static ModifyTable *
2660{
2662 Path *subpath = best_path->subpath;
2663 Plan *subplan;
2664
2665 /* Subplan must produce exactly the specified tlist */
2667
2668 /* Transfer resname/resjunk labeling, too, to keep executor happy */
2669 apply_tlist_labeling(subplan->targetlist, root->processed_tlist);
2670
2672 subplan,
2673 best_path->operation,
2674 best_path->canSetTag,
2675 best_path->nominalRelation,
2676 best_path->rootRelation,
2677 best_path->resultRelations,
2678 best_path->updateColnosLists,
2679 best_path->withCheckOptionLists,
2680 best_path->returningLists,
2681 best_path->rowMarks,
2682 best_path->onconflict,
2683 best_path->mergeActionLists,
2684 best_path->mergeJoinConditions,
2685 best_path->forPortionOf,
2686 best_path->epqParam);
2687
2688 copy_generic_path_info(&plan->plan, &best_path->path);
2689
2690 return plan;
2691}
2692
2693/*
2694 * create_limit_plan
2695 *
2696 * Create a Limit plan for 'best_path' and (recursively) plans
2697 * for its subpaths.
2698 */
2699static Limit *
2701{
2702 Limit *plan;
2703 Plan *subplan;
2704 int numUniqkeys = 0;
2708
2709 /* Limit doesn't project, so tlist requirements pass through */
2710 subplan = create_plan_recurse(root, best_path->subpath, flags);
2711
2712 /* Extract information necessary for comparing rows for WITH TIES. */
2713 if (best_path->limitOption == LIMIT_OPTION_WITH_TIES)
2714 {
2715 Query *parse = root->parse;
2716 ListCell *l;
2717
2718 numUniqkeys = list_length(parse->sortClause);
2720 uniqOperators = (Oid *) palloc(numUniqkeys * sizeof(Oid));
2721 uniqCollations = (Oid *) palloc(numUniqkeys * sizeof(Oid));
2722
2723 numUniqkeys = 0;
2724 foreach(l, parse->sortClause)
2725 {
2728
2729 uniqColIdx[numUniqkeys] = tle->resno;
2732 numUniqkeys++;
2733 }
2734 }
2735
2736 plan = make_limit(subplan,
2737 best_path->limitOffset,
2738 best_path->limitCount,
2739 best_path->limitOption,
2741
2743
2744 return plan;
2745}
2746
2747
2748/*****************************************************************************
2749 *
2750 * BASE-RELATION SCAN METHODS
2751 *
2752 *****************************************************************************/
2753
2754
2755/*
2756 * create_seqscan_plan
2757 * Returns a seqscan plan for the base relation scanned by 'best_path'
2758 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2759 */
2760static SeqScan *
2762 List *tlist, List *scan_clauses)
2763{
2765 Index scan_relid = best_path->parent->relid;
2766
2767 /* it should be a base rel... */
2768 Assert(scan_relid > 0);
2769 Assert(best_path->parent->rtekind == RTE_RELATION);
2770
2771 /* Sort clauses into best execution order */
2773
2774 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2776
2777 /* Replace any outer-relation variables with nestloop params */
2778 if (best_path->param_info)
2779 {
2780 scan_clauses = (List *)
2782 }
2783
2784 scan_plan = make_seqscan(tlist,
2786 scan_relid);
2787
2789
2790 return scan_plan;
2791}
2792
2793/*
2794 * create_samplescan_plan
2795 * Returns a samplescan plan for the base relation scanned by 'best_path'
2796 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2797 */
2798static SampleScan *
2800 List *tlist, List *scan_clauses)
2801{
2803 Index scan_relid = best_path->parent->relid;
2806
2807 /* it should be a base rel with a tablesample clause... */
2808 Assert(scan_relid > 0);
2810 Assert(rte->rtekind == RTE_RELATION);
2811 tsc = rte->tablesample;
2812 Assert(tsc != NULL);
2813
2814 /* Sort clauses into best execution order */
2816
2817 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2819
2820 /* Replace any outer-relation variables with nestloop params */
2821 if (best_path->param_info)
2822 {
2823 scan_clauses = (List *)
2827 }
2828
2829 scan_plan = make_samplescan(tlist,
2831 scan_relid,
2832 tsc);
2833
2835
2836 return scan_plan;
2837}
2838
2839/*
2840 * create_indexscan_plan
2841 * Returns an indexscan plan for the base relation scanned by 'best_path'
2842 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2843 *
2844 * We use this for both plain IndexScans and IndexOnlyScans, because the
2845 * qual preprocessing work is the same for both. Note that the caller tells
2846 * us which to build --- we don't look at best_path->path.pathtype, because
2847 * create_bitmap_subplan needs to be able to override the prior decision.
2848 */
2849static Scan *
2852 List *tlist,
2854 bool indexonly)
2855{
2856 Scan *scan_plan;
2857 List *indexclauses = best_path->indexclauses;
2858 List *indexorderbys = best_path->indexorderbys;
2859 Index baserelid = best_path->path.parent->relid;
2860 IndexOptInfo *indexinfo = best_path->indexinfo;
2861 Oid indexoid = indexinfo->indexoid;
2862 List *qpqual;
2866 List *indexorderbyops = NIL;
2867 ListCell *l;
2868
2869 /* it should be a base rel... */
2870 Assert(baserelid > 0);
2871 Assert(best_path->path.parent->rtekind == RTE_RELATION);
2872 /* check the scan direction is valid */
2873 Assert(best_path->indexscandir == ForwardScanDirection ||
2874 best_path->indexscandir == BackwardScanDirection);
2875
2876 /*
2877 * Extract the index qual expressions (stripped of RestrictInfos) from the
2878 * IndexClauses list, and prepare a copy with index Vars substituted for
2879 * table Vars. (This step also does replace_nestloop_params on the
2880 * fixed_indexquals.)
2881 */
2885
2886 /*
2887 * Likewise fix up index attr references in the ORDER BY expressions.
2888 */
2890
2891 /*
2892 * The qpqual list must contain all restrictions not automatically handled
2893 * by the index, other than pseudoconstant clauses which will be handled
2894 * by a separate gating plan node. All the predicates in the indexquals
2895 * will be checked (either by the index itself, or by nodeIndexscan.c),
2896 * but if there are any "special" operators involved then they must be
2897 * included in qpqual. The upshot is that qpqual must contain
2898 * scan_clauses minus whatever appears in indexquals.
2899 *
2900 * is_redundant_with_indexclauses() detects cases where a scan clause is
2901 * present in the indexclauses list or is generated from the same
2902 * EquivalenceClass as some indexclause, and is therefore redundant with
2903 * it, though not equal. (The latter happens when indxpath.c prefers a
2904 * different derived equality than what generate_join_implied_equalities
2905 * picked for a parameterized scan's ppi_clauses.) Note that it will not
2906 * match to lossy index clauses, which is critical because we have to
2907 * include the original clause in qpqual in that case.
2908 *
2909 * In some situations (particularly with OR'd index conditions) we may
2910 * have scan_clauses that are not equal to, but are logically implied by,
2911 * the index quals; so we also try a predicate_implied_by() check to see
2912 * if we can discard quals that way. (predicate_implied_by assumes its
2913 * first input contains only immutable functions, so we have to check
2914 * that.)
2915 *
2916 * Note: if you change this bit of code you should also look at
2917 * extract_nonindex_conditions() in costsize.c.
2918 */
2919 qpqual = NIL;
2920 foreach(l, scan_clauses)
2921 {
2923
2924 if (rinfo->pseudoconstant)
2925 continue; /* we may drop pseudoconstants here */
2926 if (is_redundant_with_indexclauses(rinfo, indexclauses))
2927 continue; /* dup or derived from same EquivalenceClass */
2928 if (!contain_mutable_functions((Node *) rinfo->clause) &&
2930 false))
2931 continue; /* provably implied by indexquals */
2932 qpqual = lappend(qpqual, rinfo);
2933 }
2934
2935 /* Sort clauses into best execution order */
2937
2938 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2940
2941 /*
2942 * We have to replace any outer-relation variables with nestloop params in
2943 * the indexqualorig, qpqual, and indexorderbyorig expressions. A bit
2944 * annoying to have to do this separately from the processing in
2945 * fix_indexqual_references --- rethink this when generalizing the inner
2946 * indexscan support. But note we can't really do this earlier because
2947 * it'd break the comparisons to predicates above ... (or would it? Those
2948 * wouldn't have outer refs)
2949 */
2950 if (best_path->path.param_info)
2951 {
2954 qpqual = (List *)
2956 indexorderbys = (List *)
2957 replace_nestloop_params(root, (Node *) indexorderbys);
2958 }
2959
2960 /*
2961 * If there are ORDER BY expressions, look up the sort operators for their
2962 * result datatypes.
2963 */
2964 if (indexorderbys)
2965 {
2967 *exprCell;
2968
2969 /*
2970 * PathKey contains OID of the btree opfamily we're sorting by, but
2971 * that's not quite enough because we need the expression's datatype
2972 * to look up the sort operator in the operator family.
2973 */
2974 Assert(list_length(best_path->path.pathkeys) == list_length(indexorderbys));
2975 forboth(pathkeyCell, best_path->path.pathkeys, exprCell, indexorderbys)
2976 {
2978 Node *expr = (Node *) lfirst(exprCell);
2979 Oid exprtype = exprType(expr);
2980 Oid sortop;
2981
2982 /* Get sort operator from opfamily */
2983 sortop = get_opfamily_member_for_cmptype(pathkey->pk_opfamily,
2984 exprtype,
2985 exprtype,
2986 pathkey->pk_cmptype);
2987 if (!OidIsValid(sortop))
2988 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
2989 pathkey->pk_cmptype, exprtype, exprtype, pathkey->pk_opfamily);
2990 indexorderbyops = lappend_oid(indexorderbyops, sortop);
2991 }
2992 }
2993
2994 /*
2995 * For an index-only scan, we must mark indextlist entries as resjunk if
2996 * they are columns that the index AM can't return; this cues setrefs.c to
2997 * not generate references to those columns.
2998 */
2999 if (indexonly)
3000 {
3001 int i = 0;
3002
3003 foreach(l, indexinfo->indextlist)
3004 {
3006
3007 indextle->resjunk = !indexinfo->canreturn[i];
3008 i++;
3009 }
3010 }
3011
3012 /* Finally ready to build the plan node */
3013 if (indexonly)
3014 scan_plan = (Scan *) make_indexonlyscan(tlist,
3015 qpqual,
3016 baserelid,
3017 indexoid,
3021 indexinfo->indextlist,
3022 best_path->indexscandir);
3023 else
3024 scan_plan = (Scan *) make_indexscan(tlist,
3025 qpqual,
3026 baserelid,
3027 indexoid,
3031 indexorderbys,
3032 indexorderbyops,
3033 best_path->indexscandir);
3034
3036
3037 return scan_plan;
3038}
3039
3040/*
3041 * create_bitmap_scan_plan
3042 * Returns a bitmap scan plan for the base relation scanned by 'best_path'
3043 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3044 */
3045static BitmapHeapScan *
3048 List *tlist,
3050{
3051 Index baserelid = best_path->path.parent->relid;
3053 List *bitmapqualorig;
3054 List *indexquals;
3055 List *indexECs;
3056 List *qpqual;
3057 ListCell *l;
3059
3060 /* it should be a base rel... */
3061 Assert(baserelid > 0);
3062 Assert(best_path->path.parent->rtekind == RTE_RELATION);
3063
3064 /* Process the bitmapqual tree into a Plan tree and qual lists */
3066 &bitmapqualorig, &indexquals,
3067 &indexECs);
3068
3069 if (best_path->path.parallel_aware)
3071
3072 /*
3073 * The qpqual list must contain all restrictions not automatically handled
3074 * by the index, other than pseudoconstant clauses which will be handled
3075 * by a separate gating plan node. All the predicates in the indexquals
3076 * will be checked (either by the index itself, or by
3077 * nodeBitmapHeapscan.c), but if there are any "special" operators
3078 * involved then they must be added to qpqual. The upshot is that qpqual
3079 * must contain scan_clauses minus whatever appears in indexquals.
3080 *
3081 * This loop is similar to the comparable code in create_indexscan_plan(),
3082 * but with some differences because it has to compare the scan clauses to
3083 * stripped (no RestrictInfos) indexquals. See comments there for more
3084 * info.
3085 *
3086 * In normal cases simple equal() checks will be enough to spot duplicate
3087 * clauses, so we try that first. We next see if the scan clause is
3088 * redundant with any top-level indexqual by virtue of being generated
3089 * from the same EC. After that, try predicate_implied_by().
3090 *
3091 * Unlike create_indexscan_plan(), the predicate_implied_by() test here is
3092 * useful for getting rid of qpquals that are implied by index predicates,
3093 * because the predicate conditions are included in the "indexquals"
3094 * returned by create_bitmap_subplan(). Bitmap scans have to do it that
3095 * way because predicate conditions need to be rechecked if the scan
3096 * becomes lossy, so they have to be included in bitmapqualorig.
3097 */
3098 qpqual = NIL;
3099 foreach(l, scan_clauses)
3100 {
3102 Node *clause = (Node *) rinfo->clause;
3103
3104 if (rinfo->pseudoconstant)
3105 continue; /* we may drop pseudoconstants here */
3106 if (list_member(indexquals, clause))
3107 continue; /* simple duplicate */
3108 if (rinfo->parent_ec && list_member_ptr(indexECs, rinfo->parent_ec))
3109 continue; /* derived from same EquivalenceClass */
3110 if (!contain_mutable_functions(clause) &&
3111 predicate_implied_by(list_make1(clause), indexquals, false))
3112 continue; /* provably implied by indexquals */
3113 qpqual = lappend(qpqual, rinfo);
3114 }
3115
3116 /* Sort clauses into best execution order */
3118
3119 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3121
3122 /*
3123 * When dealing with special operators, we will at this point have
3124 * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
3125 * 'em from bitmapqualorig, since there's no point in making the tests
3126 * twice.
3127 */
3128 bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
3129
3130 /*
3131 * We have to replace any outer-relation variables with nestloop params in
3132 * the qpqual and bitmapqualorig expressions. (This was already done for
3133 * expressions attached to plan nodes in the bitmapqualplan tree.)
3134 */
3135 if (best_path->path.param_info)
3136 {
3137 qpqual = (List *)
3139 bitmapqualorig = (List *)
3140 replace_nestloop_params(root, (Node *) bitmapqualorig);
3141 }
3142
3143 /* Finally ready to build the plan node */
3145 qpqual,
3147 bitmapqualorig,
3148 baserelid);
3149
3150 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3151
3152 return scan_plan;
3153}
3154
3155/*
3156 * Given a bitmapqual tree, generate the Plan tree that implements it
3157 *
3158 * As byproducts, we also return in *qual and *indexqual the qual lists
3159 * (in implicit-AND form, without RestrictInfos) describing the original index
3160 * conditions and the generated indexqual conditions. (These are the same in
3161 * simple cases, but when special index operators are involved, the former
3162 * list includes the special conditions while the latter includes the actual
3163 * indexable conditions derived from them.) Both lists include partial-index
3164 * predicates, because we have to recheck predicates as well as index
3165 * conditions if the bitmap scan becomes lossy.
3166 *
3167 * In addition, we return a list of EquivalenceClass pointers for all the
3168 * top-level indexquals that were possibly-redundantly derived from ECs.
3169 * This allows removal of scan_clauses that are redundant with such quals.
3170 * (We do not attempt to detect such redundancies for quals that are within
3171 * OR subtrees. This could be done in a less hacky way if we returned the
3172 * indexquals in RestrictInfo form, but that would be slower and still pretty
3173 * messy, since we'd have to build new RestrictInfos in many cases.)
3174 */
3175static Plan *
3177 List **qual, List **indexqual, List **indexECs)
3178{
3179 Plan *plan;
3180
3181 if (IsA(bitmapqual, BitmapAndPath))
3182 {
3183 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
3184 List *subplans = NIL;
3185 List *subquals = NIL;
3187 List *subindexECs = NIL;
3188 ListCell *l;
3189
3190 /*
3191 * There may well be redundant quals among the subplans, since a
3192 * top-level WHERE qual might have gotten used to form several
3193 * different index quals. We don't try exceedingly hard to eliminate
3194 * redundancies, but we do eliminate obvious duplicates by using
3195 * list_concat_unique.
3196 */
3197 foreach(l, apath->bitmapquals)
3198 {
3199 Plan *subplan;
3200 List *subqual;
3203
3204 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
3206 &subindexEC);
3207 subplans = lappend(subplans, subplan);
3210 /* Duplicates in indexECs aren't worth getting rid of */
3212 }
3213 plan = (Plan *) make_bitmap_and(subplans);
3214 plan->startup_cost = apath->path.startup_cost;
3215 plan->total_cost = apath->path.total_cost;
3216 plan->plan_rows =
3217 clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
3218 plan->plan_width = 0; /* meaningless */
3219 plan->parallel_aware = false;
3220 plan->parallel_safe = apath->path.parallel_safe;
3221 *qual = subquals;
3222 *indexqual = subindexquals;
3224 }
3225 else if (IsA(bitmapqual, BitmapOrPath))
3226 {
3227 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
3228 List *subplans = NIL;
3229 List *subquals = NIL;
3231 bool const_true_subqual = false;
3232 bool const_true_subindexqual = false;
3233 ListCell *l;
3234
3235 /*
3236 * Here, we only detect qual-free subplans. A qual-free subplan would
3237 * cause us to generate "... OR true ..." which we may as well reduce
3238 * to just "true". We do not try to eliminate redundant subclauses
3239 * because (a) it's not as likely as in the AND case, and (b) we might
3240 * well be working with hundreds or even thousands of OR conditions,
3241 * perhaps from a long IN list. The performance of list_append_unique
3242 * would be unacceptable.
3243 */
3244 foreach(l, opath->bitmapquals)
3245 {
3246 Plan *subplan;
3247 List *subqual;
3250
3251 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
3253 &subindexEC);
3254 subplans = lappend(subplans, subplan);
3255 if (subqual == NIL)
3256 const_true_subqual = true;
3257 else if (!const_true_subqual)
3260 if (subindexqual == NIL)
3262 else if (!const_true_subindexqual)
3265 }
3266
3267 /*
3268 * In the presence of ScalarArrayOpExpr quals, we might have built
3269 * BitmapOrPaths with just one subpath; don't add an OR step.
3270 */
3271 if (list_length(subplans) == 1)
3272 {
3273 plan = (Plan *) linitial(subplans);
3274 }
3275 else
3276 {
3277 plan = (Plan *) make_bitmap_or(subplans);
3278 plan->startup_cost = opath->path.startup_cost;
3279 plan->total_cost = opath->path.total_cost;
3280 plan->plan_rows =
3281 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
3282 plan->plan_width = 0; /* meaningless */
3283 plan->parallel_aware = false;
3284 plan->parallel_safe = opath->path.parallel_safe;
3285 }
3286
3287 /*
3288 * If there were constant-TRUE subquals, the OR reduces to constant
3289 * TRUE. Also, avoid generating one-element ORs, which could happen
3290 * due to redundancy elimination or ScalarArrayOpExpr quals.
3291 */
3293 *qual = NIL;
3294 else if (list_length(subquals) <= 1)
3295 *qual = subquals;
3296 else
3299 *indexqual = NIL;
3300 else if (list_length(subindexquals) <= 1)
3301 *indexqual = subindexquals;
3302 else
3303 *indexqual = list_make1(make_orclause(subindexquals));
3304 *indexECs = NIL;
3305 }
3306 else if (IsA(bitmapqual, IndexPath))
3307 {
3308 IndexPath *ipath = (IndexPath *) bitmapqual;
3309 IndexScan *iscan;
3310 List *subquals;
3313 ListCell *l;
3314
3315 /* Use the regular indexscan plan build machinery... */
3316 iscan = castNode(IndexScan,
3318 NIL, NIL, false));
3319 /* then convert to a bitmap indexscan */
3321 iscan->indexid,
3322 iscan->indexqual,
3323 iscan->indexqualorig);
3324 /* and set its cost/width fields appropriately */
3325 plan->startup_cost = 0.0;
3326 plan->total_cost = ipath->indextotalcost;
3327 plan->plan_rows =
3328 clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
3329 plan->plan_width = 0; /* meaningless */
3330 plan->parallel_aware = false;
3331 plan->parallel_safe = ipath->path.parallel_safe;
3332 /* Extract original index clauses, actual index quals, relevant ECs */
3333 subquals = NIL;
3335 subindexECs = NIL;
3336 foreach(l, ipath->indexclauses)
3337 {
3339 RestrictInfo *rinfo = iclause->rinfo;
3340
3341 Assert(!rinfo->pseudoconstant);
3342 subquals = lappend(subquals, rinfo->clause);
3344 get_actual_clauses(iclause->indexquals));
3345 if (rinfo->parent_ec)
3346 subindexECs = lappend(subindexECs, rinfo->parent_ec);
3347 }
3348 /* We can add any index predicate conditions, too */
3349 foreach(l, ipath->indexinfo->indpred)
3350 {
3351 Expr *pred = (Expr *) lfirst(l);
3352
3353 /*
3354 * We know that the index predicate must have been implied by the
3355 * query condition as a whole, but it may or may not be implied by
3356 * the conditions that got pushed into the bitmapqual. Avoid
3357 * generating redundant conditions.
3358 */
3359 if (!predicate_implied_by(list_make1(pred), subquals, false))
3360 {
3361 subquals = lappend(subquals, pred);
3363 }
3364 }
3365 *qual = subquals;
3366 *indexqual = subindexquals;
3368 }
3369 else
3370 {
3371 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
3372 plan = NULL; /* keep compiler quiet */
3373 }
3374
3375 return plan;
3376}
3377
3378/*
3379 * create_tidscan_plan
3380 * Returns a tidscan plan for the base relation scanned by 'best_path'
3381 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3382 */
3383static TidScan *
3385 List *tlist, List *scan_clauses)
3386{
3388 Index scan_relid = best_path->path.parent->relid;
3389 List *tidquals = best_path->tidquals;
3390
3391 /* it should be a base rel... */
3392 Assert(scan_relid > 0);
3393 Assert(best_path->path.parent->rtekind == RTE_RELATION);
3394
3395 /*
3396 * The qpqual list must contain all restrictions not enforced by the
3397 * tidquals list. Since tidquals has OR semantics, we have to be careful
3398 * about matching it up to scan_clauses. It's convenient to handle the
3399 * single-tidqual case separately from the multiple-tidqual case. In the
3400 * single-tidqual case, we look through the scan_clauses while they are
3401 * still in RestrictInfo form, and drop any that are redundant with the
3402 * tidqual.
3403 *
3404 * In normal cases simple pointer equality checks will be enough to spot
3405 * duplicate RestrictInfos, so we try that first.
3406 *
3407 * Another common case is that a scan_clauses entry is generated from the
3408 * same EquivalenceClass as some tidqual, and is therefore redundant with
3409 * it, though not equal.
3410 *
3411 * Unlike indexpaths, we don't bother with predicate_implied_by(); the
3412 * number of cases where it could win are pretty small.
3413 */
3414 if (list_length(tidquals) == 1)
3415 {
3416 List *qpqual = NIL;
3417 ListCell *l;
3418
3419 foreach(l, scan_clauses)
3420 {
3422
3423 if (rinfo->pseudoconstant)
3424 continue; /* we may drop pseudoconstants here */
3425 if (list_member_ptr(tidquals, rinfo))
3426 continue; /* simple duplicate */
3427 if (is_redundant_derived_clause(rinfo, tidquals))
3428 continue; /* derived from same EquivalenceClass */
3429 qpqual = lappend(qpqual, rinfo);
3430 }
3432 }
3433
3434 /* Sort clauses into best execution order */
3436
3437 /* Reduce RestrictInfo lists to bare expressions; ignore pseudoconstants */
3438 tidquals = extract_actual_clauses(tidquals, false);
3440
3441 /*
3442 * If we have multiple tidquals, it's more convenient to remove duplicate
3443 * scan_clauses after stripping the RestrictInfos. In this situation,
3444 * because the tidquals represent OR sub-clauses, they could not have come
3445 * from EquivalenceClasses so we don't have to worry about matching up
3446 * non-identical clauses. On the other hand, because tidpath.c will have
3447 * extracted those sub-clauses from some OR clause and built its own list,
3448 * we will certainly not have pointer equality to any scan clause. So
3449 * convert the tidquals list to an explicit OR clause and see if we can
3450 * match it via equal() to any scan clause.
3451 */
3452 if (list_length(tidquals) > 1)
3454 list_make1(make_orclause(tidquals)));
3455
3456 /* Replace any outer-relation variables with nestloop params */
3457 if (best_path->path.param_info)
3458 {
3459 tidquals = (List *)
3460 replace_nestloop_params(root, (Node *) tidquals);
3461 scan_clauses = (List *)
3463 }
3464
3465 scan_plan = make_tidscan(tlist,
3467 scan_relid,
3468 tidquals);
3469
3470 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3471
3472 return scan_plan;
3473}
3474
3475/*
3476 * create_tidrangescan_plan
3477 * Returns a tidrangescan plan for the base relation scanned by 'best_path'
3478 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3479 */
3480static TidRangeScan *
3482 List *tlist, List *scan_clauses)
3483{
3485 Index scan_relid = best_path->path.parent->relid;
3486 List *tidrangequals = best_path->tidrangequals;
3487
3488 /* it should be a base rel... */
3489 Assert(scan_relid > 0);
3490 Assert(best_path->path.parent->rtekind == RTE_RELATION);
3491
3492 /*
3493 * The qpqual list must contain all restrictions not enforced by the
3494 * tidrangequals list. tidrangequals has AND semantics, so we can simply
3495 * remove any qual that appears in it.
3496 */
3497 {
3498 List *qpqual = NIL;
3499 ListCell *l;
3500
3501 foreach(l, scan_clauses)
3502 {
3504
3505 if (rinfo->pseudoconstant)
3506 continue; /* we may drop pseudoconstants here */
3507 if (list_member_ptr(tidrangequals, rinfo))
3508 continue; /* simple duplicate */
3509 qpqual = lappend(qpqual, rinfo);
3510 }
3512 }
3513
3514 /* Sort clauses into best execution order */
3516
3517 /* Reduce RestrictInfo lists to bare expressions; ignore pseudoconstants */
3518 tidrangequals = extract_actual_clauses(tidrangequals, false);
3520
3521 /* Replace any outer-relation variables with nestloop params */
3522 if (best_path->path.param_info)
3523 {
3524 tidrangequals = (List *)
3525 replace_nestloop_params(root, (Node *) tidrangequals);
3526 scan_clauses = (List *)
3528 }
3529
3532 scan_relid,
3533 tidrangequals);
3534
3535 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3536
3537 return scan_plan;
3538}
3539
3540/*
3541 * create_subqueryscan_plan
3542 * Returns a subqueryscan plan for the base relation scanned by 'best_path'
3543 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3544 */
3545static SubqueryScan *
3547 List *tlist, List *scan_clauses)
3548{
3550 RelOptInfo *rel = best_path->path.parent;
3551 Index scan_relid = rel->relid;
3552 Plan *subplan;
3553
3554 /* it should be a subquery base rel... */
3555 Assert(scan_relid > 0);
3556 Assert(rel->rtekind == RTE_SUBQUERY);
3557
3558 /*
3559 * Recursively create Plan from Path for subquery. Since we are entering
3560 * a different planner context (subroot), recurse to create_plan not
3561 * create_plan_recurse.
3562 */
3563 subplan = create_plan(rel->subroot, best_path->subpath);
3564
3565 /* Sort clauses into best execution order */
3567
3568 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3570
3571 /*
3572 * Replace any outer-relation variables with nestloop params.
3573 *
3574 * We must provide nestloop params for both lateral references of the
3575 * subquery and outer vars in the scan_clauses. It's better to assign the
3576 * former first, because that code path requires specific param IDs, while
3577 * replace_nestloop_params can adapt to the IDs assigned by
3578 * process_subquery_nestloop_params. This avoids possibly duplicating
3579 * nestloop params when the same Var is needed for both reasons.
3580 */
3581 if (best_path->path.param_info)
3582 {
3584 rel->subplan_params);
3585 scan_clauses = (List *)
3587 }
3588
3591 scan_relid,
3592 subplan);
3593
3594 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3595
3596 return scan_plan;
3597}
3598
3599/*
3600 * create_functionscan_plan
3601 * Returns a functionscan plan for the base relation scanned by 'best_path'
3602 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3603 */
3604static FunctionScan *
3606 List *tlist, List *scan_clauses)
3607{
3609 Index scan_relid = best_path->parent->relid;
3611 List *functions;
3612
3613 /* it should be a function base rel... */
3614 Assert(scan_relid > 0);
3616 Assert(rte->rtekind == RTE_FUNCTION);
3617 functions = rte->functions;
3618
3619 /* Sort clauses into best execution order */
3621
3622 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3624
3625 /* Replace any outer-relation variables with nestloop params */
3626 if (best_path->param_info)
3627 {
3628 scan_clauses = (List *)
3630 /* The function expressions could contain nestloop params, too */
3632 }
3633
3635 functions, rte->funcordinality);
3636
3638
3639 return scan_plan;
3640}
3641
3642/*
3643 * create_tablefuncscan_plan
3644 * Returns a tablefuncscan plan for the base relation scanned by 'best_path'
3645 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3646 */
3647static TableFuncScan *
3649 List *tlist, List *scan_clauses)
3650{
3652 Index scan_relid = best_path->parent->relid;
3654 TableFunc *tablefunc;
3655
3656 /* it should be a function base rel... */
3657 Assert(scan_relid > 0);
3659 Assert(rte->rtekind == RTE_TABLEFUNC);
3660 tablefunc = rte->tablefunc;
3661
3662 /* Sort clauses into best execution order */
3664
3665 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3667
3668 /* Replace any outer-relation variables with nestloop params */
3669 if (best_path->param_info)
3670 {
3671 scan_clauses = (List *)
3673 /* The function expressions could contain nestloop params, too */
3674 tablefunc = (TableFunc *) replace_nestloop_params(root, (Node *) tablefunc);
3675 }
3676
3678 tablefunc);
3679
3681
3682 return scan_plan;
3683}
3684
3685/*
3686 * create_valuesscan_plan
3687 * Returns a valuesscan plan for the base relation scanned by 'best_path'
3688 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3689 */
3690static ValuesScan *
3692 List *tlist, List *scan_clauses)
3693{
3695 Index scan_relid = best_path->parent->relid;
3697 List *values_lists;
3698
3699 /* it should be a values base rel... */
3700 Assert(scan_relid > 0);
3702 Assert(rte->rtekind == RTE_VALUES);
3703 values_lists = rte->values_lists;
3704
3705 /* Sort clauses into best execution order */
3707
3708 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3710
3711 /* Replace any outer-relation variables with nestloop params */
3712 if (best_path->param_info)
3713 {
3714 scan_clauses = (List *)
3716 /* The values lists could contain nestloop params, too */
3717 values_lists = (List *)
3718 replace_nestloop_params(root, (Node *) values_lists);
3719 }
3720
3722 values_lists);
3723
3725
3726 return scan_plan;
3727}
3728
3729/*
3730 * create_ctescan_plan
3731 * Returns a ctescan plan for the base relation scanned by 'best_path'
3732 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3733 */
3734static CteScan *
3736 List *tlist, List *scan_clauses)
3737{
3739 Index scan_relid = best_path->parent->relid;
3742 int plan_id;
3743 int cte_param_id;
3745 Index levelsup;
3746 int ndx;
3747 ListCell *lc;
3748
3749 Assert(scan_relid > 0);
3751 Assert(rte->rtekind == RTE_CTE);
3752 Assert(!rte->self_reference);
3753
3754 /*
3755 * Find the referenced CTE, and locate the SubPlan previously made for it.
3756 */
3757 levelsup = rte->ctelevelsup;
3758 cteroot = root;
3759 while (levelsup-- > 0)
3760 {
3761 cteroot = cteroot->parent_root;
3762 if (!cteroot) /* shouldn't happen */
3763 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3764 }
3765
3766 /*
3767 * Note: cte_plan_ids can be shorter than cteList, if we are still working
3768 * on planning the CTEs (ie, this is a side-reference from another CTE).
3769 * So we mustn't use forboth here.
3770 */
3771 ndx = 0;
3772 foreach(lc, cteroot->parse->cteList)
3773 {
3775
3776 if (strcmp(cte->ctename, rte->ctename) == 0)
3777 break;
3778 ndx++;
3779 }
3780 if (lc == NULL) /* shouldn't happen */
3781 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
3782 if (ndx >= list_length(cteroot->cte_plan_ids))
3783 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3784 plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
3785 if (plan_id <= 0)
3786 elog(ERROR, "no plan was made for CTE \"%s\"", rte->ctename);
3787 foreach(lc, cteroot->init_plans)
3788 {
3789 ctesplan = (SubPlan *) lfirst(lc);
3790 if (ctesplan->plan_id == plan_id)
3791 break;
3792 }
3793 if (lc == NULL) /* shouldn't happen */
3794 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3795
3796 /*
3797 * We need the CTE param ID, which is the sole member of the SubPlan's
3798 * setParam list.
3799 */
3800 cte_param_id = linitial_int(ctesplan->setParam);
3801
3802 /* Sort clauses into best execution order */
3804
3805 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3807
3808 /* Replace any outer-relation variables with nestloop params */
3809 if (best_path->param_info)
3810 {
3811 scan_clauses = (List *)
3813 }
3814
3816 plan_id, cte_param_id);
3817
3819
3820 return scan_plan;
3821}
3822
3823/*
3824 * create_namedtuplestorescan_plan
3825 * Returns a tuplestorescan plan for the base relation scanned by
3826 * 'best_path' with restriction clauses 'scan_clauses' and targetlist
3827 * 'tlist'.
3828 */
3829static NamedTuplestoreScan *
3831 List *tlist, List *scan_clauses)
3832{
3834 Index scan_relid = best_path->parent->relid;
3836
3837 Assert(scan_relid > 0);
3839 Assert(rte->rtekind == RTE_NAMEDTUPLESTORE);
3840
3841 /* Sort clauses into best execution order */
3843
3844 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3846
3847 /* Replace any outer-relation variables with nestloop params */
3848 if (best_path->param_info)
3849 {
3850 scan_clauses = (List *)
3852 }
3853
3855 rte->enrname);
3856
3858
3859 return scan_plan;
3860}
3861
3862/*
3863 * create_resultscan_plan
3864 * Returns a Result plan for the RTE_RESULT base relation scanned by
3865 * 'best_path' with restriction clauses 'scan_clauses' and targetlist
3866 * 'tlist'.
3867 */
3868static Result *
3870 List *tlist, List *scan_clauses)
3871{
3873 Index scan_relid = best_path->parent->relid;
3875
3876 Assert(scan_relid > 0);
3878 Assert(rte->rtekind == RTE_RESULT);
3879
3880 /* Sort clauses into best execution order */
3882
3883 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3885
3886 /* Replace any outer-relation variables with nestloop params */
3887 if (best_path->param_info)
3888 {
3889 scan_clauses = (List *)
3891 }
3892
3894 best_path->parent);
3895
3897
3898 return scan_plan;
3899}
3900
3901/*
3902 * create_worktablescan_plan
3903 * Returns a worktablescan plan for the base relation scanned by 'best_path'
3904 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3905 */
3906static WorkTableScan *
3908 List *tlist, List *scan_clauses)
3909{
3911 Index scan_relid = best_path->parent->relid;
3913 Index levelsup;
3915
3916 Assert(scan_relid > 0);
3918 Assert(rte->rtekind == RTE_CTE);
3919 Assert(rte->self_reference);
3920
3921 /*
3922 * We need to find the worktable param ID, which is in the plan level
3923 * that's processing the recursive UNION, which is one level *below* where
3924 * the CTE comes from.
3925 */
3926 levelsup = rte->ctelevelsup;
3927 if (levelsup == 0) /* shouldn't happen */
3928 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3929 levelsup--;
3930 cteroot = root;
3931 while (levelsup-- > 0)
3932 {
3933 cteroot = cteroot->parent_root;
3934 if (!cteroot) /* shouldn't happen */
3935 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3936 }
3937 if (cteroot->wt_param_id < 0) /* shouldn't happen */
3938 elog(ERROR, "could not find param ID for CTE \"%s\"", rte->ctename);
3939
3940 /* Sort clauses into best execution order */
3942
3943 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3945
3946 /* Replace any outer-relation variables with nestloop params */
3947 if (best_path->param_info)
3948 {
3949 scan_clauses = (List *)
3951 }
3952
3954 cteroot->wt_param_id);
3955
3957
3958 return scan_plan;
3959}
3960
3961/*
3962 * create_foreignscan_plan
3963 * Returns a foreignscan plan for the relation scanned by 'best_path'
3964 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3965 */
3966static ForeignScan *
3968 List *tlist, List *scan_clauses)
3969{
3971 RelOptInfo *rel = best_path->path.parent;
3972 Index scan_relid = rel->relid;
3974 Plan *outer_plan = NULL;
3975
3976 Assert(rel->fdwroutine != NULL);
3977
3978 /* transform the child path if any */
3979 if (best_path->fdw_outerpath)
3980 outer_plan = create_plan_recurse(root, best_path->fdw_outerpath,
3982
3983 /*
3984 * If we're scanning a base relation, fetch its OID. (Irrelevant if
3985 * scanning a join relation.)
3986 */
3987 if (scan_relid > 0)
3988 {
3990
3991 Assert(rel->rtekind == RTE_RELATION);
3993 Assert(rte->rtekind == RTE_RELATION);
3994 rel_oid = rte->relid;
3995 }
3996
3997 /*
3998 * Sort clauses into best execution order. We do this first since the FDW
3999 * might have more info than we do and wish to adjust the ordering.
4000 */
4002
4003 /*
4004 * Let the FDW perform its processing on the restriction clauses and
4005 * generate the plan node. Note that the FDW might remove restriction
4006 * clauses that it intends to execute remotely, or even add more (if it
4007 * has selected some join clauses for remote use but also wants them
4008 * rechecked locally).
4009 */
4010 scan_plan = rel->fdwroutine->GetForeignPlan(root, rel, rel_oid,
4011 best_path,
4012 tlist, scan_clauses,
4013 outer_plan);
4014
4015 /* Copy cost data from Path to Plan; no need to make FDW do this */
4016 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
4017
4018 /* Copy user OID to access as; likewise no need to make FDW do this */
4019 scan_plan->checkAsUser = rel->userid;
4020
4021 /* Copy foreign server OID; likewise, no need to make FDW do this */
4022 scan_plan->fs_server = rel->serverid;
4023
4024 /*
4025 * Likewise, copy the relids that are represented by this foreign scan. An
4026 * upper rel doesn't have relids set, but it covers all the relations
4027 * participating in the underlying scan/join, so use root->all_query_rels.
4028 */
4029 if (rel->reloptkind == RELOPT_UPPER_REL)
4030 scan_plan->fs_relids = root->all_query_rels;
4031 else
4032 scan_plan->fs_relids = best_path->path.parent->relids;
4033
4034 /*
4035 * Join relid sets include relevant outer joins, but FDWs may need to know
4036 * which are the included base rels. That's a bit tedious to get without
4037 * access to the plan-time data structures, so compute it here.
4038 */
4039 scan_plan->fs_base_relids = bms_difference(scan_plan->fs_relids,
4040 root->outer_join_rels);
4041
4042 /*
4043 * If this is a foreign join, and to make it valid to push down we had to
4044 * assume that the current user is the same as some user explicitly named
4045 * in the query, mark the finished plan as depending on the current user.
4046 */
4047 if (rel->useridiscurrent)
4048 root->glob->dependsOnRole = true;
4049
4050 /*
4051 * Replace any outer-relation variables with nestloop params in the qual,
4052 * fdw_exprs and fdw_recheck_quals expressions. We do this last so that
4053 * the FDW doesn't have to be involved. (Note that parts of fdw_exprs or
4054 * fdw_recheck_quals could have come from join clauses, so doing this
4055 * beforehand on the scan_clauses wouldn't work.) We assume
4056 * fdw_scan_tlist contains no such variables.
4057 */
4058 if (best_path->path.param_info)
4059 {
4060 scan_plan->scan.plan.qual = (List *)
4061 replace_nestloop_params(root, (Node *) scan_plan->scan.plan.qual);
4062 scan_plan->fdw_exprs = (List *)
4063 replace_nestloop_params(root, (Node *) scan_plan->fdw_exprs);
4064 scan_plan->fdw_recheck_quals = (List *)
4066 (Node *) scan_plan->fdw_recheck_quals);
4067 }
4068
4069 /*
4070 * If rel is a base relation, detect whether any system columns are
4071 * requested from the rel. (If rel is a join relation, rel->relid will be
4072 * 0, but there can be no Var with relid 0 in the rel's targetlist or the
4073 * restriction clauses, so we skip this in that case. Note that any such
4074 * columns in base relations that were joined are assumed to be contained
4075 * in fdw_scan_tlist.) This is a bit of a kluge and might go away
4076 * someday, so we intentionally leave it out of the API presented to FDWs.
4077 */
4078 scan_plan->fsSystemCol = false;
4079 if (scan_relid > 0)
4080 {
4081 Bitmapset *attrs_used = NULL;
4082 ListCell *lc;
4083 int i;
4084
4085 /*
4086 * First, examine all the attributes needed for joins or final output.
4087 * Note: we must look at rel's targetlist, not the attr_needed data,
4088 * because attr_needed isn't computed for inheritance child rels.
4089 */
4090 pull_varattnos((Node *) rel->reltarget->exprs, scan_relid, &attrs_used);
4091
4092 /* Add all the attributes used by restriction clauses. */
4093 foreach(lc, rel->baserestrictinfo)
4094 {
4095 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
4096
4097 pull_varattnos((Node *) rinfo->clause, scan_relid, &attrs_used);
4098 }
4099
4100 /* Now, are any system columns requested from rel? */
4101 for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
4102 {
4104 {
4105 scan_plan->fsSystemCol = true;
4106 break;
4107 }
4108 }
4109
4110 bms_free(attrs_used);
4111 }
4112
4113 return scan_plan;
4114}
4115
4116/*
4117 * create_customscan_plan
4118 *
4119 * Transform a CustomPath into a Plan.
4120 */
4121static CustomScan *
4123 List *tlist, List *scan_clauses)
4124{
4125 CustomScan *cplan;
4126 RelOptInfo *rel = best_path->path.parent;
4127 List *custom_plans = NIL;
4128 ListCell *lc;
4129
4130 /* Recursively transform child paths. */
4131 foreach(lc, best_path->custom_paths)
4132 {
4135
4136 custom_plans = lappend(custom_plans, plan);
4137 }
4138
4139 /*
4140 * Sort clauses into the best execution order, although custom-scan
4141 * provider can reorder them again.
4142 */
4144
4145 /*
4146 * Invoke custom plan provider to create the Plan node represented by the
4147 * CustomPath.
4148 */
4149 cplan = castNode(CustomScan,
4150 best_path->methods->PlanCustomPath(root,
4151 rel,
4152 best_path,
4153 tlist,
4155 custom_plans));
4156
4157 /*
4158 * Copy cost data from Path to Plan; no need to make custom-plan providers
4159 * do this
4160 */
4161 copy_generic_path_info(&cplan->scan.plan, &best_path->path);
4162
4163 /* Likewise, copy the relids that are represented by this custom scan */
4164 cplan->custom_relids = best_path->path.parent->relids;
4165
4166 /*
4167 * Replace any outer-relation variables with nestloop params in the qual
4168 * and custom_exprs expressions. We do this last so that the custom-plan
4169 * provider doesn't have to be involved. (Note that parts of custom_exprs
4170 * could have come from join clauses, so doing this beforehand on the
4171 * scan_clauses wouldn't work.) We assume custom_scan_tlist contains no
4172 * such variables.
4173 */
4174 if (best_path->path.param_info)
4175 {
4176 cplan->scan.plan.qual = (List *)
4177 replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
4178 cplan->custom_exprs = (List *)
4180 }
4181
4182 return cplan;
4183}
4184
4185
4186/*****************************************************************************
4187 *
4188 * JOIN METHODS
4189 *
4190 *****************************************************************************/
4191
4192static NestLoop *
4195{
4197 Plan *outer_plan;
4198 Plan *inner_plan;
4199 Relids outerrelids;
4200 Relids ojrelids;
4201 List *tlist = build_path_tlist(root, &best_path->jpath.path);
4202 List *joinrestrictclauses = best_path->jpath.joinrestrictinfo;
4203 List *joinclauses;
4205 List *nestParams;
4206 List *outer_tlist;
4208 Relids saveOuterRels = root->curOuterRels;
4209 ListCell *lc;
4210
4211 /*
4212 * If the inner path is parameterized by the topmost parent of the outer
4213 * rel rather than the outer rel itself, fix that. (Nothing happens here
4214 * if it is not so parameterized.)
4215 */
4216 best_path->jpath.innerjoinpath =
4218 best_path->jpath.innerjoinpath,
4219 best_path->jpath.outerjoinpath->parent);
4220
4221 /*
4222 * Failure here probably means that reparameterize_path_by_child() is not
4223 * in sync with path_is_reparameterizable_by_child().
4224 */
4225 Assert(best_path->jpath.innerjoinpath != NULL);
4226
4227 /* NestLoop can project, so no need to be picky about child tlists */
4228 outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath, 0);
4229
4230 /* For a nestloop, include outer relids in curOuterRels for inner side */
4231 outerrelids = best_path->jpath.outerjoinpath->parent->relids;
4232 root->curOuterRels = bms_union(root->curOuterRels, outerrelids);
4233
4234 inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath, 0);
4235
4236 /* Restore curOuterRels */
4237 bms_free(root->curOuterRels);
4238 root->curOuterRels = saveOuterRels;
4239
4240 /* Sort join qual clauses into best execution order */
4242
4243 /* Get the join qual clauses (in plain expression form) */
4244 /* Any pseudoconstant clauses are ignored here */
4245 if (IS_OUTER_JOIN(best_path->jpath.jointype))
4246 {
4248 best_path->jpath.path.parent->relids,
4249 &joinclauses, &otherclauses);
4250 }
4251 else
4252 {
4253 /* We can treat all clauses alike for an inner join */
4254 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
4255 otherclauses = NIL;
4256 }
4257
4258 /* Replace any outer-relation variables with nestloop params */
4259 if (best_path->jpath.path.param_info)
4260 {
4261 joinclauses = (List *)
4262 replace_nestloop_params(root, (Node *) joinclauses);
4263 otherclauses = (List *)
4265 }
4266
4267 /* Identify any outer joins computed at this level */
4268 ojrelids = bms_difference(best_path->jpath.path.parent->relids,
4269 bms_union(best_path->jpath.outerjoinpath->parent->relids,
4270 best_path->jpath.innerjoinpath->parent->relids));
4271
4272 /*
4273 * Identify any nestloop parameters that should be supplied by this join
4274 * node, and remove them from root->curOuterParams.
4275 */
4277 outerrelids,
4279
4280 /*
4281 * While nestloop parameters that are Vars had better be available from
4282 * the outer_plan already, there are edge cases where nestloop parameters
4283 * that are PHVs won't be. In such cases we must add them to the
4284 * outer_plan's tlist, since the executor's NestLoopParam machinery
4285 * requires the params to be simple outer-Var references to that tlist.
4286 * (This is cheating a little bit, because the outer path's required-outer
4287 * relids might not be enough to allow evaluating such a PHV. But in
4288 * practice, if we could have evaluated the PHV at the nestloop node, we
4289 * can do so in the outer plan too.)
4290 */
4291 outer_tlist = outer_plan->targetlist;
4292 outer_parallel_safe = outer_plan->parallel_safe;
4293 foreach(lc, nestParams)
4294 {
4298
4299 if (IsA(nlp->paramval, Var))
4300 continue; /* nothing to do for simple Vars */
4301 /* Otherwise it must be a PHV */
4302 phv = castNode(PlaceHolderVar, nlp->paramval);
4303
4304 if (tlist_member((Expr *) phv, outer_tlist))
4305 continue; /* already available */
4306
4307 /*
4308 * It's possible that nestloop parameter PHVs selected to evaluate
4309 * here contain references to surviving root->curOuterParams items
4310 * (that is, they reference values that will be supplied by some
4311 * higher-level nestloop). Those need to be converted to Params now.
4312 * Note: it's safe to do this after the tlist_member() check, because
4313 * equal() won't pay attention to phv->phexpr.
4314 */
4315 phv->phexpr = (Expr *) replace_nestloop_params(root,
4316 (Node *) phv->phexpr);
4317
4318 /* Make a shallow copy of outer_tlist, if we didn't already */
4319 if (outer_tlist == outer_plan->targetlist)
4320 outer_tlist = list_copy(outer_tlist);
4321 /* ... and add the needed expression */
4323 list_length(outer_tlist) + 1,
4324 NULL,
4325 true);
4326 outer_tlist = lappend(outer_tlist, tle);
4327 /* ... and track whether tlist is (still) parallel-safe */
4330 }
4331 if (outer_tlist != outer_plan->targetlist)
4332 outer_plan = change_plan_targetlist(outer_plan, outer_tlist,
4334
4335 /* And finally, we can build the join plan node */
4336 join_plan = make_nestloop(tlist,
4337 joinclauses,
4339 nestParams,
4340 outer_plan,
4341 inner_plan,
4342 best_path->jpath.jointype,
4343 ojrelids,
4344 best_path->jpath.inner_unique);
4345
4346 copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4347
4348 return join_plan;
4349}
4350
4351static MergeJoin *
4354{
4356 Plan *outer_plan;
4357 Plan *inner_plan;
4358 Relids ojrelids;
4359 List *tlist = build_path_tlist(root, &best_path->jpath.path);
4360 List *joinclauses;
4362 List *mergeclauses;
4365 int nClauses;
4368 bool *mergereversals;
4369 bool *mergenullsfirst;
4372 int i;
4373 ListCell *lc;
4374 ListCell *lop;
4375 ListCell *lip;
4376 Path *outer_path = best_path->jpath.outerjoinpath;
4377 Path *inner_path = best_path->jpath.innerjoinpath;
4378
4379 /*
4380 * MergeJoin can project, so we don't have to demand exact tlists from the
4381 * inputs. However, if we're intending to sort an input's result, it's
4382 * best to request a small tlist so we aren't sorting more data than
4383 * necessary.
4384 */
4385 outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4386 (best_path->outersortkeys != NIL) ? CP_SMALL_TLIST : 0);
4387
4388 inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4389 (best_path->innersortkeys != NIL) ? CP_SMALL_TLIST : 0);
4390
4391 /* Sort join qual clauses into best execution order */
4392 /* NB: do NOT reorder the mergeclauses */
4393 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4394
4395 /* Get the join qual clauses (in plain expression form) */
4396 /* Any pseudoconstant clauses are ignored here */
4397 if (IS_OUTER_JOIN(best_path->jpath.jointype))
4398 {
4399 extract_actual_join_clauses(joinclauses,
4400 best_path->jpath.path.parent->relids,
4401 &joinclauses, &otherclauses);
4402 }
4403 else
4404 {
4405 /* We can treat all clauses alike for an inner join */
4406 joinclauses = extract_actual_clauses(joinclauses, false);
4407 otherclauses = NIL;
4408 }
4409
4410 /*
4411 * Remove the mergeclauses from the list of join qual clauses, leaving the
4412 * list of quals that must be checked as qpquals.
4413 */
4414 mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
4415 joinclauses = list_difference(joinclauses, mergeclauses);
4416
4417 /*
4418 * Replace any outer-relation variables with nestloop params. There
4419 * should not be any in the mergeclauses.
4420 */
4421 if (best_path->jpath.path.param_info)
4422 {
4423 joinclauses = (List *)
4424 replace_nestloop_params(root, (Node *) joinclauses);
4425 otherclauses = (List *)
4427 }
4428
4429 /*
4430 * Rearrange mergeclauses, if needed, so that the outer variable is always
4431 * on the left; mark the mergeclause restrictinfos with correct
4432 * outer_is_left status.
4433 */
4434 mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
4435 best_path->jpath.outerjoinpath->parent->relids);
4436
4437 /* Identify any outer joins computed at this level */
4438 ojrelids = bms_difference(best_path->jpath.path.parent->relids,
4439 bms_union(outer_path->parent->relids,
4440 inner_path->parent->relids));
4441
4442 /*
4443 * Create explicit sort nodes for the outer and inner paths if necessary.
4444 */
4445 if (best_path->outersortkeys)
4446 {
4447 Relids outer_relids = outer_path->parent->relids;
4448 Plan *sort_plan;
4449
4450 /*
4451 * We can assert that the outer path is not already ordered
4452 * appropriately for the mergejoin; otherwise, outersortkeys would
4453 * have been set to NIL.
4454 */
4455 Assert(!pathkeys_contained_in(best_path->outersortkeys,
4456 outer_path->pathkeys));
4457
4458 /*
4459 * We choose to use incremental sort if it is enabled and there are
4460 * presorted keys; otherwise we use full sort.
4461 */
4462 if (enable_incremental_sort && best_path->outer_presorted_keys > 0)
4463 {
4464 sort_plan = (Plan *)
4466 best_path->outersortkeys,
4467 outer_relids,
4468 best_path->outer_presorted_keys);
4469
4472 best_path->outersortkeys,
4473 -1.0);
4474 }
4475 else
4476 {
4477 sort_plan = (Plan *)
4478 make_sort_from_pathkeys(outer_plan,
4479 best_path->outersortkeys,
4480 outer_relids);
4481
4483 }
4484
4485 outer_plan = sort_plan;
4486 outerpathkeys = best_path->outersortkeys;
4487 }
4488 else
4489 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
4490
4491 if (best_path->innersortkeys)
4492 {
4493 /*
4494 * We do not consider incremental sort for inner path, because
4495 * incremental sort does not support mark/restore.
4496 */
4497
4498 Relids inner_relids = inner_path->parent->relids;
4499 Sort *sort;
4500
4501 /*
4502 * We can assert that the inner path is not already ordered
4503 * appropriately for the mergejoin; otherwise, innersortkeys would
4504 * have been set to NIL.
4505 */
4506 Assert(!pathkeys_contained_in(best_path->innersortkeys,
4507 inner_path->pathkeys));
4508
4509 sort = make_sort_from_pathkeys(inner_plan,
4510 best_path->innersortkeys,
4511 inner_relids);
4512
4514 inner_plan = (Plan *) sort;
4515 innerpathkeys = best_path->innersortkeys;
4516 }
4517 else
4518 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
4519
4520 /*
4521 * If specified, add a materialize node to shield the inner plan from the
4522 * need to handle mark/restore.
4523 */
4524 if (best_path->materialize_inner)
4525 {
4526 Plan *matplan = (Plan *) make_material(inner_plan);
4527
4528 /*
4529 * We assume the materialize will not spill to disk, and therefore
4530 * charge just cpu_operator_cost per tuple. (Keep this estimate in
4531 * sync with final_cost_mergejoin.)
4532 */
4533 copy_plan_costsize(matplan, inner_plan);
4534 matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
4535
4536 inner_plan = matplan;
4537 }
4538
4539 /*
4540 * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
4541 * executor. The information is in the pathkeys for the two inputs, but
4542 * we need to be careful about the possibility of mergeclauses sharing a
4543 * pathkey, as well as the possibility that the inner pathkeys are not in
4544 * an order matching the mergeclauses.
4545 */
4546 nClauses = list_length(mergeclauses);
4547 Assert(nClauses == list_length(best_path->path_mergeclauses));
4548 mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
4549 mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
4550 mergereversals = (bool *) palloc(nClauses * sizeof(bool));
4551 mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
4552
4553 opathkey = NULL;
4554 opeclass = NULL;
4557 i = 0;
4558 foreach(lc, best_path->path_mergeclauses)
4559 {
4565 bool first_inner_match = false;
4566
4567 /* fetch outer/inner eclass from mergeclause */
4568 if (rinfo->outer_is_left)
4569 {
4570 oeclass = rinfo->left_ec;
4571 ieclass = rinfo->right_ec;
4572 }
4573 else
4574 {
4575 oeclass = rinfo->right_ec;
4576 ieclass = rinfo->left_ec;
4577 }
4578 Assert(oeclass != NULL);
4579 Assert(ieclass != NULL);
4580
4581 /*
4582 * We must identify the pathkey elements associated with this clause
4583 * by matching the eclasses (which should give a unique match, since
4584 * the pathkey lists should be canonical). In typical cases the merge
4585 * clauses are one-to-one with the pathkeys, but when dealing with
4586 * partially redundant query conditions, things are more complicated.
4587 *
4588 * lop and lip reference the first as-yet-unmatched pathkey elements.
4589 * If they're NULL then all pathkey elements have been matched.
4590 *
4591 * The ordering of the outer pathkeys should match the mergeclauses,
4592 * by construction (see find_mergeclauses_for_outer_pathkeys()). There
4593 * could be more than one mergeclause for the same outer pathkey, but
4594 * no pathkey may be entirely skipped over.
4595 */
4596 if (oeclass != opeclass) /* multiple matches are not interesting */
4597 {
4598 /* doesn't match the current opathkey, so must match the next */
4599 if (lop == NULL)
4600 elog(ERROR, "outer pathkeys do not match mergeclauses");
4601 opathkey = (PathKey *) lfirst(lop);
4602 opeclass = opathkey->pk_eclass;
4604 if (oeclass != opeclass)
4605 elog(ERROR, "outer pathkeys do not match mergeclauses");
4606 }
4607
4608 /*
4609 * The inner pathkeys likewise should not have skipped-over keys, but
4610 * it's possible for a mergeclause to reference some earlier inner
4611 * pathkey if we had redundant pathkeys. For example we might have
4612 * mergeclauses like "o.a = i.x AND o.b = i.y AND o.c = i.x". The
4613 * implied inner ordering is then "ORDER BY x, y, x", but the pathkey
4614 * mechanism drops the second sort by x as redundant, and this code
4615 * must cope.
4616 *
4617 * It's also possible for the implied inner-rel ordering to be like
4618 * "ORDER BY x, y, x DESC". We still drop the second instance of x as
4619 * redundant; but this means that the sort ordering of a redundant
4620 * inner pathkey should not be considered significant. So we must
4621 * detect whether this is the first clause matching an inner pathkey.
4622 */
4623 if (lip)
4624 {
4625 ipathkey = (PathKey *) lfirst(lip);
4626 ipeclass = ipathkey->pk_eclass;
4627 if (ieclass == ipeclass)
4628 {
4629 /* successful first match to this inner pathkey */
4631 first_inner_match = true;
4632 }
4633 }
4634 if (!first_inner_match)
4635 {
4636 /* redundant clause ... must match something before lip */
4637 ListCell *l2;
4638
4639 foreach(l2, innerpathkeys)
4640 {
4641 if (l2 == lip)
4642 break;
4643 ipathkey = (PathKey *) lfirst(l2);
4644 ipeclass = ipathkey->pk_eclass;
4645 if (ieclass == ipeclass)
4646 break;
4647 }
4648 if (ieclass != ipeclass)
4649 elog(ERROR, "inner pathkeys do not match mergeclauses");
4650 }
4651
4652 /*
4653 * The pathkeys should always match each other as to opfamily and
4654 * collation (which affect equality), but if we're considering a
4655 * redundant inner pathkey, its sort ordering might not match. In
4656 * such cases we may ignore the inner pathkey's sort ordering and use
4657 * the outer's. (In effect, we're lying to the executor about the
4658 * sort direction of this inner column, but it does not matter since
4659 * the run-time row comparisons would only reach this column when
4660 * there's equality for the earlier column containing the same eclass.
4661 * There could be only one value in this column for the range of inner
4662 * rows having a given value in the earlier column, so it does not
4663 * matter which way we imagine this column to be ordered.) But a
4664 * non-redundant inner pathkey had better match outer's ordering too.
4665 */
4666 if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
4667 opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation)
4668 elog(ERROR, "left and right pathkeys do not match in mergejoin");
4669 if (first_inner_match &&
4670 (opathkey->pk_cmptype != ipathkey->pk_cmptype ||
4671 opathkey->pk_nulls_first != ipathkey->pk_nulls_first))
4672 elog(ERROR, "left and right pathkeys do not match in mergejoin");
4673
4674 /* OK, save info for executor */
4675 mergefamilies[i] = opathkey->pk_opfamily;
4676 mergecollations[i] = opathkey->pk_eclass->ec_collation;
4677 mergereversals[i] = (opathkey->pk_cmptype == COMPARE_GT ? true : false);
4678 mergenullsfirst[i] = opathkey->pk_nulls_first;
4679 i++;
4680 }
4681
4682 /*
4683 * Note: it is not an error if we have additional pathkey elements (i.e.,
4684 * lop or lip isn't NULL here). The input paths might be better-sorted
4685 * than we need for the current mergejoin.
4686 */
4687
4688 /*
4689 * Now we can build the mergejoin node.
4690 */
4691 join_plan = make_mergejoin(tlist,
4692 joinclauses,
4694 mergeclauses,
4699 outer_plan,
4700 inner_plan,
4701 best_path->jpath.jointype,
4702 ojrelids,
4703 best_path->jpath.inner_unique,
4704 best_path->skip_mark_restore);
4705
4706 /* Costs of sort and material steps are included in path cost already */
4707 copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4708
4709 return join_plan;
4710}
4711
4712static HashJoin *
4715{
4717 Hash *hash_plan;
4718 Plan *outer_plan;
4719 Plan *inner_plan;
4720 Relids ojrelids;
4721 List *tlist = build_path_tlist(root, &best_path->jpath.path);
4722 List *joinclauses;
4724 List *hashclauses;
4725 List *hashoperators = NIL;
4726 List *hashcollations = NIL;
4729 Oid skewTable = InvalidOid;
4730 AttrNumber skewColumn = InvalidAttrNumber;
4731 bool skewInherit = false;
4732 ListCell *lc;
4733
4734 /*
4735 * HashJoin can project, so we don't have to demand exact tlists from the
4736 * inputs. However, it's best to request a small tlist from the inner
4737 * side, so that we aren't storing more data than necessary. Likewise, if
4738 * we anticipate batching, request a small tlist from the outer side so
4739 * that we don't put extra data in the outer batch files.
4740 */
4741 outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4742 (best_path->num_batches > 1) ? CP_SMALL_TLIST : 0);
4743
4744 inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4746
4747 /* Sort join qual clauses into best execution order */
4748 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4749 /* There's no point in sorting the hash clauses ... */
4750
4751 /* Get the join qual clauses (in plain expression form) */
4752 /* Any pseudoconstant clauses are ignored here */
4753 if (IS_OUTER_JOIN(best_path->jpath.jointype))
4754 {
4755 extract_actual_join_clauses(joinclauses,
4756 best_path->jpath.path.parent->relids,
4757 &joinclauses, &otherclauses);
4758 }
4759 else
4760 {
4761 /* We can treat all clauses alike for an inner join */
4762 joinclauses = extract_actual_clauses(joinclauses, false);
4763 otherclauses = NIL;
4764 }
4765
4766 /*
4767 * Remove the hashclauses from the list of join qual clauses, leaving the
4768 * list of quals that must be checked as qpquals.
4769 */
4770 hashclauses = get_actual_clauses(best_path->path_hashclauses);
4771 joinclauses = list_difference(joinclauses, hashclauses);
4772
4773 /*
4774 * Replace any outer-relation variables with nestloop params. There
4775 * should not be any in the hashclauses.
4776 */
4777 if (best_path->jpath.path.param_info)
4778 {
4779 joinclauses = (List *)
4780 replace_nestloop_params(root, (Node *) joinclauses);
4781 otherclauses = (List *)
4783 }
4784
4785 /*
4786 * Rearrange hashclauses, if needed, so that the outer variable is always
4787 * on the left.
4788 */
4789 hashclauses = get_switched_clauses(best_path->path_hashclauses,
4790 best_path->jpath.outerjoinpath->parent->relids);
4791
4792 /*
4793 * If there is a single join clause and we can identify the outer variable
4794 * as a simple column reference, supply its identity for possible use in
4795 * skew optimization. (Note: in principle we could do skew optimization
4796 * with multiple join clauses, but we'd have to be able to determine the
4797 * most common combinations of outer values, which we don't currently have
4798 * enough stats for.)
4799 */
4800 if (list_length(hashclauses) == 1)
4801 {
4802 OpExpr *clause = (OpExpr *) linitial(hashclauses);
4803 Node *node;
4804
4805 Assert(is_opclause(clause));
4806 node = (Node *) linitial(clause->args);
4807 if (IsA(node, RelabelType))
4808 node = (Node *) ((RelabelType *) node)->arg;
4809 if (IsA(node, Var))
4810 {
4811 Var *var = (Var *) node;
4813
4814 rte = root->simple_rte_array[var->varno];
4815 if (rte->rtekind == RTE_RELATION)
4816 {
4817 skewTable = rte->relid;
4818 skewColumn = var->varattno;
4819 skewInherit = rte->inh;
4820 }
4821 }
4822 }
4823
4824 /*
4825 * Collect hash related information. The hashed expressions are
4826 * deconstructed into outer/inner expressions, so they can be computed
4827 * separately (inner expressions are used to build the hashtable via Hash,
4828 * outer expressions to perform lookups of tuples from HashJoin's outer
4829 * plan in the hashtable). Also collect operator information necessary to
4830 * build the hashtable.
4831 */
4832 foreach(lc, hashclauses)
4833 {
4835
4836 hashoperators = lappend_oid(hashoperators, hclause->opno);
4837 hashcollations = lappend_oid(hashcollations, hclause->inputcollid);
4840 }
4841
4842 /*
4843 * Build the hash node and hash join node.
4844 */
4845 hash_plan = make_hash(inner_plan,
4847 skewTable,
4848 skewColumn,
4849 skewInherit);
4850
4851 /*
4852 * Set Hash node's startup & total costs equal to total cost of input
4853 * plan; this only affects EXPLAIN display not decisions.
4854 */
4855 copy_plan_costsize(&hash_plan->plan, inner_plan);
4856 hash_plan->plan.startup_cost = hash_plan->plan.total_cost;
4857
4858 /*
4859 * If parallel-aware, the executor will also need an estimate of the total
4860 * number of rows expected from all participants so that it can size the
4861 * shared hash table.
4862 */
4863 if (best_path->jpath.path.parallel_aware)
4864 {
4865 hash_plan->plan.parallel_aware = true;
4866 hash_plan->rows_total = best_path->inner_rows_total;
4867 }
4868
4869 /* Identify any outer joins computed at this level */
4870 ojrelids = bms_difference(best_path->jpath.path.parent->relids,
4871 bms_union(best_path->jpath.outerjoinpath->parent->relids,
4872 best_path->jpath.innerjoinpath->parent->relids));
4873
4874 join_plan = make_hashjoin(tlist,
4875 joinclauses,
4877 hashclauses,
4878 hashoperators,
4879 hashcollations,
4881 outer_plan,
4882 (Plan *) hash_plan,
4883 best_path->jpath.jointype,
4884 ojrelids,
4885 best_path->jpath.inner_unique);
4886
4887 copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4888
4889 return join_plan;
4890}
4891
4892
4893/*****************************************************************************
4894 *
4895 * SUPPORTING ROUTINES
4896 *
4897 *****************************************************************************/
4898
4899/*
4900 * replace_nestloop_params
4901 * Replace outer-relation Vars and PlaceHolderVars in the given expression
4902 * with nestloop Params
4903 *
4904 * All Vars and PlaceHolderVars belonging to the relation(s) identified by
4905 * root->curOuterRels are replaced by Params, and entries are added to
4906 * root->curOuterParams if not already present.
4907 */
4908static Node *
4910{
4911 /* No setup needed for tree walk, so away we go */
4913}
4914
4915static Node *
4917{
4918 if (node == NULL)
4919 return NULL;
4920 if (IsA(node, Var))
4921 {
4922 Var *var = (Var *) node;
4923
4924 /* Upper-level Vars should be long gone at this point */
4925 Assert(var->varlevelsup == 0);
4926 /* If not to be replaced, we can just return the Var unmodified */
4927 if (IS_SPECIAL_VARNO(var->varno) ||
4928 !bms_is_member(var->varno, root->curOuterRels))
4929 return node;
4930 /* Replace the Var with a nestloop Param */
4931 return (Node *) replace_nestloop_param_var(root, var);
4932 }
4933 if (IsA(node, PlaceHolderVar))
4934 {
4935 PlaceHolderVar *phv = (PlaceHolderVar *) node;
4936
4937 /* Upper-level PlaceHolderVars should be long gone at this point */
4938 Assert(phv->phlevelsup == 0);
4939
4940 /* Check whether we need to replace the PHV */
4941 if (!bms_is_subset(find_placeholder_info(root, phv)->ph_eval_at,
4942 root->curOuterRels))
4943 {
4944 /*
4945 * We can't replace the whole PHV, but we might still need to
4946 * replace Vars or PHVs within its expression, in case it ends up
4947 * actually getting evaluated here. (It might get evaluated in
4948 * this plan node, or some child node; in the latter case we don't
4949 * really need to process the expression here, but we haven't got
4950 * enough info to tell if that's the case.) Flat-copy the PHV
4951 * node and then recurse on its expression.
4952 *
4953 * Note that after doing this, we might have different
4954 * representations of the contents of the same PHV in different
4955 * parts of the plan tree. This is OK because equal() will just
4956 * match on phid/phlevelsup, so setrefs.c will still recognize an
4957 * upper-level reference to a lower-level copy of the same PHV.
4958 */
4960
4961 memcpy(newphv, phv, sizeof(PlaceHolderVar));
4962 newphv->phexpr = (Expr *)
4964 root);
4965 return (Node *) newphv;
4966 }
4967 /* Replace the PlaceHolderVar with a nestloop Param */
4969 }
4971}
4972
4973/*
4974 * fix_indexqual_references
4975 * Adjust indexqual clauses to the form the executor's indexqual
4976 * machinery needs.
4977 *
4978 * We have three tasks here:
4979 * * Select the actual qual clauses out of the input IndexClause list,
4980 * and remove RestrictInfo nodes from the qual clauses.
4981 * * Replace any outer-relation Var or PHV nodes with nestloop Params.
4982 * (XXX eventually, that responsibility should go elsewhere?)
4983 * * Index keys must be represented by Var nodes with varattno set to the
4984 * index's attribute number, not the attribute number in the original rel.
4985 *
4986 * *stripped_indexquals_p receives a list of the actual qual clauses.
4987 *
4988 * *fixed_indexquals_p receives a list of the adjusted quals. This is a copy
4989 * that shares no substructure with the original; this is needed in case there
4990 * are subplans in it (we need two separate copies of the subplan tree, or
4991 * things will go awry).
4992 */
4993static void
4996{
4997 IndexOptInfo *index = index_path->indexinfo;
5000 ListCell *lc;
5001
5003
5004 foreach(lc, index_path->indexclauses)
5005 {
5007 int indexcol = iclause->indexcol;
5008 ListCell *lc2;
5009
5010 foreach(lc2, iclause->indexquals)
5011 {
5013 Node *clause = (Node *) rinfo->clause;
5014
5016 clause = fix_indexqual_clause(root, index, indexcol,
5017 clause, iclause->indexcols);
5019 }
5020 }
5021
5024}
5025
5026/*
5027 * fix_indexorderby_references
5028 * Adjust indexorderby clauses to the form the executor's index
5029 * machinery needs.
5030 *
5031 * This is a simplified version of fix_indexqual_references. The input is
5032 * bare clauses and a separate indexcol list, instead of IndexClauses.
5033 */
5034static List *
5036{
5037 IndexOptInfo *index = index_path->indexinfo;
5039 ListCell *lcc,
5040 *lci;
5041
5043
5044 forboth(lcc, index_path->indexorderbys, lci, index_path->indexorderbycols)
5045 {
5046 Node *clause = (Node *) lfirst(lcc);
5047 int indexcol = lfirst_int(lci);
5048
5049 clause = fix_indexqual_clause(root, index, indexcol, clause, NIL);
5051 }
5052
5053 return fixed_indexorderbys;
5054}
5055
5056/*
5057 * fix_indexqual_clause
5058 * Convert a single indexqual clause to the form needed by the executor.
5059 *
5060 * We replace nestloop params here, and replace the index key variables
5061 * or expressions by index Var nodes.
5062 */
5063static Node *
5065 Node *clause, List *indexcolnos)
5066{
5067 /*
5068 * Replace any outer-relation variables with nestloop params.
5069 *
5070 * This also makes a copy of the clause, so it's safe to modify it
5071 * in-place below.
5072 */
5073 clause = replace_nestloop_params(root, clause);
5074
5075 if (IsA(clause, OpExpr))
5076 {
5077 OpExpr *op = (OpExpr *) clause;
5078
5079 /* Replace the indexkey expression with an index Var. */
5081 index,
5082 indexcol);
5083 }
5084 else if (IsA(clause, RowCompareExpr))
5085 {
5086 RowCompareExpr *rc = (RowCompareExpr *) clause;
5087 ListCell *lca,
5088 *lcai;
5089
5090 /* Replace the indexkey expressions with index Vars. */
5093 {
5095 index,
5096 lfirst_int(lcai));
5097 }
5098 }
5099 else if (IsA(clause, ScalarArrayOpExpr))
5100 {
5101 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
5102
5103 /* Replace the indexkey expression with an index Var. */
5105 index,
5106 indexcol);
5107 }
5108 else if (IsA(clause, NullTest))
5109 {
5110 NullTest *nt = (NullTest *) clause;
5111
5112 /* Replace the indexkey expression with an index Var. */
5113 nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
5114 index,
5115 indexcol);
5116 }
5117 else
5118 elog(ERROR, "unsupported indexqual type: %d",
5119 (int) nodeTag(clause));
5120
5121 return clause;
5122}
5123
5124/*
5125 * fix_indexqual_operand
5126 * Convert an indexqual expression to a Var referencing the index column.
5127 *
5128 * We represent index keys by Var nodes having varno == INDEX_VAR and varattno
5129 * equal to the index's attribute number (index column position).
5130 *
5131 * Most of the code here is just for sanity cross-checking that the given
5132 * expression actually matches the index column it's claimed to. It should
5133 * match the logic in match_index_to_operand().
5134 */
5135static Node *
5137{
5138 Var *result;
5139 int pos;
5141
5142 Assert(indexcol >= 0 && indexcol < index->ncolumns);
5143
5144 /*
5145 * Remove any PlaceHolderVar wrapping of the indexkey
5146 */
5147 node = strip_noop_phvs(node);
5148
5149 /*
5150 * Remove any binary-compatible relabeling of the indexkey
5151 */
5152 while (IsA(node, RelabelType))
5153 node = (Node *) ((RelabelType *) node)->arg;
5154
5155 if (index->indexkeys[indexcol] != 0)
5156 {
5157 /* It's a simple index column */
5158 if (IsA(node, Var) &&
5159 ((Var *) node)->varno == index->rel->relid &&
5160 ((Var *) node)->varattno == index->indexkeys[indexcol])
5161 {
5162 result = (Var *) copyObject(node);
5163 result->varno = INDEX_VAR;
5164 result->varattno = indexcol + 1;
5165 return (Node *) result;
5166 }
5167 else
5168 elog(ERROR, "index key does not match expected index column");
5169 }
5170
5171 /* It's an index expression, so find and cross-check the expression */
5172 indexpr_item = list_head(index->indexprs);
5173 for (pos = 0; pos < index->ncolumns; pos++)
5174 {
5175 if (index->indexkeys[pos] == 0)
5176 {
5177 if (indexpr_item == NULL)
5178 elog(ERROR, "too few entries in indexprs list");
5179 if (pos == indexcol)
5180 {
5181 Node *indexkey;
5182
5185 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
5186 if (equal(node, indexkey))
5187 {
5188 result = makeVar(INDEX_VAR, indexcol + 1,
5191 0);
5192 return (Node *) result;
5193 }
5194 else
5195 elog(ERROR, "index key does not match expected index column");
5196 }
5197 indexpr_item = lnext(index->indexprs, indexpr_item);
5198 }
5199 }
5200
5201 /* Oops... */
5202 elog(ERROR, "index key does not match expected index column");
5203 return NULL; /* keep compiler quiet */
5204}
5205
5206/*
5207 * get_switched_clauses
5208 * Given a list of merge or hash joinclauses (as RestrictInfo nodes),
5209 * extract the bare clauses, and rearrange the elements within the
5210 * clauses, if needed, so the outer join variable is on the left and
5211 * the inner is on the right. The original clause data structure is not
5212 * touched; a modified list is returned. We do, however, set the transient
5213 * outer_is_left field in each RestrictInfo to show which side was which.
5214 */
5215static List *
5216get_switched_clauses(List *clauses, Relids outerrelids)
5217{
5218 List *t_list = NIL;
5219 ListCell *l;
5220
5221 foreach(l, clauses)
5222 {
5224 OpExpr *clause = (OpExpr *) restrictinfo->clause;
5225
5226 Assert(is_opclause(clause));
5227 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
5228 {
5229 /*
5230 * Duplicate just enough of the structure to allow commuting the
5231 * clause without changing the original list. Could use
5232 * copyObject, but a complete deep copy is overkill.
5233 */
5235
5236 temp->opno = clause->opno;
5237 temp->opfuncid = InvalidOid;
5238 temp->opresulttype = clause->opresulttype;
5239 temp->opretset = clause->opretset;
5240 temp->opcollid = clause->opcollid;
5241 temp->inputcollid = clause->inputcollid;
5242 temp->args = list_copy(clause->args);
5243 temp->location = clause->location;
5244 /* Commute it --- note this modifies the temp node in-place. */
5247 restrictinfo->outer_is_left = false;
5248 }
5249 else
5250 {
5251 Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
5252 t_list = lappend(t_list, clause);
5253 restrictinfo->outer_is_left = true;
5254 }
5255 }
5256 return t_list;
5257}
5258
5259/*
5260 * order_qual_clauses
5261 * Given a list of qual clauses that will all be evaluated at the same
5262 * plan node, sort the list into the order we want to check the quals
5263 * in at runtime.
5264 *
5265 * When security barrier quals are used in the query, we may have quals with
5266 * different security levels in the list. Quals of lower security_level
5267 * must go before quals of higher security_level, except that we can grant
5268 * exceptions to move up quals that are leakproof. When security level
5269 * doesn't force the decision, we prefer to order clauses by estimated
5270 * execution cost, cheapest first.
5271 *
5272 * Ideally the order should be driven by a combination of execution cost and
5273 * selectivity, but it's not immediately clear how to account for both,
5274 * and given the uncertainty of the estimates the reliability of the decisions
5275 * would be doubtful anyway. So we just order by security level then
5276 * estimated per-tuple cost, being careful not to change the order when
5277 * (as is often the case) the estimates are identical.
5278 *
5279 * Although this will work on either bare clauses or RestrictInfos, it's
5280 * much faster to apply it to RestrictInfos, since it can re-use cost
5281 * information that is cached in RestrictInfos. XXX in the bare-clause
5282 * case, we are also not able to apply security considerations. That is
5283 * all right for the moment, because the bare-clause case doesn't occur
5284 * anywhere that barrier quals could be present, but it would be better to
5285 * get rid of it.
5286 *
5287 * Note: some callers pass lists that contain entries that will later be
5288 * removed; this is the easiest way to let this routine see RestrictInfos
5289 * instead of bare clauses. This is another reason why trying to consider
5290 * selectivity in the ordering would likely do the wrong thing.
5291 */
5292static List *
5294{
5295 typedef struct
5296 {
5297 Node *clause;
5298 Cost cost;
5299 Index security_level;
5300 } QualItem;
5301 int nitems = list_length(clauses);
5302 QualItem *items;
5303 ListCell *lc;
5304 int i;
5305 List *result;
5306
5307 /* No need to work hard for 0 or 1 clause */
5308 if (nitems <= 1)
5309 return clauses;
5310
5311 /*
5312 * Collect the items and costs into an array. This is to avoid repeated
5313 * cost_qual_eval work if the inputs aren't RestrictInfos.
5314 */
5315 items = (QualItem *) palloc(nitems * sizeof(QualItem));
5316 i = 0;
5317 foreach(lc, clauses)
5318 {
5319 Node *clause = (Node *) lfirst(lc);
5321
5322 cost_qual_eval_node(&qcost, clause, root);
5323 items[i].clause = clause;
5324 items[i].cost = qcost.per_tuple;
5325 if (IsA(clause, RestrictInfo))
5326 {
5327 RestrictInfo *rinfo = (RestrictInfo *) clause;
5328
5329 /*
5330 * If a clause is leakproof, it doesn't have to be constrained by
5331 * its nominal security level. If it's also reasonably cheap
5332 * (here defined as 10X cpu_operator_cost), pretend it has
5333 * security_level 0, which will allow it to go in front of
5334 * more-expensive quals of lower security levels. Of course, that
5335 * will also force it to go in front of cheaper quals of its own
5336 * security level, which is not so great, but we can alleviate
5337 * that risk by applying the cost limit cutoff.
5338 */
5339 if (rinfo->leakproof && items[i].cost < 10 * cpu_operator_cost)
5340 items[i].security_level = 0;
5341 else
5342 items[i].security_level = rinfo->security_level;
5343 }
5344 else
5345 items[i].security_level = 0;
5346 i++;
5347 }
5348
5349 /*
5350 * Sort. We don't use qsort() because it's not guaranteed stable for
5351 * equal keys. The expected number of entries is small enough that a
5352 * simple insertion sort should be good enough.
5353 */
5354 for (i = 1; i < nitems; i++)
5355 {
5356 QualItem newitem = items[i];
5357 int j;
5358
5359 /* insert newitem into the already-sorted subarray */
5360 for (j = i; j > 0; j--)
5361 {
5362 QualItem *olditem = &items[j - 1];
5363
5364 if (newitem.security_level > olditem->security_level ||
5365 (newitem.security_level == olditem->security_level &&
5366 newitem.cost >= olditem->cost))
5367 break;
5368 items[j] = *olditem;
5369 }
5370 items[j] = newitem;
5371 }
5372
5373 /* Convert back to a list */
5374 result = NIL;
5375 for (i = 0; i < nitems; i++)
5376 result = lappend(result, items[i].clause);
5377
5378 return result;
5379}
5380
5381/*
5382 * Copy cost and size info from a Path node to the Plan node created from it.
5383 * The executor usually won't use this info, but it's needed by EXPLAIN.
5384 * Also copy the parallel-related flags, which the executor *will* use.
5385 */
5386static void
5388{
5389 dest->disabled_nodes = src->disabled_nodes;
5390 dest->startup_cost = src->startup_cost;
5391 dest->total_cost = src->total_cost;
5392 dest->plan_rows = src->rows;
5393 dest->plan_width = src->pathtarget->width;
5394 dest->parallel_aware = src->parallel_aware;
5395 dest->parallel_safe = src->parallel_safe;
5396}
5397
5398/*
5399 * Copy cost and size info from a lower plan node to an inserted node.
5400 * (Most callers alter the info after copying it.)
5401 */
5402static void
5404{
5405 dest->disabled_nodes = src->disabled_nodes;
5406 dest->startup_cost = src->startup_cost;
5407 dest->total_cost = src->total_cost;
5408 dest->plan_rows = src->plan_rows;
5409 dest->plan_width = src->plan_width;
5410 /* Assume the inserted node is not parallel-aware. */
5411 dest->parallel_aware = false;
5412 /* Assume the inserted node is parallel-safe, if child plan is. */
5413 dest->parallel_safe = src->parallel_safe;
5414}
5415
5416/*
5417 * Some places in this file build Sort nodes that don't have a directly
5418 * corresponding Path node. The cost of the sort is, or should have been,
5419 * included in the cost of the Path node we're working from, but since it's
5420 * not split out, we have to re-figure it using cost_sort(). This is just
5421 * to label the Sort node nicely for EXPLAIN.
5422 *
5423 * limit_tuples is as for cost_sort (in particular, pass -1 if no limit)
5424 */
5425static void
5427{
5428 Plan *lefttree = plan->plan.lefttree;
5429 Path sort_path; /* dummy for result of cost_sort */
5430
5431 Assert(IsA(plan, Sort));
5432
5434 plan->plan.disabled_nodes,
5435 lefttree->total_cost,
5436 lefttree->plan_rows,
5437 lefttree->plan_width,
5438 0.0,
5439 work_mem,
5440 limit_tuples);
5441 plan->plan.startup_cost = sort_path.startup_cost;
5442 plan->plan.total_cost = sort_path.total_cost;
5443 plan->plan.plan_rows = lefttree->plan_rows;
5444 plan->plan.plan_width = lefttree->plan_width;
5445 plan->plan.parallel_aware = false;
5446 plan->plan.parallel_safe = lefttree->parallel_safe;
5447}
5448
5449/*
5450 * Same as label_sort_with_costsize, but labels the IncrementalSort node
5451 * instead.
5452 */
5453static void
5455 List *pathkeys, double limit_tuples)
5456{
5457 Plan *lefttree = plan->sort.plan.lefttree;
5458 Path sort_path; /* dummy for result of cost_incremental_sort */
5459
5461
5463 plan->nPresortedCols,
5464 plan->sort.plan.disabled_nodes,
5465 lefttree->startup_cost,
5466 lefttree->total_cost,
5467 lefttree->plan_rows,
5468 lefttree->plan_width,
5469 0.0,
5470 work_mem,
5471 limit_tuples);
5472 plan->sort.plan.startup_cost = sort_path.startup_cost;
5473 plan->sort.plan.total_cost = sort_path.total_cost;
5474 plan->sort.plan.plan_rows = lefttree->plan_rows;
5475 plan->sort.plan.plan_width = lefttree->plan_width;
5476 plan->sort.plan.parallel_aware = false;
5477 plan->sort.plan.parallel_safe = lefttree->parallel_safe;
5478}
5479
5480/*
5481 * bitmap_subplan_mark_shared
5482 * Set isshared flag in bitmap subplan so that it will be created in
5483 * shared memory.
5484 */
5485static void
5487{
5488 if (IsA(plan, BitmapAnd))
5490 else if (IsA(plan, BitmapOr))
5491 {
5492 ((BitmapOr *) plan)->isshared = true;
5493 bitmap_subplan_mark_shared(linitial(((BitmapOr *) plan)->bitmapplans));
5494 }
5495 else if (IsA(plan, BitmapIndexScan))
5496 ((BitmapIndexScan *) plan)->isshared = true;
5497 else
5498 elog(ERROR, "unrecognized node type: %d", nodeTag(plan));
5499}
5500
5501/*****************************************************************************
5502 *
5503 * PLAN NODE BUILDING ROUTINES
5504 *
5505 * In general, these functions are not passed the original Path and therefore
5506 * leave it to the caller to fill in the cost/width fields from the Path,
5507 * typically by calling copy_generic_path_info(). This convention is
5508 * somewhat historical, but it does support a few places above where we build
5509 * a plan node without having an exactly corresponding Path node. Under no
5510 * circumstances should one of these functions do its own cost calculations,
5511 * as that would be redundant with calculations done while building Paths.
5512 *
5513 *****************************************************************************/
5514
5515static SeqScan *
5517 List *qpqual,
5518 Index scanrelid)
5519{
5520 SeqScan *node = makeNode(SeqScan);
5521 Plan *plan = &node->scan.plan;
5522
5523 plan->targetlist = qptlist;
5524 plan->qual = qpqual;
5525 plan->lefttree = NULL;
5526 plan->righttree = NULL;
5527 node->scan.scanrelid = scanrelid;
5528
5529 return node;
5530}
5531
5532static SampleScan *
5534 List *qpqual,
5535 Index scanrelid,
5537{
5539 Plan *plan = &node->scan.plan;
5540
5541 plan->targetlist = qptlist;
5542 plan->qual = qpqual;
5543 plan->lefttree = NULL;
5544 plan->righttree = NULL;
5545 node->scan.scanrelid = scanrelid;
5546 node->tablesample = tsc;
5547
5548 return node;
5549}
5550
5551static IndexScan *
5553 List *qpqual,
5554 Index scanrelid,
5555 Oid indexid,
5556 List *indexqual,
5557 List *indexqualorig,
5558 List *indexorderby,
5559 List *indexorderbyorig,
5560 List *indexorderbyops,
5561 ScanDirection indexscandir)
5562{
5563 IndexScan *node = makeNode(IndexScan);
5564 Plan *plan = &node->scan.plan;
5565
5566 plan->targetlist = qptlist;
5567 plan->qual = qpqual;
5568 plan->lefttree = NULL;
5569 plan->righttree = NULL;
5570 node->scan.scanrelid = scanrelid;
5571 node->indexid = indexid;
5572 node->indexqual = indexqual;
5573 node->indexqualorig = indexqualorig;
5574 node->indexorderby = indexorderby;
5575 node->indexorderbyorig = indexorderbyorig;
5576 node->indexorderbyops = indexorderbyops;
5577 node->indexorderdir = indexscandir;
5578
5579 return node;
5580}
5581
5582static IndexOnlyScan *
5584 List *qpqual,
5585 Index scanrelid,
5586 Oid indexid,
5587 List *indexqual,
5588 List *recheckqual,
5589 List *indexorderby,
5590 List *indextlist,
5591 ScanDirection indexscandir)
5592{
5594 Plan *plan = &node->scan.plan;
5595
5596 plan->targetlist = qptlist;
5597 plan->qual = qpqual;
5598 plan->lefttree = NULL;
5599 plan->righttree = NULL;
5600 node->scan.scanrelid = scanrelid;
5601 node->indexid = indexid;
5602 node->indexqual = indexqual;
5603 node->recheckqual = recheckqual;
5604 node->indexorderby = indexorderby;
5605 node->indextlist = indextlist;
5606 node->indexorderdir = indexscandir;
5607
5608 return node;
5609}
5610
5611static BitmapIndexScan *
5613 Oid indexid,
5614 List *indexqual,
5615 List *indexqualorig)
5616{
5618 Plan *plan = &node->scan.plan;
5619
5620 plan->targetlist = NIL; /* not used */
5621 plan->qual = NIL; /* not used */
5622 plan->lefttree = NULL;
5623 plan->righttree = NULL;
5624 node->scan.scanrelid = scanrelid;
5625 node->indexid = indexid;
5626 node->indexqual = indexqual;
5627 node->indexqualorig = indexqualorig;
5628
5629 return node;
5630}
5631
5632static BitmapHeapScan *
5634 List *qpqual,
5635 Plan *lefttree,
5636 List *bitmapqualorig,
5637 Index scanrelid)
5638{
5640 Plan *plan = &node->scan.plan;
5641
5642 plan->targetlist = qptlist;
5643 plan->qual = qpqual;
5644 plan->lefttree = lefttree;
5645 plan->righttree = NULL;
5646 node->scan.scanrelid = scanrelid;
5647 node->bitmapqualorig = bitmapqualorig;
5648
5649 return node;
5650}
5651
5652static TidScan *
5654 List *qpqual,
5655 Index scanrelid,
5656 List *tidquals)
5657{
5658 TidScan *node = makeNode(TidScan);
5659 Plan *plan = &node->scan.plan;
5660
5661 plan->targetlist = qptlist;
5662 plan->qual = qpqual;
5663 plan->lefttree = NULL;
5664 plan->righttree = NULL;
5665 node->scan.scanrelid = scanrelid;
5666 node->tidquals = tidquals;
5667
5668 return node;
5669}
5670
5671static TidRangeScan *
5673 List *qpqual,
5674 Index scanrelid,
5675 List *tidrangequals)
5676{
5678 Plan *plan = &node->scan.plan;
5679
5680 plan->targetlist = qptlist;
5681 plan->qual = qpqual;
5682 plan->lefttree = NULL;
5683 plan->righttree = NULL;
5684 node->scan.scanrelid = scanrelid;
5685 node->tidrangequals = tidrangequals;
5686
5687 return node;
5688}
5689
5690static SubqueryScan *
5692 List *qpqual,
5693 Index scanrelid,
5694 Plan *subplan)
5695{
5697 Plan *plan = &node->scan.plan;
5698
5699 plan->targetlist = qptlist;
5700 plan->qual = qpqual;
5701 plan->lefttree = NULL;
5702 plan->righttree = NULL;
5703 node->scan.scanrelid = scanrelid;
5704 node->subplan = subplan;
5706
5707 return node;
5708}
5709
5710static FunctionScan *
5712 List *qpqual,
5713 Index scanrelid,
5714 List *functions,
5715 bool funcordinality)
5716{
5718 Plan *plan = &node->scan.plan;
5719
5720 plan->targetlist = qptlist;
5721 plan->qual = qpqual;
5722 plan->lefttree = NULL;
5723 plan->righttree = NULL;
5724 node->scan.scanrelid = scanrelid;
5725 node->functions = functions;
5726 node->funcordinality = funcordinality;
5727
5728 return node;
5729}
5730
5731static TableFuncScan *
5733 List *qpqual,
5734 Index scanrelid,
5735 TableFunc *tablefunc)
5736{
5738 Plan *plan = &node->scan.plan;
5739
5740 plan->targetlist = qptlist;
5741 plan->qual = qpqual;
5742 plan->lefttree = NULL;
5743 plan->righttree = NULL;
5744 node->scan.scanrelid = scanrelid;
5745 node->tablefunc = tablefunc;
5746
5747 return node;
5748}
5749
5750static ValuesScan *
5752 List *qpqual,
5753 Index scanrelid,
5754 List *values_lists)
5755{
5757 Plan *plan = &node->scan.plan;
5758
5759 plan->targetlist = qptlist;
5760 plan->qual = qpqual;
5761 plan->lefttree = NULL;
5762 plan->righttree = NULL;
5763 node->scan.scanrelid = scanrelid;
5764 node->values_lists = values_lists;
5765
5766 return node;
5767}
5768
5769static CteScan *
5771 List *qpqual,
5772 Index scanrelid,
5773 int ctePlanId,
5774 int cteParam)
5775{
5776 CteScan *node = makeNode(CteScan);
5777 Plan *plan = &node->scan.plan;
5778
5779 plan->targetlist = qptlist;
5780 plan->qual = qpqual;
5781 plan->lefttree = NULL;
5782 plan->righttree = NULL;
5783 node->scan.scanrelid = scanrelid;
5784 node->ctePlanId = ctePlanId;
5785 node->cteParam = cteParam;
5786
5787 return node;
5788}
5789
5790static NamedTuplestoreScan *
5792 List *qpqual,
5793 Index scanrelid,
5794 char *enrname)
5795{
5797 Plan *plan = &node->scan.plan;
5798
5799 /* cost should be inserted by caller */
5800 plan->targetlist = qptlist;
5801 plan->qual = qpqual;
5802 plan->lefttree = NULL;
5803 plan->righttree = NULL;
5804 node->scan.scanrelid = scanrelid;
5805 node->enrname = enrname;
5806
5807 return node;
5808}
5809
5810static WorkTableScan *
5812 List *qpqual,
5813 Index scanrelid,
5814 int wtParam)
5815{
5817 Plan *plan = &node->scan.plan;
5818
5819 plan->targetlist = qptlist;
5820 plan->qual = qpqual;
5821 plan->lefttree = NULL;
5822 plan->righttree = NULL;
5823 node->scan.scanrelid = scanrelid;
5824 node->wtParam = wtParam;
5825
5826 return node;
5827}
5828
5831 List *qpqual,
5832 Index scanrelid,
5833 List *fdw_exprs,
5834 List *fdw_private,
5835 List *fdw_scan_tlist,
5836 List *fdw_recheck_quals,
5837 Plan *outer_plan)
5838{
5840 Plan *plan = &node->scan.plan;
5841
5842 /* cost will be filled in by create_foreignscan_plan */
5843 plan->targetlist = qptlist;
5844 plan->qual = qpqual;
5845 plan->lefttree = outer_plan;
5846 plan->righttree = NULL;
5847 node->scan.scanrelid = scanrelid;
5848
5849 /* these may be overridden by the FDW's PlanDirectModify callback. */
5850 node->operation = CMD_SELECT;
5851 node->resultRelation = 0;
5852
5853 /* checkAsUser, fs_server will be filled in by create_foreignscan_plan */
5854 node->checkAsUser = InvalidOid;
5855 node->fs_server = InvalidOid;
5856 node->fdw_exprs = fdw_exprs;
5857 node->fdw_private = fdw_private;
5858 node->fdw_scan_tlist = fdw_scan_tlist;
5859 node->fdw_recheck_quals = fdw_recheck_quals;
5860 /* fs_relids, fs_base_relids will be filled by create_foreignscan_plan */
5861 node->fs_relids = NULL;
5862 node->fs_base_relids = NULL;
5863 /* fsSystemCol will be filled in by create_foreignscan_plan */
5864 node->fsSystemCol = false;
5865
5866 return node;
5867}
5868
5869static RecursiveUnion *
5871 Plan *lefttree,
5872 Plan *righttree,
5873 int wtParam,
5874 List *distinctList,
5875 Cardinality numGroups)
5876{
5878 Plan *plan = &node->plan;
5879 int numCols = list_length(distinctList);
5880
5881 plan->targetlist = tlist;
5882 plan->qual = NIL;
5883 plan->lefttree = lefttree;
5884 plan->righttree = righttree;
5885 node->wtParam = wtParam;
5886
5887 /*
5888 * convert SortGroupClause list into arrays of attr indexes and equality
5889 * operators, as wanted by executor
5890 */
5891 node->numCols = numCols;
5892 if (numCols > 0)
5893 {
5894 int keyno = 0;
5899
5900 dupColIdx = palloc_array(AttrNumber, numCols);
5901 dupOperators = palloc_array(Oid, numCols);
5902 dupCollations = palloc_array(Oid, numCols);
5903
5904 foreach(slitem, distinctList)
5905 {
5908 plan->targetlist);
5909
5910 dupColIdx[keyno] = tle->resno;
5911 dupOperators[keyno] = sortcl->eqop;
5912 dupCollations[keyno] = exprCollation((Node *) tle->expr);
5914 keyno++;
5915 }
5916 node->dupColIdx = dupColIdx;
5917 node->dupOperators = dupOperators;
5918 node->dupCollations = dupCollations;
5919 }
5920 node->numGroups = numGroups;
5921
5922 return node;
5923}
5924
5925static BitmapAnd *
5927{
5928 BitmapAnd *node = makeNode(BitmapAnd);
5929 Plan *plan = &node->plan;
5930
5931 plan->targetlist = NIL;
5932 plan->qual = NIL;
5933 plan->lefttree = NULL;
5934 plan->righttree = NULL;
5935 node->bitmapplans = bitmapplans;
5936
5937 return node;
5938}
5939
5940static BitmapOr *
5942{
5943 BitmapOr *node = makeNode(BitmapOr);
5944 Plan *plan = &node->plan;
5945
5946 plan->targetlist = NIL;
5947 plan->qual = NIL;
5948 plan->lefttree = NULL;
5949 plan->righttree = NULL;
5950 node->bitmapplans = bitmapplans;
5951
5952 return node;
5953}
5954
5955static NestLoop *
5957 List *joinclauses,
5959 List *nestParams,
5960 Plan *lefttree,
5961 Plan *righttree,
5962 JoinType jointype,
5963 Relids ojrelids,
5964 bool inner_unique)
5965{
5966 NestLoop *node = makeNode(NestLoop);
5967 Plan *plan = &node->join.plan;
5968
5969 plan->targetlist = tlist;
5970 plan->qual = otherclauses;
5971 plan->lefttree = lefttree;
5972 plan->righttree = righttree;
5973 node->join.jointype = jointype;
5974 node->join.inner_unique = inner_unique;
5975 node->join.joinqual = joinclauses;
5976 node->join.ojrelids = ojrelids;
5977 node->nestParams = nestParams;
5978
5979 return node;
5980}
5981
5982static HashJoin *
5984 List *joinclauses,
5986 List *hashclauses,
5987 List *hashoperators,
5988 List *hashcollations,
5989 List *hashkeys,
5990 Plan *lefttree,
5991 Plan *righttree,
5992 JoinType jointype,
5993 Relids ojrelids,
5994 bool inner_unique)
5995{
5996 HashJoin *node = makeNode(HashJoin);
5997 Plan *plan = &node->join.plan;
5998
5999 plan->targetlist = tlist;
6000 plan->qual = otherclauses;
6001 plan->lefttree = lefttree;
6002 plan->righttree = righttree;
6003 node->hashclauses = hashclauses;
6004 node->hashoperators = hashoperators;
6005 node->hashcollations = hashcollations;
6006 node->hashkeys = hashkeys;
6007 node->join.jointype = jointype;
6008 node->join.inner_unique = inner_unique;
6009 node->join.joinqual = joinclauses;
6010 node->join.ojrelids = ojrelids;
6011
6012 return node;
6013}
6014
6015static Hash *
6016make_hash(Plan *lefttree,
6017 List *hashkeys,
6018 Oid skewTable,
6019 AttrNumber skewColumn,
6020 bool skewInherit)
6021{
6022 Hash *node = makeNode(Hash);
6023 Plan *plan = &node->plan;
6024
6025 plan->targetlist = lefttree->targetlist;
6026 plan->qual = NIL;
6027 plan->lefttree = lefttree;
6028 plan->righttree = NULL;
6029
6030 node->hashkeys = hashkeys;
6031 node->skewTable = skewTable;
6032 node->skewColumn = skewColumn;
6033 node->skewInherit = skewInherit;
6034
6035 return node;
6036}
6037
6038static MergeJoin *
6040 List *joinclauses,
6042 List *mergeclauses,
6045 bool *mergereversals,
6046 bool *mergenullsfirst,
6047 Plan *lefttree,
6048 Plan *righttree,
6049 JoinType jointype,
6050 Relids ojrelids,
6051 bool inner_unique,
6052 bool skip_mark_restore)
6053{
6054 MergeJoin *node = makeNode(MergeJoin);
6055 Plan *plan = &node->join.plan;
6056
6057 plan->targetlist = tlist;
6058 plan->qual = otherclauses;
6059 plan->lefttree = lefttree;
6060 plan->righttree = righttree;
6061 node->skip_mark_restore = skip_mark_restore;
6062 node->mergeclauses = mergeclauses;
6063 node->mergeFamilies = mergefamilies;
6064 node->mergeCollations = mergecollations;
6065 node->mergeReversals = mergereversals;
6066 node->mergeNullsFirst = mergenullsfirst;
6067 node->join.jointype = jointype;
6068 node->join.inner_unique = inner_unique;
6069 node->join.joinqual = joinclauses;
6070 node->join.ojrelids = ojrelids;
6071
6072 return node;
6073}
6074
6075/*
6076 * make_sort --- basic routine to build a Sort plan node
6077 *
6078 * Caller must have built the sortColIdx, sortOperators, collations, and
6079 * nullsFirst arrays already.
6080 */
6081static Sort *
6082make_sort(Plan *lefttree, int numCols,
6083 AttrNumber *sortColIdx, Oid *sortOperators,
6084 Oid *collations, bool *nullsFirst)
6085{
6086 Sort *node;
6087 Plan *plan;
6088
6089 node = makeNode(Sort);
6090
6091 plan = &node->plan;
6092 plan->targetlist = lefttree->targetlist;
6093 plan->disabled_nodes = lefttree->disabled_nodes + (enable_sort == false);
6094 plan->qual = NIL;
6095 plan->lefttree = lefttree;
6096 plan->righttree = NULL;
6097 node->numCols = numCols;
6098 node->sortColIdx = sortColIdx;
6099 node->sortOperators = sortOperators;
6100 node->collations = collations;
6101 node->nullsFirst = nullsFirst;
6102
6103 return node;
6104}
6105
6106/*
6107 * make_incrementalsort --- basic routine to build an IncrementalSort plan node
6108 *
6109 * Caller must have built the sortColIdx, sortOperators, collations, and
6110 * nullsFirst arrays already.
6111 */
6112static IncrementalSort *
6113make_incrementalsort(Plan *lefttree, int numCols, int nPresortedCols,
6114 AttrNumber *sortColIdx, Oid *sortOperators,
6115 Oid *collations, bool *nullsFirst)
6116{
6117 IncrementalSort *node;
6118 Plan *plan;
6119
6120 node = makeNode(IncrementalSort);
6121
6122 plan = &node->sort.plan;
6123 plan->targetlist = lefttree->targetlist;
6124 plan->qual = NIL;
6125 plan->lefttree = lefttree;
6126 plan->righttree = NULL;
6127 node->nPresortedCols = nPresortedCols;
6128 node->sort.numCols = numCols;
6129 node->sort.sortColIdx = sortColIdx;
6130 node->sort.sortOperators = sortOperators;
6131 node->sort.collations = collations;
6132 node->sort.nullsFirst = nullsFirst;
6133
6134 return node;
6135}
6136
6137/*
6138 * prepare_sort_from_pathkeys
6139 * Prepare to sort according to given pathkeys
6140 *
6141 * This is used to set up for Sort, MergeAppend, and Gather Merge nodes. It
6142 * calculates the executor's representation of the sort key information, and
6143 * adjusts the plan targetlist if needed to add resjunk sort columns.
6144 *
6145 * Input parameters:
6146 * 'lefttree' is the plan node which yields input tuples
6147 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
6148 * 'relids' identifies the child relation being sorted, if any
6149 * 'reqColIdx' is NULL or an array of required sort key column numbers
6150 * 'adjust_tlist_in_place' is true if lefttree must be modified in-place
6151 *
6152 * We must convert the pathkey information into arrays of sort key column
6153 * numbers, sort operator OIDs, collation OIDs, and nulls-first flags,
6154 * which is the representation the executor wants. These are returned into
6155 * the output parameters *p_numsortkeys etc.
6156 *
6157 * When looking for matches to an EquivalenceClass's members, we will only
6158 * consider child EC members if they belong to given 'relids'. This protects
6159 * against possible incorrect matches to child expressions that contain no
6160 * Vars.
6161 *
6162 * If reqColIdx isn't NULL then it contains sort key column numbers that
6163 * we should match. This is used when making child plans for a MergeAppend;
6164 * it's an error if we can't match the columns.
6165 *
6166 * If the pathkeys include expressions that aren't simple Vars, we will
6167 * usually need to add resjunk items to the input plan's targetlist to
6168 * compute these expressions, since a Sort or MergeAppend node itself won't
6169 * do any such calculations. If the input plan type isn't one that can do
6170 * projections, this means adding a Result node just to do the projection.
6171 * However, the caller can pass adjust_tlist_in_place = true to force the
6172 * lefttree tlist to be modified in-place regardless of whether the node type
6173 * can project --- we use this for fixing the tlist of MergeAppend itself.
6174 *
6175 * Returns the node which is to be the input to the Sort (either lefttree,
6176 * or a Result stacked atop lefttree).
6177 */
6178static Plan *
6180 Relids relids,
6181 const AttrNumber *reqColIdx,
6183 int *p_numsortkeys,
6186 Oid **p_collations,
6187 bool **p_nullsFirst)
6188{
6189 List *tlist = lefttree->targetlist;
6190 ListCell *i;
6191 int numsortkeys;
6192 AttrNumber *sortColIdx;
6193 Oid *sortOperators;
6194 Oid *collations;
6195 bool *nullsFirst;
6196
6197 /*
6198 * We will need at most list_length(pathkeys) sort columns; possibly less
6199 */
6200 numsortkeys = list_length(pathkeys);
6201 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
6202 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
6203 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
6204 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
6205
6206 numsortkeys = 0;
6207
6208 foreach(i, pathkeys)
6209 {
6210 PathKey *pathkey = (PathKey *) lfirst(i);
6211 EquivalenceClass *ec = pathkey->pk_eclass;
6213 TargetEntry *tle = NULL;
6215 Oid sortop;
6216 ListCell *j;
6217
6218 if (ec->ec_has_volatile)
6219 {
6220 /*
6221 * If the pathkey's EquivalenceClass is volatile, then it must
6222 * have come from an ORDER BY clause, and we have to match it to
6223 * that same targetlist entry.
6224 */
6225 if (ec->ec_sortref == 0) /* can't happen */
6226 elog(ERROR, "volatile EquivalenceClass has no sortref");
6227 tle = get_sortgroupref_tle(ec->ec_sortref, tlist);
6228 Assert(tle);
6229 Assert(list_length(ec->ec_members) == 1);
6230 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
6231 }
6232 else if (reqColIdx != NULL)
6233 {
6234 /*
6235 * If we are given a sort column number to match, only consider
6236 * the single TLE at that position. It's possible that there is
6237 * no such TLE, in which case fall through and generate a resjunk
6238 * targetentry (we assume this must have happened in the parent
6239 * plan as well). If there is a TLE but it doesn't match the
6240 * pathkey's EC, we do the same, which is probably the wrong thing
6241 * but we'll leave it to caller to complain about the mismatch.
6242 */
6244 if (tle)
6245 {
6246 em = find_ec_member_matching_expr(ec, tle->expr, relids);
6247 if (em)
6248 {
6249 /* found expr at right place in tlist */
6250 pk_datatype = em->em_datatype;
6251 }
6252 else
6253 tle = NULL;
6254 }
6255 }
6256 else
6257 {
6258 /*
6259 * Otherwise, we can sort by any non-constant expression listed in
6260 * the pathkey's EquivalenceClass. For now, we take the first
6261 * tlist item found in the EC. If there's no match, we'll generate
6262 * a resjunk entry using the first EC member that is an expression
6263 * in the input's vars.
6264 *
6265 * XXX if we have a choice, is there any way of figuring out which
6266 * might be cheapest to execute? (For example, int4lt is likely
6267 * much cheaper to execute than numericlt, but both might appear
6268 * in the same equivalence class...) Not clear that we ever will
6269 * have an interesting choice in practice, so it may not matter.
6270 */
6271 foreach(j, tlist)
6272 {
6273 tle = (TargetEntry *) lfirst(j);
6274 em = find_ec_member_matching_expr(ec, tle->expr, relids);
6275 if (em)
6276 {
6277 /* found expr already in tlist */
6278 pk_datatype = em->em_datatype;
6279 break;
6280 }
6281 tle = NULL;
6282 }
6283 }
6284
6285 if (!tle)
6286 {
6287 /*
6288 * No matching tlist item; look for a computable expression.
6289 */
6290 em = find_computable_ec_member(NULL, ec, tlist, relids, false);
6291 if (!em)
6292 elog(ERROR, "could not find pathkey item to sort");
6293 pk_datatype = em->em_datatype;
6294
6295 /*
6296 * Do we need to insert a Result node?
6297 */
6298 if (!adjust_tlist_in_place &&
6299 !is_projection_capable_plan(lefttree))
6300 {
6301 /* copy needed so we don't modify input's tlist below */
6302 tlist = copyObject(tlist);
6303 lefttree = inject_projection_plan(lefttree, tlist,
6304 lefttree->parallel_safe);
6305 }
6306
6307 /* Don't bother testing is_projection_capable_plan again */
6308 adjust_tlist_in_place = true;
6309
6310 /*
6311 * Add resjunk entry to input's tlist
6312 */
6313 tle = makeTargetEntry(copyObject(em->em_expr),
6314 list_length(tlist) + 1,
6315 NULL,
6316 true);
6317 tlist = lappend(tlist, tle);
6318 lefttree->targetlist = tlist; /* just in case NIL before */
6319 }
6320
6321 /*
6322 * Look up the correct sort operator from the PathKey's slightly
6323 * abstracted representation.
6324 */
6325 sortop = get_opfamily_member_for_cmptype(pathkey->pk_opfamily,
6328 pathkey->pk_cmptype);
6329 if (!OidIsValid(sortop)) /* should not happen */
6330 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
6331 pathkey->pk_cmptype, pk_datatype, pk_datatype,
6332 pathkey->pk_opfamily);
6333
6334 /* Add the column to the sort arrays */
6335 sortColIdx[numsortkeys] = tle->resno;
6336 sortOperators[numsortkeys] = sortop;
6337 collations[numsortkeys] = ec->ec_collation;
6338 nullsFirst[numsortkeys] = pathkey->pk_nulls_first;
6339 numsortkeys++;
6340 }
6341
6342 /* Return results */
6344 *p_sortColIdx = sortColIdx;
6345 *p_sortOperators = sortOperators;
6346 *p_collations = collations;
6348
6349 return lefttree;
6350}
6351
6352/*
6353 * make_sort_from_pathkeys
6354 * Create sort plan to sort according to given pathkeys
6355 *
6356 * 'lefttree' is the node which yields input tuples
6357 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
6358 * 'relids' is the set of relations required by prepare_sort_from_pathkeys()
6359 */
6360static Sort *
6361make_sort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids)
6362{
6363 int numsortkeys;
6364 AttrNumber *sortColIdx;
6365 Oid *sortOperators;
6366 Oid *collations;
6367 bool *nullsFirst;
6368
6369 /* Compute sort column info, and adjust lefttree as needed */
6370 lefttree = prepare_sort_from_pathkeys(lefttree, pathkeys,
6371 relids,
6372 NULL,
6373 false,
6374 &numsortkeys,
6375 &sortColIdx,
6376 &sortOperators,
6377 &collations,
6378 &nullsFirst);
6379
6380 /* Now build the Sort node */
6381 return make_sort(lefttree, numsortkeys,
6382 sortColIdx, sortOperators,
6383 collations, nullsFirst);
6384}
6385
6386/*
6387 * make_incrementalsort_from_pathkeys
6388 * Create sort plan to sort according to given pathkeys
6389 *
6390 * 'lefttree' is the node which yields input tuples
6391 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
6392 * 'relids' is the set of relations required by prepare_sort_from_pathkeys()
6393 * 'nPresortedCols' is the number of presorted columns in input tuples
6394 */
6395static IncrementalSort *
6397 Relids relids, int nPresortedCols)
6398{
6399 int numsortkeys;
6400 AttrNumber *sortColIdx;
6401 Oid *sortOperators;
6402 Oid *collations;
6403 bool *nullsFirst;
6404
6405 /* Compute sort column info, and adjust lefttree as needed */
6406 lefttree = prepare_sort_from_pathkeys(lefttree, pathkeys,
6407 relids,
6408 NULL,
6409 false,
6410 &numsortkeys,
6411 &sortColIdx,
6412 &sortOperators,
6413 &collations,
6414 &nullsFirst);
6415
6416 /* Now build the Sort node */
6417 return make_incrementalsort(lefttree, numsortkeys, nPresortedCols,
6418 sortColIdx, sortOperators,
6419 collations, nullsFirst);
6420}
6421
6422/*
6423 * make_sort_from_sortclauses
6424 * Create sort plan to sort according to given sortclauses
6425 *
6426 * 'sortcls' is a list of SortGroupClauses
6427 * 'lefttree' is the node which yields input tuples
6428 */
6429Sort *
6431{
6432 List *sub_tlist = lefttree->targetlist;
6433 ListCell *l;
6434 int numsortkeys;
6435 AttrNumber *sortColIdx;
6436 Oid *sortOperators;
6437 Oid *collations;
6438 bool *nullsFirst;
6439
6440 /* Convert list-ish representation to arrays wanted by executor */
6442 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
6443 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
6444 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
6445 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
6446
6447 numsortkeys = 0;
6448 foreach(l, sortcls)
6449 {
6452
6453 sortColIdx[numsortkeys] = tle->resno;
6454 sortOperators[numsortkeys] = sortcl->sortop;
6455 collations[numsortkeys] = exprCollation((Node *) tle->expr);
6456 nullsFirst[numsortkeys] = sortcl->nulls_first;
6457 numsortkeys++;
6458 }
6459
6460 return make_sort(lefttree, numsortkeys,
6461 sortColIdx, sortOperators,
6462 collations, nullsFirst);
6463}
6464
6465/*
6466 * make_sort_from_groupcols
6467 * Create sort plan to sort based on grouping columns
6468 *
6469 * 'groupcls' is the list of SortGroupClauses
6470 * 'grpColIdx' gives the column numbers to use
6471 *
6472 * This might look like it could be merged with make_sort_from_sortclauses,
6473 * but presently we *must* use the grpColIdx[] array to locate sort columns,
6474 * because the child plan's tlist is not marked with ressortgroupref info
6475 * appropriate to the grouping node. So, only the sort ordering info
6476 * is used from the SortGroupClause entries.
6477 */
6478static Sort *
6481 Plan *lefttree)
6482{
6483 List *sub_tlist = lefttree->targetlist;
6484 ListCell *l;
6485 int numsortkeys;
6486 AttrNumber *sortColIdx;
6487 Oid *sortOperators;
6488 Oid *collations;
6489 bool *nullsFirst;
6490
6491 /* Convert list-ish representation to arrays wanted by executor */
6493 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
6494 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
6495 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
6496 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
6497
6498 numsortkeys = 0;
6499 foreach(l, groupcls)
6500 {
6503
6504 if (!tle)
6505 elog(ERROR, "could not retrieve tle for sort-from-groupcols");
6506
6507 sortColIdx[numsortkeys] = tle->resno;
6508 sortOperators[numsortkeys] = grpcl->sortop;
6509 collations[numsortkeys] = exprCollation((Node *) tle->expr);
6510 nullsFirst[numsortkeys] = grpcl->nulls_first;
6511 numsortkeys++;
6512 }
6513
6514 return make_sort(lefttree, numsortkeys,
6515 sortColIdx, sortOperators,
6516 collations, nullsFirst);
6517}
6518
6519static Material *
6521{
6522 Material *node = makeNode(Material);
6523 Plan *plan = &node->plan;
6524
6525 plan->targetlist = lefttree->targetlist;
6526 plan->qual = NIL;
6527 plan->lefttree = lefttree;
6528 plan->righttree = NULL;
6529
6530 return node;
6531}
6532
6533/*
6534 * materialize_finished_plan: stick a Material node atop a completed plan
6535 *
6536 * There are a couple of places where we want to attach a Material node
6537 * after completion of create_plan(), without any MaterialPath path.
6538 * Those places should probably be refactored someday to do this on the
6539 * Path representation, but it's not worth the trouble yet.
6540 */
6541Plan *
6543{
6544 Plan *matplan;
6545 Path matpath; /* dummy for cost_material */
6547 bool unsafe_initplans;
6548
6549 matplan = (Plan *) make_material(subplan);
6550
6551 /*
6552 * XXX horrid kluge: if there are any initPlans attached to the subplan,
6553 * move them up to the Material node, which is now effectively the top
6554 * plan node in its query level. This prevents failure in
6555 * SS_finalize_plan(), which see for comments.
6556 */
6557 matplan->initPlan = subplan->initPlan;
6558 subplan->initPlan = NIL;
6559
6560 /* Move the initplans' cost delta, as well */
6563 subplan->startup_cost -= initplan_cost;
6564 subplan->total_cost -= initplan_cost;
6565
6566 /* Set cost data */
6569 subplan->disabled_nodes,
6570 subplan->startup_cost,
6571 subplan->total_cost,
6572 subplan->plan_rows,
6573 subplan->plan_width);
6574 matplan->disabled_nodes = subplan->disabled_nodes;
6575 matplan->startup_cost = matpath.startup_cost + initplan_cost;
6576 matplan->total_cost = matpath.total_cost + initplan_cost;
6577 matplan->plan_rows = subplan->plan_rows;
6578 matplan->plan_width = subplan->plan_width;
6579 matplan->parallel_aware = false;
6580 matplan->parallel_safe = subplan->parallel_safe;
6581
6582 return matplan;
6583}
6584
6585static Memoize *
6586make_memoize(Plan *lefttree, Oid *hashoperators, Oid *collations,
6587 List *param_exprs, bool singlerow, bool binary_mode,
6588 uint32 est_entries, Bitmapset *keyparamids,
6589 Cardinality est_calls, Cardinality est_unique_keys,
6590 double est_hit_ratio)
6591{
6592 Memoize *node = makeNode(Memoize);
6593 Plan *plan = &node->plan;
6594
6595 plan->targetlist = lefttree->targetlist;
6596 plan->qual = NIL;
6597 plan->lefttree = lefttree;
6598 plan->righttree = NULL;
6599
6600 node->numKeys = list_length(param_exprs);
6601 node->hashOperators = hashoperators;
6602 node->collations = collations;
6603 node->param_exprs = param_exprs;
6604 node->singlerow = singlerow;
6605 node->binary_mode = binary_mode;
6606 node->est_entries = est_entries;
6607 node->keyparamids = keyparamids;
6608 node->est_calls = est_calls;
6609 node->est_unique_keys = est_unique_keys;
6610 node->est_hit_ratio = est_hit_ratio;
6611
6612 return node;
6613}
6614
6615Agg *
6616make_agg(List *tlist, List *qual,
6617 AggStrategy aggstrategy, AggSplit aggsplit,
6619 List *groupingSets, List *chain, Cardinality numGroups,
6620 Size transitionSpace, Plan *lefttree)
6621{
6622 Agg *node = makeNode(Agg);
6623 Plan *plan = &node->plan;
6624
6625 node->aggstrategy = aggstrategy;
6626 node->aggsplit = aggsplit;
6627 node->numCols = numGroupCols;
6628 node->grpColIdx = grpColIdx;
6629 node->grpOperators = grpOperators;
6630 node->grpCollations = grpCollations;
6631 node->numGroups = numGroups;
6632 node->transitionSpace = transitionSpace;
6633 node->aggParams = NULL; /* SS_finalize_plan() will fill this */
6634 node->groupingSets = groupingSets;
6635 node->chain = chain;
6636
6637 plan->qual = qual;
6638 plan->targetlist = tlist;
6639 plan->lefttree = lefttree;
6640 plan->righttree = NULL;
6641
6642 return node;
6643}
6644
6645static WindowAgg *
6649 List *runCondition, List *qual, bool topWindow, Plan *lefttree)
6650{
6651 WindowAgg *node = makeNode(WindowAgg);
6652 Plan *plan = &node->plan;
6653
6654 node->winname = wc->name;
6655 node->winref = wc->winref;
6656 node->partNumCols = partNumCols;
6657 node->partColIdx = partColIdx;
6658 node->partOperators = partOperators;
6659 node->partCollations = partCollations;
6660 node->ordNumCols = ordNumCols;
6661 node->ordColIdx = ordColIdx;
6662 node->ordOperators = ordOperators;
6663 node->ordCollations = ordCollations;
6664 node->frameOptions = wc->frameOptions;
6665 node->startOffset = wc->startOffset;
6666 node->endOffset = wc->endOffset;
6667 node->runCondition = runCondition;
6668 /* a duplicate of the above for EXPLAIN */
6669 node->runConditionOrig = runCondition;
6670 node->startInRangeFunc = wc->startInRangeFunc;
6671 node->endInRangeFunc = wc->endInRangeFunc;
6672 node->inRangeColl = wc->inRangeColl;
6673 node->inRangeAsc = wc->inRangeAsc;
6674 node->inRangeNullsFirst = wc->inRangeNullsFirst;
6675 node->topWindow = topWindow;
6676
6677 plan->targetlist = tlist;
6678 plan->lefttree = lefttree;
6679 plan->righttree = NULL;
6680 plan->qual = qual;
6681
6682 return node;
6683}
6684
6685static Group *
6687 List *qual,
6688 int numGroupCols,
6692 Plan *lefttree)
6693{
6694 Group *node = makeNode(Group);
6695 Plan *plan = &node->plan;
6696
6697 node->numCols = numGroupCols;
6698 node->grpColIdx = grpColIdx;
6699 node->grpOperators = grpOperators;
6700 node->grpCollations = grpCollations;
6701
6702 plan->qual = qual;
6703 plan->targetlist = tlist;
6704 plan->lefttree = lefttree;
6705 plan->righttree = NULL;
6706
6707 return node;
6708}
6709
6710/*
6711 * pathkeys is a list of PathKeys, identifying the sort columns and semantics.
6712 * The input plan must already be sorted accordingly.
6713 *
6714 * relids identifies the child relation being unique-ified, if any.
6715 */
6716static Unique *
6717make_unique_from_pathkeys(Plan *lefttree, List *pathkeys, int numCols,
6718 Relids relids)
6719{
6720 Unique *node = makeNode(Unique);
6721 Plan *plan = &node->plan;
6722 int keyno = 0;
6726 ListCell *lc;
6727
6728 plan->targetlist = lefttree->targetlist;
6729 plan->qual = NIL;
6730 plan->lefttree = lefttree;
6731 plan->righttree = NULL;
6732
6733 /*
6734 * Convert pathkeys list into arrays of attr indexes and equality
6735 * operators, as wanted by executor. This has a lot in common with
6736 * prepare_sort_from_pathkeys ... maybe unify sometime?
6737 */
6738 Assert(numCols >= 0 && numCols <= list_length(pathkeys));
6740 uniqOperators = palloc_array(Oid, numCols);
6741 uniqCollations = palloc_array(Oid, numCols);
6742
6743 foreach(lc, pathkeys)
6744 {
6746 EquivalenceClass *ec = pathkey->pk_eclass;
6748 TargetEntry *tle = NULL;
6750 Oid eqop;
6751 ListCell *j;
6752
6753 /* Ignore pathkeys beyond the specified number of columns */
6754 if (keyno >= numCols)
6755 break;
6756
6757 if (ec->ec_has_volatile)
6758 {
6759 /*
6760 * If the pathkey's EquivalenceClass is volatile, then it must
6761 * have come from an ORDER BY clause, and we have to match it to
6762 * that same targetlist entry.
6763 */
6764 if (ec->ec_sortref == 0) /* can't happen */
6765 elog(ERROR, "volatile EquivalenceClass has no sortref");
6766 tle = get_sortgroupref_tle(ec->ec_sortref, plan->targetlist);
6767 Assert(tle);
6768 Assert(list_length(ec->ec_members) == 1);
6769 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
6770 }
6771 else
6772 {
6773 /*
6774 * Otherwise, we can use any non-constant expression listed in the
6775 * pathkey's EquivalenceClass. For now, we take the first tlist
6776 * item found in the EC.
6777 */
6778 foreach(j, plan->targetlist)
6779 {
6780 tle = (TargetEntry *) lfirst(j);
6781 em = find_ec_member_matching_expr(ec, tle->expr, relids);
6782 if (em)
6783 {
6784 /* found expr already in tlist */
6785 pk_datatype = em->em_datatype;
6786 break;
6787 }
6788 tle = NULL;
6789 }
6790 }
6791
6792 if (!tle)
6793 elog(ERROR, "could not find pathkey item to sort");
6794
6795 /*
6796 * Look up the correct equality operator from the PathKey's slightly
6797 * abstracted representation.
6798 */
6799 eqop = get_opfamily_member_for_cmptype(pathkey->pk_opfamily,
6802 COMPARE_EQ);
6803 if (!OidIsValid(eqop)) /* should not happen */
6804 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
6806 pathkey->pk_opfamily);
6807
6808 uniqColIdx[keyno] = tle->resno;
6809 uniqOperators[keyno] = eqop;
6810 uniqCollations[keyno] = ec->ec_collation;
6811
6812 keyno++;
6813 }
6814
6815 node->numCols = numCols;
6816 node->uniqColIdx = uniqColIdx;
6817 node->uniqOperators = uniqOperators;
6818 node->uniqCollations = uniqCollations;
6819
6820 return node;
6821}
6822
6823static Gather *
6825 List *qpqual,
6826 int nworkers,
6827 int rescan_param,
6828 bool single_copy,
6829 Plan *subplan)
6830{
6831 Gather *node = makeNode(Gather);
6832 Plan *plan = &node->plan;
6833
6835 plan->qual = qpqual;
6836 plan->lefttree = subplan;
6837 plan->righttree = NULL;
6838 node->num_workers = nworkers;
6839 node->rescan_param = rescan_param;
6840 node->single_copy = single_copy;
6841 node->invisible = false;
6842 node->initParam = NULL;
6843
6844 return node;
6845}
6846
6847/*
6848 * groupList is a list of SortGroupClauses, identifying the targetlist
6849 * items that should be considered by the SetOp filter. The input plans must
6850 * already be sorted accordingly, if we're doing SETOP_SORTED mode.
6851 */
6852static SetOp *
6854 List *tlist, Plan *lefttree, Plan *righttree,
6855 List *groupList, Cardinality numGroups)
6856{
6857 SetOp *node = makeNode(SetOp);
6858 Plan *plan = &node->plan;
6859 int numCols = list_length(groupList);
6860 int keyno = 0;
6864 bool *cmpNullsFirst;
6866
6867 plan->targetlist = tlist;
6868 plan->qual = NIL;
6869 plan->lefttree = lefttree;
6870 plan->righttree = righttree;
6871
6872 /*
6873 * convert SortGroupClause list into arrays of attr indexes and comparison
6874 * operators, as wanted by executor
6875 */
6876 cmpColIdx = palloc_array(AttrNumber, numCols);
6877 cmpOperators = palloc_array(Oid, numCols);
6878 cmpCollations = palloc_array(Oid, numCols);
6879 cmpNullsFirst = palloc_array(bool, numCols);
6880
6881 foreach(slitem, groupList)
6882 {
6885
6886 cmpColIdx[keyno] = tle->resno;
6887 if (strategy == SETOP_HASHED)
6888 cmpOperators[keyno] = sortcl->eqop;
6889 else
6890 cmpOperators[keyno] = sortcl->sortop;
6892 cmpCollations[keyno] = exprCollation((Node *) tle->expr);
6893 cmpNullsFirst[keyno] = sortcl->nulls_first;
6894 keyno++;
6895 }
6896
6897 node->cmd = cmd;
6898 node->strategy = strategy;
6899 node->numCols = numCols;
6900 node->cmpColIdx = cmpColIdx;
6901 node->cmpOperators = cmpOperators;
6902 node->cmpCollations = cmpCollations;
6903 node->cmpNullsFirst = cmpNullsFirst;
6904 node->numGroups = numGroups;
6905
6906 return node;
6907}
6908
6909/*
6910 * make_lockrows
6911 * Build a LockRows plan node
6912 */
6913static LockRows *
6914make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
6915{
6916 LockRows *node = makeNode(LockRows);
6917 Plan *plan = &node->plan;
6918
6919 plan->targetlist = lefttree->targetlist;
6920 plan->qual = NIL;
6921 plan->lefttree = lefttree;
6922 plan->righttree = NULL;
6923
6924 node->rowMarks = rowMarks;
6925 node->epqParam = epqParam;
6926
6927 return node;
6928}
6929
6930/*
6931 * make_limit
6932 * Build a Limit plan node
6933 */
6934Limit *
6935make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount,
6936 LimitOption limitOption, int uniqNumCols, AttrNumber *uniqColIdx,
6938{
6939 Limit *node = makeNode(Limit);
6940 Plan *plan = &node->plan;
6941
6942 plan->targetlist = lefttree->targetlist;
6943 plan->qual = NIL;
6944 plan->lefttree = lefttree;
6945 plan->righttree = NULL;
6946
6947 node->limitOffset = limitOffset;
6948 node->limitCount = limitCount;
6949 node->limitOption = limitOption;
6950 node->uniqNumCols = uniqNumCols;
6951 node->uniqColIdx = uniqColIdx;
6952 node->uniqOperators = uniqOperators;
6953 node->uniqCollations = uniqCollations;
6954
6955 return node;
6956}
6957
6958/*
6959 * make_gating_result
6960 * Build a Result plan node that performs projection of a subplan, and/or
6961 * applies a one time filter (resconstantqual)
6962 */
6963static Result *
6965 Node *resconstantqual,
6966 Plan *subplan)
6967{
6968 Result *node = makeNode(Result);
6969 Plan *plan = &node->plan;
6970
6971 Assert(subplan != NULL);
6972
6973 plan->targetlist = tlist;
6974 plan->qual = NIL;
6975 plan->lefttree = subplan;
6976 plan->righttree = NULL;
6978 node->resconstantqual = resconstantqual;
6979 node->relids = NULL;
6980
6981 return node;
6982}
6983
6984/*
6985 * make_one_row_result
6986 * Build a Result plan node that returns a single row (or possibly no rows,
6987 * if the one-time filtered defined by resconstantqual returns false)
6988 *
6989 * 'rel' should be this path's RelOptInfo. In essence, we're saying that this
6990 * Result node generates all the tuples for that RelOptInfo. Note that the same
6991 * consideration can never arise in make_gating_result(), because in that case
6992 * the tuples are always coming from some subordinate node.
6993 */
6994static Result *
6996 Node *resconstantqual,
6997 RelOptInfo *rel)
6998{
6999 Result *node = makeNode(Result);
7000 Plan *plan = &node->plan;
7001
7002 plan->targetlist = tlist;
7003 plan->qual = NIL;
7004 plan->lefttree = NULL;
7005 plan->righttree = NULL;
7008 node->resconstantqual = resconstantqual;
7009 node->relids = rel->relids;
7010
7011 return node;
7012}
7013
7014/*
7015 * make_project_set
7016 * Build a ProjectSet plan node
7017 */
7018static ProjectSet *
7020 Plan *subplan)
7021{
7023 Plan *plan = &node->plan;
7024
7025 plan->targetlist = tlist;
7026 plan->qual = NIL;
7027 plan->lefttree = subplan;
7028 plan->righttree = NULL;
7029
7030 return node;
7031}
7032
7033/*
7034 * make_modifytable
7035 * Build a ModifyTable plan node
7036 */
7037static ModifyTable *
7039 CmdType operation, bool canSetTag,
7040 Index nominalRelation, Index rootRelation,
7041 List *resultRelations,
7042 List *updateColnosLists,
7043 List *withCheckOptionLists, List *returningLists,
7044 List *rowMarks, OnConflictExpr *onconflict,
7045 List *mergeActionLists, List *mergeJoinConditions,
7046 ForPortionOfExpr *forPortionOf, int epqParam)
7047{
7049 bool returning_old_or_new = false;
7050 bool returning_old_or_new_valid = false;
7051 bool transition_tables = false;
7052 bool transition_tables_valid = false;
7055 ListCell *lc;
7056 int i;
7057
7059 (operation == CMD_UPDATE ?
7060 list_length(resultRelations) == list_length(updateColnosLists) :
7061 updateColnosLists == NIL));
7062 Assert(withCheckOptionLists == NIL ||
7063 list_length(resultRelations) == list_length(withCheckOptionLists));
7064 Assert(returningLists == NIL ||
7065 list_length(resultRelations) == list_length(returningLists));
7066
7067 node->plan.lefttree = subplan;
7068 node->plan.righttree = NULL;
7069 node->plan.qual = NIL;
7070 /* setrefs.c will fill in the targetlist, if needed */
7071 node->plan.targetlist = NIL;
7072
7073 node->operation = operation;
7074 node->canSetTag = canSetTag;
7075 node->nominalRelation = nominalRelation;
7076 node->rootRelation = rootRelation;
7077 node->resultRelations = resultRelations;
7078 if (!onconflict)
7079 {
7082 node->onConflictSet = NIL;
7083 node->onConflictCols = NIL;
7084 node->onConflictWhere = NULL;
7085 node->arbiterIndexes = NIL;
7086 node->exclRelRTI = 0;
7087 node->exclRelTlist = NIL;
7088 }
7089 else
7090 {
7091 node->onConflictAction = onconflict->action;
7092
7093 /* Lock strength for ON CONFLICT DO SELECT [FOR UPDATE/SHARE] */
7094 node->onConflictLockStrength = onconflict->lockStrength;
7095
7096 /*
7097 * Here we convert the ON CONFLICT UPDATE tlist, if any, to the
7098 * executor's convention of having consecutive resno's. The actual
7099 * target column numbers are saved in node->onConflictCols. (This
7100 * could be done earlier, but there seems no need to.)
7101 */
7102 node->onConflictSet = onconflict->onConflictSet;
7103 node->onConflictCols =
7105 node->onConflictWhere = onconflict->onConflictWhere;
7106
7107 /*
7108 * If a set of unique index inference elements was provided (an
7109 * INSERT...ON CONFLICT "inference specification"), then infer
7110 * appropriate unique indexes (or throw an error if none are
7111 * available).
7112 */
7114
7115 node->exclRelRTI = onconflict->exclRelIndex;
7116 node->exclRelTlist = onconflict->exclRelTlist;
7117 }
7118 node->updateColnosLists = updateColnosLists;
7119 node->forPortionOf = (Node *) forPortionOf;
7120 node->withCheckOptionLists = withCheckOptionLists;
7121 node->returningOldAlias = root->parse->returningOldAlias;
7122 node->returningNewAlias = root->parse->returningNewAlias;
7123 node->returningLists = returningLists;
7124 node->rowMarks = rowMarks;
7125 node->mergeActionLists = mergeActionLists;
7126 node->mergeJoinConditions = mergeJoinConditions;
7127 node->epqParam = epqParam;
7128
7129 /*
7130 * For each result relation that is a foreign table, allow the FDW to
7131 * construct private plan data, and accumulate it all into a list.
7132 */
7135 i = 0;
7136 foreach(lc, resultRelations)
7137 {
7138 Index rti = lfirst_int(lc);
7139 FdwRoutine *fdwroutine;
7140 List *fdw_private;
7141 bool direct_modify;
7142
7143 /*
7144 * If possible, we want to get the FdwRoutine from our RelOptInfo for
7145 * the table. But sometimes we don't have a RelOptInfo and must get
7146 * it the hard way. (In INSERT, the target relation is not scanned,
7147 * so it's not a baserel; and there are also corner cases for
7148 * updatable views where the target rel isn't a baserel.)
7149 */
7150 if (rti < root->simple_rel_array_size &&
7151 root->simple_rel_array[rti] != NULL)
7152 {
7153 RelOptInfo *resultRel = root->simple_rel_array[rti];
7154
7155 fdwroutine = resultRel->fdwroutine;
7156 }
7157 else
7158 {
7160
7161 if (rte->rtekind == RTE_RELATION &&
7162 rte->relkind == RELKIND_FOREIGN_TABLE)
7163 {
7164 /* Check if the access to foreign tables is restricted */
7166 {
7167 /* there must not be built-in foreign tables */
7168 Assert(rte->relid >= FirstNormalObjectId);
7169 ereport(ERROR,
7171 errmsg("access to non-system foreign table is restricted")));
7172 }
7173
7174 fdwroutine = GetFdwRoutineByRelId(rte->relid);
7175 }
7176 else
7177 fdwroutine = NULL;
7178 }
7179
7180 /*
7181 * MERGE is not currently supported for foreign tables. We already
7182 * checked that when the table mentioned in the query is foreign; but
7183 * we can still get here if a partitioned table has a foreign table as
7184 * partition. Disallow that now, to avoid an uglier error message
7185 * later.
7186 */
7187 if (operation == CMD_MERGE && fdwroutine != NULL)
7188 {
7190
7191 ereport(ERROR,
7193 errmsg("cannot execute MERGE on relation \"%s\"",
7194 get_rel_name(rte->relid)),
7196 }
7197
7198 /*
7199 * Try to modify the foreign table directly if (1) the FDW provides
7200 * callback functions needed for that and (2) there are no local
7201 * structures that need to be run for each modified row: row-level
7202 * triggers on the foreign table, stored generated columns, WITH CHECK
7203 * OPTIONs from parent views, Vars returning OLD/NEW in the RETURNING
7204 * list, or transition tables on the named relation.
7205 */
7206 direct_modify = false;
7207 if (fdwroutine != NULL &&
7208 fdwroutine->PlanDirectModify != NULL &&
7209 fdwroutine->BeginDirectModify != NULL &&
7210 fdwroutine->IterateDirectModify != NULL &&
7211 fdwroutine->EndDirectModify != NULL &&
7212 withCheckOptionLists == NIL &&
7215 {
7216 /*
7217 * returning_old_or_new and transition_tables are the same for all
7218 * result relations, respectively
7219 */
7221 {
7224 root->parse->returningList);
7226 }
7228 {
7230 {
7232 nominalRelation,
7233 operation);
7235 }
7236 if (!transition_tables)
7237 direct_modify = fdwroutine->PlanDirectModify(root, node,
7238 rti, i);
7239 }
7240 }
7241 if (direct_modify)
7243
7244 if (!direct_modify &&
7245 fdwroutine != NULL &&
7246 fdwroutine->PlanForeignModify != NULL)
7247 fdw_private = fdwroutine->PlanForeignModify(root, node, rti, i);
7248 else
7249 fdw_private = NIL;
7251 i++;
7252 }
7255
7256 return node;
7257}
7258
7259/*
7260 * is_projection_capable_path
7261 * Check whether a given Path node is able to do projection.
7262 */
7263bool
7265{
7266 /* Most plan types can project, so just list the ones that can't */
7267 switch (path->pathtype)
7268 {
7269 case T_Hash:
7270 case T_Material:
7271 case T_Memoize:
7272 case T_Sort:
7273 case T_IncrementalSort:
7274 case T_Unique:
7275 case T_SetOp:
7276 case T_LockRows:
7277 case T_Limit:
7278 case T_ModifyTable:
7279 case T_MergeAppend:
7280 case T_RecursiveUnion:
7281 return false;
7282 case T_CustomScan:
7284 return true;
7285 return false;
7286 case T_Append:
7287
7288 /*
7289 * Append can't project, but if an AppendPath is being used to
7290 * represent a dummy path, what will actually be generated is a
7291 * Result which can project.
7292 */
7293 return IS_DUMMY_APPEND(path);
7294 case T_ProjectSet:
7295
7296 /*
7297 * Although ProjectSet certainly projects, say "no" because we
7298 * don't want the planner to randomly replace its tlist with
7299 * something else; the SRFs have to stay at top level. This might
7300 * get relaxed later.
7301 */
7302 return false;
7303 default:
7304 break;
7305 }
7306 return true;
7307}
7308
7309/*
7310 * is_projection_capable_plan
7311 * Check whether a given Plan node is able to do projection.
7312 */
7313bool
7315{
7316 /* Most plan types can project, so just list the ones that can't */
7317 switch (nodeTag(plan))
7318 {
7319 case T_Hash:
7320 case T_Material:
7321 case T_Memoize:
7322 case T_Sort:
7323 case T_Unique:
7324 case T_SetOp:
7325 case T_LockRows:
7326 case T_Limit:
7327 case T_ModifyTable:
7328 case T_Append:
7329 case T_MergeAppend:
7330 case T_RecursiveUnion:
7331 return false;
7332 case T_CustomScan:
7334 return true;
7335 return false;
7336 case T_ProjectSet:
7337
7338 /*
7339 * Although ProjectSet certainly projects, say "no" because we
7340 * don't want the planner to randomly replace its tlist with
7341 * something else; the SRFs have to stay at top level. This might
7342 * get relaxed later.
7343 */
7344 return false;
7345 default:
7346 break;
7347 }
7348 return true;
7349}
Datum sort(PG_FUNCTION_ARGS)
Definition _int_op.c:199
int16 AttrNumber
Definition attnum.h:21
#define InvalidAttrNumber
Definition attnum.h:23
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:347
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:547
void bms_free(Bitmapset *a)
Definition bitmapset.c:240
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:645
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:934
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:252
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:769
#define bms_is_empty(a)
Definition bitmapset.h:119
#define PG_USED_FOR_ASSERTS_ONLY
Definition c.h:308
#define Assert(condition)
Definition c.h:1002
#define unlikely(x)
Definition c.h:497
uint32_t uint32
Definition c.h:683
unsigned int Index
Definition c.h:757
#define OidIsValid(objectId)
Definition c.h:917
size_t Size
Definition c.h:748
uint32 result
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
bool contain_mutable_functions(Node *clause)
Definition clauses.c:399
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition clauses.c:782
Bitmapset * pull_paramids(Expr *expr)
Definition clauses.c:6259
void CommuteOpExpr(OpExpr *clause)
Definition clauses.c:2408
@ COMPARE_GT
Definition cmptype.h:38
@ COMPARE_EQ
Definition cmptype.h:36
double cpu_operator_cost
Definition costsize.c:135
void cost_material(Path *path, bool enabled, int input_disabled_nodes, Cost input_startup_cost, Cost input_total_cost, double tuples, int width)
Definition costsize.c:2584
void cost_sort(Path *path, PlannerInfo *root, List *pathkeys, int input_disabled_nodes, Cost input_cost, double tuples, int width, Cost comparison_cost, int sort_mem, double limit_tuples)
Definition costsize.c:2202
bool enable_material
Definition costsize.c:156
void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
Definition costsize.c:4949
void cost_incremental_sort(Path *path, PlannerInfo *root, List *pathkeys, int presorted_keys, int input_disabled_nodes, Cost input_startup_cost, Cost input_total_cost, double input_tuples, int width, Cost comparison_cost, int sort_mem, double limit_tuples)
Definition costsize.c:2054
bool enable_async_append
Definition costsize.c:167
double clamp_row_est(double nrows)
Definition costsize.c:215
bool enable_partition_pruning
Definition costsize.c:165
bool enable_sort
Definition costsize.c:151
bool enable_incremental_sort
Definition costsize.c:152
static Plan * create_join_plan(PlannerInfo *root, JoinPath *best_path)
static bool use_physical_tlist(PlannerInfo *root, Path *path, int flags)
Definition createplan.c:863
static SeqScan * create_seqscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
static WorkTableScan * make_worktablescan(List *qptlist, List *qpqual, Index scanrelid, int wtParam)
static Plan * create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path, int flags)
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
static GatherMerge * create_gather_merge_plan(PlannerInfo *root, GatherMergePath *best_path)
static ValuesScan * create_valuesscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
static void copy_generic_path_info(Plan *dest, Path *src)
static WindowAgg * make_windowagg(List *tlist, WindowClause *wc, int partNumCols, AttrNumber *partColIdx, Oid *partOperators, Oid *partCollations, int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators, Oid *ordCollations, List *runCondition, List *qual, bool topWindow, Plan *lefttree)
Sort * make_sort_from_sortclauses(List *sortcls, Plan *lefttree)
static BitmapOr * make_bitmap_or(List *bitmapplans)
static HashJoin * create_hashjoin_plan(PlannerInfo *root, HashPath *best_path)
static SeqScan * make_seqscan(List *qptlist, List *qpqual, Index scanrelid)
static TableFuncScan * create_tablefuncscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
static CustomScan * create_customscan_plan(PlannerInfo *root, CustomPath *best_path, List *tlist, List *scan_clauses)
static Node * fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol)
static void fix_indexqual_references(PlannerInfo *root, IndexPath *index_path, List **stripped_indexquals_p, List **fixed_indexquals_p)
static MergeJoin * make_mergejoin(List *tlist, List *joinclauses, List *otherclauses, List *mergeclauses, Oid *mergefamilies, Oid *mergecollations, bool *mergereversals, bool *mergenullsfirst, Plan *lefttree, Plan *righttree, JoinType jointype, Relids ojrelids, bool inner_unique, bool skip_mark_restore)
static List * fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path)
static AttrNumber * remap_groupColIdx(PlannerInfo *root, List *groupClause)
static Plan * create_append_plan(PlannerInfo *root, AppendPath *best_path, int flags)
static void bitmap_subplan_mark_shared(Plan *plan)
static Unique * create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
static Result * make_one_row_result(List *tlist, Node *resconstantqual, RelOptInfo *rel)
static TidScan * make_tidscan(List *qptlist, List *qpqual, Index scanrelid, List *tidquals)
static MergeJoin * create_mergejoin_plan(PlannerInfo *root, MergePath *best_path)
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition createplan.c:396
static void label_sort_with_costsize(PlannerInfo *root, Sort *plan, double limit_tuples)
static ForeignScan * create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path, List *tlist, List *scan_clauses)
static BitmapHeapScan * create_bitmap_scan_plan(PlannerInfo *root, BitmapHeapPath *best_path, List *tlist, List *scan_clauses)
static IncrementalSort * make_incrementalsort(Plan *lefttree, int numCols, int nPresortedCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
static Result * create_group_result_plan(PlannerInfo *root, GroupResultPath *best_path)
static Limit * create_limit_plan(PlannerInfo *root, LimitPath *best_path, int flags)
static NestLoop * make_nestloop(List *tlist, List *joinclauses, List *otherclauses, List *nestParams, Plan *lefttree, Plan *righttree, JoinType jointype, Relids ojrelids, bool inner_unique)
static Unique * make_unique_from_pathkeys(Plan *lefttree, List *pathkeys, int numCols, Relids relids)
static Agg * create_agg_plan(PlannerInfo *root, AggPath *best_path)
bool is_projection_capable_path(Path *path)
static CteScan * make_ctescan(List *qptlist, List *qpqual, Index scanrelid, int ctePlanId, int cteParam)
static TidScan * create_tidscan_plan(PlannerInfo *root, TidPath *best_path, List *tlist, List *scan_clauses)
static TidRangeScan * make_tidrangescan(List *qptlist, List *qpqual, Index scanrelid, List *tidrangequals)
static Plan * create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual, List **qual, List **indexqual, List **indexECs)
static RecursiveUnion * make_recursive_union(List *tlist, Plan *lefttree, Plan *righttree, int wtParam, List *distinctList, Cardinality numGroups)
static Node * fix_indexqual_clause(PlannerInfo *root, IndexOptInfo *index, int indexcol, Node *clause, List *indexcolnos)
static WorkTableScan * create_worktablescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
static Plan * create_gating_plan(PlannerInfo *root, Path *path, Plan *plan, List *gating_quals)
static Memoize * make_memoize(Plan *lefttree, Oid *hashoperators, Oid *collations, List *param_exprs, bool singlerow, bool binary_mode, uint32 est_entries, Bitmapset *keyparamids, Cardinality est_calls, Cardinality est_unique_keys, double est_hit_ratio)
static FunctionScan * create_functionscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
static Result * create_resultscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
static BitmapHeapScan * make_bitmap_heapscan(List *qptlist, List *qpqual, Plan *lefttree, List *bitmapqualorig, Index scanrelid)
static Node * replace_nestloop_params_mutator(Node *node, PlannerInfo *root)
static SetOp * create_setop_plan(PlannerInfo *root, SetOpPath *best_path, int flags)
bool is_projection_capable_plan(Plan *plan)
static CteScan * create_ctescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
static Sort * create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags)
#define CP_SMALL_TLIST
Definition createplan.c:70
static ProjectSet * make_project_set(List *tlist, Plan *subplan)
static Sort * make_sort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids)
static Gather * make_gather(List *qptlist, List *qpqual, int nworkers, int rescan_param, bool single_copy, Plan *subplan)
static Gather * create_gather_plan(PlannerInfo *root, GatherPath *best_path)
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount, LimitOption limitOption, int uniqNumCols, AttrNumber *uniqColIdx, Oid *uniqOperators, Oid *uniqCollations)
static ProjectSet * create_project_set_plan(PlannerInfo *root, ProjectSetPath *best_path)
static void label_incrementalsort_with_costsize(PlannerInfo *root, IncrementalSort *plan, List *pathkeys, double limit_tuples)
static SetOp * make_setop(SetOpCmd cmd, SetOpStrategy strategy, List *tlist, Plan *lefttree, Plan *righttree, List *groupList, Cardinality numGroups)
ForeignScan * make_foreignscan(List *qptlist, List *qpqual, Index scanrelid, List *fdw_exprs, List *fdw_private, List *fdw_scan_tlist, List *fdw_recheck_quals, Plan *outer_plan)
static Group * create_group_plan(PlannerInfo *root, GroupPath *best_path)
static ModifyTable * create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path)
static Result * create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path)
static LockRows * create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path, int flags)
static Material * create_material_plan(PlannerInfo *root, MaterialPath *best_path, int flags)
static List * get_gating_quals(PlannerInfo *root, List *quals)
static HashJoin * make_hashjoin(List *tlist, List *joinclauses, List *otherclauses, List *hashclauses, List *hashoperators, List *hashcollations, List *hashkeys, Plan *lefttree, Plan *righttree, JoinType jointype, Relids ojrelids, bool inner_unique)
static Plan * create_scan_plan(PlannerInfo *root, Path *best_path, int flags)
Definition createplan.c:557
static Group * make_group(List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations, Plan *lefttree)
static LockRows * make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
static IncrementalSort * create_incrementalsort_plan(PlannerInfo *root, IncrementalSortPath *best_path, int flags)
static NamedTuplestoreScan * create_namedtuplestorescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
static Plan * create_projection_plan(PlannerInfo *root, ProjectionPath *best_path, int flags)
static IndexOnlyScan * make_indexonlyscan(List *qptlist, List *qpqual, Index scanrelid, Oid indexid, List *indexqual, List *recheckqual, List *indexorderby, List *indextlist, ScanDirection indexscandir)
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition createplan.c:823
static IndexScan * make_indexscan(List *qptlist, List *qpqual, Index scanrelid, Oid indexid, List *indexqual, List *indexqualorig, List *indexorderby, List *indexorderbyorig, List *indexorderbyops, ScanDirection indexscandir)
static FunctionScan * make_functionscan(List *qptlist, List *qpqual, Index scanrelid, List *functions, bool funcordinality)
static TableFuncScan * make_tablefuncscan(List *qptlist, List *qpqual, Index scanrelid, TableFunc *tablefunc)
static SubqueryScan * create_subqueryscan_plan(PlannerInfo *root, SubqueryScanPath *best_path, List *tlist, List *scan_clauses)
static Plan * inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
static TidRangeScan * create_tidrangescan_plan(PlannerInfo *root, TidRangePath *best_path, List *tlist, List *scan_clauses)
static List * get_switched_clauses(List *clauses, Relids outerrelids)
static void copy_plan_costsize(Plan *dest, Plan *src)
static ValuesScan * make_valuesscan(List *qptlist, List *qpqual, Index scanrelid, List *values_lists)
Plan * materialize_finished_plan(Plan *subplan)
static SampleScan * make_samplescan(List *qptlist, List *qpqual, Index scanrelid, TableSampleClause *tsc)
static NestLoop * create_nestloop_plan(PlannerInfo *root, NestPath *best_path)
static Memoize * create_memoize_plan(PlannerInfo *root, MemoizePath *best_path, int flags)
static Result * make_gating_result(List *tlist, Node *resconstantqual, Plan *subplan)
Agg * make_agg(List *tlist, List *qual, AggStrategy aggstrategy, AggSplit aggsplit, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations, List *groupingSets, List *chain, Cardinality numGroups, Size transitionSpace, Plan *lefttree)
#define CP_EXACT_TLIST
Definition createplan.c:69
static NamedTuplestoreScan * make_namedtuplestorescan(List *qptlist, List *qpqual, Index scanrelid, char *enrname)
static bool mark_async_capable_plan(Plan *plan, Path *path)
static Material * make_material(Plan *lefttree)
Plan * change_plan_targetlist(Plan *subplan, List *tlist, bool tlist_parallel_safe)
static ModifyTable * make_modifytable(PlannerInfo *root, Plan *subplan, CmdType operation, bool canSetTag, Index nominalRelation, Index rootRelation, List *resultRelations, List *updateColnosLists, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, List *mergeActionLists, List *mergeJoinConditions, ForPortionOfExpr *forPortionOf, int epqParam)
static BitmapIndexScan * make_bitmap_indexscan(Index scanrelid, Oid indexid, List *indexqual, List *indexqualorig)
static SubqueryScan * make_subqueryscan(List *qptlist, List *qpqual, Index scanrelid, Plan *subplan)
static Hash * make_hash(Plan *lefttree, List *hashkeys, Oid skewTable, AttrNumber skewColumn, bool skewInherit)
static WindowAgg * create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
#define CP_LABEL_TLIST
Definition createplan.c:71
static BitmapAnd * make_bitmap_and(List *bitmapplans)
static Plan * create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path)
static RecursiveUnion * create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path)
static Sort * make_sort_from_groupcols(List *groupcls, AttrNumber *grpColIdx, Plan *lefttree)
#define CP_IGNORE_TLIST
Definition createplan.c:72
static Scan * create_indexscan_plan(PlannerInfo *root, IndexPath *best_path, List *tlist, List *scan_clauses, bool indexonly)
static Plan * prepare_sort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids, const AttrNumber *reqColIdx, bool adjust_tlist_in_place, int *p_numsortkeys, AttrNumber **p_sortColIdx, Oid **p_sortOperators, Oid **p_collations, bool **p_nullsFirst)
static IncrementalSort * make_incrementalsort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids, int nPresortedCols)
static SampleScan * create_samplescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Plan * create_plan(PlannerInfo *root, Path *best_path)
Definition createplan.c:345
static DataChecksumsWorkerOperation operation
Datum arg
Definition elog.c:1323
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
bool equal(const void *a, const void *b)
Definition equalfuncs.c:223
bool is_redundant_with_indexclauses(RestrictInfo *rinfo, List *indexclauses)
EquivalenceMember * find_ec_member_matching_expr(EquivalenceClass *ec, Expr *expr, Relids relids)
Definition equivclass.c:916
EquivalenceMember * find_computable_ec_member(PlannerInfo *root, EquivalenceClass *ec, List *exprs, Relids relids, bool require_parallel_safe)
Definition equivclass.c:991
bool is_redundant_derived_clause(RestrictInfo *rinfo, List *clauselist)
#define CUSTOMPATH_SUPPORT_PROJECTION
Definition extensible.h:86
#define palloc_array(type, count)
Definition fe_memutils.h:91
#define palloc0_array(type, count)
Definition fe_memutils.h:92
FdwRoutine * GetFdwRoutineByRelId(Oid relid)
Definition foreign.c:451
int work_mem
Definition globals.c:133
void parse(int)
Definition parse.c:49
#define nitems(x)
Definition indent.h:31
return true
Definition isn.c:130
int j
Definition isn.c:78
int i
Definition isn.c:77
List * list_difference(const List *list1, const List *list2)
Definition list.c:1237
List * lappend(List *list, void *datum)
Definition list.c:339
List * list_difference_ptr(const List *list1, const List *list2)
Definition list.c:1263
List * list_concat(List *list1, const List *list2)
Definition list.c:561
List * list_concat_copy(const List *list1, const List *list2)
Definition list.c:598
List * list_copy(const List *oldlist)
Definition list.c:1573
List * lappend_oid(List *list, Oid datum)
Definition list.c:375
bool list_member_ptr(const List *list, const void *datum)
Definition list.c:682
bool list_member(const List *list, const void *datum)
Definition list.c:661
List * list_copy_head(const List *oldlist, int len)
Definition list.c:1593
List * list_concat_unique(List *list1, const List *list2)
Definition list.c:1405
@ LCS_NONE
Definition lockoptions.h:23
char * get_rel_name(Oid relid)
Definition lsyscache.c:2242
Oid get_opfamily_member_for_cmptype(Oid opfamily, Oid lefttype, Oid righttype, CompareType cmptype)
Definition lsyscache.c:199
Datum lca(PG_FUNCTION_ARGS)
Definition ltree_op.c:600
Datum subpath(PG_FUNCTION_ARGS)
Definition ltree_op.c:348
Expr * make_orclause(List *orclauses)
Definition makefuncs.c:743
Expr * make_ands_explicit(List *andclauses)
Definition makefuncs.c:799
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition makefuncs.c:66
Node * makeBoolConst(bool value, bool isnull)
Definition makefuncs.c:408
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition makefuncs.c:289
void * palloc0(Size size)
Definition mcxt.c:1420
void * palloc(Size size)
Definition mcxt.c:1390
Oid exprType(const Node *expr)
Definition nodeFuncs.c:42
Oid exprCollation(const Node *expr)
Definition nodeFuncs.c:826
#define expression_tree_mutator(n, m, c)
Definition nodeFuncs.h:155
static bool is_opclause(const void *clause)
Definition nodeFuncs.h:76
SetOpCmd
Definition nodes.h:405
SetOpStrategy
Definition nodes.h:413
@ SETOP_HASHED
Definition nodes.h:415
#define IsA(nodeptr, _type_)
Definition nodes.h:162
#define copyObject(obj)
Definition nodes.h:230
double Cost
Definition nodes.h:259
#define nodeTag(nodeptr)
Definition nodes.h:137
#define IS_OUTER_JOIN(jointype)
Definition nodes.h:346
@ ONCONFLICT_NONE
Definition nodes.h:426
double Cardinality
Definition nodes.h:260
CmdType
Definition nodes.h:271
@ CMD_MERGE
Definition nodes.h:277
@ CMD_UPDATE
Definition nodes.h:274
@ CMD_SELECT
Definition nodes.h:273
AggStrategy
Definition nodes.h:361
@ AGG_SORTED
Definition nodes.h:363
@ AGG_HASHED
Definition nodes.h:364
@ AGG_PLAIN
Definition nodes.h:362
AggSplit
Definition nodes.h:383
@ AGGSPLIT_SIMPLE
Definition nodes.h:385
LimitOption
Definition nodes.h:439
@ LIMIT_OPTION_WITH_TIES
Definition nodes.h:441
#define makeNode(_type_)
Definition nodes.h:159
#define castNode(_type_, nodeptr)
Definition nodes.h:180
JoinType
Definition nodes.h:296
static char * errmsg
void process_subquery_nestloop_params(PlannerInfo *root, List *subplan_params)
List * identify_current_nestloop_params(PlannerInfo *root, Relids leftrelids, Relids outerrelids)
Param * replace_nestloop_param_placeholdervar(PlannerInfo *root, PlaceHolderVar *phv)
int assign_special_exec_param(PlannerInfo *root)
Param * replace_nestloop_param_var(PlannerInfo *root, Var *var)
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
@ RTE_CTE
@ RTE_NAMEDTUPLESTORE
@ RTE_VALUES
@ RTE_SUBQUERY
@ RTE_RESULT
@ RTE_FUNCTION
@ RTE_TABLEFUNC
@ RTE_RELATION
int make_partition_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel, List *subpaths, List *prunequal)
Definition partprune.c:225
bool pathkeys_count_contained_in(List *keys1, List *keys2, int *n_common)
Definition pathkeys.c:558
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition pathkeys.c:343
Path * reparameterize_path_by_child(PlannerInfo *root, Path *path, RelOptInfo *child_rel)
Definition pathnode.c:4107
#define IS_DUMMY_APPEND(p)
Definition pathnodes.h:2291
#define IS_JOIN_REL(rel)
Definition pathnodes.h:994
#define PATH_REQ_OUTER(path)
Definition pathnodes.h:2015
#define planner_rt_fetch(rti, root)
Definition pathnodes.h:704
@ RELOPT_BASEREL
Definition pathnodes.h:977
@ RELOPT_UPPER_REL
Definition pathnodes.h:981
#define IS_OTHER_REL(rel)
Definition pathnodes.h:1004
#define IS_UPPER_REL(rel)
Definition pathnodes.h:999
int errdetail_relkind_not_supported(char relkind)
Definition pg_class.c:24
#define lfirst(lc)
Definition pg_list.h:172
#define lfirst_node(type, lc)
Definition pg_list.h:176
static int list_length(const List *l)
Definition pg_list.h:152
#define NIL
Definition pg_list.h:68
#define forboth(cell1, list1, cell2, list2)
Definition pg_list.h:550
#define lfirst_int(lc)
Definition pg_list.h:173
#define list_make1(x1)
Definition pg_list.h:244
#define linitial_int(l)
Definition pg_list.h:179
#define for_each_from(cell, lst, N)
Definition pg_list.h:446
#define linitial(l)
Definition pg_list.h:178
#define lsecond(l)
Definition pg_list.h:183
static ListCell * list_head(const List *l)
Definition pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition pg_list.h:375
#define lfirst_oid(lc)
Definition pg_list.h:174
static int list_nth_int(const List *list, int n)
Definition pg_list.h:342
#define plan(x)
Definition pg_regress.c:164
PlaceHolderInfo * find_placeholder_info(PlannerInfo *root, PlaceHolderVar *phv)
Definition placeholder.c:85
Node * strip_noop_phvs(Node *node)
bool has_stored_generated_columns(PlannerInfo *root, Index rti)
Definition plancat.c:2613
bool has_row_triggers(PlannerInfo *root, Index rti, CmdType event)
Definition plancat.c:2509
List * build_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
Definition plancat.c:2042
bool has_transition_tables(PlannerInfo *root, Index rti, CmdType event)
Definition plancat.c:2559
List * infer_arbiter_indexes(PlannerInfo *root)
Definition plancat.c:802
@ SUBQUERY_SCAN_UNKNOWN
Definition plannodes.h:768
@ RESULT_TYPE_UPPER
Definition plannodes.h:280
@ RESULT_TYPE_SCAN
Definition plannodes.h:278
@ RESULT_TYPE_GATING
Definition plannodes.h:277
@ RESULT_TYPE_MINMAX
Definition plannodes.h:281
@ RESULT_TYPE_JOIN
Definition plannodes.h:279
int restrict_nonsystem_relation_kind
Definition postgres.c:111
#define InvalidOid
unsigned int Oid
bool predicate_implied_by(List *predicate_list, List *clause_list, bool weak)
Definition predtest.c:154
static int fb(int x)
List * extract_update_targetlist_colnos(List *tlist)
Definition preptlist.c:350
#define IS_SPECIAL_VARNO(varno)
Definition primnodes.h:248
#define INDEX_VAR
Definition primnodes.h:245
tree ctl root
Definition radixtree.h:1857
static const struct fns functions
Definition regcomp.c:358
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
void extract_actual_join_clauses(List *restrictinfo_list, Relids joinrelids, List **joinquals, List **otherquals)
List * get_actual_clauses(List *restrictinfo_list)
ScanDirection
Definition sdir.h:25
@ BackwardScanDirection
Definition sdir.h:26
@ ForwardScanDirection
Definition sdir.h:28
bool trivial_subqueryscan(SubqueryScan *plan)
Definition setrefs.c:1522
void check_stack_depth(void)
Definition stack_depth.c:96
AggSplit aggsplit
Definition plannodes.h:1219
List * chain
Definition plannodes.h:1246
List * groupingSets
Definition plannodes.h:1243
Bitmapset * aggParams
Definition plannodes.h:1238
Cardinality numGroups
Definition plannodes.h:1232
Plan plan
Definition plannodes.h:1213
int numCols
Definition plannodes.h:1222
uint64 transitionSpace
Definition plannodes.h:1235
AggStrategy aggstrategy
Definition plannodes.h:1216
Plan plan
Definition plannodes.h:512
List * bitmapplans
Definition plannodes.h:513
List * bitmapqualorig
Definition plannodes.h:714
List * indexqualorig
Definition plannodes.h:698
List * bitmapplans
Definition plannodes.h:528
Plan plan
Definition plannodes.h:526
int ctePlanId
Definition plannodes.h:823
int cteParam
Definition plannodes.h:825
Scan scan
Definition plannodes.h:821
Bitmapset * custom_relids
Definition plannodes.h:946
List * custom_exprs
Definition plannodes.h:940
const struct CustomScanMethods * methods
Definition plannodes.h:953
BeginDirectModify_function BeginDirectModify
Definition fdwapi.h:246
PlanForeignModify_function PlanForeignModify
Definition fdwapi.h:234
PlanDirectModify_function PlanDirectModify
Definition fdwapi.h:245
IterateDirectModify_function IterateDirectModify
Definition fdwapi.h:247
EndDirectModify_function EndDirectModify
Definition fdwapi.h:248
IsForeignPathAsyncCapable_function IsForeignPathAsyncCapable
Definition fdwapi.h:282
Oid checkAsUser
Definition plannodes.h:898
CmdType operation
Definition plannodes.h:894
List * fdw_exprs
Definition plannodes.h:902
bool fsSystemCol
Definition plannodes.h:914
Bitmapset * fs_relids
Definition plannodes.h:910
List * fdw_private
Definition plannodes.h:904
Bitmapset * fs_base_relids
Definition plannodes.h:912
Index resultRelation
Definition plannodes.h:896
List * fdw_recheck_quals
Definition plannodes.h:908
List * fdw_scan_tlist
Definition plannodes.h:906
List * functions
Definition plannodes.h:788
bool funcordinality
Definition plannodes.h:790
int num_workers
Definition plannodes.h:1362
bool invisible
Definition plannodes.h:1368
Bitmapset * initParam
Definition plannodes.h:1374
bool single_copy
Definition plannodes.h:1366
Plan plan
Definition plannodes.h:1360
int rescan_param
Definition plannodes.h:1364
int numCols
Definition plannodes.h:1187
Plan plan
Definition plannodes.h:1184
List * hashcollations
Definition plannodes.h:1071
List * hashclauses
Definition plannodes.h:1069
List * hashoperators
Definition plannodes.h:1070
Join join
Definition plannodes.h:1068
List * hashkeys
Definition plannodes.h:1077
AttrNumber skewColumn
Definition plannodes.h:1436
List * hashkeys
Definition plannodes.h:1432
Oid skewTable
Definition plannodes.h:1434
bool skewInherit
Definition plannodes.h:1438
Plan plan
Definition plannodes.h:1425
List * indexqual
Definition plannodes.h:660
List * recheckqual
Definition plannodes.h:662
List * indextlist
Definition plannodes.h:666
ScanDirection indexorderdir
Definition plannodes.h:668
List * indexorderby
Definition plannodes.h:664
List * indextlist
Definition pathnodes.h:1410
List * indexorderby
Definition plannodes.h:614
List * indexorderbyops
Definition plannodes.h:618
ScanDirection indexorderdir
Definition plannodes.h:620
Scan scan
Definition plannodes.h:606
List * indexqualorig
Definition plannodes.h:612
Oid indexid
Definition plannodes.h:608
List * indexqual
Definition plannodes.h:610
List * indexorderbyorig
Definition plannodes.h:616
List * joinqual
Definition plannodes.h:993
JoinType jointype
Definition plannodes.h:990
Bitmapset * ojrelids
Definition plannodes.h:994
bool inner_unique
Definition plannodes.h:991
LimitOption limitOption
Definition plannodes.h:1511
Plan plan
Definition plannodes.h:1502
Node * limitCount
Definition plannodes.h:1508
int uniqNumCols
Definition plannodes.h:1514
Node * limitOffset
Definition plannodes.h:1505
Definition pg_list.h:54
int epqParam
Definition plannodes.h:1490
List * rowMarks
Definition plannodes.h:1488
Plan plan
Definition plannodes.h:1486
Plan plan
Definition plannodes.h:1086
Plan plan
Definition plannodes.h:1095
bool singlerow
Definition plannodes.h:1113
Cardinality est_calls
Definition plannodes.h:1131
Bitmapset * keyparamids
Definition plannodes.h:1128
bool binary_mode
Definition plannodes.h:1119
int numKeys
Definition plannodes.h:1098
Cardinality est_unique_keys
Definition plannodes.h:1134
List * param_exprs
Definition plannodes.h:1107
double est_hit_ratio
Definition plannodes.h:1137
uint32 est_entries
Definition plannodes.h:1125
int part_prune_index
Definition plannodes.h:468
Bitmapset * apprelids
Definition plannodes.h:438
List * mergeplans
Definition plannodes.h:444
List * child_append_relid_sets
Definition plannodes.h:441
List * mergeclauses
Definition plannodes.h:1045
bool skip_mark_restore
Definition plannodes.h:1042
List * updateColnosLists
Definition plannodes.h:350
Index nominalRelation
Definition plannodes.h:344
List * arbiterIndexes
Definition plannodes.h:370
List * onConflictCols
Definition plannodes.h:376
List * mergeJoinConditions
Definition plannodes.h:388
char * returningOldAlias
Definition plannodes.h:354
char * returningNewAlias
Definition plannodes.h:356
CmdType operation
Definition plannodes.h:340
Node * forPortionOf
Definition plannodes.h:380
List * resultRelations
Definition plannodes.h:348
Bitmapset * fdwDirectModifyPlans
Definition plannodes.h:362
List * onConflictSet
Definition plannodes.h:374
List * exclRelTlist
Definition plannodes.h:384
List * mergeActionLists
Definition plannodes.h:386
bool canSetTag
Definition plannodes.h:342
List * fdwPrivLists
Definition plannodes.h:360
List * returningLists
Definition plannodes.h:358
List * withCheckOptionLists
Definition plannodes.h:352
LockClauseStrength onConflictLockStrength
Definition plannodes.h:372
Index rootRelation
Definition plannodes.h:346
Node * onConflictWhere
Definition plannodes.h:378
List * rowMarks
Definition plannodes.h:364
OnConflictAction onConflictAction
Definition plannodes.h:368
Index exclRelRTI
Definition plannodes.h:382
List * nestParams
Definition plannodes.h:1012
Join join
Definition plannodes.h:1010
Definition nodes.h:133
OnConflictAction action
Definition primnodes.h:2395
LockClauseStrength lockStrength
Definition primnodes.h:2404
List * onConflictSet
Definition primnodes.h:2407
List * exclRelTlist
Definition primnodes.h:2412
Node * onConflictWhere
Definition primnodes.h:2410
Oid opno
Definition primnodes.h:835
List * args
Definition primnodes.h:853
ParseLoc location
Definition primnodes.h:856
List * exprs
Definition pathnodes.h:1878
NodeTag pathtype
Definition pathnodes.h:1971
Cardinality rows
Definition pathnodes.h:2005
Cost startup_cost
Definition pathnodes.h:2007
int disabled_nodes
Definition pathnodes.h:2006
Cost total_cost
Definition pathnodes.h:2008
bool parallel_aware
Definition pathnodes.h:1998
bool parallel_safe
Definition pathnodes.h:2000
struct Plan * lefttree
Definition plannodes.h:239
bool async_capable
Definition plannodes.h:227
Cost total_cost
Definition plannodes.h:205
struct Plan * righttree
Definition plannodes.h:240
Cost startup_cost
Definition plannodes.h:203
List * qual
Definition plannodes.h:237
int plan_width
Definition plannodes.h:213
bool parallel_safe
Definition plannodes.h:221
Cardinality plan_rows
Definition plannodes.h:211
int disabled_nodes
Definition plannodes.h:201
List * targetlist
Definition plannodes.h:235
List * initPlan
Definition plannodes.h:242
Query * parse
Definition pathnodes.h:309
Cardinality numGroups
Definition plannodes.h:499
List * baserestrictinfo
Definition pathnodes.h:1142
List * subplan_params
Definition pathnodes.h:1101
bool useridiscurrent
Definition pathnodes.h:1115
Relids relids
Definition pathnodes.h:1021
struct PathTarget * reltarget
Definition pathnodes.h:1045
Index relid
Definition pathnodes.h:1069
RelOptKind reloptkind
Definition pathnodes.h:1015
PlannerInfo * subroot
Definition pathnodes.h:1100
AttrNumber min_attr
Definition pathnodes.h:1075
RTEKind rtekind
Definition pathnodes.h:1073
Index security_level
Definition pathnodes.h:2923
Expr * clause
Definition pathnodes.h:2901
Node * resconstantqual
Definition plannodes.h:305
ResultType result_type
Definition plannodes.h:304
Bitmapset * relids
Definition plannodes.h:306
Plan plan
Definition plannodes.h:303
struct TableSampleClause * tablesample
Definition plannodes.h:564
Index scanrelid
Definition plannodes.h:544
Scan scan
Definition plannodes.h:553
SetOpStrategy strategy
Definition plannodes.h:1456
SetOpCmd cmd
Definition plannodes.h:1453
int numCols
Definition plannodes.h:1459
Plan plan
Definition plannodes.h:1450
Cardinality numGroups
Definition plannodes.h:1472
int numCols
Definition plannodes.h:1150
Plan plan
Definition plannodes.h:1147
SubqueryScanStatus scanstatus
Definition plannodes.h:777
Plan * subplan
Definition plannodes.h:776
TableFunc * tablefunc
Definition plannodes.h:812
List * tidrangequals
Definition plannodes.h:743
Scan scan
Definition plannodes.h:727
List * tidquals
Definition plannodes.h:729
Plan plan
Definition plannodes.h:1332
int numCols
Definition plannodes.h:1335
List * values_lists
Definition plannodes.h:801
AttrNumber varattno
Definition primnodes.h:275
int varno
Definition primnodes.h:270
Index varlevelsup
Definition primnodes.h:295
char * winname
Definition plannodes.h:1258
int partNumCols
Definition plannodes.h:1264
Oid endInRangeFunc
Definition plannodes.h:1308
Node * endOffset
Definition plannodes.h:1294
bool topWindow
Definition plannodes.h:1323
List * runConditionOrig
Definition plannodes.h:1300
Oid inRangeColl
Definition plannodes.h:1311
Node * startOffset
Definition plannodes.h:1291
List * runCondition
Definition plannodes.h:1297
Oid startInRangeFunc
Definition plannodes.h:1305
bool inRangeAsc
Definition plannodes.h:1314
Index winref
Definition plannodes.h:1261
bool inRangeNullsFirst
Definition plannodes.h:1317
int ordNumCols
Definition plannodes.h:1276
int frameOptions
Definition plannodes.h:1288
Node * startOffset
List * partitionClause
Node * endOffset
List * orderClause
Definition type.h:97
void SS_attach_initplans(PlannerInfo *root, Plan *plan)
Definition subselect.c:2536
void SS_compute_initplan_cost(List *init_plans, Cost *initplan_cost_p, bool *unsafe_initplans_p)
Definition subselect.c:2495
void SS_make_initplan_from_plan(PlannerInfo *root, PlannerInfo *subroot, Plan *plan, Param *prm)
Definition subselect.c:3312
#define FirstLowInvalidHeapAttributeNumber
Definition sysattr.h:27
#define RESTRICT_RELKIND_FOREIGN_TABLE
Definition tcopprot.h:45
static ItemArray items
Oid * extract_grouping_ops(List *groupClause)
Definition tlist.c:472
TargetEntry * tlist_member(Expr *node, List *targetlist)
Definition tlist.c:88
bool tlist_same_exprs(List *tlist1, List *tlist2)
Definition tlist.c:227
void apply_tlist_labeling(List *dest_tlist, List *src_tlist)
Definition tlist.c:327
void apply_pathtarget_labeling_to_tlist(List *tlist, PathTarget *target)
Definition tlist.c:783
AttrNumber * extract_grouping_cols(List *groupClause, List *tlist)
Definition tlist.c:523
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition tlist.c:376
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition tlist.c:354
Oid * extract_grouping_collations(List *groupClause, List *tlist)
Definition tlist.c:498
#define FirstNormalObjectId
Definition transam.h:197
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition var.c:296
bool contain_vars_returning_old_or_new(Node *node)
Definition var.c:511