PostgreSQL Source Code  git master
createplan.c File Reference
#include "postgres.h"
#include <math.h>
#include "access/sysattr.h"
#include "catalog/pg_class.h"
#include "foreign/fdwapi.h"
#include "miscadmin.h"
#include "nodes/extensible.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/optimizer.h"
#include "optimizer/paramassign.h"
#include "optimizer/paths.h"
#include "optimizer/placeholder.h"
#include "optimizer/plancat.h"
#include "optimizer/planmain.h"
#include "optimizer/prep.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/subselect.h"
#include "optimizer/tlist.h"
#include "parser/parse_clause.h"
#include "parser/parsetree.h"
#include "partitioning/partprune.h"
#include "utils/lsyscache.h"
Include dependency graph for createplan.c:

Go to the source code of this file.

Macros

#define CP_EXACT_TLIST   0x0001 /* Plan must return specified tlist */
 
#define CP_SMALL_TLIST   0x0002 /* Prefer narrower tlists */
 
#define CP_LABEL_TLIST   0x0004 /* tlist must contain sortgrouprefs */
 
#define CP_IGNORE_TLIST   0x0008 /* caller will replace tlist */
 

Functions

static Plancreate_plan_recurse (PlannerInfo *root, Path *best_path, int flags)
 
static Plancreate_scan_plan (PlannerInfo *root, Path *best_path, int flags)
 
static Listbuild_path_tlist (PlannerInfo *root, Path *path)
 
static bool use_physical_tlist (PlannerInfo *root, Path *path, int flags)
 
static Listget_gating_quals (PlannerInfo *root, List *quals)
 
static Plancreate_gating_plan (PlannerInfo *root, Path *path, Plan *plan, List *gating_quals)
 
static Plancreate_join_plan (PlannerInfo *root, JoinPath *best_path)
 
static bool mark_async_capable_plan (Plan *plan, Path *path)
 
static Plancreate_append_plan (PlannerInfo *root, AppendPath *best_path, int flags)
 
static Plancreate_merge_append_plan (PlannerInfo *root, MergeAppendPath *best_path, int flags)
 
static Resultcreate_group_result_plan (PlannerInfo *root, GroupResultPath *best_path)
 
static ProjectSetcreate_project_set_plan (PlannerInfo *root, ProjectSetPath *best_path)
 
static Materialcreate_material_plan (PlannerInfo *root, MaterialPath *best_path, int flags)
 
static Memoizecreate_memoize_plan (PlannerInfo *root, MemoizePath *best_path, int flags)
 
static Plancreate_unique_plan (PlannerInfo *root, UniquePath *best_path, int flags)
 
static Gathercreate_gather_plan (PlannerInfo *root, GatherPath *best_path)
 
static Plancreate_projection_plan (PlannerInfo *root, ProjectionPath *best_path, int flags)
 
static Planinject_projection_plan (Plan *subplan, List *tlist, bool parallel_safe)
 
static Sortcreate_sort_plan (PlannerInfo *root, SortPath *best_path, int flags)
 
static IncrementalSortcreate_incrementalsort_plan (PlannerInfo *root, IncrementalSortPath *best_path, int flags)
 
static Groupcreate_group_plan (PlannerInfo *root, GroupPath *best_path)
 
static Uniquecreate_upper_unique_plan (PlannerInfo *root, UpperUniquePath *best_path, int flags)
 
static Aggcreate_agg_plan (PlannerInfo *root, AggPath *best_path)
 
static Plancreate_groupingsets_plan (PlannerInfo *root, GroupingSetsPath *best_path)
 
static Resultcreate_minmaxagg_plan (PlannerInfo *root, MinMaxAggPath *best_path)
 
static WindowAggcreate_windowagg_plan (PlannerInfo *root, WindowAggPath *best_path)
 
static SetOpcreate_setop_plan (PlannerInfo *root, SetOpPath *best_path, int flags)
 
static RecursiveUnioncreate_recursiveunion_plan (PlannerInfo *root, RecursiveUnionPath *best_path)
 
static LockRowscreate_lockrows_plan (PlannerInfo *root, LockRowsPath *best_path, int flags)
 
static ModifyTablecreate_modifytable_plan (PlannerInfo *root, ModifyTablePath *best_path)
 
static Limitcreate_limit_plan (PlannerInfo *root, LimitPath *best_path, int flags)
 
static SeqScancreate_seqscan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static SampleScancreate_samplescan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static Scancreate_indexscan_plan (PlannerInfo *root, IndexPath *best_path, List *tlist, List *scan_clauses, bool indexonly)
 
static BitmapHeapScancreate_bitmap_scan_plan (PlannerInfo *root, BitmapHeapPath *best_path, List *tlist, List *scan_clauses)
 
static Plancreate_bitmap_subplan (PlannerInfo *root, Path *bitmapqual, List **qual, List **indexqual, List **indexECs)
 
static void bitmap_subplan_mark_shared (Plan *plan)
 
static TidScancreate_tidscan_plan (PlannerInfo *root, TidPath *best_path, List *tlist, List *scan_clauses)
 
static TidRangeScancreate_tidrangescan_plan (PlannerInfo *root, TidRangePath *best_path, List *tlist, List *scan_clauses)
 
static SubqueryScancreate_subqueryscan_plan (PlannerInfo *root, SubqueryScanPath *best_path, List *tlist, List *scan_clauses)
 
static FunctionScancreate_functionscan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static ValuesScancreate_valuesscan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static TableFuncScancreate_tablefuncscan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static CteScancreate_ctescan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static NamedTuplestoreScancreate_namedtuplestorescan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static Resultcreate_resultscan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static WorkTableScancreate_worktablescan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static ForeignScancreate_foreignscan_plan (PlannerInfo *root, ForeignPath *best_path, List *tlist, List *scan_clauses)
 
static CustomScancreate_customscan_plan (PlannerInfo *root, CustomPath *best_path, List *tlist, List *scan_clauses)
 
static NestLoopcreate_nestloop_plan (PlannerInfo *root, NestPath *best_path)
 
static MergeJoincreate_mergejoin_plan (PlannerInfo *root, MergePath *best_path)
 
static HashJoincreate_hashjoin_plan (PlannerInfo *root, HashPath *best_path)
 
static Nodereplace_nestloop_params (PlannerInfo *root, Node *expr)
 
static Nodereplace_nestloop_params_mutator (Node *node, PlannerInfo *root)
 
static void fix_indexqual_references (PlannerInfo *root, IndexPath *index_path, List **stripped_indexquals_p, List **fixed_indexquals_p)
 
static Listfix_indexorderby_references (PlannerInfo *root, IndexPath *index_path)
 
static Nodefix_indexqual_clause (PlannerInfo *root, IndexOptInfo *index, int indexcol, Node *clause, List *indexcolnos)
 
static Nodefix_indexqual_operand (Node *node, IndexOptInfo *index, int indexcol)
 
static Listget_switched_clauses (List *clauses, Relids outerrelids)
 
static Listorder_qual_clauses (PlannerInfo *root, List *clauses)
 
static void copy_generic_path_info (Plan *dest, Path *src)
 
static void copy_plan_costsize (Plan *dest, Plan *src)
 
static void label_sort_with_costsize (PlannerInfo *root, Sort *plan, double limit_tuples)
 
static SeqScanmake_seqscan (List *qptlist, List *qpqual, Index scanrelid)
 
static SampleScanmake_samplescan (List *qptlist, List *qpqual, Index scanrelid, TableSampleClause *tsc)
 
static IndexScanmake_indexscan (List *qptlist, List *qpqual, Index scanrelid, Oid indexid, List *indexqual, List *indexqualorig, List *indexorderby, List *indexorderbyorig, List *indexorderbyops, ScanDirection indexscandir)
 
static IndexOnlyScanmake_indexonlyscan (List *qptlist, List *qpqual, Index scanrelid, Oid indexid, List *indexqual, List *recheckqual, List *indexorderby, List *indextlist, ScanDirection indexscandir)
 
static BitmapIndexScanmake_bitmap_indexscan (Index scanrelid, Oid indexid, List *indexqual, List *indexqualorig)
 
static BitmapHeapScanmake_bitmap_heapscan (List *qptlist, List *qpqual, Plan *lefttree, List *bitmapqualorig, Index scanrelid)
 
static TidScanmake_tidscan (List *qptlist, List *qpqual, Index scanrelid, List *tidquals)
 
static TidRangeScanmake_tidrangescan (List *qptlist, List *qpqual, Index scanrelid, List *tidrangequals)
 
static SubqueryScanmake_subqueryscan (List *qptlist, List *qpqual, Index scanrelid, Plan *subplan)
 
static FunctionScanmake_functionscan (List *qptlist, List *qpqual, Index scanrelid, List *functions, bool funcordinality)
 
static ValuesScanmake_valuesscan (List *qptlist, List *qpqual, Index scanrelid, List *values_lists)
 
static TableFuncScanmake_tablefuncscan (List *qptlist, List *qpqual, Index scanrelid, TableFunc *tablefunc)
 
static CteScanmake_ctescan (List *qptlist, List *qpqual, Index scanrelid, int ctePlanId, int cteParam)
 
static NamedTuplestoreScanmake_namedtuplestorescan (List *qptlist, List *qpqual, Index scanrelid, char *enrname)
 
static WorkTableScanmake_worktablescan (List *qptlist, List *qpqual, Index scanrelid, int wtParam)
 
static RecursiveUnionmake_recursive_union (List *tlist, Plan *lefttree, Plan *righttree, int wtParam, List *distinctList, long numGroups)
 
static BitmapAndmake_bitmap_and (List *bitmapplans)
 
static BitmapOrmake_bitmap_or (List *bitmapplans)
 
static NestLoopmake_nestloop (List *tlist, List *joinclauses, List *otherclauses, List *nestParams, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
 
static HashJoinmake_hashjoin (List *tlist, List *joinclauses, List *otherclauses, List *hashclauses, List *hashoperators, List *hashcollations, List *hashkeys, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
 
static Hashmake_hash (Plan *lefttree, List *hashkeys, Oid skewTable, AttrNumber skewColumn, bool skewInherit)
 
static MergeJoinmake_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)
 
static Sortmake_sort (Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
 
static IncrementalSortmake_incrementalsort (Plan *lefttree, int numCols, int nPresortedCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
 
static Planprepare_sort_from_pathkeys (Plan *lefttree, List *pathkeys, Relids relids, const AttrNumber *reqColIdx, bool adjust_tlist_in_place, int *p_numsortkeys, AttrNumber **p_sortColIdx, Oid **p_sortOperators, Oid **p_collations, bool **p_nullsFirst)
 
static Sortmake_sort_from_pathkeys (Plan *lefttree, List *pathkeys, Relids relids)
 
static IncrementalSortmake_incrementalsort_from_pathkeys (Plan *lefttree, List *pathkeys, Relids relids, int nPresortedCols)
 
static Sortmake_sort_from_groupcols (List *groupcls, AttrNumber *grpColIdx, Plan *lefttree)
 
static Materialmake_material (Plan *lefttree)
 
static Memoizemake_memoize (Plan *lefttree, Oid *hashoperators, Oid *collations, List *param_exprs, bool singlerow, bool binary_mode, uint32 est_entries, Bitmapset *keyparamids)
 
static WindowAggmake_windowagg (List *tlist, Index winref, int partNumCols, AttrNumber *partColIdx, Oid *partOperators, Oid *partCollations, int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators, Oid *ordCollations, int frameOptions, Node *startOffset, Node *endOffset, Oid startInRangeFunc, Oid endInRangeFunc, Oid inRangeColl, bool inRangeAsc, bool inRangeNullsFirst, List *runCondition, List *qual, bool topWindow, Plan *lefttree)
 
static Groupmake_group (List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations, Plan *lefttree)
 
static Uniquemake_unique_from_sortclauses (Plan *lefttree, List *distinctList)
 
static Uniquemake_unique_from_pathkeys (Plan *lefttree, List *pathkeys, int numCols)
 
static Gathermake_gather (List *qptlist, List *qpqual, int nworkers, int rescan_param, bool single_copy, Plan *subplan)
 
static SetOpmake_setop (SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree, List *distinctList, AttrNumber flagColIdx, int firstFlag, long numGroups)
 
static LockRowsmake_lockrows (Plan *lefttree, List *rowMarks, int epqParam)
 
static Resultmake_result (List *tlist, Node *resconstantqual, Plan *subplan)
 
static ProjectSetmake_project_set (List *tlist, Plan *subplan)
 
static ModifyTablemake_modifytable (PlannerInfo *root, Plan *subplan, CmdType operation, bool canSetTag, Index nominalRelation, Index rootRelation, bool partColsUpdated, List *resultRelations, List *updateColnosLists, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, List *mergeActionLists, int epqParam)
 
static GatherMergecreate_gather_merge_plan (PlannerInfo *root, GatherMergePath *best_path)
 
Plancreate_plan (PlannerInfo *root, Path *best_path)
 
Planchange_plan_targetlist (Plan *subplan, List *tlist, bool tlist_parallel_safe)
 
static AttrNumberremap_groupColIdx (PlannerInfo *root, List *groupClause)
 
ForeignScanmake_foreignscan (List *qptlist, List *qpqual, Index scanrelid, List *fdw_exprs, List *fdw_private, List *fdw_scan_tlist, List *fdw_recheck_quals, Plan *outer_plan)
 
Sortmake_sort_from_sortclauses (List *sortcls, Plan *lefttree)
 
Planmaterialize_finished_plan (Plan *subplan)
 
Aggmake_agg (List *tlist, List *qual, AggStrategy aggstrategy, AggSplit aggsplit, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations, List *groupingSets, List *chain, double dNumGroups, Size transitionSpace, Plan *lefttree)
 
Limitmake_limit (Plan *lefttree, Node *limitOffset, Node *limitCount, LimitOption limitOption, int uniqNumCols, AttrNumber *uniqColIdx, Oid *uniqOperators, Oid *uniqCollations)
 
bool is_projection_capable_path (Path *path)
 
bool is_projection_capable_plan (Plan *plan)
 

Macro Definition Documentation

◆ CP_EXACT_TLIST

#define CP_EXACT_TLIST   0x0001 /* Plan must return specified tlist */

Definition at line 68 of file createplan.c.

◆ CP_IGNORE_TLIST

#define CP_IGNORE_TLIST   0x0008 /* caller will replace tlist */

Definition at line 71 of file createplan.c.

◆ CP_LABEL_TLIST

#define CP_LABEL_TLIST   0x0004 /* tlist must contain sortgrouprefs */

Definition at line 70 of file createplan.c.

◆ CP_SMALL_TLIST

#define CP_SMALL_TLIST   0x0002 /* Prefer narrower tlists */

Definition at line 69 of file createplan.c.

Function Documentation

◆ bitmap_subplan_mark_shared()

static void bitmap_subplan_mark_shared ( Plan plan)
static

Definition at line 5450 of file createplan.c.

5451 {
5452  if (IsA(plan, BitmapAnd))
5453  bitmap_subplan_mark_shared(linitial(((BitmapAnd *) plan)->bitmapplans));
5454  else if (IsA(plan, BitmapOr))
5455  {
5456  ((BitmapOr *) plan)->isshared = true;
5457  bitmap_subplan_mark_shared(linitial(((BitmapOr *) plan)->bitmapplans));
5458  }
5459  else if (IsA(plan, BitmapIndexScan))
5460  ((BitmapIndexScan *) plan)->isshared = true;
5461  else
5462  elog(ERROR, "unrecognized node type: %d", nodeTag(plan));
5463 }
static void bitmap_subplan_mark_shared(Plan *plan)
Definition: createplan.c:5450
#define ERROR
Definition: elog.h:39
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
#define nodeTag(nodeptr)
Definition: nodes.h:133
#define linitial(l)
Definition: pg_list.h:178
#define plan(x)
Definition: pg_regress.c:154

References elog(), ERROR, IsA, linitial, nodeTag, and plan.

Referenced by create_bitmap_scan_plan().

◆ build_path_tlist()

static List * build_path_tlist ( PlannerInfo root,
Path path 
)
static

Definition at line 823 of file createplan.c.

824 {
825  List *tlist = NIL;
826  Index *sortgrouprefs = path->pathtarget->sortgrouprefs;
827  int resno = 1;
828  ListCell *v;
829 
830  foreach(v, path->pathtarget->exprs)
831  {
832  Node *node = (Node *) lfirst(v);
833  TargetEntry *tle;
834 
835  /*
836  * If it's a parameterized path, there might be lateral references in
837  * the tlist, which need to be replaced with Params. There's no need
838  * to remake the TargetEntry nodes, so apply this to each list item
839  * separately.
840  */
841  if (path->param_info)
842  node = replace_nestloop_params(root, node);
843 
844  tle = makeTargetEntry((Expr *) node,
845  resno,
846  NULL,
847  false);
848  if (sortgrouprefs)
849  tle->ressortgroupref = sortgrouprefs[resno - 1];
850 
851  tlist = lappend(tlist, tle);
852  resno++;
853  }
854  return tlist;
855 }
unsigned int Index
Definition: c.h:603
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4907
List * lappend(List *list, void *datum)
Definition: list.c:338
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:241
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
Definition: pg_list.h:54
Definition: nodes.h:129
Index ressortgroupref
Definition: primnodes.h:1901

References lappend(), lfirst, makeTargetEntry(), NIL, replace_nestloop_params(), and TargetEntry::ressortgroupref.

Referenced by create_agg_plan(), create_append_plan(), create_gather_merge_plan(), create_gather_plan(), create_gating_plan(), create_group_plan(), create_group_result_plan(), create_groupingsets_plan(), create_hashjoin_plan(), create_merge_append_plan(), create_mergejoin_plan(), create_minmaxagg_plan(), create_nestloop_plan(), create_project_set_plan(), create_projection_plan(), create_recursiveunion_plan(), create_scan_plan(), create_unique_plan(), and create_windowagg_plan().

◆ change_plan_targetlist()

Plan* change_plan_targetlist ( Plan subplan,
List tlist,
bool  tlist_parallel_safe 
)

Definition at line 2150 of file createplan.c.

2151 {
2152  /*
2153  * If the top plan node can't do projections and its existing target list
2154  * isn't already what we need, we need to add a Result node to help it
2155  * along.
2156  */
2157  if (!is_projection_capable_plan(subplan) &&
2158  !tlist_same_exprs(tlist, subplan->targetlist))
2159  subplan = inject_projection_plan(subplan, tlist,
2160  subplan->parallel_safe &&
2161  tlist_parallel_safe);
2162  else
2163  {
2164  /* Else we can just replace the plan node's tlist */
2165  subplan->targetlist = tlist;
2166  subplan->parallel_safe &= tlist_parallel_safe;
2167  }
2168  return subplan;
2169 }
bool is_projection_capable_plan(Plan *plan)
Definition: createplan.c:7227
static Plan * inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
Definition: createplan.c:2118
bool parallel_safe
Definition: plannodes.h:142
List * targetlist
Definition: plannodes.h:153
bool tlist_same_exprs(List *tlist1, List *tlist2)
Definition: tlist.c:218

References inject_projection_plan(), is_projection_capable_plan(), Plan::parallel_safe, Plan::targetlist, and tlist_same_exprs().

Referenced by create_unique_plan(), and postgresGetForeignPlan().

◆ copy_generic_path_info()

static void copy_generic_path_info ( Plan dest,
Path src 
)
static

Definition at line 5381 of file createplan.c.

5382 {
5383  dest->startup_cost = src->startup_cost;
5384  dest->total_cost = src->total_cost;
5385  dest->plan_rows = src->rows;
5386  dest->plan_width = src->pathtarget->width;
5387  dest->parallel_aware = src->parallel_aware;
5388  dest->parallel_safe = src->parallel_safe;
5389 }
Cardinality rows
Definition: pathnodes.h:1628
Cost startup_cost
Definition: pathnodes.h:1629
Cost total_cost
Definition: pathnodes.h:1630
bool parallel_aware
Definition: pathnodes.h:1621
bool parallel_safe
Definition: pathnodes.h:1623

References generate_unaccent_rules::dest, Path::parallel_aware, Path::parallel_safe, Path::rows, Path::startup_cost, and Path::total_cost.

Referenced by create_agg_plan(), create_append_plan(), create_bitmap_scan_plan(), create_ctescan_plan(), create_customscan_plan(), create_foreignscan_plan(), create_functionscan_plan(), create_gather_merge_plan(), create_gather_plan(), create_group_plan(), create_group_result_plan(), create_groupingsets_plan(), create_hashjoin_plan(), create_incrementalsort_plan(), create_indexscan_plan(), create_limit_plan(), create_lockrows_plan(), create_material_plan(), create_memoize_plan(), create_merge_append_plan(), create_mergejoin_plan(), create_minmaxagg_plan(), create_modifytable_plan(), create_namedtuplestorescan_plan(), create_nestloop_plan(), create_project_set_plan(), create_projection_plan(), create_recursiveunion_plan(), create_resultscan_plan(), create_samplescan_plan(), create_seqscan_plan(), create_setop_plan(), create_sort_plan(), create_subqueryscan_plan(), create_tablefuncscan_plan(), create_tidrangescan_plan(), create_tidscan_plan(), create_unique_plan(), create_upper_unique_plan(), create_valuesscan_plan(), create_windowagg_plan(), and create_worktablescan_plan().

◆ copy_plan_costsize()

static void copy_plan_costsize ( Plan dest,
Plan src 
)
static

Definition at line 5396 of file createplan.c.

5397 {
5398  dest->startup_cost = src->startup_cost;
5399  dest->total_cost = src->total_cost;
5400  dest->plan_rows = src->plan_rows;
5401  dest->plan_width = src->plan_width;
5402  /* Assume the inserted node is not parallel-aware. */
5403  dest->parallel_aware = false;
5404  /* Assume the inserted node is parallel-safe, if child plan is. */
5405  dest->parallel_safe = src->parallel_safe;
5406 }
Cost total_cost
Definition: plannodes.h:130
Cost startup_cost
Definition: plannodes.h:129
int plan_width
Definition: plannodes.h:136
Cardinality plan_rows
Definition: plannodes.h:135

References generate_unaccent_rules::dest, Plan::parallel_safe, Plan::plan_rows, Plan::plan_width, Plan::startup_cost, and Plan::total_cost.

Referenced by create_gating_plan(), create_hashjoin_plan(), create_mergejoin_plan(), and inject_projection_plan().

◆ create_agg_plan()

static Agg * create_agg_plan ( PlannerInfo root,
AggPath best_path 
)
static

Definition at line 2306 of file createplan.c.

2307 {
2308  Agg *plan;
2309  Plan *subplan;
2310  List *tlist;
2311  List *quals;
2312 
2313  /*
2314  * Agg can project, so no need to be terribly picky about child tlist, but
2315  * we do need grouping columns to be available
2316  */
2317  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2318 
2319  tlist = build_path_tlist(root, &best_path->path);
2320 
2321  quals = order_qual_clauses(root, best_path->qual);
2322 
2323  plan = make_agg(tlist, quals,
2324  best_path->aggstrategy,
2325  best_path->aggsplit,
2326  list_length(best_path->groupClause),
2328  subplan->targetlist),
2329  extract_grouping_ops(best_path->groupClause),
2331  subplan->targetlist),
2332  NIL,
2333  NIL,
2334  best_path->numGroups,
2335  best_path->transitionSpace,
2336  subplan);
2337 
2338  copy_generic_path_info(&plan->plan, (Path *) best_path);
2339 
2340  return plan;
2341 }
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5287
Agg * make_agg(List *tlist, List *qual, AggStrategy aggstrategy, AggSplit aggsplit, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations, List *groupingSets, List *chain, double dNumGroups, Size transitionSpace, Plan *lefttree)
Definition: createplan.c:6565
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5381
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:386
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:823
#define CP_LABEL_TLIST
Definition: createplan.c:70
static int list_length(const List *l)
Definition: pg_list.h:152
Path * subpath
Definition: pathnodes.h:2222
Cardinality numGroups
Definition: pathnodes.h:2225
AggSplit aggsplit
Definition: pathnodes.h:2224
List * groupClause
Definition: pathnodes.h:2227
uint64 transitionSpace
Definition: pathnodes.h:2226
AggStrategy aggstrategy
Definition: pathnodes.h:2223
Path path
Definition: pathnodes.h:2221
List * qual
Definition: pathnodes.h:2228
Definition: plannodes.h:995
Oid * extract_grouping_collations(List *groupClause, List *tlist)
Definition: tlist.c:489
Oid * extract_grouping_ops(List *groupClause)
Definition: tlist.c:463
AttrNumber * extract_grouping_cols(List *groupClause, List *tlist)
Definition: tlist.c:514

References AggPath::aggsplit, AggPath::aggstrategy, build_path_tlist(), copy_generic_path_info(), CP_LABEL_TLIST, create_plan_recurse(), extract_grouping_collations(), extract_grouping_cols(), extract_grouping_ops(), AggPath::groupClause, list_length(), make_agg(), NIL, AggPath::numGroups, order_qual_clauses(), AggPath::path, plan, AggPath::qual, AggPath::subpath, Plan::targetlist, and AggPath::transitionSpace.

Referenced by create_plan_recurse().

◆ create_append_plan()

static Plan * create_append_plan ( PlannerInfo root,
AppendPath best_path,
int  flags 
)
static

Definition at line 1214 of file createplan.c.

1215 {
1216  Append *plan;
1217  List *tlist = build_path_tlist(root, &best_path->path);
1218  int orig_tlist_length = list_length(tlist);
1219  bool tlist_was_changed = false;
1220  List *pathkeys = best_path->path.pathkeys;
1221  List *subplans = NIL;
1222  ListCell *subpaths;
1223  int nasyncplans = 0;
1224  RelOptInfo *rel = best_path->path.parent;
1225  PartitionPruneInfo *partpruneinfo = NULL;
1226  int nodenumsortkeys = 0;
1227  AttrNumber *nodeSortColIdx = NULL;
1228  Oid *nodeSortOperators = NULL;
1229  Oid *nodeCollations = NULL;
1230  bool *nodeNullsFirst = NULL;
1231  bool consider_async = false;
1232 
1233  /*
1234  * The subpaths list could be empty, if every child was proven empty by
1235  * constraint exclusion. In that case generate a dummy plan that returns
1236  * no rows.
1237  *
1238  * Note that an AppendPath with no members is also generated in certain
1239  * cases where there was no appending construct at all, but we know the
1240  * relation is empty (see set_dummy_rel_pathlist and mark_dummy_rel).
1241  */
1242  if (best_path->subpaths == NIL)
1243  {
1244  /* Generate a Result plan with constant-FALSE gating qual */
1245  Plan *plan;
1246 
1247  plan = (Plan *) make_result(tlist,
1248  (Node *) list_make1(makeBoolConst(false,
1249  false)),
1250  NULL);
1251 
1252  copy_generic_path_info(plan, (Path *) best_path);
1253 
1254  return plan;
1255  }
1256 
1257  /*
1258  * Otherwise build an Append plan. Note that if there's just one child,
1259  * the Append is pretty useless; but we wait till setrefs.c to get rid of
1260  * it. Doing so here doesn't work because the varno of the child scan
1261  * plan won't match the parent-rel Vars it'll be asked to emit.
1262  *
1263  * We don't have the actual creation of the Append node split out into a
1264  * separate make_xxx function. This is because we want to run
1265  * prepare_sort_from_pathkeys on it before we do so on the individual
1266  * child plans, to make cross-checking the sort info easier.
1267  */
1268  plan = makeNode(Append);
1269  plan->plan.targetlist = tlist;
1270  plan->plan.qual = NIL;
1271  plan->plan.lefttree = NULL;
1272  plan->plan.righttree = NULL;
1273  plan->apprelids = rel->relids;
1274 
1275  if (pathkeys != NIL)
1276  {
1277  /*
1278  * Compute sort column info, and adjust the Append's tlist as needed.
1279  * Because we pass adjust_tlist_in_place = true, we may ignore the
1280  * function result; it must be the same plan node. However, we then
1281  * need to detect whether any tlist entries were added.
1282  */
1283  (void) prepare_sort_from_pathkeys((Plan *) plan, pathkeys,
1284  best_path->path.parent->relids,
1285  NULL,
1286  true,
1287  &nodenumsortkeys,
1288  &nodeSortColIdx,
1289  &nodeSortOperators,
1290  &nodeCollations,
1291  &nodeNullsFirst);
1292  tlist_was_changed = (orig_tlist_length != list_length(plan->plan.targetlist));
1293  }
1294 
1295  /* If appropriate, consider async append */
1296  consider_async = (enable_async_append && pathkeys == NIL &&
1297  !best_path->path.parallel_safe &&
1298  list_length(best_path->subpaths) > 1);
1299 
1300  /* Build the plan for each child */
1301  foreach(subpaths, best_path->subpaths)
1302  {
1303  Path *subpath = (Path *) lfirst(subpaths);
1304  Plan *subplan;
1305 
1306  /* Must insist that all children return the same tlist */
1307  subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
1308 
1309  /*
1310  * For ordered Appends, we must insert a Sort node if subplan isn't
1311  * sufficiently ordered.
1312  */
1313  if (pathkeys != NIL)
1314  {
1315  int numsortkeys;
1316  AttrNumber *sortColIdx;
1317  Oid *sortOperators;
1318  Oid *collations;
1319  bool *nullsFirst;
1320 
1321  /*
1322  * Compute sort column info, and adjust subplan's tlist as needed.
1323  * We must apply prepare_sort_from_pathkeys even to subplans that
1324  * don't need an explicit sort, to make sure they are returning
1325  * the same sort key columns the Append expects.
1326  */
1327  subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1328  subpath->parent->relids,
1329  nodeSortColIdx,
1330  false,
1331  &numsortkeys,
1332  &sortColIdx,
1333  &sortOperators,
1334  &collations,
1335  &nullsFirst);
1336 
1337  /*
1338  * Check that we got the same sort key information. We just
1339  * Assert that the sortops match, since those depend only on the
1340  * pathkeys; but it seems like a good idea to check the sort
1341  * column numbers explicitly, to ensure the tlists match up.
1342  */
1343  Assert(numsortkeys == nodenumsortkeys);
1344  if (memcmp(sortColIdx, nodeSortColIdx,
1345  numsortkeys * sizeof(AttrNumber)) != 0)
1346  elog(ERROR, "Append child's targetlist doesn't match Append");
1347  Assert(memcmp(sortOperators, nodeSortOperators,
1348  numsortkeys * sizeof(Oid)) == 0);
1349  Assert(memcmp(collations, nodeCollations,
1350  numsortkeys * sizeof(Oid)) == 0);
1351  Assert(memcmp(nullsFirst, nodeNullsFirst,
1352  numsortkeys * sizeof(bool)) == 0);
1353 
1354  /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1355  if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
1356  {
1357  Sort *sort = make_sort(subplan, numsortkeys,
1358  sortColIdx, sortOperators,
1359  collations, nullsFirst);
1360 
1361  label_sort_with_costsize(root, sort, best_path->limit_tuples);
1362  subplan = (Plan *) sort;
1363  }
1364  }
1365 
1366  /* If needed, check to see if subplan can be executed asynchronously */
1367  if (consider_async && mark_async_capable_plan(subplan, subpath))
1368  {
1369  Assert(subplan->async_capable);
1370  ++nasyncplans;
1371  }
1372 
1373  subplans = lappend(subplans, subplan);
1374  }
1375 
1376  /*
1377  * If any quals exist, they may be useful to perform further partition
1378  * pruning during execution. Gather information needed by the executor to
1379  * do partition pruning.
1380  */
1382  {
1383  List *prunequal;
1384 
1385  prunequal = extract_actual_clauses(rel->baserestrictinfo, false);
1386 
1387  if (best_path->path.param_info)
1388  {
1389  List *prmquals = best_path->path.param_info->ppi_clauses;
1390 
1391  prmquals = extract_actual_clauses(prmquals, false);
1392  prmquals = (List *) replace_nestloop_params(root,
1393  (Node *) prmquals);
1394 
1395  prunequal = list_concat(prunequal, prmquals);
1396  }
1397 
1398  if (prunequal != NIL)
1399  partpruneinfo =
1400  make_partition_pruneinfo(root, rel,
1401  best_path->subpaths,
1402  prunequal);
1403  }
1404 
1405  plan->appendplans = subplans;
1406  plan->nasyncplans = nasyncplans;
1407  plan->first_partial_plan = best_path->first_partial_path;
1408  plan->part_prune_info = partpruneinfo;
1409 
1410  copy_generic_path_info(&plan->plan, (Path *) best_path);
1411 
1412  /*
1413  * If prepare_sort_from_pathkeys added sort columns, but we were told to
1414  * produce either the exact tlist or a narrow tlist, we should get rid of
1415  * the sort columns again. We must inject a projection node to do so.
1416  */
1417  if (tlist_was_changed && (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST)))
1418  {
1419  tlist = list_copy_head(plan->plan.targetlist, orig_tlist_length);
1420  return inject_projection_plan((Plan *) plan, tlist,
1421  plan->plan.parallel_safe);
1422  }
1423  else
1424  return (Plan *) plan;
1425 }
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:195
int16 AttrNumber
Definition: attnum.h:21
bool enable_async_append
Definition: costsize.c:155
bool enable_partition_pruning
Definition: costsize.c:153
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6960
static void label_sort_with_costsize(PlannerInfo *root, Sort *plan, double limit_tuples)
Definition: createplan.c:5418
#define CP_SMALL_TLIST
Definition: createplan.c:69
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
Definition: createplan.c:6040
#define CP_EXACT_TLIST
Definition: createplan.c:68
static bool mark_async_capable_plan(Plan *plan, Path *path)
Definition: createplan.c:1138
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)
Definition: createplan.c:6136
Assert(fmt[strlen(fmt) - 1] !='\n')
List * list_copy_head(const List *oldlist, int len)
Definition: list.c:1592
List * list_concat(List *list1, const List *list2)
Definition: list.c:560
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:241
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:360
#define makeNode(_type_)
Definition: nodes.h:176
PartitionPruneInfo * make_partition_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel, List *subpaths, List *prunequal)
Definition: partprune.c:223
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:340
#define list_make1(x1)
Definition: pg_list.h:212
unsigned int Oid
Definition: postgres_ext.h:31
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:494
int first_partial_path
Definition: pathnodes.h:1902
Cardinality limit_tuples
Definition: pathnodes.h:1903
List * subpaths
Definition: pathnodes.h:1900
List * pathkeys
Definition: pathnodes.h:1633
bool async_capable
Definition: plannodes.h:147
List * baserestrictinfo
Definition: pathnodes.h:964
Relids relids
Definition: pathnodes.h:856

