PostgreSQL Source Code  git master
createplan.c File Reference
#include "postgres.h"
#include <limits.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 is_async_capable_path (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 *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, uint32 est_entries)
 
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, 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, 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

◆ CP_IGNORE_TLIST

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

Definition at line 72 of file createplan.c.

Referenced by create_projection_plan(), and create_scan_plan().

◆ CP_LABEL_TLIST

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

◆ CP_SMALL_TLIST

Function Documentation

◆ bitmap_subplan_mark_shared()

static void bitmap_subplan_mark_shared ( Plan plan)
static

Definition at line 5338 of file createplan.c.

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

Referenced by create_bitmap_scan_plan().

5339 {
5340  if (IsA(plan, BitmapAnd))
5341  bitmap_subplan_mark_shared(linitial(((BitmapAnd *) plan)->bitmapplans));
5342  else if (IsA(plan, BitmapOr))
5343  {
5344  ((BitmapOr *) plan)->isshared = true;
5345  bitmap_subplan_mark_shared(linitial(((BitmapOr *) plan)->bitmapplans));
5346  }
5347  else if (IsA(plan, BitmapIndexScan))
5348  ((BitmapIndexScan *) plan)->isshared = true;
5349  else
5350  elog(ERROR, "unrecognized node type: %d", nodeTag(plan));
5351 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:589
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:46
static void bitmap_subplan_mark_shared(Plan *plan)
Definition: createplan.c:5338
#define nodeTag(nodeptr)
Definition: nodes.h:543
#define elog(elevel,...)
Definition: elog.h:232

◆ build_path_tlist()

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

Definition at line 801 of file createplan.c.

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().

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

◆ change_plan_targetlist()

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

Definition at line 2067 of file createplan.c.

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

Referenced by create_unique_plan(), and postgresGetForeignPlan().

2068 {
2069  /*
2070  * If the top plan node can't do projections and its existing target list
2071  * isn't already what we need, we need to add a Result node to help it
2072  * along.
2073  */
2074  if (!is_projection_capable_plan(subplan) &&
2075  !tlist_same_exprs(tlist, subplan->targetlist))
2076  subplan = inject_projection_plan(subplan, tlist,
2077  subplan->parallel_safe &&
2078  tlist_parallel_safe);
2079  else
2080  {
2081  /* Else we can just replace the plan node's tlist */
2082  subplan->targetlist = tlist;
2083  subplan->parallel_safe &= tlist_parallel_safe;
2084  }
2085  return subplan;
2086 }
bool is_projection_capable_plan(Plan *plan)
Definition: createplan.c:7079
static Plan * inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
Definition: createplan.c:2035
bool tlist_same_exprs(List *tlist1, List *tlist2)
Definition: tlist.c:207
List * targetlist
Definition: plannodes.h:141
bool parallel_safe
Definition: plannodes.h:130

◆ copy_generic_path_info()

static void copy_generic_path_info ( Plan dest,
Path src 
)
static

Definition at line 5269 of file createplan.c.

References Plan::parallel_aware, Path::parallel_aware, Plan::parallel_safe, Path::parallel_safe, Path::pathtarget, Plan::plan_rows, Plan::plan_width, Path::rows, Plan::startup_cost, Path::startup_cost, Plan::total_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().

5270 {
5271  dest->startup_cost = src->startup_cost;
5272  dest->total_cost = src->total_cost;
5273  dest->plan_rows = src->rows;
5274  dest->plan_width = src->pathtarget->width;
5275  dest->parallel_aware = src->parallel_aware;
5276  dest->parallel_safe = src->parallel_safe;
5277 }
PathTarget * pathtarget
Definition: pathnodes.h:1183
Cost startup_cost
Definition: pathnodes.h:1193
Cost startup_cost
Definition: plannodes.h:117
bool parallel_aware
Definition: plannodes.h:129
Cost total_cost
Definition: pathnodes.h:1194
int plan_width
Definition: plannodes.h:124
bool parallel_safe
Definition: pathnodes.h:1188
Cardinality plan_rows
Definition: plannodes.h:123
Cost total_cost
Definition: plannodes.h:118
bool parallel_aware
Definition: pathnodes.h:1187
bool parallel_safe
Definition: plannodes.h:130
Cardinality rows
Definition: pathnodes.h:1192

◆ copy_plan_costsize()

static void copy_plan_costsize ( Plan dest,
Plan src 
)
static

Definition at line 5284 of file createplan.c.

References Plan::parallel_aware, 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().

5285 {
5286  dest->startup_cost = src->startup_cost;
5287  dest->total_cost = src->total_cost;
5288  dest->plan_rows = src->plan_rows;
5289  dest->plan_width = src->plan_width;
5290  /* Assume the inserted node is not parallel-aware. */
5291  dest->parallel_aware = false;
5292  /* Assume the inserted node is parallel-safe, if child plan is. */
5293  dest->parallel_safe = src->parallel_safe;
5294 }
Cost startup_cost
Definition: plannodes.h:117
bool parallel_aware
Definition: plannodes.h:129
int plan_width
Definition: plannodes.h:124
Cardinality plan_rows
Definition: plannodes.h:123
Cost total_cost
Definition: plannodes.h:118
bool parallel_safe
Definition: plannodes.h:130

◆ create_agg_plan()

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

Definition at line 2223 of file createplan.c.

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().

2224 {
2225  Agg *plan;
2226  Plan *subplan;
2227  List *tlist;
2228  List *quals;
2229 
2230  /*
2231  * Agg can project, so no need to be terribly picky about child tlist, but
2232  * we do need grouping columns to be available
2233  */
2234  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2235 
2236  tlist = build_path_tlist(root, &best_path->path);
2237 
2238  quals = order_qual_clauses(root, best_path->qual);
2239 
2240  plan = make_agg(tlist, quals,
2241  best_path->aggstrategy,
2242  best_path->aggsplit,
2243  list_length(best_path->groupClause),
2245  subplan->targetlist),
2246  extract_grouping_ops(best_path->groupClause),
2248  subplan->targetlist),
2249  NIL,
2250  NIL,
2251  best_path->numGroups,
2252  best_path->transitionSpace,
2253  subplan);
2254 
2255  copy_generic_path_info(&plan->plan, (Path *) best_path);
2256 
2257  return plan;
2258 }
#define NIL
Definition: pg_list.h:65
AggStrategy aggstrategy
Definition: pathnodes.h:1771
Oid * extract_grouping_collations(List *groupClause, List *tlist)
Definition: tlist.c:478
List * qual
Definition: pathnodes.h:1776
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5175
AggSplit aggsplit
Definition: pathnodes.h:1772
Oid * extract_grouping_ops(List *groupClause)
Definition: tlist.c:452
AttrNumber * extract_grouping_cols(List *groupClause, List *tlist)
Definition: tlist.c:503
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:383
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
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:6441
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:801
List * groupClause
Definition: pathnodes.h:1775
Plan plan
Definition: plannodes.h:862
#define CP_LABEL_TLIST
Definition: createplan.c:71
Cardinality numGroups
Definition: pathnodes.h:1773
uint64 transitionSpace
Definition: pathnodes.h:1774
static int list_length(const List *l)
Definition: pg_list.h:149
Path * subpath
Definition: pathnodes.h:1770
List * targetlist
Definition: plannodes.h:141
Definition: plannodes.h:860
Definition: pg_list.h:50
Path path
Definition: pathnodes.h:1769

◆ create_append_plan()

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

Definition at line 1126 of file createplan.c.

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(), is_async_capable_path(), 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, Append::nasyncplans, NIL, Plan::parallel_safe, Path::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().

1127 {
1128  Append *plan;
1129  List *tlist = build_path_tlist(root, &best_path->path);
1130  int orig_tlist_length = list_length(tlist);
1131  bool tlist_was_changed = false;
1132  List *pathkeys = best_path->path.pathkeys;
1133  List *subplans = NIL;
1134  ListCell *subpaths;
1135  int nasyncplans = 0;
1136  RelOptInfo *rel = best_path->path.parent;
1137  PartitionPruneInfo *partpruneinfo = NULL;
1138  int nodenumsortkeys = 0;
1139  AttrNumber *nodeSortColIdx = NULL;
1140  Oid *nodeSortOperators = NULL;
1141  Oid *nodeCollations = NULL;
1142  bool *nodeNullsFirst = NULL;
1143  bool consider_async = false;
1144 
1145  /*
1146  * The subpaths list could be empty, if every child was proven empty by
1147  * constraint exclusion. In that case generate a dummy plan that returns
1148  * no rows.
1149  *
1150  * Note that an AppendPath with no members is also generated in certain
1151  * cases where there was no appending construct at all, but we know the
1152  * relation is empty (see set_dummy_rel_pathlist and mark_dummy_rel).
1153  */
1154  if (best_path->subpaths == NIL)
1155  {
1156  /* Generate a Result plan with constant-FALSE gating qual */
1157  Plan *plan;
1158 
1159  plan = (Plan *) make_result(tlist,
1160  (Node *) list_make1(makeBoolConst(false,
1161  false)),
1162  NULL);
1163 
1164  copy_generic_path_info(plan, (Path *) best_path);
1165 
1166  return plan;
1167  }
1168 
1169  /*
1170  * Otherwise build an Append plan. Note that if there's just one child,
1171  * the Append is pretty useless; but we wait till setrefs.c to get rid of
1172  * it. Doing so here doesn't work because the varno of the child scan
1173  * plan won't match the parent-rel Vars it'll be asked to emit.
1174  *
1175  * We don't have the actual creation of the Append node split out into a
1176  * separate make_xxx function. This is because we want to run
1177  * prepare_sort_from_pathkeys on it before we do so on the individual
1178  * child plans, to make cross-checking the sort info easier.
1179  */
1180  plan = makeNode(Append);
1181  plan->plan.targetlist = tlist;
1182  plan->plan.qual = NIL;
1183  plan->plan.lefttree = NULL;
1184  plan->plan.righttree = NULL;
1185  plan->apprelids = rel->relids;
1186 
1187  if (pathkeys != NIL)
1188  {
1189  /*
1190  * Compute sort column info, and adjust the Append's tlist as needed.
1191  * Because we pass adjust_tlist_in_place = true, we may ignore the
1192  * function result; it must be the same plan node. However, we then
1193  * need to detect whether any tlist entries were added.
1194  */
1195  (void) prepare_sort_from_pathkeys((Plan *) plan, pathkeys,
1196  best_path->path.parent->relids,
1197  NULL,
1198  true,
1199  &nodenumsortkeys,
1200  &nodeSortColIdx,
1201  &nodeSortOperators,
1202  &nodeCollations,
1203  &nodeNullsFirst);
1204  tlist_was_changed = (orig_tlist_length != list_length(plan->plan.targetlist));
1205  }
1206 
1207  /* If appropriate, consider async append */
1208  consider_async = (enable_async_append && pathkeys == NIL &&
1209  !best_path->path.parallel_safe &&
1210  list_length(best_path->subpaths) > 1);
1211 
1212  /* Build the plan for each child */
1213  foreach(subpaths, best_path->subpaths)
1214  {
1215  Path *subpath = (Path *) lfirst(subpaths);
1216  Plan *subplan;
1217 
1218  /* Must insist that all children return the same tlist */
1219  subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
1220 
1221  /*
1222  * For ordered Appends, we must insert a Sort node if subplan isn't
1223  * sufficiently ordered.
1224  */
1225  if (pathkeys != NIL)
1226  {
1227  int numsortkeys;
1228  AttrNumber *sortColIdx;
1229  Oid *sortOperators;
1230  Oid *collations;
1231  bool *nullsFirst;
1232 
1233  /*
1234  * Compute sort column info, and adjust subplan's tlist as needed.
1235  * We must apply prepare_sort_from_pathkeys even to subplans that
1236  * don't need an explicit sort, to make sure they are returning
1237  * the same sort key columns the Append expects.
1238  */
1239  subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1240  subpath->parent->relids,
1241  nodeSortColIdx,
1242  false,
1243  &numsortkeys,
1244  &sortColIdx,
1245  &sortOperators,
1246  &collations,
1247  &nullsFirst);
1248 
1249  /*
1250  * Check that we got the same sort key information. We just
1251  * Assert that the sortops match, since those depend only on the
1252  * pathkeys; but it seems like a good idea to check the sort
1253  * column numbers explicitly, to ensure the tlists match up.
1254  */
1255  Assert(numsortkeys == nodenumsortkeys);
1256  if (memcmp(sortColIdx, nodeSortColIdx,
1257  numsortkeys * sizeof(AttrNumber)) != 0)
1258  elog(ERROR, "Append child's targetlist doesn't match Append");
1259  Assert(memcmp(sortOperators, nodeSortOperators,
1260  numsortkeys * sizeof(Oid)) == 0);
1261  Assert(memcmp(collations, nodeCollations,
1262  numsortkeys * sizeof(Oid)) == 0);
1263  Assert(memcmp(nullsFirst, nodeNullsFirst,
1264  numsortkeys * sizeof(bool)) == 0);
1265 
1266  /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1267  if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
1268  {
1269  Sort *sort = make_sort(subplan, numsortkeys,
1270  sortColIdx, sortOperators,
1271  collations, nullsFirst);
1272 
1273  label_sort_with_costsize(root, sort, best_path->limit_tuples);
1274  subplan = (Plan *) sort;
1275  }
1276  }
1277 
1278  subplans = lappend(subplans, subplan);
1279 
1280  /* Check to see if subplan can be executed asynchronously */
1281  if (consider_async && is_async_capable_path(subpath))
1282  {
1283  subplan->async_capable = true;
1284  ++nasyncplans;
1285  }
1286  }
1287 
1288  /*
1289  * If any quals exist, they may be useful to perform further partition
1290  * pruning during execution. Gather information needed by the executor to
1291  * do partition pruning.
1292  */
1294  {
1295  List *prunequal;
1296 
1297  prunequal = extract_actual_clauses(rel->baserestrictinfo, false);
1298 
1299  if (best_path->path.param_info)
1300  {
1301  List *prmquals = best_path->path.param_info->ppi_clauses;
1302 
1303  prmquals = extract_actual_clauses(prmquals, false);
1304  prmquals = (List *) replace_nestloop_params(root,
1305  (Node *) prmquals);
1306 
1307  prunequal = list_concat(prunequal, prmquals);
1308  }
1309 
1310  if (prunequal != NIL)
1311  partpruneinfo =
1312  make_partition_pruneinfo(root, rel,
1313  best_path->subpaths,
1314  prunequal);
1315  }
1316 
1317  plan->appendplans = subplans;
1318  plan->nasyncplans = nasyncplans;
1319  plan->first_partial_plan = best_path->first_partial_path;
1320  plan->part_prune_info = partpruneinfo;
1321 
1322  copy_generic_path_info(&plan->plan, (Path *) best_path);
1323 
1324  /*
1325  * If prepare_sort_from_pathkeys added sort columns, but we were told to
1326  * produce either the exact tlist or a narrow tlist, we should get rid of
1327  * the sort columns again. We must inject a projection node to do so.
1328  */
1329  if (tlist_was_changed && (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST)))
1330  {
1331  tlist = list_truncate(list_copy(plan->plan.targetlist),
1332  orig_tlist_length);
1333  return inject_projection_plan((Plan *) plan, tlist,
1334  plan->plan.parallel_safe);
1335  }
1336  else
1337  return (Plan *) plan;
1338 }
#define NIL
Definition: pg_list.h:65
List * qual
Definition: plannodes.h:142
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4790
List * baserestrictinfo
Definition: pathnodes.h:745
List * list_truncate(List *list, int new_size)
Definition: list.c:600
ParamPathInfo * param_info
Definition: pathnodes.h:1185
#define CP_SMALL_TLIST
Definition: createplan.c:70
List * list_copy(const List *oldlist)
Definition: list.c:1418
Definition: nodes.h:538
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
unsigned int Oid
Definition: postgres_ext.h:31
int first_partial_path
Definition: pathnodes.h:1454
#define list_make1(x1)
Definition: pg_list.h:206
List * subpaths
Definition: pathnodes.h:1452
bool async_capable
Definition: plannodes.h:135
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:383
struct Plan * righttree
Definition: plannodes.h:144
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
#define ERROR
Definition: elog.h:46
Cardinality limit_tuples
Definition: pathnodes.h:1455
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:801
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:357
RelOptInfo * parent
Definition: pathnodes.h:1182
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:195
Relids relids
Definition: pathnodes.h:681
bool enable_async_append
Definition: costsize.c:152
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6833
List * lappend(List *list, void *datum)
Definition: list.c:336
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:324
bool enable_partition_pruning
Definition: costsize.c:151
List * pathkeys
Definition: pathnodes.h:1196
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
Definition: createplan.c:5923
#define makeNode(_type_)
Definition: nodes.h:586
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
bool parallel_safe
Definition: pathnodes.h:1188
List * ppi_clauses
Definition: pathnodes.h:1143
static Plan * inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
Definition: createplan.c:2035
static int list_length(const List *l)
Definition: pg_list.h:149
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:443
struct Plan * lefttree
Definition: plannodes.h:143
static bool is_async_capable_path(Path *path)
Definition: createplan.c:1098
List * targetlist
Definition: plannodes.h:141
PartitionPruneInfo * make_partition_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel, List *subpaths, List *prunequal)
Definition: partprune.c:222
#define elog(elevel,...)
Definition: elog.h:232
bool parallel_safe
Definition: plannodes.h:130
#define CP_EXACT_TLIST
Definition: createplan.c:69
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
static void label_sort_with_costsize(PlannerInfo *root, Sort *plan, double limit_tuples)
Definition: createplan.c:5306
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:241
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:6019

