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

Go to the source code of this file.

Macros

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

Functions

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

Macro Definition Documentation

◆ CP_EXACT_TLIST

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

Definition at line 68 of file createplan.c.

◆ CP_IGNORE_TLIST

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

Definition at line 71 of file createplan.c.

◆ CP_LABEL_TLIST

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

Definition at line 70 of file createplan.c.

◆ CP_SMALL_TLIST

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

Definition at line 69 of file createplan.c.

Function Documentation

◆ bitmap_subplan_mark_shared()

static void bitmap_subplan_mark_shared ( Plan plan)
static

Definition at line 5435 of file createplan.c.

5436 {
5437  if (IsA(plan, BitmapAnd))
5438  bitmap_subplan_mark_shared(linitial(((BitmapAnd *) plan)->bitmapplans));
5439  else if (IsA(plan, BitmapOr))
5440  {
5441  ((BitmapOr *) plan)->isshared = true;
5442  bitmap_subplan_mark_shared(linitial(((BitmapOr *) plan)->bitmapplans));
5443  }
5444  else if (IsA(plan, BitmapIndexScan))
5445  ((BitmapIndexScan *) plan)->isshared = true;
5446  else
5447  elog(ERROR, "unrecognized node type: %d", nodeTag(plan));
5448 }
static void bitmap_subplan_mark_shared(Plan *plan)
Definition: createplan.c:5435
#define ERROR
Definition: elog.h:35
#define IsA(nodeptr, _type_)
Definition: nodes.h:168
#define nodeTag(nodeptr)
Definition: nodes.h:122
#define linitial(l)
Definition: pg_list.h:176

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

Referenced by create_bitmap_scan_plan().

◆ build_path_tlist()

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

Definition at line 804 of file createplan.c.

805 {
806  List *tlist = NIL;
807  Index *sortgrouprefs = path->pathtarget->sortgrouprefs;
808  int resno = 1;
809  ListCell *v;
810 
811  foreach(v, path->pathtarget->exprs)
812  {
813  Node *node = (Node *) lfirst(v);
814  TargetEntry *tle;
815 
816  /*
817  * If it's a parameterized path, there might be lateral references in
818  * the tlist, which need to be replaced with Params. There's no need
819  * to remake the TargetEntry nodes, so apply this to each list item
820  * separately.
821  */
822  if (path->param_info)
823  node = replace_nestloop_params(root, node);
824 
825  tle = makeTargetEntry((Expr *) node,
826  resno,
827  NULL,
828  false);
829  if (sortgrouprefs)
830  tle->ressortgroupref = sortgrouprefs[resno - 1];
831 
832  tlist = lappend(tlist, tle);
833  resno++;
834  }
835  return tlist;
836 }
unsigned int Index
Definition: c.h:550
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4892
List * lappend(List *list, void *datum)
Definition: list.c:338
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:238
#define lfirst(lc)
Definition: pg_list.h:170
#define NIL
Definition: pg_list.h:66
Definition: pg_list.h:52
Definition: nodes.h:118
Index ressortgroupref
Definition: primnodes.h:1558

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

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

◆ change_plan_targetlist()

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

Definition at line 2141 of file createplan.c.

2142 {
2143  /*
2144  * If the top plan node can't do projections and its existing target list
2145  * isn't already what we need, we need to add a Result node to help it
2146  * along.
2147  */
2148  if (!is_projection_capable_plan(subplan) &&
2149  !tlist_same_exprs(tlist, subplan->targetlist))
2150  subplan = inject_projection_plan(subplan, tlist,
2151  subplan->parallel_safe &&
2152  tlist_parallel_safe);
2153  else
2154  {
2155  /* Else we can just replace the plan node's tlist */
2156  subplan->targetlist = tlist;
2157  subplan->parallel_safe &= tlist_parallel_safe;
2158  }
2159  return subplan;
2160 }
bool is_projection_capable_plan(Plan *plan)
Definition: createplan.c:7209
static Plan * inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
Definition: createplan.c:2109
bool parallel_safe
Definition: plannodes.h:142
List * targetlist
Definition: plannodes.h:153
bool tlist_same_exprs(List *tlist1, List *tlist2)
Definition: tlist.c:218

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

Referenced by create_unique_plan(), and postgresGetForeignPlan().

◆ copy_generic_path_info()

static void copy_generic_path_info ( Plan dest,
Path src 
)
static

Definition at line 5366 of file createplan.c.

