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 *mergeActionList, 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 5436 of file createplan.c.

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

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:549
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4888
List * lappend(List *list, void *datum)
Definition: list.c:336
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:239
#define lfirst(lc)
Definition: pg_list.h:169
#define NIL
Definition: pg_list.h:65
Definition: pg_list.h:51
Definition: nodes.h:574
Index * sortgrouprefs
Definition: pathnodes.h:1123
List * exprs
Definition: pathnodes.h:1122
ParamPathInfo * param_info
Definition: pathnodes.h:1197
PathTarget * pathtarget
Definition: pathnodes.h:1195
Index ressortgroupref
Definition: primnodes.h:1719

References PathTarget::exprs, lappend(), lfirst, makeTargetEntry(), NIL, Path::param_info, Path::pathtarget, replace_nestloop_params(), TargetEntry::ressortgroupref, and PathTarget::sortgrouprefs.

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

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

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

5368 {
5369  dest->startup_cost = src->startup_cost;
5370  dest->total_cost = src->total_cost;
5371  dest->plan_rows = src->rows;
5372  dest->plan_width = src->pathtarget->width;
5373  dest->parallel_aware = src->parallel_aware;
5374  dest->parallel_safe = src->parallel_safe;
5375 }
Cardinality rows
Definition: pathnodes.h:1204
Cost startup_cost
Definition: pathnodes.h:1205
Cost total_cost
Definition: pathnodes.h:1206
bool parallel_aware
Definition: pathnodes.h:1199
bool parallel_safe
Definition: pathnodes.h:1200

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

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

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

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

2297 {
2298  Agg *plan;
2299  Plan *subplan;
2300  List *tlist;
2301  List *quals;
2302 
2303  /*
2304  * Agg can project, so no need to be terribly picky about child tlist, but
2305  * we do need grouping columns to be available
2306  */
2307  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2308 
2309  tlist = build_path_tlist(root, &best_path->path);
2310 
2311  quals = order_qual_clauses(root, best_path->qual);
2312 
2313  plan = make_agg(tlist, quals,
2314  best_path->aggstrategy,
2315  best_path->aggsplit,
2316  list_length(best_path->groupClause),
2318  subplan->targetlist),
2319  extract_grouping_ops(best_path->groupClause),
2321  subplan->targetlist),
2322  NIL,
2323  NIL,
2324  best_path->numGroups,
2325  best_path->transitionSpace,
2326  subplan);
2327 
2328  copy_generic_path_info(&plan->plan, (Path *) best_path);
2329 
2330  return plan;
2331 }
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5273
Agg * make_agg(List *tlist, List *qual, AggStrategy aggstrategy, AggSplit aggsplit, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations, List *groupingSets, List *chain, double dNumGroups, Size transitionSpace, Plan *lefttree)
Definition: createplan.c:6545
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5367
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:149
Path * subpath
Definition: pathnodes.h:1784
Cardinality numGroups
Definition: pathnodes.h:1787
AggSplit aggsplit
Definition: pathnodes.h:1786
List * groupClause
Definition: pathnodes.h:1789
uint64 transitionSpace
Definition: pathnodes.h:1788
AggStrategy aggstrategy
Definition: pathnodes.h:1785
Path path
Definition: pathnodes.h:1783
List * qual
Definition: pathnodes.h:1790
Definition: plannodes.h:895
Plan plan
Definition: plannodes.h:896
Oid * extract_grouping_collations(List *groupClause, List *tlist)
Definition: tlist.c:478
Oid * extract_grouping_ops(List *groupClause)
Definition: tlist.c:452
AttrNumber * extract_grouping_cols(List *groupClause, List *tlist)
Definition: tlist.c:503

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

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(), list_length(), list_make1, list_truncate(), make_partition_pruneinfo(), make_result(), make_sort(), makeBoolConst(), makeNode, mark_async_capable_plan(), Append::nasyncplans, NIL, Path::parallel_safe, Plan::parallel_safe, Path::param_info, Path::parent, Append::part_prune_info, AppendPath::path, Path::pathkeys, pathkeys_contained_in(), Append::plan, ParamPathInfo::ppi_clauses, 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 3190 of file createplan.c.

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

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, Path::param_info, Path::parent, RestrictInfo::parent_ec, BitmapHeapPath::path, Scan::plan, predicate_implied_by(), RestrictInfo::pseudoconstant, RelOptInfo::relid, replace_nestloop_params(), RTE_RELATION, RelOptInfo::rtekind, 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 3320 of file createplan.c.

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

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, Path::parent, RestrictInfo::parent_ec, IndexPath::path, BitmapAndPath::path, BitmapOrPath::path, Plan::plan_rows, Plan::plan_width, predicate_implied_by(), RestrictInfo::pseudoconstant, IndexClause::rinfo, IndexScan::scan, Scan::scanrelid, Path::startup_cost, Plan::startup_cost, Path::total_cost, Plan::total_cost, and RelOptInfo::tuples.

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

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

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(), Path::param_info, Path::parent, PlannerInfo::parent_root, PlannerInfo::parse, Scan::plan, SubPlan::plan_id, planner_rt_fetch, RelOptInfo::relid, 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 4245 of file createplan.c.

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

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(), Path::param_info, Path::parent, CustomPath::path, Scan::plan, CustomPathMethods::PlanCustomPath, Plan::qual, RelOptInfo::relids, 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 4101 of file createplan.c.

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

