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/pathnode.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, List *mergeJoinConditions, 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 69 of file createplan.c.

◆ CP_IGNORE_TLIST

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

Definition at line 72 of file createplan.c.

◆ CP_LABEL_TLIST

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

Definition at line 71 of file createplan.c.

◆ CP_SMALL_TLIST

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

Definition at line 70 of file createplan.c.

Function Documentation

◆ bitmap_subplan_mark_shared()

static void bitmap_subplan_mark_shared ( Plan plan)
static

Definition at line 5478 of file createplan.c.

5479 {
5480  if (IsA(plan, BitmapAnd))
5481  bitmap_subplan_mark_shared(linitial(((BitmapAnd *) plan)->bitmapplans));
5482  else if (IsA(plan, BitmapOr))
5483  {
5484  ((BitmapOr *) plan)->isshared = true;
5485  bitmap_subplan_mark_shared(linitial(((BitmapOr *) plan)->bitmapplans));
5486  }
5487  else if (IsA(plan, BitmapIndexScan))
5488  ((BitmapIndexScan *) plan)->isshared = true;
5489  else
5490  elog(ERROR, "unrecognized node type: %d", nodeTag(plan));
5491 }
static void bitmap_subplan_mark_shared(Plan *plan)
Definition: createplan.c:5478
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define nodeTag(nodeptr)
Definition: nodes.h:133
#define linitial(l)
Definition: pg_list.h:178
#define plan(x)
Definition: pg_regress.c:162

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 825 of file createplan.c.

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

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

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 2152 of file createplan.c.

2153 {
2154  /*
2155  * If the top plan node can't do projections and its existing target list
2156  * isn't already what we need, we need to add a Result node to help it
2157  * along.
2158  */
2159  if (!is_projection_capable_plan(subplan) &&
2160  !tlist_same_exprs(tlist, subplan->targetlist))
2161  subplan = inject_projection_plan(subplan, tlist,
2162  subplan->parallel_safe &&
2163  tlist_parallel_safe);
2164  else
2165  {
2166  /* Else we can just replace the plan node's tlist */
2167  subplan->targetlist = tlist;
2168  subplan->parallel_safe &= tlist_parallel_safe;
2169  }
2170  return subplan;
2171 }
bool is_projection_capable_plan(Plan *plan)
Definition: createplan.c:7257
static Plan * inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
Definition: createplan.c:2120
bool parallel_safe
Definition: plannodes.h:141
List * targetlist
Definition: plannodes.h:152
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 5409 of file createplan.c.

5410 {
5411  dest->startup_cost = src->startup_cost;
5412  dest->total_cost = src->total_cost;
5413  dest->plan_rows = src->rows;
5414  dest->plan_width = src->pathtarget->width;
5415  dest->parallel_aware = src->parallel_aware;
5416  dest->parallel_safe = src->parallel_safe;
5417 }
Cardinality rows
Definition: pathnodes.h:1660
Cost startup_cost
Definition: pathnodes.h:1661
Cost total_cost
Definition: pathnodes.h:1662
bool parallel_aware
Definition: pathnodes.h:1653
bool parallel_safe
Definition: pathnodes.h:1655

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 5424 of file createplan.c.

5425 {
5426  dest->startup_cost = src->startup_cost;
5427  dest->total_cost = src->total_cost;
5428  dest->plan_rows = src->plan_rows;
5429  dest->plan_width = src->plan_width;
5430  /* Assume the inserted node is not parallel-aware. */
5431  dest->parallel_aware = false;
5432  /* Assume the inserted node is parallel-safe, if child plan is. */
5433  dest->parallel_safe = src->parallel_safe;
5434 }
Cost total_cost
Definition: plannodes.h:129
Cost startup_cost
Definition: plannodes.h:128
int plan_width
Definition: plannodes.h:135
Cardinality plan_rows
Definition: plannodes.h:134

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 2308 of file createplan.c.

2309 {
2310  Agg *plan;
2311  Plan *subplan;
2312  List *tlist;
2313  List *quals;
2314 
2315  /*
2316  * Agg can project, so no need to be terribly picky about child tlist, but
2317  * we do need grouping columns to be available
2318  */
2319  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2320 
2321  tlist = build_path_tlist(root, &best_path->path);
2322 
2323  quals = order_qual_clauses(root, best_path->qual);
2324 
2325  plan = make_agg(tlist, quals,
2326  best_path->aggstrategy,
2327  best_path->aggsplit,
2328  list_length(best_path->groupClause),
2330  subplan->targetlist),
2331  extract_grouping_ops(best_path->groupClause),
2333  subplan->targetlist),
2334  NIL,
2335  NIL,
2336  best_path->numGroups,
2337  best_path->transitionSpace,
2338  subplan);
2339 
2340  copy_generic_path_info(&plan->plan, (Path *) best_path);
2341 
2342  return plan;
2343 }
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5315
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:6593
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5409
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:388
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:825
#define CP_LABEL_TLIST
Definition: createplan.c:71
static int list_length(const List *l)
Definition: pg_list.h:152
Path * subpath
Definition: pathnodes.h:2254
Cardinality numGroups
Definition: pathnodes.h:2257
AggSplit aggsplit
Definition: pathnodes.h:2256
List * groupClause
Definition: pathnodes.h:2259
uint64 transitionSpace
Definition: pathnodes.h:2258
AggStrategy aggstrategy
Definition: pathnodes.h:2255
Path path
Definition: pathnodes.h:2253
List * qual
Definition: pathnodes.h:2260
Definition: plannodes.h:997
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, root, 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 1216 of file createplan.c.

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

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(), root, 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 3201 of file createplan.c.

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

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(), root, 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 3331 of file createplan.c.

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

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, root, 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 3890 of file createplan.c.