References Assert(), Plan::async_capable, RelOptInfo::baserestrictinfo, build_path_tlist(), copy_generic_path_info(), CP_EXACT_TLIST, CP_SMALL_TLIST, create_plan_recurse(), elog(), enable_async_append, enable_partition_pruning, ERROR, extract_actual_clauses(), AppendPath::first_partial_path, inject_projection_plan(), label_sort_with_costsize(), lappend(), lfirst, AppendPath::limit_tuples, list_concat(), list_copy_head(), list_length(), list_make1, make_partition_pruneinfo(), make_result(), make_sort(), makeBoolConst(), makeNode, mark_async_capable_plan(), NIL, Path::parallel_safe, AppendPath::path, Path::pathkeys, pathkeys_contained_in(), plan, prepare_sort_from_pathkeys(), RelOptInfo::relids, replace_nestloop_params(), sort(), subpath(), and AppendPath::subpaths.

Referenced by create_plan_recurse().

◆ create_bitmap_scan_plan()

static BitmapHeapScan * create_bitmap_scan_plan ( PlannerInfo root,
BitmapHeapPath best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3198 of file createplan.c.

3202 {
3203  Index baserelid = best_path->path.parent->relid;
3204  Plan *bitmapqualplan;
3205  List *bitmapqualorig;
3206  List *indexquals;
3207  List *indexECs;
3208  List *qpqual;
3209  ListCell *l;
3210  BitmapHeapScan *scan_plan;
3211 
3212  /* it should be a base rel... */
3213  Assert(baserelid > 0);
3214  Assert(best_path->path.parent->rtekind == RTE_RELATION);
3215 
3216  /* Process the bitmapqual tree into a Plan tree and qual lists */
3217  bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
3218  &bitmapqualorig, &indexquals,
3219  &indexECs);
3220 
3221  if (best_path->path.parallel_aware)
3222  bitmap_subplan_mark_shared(bitmapqualplan);
3223 
3224  /*
3225  * The qpqual list must contain all restrictions not automatically handled
3226  * by the index, other than pseudoconstant clauses which will be handled
3227  * by a separate gating plan node. All the predicates in the indexquals
3228  * will be checked (either by the index itself, or by
3229  * nodeBitmapHeapscan.c), but if there are any "special" operators
3230  * involved then they must be added to qpqual. The upshot is that qpqual
3231  * must contain scan_clauses minus whatever appears in indexquals.
3232  *
3233  * This loop is similar to the comparable code in create_indexscan_plan(),
3234  * but with some differences because it has to compare the scan clauses to
3235  * stripped (no RestrictInfos) indexquals. See comments there for more
3236  * info.
3237  *
3238  * In normal cases simple equal() checks will be enough to spot duplicate
3239  * clauses, so we try that first. We next see if the scan clause is
3240  * redundant with any top-level indexqual by virtue of being generated
3241  * from the same EC. After that, try predicate_implied_by().
3242  *
3243  * Unlike create_indexscan_plan(), the predicate_implied_by() test here is
3244  * useful for getting rid of qpquals that are implied by index predicates,
3245  * because the predicate conditions are included in the "indexquals"
3246  * returned by create_bitmap_subplan(). Bitmap scans have to do it that
3247  * way because predicate conditions need to be rechecked if the scan
3248  * becomes lossy, so they have to be included in bitmapqualorig.
3249  */
3250  qpqual = NIL;
3251  foreach(l, scan_clauses)
3252  {
3253  RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
3254  Node *clause = (Node *) rinfo->clause;
3255 
3256  if (rinfo->pseudoconstant)
3257  continue; /* we may drop pseudoconstants here */
3258  if (list_member(indexquals, clause))
3259  continue; /* simple duplicate */
3260  if (rinfo->parent_ec && list_member_ptr(indexECs, rinfo->parent_ec))
3261  continue; /* derived from same EquivalenceClass */
3262  if (!contain_mutable_functions(clause) &&
3263  predicate_implied_by(list_make1(clause), indexquals, false))
3264  continue; /* provably implied by indexquals */
3265  qpqual = lappend(qpqual, rinfo);
3266  }
3267 
3268  /* Sort clauses into best execution order */
3269  qpqual = order_qual_clauses(root, qpqual);
3270 
3271  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3272  qpqual = extract_actual_clauses(qpqual, false);
3273 
3274  /*
3275  * When dealing with special operators, we will at this point have
3276  * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
3277  * 'em from bitmapqualorig, since there's no point in making the tests
3278  * twice.
3279  */
3280  bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
3281 
3282  /*
3283  * We have to replace any outer-relation variables with nestloop params in
3284  * the qpqual and bitmapqualorig expressions. (This was already done for
3285  * expressions attached to plan nodes in the bitmapqualplan tree.)
3286  */
3287  if (best_path->path.param_info)
3288  {
3289  qpqual = (List *)
3290  replace_nestloop_params(root, (Node *) qpqual);
3291  bitmapqualorig = (List *)
3292  replace_nestloop_params(root, (Node *) bitmapqualorig);
3293  }
3294 
3295  /* Finally ready to build the plan node */
3296  scan_plan = make_bitmap_heapscan(tlist,
3297  qpqual,
3298  bitmapqualplan,
3299  bitmapqualorig,
3300  baserelid);
3301 
3302  copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3303 
3304  return scan_plan;
3305 }
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:367
static Plan * create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual, List **qual, List **indexqual, List **indexECs)
Definition: createplan.c:3328
static BitmapHeapScan * make_bitmap_heapscan(List *qptlist, List *qpqual, Plan *lefttree, List *bitmapqualorig, Index scanrelid)
Definition: createplan.c:5597
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
List * list_difference_ptr(const List *list1, const List *list2)
Definition: list.c:1262
bool list_member_ptr(const List *list, const void *datum)
Definition: list.c:681
bool list_member(const List *list, const void *datum)
Definition: list.c:660
@ RTE_RELATION
Definition: parsenodes.h:1013
#define lfirst_node(type, lc)
Definition: pg_list.h:176
bool predicate_implied_by(List *predicate_list, List *clause_list, bool weak)
Definition: predtest.c:152
Path * bitmapqual
Definition: pathnodes.h:1753
Expr * clause
Definition: pathnodes.h:2529

References Assert(), bitmap_subplan_mark_shared(), BitmapHeapPath::bitmapqual, RestrictInfo::clause, contain_mutable_functions(), copy_generic_path_info(), create_bitmap_subplan(), extract_actual_clauses(), if(), lappend(), lfirst_node, list_difference_ptr(), list_make1, list_member(), list_member_ptr(), make_bitmap_heapscan(), NIL, order_qual_clauses(), Path::parallel_aware, BitmapHeapPath::path, predicate_implied_by(), replace_nestloop_params(), RTE_RELATION, and BitmapHeapScan::scan.

Referenced by create_scan_plan().

◆ create_bitmap_subplan()

static Plan * create_bitmap_subplan ( PlannerInfo root,
Path bitmapqual,
List **  qual,
List **  indexqual,
List **  indexECs 
)
static

Definition at line 3328 of file createplan.c.

3330 {
3331  Plan *plan;
3332 
3333  if (IsA(bitmapqual, BitmapAndPath))
3334  {
3335  BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
3336  List *subplans = NIL;
3337  List *subquals = NIL;
3338  List *subindexquals = NIL;
3339  List *subindexECs = NIL;
3340  ListCell *l;
3341 
3342  /*
3343  * There may well be redundant quals among the subplans, since a
3344  * top-level WHERE qual might have gotten used to form several
3345  * different index quals. We don't try exceedingly hard to eliminate
3346  * redundancies, but we do eliminate obvious duplicates by using
3347  * list_concat_unique.
3348  */
3349  foreach(l, apath->bitmapquals)
3350  {
3351  Plan *subplan;
3352  List *subqual;
3353  List *subindexqual;
3354  List *subindexEC;
3355 
3356  subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
3357  &subqual, &subindexqual,
3358  &subindexEC);
3359  subplans = lappend(subplans, subplan);
3360  subquals = list_concat_unique(subquals, subqual);
3361  subindexquals = list_concat_unique(subindexquals, subindexqual);
3362  /* Duplicates in indexECs aren't worth getting rid of */
3363  subindexECs = list_concat(subindexECs, subindexEC);
3364  }
3365  plan = (Plan *) make_bitmap_and(subplans);
3366  plan->startup_cost = apath->path.startup_cost;
3367  plan->total_cost = apath->path.total_cost;
3368  plan->plan_rows =
3369  clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
3370  plan->plan_width = 0; /* meaningless */
3371  plan->parallel_aware = false;
3372  plan->parallel_safe = apath->path.parallel_safe;
3373  *qual = subquals;
3374  *indexqual = subindexquals;
3375  *indexECs = subindexECs;
3376  }
3377  else if (IsA(bitmapqual, BitmapOrPath))
3378  {
3379  BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
3380  List *subplans = NIL;
3381  List *subquals = NIL;
3382  List *subindexquals = NIL;
3383  bool const_true_subqual = false;
3384  bool const_true_subindexqual = false;
3385  ListCell *l;
3386 
3387  /*
3388  * Here, we only detect qual-free subplans. A qual-free subplan would
3389  * cause us to generate "... OR true ..." which we may as well reduce
3390  * to just "true". We do not try to eliminate redundant subclauses
3391  * because (a) it's not as likely as in the AND case, and (b) we might
3392  * well be working with hundreds or even thousands of OR conditions,
3393  * perhaps from a long IN list. The performance of list_append_unique
3394  * would be unacceptable.
3395  */
3396  foreach(l, opath->bitmapquals)
3397  {
3398  Plan *subplan;
3399  List *subqual;
3400  List *subindexqual;
3401  List *subindexEC;
3402 
3403  subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
3404  &subqual, &subindexqual,
3405  &subindexEC);
3406  subplans = lappend(subplans, subplan);
3407  if (subqual == NIL)
3408  const_true_subqual = true;
3409  else if (!const_true_subqual)
3410  subquals = lappend(subquals,
3411  make_ands_explicit(subqual));
3412  if (subindexqual == NIL)
3413  const_true_subindexqual = true;
3414  else if (!const_true_subindexqual)
3415  subindexquals = lappend(subindexquals,
3416  make_ands_explicit(subindexqual));
3417  }
3418 
3419  /*
3420  * In the presence of ScalarArrayOpExpr quals, we might have built
3421  * BitmapOrPaths with just one subpath; don't add an OR step.
3422  */
3423  if (list_length(subplans) == 1)
3424  {
3425  plan = (Plan *) linitial(subplans);
3426  }
3427  else
3428  {
3429  plan = (Plan *) make_bitmap_or(subplans);
3430  plan->startup_cost = opath->path.startup_cost;
3431  plan->total_cost = opath->path.total_cost;
3432  plan->plan_rows =
3433  clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
3434  plan->plan_width = 0; /* meaningless */
3435  plan->parallel_aware = false;
3436  plan->parallel_safe = opath->path.parallel_safe;
3437  }
3438 
3439  /*
3440  * If there were constant-TRUE subquals, the OR reduces to constant
3441  * TRUE. Also, avoid generating one-element ORs, which could happen
3442  * due to redundancy elimination or ScalarArrayOpExpr quals.
3443  */
3444  if (const_true_subqual)
3445  *qual = NIL;
3446  else if (list_length(subquals) <= 1)
3447  *qual = subquals;
3448  else
3449  *qual = list_make1(make_orclause(subquals));
3450  if (const_true_subindexqual)
3451  *indexqual = NIL;
3452  else if (list_length(subindexquals) <= 1)
3453  *indexqual = subindexquals;
3454  else
3455  *indexqual = list_make1(make_orclause(subindexquals));
3456  *indexECs = NIL;
3457  }
3458  else if (IsA(bitmapqual, IndexPath))
3459  {
3460  IndexPath *ipath = (IndexPath *) bitmapqual;
3461  IndexScan *iscan;
3462  List *subquals;
3463  List *subindexquals;
3464  List *subindexECs;
3465  ListCell *l;
3466 
3467  /* Use the regular indexscan plan build machinery... */
3468  iscan = castNode(IndexScan,
3469  create_indexscan_plan(root, ipath,
3470  NIL, NIL, false));
3471  /* then convert to a bitmap indexscan */
3473  iscan->indexid,
3474  iscan->indexqual,
3475  iscan->indexqualorig);
3476  /* and set its cost/width fields appropriately */
3477  plan->startup_cost = 0.0;
3478  plan->total_cost = ipath->indextotalcost;
3479  plan->plan_rows =
3480  clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
3481  plan->plan_width = 0; /* meaningless */
3482  plan->parallel_aware = false;
3483  plan->parallel_safe = ipath->path.parallel_safe;
3484  /* Extract original index clauses, actual index quals, relevant ECs */
3485  subquals = NIL;
3486  subindexquals = NIL;
3487  subindexECs = NIL;
3488  foreach(l, ipath->indexclauses)
3489  {
3490  IndexClause *iclause = (IndexClause *) lfirst(l);
3491  RestrictInfo *rinfo = iclause->rinfo;
3492 
3493  Assert(!rinfo->pseudoconstant);
3494  subquals = lappend(subquals, rinfo->clause);
3495  subindexquals = list_concat(subindexquals,
3496  get_actual_clauses(iclause->indexquals));
3497  if (rinfo->parent_ec)
3498  subindexECs = lappend(subindexECs, rinfo->parent_ec);
3499  }
3500  /* We can add any index predicate conditions, too */
3501  foreach(l, ipath->indexinfo->indpred)
3502  {
3503  Expr *pred = (Expr *) lfirst(l);
3504 
3505  /*
3506  * We know that the index predicate must have been implied by the
3507  * query condition as a whole, but it may or may not be implied by
3508  * the conditions that got pushed into the bitmapqual. Avoid
3509  * generating redundant conditions.
3510  */
3511  if (!predicate_implied_by(list_make1(pred), subquals, false))
3512  {
3513  subquals = lappend(subquals, pred);
3514  subindexquals = lappend(subindexquals, pred);
3515  }
3516  }
3517  *qual = subquals;
3518  *indexqual = subindexquals;
3519  *indexECs = subindexECs;
3520  }
3521  else
3522  {
3523  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
3524  plan = NULL; /* keep compiler quiet */
3525  }
3526 
3527  return plan;
3528 }
double clamp_row_est(double nrows)
Definition: costsize.c:203
static BitmapOr * make_bitmap_or(List *bitmapplans)
Definition: createplan.c:5905
static BitmapIndexScan * make_bitmap_indexscan(Index scanrelid, Oid indexid, List *indexqual, List *indexqualorig)
Definition: createplan.c:5576
static BitmapAnd * make_bitmap_and(List *bitmapplans)
Definition: createplan.c:5890
static Scan * create_indexscan_plan(PlannerInfo *root, IndexPath *best_path, List *tlist, List *scan_clauses, bool indexonly)
Definition: createplan.c:3002
List * list_concat_unique(List *list1, const List *list2)
Definition: list.c:1404
Expr * make_ands_explicit(List *andclauses)
Definition: makefuncs.c:711
Expr * make_orclause(List *orclauses)
Definition: makefuncs.c:655
#define castNode(_type_, nodeptr)
Definition: nodes.h:197
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:469
Selectivity bitmapselectivity
Definition: pathnodes.h:1766
List * bitmapquals
Definition: pathnodes.h:1765
Selectivity bitmapselectivity
Definition: pathnodes.h:1779
List * bitmapquals
Definition: pathnodes.h:1778
List * indexquals
Definition: pathnodes.h:1727
struct RestrictInfo * rinfo
Definition: pathnodes.h:1726
List * indpred
Definition: pathnodes.h:1150
List * indexclauses
Definition: pathnodes.h:1679
Path path
Definition: pathnodes.h:1677
Selectivity indexselectivity
Definition: pathnodes.h:1684
Cost indextotalcost
Definition: pathnodes.h:1683
IndexOptInfo * indexinfo
Definition: pathnodes.h:1678
Scan scan
Definition: plannodes.h:449
List * indexqualorig
Definition: plannodes.h:452
Oid indexid
Definition: plannodes.h:450
List * indexqual
Definition: plannodes.h:451
Index scanrelid
Definition: plannodes.h:387