References PlannerInfo::all_baserels, Assert(), RelOptInfo::baserestrictinfo, bms_free(), bms_is_member(), 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, RelOptInfo::fdwroutine, FirstLowInvalidHeapAttributeNumber, ForeignScan::fs_relids, ForeignScan::fs_server, ForeignScan::fsSystemCol, FdwRoutine::GetForeignPlan, PlannerInfo::glob, i, InvalidOid, lfirst, order_qual_clauses(), Path::param_info, Path::parent, ForeignPath::path, Scan::plan, planner_rt_fetch, pull_varattnos(), Plan::qual, RangeTblEntry::relid, RelOptInfo::relid, RelOptInfo::relids, RELOPT_UPPER_REL, RelOptInfo::reloptkind, RelOptInfo::reltarget, replace_nestloop_params(), RTE_RELATION, RangeTblEntry::rtekind, RelOptInfo::rtekind, ForeignScan::scan, RelOptInfo::serverid, 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 3740 of file createplan.c.

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

References Assert(), copy_generic_path_info(), extract_actual_clauses(), RangeTblEntry::funcordinality, functions, RangeTblEntry::functions, make_functionscan(), order_qual_clauses(), Path::param_info, Path::parent, Scan::plan, planner_rt_fetch, RelOptInfo::relid, 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 1945 of file createplan.c.

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

References Assert(), assign_special_exec_param(), build_path_tlist(), GatherMerge::collations, copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), elog, ERROR, PlannerInfo::glob, Plan::lefttree, makeNode, NIL, GatherMerge::nullsFirst, GatherMergePath::num_workers, GatherMerge::num_workers, GatherMerge::numCols, PlannerGlobal::parallelModeNeeded, Path::parent, GatherMergePath::path, Path::pathkeys, pathkeys_contained_in(), GatherMerge::plan, prepare_sort_from_pathkeys(), RelOptInfo::relids, GatherMerge::rescan_param, GatherMerge::sortColIdx, GatherMerge::sortOperators, 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 1907 of file createplan.c.

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

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:5382
static SPIPlanPtr splan
Definition: regress.c:264
Node * resconstantqual
Definition: plannodes.h:189
Plan plan
Definition: plannodes.h:188

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

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

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

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

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

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

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

Referenced by create_plan_recurse().

◆ create_hashjoin_plan()

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

Definition at line 4699 of file createplan.c.

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