3892 {
3893  CteScan *scan_plan;
3894  Index scan_relid = best_path->parent->relid;
3895  RangeTblEntry *rte;
3896  SubPlan *ctesplan = NULL;
3897  int plan_id;
3898  int cte_param_id;
3899  PlannerInfo *cteroot;
3900  Index levelsup;
3901  int ndx;
3902  ListCell *lc;
3903 
3904  Assert(scan_relid > 0);
3905  rte = planner_rt_fetch(scan_relid, root);
3906  Assert(rte->rtekind == RTE_CTE);
3907  Assert(!rte->self_reference);
3908 
3909  /*
3910  * Find the referenced CTE, and locate the SubPlan previously made for it.
3911  */
3912  levelsup = rte->ctelevelsup;
3913  cteroot = root;
3914  while (levelsup-- > 0)
3915  {
3916  cteroot = cteroot->parent_root;
3917  if (!cteroot) /* shouldn't happen */
3918  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3919  }
3920 
3921  /*
3922  * Note: cte_plan_ids can be shorter than cteList, if we are still working
3923  * on planning the CTEs (ie, this is a side-reference from another CTE).
3924  * So we mustn't use forboth here.
3925  */
3926  ndx = 0;
3927  foreach(lc, cteroot->parse->cteList)
3928  {
3929  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
3930 
3931  if (strcmp(cte->ctename, rte->ctename) == 0)
3932  break;
3933  ndx++;
3934  }
3935  if (lc == NULL) /* shouldn't happen */
3936  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
3937  if (ndx >= list_length(cteroot->cte_plan_ids))
3938  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3939  plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
3940  if (plan_id <= 0)
3941  elog(ERROR, "no plan was made for CTE \"%s\"", rte->ctename);
3942  foreach(lc, cteroot->init_plans)
3943  {
3944  ctesplan = (SubPlan *) lfirst(lc);
3945  if (ctesplan->plan_id == plan_id)
3946  break;
3947  }
3948  if (lc == NULL) /* shouldn't happen */
3949  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3950 
3951  /*
3952  * We need the CTE param ID, which is the sole member of the SubPlan's
3953  * setParam list.
3954  */
3955  cte_param_id = linitial_int(ctesplan->setParam);
3956 
3957  /* Sort clauses into best execution order */
3958  scan_clauses = order_qual_clauses(root, scan_clauses);
3959 
3960  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3961  scan_clauses = extract_actual_clauses(scan_clauses, false);
3962 
3963  /* Replace any outer-relation variables with nestloop params */
3964  if (best_path->param_info)
3965  {
3966  scan_clauses = (List *)
3967  replace_nestloop_params(root, (Node *) scan_clauses);
3968  }
3969 
3970  scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
3971  plan_id, cte_param_id);
3972 
3973  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3974 
3975  return scan_plan;
3976 }
static CteScan * make_ctescan(List *qptlist, List *qpqual, Index scanrelid, int ctePlanId, int cteParam)
Definition: createplan.c:5762
@ RTE_CTE
Definition: parsenodes.h:1034
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:564
#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:642
List * cte_plan_ids
Definition: pathnodes.h:305
List * init_plans
Definition: pathnodes.h:299
Query * parse
Definition: pathnodes.h:202
List * cteList
Definition: parsenodes.h:166
char * ctename
Definition: parsenodes.h:1206
Index ctelevelsup
Definition: parsenodes.h:1208
RTEKind rtekind
Definition: parsenodes.h:1057
int plan_id
Definition: primnodes.h:1070
List * setParam
Definition: primnodes.h:1088

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(), root, RTE_CTE, RangeTblEntry::rtekind, CteScan::scan, 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 4276 of file createplan.c.

4278 {
4279  CustomScan *cplan;
4280  RelOptInfo *rel = best_path->path.parent;
4281  List *custom_plans = NIL;
4282  ListCell *lc;
4283 
4284  /* Recursively transform child paths. */
4285  foreach(lc, best_path->custom_paths)
4286  {
4288  CP_EXACT_TLIST);
4289 
4290  custom_plans = lappend(custom_plans, plan);
4291  }
4292 
4293  /*
4294  * Sort clauses into the best execution order, although custom-scan
4295  * provider can reorder them again.
4296  */
4297  scan_clauses = order_qual_clauses(root, scan_clauses);
4298 
4299  /*
4300  * Invoke custom plan provider to create the Plan node represented by the
4301  * CustomPath.
4302  */
4303  cplan = castNode(CustomScan,
4304  best_path->methods->PlanCustomPath(root,
4305  rel,
4306  best_path,
4307  tlist,
4308  scan_clauses,
4309  custom_plans));
4310 
4311  /*
4312  * Copy cost data from Path to Plan; no need to make custom-plan providers
4313  * do this
4314  */
4315  copy_generic_path_info(&cplan->scan.plan, &best_path->path);
4316 
4317  /* Likewise, copy the relids that are represented by this custom scan */
4318  cplan->custom_relids = best_path->path.parent->relids;
4319 
4320  /*
4321  * Replace any outer-relation variables with nestloop params in the qual
4322  * and custom_exprs expressions. We do this last so that the custom-plan
4323  * provider doesn't have to be involved. (Note that parts of custom_exprs
4324  * could have come from join clauses, so doing this beforehand on the
4325  * scan_clauses wouldn't work.) We assume custom_scan_tlist contains no
4326  * such variables.
4327  */
4328  if (best_path->path.param_info)
4329  {
4330  cplan->scan.plan.qual = (List *)
4331  replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
4332  cplan->custom_exprs = (List *)
4334  }
4335 
4336  return cplan;
4337 }
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:1911
List * custom_paths
Definition: pathnodes.h:1908
Scan scan
Definition: plannodes.h:741
Bitmapset * custom_relids
Definition: plannodes.h:748
List * custom_exprs
Definition: plannodes.h:745

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(), root, 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 4121 of file createplan.c.

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