5367 {
5368  dest->startup_cost = src->startup_cost;
5369  dest->total_cost = src->total_cost;
5370  dest->plan_rows = src->rows;
5371  dest->plan_width = src->pathtarget->width;
5372  dest->parallel_aware = src->parallel_aware;
5373  dest->parallel_safe = src->parallel_safe;
5374 }
Cardinality rows
Definition: pathnodes.h:1544
Cost startup_cost
Definition: pathnodes.h:1545
Cost total_cost
Definition: pathnodes.h:1546
bool parallel_aware
Definition: pathnodes.h:1537
bool parallel_safe
Definition: pathnodes.h:1539

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

5382 {
5383  dest->startup_cost = src->startup_cost;
5384  dest->total_cost = src->total_cost;
5385  dest->plan_rows = src->plan_rows;
5386  dest->plan_width = src->plan_width;
5387  /* Assume the inserted node is not parallel-aware. */
5388  dest->parallel_aware = false;
5389  /* Assume the inserted node is parallel-safe, if child plan is. */
5390  dest->parallel_safe = src->parallel_safe;
5391 }
Cost total_cost
Definition: plannodes.h:130
Cost startup_cost
Definition: plannodes.h:129
int plan_width
Definition: plannodes.h:136
Cardinality plan_rows
Definition: plannodes.h:135

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

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

◆ create_agg_plan()

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

Definition at line 2297 of file createplan.c.

2298 {
2299  Agg *plan;
2300  Plan *subplan;
2301  List *tlist;
2302  List *quals;
2303 
2304  /*
2305  * Agg can project, so no need to be terribly picky about child tlist, but
2306  * we do need grouping columns to be available
2307  */
2308  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2309 
2310  tlist = build_path_tlist(root, &best_path->path);
2311 
2312  quals = order_qual_clauses(root, best_path->qual);
2313 
2314  plan = make_agg(tlist, quals,
2315  best_path->aggstrategy,
2316  best_path->aggsplit,
2317  list_length(best_path->groupClause),
2319  subplan->targetlist),
2320  extract_grouping_ops(best_path->groupClause),
2322  subplan->targetlist),
2323  NIL,
2324  NIL,
2325  best_path->numGroups,
2326  best_path->transitionSpace,
2327  subplan);
2328 
2329  copy_generic_path_info(&plan->plan, (Path *) best_path);
2330 
2331  return plan;
2332 }
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5272
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:6545
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5366
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:386
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:804
#define CP_LABEL_TLIST
Definition: createplan.c:70
static int list_length(const List *l)
Definition: pg_list.h:150
Path * subpath
Definition: pathnodes.h:2128
Cardinality numGroups
Definition: pathnodes.h:2131
AggSplit aggsplit
Definition: pathnodes.h:2130
List * groupClause
Definition: pathnodes.h:2133
uint64 transitionSpace
Definition: pathnodes.h:2132
AggStrategy aggstrategy
Definition: pathnodes.h:2129
Path path
Definition: pathnodes.h:2127
List * qual
Definition: pathnodes.h:2134
Definition: plannodes.h:993
Plan plan
Definition: plannodes.h:994
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, Agg::plan, AggPath::qual, AggPath::subpath, Plan::targetlist, and AggPath::transitionSpace.

Referenced by create_plan_recurse().

◆ create_append_plan()

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

Definition at line 1195 of file createplan.c.

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

References Append::appendplans, Append::apprelids, 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, Append::first_partial_plan, inject_projection_plan(), label_sort_with_costsize(), lappend(), Plan::lefttree, 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(), Append::nasyncplans, NIL, Path::parallel_safe, Plan::parallel_safe, Append::part_prune_index, AppendPath::path, Path::pathkeys, pathkeys_contained_in(), Append::plan, prepare_sort_from_pathkeys(), Plan::qual, RelOptInfo::relids, replace_nestloop_params(), Plan::righttree, sort(), subpath(), AppendPath::subpaths, and Plan::targetlist.

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

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

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

Referenced by create_scan_plan().

◆ create_bitmap_subplan()

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

Definition at line 3321 of file createplan.c.

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

