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-2018, 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 <limits.h>
20 #include <math.h>
21
22 #include "access/sysattr.h"
23 #include "catalog/pg_class.h"
24 #include "foreign/fdwapi.h"
25 #include "miscadmin.h"
26 #include "nodes/extensible.h"
27 #include "nodes/makefuncs.h"
28 #include "nodes/nodeFuncs.h"
29 #include "optimizer/clauses.h"
30 #include "optimizer/cost.h"
31 #include "optimizer/paramassign.h"
32 #include "optimizer/paths.h"
33 #include "optimizer/placeholder.h"
34 #include "optimizer/plancat.h"
35 #include "optimizer/planmain.h"
36 #include "optimizer/predtest.h"
37 #include "optimizer/restrictinfo.h"
38 #include "optimizer/subselect.h"
39 #include "optimizer/tlist.h"
40 #include "optimizer/var.h"
41 #include "parser/parse_clause.h"
42 #include "parser/parsetree.h"
43 #include "partitioning/partprune.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 doens'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
75 static Plan *create_plan_recurse(PlannerInfo *root, Path *best_path,
76 int flags);
77 static Plan *create_scan_plan(PlannerInfo *root, Path *best_path,
78 int flags);
79 static List *build_path_tlist(PlannerInfo *root, Path *path);
80 static bool use_physical_tlist(PlannerInfo *root, Path *path, int flags);
81 static List *get_gating_quals(PlannerInfo *root, List *quals);
82 static Plan *create_gating_plan(PlannerInfo *root, Path *path, Plan *plan,
83 List *gating_quals);
84 static Plan *create_join_plan(PlannerInfo *root, JoinPath *best_path);
85 static Plan *create_append_plan(PlannerInfo *root, AppendPath *best_path);
86 static Plan *create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path,
87 int flags);
88 static Result *create_result_plan(PlannerInfo *root, ResultPath *best_path);
89 static ProjectSet *create_project_set_plan(PlannerInfo *root, ProjectSetPath *best_path);
90 static Material *create_material_plan(PlannerInfo *root, MaterialPath *best_path,
91 int flags);
92 static Plan *create_unique_plan(PlannerInfo *root, UniquePath *best_path,
93 int flags);
94 static Gather *create_gather_plan(PlannerInfo *root, GatherPath *best_path);
95 static Plan *create_projection_plan(PlannerInfo *root,
96 ProjectionPath *best_path,
97 int flags);
98 static Plan *inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe);
99 static Sort *create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags);
100 static Group *create_group_plan(PlannerInfo *root, GroupPath *best_path);
101 static Unique *create_upper_unique_plan(PlannerInfo *root, UpperUniquePath *best_path,
102 int flags);
103 static Agg *create_agg_plan(PlannerInfo *root, AggPath *best_path);
104 static Plan *create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path);
105 static Result *create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path);
106 static WindowAgg *create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path);
107 static SetOp *create_setop_plan(PlannerInfo *root, SetOpPath *best_path,
108 int flags);
109 static RecursiveUnion *create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path);
110 static LockRows *create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path,
111 int flags);
112 static ModifyTable *create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path);
113 static Limit *create_limit_plan(PlannerInfo *root, LimitPath *best_path,
114 int flags);
115 static SeqScan *create_seqscan_plan(PlannerInfo *root, Path *best_path,
116 List *tlist, List *scan_clauses);
117 static SampleScan *create_samplescan_plan(PlannerInfo *root, Path *best_path,
118 List *tlist, List *scan_clauses);
119 static Scan *create_indexscan_plan(PlannerInfo *root, IndexPath *best_path,
120 List *tlist, List *scan_clauses, bool indexonly);
121 static BitmapHeapScan *create_bitmap_scan_plan(PlannerInfo *root,
122 BitmapHeapPath *best_path,
123 List *tlist, List *scan_clauses);
124 static Plan *create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
125 List **qual, List **indexqual, List **indexECs);
126 static void bitmap_subplan_mark_shared(Plan *plan);
127 static List *flatten_partitioned_rels(List *partitioned_rels);
128 static TidScan *create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
129 List *tlist, List *scan_clauses);
130 static SubqueryScan *create_subqueryscan_plan(PlannerInfo *root,
131 SubqueryScanPath *best_path,
132 List *tlist, List *scan_clauses);
133 static FunctionScan *create_functionscan_plan(PlannerInfo *root, Path *best_path,
134 List *tlist, List *scan_clauses);
135 static ValuesScan *create_valuesscan_plan(PlannerInfo *root, Path *best_path,
136 List *tlist, List *scan_clauses);
137 static TableFuncScan *create_tablefuncscan_plan(PlannerInfo *root, Path *best_path,
138 List *tlist, List *scan_clauses);
139 static CteScan *create_ctescan_plan(PlannerInfo *root, Path *best_path,
140 List *tlist, List *scan_clauses);
141 static NamedTuplestoreScan *create_namedtuplestorescan_plan(PlannerInfo *root,
142 Path *best_path, List *tlist, List *scan_clauses);
143 static WorkTableScan *create_worktablescan_plan(PlannerInfo *root, Path *best_path,
144 List *tlist, List *scan_clauses);
145 static ForeignScan *create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
146 List *tlist, List *scan_clauses);
147 static CustomScan *create_customscan_plan(PlannerInfo *root,
148 CustomPath *best_path,
149 List *tlist, List *scan_clauses);
150 static NestLoop *create_nestloop_plan(PlannerInfo *root, NestPath *best_path);
151 static MergeJoin *create_mergejoin_plan(PlannerInfo *root, MergePath *best_path);
152 static HashJoin *create_hashjoin_plan(PlannerInfo *root, HashPath *best_path);
153 static Node *replace_nestloop_params(PlannerInfo *root, Node *expr);
154 static Node *replace_nestloop_params_mutator(Node *node, PlannerInfo *root);
155 static List *fix_indexqual_references(PlannerInfo *root, IndexPath *index_path);
156 static List *fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path);
157 static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol);
158 static List *get_switched_clauses(List *clauses, Relids outerrelids);
159 static List *order_qual_clauses(PlannerInfo *root, List *clauses);
160 static void copy_generic_path_info(Plan *dest, Path *src);
161 static void copy_plan_costsize(Plan *dest, Plan *src);
162 static void label_sort_with_costsize(PlannerInfo *root, Sort *plan,
163 double limit_tuples);
164 static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid);
165 static SampleScan *make_samplescan(List *qptlist, List *qpqual, Index scanrelid,
166 TableSampleClause *tsc);
167 static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
168 Oid indexid, List *indexqual, List *indexqualorig,
169 List *indexorderby, List *indexorderbyorig,
170 List *indexorderbyops,
171 ScanDirection indexscandir);
172 static IndexOnlyScan *make_indexonlyscan(List *qptlist, List *qpqual,
173 Index scanrelid, Oid indexid,
174 List *indexqual, List *indexorderby,
175 List *indextlist,
176 ScanDirection indexscandir);
177 static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
178 List *indexqual,
179 List *indexqualorig);
180 static BitmapHeapScan *make_bitmap_heapscan(List *qptlist,
181 List *qpqual,
182 Plan *lefttree,
183 List *bitmapqualorig,
184 Index scanrelid);
185 static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
186 List *tidquals);
187 static SubqueryScan *make_subqueryscan(List *qptlist,
188 List *qpqual,
189 Index scanrelid,
190 Plan *subplan);
191 static FunctionScan *make_functionscan(List *qptlist, List *qpqual,
192 Index scanrelid, List *functions, bool funcordinality);
193 static ValuesScan *make_valuesscan(List *qptlist, List *qpqual,
194 Index scanrelid, List *values_lists);
195 static TableFuncScan *make_tablefuncscan(List *qptlist, List *qpqual,
196 Index scanrelid, TableFunc *tablefunc);
197 static CteScan *make_ctescan(List *qptlist, List *qpqual,
198 Index scanrelid, int ctePlanId, int cteParam);
199 static NamedTuplestoreScan *make_namedtuplestorescan(List *qptlist, List *qpqual,
200 Index scanrelid, char *enrname);
201 static WorkTableScan *make_worktablescan(List *qptlist, List *qpqual,
202 Index scanrelid, int wtParam);
203 static Append *make_append(List *appendplans, int first_partial_plan,
204 List *tlist, List *partitioned_rels,
205 PartitionPruneInfo *partpruneinfo);
206 static RecursiveUnion *make_recursive_union(List *tlist,
207 Plan *lefttree,
208 Plan *righttree,
209 int wtParam,
210 List *distinctList,
211 long numGroups);
212 static BitmapAnd *make_bitmap_and(List *bitmapplans);
213 static BitmapOr *make_bitmap_or(List *bitmapplans);
214 static NestLoop *make_nestloop(List *tlist,
215 List *joinclauses, List *otherclauses, List *nestParams,
216 Plan *lefttree, Plan *righttree,
217 JoinType jointype, bool inner_unique);
218 static HashJoin *make_hashjoin(List *tlist,
219 List *joinclauses, List *otherclauses,
220 List *hashclauses,
221 Plan *lefttree, Plan *righttree,
222 JoinType jointype, bool inner_unique);
223 static Hash *make_hash(Plan *lefttree,
224 Oid skewTable,
225 AttrNumber skewColumn,
226 bool skewInherit);
227 static MergeJoin *make_mergejoin(List *tlist,
228 List *joinclauses, List *otherclauses,
229 List *mergeclauses,
230 Oid *mergefamilies,
231 Oid *mergecollations,
232 int *mergestrategies,
233 bool *mergenullsfirst,
234 Plan *lefttree, Plan *righttree,
235 JoinType jointype, bool inner_unique,
236 bool skip_mark_restore);
237 static Sort *make_sort(Plan *lefttree, int numCols,
238 AttrNumber *sortColIdx, Oid *sortOperators,
239 Oid *collations, bool *nullsFirst);
240 static Plan *prepare_sort_from_pathkeys(Plan *lefttree, List *pathkeys,
241 Relids relids,
242 const AttrNumber *reqColIdx,
243 bool adjust_tlist_in_place,
244 int *p_numsortkeys,
245 AttrNumber **p_sortColIdx,
246 Oid **p_sortOperators,
247 Oid **p_collations,
248 bool **p_nullsFirst);
249 static EquivalenceMember *find_ec_member_for_tle(EquivalenceClass *ec,
250 TargetEntry *tle,
251 Relids relids);
252 static Sort *make_sort_from_pathkeys(Plan *lefttree, List *pathkeys,
253 Relids relids);
254 static Sort *make_sort_from_groupcols(List *groupcls,
255 AttrNumber *grpColIdx,
256 Plan *lefttree);
257 static Material *make_material(Plan *lefttree);
258 static WindowAgg *make_windowagg(List *tlist, Index winref,
259 int partNumCols, AttrNumber *partColIdx, Oid *partOperators,
260 int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators,
261 int frameOptions, Node *startOffset, Node *endOffset,
262 Oid startInRangeFunc, Oid endInRangeFunc,
263 Oid inRangeColl, bool inRangeAsc, bool inRangeNullsFirst,
264 Plan *lefttree);
265 static Group *make_group(List *tlist, List *qual, int numGroupCols,
266 AttrNumber *grpColIdx, Oid *grpOperators,
267 Plan *lefttree);
268 static Unique *make_unique_from_sortclauses(Plan *lefttree, List *distinctList);
269 static Unique *make_unique_from_pathkeys(Plan *lefttree,
270 List *pathkeys, int numCols);
271 static Gather *make_gather(List *qptlist, List *qpqual,
272 int nworkers, int rescan_param, bool single_copy, Plan *subplan);
273 static SetOp *make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
274 List *distinctList, AttrNumber flagColIdx, int firstFlag,
275 long numGroups);
276 static LockRows *make_lockrows(Plan *lefttree, List *rowMarks, int epqParam);
277 static Result *make_result(List *tlist, Node *resconstantqual, Plan *subplan);
278 static ProjectSet *make_project_set(List *tlist, Plan *subplan);
279 static ModifyTable *make_modifytable(PlannerInfo *root,
280 CmdType operation, bool canSetTag,
281 Index nominalRelation, List *partitioned_rels,
282 bool partColsUpdated,
283 List *resultRelations, List *subplans, List *subroots,
284 List *withCheckOptionLists, List *returningLists,
285 List *rowMarks, OnConflictExpr *onconflict, int epqParam);
286 static GatherMerge *create_gather_merge_plan(PlannerInfo *root,
287 GatherMergePath *best_path);
288
289
290 /*
291 * create_plan
292 * Creates the access plan for a query by recursively processing the
293 * desired tree of pathnodes, starting at the node 'best_path'. For
294 * every pathnode found, we create a corresponding plan node containing
295 * appropriate id, target list, and qualification information.
296 *
297 * The tlists and quals in the plan tree are still in planner format,
298 * ie, Vars still correspond to the parser's numbering. This will be
299 * fixed later by setrefs.c.
300 *
301 * best_path is the best access path
302 *
303 * Returns a Plan tree.
304 */
305 Plan *
create_plan(PlannerInfo * root,Path * best_path)306 create_plan(PlannerInfo *root, Path *best_path)
307 {
308 Plan *plan;
309
310 /* plan_params should not be in use in current query level */
311 Assert(root->plan_params == NIL);
312
313 /* Initialize this module's workspace in PlannerInfo */
314 root->curOuterRels = NULL;
315 root->curOuterParams = NIL;
316
317 /* Recursively process the path tree, demanding the correct tlist result */
318 plan = create_plan_recurse(root, best_path, CP_EXACT_TLIST);
319
320 /*
321 * Make sure the topmost plan node's targetlist exposes the original
322 * column names and other decorative info. Targetlists generated within
323 * the planner don't bother with that stuff, but we must have it on the
324 * top-level tlist seen at execution time. However, ModifyTable plan
325 * nodes don't have a tlist matching the querytree targetlist.
326 */
327 if (!IsA(plan, ModifyTable))
328 apply_tlist_labeling(plan->targetlist, root->processed_tlist);
329
330 /*
331 * Attach any initPlans created in this query level to the topmost plan
332 * node. (In principle the initplans could go in any plan node at or
333 * above where they're referenced, but there seems no reason to put them
334 * any lower than the topmost node for the query level. Also, see
335 * comments for SS_finalize_plan before you try to change this.)
336 */
337 SS_attach_initplans(root, plan);
338
339 /* Check we successfully assigned all NestLoopParams to plan nodes */
340 if (root->curOuterParams != NIL)
341 elog(ERROR, "failed to assign all NestLoopParams to plan nodes");
342
343 /*
344 * Reset plan_params to ensure param IDs used for nestloop params are not
345 * re-used later
346 */
347 root->plan_params = NIL;
348
349 return plan;
350 }
351
352 /*
353 * create_plan_recurse
354 * Recursive guts of create_plan().
355 */
356 static Plan *
create_plan_recurse(PlannerInfo * root,Path * best_path,int flags)357 create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
358 {
359 Plan *plan;
360
361 /* Guard against stack overflow due to overly complex plans */
362 check_stack_depth();
363
364 switch (best_path->pathtype)
365 {
366 case T_SeqScan:
367 case T_SampleScan:
368 case T_IndexScan:
369 case T_IndexOnlyScan:
370 case T_BitmapHeapScan:
371 case T_TidScan:
372 case T_SubqueryScan:
373 case T_FunctionScan:
374 case T_TableFuncScan:
375 case T_ValuesScan:
376 case T_CteScan:
377 case T_WorkTableScan:
378 case T_NamedTuplestoreScan:
379 case T_ForeignScan:
380 case T_CustomScan:
381 plan = create_scan_plan(root, best_path, flags);
382 break;
383 case T_HashJoin:
384 case T_MergeJoin:
385 case T_NestLoop:
386 plan = create_join_plan(root,
387 (JoinPath *) best_path);
388 break;
389 case T_Append:
390 plan = create_append_plan(root,
391 (AppendPath *) best_path);
392 break;
393 case T_MergeAppend:
394 plan = create_merge_append_plan(root,
395 (MergeAppendPath *) best_path,
396 flags);
397 break;
398 case T_Result:
399 if (IsA(best_path, ProjectionPath))
400 {
401 plan = create_projection_plan(root,
402 (ProjectionPath *) best_path,
403 flags);
404 }
405 else if (IsA(best_path, MinMaxAggPath))
406 {
407 plan = (Plan *) create_minmaxagg_plan(root,
408 (MinMaxAggPath *) best_path);
409 }
410 else
411 {
412 Assert(IsA(best_path, ResultPath));
413 plan = (Plan *) create_result_plan(root,
414 (ResultPath *) best_path);
415 }
416 break;
417 case T_ProjectSet:
418 plan = (Plan *) create_project_set_plan(root,
419 (ProjectSetPath *) best_path);
420 break;
421 case T_Material:
422 plan = (Plan *) create_material_plan(root,
423 (MaterialPath *) best_path,
424 flags);
425 break;
426 case T_Unique:
427 if (IsA(best_path, UpperUniquePath))
428 {
429 plan = (Plan *) create_upper_unique_plan(root,
430 (UpperUniquePath *) best_path,
431 flags);
432 }
433 else
434 {
435 Assert(IsA(best_path, UniquePath));
436 plan = create_unique_plan(root,
437 (UniquePath *) best_path,
438 flags);
439 }
440 break;
441 case T_Gather:
442 plan = (Plan *) create_gather_plan(root,
443 (GatherPath *) best_path);
444 break;
445 case T_Sort:
446 plan = (Plan *) create_sort_plan(root,
447 (SortPath *) best_path,
448 flags);
449 break;
450 case T_Group:
451 plan = (Plan *) create_group_plan(root,
452 (GroupPath *) best_path);
453 break;
454 case T_Agg:
455 if (IsA(best_path, GroupingSetsPath))
456 plan = create_groupingsets_plan(root,
457 (GroupingSetsPath *) best_path);
458 else
459 {
460 Assert(IsA(best_path, AggPath));
461 plan = (Plan *) create_agg_plan(root,
462 (AggPath *) best_path);
463 }
464 break;
465 case T_WindowAgg:
466 plan = (Plan *) create_windowagg_plan(root,
467 (WindowAggPath *) best_path);
468 break;
469 case T_SetOp:
470 plan = (Plan *) create_setop_plan(root,
471 (SetOpPath *) best_path,
472 flags);
473 break;
474 case T_RecursiveUnion:
475 plan = (Plan *) create_recursiveunion_plan(root,
476 (RecursiveUnionPath *) best_path);
477 break;
478 case T_LockRows:
479 plan = (Plan *) create_lockrows_plan(root,
480 (LockRowsPath *) best_path,
481 flags);
482 break;
483 case T_ModifyTable:
484 plan = (Plan *) create_modifytable_plan(root,
485 (ModifyTablePath *) best_path);
486 break;
487 case T_Limit:
488 plan = (Plan *) create_limit_plan(root,
489 (LimitPath *) best_path,
490 flags);
491 break;
492 case T_GatherMerge:
493 plan = (Plan *) create_gather_merge_plan(root,
494 (GatherMergePath *) best_path);
495 break;
496 default:
497 elog(ERROR, "unrecognized node type: %d",
498 (int) best_path->pathtype);
499 plan = NULL; /* keep compiler quiet */
500 break;
501 }
502
503 return plan;
504 }
505
506 /*
507 * create_scan_plan
508 * Create a scan plan for the parent relation of 'best_path'.
509 */
510 static Plan *
create_scan_plan(PlannerInfo * root,Path * best_path,int flags)511 create_scan_plan(PlannerInfo *root, Path *best_path, int flags)
512 {
513 RelOptInfo *rel = best_path->parent;
514 List *scan_clauses;
515 List *gating_clauses;
516 List *tlist;
517 Plan *plan;
518
519 /*
520 * Extract the relevant restriction clauses from the parent relation. The
521 * executor must apply all these restrictions during the scan, except for
522 * pseudoconstants which we'll take care of below.
523 *
524 * If this is a plain indexscan or index-only scan, we need not consider
525 * restriction clauses that are implied by the index's predicate, so use
526 * indrestrictinfo not baserestrictinfo. Note that we can't do that for
527 * bitmap indexscans, since there's not necessarily a single index
528 * involved; but it doesn't matter since create_bitmap_scan_plan() will be
529 * able to get rid of such clauses anyway via predicate proof.
530 */
531 switch (best_path->pathtype)
532 {
533 case T_IndexScan:
534 case T_IndexOnlyScan:
535 scan_clauses = castNode(IndexPath, best_path)->indexinfo->indrestrictinfo;
536 break;
537 default:
538 scan_clauses = rel->baserestrictinfo;
539 break;
540 }
541
542 /*
543 * If this is a parameterized scan, we also need to enforce all the join
544 * clauses available from the outer relation(s).
545 *
546 * For paranoia's sake, don't modify the stored baserestrictinfo list.
547 */
548 if (best_path->param_info)
549 scan_clauses = list_concat(list_copy(scan_clauses),
550 best_path->param_info->ppi_clauses);
551
552 /*
553 * Detect whether we have any pseudoconstant quals to deal with. Then, if
554 * we'll need a gating Result node, it will be able to project, so there
555 * are no requirements on the child's tlist.
556 */
557 gating_clauses = get_gating_quals(root, scan_clauses);
558 if (gating_clauses)
559 flags = 0;
560
561 /*
562 * For table scans, rather than using the relation targetlist (which is
563 * only those Vars actually needed by the query), we prefer to generate a
564 * tlist containing all Vars in order. This will allow the executor to
565 * optimize away projection of the table tuples, if possible.
566 *
567 * But if the caller is going to ignore our tlist anyway, then don't
568 * bother generating one at all. We use an exact equality test here, so
569 * that this only applies when CP_IGNORE_TLIST is the only flag set.
570 */
571 if (flags == CP_IGNORE_TLIST)
572 {
573 tlist = NULL;
574 }
575 else if (use_physical_tlist(root, best_path, flags))
576 {
577 if (best_path->pathtype == T_IndexOnlyScan)
578 {
579 /* For index-only scan, the preferred tlist is the index's */
580 tlist = copyObject(((IndexPath *) best_path)->indexinfo->indextlist);
581
582 /*
583 * Transfer sortgroupref data to the replacement tlist, if
584 * requested (use_physical_tlist checked that this will work).
585 */
586 if (flags & CP_LABEL_TLIST)
587 apply_pathtarget_labeling_to_tlist(tlist, best_path->pathtarget);
588 }
589 else
590 {
591 tlist = build_physical_tlist(root, rel);
592 if (tlist == NIL)
593 {
594 /* Failed because of dropped cols, so use regular method */
595 tlist = build_path_tlist(root, best_path);
596 }
597 else
598 {
599 /* As above, transfer sortgroupref data to replacement tlist */
600 if (flags & CP_LABEL_TLIST)
601 apply_pathtarget_labeling_to_tlist(tlist, best_path->pathtarget);
602 }
603 }
604 }
605 else
606 {
607 tlist = build_path_tlist(root, best_path);
608 }
609
610 switch (best_path->pathtype)
611 {
612 case T_SeqScan:
613 plan = (Plan *) create_seqscan_plan(root,
614 best_path,
615 tlist,
616 scan_clauses);
617 break;
618
619 case T_SampleScan:
620 plan = (Plan *) create_samplescan_plan(root,
621 best_path,
622 tlist,
623 scan_clauses);
624 break;
625
626 case T_IndexScan:
627 plan = (Plan *) create_indexscan_plan(root,
628 (IndexPath *) best_path,
629 tlist,
630 scan_clauses,
631 false);
632 break;
633
634 case T_IndexOnlyScan:
635 plan = (Plan *) create_indexscan_plan(root,
636 (IndexPath *) best_path,
637 tlist,
638 scan_clauses,
639 true);
640 break;
641
642 case T_BitmapHeapScan:
643 plan = (Plan *) create_bitmap_scan_plan(root,
644 (BitmapHeapPath *) best_path,
645 tlist,
646 scan_clauses);
647 break;
648
649 case T_TidScan:
650 plan = (Plan *) create_tidscan_plan(root,
651 (TidPath *) best_path,
652 tlist,
653 scan_clauses);
654 break;
655
656 case T_SubqueryScan:
657 plan = (Plan *) create_subqueryscan_plan(root,
658 (SubqueryScanPath *) best_path,
659 tlist,
660 scan_clauses);
661 break;
662
663 case T_FunctionScan:
664 plan = (Plan *) create_functionscan_plan(root,
665 best_path,
666 tlist,
667 scan_clauses);
668 break;
669
670 case T_TableFuncScan:
671 plan = (Plan *) create_tablefuncscan_plan(root,
672 best_path,
673 tlist,
674 scan_clauses);
675 break;
676
677 case T_ValuesScan:
678 plan = (Plan *) create_valuesscan_plan(root,
679 best_path,
680 tlist,
681 scan_clauses);
682 break;
683
684 case T_CteScan:
685 plan = (Plan *) create_ctescan_plan(root,
686 best_path,
687 tlist,
688 scan_clauses);
689 break;
690
691 case T_NamedTuplestoreScan:
692 plan = (Plan *) create_namedtuplestorescan_plan(root,
693 best_path,
694 tlist,
695 scan_clauses);
696 break;
697
698 case T_WorkTableScan:
699 plan = (Plan *) create_worktablescan_plan(root,
700 best_path,
701 tlist,
702 scan_clauses);
703 break;
704
705 case T_ForeignScan:
706 plan = (Plan *) create_foreignscan_plan(root,
707 (ForeignPath *) best_path,
708 tlist,
709 scan_clauses);
710 break;
711
712 case T_CustomScan:
713 plan = (Plan *) create_customscan_plan(root,
714 (CustomPath *) best_path,
715 tlist,
716 scan_clauses);
717 break;
718
719 default:
720 elog(ERROR, "unrecognized node type: %d",
721 (int) best_path->pathtype);
722 plan = NULL; /* keep compiler quiet */
723 break;
724 }
725
726 /*
727 * If there are any pseudoconstant clauses attached to this node, insert a
728 * gating Result node that evaluates the pseudoconstants as one-time
729 * quals.
730 */
731 if (gating_clauses)
732 plan = create_gating_plan(root, best_path, plan, gating_clauses);
733
734 return plan;
735 }
736
737 /*
738 * Build a target list (ie, a list of TargetEntry) for the Path's output.
739 *
740 * This is almost just make_tlist_from_pathtarget(), but we also have to
741 * deal with replacing nestloop params.
742 */
743 static List *
build_path_tlist(PlannerInfo * root,Path * path)744 build_path_tlist(PlannerInfo *root, Path *path)
745 {
746 List *tlist = NIL;
747 Index *sortgrouprefs = path->pathtarget->sortgrouprefs;
748 int resno = 1;
749 ListCell *v;
750
751 foreach(v, path->pathtarget->exprs)
752 {
753 Node *node = (Node *) lfirst(v);
754 TargetEntry *tle;
755
756 /*
757 * If it's a parameterized path, there might be lateral references in
758 * the tlist, which need to be replaced with Params. There's no need
759 * to remake the TargetEntry nodes, so apply this to each list item
760 * separately.
761 */
762 if (path->param_info)
763 node = replace_nestloop_params(root, node);
764
765 tle = makeTargetEntry((Expr *) node,
766 resno,
767 NULL,
768 false);
769 if (sortgrouprefs)
770 tle->ressortgroupref = sortgrouprefs[resno - 1];
771
772 tlist = lappend(tlist, tle);
773 resno++;
774 }
775 return tlist;
776 }
777
778 /*
779 * use_physical_tlist
780 * Decide whether to use a tlist matching relation structure,
781 * rather than only those Vars actually referenced.
782 */
783 static bool
use_physical_tlist(PlannerInfo * root,Path * path,int flags)784 use_physical_tlist(PlannerInfo *root, Path *path, int flags)
785 {
786 RelOptInfo *rel = path->parent;
787 int i;
788 ListCell *lc;
789
790 /*
791 * Forget it if either exact tlist or small tlist is demanded.
792 */
793 if (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST))
794 return false;
795
796 /*
797 * We can do this for real relation scans, subquery scans, function scans,
798 * tablefunc scans, values scans, and CTE scans (but not for, eg, joins).
799 */
800 if (rel->rtekind != RTE_RELATION &&
801 rel->rtekind != RTE_SUBQUERY &&
802 rel->rtekind != RTE_FUNCTION &&
803 rel->rtekind != RTE_TABLEFUNC &&
804 rel->rtekind != RTE_VALUES &&
805 rel->rtekind != RTE_CTE)
806 return false;
807
808 /*
809 * Can't do it with inheritance cases either (mainly because Append
810 * doesn't project; this test may be unnecessary now that
811 * create_append_plan instructs its children to return an exact tlist).
812 */
813 if (rel->reloptkind != RELOPT_BASEREL)
814 return false;
815
816 /*
817 * Also, don't do it to a CustomPath; the premise that we're extracting
818 * columns from a simple physical tuple is unlikely to hold for those.
819 * (When it does make sense, the custom path creator can set up the path's
820 * pathtarget that way.)
821 */
822 if (IsA(path, CustomPath))
823 return false;
824
825 /*
826 * If a bitmap scan's tlist is empty, keep it as-is. This may allow the
827 * executor to skip heap page fetches, and in any case, the benefit of
828 * using a physical tlist instead would be minimal.
829 */
830 if (IsA(path, BitmapHeapPath) &&
831 path->pathtarget->exprs == NIL)
832 return false;
833
834 /*
835 * Can't do it if any system columns or whole-row Vars are requested.
836 * (This could possibly be fixed but would take some fragile assumptions
837 * in setrefs.c, I think.)
838 */
839 for (i = rel->min_attr; i <= 0; i++)
840 {
841 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
842 return false;
843 }
844
845 /*
846 * Can't do it if the rel is required to emit any placeholder expressions,
847 * either.
848 */
849 foreach(lc, root->placeholder_list)
850 {
851 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
852
853 if (bms_nonempty_difference(phinfo->ph_needed, rel->relids) &&
854 bms_is_subset(phinfo->ph_eval_at, rel->relids))
855 return false;
856 }
857
858 /*
859 * Also, can't do it if CP_LABEL_TLIST is specified and path is requested
860 * to emit any sort/group columns that are not simple Vars. (If they are
861 * simple Vars, they should appear in the physical tlist, and
862 * apply_pathtarget_labeling_to_tlist will take care of getting them
863 * labeled again.) We also have to check that no two sort/group columns
864 * are the same Var, else that element of the physical tlist would need
865 * conflicting ressortgroupref labels.
866 */
867 if ((flags & CP_LABEL_TLIST) && path->pathtarget->sortgrouprefs)
868 {
869 Bitmapset *sortgroupatts = NULL;
870
871 i = 0;
872 foreach(lc, path->pathtarget->exprs)
873 {
874 Expr *expr = (Expr *) lfirst(lc);
875
876 if (path->pathtarget->sortgrouprefs[i])
877 {
878 if (expr && IsA(expr, Var))
879 {
880 int attno = ((Var *) expr)->varattno;
881
882 attno -= FirstLowInvalidHeapAttributeNumber;
883 if (bms_is_member(attno, sortgroupatts))
884 return false;
885 sortgroupatts = bms_add_member(sortgroupatts, attno);
886 }
887 else
888 return false;
889 }
890 i++;
891 }
892 }
893
894 return true;
895 }
896
897 /*
898 * get_gating_quals
899 * See if there are pseudoconstant quals in a node's quals list
900 *
901 * If the node's quals list includes any pseudoconstant quals,
902 * return just those quals.
903 */
904 static List *
get_gating_quals(PlannerInfo * root,List * quals)905 get_gating_quals(PlannerInfo *root, List *quals)
906 {
907 /* No need to look if we know there are no pseudoconstants */
908 if (!root->hasPseudoConstantQuals)
909 return NIL;
910
911 /* Sort into desirable execution order while still in RestrictInfo form */
912 quals = order_qual_clauses(root, quals);
913
914 /* Pull out any pseudoconstant quals from the RestrictInfo list */
915 return extract_actual_clauses(quals, true);
916 }
917
918 /*
919 * create_gating_plan
920 * Deal with pseudoconstant qual clauses
921 *
922 * Add a gating Result node atop the already-built plan.
923 */
924 static Plan *
create_gating_plan(PlannerInfo * root,Path * path,Plan * plan,List * gating_quals)925 create_gating_plan(PlannerInfo *root, Path *path, Plan *plan,
926 List *gating_quals)
927 {
928 Plan *gplan;
929
930 Assert(gating_quals);
931
932 /*
933 * Since we need a Result node anyway, always return the path's requested
934 * tlist; that's never a wrong choice, even if the parent node didn't ask
935 * for CP_EXACT_TLIST.
936 */
937 gplan = (Plan *) make_result(build_path_tlist(root, path),
938 (Node *) gating_quals,
939 plan);
940
941 /*
942 * Notice that we don't change cost or size estimates when doing gating.
943 * The costs of qual eval were already included in the subplan's cost.
944 * Leaving the size alone amounts to assuming that the gating qual will
945 * succeed, which is the conservative estimate for planning upper queries.
946 * We certainly don't want to assume the output size is zero (unless the
947 * gating qual is actually constant FALSE, and that case is dealt with in
948 * clausesel.c). Interpolating between the two cases is silly, because it
949 * doesn't reflect what will really happen at runtime, and besides which
950 * in most cases we have only a very bad idea of the probability of the
951 * gating qual being true.
952 */
953 copy_plan_costsize(gplan, plan);
954
955 /* Gating quals could be unsafe, so better use the Path's safety flag */
956 gplan->parallel_safe = path->parallel_safe;
957
958 return gplan;
959 }
960
961 /*
962 * create_join_plan
963 * Create a join plan for 'best_path' and (recursively) plans for its
964 * inner and outer paths.
965 */
966 static Plan *
create_join_plan(PlannerInfo * root,JoinPath * best_path)967 create_join_plan(PlannerInfo *root, JoinPath *best_path)
968 {
969 Plan *plan;
970 List *gating_clauses;
971
972 switch (best_path->path.pathtype)
973 {
974 case T_MergeJoin:
975 plan = (Plan *) create_mergejoin_plan(root,
976 (MergePath *) best_path);
977 break;
978 case T_HashJoin:
979 plan = (Plan *) create_hashjoin_plan(root,
980 (HashPath *) best_path);
981 break;
982 case T_NestLoop:
983 plan = (Plan *) create_nestloop_plan(root,
984 (NestPath *) best_path);
985 break;
986 default:
987 elog(ERROR, "unrecognized node type: %d",
988 (int) best_path->path.pathtype);
989 plan = NULL; /* keep compiler quiet */
990 break;
991 }
992
993 /*
994 * If there are any pseudoconstant clauses attached to this node, insert a
995 * gating Result node that evaluates the pseudoconstants as one-time
996 * quals.
997 */
998 gating_clauses = get_gating_quals(root, best_path->joinrestrictinfo);
999 if (gating_clauses)
1000 plan = create_gating_plan(root, (Path *) best_path, plan,
1001 gating_clauses);
1002
1003 #ifdef NOT_USED
1004
1005 /*
1006 * * Expensive function pullups may have pulled local predicates * into
1007 * this path node. Put them in the qpqual of the plan node. * JMH,
1008 * 6/15/92
1009 */
1010 if (get_loc_restrictinfo(best_path) != NIL)
1011 set_qpqual((Plan) plan,
1012 list_concat(get_qpqual((Plan) plan),
1013 get_actual_clauses(get_loc_restrictinfo(best_path))));
1014 #endif
1015
1016 return plan;
1017 }
1018
1019 /*
1020 * create_append_plan
1021 * Create an Append plan for 'best_path' and (recursively) plans
1022 * for its subpaths.
1023 *
1024 * Returns a Plan node.
1025 */
1026 static Plan *
create_append_plan(PlannerInfo * root,AppendPath * best_path)1027 create_append_plan(PlannerInfo *root, AppendPath *best_path)
1028 {
1029 Append *plan;
1030 List *tlist = build_path_tlist(root, &best_path->path);
1031 List *subplans = NIL;
1032 ListCell *subpaths;
1033 RelOptInfo *rel = best_path->path.parent;
1034 PartitionPruneInfo *partpruneinfo = NULL;
1035
1036 /*
1037 * The subpaths list could be empty, if every child was proven empty by
1038 * constraint exclusion. In that case generate a dummy plan that returns
1039 * no rows.
1040 *
1041 * Note that an AppendPath with no members is also generated in certain
1042 * cases where there was no appending construct at all, but we know the
1043 * relation is empty (see set_dummy_rel_pathlist and mark_dummy_rel).
1044 */
1045 if (best_path->subpaths == NIL)
1046 {
1047 /* Generate a Result plan with constant-FALSE gating qual */
1048 Plan *plan;
1049
1050 plan = (Plan *) make_result(tlist,
1051 (Node *) list_make1(makeBoolConst(false,
1052 false)),
1053 NULL);
1054
1055 copy_generic_path_info(plan, (Path *) best_path);
1056
1057 return plan;
1058 }
1059
1060 /* Build the plan for each child */
1061 foreach(subpaths, best_path->subpaths)
1062 {
1063 Path *subpath = (Path *) lfirst(subpaths);
1064 Plan *subplan;
1065
1066 /* Must insist that all children return the same tlist */
1067 subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
1068
1069 subplans = lappend(subplans, subplan);
1070 }
1071
1072 /*
1073 * If any quals exist, they may be useful to perform further partition
1074 * pruning during execution. Gather information needed by the executor to
1075 * do partition pruning.
1076 */
1077 if (enable_partition_pruning &&
1078 rel->reloptkind == RELOPT_BASEREL &&
1079 best_path->partitioned_rels != NIL)
1080 {
1081 List *prunequal;
1082
1083 prunequal = extract_actual_clauses(rel->baserestrictinfo, false);
1084
1085 if (best_path->path.param_info)
1086 {
1087
1088 List *prmquals = best_path->path.param_info->ppi_clauses;
1089
1090 prmquals = extract_actual_clauses(prmquals, false);
1091 prmquals = (List *) replace_nestloop_params(root,
1092 (Node *) prmquals);
1093
1094 prunequal = list_concat(prunequal, prmquals);
1095 }
1096
1097 /*
1098 * If any quals exist, they may be useful to perform further partition
1099 * pruning during execution. Generate a PartitionPruneInfo for each
1100 * partitioned rel to store these quals and allow translation of
1101 * partition indexes into subpath indexes.
1102 */
1103 if (prunequal != NIL)
1104 partpruneinfo =
1105 make_partition_pruneinfo(root, rel,
1106 best_path->subpaths,
1107 best_path->partitioned_rels,
1108 prunequal);
1109 }
1110
1111 /*
1112 * XXX ideally, if there's just one child, we'd not bother to generate an
1113 * Append node but just return the single child. At the moment this does
1114 * not work because the varno of the child scan plan won't match the
1115 * parent-rel Vars it'll be asked to emit.
1116 */
1117
1118 plan = make_append(subplans, best_path->first_partial_path,
1119 tlist, best_path->partitioned_rels,
1120 partpruneinfo);
1121
1122 copy_generic_path_info(&plan->plan, (Path *) best_path);
1123
1124 return (Plan *) plan;
1125 }
1126
1127 /*
1128 * create_merge_append_plan
1129 * Create a MergeAppend plan for 'best_path' and (recursively) plans
1130 * for its subpaths.
1131 *
1132 * Returns a Plan node.
1133 */
1134 static Plan *
create_merge_append_plan(PlannerInfo * root,MergeAppendPath * best_path,int flags)1135 create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path,
1136 int flags)
1137 {
1138 MergeAppend *node = makeNode(MergeAppend);
1139 Plan *plan = &node->plan;
1140 List *tlist = build_path_tlist(root, &best_path->path);
1141 int orig_tlist_length = list_length(tlist);
1142 bool tlist_was_changed;
1143 List *pathkeys = best_path->path.pathkeys;
1144 List *subplans = NIL;
1145 ListCell *subpaths;
1146
1147 /*
1148 * We don't have the actual creation of the MergeAppend node split out
1149 * into a separate make_xxx function. This is because we want to run
1150 * prepare_sort_from_pathkeys on it before we do so on the individual
1151 * child plans, to make cross-checking the sort info easier.
1152 */
1153 copy_generic_path_info(plan, (Path *) best_path);
1154 plan->targetlist = tlist;
1155 plan->qual = NIL;
1156 plan->lefttree = NULL;
1157 plan->righttree = NULL;
1158
1159 /*
1160 * Compute sort column info, and adjust MergeAppend's tlist as needed.
1161 * Because we pass adjust_tlist_in_place = true, we may ignore the
1162 * function result; it must be the same plan node. However, we then need
1163 * to detect whether any tlist entries were added.
1164 */
1165 (void) prepare_sort_from_pathkeys(plan, pathkeys,
1166 best_path->path.parent->relids,
1167 NULL,
1168 true,
1169 &node->numCols,
1170 &node->sortColIdx,
1171 &node->sortOperators,
1172 &node->collations,
1173 &node->nullsFirst);
1174 tlist_was_changed = (orig_tlist_length != list_length(plan->targetlist));
1175
1176 /*
1177 * Now prepare the child plans. We must apply prepare_sort_from_pathkeys
1178 * even to subplans that don't need an explicit sort, to make sure they
1179 * are returning the same sort key columns the MergeAppend expects.
1180 */
1181 foreach(subpaths, best_path->subpaths)
1182 {
1183 Path *subpath = (Path *) lfirst(subpaths);
1184 Plan *subplan;
1185 int numsortkeys;
1186 AttrNumber *sortColIdx;
1187 Oid *sortOperators;
1188 Oid *collations;
1189 bool *nullsFirst;
1190
1191 /* Build the child plan */
1192 /* Must insist that all children return the same tlist */
1193 subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
1194
1195 /* Compute sort column info, and adjust subplan's tlist as needed */
1196 subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1197 subpath->parent->relids,
1198 node->sortColIdx,
1199 false,
1200 &numsortkeys,
1201 &sortColIdx,
1202 &sortOperators,
1203 &collations,
1204 &nullsFirst);
1205
1206 /*
1207 * Check that we got the same sort key information. We just Assert
1208 * that the sortops match, since those depend only on the pathkeys;
1209 * but it seems like a good idea to check the sort column numbers
1210 * explicitly, to ensure the tlists really do match up.
1211 */
1212 Assert(numsortkeys == node->numCols);
1213 if (memcmp(sortColIdx, node->sortColIdx,
1214 numsortkeys * sizeof(AttrNumber)) != 0)
1215 elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
1216 Assert(memcmp(sortOperators, node->sortOperators,
1217 numsortkeys * sizeof(Oid)) == 0);
1218 Assert(memcmp(collations, node->collations,
1219 numsortkeys * sizeof(Oid)) == 0);
1220 Assert(memcmp(nullsFirst, node->nullsFirst,
1221 numsortkeys * sizeof(bool)) == 0);
1222
1223 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1224 if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
1225 {
1226 Sort *sort = make_sort(subplan, numsortkeys,
1227 sortColIdx, sortOperators,
1228 collations, nullsFirst);
1229
1230 label_sort_with_costsize(root, sort, best_path->limit_tuples);
1231 subplan = (Plan *) sort;
1232 }
1233
1234 subplans = lappend(subplans, subplan);
1235 }
1236
1237 node->partitioned_rels =
1238 flatten_partitioned_rels(best_path->partitioned_rels);
1239 node->mergeplans = subplans;
1240
1241 /*
1242 * If prepare_sort_from_pathkeys added sort columns, but we were told to
1243 * produce either the exact tlist or a narrow tlist, we should get rid of
1244 * the sort columns again. We must inject a projection node to do so.
1245 */
1246 if (tlist_was_changed && (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST)))
1247 {
1248 tlist = list_truncate(list_copy(plan->targetlist), orig_tlist_length);
1249 return inject_projection_plan(plan, tlist, plan->parallel_safe);
1250 }
1251 else
1252 return plan;
1253 }
1254
1255 /*
1256 * create_result_plan
1257 * Create a Result plan for 'best_path'.
1258 * This is only used for degenerate cases, such as a query with an empty
1259 * jointree.
1260 *
1261 * Returns a Plan node.
1262 */
1263 static Result *
create_result_plan(PlannerInfo * root,ResultPath * best_path)1264 create_result_plan(PlannerInfo *root, ResultPath *best_path)
1265 {
1266 Result *plan;
1267 List *tlist;
1268 List *quals;
1269
1270 tlist = build_path_tlist(root, &best_path->path);
1271
1272 /* best_path->quals is just bare clauses */
1273 quals = order_qual_clauses(root, best_path->quals);
1274
1275 plan = make_result(tlist, (Node *) quals, NULL);
1276
1277 copy_generic_path_info(&plan->plan, (Path *) best_path);
1278
1279 return plan;
1280 }
1281
1282 /*
1283 * create_project_set_plan
1284 * Create a ProjectSet plan for 'best_path'.
1285 *
1286 * Returns a Plan node.
1287 */
1288 static ProjectSet *
create_project_set_plan(PlannerInfo * root,ProjectSetPath * best_path)1289 create_project_set_plan(PlannerInfo *root, ProjectSetPath *best_path)
1290 {
1291 ProjectSet *plan;
1292 Plan *subplan;
1293 List *tlist;
1294
1295 /* Since we intend to project, we don't need to constrain child tlist */
1296 subplan = create_plan_recurse(root, best_path->subpath, 0);
1297
1298 tlist = build_path_tlist(root, &best_path->path);
1299
1300 plan = make_project_set(tlist, subplan);
1301
1302 copy_generic_path_info(&plan->plan, (Path *) best_path);
1303
1304 return plan;
1305 }
1306
1307 /*
1308 * create_material_plan
1309 * Create a Material plan for 'best_path' and (recursively) plans
1310 * for its subpaths.
1311 *
1312 * Returns a Plan node.
1313 */
1314 static Material *
create_material_plan(PlannerInfo * root,MaterialPath * best_path,int flags)1315 create_material_plan(PlannerInfo *root, MaterialPath *best_path, int flags)
1316 {
1317 Material *plan;
1318 Plan *subplan;
1319
1320 /*
1321 * We don't want any excess columns in the materialized tuples, so request
1322 * a smaller tlist. Otherwise, since Material doesn't project, tlist
1323 * requirements pass through.
1324 */
1325 subplan = create_plan_recurse(root, best_path->subpath,
1326 flags | CP_SMALL_TLIST);
1327
1328 plan = make_material(subplan);
1329
1330 copy_generic_path_info(&plan->plan, (Path *) best_path);
1331
1332 return plan;
1333 }
1334
1335 /*
1336 * create_unique_plan
1337 * Create a Unique plan for 'best_path' and (recursively) plans
1338 * for its subpaths.
1339 *
1340 * Returns a Plan node.
1341 */
1342 static Plan *
create_unique_plan(PlannerInfo * root,UniquePath * best_path,int flags)1343 create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
1344 {
1345 Plan *plan;
1346 Plan *subplan;
1347 List *in_operators;
1348 List *uniq_exprs;
1349 List *newtlist;
1350 int nextresno;
1351 bool newitems;
1352 int numGroupCols;
1353 AttrNumber *groupColIdx;
1354 int groupColPos;
1355 ListCell *l;
1356
1357 /* Unique doesn't project, so tlist requirements pass through */
1358 subplan = create_plan_recurse(root, best_path->subpath, flags);
1359
1360 /* Done if we don't need to do any actual unique-ifying */
1361 if (best_path->umethod == UNIQUE_PATH_NOOP)
1362 return subplan;
1363
1364 /*
1365 * As constructed, the subplan has a "flat" tlist containing just the Vars
1366 * needed here and at upper levels. The values we are supposed to
1367 * unique-ify may be expressions in these variables. We have to add any
1368 * such expressions to the subplan's tlist.
1369 *
1370 * The subplan may have a "physical" tlist if it is a simple scan plan. If
1371 * we're going to sort, this should be reduced to the regular tlist, so
1372 * that we don't sort more data than we need to. For hashing, the tlist
1373 * should be left as-is if we don't need to add any expressions; but if we
1374 * do have to add expressions, then a projection step will be needed at
1375 * runtime anyway, so we may as well remove unneeded items. Therefore
1376 * newtlist starts from build_path_tlist() not just a copy of the
1377 * subplan's tlist; and we don't install it into the subplan unless we are
1378 * sorting or stuff has to be added.
1379 */
1380 in_operators = best_path->in_operators;
1381 uniq_exprs = best_path->uniq_exprs;
1382
1383 /* initialize modified subplan tlist as just the "required" vars */
1384 newtlist = build_path_tlist(root, &best_path->path);
1385 nextresno = list_length(newtlist) + 1;
1386 newitems = false;
1387
1388 foreach(l, uniq_exprs)
1389 {
1390 Expr *uniqexpr = lfirst(l);
1391 TargetEntry *tle;
1392
1393 tle = tlist_member(uniqexpr, newtlist);
1394 if (!tle)
1395 {
1396 tle = makeTargetEntry((Expr *) uniqexpr,
1397 nextresno,
1398 NULL,
1399 false);
1400 newtlist = lappend(newtlist, tle);
1401 nextresno++;
1402 newitems = true;
1403 }
1404 }
1405
1406 /* Use change_plan_targetlist in case we need to insert a Result node */
1407 if (newitems || best_path->umethod == UNIQUE_PATH_SORT)
1408 subplan = change_plan_targetlist(subplan, newtlist,
1409 best_path->path.parallel_safe);
1410
1411 /*
1412 * Build control information showing which subplan output columns are to
1413 * be examined by the grouping step. Unfortunately we can't merge this
1414 * with the previous loop, since we didn't then know which version of the
1415 * subplan tlist we'd end up using.
1416 */
1417 newtlist = subplan->targetlist;
1418 numGroupCols = list_length(uniq_exprs);
1419 groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
1420
1421 groupColPos = 0;
1422 foreach(l, uniq_exprs)
1423 {
1424 Expr *uniqexpr = lfirst(l);
1425 TargetEntry *tle;
1426
1427 tle = tlist_member(uniqexpr, newtlist);
1428 if (!tle) /* shouldn't happen */
1429 elog(ERROR, "failed to find unique expression in subplan tlist");
1430 groupColIdx[groupColPos++] = tle->resno;
1431 }
1432
1433 if (best_path->umethod == UNIQUE_PATH_HASH)
1434 {
1435 Oid *groupOperators;
1436
1437 /*
1438 * Get the hashable equality operators for the Agg node to use.
1439 * Normally these are the same as the IN clause operators, but if
1440 * those are cross-type operators then the equality operators are the
1441 * ones for the IN clause operators' RHS datatype.
1442 */
1443 groupOperators = (Oid *) palloc(numGroupCols * sizeof(Oid));
1444 groupColPos = 0;
1445 foreach(l, in_operators)
1446 {
1447 Oid in_oper = lfirst_oid(l);
1448 Oid eq_oper;
1449
1450 if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
1451 elog(ERROR, "could not find compatible hash operator for operator %u",
1452 in_oper);
1453 groupOperators[groupColPos++] = eq_oper;
1454 }
1455
1456 /*
1457 * Since the Agg node is going to project anyway, we can give it the
1458 * minimum output tlist, without any stuff we might have added to the
1459 * subplan tlist.
1460 */
1461 plan = (Plan *) make_agg(build_path_tlist(root, &best_path->path),
1462 NIL,
1463 AGG_HASHED,
1464 AGGSPLIT_SIMPLE,
1465 numGroupCols,
1466 groupColIdx,
1467 groupOperators,
1468 NIL,
1469 NIL,
1470 best_path->path.rows,
1471 subplan);
1472 }
1473 else
1474 {
1475 List *sortList = NIL;
1476 Sort *sort;
1477
1478 /* Create an ORDER BY list to sort the input compatibly */
1479 groupColPos = 0;
1480 foreach(l, in_operators)
1481 {
1482 Oid in_oper = lfirst_oid(l);
1483 Oid sortop;
1484 Oid eqop;
1485 TargetEntry *tle;
1486 SortGroupClause *sortcl;
1487
1488 sortop = get_ordering_op_for_equality_op(in_oper, false);
1489 if (!OidIsValid(sortop)) /* shouldn't happen */
1490 elog(ERROR, "could not find ordering operator for equality operator %u",
1491 in_oper);
1492
1493 /*
1494 * The Unique node will need equality operators. Normally these
1495 * are the same as the IN clause operators, but if those are
1496 * cross-type operators then the equality operators are the ones
1497 * for the IN clause operators' RHS datatype.
1498 */
1499 eqop = get_equality_op_for_ordering_op(sortop, NULL);
1500 if (!OidIsValid(eqop)) /* shouldn't happen */
1501 elog(ERROR, "could not find equality operator for ordering operator %u",
1502 sortop);
1503
1504 tle = get_tle_by_resno(subplan->targetlist,
1505 groupColIdx[groupColPos]);
1506 Assert(tle != NULL);
1507
1508 sortcl = makeNode(SortGroupClause);
1509 sortcl->tleSortGroupRef = assignSortGroupRef(tle,
1510 subplan->targetlist);
1511 sortcl->eqop = eqop;
1512 sortcl->sortop = sortop;
1513 sortcl->nulls_first = false;
1514 sortcl->hashable = false; /* no need to make this accurate */
1515 sortList = lappend(sortList, sortcl);
1516 groupColPos++;
1517 }
1518 sort = make_sort_from_sortclauses(sortList, subplan);
1519 label_sort_with_costsize(root, sort, -1.0);
1520 plan = (Plan *) make_unique_from_sortclauses((Plan *) sort, sortList);
1521 }
1522
1523 /* Copy cost data from Path to Plan */
1524 copy_generic_path_info(plan, &best_path->path);
1525
1526 return plan;
1527 }
1528
1529 /*
1530 * create_gather_plan
1531 *
1532 * Create a Gather plan for 'best_path' and (recursively) plans
1533 * for its subpaths.
1534 */
1535 static Gather *
create_gather_plan(PlannerInfo * root,GatherPath * best_path)1536 create_gather_plan(PlannerInfo *root, GatherPath *best_path)
1537 {
1538 Gather *gather_plan;
1539 Plan *subplan;
1540 List *tlist;
1541
1542 /*
1543 * Although the Gather node can project, we prefer to push down such work
1544 * to its child node, so demand an exact tlist from the child.
1545 */
1546 subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1547
1548 tlist = build_path_tlist(root, &best_path->path);
1549
1550 gather_plan = make_gather(tlist,
1551 NIL,
1552 best_path->num_workers,
1553 assign_special_exec_param(root),
1554 best_path->single_copy,
1555 subplan);
1556
1557 copy_generic_path_info(&gather_plan->plan, &best_path->path);
1558
1559 /* use parallel mode for parallel plans. */
1560 root->glob->parallelModeNeeded = true;
1561
1562 return gather_plan;
1563 }
1564
1565 /*
1566 * create_gather_merge_plan
1567 *
1568 * Create a Gather Merge plan for 'best_path' and (recursively)
1569 * plans for its subpaths.
1570 */
1571 static GatherMerge *
create_gather_merge_plan(PlannerInfo * root,GatherMergePath * best_path)1572 create_gather_merge_plan(PlannerInfo *root, GatherMergePath *best_path)
1573 {
1574 GatherMerge *gm_plan;
1575 Plan *subplan;
1576 List *pathkeys = best_path->path.pathkeys;
1577 List *tlist = build_path_tlist(root, &best_path->path);
1578
1579 /* As with Gather, it's best to project away columns in the workers. */
1580 subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1581
1582 /* Create a shell for a GatherMerge plan. */
1583 gm_plan = makeNode(GatherMerge);
1584 gm_plan->plan.targetlist = tlist;
1585 gm_plan->num_workers = best_path->num_workers;
1586 copy_generic_path_info(&gm_plan->plan, &best_path->path);
1587
1588 /* Assign the rescan Param. */
1589 gm_plan->rescan_param = assign_special_exec_param(root);
1590
1591 /* Gather Merge is pointless with no pathkeys; use Gather instead. */
1592 Assert(pathkeys != NIL);
1593
1594 /* Compute sort column info, and adjust subplan's tlist as needed */
1595 subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1596 best_path->subpath->parent->relids,
1597 gm_plan->sortColIdx,
1598 false,
1599 &gm_plan->numCols,
1600 &gm_plan->sortColIdx,
1601 &gm_plan->sortOperators,
1602 &gm_plan->collations,
1603 &gm_plan->nullsFirst);
1604
1605
1606 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1607 if (!pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys))
1608 subplan = (Plan *) make_sort(subplan, gm_plan->numCols,
1609 gm_plan->sortColIdx,
1610 gm_plan->sortOperators,
1611 gm_plan->collations,
1612 gm_plan->nullsFirst);
1613
1614 /* Now insert the subplan under GatherMerge. */
1615 gm_plan->plan.lefttree = subplan;
1616
1617 /* use parallel mode for parallel plans. */
1618 root->glob->parallelModeNeeded = true;
1619
1620 return gm_plan;
1621 }
1622
1623 /*
1624 * create_projection_plan
1625 *
1626 * Create a plan tree to do a projection step and (recursively) plans
1627 * for its subpaths. We may need a Result node for the projection,
1628 * but sometimes we can just let the subplan do the work.
1629 */
1630 static Plan *
create_projection_plan(PlannerInfo * root,ProjectionPath * best_path,int flags)1631 create_projection_plan(PlannerInfo *root, ProjectionPath *best_path, int flags)
1632 {
1633 Plan *plan;
1634 Plan *subplan;
1635 List *tlist;
1636 bool needs_result_node = false;
1637
1638 /*
1639 * Convert our subpath to a Plan and determine whether we need a Result
1640 * node.
1641 *
1642 * In most cases where we don't need to project, creation_projection_path
1643 * will have set dummypp, but not always. First, some createplan.c
1644 * routines change the tlists of their nodes. (An example is that
1645 * create_merge_append_plan might add resjunk sort columns to a
1646 * MergeAppend.) Second, create_projection_path has no way of knowing
1647 * what path node will be placed on top of the projection path and
1648 * therefore can't predict whether it will require an exact tlist. For
1649 * both of these reasons, we have to recheck here.
1650 */
1651 if (use_physical_tlist(root, &best_path->path, flags))
1652 {
1653 /*
1654 * Our caller doesn't really care what tlist we return, so we don't
1655 * actually need to project. However, we may still need to ensure
1656 * proper sortgroupref labels, if the caller cares about those.
1657 */
1658 subplan = create_plan_recurse(root, best_path->subpath, 0);
1659 tlist = subplan->targetlist;
1660 if (flags & CP_LABEL_TLIST)
1661 apply_pathtarget_labeling_to_tlist(tlist,
1662 best_path->path.pathtarget);
1663 }
1664 else if (is_projection_capable_path(best_path->subpath))
1665 {
1666 /*
1667 * Our caller requires that we return the exact tlist, but no separate
1668 * result node is needed because the subpath is projection-capable.
1669 * Tell create_plan_recurse that we're going to ignore the tlist it
1670 * produces.
1671 */
1672 subplan = create_plan_recurse(root, best_path->subpath,
1673 CP_IGNORE_TLIST);
1674 Assert(is_projection_capable_plan(subplan));
1675 tlist = build_path_tlist(root, &best_path->path);
1676 }
1677 else
1678 {
1679 /*
1680 * It looks like we need a result node, unless by good fortune the
1681 * requested tlist is exactly the one the child wants to produce.
1682 */
1683 subplan = create_plan_recurse(root, best_path->subpath, 0);
1684 tlist = build_path_tlist(root, &best_path->path);
1685 needs_result_node = !tlist_same_exprs(tlist, subplan->targetlist);
1686 }
1687
1688 /*
1689 * If we make a different decision about whether to include a Result node
1690 * than create_projection_path did, we'll have made slightly wrong cost
1691 * estimates; but label the plan with the cost estimates we actually used,
1692 * not "corrected" ones. (XXX this could be cleaned up if we moved more
1693 * of the sortcolumn setup logic into Path creation, but that would add
1694 * expense to creating Paths we might end up not using.)
1695 */
1696 if (!needs_result_node)
1697 {
1698 /* Don't need a separate Result, just assign tlist to subplan */
1699 plan = subplan;
1700 plan->targetlist = tlist;
1701
1702 /* Label plan with the estimated costs we actually used */
1703 plan->startup_cost = best_path->path.startup_cost;
1704 plan->total_cost = best_path->path.total_cost;
1705 plan->plan_rows = best_path->path.rows;
1706 plan->plan_width = best_path->path.pathtarget->width;
1707 plan->parallel_safe = best_path->path.parallel_safe;
1708 /* ... but don't change subplan's parallel_aware flag */
1709 }
1710 else
1711 {
1712 /* We need a Result node */
1713 plan = (Plan *) make_result(tlist, NULL, subplan);
1714
1715 copy_generic_path_info(plan, (Path *) best_path);
1716 }
1717
1718 return plan;
1719 }
1720
1721 /*
1722 * inject_projection_plan
1723 * Insert a Result node to do a projection step.
1724 *
1725 * This is used in a few places where we decide on-the-fly that we need a
1726 * projection step as part of the tree generated for some Path node.
1727 * We should try to get rid of this in favor of doing it more honestly.
1728 *
1729 * One reason it's ugly is we have to be told the right parallel_safe marking
1730 * to apply (since the tlist might be unsafe even if the child plan is safe).
1731 */
1732 static Plan *
inject_projection_plan(Plan * subplan,List * tlist,bool parallel_safe)1733 inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
1734 {
1735 Plan *plan;
1736
1737 plan = (Plan *) make_result(tlist, NULL, subplan);
1738
1739 /*
1740 * In principle, we should charge tlist eval cost plus cpu_per_tuple per
1741 * row for the Result node. But the former has probably been factored in
1742 * already and the latter was not accounted for during Path construction,
1743 * so being formally correct might just make the EXPLAIN output look less
1744 * consistent not more so. Hence, just copy the subplan's cost.
1745 */
1746 copy_plan_costsize(plan, subplan);
1747 plan->parallel_safe = parallel_safe;
1748
1749 return plan;
1750 }
1751
1752 /*
1753 * change_plan_targetlist
1754 * Externally available wrapper for inject_projection_plan.
1755 *
1756 * This is meant for use by FDW plan-generation functions, which might
1757 * want to adjust the tlist computed by some subplan tree. In general,
1758 * a Result node is needed to compute the new tlist, but we can optimize
1759 * some cases.
1760 *
1761 * In most cases, tlist_parallel_safe can just be passed as the parallel_safe
1762 * flag of the FDW's own Path node.
1763 */
1764 Plan *
change_plan_targetlist(Plan * subplan,List * tlist,bool tlist_parallel_safe)1765 change_plan_targetlist(Plan *subplan, List *tlist, bool tlist_parallel_safe)
1766 {
1767 /*
1768 * If the top plan node can't do projections and its existing target list
1769 * isn't already what we need, we need to add a Result node to help it
1770 * along.
1771 */
1772 if (!is_projection_capable_plan(subplan) &&
1773 !tlist_same_exprs(tlist, subplan->targetlist))
1774 subplan = inject_projection_plan(subplan, tlist,
1775 subplan->parallel_safe &&
1776 tlist_parallel_safe);
1777 else
1778 {
1779 /* Else we can just replace the plan node's tlist */
1780 subplan->targetlist = tlist;
1781 subplan->parallel_safe &= tlist_parallel_safe;
1782 }
1783 return subplan;
1784 }
1785
1786 /*
1787 * create_sort_plan
1788 *
1789 * Create a Sort plan for 'best_path' and (recursively) plans
1790 * for its subpaths.
1791 */
1792 static Sort *
create_sort_plan(PlannerInfo * root,SortPath * best_path,int flags)1793 create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags)
1794 {
1795 Sort *plan;
1796 Plan *subplan;
1797
1798 /*
1799 * We don't want any excess columns in the sorted tuples, so request a
1800 * smaller tlist. Otherwise, since Sort doesn't project, tlist
1801 * requirements pass through.
1802 */
1803 subplan = create_plan_recurse(root, best_path->subpath,
1804 flags | CP_SMALL_TLIST);
1805
1806 /*
1807 * make_sort_from_pathkeys() indirectly calls find_ec_member_for_tle(),
1808 * which will ignore any child EC members that don't belong to the given
1809 * relids. Thus, if this sort path is based on a child relation, we must
1810 * pass its relids.
1811 */
1812 plan = make_sort_from_pathkeys(subplan, best_path->path.pathkeys,
1813 IS_OTHER_REL(best_path->subpath->parent) ?
1814 best_path->path.parent->relids : NULL);
1815
1816 copy_generic_path_info(&plan->plan, (Path *) best_path);
1817
1818 return plan;
1819 }
1820
1821 /*
1822 * create_group_plan
1823 *
1824 * Create a Group plan for 'best_path' and (recursively) plans
1825 * for its subpaths.
1826 */
1827 static Group *
create_group_plan(PlannerInfo * root,GroupPath * best_path)1828 create_group_plan(PlannerInfo *root, GroupPath *best_path)
1829 {
1830 Group *plan;
1831 Plan *subplan;
1832 List *tlist;
1833 List *quals;
1834
1835 /*
1836 * Group can project, so no need to be terribly picky about child tlist,
1837 * but we do need grouping columns to be available
1838 */
1839 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1840
1841 tlist = build_path_tlist(root, &best_path->path);
1842
1843 quals = order_qual_clauses(root, best_path->qual);
1844
1845 plan = make_group(tlist,
1846 quals,
1847 list_length(best_path->groupClause),
1848 extract_grouping_cols(best_path->groupClause,
1849 subplan->targetlist),
1850 extract_grouping_ops(best_path->groupClause),
1851 subplan);
1852
1853 copy_generic_path_info(&plan->plan, (Path *) best_path);
1854
1855 return plan;
1856 }
1857
1858 /*
1859 * create_upper_unique_plan
1860 *
1861 * Create a Unique plan for 'best_path' and (recursively) plans
1862 * for its subpaths.
1863 */
1864 static Unique *
create_upper_unique_plan(PlannerInfo * root,UpperUniquePath * best_path,int flags)1865 create_upper_unique_plan(PlannerInfo *root, UpperUniquePath *best_path, int flags)
1866 {
1867 Unique *plan;
1868 Plan *subplan;
1869
1870 /*
1871 * Unique doesn't project, so tlist requirements pass through; moreover we
1872 * need grouping columns to be labeled.
1873 */
1874 subplan = create_plan_recurse(root, best_path->subpath,
1875 flags | CP_LABEL_TLIST);
1876
1877 plan = make_unique_from_pathkeys(subplan,
1878 best_path->path.pathkeys,
1879 best_path->numkeys);
1880
1881 copy_generic_path_info(&plan->plan, (Path *) best_path);
1882
1883 return plan;
1884 }
1885
1886 /*
1887 * create_agg_plan
1888 *
1889 * Create an Agg plan for 'best_path' and (recursively) plans
1890 * for its subpaths.
1891 */
1892 static Agg *
create_agg_plan(PlannerInfo * root,AggPath * best_path)1893 create_agg_plan(PlannerInfo *root, AggPath *best_path)
1894 {
1895 Agg *plan;
1896 Plan *subplan;
1897 List *tlist;
1898 List *quals;
1899
1900 /*
1901 * Agg can project, so no need to be terribly picky about child tlist, but
1902 * we do need grouping columns to be available
1903 */
1904 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1905
1906 tlist = build_path_tlist(root, &best_path->path);
1907
1908 quals = order_qual_clauses(root, best_path->qual);
1909
1910 plan = make_agg(tlist, quals,
1911 best_path->aggstrategy,
1912 best_path->aggsplit,
1913 list_length(best_path->groupClause),
1914 extract_grouping_cols(best_path->groupClause,
1915 subplan->targetlist),
1916 extract_grouping_ops(best_path->groupClause),
1917 NIL,
1918 NIL,
1919 best_path->numGroups,
1920 subplan);
1921
1922 copy_generic_path_info(&plan->plan, (Path *) best_path);
1923
1924 return plan;
1925 }
1926
1927 /*
1928 * Given a groupclause for a collection of grouping sets, produce the
1929 * corresponding groupColIdx.
1930 *
1931 * root->grouping_map maps the tleSortGroupRef to the actual column position in
1932 * the input tuple. So we get the ref from the entries in the groupclause and
1933 * look them up there.
1934 */
1935 static AttrNumber *
remap_groupColIdx(PlannerInfo * root,List * groupClause)1936 remap_groupColIdx(PlannerInfo *root, List *groupClause)
1937 {
1938 AttrNumber *grouping_map = root->grouping_map;
1939 AttrNumber *new_grpColIdx;
1940 ListCell *lc;
1941 int i;
1942
1943 Assert(grouping_map);
1944
1945 new_grpColIdx = palloc0(sizeof(AttrNumber) * list_length(groupClause));
1946
1947 i = 0;
1948 foreach(lc, groupClause)
1949 {
1950 SortGroupClause *clause = lfirst(lc);
1951
1952 new_grpColIdx[i++] = grouping_map[clause->tleSortGroupRef];
1953 }
1954
1955 return new_grpColIdx;
1956 }
1957
1958 /*
1959 * create_groupingsets_plan
1960 * Create a plan for 'best_path' and (recursively) plans
1961 * for its subpaths.
1962 *
1963 * What we emit is an Agg plan with some vestigial Agg and Sort nodes
1964 * hanging off the side. The top Agg implements the last grouping set
1965 * specified in the GroupingSetsPath, and any additional grouping sets
1966 * each give rise to a subsidiary Agg and Sort node in the top Agg's
1967 * "chain" list. These nodes don't participate in the plan directly,
1968 * but they are a convenient way to represent the required data for
1969 * the extra steps.
1970 *
1971 * Returns a Plan node.
1972 */
1973 static Plan *
create_groupingsets_plan(PlannerInfo * root,GroupingSetsPath * best_path)1974 create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path)
1975 {
1976 Agg *plan;
1977 Plan *subplan;
1978 List *rollups = best_path->rollups;
1979 AttrNumber *grouping_map;
1980 int maxref;
1981 List *chain;
1982 ListCell *lc;
1983
1984 /* Shouldn't get here without grouping sets */
1985 Assert(root->parse->groupingSets);
1986 Assert(rollups != NIL);
1987
1988 /*
1989 * Agg can project, so no need to be terribly picky about child tlist, but
1990 * we do need grouping columns to be available
1991 */
1992 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1993
1994 /*
1995 * Compute the mapping from tleSortGroupRef to column index in the child's
1996 * tlist. First, identify max SortGroupRef in groupClause, for array
1997 * sizing.
1998 */
1999 maxref = 0;
2000 foreach(lc, root->parse->groupClause)
2001 {
2002 SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
2003
2004 if (gc->tleSortGroupRef > maxref)
2005 maxref = gc->tleSortGroupRef;
2006 }
2007
2008 grouping_map = (AttrNumber *) palloc0((maxref + 1) * sizeof(AttrNumber));
2009
2010 /* Now look up the column numbers in the child's tlist */
2011 foreach(lc, root->parse->groupClause)
2012 {
2013 SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
2014 TargetEntry *tle = get_sortgroupclause_tle(gc, subplan->targetlist);
2015
2016 grouping_map[gc->tleSortGroupRef] = tle->resno;
2017 }
2018
2019 /*
2020 * During setrefs.c, we'll need the grouping_map to fix up the cols lists
2021 * in GroupingFunc nodes. Save it for setrefs.c to use.
2022 *
2023 * This doesn't work if we're in an inheritance subtree (see notes in
2024 * create_modifytable_plan). Fortunately we can't be because there would
2025 * never be grouping in an UPDATE/DELETE; but let's Assert that.
2026 */
2027 Assert(root->inhTargetKind == INHKIND_NONE);
2028 Assert(root->grouping_map == NULL);
2029 root->grouping_map = grouping_map;
2030
2031 /*
2032 * Generate the side nodes that describe the other sort and group
2033 * operations besides the top one. Note that we don't worry about putting
2034 * accurate cost estimates in the side nodes; only the topmost Agg node's
2035 * costs will be shown by EXPLAIN.
2036 */
2037 chain = NIL;
2038 if (list_length(rollups) > 1)
2039 {
2040 ListCell *lc2 = lnext(list_head(rollups));
2041 bool is_first_sort = ((RollupData *) linitial(rollups))->is_hashed;
2042
2043 for_each_cell(lc, lc2)
2044 {
2045 RollupData *rollup = lfirst(lc);
2046 AttrNumber *new_grpColIdx;
2047 Plan *sort_plan = NULL;
2048 Plan *agg_plan;
2049 AggStrategy strat;
2050
2051 new_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2052
2053 if (!rollup->is_hashed && !is_first_sort)
2054 {
2055 sort_plan = (Plan *)
2056 make_sort_from_groupcols(rollup->groupClause,
2057 new_grpColIdx,
2058 subplan);
2059 }
2060
2061 if (!rollup->is_hashed)
2062 is_first_sort = false;
2063
2064 if (rollup->is_hashed)
2065 strat = AGG_HASHED;
2066 else if (list_length(linitial(rollup->gsets)) == 0)
2067 strat = AGG_PLAIN;
2068 else
2069 strat = AGG_SORTED;
2070
2071 agg_plan = (Plan *) make_agg(NIL,
2072 NIL,
2073 strat,
2074 AGGSPLIT_SIMPLE,
2075 list_length((List *) linitial(rollup->gsets)),
2076 new_grpColIdx,
2077 extract_grouping_ops(rollup->groupClause),
2078 rollup->gsets,
2079 NIL,
2080 rollup->numGroups,
2081 sort_plan);
2082
2083 /*
2084 * Remove stuff we don't need to avoid bloating debug output.
2085 */
2086 if (sort_plan)
2087 {
2088 sort_plan->targetlist = NIL;
2089 sort_plan->lefttree = NULL;
2090 }
2091
2092 chain = lappend(chain, agg_plan);
2093 }
2094 }
2095
2096 /*
2097 * Now make the real Agg node
2098 */
2099 {
2100 RollupData *rollup = linitial(rollups);
2101 AttrNumber *top_grpColIdx;
2102 int numGroupCols;
2103
2104 top_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2105
2106 numGroupCols = list_length((List *) linitial(rollup->gsets));
2107
2108 plan = make_agg(build_path_tlist(root, &best_path->path),
2109 best_path->qual,
2110 best_path->aggstrategy,
2111 AGGSPLIT_SIMPLE,
2112 numGroupCols,
2113 top_grpColIdx,
2114 extract_grouping_ops(rollup->groupClause),
2115 rollup->gsets,
2116 chain,
2117 rollup->numGroups,
2118 subplan);
2119
2120 /* Copy cost data from Path to Plan */
2121 copy_generic_path_info(&plan->plan, &best_path->path);
2122 }
2123
2124 return (Plan *) plan;
2125 }
2126
2127 /*
2128 * create_minmaxagg_plan
2129 *
2130 * Create a Result plan for 'best_path' and (recursively) plans
2131 * for its subpaths.
2132 */
2133 static Result *
create_minmaxagg_plan(PlannerInfo * root,MinMaxAggPath * best_path)2134 create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path)
2135 {
2136 Result *plan;
2137 List *tlist;
2138 ListCell *lc;
2139
2140 /* Prepare an InitPlan for each aggregate's subquery. */
2141 foreach(lc, best_path->mmaggregates)
2142 {
2143 MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
2144 PlannerInfo *subroot = mminfo->subroot;
2145 Query *subparse = subroot->parse;
2146 Plan *plan;
2147
2148 /*
2149 * Generate the plan for the subquery. We already have a Path, but we
2150 * have to convert it to a Plan and attach a LIMIT node above it.
2151 * Since we are entering a different planner context (subroot),
2152 * recurse to create_plan not create_plan_recurse.
2153 */
2154 plan = create_plan(subroot, mminfo->path);
2155
2156 plan = (Plan *) make_limit(plan,
2157 subparse->limitOffset,
2158 subparse->limitCount);
2159
2160 /* Must apply correct cost/width data to Limit node */
2161 plan->startup_cost = mminfo->path->startup_cost;
2162 plan->total_cost = mminfo->pathcost;
2163 plan->plan_rows = 1;
2164 plan->plan_width = mminfo->path->pathtarget->width;
2165 plan->parallel_aware = false;
2166 plan->parallel_safe = mminfo->path->parallel_safe;
2167
2168 /* Convert the plan into an InitPlan in the outer query. */
2169 SS_make_initplan_from_plan(root, subroot, plan, mminfo->param);
2170 }
2171
2172 /* Generate the output plan --- basically just a Result */
2173 tlist = build_path_tlist(root, &best_path->path);
2174
2175 plan = make_result(tlist, (Node *) best_path->quals, NULL);
2176
2177 copy_generic_path_info(&plan->plan, (Path *) best_path);
2178
2179 /*
2180 * During setrefs.c, we'll need to replace references to the Agg nodes
2181 * with InitPlan output params. (We can't just do that locally in the
2182 * MinMaxAgg node, because path nodes above here may have Agg references
2183 * as well.) Save the mmaggregates list to tell setrefs.c to do that.
2184 *
2185 * This doesn't work if we're in an inheritance subtree (see notes in
2186 * create_modifytable_plan). Fortunately we can't be because there would
2187 * never be aggregates in an UPDATE/DELETE; but let's Assert that.
2188 */
2189 Assert(root->inhTargetKind == INHKIND_NONE);
2190 Assert(root->minmax_aggs == NIL);
2191 root->minmax_aggs = best_path->mmaggregates;
2192
2193 return plan;
2194 }
2195
2196 /*
2197 * create_windowagg_plan
2198 *
2199 * Create a WindowAgg plan for 'best_path' and (recursively) plans
2200 * for its subpaths.
2201 */
2202 static WindowAgg *
create_windowagg_plan(PlannerInfo * root,WindowAggPath * best_path)2203 create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
2204 {
2205 WindowAgg *plan;
2206 WindowClause *wc = best_path->winclause;
2207 int numPart = list_length(wc->partitionClause);
2208 int numOrder = list_length(wc->orderClause);
2209 Plan *subplan;
2210 List *tlist;
2211 int partNumCols;
2212 AttrNumber *partColIdx;
2213 Oid *partOperators;
2214 int ordNumCols;
2215 AttrNumber *ordColIdx;
2216 Oid *ordOperators;
2217 ListCell *lc;
2218
2219 /*
2220 * Choice of tlist here is motivated by the fact that WindowAgg will be
2221 * storing the input rows of window frames in a tuplestore; it therefore
2222 * behooves us to request a small tlist to avoid wasting space. We do of
2223 * course need grouping columns to be available.
2224 */
2225 subplan = create_plan_recurse(root, best_path->subpath,
2226 CP_LABEL_TLIST | CP_SMALL_TLIST);
2227
2228 tlist = build_path_tlist(root, &best_path->path);
2229
2230 /*
2231 * Convert SortGroupClause lists into arrays of attr indexes and equality
2232 * operators, as wanted by executor. (Note: in principle, it's possible
2233 * to drop some of the sort columns, if they were proved redundant by
2234 * pathkey logic. However, it doesn't seem worth going out of our way to
2235 * optimize such cases. In any case, we must *not* remove the ordering
2236 * column for RANGE OFFSET cases, as the executor needs that for in_range
2237 * tests even if it's known to be equal to some partitioning column.)
2238 */
2239 partColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numPart);
2240 partOperators = (Oid *) palloc(sizeof(Oid) * numPart);
2241
2242 partNumCols = 0;
2243 foreach(lc, wc->partitionClause)
2244 {
2245 SortGroupClause *sgc = (SortGroupClause *) lfirst(lc);
2246 TargetEntry *tle = get_sortgroupclause_tle(sgc, subplan->targetlist);
2247
2248 Assert(OidIsValid(sgc->eqop));
2249 partColIdx[partNumCols] = tle->resno;
2250 partOperators[partNumCols] = sgc->eqop;
2251 partNumCols++;
2252 }
2253
2254 ordColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numOrder);
2255 ordOperators = (Oid *) palloc(sizeof(Oid) * numOrder);
2256
2257 ordNumCols = 0;
2258 foreach(lc, wc->orderClause)
2259 {
2260 SortGroupClause *sgc = (SortGroupClause *) lfirst(lc);
2261 TargetEntry *tle = get_sortgroupclause_tle(sgc, subplan->targetlist);
2262
2263 Assert(OidIsValid(sgc->eqop));
2264 ordColIdx[ordNumCols] = tle->resno;
2265 ordOperators[ordNumCols] = sgc->eqop;
2266 ordNumCols++;
2267 }
2268
2269 /* And finally we can make the WindowAgg node */
2270 plan = make_windowagg(tlist,
2271 wc->winref,
2272 partNumCols,
2273 partColIdx,
2274 partOperators,
2275 ordNumCols,
2276 ordColIdx,
2277 ordOperators,
2278 wc->frameOptions,
2279 wc->startOffset,
2280 wc->endOffset,
2281 wc->startInRangeFunc,
2282 wc->endInRangeFunc,
2283 wc->inRangeColl,
2284 wc->inRangeAsc,
2285 wc->inRangeNullsFirst,
2286 subplan);
2287
2288 copy_generic_path_info(&plan->plan, (Path *) best_path);
2289
2290 return plan;
2291 }
2292
2293 /*
2294 * create_setop_plan
2295 *
2296 * Create a SetOp plan for 'best_path' and (recursively) plans
2297 * for its subpaths.
2298 */
2299 static SetOp *
create_setop_plan(PlannerInfo * root,SetOpPath * best_path,int flags)2300 create_setop_plan(PlannerInfo *root, SetOpPath *best_path, int flags)
2301 {
2302 SetOp *plan;
2303 Plan *subplan;
2304 long numGroups;
2305
2306 /*
2307 * SetOp doesn't project, so tlist requirements pass through; moreover we
2308 * need grouping columns to be labeled.
2309 */
2310 subplan = create_plan_recurse(root, best_path->subpath,
2311 flags | CP_LABEL_TLIST);
2312
2313 /* Convert numGroups to long int --- but 'ware overflow! */
2314 numGroups = (long) Min(best_path->numGroups, (double) LONG_MAX);
2315
2316 plan = make_setop(best_path->cmd,
2317 best_path->strategy,
2318 subplan,
2319 best_path->distinctList,
2320 best_path->flagColIdx,
2321 best_path->firstFlag,
2322 numGroups);
2323
2324 copy_generic_path_info(&plan->plan, (Path *) best_path);
2325
2326 return plan;
2327 }
2328
2329 /*
2330 * create_recursiveunion_plan
2331 *
2332 * Create a RecursiveUnion plan for 'best_path' and (recursively) plans
2333 * for its subpaths.
2334 */
2335 static RecursiveUnion *
create_recursiveunion_plan(PlannerInfo * root,RecursiveUnionPath * best_path)2336 create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path)
2337 {
2338 RecursiveUnion *plan;
2339 Plan *leftplan;
2340 Plan *rightplan;
2341 List *tlist;
2342 long numGroups;
2343
2344 /* Need both children to produce same tlist, so force it */
2345 leftplan = create_plan_recurse(root, best_path->leftpath, CP_EXACT_TLIST);
2346 rightplan = create_plan_recurse(root, best_path->rightpath, CP_EXACT_TLIST);
2347
2348 tlist = build_path_tlist(root, &best_path->path);
2349
2350 /* Convert numGroups to long int --- but 'ware overflow! */
2351 numGroups = (long) Min(best_path->numGroups, (double) LONG_MAX);
2352
2353 plan = make_recursive_union(tlist,
2354 leftplan,
2355 rightplan,
2356 best_path->wtParam,
2357 best_path->distinctList,
2358 numGroups);
2359
2360 copy_generic_path_info(&plan->plan, (Path *) best_path);
2361
2362 return plan;
2363 }
2364
2365 /*
2366 * create_lockrows_plan
2367 *
2368 * Create a LockRows plan for 'best_path' and (recursively) plans
2369 * for its subpaths.
2370 */
2371 static LockRows *
create_lockrows_plan(PlannerInfo * root,LockRowsPath * best_path,int flags)2372 create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path,
2373 int flags)
2374 {
2375 LockRows *plan;
2376 Plan *subplan;
2377
2378 /* LockRows doesn't project, so tlist requirements pass through */
2379 subplan = create_plan_recurse(root, best_path->subpath, flags);
2380
2381 plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
2382
2383 copy_generic_path_info(&plan->plan, (Path *) best_path);
2384
2385 return plan;
2386 }
2387
2388 /*
2389 * create_modifytable_plan
2390 * Create a ModifyTable plan for 'best_path'.
2391 *
2392 * Returns a Plan node.
2393 */
2394 static ModifyTable *
create_modifytable_plan(PlannerInfo * root,ModifyTablePath * best_path)2395 create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path)
2396 {
2397 ModifyTable *plan;
2398 List *subplans = NIL;
2399 ListCell *subpaths,
2400 *subroots;
2401
2402 /* Build the plan for each input path */
2403 forboth(subpaths, best_path->subpaths,
2404 subroots, best_path->subroots)
2405 {
2406 Path *subpath = (Path *) lfirst(subpaths);
2407 PlannerInfo *subroot = (PlannerInfo *) lfirst(subroots);
2408 Plan *subplan;
2409
2410 /*
2411 * In an inherited UPDATE/DELETE, reference the per-child modified
2412 * subroot while creating Plans from Paths for the child rel. This is
2413 * a kluge, but otherwise it's too hard to ensure that Plan creation
2414 * functions (particularly in FDWs) don't depend on the contents of
2415 * "root" matching what they saw at Path creation time. The main
2416 * downside is that creation functions for Plans that might appear
2417 * below a ModifyTable cannot expect to modify the contents of "root"
2418 * and have it "stick" for subsequent processing such as setrefs.c.
2419 * That's not great, but it seems better than the alternative.
2420 */
2421 subplan = create_plan_recurse(subroot, subpath, CP_EXACT_TLIST);
2422
2423 /* Transfer resname/resjunk labeling, too, to keep executor happy */
2424 apply_tlist_labeling(subplan->targetlist, subroot->processed_tlist);
2425
2426 subplans = lappend(subplans, subplan);
2427 }
2428
2429 plan = make_modifytable(root,
2430 best_path->operation,
2431 best_path->canSetTag,
2432 best_path->nominalRelation,
2433 best_path->partitioned_rels,
2434 best_path->partColsUpdated,
2435 best_path->resultRelations,
2436 subplans,
2437 best_path->subroots,
2438 best_path->withCheckOptionLists,
2439 best_path->returningLists,
2440 best_path->rowMarks,
2441 best_path->onconflict,
2442 best_path->epqParam);
2443
2444 copy_generic_path_info(&plan->plan, &best_path->path);
2445
2446 return plan;
2447 }
2448
2449 /*
2450 * create_limit_plan
2451 *
2452 * Create a Limit plan for 'best_path' and (recursively) plans
2453 * for its subpaths.
2454 */
2455 static Limit *
create_limit_plan(PlannerInfo * root,LimitPath * best_path,int flags)2456 create_limit_plan(PlannerInfo *root, LimitPath *best_path, int flags)
2457 {
2458 Limit *plan;
2459 Plan *subplan;
2460
2461 /* Limit doesn't project, so tlist requirements pass through */
2462 subplan = create_plan_recurse(root, best_path->subpath, flags);
2463
2464 plan = make_limit(subplan,
2465 best_path->limitOffset,
2466 best_path->limitCount);
2467
2468 copy_generic_path_info(&plan->plan, (Path *) best_path);
2469
2470 return plan;
2471 }
2472
2473
2474 /*****************************************************************************
2475 *
2476 * BASE-RELATION SCAN METHODS
2477 *
2478 *****************************************************************************/
2479
2480
2481 /*
2482 * create_seqscan_plan
2483 * Returns a seqscan plan for the base relation scanned by 'best_path'
2484 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2485 */
2486 static SeqScan *
create_seqscan_plan(PlannerInfo * root,Path * best_path,List * tlist,List * scan_clauses)2487 create_seqscan_plan(PlannerInfo *root, Path *best_path,
2488 List *tlist, List *scan_clauses)
2489 {
2490 SeqScan *scan_plan;
2491 Index scan_relid = best_path->parent->relid;
2492
2493 /* it should be a base rel... */
2494 Assert(scan_relid > 0);
2495 Assert(best_path->parent->rtekind == RTE_RELATION);
2496
2497 /* Sort clauses into best execution order */
2498 scan_clauses = order_qual_clauses(root, scan_clauses);
2499
2500 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2501 scan_clauses = extract_actual_clauses(scan_clauses, false);
2502
2503 /* Replace any outer-relation variables with nestloop params */
2504 if (best_path->param_info)
2505 {
2506 scan_clauses = (List *)
2507 replace_nestloop_params(root, (Node *) scan_clauses);
2508 }
2509
2510 scan_plan = make_seqscan(tlist,
2511 scan_clauses,
2512 scan_relid);
2513
2514 copy_generic_path_info(&scan_plan->plan, best_path);
2515
2516 return scan_plan;
2517 }
2518
2519 /*
2520 * create_samplescan_plan
2521 * Returns a samplescan plan for the base relation scanned by 'best_path'
2522 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2523 */
2524 static SampleScan *
create_samplescan_plan(PlannerInfo * root,Path * best_path,List * tlist,List * scan_clauses)2525 create_samplescan_plan(PlannerInfo *root, Path *best_path,
2526 List *tlist, List *scan_clauses)
2527 {
2528 SampleScan *scan_plan;
2529 Index scan_relid = best_path->parent->relid;
2530 RangeTblEntry *rte;
2531 TableSampleClause *tsc;
2532
2533 /* it should be a base rel with a tablesample clause... */
2534 Assert(scan_relid > 0);
2535 rte = planner_rt_fetch(scan_relid, root);
2536 Assert(rte->rtekind == RTE_RELATION);
2537 tsc = rte->tablesample;
2538 Assert(tsc != NULL);
2539
2540 /* Sort clauses into best execution order */
2541 scan_clauses = order_qual_clauses(root, scan_clauses);
2542
2543 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2544 scan_clauses = extract_actual_clauses(scan_clauses, false);
2545
2546 /* Replace any outer-relation variables with nestloop params */
2547 if (best_path->param_info)
2548 {
2549 scan_clauses = (List *)
2550 replace_nestloop_params(root, (Node *) scan_clauses);
2551 tsc = (TableSampleClause *)
2552 replace_nestloop_params(root, (Node *) tsc);
2553 }
2554
2555 scan_plan = make_samplescan(tlist,
2556 scan_clauses,
2557 scan_relid,
2558 tsc);
2559
2560 copy_generic_path_info(&scan_plan->scan.plan, best_path);
2561
2562 return scan_plan;
2563 }
2564
2565 /*
2566 * create_indexscan_plan
2567 * Returns an indexscan plan for the base relation scanned by 'best_path'
2568 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2569 *
2570 * We use this for both plain IndexScans and IndexOnlyScans, because the
2571 * qual preprocessing work is the same for both. Note that the caller tells
2572 * us which to build --- we don't look at best_path->path.pathtype, because
2573 * create_bitmap_subplan needs to be able to override the prior decision.
2574 */
2575 static Scan *
create_indexscan_plan(PlannerInfo * root,IndexPath * best_path,List * tlist,List * scan_clauses,bool indexonly)2576 create_indexscan_plan(PlannerInfo *root,
2577 IndexPath *best_path,
2578 List *tlist,
2579 List *scan_clauses,
2580 bool indexonly)
2581 {
2582 Scan *scan_plan;
2583 List *indexquals = best_path->indexquals;
2584 List *indexorderbys = best_path->indexorderbys;
2585 Index baserelid = best_path->path.parent->relid;
2586 Oid indexoid = best_path->indexinfo->indexoid;
2587 List *qpqual;
2588 List *stripped_indexquals;
2589 List *fixed_indexquals;
2590 List *fixed_indexorderbys;
2591 List *indexorderbyops = NIL;
2592 ListCell *l;
2593
2594 /* it should be a base rel... */
2595 Assert(baserelid > 0);
2596 Assert(best_path->path.parent->rtekind == RTE_RELATION);
2597
2598 /*
2599 * Build "stripped" indexquals structure (no RestrictInfos) to pass to
2600 * executor as indexqualorig
2601 */
2602 stripped_indexquals = get_actual_clauses(indexquals);
2603
2604 /*
2605 * The executor needs a copy with the indexkey on the left of each clause
2606 * and with index Vars substituted for table ones.
2607 */
2608 fixed_indexquals = fix_indexqual_references(root, best_path);
2609
2610 /*
2611 * Likewise fix up index attr references in the ORDER BY expressions.
2612 */
2613 fixed_indexorderbys = fix_indexorderby_references(root, best_path);
2614
2615 /*
2616 * The qpqual list must contain all restrictions not automatically handled
2617 * by the index, other than pseudoconstant clauses which will be handled
2618 * by a separate gating plan node. All the predicates in the indexquals
2619 * will be checked (either by the index itself, or by nodeIndexscan.c),
2620 * but if there are any "special" operators involved then they must be
2621 * included in qpqual. The upshot is that qpqual must contain
2622 * scan_clauses minus whatever appears in indexquals.
2623 *
2624 * In normal cases simple pointer equality checks will be enough to spot
2625 * duplicate RestrictInfos, so we try that first.
2626 *
2627 * Another common case is that a scan_clauses entry is generated from the
2628 * same EquivalenceClass as some indexqual, and is therefore redundant
2629 * with it, though not equal. (This happens when indxpath.c prefers a
2630 * different derived equality than what generate_join_implied_equalities
2631 * picked for a parameterized scan's ppi_clauses.)
2632 *
2633 * In some situations (particularly with OR'd index conditions) we may
2634 * have scan_clauses that are not equal to, but are logically implied by,
2635 * the index quals; so we also try a predicate_implied_by() check to see
2636 * if we can discard quals that way. (predicate_implied_by assumes its
2637 * first input contains only immutable functions, so we have to check
2638 * that.)
2639 *
2640 * Note: if you change this bit of code you should also look at
2641 * extract_nonindex_conditions() in costsize.c.
2642 */
2643 qpqual = NIL;
2644 foreach(l, scan_clauses)
2645 {
2646 RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
2647
2648 if (rinfo->pseudoconstant)
2649 continue; /* we may drop pseudoconstants here */
2650 if (list_member_ptr(indexquals, rinfo))
2651 continue; /* simple duplicate */
2652 if (is_redundant_derived_clause(rinfo, indexquals))
2653 continue; /* derived from same EquivalenceClass */
2654 if (!contain_mutable_functions((Node *) rinfo->clause) &&
2655 predicate_implied_by(list_make1(rinfo->clause), indexquals, false))
2656 continue; /* provably implied by indexquals */
2657 qpqual = lappend(qpqual, rinfo);
2658 }
2659
2660 /* Sort clauses into best execution order */
2661 qpqual = order_qual_clauses(root, qpqual);
2662
2663 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2664 qpqual = extract_actual_clauses(qpqual, false);
2665
2666 /*
2667 * We have to replace any outer-relation variables with nestloop params in
2668 * the indexqualorig, qpqual, and indexorderbyorig expressions. A bit
2669 * annoying to have to do this separately from the processing in
2670 * fix_indexqual_references --- rethink this when generalizing the inner
2671 * indexscan support. But note we can't really do this earlier because
2672 * it'd break the comparisons to predicates above ... (or would it? Those
2673 * wouldn't have outer refs)
2674 */
2675 if (best_path->path.param_info)
2676 {
2677 stripped_indexquals = (List *)
2678 replace_nestloop_params(root, (Node *) stripped_indexquals);
2679 qpqual = (List *)
2680 replace_nestloop_params(root, (Node *) qpqual);
2681 indexorderbys = (List *)
2682 replace_nestloop_params(root, (Node *) indexorderbys);
2683 }
2684
2685 /*
2686 * If there are ORDER BY expressions, look up the sort operators for their
2687 * result datatypes.
2688 */
2689 if (indexorderbys)
2690 {
2691 ListCell *pathkeyCell,
2692 *exprCell;
2693
2694 /*
2695 * PathKey contains OID of the btree opfamily we're sorting by, but
2696 * that's not quite enough because we need the expression's datatype
2697 * to look up the sort operator in the operator family.
2698 */
2699 Assert(list_length(best_path->path.pathkeys) == list_length(indexorderbys));
2700 forboth(pathkeyCell, best_path->path.pathkeys, exprCell, indexorderbys)
2701 {
2702 PathKey *pathkey = (PathKey *) lfirst(pathkeyCell);
2703 Node *expr = (Node *) lfirst(exprCell);
2704 Oid exprtype = exprType(expr);
2705 Oid sortop;
2706
2707 /* Get sort operator from opfamily */
2708 sortop = get_opfamily_member(pathkey->pk_opfamily,
2709 exprtype,
2710 exprtype,
2711 pathkey->pk_strategy);
2712 if (!OidIsValid(sortop))
2713 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
2714 pathkey->pk_strategy, exprtype, exprtype, pathkey->pk_opfamily);
2715 indexorderbyops = lappend_oid(indexorderbyops, sortop);
2716 }
2717 }
2718
2719 /* Finally ready to build the plan node */
2720 if (indexonly)
2721 scan_plan = (Scan *) make_indexonlyscan(tlist,
2722 qpqual,
2723 baserelid,
2724 indexoid,
2725 fixed_indexquals,
2726 fixed_indexorderbys,
2727 best_path->indexinfo->indextlist,
2728 best_path->indexscandir);
2729 else
2730 scan_plan = (Scan *) make_indexscan(tlist,
2731 qpqual,
2732 baserelid,
2733 indexoid,
2734 fixed_indexquals,
2735 stripped_indexquals,
2736 fixed_indexorderbys,
2737 indexorderbys,
2738 indexorderbyops,
2739 best_path->indexscandir);
2740
2741 copy_generic_path_info(&scan_plan->plan, &best_path->path);
2742
2743 return scan_plan;
2744 }
2745
2746 /*
2747 * create_bitmap_scan_plan
2748 * Returns a bitmap scan plan for the base relation scanned by 'best_path'
2749 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2750 */
2751 static BitmapHeapScan *
create_bitmap_scan_plan(PlannerInfo * root,BitmapHeapPath * best_path,List * tlist,List * scan_clauses)2752 create_bitmap_scan_plan(PlannerInfo *root,
2753 BitmapHeapPath *best_path,
2754 List *tlist,
2755 List *scan_clauses)
2756 {
2757 Index baserelid = best_path->path.parent->relid;
2758 Plan *bitmapqualplan;
2759 List *bitmapqualorig;
2760 List *indexquals;
2761 List *indexECs;
2762 List *qpqual;
2763 ListCell *l;
2764 BitmapHeapScan *scan_plan;
2765
2766 /* it should be a base rel... */
2767 Assert(baserelid > 0);
2768 Assert(best_path->path.parent->rtekind == RTE_RELATION);
2769
2770 /* Process the bitmapqual tree into a Plan tree and qual lists */
2771 bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
2772 &bitmapqualorig, &indexquals,
2773 &indexECs);
2774
2775 if (best_path->path.parallel_aware)
2776 bitmap_subplan_mark_shared(bitmapqualplan);
2777
2778 /*
2779 * The qpqual list must contain all restrictions not automatically handled
2780 * by the index, other than pseudoconstant clauses which will be handled
2781 * by a separate gating plan node. All the predicates in the indexquals
2782 * will be checked (either by the index itself, or by
2783 * nodeBitmapHeapscan.c), but if there are any "special" operators
2784 * involved then they must be added to qpqual. The upshot is that qpqual
2785 * must contain scan_clauses minus whatever appears in indexquals.
2786 *
2787 * This loop is similar to the comparable code in create_indexscan_plan(),
2788 * but with some differences because it has to compare the scan clauses to
2789 * stripped (no RestrictInfos) indexquals. See comments there for more
2790 * info.
2791 *
2792 * In normal cases simple equal() checks will be enough to spot duplicate
2793 * clauses, so we try that first. We next see if the scan clause is
2794 * redundant with any top-level indexqual by virtue of being generated
2795 * from the same EC. After that, try predicate_implied_by().
2796 *
2797 * Unlike create_indexscan_plan(), the predicate_implied_by() test here is
2798 * useful for getting rid of qpquals that are implied by index predicates,
2799 * because the predicate conditions are included in the "indexquals"
2800 * returned by create_bitmap_subplan(). Bitmap scans have to do it that
2801 * way because predicate conditions need to be rechecked if the scan
2802 * becomes lossy, so they have to be included in bitmapqualorig.
2803 */
2804 qpqual = NIL;
2805 foreach(l, scan_clauses)
2806 {
2807 RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
2808 Node *clause = (Node *) rinfo->clause;
2809
2810 if (rinfo->pseudoconstant)
2811 continue; /* we may drop pseudoconstants here */
2812 if (list_member(indexquals, clause))
2813 continue; /* simple duplicate */
2814 if (rinfo->parent_ec && list_member_ptr(indexECs, rinfo->parent_ec))
2815 continue; /* derived from same EquivalenceClass */
2816 if (!contain_mutable_functions(clause) &&
2817 predicate_implied_by(list_make1(clause), indexquals, false))
2818 continue; /* provably implied by indexquals */
2819 qpqual = lappend(qpqual, rinfo);
2820 }
2821
2822 /* Sort clauses into best execution order */
2823 qpqual = order_qual_clauses(root, qpqual);
2824
2825 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2826 qpqual = extract_actual_clauses(qpqual, false);
2827
2828 /*
2829 * When dealing with special operators, we will at this point have
2830 * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
2831 * 'em from bitmapqualorig, since there's no point in making the tests
2832 * twice.
2833 */
2834 bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
2835
2836 /*
2837 * We have to replace any outer-relation variables with nestloop params in
2838 * the qpqual and bitmapqualorig expressions. (This was already done for
2839 * expressions attached to plan nodes in the bitmapqualplan tree.)
2840 */
2841 if (best_path->path.param_info)
2842 {
2843 qpqual = (List *)
2844 replace_nestloop_params(root, (Node *) qpqual);
2845 bitmapqualorig = (List *)
2846 replace_nestloop_params(root, (Node *) bitmapqualorig);
2847 }
2848
2849 /* Finally ready to build the plan node */
2850 scan_plan = make_bitmap_heapscan(tlist,
2851 qpqual,
2852 bitmapqualplan,
2853 bitmapqualorig,
2854 baserelid);
2855
2856 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
2857
2858 return scan_plan;
2859 }
2860
2861 /*
2862 * Given a bitmapqual tree, generate the Plan tree that implements it
2863 *
2864 * As byproducts, we also return in *qual and *indexqual the qual lists
2865 * (in implicit-AND form, without RestrictInfos) describing the original index
2866 * conditions and the generated indexqual conditions. (These are the same in
2867 * simple cases, but when special index operators are involved, the former
2868 * list includes the special conditions while the latter includes the actual
2869 * indexable conditions derived from them.) Both lists include partial-index
2870 * predicates, because we have to recheck predicates as well as index
2871 * conditions if the bitmap scan becomes lossy.
2872 *
2873 * In addition, we return a list of EquivalenceClass pointers for all the
2874 * top-level indexquals that were possibly-redundantly derived from ECs.
2875 * This allows removal of scan_clauses that are redundant with such quals.
2876 * (We do not attempt to detect such redundancies for quals that are within
2877 * OR subtrees. This could be done in a less hacky way if we returned the
2878 * indexquals in RestrictInfo form, but that would be slower and still pretty
2879 * messy, since we'd have to build new RestrictInfos in many cases.)
2880 */
2881 static Plan *
create_bitmap_subplan(PlannerInfo * root,Path * bitmapqual,List ** qual,List ** indexqual,List ** indexECs)2882 create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
2883 List **qual, List **indexqual, List **indexECs)
2884 {
2885 Plan *plan;
2886
2887 if (IsA(bitmapqual, BitmapAndPath))
2888 {
2889 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
2890 List *subplans = NIL;
2891 List *subquals = NIL;
2892 List *subindexquals = NIL;
2893 List *subindexECs = NIL;
2894 ListCell *l;
2895
2896 /*
2897 * There may well be redundant quals among the subplans, since a
2898 * top-level WHERE qual might have gotten used to form several
2899 * different index quals. We don't try exceedingly hard to eliminate
2900 * redundancies, but we do eliminate obvious duplicates by using
2901 * list_concat_unique.
2902 */
2903 foreach(l, apath->bitmapquals)
2904 {
2905 Plan *subplan;
2906 List *subqual;
2907 List *subindexqual;
2908 List *subindexEC;
2909
2910 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
2911 &subqual, &subindexqual,
2912 &subindexEC);
2913 subplans = lappend(subplans, subplan);
2914 subquals = list_concat_unique(subquals, subqual);
2915 subindexquals = list_concat_unique(subindexquals, subindexqual);
2916 /* Duplicates in indexECs aren't worth getting rid of */
2917 subindexECs = list_concat(subindexECs, subindexEC);
2918 }
2919 plan = (Plan *) make_bitmap_and(subplans);
2920 plan->startup_cost = apath->path.startup_cost;
2921 plan->total_cost = apath->path.total_cost;
2922 plan->plan_rows =
2923 clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
2924 plan->plan_width = 0; /* meaningless */
2925 plan->parallel_aware = false;
2926 plan->parallel_safe = apath->path.parallel_safe;
2927 *qual = subquals;
2928 *indexqual = subindexquals;
2929 *indexECs = subindexECs;
2930 }
2931 else if (IsA(bitmapqual, BitmapOrPath))
2932 {
2933 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
2934 List *subplans = NIL;
2935 List *subquals = NIL;
2936 List *subindexquals = NIL;
2937 bool const_true_subqual = false;
2938 bool const_true_subindexqual = false;
2939 ListCell *l;
2940
2941 /*
2942 * Here, we only detect qual-free subplans. A qual-free subplan would
2943 * cause us to generate "... OR true ..." which we may as well reduce
2944 * to just "true". We do not try to eliminate redundant subclauses
2945 * because (a) it's not as likely as in the AND case, and (b) we might
2946 * well be working with hundreds or even thousands of OR conditions,
2947 * perhaps from a long IN list. The performance of list_append_unique
2948 * would be unacceptable.
2949 */
2950 foreach(l, opath->bitmapquals)
2951 {
2952 Plan *subplan;
2953 List *subqual;
2954 List *subindexqual;
2955 List *subindexEC;
2956
2957 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
2958 &subqual, &subindexqual,
2959 &subindexEC);
2960 subplans = lappend(subplans, subplan);
2961 if (subqual == NIL)
2962 const_true_subqual = true;
2963 else if (!const_true_subqual)
2964 subquals = lappend(subquals,
2965 make_ands_explicit(subqual));
2966 if (subindexqual == NIL)
2967 const_true_subindexqual = true;
2968 else if (!const_true_subindexqual)
2969 subindexquals = lappend(subindexquals,
2970 make_ands_explicit(subindexqual));
2971 }
2972
2973 /*
2974 * In the presence of ScalarArrayOpExpr quals, we might have built
2975 * BitmapOrPaths with just one subpath; don't add an OR step.
2976 */
2977 if (list_length(subplans) == 1)
2978 {
2979 plan = (Plan *) linitial(subplans);
2980 }
2981 else
2982 {
2983 plan = (Plan *) make_bitmap_or(subplans);
2984 plan->startup_cost = opath->path.startup_cost;
2985 plan->total_cost = opath->path.total_cost;
2986 plan->plan_rows =
2987 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
2988 plan->plan_width = 0; /* meaningless */
2989 plan->parallel_aware = false;
2990 plan->parallel_safe = opath->path.parallel_safe;
2991 }
2992
2993 /*
2994 * If there were constant-TRUE subquals, the OR reduces to constant
2995 * TRUE. Also, avoid generating one-element ORs, which could happen
2996 * due to redundancy elimination or ScalarArrayOpExpr quals.
2997 */
2998 if (const_true_subqual)
2999 *qual = NIL;
3000 else if (list_length(subquals) <= 1)
3001 *qual = subquals;
3002 else
3003 *qual = list_make1(make_orclause(subquals));
3004 if (const_true_subindexqual)
3005 *indexqual = NIL;
3006 else if (list_length(subindexquals) <= 1)
3007 *indexqual = subindexquals;
3008 else
3009 *indexqual = list_make1(make_orclause(subindexquals));
3010 *indexECs = NIL;
3011 }
3012 else if (IsA(bitmapqual, IndexPath))
3013 {
3014 IndexPath *ipath = (IndexPath *) bitmapqual;
3015 IndexScan *iscan;
3016 List *subindexECs;
3017 ListCell *l;
3018
3019 /* Use the regular indexscan plan build machinery... */
3020 iscan = castNode(IndexScan,
3021 create_indexscan_plan(root, ipath,
3022 NIL, NIL, false));
3023 /* then convert to a bitmap indexscan */
3024 plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
3025 iscan->indexid,
3026 iscan->indexqual,
3027 iscan->indexqualorig);
3028 /* and set its cost/width fields appropriately */
3029 plan->startup_cost = 0.0;
3030 plan->total_cost = ipath->indextotalcost;
3031 plan->plan_rows =
3032 clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
3033 plan->plan_width = 0; /* meaningless */
3034 plan->parallel_aware = false;
3035 plan->parallel_safe = ipath->path.parallel_safe;
3036 *qual = get_actual_clauses(ipath->indexclauses);
3037 *indexqual = get_actual_clauses(ipath->indexquals);
3038 foreach(l, ipath->indexinfo->indpred)
3039 {
3040 Expr *pred = (Expr *) lfirst(l);
3041
3042 /*
3043 * We know that the index predicate must have been implied by the
3044 * query condition as a whole, but it may or may not be implied by
3045 * the conditions that got pushed into the bitmapqual. Avoid
3046 * generating redundant conditions.
3047 */
3048 if (!predicate_implied_by(list_make1(pred), ipath->indexclauses,
3049 false))
3050 {
3051 *qual = lappend(*qual, pred);
3052 *indexqual = lappend(*indexqual, pred);
3053 }
3054 }
3055 subindexECs = NIL;
3056 foreach(l, ipath->indexquals)
3057 {
3058 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
3059
3060 if (rinfo->parent_ec)
3061 subindexECs = lappend(subindexECs, rinfo->parent_ec);
3062 }
3063 *indexECs = subindexECs;
3064 }
3065 else
3066 {
3067 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
3068 plan = NULL; /* keep compiler quiet */
3069 }
3070
3071 return plan;
3072 }
3073
3074 /*
3075 * create_tidscan_plan
3076 * Returns a tidscan plan for the base relation scanned by 'best_path'
3077 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3078 */
3079 static TidScan *
create_tidscan_plan(PlannerInfo * root,TidPath * best_path,List * tlist,List * scan_clauses)3080 create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
3081 List *tlist, List *scan_clauses)
3082 {
3083 TidScan *scan_plan;
3084 Index scan_relid = best_path->path.parent->relid;
3085 List *tidquals = best_path->tidquals;
3086 List *ortidquals;
3087
3088 /* it should be a base rel... */
3089 Assert(scan_relid > 0);
3090 Assert(best_path->path.parent->rtekind == RTE_RELATION);
3091
3092 /* Sort clauses into best execution order */
3093 scan_clauses = order_qual_clauses(root, scan_clauses);
3094
3095 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3096 scan_clauses = extract_actual_clauses(scan_clauses, false);
3097
3098 /* Replace any outer-relation variables with nestloop params */
3099 if (best_path->path.param_info)
3100 {
3101 tidquals = (List *)
3102 replace_nestloop_params(root, (Node *) tidquals);
3103 scan_clauses = (List *)
3104 replace_nestloop_params(root, (Node *) scan_clauses);
3105 }
3106
3107 /*
3108 * Remove any clauses that are TID quals. This is a bit tricky since the
3109 * tidquals list has implicit OR semantics.
3110 */
3111 ortidquals = tidquals;
3112 if (list_length(ortidquals) > 1)
3113 ortidquals = list_make1(make_orclause(ortidquals));
3114 scan_clauses = list_difference(scan_clauses, ortidquals);
3115
3116 scan_plan = make_tidscan(tlist,
3117 scan_clauses,
3118 scan_relid,
3119 tidquals);
3120
3121 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3122
3123 return scan_plan;
3124 }
3125
3126 /*
3127 * create_subqueryscan_plan
3128 * Returns a subqueryscan plan for the base relation scanned by 'best_path'
3129 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3130 */
3131 static SubqueryScan *
create_subqueryscan_plan(PlannerInfo * root,SubqueryScanPath * best_path,List * tlist,List * scan_clauses)3132 create_subqueryscan_plan(PlannerInfo *root, SubqueryScanPath *best_path,
3133 List *tlist, List *scan_clauses)
3134 {
3135 SubqueryScan *scan_plan;
3136 RelOptInfo *rel = best_path->path.parent;
3137 Index scan_relid = rel->relid;
3138 Plan *subplan;
3139
3140 /* it should be a subquery base rel... */
3141 Assert(scan_relid > 0);
3142 Assert(rel->rtekind == RTE_SUBQUERY);
3143
3144 /*
3145 * Recursively create Plan from Path for subquery. Since we are entering
3146 * a different planner context (subroot), recurse to create_plan not
3147 * create_plan_recurse.
3148 */
3149 subplan = create_plan(rel->subroot, best_path->subpath);
3150
3151 /* Sort clauses into best execution order */
3152 scan_clauses = order_qual_clauses(root, scan_clauses);
3153
3154 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3155 scan_clauses = extract_actual_clauses(scan_clauses, false);
3156
3157 /* Replace any outer-relation variables with nestloop params */
3158 if (best_path->path.param_info)
3159 {
3160 scan_clauses = (List *)
3161 replace_nestloop_params(root, (Node *) scan_clauses);
3162 process_subquery_nestloop_params(root,
3163 rel->subplan_params);
3164 }
3165
3166 scan_plan = make_subqueryscan(tlist,
3167 scan_clauses,
3168 scan_relid,
3169 subplan);
3170
3171 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3172
3173 return scan_plan;
3174 }
3175
3176 /*
3177 * create_functionscan_plan
3178 * Returns a functionscan plan for the base relation scanned by 'best_path'
3179 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3180 */
3181 static FunctionScan *
create_functionscan_plan(PlannerInfo * root,Path * best_path,List * tlist,List * scan_clauses)3182 create_functionscan_plan(PlannerInfo *root, Path *best_path,
3183 List *tlist, List *scan_clauses)
3184 {
3185 FunctionScan *scan_plan;
3186 Index scan_relid = best_path->parent->relid;
3187 RangeTblEntry *rte;
3188 List *functions;
3189
3190 /* it should be a function base rel... */
3191 Assert(scan_relid > 0);
3192 rte = planner_rt_fetch(scan_relid, root);
3193 Assert(rte->rtekind == RTE_FUNCTION);
3194 functions = rte->functions;
3195
3196 /* Sort clauses into best execution order */
3197 scan_clauses = order_qual_clauses(root, scan_clauses);
3198
3199 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3200 scan_clauses = extract_actual_clauses(scan_clauses, false);
3201
3202 /* Replace any outer-relation variables with nestloop params */
3203 if (best_path->param_info)
3204 {
3205 scan_clauses = (List *)
3206 replace_nestloop_params(root, (Node *) scan_clauses);
3207 /* The function expressions could contain nestloop params, too */
3208 functions = (List *) replace_nestloop_params(root, (Node *) functions);
3209 }
3210
3211 scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
3212 functions, rte->funcordinality);
3213
3214 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3215
3216 return scan_plan;
3217 }
3218
3219 /*
3220 * create_tablefuncscan_plan
3221 * Returns a tablefuncscan plan for the base relation scanned by 'best_path'
3222 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3223 */
3224 static TableFuncScan *
create_tablefuncscan_plan(PlannerInfo * root,Path * best_path,List * tlist,List * scan_clauses)3225 create_tablefuncscan_plan(PlannerInfo *root, Path *best_path,
3226 List *tlist, List *scan_clauses)
3227 {
3228 TableFuncScan *scan_plan;
3229 Index scan_relid = best_path->parent->relid;
3230 RangeTblEntry *rte;
3231 TableFunc *tablefunc;
3232
3233 /* it should be a function base rel... */
3234 Assert(scan_relid > 0);
3235 rte = planner_rt_fetch(scan_relid, root);
3236 Assert(rte->rtekind == RTE_TABLEFUNC);
3237 tablefunc = rte->tablefunc;
3238
3239 /* Sort clauses into best execution order */
3240 scan_clauses = order_qual_clauses(root, scan_clauses);
3241
3242 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3243 scan_clauses = extract_actual_clauses(scan_clauses, false);
3244
3245 /* Replace any outer-relation variables with nestloop params */
3246 if (best_path->param_info)
3247 {
3248 scan_clauses = (List *)
3249 replace_nestloop_params(root, (Node *) scan_clauses);
3250 /* The function expressions could contain nestloop params, too */
3251 tablefunc = (TableFunc *) replace_nestloop_params(root, (Node *) tablefunc);
3252 }
3253
3254 scan_plan = make_tablefuncscan(tlist, scan_clauses, scan_relid,
3255 tablefunc);
3256
3257 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3258
3259 return scan_plan;
3260 }
3261
3262 /*
3263 * create_valuesscan_plan
3264 * Returns a valuesscan plan for the base relation scanned by 'best_path'
3265 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3266 */
3267 static ValuesScan *
create_valuesscan_plan(PlannerInfo * root,Path * best_path,List * tlist,List * scan_clauses)3268 create_valuesscan_plan(PlannerInfo *root, Path *best_path,
3269 List *tlist, List *scan_clauses)
3270 {
3271 ValuesScan *scan_plan;
3272 Index scan_relid = best_path->parent->relid;
3273 RangeTblEntry *rte;
3274 List *values_lists;
3275
3276 /* it should be a values base rel... */
3277 Assert(scan_relid > 0);
3278 rte = planner_rt_fetch(scan_relid, root);
3279 Assert(rte->rtekind == RTE_VALUES);
3280 values_lists = rte->values_lists;
3281
3282 /* Sort clauses into best execution order */
3283 scan_clauses = order_qual_clauses(root, scan_clauses);
3284
3285 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3286 scan_clauses = extract_actual_clauses(scan_clauses, false);
3287
3288 /* Replace any outer-relation variables with nestloop params */
3289 if (best_path->param_info)
3290 {
3291 scan_clauses = (List *)
3292 replace_nestloop_params(root, (Node *) scan_clauses);
3293 /* The values lists could contain nestloop params, too */
3294 values_lists = (List *)
3295 replace_nestloop_params(root, (Node *) values_lists);
3296 }
3297
3298 scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
3299 values_lists);
3300
3301 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3302
3303 return scan_plan;
3304 }
3305
3306 /*
3307 * create_ctescan_plan
3308 * Returns a ctescan plan for the base relation scanned by 'best_path'
3309 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3310 */
3311 static CteScan *
create_ctescan_plan(PlannerInfo * root,Path * best_path,List * tlist,List * scan_clauses)3312 create_ctescan_plan(PlannerInfo *root, Path *best_path,
3313 List *tlist, List *scan_clauses)
3314 {
3315 CteScan *scan_plan;
3316 Index scan_relid = best_path->parent->relid;
3317 RangeTblEntry *rte;
3318 SubPlan *ctesplan = NULL;
3319 int plan_id;
3320 int cte_param_id;
3321 PlannerInfo *cteroot;
3322 Index levelsup;
3323 int ndx;
3324 ListCell *lc;
3325
3326 Assert(scan_relid > 0);
3327 rte = planner_rt_fetch(scan_relid, root);
3328 Assert(rte->rtekind == RTE_CTE);
3329 Assert(!rte->self_reference);
3330
3331 /*
3332 * Find the referenced CTE, and locate the SubPlan previously made for it.
3333 */
3334 levelsup = rte->ctelevelsup;
3335 cteroot = root;
3336 while (levelsup-- > 0)
3337 {
3338 cteroot = cteroot->parent_root;
3339 if (!cteroot) /* shouldn't happen */
3340 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3341 }
3342
3343 /*
3344 * Note: cte_plan_ids can be shorter than cteList, if we are still working
3345 * on planning the CTEs (ie, this is a side-reference from another CTE).
3346 * So we mustn't use forboth here.
3347 */
3348 ndx = 0;
3349 foreach(lc, cteroot->parse->cteList)
3350 {
3351 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
3352
3353 if (strcmp(cte->ctename, rte->ctename) == 0)
3354 break;
3355 ndx++;
3356 }
3357 if (lc == NULL) /* shouldn't happen */
3358 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
3359 if (ndx >= list_length(cteroot->cte_plan_ids))
3360 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3361 plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
3362 Assert(plan_id > 0);
3363 foreach(lc, cteroot->init_plans)
3364 {
3365 ctesplan = (SubPlan *) lfirst(lc);
3366 if (ctesplan->plan_id == plan_id)
3367 break;
3368 }
3369 if (lc == NULL) /* shouldn't happen */
3370 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3371
3372 /*
3373 * We need the CTE param ID, which is the sole member of the SubPlan's
3374 * setParam list.
3375 */
3376 cte_param_id = linitial_int(ctesplan->setParam);
3377
3378 /* Sort clauses into best execution order */
3379 scan_clauses = order_qual_clauses(root, scan_clauses);
3380
3381 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3382 scan_clauses = extract_actual_clauses(scan_clauses, false);
3383
3384 /* Replace any outer-relation variables with nestloop params */
3385 if (best_path->param_info)
3386 {
3387 scan_clauses = (List *)
3388 replace_nestloop_params(root, (Node *) scan_clauses);
3389 }
3390
3391 scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
3392 plan_id, cte_param_id);
3393
3394 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3395
3396 return scan_plan;
3397 }
3398
3399 /*
3400 * create_namedtuplestorescan_plan
3401 * Returns a tuplestorescan plan for the base relation scanned by
3402 * 'best_path' with restriction clauses 'scan_clauses' and targetlist
3403 * 'tlist'.
3404 */
3405 static NamedTuplestoreScan *
create_namedtuplestorescan_plan(PlannerInfo * root,Path * best_path,List * tlist,List * scan_clauses)3406 create_namedtuplestorescan_plan(PlannerInfo *root, Path *best_path,
3407 List *tlist, List *scan_clauses)
3408 {
3409 NamedTuplestoreScan *scan_plan;
3410 Index scan_relid = best_path->parent->relid;
3411 RangeTblEntry *rte;
3412
3413 Assert(scan_relid > 0);
3414 rte = planner_rt_fetch(scan_relid, root);
3415 Assert(rte->rtekind == RTE_NAMEDTUPLESTORE);
3416
3417 /* Sort clauses into best execution order */
3418 scan_clauses = order_qual_clauses(root, scan_clauses);
3419
3420 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3421 scan_clauses = extract_actual_clauses(scan_clauses, false);
3422
3423 /* Replace any outer-relation variables with nestloop params */
3424 if (best_path->param_info)
3425 {
3426 scan_clauses = (List *)
3427 replace_nestloop_params(root, (Node *) scan_clauses);
3428 }
3429
3430 scan_plan = make_namedtuplestorescan(tlist, scan_clauses, scan_relid,
3431 rte->enrname);
3432
3433 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3434
3435 return scan_plan;
3436 }
3437
3438 /*
3439 * create_worktablescan_plan
3440 * Returns a worktablescan plan for the base relation scanned by 'best_path'
3441 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3442 */
3443 static WorkTableScan *
create_worktablescan_plan(PlannerInfo * root,Path * best_path,List * tlist,List * scan_clauses)3444 create_worktablescan_plan(PlannerInfo *root, Path *best_path,
3445 List *tlist, List *scan_clauses)
3446 {
3447 WorkTableScan *scan_plan;
3448 Index scan_relid = best_path->parent->relid;
3449 RangeTblEntry *rte;
3450 Index levelsup;
3451 PlannerInfo *cteroot;
3452
3453 Assert(scan_relid > 0);
3454 rte = planner_rt_fetch(scan_relid, root);
3455 Assert(rte->rtekind == RTE_CTE);
3456 Assert(rte->self_reference);
3457
3458 /*
3459 * We need to find the worktable param ID, which is in the plan level
3460 * that's processing the recursive UNION, which is one level *below* where
3461 * the CTE comes from.
3462 */
3463 levelsup = rte->ctelevelsup;
3464 if (levelsup == 0) /* shouldn't happen */
3465 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3466 levelsup--;
3467 cteroot = root;
3468 while (levelsup-- > 0)
3469 {
3470 cteroot = cteroot->parent_root;
3471 if (!cteroot) /* shouldn't happen */
3472 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3473 }
3474 if (cteroot->wt_param_id < 0) /* shouldn't happen */
3475 elog(ERROR, "could not find param ID for CTE \"%s\"", rte->ctename);
3476
3477 /* Sort clauses into best execution order */
3478 scan_clauses = order_qual_clauses(root, scan_clauses);
3479
3480 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3481 scan_clauses = extract_actual_clauses(scan_clauses, false);
3482
3483 /* Replace any outer-relation variables with nestloop params */
3484 if (best_path->param_info)
3485 {
3486 scan_clauses = (List *)
3487 replace_nestloop_params(root, (Node *) scan_clauses);
3488 }
3489
3490 scan_plan = make_worktablescan(tlist, scan_clauses, scan_relid,
3491 cteroot->wt_param_id);
3492
3493 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3494
3495 return scan_plan;
3496 }
3497
3498 /*
3499 * create_foreignscan_plan
3500 * Returns a foreignscan plan for the relation scanned by 'best_path'
3501 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
3502 */
3503 static ForeignScan *
create_foreignscan_plan(PlannerInfo * root,ForeignPath * best_path,List * tlist,List * scan_clauses)3504 create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
3505 List *tlist, List *scan_clauses)
3506 {
3507 ForeignScan *scan_plan;
3508 RelOptInfo *rel = best_path->path.parent;
3509 Index scan_relid = rel->relid;
3510 Oid rel_oid = InvalidOid;
3511 Plan *outer_plan = NULL;
3512
3513 Assert(rel->fdwroutine != NULL);
3514
3515 /* transform the child path if any */
3516 if (best_path->fdw_outerpath)
3517 outer_plan = create_plan_recurse(root, best_path->fdw_outerpath,
3518 CP_EXACT_TLIST);
3519
3520 /*
3521 * If we're scanning a base relation, fetch its OID. (Irrelevant if
3522 * scanning a join relation.)
3523 */
3524 if (scan_relid > 0)
3525 {
3526 RangeTblEntry *rte;
3527
3528 Assert(rel->rtekind == RTE_RELATION);
3529 rte = planner_rt_fetch(scan_relid, root);
3530 Assert(rte->rtekind == RTE_RELATION);
3531 rel_oid = rte->relid;
3532 }
3533
3534 /*
3535 * Sort clauses into best execution order. We do this first since the FDW
3536 * might have more info than we do and wish to adjust the ordering.
3537 */
3538 scan_clauses = order_qual_clauses(root, scan_clauses);
3539
3540 /*
3541 * Let the FDW perform its processing on the restriction clauses and
3542 * generate the plan node. Note that the FDW might remove restriction
3543 * clauses that it intends to execute remotely, or even add more (if it
3544 * has selected some join clauses for remote use but also wants them
3545 * rechecked locally).
3546 */
3547 scan_plan = rel->fdwroutine->GetForeignPlan(root, rel, rel_oid,
3548 best_path,
3549 tlist, scan_clauses,
3550 outer_plan);
3551
3552 /* Copy cost data from Path to Plan; no need to make FDW do this */
3553 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3554
3555 /* Copy foreign server OID; likewise, no need to make FDW do this */
3556 scan_plan->fs_server = rel->serverid;
3557
3558 /*
3559 * Likewise, copy the relids that are represented by this foreign scan. An
3560 * upper rel doesn't have relids set, but it covers all the base relations
3561 * participating in the underlying scan, so use root's all_baserels.
3562 */
3563 if (rel->reloptkind == RELOPT_UPPER_REL)
3564 scan_plan->fs_relids = root->all_baserels;
3565 else
3566 scan_plan->fs_relids = best_path->path.parent->relids;
3567
3568 /*
3569 * If this is a foreign join, and to make it valid to push down we had to
3570 * assume that the current user is the same as some user explicitly named
3571 * in the query, mark the finished plan as depending on the current user.
3572 */
3573 if (rel->useridiscurrent)
3574 root->glob->dependsOnRole = true;
3575
3576 /*
3577 * Replace any outer-relation variables with nestloop params in the qual,
3578 * fdw_exprs and fdw_recheck_quals expressions. We do this last so that
3579 * the FDW doesn't have to be involved. (Note that parts of fdw_exprs or
3580 * fdw_recheck_quals could have come from join clauses, so doing this
3581 * beforehand on the scan_clauses wouldn't work.) We assume
3582 * fdw_scan_tlist contains no such variables.
3583 */
3584 if (best_path->path.param_info)
3585 {
3586 scan_plan->scan.plan.qual = (List *)
3587 replace_nestloop_params(root, (Node *) scan_plan->scan.plan.qual);
3588 scan_plan->fdw_exprs = (List *)
3589 replace_nestloop_params(root, (Node *) scan_plan->fdw_exprs);
3590 scan_plan->fdw_recheck_quals = (List *)
3591 replace_nestloop_params(root,
3592 (Node *) scan_plan->fdw_recheck_quals);
3593 }
3594
3595 /*
3596 * If rel is a base relation, detect whether any system columns are
3597 * requested from the rel. (If rel is a join relation, rel->relid will be
3598 * 0, but there can be no Var with relid 0 in the rel's targetlist or the
3599 * restriction clauses, so we skip this in that case. Note that any such
3600 * columns in base relations that were joined are assumed to be contained
3601 * in fdw_scan_tlist.) This is a bit of a kluge and might go away
3602 * someday, so we intentionally leave it out of the API presented to FDWs.
3603 */
3604 scan_plan->fsSystemCol = false;
3605 if (scan_relid > 0)
3606 {
3607 Bitmapset *attrs_used = NULL;
3608 ListCell *lc;
3609 int i;
3610
3611 /*
3612 * First, examine all the attributes needed for joins or final output.
3613 * Note: we must look at rel's targetlist, not the attr_needed data,
3614 * because attr_needed isn't computed for inheritance child rels.
3615 */
3616 pull_varattnos((Node *) rel->reltarget->exprs, scan_relid, &attrs_used);
3617
3618 /* Add all the attributes used by restriction clauses. */
3619 foreach(lc, rel->baserestrictinfo)
3620 {
3621 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3622
3623 pull_varattnos((Node *) rinfo->clause, scan_relid, &attrs_used);
3624 }
3625
3626 /* Now, are any system columns requested from rel? */
3627 for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
3628 {
3629 if (bms_is_member(i - FirstLowInvalidHeapAttributeNumber, attrs_used))
3630 {
3631 scan_plan->fsSystemCol = true;
3632 break;
3633 }
3634 }
3635
3636 bms_free(attrs_used);
3637 }
3638
3639 return scan_plan;
3640 }
3641
3642 /*
3643 * create_custom_plan
3644 *
3645 * Transform a CustomPath into a Plan.
3646 */
3647 static CustomScan *
create_customscan_plan(PlannerInfo * root,CustomPath * best_path,List * tlist,List * scan_clauses)3648 create_customscan_plan(PlannerInfo *root, CustomPath *best_path,
3649 List *tlist, List *scan_clauses)
3650 {
3651 CustomScan *cplan;
3652 RelOptInfo *rel = best_path->path.parent;
3653 List *custom_plans = NIL;
3654 ListCell *lc;
3655
3656 /* Recursively transform child paths. */
3657 foreach(lc, best_path->custom_paths)
3658 {
3659 Plan *plan = create_plan_recurse(root, (Path *) lfirst(lc),
3660 CP_EXACT_TLIST);
3661
3662 custom_plans = lappend(custom_plans, plan);
3663 }
3664
3665 /*
3666 * Sort clauses into the best execution order, although custom-scan
3667 * provider can reorder them again.
3668 */
3669 scan_clauses = order_qual_clauses(root, scan_clauses);
3670
3671 /*
3672 * Invoke custom plan provider to create the Plan node represented by the
3673 * CustomPath.
3674 */
3675 cplan = castNode(CustomScan,
3676 best_path->methods->PlanCustomPath(root,
3677 rel,
3678 best_path,
3679 tlist,
3680 scan_clauses,
3681 custom_plans));
3682
3683 /*
3684 * Copy cost data from Path to Plan; no need to make custom-plan providers
3685 * do this
3686 */
3687 copy_generic_path_info(&cplan->scan.plan, &best_path->path);
3688
3689 /* Likewise, copy the relids that are represented by this custom scan */
3690 cplan->custom_relids = best_path->path.parent->relids;
3691
3692 /*
3693 * Replace any outer-relation variables with nestloop params in the qual
3694 * and custom_exprs expressions. We do this last so that the custom-plan
3695 * provider doesn't have to be involved. (Note that parts of custom_exprs
3696 * could have come from join clauses, so doing this beforehand on the
3697 * scan_clauses wouldn't work.) We assume custom_scan_tlist contains no
3698 * such variables.
3699 */
3700 if (best_path->path.param_info)
3701 {
3702 cplan->scan.plan.qual = (List *)
3703 replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
3704 cplan->custom_exprs = (List *)
3705 replace_nestloop_params(root, (Node *) cplan->custom_exprs);
3706 }
3707
3708 return cplan;
3709 }
3710
3711
3712 /*****************************************************************************
3713 *
3714 * JOIN METHODS
3715 *
3716 *****************************************************************************/
3717
3718 static NestLoop *
create_nestloop_plan(PlannerInfo * root,NestPath * best_path)3719 create_nestloop_plan(PlannerInfo *root,
3720 NestPath *best_path)
3721 {
3722 NestLoop *join_plan;
3723 Plan *outer_plan;
3724 Plan *inner_plan;
3725 List *tlist = build_path_tlist(root, &best_path->path);
3726 List *joinrestrictclauses = best_path->joinrestrictinfo;
3727 List *joinclauses;
3728 List *otherclauses;
3729 Relids outerrelids;
3730 List *nestParams;
3731 Relids saveOuterRels = root->curOuterRels;
3732
3733 /* NestLoop can project, so no need to be picky about child tlists */
3734 outer_plan = create_plan_recurse(root, best_path->outerjoinpath, 0);
3735
3736 /* For a nestloop, include outer relids in curOuterRels for inner side */
3737 root->curOuterRels = bms_union(root->curOuterRels,
3738 best_path->outerjoinpath->parent->relids);
3739
3740 inner_plan = create_plan_recurse(root, best_path->innerjoinpath, 0);
3741
3742 /* Restore curOuterRels */
3743 bms_free(root->curOuterRels);
3744 root->curOuterRels = saveOuterRels;
3745
3746 /* Sort join qual clauses into best execution order */
3747 joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
3748
3749 /* Get the join qual clauses (in plain expression form) */
3750 /* Any pseudoconstant clauses are ignored here */
3751 if (IS_OUTER_JOIN(best_path->jointype))
3752 {
3753 extract_actual_join_clauses(joinrestrictclauses,
3754 best_path->path.parent->relids,
3755 &joinclauses, &otherclauses);
3756 }
3757 else
3758 {
3759 /* We can treat all clauses alike for an inner join */
3760 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
3761 otherclauses = NIL;
3762 }
3763
3764 /* Replace any outer-relation variables with nestloop params */
3765 if (best_path->path.param_info)
3766 {
3767 joinclauses = (List *)
3768 replace_nestloop_params(root, (Node *) joinclauses);
3769 otherclauses = (List *)
3770 replace_nestloop_params(root, (Node *) otherclauses);
3771 }
3772
3773 /*
3774 * Identify any nestloop parameters that should be supplied by this join
3775 * node, and remove them from root->curOuterParams.
3776 */
3777 outerrelids = best_path->outerjoinpath->parent->relids;
3778 nestParams = identify_current_nestloop_params(root, outerrelids);
3779
3780 join_plan = make_nestloop(tlist,
3781 joinclauses,
3782 otherclauses,
3783 nestParams,
3784 outer_plan,
3785 inner_plan,
3786 best_path->jointype,
3787 best_path->inner_unique);
3788
3789 copy_generic_path_info(&join_plan->join.plan, &best_path->path);
3790
3791 return join_plan;
3792 }
3793
3794 static MergeJoin *
create_mergejoin_plan(PlannerInfo * root,MergePath * best_path)3795 create_mergejoin_plan(PlannerInfo *root,
3796 MergePath *best_path)
3797 {
3798 MergeJoin *join_plan;
3799 Plan *outer_plan;
3800 Plan *inner_plan;
3801 List *tlist = build_path_tlist(root, &best_path->jpath.path);
3802 List *joinclauses;
3803 List *otherclauses;
3804 List *mergeclauses;
3805 List *outerpathkeys;
3806 List *innerpathkeys;
3807 int nClauses;
3808 Oid *mergefamilies;
3809 Oid *mergecollations;
3810 int *mergestrategies;
3811 bool *mergenullsfirst;
3812 PathKey *opathkey;
3813 EquivalenceClass *opeclass;
3814 int i;
3815 ListCell *lc;
3816 ListCell *lop;
3817 ListCell *lip;
3818 Path *outer_path = best_path->jpath.outerjoinpath;
3819 Path *inner_path = best_path->jpath.innerjoinpath;
3820
3821 /*
3822 * MergeJoin can project, so we don't have to demand exact tlists from the
3823 * inputs. However, if we're intending to sort an input's result, it's
3824 * best to request a small tlist so we aren't sorting more data than
3825 * necessary.
3826 */
3827 outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
3828 (best_path->outersortkeys != NIL) ? CP_SMALL_TLIST : 0);
3829
3830 inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
3831 (best_path->innersortkeys != NIL) ? CP_SMALL_TLIST : 0);
3832
3833 /* Sort join qual clauses into best execution order */
3834 /* NB: do NOT reorder the mergeclauses */
3835 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
3836
3837 /* Get the join qual clauses (in plain expression form) */
3838 /* Any pseudoconstant clauses are ignored here */
3839 if (IS_OUTER_JOIN(best_path->jpath.jointype))
3840 {
3841 extract_actual_join_clauses(joinclauses,
3842 best_path->jpath.path.parent->relids,
3843 &joinclauses, &otherclauses);
3844 }
3845 else
3846 {
3847 /* We can treat all clauses alike for an inner join */
3848 joinclauses = extract_actual_clauses(joinclauses, false);
3849 otherclauses = NIL;
3850 }
3851
3852 /*
3853 * Remove the mergeclauses from the list of join qual clauses, leaving the
3854 * list of quals that must be checked as qpquals.
3855 */
3856 mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
3857 joinclauses = list_difference(joinclauses, mergeclauses);
3858
3859 /*
3860 * Replace any outer-relation variables with nestloop params. There
3861 * should not be any in the mergeclauses.
3862 */
3863 if (best_path->jpath.path.param_info)
3864 {
3865 joinclauses = (List *)
3866 replace_nestloop_params(root, (Node *) joinclauses);
3867 otherclauses = (List *)
3868 replace_nestloop_params(root, (Node *) otherclauses);
3869 }
3870
3871 /*
3872 * Rearrange mergeclauses, if needed, so that the outer variable is always
3873 * on the left; mark the mergeclause restrictinfos with correct
3874 * outer_is_left status.
3875 */
3876 mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
3877 best_path->jpath.outerjoinpath->parent->relids);
3878
3879 /*
3880 * Create explicit sort nodes for the outer and inner paths if necessary.
3881 */
3882 if (best_path->outersortkeys)
3883 {
3884 Relids outer_relids = outer_path->parent->relids;
3885 Sort *sort = make_sort_from_pathkeys(outer_plan,
3886 best_path->outersortkeys,
3887 outer_relids);
3888
3889 label_sort_with_costsize(root, sort, -1.0);
3890 outer_plan = (Plan *) sort;
3891 outerpathkeys = best_path->outersortkeys;
3892 }
3893 else
3894 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
3895
3896 if (best_path->innersortkeys)
3897 {
3898 Relids inner_relids = inner_path->parent->relids;
3899 Sort *sort = make_sort_from_pathkeys(inner_plan,
3900 best_path->innersortkeys,
3901 inner_relids);
3902
3903 label_sort_with_costsize(root, sort, -1.0);
3904 inner_plan = (Plan *) sort;
3905 innerpathkeys = best_path->innersortkeys;
3906 }
3907 else
3908 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
3909
3910 /*
3911 * If specified, add a materialize node to shield the inner plan from the
3912 * need to handle mark/restore.
3913 */
3914 if (best_path->materialize_inner)
3915 {
3916 Plan *matplan = (Plan *) make_material(inner_plan);
3917
3918 /*
3919 * We assume the materialize will not spill to disk, and therefore
3920 * charge just cpu_operator_cost per tuple. (Keep this estimate in
3921 * sync with final_cost_mergejoin.)
3922 */
3923 copy_plan_costsize(matplan, inner_plan);
3924 matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
3925
3926 inner_plan = matplan;
3927 }
3928
3929 /*
3930 * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
3931 * executor. The information is in the pathkeys for the two inputs, but
3932 * we need to be careful about the possibility of mergeclauses sharing a
3933 * pathkey, as well as the possibility that the inner pathkeys are not in
3934 * an order matching the mergeclauses.
3935 */
3936 nClauses = list_length(mergeclauses);
3937 Assert(nClauses == list_length(best_path->path_mergeclauses));
3938 mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
3939 mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
3940 mergestrategies = (int *) palloc(nClauses * sizeof(int));
3941 mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
3942
3943 opathkey = NULL;
3944 opeclass = NULL;
3945 lop = list_head(outerpathkeys);
3946 lip = list_head(innerpathkeys);
3947 i = 0;
3948 foreach(lc, best_path->path_mergeclauses)
3949 {
3950 RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
3951 EquivalenceClass *oeclass;
3952 EquivalenceClass *ieclass;
3953 PathKey *ipathkey = NULL;
3954 EquivalenceClass *ipeclass = NULL;
3955 bool first_inner_match = false;
3956
3957 /* fetch outer/inner eclass from mergeclause */
3958 if (rinfo->outer_is_left)
3959 {
3960 oeclass = rinfo->left_ec;
3961 ieclass = rinfo->right_ec;
3962 }
3963 else
3964 {
3965 oeclass = rinfo->right_ec;
3966 ieclass = rinfo->left_ec;
3967 }
3968 Assert(oeclass != NULL);
3969 Assert(ieclass != NULL);
3970
3971 /*
3972 * We must identify the pathkey elements associated with this clause
3973 * by matching the eclasses (which should give a unique match, since
3974 * the pathkey lists should be canonical). In typical cases the merge
3975 * clauses are one-to-one with the pathkeys, but when dealing with
3976 * partially redundant query conditions, things are more complicated.
3977 *
3978 * lop and lip reference the first as-yet-unmatched pathkey elements.
3979 * If they're NULL then all pathkey elements have been matched.
3980 *
3981 * The ordering of the outer pathkeys should match the mergeclauses,
3982 * by construction (see find_mergeclauses_for_outer_pathkeys()). There
3983 * could be more than one mergeclause for the same outer pathkey, but
3984 * no pathkey may be entirely skipped over.
3985 */
3986 if (oeclass != opeclass) /* multiple matches are not interesting */
3987 {
3988 /* doesn't match the current opathkey, so must match the next */
3989 if (lop == NULL)
3990 elog(ERROR, "outer pathkeys do not match mergeclauses");
3991 opathkey = (PathKey *) lfirst(lop);
3992 opeclass = opathkey->pk_eclass;
3993 lop = lnext(lop);
3994 if (oeclass != opeclass)
3995 elog(ERROR, "outer pathkeys do not match mergeclauses");
3996 }
3997
3998 /*
3999 * The inner pathkeys likewise should not have skipped-over keys, but
4000 * it's possible for a mergeclause to reference some earlier inner
4001 * pathkey if we had redundant pathkeys. For example we might have
4002 * mergeclauses like "o.a = i.x AND o.b = i.y AND o.c = i.x". The
4003 * implied inner ordering is then "ORDER BY x, y, x", but the pathkey
4004 * mechanism drops the second sort by x as redundant, and this code
4005 * must cope.
4006 *
4007 * It's also possible for the implied inner-rel ordering to be like
4008 * "ORDER BY x, y, x DESC". We still drop the second instance of x as
4009 * redundant; but this means that the sort ordering of a redundant
4010 * inner pathkey should not be considered significant. So we must
4011 * detect whether this is the first clause matching an inner pathkey.
4012 */
4013 if (lip)
4014 {
4015 ipathkey = (PathKey *) lfirst(lip);
4016 ipeclass = ipathkey->pk_eclass;
4017 if (ieclass == ipeclass)
4018 {
4019 /* successful first match to this inner pathkey */
4020 lip = lnext(lip);
4021 first_inner_match = true;
4022 }
4023 }
4024 if (!first_inner_match)
4025 {
4026 /* redundant clause ... must match something before lip */
4027 ListCell *l2;
4028
4029 foreach(l2, innerpathkeys)
4030 {
4031 if (l2 == lip)
4032 break;
4033 ipathkey = (PathKey *) lfirst(l2);
4034 ipeclass = ipathkey->pk_eclass;
4035 if (ieclass == ipeclass)
4036 break;
4037 }
4038 if (ieclass != ipeclass)
4039 elog(ERROR, "inner pathkeys do not match mergeclauses");
4040 }
4041
4042 /*
4043 * The pathkeys should always match each other as to opfamily and
4044 * collation (which affect equality), but if we're considering a
4045 * redundant inner pathkey, its sort ordering might not match. In
4046 * such cases we may ignore the inner pathkey's sort ordering and use
4047 * the outer's. (In effect, we're lying to the executor about the
4048 * sort direction of this inner column, but it does not matter since
4049 * the run-time row comparisons would only reach this column when
4050 * there's equality for the earlier column containing the same eclass.
4051 * There could be only one value in this column for the range of inner
4052 * rows having a given value in the earlier column, so it does not
4053 * matter which way we imagine this column to be ordered.) But a
4054 * non-redundant inner pathkey had better match outer's ordering too.
4055 */
4056 if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
4057 opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation)
4058 elog(ERROR, "left and right pathkeys do not match in mergejoin");
4059 if (first_inner_match &&
4060 (opathkey->pk_strategy != ipathkey->pk_strategy ||
4061 opathkey->pk_nulls_first != ipathkey->pk_nulls_first))
4062 elog(ERROR, "left and right pathkeys do not match in mergejoin");
4063
4064 /* OK, save info for executor */
4065 mergefamilies[i] = opathkey->pk_opfamily;
4066 mergecollations[i] = opathkey->pk_eclass->ec_collation;
4067 mergestrategies[i] = opathkey->pk_strategy;
4068 mergenullsfirst[i] = opathkey->pk_nulls_first;
4069 i++;
4070 }
4071
4072 /*
4073 * Note: it is not an error if we have additional pathkey elements (i.e.,
4074 * lop or lip isn't NULL here). The input paths might be better-sorted
4075 * than we need for the current mergejoin.
4076 */
4077
4078 /*
4079 * Now we can build the mergejoin node.
4080 */
4081 join_plan = make_mergejoin(tlist,
4082 joinclauses,
4083 otherclauses,
4084 mergeclauses,
4085 mergefamilies,
4086 mergecollations,
4087 mergestrategies,
4088 mergenullsfirst,
4089 outer_plan,
4090 inner_plan,
4091 best_path->jpath.jointype,
4092 best_path->jpath.inner_unique,
4093 best_path->skip_mark_restore);
4094
4095 /* Costs of sort and material steps are included in path cost already */
4096 copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4097
4098 return join_plan;
4099 }
4100
4101 static HashJoin *
create_hashjoin_plan(PlannerInfo * root,HashPath * best_path)4102 create_hashjoin_plan(PlannerInfo *root,
4103 HashPath *best_path)
4104 {
4105 HashJoin *join_plan;
4106 Hash *hash_plan;
4107 Plan *outer_plan;
4108 Plan *inner_plan;
4109 List *tlist = build_path_tlist(root, &best_path->jpath.path);
4110 List *joinclauses;
4111 List *otherclauses;
4112 List *hashclauses;
4113 Oid skewTable = InvalidOid;
4114 AttrNumber skewColumn = InvalidAttrNumber;
4115 bool skewInherit = false;
4116
4117 /*
4118 * HashJoin can project, so we don't have to demand exact tlists from the
4119 * inputs. However, it's best to request a small tlist from the inner
4120 * side, so that we aren't storing more data than necessary. Likewise, if
4121 * we anticipate batching, request a small tlist from the outer side so
4122 * that we don't put extra data in the outer batch files.
4123 */
4124 outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4125 (best_path->num_batches > 1) ? CP_SMALL_TLIST : 0);
4126
4127 inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4128 CP_SMALL_TLIST);
4129
4130 /* Sort join qual clauses into best execution order */
4131 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4132 /* There's no point in sorting the hash clauses ... */
4133
4134 /* Get the join qual clauses (in plain expression form) */
4135 /* Any pseudoconstant clauses are ignored here */
4136 if (IS_OUTER_JOIN(best_path->jpath.jointype))
4137 {
4138 extract_actual_join_clauses(joinclauses,
4139 best_path->jpath.path.parent->relids,
4140 &joinclauses, &otherclauses);
4141 }
4142 else
4143 {
4144 /* We can treat all clauses alike for an inner join */
4145 joinclauses = extract_actual_clauses(joinclauses, false);
4146 otherclauses = NIL;
4147 }
4148
4149 /*
4150 * Remove the hashclauses from the list of join qual clauses, leaving the
4151 * list of quals that must be checked as qpquals.
4152 */
4153 hashclauses = get_actual_clauses(best_path->path_hashclauses);
4154 joinclauses = list_difference(joinclauses, hashclauses);
4155
4156 /*
4157 * Replace any outer-relation variables with nestloop params. There
4158 * should not be any in the hashclauses.
4159 */
4160 if (best_path->jpath.path.param_info)
4161 {
4162 joinclauses = (List *)
4163 replace_nestloop_params(root, (Node *) joinclauses);
4164 otherclauses = (List *)
4165 replace_nestloop_params(root, (Node *) otherclauses);
4166 }
4167
4168 /*
4169 * Rearrange hashclauses, if needed, so that the outer variable is always
4170 * on the left.
4171 */
4172 hashclauses = get_switched_clauses(best_path->path_hashclauses,
4173 best_path->jpath.outerjoinpath->parent->relids);
4174
4175 /*
4176 * If there is a single join clause and we can identify the outer variable
4177 * as a simple column reference, supply its identity for possible use in
4178 * skew optimization. (Note: in principle we could do skew optimization
4179 * with multiple join clauses, but we'd have to be able to determine the
4180 * most common combinations of outer values, which we don't currently have
4181 * enough stats for.)
4182 */
4183 if (list_length(hashclauses) == 1)
4184 {
4185 OpExpr *clause = (OpExpr *) linitial(hashclauses);
4186 Node *node;
4187
4188 Assert(is_opclause(clause));
4189 node = (Node *) linitial(clause->args);
4190 if (IsA(node, RelabelType))
4191 node = (Node *) ((RelabelType *) node)->arg;
4192 if (IsA(node, Var))
4193 {
4194 Var *var = (Var *) node;
4195 RangeTblEntry *rte;
4196
4197 rte = root->simple_rte_array[var->varno];
4198 if (rte->rtekind == RTE_RELATION)
4199 {
4200 skewTable = rte->relid;
4201 skewColumn = var->varattno;
4202 skewInherit = rte->inh;
4203 }
4204 }
4205 }
4206
4207 /*
4208 * Build the hash node and hash join node.
4209 */
4210 hash_plan = make_hash(inner_plan,
4211 skewTable,
4212 skewColumn,
4213 skewInherit);
4214
4215 /*
4216 * Set Hash node's startup & total costs equal to total cost of input
4217 * plan; this only affects EXPLAIN display not decisions.
4218 */
4219 copy_plan_costsize(&hash_plan->plan, inner_plan);
4220 hash_plan->plan.startup_cost = hash_plan->plan.total_cost;
4221
4222 /*
4223 * If parallel-aware, the executor will also need an estimate of the total
4224 * number of rows expected from all participants so that it can size the
4225 * shared hash table.
4226 */
4227 if (best_path->jpath.path.parallel_aware)
4228 {
4229 hash_plan->plan.parallel_aware = true;
4230 hash_plan->rows_total = best_path->inner_rows_total;
4231 }
4232
4233 join_plan = make_hashjoin(tlist,
4234 joinclauses,
4235 otherclauses,
4236 hashclauses,
4237 outer_plan,
4238 (Plan *) hash_plan,
4239 best_path->jpath.jointype,
4240 best_path->jpath.inner_unique);
4241
4242 copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4243
4244 return join_plan;
4245 }
4246
4247
4248 /*****************************************************************************
4249 *
4250 * SUPPORTING ROUTINES
4251 *
4252 *****************************************************************************/
4253
4254 /*
4255 * replace_nestloop_params
4256 * Replace outer-relation Vars and PlaceHolderVars in the given expression
4257 * with nestloop Params
4258 *
4259 * All Vars and PlaceHolderVars belonging to the relation(s) identified by
4260 * root->curOuterRels are replaced by Params, and entries are added to
4261 * root->curOuterParams if not already present.
4262 */
4263 static Node *
replace_nestloop_params(PlannerInfo * root,Node * expr)4264 replace_nestloop_params(PlannerInfo *root, Node *expr)
4265 {
4266 /* No setup needed for tree walk, so away we go */
4267 return replace_nestloop_params_mutator(expr, root);
4268 }
4269
4270 static Node *
replace_nestloop_params_mutator(Node * node,PlannerInfo * root)4271 replace_nestloop_params_mutator(Node *node, PlannerInfo *root)
4272 {
4273 if (node == NULL)
4274 return NULL;
4275 if (IsA(node, Var))
4276 {
4277 Var *var = (Var *) node;
4278
4279 /* Upper-level Vars should be long gone at this point */
4280 Assert(var->varlevelsup == 0);
4281 /* If not to be replaced, we can just return the Var unmodified */
4282 if (!bms_is_member(var->varno, root->curOuterRels))
4283 return node;
4284 /* Replace the Var with a nestloop Param */
4285 return (Node *) replace_nestloop_param_var(root, var);
4286 }
4287 if (IsA(node, PlaceHolderVar))
4288 {
4289 PlaceHolderVar *phv = (PlaceHolderVar *) node;
4290
4291 /* Upper-level PlaceHolderVars should be long gone at this point */
4292 Assert(phv->phlevelsup == 0);
4293
4294 /*
4295 * Check whether we need to replace the PHV. We use bms_overlap as a
4296 * cheap/quick test to see if the PHV might be evaluated in the outer
4297 * rels, and then grab its PlaceHolderInfo to tell for sure.
4298 */
4299 if (!bms_overlap(phv->phrels, root->curOuterRels) ||
4300 !bms_is_subset(find_placeholder_info(root, phv, false)->ph_eval_at,
4301 root->curOuterRels))
4302 {
4303 /*
4304 * We can't replace the whole PHV, but we might still need to
4305 * replace Vars or PHVs within its expression, in case it ends up
4306 * actually getting evaluated here. (It might get evaluated in
4307 * this plan node, or some child node; in the latter case we don't
4308 * really need to process the expression here, but we haven't got
4309 * enough info to tell if that's the case.) Flat-copy the PHV
4310 * node and then recurse on its expression.
4311 *
4312 * Note that after doing this, we might have different
4313 * representations of the contents of the same PHV in different
4314 * parts of the plan tree. This is OK because equal() will just
4315 * match on phid/phlevelsup, so setrefs.c will still recognize an
4316 * upper-level reference to a lower-level copy of the same PHV.
4317 */
4318 PlaceHolderVar *newphv = makeNode(PlaceHolderVar);
4319
4320 memcpy(newphv, phv, sizeof(PlaceHolderVar));
4321 newphv->phexpr = (Expr *)
4322 replace_nestloop_params_mutator((Node *) phv->phexpr,
4323 root);
4324 return (Node *) newphv;
4325 }
4326 /* Replace the PlaceHolderVar with a nestloop Param */
4327 return (Node *) replace_nestloop_param_placeholdervar(root, phv);
4328 }
4329 return expression_tree_mutator(node,
4330 replace_nestloop_params_mutator,
4331 (void *) root);
4332 }
4333
4334 /*
4335 * fix_indexqual_references
4336 * Adjust indexqual clauses to the form the executor's indexqual
4337 * machinery needs.
4338 *
4339 * We have four tasks here:
4340 * * Remove RestrictInfo nodes from the input clauses.
4341 * * Replace any outer-relation Var or PHV nodes with nestloop Params.
4342 * (XXX eventually, that responsibility should go elsewhere?)
4343 * * Index keys must be represented by Var nodes with varattno set to the
4344 * index's attribute number, not the attribute number in the original rel.
4345 * * If the index key is on the right, commute the clause to put it on the
4346 * left.
4347 *
4348 * The result is a modified copy of the path's indexquals list --- the
4349 * original is not changed. Note also that the copy shares no substructure
4350 * with the original; this is needed in case there is a subplan in it (we need
4351 * two separate copies of the subplan tree, or things will go awry).
4352 */
4353 static List *
fix_indexqual_references(PlannerInfo * root,IndexPath * index_path)4354 fix_indexqual_references(PlannerInfo *root, IndexPath *index_path)
4355 {
4356 IndexOptInfo *index = index_path->indexinfo;
4357 List *fixed_indexquals;
4358 ListCell *lcc,
4359 *lci;
4360
4361 fixed_indexquals = NIL;
4362
4363 forboth(lcc, index_path->indexquals, lci, index_path->indexqualcols)
4364 {
4365 RestrictInfo *rinfo = lfirst_node(RestrictInfo, lcc);
4366 int indexcol = lfirst_int(lci);
4367 Node *clause;
4368
4369 /*
4370 * Replace any outer-relation variables with nestloop params.
4371 *
4372 * This also makes a copy of the clause, so it's safe to modify it
4373 * in-place below.
4374 */
4375 clause = replace_nestloop_params(root, (Node *) rinfo->clause);
4376
4377 if (IsA(clause, OpExpr))
4378 {
4379 OpExpr *op = (OpExpr *) clause;
4380
4381 if (list_length(op->args) != 2)
4382 elog(ERROR, "indexqual clause is not binary opclause");
4383
4384 /*
4385 * Check to see if the indexkey is on the right; if so, commute
4386 * the clause. The indexkey should be the side that refers to
4387 * (only) the base relation.
4388 */
4389 if (!bms_equal(rinfo->left_relids, index->rel->relids))
4390 CommuteOpExpr(op);
4391
4392 /*
4393 * Now replace the indexkey expression with an index Var.
4394 */
4395 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
4396 index,
4397 indexcol);
4398 }
4399 else if (IsA(clause, RowCompareExpr))
4400 {
4401 RowCompareExpr *rc = (RowCompareExpr *) clause;
4402 Expr *newrc;
4403 List *indexcolnos;
4404 bool var_on_left;
4405 ListCell *lca,
4406 *lcai;
4407
4408 /*
4409 * Re-discover which index columns are used in the rowcompare.
4410 */
4411 newrc = adjust_rowcompare_for_index(rc,
4412 index,
4413 indexcol,
4414 &indexcolnos,
4415 &var_on_left);
4416
4417 /*
4418 * Trouble if adjust_rowcompare_for_index thought the
4419 * RowCompareExpr didn't match the index as-is; the clause should
4420 * have gone through that routine already.
4421 */
4422 if (newrc != (Expr *) rc)
4423 elog(ERROR, "inconsistent results from adjust_rowcompare_for_index");
4424
4425 /*
4426 * Check to see if the indexkey is on the right; if so, commute
4427 * the clause.
4428 */
4429 if (!var_on_left)
4430 CommuteRowCompareExpr(rc);
4431
4432 /*
4433 * Now replace the indexkey expressions with index Vars.
4434 */
4435 Assert(list_length(rc->largs) == list_length(indexcolnos));
4436 forboth(lca, rc->largs, lcai, indexcolnos)
4437 {
4438 lfirst(lca) = fix_indexqual_operand(lfirst(lca),
4439 index,
4440 lfirst_int(lcai));
4441 }
4442 }
4443 else if (IsA(clause, ScalarArrayOpExpr))
4444 {
4445 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
4446
4447 /* Never need to commute... */
4448
4449 /* Replace the indexkey expression with an index Var. */
4450 linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
4451 index,
4452 indexcol);
4453 }
4454 else if (IsA(clause, NullTest))
4455 {
4456 NullTest *nt = (NullTest *) clause;
4457
4458 /* Replace the indexkey expression with an index Var. */
4459 nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
4460 index,
4461 indexcol);
4462 }
4463 else
4464 elog(ERROR, "unsupported indexqual type: %d",
4465 (int) nodeTag(clause));
4466
4467 fixed_indexquals = lappend(fixed_indexquals, clause);
4468 }
4469
4470 return fixed_indexquals;
4471 }
4472
4473 /*
4474 * fix_indexorderby_references
4475 * Adjust indexorderby clauses to the form the executor's index
4476 * machinery needs.
4477 *
4478 * This is a simplified version of fix_indexqual_references. The input does
4479 * not have RestrictInfo nodes, and we assume that indxpath.c already
4480 * commuted the clauses to put the index keys on the left. Also, we don't
4481 * bother to support any cases except simple OpExprs, since nothing else
4482 * is allowed for ordering operators.
4483 */
4484 static List *
fix_indexorderby_references(PlannerInfo * root,IndexPath * index_path)4485 fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path)
4486 {
4487 IndexOptInfo *index = index_path->indexinfo;
4488 List *fixed_indexorderbys;
4489 ListCell *lcc,
4490 *lci;
4491
4492 fixed_indexorderbys = NIL;
4493
4494 forboth(lcc, index_path->indexorderbys, lci, index_path->indexorderbycols)
4495 {
4496 Node *clause = (Node *) lfirst(lcc);
4497 int indexcol = lfirst_int(lci);
4498
4499 /*
4500 * Replace any outer-relation variables with nestloop params.
4501 *
4502 * This also makes a copy of the clause, so it's safe to modify it
4503 * in-place below.
4504 */
4505 clause = replace_nestloop_params(root, clause);
4506
4507 if (IsA(clause, OpExpr))
4508 {
4509 OpExpr *op = (OpExpr *) clause;
4510
4511 if (list_length(op->args) != 2)
4512 elog(ERROR, "indexorderby clause is not binary opclause");
4513
4514 /*
4515 * Now replace the indexkey expression with an index Var.
4516 */
4517 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
4518 index,
4519 indexcol);
4520 }
4521 else
4522 elog(ERROR, "unsupported indexorderby type: %d",
4523 (int) nodeTag(clause));
4524
4525 fixed_indexorderbys = lappend(fixed_indexorderbys, clause);
4526 }
4527
4528 return fixed_indexorderbys;
4529 }
4530
4531 /*
4532 * fix_indexqual_operand
4533 * Convert an indexqual expression to a Var referencing the index column.
4534 *
4535 * We represent index keys by Var nodes having varno == INDEX_VAR and varattno
4536 * equal to the index's attribute number (index column position).
4537 *
4538 * Most of the code here is just for sanity cross-checking that the given
4539 * expression actually matches the index column it's claimed to.
4540 */
4541 static Node *
fix_indexqual_operand(Node * node,IndexOptInfo * index,int indexcol)4542 fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol)
4543 {
4544 Var *result;
4545 int pos;
4546 ListCell *indexpr_item;
4547
4548 /*
4549 * Remove any binary-compatible relabeling of the indexkey
4550 */
4551 if (IsA(node, RelabelType))
4552 node = (Node *) ((RelabelType *) node)->arg;
4553
4554 Assert(indexcol >= 0 && indexcol < index->ncolumns);
4555
4556 if (index->indexkeys[indexcol] != 0)
4557 {
4558 /* It's a simple index column */
4559 if (IsA(node, Var) &&
4560 ((Var *) node)->varno == index->rel->relid &&
4561 ((Var *) node)->varattno == index->indexkeys[indexcol])
4562 {
4563 result = (Var *) copyObject(node);
4564 result->varno = INDEX_VAR;
4565 result->varattno = indexcol + 1;
4566 return (Node *) result;
4567 }
4568 else
4569 elog(ERROR, "index key does not match expected index column");
4570 }
4571
4572 /* It's an index expression, so find and cross-check the expression */
4573 indexpr_item = list_head(index->indexprs);
4574 for (pos = 0; pos < index->ncolumns; pos++)
4575 {
4576 if (index->indexkeys[pos] == 0)
4577 {
4578 if (indexpr_item == NULL)
4579 elog(ERROR, "too few entries in indexprs list");
4580 if (pos == indexcol)
4581 {
4582 Node *indexkey;
4583
4584 indexkey = (Node *) lfirst(indexpr_item);
4585 if (indexkey && IsA(indexkey, RelabelType))
4586 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
4587 if (equal(node, indexkey))
4588 {
4589 result = makeVar(INDEX_VAR, indexcol + 1,
4590 exprType(lfirst(indexpr_item)), -1,
4591 exprCollation(lfirst(indexpr_item)),
4592 0);
4593 return (Node *) result;
4594 }
4595 else
4596 elog(ERROR, "index key does not match expected index column");
4597 }
4598 indexpr_item = lnext(indexpr_item);
4599 }
4600 }
4601
4602 /* Oops... */
4603 elog(ERROR, "index key does not match expected index column");
4604 return NULL; /* keep compiler quiet */
4605 }
4606
4607 /*
4608 * get_switched_clauses
4609 * Given a list of merge or hash joinclauses (as RestrictInfo nodes),
4610 * extract the bare clauses, and rearrange the elements within the
4611 * clauses, if needed, so the outer join variable is on the left and
4612 * the inner is on the right. The original clause data structure is not
4613 * touched; a modified list is returned. We do, however, set the transient
4614 * outer_is_left field in each RestrictInfo to show which side was which.
4615 */
4616 static List *
get_switched_clauses(List * clauses,Relids outerrelids)4617 get_switched_clauses(List *clauses, Relids outerrelids)
4618 {
4619 List *t_list = NIL;
4620 ListCell *l;
4621
4622 foreach(l, clauses)
4623 {
4624 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
4625 OpExpr *clause = (OpExpr *) restrictinfo->clause;
4626
4627 Assert(is_opclause(clause));
4628 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
4629 {
4630 /*
4631 * Duplicate just enough of the structure to allow commuting the
4632 * clause without changing the original list. Could use
4633 * copyObject, but a complete deep copy is overkill.
4634 */
4635 OpExpr *temp = makeNode(OpExpr);
4636
4637 temp->opno = clause->opno;
4638 temp->opfuncid = InvalidOid;
4639 temp->opresulttype = clause->opresulttype;
4640 temp->opretset = clause->opretset;
4641 temp->opcollid = clause->opcollid;
4642 temp->inputcollid = clause->inputcollid;
4643 temp->args = list_copy(clause->args);
4644 temp->location = clause->location;
4645 /* Commute it --- note this modifies the temp node in-place. */
4646 CommuteOpExpr(temp);
4647 t_list = lappend(t_list, temp);
4648 restrictinfo->outer_is_left = false;
4649 }
4650 else
4651 {
4652 Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
4653 t_list = lappend(t_list, clause);
4654 restrictinfo->outer_is_left = true;
4655 }
4656 }
4657 return t_list;
4658 }
4659
4660 /*
4661 * order_qual_clauses
4662 * Given a list of qual clauses that will all be evaluated at the same
4663 * plan node, sort the list into the order we want to check the quals
4664 * in at runtime.
4665 *
4666 * When security barrier quals are used in the query, we may have quals with
4667 * different security levels in the list. Quals of lower security_level
4668 * must go before quals of higher security_level, except that we can grant
4669 * exceptions to move up quals that are leakproof. When security level
4670 * doesn't force the decision, we prefer to order clauses by estimated
4671 * execution cost, cheapest first.
4672 *
4673 * Ideally the order should be driven by a combination of execution cost and
4674 * selectivity, but it's not immediately clear how to account for both,
4675 * and given the uncertainty of the estimates the reliability of the decisions
4676 * would be doubtful anyway. So we just order by security level then
4677 * estimated per-tuple cost, being careful not to change the order when
4678 * (as is often the case) the estimates are identical.
4679 *
4680 * Although this will work on either bare clauses or RestrictInfos, it's
4681 * much faster to apply it to RestrictInfos, since it can re-use cost
4682 * information that is cached in RestrictInfos. XXX in the bare-clause
4683 * case, we are also not able to apply security considerations. That is
4684 * all right for the moment, because the bare-clause case doesn't occur
4685 * anywhere that barrier quals could be present, but it would be better to
4686 * get rid of it.
4687 *
4688 * Note: some callers pass lists that contain entries that will later be
4689 * removed; this is the easiest way to let this routine see RestrictInfos
4690 * instead of bare clauses. This is another reason why trying to consider
4691 * selectivity in the ordering would likely do the wrong thing.
4692 */
4693 static List *
order_qual_clauses(PlannerInfo * root,List * clauses)4694 order_qual_clauses(PlannerInfo *root, List *clauses)
4695 {
4696 typedef struct
4697 {
4698 Node *clause;
4699 Cost cost;
4700 Index security_level;
4701 } QualItem;
4702 int nitems = list_length(clauses);
4703 QualItem *items;
4704 ListCell *lc;
4705 int i;
4706 List *result;
4707
4708 /* No need to work hard for 0 or 1 clause */
4709 if (nitems <= 1)
4710 return clauses;
4711
4712 /*
4713 * Collect the items and costs into an array. This is to avoid repeated
4714 * cost_qual_eval work if the inputs aren't RestrictInfos.
4715 */
4716 items = (QualItem *) palloc(nitems * sizeof(QualItem));
4717 i = 0;
4718 foreach(lc, clauses)
4719 {
4720 Node *clause = (Node *) lfirst(lc);
4721 QualCost qcost;
4722
4723 cost_qual_eval_node(&qcost, clause, root);
4724 items[i].clause = clause;
4725 items[i].cost = qcost.per_tuple;
4726 if (IsA(clause, RestrictInfo))
4727 {
4728 RestrictInfo *rinfo = (RestrictInfo *) clause;
4729
4730 /*
4731 * If a clause is leakproof, it doesn't have to be constrained by
4732 * its nominal security level. If it's also reasonably cheap
4733 * (here defined as 10X cpu_operator_cost), pretend it has
4734 * security_level 0, which will allow it to go in front of
4735 * more-expensive quals of lower security levels. Of course, that
4736 * will also force it to go in front of cheaper quals of its own
4737 * security level, which is not so great, but we can alleviate
4738 * that risk by applying the cost limit cutoff.
4739 */
4740 if (rinfo->leakproof && items[i].cost < 10 * cpu_operator_cost)
4741 items[i].security_level = 0;
4742 else
4743 items[i].security_level = rinfo->security_level;
4744 }
4745 else
4746 items[i].security_level = 0;
4747 i++;
4748 }
4749
4750 /*
4751 * Sort. We don't use qsort() because it's not guaranteed stable for
4752 * equal keys. The expected number of entries is small enough that a
4753 * simple insertion sort should be good enough.
4754 */
4755 for (i = 1; i < nitems; i++)
4756 {
4757 QualItem newitem = items[i];
4758 int j;
4759
4760 /* insert newitem into the already-sorted subarray */
4761 for (j = i; j > 0; j--)
4762 {
4763 QualItem *olditem = &items[j - 1];
4764
4765 if (newitem.security_level > olditem->security_level ||
4766 (newitem.security_level == olditem->security_level &&
4767 newitem.cost >= olditem->cost))
4768 break;
4769 items[j] = *olditem;
4770 }
4771 items[j] = newitem;
4772 }
4773
4774 /* Convert back to a list */
4775 result = NIL;
4776 for (i = 0; i < nitems; i++)
4777 result = lappend(result, items[i].clause);
4778
4779 return result;
4780 }
4781
4782 /*
4783 * Copy cost and size info from a Path node to the Plan node created from it.
4784 * The executor usually won't use this info, but it's needed by EXPLAIN.
4785 * Also copy the parallel-related flags, which the executor *will* use.
4786 */
4787 static void
copy_generic_path_info(Plan * dest,Path * src)4788 copy_generic_path_info(Plan *dest, Path *src)
4789 {
4790 dest->startup_cost = src->startup_cost;
4791 dest->total_cost = src->total_cost;
4792 dest->plan_rows = src->rows;
4793 dest->plan_width = src->pathtarget->width;
4794 dest->parallel_aware = src->parallel_aware;
4795 dest->parallel_safe = src->parallel_safe;
4796 }
4797
4798 /*
4799 * Copy cost and size info from a lower plan node to an inserted node.
4800 * (Most callers alter the info after copying it.)
4801 */
4802 static void
copy_plan_costsize(Plan * dest,Plan * src)4803 copy_plan_costsize(Plan *dest, Plan *src)
4804 {
4805 dest->startup_cost = src->startup_cost;
4806 dest->total_cost = src->total_cost;
4807 dest->plan_rows = src->plan_rows;
4808 dest->plan_width = src->plan_width;
4809 /* Assume the inserted node is not parallel-aware. */
4810 dest->parallel_aware = false;
4811 /* Assume the inserted node is parallel-safe, if child plan is. */
4812 dest->parallel_safe = src->parallel_safe;
4813 }
4814
4815 /*
4816 * Some places in this file build Sort nodes that don't have a directly
4817 * corresponding Path node. The cost of the sort is, or should have been,
4818 * included in the cost of the Path node we're working from, but since it's
4819 * not split out, we have to re-figure it using cost_sort(). This is just
4820 * to label the Sort node nicely for EXPLAIN.
4821 *
4822 * limit_tuples is as for cost_sort (in particular, pass -1 if no limit)
4823 */
4824 static void
label_sort_with_costsize(PlannerInfo * root,Sort * plan,double limit_tuples)4825 label_sort_with_costsize(PlannerInfo *root, Sort *plan, double limit_tuples)
4826 {
4827 Plan *lefttree = plan->plan.lefttree;
4828 Path sort_path; /* dummy for result of cost_sort */
4829
4830 cost_sort(&sort_path, root, NIL,
4831 lefttree->total_cost,
4832 lefttree->plan_rows,
4833 lefttree->plan_width,
4834 0.0,
4835 work_mem,
4836 limit_tuples);
4837 plan->plan.startup_cost = sort_path.startup_cost;
4838 plan->plan.total_cost = sort_path.total_cost;
4839 plan->plan.plan_rows = lefttree->plan_rows;
4840 plan->plan.plan_width = lefttree->plan_width;
4841 plan->plan.parallel_aware = false;
4842 plan->plan.parallel_safe = lefttree->parallel_safe;
4843 }
4844
4845 /*
4846 * bitmap_subplan_mark_shared
4847 * Set isshared flag in bitmap subplan so that it will be created in
4848 * shared memory.
4849 */
4850 static void
bitmap_subplan_mark_shared(Plan * plan)4851 bitmap_subplan_mark_shared(Plan *plan)
4852 {
4853 if (IsA(plan, BitmapAnd))
4854 bitmap_subplan_mark_shared(
4855 linitial(((BitmapAnd *) plan)->bitmapplans));
4856 else if (IsA(plan, BitmapOr))
4857 {
4858 ((BitmapOr *) plan)->isshared = true;
4859 bitmap_subplan_mark_shared(
4860 linitial(((BitmapOr *) plan)->bitmapplans));
4861 }
4862 else if (IsA(plan, BitmapIndexScan))
4863 ((BitmapIndexScan *) plan)->isshared = true;
4864 else
4865 elog(ERROR, "unrecognized node type: %d", nodeTag(plan));
4866 }
4867
4868 /*
4869 * flatten_partitioned_rels
4870 * Convert List of Lists into a single List with all elements from the
4871 * sub-lists.
4872 */
4873 static List *
flatten_partitioned_rels(List * partitioned_rels)4874 flatten_partitioned_rels(List *partitioned_rels)
4875 {
4876 List *newlist = NIL;
4877 ListCell *lc;
4878
4879 foreach(lc, partitioned_rels)
4880 {
4881 List *sublist = lfirst(lc);
4882
4883 newlist = list_concat(newlist, list_copy(sublist));
4884 }
4885
4886 return newlist;
4887 }
4888
4889 /*****************************************************************************
4890 *
4891 * PLAN NODE BUILDING ROUTINES
4892 *
4893 * In general, these functions are not passed the original Path and therefore
4894 * leave it to the caller to fill in the cost/width fields from the Path,
4895 * typically by calling copy_generic_path_info(). This convention is
4896 * somewhat historical, but it does support a few places above where we build
4897 * a plan node without having an exactly corresponding Path node. Under no
4898 * circumstances should one of these functions do its own cost calculations,
4899 * as that would be redundant with calculations done while building Paths.
4900 *
4901 *****************************************************************************/
4902
4903 static SeqScan *
make_seqscan(List * qptlist,List * qpqual,Index scanrelid)4904 make_seqscan(List *qptlist,
4905 List *qpqual,
4906 Index scanrelid)
4907 {
4908 SeqScan *node = makeNode(SeqScan);
4909 Plan *plan = &node->plan;
4910
4911 plan->targetlist = qptlist;
4912 plan->qual = qpqual;
4913 plan->lefttree = NULL;
4914 plan->righttree = NULL;
4915 node->scanrelid = scanrelid;
4916
4917 return node;
4918 }
4919
4920 static SampleScan *
make_samplescan(List * qptlist,List * qpqual,Index scanrelid,TableSampleClause * tsc)4921 make_samplescan(List *qptlist,
4922 List *qpqual,
4923 Index scanrelid,
4924 TableSampleClause *tsc)
4925 {
4926 SampleScan *node = makeNode(SampleScan);
4927 Plan *plan = &node->scan.plan;
4928
4929 plan->targetlist = qptlist;
4930 plan->qual = qpqual;
4931 plan->lefttree = NULL;
4932 plan->righttree = NULL;
4933 node->scan.scanrelid = scanrelid;
4934 node->tablesample = tsc;
4935
4936 return node;
4937 }
4938
4939 static IndexScan *
make_indexscan(List * qptlist,List * qpqual,Index scanrelid,Oid indexid,List * indexqual,List * indexqualorig,List * indexorderby,List * indexorderbyorig,List * indexorderbyops,ScanDirection indexscandir)4940 make_indexscan(List *qptlist,
4941 List *qpqual,
4942 Index scanrelid,
4943 Oid indexid,
4944 List *indexqual,
4945 List *indexqualorig,
4946 List *indexorderby,
4947 List *indexorderbyorig,
4948 List *indexorderbyops,
4949 ScanDirection indexscandir)
4950 {
4951 IndexScan *node = makeNode(IndexScan);
4952 Plan *plan = &node->scan.plan;
4953
4954 plan->targetlist = qptlist;
4955 plan->qual = qpqual;
4956 plan->lefttree = NULL;
4957 plan->righttree = NULL;
4958 node->scan.scanrelid = scanrelid;
4959 node->indexid = indexid;
4960 node->indexqual = indexqual;
4961 node->indexqualorig = indexqualorig;
4962 node->indexorderby = indexorderby;
4963 node->indexorderbyorig = indexorderbyorig;
4964 node->indexorderbyops = indexorderbyops;
4965 node->indexorderdir = indexscandir;
4966
4967 return node;
4968 }
4969
4970 static IndexOnlyScan *
make_indexonlyscan(List * qptlist,List * qpqual,Index scanrelid,Oid indexid,List * indexqual,List * indexorderby,List * indextlist,ScanDirection indexscandir)4971 make_indexonlyscan(List *qptlist,
4972 List *qpqual,
4973 Index scanrelid,
4974 Oid indexid,
4975 List *indexqual,
4976 List *indexorderby,
4977 List *indextlist,
4978 ScanDirection indexscandir)
4979 {
4980 IndexOnlyScan *node = makeNode(IndexOnlyScan);
4981 Plan *plan = &node->scan.plan;
4982
4983 plan->targetlist = qptlist;
4984 plan->qual = qpqual;
4985 plan->lefttree = NULL;
4986 plan->righttree = NULL;
4987 node->scan.scanrelid = scanrelid;
4988 node->indexid = indexid;
4989 node->indexqual = indexqual;
4990 node->indexorderby = indexorderby;
4991 node->indextlist = indextlist;
4992 node->indexorderdir = indexscandir;
4993
4994 return node;
4995 }
4996
4997 static BitmapIndexScan *
make_bitmap_indexscan(Index scanrelid,Oid indexid,List * indexqual,List * indexqualorig)4998 make_bitmap_indexscan(Index scanrelid,
4999 Oid indexid,
5000 List *indexqual,
5001 List *indexqualorig)
5002 {
5003 BitmapIndexScan *node = makeNode(BitmapIndexScan);
5004 Plan *plan = &node->scan.plan;
5005
5006 plan->targetlist = NIL; /* not used */
5007 plan->qual = NIL; /* not used */
5008 plan->lefttree = NULL;
5009 plan->righttree = NULL;
5010 node->scan.scanrelid = scanrelid;
5011 node->indexid = indexid;
5012 node->indexqual = indexqual;
5013 node->indexqualorig = indexqualorig;
5014
5015 return node;
5016 }
5017
5018 static BitmapHeapScan *
make_bitmap_heapscan(List * qptlist,List * qpqual,Plan * lefttree,List * bitmapqualorig,Index scanrelid)5019 make_bitmap_heapscan(List *qptlist,
5020 List *qpqual,
5021 Plan *lefttree,
5022 List *bitmapqualorig,
5023 Index scanrelid)
5024 {
5025 BitmapHeapScan *node = makeNode(BitmapHeapScan);
5026 Plan *plan = &node->scan.plan;
5027
5028 plan->targetlist = qptlist;
5029 plan->qual = qpqual;
5030 plan->lefttree = lefttree;
5031 plan->righttree = NULL;
5032 node->scan.scanrelid = scanrelid;
5033 node->bitmapqualorig = bitmapqualorig;
5034
5035 return node;
5036 }
5037
5038 static TidScan *
make_tidscan(List * qptlist,List * qpqual,Index scanrelid,List * tidquals)5039 make_tidscan(List *qptlist,
5040 List *qpqual,
5041 Index scanrelid,
5042 List *tidquals)
5043 {
5044 TidScan *node = makeNode(TidScan);
5045 Plan *plan = &node->scan.plan;
5046
5047 plan->targetlist = qptlist;
5048 plan->qual = qpqual;
5049 plan->lefttree = NULL;
5050 plan->righttree = NULL;
5051 node->scan.scanrelid = scanrelid;
5052 node->tidquals = tidquals;
5053
5054 return node;
5055 }
5056
5057 static SubqueryScan *
make_subqueryscan(List * qptlist,List * qpqual,Index scanrelid,Plan * subplan)5058 make_subqueryscan(List *qptlist,
5059 List *qpqual,
5060 Index scanrelid,
5061 Plan *subplan)
5062 {
5063 SubqueryScan *node = makeNode(SubqueryScan);
5064 Plan *plan = &node->scan.plan;
5065
5066 plan->targetlist = qptlist;
5067 plan->qual = qpqual;
5068 plan->lefttree = NULL;
5069 plan->righttree = NULL;
5070 node->scan.scanrelid = scanrelid;
5071 node->subplan = subplan;
5072
5073 return node;
5074 }
5075
5076 static FunctionScan *
make_functionscan(List * qptlist,List * qpqual,Index scanrelid,List * functions,bool funcordinality)5077 make_functionscan(List *qptlist,
5078 List *qpqual,
5079 Index scanrelid,
5080 List *functions,
5081 bool funcordinality)
5082 {
5083 FunctionScan *node = makeNode(FunctionScan);
5084 Plan *plan = &node->scan.plan;
5085
5086 plan->targetlist = qptlist;
5087 plan->qual = qpqual;
5088 plan->lefttree = NULL;
5089 plan->righttree = NULL;
5090 node->scan.scanrelid = scanrelid;
5091 node->functions = functions;
5092 node->funcordinality = funcordinality;
5093
5094 return node;
5095 }
5096
5097 static TableFuncScan *
make_tablefuncscan(List * qptlist,List * qpqual,Index scanrelid,TableFunc * tablefunc)5098 make_tablefuncscan(List *qptlist,
5099 List *qpqual,
5100 Index scanrelid,
5101 TableFunc *tablefunc)
5102 {
5103 TableFuncScan *node = makeNode(TableFuncScan);
5104 Plan *plan = &node->scan.plan;
5105
5106 plan->targetlist = qptlist;
5107 plan->qual = qpqual;
5108 plan->lefttree = NULL;
5109 plan->righttree = NULL;
5110 node->scan.scanrelid = scanrelid;
5111 node->tablefunc = tablefunc;
5112
5113 return node;
5114 }
5115
5116 static ValuesScan *
make_valuesscan(List * qptlist,List * qpqual,Index scanrelid,List * values_lists)5117 make_valuesscan(List *qptlist,
5118 List *qpqual,
5119 Index scanrelid,
5120 List *values_lists)
5121 {
5122 ValuesScan *node = makeNode(ValuesScan);
5123 Plan *plan = &node->scan.plan;
5124
5125 plan->targetlist = qptlist;
5126 plan->qual = qpqual;
5127 plan->lefttree = NULL;
5128 plan->righttree = NULL;
5129 node->scan.scanrelid = scanrelid;
5130 node->values_lists = values_lists;
5131
5132 return node;
5133 }
5134
5135 static CteScan *
make_ctescan(List * qptlist,List * qpqual,Index scanrelid,int ctePlanId,int cteParam)5136 make_ctescan(List *qptlist,
5137 List *qpqual,
5138 Index scanrelid,
5139 int ctePlanId,
5140 int cteParam)
5141 {
5142 CteScan *node = makeNode(CteScan);
5143 Plan *plan = &node->scan.plan;
5144
5145 plan->targetlist = qptlist;
5146 plan->qual = qpqual;
5147 plan->lefttree = NULL;
5148 plan->righttree = NULL;
5149 node->scan.scanrelid = scanrelid;
5150 node->ctePlanId = ctePlanId;
5151 node->cteParam = cteParam;
5152
5153 return node;
5154 }
5155
5156 static NamedTuplestoreScan *
make_namedtuplestorescan(List * qptlist,List * qpqual,Index scanrelid,char * enrname)5157 make_namedtuplestorescan(List *qptlist,
5158 List *qpqual,
5159 Index scanrelid,
5160 char *enrname)
5161 {
5162 NamedTuplestoreScan *node = makeNode(NamedTuplestoreScan);
5163 Plan *plan = &node->scan.plan;
5164
5165 /* cost should be inserted by caller */
5166 plan->targetlist = qptlist;
5167 plan->qual = qpqual;
5168 plan->lefttree = NULL;
5169 plan->righttree = NULL;
5170 node->scan.scanrelid = scanrelid;
5171 node->enrname = enrname;
5172
5173 return node;
5174 }
5175
5176 static WorkTableScan *
make_worktablescan(List * qptlist,List * qpqual,Index scanrelid,int wtParam)5177 make_worktablescan(List *qptlist,
5178 List *qpqual,
5179 Index scanrelid,
5180 int wtParam)
5181 {
5182 WorkTableScan *node = makeNode(WorkTableScan);
5183 Plan *plan = &node->scan.plan;
5184
5185 plan->targetlist = qptlist;
5186 plan->qual = qpqual;
5187 plan->lefttree = NULL;
5188 plan->righttree = NULL;
5189 node->scan.scanrelid = scanrelid;
5190 node->wtParam = wtParam;
5191
5192 return node;
5193 }
5194
5195 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)5196 make_foreignscan(List *qptlist,
5197 List *qpqual,
5198 Index scanrelid,
5199 List *fdw_exprs,
5200 List *fdw_private,
5201 List *fdw_scan_tlist,
5202 List *fdw_recheck_quals,
5203 Plan *outer_plan)
5204 {
5205 ForeignScan *node = makeNode(ForeignScan);
5206 Plan *plan = &node->scan.plan;
5207
5208 /* cost will be filled in by create_foreignscan_plan */
5209 plan->targetlist = qptlist;
5210 plan->qual = qpqual;
5211 plan->lefttree = outer_plan;
5212 plan->righttree = NULL;
5213 node->scan.scanrelid = scanrelid;
5214 node->operation = CMD_SELECT;
5215 /* fs_server will be filled in by create_foreignscan_plan */
5216 node->fs_server = InvalidOid;
5217 node->fdw_exprs = fdw_exprs;
5218 node->fdw_private = fdw_private;
5219 node->fdw_scan_tlist = fdw_scan_tlist;
5220 node->fdw_recheck_quals = fdw_recheck_quals;
5221 /* fs_relids will be filled in by create_foreignscan_plan */
5222 node->fs_relids = NULL;
5223 /* fsSystemCol will be filled in by create_foreignscan_plan */
5224 node->fsSystemCol = false;
5225
5226 return node;
5227 }
5228
5229 static Append *
make_append(List * appendplans,int first_partial_plan,List * tlist,List * partitioned_rels,PartitionPruneInfo * partpruneinfo)5230 make_append(List *appendplans, int first_partial_plan,
5231 List *tlist, List *partitioned_rels,
5232 PartitionPruneInfo *partpruneinfo)
5233 {
5234 Append *node = makeNode(Append);
5235 Plan *plan = &node->plan;
5236
5237 plan->targetlist = tlist;
5238 plan->qual = NIL;
5239 plan->lefttree = NULL;
5240 plan->righttree = NULL;
5241 node->appendplans = appendplans;
5242 node->first_partial_plan = first_partial_plan;
5243 node->partitioned_rels = flatten_partitioned_rels(partitioned_rels);
5244 node->part_prune_info = partpruneinfo;
5245 return node;
5246 }
5247
5248 static RecursiveUnion *
make_recursive_union(List * tlist,Plan * lefttree,Plan * righttree,int wtParam,List * distinctList,long numGroups)5249 make_recursive_union(List *tlist,
5250 Plan *lefttree,
5251 Plan *righttree,
5252 int wtParam,
5253 List *distinctList,
5254 long numGroups)
5255 {
5256 RecursiveUnion *node = makeNode(RecursiveUnion);
5257 Plan *plan = &node->plan;
5258 int numCols = list_length(distinctList);
5259
5260 plan->targetlist = tlist;
5261 plan->qual = NIL;
5262 plan->lefttree = lefttree;
5263 plan->righttree = righttree;
5264 node->wtParam = wtParam;
5265
5266 /*
5267 * convert SortGroupClause list into arrays of attr indexes and equality
5268 * operators, as wanted by executor
5269 */
5270 node->numCols = numCols;
5271 if (numCols > 0)
5272 {
5273 int keyno = 0;
5274 AttrNumber *dupColIdx;
5275 Oid *dupOperators;
5276 ListCell *slitem;
5277
5278 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
5279 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
5280
5281 foreach(slitem, distinctList)
5282 {
5283 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
5284 TargetEntry *tle = get_sortgroupclause_tle(sortcl,
5285 plan->targetlist);
5286
5287 dupColIdx[keyno] = tle->resno;
5288 dupOperators[keyno] = sortcl->eqop;
5289 Assert(OidIsValid(dupOperators[keyno]));
5290 keyno++;
5291 }
5292 node->dupColIdx = dupColIdx;
5293 node->dupOperators = dupOperators;
5294 }
5295 node->numGroups = numGroups;
5296
5297 return node;
5298 }
5299
5300 static BitmapAnd *
make_bitmap_and(List * bitmapplans)5301 make_bitmap_and(List *bitmapplans)
5302 {
5303 BitmapAnd *node = makeNode(BitmapAnd);
5304 Plan *plan = &node->plan;
5305
5306 plan->targetlist = NIL;
5307 plan->qual = NIL;
5308 plan->lefttree = NULL;
5309 plan->righttree = NULL;
5310 node->bitmapplans = bitmapplans;
5311
5312 return node;
5313 }
5314
5315 static BitmapOr *
make_bitmap_or(List * bitmapplans)5316 make_bitmap_or(List *bitmapplans)
5317 {
5318 BitmapOr *node = makeNode(BitmapOr);
5319 Plan *plan = &node->plan;
5320
5321 plan->targetlist = NIL;
5322 plan->qual = NIL;
5323 plan->lefttree = NULL;
5324 plan->righttree = NULL;
5325 node->bitmapplans = bitmapplans;
5326
5327 return node;
5328 }
5329
5330 static NestLoop *
make_nestloop(List * tlist,List * joinclauses,List * otherclauses,List * nestParams,Plan * lefttree,Plan * righttree,JoinType jointype,bool inner_unique)5331 make_nestloop(List *tlist,
5332 List *joinclauses,
5333 List *otherclauses,
5334 List *nestParams,
5335 Plan *lefttree,
5336 Plan *righttree,
5337 JoinType jointype,
5338 bool inner_unique)
5339 {
5340 NestLoop *node = makeNode(NestLoop);
5341 Plan *plan = &node->join.plan;
5342
5343 plan->targetlist = tlist;
5344 plan->qual = otherclauses;
5345 plan->lefttree = lefttree;
5346 plan->righttree = righttree;
5347 node->join.jointype = jointype;
5348 node->join.inner_unique = inner_unique;
5349 node->join.joinqual = joinclauses;
5350 node->nestParams = nestParams;
5351
5352 return node;
5353 }
5354
5355 static HashJoin *
make_hashjoin(List * tlist,List * joinclauses,List * otherclauses,List * hashclauses,Plan * lefttree,Plan * righttree,JoinType jointype,bool inner_unique)5356 make_hashjoin(List *tlist,
5357 List *joinclauses,
5358 List *otherclauses,
5359 List *hashclauses,
5360 Plan *lefttree,
5361 Plan *righttree,
5362 JoinType jointype,
5363 bool inner_unique)
5364 {
5365 HashJoin *node = makeNode(HashJoin);
5366 Plan *plan = &node->join.plan;
5367
5368 plan->targetlist = tlist;
5369 plan->qual = otherclauses;
5370 plan->lefttree = lefttree;
5371 plan->righttree = righttree;
5372 node->hashclauses = hashclauses;
5373 node->join.jointype = jointype;
5374 node->join.inner_unique = inner_unique;
5375 node->join.joinqual = joinclauses;
5376
5377 return node;
5378 }
5379
5380 static Hash *
make_hash(Plan * lefttree,Oid skewTable,AttrNumber skewColumn,bool skewInherit)5381 make_hash(Plan *lefttree,
5382 Oid skewTable,
5383 AttrNumber skewColumn,
5384 bool skewInherit)
5385 {
5386 Hash *node = makeNode(Hash);
5387 Plan *plan = &node->plan;
5388
5389 plan->targetlist = lefttree->targetlist;
5390 plan->qual = NIL;
5391 plan->lefttree = lefttree;
5392 plan->righttree = NULL;
5393
5394 node->skewTable = skewTable;
5395 node->skewColumn = skewColumn;
5396 node->skewInherit = skewInherit;
5397
5398 return node;
5399 }
5400
5401 static MergeJoin *
make_mergejoin(List * tlist,List * joinclauses,List * otherclauses,List * mergeclauses,Oid * mergefamilies,Oid * mergecollations,int * mergestrategies,bool * mergenullsfirst,Plan * lefttree,Plan * righttree,JoinType jointype,bool inner_unique,bool skip_mark_restore)5402 make_mergejoin(List *tlist,
5403 List *joinclauses,
5404 List *otherclauses,
5405 List *mergeclauses,
5406 Oid *mergefamilies,
5407 Oid *mergecollations,
5408 int *mergestrategies,
5409 bool *mergenullsfirst,
5410 Plan *lefttree,
5411 Plan *righttree,
5412 JoinType jointype,
5413 bool inner_unique,
5414 bool skip_mark_restore)
5415 {
5416 MergeJoin *node = makeNode(MergeJoin);
5417 Plan *plan = &node->join.plan;
5418
5419 plan->targetlist = tlist;
5420 plan->qual = otherclauses;
5421 plan->lefttree = lefttree;
5422 plan->righttree = righttree;
5423 node->skip_mark_restore = skip_mark_restore;
5424 node->mergeclauses = mergeclauses;
5425 node->mergeFamilies = mergefamilies;
5426 node->mergeCollations = mergecollations;
5427 node->mergeStrategies = mergestrategies;
5428 node->mergeNullsFirst = mergenullsfirst;
5429 node->join.jointype = jointype;
5430 node->join.inner_unique = inner_unique;
5431 node->join.joinqual = joinclauses;
5432
5433 return node;
5434 }
5435
5436 /*
5437 * make_sort --- basic routine to build a Sort plan node
5438 *
5439 * Caller must have built the sortColIdx, sortOperators, collations, and
5440 * nullsFirst arrays already.
5441 */
5442 static Sort *
make_sort(Plan * lefttree,int numCols,AttrNumber * sortColIdx,Oid * sortOperators,Oid * collations,bool * nullsFirst)5443 make_sort(Plan *lefttree, int numCols,
5444 AttrNumber *sortColIdx, Oid *sortOperators,
5445 Oid *collations, bool *nullsFirst)
5446 {
5447 Sort *node = makeNode(Sort);
5448 Plan *plan = &node->plan;
5449
5450 plan->targetlist = lefttree->targetlist;
5451 plan->qual = NIL;
5452 plan->lefttree = lefttree;
5453 plan->righttree = NULL;
5454 node->numCols = numCols;
5455 node->sortColIdx = sortColIdx;
5456 node->sortOperators = sortOperators;
5457 node->collations = collations;
5458 node->nullsFirst = nullsFirst;
5459
5460 return node;
5461 }
5462
5463 /*
5464 * prepare_sort_from_pathkeys
5465 * Prepare to sort according to given pathkeys
5466 *
5467 * This is used to set up for Sort, MergeAppend, and Gather Merge nodes. It
5468 * calculates the executor's representation of the sort key information, and
5469 * adjusts the plan targetlist if needed to add resjunk sort columns.
5470 *
5471 * Input parameters:
5472 * 'lefttree' is the plan node which yields input tuples
5473 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
5474 * 'relids' identifies the child relation being sorted, if any
5475 * 'reqColIdx' is NULL or an array of required sort key column numbers
5476 * 'adjust_tlist_in_place' is true if lefttree must be modified in-place
5477 *
5478 * We must convert the pathkey information into arrays of sort key column
5479 * numbers, sort operator OIDs, collation OIDs, and nulls-first flags,
5480 * which is the representation the executor wants. These are returned into
5481 * the output parameters *p_numsortkeys etc.
5482 *
5483 * When looking for matches to an EquivalenceClass's members, we will only
5484 * consider child EC members if they belong to given 'relids'. This protects
5485 * against possible incorrect matches to child expressions that contain no
5486 * Vars.
5487 *
5488 * If reqColIdx isn't NULL then it contains sort key column numbers that
5489 * we should match. This is used when making child plans for a MergeAppend;
5490 * it's an error if we can't match the columns.
5491 *
5492 * If the pathkeys include expressions that aren't simple Vars, we will
5493 * usually need to add resjunk items to the input plan's targetlist to
5494 * compute these expressions, since a Sort or MergeAppend node itself won't
5495 * do any such calculations. If the input plan type isn't one that can do
5496 * projections, this means adding a Result node just to do the projection.
5497 * However, the caller can pass adjust_tlist_in_place = true to force the
5498 * lefttree tlist to be modified in-place regardless of whether the node type
5499 * can project --- we use this for fixing the tlist of MergeAppend itself.
5500 *
5501 * Returns the node which is to be the input to the Sort (either lefttree,
5502 * or a Result stacked atop lefttree).
5503 */
5504 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)5505 prepare_sort_from_pathkeys(Plan *lefttree, List *pathkeys,
5506 Relids relids,
5507 const AttrNumber *reqColIdx,
5508 bool adjust_tlist_in_place,
5509 int *p_numsortkeys,
5510 AttrNumber **p_sortColIdx,
5511 Oid **p_sortOperators,
5512 Oid **p_collations,
5513 bool **p_nullsFirst)
5514 {
5515 List *tlist = lefttree->targetlist;
5516 ListCell *i;
5517 int numsortkeys;
5518 AttrNumber *sortColIdx;
5519 Oid *sortOperators;
5520 Oid *collations;
5521 bool *nullsFirst;
5522
5523 /*
5524 * We will need at most list_length(pathkeys) sort columns; possibly less
5525 */
5526 numsortkeys = list_length(pathkeys);
5527 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
5528 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
5529 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
5530 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
5531
5532 numsortkeys = 0;
5533
5534 foreach(i, pathkeys)
5535 {
5536 PathKey *pathkey = (PathKey *) lfirst(i);
5537 EquivalenceClass *ec = pathkey->pk_eclass;
5538 EquivalenceMember *em;
5539 TargetEntry *tle = NULL;
5540 Oid pk_datatype = InvalidOid;
5541 Oid sortop;
5542 ListCell *j;
5543
5544 if (ec->ec_has_volatile)
5545 {
5546 /*
5547 * If the pathkey's EquivalenceClass is volatile, then it must
5548 * have come from an ORDER BY clause, and we have to match it to
5549 * that same targetlist entry.
5550 */
5551 if (ec->ec_sortref == 0) /* can't happen */
5552 elog(ERROR, "volatile EquivalenceClass has no sortref");
5553 tle = get_sortgroupref_tle(ec->ec_sortref, tlist);
5554 Assert(tle);
5555 Assert(list_length(ec->ec_members) == 1);
5556 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
5557 }
5558 else if (reqColIdx != NULL)
5559 {
5560 /*
5561 * If we are given a sort column number to match, only consider
5562 * the single TLE at that position. It's possible that there is
5563 * no such TLE, in which case fall through and generate a resjunk
5564 * targetentry (we assume this must have happened in the parent
5565 * plan as well). If there is a TLE but it doesn't match the
5566 * pathkey's EC, we do the same, which is probably the wrong thing
5567 * but we'll leave it to caller to complain about the mismatch.
5568 */
5569 tle = get_tle_by_resno(tlist, reqColIdx[numsortkeys]);
5570 if (tle)
5571 {
5572 em = find_ec_member_for_tle(ec, tle, relids);
5573 if (em)
5574 {
5575 /* found expr at right place in tlist */
5576 pk_datatype = em->em_datatype;
5577 }
5578 else
5579 tle = NULL;
5580 }
5581 }
5582 else
5583 {
5584 /*
5585 * Otherwise, we can sort by any non-constant expression listed in
5586 * the pathkey's EquivalenceClass. For now, we take the first
5587 * tlist item found in the EC. If there's no match, we'll generate
5588 * a resjunk entry using the first EC member that is an expression
5589 * in the input's vars. (The non-const restriction only matters
5590 * if the EC is below_outer_join; but if it isn't, it won't
5591 * contain consts anyway, else we'd have discarded the pathkey as
5592 * redundant.)
5593 *
5594 * XXX if we have a choice, is there any way of figuring out which
5595 * might be cheapest to execute? (For example, int4lt is likely
5596 * much cheaper to execute than numericlt, but both might appear
5597 * in the same equivalence class...) Not clear that we ever will
5598 * have an interesting choice in practice, so it may not matter.
5599 */
5600 foreach(j, tlist)
5601 {
5602 tle = (TargetEntry *) lfirst(j);
5603 em = find_ec_member_for_tle(ec, tle, relids);
5604 if (em)
5605 {
5606 /* found expr already in tlist */
5607 pk_datatype = em->em_datatype;
5608 break;
5609 }
5610 tle = NULL;
5611 }
5612 }
5613
5614 if (!tle)
5615 {
5616 /*
5617 * No matching tlist item; look for a computable expression. Note
5618 * that we treat Aggrefs as if they were variables; this is
5619 * necessary when attempting to sort the output from an Agg node
5620 * for use in a WindowFunc (since grouping_planner will have
5621 * treated the Aggrefs as variables, too). Likewise, if we find a
5622 * WindowFunc in a sort expression, treat it as a variable.
5623 */
5624 Expr *sortexpr = NULL;
5625
5626 foreach(j, ec->ec_members)
5627 {
5628 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
5629 List *exprvars;
5630 ListCell *k;
5631
5632 /*
5633 * We shouldn't be trying to sort by an equivalence class that
5634 * contains a constant, so no need to consider such cases any
5635 * further.
5636 */
5637 if (em->em_is_const)
5638 continue;
5639
5640 /*
5641 * Ignore child members unless they belong to the rel being
5642 * sorted.
5643 */
5644 if (em->em_is_child &&
5645 !bms_is_subset(em->em_relids, relids))
5646 continue;
5647
5648 sortexpr = em->em_expr;
5649 exprvars = pull_var_clause((Node *) sortexpr,
5650 PVC_INCLUDE_AGGREGATES |
5651 PVC_INCLUDE_WINDOWFUNCS |
5652 PVC_INCLUDE_PLACEHOLDERS);
5653 foreach(k, exprvars)
5654 {
5655 if (!tlist_member_ignore_relabel(lfirst(k), tlist))
5656 break;
5657 }
5658 list_free(exprvars);
5659 if (!k)
5660 {
5661 pk_datatype = em->em_datatype;
5662 break; /* found usable expression */
5663 }
5664 }
5665 if (!j)
5666 elog(ERROR, "could not find pathkey item to sort");
5667
5668 /*
5669 * Do we need to insert a Result node?
5670 */
5671 if (!adjust_tlist_in_place &&
5672 !is_projection_capable_plan(lefttree))
5673 {
5674 /* copy needed so we don't modify input's tlist below */
5675 tlist = copyObject(tlist);
5676 lefttree = inject_projection_plan(lefttree, tlist,
5677 lefttree->parallel_safe);
5678 }
5679
5680 /* Don't bother testing is_projection_capable_plan again */
5681 adjust_tlist_in_place = true;
5682
5683 /*
5684 * Add resjunk entry to input's tlist
5685 */
5686 tle = makeTargetEntry(sortexpr,
5687 list_length(tlist) + 1,
5688 NULL,
5689 true);
5690 tlist = lappend(tlist, tle);
5691 lefttree->targetlist = tlist; /* just in case NIL before */
5692 }
5693
5694 /*
5695 * Look up the correct sort operator from the PathKey's slightly
5696 * abstracted representation.
5697 */
5698 sortop = get_opfamily_member(pathkey->pk_opfamily,
5699 pk_datatype,
5700 pk_datatype,
5701 pathkey->pk_strategy);
5702 if (!OidIsValid(sortop)) /* should not happen */
5703 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
5704 pathkey->pk_strategy, pk_datatype, pk_datatype,
5705 pathkey->pk_opfamily);
5706
5707 /* Add the column to the sort arrays */
5708 sortColIdx[numsortkeys] = tle->resno;
5709 sortOperators[numsortkeys] = sortop;
5710 collations[numsortkeys] = ec->ec_collation;
5711 nullsFirst[numsortkeys] = pathkey->pk_nulls_first;
5712 numsortkeys++;
5713 }
5714
5715 /* Return results */
5716 *p_numsortkeys = numsortkeys;
5717 *p_sortColIdx = sortColIdx;
5718 *p_sortOperators = sortOperators;
5719 *p_collations = collations;
5720 *p_nullsFirst = nullsFirst;
5721
5722 return lefttree;
5723 }
5724
5725 /*
5726 * find_ec_member_for_tle
5727 * Locate an EquivalenceClass member matching the given TLE, if any
5728 *
5729 * Child EC members are ignored unless they belong to given 'relids'.
5730 */
5731 static EquivalenceMember *
find_ec_member_for_tle(EquivalenceClass * ec,TargetEntry * tle,Relids relids)5732 find_ec_member_for_tle(EquivalenceClass *ec,
5733 TargetEntry *tle,
5734 Relids relids)
5735 {
5736 Expr *tlexpr;
5737 ListCell *lc;
5738
5739 /* We ignore binary-compatible relabeling on both ends */
5740 tlexpr = tle->expr;
5741 while (tlexpr && IsA(tlexpr, RelabelType))
5742 tlexpr = ((RelabelType *) tlexpr)->arg;
5743
5744 foreach(lc, ec->ec_members)
5745 {
5746 EquivalenceMember *em = (EquivalenceMember *) lfirst(lc);
5747 Expr *emexpr;
5748
5749 /*
5750 * We shouldn't be trying to sort by an equivalence class that
5751 * contains a constant, so no need to consider such cases any further.
5752 */
5753 if (em->em_is_const)
5754 continue;
5755
5756 /*
5757 * Ignore child members unless they belong to the rel being sorted.
5758 */
5759 if (em->em_is_child &&
5760 !bms_is_subset(em->em_relids, relids))
5761 continue;
5762
5763 /* Match if same expression (after stripping relabel) */
5764 emexpr = em->em_expr;
5765 while (emexpr && IsA(emexpr, RelabelType))
5766 emexpr = ((RelabelType *) emexpr)->arg;
5767
5768 if (equal(emexpr, tlexpr))
5769 return em;
5770 }
5771
5772 return NULL;
5773 }
5774
5775 /*
5776 * make_sort_from_pathkeys
5777 * Create sort plan to sort according to given pathkeys
5778 *
5779 * 'lefttree' is the node which yields input tuples
5780 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
5781 * 'relids' is the set of relations required by prepare_sort_from_pathkeys()
5782 */
5783 static Sort *
make_sort_from_pathkeys(Plan * lefttree,List * pathkeys,Relids relids)5784 make_sort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids)
5785 {
5786 int numsortkeys;
5787 AttrNumber *sortColIdx;
5788 Oid *sortOperators;
5789 Oid *collations;
5790 bool *nullsFirst;
5791
5792 /* Compute sort column info, and adjust lefttree as needed */
5793 lefttree = prepare_sort_from_pathkeys(lefttree, pathkeys,
5794 relids,
5795 NULL,
5796 false,
5797 &numsortkeys,
5798 &sortColIdx,
5799 &sortOperators,
5800 &collations,
5801 &nullsFirst);
5802
5803 /* Now build the Sort node */
5804 return make_sort(lefttree, numsortkeys,
5805 sortColIdx, sortOperators,
5806 collations, nullsFirst);
5807 }
5808
5809 /*
5810 * make_sort_from_sortclauses
5811 * Create sort plan to sort according to given sortclauses
5812 *
5813 * 'sortcls' is a list of SortGroupClauses
5814 * 'lefttree' is the node which yields input tuples
5815 */
5816 Sort *
make_sort_from_sortclauses(List * sortcls,Plan * lefttree)5817 make_sort_from_sortclauses(List *sortcls, Plan *lefttree)
5818 {
5819 List *sub_tlist = lefttree->targetlist;
5820 ListCell *l;
5821 int numsortkeys;
5822 AttrNumber *sortColIdx;
5823 Oid *sortOperators;
5824 Oid *collations;
5825 bool *nullsFirst;
5826
5827 /* Convert list-ish representation to arrays wanted by executor */
5828 numsortkeys = list_length(sortcls);
5829 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
5830 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
5831 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
5832 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
5833
5834 numsortkeys = 0;
5835 foreach(l, sortcls)
5836 {
5837 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
5838 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
5839
5840 sortColIdx[numsortkeys] = tle->resno;
5841 sortOperators[numsortkeys] = sortcl->sortop;
5842 collations[numsortkeys] = exprCollation((Node *) tle->expr);
5843 nullsFirst[numsortkeys] = sortcl->nulls_first;
5844 numsortkeys++;
5845 }
5846
5847 return make_sort(lefttree, numsortkeys,
5848 sortColIdx, sortOperators,
5849 collations, nullsFirst);
5850 }
5851
5852 /*
5853 * make_sort_from_groupcols
5854 * Create sort plan to sort based on grouping columns
5855 *
5856 * 'groupcls' is the list of SortGroupClauses
5857 * 'grpColIdx' gives the column numbers to use
5858 *
5859 * This might look like it could be merged with make_sort_from_sortclauses,
5860 * but presently we *must* use the grpColIdx[] array to locate sort columns,
5861 * because the child plan's tlist is not marked with ressortgroupref info
5862 * appropriate to the grouping node. So, only the sort ordering info
5863 * is used from the SortGroupClause entries.
5864 */
5865 static Sort *
make_sort_from_groupcols(List * groupcls,AttrNumber * grpColIdx,Plan * lefttree)5866 make_sort_from_groupcols(List *groupcls,
5867 AttrNumber *grpColIdx,
5868 Plan *lefttree)
5869 {
5870 List *sub_tlist = lefttree->targetlist;
5871 ListCell *l;
5872 int numsortkeys;
5873 AttrNumber *sortColIdx;
5874 Oid *sortOperators;
5875 Oid *collations;
5876 bool *nullsFirst;
5877
5878 /* Convert list-ish representation to arrays wanted by executor */
5879 numsortkeys = list_length(groupcls);
5880 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
5881 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
5882 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
5883 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
5884
5885 numsortkeys = 0;
5886 foreach(l, groupcls)
5887 {
5888 SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
5889 TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[numsortkeys]);
5890
5891 if (!tle)
5892 elog(ERROR, "could not retrieve tle for sort-from-groupcols");
5893
5894 sortColIdx[numsortkeys] = tle->resno;
5895 sortOperators[numsortkeys] = grpcl->sortop;
5896 collations[numsortkeys] = exprCollation((Node *) tle->expr);
5897 nullsFirst[numsortkeys] = grpcl->nulls_first;
5898 numsortkeys++;
5899 }
5900
5901 return make_sort(lefttree, numsortkeys,
5902 sortColIdx, sortOperators,
5903 collations, nullsFirst);
5904 }
5905
5906 static Material *
make_material(Plan * lefttree)5907 make_material(Plan *lefttree)
5908 {
5909 Material *node = makeNode(Material);
5910 Plan *plan = &node->plan;
5911
5912 plan->targetlist = lefttree->targetlist;
5913 plan->qual = NIL;
5914 plan->lefttree = lefttree;
5915 plan->righttree = NULL;
5916
5917 return node;
5918 }
5919
5920 /*
5921 * materialize_finished_plan: stick a Material node atop a completed plan
5922 *
5923 * There are a couple of places where we want to attach a Material node
5924 * after completion of create_plan(), without any MaterialPath path.
5925 * Those places should probably be refactored someday to do this on the
5926 * Path representation, but it's not worth the trouble yet.
5927 */
5928 Plan *
materialize_finished_plan(Plan * subplan)5929 materialize_finished_plan(Plan *subplan)
5930 {
5931 Plan *matplan;
5932 Path matpath; /* dummy for result of cost_material */
5933
5934 matplan = (Plan *) make_material(subplan);
5935
5936 /*
5937 * XXX horrid kluge: if there are any initPlans attached to the subplan,
5938 * move them up to the Material node, which is now effectively the top
5939 * plan node in its query level. This prevents failure in
5940 * SS_finalize_plan(), which see for comments. We don't bother adjusting
5941 * the subplan's cost estimate for this.
5942 */
5943 matplan->initPlan = subplan->initPlan;
5944 subplan->initPlan = NIL;
5945
5946 /* Set cost data */
5947 cost_material(&matpath,
5948 subplan->startup_cost,
5949 subplan->total_cost,
5950 subplan->plan_rows,
5951 subplan->plan_width);
5952 matplan->startup_cost = matpath.startup_cost;
5953 matplan->total_cost = matpath.total_cost;
5954 matplan->plan_rows = subplan->plan_rows;
5955 matplan->plan_width = subplan->plan_width;
5956 matplan->parallel_aware = false;
5957 matplan->parallel_safe = subplan->parallel_safe;
5958
5959 return matplan;
5960 }
5961
5962 Agg *
make_agg(List * tlist,List * qual,AggStrategy aggstrategy,AggSplit aggsplit,int numGroupCols,AttrNumber * grpColIdx,Oid * grpOperators,List * groupingSets,List * chain,double dNumGroups,Plan * lefttree)5963 make_agg(List *tlist, List *qual,
5964 AggStrategy aggstrategy, AggSplit aggsplit,
5965 int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators,
5966 List *groupingSets, List *chain,
5967 double dNumGroups, Plan *lefttree)
5968 {
5969 Agg *node = makeNode(Agg);
5970 Plan *plan = &node->plan;
5971 long numGroups;
5972
5973 /* Reduce to long, but 'ware overflow! */
5974 numGroups = (long) Min(dNumGroups, (double) LONG_MAX);
5975
5976 node->aggstrategy = aggstrategy;
5977 node->aggsplit = aggsplit;
5978 node->numCols = numGroupCols;
5979 node->grpColIdx = grpColIdx;
5980 node->grpOperators = grpOperators;
5981 node->numGroups = numGroups;
5982 node->aggParams = NULL; /* SS_finalize_plan() will fill this */
5983 node->groupingSets = groupingSets;
5984 node->chain = chain;
5985
5986 plan->qual = qual;
5987 plan->targetlist = tlist;
5988 plan->lefttree = lefttree;
5989 plan->righttree = NULL;
5990
5991 return node;
5992 }
5993
5994 static WindowAgg *
make_windowagg(List * tlist,Index winref,int partNumCols,AttrNumber * partColIdx,Oid * partOperators,int ordNumCols,AttrNumber * ordColIdx,Oid * ordOperators,int frameOptions,Node * startOffset,Node * endOffset,Oid startInRangeFunc,Oid endInRangeFunc,Oid inRangeColl,bool inRangeAsc,bool inRangeNullsFirst,Plan * lefttree)5995 make_windowagg(List *tlist, Index winref,
5996 int partNumCols, AttrNumber *partColIdx, Oid *partOperators,
5997 int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators,
5998 int frameOptions, Node *startOffset, Node *endOffset,
5999 Oid startInRangeFunc, Oid endInRangeFunc,
6000 Oid inRangeColl, bool inRangeAsc, bool inRangeNullsFirst,
6001 Plan *lefttree)
6002 {
6003 WindowAgg *node = makeNode(WindowAgg);
6004 Plan *plan = &node->plan;
6005
6006 node->winref = winref;
6007 node->partNumCols = partNumCols;
6008 node->partColIdx = partColIdx;
6009 node->partOperators = partOperators;
6010 node->ordNumCols = ordNumCols;
6011 node->ordColIdx = ordColIdx;
6012 node->ordOperators = ordOperators;
6013 node->frameOptions = frameOptions;
6014 node->startOffset = startOffset;
6015 node->endOffset = endOffset;
6016 node->startInRangeFunc = startInRangeFunc;
6017 node->endInRangeFunc = endInRangeFunc;
6018 node->inRangeColl = inRangeColl;
6019 node->inRangeAsc = inRangeAsc;
6020 node->inRangeNullsFirst = inRangeNullsFirst;
6021
6022 plan->targetlist = tlist;
6023 plan->lefttree = lefttree;
6024 plan->righttree = NULL;
6025 /* WindowAgg nodes never have a qual clause */
6026 plan->qual = NIL;
6027
6028 return node;
6029 }
6030
6031 static Group *
make_group(List * tlist,List * qual,int numGroupCols,AttrNumber * grpColIdx,Oid * grpOperators,Plan * lefttree)6032 make_group(List *tlist,
6033 List *qual,
6034 int numGroupCols,
6035 AttrNumber *grpColIdx,
6036 Oid *grpOperators,
6037 Plan *lefttree)
6038 {
6039 Group *node = makeNode(Group);
6040 Plan *plan = &node->plan;
6041
6042 node->numCols = numGroupCols;
6043 node->grpColIdx = grpColIdx;
6044 node->grpOperators = grpOperators;
6045
6046 plan->qual = qual;
6047 plan->targetlist = tlist;
6048 plan->lefttree = lefttree;
6049 plan->righttree = NULL;
6050
6051 return node;
6052 }
6053
6054 /*
6055 * distinctList is a list of SortGroupClauses, identifying the targetlist items
6056 * that should be considered by the Unique filter. The input path must
6057 * already be sorted accordingly.
6058 */
6059 static Unique *
make_unique_from_sortclauses(Plan * lefttree,List * distinctList)6060 make_unique_from_sortclauses(Plan *lefttree, List *distinctList)
6061 {
6062 Unique *node = makeNode(Unique);
6063 Plan *plan = &node->plan;
6064 int numCols = list_length(distinctList);
6065 int keyno = 0;
6066 AttrNumber *uniqColIdx;
6067 Oid *uniqOperators;
6068 ListCell *slitem;
6069
6070 plan->targetlist = lefttree->targetlist;
6071 plan->qual = NIL;
6072 plan->lefttree = lefttree;
6073 plan->righttree = NULL;
6074
6075 /*
6076 * convert SortGroupClause list into arrays of attr indexes and equality
6077 * operators, as wanted by executor
6078 */
6079 Assert(numCols > 0);
6080 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
6081 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
6082
6083 foreach(slitem, distinctList)
6084 {
6085 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
6086 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
6087
6088 uniqColIdx[keyno] = tle->resno;
6089 uniqOperators[keyno] = sortcl->eqop;
6090 Assert(OidIsValid(uniqOperators[keyno]));
6091 keyno++;
6092 }
6093
6094 node->numCols = numCols;
6095 node->uniqColIdx = uniqColIdx;
6096 node->uniqOperators = uniqOperators;
6097
6098 return node;
6099 }
6100
6101 /*
6102 * as above, but use pathkeys to identify the sort columns and semantics
6103 */
6104 static Unique *
make_unique_from_pathkeys(Plan * lefttree,List * pathkeys,int numCols)6105 make_unique_from_pathkeys(Plan *lefttree, List *pathkeys, int numCols)
6106 {
6107 Unique *node = makeNode(Unique);
6108 Plan *plan = &node->plan;
6109 int keyno = 0;
6110 AttrNumber *uniqColIdx;
6111 Oid *uniqOperators;
6112 ListCell *lc;
6113
6114 plan->targetlist = lefttree->targetlist;
6115 plan->qual = NIL;
6116 plan->lefttree = lefttree;
6117 plan->righttree = NULL;
6118
6119 /*
6120 * Convert pathkeys list into arrays of attr indexes and equality
6121 * operators, as wanted by executor. This has a lot in common with
6122 * prepare_sort_from_pathkeys ... maybe unify sometime?
6123 */
6124 Assert(numCols >= 0 && numCols <= list_length(pathkeys));
6125 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
6126 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
6127
6128 foreach(lc, pathkeys)
6129 {
6130 PathKey *pathkey = (PathKey *) lfirst(lc);
6131 EquivalenceClass *ec = pathkey->pk_eclass;
6132 EquivalenceMember *em;
6133 TargetEntry *tle = NULL;
6134 Oid pk_datatype = InvalidOid;
6135 Oid eqop;
6136 ListCell *j;
6137
6138 /* Ignore pathkeys beyond the specified number of columns */
6139 if (keyno >= numCols)
6140 break;
6141
6142 if (ec->ec_has_volatile)
6143 {
6144 /*
6145 * If the pathkey's EquivalenceClass is volatile, then it must
6146 * have come from an ORDER BY clause, and we have to match it to
6147 * that same targetlist entry.
6148 */
6149 if (ec->ec_sortref == 0) /* can't happen */
6150 elog(ERROR, "volatile EquivalenceClass has no sortref");
6151 tle = get_sortgroupref_tle(ec->ec_sortref, plan->targetlist);
6152 Assert(tle);
6153 Assert(list_length(ec->ec_members) == 1);
6154 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
6155 }
6156 else
6157 {
6158 /*
6159 * Otherwise, we can use any non-constant expression listed in the
6160 * pathkey's EquivalenceClass. For now, we take the first tlist
6161 * item found in the EC.
6162 */
6163 foreach(j, plan->targetlist)
6164 {
6165 tle = (TargetEntry *) lfirst(j);
6166 em = find_ec_member_for_tle(ec, tle, NULL);
6167 if (em)
6168 {
6169 /* found expr already in tlist */
6170 pk_datatype = em->em_datatype;
6171 break;
6172 }
6173 tle = NULL;
6174 }
6175 }
6176
6177 if (!tle)
6178 elog(ERROR, "could not find pathkey item to sort");
6179
6180 /*
6181 * Look up the correct equality operator from the PathKey's slightly
6182 * abstracted representation.
6183 */
6184 eqop = get_opfamily_member(pathkey->pk_opfamily,
6185 pk_datatype,
6186 pk_datatype,
6187 BTEqualStrategyNumber);
6188 if (!OidIsValid(eqop)) /* should not happen */
6189 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
6190 BTEqualStrategyNumber, pk_datatype, pk_datatype,
6191 pathkey->pk_opfamily);
6192
6193 uniqColIdx[keyno] = tle->resno;
6194 uniqOperators[keyno] = eqop;
6195
6196 keyno++;
6197 }
6198
6199 node->numCols = numCols;
6200 node->uniqColIdx = uniqColIdx;
6201 node->uniqOperators = uniqOperators;
6202
6203 return node;
6204 }
6205
6206 static Gather *
make_gather(List * qptlist,List * qpqual,int nworkers,int rescan_param,bool single_copy,Plan * subplan)6207 make_gather(List *qptlist,
6208 List *qpqual,
6209 int nworkers,
6210 int rescan_param,
6211 bool single_copy,
6212 Plan *subplan)
6213 {
6214 Gather *node = makeNode(Gather);
6215 Plan *plan = &node->plan;
6216
6217 plan->targetlist = qptlist;
6218 plan->qual = qpqual;
6219 plan->lefttree = subplan;
6220 plan->righttree = NULL;
6221 node->num_workers = nworkers;
6222 node->rescan_param = rescan_param;
6223 node->single_copy = single_copy;
6224 node->invisible = false;
6225 node->initParam = NULL;
6226
6227 return node;
6228 }
6229
6230 /*
6231 * distinctList is a list of SortGroupClauses, identifying the targetlist
6232 * items that should be considered by the SetOp filter. The input path must
6233 * already be sorted accordingly.
6234 */
6235 static SetOp *
make_setop(SetOpCmd cmd,SetOpStrategy strategy,Plan * lefttree,List * distinctList,AttrNumber flagColIdx,int firstFlag,long numGroups)6236 make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
6237 List *distinctList, AttrNumber flagColIdx, int firstFlag,
6238 long numGroups)
6239 {
6240 SetOp *node = makeNode(SetOp);
6241 Plan *plan = &node->plan;
6242 int numCols = list_length(distinctList);
6243 int keyno = 0;
6244 AttrNumber *dupColIdx;
6245 Oid *dupOperators;
6246 ListCell *slitem;
6247
6248 plan->targetlist = lefttree->targetlist;
6249 plan->qual = NIL;
6250 plan->lefttree = lefttree;
6251 plan->righttree = NULL;
6252
6253 /*
6254 * convert SortGroupClause list into arrays of attr indexes and equality
6255 * operators, as wanted by executor
6256 */
6257 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
6258 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
6259
6260 foreach(slitem, distinctList)
6261 {
6262 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
6263 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
6264
6265 dupColIdx[keyno] = tle->resno;
6266 dupOperators[keyno] = sortcl->eqop;
6267 Assert(OidIsValid(dupOperators[keyno]));
6268 keyno++;
6269 }
6270
6271 node->cmd = cmd;
6272 node->strategy = strategy;
6273 node->numCols = numCols;
6274 node->dupColIdx = dupColIdx;
6275 node->dupOperators = dupOperators;
6276 node->flagColIdx = flagColIdx;
6277 node->firstFlag = firstFlag;
6278 node->numGroups = numGroups;
6279
6280 return node;
6281 }
6282
6283 /*
6284 * make_lockrows
6285 * Build a LockRows plan node
6286 */
6287 static LockRows *
make_lockrows(Plan * lefttree,List * rowMarks,int epqParam)6288 make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
6289 {
6290 LockRows *node = makeNode(LockRows);
6291 Plan *plan = &node->plan;
6292
6293 plan->targetlist = lefttree->targetlist;
6294 plan->qual = NIL;
6295 plan->lefttree = lefttree;
6296 plan->righttree = NULL;
6297
6298 node->rowMarks = rowMarks;
6299 node->epqParam = epqParam;
6300
6301 return node;
6302 }
6303
6304 /*
6305 * make_limit
6306 * Build a Limit plan node
6307 */
6308 Limit *
make_limit(Plan * lefttree,Node * limitOffset,Node * limitCount)6309 make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
6310 {
6311 Limit *node = makeNode(Limit);
6312 Plan *plan = &node->plan;
6313
6314 plan->targetlist = lefttree->targetlist;
6315 plan->qual = NIL;
6316 plan->lefttree = lefttree;
6317 plan->righttree = NULL;
6318
6319 node->limitOffset = limitOffset;
6320 node->limitCount = limitCount;
6321
6322 return node;
6323 }
6324
6325 /*
6326 * make_result
6327 * Build a Result plan node
6328 */
6329 static Result *
make_result(List * tlist,Node * resconstantqual,Plan * subplan)6330 make_result(List *tlist,
6331 Node *resconstantqual,
6332 Plan *subplan)
6333 {
6334 Result *node = makeNode(Result);
6335 Plan *plan = &node->plan;
6336
6337 plan->targetlist = tlist;
6338 plan->qual = NIL;
6339 plan->lefttree = subplan;
6340 plan->righttree = NULL;
6341 node->resconstantqual = resconstantqual;
6342
6343 return node;
6344 }
6345
6346 /*
6347 * make_project_set
6348 * Build a ProjectSet plan node
6349 */
6350 static ProjectSet *
make_project_set(List * tlist,Plan * subplan)6351 make_project_set(List *tlist,
6352 Plan *subplan)
6353 {
6354 ProjectSet *node = makeNode(ProjectSet);
6355 Plan *plan = &node->plan;
6356
6357 plan->targetlist = tlist;
6358 plan->qual = NIL;
6359 plan->lefttree = subplan;
6360 plan->righttree = NULL;
6361
6362 return node;
6363 }
6364
6365 /*
6366 * make_modifytable
6367 * Build a ModifyTable plan node
6368 */
6369 static ModifyTable *
make_modifytable(PlannerInfo * root,CmdType operation,bool canSetTag,Index nominalRelation,List * partitioned_rels,bool partColsUpdated,List * resultRelations,List * subplans,List * subroots,List * withCheckOptionLists,List * returningLists,List * rowMarks,OnConflictExpr * onconflict,int epqParam)6370 make_modifytable(PlannerInfo *root,
6371 CmdType operation, bool canSetTag,
6372 Index nominalRelation, List *partitioned_rels,
6373 bool partColsUpdated,
6374 List *resultRelations, List *subplans, List *subroots,
6375 List *withCheckOptionLists, List *returningLists,
6376 List *rowMarks, OnConflictExpr *onconflict, int epqParam)
6377 {
6378 ModifyTable *node = makeNode(ModifyTable);
6379 List *fdw_private_list;
6380 Bitmapset *direct_modify_plans;
6381 ListCell *lc;
6382 ListCell *lc2;
6383 int i;
6384
6385 Assert(list_length(resultRelations) == list_length(subplans));
6386 Assert(list_length(resultRelations) == list_length(subroots));
6387 Assert(withCheckOptionLists == NIL ||
6388 list_length(resultRelations) == list_length(withCheckOptionLists));
6389 Assert(returningLists == NIL ||
6390 list_length(resultRelations) == list_length(returningLists));
6391
6392 node->plan.lefttree = NULL;
6393 node->plan.righttree = NULL;
6394 node->plan.qual = NIL;
6395 /* setrefs.c will fill in the targetlist, if needed */
6396 node->plan.targetlist = NIL;
6397
6398 node->operation = operation;
6399 node->canSetTag = canSetTag;
6400 node->nominalRelation = nominalRelation;
6401 node->partitioned_rels = flatten_partitioned_rels(partitioned_rels);
6402 node->partColsUpdated = partColsUpdated;
6403 node->resultRelations = resultRelations;
6404 node->resultRelIndex = -1; /* will be set correctly in setrefs.c */
6405 node->rootResultRelIndex = -1; /* will be set correctly in setrefs.c */
6406 node->plans = subplans;
6407 if (!onconflict)
6408 {
6409 node->onConflictAction = ONCONFLICT_NONE;
6410 node->onConflictSet = NIL;
6411 node->onConflictWhere = NULL;
6412 node->arbiterIndexes = NIL;
6413 node->exclRelRTI = 0;
6414 node->exclRelTlist = NIL;
6415 }
6416 else
6417 {
6418 node->onConflictAction = onconflict->action;
6419 node->onConflictSet = onconflict->onConflictSet;
6420 node->onConflictWhere = onconflict->onConflictWhere;
6421
6422 /*
6423 * If a set of unique index inference elements was provided (an
6424 * INSERT...ON CONFLICT "inference specification"), then infer
6425 * appropriate unique indexes (or throw an error if none are
6426 * available).
6427 */
6428 node->arbiterIndexes = infer_arbiter_indexes(root);
6429
6430 node->exclRelRTI = onconflict->exclRelIndex;
6431 node->exclRelTlist = onconflict->exclRelTlist;
6432 }
6433 node->withCheckOptionLists = withCheckOptionLists;
6434 node->returningLists = returningLists;
6435 node->rowMarks = rowMarks;
6436 node->epqParam = epqParam;
6437
6438 /*
6439 * For each result relation that is a foreign table, allow the FDW to
6440 * construct private plan data, and accumulate it all into a list.
6441 */
6442 fdw_private_list = NIL;
6443 direct_modify_plans = NULL;
6444 i = 0;
6445 forboth(lc, resultRelations, lc2, subroots)
6446 {
6447 Index rti = lfirst_int(lc);
6448 PlannerInfo *subroot = lfirst_node(PlannerInfo, lc2);
6449 FdwRoutine *fdwroutine;
6450 List *fdw_private;
6451 bool direct_modify;
6452
6453 /*
6454 * If possible, we want to get the FdwRoutine from our RelOptInfo for
6455 * the table. But sometimes we don't have a RelOptInfo and must get
6456 * it the hard way. (In INSERT, the target relation is not scanned,
6457 * so it's not a baserel; and there are also corner cases for
6458 * updatable views where the target rel isn't a baserel.)
6459 */
6460 if (rti < subroot->simple_rel_array_size &&
6461 subroot->simple_rel_array[rti] != NULL)
6462 {
6463 RelOptInfo *resultRel = subroot->simple_rel_array[rti];
6464
6465 fdwroutine = resultRel->fdwroutine;
6466 }
6467 else
6468 {
6469 RangeTblEntry *rte = planner_rt_fetch(rti, subroot);
6470
6471 Assert(rte->rtekind == RTE_RELATION);
6472 if (rte->relkind == RELKIND_FOREIGN_TABLE)
6473 fdwroutine = GetFdwRoutineByRelId(rte->relid);
6474 else
6475 fdwroutine = NULL;
6476 }
6477
6478 /*
6479 * Try to modify the foreign table directly if (1) the FDW provides
6480 * callback functions needed for that, (2) there are no row-level
6481 * triggers on the foreign table, and (3) there are no WITH CHECK
6482 * OPTIONs from parent views.
6483 */
6484 direct_modify = false;
6485 if (fdwroutine != NULL &&
6486 fdwroutine->PlanDirectModify != NULL &&
6487 fdwroutine->BeginDirectModify != NULL &&
6488 fdwroutine->IterateDirectModify != NULL &&
6489 fdwroutine->EndDirectModify != NULL &&
6490 withCheckOptionLists == NIL &&
6491 !has_row_triggers(subroot, rti, operation))
6492 direct_modify = fdwroutine->PlanDirectModify(subroot, node, rti, i);
6493 if (direct_modify)
6494 direct_modify_plans = bms_add_member(direct_modify_plans, i);
6495
6496 if (!direct_modify &&
6497 fdwroutine != NULL &&
6498 fdwroutine->PlanForeignModify != NULL)
6499 fdw_private = fdwroutine->PlanForeignModify(subroot, node, rti, i);
6500 else
6501 fdw_private = NIL;
6502 fdw_private_list = lappend(fdw_private_list, fdw_private);
6503 i++;
6504 }
6505 node->fdwPrivLists = fdw_private_list;
6506 node->fdwDirectModifyPlans = direct_modify_plans;
6507
6508 return node;
6509 }
6510
6511 /*
6512 * is_projection_capable_path
6513 * Check whether a given Path node is able to do projection.
6514 */
6515 bool
is_projection_capable_path(Path * path)6516 is_projection_capable_path(Path *path)
6517 {
6518 /* Most plan types can project, so just list the ones that can't */
6519 switch (path->pathtype)
6520 {
6521 case T_Hash:
6522 case T_Material:
6523 case T_Sort:
6524 case T_Unique:
6525 case T_SetOp:
6526 case T_LockRows:
6527 case T_Limit:
6528 case T_ModifyTable:
6529 case T_MergeAppend:
6530 case T_RecursiveUnion:
6531 return false;
6532 case T_Append:
6533
6534 /*
6535 * Append can't project, but if an AppendPath is being used to
6536 * represent a dummy path, what will actually be generated is a
6537 * Result which can project.
6538 */
6539 return IS_DUMMY_APPEND(path);
6540 case T_ProjectSet:
6541
6542 /*
6543 * Although ProjectSet certainly projects, say "no" because we
6544 * don't want the planner to randomly replace its tlist with
6545 * something else; the SRFs have to stay at top level. This might
6546 * get relaxed later.
6547 */
6548 return false;
6549 default:
6550 break;
6551 }
6552 return true;
6553 }
6554
6555 /*
6556 * is_projection_capable_plan
6557 * Check whether a given Plan node is able to do projection.
6558 */
6559 bool
is_projection_capable_plan(Plan * plan)6560 is_projection_capable_plan(Plan *plan)
6561 {
6562 /* Most plan types can project, so just list the ones that can't */
6563 switch (nodeTag(plan))
6564 {
6565 case T_Hash:
6566 case T_Material:
6567 case T_Sort:
6568 case T_Unique:
6569 case T_SetOp:
6570 case T_LockRows:
6571 case T_Limit:
6572 case T_ModifyTable:
6573 case T_Append:
6574 case T_MergeAppend:
6575 case T_RecursiveUnion:
6576 return false;
6577 case T_ProjectSet:
6578
6579 /*
6580 * Although ProjectSet certainly projects, say "no" because we
6581 * don't want the planner to randomly replace its tlist with
6582 * something else; the SRFs have to stay at top level. This might
6583 * get relaxed later.
6584 */
6585 return false;
6586 default:
6587 break;
6588 }
6589 return true;
6590 }
6591