References Assert, RelOptInfo::baserestrictinfo, bms_difference(), bms_free(), bms_is_member(), ForeignScan::checkAsUser, RestrictInfo::clause, copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), 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, i, InvalidOid, lfirst, order_qual_clauses(), ForeignPath::path, planner_rt_fetch, pull_varattnos(), RangeTblEntry::relid, RelOptInfo::relid, RELOPT_UPPER_REL, RelOptInfo::reloptkind, RelOptInfo::reltarget, replace_nestloop_params(), root, 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 3760 of file createplan.c.

3762 {
3763  FunctionScan *scan_plan;
3764  Index scan_relid = best_path->parent->relid;
3765  RangeTblEntry *rte;
3766  List *functions;
3767 
3768  /* it should be a function base rel... */
3769  Assert(scan_relid > 0);
3770  rte = planner_rt_fetch(scan_relid, root);
3771  Assert(rte->rtekind == RTE_FUNCTION);
3772  functions = rte->functions;
3773 
3774  /* Sort clauses into best execution order */
3775  scan_clauses = order_qual_clauses(root, scan_clauses);
3776 
3777  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3778  scan_clauses = extract_actual_clauses(scan_clauses, false);
3779 
3780  /* Replace any outer-relation variables with nestloop params */
3781  if (best_path->param_info)
3782  {
3783  scan_clauses = (List *)
3784  replace_nestloop_params(root, (Node *) scan_clauses);
3785  /* The function expressions could contain nestloop params, too */
3787  }
3788 
3789  scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
3790  functions, rte->funcordinality);
3791 
3792  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3793 
3794  return scan_plan;
3795 }
static FunctionScan * make_functionscan(List *qptlist, List *qpqual, Index scanrelid, List *functions, bool funcordinality)
Definition: createplan.c:5703
@ RTE_FUNCTION
Definition: parsenodes.h:1031
static const struct fns functions
Definition: regcomp.c:356
bool funcordinality
Definition: parsenodes.h:1189
List * functions
Definition: parsenodes.h:1187

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(), root, 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 1957 of file createplan.c.

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

References Assert, assign_special_exec_param(), build_path_tlist(), copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), elog, ERROR, Plan::lefttree, makeNode, NIL, GatherMergePath::num_workers, GatherMerge::num_workers, GatherMerge::numCols, GatherMergePath::path, Path::pathkeys, pathkeys_contained_in(), GatherMerge::plan, prepare_sort_from_pathkeys(), GatherMerge::rescan_param, root, 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 1919 of file createplan.c.

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

References assign_special_exec_param(), build_path_tlist(), copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), make_gather(), NIL, GatherPath::num_workers, GatherPath::path, Gather::plan, root, 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 1022 of file createplan.c.

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

References Assert, build_path_tlist(), copy_plan_costsize(), IsA, Plan::lefttree, make_result(), Path::parallel_safe, Plan::parallel_safe, Result::plan, plan, Result::resconstantqual, root, 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 2241 of file createplan.c.

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

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, root, 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 1587 of file createplan.c.

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

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

Referenced by create_plan_recurse().

◆ create_groupingsets_plan()

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

Definition at line 2392 of file createplan.c.

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