References Assert(), BitmapAndPath::bitmapquals, BitmapOrPath::bitmapquals, BitmapAndPath::bitmapselectivity, BitmapOrPath::bitmapselectivity, castNode, clamp_row_est(), RestrictInfo::clause, create_indexscan_plan(), elog(), ERROR, get_actual_clauses(), IndexPath::indexclauses, IndexScan::indexid, IndexPath::indexinfo, IndexScan::indexqual, IndexScan::indexqualorig, IndexClause::indexquals, IndexPath::indexselectivity, IndexPath::indextotalcost, IndexOptInfo::indpred, IsA, lappend(), lfirst, linitial, list_concat(), list_concat_unique(), list_length(), list_make1, make_ands_explicit(), make_bitmap_and(), make_bitmap_indexscan(), make_bitmap_or(), make_orclause(), NIL, nodeTag, Plan::parallel_aware, Path::parallel_safe, Plan::parallel_safe, IndexPath::path, BitmapAndPath::path, BitmapOrPath::path, Plan::plan_rows, Plan::plan_width, predicate_implied_by(), IndexClause::rinfo, IndexScan::scan, Scan::scanrelid, Path::startup_cost, Plan::startup_cost, Path::total_cost, and Plan::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 3871 of file createplan.c.

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

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

Referenced by create_scan_plan().

◆ create_customscan_plan()

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

Definition at line 4249 of file createplan.c.

4251 {
4252  CustomScan *cplan;
4253  RelOptInfo *rel = best_path->path.parent;
4254  List *custom_plans = NIL;
4255  ListCell *lc;
4256 
4257  /* Recursively transform child paths. */
4258  foreach(lc, best_path->custom_paths)
4259  {
4260  Plan *plan = create_plan_recurse(root, (Path *) lfirst(lc),
4261  CP_EXACT_TLIST);
4262 
4263  custom_plans = lappend(custom_plans, plan);
4264  }
4265 
4266  /*
4267  * Sort clauses into the best execution order, although custom-scan
4268  * provider can reorder them again.
4269  */
4270  scan_clauses = order_qual_clauses(root, scan_clauses);
4271 
4272  /*
4273  * Invoke custom plan provider to create the Plan node represented by the
4274  * CustomPath.
4275  */
4276  cplan = castNode(CustomScan,
4277  best_path->methods->PlanCustomPath(root,
4278  rel,
4279  best_path,
4280  tlist,
4281  scan_clauses,
4282  custom_plans));
4283 
4284  /*
4285  * Copy cost data from Path to Plan; no need to make custom-plan providers
4286  * do this
4287  */
4288  copy_generic_path_info(&cplan->scan.plan, &best_path->path);
4289 
4290  /* Likewise, copy the relids that are represented by this custom scan */
4291  cplan->custom_relids = best_path->path.parent->relids;
4292 
4293  /*
4294  * Replace any outer-relation variables with nestloop params in the qual
4295  * and custom_exprs expressions. We do this last so that the custom-plan
4296  * provider doesn't have to be involved. (Note that parts of custom_exprs
4297  * could have come from join clauses, so doing this beforehand on the
4298  * scan_clauses wouldn't work.) We assume custom_scan_tlist contains no
4299  * such variables.
4300  */
4301  if (best_path->path.param_info)
4302  {
4303  cplan->scan.plan.qual = (List *)
4304  replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
4305  cplan->custom_exprs = (List *)
4306  replace_nestloop_params(root, (Node *) cplan->custom_exprs);
4307  }
4308 
4309  return cplan;
4310 }
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:1785
List * custom_paths
Definition: pathnodes.h:1783
Scan scan
Definition: plannodes.h:737
Bitmapset * custom_relids
Definition: plannodes.h:744
List * custom_exprs
Definition: plannodes.h:741

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, CustomPathMethods::PlanCustomPath, replace_nestloop_params(), and CustomScan::scan.

Referenced by create_scan_plan().

◆ create_foreignscan_plan()

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

Definition at line 4102 of file createplan.c.

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

References PlannerInfo::all_baserels, Assert(), RelOptInfo::baserestrictinfo, bms_free(), bms_is_member(), ForeignScan::checkAsUser, RestrictInfo::clause, copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), PlannerGlobal::dependsOnRole, PathTarget::exprs, ForeignScan::fdw_exprs, ForeignPath::fdw_outerpath, ForeignScan::fdw_recheck_quals, FirstLowInvalidHeapAttributeNumber, ForeignScan::fs_relids, ForeignScan::fs_server, ForeignScan::fsSystemCol, PlannerInfo::glob, 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(), 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 3741 of file createplan.c.

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

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

Referenced by create_scan_plan().

◆ create_gather_merge_plan()

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

Definition at line 1946 of file createplan.c.

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

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

Referenced by create_plan_recurse().