References Assert(), BitmapAndPath::bitmapquals, BitmapOrPath::bitmapquals, BitmapAndPath::bitmapselectivity, BitmapOrPath::bitmapselectivity, castNode, clamp_row_est(), RestrictInfo::clause, create_indexscan_plan(), elog(), ERROR, get_actual_clauses(), IndexPath::indexclauses, IndexScan::indexid, IndexPath::indexinfo, IndexScan::indexqual, IndexScan::indexqualorig, IndexClause::indexquals, IndexPath::indexselectivity, IndexPath::indextotalcost, IndexOptInfo::indpred, IsA, lappend(), lfirst, linitial, list_concat(), list_concat_unique(), list_length(), list_make1, make_ands_explicit(), make_bitmap_and(), make_bitmap_indexscan(), make_bitmap_or(), make_orclause(), NIL, nodeTag, Path::parallel_safe, IndexPath::path, BitmapAndPath::path, BitmapOrPath::path, plan, predicate_implied_by(), IndexClause::rinfo, IndexScan::scan, Scan::scanrelid, Path::startup_cost, and Path::total_cost.

Referenced by create_bitmap_scan_plan().

◆ create_ctescan_plan()

static CteScan * create_ctescan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3878 of file createplan.c.

3880 {
3881  CteScan *scan_plan;
3882  Index scan_relid = best_path->parent->relid;
3883  RangeTblEntry *rte;
3884  SubPlan *ctesplan = NULL;
3885  int plan_id;
3886  int cte_param_id;
3887  PlannerInfo *cteroot;
3888  Index levelsup;
3889  int ndx;
3890  ListCell *lc;
3891 
3892  Assert(scan_relid > 0);
3893  rte = planner_rt_fetch(scan_relid, root);
3894  Assert(rte->rtekind == RTE_CTE);
3895  Assert(!rte->self_reference);
3896 
3897  /*
3898  * Find the referenced CTE, and locate the SubPlan previously made for it.
3899  */
3900  levelsup = rte->ctelevelsup;
3901  cteroot = root;
3902  while (levelsup-- > 0)
3903  {
3904  cteroot = cteroot->parent_root;
3905  if (!cteroot) /* shouldn't happen */
3906  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3907  }
3908 
3909  /*
3910  * Note: cte_plan_ids can be shorter than cteList, if we are still working
3911  * on planning the CTEs (ie, this is a side-reference from another CTE).
3912  * So we mustn't use forboth here.
3913  */
3914  ndx = 0;
3915  foreach(lc, cteroot->parse->cteList)
3916  {
3917  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
3918 
3919  if (strcmp(cte->ctename, rte->ctename) == 0)
3920  break;
3921  ndx++;
3922  }
3923  if (lc == NULL) /* shouldn't happen */
3924  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
3925  if (ndx >= list_length(cteroot->cte_plan_ids))
3926  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3927  plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
3928  if (plan_id <= 0)
3929  elog(ERROR, "no plan was made for CTE \"%s\"", rte->ctename);
3930  foreach(lc, cteroot->init_plans)
3931  {
3932  ctesplan = (SubPlan *) lfirst(lc);
3933  if (ctesplan->plan_id == plan_id)
3934  break;
3935  }
3936  if (lc == NULL) /* shouldn't happen */
3937  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3938 
3939  /*
3940  * We need the CTE param ID, which is the sole member of the SubPlan's
3941  * setParam list.
3942  */
3943  cte_param_id = linitial_int(ctesplan->setParam);
3944 
3945  /* Sort clauses into best execution order */
3946  scan_clauses = order_qual_clauses(root, scan_clauses);
3947 
3948  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3949  scan_clauses = extract_actual_clauses(scan_clauses, false);
3950 
3951  /* Replace any outer-relation variables with nestloop params */
3952  if (best_path->param_info)
3953  {
3954  scan_clauses = (List *)
3955  replace_nestloop_params(root, (Node *) scan_clauses);
3956  }
3957 
3958  scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
3959  plan_id, cte_param_id);
3960 
3961  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3962 
3963  return scan_plan;
3964 }
static CteScan * make_ctescan(List *qptlist, List *qpqual, Index scanrelid, int ctePlanId, int cteParam)
Definition: createplan.c:5734
@ RTE_CTE
Definition: parsenodes.h:1019
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:555
#define linitial_int(l)
Definition: pg_list.h:179
static int list_nth_int(const List *list, int n)
Definition: pg_list.h:310
Scan scan
Definition: plannodes.h:640
List * cte_plan_ids
Definition: pathnodes.h:302
List * init_plans
Definition: pathnodes.h:296
Query * parse
Definition: pathnodes.h:199
List * cteList
Definition: parsenodes.h:172
char * ctename
Definition: parsenodes.h:1163
bool self_reference
Definition: parsenodes.h:1165
Index ctelevelsup
Definition: parsenodes.h:1164
RTEKind rtekind
Definition: parsenodes.h:1032
int plan_id
Definition: primnodes.h:997
List * setParam
Definition: primnodes.h:1015

References Assert(), copy_generic_path_info(), PlannerInfo::cte_plan_ids, RangeTblEntry::ctelevelsup, Query::cteList, RangeTblEntry::ctename, CommonTableExpr::ctename, elog(), ERROR, extract_actual_clauses(), PlannerInfo::init_plans, lfirst, linitial_int, list_length(), list_nth_int(), make_ctescan(), order_qual_clauses(), PlannerInfo::parse, SubPlan::plan_id, planner_rt_fetch, replace_nestloop_params(), RTE_CTE, RangeTblEntry::rtekind, CteScan::scan, RangeTblEntry::self_reference, and SubPlan::setParam.

Referenced by create_scan_plan().

◆ create_customscan_plan()

static CustomScan * create_customscan_plan ( PlannerInfo root,
CustomPath best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 4264 of file createplan.c.

4266 {
4267  CustomScan *cplan;
4268  RelOptInfo *rel = best_path->path.parent;
4269  List *custom_plans = NIL;
4270  ListCell *lc;
4271 
4272  /* Recursively transform child paths. */
4273  foreach(lc, best_path->custom_paths)
4274  {
4275  Plan *plan = create_plan_recurse(root, (Path *) lfirst(lc),
4276  CP_EXACT_TLIST);
4277 
4278  custom_plans = lappend(custom_plans, plan);
4279  }
4280 
4281  /*
4282  * Sort clauses into the best execution order, although custom-scan
4283  * provider can reorder them again.
4284  */
4285  scan_clauses = order_qual_clauses(root, scan_clauses);
4286 
4287  /*
4288  * Invoke custom plan provider to create the Plan node represented by the
4289  * CustomPath.
4290  */
4291  cplan = castNode(CustomScan,
4292  best_path->methods->PlanCustomPath(root,
4293  rel,
4294  best_path,
4295  tlist,
4296  scan_clauses,
4297  custom_plans));
4298 
4299  /*
4300  * Copy cost data from Path to Plan; no need to make custom-plan providers
4301  * do this
4302  */
4303  copy_generic_path_info(&cplan->scan.plan, &best_path->path);
4304 
4305  /* Likewise, copy the relids that are represented by this custom scan */
4306  cplan->custom_relids = best_path->path.parent->relids;
4307 
4308  /*
4309  * Replace any outer-relation variables with nestloop params in the qual
4310  * and custom_exprs expressions. We do this last so that the custom-plan
4311  * provider doesn't have to be involved. (Note that parts of custom_exprs
4312  * could have come from join clauses, so doing this beforehand on the
4313  * scan_clauses wouldn't work.) We assume custom_scan_tlist contains no
4314  * such variables.
4315  */
4316  if (best_path->path.param_info)
4317  {
4318  cplan->scan.plan.qual = (List *)
4319  replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
4320  cplan->custom_exprs = (List *)
4321  replace_nestloop_params(root, (Node *) cplan->custom_exprs);
4322  }
4323 
4324  return cplan;
4325 }
struct Plan *(* PlanCustomPath)(PlannerInfo *root, RelOptInfo *rel, struct CustomPath *best_path, List *tlist, List *clauses, List *custom_plans)
Definition: extensible.h:97
const struct CustomPathMethods * methods
Definition: pathnodes.h:1879
List * custom_paths
Definition: pathnodes.h:1876
Scan scan
Definition: plannodes.h:739
Bitmapset * custom_relids
Definition: plannodes.h:746
List * custom_exprs
Definition: plannodes.h:743

References castNode, copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), CustomScan::custom_exprs, CustomPath::custom_paths, CustomScan::custom_relids, lappend(), lfirst, CustomPath::methods, NIL, order_qual_clauses(), CustomPath::path, plan, CustomPathMethods::PlanCustomPath, replace_nestloop_params(), and CustomScan::scan.

Referenced by create_scan_plan().

◆ create_foreignscan_plan()

static ForeignScan * create_foreignscan_plan ( PlannerInfo root,
ForeignPath best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 4109 of file createplan.c.

4111 {
4112  ForeignScan *scan_plan;
4113  RelOptInfo *rel = best_path->path.parent;
4114  Index scan_relid = rel->relid;
4115  Oid rel_oid = InvalidOid;
4116  Plan *outer_plan = NULL;
4117 
4118  Assert(rel->fdwroutine != NULL);
4119 
4120  /* transform the child path if any */
4121  if (best_path->fdw_outerpath)
4122  outer_plan = create_plan_recurse(root, best_path->fdw_outerpath,
4123  CP_EXACT_TLIST);
4124 
4125  /*
4126  * If we're scanning a base relation, fetch its OID. (Irrelevant if
4127  * scanning a join relation.)
4128  */
4129  if (scan_relid > 0)
4130  {
4131  RangeTblEntry *rte;
4132 
4133  Assert(rel->rtekind == RTE_RELATION);
4134  rte = planner_rt_fetch(scan_relid, root);
4135  Assert(rte->rtekind == RTE_RELATION);
4136  rel_oid = rte->relid;
4137  }
4138 
4139  /*
4140  * Sort clauses into best execution order. We do this first since the FDW
4141  * might have more info than we do and wish to adjust the ordering.
4142  */
4143  scan_clauses = order_qual_clauses(root, scan_clauses);
4144 
4145  /*
4146  * Let the FDW perform its processing on the restriction clauses and
4147  * generate the plan node. Note that the FDW might remove restriction
4148  * clauses that it intends to execute remotely, or even add more (if it
4149  * has selected some join clauses for remote use but also wants them
4150  * rechecked locally).
4151  */
4152  scan_plan = rel->fdwroutine->GetForeignPlan(root, rel, rel_oid,
4153  best_path,
4154  tlist, scan_clauses,
4155  outer_plan);
4156 
4157  /* Copy cost data from Path to Plan; no need to make FDW do this */
4158  copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
4159 
4160  /* Copy user OID to access as; likewise no need to make FDW do this */
4161  scan_plan->checkAsUser = rel->userid;
4162 
4163  /* Copy foreign server OID; likewise, no need to make FDW do this */
4164  scan_plan->fs_server = rel->serverid;
4165 
4166  /*
4167  * Likewise, copy the relids that are represented by this foreign scan. An
4168  * upper rel doesn't have relids set, but it covers all the relations
4169  * participating in the underlying scan/join, so use root->all_query_rels.
4170  */
4171  if (rel->reloptkind == RELOPT_UPPER_REL)
4172  scan_plan->fs_relids = root->all_query_rels;
4173  else
4174  scan_plan->fs_relids = best_path->path.parent->relids;
4175 
4176  /*
4177  * Join relid sets include relevant outer joins, but FDWs may need to know
4178  * which are the included base rels. That's a bit tedious to get without
4179  * access to the plan-time data structures, so compute it here.
4180  */
4181  scan_plan->fs_base_relids = bms_difference(scan_plan->fs_relids,
4182  root->outer_join_rels);
4183 
4184  /*
4185  * If this is a foreign join, and to make it valid to push down we had to
4186  * assume that the current user is the same as some user explicitly named
4187  * in the query, mark the finished plan as depending on the current user.
4188  */
4189  if (rel->useridiscurrent)
4190  root->glob->dependsOnRole = true;
4191 
4192  /*
4193  * Replace any outer-relation variables with nestloop params in the qual,
4194  * fdw_exprs and fdw_recheck_quals expressions. We do this last so that
4195  * the FDW doesn't have to be involved. (Note that parts of fdw_exprs or
4196  * fdw_recheck_quals could have come from join clauses, so doing this
4197  * beforehand on the scan_clauses wouldn't work.) We assume
4198  * fdw_scan_tlist contains no such variables.
4199  */
4200  if (best_path->path.param_info)
4201  {
4202  scan_plan->scan.plan.qual = (List *)
4203  replace_nestloop_params(root, (Node *) scan_plan->scan.plan.qual);
4204  scan_plan->fdw_exprs = (List *)
4205  replace_nestloop_params(root, (Node *) scan_plan->fdw_exprs);
4206  scan_plan->fdw_recheck_quals = (List *)
4208  (Node *) scan_plan->fdw_recheck_quals);
4209  }
4210 
4211  /*
4212  * If rel is a base relation, detect whether any system columns are
4213  * requested from the rel. (If rel is a join relation, rel->relid will be
4214  * 0, but there can be no Var with relid 0 in the rel's targetlist or the
4215  * restriction clauses, so we skip this in that case. Note that any such
4216  * columns in base relations that were joined are assumed to be contained
4217  * in fdw_scan_tlist.) This is a bit of a kluge and might go away
4218  * someday, so we intentionally leave it out of the API presented to FDWs.
4219  */
4220  scan_plan->fsSystemCol = false;
4221  if (scan_relid > 0)
4222  {
4223  Bitmapset *attrs_used = NULL;
4224  ListCell *lc;
4225  int i;
4226 
4227  /*
4228  * First, examine all the attributes needed for joins or final output.
4229  * Note: we must look at rel's targetlist, not the attr_needed data,
4230  * because attr_needed isn't computed for inheritance child rels.
4231  */
4232  pull_varattnos((Node *) rel->reltarget->exprs, scan_relid, &attrs_used);
4233 
4234  /* Add all the attributes used by restriction clauses. */
4235  foreach(lc, rel->baserestrictinfo)
4236  {
4237  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
4238 
4239  pull_varattnos((Node *) rinfo->clause, scan_relid, &attrs_used);
4240  }
4241 
4242  /* Now, are any system columns requested from rel? */
4243  for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
4244  {
4246  {
4247  scan_plan->fsSystemCol = true;
4248  break;
4249  }
4250  }
4251 
4252  bms_free(attrs_used);
4253  }
4254 
4255  return scan_plan;
4256 }
void bms_free(Bitmapset *a)
Definition: bitmapset.c:194
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:460
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:297
int i
Definition: isn.c:73
@ RELOPT_UPPER_REL
Definition: pathnodes.h:816
#define InvalidOid
Definition: postgres_ext.h:36
Path * fdw_outerpath
Definition: pathnodes.h:1838
Oid checkAsUser
Definition: plannodes.h:710
Oid fs_server
Definition: plannodes.h:712
List * fdw_exprs
Definition: plannodes.h:713
bool fsSystemCol
Definition: plannodes.h:719
Bitmapset * fs_relids
Definition: plannodes.h:717
Bitmapset * fs_base_relids
Definition: plannodes.h:718
List * fdw_recheck_quals
Definition: plannodes.h:716
List * exprs
Definition: pathnodes.h:1501
bool dependsOnRole
Definition: pathnodes.h:150
Relids all_query_rels
Definition: pathnodes.h:266
Relids outer_join_rels
Definition: pathnodes.h:258
PlannerGlobal * glob
Definition: pathnodes.h:202
bool useridiscurrent
Definition: pathnodes.h:947
struct PathTarget * reltarget
Definition: pathnodes.h:878
Index relid
Definition: pathnodes.h:903
RelOptKind reloptkind
Definition: pathnodes.h:850
Oid userid
Definition: pathnodes.h:945
Oid serverid
Definition: pathnodes.h:943
RTEKind rtekind
Definition: pathnodes.h:907
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:291

References PlannerInfo::all_query_rels, Assert(), RelOptInfo::baserestrictinfo, bms_difference(), bms_free(), bms_is_member(), ForeignScan::checkAsUser, RestrictInfo::clause, copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), PlannerGlobal::dependsOnRole, PathTarget::exprs, ForeignScan::fdw_exprs, ForeignPath::fdw_outerpath, ForeignScan::fdw_recheck_quals, FirstLowInvalidHeapAttributeNumber, ForeignScan::fs_base_relids, ForeignScan::fs_relids, ForeignScan::fs_server, ForeignScan::fsSystemCol, PlannerInfo::glob, i, InvalidOid, lfirst, order_qual_clauses(), PlannerInfo::outer_join_rels, ForeignPath::path, planner_rt_fetch, pull_varattnos(), RangeTblEntry::relid, RelOptInfo::relid, RELOPT_UPPER_REL, RelOptInfo::reloptkind, RelOptInfo::reltarget, replace_nestloop_params(), RTE_RELATION, RangeTblEntry::rtekind, RelOptInfo::rtekind, ForeignScan::scan, RelOptInfo::serverid, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by create_scan_plan().

◆ create_functionscan_plan()

static FunctionScan * create_functionscan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3748 of file createplan.c.

3750 {
3751  FunctionScan *scan_plan;
3752  Index scan_relid = best_path->parent->relid;
3753  RangeTblEntry *rte;
3754  List *functions;
3755 
3756  /* it should be a function base rel... */
3757  Assert(scan_relid > 0);
3758  rte = planner_rt_fetch(scan_relid, root);
3759  Assert(rte->rtekind == RTE_FUNCTION);
3760  functions = rte->functions;
3761 
3762  /* Sort clauses into best execution order */
3763  scan_clauses = order_qual_clauses(root, scan_clauses);
3764 
3765  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3766  scan_clauses = extract_actual_clauses(scan_clauses, false);
3767 
3768  /* Replace any outer-relation variables with nestloop params */
3769  if (best_path->param_info)
3770  {
3771  scan_clauses = (List *)
3772  replace_nestloop_params(root, (Node *) scan_clauses);
3773  /* The function expressions could contain nestloop params, too */
3775  }
3776 
3777  scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
3778  functions, rte->funcordinality);
3779 
3780  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3781 
3782  return scan_plan;
3783 }
static FunctionScan * make_functionscan(List *qptlist, List *qpqual, Index scanrelid, List *functions, bool funcordinality)
Definition: createplan.c:5675
@ RTE_FUNCTION
Definition: parsenodes.h:1016
static const struct fns functions
Definition: regcomp.c:356
bool funcordinality
Definition: parsenodes.h:1148
List * functions
Definition: parsenodes.h:1147

References Assert(), copy_generic_path_info(), extract_actual_clauses(), RangeTblEntry::funcordinality, functions, RangeTblEntry::functions, make_functionscan(), order_qual_clauses(), planner_rt_fetch, replace_nestloop_params(), RTE_FUNCTION, RangeTblEntry::rtekind, and FunctionScan::scan.

Referenced by create_scan_plan().

◆ create_gather_merge_plan()

static GatherMerge * create_gather_merge_plan ( PlannerInfo root,
GatherMergePath best_path 
)
static

Definition at line 1955 of file createplan.c.

1956 {
1957  GatherMerge *gm_plan;
1958  Plan *subplan;
1959  List *pathkeys = best_path->path.pathkeys;
1960  List *tlist = build_path_tlist(root, &best_path->path);
1961 
1962  /* As with Gather, project away columns in the workers. */
1963  subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1964 
1965  /* Create a shell for a GatherMerge plan. */
1966  gm_plan = makeNode(GatherMerge);
1967  gm_plan->plan.targetlist = tlist;
1968  gm_plan->num_workers = best_path->num_workers;
1969  copy_generic_path_info(&gm_plan->plan, &best_path->path);
1970 
1971  /* Assign the rescan Param. */
1972  gm_plan->rescan_param = assign_special_exec_param(root);
1973 
1974  /* Gather Merge is pointless with no pathkeys; use Gather instead. */
1975  Assert(pathkeys != NIL);
1976 
1977  /* Compute sort column info, and adjust subplan's tlist as needed */
1978  subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1979  best_path->subpath->parent->relids,
1980  gm_plan->sortColIdx,
1981  false,
1982  &gm_plan->numCols,
1983  &gm_plan->sortColIdx,
1984  &gm_plan->sortOperators,
1985  &gm_plan->collations,
1986  &gm_plan->nullsFirst);
1987 
1988 
1989  /*
1990  * All gather merge paths should have already guaranteed the necessary
1991  * sort order either by adding an explicit sort node or by using presorted
1992  * input. We can't simply add a sort here on additional pathkeys, because
1993  * we can't guarantee the sort would be safe. For example, expressions may
1994  * be volatile or otherwise parallel unsafe.
1995  */
1996  if (!pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys))
1997  elog(ERROR, "gather merge input not sufficiently sorted");
1998 
1999  /* Now insert the subplan under GatherMerge. */
2000  gm_plan->plan.lefttree = subplan;
2001 
2002  /* use parallel mode for parallel plans. */
2003  root->glob->parallelModeNeeded = true;
2004 
2005  return gm_plan;
2006 }
int assign_special_exec_param(PlannerInfo *root)
Definition: paramassign.c:612
int rescan_param
Definition: plannodes.h:1161
int num_workers
Definition: plannodes.h:1158
struct Plan * lefttree
Definition: plannodes.h:155
bool parallelModeNeeded
Definition: pathnodes.h:156