◆ create_bitmap_scan_plan()

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

Definition at line 3093 of file createplan.c.

References Assert, bitmap_subplan_mark_shared(), BitmapHeapPath::bitmapqual, RestrictInfo::clause, contain_mutable_functions(), copy_generic_path_info(), create_bitmap_subplan(), extract_actual_clauses(), 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().

3097 {
3098  Index baserelid = best_path->path.parent->relid;
3099  Plan *bitmapqualplan;
3100  List *bitmapqualorig;
3101  List *indexquals;
3102  List *indexECs;
3103  List *qpqual;
3104  ListCell *l;
3105  BitmapHeapScan *scan_plan;
3106 
3107  /* it should be a base rel... */
3108  Assert(baserelid > 0);
3109  Assert(best_path->path.parent->rtekind == RTE_RELATION);
3110 
3111  /* Process the bitmapqual tree into a Plan tree and qual lists */
3112  bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
3113  &bitmapqualorig, &indexquals,
3114  &indexECs);
3115 
3116  if (best_path->path.parallel_aware)
3117  bitmap_subplan_mark_shared(bitmapqualplan);
3118 
3119  /*
3120  * The qpqual list must contain all restrictions not automatically handled
3121  * by the index, other than pseudoconstant clauses which will be handled
3122  * by a separate gating plan node. All the predicates in the indexquals
3123  * will be checked (either by the index itself, or by
3124  * nodeBitmapHeapscan.c), but if there are any "special" operators
3125  * involved then they must be added to qpqual. The upshot is that qpqual
3126  * must contain scan_clauses minus whatever appears in indexquals.
3127  *
3128  * This loop is similar to the comparable code in create_indexscan_plan(),
3129  * but with some differences because it has to compare the scan clauses to
3130  * stripped (no RestrictInfos) indexquals. See comments there for more
3131  * info.
3132  *
3133  * In normal cases simple equal() checks will be enough to spot duplicate
3134  * clauses, so we try that first. We next see if the scan clause is
3135  * redundant with any top-level indexqual by virtue of being generated
3136  * from the same EC. After that, try predicate_implied_by().
3137  *
3138  * Unlike create_indexscan_plan(), the predicate_implied_by() test here is
3139  * useful for getting rid of qpquals that are implied by index predicates,
3140  * because the predicate conditions are included in the "indexquals"
3141  * returned by create_bitmap_subplan(). Bitmap scans have to do it that
3142  * way because predicate conditions need to be rechecked if the scan
3143  * becomes lossy, so they have to be included in bitmapqualorig.
3144  */
3145  qpqual = NIL;
3146  foreach(l, scan_clauses)
3147  {
3148  RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
3149  Node *clause = (Node *) rinfo->clause;
3150 
3151  if (rinfo->pseudoconstant)
3152  continue; /* we may drop pseudoconstants here */
3153  if (list_member(indexquals, clause))
3154  continue; /* simple duplicate */
3155  if (rinfo->parent_ec && list_member_ptr(indexECs, rinfo->parent_ec))
3156  continue; /* derived from same EquivalenceClass */
3157  if (!contain_mutable_functions(clause) &&
3158  predicate_implied_by(list_make1(clause), indexquals, false))
3159  continue; /* provably implied by indexquals */
3160  qpqual = lappend(qpqual, rinfo);
3161  }
3162 
3163  /* Sort clauses into best execution order */
3164  qpqual = order_qual_clauses(root, qpqual);
3165 
3166  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3167  qpqual = extract_actual_clauses(qpqual, false);
3168 
3169  /*
3170  * When dealing with special operators, we will at this point have
3171  * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
3172  * 'em from bitmapqualorig, since there's no point in making the tests
3173  * twice.
3174  */
3175  bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
3176 
3177  /*
3178  * We have to replace any outer-relation variables with nestloop params in
3179  * the qpqual and bitmapqualorig expressions. (This was already done for
3180  * expressions attached to plan nodes in the bitmapqualplan tree.)
3181  */
3182  if (best_path->path.param_info)
3183  {
3184  qpqual = (List *)
3185  replace_nestloop_params(root, (Node *) qpqual);
3186  bitmapqualorig = (List *)
3187  replace_nestloop_params(root, (Node *) bitmapqualorig);
3188  }
3189 
3190  /* Finally ready to build the plan node */
3191  scan_plan = make_bitmap_heapscan(tlist,
3192  qpqual,
3193  bitmapqualplan,
3194  bitmapqualorig,
3195  baserelid);
3196 
3197  copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3198 
3199  return scan_plan;
3200 }
#define NIL
Definition: pg_list.h:65
Plan plan
Definition: plannodes.h:343
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4790
List * list_difference_ptr(const List *list1, const List *list2)
Definition: list.c:1117
bool pseudoconstant
Definition: pathnodes.h:2064
ParamPathInfo * param_info
Definition: pathnodes.h:1185
Definition: nodes.h:538
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5175
static Plan * create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual, List **qual, List **indexqual, List **indexECs)
Definition: createplan.c:3223
#define list_make1(x1)
Definition: pg_list.h:206
static BitmapHeapScan * make_bitmap_heapscan(List *qptlist, List *qpqual, Plan *lefttree, List *bitmapqualorig, Index scanrelid)
Definition: createplan.c:5483
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
bool list_member(const List *list, const void *datum)
Definition: list.c:628
EquivalenceClass * parent_ec
Definition: pathnodes.h:2093
RelOptInfo * parent
Definition: pathnodes.h:1182
Path * bitmapqual
Definition: pathnodes.h:1318
#define lfirst_node(type, lc)
Definition: pg_list.h:172
Index relid
Definition: pathnodes.h:709
List * lappend(List *list, void *datum)
Definition: list.c:336
static void bitmap_subplan_mark_shared(Plan *plan)
Definition: createplan.c:5338
Expr * clause
Definition: pathnodes.h:2056
unsigned int Index
Definition: c.h:549
RTEKind rtekind
Definition: pathnodes.h:711
bool list_member_ptr(const List *list, const void *datum)
Definition: list.c:649
#define Assert(condition)
Definition: c.h:804
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:443
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:363
bool parallel_aware
Definition: pathnodes.h:1187
bool predicate_implied_by(List *predicate_list, List *clause_list, bool weak)
Definition: predtest.c:151
Definition: pg_list.h:50

◆ create_bitmap_subplan()

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

Definition at line 3223 of file createplan.c.

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, Plan::parallel_safe, Path::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, Plan::startup_cost, Path::startup_cost, Plan::total_cost, Path::total_cost, and RelOptInfo::tuples.

Referenced by create_bitmap_scan_plan().

3225 {
3226  Plan *plan;
3227 
3228  if (IsA(bitmapqual, BitmapAndPath))
3229  {
3230  BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
3231  List *subplans = NIL;
3232  List *subquals = NIL;
3233  List *subindexquals = NIL;
3234  List *subindexECs = NIL;
3235  ListCell *l;
3236 
3237  /*
3238  * There may well be redundant quals among the subplans, since a
3239  * top-level WHERE qual might have gotten used to form several
3240  * different index quals. We don't try exceedingly hard to eliminate
3241  * redundancies, but we do eliminate obvious duplicates by using
3242  * list_concat_unique.
3243  */
3244  foreach(l, apath->bitmapquals)
3245  {
3246  Plan *subplan;
3247  List *subqual;
3248  List *subindexqual;
3249  List *subindexEC;
3250 
3251  subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
3252  &subqual, &subindexqual,
3253  &subindexEC);
3254  subplans = lappend(subplans, subplan);
3255  subquals = list_concat_unique(subquals, subqual);
3256  subindexquals = list_concat_unique(subindexquals, subindexqual);
3257  /* Duplicates in indexECs aren't worth getting rid of */
3258  subindexECs = list_concat(subindexECs, subindexEC);
3259  }
3260  plan = (Plan *) make_bitmap_and(subplans);
3261  plan->startup_cost = apath->path.startup_cost;
3262  plan->total_cost = apath->path.total_cost;
3263  plan->plan_rows =
3264  clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
3265  plan->plan_width = 0; /* meaningless */
3266  plan->parallel_aware = false;
3267  plan->parallel_safe = apath->path.parallel_safe;
3268  *qual = subquals;
3269  *indexqual = subindexquals;
3270  *indexECs = subindexECs;
3271  }
3272  else if (IsA(bitmapqual, BitmapOrPath))
3273  {
3274  BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
3275  List *subplans = NIL;
3276  List *subquals = NIL;
3277  List *subindexquals = NIL;
3278  bool const_true_subqual = false;
3279  bool const_true_subindexqual = false;
3280  ListCell *l;
3281 
3282  /*
3283  * Here, we only detect qual-free subplans. A qual-free subplan would
3284  * cause us to generate "... OR true ..." which we may as well reduce
3285  * to just "true". We do not try to eliminate redundant subclauses
3286  * because (a) it's not as likely as in the AND case, and (b) we might
3287  * well be working with hundreds or even thousands of OR conditions,
3288  * perhaps from a long IN list. The performance of list_append_unique
3289  * would be unacceptable.
3290  */
3291  foreach(l, opath->bitmapquals)
3292  {
3293  Plan *subplan;
3294  List *subqual;
3295  List *subindexqual;
3296  List *subindexEC;
3297 
3298  subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
3299  &subqual, &subindexqual,
3300  &subindexEC);
3301  subplans = lappend(subplans, subplan);
3302  if (subqual == NIL)
3303  const_true_subqual = true;
3304  else if (!const_true_subqual)
3305  subquals = lappend(subquals,
3306  make_ands_explicit(subqual));
3307  if (subindexqual == NIL)
3308  const_true_subindexqual = true;
3309  else if (!const_true_subindexqual)
3310  subindexquals = lappend(subindexquals,
3311  make_ands_explicit(subindexqual));
3312  }
3313 
3314  /*
3315  * In the presence of ScalarArrayOpExpr quals, we might have built
3316  * BitmapOrPaths with just one subpath; don't add an OR step.
3317  */
3318  if (list_length(subplans) == 1)
3319  {
3320  plan = (Plan *) linitial(subplans);
3321  }
3322  else
3323  {
3324  plan = (Plan *) make_bitmap_or(subplans);
3325  plan->startup_cost = opath->path.startup_cost;
3326  plan->total_cost = opath->path.total_cost;
3327  plan->plan_rows =
3328  clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
3329  plan->plan_width = 0; /* meaningless */
3330  plan->parallel_aware = false;
3331  plan->parallel_safe = opath->path.parallel_safe;
3332  }
3333 
3334  /*
3335  * If there were constant-TRUE subquals, the OR reduces to constant
3336  * TRUE. Also, avoid generating one-element ORs, which could happen
3337  * due to redundancy elimination or ScalarArrayOpExpr quals.
3338  */
3339  if (const_true_subqual)
3340  *qual = NIL;
3341  else if (list_length(subquals) <= 1)
3342  *qual = subquals;
3343  else
3344  *qual = list_make1(make_orclause(subquals));
3345  if (const_true_subindexqual)
3346  *indexqual = NIL;
3347  else if (list_length(subindexquals) <= 1)
3348  *indexqual = subindexquals;
3349  else
3350  *indexqual = list_make1(make_orclause(subindexquals));
3351  *indexECs = NIL;
3352  }
3353  else if (IsA(bitmapqual, IndexPath))
3354  {
3355  IndexPath *ipath = (IndexPath *) bitmapqual;
3356  IndexScan *iscan;
3357  List *subquals;
3358  List *subindexquals;
3359  List *subindexECs;
3360  ListCell *l;
3361 
3362  /* Use the regular indexscan plan build machinery... */
3363  iscan = castNode(IndexScan,
3364  create_indexscan_plan(root, ipath,
3365  NIL, NIL, false));
3366  /* then convert to a bitmap indexscan */
3367  plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
3368  iscan->indexid,
3369  iscan->indexqual,
3370  iscan->indexqualorig);
3371  /* and set its cost/width fields appropriately */
3372  plan->startup_cost = 0.0;
3373  plan->total_cost = ipath->indextotalcost;
3374  plan->plan_rows =
3375  clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
3376  plan->plan_width = 0; /* meaningless */
3377  plan->parallel_aware = false;
3378  plan->parallel_safe = ipath->path.parallel_safe;
3379  /* Extract original index clauses, actual index quals, relevant ECs */
3380  subquals = NIL;
3381  subindexquals = NIL;
3382  subindexECs = NIL;
3383  foreach(l, ipath->indexclauses)
3384  {
3385  IndexClause *iclause = (IndexClause *) lfirst(l);
3386  RestrictInfo *rinfo = iclause->rinfo;
3387 
3388  Assert(!rinfo->pseudoconstant);
3389  subquals = lappend(subquals, rinfo->clause);
3390  subindexquals = list_concat(subindexquals,
3391  get_actual_clauses(iclause->indexquals));
3392  if (rinfo->parent_ec)
3393  subindexECs = lappend(subindexECs, rinfo->parent_ec);
3394  }
3395  /* We can add any index predicate conditions, too */
3396  foreach(l, ipath->indexinfo->indpred)
3397  {
3398  Expr *pred = (Expr *) lfirst(l);
3399 
3400  /*
3401  * We know that the index predicate must have been implied by the
3402  * query condition as a whole, but it may or may not be implied by
3403  * the conditions that got pushed into the bitmapqual. Avoid
3404  * generating redundant conditions.
3405  */
3406  if (!predicate_implied_by(list_make1(pred), subquals, false))
3407  {
3408  subquals = lappend(subquals, pred);
3409  subindexquals = lappend(subindexquals, pred);
3410  }
3411  }
3412  *qual = subquals;
3413  *indexqual = subindexquals;
3414  *indexECs = subindexECs;
3415  }
3416  else
3417  {
3418  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
3419  plan = NULL; /* keep compiler quiet */
3420  }
3421 
3422  return plan;
3423 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:589
Path path
Definition: pathnodes.h:1244
IndexOptInfo * indexinfo
Definition: pathnodes.h:1245
Index scanrelid
Definition: plannodes.h:344
#define castNode(_type_, nodeptr)
Definition: nodes.h:607
bool pseudoconstant
Definition: pathnodes.h:2064
List * indexqualorig
Definition: plannodes.h:409
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:420
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
List * indexclauses
Definition: pathnodes.h:1246
static Plan * create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual, List **qual, List **indexqual, List **indexECs)
Definition: createplan.c:3223
Selectivity bitmapselectivity
Definition: pathnodes.h:1331
Expr * make_orclause(List *orclauses)
Definition: makefuncs.c:652
static BitmapAnd * make_bitmap_and(List *bitmapplans)
Definition: createplan.c:5773
Oid indexid
Definition: plannodes.h:407
List * bitmapquals
Definition: pathnodes.h:1330
List * bitmapquals
Definition: pathnodes.h:1343
#define list_make1(x1)
Definition: pg_list.h:206
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:46
Cost indextotalcost
Definition: pathnodes.h:1250
Cost startup_cost
Definition: pathnodes.h:1193
Scan scan
Definition: plannodes.h:406
EquivalenceClass * parent_ec
Definition: pathnodes.h:2093
RelOptInfo * parent
Definition: pathnodes.h:1182
Selectivity indexselectivity
Definition: pathnodes.h:1251
struct RestrictInfo * rinfo
Definition: pathnodes.h:1291
Cost startup_cost
Definition: plannodes.h:117
bool parallel_aware
Definition: plannodes.h:129
Selectivity bitmapselectivity
Definition: pathnodes.h:1344
List * indexqual
Definition: plannodes.h:408
List * indexquals
Definition: pathnodes.h:1292
List * lappend(List *list, void *datum)
Definition: list.c:336
Expr * clause
Definition: pathnodes.h:2056
Expr * make_ands_explicit(List *andclauses)
Definition: makefuncs.c:708
static BitmapOr * make_bitmap_or(List *bitmapplans)
Definition: createplan.c:5788
Cost total_cost
Definition: pathnodes.h:1194
int plan_width
Definition: plannodes.h:124
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
bool parallel_safe
Definition: pathnodes.h:1188
static int list_length(const List *l)
Definition: pg_list.h:149
Cardinality plan_rows
Definition: plannodes.h:123
static BitmapIndexScan * make_bitmap_indexscan(Index scanrelid, Oid indexid, List *indexqual, List *indexqualorig)
Definition: createplan.c:5462
#define nodeTag(nodeptr)
Definition: nodes.h:543
#define elog(elevel,...)
Definition: elog.h:232
Cost total_cost
Definition: plannodes.h:118
bool parallel_safe
Definition: plannodes.h:130
List * indpred
Definition: pathnodes.h:864
double clamp_row_est(double nrows)
Definition: costsize.c:199
bool predicate_implied_by(List *predicate_list, List *clause_list, bool weak)
Definition: predtest.c:151
Definition: pg_list.h:50
static Scan * create_indexscan_plan(PlannerInfo *root, IndexPath *best_path, List *tlist, List *scan_clauses, bool indexonly)
Definition: createplan.c:2920
List * list_concat_unique(List *list1, const List *list2)
Definition: list.c:1252
Cardinality tuples
Definition: pathnodes.h:721