◆ create_gather_plan()

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

Definition at line 1908 of file createplan.c.

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

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

Referenced by create_plan_recurse().

◆ create_gating_plan()

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

Definition at line 1001 of file createplan.c.

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

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

Referenced by create_join_plan(), and create_scan_plan().

◆ create_group_plan()

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

Definition at line 2230 of file createplan.c.

2231 {
2232  Group *plan;
2233  Plan *subplan;
2234  List *tlist;
2235  List *quals;
2236 
2237  /*
2238  * Group can project, so no need to be terribly picky about child tlist,
2239  * but we do need grouping columns to be available
2240  */
2241  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2242 
2243  tlist = build_path_tlist(root, &best_path->path);
2244 
2245  quals = order_qual_clauses(root, best_path->qual);
2246 
2247  plan = make_group(tlist,
2248  quals,
2249  list_length(best_path->groupClause),
2251  subplan->targetlist),
2252  extract_grouping_ops(best_path->groupClause),
2254  subplan->targetlist),
2255  subplan);
2256 
2257  copy_generic_path_info(&plan->plan, (Path *) best_path);
2258 
2259  return plan;
2260 }
static Group * make_group(List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations, Plan *lefttree)
Definition: createplan.c:6621
List * qual
Definition: pathnodes.h:2102
List * groupClause
Definition: pathnodes.h:2101
Path * subpath
Definition: pathnodes.h:2100
Path path
Definition: pathnodes.h:2099
Plan plan
Definition: plannodes.h:965

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, Group::plan, GroupPath::qual, GroupPath::subpath, and Plan::targetlist.

Referenced by create_plan_recurse().

◆ create_group_result_plan()

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

Definition at line 1576 of file createplan.c.

1577 {
1578  Result *plan;
1579  List *tlist;
1580  List *quals;
1581 
1582  tlist = build_path_tlist(root, &best_path->path);
1583 
1584  /* best_path->quals is just bare clauses */
1585  quals = order_qual_clauses(root, best_path->quals);
1586 
1587  plan = make_result(tlist, (Node *) quals, NULL);
1588 
1589  copy_generic_path_info(&plan->plan, (Path *) best_path);
1590 
1591  return plan;
1592 }

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

Referenced by create_plan_recurse().

◆ create_groupingsets_plan()

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

Definition at line 2381 of file createplan.c.

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

Referenced by create_plan_recurse().

◆ create_hashjoin_plan()

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

Definition at line 4703 of file createplan.c.

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

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, OpExpr::inputcollid, InvalidAttrNumber, InvalidOid, is_opclause(), IS_OUTER_JOIN, IsA, HashJoin::join, JoinPath::joinrestrictinfo, JoinPath::jointype, HashPath::jpath, lappend(), lappend_oid(), lfirst_node, linitial, list_difference(), list_length(), lsecond, make_hash(), make_hashjoin(), NIL, HashPath::num_batches, OpExpr::opno, order_qual_clauses(), JoinPath::outerjoinpath, Plan::parallel_aware, HashPath::path_hashclauses, Hash::plan, RangeTblEntry::relid, replace_nestloop_params(), Hash::rows_total, RTE_RELATION, RangeTblEntry::rtekind, Plan::startup_cost, Plan::total_cost, Var::varattno, and Var::varno.

Referenced by create_join_plan().

◆ create_incrementalsort_plan()

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

Definition at line 2203 of file createplan.c.

2205 {
2206  IncrementalSort *plan;
2207  Plan *subplan;
2208 
2209  /* See comments in create_sort_plan() above */
2210  subplan = create_plan_recurse(root, best_path->spath.subpath,
2211  flags | CP_SMALL_TLIST);
2212  plan = make_incrementalsort_from_pathkeys(subplan,
2213  best_path->spath.path.pathkeys,
2214  IS_OTHER_REL(best_path->spath.subpath->parent) ?
2215  best_path->spath.path.parent->relids : NULL,
2216  best_path->nPresortedCols);
2217 
2218  copy_generic_path_info(&plan->sort.plan, (Path *) best_path);
2219 
2220  return plan;
2221 }
static IncrementalSort * make_incrementalsort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids, int nPresortedCols)
Definition: createplan.c:6340
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:804
Path path
Definition: pathnodes.h:2073
Path * subpath
Definition: pathnodes.h:2074
Plan plan
Definition: plannodes.h:929

