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

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

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:598
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4895
List * lappend(List *list, void *datum)
Definition: list.c:338
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:241
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
Definition: pg_list.h:54
Definition: nodes.h:129
Index ressortgroupref
Definition: primnodes.h:1822

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

2134 {
2135  /*
2136  * If the top plan node can't do projections and its existing target list
2137  * isn't already what we need, we need to add a Result node to help it
2138  * along.
2139  */
2140  if (!is_projection_capable_plan(subplan) &&
2141  !tlist_same_exprs(tlist, subplan->targetlist))
2142  subplan = inject_projection_plan(subplan, tlist,
2143  subplan->parallel_safe &&
2144  tlist_parallel_safe);
2145  else
2146  {
2147  /* Else we can just replace the plan node's tlist */
2148  subplan->targetlist = tlist;
2149  subplan->parallel_safe &= tlist_parallel_safe;
2150  }
2151  return subplan;
2152 }
bool is_projection_capable_plan(Plan *plan)
Definition: createplan.c:7208
static Plan * inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
Definition: createplan.c:2101
bool parallel_safe
Definition: plannodes.h:145
List * targetlist
Definition: plannodes.h:156
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 5369 of file createplan.c.

5370 {
5371  dest->startup_cost = src->startup_cost;
5372  dest->total_cost = src->total_cost;
5373  dest->plan_rows = src->rows;
5374  dest->plan_width = src->pathtarget->width;
5375  dest->parallel_aware = src->parallel_aware;
5376  dest->parallel_safe = src->parallel_safe;
5377 }
Cardinality rows
Definition: pathnodes.h:1634
Cost startup_cost
Definition: pathnodes.h:1635
Cost total_cost
Definition: pathnodes.h:1636
bool parallel_aware
Definition: pathnodes.h:1627
bool parallel_safe
Definition: pathnodes.h:1629

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

5385 {
5386  dest->startup_cost = src->startup_cost;
5387  dest->total_cost = src->total_cost;
5388  dest->plan_rows = src->plan_rows;
5389  dest->plan_width = src->plan_width;
5390  /* Assume the inserted node is not parallel-aware. */
5391  dest->parallel_aware = false;
5392  /* Assume the inserted node is parallel-safe, if child plan is. */
5393  dest->parallel_safe = src->parallel_safe;
5394 }
Cost total_cost
Definition: plannodes.h:133
Cost startup_cost
Definition: plannodes.h:132
int plan_width
Definition: plannodes.h:139
Cardinality plan_rows
Definition: plannodes.h:138

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

2290 {
2291  Agg *plan;
2292  Plan *subplan;
2293  List *tlist;
2294  List *quals;
2295 
2296  /*
2297  * Agg can project, so no need to be terribly picky about child tlist, but
2298  * we do need grouping columns to be available
2299  */
2300  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2301 
2302  tlist = build_path_tlist(root, &best_path->path);
2303 
2304  quals = order_qual_clauses(root, best_path->qual);
2305 
2306  plan = make_agg(tlist, quals,
2307  best_path->aggstrategy,
2308  best_path->aggsplit,
2309  list_length(best_path->groupClause),
2311  subplan->targetlist),
2312  extract_grouping_ops(best_path->groupClause),
2314  subplan->targetlist),
2315  NIL,
2316  NIL,
2317  best_path->numGroups,
2318  best_path->transitionSpace,
2319  subplan);
2320 
2321  copy_generic_path_info(&plan->plan, (Path *) best_path);
2322 
2323  return plan;
2324 }
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5275
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:6546
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5369
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:152
Path * subpath
Definition: pathnodes.h:2215
Cardinality numGroups
Definition: pathnodes.h:2218
AggSplit aggsplit
Definition: pathnodes.h:2217
List * groupClause
Definition: pathnodes.h:2220
uint64 transitionSpace
Definition: pathnodes.h:2219
AggStrategy aggstrategy
Definition: pathnodes.h:2216
Path path
Definition: pathnodes.h:2214
List * qual
Definition: pathnodes.h:2221
Definition: plannodes.h:998
Plan plan
Definition: plannodes.h:999
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:155
bool enable_partition_pruning
Definition: costsize.c:153
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6941
static void label_sort_with_costsize(PlannerInfo *root, Sort *plan, double limit_tuples)
Definition: createplan.c:5406
#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:6028
#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:6124
Assert(fmt[strlen(fmt) - 1] !='\n')
List * list_copy_head(const List *oldlist, int len)
Definition: list.c:1592
List * list_concat(List *list1, const List *list2)
Definition: list.c:560
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:241
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:360
#define makeNode(_type_)
Definition: nodes.h:176
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:340
#define list_make1(x1)
Definition: pg_list.h:212
unsigned int Oid
Definition: postgres_ext.h:31
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:464
int first_partial_path
Definition: pathnodes.h:1895
Cardinality limit_tuples
Definition: pathnodes.h:1896
List * subpaths
Definition: pathnodes.h:1893
List * pathkeys
Definition: pathnodes.h:1639
struct Plan * lefttree
Definition: plannodes.h:158
bool async_capable
Definition: plannodes.h:150
struct Plan * righttree
Definition: plannodes.h:159
List * qual
Definition: plannodes.h:157
List * baserestrictinfo
Definition: pathnodes.h:970
Relids relids
Definition: pathnodes.h:862

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

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

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

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

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

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

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