References OpExpr::args, Assert(), build_path_tlist(), copy_generic_path_info(), copy_plan_costsize(), CP_SMALL_TLIST, create_plan_recurse(), extract_actual_clauses(), extract_actual_join_clauses(), get_actual_clauses(), get_switched_clauses(), RangeTblEntry::inh, HashPath::inner_rows_total, JoinPath::inner_unique, JoinPath::innerjoinpath, OpExpr::inputcollid, InvalidAttrNumber, InvalidOid, is_opclause(), IS_OUTER_JOIN, IsA, HashJoin::join, JoinPath::joinrestrictinfo, JoinPath::jointype, HashPath::jpath, lappend(), lappend_oid(), lfirst_node, linitial, list_difference(), list_length(), lsecond, make_hash(), make_hashjoin(), NIL, HashPath::num_batches, OpExpr::opno, order_qual_clauses(), JoinPath::outerjoinpath, Path::parallel_aware, Plan::parallel_aware, Path::param_info, Path::parent, JoinPath::path, HashPath::path_hashclauses, Join::plan, Hash::plan, RangeTblEntry::relid, RelOptInfo::relids, replace_nestloop_params(), Hash::rows_total, RTE_RELATION, RangeTblEntry::rtekind, PlannerInfo::simple_rte_array, 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 2202 of file createplan.c.

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

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

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

References Assert(), IndexOptInfo::canreturn, RestrictInfo::clause, contain_mutable_functions(), copy_generic_path_info(), elog, ERROR, exprType(), extract_actual_clauses(), fix_indexorderby_references(), fix_indexqual_references(), forboth, get_opfamily_member(), i, IndexPath::indexclauses, IndexPath::indexinfo, IndexOptInfo::indexoid, IndexPath::indexorderbys, IndexPath::indexscandir, IndexOptInfo::indextlist, is_redundant_with_indexclauses(), lappend(), lappend_oid(), lfirst, lfirst_node, list_length(), list_make1, make_indexonlyscan(), make_indexscan(), NIL, OidIsValid, order_qual_clauses(), Path::param_info, Path::parent, IndexPath::path, Path::pathkeys, PathKey::pk_opfamily, PathKey::pk_strategy, Scan::plan, predicate_implied_by(), RestrictInfo::pseudoconstant, RelOptInfo::relid, replace_nestloop_params(), TargetEntry::resjunk, RTE_RELATION, and RelOptInfo::rtekind.

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:4699
static MergeJoin * create_mergejoin_plan(PlannerInfo *root, MergePath *best_path)
Definition: createplan.c:4392
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:4316
@ T_MergeJoin
Definition: nodes.h:75
@ T_NestLoop
Definition: nodes.h:74
@ T_HashJoin
Definition: nodes.h:76
NodeTag pathtype
Definition: pathnodes.h:1192

References create_gating_plan(), create_hashjoin_plan(), create_mergejoin_plan(), create_nestloop_plan(), elog, ERROR, get_actual_clauses(), get_gating_quals(), JoinPath::joinrestrictinfo, list_concat(), NIL, JoinPath::path, Path::pathtype, T_HashJoin, T_MergeJoin, and T_NestLoop.

Referenced by create_plan_recurse().

◆ create_limit_plan()

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

Definition at line 2847 of file createplan.c.

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

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

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

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

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

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

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

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

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

References MergeAppend::apprelids, Assert(), RelOptInfo::baserestrictinfo, build_path_tlist(), MergeAppend::collations, copy_generic_path_info(), CP_EXACT_TLIST, CP_SMALL_TLIST, create_plan_recurse(), elog, enable_partition_pruning, ERROR, extract_actual_clauses(), inject_projection_plan(), label_sort_with_costsize(), lappend(), Plan::lefttree, lfirst, MergeAppendPath::limit_tuples, list_concat(), list_copy(), list_length(), list_truncate(), make_partition_pruneinfo(), make_sort(), makeNode, MergeAppend::mergeplans, NIL, MergeAppend::nullsFirst, MergeAppend::numCols, Plan::parallel_safe, Path::param_info, Path::parent, MergeAppend::part_prune_info, MergeAppendPath::path, Path::pathkeys, pathkeys_contained_in(), MergeAppend::plan, ParamPathInfo::ppi_clauses, prepare_sort_from_pathkeys(), Plan::qual, RelOptInfo::relids, replace_nestloop_params(), Plan::righttree, sort(), MergeAppend::sortColIdx, MergeAppend::sortOperators, 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 4392 of file createplan.c.

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