◆ create_ctescan_plan()

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

Definition at line 3773 of file createplan.c.

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().

3775 {
3776  CteScan *scan_plan;
3777  Index scan_relid = best_path->parent->relid;
3778  RangeTblEntry *rte;
3779  SubPlan *ctesplan = NULL;
3780  int plan_id;
3781  int cte_param_id;
3782  PlannerInfo *cteroot;
3783  Index levelsup;
3784  int ndx;
3785  ListCell *lc;
3786 
3787  Assert(scan_relid > 0);
3788  rte = planner_rt_fetch(scan_relid, root);
3789  Assert(rte->rtekind == RTE_CTE);
3790  Assert(!rte->self_reference);
3791 
3792  /*
3793  * Find the referenced CTE, and locate the SubPlan previously made for it.
3794  */
3795  levelsup = rte->ctelevelsup;
3796  cteroot = root;
3797  while (levelsup-- > 0)
3798  {
3799  cteroot = cteroot->parent_root;
3800  if (!cteroot) /* shouldn't happen */
3801  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3802  }
3803 
3804  /*
3805  * Note: cte_plan_ids can be shorter than cteList, if we are still working
3806  * on planning the CTEs (ie, this is a side-reference from another CTE).
3807  * So we mustn't use forboth here.
3808  */
3809  ndx = 0;
3810  foreach(lc, cteroot->parse->cteList)
3811  {
3812  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
3813 
3814  if (strcmp(cte->ctename, rte->ctename) == 0)
3815  break;
3816  ndx++;
3817  }
3818  if (lc == NULL) /* shouldn't happen */
3819  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
3820  if (ndx >= list_length(cteroot->cte_plan_ids))
3821  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3822  plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
3823  Assert(plan_id > 0);
3824  foreach(lc, cteroot->init_plans)
3825  {
3826  ctesplan = (SubPlan *) lfirst(lc);
3827  if (ctesplan->plan_id == plan_id)
3828  break;
3829  }
3830  if (lc == NULL) /* shouldn't happen */
3831  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3832 
3833  /*
3834  * We need the CTE param ID, which is the sole member of the SubPlan's
3835  * setParam list.
3836  */
3837  cte_param_id = linitial_int(ctesplan->setParam);
3838 
3839  /* Sort clauses into best execution order */
3840  scan_clauses = order_qual_clauses(root, scan_clauses);
3841 
3842  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3843  scan_clauses = extract_actual_clauses(scan_clauses, false);
3844 
3845  /* Replace any outer-relation variables with nestloop params */
3846  if (best_path->param_info)
3847  {
3848  scan_clauses = (List *)
3849  replace_nestloop_params(root, (Node *) scan_clauses);
3850  }
3851 
3852  scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
3853  plan_id, cte_param_id);
3854 
3855  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3856 
3857  return scan_plan;
3858 }
Plan plan
Definition: plannodes.h:343
Query * parse
Definition: pathnodes.h:162
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4790
int plan_id
Definition: primnodes.h:752
PlannerInfo * parent_root
Definition: pathnodes.h:168
ParamPathInfo * param_info
Definition: pathnodes.h:1185
Definition: nodes.h:538
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5175
static int list_nth_int(const List *list, int n)
Definition: pg_list.h:289
#define linitial_int(l)
Definition: pg_list.h:175
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
#define ERROR
Definition: elog.h:46
Scan scan
Definition: plannodes.h:570
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:388
RelOptInfo * parent
Definition: pathnodes.h:1182
Index relid
Definition: pathnodes.h:709
static CteScan * make_ctescan(List *qptlist, List *qpqual, Index scanrelid, int ctePlanId, int cteParam)
Definition: createplan.c:5619
List * cte_plan_ids
Definition: pathnodes.h:244
bool self_reference
Definition: parsenodes.h:1119
unsigned int Index
Definition: c.h:549
List * init_plans
Definition: pathnodes.h:242
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
List * setParam
Definition: primnodes.h:770
static int list_length(const List *l)
Definition: pg_list.h:149
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:443
Index ctelevelsup
Definition: parsenodes.h:1118
RTEKind rtekind
Definition: parsenodes.h:1007
List * cteList
Definition: parsenodes.h:145
char * ctename
Definition: parsenodes.h:1117
#define elog(elevel,...)
Definition: elog.h:232
Definition: pg_list.h:50

◆ create_customscan_plan()

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

Definition at line 4147 of file createplan.c.

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().

4149 {
4150  CustomScan *cplan;
4151  RelOptInfo *rel = best_path->path.parent;
4152  List *custom_plans = NIL;
4153  ListCell *lc;
4154 
4155  /* Recursively transform child paths. */
4156  foreach(lc, best_path->custom_paths)
4157  {
4158  Plan *plan = create_plan_recurse(root, (Path *) lfirst(lc),
4159  CP_EXACT_TLIST);
4160 
4161  custom_plans = lappend(custom_plans, plan);
4162  }
4163 
4164  /*
4165  * Sort clauses into the best execution order, although custom-scan
4166  * provider can reorder them again.
4167  */
4168  scan_clauses = order_qual_clauses(root, scan_clauses);
4169 
4170  /*
4171  * Invoke custom plan provider to create the Plan node represented by the
4172  * CustomPath.
4173  */
4174  cplan = castNode(CustomScan,
4175  best_path->methods->PlanCustomPath(root,
4176  rel,
4177  best_path,
4178  tlist,
4179  scan_clauses,
4180  custom_plans));
4181 
4182  /*
4183  * Copy cost data from Path to Plan; no need to make custom-plan providers
4184  * do this
4185  */
4186  copy_generic_path_info(&cplan->scan.plan, &best_path->path);
4187 
4188  /* Likewise, copy the relids that are represented by this custom scan */
4189  cplan->custom_relids = best_path->path.parent->relids;
4190 
4191  /*
4192  * Replace any outer-relation variables with nestloop params in the qual
4193  * and custom_exprs expressions. We do this last so that the custom-plan
4194  * provider doesn't have to be involved. (Note that parts of custom_exprs
4195  * could have come from join clauses, so doing this beforehand on the
4196  * scan_clauses wouldn't work.) We assume custom_scan_tlist contains no
4197  * such variables.
4198  */
4199  if (best_path->path.param_info)
4200  {
4201  cplan->scan.plan.qual = (List *)
4202  replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
4203  cplan->custom_exprs = (List *)
4204  replace_nestloop_params(root, (Node *) cplan->custom_exprs);
4205  }
4206 
4207  return cplan;
4208 }
#define NIL
Definition: pg_list.h:65
List * qual
Definition: plannodes.h:142
Plan plan
Definition: plannodes.h:343
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4790
List * custom_paths
Definition: pathnodes.h:1429
#define castNode(_type_, nodeptr)
Definition: nodes.h:607
ParamPathInfo * param_info
Definition: pathnodes.h:1185
Definition: nodes.h:538
List * custom_exprs
Definition: plannodes.h:669
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5175
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:383
const struct CustomPathMethods * methods
Definition: pathnodes.h:1431
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
RelOptInfo * parent
Definition: pathnodes.h:1182
Relids relids
Definition: pathnodes.h:681
List * lappend(List *list, void *datum)
Definition: list.c:336
Scan scan
Definition: plannodes.h:665
struct Plan *(* PlanCustomPath)(PlannerInfo *root, RelOptInfo *rel, struct CustomPath *best_path, List *tlist, List *clauses, List *custom_plans)
Definition: extensible.h:95
#define lfirst(lc)
Definition: pg_list.h:169
Bitmapset * custom_relids
Definition: plannodes.h:672
#define CP_EXACT_TLIST
Definition: createplan.c:69
Definition: pg_list.h:50

◆ create_foreignscan_plan()

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

Definition at line 4003 of file createplan.c.

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, RelOptInfo::relid, RangeTblEntry::relid, RelOptInfo::relids, RELOPT_UPPER_REL, RelOptInfo::reloptkind, RelOptInfo::reltarget, replace_nestloop_params(), RTE_RELATION, RelOptInfo::rtekind, RangeTblEntry::rtekind, ForeignScan::scan, RelOptInfo::serverid, and RelOptInfo::useridiscurrent.

Referenced by create_scan_plan().

4005 {
4006  ForeignScan *scan_plan;
4007  RelOptInfo *rel = best_path->path.parent;
4008  Index scan_relid = rel->relid;
4009  Oid rel_oid = InvalidOid;
4010  Plan *outer_plan = NULL;
4011 
4012  Assert(rel->fdwroutine != NULL);
4013 
4014  /* transform the child path if any */
4015  if (best_path->fdw_outerpath)
4016  outer_plan = create_plan_recurse(root, best_path->fdw_outerpath,
4017  CP_EXACT_TLIST);
4018 
4019  /*
4020  * If we're scanning a base relation, fetch its OID. (Irrelevant if
4021  * scanning a join relation.)
4022  */
4023  if (scan_relid > 0)
4024  {
4025  RangeTblEntry *rte;
4026 
4027  Assert(rel->rtekind == RTE_RELATION);
4028  rte = planner_rt_fetch(scan_relid, root);
4029  Assert(rte->rtekind == RTE_RELATION);
4030  rel_oid = rte->relid;
4031  }
4032 
4033  /*
4034  * Sort clauses into best execution order. We do this first since the FDW
4035  * might have more info than we do and wish to adjust the ordering.
4036  */
4037  scan_clauses = order_qual_clauses(root, scan_clauses);
4038 
4039  /*
4040  * Let the FDW perform its processing on the restriction clauses and
4041  * generate the plan node. Note that the FDW might remove restriction
4042  * clauses that it intends to execute remotely, or even add more (if it
4043  * has selected some join clauses for remote use but also wants them
4044  * rechecked locally).
4045  */
4046  scan_plan = rel->fdwroutine->GetForeignPlan(root, rel, rel_oid,
4047  best_path,
4048  tlist, scan_clauses,
4049  outer_plan);
4050 
4051  /* Copy cost data from Path to Plan; no need to make FDW do this */
4052  copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
4053 
4054  /* Copy foreign server OID; likewise, no need to make FDW do this */
4055  scan_plan->fs_server = rel->serverid;
4056 
4057  /*
4058  * Likewise, copy the relids that are represented by this foreign scan. An
4059  * upper rel doesn't have relids set, but it covers all the base relations
4060  * participating in the underlying scan, so use root's all_baserels.
4061  */
4062  if (rel->reloptkind == RELOPT_UPPER_REL)
4063  scan_plan->fs_relids = root->all_baserels;
4064  else
4065  scan_plan->fs_relids = best_path->path.parent->relids;
4066 
4067  /*
4068  * If this is a foreign join, and to make it valid to push down we had to
4069  * assume that the current user is the same as some user explicitly named
4070  * in the query, mark the finished plan as depending on the current user.
4071  */
4072  if (rel->useridiscurrent)
4073  root->glob->dependsOnRole = true;
4074 
4075  /*
4076  * Replace any outer-relation variables with nestloop params in the qual,
4077  * fdw_exprs and fdw_recheck_quals expressions. We do this last so that
4078  * the FDW doesn't have to be involved. (Note that parts of fdw_exprs or
4079  * fdw_recheck_quals could have come from join clauses, so doing this
4080  * beforehand on the scan_clauses wouldn't work.) We assume
4081  * fdw_scan_tlist contains no such variables.
4082  */
4083  if (best_path->path.param_info)
4084  {
4085  scan_plan->scan.plan.qual = (List *)
4086  replace_nestloop_params(root, (Node *) scan_plan->scan.plan.qual);
4087  scan_plan->fdw_exprs = (List *)
4088  replace_nestloop_params(root, (Node *) scan_plan->fdw_exprs);
4089  scan_plan->fdw_recheck_quals = (List *)
4091  (Node *) scan_plan->fdw_recheck_quals);
4092  }
4093 
4094  /*
4095  * If rel is a base relation, detect whether any system columns are
4096  * requested from the rel. (If rel is a join relation, rel->relid will be
4097  * 0, but there can be no Var with relid 0 in the rel's targetlist or the
4098  * restriction clauses, so we skip this in that case. Note that any such
4099  * columns in base relations that were joined are assumed to be contained
4100  * in fdw_scan_tlist.) This is a bit of a kluge and might go away
4101  * someday, so we intentionally leave it out of the API presented to FDWs.
4102  */
4103  scan_plan->fsSystemCol = false;
4104  if (scan_relid > 0)
4105  {
4106  Bitmapset *attrs_used = NULL;
4107  ListCell *lc;
4108  int i;
4109 
4110  /*
4111  * First, examine all the attributes needed for joins or final output.
4112  * Note: we must look at rel's targetlist, not the attr_needed data,
4113  * because attr_needed isn't computed for inheritance child rels.
4114  */
4115  pull_varattnos((Node *) rel->reltarget->exprs, scan_relid, &attrs_used);
4116 
4117  /* Add all the attributes used by restriction clauses. */
4118  foreach(lc, rel->baserestrictinfo)
4119  {
4120  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
4121 
4122  pull_varattnos((Node *) rinfo->clause, scan_relid, &attrs_used);
4123  }
4124 
4125  /* Now, are any system columns requested from rel? */
4126  for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
4127  {
4129  {
4130  scan_plan->fsSystemCol = true;
4131  break;
4132  }
4133  }
4134 
4135  bms_free(attrs_used);
4136  }
4137 
4138  return scan_plan;
4139 }
GetForeignPlan_function GetForeignPlan
Definition: fdwapi.h:211
List * qual
Definition: plannodes.h:142
Plan plan
Definition: plannodes.h:343
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4790
Path * fdw_outerpath
Definition: pathnodes.h:1399
RelOptKind reloptkind
Definition: pathnodes.h:678
Oid fs_server
Definition: plannodes.h:639
List * baserestrictinfo
Definition: pathnodes.h:745
List * fdw_exprs
Definition: plannodes.h:640
ParamPathInfo * param_info
Definition: pathnodes.h:1185
Definition: nodes.h:538
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5175
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
bool useridiscurrent
Definition: pathnodes.h:734
unsigned int Oid
Definition: postgres_ext.h:31
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:281
bool dependsOnRole
Definition: pathnodes.h:124
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:383
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
Relids all_baserels
Definition: pathnodes.h:210
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:388
RelOptInfo * parent
Definition: pathnodes.h:1182
PlannerGlobal * glob
Definition: pathnodes.h:164
struct FdwRoutine * fdwroutine
Definition: pathnodes.h:736
Relids relids
Definition: pathnodes.h:681
List * fdw_recheck_quals
Definition: plannodes.h:643
Index relid
Definition: pathnodes.h:709
Expr * clause
Definition: pathnodes.h:2056
Oid serverid
Definition: pathnodes.h:732
List * exprs
Definition: pathnodes.h:1110
unsigned int Index
Definition: c.h:549
RTEKind rtekind
Definition: pathnodes.h:711
#define InvalidOid
Definition: postgres_ext.h:36
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
RTEKind rtekind
Definition: parsenodes.h:1007
int i
#define CP_EXACT_TLIST
Definition: createplan.c:69
Definition: pg_list.h:50
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
struct PathTarget * reltarget
Definition: pathnodes.h:692
bool fsSystemCol
Definition: plannodes.h:645
Bitmapset * fs_relids
Definition: plannodes.h:644

◆ create_functionscan_plan()

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

Definition at line 3643 of file createplan.c.

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().