Referenced by create_scan_plan().

◆ create_customscan_plan()

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

Definition at line 4252 of file createplan.c.

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

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

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

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

Referenced by create_scan_plan().

◆ create_functionscan_plan()

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

Definition at line 3736 of file createplan.c.

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

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

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

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

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

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:5384
static SPIPlanPtr splan
Definition: regress.c:270
Node * resconstantqual
Definition: plannodes.h:203
Plan plan
Definition: plannodes.h:202

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

2223 {
2224  Group *plan;
2225  Plan *subplan;
2226  List *tlist;
2227  List *quals;
2228 
2229  /*
2230  * Group can project, so no need to be terribly picky about child tlist,
2231  * but we do need grouping columns to be available
2232  */
2233  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2234 
2235  tlist = build_path_tlist(root, &best_path->path);
2236 
2237  quals = order_qual_clauses(root, best_path->qual);
2238 
2239  plan = make_group(tlist,
2240  quals,
2241  list_length(best_path->groupClause),
2243  subplan->targetlist),
2244  extract_grouping_ops(best_path->groupClause),
2246  subplan->targetlist),
2247  subplan);
2248 
2249  copy_generic_path_info(&plan->plan, (Path *) best_path);
2250 
2251  return plan;
2252 }
static Group * make_group(List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations, Plan *lefttree)
Definition: createplan.c:6622
List * qual
Definition: pathnodes.h:2189
List * groupClause
Definition: pathnodes.h:2188
Path * subpath
Definition: pathnodes.h:2187
Path path
Definition: pathnodes.h:2186
Plan plan
Definition: plannodes.h:970

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

1569 {
1570  Result *plan;
1571  List *tlist;
1572  List *quals;
1573 
1574  tlist = build_path_tlist(root, &best_path->path);
1575 
1576  /* best_path->quals is just bare clauses */
1577  quals = order_qual_clauses(root, best_path->quals);
1578 
1579  plan = make_result(tlist, (Node *) quals, NULL);
1580 
1581  copy_generic_path_info(&plan->plan, (Path *) best_path);
1582 
1583  return plan;
1584 }

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

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

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

Referenced by create_plan_recurse().

◆ create_hashjoin_plan()

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

Definition at line 4706 of file createplan.c.

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

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

Referenced by create_join_plan().

◆ create_incrementalsort_plan()

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

Definition at line 2195 of file createplan.c.

2197 {
2198  IncrementalSort *plan;
2199  Plan *subplan;
2200 
2201  /* See comments in create_sort_plan() above */
2202  subplan = create_plan_recurse(root, best_path->spath.subpath,
2203  flags | CP_SMALL_TLIST);
2204  plan = make_incrementalsort_from_pathkeys(subplan,
2205  best_path->spath.path.pathkeys,
2206  IS_OTHER_REL(best_path->spath.subpath->parent) ?
2207  best_path->spath.path.parent->relids : NULL,
2208  best_path->nPresortedCols);
2209 
2210  copy_generic_path_info(&plan->sort.plan, (Path *) best_path);
2211 
2212  return plan;
2213 }
static IncrementalSort * make_incrementalsort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids, int nPresortedCols)
Definition: createplan.c:6341
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:845
Path path
Definition: pathnodes.h:2160
Path * subpath
Definition: pathnodes.h:2161
Plan plan
Definition: plannodes.h:934

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

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

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