References copy_generic_path_info(), CP_SMALL_TLIST, create_plan_recurse(), IS_OTHER_REL, make_incrementalsort_from_pathkeys(), IncrementalSortPath::nPresortedCols, SortPath::path, Path::pathkeys, Sort::plan, IncrementalSort::sort, 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 2998 of file createplan.c.

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

References Assert(), RestrictInfo::clause, contain_mutable_functions(), copy_generic_path_info(), elog(), ERROR, exprType(), extract_actual_clauses(), fix_indexorderby_references(), fix_indexqual_references(), forboth, 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(), TargetEntry::resjunk, 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 1060 of file createplan.c.

1061 {
1062  Plan *plan;
1063  List *gating_clauses;
1064 
1065  switch (best_path->path.pathtype)
1066  {
1067  case T_MergeJoin:
1068  plan = (Plan *) create_mergejoin_plan(root,
1069  (MergePath *) best_path);
1070  break;
1071  case T_HashJoin:
1072  plan = (Plan *) create_hashjoin_plan(root,
1073  (HashPath *) best_path);
1074  break;
1075  case T_NestLoop:
1076  plan = (Plan *) create_nestloop_plan(root,
1077  (NestPath *) best_path);
1078  break;
1079  default:
1080  elog(ERROR, "unrecognized node type: %d",
1081  (int) best_path->path.pathtype);
1082  plan = NULL; /* keep compiler quiet */
1083  break;
1084  }
1085 
1086  /*
1087  * If there are any pseudoconstant clauses attached to this node, insert a
1088  * gating Result node that evaluates the pseudoconstants as one-time
1089  * quals.
1090  */
1091  gating_clauses = get_gating_quals(root, best_path->joinrestrictinfo);
1092  if (gating_clauses)
1093  plan = create_gating_plan(root, (Path *) best_path, plan,
1094  gating_clauses);
1095 
1096 #ifdef NOT_USED
1097 
1098  /*
1099  * * Expensive function pullups may have pulled local predicates * into
1100  * this path node. Put them in the qpqual of the plan node. * JMH,
1101  * 6/15/92
1102  */
1103  if (get_loc_restrictinfo(best_path) != NIL)
1104  set_qpqual((Plan) plan,
1105  list_concat(get_qpqual((Plan) plan),
1106  get_actual_clauses(get_loc_restrictinfo(best_path))));
1107 #endif
1108 
1109  return plan;
1110 }
static HashJoin * create_hashjoin_plan(PlannerInfo *root, HashPath *best_path)
Definition: createplan.c:4703
static MergeJoin * create_mergejoin_plan(PlannerInfo *root, MergePath *best_path)
Definition: createplan.c:4396
static Plan * create_gating_plan(PlannerInfo *root, Path *path, Plan *plan, List *gating_quals)
Definition: createplan.c:1001
static List * get_gating_quals(PlannerInfo *root, List *quals)
Definition: createplan.c:981
static NestLoop * create_nestloop_plan(PlannerInfo *root, NestPath *best_path)
Definition: createplan.c:4320

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(), and NIL.

Referenced by create_plan_recurse().

◆ create_limit_plan()

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

Definition at line 2848 of file createplan.c.

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

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

Referenced by create_plan_recurse().

◆ create_lockrows_plan()

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

Definition at line 2785 of file createplan.c.

2787 {
2788  LockRows *plan;
2789  Plan *subplan;
2790 
2791  /* LockRows doesn't project, so tlist requirements pass through */
2792  subplan = create_plan_recurse(root, best_path->subpath, flags);
2793 
2794  plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
2795 
2796  copy_generic_path_info(&plan->plan, (Path *) best_path);
2797 
2798  return plan;
2799 }
static LockRows * make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
Definition: createplan.c:6891
Path * subpath
Definition: pathnodes.h:2234
List * rowMarks
Definition: pathnodes.h:2235
Plan plan
Definition: plannodes.h:1254

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

Referenced by create_plan_recurse().

◆ create_material_plan()

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

Definition at line 1627 of file createplan.c.

1628 {
1629  Material *plan;
1630  Plan *subplan;
1631 
1632  /*
1633  * We don't want any excess columns in the materialized tuples, so request
1634  * a smaller tlist. Otherwise, since Material doesn't project, tlist
1635  * requirements pass through.
1636  */
1637  subplan = create_plan_recurse(root, best_path->subpath,
1638  flags | CP_SMALL_TLIST);
1639 
1640  plan = make_material(subplan);
1641 
1642  copy_generic_path_info(&plan->plan, (Path *) best_path);
1643 
1644  return plan;
1645 }
static Material * make_material(Plan *lefttree)
Definition: createplan.c:6464
Path * subpath
Definition: pathnodes.h:1856
Plan plan
Definition: plannodes.h:878

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