3645 {
3646  FunctionScan *scan_plan;
3647  Index scan_relid = best_path->parent->relid;
3648  RangeTblEntry *rte;
3649  List *functions;
3650 
3651  /* it should be a function base rel... */
3652  Assert(scan_relid > 0);
3653  rte = planner_rt_fetch(scan_relid, root);
3654  Assert(rte->rtekind == RTE_FUNCTION);
3655  functions = rte->functions;
3656 
3657  /* Sort clauses into best execution order */
3658  scan_clauses = order_qual_clauses(root, scan_clauses);
3659 
3660  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3661  scan_clauses = extract_actual_clauses(scan_clauses, false);
3662 
3663  /* Replace any outer-relation variables with nestloop params */
3664  if (best_path->param_info)
3665  {
3666  scan_clauses = (List *)
3667  replace_nestloop_params(root, (Node *) scan_clauses);
3668  /* The function expressions could contain nestloop params, too */
3669  functions = (List *) replace_nestloop_params(root, (Node *) functions);
3670  }
3671 
3672  scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
3673  functions, rte->funcordinality);
3674 
3675  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3676 
3677  return scan_plan;
3678 }
Plan plan
Definition: plannodes.h:343
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4790
ParamPathInfo * param_info
Definition: pathnodes.h:1185
Definition: nodes.h:538
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5175
bool funcordinality
Definition: parsenodes.h:1102
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:388
RelOptInfo * parent
Definition: pathnodes.h:1182
Index relid
Definition: pathnodes.h:709
unsigned int Index
Definition: c.h:549
static FunctionScan * make_functionscan(List *qptlist, List *qpqual, Index scanrelid, List *functions, bool funcordinality)
Definition: createplan.c:5560
#define Assert(condition)
Definition: c.h:804
List * functions
Definition: parsenodes.h:1101
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:443
static const struct fns functions
Definition: regcomp.c:313
RTEKind rtekind
Definition: parsenodes.h:1007
Definition: pg_list.h:50

◆ create_gather_merge_plan()

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

Definition at line 1872 of file createplan.c.

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, GatherMerge::num_workers, GatherMergePath::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().

1873 {
1874  GatherMerge *gm_plan;
1875  Plan *subplan;
1876  List *pathkeys = best_path->path.pathkeys;
1877  List *tlist = build_path_tlist(root, &best_path->path);
1878 
1879  /* As with Gather, project away columns in the workers. */
1880  subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1881 
1882  /* Create a shell for a GatherMerge plan. */
1883  gm_plan = makeNode(GatherMerge);
1884  gm_plan->plan.targetlist = tlist;
1885  gm_plan->num_workers = best_path->num_workers;
1886  copy_generic_path_info(&gm_plan->plan, &best_path->path);
1887 
1888  /* Assign the rescan Param. */
1889  gm_plan->rescan_param = assign_special_exec_param(root);
1890 
1891  /* Gather Merge is pointless with no pathkeys; use Gather instead. */
1892  Assert(pathkeys != NIL);
1893 
1894  /* Compute sort column info, and adjust subplan's tlist as needed */
1895  subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1896  best_path->subpath->parent->relids,
1897  gm_plan->sortColIdx,
1898  false,
1899  &gm_plan->numCols,
1900  &gm_plan->sortColIdx,
1901  &gm_plan->sortOperators,
1902  &gm_plan->collations,
1903  &gm_plan->nullsFirst);
1904 
1905 
1906  /*
1907  * All gather merge paths should have already guaranteed the necessary
1908  * sort order either by adding an explicit sort node or by using presorted
1909  * input. We can't simply add a sort here on additional pathkeys, because
1910  * we can't guarantee the sort would be safe. For example, expressions may
1911  * be volatile or otherwise parallel unsafe.
1912  */
1913  if (!pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys))
1914  elog(ERROR, "gather merge input not sufficiently sorted");
1915 
1916  /* Now insert the subplan under GatherMerge. */
1917  gm_plan->plan.lefttree = subplan;
1918 
1919  /* use parallel mode for parallel plans. */
1920  root->glob->parallelModeNeeded = true;
1921 
1922  return gm_plan;
1923 }
#define NIL
Definition: pg_list.h:65
Oid * collations
Definition: plannodes.h:952
AttrNumber * sortColIdx
Definition: plannodes.h:950
bool * nullsFirst
Definition: plannodes.h:953
int assign_special_exec_param(PlannerInfo *root)
Definition: paramassign.c:584
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:383
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
bool parallelModeNeeded
Definition: pathnodes.h:128
#define ERROR
Definition: elog.h:46
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:801
RelOptInfo * parent
Definition: pathnodes.h:1182
PlannerGlobal * glob
Definition: pathnodes.h:164
Relids relids
Definition: pathnodes.h:681
Oid * sortOperators
Definition: plannodes.h:951
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:324
List * pathkeys
Definition: pathnodes.h:1196
#define makeNode(_type_)
Definition: nodes.h:586
#define Assert(condition)
Definition: c.h:804
struct Plan * lefttree
Definition: plannodes.h:143
List * targetlist
Definition: plannodes.h:141
int num_workers
Definition: plannodes.h:946
#define elog(elevel,...)
Definition: elog.h:232
int rescan_param
Definition: plannodes.h:947
#define CP_EXACT_TLIST
Definition: createplan.c:69
Definition: pg_list.h:50
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:6019

◆ create_gather_plan()

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

Definition at line 1834 of file createplan.c.

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().

1835 {
1836  Gather *gather_plan;
1837  Plan *subplan;
1838  List *tlist;
1839 
1840  /*
1841  * Push projection down to the child node. That way, the projection work
1842  * is parallelized, and there can be no system columns in the result (they
1843  * can't travel through a tuple queue because it uses MinimalTuple
1844  * representation).
1845  */
1846  subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1847 
1848  tlist = build_path_tlist(root, &best_path->path);
1849 
1850  gather_plan = make_gather(tlist,
1851  NIL,
1852  best_path->num_workers,
1854  best_path->single_copy,
1855  subplan);
1856 
1857  copy_generic_path_info(&gather_plan->plan, &best_path->path);
1858 
1859  /* use parallel mode for parallel plans. */
1860  root->glob->parallelModeNeeded = true;
1861 
1862  return gather_plan;
1863 }
#define NIL
Definition: pg_list.h:65
bool single_copy
Definition: pathnodes.h:1561
int assign_special_exec_param(PlannerInfo *root)
Definition: paramassign.c:584
int num_workers
Definition: pathnodes.h:1562
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:383
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
bool parallelModeNeeded
Definition: pathnodes.h:128
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:801
Plan plan
Definition: plannodes.h:930
PlannerGlobal * glob
Definition: pathnodes.h:164
Path * subpath
Definition: pathnodes.h:1560
static Gather * make_gather(List *qptlist, List *qpqual, int nworkers, int rescan_param, bool single_copy, Plan *subplan)
Definition: createplan.c:6699
#define CP_EXACT_TLIST
Definition: createplan.c:69
Definition: pg_list.h:50

◆ create_gating_plan()

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

Definition at line 982 of file createplan.c.

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

Referenced by create_join_plan(), and create_scan_plan().

984 {
985  Plan *gplan;
986  Plan *splan;
987 
988  Assert(gating_quals);
989 
990  /*
991  * We might have a trivial Result plan already. Stacking one Result atop
992  * another is silly, so if that applies, just discard the input plan.
993  * (We're assuming its targetlist is uninteresting; it should be either
994  * the same as the result of build_path_tlist, or a simplified version.)
995  */
996  splan = plan;
997  if (IsA(plan, Result))
998  {
999  Result *rplan = (Result *) plan;
1000 
1001  if (rplan->plan.lefttree == NULL &&
1002  rplan->resconstantqual == NULL)
1003  splan = NULL;
1004  }
1005 
1006  /*
1007  * Since we need a Result node anyway, always return the path's requested
1008  * tlist; that's never a wrong choice, even if the parent node didn't ask
1009  * for CP_EXACT_TLIST.
1010  */
1011  gplan = (Plan *) make_result(build_path_tlist(root, path),
1012  (Node *) gating_quals,
1013  splan);
1014 
1015  /*
1016  * Notice that we don't change cost or size estimates when doing gating.
1017  * The costs of qual eval were already included in the subplan's cost.
1018  * Leaving the size alone amounts to assuming that the gating qual will
1019  * succeed, which is the conservative estimate for planning upper queries.
1020  * We certainly don't want to assume the output size is zero (unless the
1021  * gating qual is actually constant FALSE, and that case is dealt with in
1022  * clausesel.c). Interpolating between the two cases is silly, because it
1023  * doesn't reflect what will really happen at runtime, and besides which
1024  * in most cases we have only a very bad idea of the probability of the
1025  * gating qual being true.
1026  */
1027  copy_plan_costsize(gplan, plan);
1028 
1029  /* Gating quals could be unsafe, so better use the Path's safety flag */
1030  gplan->parallel_safe = path->parallel_safe;
1031 
1032  return gplan;
1033 }
Plan plan
Definition: plannodes.h:187
#define IsA(nodeptr, _type_)
Definition: nodes.h:589
Definition: nodes.h:538
Node * resconstantqual
Definition: plannodes.h:188
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:801
static void copy_plan_costsize(Plan *dest, Plan *src)
Definition: createplan.c:5284
static SPIPlanPtr splan
Definition: regress.c:263
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6833
#define Assert(condition)
Definition: c.h:804
bool parallel_safe
Definition: pathnodes.h:1188
struct Plan * lefttree
Definition: plannodes.h:143
bool parallel_safe
Definition: plannodes.h:130

◆ create_group_plan()

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

Definition at line 2156 of file createplan.c.

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().

2157 {
2158  Group *plan;
2159  Plan *subplan;
2160  List *tlist;
2161  List *quals;
2162 
2163  /*
2164  * Group can project, so no need to be terribly picky about child tlist,
2165  * but we do need grouping columns to be available
2166  */
2167  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2168 
2169  tlist = build_path_tlist(root, &best_path->path);
2170 
2171  quals = order_qual_clauses(root, best_path->qual);
2172 
2173  plan = make_group(tlist,
2174  quals,
2175  list_length(best_path->groupClause),
2177  subplan->targetlist),
2178  extract_grouping_ops(best_path->groupClause),
2180  subplan->targetlist),
2181  subplan);
2182 
2183  copy_generic_path_info(&plan->plan, (Path *) best_path);
2184 
2185  return plan;
2186 }
List * qual
Definition: pathnodes.h:1744
Oid * extract_grouping_collations(List *groupClause, List *tlist)
Definition: tlist.c:478
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5175
Oid * extract_grouping_ops(List *groupClause)
Definition: tlist.c:452
AttrNumber * extract_grouping_cols(List *groupClause, List *tlist)
Definition: tlist.c:503
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:383
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
static Group * make_group(List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations, Plan *lefttree)
Definition: createplan.c:6514
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:801
Path path
Definition: pathnodes.h:1741
List * groupClause
Definition: pathnodes.h:1743
#define CP_LABEL_TLIST
Definition: createplan.c:71
Path * subpath
Definition: pathnodes.h:1742
Plan plan
Definition: plannodes.h:839
static int list_length(const List *l)
Definition: pg_list.h:149
List * targetlist
Definition: plannodes.h:141
Definition: pg_list.h:50

◆ create_group_result_plan()

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

Definition at line 1506 of file createplan.c.

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().

1507 {
1508  Result *plan;
1509  List *tlist;
1510  List *quals;
1511 
1512  tlist = build_path_tlist(root, &best_path->path);
1513 
1514  /* best_path->quals is just bare clauses */
1515  quals = order_qual_clauses(root, best_path->quals);
1516 
1517  plan = make_result(tlist, (Node *) quals, NULL);
1518 
1519  copy_generic_path_info(&plan->plan, (Path *) best_path);
1520 
1521  return plan;
1522 }
Plan plan
Definition: plannodes.h:187
Definition: nodes.h:538
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5175
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:801
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6833
Definition: pg_list.h:50

◆ create_groupingsets_plan()

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

Definition at line 2307 of file createplan.c.

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().

2308 {
2309  Agg *plan;
2310  Plan *subplan;
2311  List *rollups = best_path->rollups;
2312  AttrNumber *grouping_map;
2313  int maxref;
2314  List *chain;
2315  ListCell *lc;
2316 
2317  /* Shouldn't get here without grouping sets */
2318  Assert(root->parse->groupingSets);
2319  Assert(rollups != NIL);
2320 
2321  /*
2322  * Agg can project, so no need to be terribly picky about child tlist, but
2323  * we do need grouping columns to be available
2324  */
2325  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2326 
2327  /*
2328  * Compute the mapping from tleSortGroupRef to column index in the child's
2329  * tlist. First, identify max SortGroupRef in groupClause, for array
2330  * sizing.
2331  */
2332  maxref = 0;
2333  foreach(lc, root->parse->groupClause)
2334  {
2335  SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
2336 
2337  if (gc->tleSortGroupRef > maxref)
2338  maxref = gc->tleSortGroupRef;
2339  }
2340 
2341  grouping_map = (AttrNumber *) palloc0((maxref + 1) * sizeof(AttrNumber));
2342 
2343  /* Now look up the column numbers in the child's tlist */
2344  foreach(lc, root->parse->groupClause)
2345  {
2346  SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
2347  TargetEntry *tle = get_sortgroupclause_tle(gc, subplan->targetlist);
2348 
2349  grouping_map[gc->tleSortGroupRef] = tle->resno;
2350  }
2351 
2352  /*
2353  * During setrefs.c, we'll need the grouping_map to fix up the cols lists
2354  * in GroupingFunc nodes. Save it for setrefs.c to use.
2355  */
2356  Assert(root->grouping_map == NULL);
2357  root->grouping_map = grouping_map;
2358 
2359  /*
2360  * Generate the side nodes that describe the other sort and group
2361  * operations besides the top one. Note that we don't worry about putting
2362  * accurate cost estimates in the side nodes; only the topmost Agg node's
2363  * costs will be shown by EXPLAIN.
2364  */
2365  chain = NIL;
2366  if (list_length(rollups) > 1)
2367  {
2368  bool is_first_sort = ((RollupData *) linitial(rollups))->is_hashed;
2369 
2370  for_each_from(lc, rollups, 1)
2371  {
2372  RollupData *rollup = lfirst(lc);
2373  AttrNumber *new_grpColIdx;
2374  Plan *sort_plan = NULL;
2375  Plan *agg_plan;
2376  AggStrategy strat;
2377 
2378  new_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2379 
2380  if (!rollup->is_hashed && !is_first_sort)
2381  {
2382  sort_plan = (Plan *)
2384  new_grpColIdx,
2385  subplan);
2386  }
2387 
2388  if (!rollup->is_hashed)
2389  is_first_sort = false;
2390 
2391  if (rollup->is_hashed)
2392  strat = AGG_HASHED;
2393  else if (list_length(linitial(rollup->gsets)) == 0)
2394  strat = AGG_PLAIN;
2395  else
2396  strat = AGG_SORTED;
2397 
2398  agg_plan = (Plan *) make_agg(NIL,
2399  NIL,
2400  strat,
2402  list_length((List *) linitial(rollup->gsets)),
2403  new_grpColIdx,
2406  rollup->gsets,
2407  NIL,
2408  rollup->numGroups,
2409  best_path->transitionSpace,
2410  sort_plan);
2411 
2412  /*
2413  * Remove stuff we don't need to avoid bloating debug output.
2414  */
2415  if (sort_plan)
2416  {
2417  sort_plan->targetlist = NIL;
2418  sort_plan->lefttree = NULL;
2419  }
2420 
2421  chain = lappend(chain, agg_plan);
2422  }
2423  }
2424 
2425  /*
2426  * Now make the real Agg node
2427  */
2428  {
2429  RollupData *rollup = linitial(rollups);
2430  AttrNumber *top_grpColIdx;
2431  int numGroupCols;
2432 
2433  top_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2434 
2435  numGroupCols = list_length((List *) linitial(rollup->gsets));
2436 
2437  plan = make_agg(build_path_tlist(root, &best_path->path),
2438  best_path->qual,
2439  best_path->aggstrategy,
2441  numGroupCols,
2442  top_grpColIdx,
2445  rollup->gsets,
2446  chain,
2447  rollup->numGroups,
2448  best_path->transitionSpace,
2449  subplan);
2450 
2451  /* Copy cost data from Path to Plan */
2452  copy_generic_path_info(&plan->plan, &best_path->path);
2453  }
2454 
2455  return (Plan *) plan;
2456 }
#define NIL
Definition: pg_list.h:65
Query * parse
Definition: pathnodes.h:162
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:356
List * groupClause
Definition: pathnodes.h:1793
Oid * extract_grouping_collations(List *groupClause, List *tlist)
Definition: tlist.c:478
Cardinality numGroups
Definition: pathnodes.h:1796
static AttrNumber * remap_groupColIdx(PlannerInfo *root, List *groupClause)
Definition: createplan.c:2269
Index tleSortGroupRef
Definition: parsenodes.h:1295
bool is_hashed
Definition: pathnodes.h:1798
List * groupingSets
Definition: parsenodes.h:161
Oid * extract_grouping_ops(List *groupClause)
Definition: tlist.c:452
AttrNumber * grouping_map
Definition: pathnodes.h:332
uint64 transitionSpace
Definition: pathnodes.h:1812
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:383
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
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:6441
#define linitial(l)
Definition: pg_list.h:174
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:801
AttrNumber resno
Definition: primnodes.h:1456
static Sort * make_sort_from_groupcols(List *groupcls, AttrNumber *grpColIdx, Plan *lefttree)
Definition: createplan.c:6322
AggStrategy aggstrategy
Definition: pathnodes.h:1809
List * lappend(List *list, void *datum)
Definition: list.c:336
Plan plan
Definition: plannodes.h:862
void * palloc0(Size size)
Definition: mcxt.c:1093
#define CP_LABEL_TLIST
Definition: createplan.c:71
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
#define for_each_from(cell, lst, N)
Definition: pg_list.h:393
struct Plan * lefttree
Definition: plannodes.h:143
List * targetlist
Definition: plannodes.h:141
AggStrategy
Definition: nodes.h:768
List * groupClause
Definition: parsenodes.h:158
Definition: plannodes.h:860
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
List * gsets
Definition: pathnodes.h:1794