References Assert(), build_path_tlist(), copy_generic_path_info(), copy_plan_costsize(), CP_SMALL_TLIST, cpu_operator_cost, create_plan_recurse(), EquivalenceClass::ec_collation, 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(), RestrictInfo::left_ec, 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(), RestrictInfo::outer_is_left, JoinPath::outerjoinpath, MergePath::outersortkeys, palloc(), Path::param_info, Path::parent, JoinPath::path, MergePath::path_mergeclauses, Path::pathkeys, PathKey::pk_eclass, PathKey::pk_nulls_first, PathKey::pk_opfamily, PathKey::pk_strategy, Join::plan, Plan::plan_rows, RelOptInfo::relids, replace_nestloop_params(), RestrictInfo::right_ec, 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 2538 of file createplan.c.

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

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, Path::pathtarget, Result::plan, Plan::plan_rows, Plan::plan_width, MinMaxAggPath::quals, SS_make_initplan_from_plan(), Path::startup_cost, Plan::startup_cost, MinMaxAggInfo::subroot, Plan::total_cost, and PathTarget::width.

Referenced by create_plan_recurse().

◆ create_modifytable_plan()

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

Definition at line 2807 of file createplan.c.

2808 {
2809  ModifyTable *plan;
2810  Path *subpath = best_path->subpath;
2811  Plan *subplan;
2812 
2813  /* Subplan must produce exactly the specified tlist */
2814  subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
2815 
2816  /* Transfer resname/resjunk labeling, too, to keep executor happy */
2818 
2819  plan = make_modifytable(root,
2820  subplan,
2821  best_path->operation,
2822  best_path->canSetTag,
2823  best_path->nominalRelation,
2824  best_path->rootRelation,
2825  best_path->partColsUpdated,
2826  best_path->resultRelations,
2827  best_path->updateColnosLists,
2828  best_path->withCheckOptionLists,
2829  best_path->returningLists,
2830  best_path->rowMarks,
2831  best_path->onconflict,
2832  best_path->mergeActionLists,
2833  best_path->epqParam);
2834 
2835  copy_generic_path_info(&plan->plan, &best_path->path);
2836 
2837  return plan;
2838 }
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 *mergeActionList, int epqParam)
Definition: createplan.c:6980
bool partColsUpdated
Definition: pathnodes.h:1906
List * returningLists
Definition: pathnodes.h:1910
List * resultRelations
Definition: pathnodes.h:1907
List * withCheckOptionLists
Definition: pathnodes.h:1909
List * updateColnosLists
Definition: pathnodes.h:1908
OnConflictExpr * onconflict
Definition: pathnodes.h:1912
CmdType operation
Definition: pathnodes.h:1902
Index rootRelation
Definition: pathnodes.h:1905
Index nominalRelation
Definition: pathnodes.h:1904
List * mergeActionLists
Definition: pathnodes.h:1914
List * processed_tlist
Definition: pathnodes.h:322
void apply_tlist_labeling(List *dest_tlist, List *src_tlist)
Definition: tlist.c:307

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

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

References Assert(), copy_generic_path_info(), RangeTblEntry::enrname, extract_actual_clauses(), make_namedtuplestorescan(), order_qual_clauses(), Path::param_info, Path::parent, Scan::plan, planner_rt_fetch, RelOptInfo::relid, 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 4316 of file createplan.c.

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

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, Path::param_info, Path::parent, JoinPath::path, Join::plan, RelOptInfo::relids, 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:176
List * curOuterParams
Definition: pathnodes.h:370
void SS_attach_initplans(PlannerInfo *root, Plan *plan)
Definition: subselect.c:2184

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

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

◆ create_plan_recurse()

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

Definition at line 386 of file createplan.c.