Referenced by create_plan_recurse().

◆ create_memoize_plan()

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

Definition at line 1655 of file createplan.c.

1656 {
1657  Memoize *plan;
1658  Bitmapset *keyparamids;
1659  Plan *subplan;
1660  Oid *operators;
1661  Oid *collations;
1662  List *param_exprs = NIL;
1663  ListCell *lc;
1664  ListCell *lc2;
1665  int nkeys;
1666  int i;
1667 
1668  subplan = create_plan_recurse(root, best_path->subpath,
1669  flags | CP_SMALL_TLIST);
1670 
1671  param_exprs = (List *) replace_nestloop_params(root, (Node *)
1672  best_path->param_exprs);
1673 
1674  nkeys = list_length(param_exprs);
1675  Assert(nkeys > 0);
1676  operators = palloc(nkeys * sizeof(Oid));
1677  collations = palloc(nkeys * sizeof(Oid));
1678 
1679  i = 0;
1680  forboth(lc, param_exprs, lc2, best_path->hash_operators)
1681  {
1682  Expr *param_expr = (Expr *) lfirst(lc);
1683  Oid opno = lfirst_oid(lc2);
1684 
1685  operators[i] = opno;
1686  collations[i] = exprCollation((Node *) param_expr);
1687  i++;
1688  }
1689 
1690  keyparamids = pull_paramids((Expr *) param_exprs);
1691 
1692  plan = make_memoize(subplan, operators, collations, param_exprs,
1693  best_path->singlerow, best_path->binary_mode,
1694  best_path->est_entries, keyparamids);
1695 
1696  copy_generic_path_info(&plan->plan, (Path *) best_path);
1697 
1698  return plan;
1699 }
Bitmapset * pull_paramids(Expr *expr)
Definition: clauses.c:5245
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:6520
#define lfirst_oid(lc)
Definition: pg_list.h:172
bool singlerow
Definition: pathnodes.h:1870
List * hash_operators
Definition: pathnodes.h:1868
uint32 est_entries
Definition: pathnodes.h:1875
bool binary_mode
Definition: pathnodes.h:1872
Path * subpath
Definition: pathnodes.h:1867
List * param_exprs
Definition: pathnodes.h:1869
Plan plan
Definition: plannodes.h:887

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, Memoize::plan, pull_paramids(), replace_nestloop_params(), MemoizePath::singlerow, and MemoizePath::subpath.

Referenced by create_plan_recurse().

◆ create_merge_append_plan()

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

Definition at line 1416 of file createplan.c.

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

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

Referenced by create_plan_recurse().

◆ create_mergejoin_plan()

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

Definition at line 4396 of file createplan.c.

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

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

Referenced by create_join_plan().

◆ create_minmaxagg_plan()

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

Definition at line 2539 of file createplan.c.

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

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

Referenced by create_plan_recurse().

◆ create_modifytable_plan()

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

Definition at line 2808 of file createplan.c.

2809 {
2810  ModifyTable *plan;
2811  Path *subpath = best_path->subpath;
2812  Plan *subplan;
2813 
2814  /* Subplan must produce exactly the specified tlist */
2815  subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
2816 
2817  /* Transfer resname/resjunk labeling, too, to keep executor happy */
2819 
2820  plan = make_modifytable(root,
2821  subplan,
2822  best_path->operation,
2823  best_path->canSetTag,
2824  best_path->nominalRelation,
2825  best_path->rootRelation,
2826  best_path->partColsUpdated,
2827  best_path->resultRelations,
2828  best_path->updateColnosLists,
2829  best_path->withCheckOptionLists,
2830  best_path->returningLists,
2831  best_path->rowMarks,
2832  best_path->onconflict,
2833  best_path->mergeActionLists,
2834  best_path->epqParam);
2835 
2836  copy_generic_path_info(&plan->plan, &best_path->path);
2837 
2838  return plan;
2839 }
static ModifyTable * make_modifytable(PlannerInfo *root, Plan *subplan, CmdType operation, bool canSetTag, Index nominalRelation, Index rootRelation, bool partColsUpdated, List *resultRelations, List *updateColnosLists, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, List *mergeActionLists, int epqParam)
Definition: createplan.c:6980
bool partColsUpdated
Definition: pathnodes.h:2254
List * returningLists
Definition: pathnodes.h:2258
List * resultRelations
Definition: pathnodes.h:2255
List * withCheckOptionLists
Definition: pathnodes.h:2257
List * updateColnosLists
Definition: pathnodes.h:2256
OnConflictExpr * onconflict
Definition: pathnodes.h:2260
CmdType operation
Definition: pathnodes.h:2250
Index rootRelation
Definition: pathnodes.h:2253
Index nominalRelation
Definition: pathnodes.h:2252
List * mergeActionLists
Definition: pathnodes.h:2262
List * processed_tlist
Definition: pathnodes.h:415
void apply_tlist_labeling(List *dest_tlist, List *src_tlist)
Definition: tlist.c:318

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