Referenced by create_bitmap_subplan(), and create_scan_plan().

◆ create_join_plan()

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

Definition at line 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:4706
static MergeJoin * create_mergejoin_plan(PlannerInfo *root, MergePath *best_path)
Definition: createplan.c:4399
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:4323

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

2841 {
2842  Limit *plan;
2843  Plan *subplan;
2844  int numUniqkeys = 0;
2845  AttrNumber *uniqColIdx = NULL;
2846  Oid *uniqOperators = NULL;
2847  Oid *uniqCollations = NULL;
2848 
2849  /* Limit doesn't project, so tlist requirements pass through */
2850  subplan = create_plan_recurse(root, best_path->subpath, flags);
2851 
2852  /* Extract information necessary for comparing rows for WITH TIES. */
2853  if (best_path->limitOption == LIMIT_OPTION_WITH_TIES)
2854  {
2855  Query *parse = root->parse;
2856  ListCell *l;
2857 
2858  numUniqkeys = list_length(parse->sortClause);
2859  uniqColIdx = (AttrNumber *) palloc(numUniqkeys * sizeof(AttrNumber));
2860  uniqOperators = (Oid *) palloc(numUniqkeys * sizeof(Oid));
2861  uniqCollations = (Oid *) palloc(numUniqkeys * sizeof(Oid));
2862 
2863  numUniqkeys = 0;
2864  foreach(l, parse->sortClause)
2865  {
2866  SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
2867  TargetEntry *tle = get_sortgroupclause_tle(sortcl, parse->targetList);
2868 
2869  uniqColIdx[numUniqkeys] = tle->resno;
2870  uniqOperators[numUniqkeys] = sortcl->eqop;
2871  uniqCollations[numUniqkeys] = exprCollation((Node *) tle->expr);
2872  numUniqkeys++;
2873  }
2874  }
2875 
2876  plan = make_limit(subplan,
2877  best_path->limitOffset,
2878  best_path->limitCount,
2879  best_path->limitOption,
2880  numUniqkeys, uniqColIdx, uniqOperators, uniqCollations);
2881 
2882  copy_generic_path_info(&plan->plan, (Path *) best_path);
2883 
2884  return plan;
2885 }
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount, LimitOption limitOption, int uniqNumCols, AttrNumber *uniqColIdx, Oid *uniqOperators, Oid *uniqCollations)
Definition: createplan.c:6913
void * palloc(Size size)
Definition: mcxt.c:1210
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:780
@ LIMIT_OPTION_WITH_TIES
Definition: nodes.h:440
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:2359
LimitOption limitOption
Definition: pathnodes.h:2362
Node * limitOffset
Definition: pathnodes.h:2360
Node * limitCount
Definition: pathnodes.h:2361
Plan plan
Definition: plannodes.h:1273
Expr * expr
Definition: primnodes.h:1816

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

2779 {
2780  LockRows *plan;
2781  Plan *subplan;
2782 
2783  /* LockRows doesn't project, so tlist requirements pass through */
2784  subplan = create_plan_recurse(root, best_path->subpath, flags);
2785 
2786  plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
2787 
2788  copy_generic_path_info(&plan->plan, (Path *) best_path);
2789 
2790  return plan;
2791 }
static LockRows * make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
Definition: createplan.c:6892
Path * subpath
Definition: pathnodes.h:2321
List * rowMarks
Definition: pathnodes.h:2322
Plan plan
Definition: plannodes.h:1259

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

1620 {
1621  Material *plan;
1622  Plan *subplan;
1623 
1624  /*
1625  * We don't want any excess columns in the materialized tuples, so request
1626  * a smaller tlist. Otherwise, since Material doesn't project, tlist
1627  * requirements pass through.
1628  */
1629  subplan = create_plan_recurse(root, best_path->subpath,
1630  flags | CP_SMALL_TLIST);
1631 
1632  plan = make_material(subplan);
1633 
1634  copy_generic_path_info(&plan->plan, (Path *) best_path);
1635 
1636  return plan;
1637 }
static Material * make_material(Plan *lefttree)
Definition: createplan.c:6465
Path * subpath
Definition: pathnodes.h:1943
Plan plan
Definition: plannodes.h:883

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