4748 {
4749  HashJoin *join_plan;
4750  Hash *hash_plan;
4751  Plan *outer_plan;
4752  Plan *inner_plan;
4753  List *tlist = build_path_tlist(root, &best_path->jpath.path);
4754  List *joinclauses;
4755  List *otherclauses;
4756  List *hashclauses;
4757  List *hashoperators = NIL;
4758  List *hashcollations = NIL;
4759  List *inner_hashkeys = NIL;
4760  List *outer_hashkeys = NIL;
4761  Oid skewTable = InvalidOid;
4762  AttrNumber skewColumn = InvalidAttrNumber;
4763  bool skewInherit = false;
4764  ListCell *lc;
4765 
4766  /*
4767  * HashJoin can project, so we don't have to demand exact tlists from the
4768  * inputs. However, it's best to request a small tlist from the inner
4769  * side, so that we aren't storing more data than necessary. Likewise, if
4770  * we anticipate batching, request a small tlist from the outer side so
4771  * that we don't put extra data in the outer batch files.
4772  */
4773  outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4774  (best_path->num_batches > 1) ? CP_SMALL_TLIST : 0);
4775 
4776  inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4777  CP_SMALL_TLIST);
4778 
4779  /* Sort join qual clauses into best execution order */
4780  joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4781  /* There's no point in sorting the hash clauses ... */
4782 
4783  /* Get the join qual clauses (in plain expression form) */
4784  /* Any pseudoconstant clauses are ignored here */
4785  if (IS_OUTER_JOIN(best_path->jpath.jointype))
4786  {
4787  extract_actual_join_clauses(joinclauses,
4788  best_path->jpath.path.parent->relids,
4789  &joinclauses, &otherclauses);
4790  }
4791  else
4792  {
4793  /* We can treat all clauses alike for an inner join */
4794  joinclauses = extract_actual_clauses(joinclauses, false);
4795  otherclauses = NIL;
4796  }
4797 
4798  /*
4799  * Remove the hashclauses from the list of join qual clauses, leaving the
4800  * list of quals that must be checked as qpquals.
4801  */
4802  hashclauses = get_actual_clauses(best_path->path_hashclauses);
4803  joinclauses = list_difference(joinclauses, hashclauses);
4804 
4805  /*
4806  * Replace any outer-relation variables with nestloop params. There
4807  * should not be any in the hashclauses.
4808  */
4809  if (best_path->jpath.path.param_info)
4810  {
4811  joinclauses = (List *)
4812  replace_nestloop_params(root, (Node *) joinclauses);
4813  otherclauses = (List *)
4814  replace_nestloop_params(root, (Node *) otherclauses);
4815  }
4816 
4817  /*
4818  * Rearrange hashclauses, if needed, so that the outer variable is always
4819  * on the left.
4820  */
4821  hashclauses = get_switched_clauses(best_path->path_hashclauses,
4822  best_path->jpath.outerjoinpath->parent->relids);
4823 
4824  /*
4825  * If there is a single join clause and we can identify the outer variable
4826  * as a simple column reference, supply its identity for possible use in
4827  * skew optimization. (Note: in principle we could do skew optimization
4828  * with multiple join clauses, but we'd have to be able to determine the
4829  * most common combinations of outer values, which we don't currently have
4830  * enough stats for.)
4831  */
4832  if (list_length(hashclauses) == 1)
4833  {
4834  OpExpr *clause = (OpExpr *) linitial(hashclauses);
4835  Node *node;
4836 
4837  Assert(is_opclause(clause));
4838  node = (Node *) linitial(clause->args);
4839  if (IsA(node, RelabelType))
4840  node = (Node *) ((RelabelType *) node)->arg;
4841  if (IsA(node, Var))
4842  {
4843  Var *var = (Var *) node;
4844  RangeTblEntry *rte;
4845 
4846  rte = root->simple_rte_array[var->varno];
4847  if (rte->rtekind == RTE_RELATION)
4848  {
4849  skewTable = rte->relid;
4850  skewColumn = var->varattno;
4851  skewInherit = rte->inh;
4852  }
4853  }
4854  }
4855 
4856  /*
4857  * Collect hash related information. The hashed expressions are
4858  * deconstructed into outer/inner expressions, so they can be computed
4859  * separately (inner expressions are used to build the hashtable via Hash,
4860  * outer expressions to perform lookups of tuples from HashJoin's outer
4861  * plan in the hashtable). Also collect operator information necessary to
4862  * build the hashtable.
4863  */
4864  foreach(lc, hashclauses)
4865  {
4866  OpExpr *hclause = lfirst_node(OpExpr, lc);
4867 
4868  hashoperators = lappend_oid(hashoperators, hclause->opno);
4869  hashcollations = lappend_oid(hashcollations, hclause->inputcollid);
4870  outer_hashkeys = lappend(outer_hashkeys, linitial(hclause->args));
4871  inner_hashkeys = lappend(inner_hashkeys, lsecond(hclause->args));
4872  }
4873 
4874  /*
4875  * Build the hash node and hash join node.
4876  */
4877  hash_plan = make_hash(inner_plan,
4878  inner_hashkeys,
4879  skewTable,
4880  skewColumn,
4881  skewInherit);
4882 
4883  /*
4884  * Set Hash node's startup & total costs equal to total cost of input
4885  * plan; this only affects EXPLAIN display not decisions.
4886  */
4887  copy_plan_costsize(&hash_plan->plan, inner_plan);
4888  hash_plan->plan.startup_cost = hash_plan->plan.total_cost;
4889 
4890  /*
4891  * If parallel-aware, the executor will also need an estimate of the total
4892  * number of rows expected from all participants so that it can size the
4893  * shared hash table.
4894  */
4895  if (best_path->jpath.path.parallel_aware)
4896  {
4897  hash_plan->plan.parallel_aware = true;
4898  hash_plan->rows_total = best_path->inner_rows_total;
4899  }
4900 
4901  join_plan = make_hashjoin(tlist,
4902  joinclauses,
4903  otherclauses,
4904  hashclauses,
4905  hashoperators,
4906  hashcollations,
4907  outer_hashkeys,
4908  outer_plan,
4909  (Plan *) hash_plan,
4910  best_path->jpath.jointype,
4911  best_path->jpath.inner_unique);
4912 
4913  copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4914 
4915  return join_plan;
4916 }
#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:5973
static List * get_switched_clauses(List *clauses, Relids outerrelids)
Definition: createplan.c:5238
static Hash * make_hash(Plan *lefttree, List *hashkeys, Oid skewTable, AttrNumber skewColumn, bool skewInherit)
Definition: createplan.c:6004
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
List * list_difference(const List *list1, const List *list2)
Definition: list.c:1237
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:74
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:337
#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:864
List * path_hashclauses
Definition: pathnodes.h:2152
Cardinality inner_rows_total
Definition: pathnodes.h:2154
int num_batches
Definition: pathnodes.h:2153
JoinPath jpath
Definition: pathnodes.h:2151
Cardinality rows_total
Definition: plannodes.h:1210
Plan plan
Definition: plannodes.h:1199
Path * outerjoinpath
Definition: pathnodes.h:2074
Path * innerjoinpath
Definition: pathnodes.h:2075
JoinType jointype
Definition: pathnodes.h:2069
bool inner_unique
Definition: pathnodes.h:2071
List * joinrestrictinfo
Definition: pathnodes.h:2077
Oid opno
Definition: primnodes.h:818
List * args
Definition: primnodes.h:836
bool parallel_aware
Definition: plannodes.h:140
Definition: primnodes.h:248
AttrNumber varattno
Definition: primnodes.h:260
int varno
Definition: primnodes.h:255

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(), root, 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 2214 of file createplan.c.

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

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, root, 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 3005 of file createplan.c.

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

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(), root, 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 1081 of file createplan.c.

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

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, plan, and root.