References Assert(), assign_special_exec_param(), build_path_tlist(), copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), elog(), ERROR, PlannerInfo::glob, Plan::lefttree, makeNode, NIL, GatherMergePath::num_workers, GatherMerge::num_workers, GatherMerge::numCols, PlannerGlobal::parallelModeNeeded, GatherMergePath::path, Path::pathkeys, pathkeys_contained_in(), GatherMerge::plan, prepare_sort_from_pathkeys(), GatherMerge::rescan_param, GatherMergePath::subpath, and Plan::targetlist.

Referenced by create_plan_recurse().

◆ create_gather_plan()

static Gather * create_gather_plan ( PlannerInfo root,
GatherPath best_path 
)
static

Definition at line 1917 of file createplan.c.

1918 {
1919  Gather *gather_plan;
1920  Plan *subplan;
1921  List *tlist;
1922 
1923  /*
1924  * Push projection down to the child node. That way, the projection work
1925  * is parallelized, and there can be no system columns in the result (they
1926  * can't travel through a tuple queue because it uses MinimalTuple
1927  * representation).
1928  */
1929  subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1930 
1931  tlist = build_path_tlist(root, &best_path->path);
1932 
1933  gather_plan = make_gather(tlist,
1934  NIL,
1935  best_path->num_workers,
1937  best_path->single_copy,
1938  subplan);
1939 
1940  copy_generic_path_info(&gather_plan->plan, &best_path->path);
1941 
1942  /* use parallel mode for parallel plans. */
1943  root->glob->parallelModeNeeded = true;
1944 
1945  return gather_plan;
1946 }
static Gather * make_gather(List *qptlist, List *qpqual, int nworkers, int rescan_param, bool single_copy, Plan *subplan)
Definition: createplan.c:6826
bool single_copy
Definition: pathnodes.h:2011
Path * subpath
Definition: pathnodes.h:2010
int num_workers
Definition: pathnodes.h:2012
Plan plan
Definition: plannodes.h:1140

References assign_special_exec_param(), build_path_tlist(), copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), PlannerInfo::glob, make_gather(), NIL, GatherPath::num_workers, PlannerGlobal::parallelModeNeeded, GatherPath::path, Gather::plan, GatherPath::single_copy, and GatherPath::subpath.

Referenced by create_plan_recurse().

◆ create_gating_plan()

static Plan * create_gating_plan ( PlannerInfo root,
Path path,
Plan plan,
List gating_quals 
)
static

Definition at line 1020 of file createplan.c.

1022 {
1023  Plan *gplan;
1024  Plan *splan;
1025 
1026  Assert(gating_quals);
1027 
1028  /*
1029  * We might have a trivial Result plan already. Stacking one Result atop
1030  * another is silly, so if that applies, just discard the input plan.
1031  * (We're assuming its targetlist is uninteresting; it should be either
1032  * the same as the result of build_path_tlist, or a simplified version.)
1033  */
1034  splan = plan;
1035  if (IsA(plan, Result))
1036  {
1037  Result *rplan = (Result *) plan;
1038 
1039  if (rplan->plan.lefttree == NULL &&
1040  rplan->resconstantqual == NULL)
1041  splan = NULL;
1042  }
1043 
1044  /*
1045  * Since we need a Result node anyway, always return the path's requested
1046  * tlist; that's never a wrong choice, even if the parent node didn't ask
1047  * for CP_EXACT_TLIST.
1048  */
1049  gplan = (Plan *) make_result(build_path_tlist(root, path),
1050  (Node *) gating_quals,
1051  splan);
1052 
1053  /*
1054  * Notice that we don't change cost or size estimates when doing gating.
1055  * The costs of qual eval were already included in the subplan's cost.
1056  * Leaving the size alone amounts to assuming that the gating qual will
1057  * succeed, which is the conservative estimate for planning upper queries.
1058  * We certainly don't want to assume the output size is zero (unless the
1059  * gating qual is actually constant FALSE, and that case is dealt with in
1060  * clausesel.c). Interpolating between the two cases is silly, because it
1061  * doesn't reflect what will really happen at runtime, and besides which
1062  * in most cases we have only a very bad idea of the probability of the
1063  * gating qual being true.
1064  */
1065  copy_plan_costsize(gplan, plan);
1066 
1067  /* Gating quals could be unsafe, so better use the Path's safety flag */
1068  gplan->parallel_safe = path->parallel_safe;
1069 
1070  return gplan;
1071 }
static void copy_plan_costsize(Plan *dest, Plan *src)
Definition: createplan.c:5396
static SPIPlanPtr splan
Definition: regress.c:270
Node * resconstantqual
Definition: plannodes.h:200
Plan plan
Definition: plannodes.h:199

References Assert(), build_path_tlist(), copy_plan_costsize(), IsA, Plan::lefttree, make_result(), Path::parallel_safe, Plan::parallel_safe, Result::plan, plan, Result::resconstantqual, and splan.

Referenced by create_join_plan(), and create_scan_plan().

◆ create_group_plan()

static Group * create_group_plan ( PlannerInfo root,
GroupPath best_path 
)
static

Definition at line 2239 of file createplan.c.

2240 {
2241  Group *plan;
2242  Plan *subplan;
2243  List *tlist;
2244  List *quals;
2245 
2246  /*
2247  * Group can project, so no need to be terribly picky about child tlist,
2248  * but we do need grouping columns to be available
2249  */
2250  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2251 
2252  tlist = build_path_tlist(root, &best_path->path);
2253 
2254  quals = order_qual_clauses(root, best_path->qual);
2255 
2256  plan = make_group(tlist,
2257  quals,
2258  list_length(best_path->groupClause),
2260  subplan->targetlist),
2261  extract_grouping_ops(best_path->groupClause),
2263  subplan->targetlist),
2264  subplan);
2265 
2266  copy_generic_path_info(&plan->plan, (Path *) best_path);
2267 
2268  return plan;
2269 }
static Group * make_group(List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations, Plan *lefttree)
Definition: createplan.c:6641
List * qual
Definition: pathnodes.h:2196
List * groupClause
Definition: pathnodes.h:2195
Path * subpath
Definition: pathnodes.h:2194
Path path
Definition: pathnodes.h:2193

References build_path_tlist(), copy_generic_path_info(), CP_LABEL_TLIST, create_plan_recurse(), extract_grouping_collations(), extract_grouping_cols(), extract_grouping_ops(), GroupPath::groupClause, list_length(), make_group(), order_qual_clauses(), GroupPath::path, plan, GroupPath::qual, GroupPath::subpath, and Plan::targetlist.

Referenced by create_plan_recurse().

◆ create_group_result_plan()

static Result * create_group_result_plan ( PlannerInfo root,
GroupResultPath best_path 
)
static

Definition at line 1585 of file createplan.c.

1586 {
1587  Result *plan;
1588  List *tlist;
1589  List *quals;
1590 
1591  tlist = build_path_tlist(root, &best_path->path);
1592 
1593  /* best_path->quals is just bare clauses */
1594  quals = order_qual_clauses(root, best_path->quals);
1595 
1596  plan = make_result(tlist, (Node *) quals, NULL);
1597 
1598  copy_generic_path_info(&plan->plan, (Path *) best_path);
1599 
1600  return plan;
1601 }

References build_path_tlist(), copy_generic_path_info(), make_result(), order_qual_clauses(), GroupResultPath::path, plan, and GroupResultPath::quals.

Referenced by create_plan_recurse().

◆ create_groupingsets_plan()

static Plan * create_groupingsets_plan ( PlannerInfo root,
GroupingSetsPath best_path 
)
static

Definition at line 2390 of file createplan.c.

2391 {
2392  Agg *plan;
2393  Plan *subplan;
2394  List *rollups = best_path->rollups;
2395  AttrNumber *grouping_map;
2396  int maxref;
2397  List *chain;
2398  ListCell *lc;
2399 
2400  /* Shouldn't get here without grouping sets */
2401  Assert(root->parse->groupingSets);
2402  Assert(rollups != NIL);
2403 
2404  /*
2405  * Agg can project, so no need to be terribly picky about child tlist, but
2406  * we do need grouping columns to be available
2407  */
2408  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2409 
2410  /*
2411  * Compute the mapping from tleSortGroupRef to column index in the child's
2412  * tlist. First, identify max SortGroupRef in groupClause, for array
2413  * sizing.
2414  */
2415  maxref = 0;
2416  foreach(lc, root->processed_groupClause)
2417  {
2418  SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
2419 
2420  if (gc->tleSortGroupRef > maxref)
2421  maxref = gc->tleSortGroupRef;
2422  }
2423 
2424  grouping_map = (AttrNumber *) palloc0((maxref + 1) * sizeof(AttrNumber));
2425 
2426  /* Now look up the column numbers in the child's tlist */
2427  foreach(lc, root->processed_groupClause)
2428  {
2429  SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
2430  TargetEntry *tle = get_sortgroupclause_tle(gc, subplan->targetlist);
2431 
2432  grouping_map[gc->tleSortGroupRef] = tle->resno;
2433  }
2434 
2435  /*
2436  * During setrefs.c, we'll need the grouping_map to fix up the cols lists
2437  * in GroupingFunc nodes. Save it for setrefs.c to use.
2438  */
2439  Assert(root->grouping_map == NULL);
2440  root->grouping_map = grouping_map;
2441 
2442  /*
2443  * Generate the side nodes that describe the other sort and group
2444  * operations besides the top one. Note that we don't worry about putting
2445  * accurate cost estimates in the side nodes; only the topmost Agg node's
2446  * costs will be shown by EXPLAIN.
2447  */
2448  chain = NIL;
2449  if (list_length(rollups) > 1)
2450  {
2451  bool is_first_sort = ((RollupData *) linitial(rollups))->is_hashed;
2452 
2453  for_each_from(lc, rollups, 1)
2454  {
2455  RollupData *rollup = lfirst(lc);
2456  AttrNumber *new_grpColIdx;
2457  Plan *sort_plan = NULL;
2458  Plan *agg_plan;
2459  AggStrategy strat;
2460 
2461  new_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2462 
2463  if (!rollup->is_hashed && !is_first_sort)
2464  {
2465  sort_plan = (Plan *)
2467  new_grpColIdx,
2468  subplan);
2469  }
2470 
2471  if (!rollup->is_hashed)
2472  is_first_sort = false;
2473 
2474  if (rollup->is_hashed)
2475  strat = AGG_HASHED;
2476  else if (linitial(rollup->gsets) == NIL)
2477  strat = AGG_PLAIN;
2478  else
2479  strat = AGG_SORTED;
2480 
2481  agg_plan = (Plan *) make_agg(NIL,
2482  NIL,
2483  strat,
2485  list_length((List *) linitial(rollup->gsets)),
2486  new_grpColIdx,
2489  rollup->gsets,
2490  NIL,
2491  rollup->numGroups,
2492  best_path->transitionSpace,
2493  sort_plan);
2494 
2495  /*
2496  * Remove stuff we don't need to avoid bloating debug output.
2497  */
2498  if (sort_plan)
2499  {
2500  sort_plan->targetlist = NIL;
2501  sort_plan->lefttree = NULL;
2502  }
2503 
2504  chain = lappend(chain, agg_plan);
2505  }
2506  }
2507 
2508  /*
2509  * Now make the real Agg node
2510  */
2511  {
2512  RollupData *rollup = linitial(rollups);
2513  AttrNumber *top_grpColIdx;
2514  int numGroupCols;
2515 
2516  top_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2517 
2518  numGroupCols = list_length((List *) linitial(rollup->gsets));
2519 
2520  plan = make_agg(build_path_tlist(root, &best_path->path),
2521  best_path->qual,
2522  best_path->aggstrategy,
2524  numGroupCols,
2525  top_grpColIdx,
2528  rollup->gsets,
2529  chain,
2530  rollup->numGroups,
2531  best_path->transitionSpace,
2532  subplan);
2533 
2534  /* Copy cost data from Path to Plan */
2535  copy_generic_path_info(&plan->plan, &best_path->path);
2536  }
2537 
2538  return (Plan *) plan;
2539 }
static AttrNumber * remap_groupColIdx(PlannerInfo *root, List *groupClause)
Definition: createplan.c:2352
static Sort * make_sort_from_groupcols(List *groupcls, AttrNumber *grpColIdx, Plan *lefttree)
Definition: createplan.c:6436
void * palloc0(Size size)
Definition: mcxt.c:1257
AggStrategy
Definition: nodes.h:363
@ AGG_SORTED
Definition: nodes.h:365
@ AGG_HASHED
Definition: nodes.h:366
@ AGG_PLAIN
Definition: nodes.h:364
@ AGGSPLIT_SIMPLE
Definition: nodes.h:387
#define for_each_from(cell, lst, N)
Definition: pg_list.h:414
uint64 transitionSpace
Definition: pathnodes.h:2268
AggStrategy aggstrategy
Definition: pathnodes.h:2265
List * processed_groupClause
Definition: pathnodes.h:430
List * groupingSets
Definition: parsenodes.h:200
Cardinality numGroups
Definition: pathnodes.h:2252
List * groupClause
Definition: pathnodes.h:2249
List * gsets
Definition: pathnodes.h:2250
bool is_hashed
Definition: pathnodes.h:2254
Index tleSortGroupRef
Definition: parsenodes.h:1392
AttrNumber resno
Definition: primnodes.h:1897
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:367

References AGG_HASHED, AGG_PLAIN, AGG_SORTED, AGGSPLIT_SIMPLE, GroupingSetsPath::aggstrategy, Assert(), build_path_tlist(), copy_generic_path_info(), CP_LABEL_TLIST, create_plan_recurse(), extract_grouping_collations(), extract_grouping_ops(), for_each_from, get_sortgroupclause_tle(), RollupData::groupClause, Query::groupingSets, RollupData::gsets, RollupData::is_hashed, lappend(), Plan::lefttree, lfirst, linitial, list_length(), make_agg(), make_sort_from_groupcols(), NIL, RollupData::numGroups, palloc0(), PlannerInfo::parse, GroupingSetsPath::path, plan, PlannerInfo::processed_groupClause, GroupingSetsPath::qual, remap_groupColIdx(), TargetEntry::resno, GroupingSetsPath::rollups, GroupingSetsPath::subpath, Plan::targetlist, SortGroupClause::tleSortGroupRef, and GroupingSetsPath::transitionSpace.

Referenced by create_plan_recurse().

◆ create_hashjoin_plan()

static HashJoin * create_hashjoin_plan ( PlannerInfo root,
HashPath best_path 
)
static

Definition at line 4718 of file createplan.c.

4720 {
4721  HashJoin *join_plan;
4722  Hash *hash_plan;
4723  Plan *outer_plan;
4724  Plan *inner_plan;
4725  List *tlist = build_path_tlist(root, &best_path->jpath.path);
4726  List *joinclauses;
4727  List *otherclauses;
4728  List *hashclauses;
4729  List *hashoperators = NIL;
4730  List *hashcollations = NIL;
4731  List *inner_hashkeys = NIL;
4732  List *outer_hashkeys = NIL;
4733  Oid skewTable = InvalidOid;
4734  AttrNumber skewColumn = InvalidAttrNumber;
4735  bool skewInherit = false;
4736  ListCell *lc;
4737 
4738  /*
4739  * HashJoin can project, so we don't have to demand exact tlists from the
4740  * inputs. However, it's best to request a small tlist from the inner
4741  * side, so that we aren't storing more data than necessary. Likewise, if
4742  * we anticipate batching, request a small tlist from the outer side so
4743  * that we don't put extra data in the outer batch files.
4744  */
4745  outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4746  (best_path->num_batches > 1) ? CP_SMALL_TLIST : 0);
4747 
4748  inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4749  CP_SMALL_TLIST);
4750 
4751  /* Sort join qual clauses into best execution order */
4752  joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4753  /* There's no point in sorting the hash clauses ... */
4754 
4755  /* Get the join qual clauses (in plain expression form) */
4756  /* Any pseudoconstant clauses are ignored here */
4757  if (IS_OUTER_JOIN(best_path->jpath.jointype))
4758  {
4759  extract_actual_join_clauses(joinclauses,
4760  best_path->jpath.path.parent->relids,
4761  &joinclauses, &otherclauses);
4762  }
4763  else
4764  {
4765  /* We can treat all clauses alike for an inner join */
4766  joinclauses = extract_actual_clauses(joinclauses, false);
4767  otherclauses = NIL;
4768  }
4769 
4770  /*
4771  * Remove the hashclauses from the list of join qual clauses, leaving the
4772  * list of quals that must be checked as qpquals.
4773  */
4774  hashclauses = get_actual_clauses(best_path->path_hashclauses);
4775  joinclauses = list_difference(joinclauses, hashclauses);
4776 
4777  /*
4778  * Replace any outer-relation variables with nestloop params. There
4779  * should not be any in the hashclauses.
4780  */
4781  if (best_path->jpath.path.param_info)
4782  {
4783  joinclauses = (List *)
4784  replace_nestloop_params(root, (Node *) joinclauses);
4785  otherclauses = (List *)
4786  replace_nestloop_params(root, (Node *) otherclauses);
4787  }
4788 
4789  /*
4790  * Rearrange hashclauses, if needed, so that the outer variable is always
4791  * on the left.
4792  */
4793  hashclauses = get_switched_clauses(best_path->path_hashclauses,
4794  best_path->jpath.outerjoinpath->parent->relids);
4795 
4796  /*
4797  * If there is a single join clause and we can identify the outer variable
4798  * as a simple column reference, supply its identity for possible use in
4799  * skew optimization. (Note: in principle we could do skew optimization
4800  * with multiple join clauses, but we'd have to be able to determine the
4801  * most common combinations of outer values, which we don't currently have
4802  * enough stats for.)
4803  */
4804  if (list_length(hashclauses) == 1)
4805  {
4806  OpExpr *clause = (OpExpr *) linitial(hashclauses);
4807  Node *node;
4808 
4809  Assert(is_opclause(clause));
4810  node = (Node *) linitial(clause->args);
4811  if (IsA(node, RelabelType))
4812  node = (Node *) ((RelabelType *) node)->arg;
4813  if (IsA(node, Var))
4814  {
4815  Var *var = (Var *) node;
4816  RangeTblEntry *rte;
4817 
4818  rte = root->simple_rte_array[var->varno];
4819  if (rte->rtekind == RTE_RELATION)
4820  {
4821  skewTable = rte->relid;
4822  skewColumn = var->varattno;
4823  skewInherit = rte->inh;
4824  }
4825  }
4826  }
4827 
4828  /*
4829  * Collect hash related information. The hashed expressions are
4830  * deconstructed into outer/inner expressions, so they can be computed
4831  * separately (inner expressions are used to build the hashtable via Hash,
4832  * outer expressions to perform lookups of tuples from HashJoin's outer
4833  * plan in the hashtable). Also collect operator information necessary to
4834  * build the hashtable.
4835  */
4836  foreach(lc, hashclauses)
4837  {
4838  OpExpr *hclause = lfirst_node(OpExpr, lc);
4839 
4840  hashoperators = lappend_oid(hashoperators, hclause->opno);
4841  hashcollations = lappend_oid(hashcollations, hclause->inputcollid);
4842  outer_hashkeys = lappend(outer_hashkeys, linitial(hclause->args));
4843  inner_hashkeys = lappend(inner_hashkeys, lsecond(hclause->args));
4844  }
4845 
4846  /*
4847  * Build the hash node and hash join node.
4848  */
4849  hash_plan = make_hash(inner_plan,
4850  inner_hashkeys,
4851  skewTable,
4852  skewColumn,
4853  skewInherit);
4854 
4855  /*
4856  * Set Hash node's startup & total costs equal to total cost of input
4857  * plan; this only affects EXPLAIN display not decisions.
4858  */
4859  copy_plan_costsize(&hash_plan->plan, inner_plan);
4860  hash_plan->plan.startup_cost = hash_plan->plan.total_cost;
4861 
4862  /*
4863  * If parallel-aware, the executor will also need an estimate of the total
4864  * number of rows expected from all participants so that it can size the
4865  * shared hash table.
4866  */
4867  if (best_path->jpath.path.parallel_aware)
4868  {
4869  hash_plan->plan.parallel_aware = true;
4870  hash_plan->rows_total = best_path->inner_rows_total;
4871  }
4872 
4873  join_plan = make_hashjoin(tlist,
4874  joinclauses,
4875  otherclauses,
4876  hashclauses,
4877  hashoperators,
4878  hashcollations,
4879  outer_hashkeys,
4880  outer_plan,
4881  (Plan *) hash_plan,
4882  best_path->jpath.jointype,
4883  best_path->jpath.inner_unique);
4884 
4885  copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4886 
4887  return join_plan;
4888 }
#define InvalidAttrNumber
Definition: attnum.h:23
static HashJoin * make_hashjoin(List *tlist, List *joinclauses, List *otherclauses, List *hashclauses, List *hashoperators, List *hashcollations, List *hashkeys, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
Definition: createplan.c:5945
static List * get_switched_clauses(List *clauses, Relids outerrelids)
Definition: createplan.c:5210
static Hash * make_hash(Plan *lefttree, List *hashkeys, Oid skewTable, AttrNumber skewColumn, bool skewInherit)
Definition: createplan.c:5976
List * lappend_oid(List *list, Oid datum)
Definition: list.c:374
List * list_difference(const List *list1, const List *list2)
Definition: list.c:1236
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:74
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:348
#define lsecond(l)
Definition: pg_list.h:183
void extract_actual_join_clauses(List *restrictinfo_list, Relids joinrelids, List **joinquals, List **otherquals)
Definition: restrictinfo.c:522
Join join
Definition: plannodes.h:862
List * path_hashclauses
Definition: pathnodes.h:2120
Cardinality inner_rows_total
Definition: pathnodes.h:2122
int num_batches
Definition: pathnodes.h:2121
JoinPath jpath
Definition: pathnodes.h:2119
Cardinality rows_total
Definition: plannodes.h:1208
Plan plan
Definition: plannodes.h:1197
Path * outerjoinpath
Definition: pathnodes.h:2042
Path * innerjoinpath
Definition: pathnodes.h:2043
JoinType jointype
Definition: pathnodes.h:2037
bool inner_unique
Definition: pathnodes.h:2039
List * joinrestrictinfo
Definition: pathnodes.h:2045
Oid opno
Definition: primnodes.h:745
List * args
Definition: primnodes.h:763
bool parallel_aware
Definition: plannodes.h:141
Definition: primnodes.h:226
AttrNumber varattno
Definition: primnodes.h:238
int varno
Definition: primnodes.h:233

References OpExpr::args, Assert(), build_path_tlist(), copy_generic_path_info(), copy_plan_costsize(), CP_SMALL_TLIST, create_plan_recurse(), extract_actual_clauses(), extract_actual_join_clauses(), get_actual_clauses(), get_switched_clauses(), RangeTblEntry::inh, HashPath::inner_rows_total, JoinPath::inner_unique, JoinPath::innerjoinpath, InvalidAttrNumber, InvalidOid, is_opclause(), IS_OUTER_JOIN, IsA, HashJoin::join, JoinPath::joinrestrictinfo, JoinPath::jointype, HashPath::jpath, lappend(), lappend_oid(), lfirst_node, linitial, list_difference(), list_length(), lsecond, make_hash(), make_hashjoin(), NIL, HashPath::num_batches, OpExpr::opno, order_qual_clauses(), JoinPath::outerjoinpath, Plan::parallel_aware, HashPath::path_hashclauses, Hash::plan, RangeTblEntry::relid, replace_nestloop_params(), Hash::rows_total, RTE_RELATION, RangeTblEntry::rtekind, Plan::startup_cost, Plan::total_cost, Var::varattno, and Var::varno.

Referenced by create_join_plan().

◆ create_incrementalsort_plan()

static IncrementalSort * create_incrementalsort_plan ( PlannerInfo root,
IncrementalSortPath best_path,
int  flags 
)
static

Definition at line 2212 of file createplan.c.

2214 {
2216  Plan *subplan;
2217 
2218  /* See comments in create_sort_plan() above */
2219  subplan = create_plan_recurse(root, best_path->spath.subpath,
2220  flags | CP_SMALL_TLIST);
2222  best_path->spath.path.pathkeys,
2223  IS_OTHER_REL(best_path->spath.subpath->parent) ?
2224  best_path->spath.path.parent->relids : NULL,
2225  best_path->nPresortedCols);
2226 
2227  copy_generic_path_info(&plan->sort.plan, (Path *) best_path);
2228 
2229  return plan;
2230 }
static IncrementalSort * make_incrementalsort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids, int nPresortedCols)
Definition: createplan.c:6353
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:839
Path path
Definition: pathnodes.h:2167
Path * subpath
Definition: pathnodes.h:2168