1648 {
1649  Memoize *plan;
1650  Bitmapset *keyparamids;
1651  Plan *subplan;
1652  Oid *operators;
1653  Oid *collations;
1654  List *param_exprs = NIL;
1655  ListCell *lc;
1656  ListCell *lc2;
1657  int nkeys;
1658  int i;
1659 
1660  subplan = create_plan_recurse(root, best_path->subpath,
1661  flags | CP_SMALL_TLIST);
1662 
1663  param_exprs = (List *) replace_nestloop_params(root, (Node *)
1664  best_path->param_exprs);
1665 
1666  nkeys = list_length(param_exprs);
1667  Assert(nkeys > 0);
1668  operators = palloc(nkeys * sizeof(Oid));
1669  collations = palloc(nkeys * sizeof(Oid));
1670 
1671  i = 0;
1672  forboth(lc, param_exprs, lc2, best_path->hash_operators)
1673  {
1674  Expr *param_expr = (Expr *) lfirst(lc);
1675  Oid opno = lfirst_oid(lc2);
1676 
1677  operators[i] = opno;
1678  collations[i] = exprCollation((Node *) param_expr);
1679  i++;
1680  }
1681 
1682  keyparamids = pull_paramids((Expr *) param_exprs);
1683 
1684  plan = make_memoize(subplan, operators, collations, param_exprs,
1685  best_path->singlerow, best_path->binary_mode,
1686  best_path->est_entries, keyparamids);
1687 
1688  copy_generic_path_info(&plan->plan, (Path *) best_path);
1689 
1690  return plan;
1691 }
Bitmapset * pull_paramids(Expr *expr)
Definition: clauses.c:5287
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:6521
#define lfirst_oid(lc)
Definition: pg_list.h:174
bool singlerow
Definition: pathnodes.h:1957
List * hash_operators
Definition: pathnodes.h:1955
uint32 est_entries
Definition: pathnodes.h:1962
bool binary_mode
Definition: pathnodes.h:1959
Path * subpath
Definition: pathnodes.h:1954
List * param_exprs
Definition: pathnodes.h:1956
Plan plan
Definition: plannodes.h:892

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  /* We don't currently generate any parameterized MergeAppend paths */
1535  Assert(best_path->path.param_info == NULL);
1536 
1537  if (prunequal != NIL)
1538  node->part_prune_index = make_partition_pruneinfo(root, rel,
1539  best_path->subpaths,
1540  prunequal);
1541  }
1542 
1543  node->mergeplans = subplans;
1544 
1545 
1546  /*
1547  * If prepare_sort_from_pathkeys added sort columns, but we were told to
1548  * produce either the exact tlist or a narrow tlist, we should get rid of
1549  * the sort columns again. We must inject a projection node to do so.
1550  */
1551  if (tlist_was_changed && (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST)))
1552  {
1553  tlist = list_copy_head(plan->targetlist, orig_tlist_length);
1554  return inject_projection_plan(plan, tlist, plan->parallel_safe);
1555  }
1556  else
1557  return plan;
1558 }
Cardinality limit_tuples
Definition: pathnodes.h:1918
int part_prune_index
Definition: plannodes.h:315
Bitmapset * apprelids
Definition: plannodes.h:293
List * mergeplans
Definition: plannodes.h:295

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_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, 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 4399 of file createplan.c.

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

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

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

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