Referenced by create_plan_recurse().

◆ create_limit_plan()

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

Definition at line 2855 of file createplan.c.

2856 {
2857  Limit *plan;
2858  Plan *subplan;
2859  int numUniqkeys = 0;
2860  AttrNumber *uniqColIdx = NULL;
2861  Oid *uniqOperators = NULL;
2862  Oid *uniqCollations = NULL;
2863 
2864  /* Limit doesn't project, so tlist requirements pass through */
2865  subplan = create_plan_recurse(root, best_path->subpath, flags);
2866 
2867  /* Extract information necessary for comparing rows for WITH TIES. */
2868  if (best_path->limitOption == LIMIT_OPTION_WITH_TIES)
2869  {
2870  Query *parse = root->parse;
2871  ListCell *l;
2872 
2873  numUniqkeys = list_length(parse->sortClause);
2874  uniqColIdx = (AttrNumber *) palloc(numUniqkeys * sizeof(AttrNumber));
2875  uniqOperators = (Oid *) palloc(numUniqkeys * sizeof(Oid));
2876  uniqCollations = (Oid *) palloc(numUniqkeys * sizeof(Oid));
2877 
2878  numUniqkeys = 0;
2879  foreach(l, parse->sortClause)
2880  {
2881  SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
2882  TargetEntry *tle = get_sortgroupclause_tle(sortcl, parse->targetList);
2883 
2884  uniqColIdx[numUniqkeys] = tle->resno;
2885  uniqOperators[numUniqkeys] = sortcl->eqop;
2886  uniqCollations[numUniqkeys] = exprCollation((Node *) tle->expr);
2887  numUniqkeys++;
2888  }
2889  }
2890 
2891  plan = make_limit(subplan,
2892  best_path->limitOffset,
2893  best_path->limitCount,
2894  best_path->limitOption,
2895  numUniqkeys, uniqColIdx, uniqOperators, uniqCollations);
2896 
2897  copy_generic_path_info(&plan->plan, (Path *) best_path);
2898 
2899  return plan;
2900 }
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount, LimitOption limitOption, int uniqNumCols, AttrNumber *uniqColIdx, Oid *uniqOperators, Oid *uniqCollations)
Definition: createplan.c:6960
void * palloc(Size size)
Definition: mcxt.c:1316
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:816
@ LIMIT_OPTION_WITH_TIES
Definition: nodes.h:431
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:2401
LimitOption limitOption
Definition: pathnodes.h:2404
Node * limitOffset
Definition: pathnodes.h:2402
Node * limitCount
Definition: pathnodes.h:2403
Expr * expr
Definition: primnodes.h:2192

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(), plan, TargetEntry::resno, root, 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 2791 of file createplan.c.

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

References copy_generic_path_info(), create_plan_recurse(), LockRowsPath::epqParam, make_lockrows(), plan, root, 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 1638 of file createplan.c.

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

References copy_generic_path_info(), CP_SMALL_TLIST, create_plan_recurse(), make_material(), plan, root, 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 1666 of file createplan.c.

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

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(), root, 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 1437 of file createplan.c.

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

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, root, 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 4439 of file createplan.c.

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

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(), root, 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 2550 of file createplan.c.

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

References Assert, build_path_tlist(), copy_generic_path_info(), create_plan(), lfirst, Query::limitCount, Query::limitOffset, Query::limitOption, make_limit(), make_result(), MinMaxAggPath::mmaggregates, NIL, Path::parallel_safe, MinMaxAggInfo::param, PlannerInfo::parse, MinMaxAggPath::path, MinMaxAggInfo::path, MinMaxAggInfo::pathcost, plan, MinMaxAggPath::quals, root, 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 2814 of file createplan.c.