◆ create_hashjoin_plan()

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

Definition at line 4601 of file createplan.c.

References OpExpr::args, Assert, build_path_tlist(), copy_generic_path_info(), copy_plan_costsize(), CP_SMALL_TLIST, create_plan_recurse(), extract_actual_clauses(), extract_actual_join_clauses(), get_actual_clauses(), get_switched_clauses(), RangeTblEntry::inh, HashPath::inner_rows_total, JoinPath::inner_unique, JoinPath::innerjoinpath, OpExpr::inputcollid, InvalidAttrNumber, InvalidOid, is_opclause(), IS_OUTER_JOIN, IsA, HashJoin::join, JoinPath::joinrestrictinfo, JoinPath::jointype, HashPath::jpath, lappend(), lappend_oid(), lfirst_node, linitial, list_difference(), list_length(), lsecond, make_hash(), make_hashjoin(), NIL, HashPath::num_batches, OpExpr::opno, order_qual_clauses(), JoinPath::outerjoinpath, Plan::parallel_aware, Path::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, RangeQueryClause::var, Var::varattno, and Var::varno.

Referenced by create_join_plan().

4603 {
4604  HashJoin *join_plan;
4605  Hash *hash_plan;
4606  Plan *outer_plan;
4607  Plan *inner_plan;
4608  List *tlist = build_path_tlist(root, &best_path->jpath.path);
4609  List *joinclauses;
4610  List *otherclauses;
4611  List *hashclauses;
4612  List *hashoperators = NIL;
4613  List *hashcollations = NIL;
4614  List *inner_hashkeys = NIL;
4615  List *outer_hashkeys = NIL;
4616  Oid skewTable = InvalidOid;
4617  AttrNumber skewColumn = InvalidAttrNumber;
4618  bool skewInherit = false;
4619  ListCell *lc;
4620 
4621  /*
4622  * HashJoin can project, so we don't have to demand exact tlists from the
4623  * inputs. However, it's best to request a small tlist from the inner
4624  * side, so that we aren't storing more data than necessary. Likewise, if
4625  * we anticipate batching, request a small tlist from the outer side so
4626  * that we don't put extra data in the outer batch files.
4627  */
4628  outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4629  (best_path->num_batches > 1) ? CP_SMALL_TLIST : 0);
4630 
4631  inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4632  CP_SMALL_TLIST);
4633 
4634  /* Sort join qual clauses into best execution order */
4635  joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4636  /* There's no point in sorting the hash clauses ... */
4637 
4638  /* Get the join qual clauses (in plain expression form) */
4639  /* Any pseudoconstant clauses are ignored here */
4640  if (IS_OUTER_JOIN(best_path->jpath.jointype))
4641  {
4642  extract_actual_join_clauses(joinclauses,
4643  best_path->jpath.path.parent->relids,
4644  &joinclauses, &otherclauses);
4645  }
4646  else
4647  {
4648  /* We can treat all clauses alike for an inner join */
4649  joinclauses = extract_actual_clauses(joinclauses, false);
4650  otherclauses = NIL;
4651  }
4652 
4653  /*
4654  * Remove the hashclauses from the list of join qual clauses, leaving the
4655  * list of quals that must be checked as qpquals.
4656  */
4657  hashclauses = get_actual_clauses(best_path->path_hashclauses);
4658  joinclauses = list_difference(joinclauses, hashclauses);
4659 
4660  /*
4661  * Replace any outer-relation variables with nestloop params. There
4662  * should not be any in the hashclauses.
4663  */
4664  if (best_path->jpath.path.param_info)
4665  {
4666  joinclauses = (List *)
4667  replace_nestloop_params(root, (Node *) joinclauses);
4668  otherclauses = (List *)
4669  replace_nestloop_params(root, (Node *) otherclauses);
4670  }
4671 
4672  /*
4673  * Rearrange hashclauses, if needed, so that the outer variable is always
4674  * on the left.
4675  */
4676  hashclauses = get_switched_clauses(best_path->path_hashclauses,
4677  best_path->jpath.outerjoinpath->parent->relids);
4678 
4679  /*
4680  * If there is a single join clause and we can identify the outer variable
4681  * as a simple column reference, supply its identity for possible use in
4682  * skew optimization. (Note: in principle we could do skew optimization
4683  * with multiple join clauses, but we'd have to be able to determine the
4684  * most common combinations of outer values, which we don't currently have
4685  * enough stats for.)
4686  */
4687  if (list_length(hashclauses) == 1)
4688  {
4689  OpExpr *clause = (OpExpr *) linitial(hashclauses);
4690  Node *node;
4691 
4692  Assert(is_opclause(clause));
4693  node = (Node *) linitial(clause->args);
4694  if (IsA(node, RelabelType))
4695  node = (Node *) ((RelabelType *) node)->arg;
4696  if (IsA(node, Var))
4697  {
4698  Var *var = (Var *) node;
4699  RangeTblEntry *rte;
4700 
4701  rte = root->simple_rte_array[var->varno];
4702  if (rte->rtekind == RTE_RELATION)
4703  {
4704  skewTable = rte->relid;
4705  skewColumn = var->varattno;
4706  skewInherit = rte->inh;
4707  }
4708  }
4709  }
4710 
4711  /*
4712  * Collect hash related information. The hashed expressions are
4713  * deconstructed into outer/inner expressions, so they can be computed
4714  * separately (inner expressions are used to build the hashtable via Hash,
4715  * outer expressions to perform lookups of tuples from HashJoin's outer
4716  * plan in the hashtable). Also collect operator information necessary to
4717  * build the hashtable.
4718  */
4719  foreach(lc, hashclauses)
4720  {
4721  OpExpr *hclause = lfirst_node(OpExpr, lc);
4722 
4723  hashoperators = lappend_oid(hashoperators, hclause->opno);
4724  hashcollations = lappend_oid(hashcollations, hclause->inputcollid);
4725  outer_hashkeys = lappend(outer_hashkeys, linitial(hclause->args));
4726  inner_hashkeys = lappend(inner_hashkeys, lsecond(hclause->args));
4727  }
4728 
4729  /*
4730  * Build the hash node and hash join node.
4731  */
4732  hash_plan = make_hash(inner_plan,
4733  inner_hashkeys,
4734  skewTable,
4735  skewColumn,
4736  skewInherit);
4737 
4738  /*
4739  * Set Hash node's startup & total costs equal to total cost of input
4740  * plan; this only affects EXPLAIN display not decisions.
4741  */
4742  copy_plan_costsize(&hash_plan->plan, inner_plan);
4743  hash_plan->plan.startup_cost = hash_plan->plan.total_cost;
4744 
4745  /*
4746  * If parallel-aware, the executor will also need an estimate of the total
4747  * number of rows expected from all participants so that it can size the
4748  * shared hash table.
4749  */
4750  if (best_path->jpath.path.parallel_aware)
4751  {
4752  hash_plan->plan.parallel_aware = true;
4753  hash_plan->rows_total = best_path->inner_rows_total;
4754  }
4755 
4756  join_plan = make_hashjoin(tlist,
4757  joinclauses,
4758  otherclauses,
4759  hashclauses,
4760  hashoperators,
4761  hashcollations,
4762  outer_hashkeys,
4763  outer_plan,
4764  (Plan *) hash_plan,
4765  best_path->jpath.jointype,
4766  best_path->jpath.inner_unique);
4767 
4768  copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4769 
4770  return join_plan;
4771 }
int varno
Definition: primnodes.h:189
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:589
JoinPath jpath
Definition: pathnodes.h:1667
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4790
static List * get_switched_clauses(List *clauses, Relids outerrelids)
Definition: createplan.c:5098
int num_batches
Definition: pathnodes.h:1669
void extract_actual_join_clauses(List *restrictinfo_list, Relids joinrelids, List **joinquals, List **otherquals)
Definition: restrictinfo.c:470
Path * innerjoinpath
Definition: pathnodes.h:1591
static Hash * make_hash(Plan *lefttree, List *hashkeys, Oid skewTable, AttrNumber skewColumn, bool skewInherit)
Definition: createplan.c:5859
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:755
ParamPathInfo * param_info
Definition: pathnodes.h:1185
#define CP_SMALL_TLIST
Definition: createplan.c:70
Definition: nodes.h:538
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:420
AttrNumber varattno
Definition: primnodes.h:191
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5175
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:186
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
#define lsecond(l)
Definition: pg_list.h:179
Join join
Definition: plannodes.h:765
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:383
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
#define linitial(l)
Definition: pg_list.h:174
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:801
List * joinrestrictinfo
Definition: pathnodes.h:1593
RelOptInfo * parent
Definition: pathnodes.h:1182
#define lfirst_node(type, lc)
Definition: pg_list.h:172
Cost startup_cost
Definition: plannodes.h:117
static void copy_plan_costsize(Plan *dest, Plan *src)
Definition: createplan.c:5284
bool parallel_aware
Definition: plannodes.h:129
Relids relids
Definition: pathnodes.h:681
List * lappend(List *list, void *datum)
Definition: list.c:336
RangeTblEntry ** simple_rte_array
Definition: pathnodes.h:194
Cardinality rows_total
Definition: plannodes.h:979
Path * outerjoinpath
Definition: pathnodes.h:1590
Cardinality inner_rows_total
Definition: pathnodes.h:1670
#define InvalidOid
Definition: postgres_ext.h:36
Path path
Definition: pathnodes.h:1583
#define Assert(condition)
Definition: c.h:804
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:5828
static int list_length(const List *l)
Definition: pg_list.h:149
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:443
Oid inputcollid
Definition: primnodes.h:547
bool inner_unique
Definition: pathnodes.h:1587
List * list_difference(const List *list1, const List *list2)
Definition: list.c:1091
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:1007
Plan plan
Definition: plannodes.h:968
JoinType jointype
Definition: pathnodes.h:1585
Cost total_cost
Definition: plannodes.h:118
bool parallel_aware
Definition: pathnodes.h:1187
List * path_hashclauses
Definition: pathnodes.h:1668
Oid opno
Definition: primnodes.h:542
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:66
List * args
Definition: primnodes.h:548
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
Plan plan
Definition: plannodes.h:706

◆ create_incrementalsort_plan()

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

Definition at line 2129 of file createplan.c.

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().

2131 {
2132  IncrementalSort *plan;
2133  Plan *subplan;
2134 
2135  /* See comments in create_sort_plan() above */
2136  subplan = create_plan_recurse(root, best_path->spath.subpath,
2137  flags | CP_SMALL_TLIST);
2138  plan = make_incrementalsort_from_pathkeys(subplan,
2139  best_path->spath.path.pathkeys,
2140  IS_OTHER_REL(best_path->spath.subpath->parent) ?
2141  best_path->spath.path.parent->relids : NULL,
2142  best_path->nPresortedCols);
2143 
2144  copy_generic_path_info(&plan->sort.plan, (Path *) best_path);
2145 
2146  return plan;
2147 }
Path * subpath
Definition: pathnodes.h:1716
Plan plan
Definition: plannodes.h:813
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:669
#define CP_SMALL_TLIST
Definition: createplan.c:70
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:383
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
RelOptInfo * parent
Definition: pathnodes.h:1182
static IncrementalSort * make_incrementalsort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids, int nPresortedCols)
Definition: createplan.c:6239
Relids relids
Definition: pathnodes.h:681
List * pathkeys
Definition: pathnodes.h:1196
Path path
Definition: pathnodes.h:1715

◆ create_indexscan_plan()

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

Definition at line 2920 of file createplan.c.

References Assert, RestrictInfo::clause, contain_mutable_functions(), copy_generic_path_info(), elog, ERROR, exprType(), extract_actual_clauses(), fix_indexorderby_references(), fix_indexqual_references(), forboth, get_opfamily_member(), 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(), RTE_RELATION, and RelOptInfo::rtekind.

Referenced by create_bitmap_subplan(), and create_scan_plan().