2801 {
2802  ModifyTable *plan;
2803  Path *subpath = best_path->subpath;
2804  Plan *subplan;
2805 
2806  /* Subplan must produce exactly the specified tlist */
2807  subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
2808 
2809  /* Transfer resname/resjunk labeling, too, to keep executor happy */
2811 
2812  plan = make_modifytable(root,
2813  subplan,
2814  best_path->operation,
2815  best_path->canSetTag,
2816  best_path->nominalRelation,
2817  best_path->rootRelation,
2818  best_path->partColsUpdated,
2819  best_path->resultRelations,
2820  best_path->updateColnosLists,
2821  best_path->withCheckOptionLists,
2822  best_path->returningLists,
2823  best_path->rowMarks,
2824  best_path->onconflict,
2825  best_path->mergeActionLists,
2826  best_path->epqParam);
2827 
2828  copy_generic_path_info(&plan->plan, &best_path->path);
2829 
2830  return plan;
2831 }
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:6981
bool partColsUpdated
Definition: pathnodes.h:2341
List * returningLists
Definition: pathnodes.h:2345
List * resultRelations
Definition: pathnodes.h:2342
List * withCheckOptionLists
Definition: pathnodes.h:2344
List * updateColnosLists
Definition: pathnodes.h:2343
OnConflictExpr * onconflict
Definition: pathnodes.h:2347
CmdType operation
Definition: pathnodes.h:2337
Index rootRelation
Definition: pathnodes.h:2340
Index nominalRelation
Definition: pathnodes.h:2339
List * mergeActionLists
Definition: pathnodes.h:2349
List * processed_tlist
Definition: pathnodes.h:456
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 3961 of file createplan.c.

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

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

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

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:220
List * curOuterParams
Definition: pathnodes.h:534
void SS_attach_initplans(PlannerInfo *root, Plan *plan)
Definition: subselect.c:2189

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:1938
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:1568
static Limit * create_limit_plan(PlannerInfo *root, LimitPath *best_path, int flags)
Definition: createplan.c:2840
static Agg * create_agg_plan(PlannerInfo *root, AggPath *best_path)
Definition: createplan.c:2289
static SetOp * create_setop_plan(PlannerInfo *root, SetOpPath *best_path, int flags)
Definition: createplan.c:2705
static Sort * create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags)
Definition: createplan.c:2161
static Unique * create_upper_unique_plan(PlannerInfo *root, UpperUniquePath *best_path, int flags)
Definition: createplan.c:2261
static Gather * create_gather_plan(PlannerInfo *root, GatherPath *best_path)
Definition: createplan.c:1900
static ProjectSet * create_project_set_plan(PlannerInfo *root, ProjectSetPath *best_path)
Definition: createplan.c:1593
static Group * create_group_plan(PlannerInfo *root, GroupPath *best_path)
Definition: createplan.c:2222
static ModifyTable * create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path)
Definition: createplan.c:2800
static Result * create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path)
Definition: createplan.c:2531
static LockRows * create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path, int flags)
Definition: createplan.c:2777
static Material * create_material_plan(PlannerInfo *root, MaterialPath *best_path, int flags)
Definition: createplan.c:1619
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:2195
static Plan * create_projection_plan(PlannerInfo *root, ProjectionPath *best_path, int flags)
Definition: createplan.c:1999
static Memoize * create_memoize_plan(PlannerInfo *root, MemoizePath *best_path, int flags)
Definition: createplan.c:1647
static WindowAgg * create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
Definition: createplan.c:2597
static Plan * create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path)
Definition: createplan.c:2373
static RecursiveUnion * create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path)
Definition: createplan.c:2741
static Plan * create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
Definition: createplan.c:1701
void check_stack_depth(void)
Definition: postgres.c:3461
NodeTag pathtype
Definition: pathnodes.h:1600

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

1594 {
1595  ProjectSet *plan;
1596  Plan *subplan;
1597  List *tlist;
1598 
1599  /* Since we intend to project, we don't need to constrain child tlist */
1600  subplan = create_plan_recurse(root, best_path->subpath, 0);
1601 
1602  tlist = build_path_tlist(root, &best_path->path);
1603 
1604  plan = make_project_set(tlist, subplan);
1605 
1606  copy_generic_path_info(&plan->plan, (Path *) best_path);
1607 
1608  return plan;
1609 }
static ProjectSet * make_project_set(List *tlist, Plan *subplan)
Definition: createplan.c:6962
Path * subpath
Definition: pathnodes.h:2147
Plan plan
Definition: plannodes.h:214

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

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