2815 {
2816  ModifyTable *plan;
2817  Path *subpath = best_path->subpath;
2818  Plan *subplan;
2819 
2820  /* Subplan must produce exactly the specified tlist */
2822 
2823  /* Transfer resname/resjunk labeling, too, to keep executor happy */
2824  apply_tlist_labeling(subplan->targetlist, root->processed_tlist);
2825 
2827  subplan,
2828  best_path->operation,
2829  best_path->canSetTag,
2830  best_path->nominalRelation,
2831  best_path->rootRelation,
2832  best_path->partColsUpdated,
2833  best_path->resultRelations,
2834  best_path->updateColnosLists,
2835  best_path->withCheckOptionLists,
2836  best_path->returningLists,
2837  best_path->rowMarks,
2838  best_path->onconflict,
2839  best_path->mergeActionLists,
2840  best_path->mergeJoinConditions,
2841  best_path->epqParam);
2842 
2843  copy_generic_path_info(&plan->plan, &best_path->path);
2844 
2845  return plan;
2846 }
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, List *mergeJoinConditions, int epqParam)
Definition: createplan.c:7028
bool partColsUpdated
Definition: pathnodes.h:2381
List * returningLists
Definition: pathnodes.h:2385
List * resultRelations
Definition: pathnodes.h:2382
List * withCheckOptionLists
Definition: pathnodes.h:2384
List * mergeJoinConditions
Definition: pathnodes.h:2391
List * updateColnosLists
Definition: pathnodes.h:2383
OnConflictExpr * onconflict
Definition: pathnodes.h:2387
CmdType operation
Definition: pathnodes.h:2377
Index rootRelation
Definition: pathnodes.h:2380
Index nominalRelation
Definition: pathnodes.h:2379
List * mergeActionLists
Definition: pathnodes.h:2389
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::mergeJoinConditions, ModifyTablePath::nominalRelation, ModifyTablePath::onconflict, ModifyTablePath::operation, ModifyTablePath::partColsUpdated, ModifyTablePath::path, plan, ModifyTablePath::resultRelations, ModifyTablePath::returningLists, root, 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 3985 of file createplan.c.

3987 {
3988  NamedTuplestoreScan *scan_plan;
3989  Index scan_relid = best_path->parent->relid;
3990  RangeTblEntry *rte;
3991 
3992  Assert(scan_relid > 0);
3993  rte = planner_rt_fetch(scan_relid, root);
3995 
3996  /* Sort clauses into best execution order */
3997  scan_clauses = order_qual_clauses(root, scan_clauses);
3998 
3999  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
4000  scan_clauses = extract_actual_clauses(scan_clauses, false);
4001 
4002  /* Replace any outer-relation variables with nestloop params */
4003  if (best_path->param_info)
4004  {
4005  scan_clauses = (List *)
4006  replace_nestloop_params(root, (Node *) scan_clauses);
4007  }
4008 
4009  scan_plan = make_namedtuplestorescan(tlist, scan_clauses, scan_relid,
4010  rte->enrname);
4011 
4012  copy_generic_path_info(&scan_plan->scan.plan, best_path);
4013 
4014  return scan_plan;
4015 }
static NamedTuplestoreScan * make_namedtuplestorescan(List *qptlist, List *qpqual, Index scanrelid, char *enrname)
Definition: createplan.c:5783
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1035
char * enrname
Definition: parsenodes.h:1241

References Assert, copy_generic_path_info(), RangeTblEntry::enrname, extract_actual_clauses(), make_namedtuplestorescan(), order_qual_clauses(), planner_rt_fetch, replace_nestloop_params(), root, 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 4347 of file createplan.c.