2925 {
2926  Scan *scan_plan;
2927  List *indexclauses = best_path->indexclauses;
2928  List *indexorderbys = best_path->indexorderbys;
2929  Index baserelid = best_path->path.parent->relid;
2930  Oid indexoid = best_path->indexinfo->indexoid;
2931  List *qpqual;
2932  List *stripped_indexquals;
2933  List *fixed_indexquals;
2934  List *fixed_indexorderbys;
2935  List *indexorderbyops = NIL;
2936  ListCell *l;
2937 
2938  /* it should be a base rel... */
2939  Assert(baserelid > 0);
2940  Assert(best_path->path.parent->rtekind == RTE_RELATION);
2941 
2942  /*
2943  * Extract the index qual expressions (stripped of RestrictInfos) from the
2944  * IndexClauses list, and prepare a copy with index Vars substituted for
2945  * table Vars. (This step also does replace_nestloop_params on the
2946  * fixed_indexquals.)
2947  */
2948  fix_indexqual_references(root, best_path,
2949  &stripped_indexquals,
2950  &fixed_indexquals);
2951 
2952  /*
2953  * Likewise fix up index attr references in the ORDER BY expressions.
2954  */
2955  fixed_indexorderbys = fix_indexorderby_references(root, best_path);
2956 
2957  /*
2958  * The qpqual list must contain all restrictions not automatically handled
2959  * by the index, other than pseudoconstant clauses which will be handled
2960  * by a separate gating plan node. All the predicates in the indexquals
2961  * will be checked (either by the index itself, or by nodeIndexscan.c),
2962  * but if there are any "special" operators involved then they must be
2963  * included in qpqual. The upshot is that qpqual must contain
2964  * scan_clauses minus whatever appears in indexquals.
2965  *
2966  * is_redundant_with_indexclauses() detects cases where a scan clause is
2967  * present in the indexclauses list or is generated from the same
2968  * EquivalenceClass as some indexclause, and is therefore redundant with
2969  * it, though not equal. (The latter happens when indxpath.c prefers a
2970  * different derived equality than what generate_join_implied_equalities
2971  * picked for a parameterized scan's ppi_clauses.) Note that it will not
2972  * match to lossy index clauses, which is critical because we have to
2973  * include the original clause in qpqual in that case.
2974  *
2975  * In some situations (particularly with OR'd index conditions) we may
2976  * have scan_clauses that are not equal to, but are logically implied by,
2977  * the index quals; so we also try a predicate_implied_by() check to see
2978  * if we can discard quals that way. (predicate_implied_by assumes its
2979  * first input contains only immutable functions, so we have to check
2980  * that.)
2981  *
2982  * Note: if you change this bit of code you should also look at
2983  * extract_nonindex_conditions() in costsize.c.
2984  */
2985  qpqual = NIL;
2986  foreach(l, scan_clauses)
2987  {
2988  RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
2989 
2990  if (rinfo->pseudoconstant)
2991  continue; /* we may drop pseudoconstants here */
2992  if (is_redundant_with_indexclauses(rinfo, indexclauses))
2993  continue; /* dup or derived from same EquivalenceClass */
2994  if (!contain_mutable_functions((Node *) rinfo->clause) &&
2995  predicate_implied_by(list_make1(rinfo->clause), stripped_indexquals,
2996  false))
2997  continue; /* provably implied by indexquals */
2998  qpqual = lappend(qpqual, rinfo);
2999  }
3000 
3001  /* Sort clauses into best execution order */
3002  qpqual = order_qual_clauses(root, qpqual);
3003 
3004  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3005  qpqual = extract_actual_clauses(qpqual, false);
3006 
3007  /*
3008  * We have to replace any outer-relation variables with nestloop params in
3009  * the indexqualorig, qpqual, and indexorderbyorig expressions. A bit
3010  * annoying to have to do this separately from the processing in
3011  * fix_indexqual_references --- rethink this when generalizing the inner
3012  * indexscan support. But note we can't really do this earlier because
3013  * it'd break the comparisons to predicates above ... (or would it? Those
3014  * wouldn't have outer refs)
3015  */
3016  if (best_path->path.param_info)
3017  {
3018  stripped_indexquals = (List *)
3019  replace_nestloop_params(root, (Node *) stripped_indexquals);
3020  qpqual = (List *)
3021  replace_nestloop_params(root, (Node *) qpqual);
3022  indexorderbys = (List *)
3023  replace_nestloop_params(root, (Node *) indexorderbys);
3024  }
3025 
3026  /*
3027  * If there are ORDER BY expressions, look up the sort operators for their
3028  * result datatypes.
3029  */
3030  if (indexorderbys)
3031  {
3032  ListCell *pathkeyCell,
3033  *exprCell;
3034 
3035  /*
3036  * PathKey contains OID of the btree opfamily we're sorting by, but
3037  * that's not quite enough because we need the expression's datatype
3038  * to look up the sort operator in the operator family.
3039  */
3040  Assert(list_length(best_path->path.pathkeys) == list_length(indexorderbys));
3041  forboth(pathkeyCell, best_path->path.pathkeys, exprCell, indexorderbys)
3042  {
3043  PathKey *pathkey = (PathKey *) lfirst(pathkeyCell);
3044  Node *expr = (Node *) lfirst(exprCell);
3045  Oid exprtype = exprType(expr);
3046  Oid sortop;
3047 
3048  /* Get sort operator from opfamily */
3049  sortop = get_opfamily_member(pathkey->pk_opfamily,
3050  exprtype,
3051  exprtype,
3052  pathkey->pk_strategy);
3053  if (!OidIsValid(sortop))
3054  elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
3055  pathkey->pk_strategy, exprtype, exprtype, pathkey->pk_opfamily);
3056  indexorderbyops = lappend_oid(indexorderbyops, sortop);
3057  }
3058  }
3059 
3060  /* Finally ready to build the plan node */
3061  if (indexonly)
3062  scan_plan = (Scan *) make_indexonlyscan(tlist,
3063  qpqual,
3064  baserelid,
3065  indexoid,
3066  fixed_indexquals,
3067  fixed_indexorderbys,
3068  best_path->indexinfo->indextlist,
3069  best_path->indexscandir);
3070  else
3071  scan_plan = (Scan *) make_indexscan(tlist,
3072  qpqual,
3073  baserelid,
3074  indexoid,
3075  fixed_indexquals,
3076  stripped_indexquals,
3077  fixed_indexorderbys,
3078  indexorderbys,
3079  indexorderbyops,
3080  best_path->indexscandir);
3081 
3082  copy_generic_path_info(&scan_plan->plan, &best_path->path);
3083 
3084  return scan_plan;
3085 }
#define NIL
Definition: pg_list.h:65
Plan plan
Definition: plannodes.h:343
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4790
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
Path path
Definition: pathnodes.h:1244
IndexOptInfo * indexinfo
Definition: pathnodes.h:1245
List * indextlist
Definition: pathnodes.h:866
bool pseudoconstant
Definition: pathnodes.h:2064
ParamPathInfo * param_info
Definition: pathnodes.h:1185
Definition: nodes.h:538
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5175
List * indexclauses
Definition: pathnodes.h:1246
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
#define OidIsValid(objectId)
Definition: c.h:710
int pk_strategy
Definition: pathnodes.h:1068
#define list_make1(x1)
Definition: pg_list.h:206
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
#define ERROR
Definition: elog.h:46
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:5404
RelOptInfo * parent
Definition: pathnodes.h:1182
bool is_redundant_with_indexclauses(RestrictInfo *rinfo, List *indexclauses)
Definition: equivclass.c:3176
#define lfirst_node(type, lc)
Definition: pg_list.h:172
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:164
static List * fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path)
Definition: createplan.c:4923
Index relid
Definition: pathnodes.h:709
List * lappend(List *list, void *datum)
Definition: list.c:336
Expr * clause
Definition: pathnodes.h:2056
List * indexorderbys
Definition: pathnodes.h:1247
unsigned int Index
Definition: c.h:549
RTEKind rtekind
Definition: pathnodes.h:711
List * pathkeys
Definition: pathnodes.h:1196
static IndexOnlyScan * make_indexonlyscan(List *qptlist, List *qpqual, Index scanrelid, Oid indexid, List *indexqual, List *indexorderby, List *indextlist, ScanDirection indexscandir)
Definition: createplan.c:5435
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:149
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:443
Oid pk_opfamily
Definition: pathnodes.h:1067
ScanDirection indexscandir
Definition: pathnodes.h:1249
static void fix_indexqual_references(PlannerInfo *root, IndexPath *index_path, List **stripped_indexquals_p, List **fixed_indexquals_p)
Definition: createplan.c:4882
#define elog(elevel,...)
Definition: elog.h:232
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:363
bool predicate_implied_by(List *predicate_list, List *clause_list, bool weak)
Definition: predtest.c:151
Definition: pg_list.h:50

◆ create_join_plan()

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

Definition at line 1041 of file createplan.c.

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().

1042 {
1043  Plan *plan;
1044  List *gating_clauses;
1045 
1046  switch (best_path->path.pathtype)
1047  {
1048  case T_MergeJoin:
1049  plan = (Plan *) create_mergejoin_plan(root,
1050  (MergePath *) best_path);
1051  break;
1052  case T_HashJoin:
1053  plan = (Plan *) create_hashjoin_plan(root,
1054  (HashPath *) best_path);
1055  break;
1056  case T_NestLoop:
1057  plan = (Plan *) create_nestloop_plan(root,
1058  (NestPath *) best_path);
1059  break;
1060  default:
1061  elog(ERROR, "unrecognized node type: %d",
1062  (int) best_path->path.pathtype);
1063  plan = NULL; /* keep compiler quiet */
1064  break;
1065  }
1066 
1067  /*
1068  * If there are any pseudoconstant clauses attached to this node, insert a
1069  * gating Result node that evaluates the pseudoconstants as one-time
1070  * quals.
1071  */
1072  gating_clauses = get_gating_quals(root, best_path->joinrestrictinfo);
1073  if (gating_clauses)
1074  plan = create_gating_plan(root, (Path *) best_path, plan,
1075  gating_clauses);
1076 
1077 #ifdef NOT_USED
1078 
1079  /*
1080  * * Expensive function pullups may have pulled local predicates * into
1081  * this path node. Put them in the qpqual of the plan node. * JMH,
1082  * 6/15/92
1083  */
1084  if (get_loc_restrictinfo(best_path) != NIL)
1085  set_qpqual((Plan) plan,
1086  list_concat(get_qpqual((Plan) plan),
1087  get_actual_clauses(get_loc_restrictinfo(best_path))));
1088 #endif
1089 
1090  return plan;
1091 }
#define NIL
Definition: pg_list.h:65
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:420
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
static NestLoop * create_nestloop_plan(PlannerInfo *root, NestPath *best_path)
Definition: createplan.c:4218
NodeTag pathtype
Definition: pathnodes.h:1180
#define ERROR
Definition: elog.h:46
List * joinrestrictinfo
Definition: pathnodes.h:1593
Path path
Definition: pathnodes.h:1583
static HashJoin * create_hashjoin_plan(PlannerInfo *root, HashPath *best_path)
Definition: createplan.c:4601
static Plan * create_gating_plan(PlannerInfo *root, Path *path, Plan *plan, List *gating_quals)
Definition: createplan.c:982
#define elog(elevel,...)
Definition: elog.h:232
static MergeJoin * create_mergejoin_plan(PlannerInfo *root, MergePath *best_path)
Definition: createplan.c:4294
Definition: pg_list.h:50
static List * get_gating_quals(PlannerInfo *root, List *quals)
Definition: createplan.c:962

◆ create_limit_plan()

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

Definition at line 2770 of file createplan.c.

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, Query::sortClause, LimitPath::subpath, and Query::targetList.

Referenced by create_plan_recurse().

2771 {
2772  Limit *plan;
2773  Plan *subplan;
2774  int numUniqkeys = 0;
2775  AttrNumber *uniqColIdx = NULL;
2776  Oid *uniqOperators = NULL;
2777  Oid *uniqCollations = NULL;
2778 
2779  /* Limit doesn't project, so tlist requirements pass through */
2780  subplan = create_plan_recurse(root, best_path->subpath, flags);
2781 
2782  /* Extract information necessary for comparing rows for WITH TIES. */
2783  if (best_path->limitOption == LIMIT_OPTION_WITH_TIES)
2784  {
2785  Query *parse = root->parse;
2786  ListCell *l;
2787 
2788  numUniqkeys = list_length(parse->sortClause);
2789  uniqColIdx = (AttrNumber *) palloc(numUniqkeys * sizeof(AttrNumber));
2790  uniqOperators = (Oid *) palloc(numUniqkeys * sizeof(Oid));
2791  uniqCollations = (Oid *) palloc(numUniqkeys * sizeof(Oid));
2792 
2793  numUniqkeys = 0;
2794  foreach(l, parse->sortClause)
2795  {
2796  SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
2797  TargetEntry *tle = get_sortgroupclause_tle(sortcl, parse->targetList);
2798 
2799  uniqColIdx[numUniqkeys] = tle->resno;
2800  uniqOperators[numUniqkeys] = sortcl->eqop;
2801  uniqCollations[numUniqkeys] = exprCollation((Node *) tle->expr);
2802  numUniqkeys++;
2803  }
2804  }
2805 
2806  plan = make_limit(subplan,
2807  best_path->limitOffset,
2808  best_path->limitCount,
2809  best_path->limitOption,
2810  numUniqkeys, uniqColIdx, uniqOperators, uniqCollations);
2811 
2812  copy_generic_path_info(&plan->plan, (Path *) best_path);
2813 
2814  return plan;
2815 }
Query * parse
Definition: pathnodes.h:162
LimitOption limitOption
Definition: pathnodes.h:1908
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:356
Plan plan
Definition: plannodes.h:1026
Node * limitOffset
Definition: pathnodes.h:1906
List * sortClause
Definition: parsenodes.h:169
Definition: nodes.h:538
unsigned int Oid
Definition: postgres_ext.h:31
List * targetList
Definition: parsenodes.h:150
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:383
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
Path * subpath
Definition: pathnodes.h:1905
AttrNumber resno
Definition: primnodes.h:1456
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount, LimitOption limitOption, int uniqNumCols, AttrNumber *uniqColIdx, Oid *uniqOperators, Oid *uniqCollations)
Definition: createplan.c:6805
#define lfirst(lc)
Definition: pg_list.h:169
Expr * expr
Definition: primnodes.h:1455
static int list_length(const List *l)
Definition: pg_list.h:149
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
void * palloc(Size size)
Definition: mcxt.c:1062
Node * limitCount
Definition: pathnodes.h:1907
int16 AttrNumber
Definition: attnum.h:21
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:673

◆ create_lockrows_plan()

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

Definition at line 2708 of file createplan.c.

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

Referenced by create_plan_recurse().

2710 {
2711  LockRows *plan;
2712  Plan *subplan;
2713 
2714  /* LockRows doesn't project, so tlist requirements pass through */
2715  subplan = create_plan_recurse(root, best_path->subpath, flags);
2716 
2717  plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
2718 
2719  copy_generic_path_info(&plan->plan, (Path *) best_path);
2720 
2721  return plan;
2722 }
Plan plan
Definition: plannodes.h:1012
List * rowMarks
Definition: pathnodes.h:1870
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:383
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
Path * subpath
Definition: pathnodes.h:1869
static LockRows * make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
Definition: createplan.c:6784

◆ create_material_plan()

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

Definition at line 1557 of file createplan.c.

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

Referenced by create_plan_recurse().

1558 {
1559  Material *plan;
1560  Plan *subplan;
1561 
1562  /*
1563  * We don't want any excess columns in the materialized tuples, so request
1564  * a smaller tlist. Otherwise, since Material doesn't project, tlist
1565  * requirements pass through.
1566  */
1567  subplan = create_plan_recurse(root, best_path->subpath,
1568  flags | CP_SMALL_TLIST);
1569 
1570  plan = make_material(subplan);
1571 
1572  copy_generic_path_info(&plan->plan, (Path *) best_path);
1573 
1574  return plan;
1575 }
#define CP_SMALL_TLIST
Definition: createplan.c:70
Path * subpath
Definition: pathnodes.h:1502
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:383
static Material * make_material(Plan *lefttree)
Definition: createplan.c:6363
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
Plan plan
Definition: plannodes.h:783

◆ create_memoize_plan()

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

Definition at line 1585 of file createplan.c.

References Assert, 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, replace_nestloop_params(), MemoizePath::singlerow, and MemoizePath::subpath.

Referenced by create_plan_recurse().

1586 {
1587  Memoize *plan;
1588  Plan *subplan;
1589  Oid *operators;
1590  Oid *collations;
1591  List *param_exprs = NIL;
1592  ListCell *lc;
1593  ListCell *lc2;
1594  int nkeys;
1595  int i;
1596 
1597  subplan = create_plan_recurse(root, best_path->subpath,
1598  flags | CP_SMALL_TLIST);
1599 
1600  param_exprs = (List *) replace_nestloop_params(root, (Node *)
1601  best_path->param_exprs);
1602 
1603  nkeys = list_length(param_exprs);
1604  Assert(nkeys > 0);
1605  operators = palloc(nkeys * sizeof(Oid));
1606  collations = palloc(nkeys * sizeof(Oid));
1607 
1608  i = 0;
1609  forboth(lc, param_exprs, lc2, best_path->hash_operators)
1610  {
1611  Expr *param_expr = (Expr *) lfirst(lc);
1612  Oid opno = lfirst_oid(lc2);
1613 
1614  operators[i] = opno;
1615  collations[i] = exprCollation((Node *) param_expr);
1616  i++;
1617  }
1618 
1619  plan = make_memoize(subplan, operators, collations, param_exprs,
1620  best_path->singlerow, best_path->est_entries);
1621 
1622  copy_generic_path_info(&plan->plan, (Path *) best_path);
1623 
1624  return plan;
1625 }
static Memoize * make_memoize(Plan *lefttree, Oid *hashoperators, Oid *collations, List *param_exprs, bool singlerow, uint32 est_entries)
Definition: createplan.c:6419
#define NIL
Definition: pg_list.h:65
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4790
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
uint32 est_entries
Definition: pathnodes.h:1519
#define CP_SMALL_TLIST
Definition: createplan.c:70
Definition: nodes.h:538
bool singlerow
Definition: pathnodes.h:1516
unsigned int Oid
Definition: postgres_ext.h:31
Path * subpath
Definition: pathnodes.h:1513
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:383
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
List * hash_operators
Definition: pathnodes.h:1514
Plan plan
Definition: plannodes.h:792
List * param_exprs
Definition: pathnodes.h:1515
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
void * palloc(Size size)
Definition: mcxt.c:1062
int i
Definition: pg_list.h:50
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ create_merge_append_plan()

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

Definition at line 1348 of file createplan.c.

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, 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(), and Plan::targetlist.

Referenced by create_plan_recurse().