References copy_generic_path_info(), CP_SMALL_TLIST, create_plan_recurse(), IS_OTHER_REL, make_incrementalsort_from_pathkeys(), IncrementalSortPath::nPresortedCols, SortPath::path, Path::pathkeys, plan, IncrementalSortPath::spath, and SortPath::subpath.

Referenced by create_plan_recurse().

◆ create_indexscan_plan()

static Scan * create_indexscan_plan ( PlannerInfo root,
IndexPath best_path,
List tlist,
List scan_clauses,
bool  indexonly 
)
static

Definition at line 3002 of file createplan.c.

3007 {
3008  Scan *scan_plan;
3009  List *indexclauses = best_path->indexclauses;
3010  List *indexorderbys = best_path->indexorderbys;
3011  Index baserelid = best_path->path.parent->relid;
3012  IndexOptInfo *indexinfo = best_path->indexinfo;
3013  Oid indexoid = indexinfo->indexoid;
3014  List *qpqual;
3015  List *stripped_indexquals;
3016  List *fixed_indexquals;
3017  List *fixed_indexorderbys;
3018  List *indexorderbyops = NIL;
3019  ListCell *l;
3020 
3021  /* it should be a base rel... */
3022  Assert(baserelid > 0);
3023  Assert(best_path->path.parent->rtekind == RTE_RELATION);
3024  /* check the scan direction is valid */
3025  Assert(best_path->indexscandir == ForwardScanDirection ||
3026  best_path->indexscandir == BackwardScanDirection);
3027 
3028  /*
3029  * Extract the index qual expressions (stripped of RestrictInfos) from the
3030  * IndexClauses list, and prepare a copy with index Vars substituted for
3031  * table Vars. (This step also does replace_nestloop_params on the
3032  * fixed_indexquals.)
3033  */
3034  fix_indexqual_references(root, best_path,
3035  &stripped_indexquals,
3036  &fixed_indexquals);
3037 
3038  /*
3039  * Likewise fix up index attr references in the ORDER BY expressions.
3040  */
3041  fixed_indexorderbys = fix_indexorderby_references(root, best_path);
3042 
3043  /*
3044  * The qpqual list must contain all restrictions not automatically handled
3045  * by the index, other than pseudoconstant clauses which will be handled
3046  * by a separate gating plan node. All the predicates in the indexquals
3047  * will be checked (either by the index itself, or by nodeIndexscan.c),
3048  * but if there are any "special" operators involved then they must be
3049  * included in qpqual. The upshot is that qpqual must contain
3050  * scan_clauses minus whatever appears in indexquals.
3051  *
3052  * is_redundant_with_indexclauses() detects cases where a scan clause is
3053  * present in the indexclauses list or is generated from the same
3054  * EquivalenceClass as some indexclause, and is therefore redundant with
3055  * it, though not equal. (The latter happens when indxpath.c prefers a
3056  * different derived equality than what generate_join_implied_equalities
3057  * picked for a parameterized scan's ppi_clauses.) Note that it will not
3058  * match to lossy index clauses, which is critical because we have to
3059  * include the original clause in qpqual in that case.
3060  *
3061  * In some situations (particularly with OR'd index conditions) we may
3062  * have scan_clauses that are not equal to, but are logically implied by,
3063  * the index quals; so we also try a predicate_implied_by() check to see
3064  * if we can discard quals that way. (predicate_implied_by assumes its
3065  * first input contains only immutable functions, so we have to check
3066  * that.)
3067  *
3068  * Note: if you change this bit of code you should also look at
3069  * extract_nonindex_conditions() in costsize.c.
3070  */
3071  qpqual = NIL;
3072  foreach(l, scan_clauses)
3073  {
3074  RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
3075 
3076  if (rinfo->pseudoconstant)
3077  continue; /* we may drop pseudoconstants here */
3078  if (is_redundant_with_indexclauses(rinfo, indexclauses))
3079  continue; /* dup or derived from same EquivalenceClass */
3080  if (!contain_mutable_functions((Node *) rinfo->clause) &&
3081  predicate_implied_by(list_make1(rinfo->clause), stripped_indexquals,
3082  false))
3083  continue; /* provably implied by indexquals */
3084  qpqual = lappend(qpqual, rinfo);
3085  }
3086 
3087  /* Sort clauses into best execution order */
3088  qpqual = order_qual_clauses(root, qpqual);
3089 
3090  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3091  qpqual = extract_actual_clauses(qpqual, false);
3092 
3093  /*
3094  * We have to replace any outer-relation variables with nestloop params in
3095  * the indexqualorig, qpqual, and indexorderbyorig expressions. A bit
3096  * annoying to have to do this separately from the processing in
3097  * fix_indexqual_references --- rethink this when generalizing the inner
3098  * indexscan support. But note we can't really do this earlier because
3099  * it'd break the comparisons to predicates above ... (or would it? Those
3100  * wouldn't have outer refs)
3101  */
3102  if (best_path->path.param_info)
3103  {
3104  stripped_indexquals = (List *)
3105  replace_nestloop_params(root, (Node *) stripped_indexquals);
3106  qpqual = (List *)
3107  replace_nestloop_params(root, (Node *) qpqual);
3108  indexorderbys = (List *)
3109  replace_nestloop_params(root, (Node *) indexorderbys);
3110  }
3111 
3112  /*
3113  * If there are ORDER BY expressions, look up the sort operators for their
3114  * result datatypes.
3115  */
3116  if (indexorderbys)
3117  {
3118  ListCell *pathkeyCell,
3119  *exprCell;
3120 
3121  /*
3122  * PathKey contains OID of the btree opfamily we're sorting by, but
3123  * that's not quite enough because we need the expression's datatype
3124  * to look up the sort operator in the operator family.
3125  */
3126  Assert(list_length(best_path->path.pathkeys) == list_length(indexorderbys));
3127  forboth(pathkeyCell, best_path->path.pathkeys, exprCell, indexorderbys)
3128  {
3129  PathKey *pathkey = (PathKey *) lfirst(pathkeyCell);
3130  Node *expr = (Node *) lfirst(exprCell);
3131  Oid exprtype = exprType(expr);
3132  Oid sortop;
3133 
3134  /* Get sort operator from opfamily */
3135  sortop = get_opfamily_member(pathkey->pk_opfamily,
3136  exprtype,
3137  exprtype,
3138  pathkey->pk_strategy);
3139  if (!OidIsValid(sortop))
3140  elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
3141  pathkey->pk_strategy, exprtype, exprtype, pathkey->pk_opfamily);
3142  indexorderbyops = lappend_oid(indexorderbyops, sortop);
3143  }
3144  }
3145 
3146  /*
3147  * For an index-only scan, we must mark indextlist entries as resjunk if
3148  * they are columns that the index AM can't return; this cues setrefs.c to
3149  * not generate references to those columns.
3150  */
3151  if (indexonly)
3152  {
3153  int i = 0;
3154 
3155  foreach(l, indexinfo->indextlist)
3156  {
3157  TargetEntry *indextle = (TargetEntry *) lfirst(l);
3158 
3159  indextle->resjunk = !indexinfo->canreturn[i];
3160  i++;
3161  }
3162  }
3163 
3164  /* Finally ready to build the plan node */
3165  if (indexonly)
3166  scan_plan = (Scan *) make_indexonlyscan(tlist,
3167  qpqual,
3168  baserelid,
3169  indexoid,
3170  fixed_indexquals,
3171  stripped_indexquals,
3172  fixed_indexorderbys,
3173  indexinfo->indextlist,
3174  best_path->indexscandir);
3175  else
3176  scan_plan = (Scan *) make_indexscan(tlist,
3177  qpqual,
3178  baserelid,
3179  indexoid,
3180  fixed_indexquals,
3181  stripped_indexquals,
3182  fixed_indexorderbys,
3183  indexorderbys,
3184  indexorderbyops,
3185  best_path->indexscandir);
3186 
3187  copy_generic_path_info(&scan_plan->plan, &best_path->path);
3188 
3189  return scan_plan;
3190 }
#define OidIsValid(objectId)
Definition: c.h:764
static void fix_indexqual_references(PlannerInfo *root, IndexPath *index_path, List **stripped_indexquals_p, List **fixed_indexquals_p)
Definition: createplan.c:4994
static List * fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path)
Definition: createplan.c:5035
static IndexOnlyScan * make_indexonlyscan(List *qptlist, List *qpqual, Index scanrelid, Oid indexid, List *indexqual, List *recheckqual, List *indexorderby, List *indextlist, ScanDirection indexscandir)
Definition: createplan.c:5547
static IndexScan * make_indexscan(List *qptlist, List *qpqual, Index scanrelid, Oid indexid, List *indexqual, List *indexqualorig, List *indexorderby, List *indexorderbyorig, List *indexorderbyops, ScanDirection indexscandir)
Definition: createplan.c:5516
bool is_redundant_with_indexclauses(RestrictInfo *rinfo, List *indexclauses)
Definition: equivclass.c:3221
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:165
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:43
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:467
@ BackwardScanDirection
Definition: sdir.h:26
@ ForwardScanDirection
Definition: sdir.h:28
List * indextlist
Definition: pathnodes.h:1153
ScanDirection indexscandir
Definition: pathnodes.h:1682
List * indexorderbys
Definition: pathnodes.h:1680
int pk_strategy
Definition: pathnodes.h:1455
Oid pk_opfamily
Definition: pathnodes.h:1454

References Assert(), BackwardScanDirection, RestrictInfo::clause, contain_mutable_functions(), copy_generic_path_info(), elog(), ERROR, exprType(), extract_actual_clauses(), fix_indexorderby_references(), fix_indexqual_references(), forboth, ForwardScanDirection, get_opfamily_member(), i, IndexPath::indexclauses, IndexPath::indexinfo, IndexOptInfo::indexoid, IndexPath::indexorderbys, IndexPath::indexscandir, IndexOptInfo::indextlist, is_redundant_with_indexclauses(), lappend(), lappend_oid(), lfirst, lfirst_node, list_length(), list_make1, make_indexonlyscan(), make_indexscan(), NIL, OidIsValid, order_qual_clauses(), IndexPath::path, Path::pathkeys, PathKey::pk_opfamily, PathKey::pk_strategy, predicate_implied_by(), replace_nestloop_params(), and RTE_RELATION.

Referenced by create_bitmap_subplan(), and create_scan_plan().

◆ create_join_plan()

static Plan * create_join_plan ( PlannerInfo root,
JoinPath best_path 
)
static

Definition at line 1079 of file createplan.c.

1080 {
1081  Plan *plan;
1082  List *gating_clauses;
1083 
1084  switch (best_path->path.pathtype)
1085  {
1086  case T_MergeJoin:
1087  plan = (Plan *) create_mergejoin_plan(root,
1088  (MergePath *) best_path);
1089  break;
1090  case T_HashJoin:
1091  plan = (Plan *) create_hashjoin_plan(root,
1092  (HashPath *) best_path);
1093  break;
1094  case T_NestLoop:
1095  plan = (Plan *) create_nestloop_plan(root,
1096  (NestPath *) best_path);
1097  break;
1098  default:
1099  elog(ERROR, "unrecognized node type: %d",
1100  (int) best_path->path.pathtype);
1101  plan = NULL; /* keep compiler quiet */
1102  break;
1103  }
1104 
1105  /*
1106  * If there are any pseudoconstant clauses attached to this node, insert a
1107  * gating Result node that evaluates the pseudoconstants as one-time
1108  * quals.
1109  */
1110  gating_clauses = get_gating_quals(root, best_path->joinrestrictinfo);
1111  if (gating_clauses)
1112  plan = create_gating_plan(root, (Path *) best_path, plan,
1113  gating_clauses);
1114 
1115 #ifdef NOT_USED
1116 
1117  /*
1118  * * Expensive function pullups may have pulled local predicates * into
1119  * this path node. Put them in the qpqual of the plan node. * JMH,
1120  * 6/15/92
1121  */
1122  if (get_loc_restrictinfo(best_path) != NIL)
1123  set_qpqual((Plan) plan,
1124  list_concat(get_qpqual((Plan) plan),
1125  get_actual_clauses(get_loc_restrictinfo(best_path))));
1126 #endif
1127 
1128  return plan;
1129 }
static HashJoin * create_hashjoin_plan(PlannerInfo *root, HashPath *best_path)
Definition: createplan.c:4718
static MergeJoin * create_mergejoin_plan(PlannerInfo *root, MergePath *best_path)
Definition: createplan.c:4411
static Plan * create_gating_plan(PlannerInfo *root, Path *path, Plan *plan, List *gating_quals)
Definition: createplan.c:1020
static List * get_gating_quals(PlannerInfo *root, List *quals)
Definition: createplan.c:1000
static NestLoop * create_nestloop_plan(PlannerInfo *root, NestPath *best_path)
Definition: createplan.c:4335

References create_gating_plan(), create_hashjoin_plan(), create_mergejoin_plan(), create_nestloop_plan(), elog(), ERROR, get_actual_clauses(), get_gating_quals(), JoinPath::joinrestrictinfo, list_concat(), NIL, and plan.

Referenced by create_plan_recurse().

◆ create_limit_plan()

static Limit * create_limit_plan ( PlannerInfo root,
LimitPath best_path,
int  flags 
)
static

Definition at line 2852 of file createplan.c.

2853 {
2854  Limit *plan;
2855  Plan *subplan;
2856  int numUniqkeys = 0;
2857  AttrNumber *uniqColIdx = NULL;
2858  Oid *uniqOperators = NULL;
2859  Oid *uniqCollations = NULL;
2860 
2861  /* Limit doesn't project, so tlist requirements pass through */
2862  subplan = create_plan_recurse(root, best_path->subpath, flags);
2863 
2864  /* Extract information necessary for comparing rows for WITH TIES. */
2865  if (best_path->limitOption == LIMIT_OPTION_WITH_TIES)
2866  {
2867  Query *parse = root->parse;
2868  ListCell *l;
2869 
2870  numUniqkeys = list_length(parse->sortClause);
2871  uniqColIdx = (AttrNumber *) palloc(numUniqkeys * sizeof(AttrNumber));
2872  uniqOperators = (Oid *) palloc(numUniqkeys * sizeof(Oid));
2873  uniqCollations = (Oid *) palloc(numUniqkeys * sizeof(Oid));
2874 
2875  numUniqkeys = 0;
2876  foreach(l, parse->sortClause)
2877  {
2878  SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
2879  TargetEntry *tle = get_sortgroupclause_tle(sortcl, parse->targetList);
2880 
2881  uniqColIdx[numUniqkeys] = tle->resno;
2882  uniqOperators[numUniqkeys] = sortcl->eqop;
2883  uniqCollations[numUniqkeys] = exprCollation((Node *) tle->expr);
2884  numUniqkeys++;
2885  }
2886  }
2887 
2888  plan = make_limit(subplan,
2889  best_path->limitOffset,
2890  best_path->limitCount,
2891  best_path->limitOption,
2892  numUniqkeys, uniqColIdx, uniqOperators, uniqCollations);
2893 
2894  copy_generic_path_info(&plan->plan, (Path *) best_path);
2895 
2896  return plan;
2897 }
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount, LimitOption limitOption, int uniqNumCols, AttrNumber *uniqColIdx, Oid *uniqOperators, Oid *uniqCollations)
Definition: createplan.c:6932
void * palloc(Size size)
Definition: mcxt.c:1226
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:786
@ LIMIT_OPTION_WITH_TIES
Definition: nodes.h:442
static struct subre * parse(struct vars *v, int stopper, int type, struct state *init, struct state *final)
Definition: regcomp.c:715
Path * subpath
Definition: pathnodes.h:2366
LimitOption limitOption
Definition: pathnodes.h:2369
Node * limitOffset
Definition: pathnodes.h:2367
Node * limitCount
Definition: pathnodes.h:2368
Expr * expr
Definition: primnodes.h:1895

References copy_generic_path_info(), create_plan_recurse(), SortGroupClause::eqop, TargetEntry::expr, exprCollation(), get_sortgroupclause_tle(), lfirst, LIMIT_OPTION_WITH_TIES, LimitPath::limitCount, LimitPath::limitOffset, LimitPath::limitOption, list_length(), make_limit(), palloc(), parse(), PlannerInfo::parse, plan, TargetEntry::resno, and LimitPath::subpath.

Referenced by create_plan_recurse().

◆ create_lockrows_plan()

static LockRows * create_lockrows_plan ( PlannerInfo root,
LockRowsPath best_path,
int  flags 
)
static

Definition at line 2789 of file createplan.c.

2791 {
2792  LockRows *plan;
2793  Plan *subplan;
2794 
2795  /* LockRows doesn't project, so tlist requirements pass through */
2796  subplan = create_plan_recurse(root, best_path->subpath, flags);
2797 
2798  plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
2799 
2800  copy_generic_path_info(&plan->plan, (Path *) best_path);
2801 
2802  return plan;
2803 }
static LockRows * make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
Definition: createplan.c:6911
Path * subpath
Definition: pathnodes.h:2328
List * rowMarks
Definition: pathnodes.h:2329

References copy_generic_path_info(), create_plan_recurse(), LockRowsPath::epqParam, make_lockrows(), plan, LockRowsPath::rowMarks, and LockRowsPath::subpath.

Referenced by create_plan_recurse().

◆ create_material_plan()

static Material * create_material_plan ( PlannerInfo root,
MaterialPath best_path,
int  flags 
)
static

Definition at line 1636 of file createplan.c.

1637 {
1638  Material *plan;
1639  Plan *subplan;
1640 
1641  /*
1642  * We don't want any excess columns in the materialized tuples, so request
1643  * a smaller tlist. Otherwise, since Material doesn't project, tlist
1644  * requirements pass through.
1645  */
1646  subplan = create_plan_recurse(root, best_path->subpath,
1647  flags | CP_SMALL_TLIST);
1648 
1649  plan = make_material(subplan);
1650 
1651  copy_generic_path_info(&plan->plan, (Path *) best_path);
1652 
1653  return plan;
1654 }
static Material * make_material(Plan *lefttree)
Definition: createplan.c:6477
Path * subpath
Definition: pathnodes.h:1950

References copy_generic_path_info(), CP_SMALL_TLIST, create_plan_recurse(), make_material(), plan, and MaterialPath::subpath.

Referenced by create_plan_recurse().

◆ create_memoize_plan()

static Memoize * create_memoize_plan ( PlannerInfo root,
MemoizePath best_path,
int  flags 
)
static

Definition at line 1664 of file createplan.c.

1665 {
1666  Memoize *plan;
1667  Bitmapset *keyparamids;
1668  Plan *subplan;
1669  Oid *operators;
1670  Oid *collations;
1671  List *param_exprs = NIL;
1672  ListCell *lc;
1673  ListCell *lc2;
1674  int nkeys;
1675  int i;
1676 
1677  subplan = create_plan_recurse(root, best_path->subpath,
1678  flags | CP_SMALL_TLIST);
1679 
1680  param_exprs = (List *) replace_nestloop_params(root, (Node *)
1681  best_path->param_exprs);
1682 
1683  nkeys = list_length(param_exprs);
1684  Assert(nkeys > 0);
1685  operators = palloc(nkeys * sizeof(Oid));
1686  collations = palloc(nkeys * sizeof(Oid));
1687 
1688  i = 0;
1689  forboth(lc, param_exprs, lc2, best_path->hash_operators)
1690  {
1691  Expr *param_expr = (Expr *) lfirst(lc);
1692  Oid opno = lfirst_oid(lc2);
1693 
1694  operators[i] = opno;
1695  collations[i] = exprCollation((Node *) param_expr);
1696  i++;
1697  }
1698 
1699  keyparamids = pull_paramids((Expr *) param_exprs);
1700 
1701  plan = make_memoize(subplan, operators, collations, param_exprs,
1702  best_path->singlerow, best_path->binary_mode,
1703  best_path->est_entries, keyparamids);
1704 
1705  copy_generic_path_info(&plan->plan, (Path *) best_path);
1706 
1707  return plan;
1708 }
Bitmapset * pull_paramids(Expr *expr)
Definition: clauses.c:5306
static Memoize * make_memoize(Plan *lefttree, Oid *hashoperators, Oid *collations, List *param_exprs, bool singlerow, bool binary_mode, uint32 est_entries, Bitmapset *keyparamids)
Definition: createplan.c:6540
#define lfirst_oid(lc)
Definition: pg_list.h:174
bool singlerow
Definition: pathnodes.h:1964
List * hash_operators
Definition: pathnodes.h:1962
uint32 est_entries
Definition: pathnodes.h:1969
bool binary_mode
Definition: pathnodes.h:1966
Path * subpath
Definition: pathnodes.h:1961
List * param_exprs
Definition: pathnodes.h:1963

References Assert(), MemoizePath::binary_mode, copy_generic_path_info(), CP_SMALL_TLIST, create_plan_recurse(), MemoizePath::est_entries, exprCollation(), forboth, MemoizePath::hash_operators, i, lfirst, lfirst_oid, list_length(), make_memoize(), NIL, palloc(), MemoizePath::param_exprs, plan, pull_paramids(), replace_nestloop_params(), MemoizePath::singlerow, and MemoizePath::subpath.

Referenced by create_plan_recurse().

◆ create_merge_append_plan()

static Plan * create_merge_append_plan ( PlannerInfo root,
MergeAppendPath best_path,
int  flags 
)
static

Definition at line 1435 of file createplan.c.