387 {
388  Plan *plan;
389 
390  /* Guard against stack overflow due to overly complex plans */
392 
393  switch (best_path->pathtype)
394  {
395  case T_SeqScan:
396  case T_SampleScan:
397  case T_IndexScan:
398  case T_IndexOnlyScan:
399  case T_BitmapHeapScan:
400  case T_TidScan:
401  case T_TidRangeScan:
402  case T_SubqueryScan:
403  case T_FunctionScan:
404  case T_TableFuncScan:
405  case T_ValuesScan:
406  case T_CteScan:
407  case T_WorkTableScan:
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:1417
static GatherMerge * create_gather_merge_plan(PlannerInfo *root, GatherMergePath *best_path)
Definition: createplan.c:1945
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:1575
static Limit * create_limit_plan(PlannerInfo *root, LimitPath *best_path, int flags)
Definition: createplan.c:2847
static Agg * create_agg_plan(PlannerInfo *root, AggPath *best_path)
Definition: createplan.c:2296
static SetOp * create_setop_plan(PlannerInfo *root, SetOpPath *best_path, int flags)
Definition: createplan.c:2712
static Sort * create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags)
Definition: createplan.c:2168
static Unique * create_upper_unique_plan(PlannerInfo *root, UpperUniquePath *best_path, int flags)
Definition: createplan.c:2268
static Gather * create_gather_plan(PlannerInfo *root, GatherPath *best_path)
Definition: createplan.c:1907
static ProjectSet * create_project_set_plan(PlannerInfo *root, ProjectSetPath *best_path)
Definition: createplan.c:1600
static Group * create_group_plan(PlannerInfo *root, GroupPath *best_path)
Definition: createplan.c:2229
static ModifyTable * create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path)
Definition: createplan.c:2807
static Result * create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path)
Definition: createplan.c:2538
static LockRows * create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path, int flags)
Definition: createplan.c:2784
static Material * create_material_plan(PlannerInfo *root, MaterialPath *best_path, int flags)
Definition: createplan.c:1626
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:2202
static Plan * create_projection_plan(PlannerInfo *root, ProjectionPath *best_path, int flags)
Definition: createplan.c:2006
static Memoize * create_memoize_plan(PlannerInfo *root, MemoizePath *best_path, int flags)
Definition: createplan.c:1654
static WindowAgg * create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
Definition: createplan.c:2604
static Plan * create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path)
Definition: createplan.c:2380
static RecursiveUnion * create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path)
Definition: createplan.c:2748
static Plan * create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
Definition: createplan.c:1708
@ T_Unique
Definition: nodes.h:84
@ T_NamedTuplestoreScan
Definition: nodes.h:69
@ T_TidRangeScan
Definition: nodes.h:63
@ T_TableFuncScan
Definition: nodes.h:67
@ T_FunctionScan
Definition: nodes.h:65
@ T_Material
Definition: nodes.h:77
@ T_IndexOnlyScan
Definition: nodes.h:59
@ T_Append
Definition: nodes.h:50
@ T_Gather
Definition: nodes.h:85
@ T_RecursiveUnion
Definition: nodes.h:52
@ T_LockRows
Definition: nodes.h:89
@ T_TidScan
Definition: nodes.h:62
@ T_Limit
Definition: nodes.h:90
@ T_Memoize
Definition: nodes.h:78
@ T_Sort
Definition: nodes.h:79
@ T_SeqScan
Definition: nodes.h:56
@ T_WindowAgg
Definition: nodes.h:83
@ T_ProjectSet
Definition: nodes.h:48
@ T_SampleScan
Definition: nodes.h:57
@ T_ValuesScan
Definition: nodes.h:66
@ T_MergeAppend
Definition: nodes.h:51
@ T_Agg
Definition: nodes.h:82
@ T_CteScan
Definition: nodes.h:68
@ T_IncrementalSort
Definition: nodes.h:80
@ T_CustomScan
Definition: nodes.h:72
@ T_Group
Definition: nodes.h:81
@ T_ModifyTable
Definition: nodes.h:49
@ T_GatherMerge
Definition: nodes.h:86
@ T_SubqueryScan
Definition: nodes.h:64
@ T_SetOp
Definition: nodes.h:88
@ T_Result
Definition: nodes.h:47
@ T_IndexScan
Definition: nodes.h:58
@ T_BitmapHeapScan
Definition: nodes.h:61
@ T_WorkTableScan
Definition: nodes.h:70
@ T_ForeignScan
Definition: nodes.h:71
void check_stack_depth(void)
Definition: postgres.c:3500