Referenced by create_plan_recurse().

◆ create_namedtuplestorescan_plan()

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

Definition at line 3966 of file createplan.c.

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

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

Referenced by create_scan_plan().

◆ create_nestloop_plan()

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

Definition at line 4320 of file createplan.c.

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

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

Referenced by create_join_plan().

◆ create_plan()

Plan* create_plan ( PlannerInfo root,
Path best_path 
)

Definition at line 335 of file createplan.c.

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

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

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

◆ create_plan_recurse()

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

Definition at line 386 of file createplan.c.

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

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, and Path::pathtype.

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

1602 {
1603  ProjectSet *plan;
1604  Plan *subplan;
1605  List *tlist;
1606 
1607  /* Since we intend to project, we don't need to constrain child tlist */
1608  subplan = create_plan_recurse(root, best_path->subpath, 0);
1609 
1610  tlist = build_path_tlist(root, &best_path->path);
1611 
1612  plan = make_project_set(tlist, subplan);
1613 
1614  copy_generic_path_info(&plan->plan, (Path *) best_path);
1615 
1616  return plan;
1617 }
static ProjectSet * make_project_set(List *tlist, Plan *subplan)
Definition: createplan.c:6961
Path * subpath
Definition: pathnodes.h:2060
Plan plan
Definition: plannodes.h:211

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

Referenced by create_plan_recurse().

◆ create_projection_plan()

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

Definition at line 2007 of file createplan.c.

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

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

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, RecursiveUnion::plan, RecursiveUnionPath::rightpath, and RecursiveUnionPath::wtParam.

Referenced by create_plan_recurse().

◆ create_resultscan_plan()

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

Definition at line 4005 of file createplan.c.

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

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

Referenced by create_scan_plan().

◆ create_samplescan_plan()

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

Definition at line 2947 of file createplan.c.

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

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

Referenced by create_scan_plan().

◆ create_scan_plan()

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

Definition at line 557 of file createplan.c.

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

References apply_pathtarget_labeling_to_tlist(), 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(), list_concat_copy(), NIL, Path::pathtype, 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 2909 of file createplan.c.

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

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

Referenced by create_scan_plan().

◆ create_setop_plan()

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

Definition at line 2713 of file createplan.c.

2714 {
2715  SetOp *plan;
2716  Plan *subplan;
2717  long numGroups;
2718 
2719  /*
2720  * SetOp doesn't project, so tlist requirements pass through; moreover we
2721  * need grouping columns to be labeled.
2722  */
2723  subplan = create_plan_recurse(root, best_path->subpath,
2724  flags | CP_LABEL_TLIST);
2725 
2726  /* Convert numGroups to long int --- but 'ware overflow! */
2727  numGroups = clamp_cardinality_to_long(best_path->numGroups);
2728 
2729  plan = make_setop(best_path->cmd,
2730  best_path->strategy,
2731  subplan,
2732  best_path->distinctList,
2733  best_path->flagColIdx,
2734  best_path->firstFlag,
2735  numGroups);
2736 
2737  copy_generic_path_info(&plan->plan, (Path *) best_path);
2738 
2739  return plan;
2740 }
static SetOp * make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree, List *distinctList, AttrNumber flagColIdx, int firstFlag, long numGroups)
Definition: createplan.c:6835
List * distinctList
Definition: pathnodes.h:2209
Cardinality numGroups
Definition: pathnodes.h:2212
int firstFlag
Definition: pathnodes.h:2211
Path * subpath
Definition: pathnodes.h:2206
SetOpCmd cmd
Definition: pathnodes.h:2207
SetOpStrategy strategy
Definition: pathnodes.h:2208
AttrNumber flagColIdx
Definition: pathnodes.h:2210
Plan plan
Definition: plannodes.h:1215

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, SetOp::plan, SetOpPath::strategy, and SetOpPath::subpath.