1437 {
1438  MergeAppend *node = makeNode(MergeAppend);
1439  Plan *plan = &node->plan;
1440  List *tlist = build_path_tlist(root, &best_path->path);
1441  int orig_tlist_length = list_length(tlist);
1442  bool tlist_was_changed;
1443  List *pathkeys = best_path->path.pathkeys;
1444  List *subplans = NIL;
1445  ListCell *subpaths;
1446  RelOptInfo *rel = best_path->path.parent;
1447  PartitionPruneInfo *partpruneinfo = NULL;
1448 
1449  /*
1450  * We don't have the actual creation of the MergeAppend node split out
1451  * into a separate make_xxx function. This is because we want to run
1452  * prepare_sort_from_pathkeys on it before we do so on the individual
1453  * child plans, to make cross-checking the sort info easier.
1454  */
1455  copy_generic_path_info(plan, (Path *) best_path);
1456  plan->targetlist = tlist;
1457  plan->qual = NIL;
1458  plan->lefttree = NULL;
1459  plan->righttree = NULL;
1460  node->apprelids = rel->relids;
1461 
1462  /*
1463  * Compute sort column info, and adjust MergeAppend's tlist as needed.
1464  * Because we pass adjust_tlist_in_place = true, we may ignore the
1465  * function result; it must be the same plan node. However, we then need
1466  * to detect whether any tlist entries were added.
1467  */
1468  (void) prepare_sort_from_pathkeys(plan, pathkeys,
1469  best_path->path.parent->relids,
1470  NULL,
1471  true,
1472  &node->numCols,
1473  &node->sortColIdx,
1474  &node->sortOperators,
1475  &node->collations,
1476  &node->nullsFirst);
1477  tlist_was_changed = (orig_tlist_length != list_length(plan->targetlist));
1478 
1479  /*
1480  * Now prepare the child plans. We must apply prepare_sort_from_pathkeys
1481  * even to subplans that don't need an explicit sort, to make sure they
1482  * are returning the same sort key columns the MergeAppend expects.
1483  */
1484  foreach(subpaths, best_path->subpaths)
1485  {
1486  Path *subpath = (Path *) lfirst(subpaths);
1487  Plan *subplan;
1488  int numsortkeys;
1489  AttrNumber *sortColIdx;
1490  Oid *sortOperators;
1491  Oid *collations;
1492  bool *nullsFirst;
1493 
1494  /* Build the child plan */
1495  /* Must insist that all children return the same tlist */
1496  subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
1497 
1498  /* Compute sort column info, and adjust subplan's tlist as needed */
1499  subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1500  subpath->parent->relids,
1501  node->sortColIdx,
1502  false,
1503  &numsortkeys,
1504  &sortColIdx,
1505  &sortOperators,
1506  &collations,
1507  &nullsFirst);
1508 
1509  /*
1510  * Check that we got the same sort key information. We just Assert
1511  * that the sortops match, since those depend only on the pathkeys;
1512  * but it seems like a good idea to check the sort column numbers
1513  * explicitly, to ensure the tlists really do match up.
1514  */
1515  Assert(numsortkeys == node->numCols);
1516  if (memcmp(sortColIdx, node->sortColIdx,
1517  numsortkeys * sizeof(AttrNumber)) != 0)
1518  elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
1519  Assert(memcmp(sortOperators, node->sortOperators,
1520  numsortkeys * sizeof(Oid)) == 0);
1521  Assert(memcmp(collations, node->collations,
1522  numsortkeys * sizeof(Oid)) == 0);
1523  Assert(memcmp(nullsFirst, node->nullsFirst,
1524  numsortkeys * sizeof(bool)) == 0);
1525 
1526  /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1527  if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
1528  {
1529  Sort *sort = make_sort(subplan, numsortkeys,
1530  sortColIdx, sortOperators,
1531  collations, nullsFirst);
1532 
1533  label_sort_with_costsize(root, sort, best_path->limit_tuples);
1534  subplan = (Plan *) sort;
1535  }
1536 
1537  subplans = lappend(subplans, subplan);
1538  }
1539 
1540  /*
1541  * If any quals exist, they may be useful to perform further partition
1542  * pruning during execution. Gather information needed by the executor to
1543  * do partition pruning.
1544  */
1546  {
1547  List *prunequal;
1548 
1549  prunequal = extract_actual_clauses(rel->baserestrictinfo, false);
1550 
1551  /* We don't currently generate any parameterized MergeAppend paths */
1552  Assert(best_path->path.param_info == NULL);
1553 
1554  if (prunequal != NIL)
1555  partpruneinfo = make_partition_pruneinfo(root, rel,
1556  best_path->subpaths,
1557  prunequal);
1558  }
1559 
1560  node->mergeplans = subplans;
1561  node->part_prune_info = partpruneinfo;
1562 
1563  /*
1564  * If prepare_sort_from_pathkeys added sort columns, but we were told to
1565  * produce either the exact tlist or a narrow tlist, we should get rid of
1566  * the sort columns again. We must inject a projection node to do so.
1567  */
1568  if (tlist_was_changed && (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST)))
1569  {
1570  tlist = list_copy_head(plan->targetlist, orig_tlist_length);
1571  return inject_projection_plan(plan, tlist, plan->parallel_safe);
1572  }
1573  else
1574  return plan;
1575 }
Cardinality limit_tuples
Definition: pathnodes.h:1925
struct PartitionPruneInfo * part_prune_info
Definition: plannodes.h:312
Bitmapset * apprelids
Definition: plannodes.h:290
List * mergeplans
Definition: plannodes.h:292

References MergeAppend::apprelids, Assert(), RelOptInfo::baserestrictinfo, build_path_tlist(), copy_generic_path_info(), CP_EXACT_TLIST, CP_SMALL_TLIST, create_plan_recurse(), elog(), enable_partition_pruning, ERROR, extract_actual_clauses(), inject_projection_plan(), label_sort_with_costsize(), lappend(), lfirst, MergeAppendPath::limit_tuples, list_copy_head(), list_length(), make_partition_pruneinfo(), make_sort(), makeNode, MergeAppend::mergeplans, NIL, MergeAppend::numCols, MergeAppend::part_prune_info, MergeAppendPath::path, Path::pathkeys, pathkeys_contained_in(), MergeAppend::plan, plan, prepare_sort_from_pathkeys(), RelOptInfo::relids, sort(), subpath(), and MergeAppendPath::subpaths.

Referenced by create_plan_recurse().

◆ create_mergejoin_plan()

static MergeJoin * create_mergejoin_plan ( PlannerInfo root,
MergePath best_path 
)
static

Definition at line 4411 of file createplan.c.

4413 {
4414  MergeJoin *join_plan;
4415  Plan *outer_plan;
4416  Plan *inner_plan;
4417  List *tlist = build_path_tlist(root, &best_path->jpath.path);
4418  List *joinclauses;
4419  List *otherclauses;
4420  List *mergeclauses;
4421  List *outerpathkeys;
4422  List *innerpathkeys;
4423  int nClauses;
4424  Oid *mergefamilies;
4425  Oid *mergecollations;
4426  int *mergestrategies;
4427  bool *mergenullsfirst;
4428  PathKey *opathkey;
4429  EquivalenceClass *opeclass;
4430  int i;
4431  ListCell *lc;
4432  ListCell *lop;
4433  ListCell *lip;
4434  Path *outer_path = best_path->jpath.outerjoinpath;
4435  Path *inner_path = best_path->jpath.innerjoinpath;
4436 
4437  /*
4438  * MergeJoin can project, so we don't have to demand exact tlists from the
4439  * inputs. However, if we're intending to sort an input's result, it's
4440  * best to request a small tlist so we aren't sorting more data than
4441  * necessary.
4442  */
4443  outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4444  (best_path->outersortkeys != NIL) ? CP_SMALL_TLIST : 0);
4445 
4446  inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4447  (best_path->innersortkeys != NIL) ? CP_SMALL_TLIST : 0);
4448 
4449  /* Sort join qual clauses into best execution order */
4450  /* NB: do NOT reorder the mergeclauses */
4451  joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4452 
4453  /* Get the join qual clauses (in plain expression form) */
4454  /* Any pseudoconstant clauses are ignored here */
4455  if (IS_OUTER_JOIN(best_path->jpath.jointype))
4456  {
4457  extract_actual_join_clauses(joinclauses,
4458  best_path->jpath.path.parent->relids,
4459  &joinclauses, &otherclauses);
4460  }
4461  else
4462  {
4463  /* We can treat all clauses alike for an inner join */
4464  joinclauses = extract_actual_clauses(joinclauses, false);
4465  otherclauses = NIL;
4466  }
4467 
4468  /*
4469  * Remove the mergeclauses from the list of join qual clauses, leaving the
4470  * list of quals that must be checked as qpquals.
4471  */
4472  mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
4473  joinclauses = list_difference(joinclauses, mergeclauses);
4474 
4475  /*
4476  * Replace any outer-relation variables with nestloop params. There
4477  * should not be any in the mergeclauses.
4478  */
4479  if (best_path->jpath.path.param_info)
4480  {
4481  joinclauses = (List *)
4482  replace_nestloop_params(root, (Node *) joinclauses);
4483  otherclauses = (List *)
4484  replace_nestloop_params(root, (Node *) otherclauses);
4485  }
4486 
4487  /*
4488  * Rearrange mergeclauses, if needed, so that the outer variable is always
4489  * on the left; mark the mergeclause restrictinfos with correct
4490  * outer_is_left status.
4491  */
4492  mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
4493  best_path->jpath.outerjoinpath->parent->relids);
4494 
4495  /*
4496  * Create explicit sort nodes for the outer and inner paths if necessary.
4497  */
4498  if (best_path->outersortkeys)
4499  {
4500  Relids outer_relids = outer_path->parent->relids;
4501  Sort *sort = make_sort_from_pathkeys(outer_plan,
4502  best_path->outersortkeys,
4503  outer_relids);
4504 
4505  label_sort_with_costsize(root, sort, -1.0);
4506  outer_plan = (Plan *) sort;
4507  outerpathkeys = best_path->outersortkeys;
4508  }
4509  else
4510  outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
4511 
4512  if (best_path->innersortkeys)
4513  {
4514  Relids inner_relids = inner_path->parent->relids;
4515  Sort *sort = make_sort_from_pathkeys(inner_plan,
4516  best_path->innersortkeys,
4517  inner_relids);
4518 
4519  label_sort_with_costsize(root, sort, -1.0);
4520  inner_plan = (Plan *) sort;
4521  innerpathkeys = best_path->innersortkeys;
4522  }
4523  else
4524  innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
4525 
4526  /*
4527  * If specified, add a materialize node to shield the inner plan from the
4528  * need to handle mark/restore.
4529  */
4530  if (best_path->materialize_inner)
4531  {
4532  Plan *matplan = (Plan *) make_material(inner_plan);
4533 
4534  /*
4535  * We assume the materialize will not spill to disk, and therefore
4536  * charge just cpu_operator_cost per tuple. (Keep this estimate in
4537  * sync with final_cost_mergejoin.)
4538  */
4539  copy_plan_costsize(matplan, inner_plan);
4540  matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
4541 
4542  inner_plan = matplan;
4543  }
4544 
4545  /*
4546  * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
4547  * executor. The information is in the pathkeys for the two inputs, but
4548  * we need to be careful about the possibility of mergeclauses sharing a
4549  * pathkey, as well as the possibility that the inner pathkeys are not in
4550  * an order matching the mergeclauses.
4551  */
4552  nClauses = list_length(mergeclauses);
4553  Assert(nClauses == list_length(best_path->path_mergeclauses));
4554  mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
4555  mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
4556  mergestrategies = (int *) palloc(nClauses * sizeof(int));
4557  mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
4558 
4559  opathkey = NULL;
4560  opeclass = NULL;
4561  lop = list_head(outerpathkeys);
4562  lip = list_head(innerpathkeys);
4563  i = 0;
4564  foreach(lc, best_path->path_mergeclauses)
4565  {
4566  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
4567  EquivalenceClass *oeclass;
4568  EquivalenceClass *ieclass;
4569  PathKey *ipathkey = NULL;
4570  EquivalenceClass *ipeclass = NULL;
4571  bool first_inner_match = false;
4572 
4573  /* fetch outer/inner eclass from mergeclause */
4574  if (rinfo->outer_is_left)
4575  {
4576  oeclass = rinfo->left_ec;
4577  ieclass = rinfo->right_ec;
4578  }
4579  else
4580  {
4581  oeclass = rinfo->right_ec;
4582  ieclass = rinfo->left_ec;
4583  }
4584  Assert(oeclass != NULL);
4585  Assert(ieclass != NULL);
4586 
4587  /*
4588  * We must identify the pathkey elements associated with this clause
4589  * by matching the eclasses (which should give a unique match, since
4590  * the pathkey lists should be canonical). In typical cases the merge
4591  * clauses are one-to-one with the pathkeys, but when dealing with
4592  * partially redundant query conditions, things are more complicated.
4593  *
4594  * lop and lip reference the first as-yet-unmatched pathkey elements.
4595  * If they're NULL then all pathkey elements have been matched.
4596  *
4597  * The ordering of the outer pathkeys should match the mergeclauses,
4598  * by construction (see find_mergeclauses_for_outer_pathkeys()). There
4599  * could be more than one mergeclause for the same outer pathkey, but
4600  * no pathkey may be entirely skipped over.
4601  */
4602  if (oeclass != opeclass) /* multiple matches are not interesting */
4603  {
4604  /* doesn't match the current opathkey, so must match the next */
4605  if (lop == NULL)
4606  elog(ERROR, "outer pathkeys do not match mergeclauses");
4607  opathkey = (PathKey *) lfirst(lop);
4608  opeclass = opathkey->pk_eclass;
4609  lop = lnext(outerpathkeys, lop);
4610  if (oeclass != opeclass)
4611  elog(ERROR, "outer pathkeys do not match mergeclauses");
4612  }
4613 
4614  /*
4615  * The inner pathkeys likewise should not have skipped-over keys, but
4616  * it's possible for a mergeclause to reference some earlier inner
4617  * pathkey if we had redundant pathkeys. For example we might have
4618  * mergeclauses like "o.a = i.x AND o.b = i.y AND o.c = i.x". The
4619  * implied inner ordering is then "ORDER BY x, y, x", but the pathkey
4620  * mechanism drops the second sort by x as redundant, and this code
4621  * must cope.
4622  *
4623  * It's also possible for the implied inner-rel ordering to be like
4624  * "ORDER BY x, y, x DESC". We still drop the second instance of x as
4625  * redundant; but this means that the sort ordering of a redundant
4626  * inner pathkey should not be considered significant. So we must
4627  * detect whether this is the first clause matching an inner pathkey.
4628  */
4629  if (lip)
4630  {
4631  ipathkey = (PathKey *) lfirst(lip);
4632  ipeclass = ipathkey->pk_eclass;
4633  if (ieclass == ipeclass)
4634  {
4635  /* successful first match to this inner pathkey */
4636  lip = lnext(innerpathkeys, lip);
4637  first_inner_match = true;
4638  }
4639  }
4640  if (!first_inner_match)
4641  {
4642  /* redundant clause ... must match something before lip */
4643  ListCell *l2;
4644 
4645  foreach(l2, innerpathkeys)
4646  {
4647  if (l2 == lip)
4648  break;
4649  ipathkey = (PathKey *) lfirst(l2);
4650  ipeclass = ipathkey->pk_eclass;
4651  if (ieclass == ipeclass)
4652  break;
4653  }
4654  if (ieclass != ipeclass)
4655  elog(ERROR, "inner pathkeys do not match mergeclauses");
4656  }
4657 
4658  /*
4659  * The pathkeys should always match each other as to opfamily and
4660  * collation (which affect equality), but if we're considering a
4661  * redundant inner pathkey, its sort ordering might not match. In
4662  * such cases we may ignore the inner pathkey's sort ordering and use
4663  * the outer's. (In effect, we're lying to the executor about the
4664  * sort direction of this inner column, but it does not matter since
4665  * the run-time row comparisons would only reach this column when
4666  * there's equality for the earlier column containing the same eclass.
4667  * There could be only one value in this column for the range of inner
4668  * rows having a given value in the earlier column, so it does not
4669  * matter which way we imagine this column to be ordered.) But a
4670  * non-redundant inner pathkey had better match outer's ordering too.
4671  */
4672  if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
4673  opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation)
4674  elog(ERROR, "left and right pathkeys do not match in mergejoin");
4675  if (first_inner_match &&
4676  (opathkey->pk_strategy != ipathkey->pk_strategy ||
4677  opathkey->pk_nulls_first != ipathkey->pk_nulls_first))
4678  elog(ERROR, "left and right pathkeys do not match in mergejoin");
4679 
4680  /* OK, save info for executor */
4681  mergefamilies[i] = opathkey->pk_opfamily;
4682  mergecollations[i] = opathkey->pk_eclass->ec_collation;
4683  mergestrategies[i] = opathkey->pk_strategy;
4684  mergenullsfirst[i] = opathkey->pk_nulls_first;
4685  i++;
4686  }
4687 
4688  /*
4689  * Note: it is not an error if we have additional pathkey elements (i.e.,
4690  * lop or lip isn't NULL here). The input paths might be better-sorted
4691  * than we need for the current mergejoin.
4692  */
4693 
4694  /*
4695  * Now we can build the mergejoin node.
4696  */
4697  join_plan = make_mergejoin(tlist,
4698  joinclauses,
4699  otherclauses,
4700  mergeclauses,
4701  mergefamilies,
4702  mergecollations,
4703  mergestrategies,
4704  mergenullsfirst,
4705  outer_plan,
4706  inner_plan,
4707  best_path->jpath.jointype,
4708  best_path->jpath.inner_unique,
4709  best_path->skip_mark_restore);
4710 
4711  /* Costs of sort and material steps are included in path cost already */
4712  copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4713 
4714  return join_plan;
4715 }
double cpu_operator_cost
Definition: costsize.c:124
static Sort * make_sort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids)
Definition: createplan.c:6318
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)
Definition: createplan.c:5999
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
Join join
Definition: plannodes.h:833
List * outersortkeys
Definition: pathnodes.h:2102
bool skip_mark_restore
Definition: pathnodes.h:2104
List * innersortkeys
Definition: pathnodes.h:2103
JoinPath jpath
Definition: pathnodes.h:2100
bool materialize_inner
Definition: pathnodes.h:2105
List * path_mergeclauses
Definition: pathnodes.h:2101
bool pk_nulls_first
Definition: pathnodes.h:1456

References Assert(), build_path_tlist(), copy_generic_path_info(), copy_plan_costsize(), CP_SMALL_TLIST, cpu_operator_cost, create_plan_recurse(), elog(), ERROR, extract_actual_clauses(), extract_actual_join_clauses(), get_actual_clauses(), get_switched_clauses(), i, JoinPath::inner_unique, JoinPath::innerjoinpath, MergePath::innersortkeys, IS_OUTER_JOIN, MergeJoin::join, JoinPath::joinrestrictinfo, JoinPath::jointype, MergePath::jpath, label_sort_with_costsize(), lfirst, lfirst_node, list_difference(), list_head(), list_length(), lnext(), make_material(), make_mergejoin(), make_sort_from_pathkeys(), MergePath::materialize_inner, NIL, order_qual_clauses(), JoinPath::outerjoinpath, MergePath::outersortkeys, palloc(), MergePath::path_mergeclauses, Path::pathkeys, PathKey::pk_nulls_first, PathKey::pk_opfamily, PathKey::pk_strategy, Plan::plan_rows, replace_nestloop_params(), MergePath::skip_mark_restore, sort(), and Plan::total_cost.

Referenced by create_join_plan().

◆ create_minmaxagg_plan()

static Result * create_minmaxagg_plan ( PlannerInfo root,
MinMaxAggPath best_path 
)
static

Definition at line 2548 of file createplan.c.

2549 {
2550  Result *plan;
2551  List *tlist;
2552  ListCell *lc;
2553 
2554  /* Prepare an InitPlan for each aggregate's subquery. */
2555  foreach(lc, best_path->mmaggregates)
2556  {
2557  MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
2558  PlannerInfo *subroot = mminfo->subroot;
2559  Query *subparse = subroot->parse;
2560  Plan *plan;
2561 
2562  /*
2563  * Generate the plan for the subquery. We already have a Path, but we
2564  * have to convert it to a Plan and attach a LIMIT node above it.
2565  * Since we are entering a different planner context (subroot),
2566  * recurse to create_plan not create_plan_recurse.
2567  */
2568  plan = create_plan(subroot, mminfo->path);
2569 
2570  plan = (Plan *) make_limit(plan,
2571  subparse->limitOffset,
2572  subparse->limitCount,
2573  subparse->limitOption,
2574  0, NULL, NULL, NULL);
2575 
2576  /* Must apply correct cost/width data to Limit node */
2577  plan->startup_cost = mminfo->path->startup_cost;
2578  plan->total_cost = mminfo->pathcost;
2579  plan->plan_rows = 1;
2580  plan->plan_width = mminfo->path->pathtarget->width;
2581  plan->parallel_aware = false;
2582  plan->parallel_safe = mminfo->path->parallel_safe;
2583 
2584  /* Convert the plan into an InitPlan in the outer query. */
2585  SS_make_initplan_from_plan(root, subroot, plan, mminfo->param);
2586  }
2587 
2588  /* Generate the output plan --- basically just a Result */
2589  tlist = build_path_tlist(root, &best_path->path);
2590 
2591  plan = make_result(tlist, (Node *) best_path->quals, NULL);
2592 
2593  copy_generic_path_info(&plan->plan, (Path *) best_path);
2594 
2595  /*
2596  * During setrefs.c, we'll need to replace references to the Agg nodes
2597  * with InitPlan output params. (We can't just do that locally in the
2598  * MinMaxAgg node, because path nodes above here may have Agg references
2599  * as well.) Save the mmaggregates list to tell setrefs.c to do that.
2600  */
2601  Assert(root->minmax_aggs == NIL);
2602  root->minmax_aggs = best_path->mmaggregates;
2603 
2604  return plan;
2605 }
Plan * create_plan(PlannerInfo *root, Path *best_path)
Definition: createplan.c:335
Param * param
Definition: pathnodes.h:3092
List * quals
Definition: pathnodes.h:2278
List * mmaggregates
Definition: pathnodes.h:2277
List * minmax_aggs
Definition: pathnodes.h:469
Node * limitCount
Definition: parsenodes.h:211
Node * limitOffset
Definition: parsenodes.h:210
LimitOption limitOption
Definition: parsenodes.h:212
void SS_make_initplan_from_plan(PlannerInfo *root, PlannerInfo *subroot, Plan *plan, Param *prm)
Definition: subselect.c:3014

References Assert(), build_path_tlist(), copy_generic_path_info(), create_plan(), lfirst, Query::limitCount, Query::limitOffset, Query::limitOption, make_limit(), make_result(), PlannerInfo::minmax_aggs, MinMaxAggPath::mmaggregates, NIL, Path::parallel_safe, MinMaxAggInfo::param, PlannerInfo::parse, MinMaxAggPath::path, MinMaxAggInfo::path, MinMaxAggInfo::pathcost, plan, MinMaxAggPath::quals, SS_make_initplan_from_plan(), and Path::startup_cost.

Referenced by create_plan_recurse().

◆ create_modifytable_plan()

static ModifyTable * create_modifytable_plan ( PlannerInfo root,
ModifyTablePath best_path 
)
static

Definition at line 2812 of file createplan.c.