4349 {
4350  NestLoop *join_plan;
4351  Plan *outer_plan;
4352  Plan *inner_plan;
4353  List *tlist = build_path_tlist(root, &best_path->jpath.path);
4354  List *joinrestrictclauses = best_path->jpath.joinrestrictinfo;
4355  List *joinclauses;
4356  List *otherclauses;
4357  Relids outerrelids;
4358  List *nestParams;
4359  Relids saveOuterRels = root->curOuterRels;
4360 
4361  /*
4362  * If the inner path is parameterized by the topmost parent of the outer
4363  * rel rather than the outer rel itself, fix that. (Nothing happens here
4364  * if it is not so parameterized.)
4365  */
4366  best_path->jpath.innerjoinpath =
4368  best_path->jpath.innerjoinpath,
4369  best_path->jpath.outerjoinpath->parent);
4370 
4371  /*
4372  * Failure here probably means that reparameterize_path_by_child() is not
4373  * in sync with path_is_reparameterizable_by_child().
4374  */
4375  Assert(best_path->jpath.innerjoinpath != NULL);
4376 
4377  /* NestLoop can project, so no need to be picky about child tlists */
4378  outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath, 0);
4379 
4380  /* For a nestloop, include outer relids in curOuterRels for inner side */
4381  root->curOuterRels = bms_union(root->curOuterRels,
4382  best_path->jpath.outerjoinpath->parent->relids);
4383 
4384  inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath, 0);
4385 
4386  /* Restore curOuterRels */
4387  bms_free(root->curOuterRels);
4388  root->curOuterRels = saveOuterRels;
4389 
4390  /* Sort join qual clauses into best execution order */
4391  joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
4392 
4393  /* Get the join qual clauses (in plain expression form) */
4394  /* Any pseudoconstant clauses are ignored here */
4395  if (IS_OUTER_JOIN(best_path->jpath.jointype))
4396  {
4397  extract_actual_join_clauses(joinrestrictclauses,
4398  best_path->jpath.path.parent->relids,
4399  &joinclauses, &otherclauses);
4400  }
4401  else
4402  {
4403  /* We can treat all clauses alike for an inner join */
4404  joinclauses = extract_actual_clauses(joinrestrictclauses, false);
4405  otherclauses = NIL;
4406  }
4407 
4408  /* Replace any outer-relation variables with nestloop params */
4409  if (best_path->jpath.path.param_info)
4410  {
4411  joinclauses = (List *)
4412  replace_nestloop_params(root, (Node *) joinclauses);
4413  otherclauses = (List *)
4414  replace_nestloop_params(root, (Node *) otherclauses);
4415  }
4416 
4417  /*
4418  * Identify any nestloop parameters that should be supplied by this join
4419  * node, and remove them from root->curOuterParams.
4420  */
4421  outerrelids = best_path->jpath.outerjoinpath->parent->relids;
4422  nestParams = identify_current_nestloop_params(root, outerrelids);
4423 
4424  join_plan = make_nestloop(tlist,
4425  joinclauses,
4426  otherclauses,
4427  nestParams,
4428  outer_plan,
4429  inner_plan,
4430  best_path->jpath.jointype,
4431  best_path->jpath.inner_unique);
4432 
4433  copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4434 
4435  return join_plan;
4436 }
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:251
static NestLoop * make_nestloop(List *tlist, List *joinclauses, List *otherclauses, List *nestParams, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
Definition: createplan.c:5948
List * identify_current_nestloop_params(PlannerInfo *root, Relids leftrelids)
Definition: paramassign.c:582
Path * reparameterize_path_by_child(PlannerInfo *root, Path *path, RelOptInfo *child_rel)
Definition: pathnode.c:4115
Join join
Definition: plannodes.h:809
JoinPath jpath
Definition: pathnodes.h:2092

References Assert, bms_free(), bms_union(), build_path_tlist(), copy_generic_path_info(), create_plan_recurse(), 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, reparameterize_path_by_child(), replace_nestloop_params(), and root.

Referenced by create_join_plan().

◆ create_plan()

Plan* create_plan ( PlannerInfo root,
Path best_path 
)

Definition at line 337 of file createplan.c.

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

References apply_tlist_labeling(), Assert, CP_EXACT_TLIST, create_plan_recurse(), elog, ERROR, IsA, NIL, plan, root, 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 388 of file createplan.c.

389 {
390  Plan *plan;
391 
392  /* Guard against stack overflow due to overly complex plans */
394 
395  switch (best_path->pathtype)
396  {
397  case T_SeqScan:
398  case T_SampleScan:
399  case T_IndexScan:
400  case T_IndexOnlyScan:
401  case T_BitmapHeapScan:
402  case T_TidScan:
403  case T_TidRangeScan:
404  case T_SubqueryScan:
405  case T_FunctionScan:
406  case T_TableFuncScan:
407  case T_ValuesScan:
408  case T_CteScan:
409  case T_WorkTableScan:
410  case T_NamedTuplestoreScan:
411  case T_ForeignScan:
412  case T_CustomScan:
413  plan = create_scan_plan(root, best_path, flags);
414  break;
415  case T_HashJoin:
416  case T_MergeJoin:
417  case T_NestLoop:
419  (JoinPath *) best_path);
420  break;
421  case T_Append:
423  (AppendPath *) best_path,
424  flags);
425  break;
426  case T_MergeAppend:
428  (MergeAppendPath *) best_path,
429  flags);
430  break;
431  case T_Result:
432  if (IsA(best_path, ProjectionPath))
433  {
435  (ProjectionPath *) best_path,
436  flags);
437  }
438  else if (IsA(best_path, MinMaxAggPath))
439  {
441  (MinMaxAggPath *) best_path);
442  }
443  else if (IsA(best_path, GroupResultPath))
444  {
446  (GroupResultPath *) best_path);
447  }
448  else
449  {
450  /* Simple RTE_RESULT base relation */
451  Assert(IsA(best_path, Path));
452  plan = create_scan_plan(root, best_path, flags);
453  }
454  break;
455  case T_ProjectSet:
457  (ProjectSetPath *) best_path);
458  break;
459  case T_Material:
461  (MaterialPath *) best_path,
462  flags);
463  break;
464  case T_Memoize:
466  (MemoizePath *) best_path,
467  flags);
468  break;
469  case T_Unique:
470  if (IsA(best_path, UpperUniquePath))
471  {
473  (UpperUniquePath *) best_path,
474  flags);
475  }
476  else
477  {
478  Assert(IsA(best_path, UniquePath));
480  (UniquePath *) best_path,
481  flags);
482  }
483  break;
484  case T_Gather:
486  (GatherPath *) best_path);
487  break;
488  case T_Sort:
490  (SortPath *) best_path,
491  flags);
492  break;
493  case T_IncrementalSort:
495  (IncrementalSortPath *) best_path,
496  flags);
497  break;
498  case T_Group:
500  (GroupPath *) best_path);
501  break;
502  case T_Agg:
503  if (IsA(best_path, GroupingSetsPath))
505  (GroupingSetsPath *) best_path);
506  else
507  {
508  Assert(IsA(best_path, AggPath));
510  (AggPath *) best_path);
511  }
512  break;
513  case T_WindowAgg:
515  (WindowAggPath *) best_path);
516  break;
517  case T_SetOp:
519  (SetOpPath *) best_path,
520  flags);
521  break;
522  case T_RecursiveUnion:
524  (RecursiveUnionPath *) best_path);
525  break;
526  case T_LockRows:
528  (LockRowsPath *) best_path,
529  flags);
530  break;
531  case T_ModifyTable:
533  (ModifyTablePath *) best_path);
534  break;
535  case T_Limit:
537  (LimitPath *) best_path,
538  flags);
539  break;
540  case T_GatherMerge:
542  (GatherMergePath *) best_path);
543  break;
544  default:
545  elog(ERROR, "unrecognized node type: %d",
546  (int) best_path->pathtype);
547  plan = NULL; /* keep compiler quiet */
548  break;
549  }
550 
551  return plan;
552 }
static Plan * create_join_plan(PlannerInfo *root, JoinPath *best_path)
Definition: createplan.c:1081
static Plan * create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path, int flags)
Definition: createplan.c:1437
static GatherMerge * create_gather_merge_plan(PlannerInfo *root, GatherMergePath *best_path)
Definition: createplan.c:1957
static Plan * create_append_plan(PlannerInfo *root, AppendPath *best_path, int flags)
Definition: createplan.c:1216
static Result * create_group_result_plan(PlannerInfo *root, GroupResultPath *best_path)
Definition: createplan.c:1587
static Limit * create_limit_plan(PlannerInfo *root, LimitPath *best_path, int flags)
Definition: createplan.c:2855
static Agg * create_agg_plan(PlannerInfo *root, AggPath *best_path)
Definition: createplan.c:2308
static SetOp * create_setop_plan(PlannerInfo *root, SetOpPath *best_path, int flags)
Definition: createplan.c:2719
static Sort * create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags)
Definition: createplan.c:2180
static Unique * create_upper_unique_plan(PlannerInfo *root, UpperUniquePath *best_path, int flags)
Definition: createplan.c:2280
static Gather * create_gather_plan(PlannerInfo *root, GatherPath *best_path)
Definition: createplan.c:1919
static ProjectSet * create_project_set_plan(PlannerInfo *root, ProjectSetPath *best_path)
Definition: createplan.c:1612
static Group * create_group_plan(PlannerInfo *root, GroupPath *best_path)
Definition: createplan.c:2241
static ModifyTable * create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path)
Definition: createplan.c:2814
static Result * create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path)
Definition: createplan.c:2550
static LockRows * create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path, int flags)
Definition: createplan.c:2791
static Material * create_material_plan(PlannerInfo *root, MaterialPath *best_path, int flags)
Definition: createplan.c:1638
static Plan * create_scan_plan(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:559
static IncrementalSort * create_incrementalsort_plan(PlannerInfo *root, IncrementalSortPath *best_path, int flags)
Definition: createplan.c:2214
static Plan * create_projection_plan(PlannerInfo *root, ProjectionPath *best_path, int flags)
Definition: createplan.c:2018
static Memoize * create_memoize_plan(PlannerInfo *root, MemoizePath *best_path, int flags)
Definition: createplan.c:1666
static WindowAgg * create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
Definition: createplan.c:2616
static Plan * create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path)
Definition: createplan.c:2392
static RecursiveUnion * create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path)
Definition: createplan.c:2755
static Plan * create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
Definition: createplan.c:1720
void check_stack_depth(void)
Definition: postgres.c:3531
NodeTag pathtype
Definition: pathnodes.h:1626

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, plan, and root.

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 1612 of file createplan.c.

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