1350 {
1351  MergeAppend *node = makeNode(MergeAppend);
1352  Plan *plan = &node->plan;
1353  List *tlist = build_path_tlist(root, &best_path->path);
1354  int orig_tlist_length = list_length(tlist);
1355  bool tlist_was_changed;
1356  List *pathkeys = best_path->path.pathkeys;
1357  List *subplans = NIL;
1358  ListCell *subpaths;
1359  RelOptInfo *rel = best_path->path.parent;
1360  PartitionPruneInfo *partpruneinfo = NULL;
1361 
1362  /*
1363  * We don't have the actual creation of the MergeAppend node split out
1364  * into a separate make_xxx function. This is because we want to run
1365  * prepare_sort_from_pathkeys on it before we do so on the individual
1366  * child plans, to make cross-checking the sort info easier.
1367  */
1368  copy_generic_path_info(plan, (Path *) best_path);
1369  plan->targetlist = tlist;
1370  plan->qual = NIL;
1371  plan->lefttree = NULL;
1372  plan->righttree = NULL;
1373  node->apprelids = rel->relids;
1374 
1375  /*
1376  * Compute sort column info, and adjust MergeAppend's tlist as needed.
1377  * Because we pass adjust_tlist_in_place = true, we may ignore the
1378  * function result; it must be the same plan node. However, we then need
1379  * to detect whether any tlist entries were added.
1380  */
1381  (void) prepare_sort_from_pathkeys(plan, pathkeys,
1382  best_path->path.parent->relids,
1383  NULL,
1384  true,
1385  &node->numCols,
1386  &node->sortColIdx,
1387  &node->sortOperators,
1388  &node->collations,
1389  &node->nullsFirst);
1390  tlist_was_changed = (orig_tlist_length != list_length(plan->targetlist));
1391 
1392  /*
1393  * Now prepare the child plans. We must apply prepare_sort_from_pathkeys
1394  * even to subplans that don't need an explicit sort, to make sure they
1395  * are returning the same sort key columns the MergeAppend expects.
1396  */
1397  foreach(subpaths, best_path->subpaths)
1398  {
1399  Path *subpath = (Path *) lfirst(subpaths);
1400  Plan *subplan;
1401  int numsortkeys;
1402  AttrNumber *sortColIdx;
1403  Oid *sortOperators;
1404  Oid *collations;
1405  bool *nullsFirst;
1406 
1407  /* Build the child plan */
1408  /* Must insist that all children return the same tlist */
1409  subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
1410 
1411  /* Compute sort column info, and adjust subplan's tlist as needed */
1412  subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1413  subpath->parent->relids,
1414  node->sortColIdx,
1415  false,
1416  &numsortkeys,
1417  &sortColIdx,
1418  &sortOperators,
1419  &collations,
1420  &nullsFirst);
1421 
1422  /*
1423  * Check that we got the same sort key information. We just Assert
1424  * that the sortops match, since those depend only on the pathkeys;
1425  * but it seems like a good idea to check the sort column numbers
1426  * explicitly, to ensure the tlists really do match up.
1427  */
1428  Assert(numsortkeys == node->numCols);
1429  if (memcmp(sortColIdx, node->sortColIdx,
1430  numsortkeys * sizeof(AttrNumber)) != 0)
1431  elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
1432  Assert(memcmp(sortOperators, node->sortOperators,
1433  numsortkeys * sizeof(Oid)) == 0);
1434  Assert(memcmp(collations, node->collations,
1435  numsortkeys * sizeof(Oid)) == 0);
1436  Assert(memcmp(nullsFirst, node->nullsFirst,
1437  numsortkeys * sizeof(bool)) == 0);
1438 
1439  /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1440  if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
1441  {
1442  Sort *sort = make_sort(subplan, numsortkeys,
1443  sortColIdx, sortOperators,
1444  collations, nullsFirst);
1445 
1446  label_sort_with_costsize(root, sort, best_path->limit_tuples);
1447  subplan = (Plan *) sort;
1448  }
1449 
1450  subplans = lappend(subplans, subplan);
1451  }
1452 
1453  /*
1454  * If any quals exist, they may be useful to perform further partition
1455  * pruning during execution. Gather information needed by the executor to
1456  * do partition pruning.
1457  */
1459  {
1460  List *prunequal;
1461 
1462  prunequal = extract_actual_clauses(rel->baserestrictinfo, false);
1463 
1464  if (best_path->path.param_info)
1465  {
1466  List *prmquals = best_path->path.param_info->ppi_clauses;
1467 
1468  prmquals = extract_actual_clauses(prmquals, false);
1469  prmquals = (List *) replace_nestloop_params(root,
1470  (Node *) prmquals);
1471 
1472  prunequal = list_concat(prunequal, prmquals);
1473  }
1474 
1475  if (prunequal != NIL)
1476  partpruneinfo = make_partition_pruneinfo(root, rel,
1477  best_path->subpaths,
1478  prunequal);
1479  }
1480 
1481  node->mergeplans = subplans;
1482  node->part_prune_info = partpruneinfo;
1483 
1484  /*
1485  * If prepare_sort_from_pathkeys added sort columns, but we were told to
1486  * produce either the exact tlist or a narrow tlist, we should get rid of
1487  * the sort columns again. We must inject a projection node to do so.
1488  */
1489  if (tlist_was_changed && (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST)))
1490  {
1491  tlist = list_truncate(list_copy(plan->targetlist), orig_tlist_length);
1492  return inject_projection_plan(plan, tlist, plan->parallel_safe);
1493  }
1494  else
1495  return plan;
1496 }
#define NIL
Definition: pg_list.h:65
List * qual
Definition: plannodes.h:142
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4790
Oid * collations
Definition: plannodes.h:280
List * baserestrictinfo
Definition: pathnodes.h:745
List * list_truncate(List *list, int new_size)
Definition: list.c:600
ParamPathInfo * param_info
Definition: pathnodes.h:1185
#define CP_SMALL_TLIST
Definition: createplan.c:70
List * list_copy(const List *oldlist)
Definition: list.c:1418
Definition: nodes.h:538
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
struct PartitionPruneInfo * part_prune_info
Definition: plannodes.h:283
unsigned int Oid
Definition: postgres_ext.h:31
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:383
struct Plan * righttree
Definition: plannodes.h:144
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
#define ERROR
Definition: elog.h:46
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:801
RelOptInfo * parent
Definition: pathnodes.h:1182
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:195
Relids relids
Definition: pathnodes.h:681
List * lappend(List *list, void *datum)
Definition: list.c:336
Bitmapset * apprelids
Definition: plannodes.h:274
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:324
bool enable_partition_pruning
Definition: costsize.c:151
List * pathkeys
Definition: pathnodes.h:1196
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
Definition: createplan.c:5923
#define makeNode(_type_)
Definition: nodes.h:586
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
AttrNumber * sortColIdx
Definition: plannodes.h:278
List * ppi_clauses
Definition: pathnodes.h:1143
static Plan * inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
Definition: createplan.c:2035
Cardinality limit_tuples
Definition: pathnodes.h:1477
static int list_length(const List *l)
Definition: pg_list.h:149
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:443
struct Plan * lefttree
Definition: plannodes.h:143
List * targetlist
Definition: plannodes.h:141
bool * nullsFirst
Definition: plannodes.h:281
List * mergeplans
Definition: plannodes.h:275
PartitionPruneInfo * make_partition_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel, List *subpaths, List *prunequal)
Definition: partprune.c:222
#define elog(elevel,...)
Definition: elog.h:232
bool parallel_safe
Definition: plannodes.h:130
Oid * sortOperators
Definition: plannodes.h:279
#define CP_EXACT_TLIST
Definition: createplan.c:69
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
static void label_sort_with_costsize(PlannerInfo *root, Sort *plan, double limit_tuples)
Definition: createplan.c:5306
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:241
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:6019

◆ create_mergejoin_plan()

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

Definition at line 4294 of file createplan.c.

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().

4296 {
4297  MergeJoin *join_plan;
4298  Plan *outer_plan;
4299  Plan *inner_plan;
4300  List *tlist = build_path_tlist(root, &best_path->jpath.path);
4301  List *joinclauses;
4302  List *otherclauses;
4303  List *mergeclauses;
4304  List *outerpathkeys;
4305  List *innerpathkeys;
4306  int nClauses;
4307  Oid *mergefamilies;
4308  Oid *mergecollations;
4309  int *mergestrategies;
4310  bool *mergenullsfirst;
4311  PathKey *opathkey;
4312  EquivalenceClass *opeclass;
4313  int i;
4314  ListCell *lc;
4315  ListCell *lop;
4316  ListCell *lip;
4317  Path *outer_path = best_path->jpath.outerjoinpath;
4318  Path *inner_path = best_path->jpath.innerjoinpath;
4319 
4320  /*
4321  * MergeJoin can project, so we don't have to demand exact tlists from the
4322  * inputs. However, if we're intending to sort an input's result, it's
4323  * best to request a small tlist so we aren't sorting more data than
4324  * necessary.
4325  */
4326  outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4327  (best_path->outersortkeys != NIL) ? CP_SMALL_TLIST : 0);
4328 
4329  inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4330  (best_path->innersortkeys != NIL) ? CP_SMALL_TLIST : 0);
4331 
4332  /* Sort join qual clauses into best execution order */
4333  /* NB: do NOT reorder the mergeclauses */
4334  joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4335 
4336  /* Get the join qual clauses (in plain expression form) */
4337  /* Any pseudoconstant clauses are ignored here */
4338  if (IS_OUTER_JOIN(best_path->jpath.jointype))
4339  {
4340  extract_actual_join_clauses(joinclauses,
4341  best_path->jpath.path.parent->relids,
4342  &joinclauses, &otherclauses);
4343  }
4344  else
4345  {
4346  /* We can treat all clauses alike for an inner join */
4347  joinclauses = extract_actual_clauses(joinclauses, false);
4348  otherclauses = NIL;
4349  }
4350 
4351  /*
4352  * Remove the mergeclauses from the list of join qual clauses, leaving the
4353  * list of quals that must be checked as qpquals.
4354  */
4355  mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
4356  joinclauses = list_difference(joinclauses, mergeclauses);
4357 
4358  /*
4359  * Replace any outer-relation variables with nestloop params. There
4360  * should not be any in the mergeclauses.
4361  */
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  * Rearrange mergeclauses, if needed, so that the outer variable is always
4372  * on the left; mark the mergeclause restrictinfos with correct
4373  * outer_is_left status.
4374  */
4375  mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
4376  best_path->jpath.outerjoinpath->parent->relids);
4377 
4378  /*
4379  * Create explicit sort nodes for the outer and inner paths if necessary.
4380  */
4381  if (best_path->outersortkeys)
4382  {
4383  Relids outer_relids = outer_path->parent->relids;
4384  Sort *sort = make_sort_from_pathkeys(outer_plan,
4385  best_path->outersortkeys,
4386  outer_relids);
4387 
4388  label_sort_with_costsize(root, sort, -1.0);
4389  outer_plan = (Plan *) sort;
4390  outerpathkeys = best_path->outersortkeys;
4391  }
4392  else
4393  outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
4394 
4395  if (best_path->innersortkeys)
4396  {
4397  Relids inner_relids = inner_path->parent->relids;
4398  Sort *sort = make_sort_from_pathkeys(inner_plan,
4399  best_path->innersortkeys,
4400  inner_relids);
4401 
4402  label_sort_with_costsize(root, sort, -1.0);
4403  inner_plan = (Plan *) sort;
4404  innerpathkeys = best_path->innersortkeys;
4405  }
4406  else
4407  innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
4408 
4409  /*
4410  * If specified, add a materialize node to shield the inner plan from the
4411  * need to handle mark/restore.
4412  */
4413  if (best_path->materialize_inner)
4414  {
4415  Plan *matplan = (Plan *) make_material(inner_plan);
4416 
4417  /*
4418  * We assume the materialize will not spill to disk, and therefore
4419  * charge just cpu_operator_cost per tuple. (Keep this estimate in
4420  * sync with final_cost_mergejoin.)
4421  */
4422  copy_plan_costsize(matplan, inner_plan);
4423  matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
4424 
4425  inner_plan = matplan;
4426  }
4427 
4428  /*
4429  * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
4430  * executor. The information is in the pathkeys for the two inputs, but
4431  * we need to be careful about the possibility of mergeclauses sharing a
4432  * pathkey, as well as the possibility that the inner pathkeys are not in
4433  * an order matching the mergeclauses.
4434  */
4435  nClauses = list_length(mergeclauses);
4436  Assert(nClauses == list_length(best_path->path_mergeclauses));
4437  mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
4438  mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
4439  mergestrategies = (int *) palloc(nClauses * sizeof(int));
4440  mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
4441 
4442  opathkey = NULL;
4443  opeclass = NULL;
4444  lop = list_head(outerpathkeys);
4445  lip = list_head(innerpathkeys);
4446  i = 0;
4447  foreach(lc, best_path->path_mergeclauses)
4448  {
4449  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
4450  EquivalenceClass *oeclass;
4451  EquivalenceClass *ieclass;
4452  PathKey *ipathkey = NULL;
4453  EquivalenceClass *ipeclass = NULL;
4454  bool first_inner_match = false;
4455 
4456  /* fetch outer/inner eclass from mergeclause */
4457  if (rinfo->outer_is_left)
4458  {
4459  oeclass = rinfo->left_ec;
4460  ieclass = rinfo->right_ec;
4461  }
4462  else
4463  {
4464  oeclass = rinfo->right_ec;
4465  ieclass = rinfo->left_ec;
4466  }
4467  Assert(oeclass != NULL);
4468  Assert(ieclass != NULL);
4469 
4470  /*
4471  * We must identify the pathkey elements associated with this clause
4472  * by matching the eclasses (which should give a unique match, since
4473  * the pathkey lists should be canonical). In typical cases the merge
4474  * clauses are one-to-one with the pathkeys, but when dealing with
4475  * partially redundant query conditions, things are more complicated.
4476  *
4477  * lop and lip reference the first as-yet-unmatched pathkey elements.
4478  * If they're NULL then all pathkey elements have been matched.
4479  *
4480  * The ordering of the outer pathkeys should match the mergeclauses,
4481  * by construction (see find_mergeclauses_for_outer_pathkeys()). There
4482  * could be more than one mergeclause for the same outer pathkey, but
4483  * no pathkey may be entirely skipped over.
4484  */
4485  if (oeclass != opeclass) /* multiple matches are not interesting */
4486  {
4487  /* doesn't match the current opathkey, so must match the next */
4488  if (lop == NULL)
4489  elog(ERROR, "outer pathkeys do not match mergeclauses");
4490  opathkey = (PathKey *) lfirst(lop);
4491  opeclass = opathkey->pk_eclass;
4492  lop = lnext(outerpathkeys, lop);
4493  if (oeclass != opeclass)
4494  elog(ERROR, "outer pathkeys do not match mergeclauses");
4495  }
4496 
4497  /*
4498  * The inner pathkeys likewise should not have skipped-over keys, but
4499  * it's possible for a mergeclause to reference some earlier inner
4500  * pathkey if we had redundant pathkeys. For example we might have
4501  * mergeclauses like "o.a = i.x AND o.b = i.y AND o.c = i.x". The
4502  * implied inner ordering is then "ORDER BY x, y, x", but the pathkey
4503  * mechanism drops the second sort by x as redundant, and this code
4504  * must cope.
4505  *
4506  * It's also possible for the implied inner-rel ordering to be like
4507  * "ORDER BY x, y, x DESC". We still drop the second instance of x as
4508  * redundant; but this means that the sort ordering of a redundant
4509  * inner pathkey should not be considered significant. So we must
4510  * detect whether this is the first clause matching an inner pathkey.
4511  */
4512  if (lip)
4513  {
4514  ipathkey = (PathKey *) lfirst(lip);
4515  ipeclass = ipathkey->pk_eclass;
4516  if (ieclass == ipeclass)
4517  {
4518  /* successful first match to this inner pathkey */
4519  lip = lnext(innerpathkeys, lip);
4520  first_inner_match = true;
4521  }
4522  }
4523  if (!first_inner_match)
4524  {
4525  /* redundant clause ... must match something before lip */
4526  ListCell *l2;
4527 
4528  foreach(l2, innerpathkeys)
4529  {
4530  if (l2 == lip)
4531  break;
4532  ipathkey = (PathKey *) lfirst(l2);
4533  ipeclass = ipathkey->pk_eclass;
4534  if (ieclass == ipeclass)
4535  break;
4536  }
4537  if (ieclass != ipeclass)
4538  elog(ERROR, "inner pathkeys do not match mergeclauses");
4539  }
4540 
4541  /*
4542  * The pathkeys should always match each other as to opfamily and
4543  * collation (which affect equality), but if we're considering a
4544  * redundant inner pathkey, its sort ordering might not match. In
4545  * such cases we may ignore the inner pathkey's sort ordering and use
4546  * the outer's. (In effect, we're lying to the executor about the
4547  * sort direction of this inner column, but it does not matter since
4548  * the run-time row comparisons would only reach this column when
4549  * there's equality for the earlier column containing the same eclass.
4550  * There could be only one value in this column for the range of inner
4551  * rows having a given value in the earlier column, so it does not
4552  * matter which way we imagine this column to be ordered.) But a
4553  * non-redundant inner pathkey had better match outer's ordering too.
4554  */
4555  if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
4556  opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation)
4557  elog(ERROR, "left and right pathkeys do not match in mergejoin");
4558  if (first_inner_match &&
4559  (opathkey->pk_strategy != ipathkey->pk_strategy ||
4560  opathkey->pk_nulls_first != ipathkey->pk_nulls_first))
4561  elog(ERROR, "left and right pathkeys do not match in mergejoin");
4562 
4563  /* OK, save info for executor */
4564  mergefamilies[i] = opathkey->pk_opfamily;
4565  mergecollations[i] = opathkey->pk_eclass->ec_collation;
4566  mergestrategies[i] = opathkey->pk_strategy;
4567  mergenullsfirst[i] = opathkey->pk_nulls_first;
4568  i++;
4569  }
4570 
4571  /*
4572  * Note: it is not an error if we have additional pathkey elements (i.e.,
4573  * lop or lip isn't NULL here). The input paths might be better-sorted
4574  * than we need for the current mergejoin.
4575  */
4576 
4577  /*
4578  * Now we can build the mergejoin node.
4579  */
4580  join_plan = make_mergejoin(tlist,
4581  joinclauses,
4582  otherclauses,
4583  mergeclauses,
4584  mergefamilies,
4585  mergecollations,
4586  mergestrategies,
4587  mergenullsfirst,
4588  outer_plan,
4589  inner_plan,
4590  best_path->jpath.jointype,
4591  best_path->jpath.inner_unique,
4592  best_path->skip_mark_restore);
4593 
4594  /* Costs of sort and material steps are included in path cost already */
4595  copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4596 
4597  return join_plan;
4598 }
#define NIL
Definition: pg_list.h:65
List * path_mergeclauses
Definition: pathnodes.h:1649
List * outersortkeys
Definition: pathnodes.h:1650
static Sort * make_sort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids)
Definition: createplan.c:6204
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4790
static List * get_switched_clauses(List *clauses, Relids outerrelids)
Definition: createplan.c:5098
bool materialize_inner
Definition: pathnodes.h:1653
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
void extract_actual_join_clauses(List *restrictinfo_list, Relids joinrelids, List **joinquals, List **otherquals)
Definition: restrictinfo.c:470
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:5882
Path * innerjoinpath
Definition: pathnodes.h:1591
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:755
ParamPathInfo * param_info
Definition: pathnodes.h:1185
#define CP_SMALL_TLIST
Definition: createplan.c:70
Definition: nodes.h:538
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:420
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5175
EquivalenceClass * right_ec
Definition: pathnodes.h:2108
unsigned int Oid
Definition: postgres_ext.h:31
int pk_strategy
Definition: pathnodes.h:1068
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:383
static Material * make_material(Plan *lefttree)
Definition: createplan.c:6363
bool skip_mark_restore
Definition: pathnodes.h:1652
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
bool pk_nulls_first
Definition: pathnodes.h:1069
#define ERROR
Definition: elog.h:46
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:801
List * joinrestrictinfo
Definition: pathnodes.h:1593
RelOptInfo * parent
Definition: pathnodes.h:1182
#define lfirst_node(type, lc)
Definition: pg_list.h:172
bool outer_is_left
Definition: pathnodes.h:2114
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:195
static void copy_plan_costsize(Plan *dest, Plan *src)
Definition: createplan.c:5284
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
Relids relids
Definition: pathnodes.h:681
double cpu_operator_cost
Definition: costsize.c:123
Path * outerjoinpath
Definition: pathnodes.h:1590
List * pathkeys
Definition: pathnodes.h:1196
Path path
Definition: pathnodes.h:1583
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
EquivalenceClass * pk_eclass
Definition: pathnodes.h:1066
static int list_length(const List *l)
Definition: pg_list.h:149
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:443
Cardinality plan_rows
Definition: plannodes.h:123
bool inner_unique
Definition: pathnodes.h:1587
List * list_difference(const List *list1, const List *list2)
Definition: list.c:1091
List * innersortkeys
Definition: pathnodes.h:1651
Oid pk_opfamily
Definition: pathnodes.h:1067
void * palloc(Size size)
Definition: mcxt.c:1062
EquivalenceClass * left_ec
Definition: pathnodes.h:2107
Join join
Definition: plannodes.h:749
JoinType jointype
Definition: pathnodes.h:1585
#define elog(elevel,...)
Definition: elog.h:232
int i
Cost total_cost
Definition: plannodes.h:118
JoinPath jpath
Definition: pathnodes.h:1648
Definition: pg_list.h:50
static void label_sort_with_costsize(PlannerInfo *root, Sort *plan, double limit_tuples)
Definition: createplan.c:5306
Plan plan
Definition: plannodes.h:706