2813 {
2814  ModifyTable *plan;
2815  Path *subpath = best_path->subpath;
2816  Plan *subplan;
2817 
2818  /* Subplan must produce exactly the specified tlist */
2819  subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
2820 
2821  /* Transfer resname/resjunk labeling, too, to keep executor happy */
2823 
2824  plan = make_modifytable(root,
2825  subplan,
2826  best_path->operation,
2827  best_path->canSetTag,
2828  best_path->nominalRelation,
2829  best_path->rootRelation,
2830  best_path->partColsUpdated,
2831  best_path->resultRelations,
2832  best_path->updateColnosLists,
2833  best_path->withCheckOptionLists,
2834  best_path->returningLists,
2835  best_path->rowMarks,
2836  best_path->onconflict,
2837  best_path->mergeActionLists,
2838  best_path->epqParam);
2839 
2840  copy_generic_path_info(&plan->plan, &best_path->path);
2841 
2842  return plan;
2843 }
static ModifyTable * make_modifytable(PlannerInfo *root, Plan *subplan, CmdType operation, bool canSetTag, Index nominalRelation, Index rootRelation, bool partColsUpdated, List *resultRelations, List *updateColnosLists, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, List *mergeActionLists, int epqParam)
Definition: createplan.c:7000
bool partColsUpdated
Definition: pathnodes.h:2348
List * returningLists
Definition: pathnodes.h:2352
List * resultRelations
Definition: pathnodes.h:2349
List * withCheckOptionLists
Definition: pathnodes.h:2351
List * updateColnosLists
Definition: pathnodes.h:2350
OnConflictExpr * onconflict
Definition: pathnodes.h:2354
CmdType operation
Definition: pathnodes.h:2344
Index rootRelation
Definition: pathnodes.h:2347
Index nominalRelation
Definition: pathnodes.h:2346
List * mergeActionLists
Definition: pathnodes.h:2356
List * processed_tlist
Definition: pathnodes.h:453
void apply_tlist_labeling(List *dest_tlist, List *src_tlist)
Definition: tlist.c:318

References apply_tlist_labeling(), ModifyTablePath::canSetTag, copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), ModifyTablePath::epqParam, make_modifytable(), ModifyTablePath::mergeActionLists, ModifyTablePath::nominalRelation, ModifyTablePath::onconflict, ModifyTablePath::operation, ModifyTablePath::partColsUpdated, ModifyTablePath::path, plan, PlannerInfo::processed_tlist, ModifyTablePath::resultRelations, ModifyTablePath::returningLists, ModifyTablePath::rootRelation, ModifyTablePath::rowMarks, subpath(), ModifyTablePath::subpath, Plan::targetlist, ModifyTablePath::updateColnosLists, and ModifyTablePath::withCheckOptionLists.

Referenced by create_plan_recurse().

◆ create_namedtuplestorescan_plan()

static NamedTuplestoreScan * create_namedtuplestorescan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3973 of file createplan.c.

3975 {
3976  NamedTuplestoreScan *scan_plan;
3977  Index scan_relid = best_path->parent->relid;
3978  RangeTblEntry *rte;
3979 
3980  Assert(scan_relid > 0);
3981  rte = planner_rt_fetch(scan_relid, root);
3983 
3984  /* Sort clauses into best execution order */
3985  scan_clauses = order_qual_clauses(root, scan_clauses);
3986 
3987  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3988  scan_clauses = extract_actual_clauses(scan_clauses, false);
3989 
3990  /* Replace any outer-relation variables with nestloop params */
3991  if (best_path->param_info)
3992  {
3993  scan_clauses = (List *)
3994  replace_nestloop_params(root, (Node *) scan_clauses);
3995  }
3996 
3997  scan_plan = make_namedtuplestorescan(tlist, scan_clauses, scan_relid,
3998  rte->enrname);
3999 
4000  copy_generic_path_info(&scan_plan->scan.plan, best_path);
4001 
4002  return scan_plan;
4003 }
static NamedTuplestoreScan * make_namedtuplestorescan(List *qptlist, List *qpqual, Index scanrelid, char *enrname)
Definition: createplan.c:5755
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1020
char * enrname
Definition: parsenodes.h:1192

References Assert(), copy_generic_path_info(), RangeTblEntry::enrname, extract_actual_clauses(), make_namedtuplestorescan(), order_qual_clauses(), planner_rt_fetch, replace_nestloop_params(), RTE_NAMEDTUPLESTORE, RangeTblEntry::rtekind, and NamedTuplestoreScan::scan.

Referenced by create_scan_plan().

◆ create_nestloop_plan()

static NestLoop * create_nestloop_plan ( PlannerInfo root,
NestPath best_path 
)
static

Definition at line 4335 of file createplan.c.

4337 {
4338  NestLoop *join_plan;
4339  Plan *outer_plan;
4340  Plan *inner_plan;
4341  List *tlist = build_path_tlist(root, &best_path->jpath.path);
4342  List *joinrestrictclauses = best_path->jpath.joinrestrictinfo;
4343  List *joinclauses;
4344  List *otherclauses;
4345  Relids outerrelids;
4346  List *nestParams;
4347  Relids saveOuterRels = root->curOuterRels;
4348 
4349  /* NestLoop can project, so no need to be picky about child tlists */
4350  outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath, 0);
4351 
4352  /* For a nestloop, include outer relids in curOuterRels for inner side */
4353  root->curOuterRels = bms_union(root->curOuterRels,
4354  best_path->jpath.outerjoinpath->parent->relids);
4355 
4356  inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath, 0);
4357 
4358  /* Restore curOuterRels */
4359  bms_free(root->curOuterRels);
4360  root->curOuterRels = saveOuterRels;
4361 
4362  /* Sort join qual clauses into best execution order */
4363  joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
4364 
4365  /* Get the join qual clauses (in plain expression form) */
4366  /* Any pseudoconstant clauses are ignored here */
4367  if (IS_OUTER_JOIN(best_path->jpath.jointype))
4368  {
4369  extract_actual_join_clauses(joinrestrictclauses,
4370  best_path->jpath.path.parent->relids,
4371  &joinclauses, &otherclauses);
4372  }
4373  else
4374  {
4375  /* We can treat all clauses alike for an inner join */
4376  joinclauses = extract_actual_clauses(joinrestrictclauses, false);
4377  otherclauses = NIL;
4378  }
4379 
4380  /* Replace any outer-relation variables with nestloop params */
4381  if (best_path->jpath.path.param_info)
4382  {
4383  joinclauses = (List *)
4384  replace_nestloop_params(root, (Node *) joinclauses);
4385  otherclauses = (List *)
4386  replace_nestloop_params(root, (Node *) otherclauses);
4387  }
4388 
4389  /*
4390  * Identify any nestloop parameters that should be supplied by this join
4391  * node, and remove them from root->curOuterParams.
4392  */
4393  outerrelids = best_path->jpath.outerjoinpath->parent->relids;
4394  nestParams = identify_current_nestloop_params(root, outerrelids);
4395 
4396  join_plan = make_nestloop(tlist,
4397  joinclauses,
4398  otherclauses,
4399  nestParams,
4400  outer_plan,
4401  inner_plan,
4402  best_path->jpath.jointype,
4403  best_path->jpath.inner_unique);
4404 
4405  copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4406 
4407  return join_plan;
4408 }
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:211
static NestLoop * make_nestloop(List *tlist, List *joinclauses, List *otherclauses, List *nestParams, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
Definition: createplan.c:5920
List * identify_current_nestloop_params(PlannerInfo *root, Relids leftrelids)
Definition: paramassign.c:530
Join join
Definition: plannodes.h:807
JoinPath jpath
Definition: pathnodes.h:2060
Relids curOuterRels
Definition: pathnodes.h:529

References bms_free(), bms_union(), build_path_tlist(), copy_generic_path_info(), create_plan_recurse(), PlannerInfo::curOuterRels, extract_actual_clauses(), extract_actual_join_clauses(), identify_current_nestloop_params(), JoinPath::inner_unique, JoinPath::innerjoinpath, IS_OUTER_JOIN, NestLoop::join, JoinPath::joinrestrictinfo, JoinPath::jointype, NestPath::jpath, make_nestloop(), NIL, order_qual_clauses(), JoinPath::outerjoinpath, and replace_nestloop_params().

Referenced by create_join_plan().

◆ create_plan()

Plan* create_plan ( PlannerInfo root,
Path best_path 
)

Definition at line 335 of file createplan.c.

336 {
337  Plan *plan;
338 
339  /* plan_params should not be in use in current query level */
340  Assert(root->plan_params == NIL);
341 
342  /* Initialize this module's workspace in PlannerInfo */
343  root->curOuterRels = NULL;
344  root->curOuterParams = NIL;
345 
346  /* Recursively process the path tree, demanding the correct tlist result */
347  plan = create_plan_recurse(root, best_path, CP_EXACT_TLIST);
348 
349  /*
350  * Make sure the topmost plan node's targetlist exposes the original
351  * column names and other decorative info. Targetlists generated within
352  * the planner don't bother with that stuff, but we must have it on the
353  * top-level tlist seen at execution time. However, ModifyTable plan
354  * nodes don't have a tlist matching the querytree targetlist.
355  */
356  if (!IsA(plan, ModifyTable))
357  apply_tlist_labeling(plan->targetlist, root->processed_tlist);
358 
359  /*
360  * Attach any initPlans created in this query level to the topmost plan
361  * node. (In principle the initplans could go in any plan node at or
362  * above where they're referenced, but there seems no reason to put them
363  * any lower than the topmost node for the query level. Also, see
364  * comments for SS_finalize_plan before you try to change this.)
365  */
366  SS_attach_initplans(root, plan);
367 
368  /* Check we successfully assigned all NestLoopParams to plan nodes */
369  if (root->curOuterParams != NIL)
370  elog(ERROR, "failed to assign all NestLoopParams to plan nodes");
371 
372  /*
373  * Reset plan_params to ensure param IDs used for nestloop params are not
374  * re-used later
375  */
376  root->plan_params = NIL;
377 
378  return plan;
379 }
List * plan_params
Definition: pathnodes.h:217
List * curOuterParams
Definition: pathnodes.h:531
void SS_attach_initplans(PlannerInfo *root, Plan *plan)
Definition: subselect.c:2236

References apply_tlist_labeling(), Assert(), CP_EXACT_TLIST, create_plan_recurse(), PlannerInfo::curOuterParams, PlannerInfo::curOuterRels, elog(), ERROR, IsA, NIL, plan, PlannerInfo::plan_params, PlannerInfo::processed_tlist, and SS_attach_initplans().

Referenced by create_minmaxagg_plan(), create_subqueryscan_plan(), make_subplan(), SS_process_ctes(), and standard_planner().

◆ create_plan_recurse()

static Plan * create_plan_recurse ( PlannerInfo root,
Path best_path,
int  flags 
)
static

Definition at line 386 of file createplan.c.

387 {
388  Plan *plan;
389 
390  /* Guard against stack overflow due to overly complex plans */
392 
393  switch (best_path->pathtype)
394  {
395  case T_SeqScan:
396  case T_SampleScan:
397  case T_IndexScan:
398  case T_IndexOnlyScan:
399  case T_BitmapHeapScan:
400  case T_TidScan:
401  case T_TidRangeScan:
402  case T_SubqueryScan:
403  case T_FunctionScan:
404  case T_TableFuncScan:
405  case T_ValuesScan:
406  case T_CteScan:
407  case T_WorkTableScan:
408  case T_NamedTuplestoreScan:
409  case T_ForeignScan:
410  case T_CustomScan:
411  plan = create_scan_plan(root, best_path, flags);
412  break;
413  case T_HashJoin:
414  case T_MergeJoin:
415  case T_NestLoop:
416  plan = create_join_plan(root,
417  (JoinPath *) best_path);
418  break;
419  case T_Append:
420  plan = create_append_plan(root,
421  (AppendPath *) best_path,
422  flags);
423  break;
424  case T_MergeAppend:
426  (MergeAppendPath *) best_path,
427  flags);
428  break;
429  case T_Result:
430  if (IsA(best_path, ProjectionPath))
431  {
433  (ProjectionPath *) best_path,
434  flags);
435  }
436  else if (IsA(best_path, MinMaxAggPath))
437  {
438  plan = (Plan *) create_minmaxagg_plan(root,
439  (MinMaxAggPath *) best_path);
440  }
441  else if (IsA(best_path, GroupResultPath))
442  {
444  (GroupResultPath *) best_path);
445  }
446  else
447  {
448  /* Simple RTE_RESULT base relation */
449  Assert(IsA(best_path, Path));
450  plan = create_scan_plan(root, best_path, flags);
451  }
452  break;
453  case T_ProjectSet:
454  plan = (Plan *) create_project_set_plan(root,
455  (ProjectSetPath *) best_path);
456  break;
457  case T_Material:
458  plan = (Plan *) create_material_plan(root,
459  (MaterialPath *) best_path,
460  flags);
461  break;
462  case T_Memoize:
463  plan = (Plan *) create_memoize_plan(root,
464  (MemoizePath *) best_path,
465  flags);
466  break;
467  case T_Unique:
468  if (IsA(best_path, UpperUniquePath))
469  {
471  (UpperUniquePath *) best_path,
472  flags);
473  }
474  else
475  {
476  Assert(IsA(best_path, UniquePath));
477  plan = create_unique_plan(root,
478  (UniquePath *) best_path,
479  flags);
480  }
481  break;
482  case T_Gather:
483  plan = (Plan *) create_gather_plan(root,
484  (GatherPath *) best_path);
485  break;
486  case T_Sort:
487  plan = (Plan *) create_sort_plan(root,
488  (SortPath *) best_path,
489  flags);
490  break;
491  case T_IncrementalSort:
493  (IncrementalSortPath *) best_path,
494  flags);
495  break;
496  case T_Group:
497  plan = (Plan *) create_group_plan(root,
498  (GroupPath *) best_path);
499  break;
500  case T_Agg:
501  if (IsA(best_path, GroupingSetsPath))
503  (GroupingSetsPath *) best_path);
504  else
505  {
506  Assert(IsA(best_path, AggPath));
507  plan = (Plan *) create_agg_plan(root,
508  (AggPath *) best_path);
509  }
510  break;
511  case T_WindowAgg:
512  plan = (Plan *) create_windowagg_plan(root,
513  (WindowAggPath *) best_path);
514  break;
515  case T_SetOp:
516  plan = (Plan *) create_setop_plan(root,
517  (SetOpPath *) best_path,
518  flags);
519  break;
520  case T_RecursiveUnion:
522  (RecursiveUnionPath *) best_path);
523  break;
524  case T_LockRows:
525  plan = (Plan *) create_lockrows_plan(root,
526  (LockRowsPath *) best_path,
527  flags);
528  break;
529  case T_ModifyTable:
530  plan = (Plan *) create_modifytable_plan(root,
531  (ModifyTablePath *) best_path);
532  break;
533  case T_Limit:
534  plan = (Plan *) create_limit_plan(root,
535  (LimitPath *) best_path,
536  flags);
537  break;
538  case T_GatherMerge:
540  (GatherMergePath *) best_path);
541  break;
542  default:
543  elog(ERROR, "unrecognized node type: %d",
544  (int) best_path->pathtype);
545  plan = NULL; /* keep compiler quiet */
546  break;
547  }
548 
549  return plan;
550 }
static Plan * create_join_plan(PlannerInfo *root, JoinPath *best_path)
Definition: createplan.c:1079
static Plan * create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path, int flags)
Definition: createplan.c:1435
static GatherMerge * create_gather_merge_plan(PlannerInfo *root, GatherMergePath *best_path)
Definition: createplan.c:1955
static Plan * create_append_plan(PlannerInfo *root, AppendPath *best_path, int flags)
Definition: createplan.c:1214
static Result * create_group_result_plan(PlannerInfo *root, GroupResultPath *best_path)
Definition: createplan.c:1585
static Limit * create_limit_plan(PlannerInfo *root, LimitPath *best_path, int flags)
Definition: createplan.c:2852
static Agg * create_agg_plan(PlannerInfo *root, AggPath *best_path)
Definition: createplan.c:2306
static SetOp * create_setop_plan(PlannerInfo *root, SetOpPath *best_path, int flags)
Definition: createplan.c:2717
static Sort * create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags)
Definition: createplan.c:2178
static Unique * create_upper_unique_plan(PlannerInfo *root, UpperUniquePath *best_path, int flags)
Definition: createplan.c:2278
static Gather * create_gather_plan(PlannerInfo *root, GatherPath *best_path)
Definition: createplan.c:1917
static ProjectSet * create_project_set_plan(PlannerInfo *root, ProjectSetPath *best_path)
Definition: createplan.c:1610
static Group * create_group_plan(PlannerInfo *root, GroupPath *best_path)
Definition: createplan.c:2239
static ModifyTable * create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path)
Definition: createplan.c:2812
static Result * create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path)
Definition: createplan.c:2548
static LockRows * create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path, int flags)
Definition: createplan.c:2789
static Material * create_material_plan(PlannerInfo *root, MaterialPath *best_path, int flags)
Definition: createplan.c:1636
static Plan * create_scan_plan(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:557
static IncrementalSort * create_incrementalsort_plan(PlannerInfo *root, IncrementalSortPath *best_path, int flags)
Definition: createplan.c:2212
static Plan * create_projection_plan(PlannerInfo *root, ProjectionPath *best_path, int flags)
Definition: createplan.c:2016
static Memoize * create_memoize_plan(PlannerInfo *root, MemoizePath *best_path, int flags)
Definition: createplan.c:1664
static WindowAgg * create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
Definition: createplan.c:2614
static Plan * create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path)
Definition: createplan.c:2390
static RecursiveUnion * create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path)
Definition: createplan.c:2753
static Plan * create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
Definition: createplan.c:1718
void check_stack_depth(void)
Definition: postgres.c:3523
NodeTag pathtype
Definition: pathnodes.h:1594

References Assert(), check_stack_depth(), create_agg_plan(), create_append_plan(), create_gather_merge_plan(), create_gather_plan(), create_group_plan(), create_group_result_plan(), create_groupingsets_plan(), create_incrementalsort_plan(), create_join_plan(), create_limit_plan(), create_lockrows_plan(), create_material_plan(), create_memoize_plan(), create_merge_append_plan(), create_minmaxagg_plan(), create_modifytable_plan(), create_project_set_plan(), create_projection_plan(), create_recursiveunion_plan(), create_scan_plan(), create_setop_plan(), create_sort_plan(), create_unique_plan(), create_upper_unique_plan(), create_windowagg_plan(), elog(), ERROR, IsA, Path::pathtype, and plan.

Referenced by create_agg_plan(), create_append_plan(), create_customscan_plan(), create_foreignscan_plan(), create_gather_merge_plan(), create_gather_plan(), create_group_plan(), create_groupingsets_plan(), create_hashjoin_plan(), create_incrementalsort_plan(), create_limit_plan(), create_lockrows_plan(), create_material_plan(), create_memoize_plan(), create_merge_append_plan(), create_mergejoin_plan(), create_modifytable_plan(), create_nestloop_plan(), create_plan(), create_project_set_plan(), create_projection_plan(), create_recursiveunion_plan(), create_setop_plan(), create_sort_plan(), create_unique_plan(), create_upper_unique_plan(), and create_windowagg_plan().

◆ create_project_set_plan()

static ProjectSet * create_project_set_plan ( PlannerInfo root,
ProjectSetPath best_path 
)
static

Definition at line 1610 of file createplan.c.

1611 {
1612  ProjectSet *plan;
1613  Plan *subplan;
1614  List *tlist;
1615 
1616  /* Since we intend to project, we don't need to constrain child tlist */
1617  subplan = create_plan_recurse(root, best_path->subpath, 0);
1618 
1619  tlist = build_path_tlist(root, &best_path->path);
1620 
1621  plan = make_project_set(tlist, subplan);
1622 
1623  copy_generic_path_info(&plan->plan, (Path *) best_path);
1624 
1625  return plan;
1626 }
static ProjectSet * make_project_set(List *tlist, Plan *subplan)
Definition: createplan.c:6981
Path * subpath
Definition: pathnodes.h:2154

References build_path_tlist(), copy_generic_path_info(), create_plan_recurse(), make_project_set(), ProjectSetPath::path, plan, and ProjectSetPath::subpath.

Referenced by create_plan_recurse().

◆ create_projection_plan()

static Plan * create_projection_plan ( PlannerInfo root,
ProjectionPath best_path,
int  flags 
)
static

Definition at line 2016 of file createplan.c.

2017 {
2018  Plan *plan;
2019  Plan *subplan;
2020  List *tlist;
2021  bool needs_result_node = false;
2022 
2023  /*
2024  * Convert our subpath to a Plan and determine whether we need a Result
2025  * node.
2026  *
2027  * In most cases where we don't need to project, creation_projection_path
2028  * will have set dummypp, but not always. First, some createplan.c
2029  * routines change the tlists of their nodes. (An example is that
2030  * create_merge_append_plan might add resjunk sort columns to a
2031  * MergeAppend.) Second, create_projection_path has no way of knowing
2032  * what path node will be placed on top of the projection path and
2033  * therefore can't predict whether it will require an exact tlist. For
2034  * both of these reasons, we have to recheck here.
2035  */
2036  if (use_physical_tlist(root, &best_path->path, flags))
2037  {
2038  /*
2039  * Our caller doesn't really care what tlist we return, so we don't
2040  * actually need to project. However, we may still need to ensure
2041  * proper sortgroupref labels, if the caller cares about those.
2042  */
2043  subplan = create_plan_recurse(root, best_path->subpath, 0);
2044  tlist = subplan->targetlist;
2045  if (flags & CP_LABEL_TLIST)
2047  best_path->path.pathtarget);
2048  }
2049  else if (is_projection_capable_path(best_path->subpath))
2050  {
2051  /*
2052  * Our caller requires that we return the exact tlist, but no separate
2053  * result node is needed because the subpath is projection-capable.
2054  * Tell create_plan_recurse that we're going to ignore the tlist it
2055  * produces.
2056  */
2057  subplan = create_plan_recurse(root, best_path->subpath,
2058  CP_IGNORE_TLIST);
2060  tlist = build_path_tlist(root, &best_path->path);
2061  }
2062  else
2063  {
2064  /*
2065  * It looks like we need a result node, unless by good fortune the
2066  * requested tlist is exactly the one the child wants to produce.
2067  */
2068  subplan = create_plan_recurse(root, best_path->subpath, 0);
2069  tlist = build_path_tlist(root, &best_path->path);
2070  needs_result_node = !tlist_same_exprs(tlist, subplan->targetlist);
2071  }
2072 
2073  /*
2074  * If we make a different decision about whether to include a Result node
2075  * than create_projection_path did, we'll have made slightly wrong cost
2076  * estimates; but label the plan with the cost estimates we actually used,
2077  * not "corrected" ones. (XXX this could be cleaned up if we moved more
2078  * of the sortcolumn setup logic into Path creation, but that would add
2079  * expense to creating Paths we might end up not using.)
2080  */
2081  if (!needs_result_node)
2082  {
2083  /* Don't need a separate Result, just assign tlist to subplan */
2084  plan = subplan;
2085  plan->targetlist = tlist;
2086 
2087  /* Label plan with the estimated costs we actually used */
2088  plan->startup_cost = best_path->path.startup_cost;
2089  plan->total_cost = best_path->path.total_cost;
2090  plan->plan_rows = best_path->path.rows;
2091  plan->plan_width = best_path->path.pathtarget->width;
2092  plan->parallel_safe = best_path->path.parallel_safe;
2093  /* ... but don't change subplan's parallel_aware flag */
2094  }
2095  else
2096  {
2097  /* We need a Result node */
2098  plan = (Plan *) make_result(tlist, NULL, subplan);
2099 
2100  copy_generic_path_info(plan, (Path *) best_path);
2101  }
2102 
2103  return plan;
2104 }
static bool use_physical_tlist(PlannerInfo *root, Path *path, int flags)
Definition: createplan.c:863
bool is_projection_capable_path(Path *path)
Definition: createplan.c:7177
#define CP_IGNORE_TLIST
Definition: createplan.c:71
Path * subpath
Definition: pathnodes.h:2142
void apply_pathtarget_labeling_to_tlist(List *tlist, PathTarget *target)
Definition: tlist.c:774