References build_path_tlist(), copy_generic_path_info(), create_plan_recurse(), make_project_set(), ProjectSetPath::path, plan, root, 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 2018 of file createplan.c.

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

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

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, root, 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 4024 of file createplan.c.

4026 {
4027  Result *scan_plan;
4028  Index scan_relid = best_path->parent->relid;
4030 
4031  Assert(scan_relid > 0);
4032  rte = planner_rt_fetch(scan_relid, root);
4033  Assert(rte->rtekind == RTE_RESULT);
4034 
4035  /* Sort clauses into best execution order */
4036  scan_clauses = order_qual_clauses(root, scan_clauses);
4037 
4038  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
4039  scan_clauses = extract_actual_clauses(scan_clauses, false);
4040 
4041  /* Replace any outer-relation variables with nestloop params */
4042  if (best_path->param_info)
4043  {
4044  scan_clauses = (List *)
4045  replace_nestloop_params(root, (Node *) scan_clauses);
4046  }
4047 
4048  scan_plan = make_result(tlist, (Node *) scan_clauses, NULL);
4049 
4050  copy_generic_path_info(&scan_plan->plan, best_path);
4051 
4052  return scan_plan;
4053 }
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:182
@ RTE_RESULT
Definition: parsenodes.h:1036

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(), root, 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 2954 of file createplan.c.

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

References Assert, copy_generic_path_info(), extract_actual_clauses(), make_samplescan(), order_qual_clauses(), planner_rt_fetch, replace_nestloop_params(), root, 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 559 of file createplan.c.

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

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, root, 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 2916 of file createplan.c.

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

References Assert, copy_generic_path_info(), extract_actual_clauses(), make_seqscan(), order_qual_clauses(), replace_nestloop_params(), root, 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 2719 of file createplan.c.

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

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, root, 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 2180 of file createplan.c.

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

References copy_generic_path_info(), CP_SMALL_TLIST, create_plan_recurse(), IS_OTHER_REL, make_sort_from_pathkeys(), SortPath::path, Path::pathkeys, plan, root, 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 3701 of file createplan.c.

3703 {
3704  SubqueryScan *scan_plan;
3705  RelOptInfo *rel = best_path->path.parent;
3706  Index scan_relid = rel->relid;
3707  Plan *subplan;
3708 
3709  /* it should be a subquery base rel... */