◆ create_minmaxagg_plan()

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

Definition at line 2465 of file createplan.c.

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, Plan::parallel_safe, Path::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(), Plan::startup_cost, Path::startup_cost, MinMaxAggInfo::subroot, Plan::total_cost, and PathTarget::width.

Referenced by create_plan_recurse().

2466 {
2467  Result *plan;
2468  List *tlist;
2469  ListCell *lc;
2470 
2471  /* Prepare an InitPlan for each aggregate's subquery. */
2472  foreach(lc, best_path->mmaggregates)
2473  {
2474  MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
2475  PlannerInfo *subroot = mminfo->subroot;
2476  Query *subparse = subroot->parse;
2477  Plan *plan;
2478 
2479  /*
2480  * Generate the plan for the subquery. We already have a Path, but we
2481  * have to convert it to a Plan and attach a LIMIT node above it.
2482  * Since we are entering a different planner context (subroot),
2483  * recurse to create_plan not create_plan_recurse.
2484  */
2485  plan = create_plan(subroot, mminfo->path);
2486 
2487  plan = (Plan *) make_limit(plan,
2488  subparse->limitOffset,
2489  subparse->limitCount,
2490  subparse->limitOption,
2491  0, NULL, NULL, NULL);
2492 
2493  /* Must apply correct cost/width data to Limit node */
2494  plan->startup_cost = mminfo->path->startup_cost;
2495  plan->total_cost = mminfo->pathcost;
2496  plan->plan_rows = 1;
2497  plan->plan_width = mminfo->path->pathtarget->width;
2498  plan->parallel_aware = false;
2499  plan->parallel_safe = mminfo->path->parallel_safe;
2500 
2501  /* Convert the plan into an InitPlan in the outer query. */
2502  SS_make_initplan_from_plan(root, subroot, plan, mminfo->param);
2503  }
2504 
2505  /* Generate the output plan --- basically just a Result */
2506  tlist = build_path_tlist(root, &best_path->path);
2507 
2508  plan = make_result(tlist, (Node *) best_path->quals, NULL);
2509 
2510  copy_generic_path_info(&plan->plan, (Path *) best_path);
2511 
2512  /*
2513  * During setrefs.c, we'll need to replace references to the Agg nodes
2514  * with InitPlan output params. (We can't just do that locally in the
2515  * MinMaxAgg node, because path nodes above here may have Agg references
2516  * as well.) Save the mmaggregates list to tell setrefs.c to do that.
2517  */
2518  Assert(root->minmax_aggs == NIL);
2519  root->minmax_aggs = best_path->mmaggregates;
2520 
2521  return plan;
2522 }
Node * limitOffset
Definition: parsenodes.h:171
#define NIL
Definition: pg_list.h:65
PathTarget * pathtarget
Definition: pathnodes.h:1183
Query * parse
Definition: pathnodes.h:162
Param * param
Definition: pathnodes.h:2434
Definition: nodes.h:538
List * minmax_aggs
Definition: pathnodes.h:333
List * quals
Definition: pathnodes.h:1822
Plan * create_plan(PlannerInfo *root, Path *best_path)
Definition: createplan.c:332
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:801
Cost startup_cost
Definition: pathnodes.h:1193
Node * limitCount
Definition: parsenodes.h:172
Cost startup_cost
Definition: plannodes.h:117
bool parallel_aware
Definition: plannodes.h:129
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount, LimitOption limitOption, int uniqNumCols, AttrNumber *uniqColIdx, Oid *uniqOperators, Oid *uniqCollations)
Definition: createplan.c:6805
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6833
LimitOption limitOption
Definition: parsenodes.h:173
List * mmaggregates
Definition: pathnodes.h:1821
int plan_width
Definition: plannodes.h:124
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
bool parallel_safe
Definition: pathnodes.h:1188
Cardinality plan_rows
Definition: plannodes.h:123
void SS_make_initplan_from_plan(PlannerInfo *root, PlannerInfo *subroot, Plan *plan, Param *prm)
Definition: subselect.c:2956
Cost total_cost
Definition: plannodes.h:118
bool parallel_safe
Definition: plannodes.h:130
Definition: pg_list.h:50
PlannerInfo * subroot
Definition: pathnodes.h:2431

◆ create_modifytable_plan()

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

Definition at line 2731 of file createplan.c.

References apply_tlist_labeling(), ModifyTablePath::canSetTag, copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), ModifyTablePath::epqParam, make_modifytable(), 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().

2732 {
2733  ModifyTable *plan;
2734  Path *subpath = best_path->subpath;
2735  Plan *subplan;
2736 
2737  /* Subplan must produce exactly the specified tlist */
2738  subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
2739 
2740  /* Transfer resname/resjunk labeling, too, to keep executor happy */
2742 
2743  plan = make_modifytable(root,
2744  subplan,
2745  best_path->operation,
2746  best_path->canSetTag,
2747  best_path->nominalRelation,
2748  best_path->rootRelation,
2749  best_path->partColsUpdated,
2750  best_path->resultRelations,
2751  best_path->updateColnosLists,
2752  best_path->withCheckOptionLists,
2753  best_path->returningLists,
2754  best_path->rowMarks,
2755  best_path->onconflict,
2756  best_path->epqParam);
2757 
2758  copy_generic_path_info(&plan->plan, &best_path->path);
2759 
2760  return plan;
2761 }
void apply_tlist_labeling(List *dest_tlist, List *src_tlist)
Definition: tlist.c:307
List * returningLists
Definition: pathnodes.h:1893
OnConflictExpr * onconflict
Definition: pathnodes.h:1895
Index nominalRelation
Definition: pathnodes.h:1887
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:383
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
bool partColsUpdated
Definition: pathnodes.h:1889
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, int epqParam)
Definition: createplan.c:6873
Index rootRelation
Definition: pathnodes.h:1888
List * targetlist
Definition: plannodes.h:141
List * withCheckOptionLists
Definition: pathnodes.h:1892
CmdType operation
Definition: pathnodes.h:1885
List * resultRelations
Definition: pathnodes.h:1890
List * updateColnosLists
Definition: pathnodes.h:1891
List * processed_tlist
Definition: pathnodes.h:321
#define CP_EXACT_TLIST
Definition: createplan.c:69
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:241

◆ create_namedtuplestorescan_plan()

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

Definition at line 3867 of file createplan.c.

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().

3869 {
3870  NamedTuplestoreScan *scan_plan;
3871  Index scan_relid = best_path->parent->relid;
3872  RangeTblEntry *rte;
3873 
3874  Assert(scan_relid > 0);
3875  rte = planner_rt_fetch(scan_relid, root);
3877 
3878  /* Sort clauses into best execution order */
3879  scan_clauses = order_qual_clauses(root, scan_clauses);
3880 
3881  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3882  scan_clauses = extract_actual_clauses(scan_clauses, false);
3883 
3884  /* Replace any outer-relation variables with nestloop params */
3885  if (best_path->param_info)
3886  {
3887  scan_clauses = (List *)
3888  replace_nestloop_params(root, (Node *) scan_clauses);
3889  }
3890 
3891  scan_plan = make_namedtuplestorescan(tlist, scan_clauses, scan_relid,
3892  rte->enrname);
3893 
3894  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3895 
3896  return scan_plan;
3897 }
Plan plan
Definition: plannodes.h:343
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4790
ParamPathInfo * param_info
Definition: pathnodes.h:1185
Definition: nodes.h:538
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5175
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:388
RelOptInfo * parent
Definition: pathnodes.h:1182
char * enrname
Definition: parsenodes.h:1146
Index relid
Definition: pathnodes.h:709
static NamedTuplestoreScan * make_namedtuplestorescan(List *qptlist, List *qpqual, Index scanrelid, char *enrname)
Definition: createplan.c:5640
unsigned int Index
Definition: c.h:549
#define Assert(condition)
Definition: c.h:804
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:443
RTEKind rtekind
Definition: parsenodes.h:1007
Definition: pg_list.h:50

◆ create_nestloop_plan()

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

Definition at line 4218 of file createplan.c.

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().

4220 {
4221  NestLoop *join_plan;
4222  Plan *outer_plan;
4223  Plan *inner_plan;
4224  List *tlist = build_path_tlist(root, &best_path->jpath.path);
4225  List *joinrestrictclauses = best_path->jpath.joinrestrictinfo;
4226  List *joinclauses;
4227  List *otherclauses;
4228  Relids outerrelids;
4229  List *nestParams;
4230  Relids saveOuterRels = root->curOuterRels;
4231 
4232  /* NestLoop can project, so no need to be picky about child tlists */
4233  outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath, 0);
4234 
4235  /* For a nestloop, include outer relids in curOuterRels for inner side */
4236  root->curOuterRels = bms_union(root->curOuterRels,
4237  best_path->jpath.outerjoinpath->parent->relids);
4238 
4239  inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath, 0);
4240 
4241  /* Restore curOuterRels */
4242  bms_free(root->curOuterRels);
4243  root->curOuterRels = saveOuterRels;
4244 
4245  /* Sort join qual clauses into best execution order */
4246  joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
4247 
4248  /* Get the join qual clauses (in plain expression form) */
4249  /* Any pseudoconstant clauses are ignored here */
4250  if (IS_OUTER_JOIN(best_path->jpath.jointype))
4251  {
4252  extract_actual_join_clauses(joinrestrictclauses,
4253  best_path->jpath.path.parent->relids,
4254  &joinclauses, &otherclauses);
4255  }
4256  else
4257  {
4258  /* We can treat all clauses alike for an inner join */
4259  joinclauses = extract_actual_clauses(joinrestrictclauses, false);
4260  otherclauses = NIL;
4261  }
4262 
4263  /* Replace any outer-relation variables with nestloop params */
4264  if (best_path->jpath.path.param_info)
4265  {
4266  joinclauses = (List *)
4267  replace_nestloop_params(root, (Node *) joinclauses);
4268  otherclauses = (List *)
4269  replace_nestloop_params(root, (Node *) otherclauses);
4270  }
4271 
4272  /*
4273  * Identify any nestloop parameters that should be supplied by this join
4274  * node, and remove them from root->curOuterParams.
4275  */
4276  outerrelids = best_path->jpath.outerjoinpath->parent->relids;
4277  nestParams = identify_current_nestloop_params(root, outerrelids);
4278 
4279  join_plan = make_nestloop(tlist,
4280  joinclauses,
4281  otherclauses,
4282  nestParams,
4283  outer_plan,
4284  inner_plan,
4285  best_path->jpath.jointype,
4286  best_path->jpath.inner_unique);
4287 
4288  copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4289 
4290  return join_plan;
4291 }
#define NIL
Definition: pg_list.h:65
List * identify_current_nestloop_params(PlannerInfo *root, Relids leftrelids)
Definition: paramassign.c:508
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4790
Relids curOuterRels
Definition: pathnodes.h:368
void extract_actual_join_clauses(List *restrictinfo_list, Relids joinrelids, List **joinquals, List **otherquals)
Definition: restrictinfo.c:470
Path * innerjoinpath
Definition: pathnodes.h:1591
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:755
ParamPathInfo * param_info
Definition: pathnodes.h:1185
Definition: nodes.h:538
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5175
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:383
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5269
Join join
Definition: plannodes.h:725
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:801
List * joinrestrictinfo
Definition: pathnodes.h:1593
RelOptInfo * parent
Definition: pathnodes.h:1182
static NestLoop * make_nestloop(List *tlist, List *joinclauses, List *otherclauses, List *nestParams, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
Definition: createplan.c:5803
Relids relids
Definition: pathnodes.h:681
JoinPath jpath
Definition: pathnodes.h:1608
Path * outerjoinpath
Definition: pathnodes.h:1590
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
Path path
Definition: pathnodes.h:1583
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:443
bool inner_unique
Definition: pathnodes.h:1587
JoinType jointype
Definition: pathnodes.h:1585
Definition: pg_list.h:50
Plan plan
Definition: plannodes.h:706

◆ create_plan()

Plan* create_plan ( PlannerInfo root,
Path best_path 
)

Definition at line 332 of file createplan.c.

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().

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

◆ create_plan_recurse()

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

Definition at line 383 of file createplan.c.

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().

384 {
385  Plan *plan;
386 
387  /* Guard against stack overflow due to overly complex plans */
389 
390  switch (best_path->pathtype)
391  {
392  case T_SeqScan:
393  case T_SampleScan:
394  case T_IndexScan:
395  case T_IndexOnlyScan:
396  case T_BitmapHeapScan:
397  case T_TidScan:
398  case T_TidRangeScan:
399  case T_SubqueryScan:
400  case T_FunctionScan:
401  case T_TableFuncScan:
402  case T_ValuesScan:
403  case T_CteScan:
404  case T_WorkTableScan:
406  case T_ForeignScan:
407  case T_CustomScan:
408  plan = create_scan_plan(root, best_path, flags);
409  break;
410  case T_HashJoin:
411  case T_MergeJoin:
412  case T_NestLoop:
413  plan = create_join_plan(root,
414  (JoinPath *) best_path);
415  break;
416  case T_Append:
417  plan = create_append_plan(root,
418  (AppendPath *) best_path,
419  flags);
420  break;
421  case T_MergeAppend:
422  plan = create_merge_append_plan(root,
423  (MergeAppendPath *) best_path,
424  flags);
425  break;
426  case T_Result:
427  if (IsA(best_path, ProjectionPath))
428  {
429  plan = create_projection_plan(root,
430  (ProjectionPath *) best_path,
431  flags);
432  }
433  else if (IsA(best_path, MinMaxAggPath))
434  {
435  plan = (Plan *) create_minmaxagg_plan(root,
436  (MinMaxAggPath *) best_path);
437  }