References apply_pathtarget_labeling_to_tlist(), Assert(), build_path_tlist(), copy_generic_path_info(), CP_IGNORE_TLIST, CP_LABEL_TLIST, create_plan_recurse(), is_projection_capable_path(), is_projection_capable_plan(), make_result(), Path::parallel_safe, ProjectionPath::path, plan, Path::rows, Path::startup_cost, ProjectionPath::subpath, Plan::targetlist, tlist_same_exprs(), Path::total_cost, and use_physical_tlist().

Referenced by create_plan_recurse().

◆ create_recursiveunion_plan()

static RecursiveUnion * create_recursiveunion_plan ( PlannerInfo root,
RecursiveUnionPath best_path 
)
static

Definition at line 2753 of file createplan.c.

2754 {
2756  Plan *leftplan;
2757  Plan *rightplan;
2758  List *tlist;
2759  long numGroups;
2760 
2761  /* Need both children to produce same tlist, so force it */
2762  leftplan = create_plan_recurse(root, best_path->leftpath, CP_EXACT_TLIST);
2763  rightplan = create_plan_recurse(root, best_path->rightpath, CP_EXACT_TLIST);
2764 
2765  tlist = build_path_tlist(root, &best_path->path);
2766 
2767  /* Convert numGroups to long int --- but 'ware overflow! */
2768  numGroups = clamp_cardinality_to_long(best_path->numGroups);
2769 
2770  plan = make_recursive_union(tlist,
2771  leftplan,
2772  rightplan,
2773  best_path->wtParam,
2774  best_path->distinctList,
2775  numGroups);
2776 
2777  copy_generic_path_info(&plan->plan, (Path *) best_path);
2778 
2779  return plan;
2780 }
long clamp_cardinality_to_long(Cardinality x)
Definition: costsize.c:226
static RecursiveUnion * make_recursive_union(List *tlist, Plan *lefttree, Plan *righttree, int wtParam, List *distinctList, long numGroups)
Definition: createplan.c:5834
Cardinality numGroups
Definition: pathnodes.h:2319

References build_path_tlist(), clamp_cardinality_to_long(), copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), RecursiveUnionPath::distinctList, RecursiveUnionPath::leftpath, make_recursive_union(), RecursiveUnionPath::numGroups, RecursiveUnionPath::path, plan, RecursiveUnionPath::rightpath, and RecursiveUnionPath::wtParam.

Referenced by create_plan_recurse().

◆ create_resultscan_plan()

static Result * create_resultscan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 4012 of file createplan.c.

4014 {
4015  Result *scan_plan;
4016  Index scan_relid = best_path->parent->relid;
4018 
4019  Assert(scan_relid > 0);
4020  rte = planner_rt_fetch(scan_relid, root);
4021  Assert(rte->rtekind == RTE_RESULT);
4022 
4023  /* Sort clauses into best execution order */
4024  scan_clauses = order_qual_clauses(root, scan_clauses);
4025 
4026  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
4027  scan_clauses = extract_actual_clauses(scan_clauses, false);
4028 
4029  /* Replace any outer-relation variables with nestloop params */
4030  if (best_path->param_info)
4031  {
4032  scan_clauses = (List *)
4033  replace_nestloop_params(root, (Node *) scan_clauses);
4034  }
4035 
4036  scan_plan = make_result(tlist, (Node *) scan_clauses, NULL);
4037 
4038  copy_generic_path_info(&scan_plan->plan, best_path);
4039 
4040  return scan_plan;
4041 }
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:171
@ RTE_RESULT
Definition: parsenodes.h:1021

References Assert(), copy_generic_path_info(), extract_actual_clauses(), make_result(), order_qual_clauses(), PG_USED_FOR_ASSERTS_ONLY, Result::plan, planner_rt_fetch, replace_nestloop_params(), and RTE_RESULT.

Referenced by create_scan_plan().

◆ create_samplescan_plan()

static SampleScan * create_samplescan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 2951 of file createplan.c.

2953 {
2954  SampleScan *scan_plan;
2955  Index scan_relid = best_path->parent->relid;
2956  RangeTblEntry *rte;
2957  TableSampleClause *tsc;
2958 
2959  /* it should be a base rel with a tablesample clause... */
2960  Assert(scan_relid > 0);
2961  rte = planner_rt_fetch(scan_relid, root);
2962  Assert(rte->rtekind == RTE_RELATION);
2963  tsc = rte->tablesample;
2964  Assert(tsc != NULL);
2965 
2966  /* Sort clauses into best execution order */
2967  scan_clauses = order_qual_clauses(root, scan_clauses);
2968 
2969  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2970  scan_clauses = extract_actual_clauses(scan_clauses, false);
2971 
2972  /* Replace any outer-relation variables with nestloop params */
2973  if (best_path->param_info)
2974  {
2975  scan_clauses = (List *)
2976  replace_nestloop_params(root, (Node *) scan_clauses);
2977  tsc = (TableSampleClause *)
2978  replace_nestloop_params(root, (Node *) tsc);
2979  }
2980 
2981  scan_plan = make_samplescan(tlist,
2982  scan_clauses,
2983  scan_relid,
2984  tsc);
2985 
2986  copy_generic_path_info(&scan_plan->scan.plan, best_path);
2987 
2988  return scan_plan;
2989 }
static SampleScan * make_samplescan(List *qptlist, List *qpqual, Index scanrelid, TableSampleClause *tsc)
Definition: createplan.c:5497
struct TableSampleClause * tablesample
Definition: parsenodes.h:1074
Scan scan
Definition: plannodes.h:405

References Assert(), copy_generic_path_info(), extract_actual_clauses(), make_samplescan(), order_qual_clauses(), planner_rt_fetch, replace_nestloop_params(), RTE_RELATION, RangeTblEntry::rtekind, SampleScan::scan, and RangeTblEntry::tablesample.

Referenced by create_scan_plan().

◆ create_scan_plan()

static Plan * create_scan_plan ( PlannerInfo root,
Path best_path,
int  flags 
)
static

Definition at line 557 of file createplan.c.

558 {
559  RelOptInfo *rel = best_path->parent;
560  List *scan_clauses;
561  List *gating_clauses;
562  List *tlist;
563  Plan *plan;
564 
565  /*
566  * Extract the relevant restriction clauses from the parent relation. The
567  * executor must apply all these restrictions during the scan, except for
568  * pseudoconstants which we'll take care of below.
569  *
570  * If this is a plain indexscan or index-only scan, we need not consider
571  * restriction clauses that are implied by the index's predicate, so use
572  * indrestrictinfo not baserestrictinfo. Note that we can't do that for
573  * bitmap indexscans, since there's not necessarily a single index
574  * involved; but it doesn't matter since create_bitmap_scan_plan() will be
575  * able to get rid of such clauses anyway via predicate proof.
576  */
577  switch (best_path->pathtype)
578  {
579  case T_IndexScan:
580  case T_IndexOnlyScan:
581  scan_clauses = castNode(IndexPath, best_path)->indexinfo->indrestrictinfo;
582  break;
583  default:
584  scan_clauses = rel->baserestrictinfo;
585  break;
586  }
587 
588  /*
589  * If this is a parameterized scan, we also need to enforce all the join
590  * clauses available from the outer relation(s).
591  *
592  * For paranoia's sake, don't modify the stored baserestrictinfo list.
593  */
594  if (best_path->param_info)
595  scan_clauses = list_concat_copy(scan_clauses,
596  best_path->param_info->ppi_clauses);
597 
598  /*
599  * Detect whether we have any pseudoconstant quals to deal with. Then, if
600  * we'll need a gating Result node, it will be able to project, so there
601  * are no requirements on the child's tlist.
602  *
603  * If this replaces a join, it must be a foreign scan or a custom scan,
604  * and the FDW or the custom scan provider would have stored in the best
605  * path the list of RestrictInfo nodes to apply to the join; check against
606  * that list in that case.
607  */
608  if (IS_JOIN_REL(rel))
609  {
610  List *join_clauses;
611 
612  Assert(best_path->pathtype == T_ForeignScan ||
613  best_path->pathtype == T_CustomScan);
614  if (best_path->pathtype == T_ForeignScan)
615  join_clauses = ((ForeignPath *) best_path)->fdw_restrictinfo;
616  else
617  join_clauses = ((CustomPath *) best_path)->custom_restrictinfo;
618 
619  gating_clauses = get_gating_quals(root, join_clauses);
620  }
621  else
622  gating_clauses = get_gating_quals(root, scan_clauses);
623  if (gating_clauses)
624  flags = 0;
625 
626  /*
627  * For table scans, rather than using the relation targetlist (which is
628  * only those Vars actually needed by the query), we prefer to generate a
629  * tlist containing all Vars in order. This will allow the executor to
630  * optimize away projection of the table tuples, if possible.
631  *
632  * But if the caller is going to ignore our tlist anyway, then don't
633  * bother generating one at all. We use an exact equality test here, so
634  * that this only applies when CP_IGNORE_TLIST is the only flag set.
635  */
636  if (flags == CP_IGNORE_TLIST)
637  {
638  tlist = NULL;
639  }
640  else if (use_physical_tlist(root, best_path, flags))
641  {
642  if (best_path->pathtype == T_IndexOnlyScan)
643  {
644  /* For index-only scan, the preferred tlist is the index's */
645  tlist = copyObject(((IndexPath *) best_path)->indexinfo->indextlist);
646 
647  /*
648  * Transfer sortgroupref data to the replacement tlist, if
649  * requested (use_physical_tlist checked that this will work).
650  */
651  if (flags & CP_LABEL_TLIST)
652  apply_pathtarget_labeling_to_tlist(tlist, best_path->pathtarget);
653  }
654  else
655  {
656  tlist = build_physical_tlist(root, rel);
657  if (tlist == NIL)
658  {
659  /* Failed because of dropped cols, so use regular method */
660  tlist = build_path_tlist(root, best_path);
661  }
662  else
663  {
664  /* As above, transfer sortgroupref data to replacement tlist */
665  if (flags & CP_LABEL_TLIST)
666  apply_pathtarget_labeling_to_tlist(tlist, best_path->pathtarget);
667  }
668  }
669  }
670  else
671  {
672  tlist = build_path_tlist(root, best_path);
673  }
674 
675  switch (best_path->pathtype)
676  {
677  case T_SeqScan:
678  plan = (Plan *) create_seqscan_plan(root,
679  best_path,
680  tlist,
681  scan_clauses);
682  break;
683 
684  case T_SampleScan:
685  plan = (Plan *) create_samplescan_plan(root,
686  best_path,
687  tlist,
688  scan_clauses);
689  break;
690 
691  case T_IndexScan:
692  plan = (Plan *) create_indexscan_plan(root,
693  (IndexPath *) best_path,
694  tlist,
695  scan_clauses,
696  false);
697  break;
698 
699  case T_IndexOnlyScan:
700  plan = (Plan *) create_indexscan_plan(root,
701  (IndexPath *) best_path,
702  tlist,
703  scan_clauses,
704  true);
705  break;
706 
707  case T_BitmapHeapScan:
708  plan = (Plan *) create_bitmap_scan_plan(root,
709  (BitmapHeapPath *) best_path,
710  tlist,
711  scan_clauses);
712  break;
713 
714  case T_TidScan:
715  plan = (Plan *) create_tidscan_plan(root,
716  (TidPath *) best_path,
717  tlist,
718  scan_clauses);
719  break;
720 
721  case T_TidRangeScan:
723  (TidRangePath *) best_path,
724  tlist,
725  scan_clauses);
726  break;
727 
728  case T_SubqueryScan:
730  (SubqueryScanPath *) best_path,
731  tlist,
732  scan_clauses);
733  break;
734 
735  case T_FunctionScan:
737  best_path,
738  tlist,
739  scan_clauses);
740  break;
741 
742  case T_TableFuncScan:
744  best_path,
745  tlist,
746  scan_clauses);
747  break;
748 
749  case T_ValuesScan:
750  plan = (Plan *) create_valuesscan_plan(root,
751  best_path,
752  tlist,
753  scan_clauses);
754  break;
755 
756  case T_CteScan:
757  plan = (Plan *) create_ctescan_plan(root,
758  best_path,
759  tlist,
760  scan_clauses);
761  break;
762 
763  case T_NamedTuplestoreScan:
765  best_path,
766  tlist,
767  scan_clauses);
768  break;
769 
770  case T_Result:
771  plan = (Plan *) create_resultscan_plan(root,
772  best_path,
773  tlist,
774  scan_clauses);
775  break;
776 
777  case T_WorkTableScan:
779  best_path,
780  tlist,
781  scan_clauses);
782  break;
783 
784  case T_ForeignScan:
785  plan = (Plan *) create_foreignscan_plan(root,
786  (ForeignPath *) best_path,
787  tlist,
788  scan_clauses);
789  break;
790 
791  case T_CustomScan:
792  plan = (Plan *) create_customscan_plan(root,
793  (CustomPath *) best_path,
794  tlist,
795  scan_clauses);
796  break;
797 
798  default:
799  elog(ERROR, "unrecognized node type: %d",
800  (int) best_path->pathtype);
801  plan = NULL; /* keep compiler quiet */
802  break;
803  }
804 
805  /*
806  * If there are any pseudoconstant clauses attached to this node, insert a
807  * gating Result node that evaluates the pseudoconstants as one-time
808  * quals.
809  */
810  if (gating_clauses)
811  plan = create_gating_plan(root, best_path, plan, gating_clauses);
812 
813  return plan;
814 }
static SeqScan * create_seqscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:2913
static ValuesScan * create_valuesscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3834
static TableFuncScan * create_tablefuncscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3791
static CustomScan * create_customscan_plan(PlannerInfo *root, CustomPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:4264
static ForeignScan * create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:4109
static BitmapHeapScan * create_bitmap_scan_plan(PlannerInfo *root, BitmapHeapPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3198
static TidScan * create_tidscan_plan(PlannerInfo *root, TidPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3536
static WorkTableScan * create_worktablescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:4049
static FunctionScan * create_functionscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3748
static Result * create_resultscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:4012
static CteScan * create_ctescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3878
static NamedTuplestoreScan * create_namedtuplestorescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3973
static SubqueryScan * create_subqueryscan_plan(PlannerInfo *root, SubqueryScanPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3698
static TidRangeScan * create_tidrangescan_plan(PlannerInfo *root, TidRangePath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3633
static SampleScan * create_samplescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:2951
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:597
#define copyObject(obj)
Definition: nodes.h:244
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:829
List * build_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
Definition: plancat.c:1718

References apply_pathtarget_labeling_to_tlist(), Assert(), RelOptInfo::baserestrictinfo, build_path_tlist(), build_physical_tlist(), castNode, copyObject, CP_IGNORE_TLIST, CP_LABEL_TLIST, create_bitmap_scan_plan(), create_ctescan_plan(), create_customscan_plan(), create_foreignscan_plan(), create_functionscan_plan(), create_gating_plan(), create_indexscan_plan(), create_namedtuplestorescan_plan(), create_resultscan_plan(), create_samplescan_plan(), create_seqscan_plan(), create_subqueryscan_plan(), create_tablefuncscan_plan(), create_tidrangescan_plan(), create_tidscan_plan(), create_valuesscan_plan(), create_worktablescan_plan(), elog(), ERROR, get_gating_quals(), IS_JOIN_REL, list_concat_copy(), NIL, Path::pathtype, plan, and use_physical_tlist().

Referenced by create_plan_recurse().

◆ create_seqscan_plan()

static SeqScan * create_seqscan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 2913 of file createplan.c.

2915 {
2916  SeqScan *scan_plan;
2917  Index scan_relid = best_path->parent->relid;
2918 
2919  /* it should be a base rel... */
2920  Assert(scan_relid > 0);
2921  Assert(best_path->parent->rtekind == RTE_RELATION);
2922 
2923  /* Sort clauses into best execution order */
2924  scan_clauses = order_qual_clauses(root, scan_clauses);
2925 
2926  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2927  scan_clauses = extract_actual_clauses(scan_clauses, false);
2928 
2929  /* Replace any outer-relation variables with nestloop params */
2930  if (best_path->param_info)
2931  {
2932  scan_clauses = (List *)
2933  replace_nestloop_params(root, (Node *) scan_clauses);
2934  }
2935 
2936  scan_plan = make_seqscan(tlist,
2937  scan_clauses,
2938  scan_relid);
2939 
2940  copy_generic_path_info(&scan_plan->scan.plan, best_path);
2941 
2942  return scan_plan;
2943 }
static SeqScan * make_seqscan(List *qptlist, List *qpqual, Index scanrelid)
Definition: createplan.c:5480
Scan scan
Definition: plannodes.h:396

References Assert(), copy_generic_path_info(), extract_actual_clauses(), make_seqscan(), order_qual_clauses(), replace_nestloop_params(), RTE_RELATION, and SeqScan::scan.

Referenced by create_scan_plan().

◆ create_setop_plan()

static SetOp * create_setop_plan ( PlannerInfo root,
SetOpPath best_path,
int  flags 
)
static

Definition at line 2717 of file createplan.c.

2718 {
2719  SetOp *plan;
2720  Plan *subplan;
2721  long numGroups;
2722 
2723  /*
2724  * SetOp doesn't project, so tlist requirements pass through; moreover we
2725  * need grouping columns to be labeled.
2726  */
2727  subplan = create_plan_recurse(root, best_path->subpath,
2728  flags | CP_LABEL_TLIST);
2729 
2730  /* Convert numGroups to long int --- but 'ware overflow! */
2731  numGroups = clamp_cardinality_to_long(best_path->numGroups);
2732 
2733  plan = make_setop(best_path->cmd,
2734  best_path->strategy,
2735  subplan,
2736  best_path->distinctList,
2737  best_path->flagColIdx,
2738  best_path->firstFlag,
2739  numGroups);
2740 
2741  copy_generic_path_info(&plan->plan, (Path *) best_path);
2742 
2743  return plan;
2744 }
static SetOp * make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree, List *distinctList, AttrNumber flagColIdx, int firstFlag, long numGroups)
Definition: createplan.c:6855
List * distinctList
Definition: pathnodes.h:2303
Cardinality numGroups
Definition: pathnodes.h:2306
int firstFlag
Definition: pathnodes.h:2305
Path * subpath
Definition: pathnodes.h:2300
SetOpCmd cmd
Definition: pathnodes.h:2301
SetOpStrategy strategy
Definition: pathnodes.h:2302
AttrNumber flagColIdx
Definition: pathnodes.h:2304

References clamp_cardinality_to_long(), SetOpPath::cmd, copy_generic_path_info(), CP_LABEL_TLIST, create_plan_recurse(), SetOpPath::distinctList, SetOpPath::firstFlag, SetOpPath::flagColIdx, make_setop(), SetOpPath::numGroups, plan, SetOpPath::strategy, and SetOpPath::subpath.

Referenced by create_plan_recurse().

◆ create_sort_plan()

static Sort * create_sort_plan ( PlannerInfo root,
SortPath best_path,
int  flags 
)
static

Definition at line 2178 of file createplan.c.

2179 {
2180  Sort *plan;
2181  Plan *subplan;
2182 
2183  /*
2184  * We don't want any excess columns in the sorted tuples, so request a
2185  * smaller tlist. Otherwise, since Sort doesn't project, tlist
2186  * requirements pass through.
2187  */
2188  subplan = create_plan_recurse(root, best_path->subpath,
2189  flags | CP_SMALL_TLIST);
2190 
2191  /*
2192  * make_sort_from_pathkeys indirectly calls find_ec_member_matching_expr,
2193  * which will ignore any child EC members that don't belong to the given
2194  * relids. Thus, if this sort path is based on a child relation, we must
2195  * pass its relids.
2196  */
2197  plan = make_sort_from_pathkeys(subplan, best_path->path.pathkeys,
2198  IS_OTHER_REL(best_path->subpath->parent) ?
2199  best_path->path.parent->relids : NULL);
2200 
2201  copy_generic_path_info(&plan->plan, (Path *) best_path);
2202 
2203  return plan;
2204 }

References copy_generic_path_info(), CP_SMALL_TLIST, create_plan_recurse(), IS_OTHER_REL, make_sort_from_pathkeys(), SortPath::path, Path::pathkeys, plan, and SortPath::subpath.

Referenced by create_plan_recurse().

◆ create_subqueryscan_plan()

static SubqueryScan * create_subqueryscan_plan ( PlannerInfo root,
SubqueryScanPath best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3698 of file createplan.c.

3700 {
3701  SubqueryScan *scan_plan;
3702  RelOptInfo *rel = best_path->path.parent;
3703  Index scan_relid = rel->relid;
3704  Plan *subplan;
3705 
3706  /* it should be a subquery base rel... */
3707  Assert(scan_relid > 0);
3708  Assert(rel->rtekind == RTE_SUBQUERY);
3709 
3710  /*
3711  * Recursively create Plan from Path for subquery. Since we are entering
3712  * a different planner context (subroot), recurse to create_plan not
3713  * create_plan_recurse.
3714  */
3715  subplan = create_plan(rel->subroot, best_path->subpath);
3716 
3717  /* Sort clauses into best execution order */
3718  scan_clauses = order_qual_clauses(root, scan_clauses);
3719 
3720  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3721  scan_clauses = extract_actual_clauses(scan_clauses, false);
3722 
3723  /* Replace any outer-relation variables with nestloop params */
3724  if (best_path->path.param_info)
3725  {
3726  scan_clauses = (List *)
3727  replace_nestloop_params(root, (Node *) scan_clauses);
3729  rel->subplan_params);
3730  }
3731 
3732  scan_plan = make_subqueryscan(tlist,
3733  scan_clauses,
3734  scan_relid,
3735  subplan);
3736 
3737  copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3738 
3739  return scan_plan;
3740 }
static SubqueryScan * make_subqueryscan(List *qptlist, List *qpqual, Index scanrelid, Plan *subplan)
Definition: createplan.c:5655
void process_subquery_nestloop_params(PlannerInfo *root, List *subplan_params)
Definition: paramassign.c:428
@ RTE_SUBQUERY
Definition: parsenodes.h:1014
List * subplan_params
Definition: pathnodes.h:933
PlannerInfo * subroot
Definition: pathnodes.h:932

References Assert(), copy_generic_path_info(), create_plan(), extract_actual_clauses(), make_subqueryscan(), order_qual_clauses(), SubqueryScanPath::path, process_subquery_nestloop_params(), RelOptInfo::relid, replace_nestloop_params(), RTE_SUBQUERY, RelOptInfo::rtekind, SubqueryScan::scan, SubqueryScanPath::subpath, RelOptInfo::subplan_params, and RelOptInfo::subroot.

Referenced by create_scan_plan().

◆ create_tablefuncscan_plan()

static TableFuncScan * create_tablefuncscan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3791 of file createplan.c.

3793 {
3794  TableFuncScan *scan_plan;
3795  Index scan_relid = best_path->parent->relid;
3796  RangeTblEntry *rte;
3797  TableFunc *tablefunc;
3798 
3799  /* it should be a function base rel... */
3800  Assert(scan_relid > 0);
3801  rte = planner_rt_fetch(scan_relid, root);
3802  Assert(rte->rtekind == RTE_TABLEFUNC);
3803  tablefunc = rte->tablefunc;
3804 
3805  /* Sort clauses into best execution order */
3806  scan_clauses = order_qual_clauses(root, scan_clauses);
3807 
3808  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3809  scan_clauses = extract_actual_clauses(scan_clauses, false);
3810 
3811  /* Replace any outer-relation variables with nestloop params */
3812  if (best_path->param_info)
3813  {
3814  scan_clauses = (List *)
3815  replace_nestloop_params(root, (Node *) scan_clauses);
3816  /* The function expressions could contain nestloop params, too */
3817  tablefunc = (TableFunc *) replace_nestloop_params(root, (Node *) tablefunc);
3818  }
3819 
3820  scan_plan = make_tablefuncscan(tlist, scan_clauses, scan_relid,
3821  tablefunc);
3822