Referenced by create_plan_recurse().

◆ create_sort_plan()

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

Definition at line 2169 of file createplan.c.

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

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

Referenced by create_plan_recurse().

◆ create_subqueryscan_plan()

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

Definition at line 3691 of file createplan.c.

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

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

Referenced by create_scan_plan().

◆ create_tablefuncscan_plan()

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

Definition at line 3784 of file createplan.c.

3786 {
3787  TableFuncScan *scan_plan;
3788  Index scan_relid = best_path->parent->relid;
3789  RangeTblEntry *rte;
3790  TableFunc *tablefunc;
3791 
3792  /* it should be a function base rel... */
3793  Assert(scan_relid > 0);
3794  rte = planner_rt_fetch(scan_relid, root);
3795  Assert(rte->rtekind == RTE_TABLEFUNC);
3796  tablefunc = rte->tablefunc;
3797 
3798  /* Sort clauses into best execution order */
3799  scan_clauses = order_qual_clauses(root, scan_clauses);
3800 
3801  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3802  scan_clauses = extract_actual_clauses(scan_clauses, false);
3803 
3804  /* Replace any outer-relation variables with nestloop params */
3805  if (best_path->param_info)
3806  {
3807  scan_clauses = (List *)
3808  replace_nestloop_params(root, (Node *) scan_clauses);
3809  /* The function expressions could contain nestloop params, too */
3810  tablefunc = (TableFunc *) replace_nestloop_params(root, (Node *) tablefunc);
3811  }
3812 
3813  scan_plan = make_tablefuncscan(tlist, scan_clauses, scan_relid,
3814  tablefunc);
3815 
3816  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3817 
3818  return scan_plan;
3819 }
static TableFuncScan * make_tablefuncscan(List *qptlist, List *qpqual, Index scanrelid, TableFunc *tablefunc)
Definition: createplan.c:5681
@ RTE_TABLEFUNC
Definition: parsenodes.h:1015
TableFunc * tablefunc
Definition: parsenodes.h:1130

References Assert(), copy_generic_path_info(), extract_actual_clauses(), make_tablefuncscan(), order_qual_clauses(), planner_rt_fetch, replace_nestloop_params(), RTE_TABLEFUNC, RangeTblEntry::rtekind, TableFuncScan::scan, and RangeTblEntry::tablefunc.

Referenced by create_scan_plan().

◆ create_tidrangescan_plan()

static TidRangeScan * create_tidrangescan_plan ( PlannerInfo root,
TidRangePath best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3626 of file createplan.c.

3628 {
3629  TidRangeScan *scan_plan;
3630  Index scan_relid = best_path->path.parent->relid;
3631  List *tidrangequals = best_path->tidrangequals;
3632 
3633  /* it should be a base rel... */
3634  Assert(scan_relid > 0);
3635  Assert(best_path->path.parent->rtekind == RTE_RELATION);
3636 
3637  /*
3638  * The qpqual list must contain all restrictions not enforced by the
3639  * tidrangequals list. tidrangequals has AND semantics, so we can simply
3640  * remove any qual that appears in it.
3641  */
3642  {
3643  List *qpqual = NIL;
3644  ListCell *l;
3645 
3646  foreach(l, scan_clauses)
3647  {
3648  RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
3649 
3650  if (rinfo->pseudoconstant)
3651  continue; /* we may drop pseudoconstants here */
3652  if (list_member_ptr(tidrangequals, rinfo))
3653  continue; /* simple duplicate */
3654  qpqual = lappend(qpqual, rinfo);
3655  }
3656  scan_clauses = qpqual;
3657  }
3658 
3659  /* Sort clauses into best execution order */
3660  scan_clauses = order_qual_clauses(root, scan_clauses);
3661 
3662  /* Reduce RestrictInfo lists to bare expressions; ignore pseudoconstants */
3663  tidrangequals = extract_actual_clauses(tidrangequals, false);
3664  scan_clauses = extract_actual_clauses(scan_clauses, false);
3665 
3666  /* Replace any outer-relation variables with nestloop params */
3667  if (best_path->path.param_info)
3668  {
3669  tidrangequals = (List *)
3670  replace_nestloop_params(root, (Node *) tidrangequals);
3671  scan_clauses = (List *)
3672