References Assert(), check_stack_depth(), create_agg_plan(), create_append_plan(), create_gather_merge_plan(), create_gather_plan(), create_group_plan(), create_group_result_plan(), create_groupingsets_plan(), create_incrementalsort_plan(), create_join_plan(), create_limit_plan(), create_lockrows_plan(), create_material_plan(), create_memoize_plan(), create_merge_append_plan(), create_minmaxagg_plan(), create_modifytable_plan(), create_project_set_plan(), create_projection_plan(), create_recursiveunion_plan(), create_scan_plan(), create_setop_plan(), create_sort_plan(), create_unique_plan(), create_upper_unique_plan(), create_windowagg_plan(), elog, ERROR, IsA, Path::pathtype, T_Agg, T_Append, T_BitmapHeapScan, T_CteScan, T_CustomScan, T_ForeignScan, T_FunctionScan, T_Gather, T_GatherMerge, T_Group, T_HashJoin, T_IncrementalSort, T_IndexOnlyScan, T_IndexScan, T_Limit, T_LockRows, T_Material, T_Memoize, T_MergeAppend, T_MergeJoin, T_ModifyTable, T_NamedTuplestoreScan, T_NestLoop, T_ProjectSet, T_RecursiveUnion, T_Result, T_SampleScan, T_SeqScan, T_SetOp, T_Sort, T_SubqueryScan, T_TableFuncScan, T_TidRangeScan, T_TidScan, T_Unique, T_ValuesScan, T_WindowAgg, and T_WorkTableScan.

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

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

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

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

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, Path::pathtarget, 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, use_physical_tlist(), and PathTarget::width.

Referenced by create_plan_recurse().

◆ create_recursiveunion_plan()

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

Definition at line 2748 of file createplan.c.

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

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

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

References Assert(), copy_generic_path_info(), extract_actual_clauses(), make_result(), order_qual_clauses(), Path::param_info, Path::parent, PG_USED_FOR_ASSERTS_ONLY, Result::plan, planner_rt_fetch, RelOptInfo::relid, 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 2946 of file createplan.c.

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

References Assert(), copy_generic_path_info(), extract_actual_clauses(), make_samplescan(), order_qual_clauses(), Path::param_info, Path::parent, Scan::plan, planner_rt_fetch, RelOptInfo::relid, 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)
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)
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 
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:2908
static ValuesScan * create_valuesscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3826
static TableFuncScan * create_tablefuncscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3783
static CustomScan * create_customscan_plan(PlannerInfo *root, CustomPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:4245
static ForeignScan * create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:4101
static BitmapHeapScan * create_bitmap_scan_plan(PlannerInfo *root, BitmapHeapPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3190
static TidScan * create_tidscan_plan(PlannerInfo *root, TidPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3528
static WorkTableScan * create_worktablescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:4041
static FunctionScan * create_functionscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3740
static Result * create_resultscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:4004
static CteScan * create_ctescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3870
static NamedTuplestoreScan * create_namedtuplestorescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3965
static SubqueryScan * create_subqueryscan_plan(PlannerInfo *root, SubqueryScanPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3690
static TidRangeScan * create_tidrangescan_plan(PlannerInfo *root, TidRangePath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3625
static SampleScan * create_samplescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:2946
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:577
#define copyObject(obj)
Definition: nodes.h:689
List * build_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
Definition: plancat.c:1663

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::param_info, Path::parent, Path::pathtarget, Path::pathtype, ParamPathInfo::ppi_clauses, T_BitmapHeapScan, T_CteScan, T_CustomScan, T_ForeignScan, T_FunctionScan, T_IndexOnlyScan, T_IndexScan, T_NamedTuplestoreScan, T_Result, T_SampleScan, T_SeqScan, T_SubqueryScan, T_TableFuncScan, T_TidRangeScan, T_TidScan, T_ValuesScan, T_WorkTableScan, 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 2908 of file createplan.c.

2910 {
2911  SeqScan *scan_plan;
2912  Index scan_relid = best_path->