2742 {
2743  RecursiveUnion *plan;
2744  Plan *leftplan;
2745  Plan *rightplan;
2746  List *tlist;
2747  long numGroups;
2748 
2749  /* Need both children to produce same tlist, so force it */
2750  leftplan = create_plan_recurse(root, best_path->leftpath, CP_EXACT_TLIST);
2751  rightplan = create_plan_recurse(root, best_path->rightpath, CP_EXACT_TLIST);
2752 
2753  tlist = build_path_tlist(root, &best_path->path);
2754 
2755  /* Convert numGroups to long int --- but 'ware overflow! */
2756  numGroups = clamp_cardinality_to_long(best_path->numGroups);
2757 
2758  plan = make_recursive_union(tlist,
2759  leftplan,
2760  rightplan,
2761  best_path->wtParam,
2762  best_path->distinctList,
2763  numGroups);
2764 
2765  copy_generic_path_info(&plan->plan, (Path *) best_path);
2766 
2767  return plan;
2768 }
long clamp_cardinality_to_long(Cardinality x)
Definition: costsize.c:226
static RecursiveUnion * make_recursive_union(List *tlist, Plan *lefttree, Plan *righttree, int wtParam, List *distinctList, long numGroups)
Definition: createplan.c:5822
Cardinality numGroups
Definition: pathnodes.h:2312

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

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

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

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

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:2901
static ValuesScan * create_valuesscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3822
static TableFuncScan * create_tablefuncscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3779
static CustomScan * create_customscan_plan(PlannerInfo *root, CustomPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:4252
static ForeignScan * create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:4097
static BitmapHeapScan * create_bitmap_scan_plan(PlannerInfo *root, BitmapHeapPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3186
static TidScan * create_tidscan_plan(PlannerInfo *root, TidPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3524
static WorkTableScan * create_worktablescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:4037
static FunctionScan * create_functionscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3736
static Result * create_resultscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:4000
static CteScan * create_ctescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3866
static NamedTuplestoreScan * create_namedtuplestorescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3961
static SubqueryScan * create_subqueryscan_plan(PlannerInfo *root, SubqueryScanPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3686
static TidRangeScan * create_tidrangescan_plan(PlannerInfo *root, TidRangePath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3621
static SampleScan * create_samplescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:2939
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:597
#define copyObject(obj)
Definition: nodes.h:244
List * build_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
Definition: plancat.c:1716

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

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

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

2706 {
2707  SetOp *plan;
2708  Plan *subplan;
2709  long numGroups;
2710 
2711  /*
2712  * SetOp doesn't project, so tlist requirements pass through; moreover we
2713  * need grouping columns to be labeled.
2714  */
2715  subplan = create_plan_recurse(root, best_path->subpath,
2716  flags | CP_LABEL_TLIST);
2717 
2718  /* Convert numGroups to long int --- but 'ware overflow! */
2719  numGroups = clamp_cardinality_to_long(best_path->numGroups);
2720 
2721  plan = make_setop(best_path->cmd,
2722  best_path->strategy,
2723  subplan,
2724  best_path->distinctList,
2725  best_path->flagColIdx,
2726  best_path->firstFlag,
2727  numGroups);
2728 
2729  copy_generic_path_info(&plan->plan, (Path *) best_path);
2730 
2731  return plan;
2732 }
static SetOp * make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree, List *distinctList, AttrNumber flagColIdx, int firstFlag, long numGroups)
Definition: createplan.c:6836
List * distinctList
Definition: pathnodes.h:2296
Cardinality numGroups
Definition: pathnodes.h:2299
int firstFlag
Definition: pathnodes.h:2298
Path * subpath
Definition: pathnodes.h:2293
SetOpCmd cmd
Definition: pathnodes.h:2294
SetOpStrategy strategy
Definition: pathnodes.h:2295
AttrNumber flagColIdx
Definition: pathnodes.h:2297
Plan plan
Definition: plannodes.h:1220

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

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

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

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

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

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

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

3623 {
3624  TidRangeScan *scan_plan;
3625  Index scan_relid = best_path->path.parent->relid;
3626  List *tidrangequals = best_path->tidrangequals;
3627 
3628  /* it should be a base rel... */
3629  Assert(scan_relid > 0);
3630  Assert(best_path->path.parent->rtekind == RTE_RELATION);
3631 
3632  /*
3633  * The qpqual list must contain all restrictions not enforced by the
3634  * tidrangequals list. tidrangequals has AND semantics, so we can simply
3635  * remove any qual that appears in it.
3636  */
3637  {
3638  List *qpqual = NIL;
3639  ListCell *l;
3640 
3641  foreach(l, scan_clauses)
3642  {
3643  RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
3644 
3645  if (rinfo->pseudoconstant)
3646  continue; /* we may drop pseudoconstants here */
3647  if (list_member_ptr(tidrangequals, rinfo))
3648  continue; /* simple duplicate */
3649  qpqual = lappend(qpqual, rinfo);
3650  }
3651  scan_clauses = qpqual;
3652  }
3653 
3654  /* Sort clauses into best execution order */
3655  scan_clauses = order_qual_clauses(