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/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 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 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 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 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 EquivalenceMemberfind_ec_member_for_tle (EquivalenceClass *ec, TargetEntry *tle, Relids relids)
 
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 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, CmdType operation, bool canSetTag, Index nominalRelation, Index rootRelation, bool partColsUpdated, List *resultRelations, List *subplans, List *subroots, 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 71 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 5211 of file createplan.c.

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

Referenced by create_bitmap_scan_plan().

5212 {
5213  if (IsA(plan, BitmapAnd))
5214  bitmap_subplan_mark_shared(linitial(((BitmapAnd *) plan)->bitmapplans));
5215  else if (IsA(plan, BitmapOr))
5216  {
5217  ((BitmapOr *) plan)->isshared = true;
5218  bitmap_subplan_mark_shared(linitial(((BitmapOr *) plan)->bitmapplans));
5219  }
5220  else if (IsA(plan, BitmapIndexScan))
5221  ((BitmapIndexScan *) plan)->isshared = true;
5222  else
5223  elog(ERROR, "unrecognized node type: %d", nodeTag(plan));
5224 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
static void bitmap_subplan_mark_shared(Plan *plan)
Definition: createplan.c:5211
#define nodeTag(nodeptr)
Definition: nodes.h:534
#define elog(elevel,...)
Definition: elog.h:214

◆ build_path_tlist()

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

Definition at line 777 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().

778 {
779  List *tlist = NIL;
780  Index *sortgrouprefs = path->pathtarget->sortgrouprefs;
781  int resno = 1;
782  ListCell *v;
783 
784  foreach(v, path->pathtarget->exprs)
785  {
786  Node *node = (Node *) lfirst(v);
787  TargetEntry *tle;
788 
789  /*
790  * If it's a parameterized path, there might be lateral references in
791  * the tlist, which need to be replaced with Params. There's no need
792  * to remake the TargetEntry nodes, so apply this to each list item
793  * separately.
794  */
795  if (path->param_info)
796  node = replace_nestloop_params(root, node);
797 
798  tle = makeTargetEntry((Expr *) node,
799  resno,
800  NULL,
801  false);
802  if (sortgrouprefs)
803  tle->ressortgroupref = sortgrouprefs[resno - 1];
804 
805  tlist = lappend(tlist, tle);
806  resno++;
807  }
808  return tlist;
809 }
#define NIL
Definition: pg_list.h:65
PathTarget * pathtarget
Definition: pathnodes.h:1145
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4664
ParamPathInfo * param_info
Definition: pathnodes.h:1147
Definition: nodes.h:529
Index * sortgrouprefs
Definition: pathnodes.h:1075
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:238
List * lappend(List *list, void *datum)
Definition: list.c:321
List * exprs
Definition: pathnodes.h:1074
unsigned int Index
Definition: c.h:475
#define lfirst(lc)
Definition: pg_list.h:190
Index ressortgroupref
Definition: primnodes.h:1410
Definition: pg_list.h:50

◆ change_plan_targetlist()

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

Definition at line 1954 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().

1955 {
1956  /*
1957  * If the top plan node can't do projections and its existing target list
1958  * isn't already what we need, we need to add a Result node to help it
1959  * along.
1960  */
1961  if (!is_projection_capable_plan(subplan) &&
1962  !tlist_same_exprs(tlist, subplan->targetlist))
1963  subplan = inject_projection_plan(subplan, tlist,
1964  subplan->parallel_safe &&
1965  tlist_parallel_safe);
1966  else
1967  {
1968  /* Else we can just replace the plan node's tlist */
1969  subplan->targetlist = tlist;
1970  subplan->parallel_safe &= tlist_parallel_safe;
1971  }
1972  return subplan;
1973 }
bool is_projection_capable_plan(Plan *plan)
Definition: createplan.c:6988
static Plan * inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
Definition: createplan.c:1922
bool tlist_same_exprs(List *tlist1, List *tlist2)
Definition: tlist.c:240
List * targetlist
Definition: plannodes.h:142
bool parallel_safe
Definition: plannodes.h:136

◆ copy_generic_path_info()

static void copy_generic_path_info ( Plan dest,
Path src 
)
static

Definition at line 5142 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_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_tidscan_plan(), create_unique_plan(), create_upper_unique_plan(), create_valuesscan_plan(), create_windowagg_plan(), and create_worktablescan_plan().

5143 {
5144  dest->startup_cost = src->startup_cost;
5145  dest->total_cost = src->total_cost;
5146  dest->plan_rows = src->rows;
5147  dest->plan_width = src->pathtarget->width;
5148  dest->parallel_aware = src->parallel_aware;
5149  dest->parallel_safe = src->parallel_safe;
5150 }
double plan_rows
Definition: plannodes.h:129
PathTarget * pathtarget
Definition: pathnodes.h:1145
Cost startup_cost
Definition: pathnodes.h:1155
Cost startup_cost
Definition: plannodes.h:123
bool parallel_aware
Definition: plannodes.h:135
Cost total_cost
Definition: pathnodes.h:1156
int plan_width
Definition: plannodes.h:130
double rows
Definition: pathnodes.h:1154
bool parallel_safe
Definition: pathnodes.h:1150
Cost total_cost
Definition: plannodes.h:124
bool parallel_aware
Definition: pathnodes.h:1149
bool parallel_safe
Definition: plannodes.h:136

◆ copy_plan_costsize()

static void copy_plan_costsize ( Plan dest,
Plan src 
)
static

Definition at line 5157 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().

5158 {
5159  dest->startup_cost = src->startup_cost;
5160  dest->total_cost = src->total_cost;
5161  dest->plan_rows = src->plan_rows;
5162  dest->plan_width = src->plan_width;
5163  /* Assume the inserted node is not parallel-aware. */
5164  dest->parallel_aware = false;
5165  /* Assume the inserted node is parallel-safe, if child plan is. */
5166  dest->parallel_safe = src->parallel_safe;
5167 }
double plan_rows
Definition: plannodes.h:129
Cost startup_cost
Definition: plannodes.h:123
bool parallel_aware
Definition: plannodes.h:135
int plan_width
Definition: plannodes.h:130
Cost total_cost
Definition: plannodes.h:124
bool parallel_safe
Definition: plannodes.h:136

◆ create_agg_plan()

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

Definition at line 2110 of file createplan.c.

References AGG_HASHED, AggPath::aggsplit, AggPath::aggstrategy, build_path_tlist(), copy_generic_path_info(), CP_LABEL_TLIST, CP_SMALL_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().

2111 {
2112  Agg *plan;
2113  Plan *subplan;
2114  List *tlist;
2115  List *quals;
2116  int flags;
2117 
2118  /*
2119  * Agg can project, so no need to be terribly picky about child tlist, but
2120  * we do need grouping columns to be available. We are a bit more careful
2121  * with hash aggregate, where we explicitly request small tlist to
2122  * minimize I/O needed for spilling (we can't be sure spilling won't be
2123  * necessary, so we just do it every time).
2124  */
2125  flags = CP_LABEL_TLIST;
2126 
2127  /* ensure small tlist for hash aggregate */
2128  if (best_path->aggstrategy == AGG_HASHED)
2129  flags |= CP_SMALL_TLIST;
2130 
2131  subplan = create_plan_recurse(root, best_path->subpath, flags);
2132 
2133  tlist = build_path_tlist(root, &best_path->path);
2134 
2135  quals = order_qual_clauses(root, best_path->qual);
2136 
2137  plan = make_agg(tlist, quals,
2138  best_path->aggstrategy,
2139  best_path->aggsplit,
2140  list_length(best_path->groupClause),
2142  subplan->targetlist),
2143  extract_grouping_ops(best_path->groupClause),
2145  subplan->targetlist),
2146  NIL,
2147  NIL,
2148  best_path->numGroups,
2149  best_path->transitionSpace,
2150  subplan);
2151 
2152  copy_generic_path_info(&plan->plan, (Path *) best_path);
2153 
2154  return plan;
2155 }
#define NIL
Definition: pg_list.h:65
AggStrategy aggstrategy
Definition: pathnodes.h:1700
Oid * extract_grouping_collations(List *groupClause, List *tlist)
Definition: tlist.c:511
List * qual
Definition: pathnodes.h:1705
#define CP_SMALL_TLIST
Definition: createplan.c:69
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5048
AggSplit aggsplit
Definition: pathnodes.h:1701
Oid * extract_grouping_ops(List *groupClause)
Definition: tlist.c:485
double numGroups
Definition: pathnodes.h:1702
AttrNumber * extract_grouping_cols(List *groupClause, List *tlist)
Definition: tlist.c:536
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:372
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
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:6363
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:777
List * groupClause
Definition: pathnodes.h:1704
Plan plan
Definition: plannodes.h:818
#define CP_LABEL_TLIST
Definition: createplan.c:70
uint64 transitionSpace
Definition: pathnodes.h:1703
static int list_length(const List *l)
Definition: pg_list.h:169
Path * subpath
Definition: pathnodes.h:1699
List * targetlist
Definition: plannodes.h:142
Definition: plannodes.h:816
Definition: pg_list.h:50
Path path
Definition: pathnodes.h:1698

◆ create_append_plan()

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

Definition at line 1077 of file createplan.c.

References Append::appendplans, Append::apprelids, Assert, RelOptInfo::baserestrictinfo, build_path_tlist(), copy_generic_path_info(), CP_EXACT_TLIST, CP_SMALL_TLIST, create_plan_recurse(), elog, enable_partition_pruning, ERROR, extract_actual_clauses(), AppendPath::first_partial_path, Append::first_partial_plan, inject_projection_plan(), label_sort_with_costsize(), lappend(), Plan::lefttree, lfirst, AppendPath::limit_tuples, list_concat(), list_copy(), list_length(), list_make1, list_truncate(), make_partition_pruneinfo(), make_result(), make_sort(), makeBoolConst(), makeNode, NIL, Plan::parallel_safe, Path::param_info, Path::parent, Append::part_prune_info, AppendPath::partitioned_rels, AppendPath::path, Path::pathkeys, pathkeys_contained_in(), Append::plan, ParamPathInfo::ppi_clauses, prepare_sort_from_pathkeys(), Plan::qual, RelOptInfo::relids, RELOPT_BASEREL, RelOptInfo::reloptkind, replace_nestloop_params(), Plan::righttree, sort(), subpath(), AppendPath::subpaths, and Plan::targetlist.

Referenced by create_plan_recurse().

1078 {
1079  Append *plan;
1080  List *tlist = build_path_tlist(root, &best_path->path);
1081  int orig_tlist_length = list_length(tlist);
1082  bool tlist_was_changed = false;
1083  List *pathkeys = best_path->path.pathkeys;
1084  List *subplans = NIL;
1085  ListCell *subpaths;
1086  RelOptInfo *rel = best_path->path.parent;
1087  PartitionPruneInfo *partpruneinfo = NULL;
1088  int nodenumsortkeys = 0;
1089  AttrNumber *nodeSortColIdx = NULL;
1090  Oid *nodeSortOperators = NULL;
1091  Oid *nodeCollations = NULL;
1092  bool *nodeNullsFirst = NULL;
1093 
1094  /*
1095  * The subpaths list could be empty, if every child was proven empty by
1096  * constraint exclusion. In that case generate a dummy plan that returns
1097  * no rows.
1098  *
1099  * Note that an AppendPath with no members is also generated in certain
1100  * cases where there was no appending construct at all, but we know the
1101  * relation is empty (see set_dummy_rel_pathlist and mark_dummy_rel).
1102  */
1103  if (best_path->subpaths == NIL)
1104  {
1105  /* Generate a Result plan with constant-FALSE gating qual */
1106  Plan *plan;
1107 
1108  plan = (Plan *) make_result(tlist,
1109  (Node *) list_make1(makeBoolConst(false,
1110  false)),
1111  NULL);
1112 
1113  copy_generic_path_info(plan, (Path *) best_path);
1114 
1115  return plan;
1116  }
1117 
1118  /*
1119  * Otherwise build an Append plan. Note that if there's just one child,
1120  * the Append is pretty useless; but we wait till setrefs.c to get rid of
1121  * it. Doing so here doesn't work because the varno of the child scan
1122  * plan won't match the parent-rel Vars it'll be asked to emit.
1123  *
1124  * We don't have the actual creation of the Append node split out into a
1125  * separate make_xxx function. This is because we want to run
1126  * prepare_sort_from_pathkeys on it before we do so on the individual
1127  * child plans, to make cross-checking the sort info easier.
1128  */
1129  plan = makeNode(Append);
1130  plan->plan.targetlist = tlist;
1131  plan->plan.qual = NIL;
1132  plan->plan.lefttree = NULL;
1133  plan->plan.righttree = NULL;
1134  plan->apprelids = rel->relids;
1135 
1136  if (pathkeys != NIL)
1137  {
1138  /*
1139  * Compute sort column info, and adjust the Append's tlist as needed.
1140  * Because we pass adjust_tlist_in_place = true, we may ignore the
1141  * function result; it must be the same plan node. However, we then
1142  * need to detect whether any tlist entries were added.
1143  */
1144  (void) prepare_sort_from_pathkeys((Plan *) plan, pathkeys,
1145  best_path->path.parent->relids,
1146  NULL,
1147  true,
1148  &nodenumsortkeys,
1149  &nodeSortColIdx,
1150  &nodeSortOperators,
1151  &nodeCollations,
1152  &nodeNullsFirst);
1153  tlist_was_changed = (orig_tlist_length != list_length(plan->plan.targetlist));
1154  }
1155 
1156  /* Build the plan for each child */
1157  foreach(subpaths, best_path->subpaths)
1158  {
1159  Path *subpath = (Path *) lfirst(subpaths);
1160  Plan *subplan;
1161 
1162  /* Must insist that all children return the same tlist */
1163  subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
1164 
1165  /*
1166  * For ordered Appends, we must insert a Sort node if subplan isn't
1167  * sufficiently ordered.
1168  */
1169  if (pathkeys != NIL)
1170  {
1171  int numsortkeys;
1172  AttrNumber *sortColIdx;
1173  Oid *sortOperators;
1174  Oid *collations;
1175  bool *nullsFirst;
1176 
1177  /*
1178  * Compute sort column info, and adjust subplan's tlist as needed.
1179  * We must apply prepare_sort_from_pathkeys even to subplans that
1180  * don't need an explicit sort, to make sure they are returning
1181  * the same sort key columns the Append expects.
1182  */
1183  subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1184  subpath->parent->relids,
1185  nodeSortColIdx,
1186  false,
1187  &numsortkeys,
1188  &sortColIdx,
1189  &sortOperators,
1190  &collations,
1191  &nullsFirst);
1192 
1193  /*
1194  * Check that we got the same sort key information. We just
1195  * Assert that the sortops match, since those depend only on the
1196  * pathkeys; but it seems like a good idea to check the sort
1197  * column numbers explicitly, to ensure the tlists match up.
1198  */
1199  Assert(numsortkeys == nodenumsortkeys);
1200  if (memcmp(sortColIdx, nodeSortColIdx,
1201  numsortkeys * sizeof(AttrNumber)) != 0)
1202  elog(ERROR, "Append child's targetlist doesn't match Append");
1203  Assert(memcmp(sortOperators, nodeSortOperators,
1204  numsortkeys * sizeof(Oid)) == 0);
1205  Assert(memcmp(collations, nodeCollations,
1206  numsortkeys * sizeof(Oid)) == 0);
1207  Assert(memcmp(nullsFirst, nodeNullsFirst,
1208  numsortkeys * sizeof(bool)) == 0);
1209 
1210  /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1211  if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
1212  {
1213  Sort *sort = make_sort(subplan, numsortkeys,
1214  sortColIdx, sortOperators,
1215  collations, nullsFirst);
1216 
1217  label_sort_with_costsize(root, sort, best_path->limit_tuples);
1218  subplan = (Plan *) sort;
1219  }
1220  }
1221 
1222  subplans = lappend(subplans, subplan);
1223  }
1224 
1225  /*
1226  * If any quals exist, they may be useful to perform further partition
1227  * pruning during execution. Gather information needed by the executor to
1228  * do partition pruning.
1229  */
1231  rel->reloptkind == RELOPT_BASEREL &&
1232  best_path->partitioned_rels != NIL)
1233  {
1234  List *prunequal;
1235 
1236  prunequal = extract_actual_clauses(rel->baserestrictinfo, false);
1237 
1238  if (best_path->path.param_info)
1239  {
1240  List *prmquals = best_path->path.param_info->ppi_clauses;
1241 
1242  prmquals = extract_actual_clauses(prmquals, false);
1243  prmquals = (List *) replace_nestloop_params(root,
1244  (Node *) prmquals);
1245 
1246  prunequal = list_concat(prunequal, prmquals);
1247  }
1248 
1249  if (prunequal != NIL)
1250  partpruneinfo =
1251  make_partition_pruneinfo(root, rel,
1252  best_path->subpaths,
1253  best_path->partitioned_rels,
1254  prunequal);
1255  }
1256 
1257  plan->appendplans = subplans;
1258  plan->first_partial_plan = best_path->first_partial_path;
1259  plan->part_prune_info = partpruneinfo;
1260 
1261  copy_generic_path_info(&plan->plan, (Path *) best_path);
1262 
1263  /*
1264  * If prepare_sort_from_pathkeys added sort columns, but we were told to
1265  * produce either the exact tlist or a narrow tlist, we should get rid of
1266  * the sort columns again. We must inject a projection node to do so.
1267  */
1268  if (tlist_was_changed && (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST)))
1269  {
1270  tlist = list_truncate(list_copy(plan->plan.targetlist),
1271  orig_tlist_length);
1272  return inject_projection_plan((Plan *) plan, tlist,
1273  plan->plan.parallel_safe);
1274  }
1275  else
1276  return (Plan *) plan;
1277 }
#define NIL
Definition: pg_list.h:65
List * qual
Definition: plannodes.h:143
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4664
RelOptKind reloptkind
Definition: pathnodes.h:662
double limit_tuples
Definition: pathnodes.h:1407
List * baserestrictinfo
Definition: pathnodes.h:727
List * list_truncate(List *list, int new_size)
Definition: list.c:585
ParamPathInfo * param_info
Definition: pathnodes.h:1147
#define CP_SMALL_TLIST
Definition: createplan.c:69
List * list_copy(const List *oldlist)
Definition: list.c:1403
Definition: nodes.h:529
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
unsigned int Oid
Definition: postgres_ext.h:31
int first_partial_path
Definition: pathnodes.h:1406
#define list_make1(x1)
Definition: pg_list.h:227
List * subpaths
Definition: pathnodes.h:1404
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:372
struct Plan * righttree
Definition: plannodes.h:145
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
#define ERROR
Definition: elog.h:43
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:777
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:357
RelOptInfo * parent
Definition: pathnodes.h:1144
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:195
Relids relids
Definition: pathnodes.h:665
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6755
List * lappend(List *list, void *datum)
Definition: list.c:321
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:324
bool enable_partition_pruning
Definition: costsize.c:143
List * pathkeys
Definition: pathnodes.h:1158
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
Definition: createplan.c:5773
#define makeNode(_type_)
Definition: nodes.h:577
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
List * ppi_clauses
Definition: pathnodes.h:1105
static Plan * inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
Definition: createplan.c:1922
static int list_length(const List *l)
Definition: pg_list.h:169
PartitionPruneInfo * make_partition_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel, List *subpaths, List *partitioned_rels, List *prunequal)
Definition: partprune.c:230
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:422
struct Plan * lefttree
Definition: plannodes.h:144
List * partitioned_rels
Definition: pathnodes.h:1403
List * targetlist
Definition: plannodes.h:142
#define elog(elevel,...)
Definition: elog.h:214
bool parallel_safe
Definition: plannodes.h:136
#define CP_EXACT_TLIST
Definition: createplan.c:68
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:5179
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:5869

◆ create_bitmap_scan_plan()

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

Definition at line 3032 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().

3036 {
3037  Index baserelid = best_path->path.parent->relid;
3038  Plan *bitmapqualplan;
3039  List *bitmapqualorig;
3040  List *indexquals;
3041  List *indexECs;
3042  List *qpqual;
3043  ListCell *l;
3044  BitmapHeapScan *scan_plan;
3045 
3046  /* it should be a base rel... */
3047  Assert(baserelid > 0);
3048  Assert(best_path->path.parent->rtekind == RTE_RELATION);
3049 
3050  /* Process the bitmapqual tree into a Plan tree and qual lists */
3051  bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
3052  &bitmapqualorig, &indexquals,
3053  &indexECs);
3054 
3055  if (best_path->path.parallel_aware)
3056  bitmap_subplan_mark_shared(bitmapqualplan);
3057 
3058  /*
3059  * The qpqual list must contain all restrictions not automatically handled
3060  * by the index, other than pseudoconstant clauses which will be handled
3061  * by a separate gating plan node. All the predicates in the indexquals
3062  * will be checked (either by the index itself, or by
3063  * nodeBitmapHeapscan.c), but if there are any "special" operators
3064  * involved then they must be added to qpqual. The upshot is that qpqual
3065  * must contain scan_clauses minus whatever appears in indexquals.
3066  *
3067  * This loop is similar to the comparable code in create_indexscan_plan(),
3068  * but with some differences because it has to compare the scan clauses to
3069  * stripped (no RestrictInfos) indexquals. See comments there for more
3070  * info.
3071  *
3072  * In normal cases simple equal() checks will be enough to spot duplicate
3073  * clauses, so we try that first. We next see if the scan clause is
3074  * redundant with any top-level indexqual by virtue of being generated
3075  * from the same EC. After that, try predicate_implied_by().
3076  *
3077  * Unlike create_indexscan_plan(), the predicate_implied_by() test here is
3078  * useful for getting rid of qpquals that are implied by index predicates,
3079  * because the predicate conditions are included in the "indexquals"
3080  * returned by create_bitmap_subplan(). Bitmap scans have to do it that
3081  * way because predicate conditions need to be rechecked if the scan
3082  * becomes lossy, so they have to be included in bitmapqualorig.
3083  */
3084  qpqual = NIL;
3085  foreach(l, scan_clauses)
3086  {
3087  RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
3088  Node *clause = (Node *) rinfo->clause;
3089 
3090  if (rinfo->pseudoconstant)
3091  continue; /* we may drop pseudoconstants here */
3092  if (list_member(indexquals, clause))
3093  continue; /* simple duplicate */
3094  if (rinfo->parent_ec && list_member_ptr(indexECs, rinfo->parent_ec))
3095  continue; /* derived from same EquivalenceClass */
3096  if (!contain_mutable_functions(clause) &&
3097  predicate_implied_by(list_make1(clause), indexquals, false))
3098  continue; /* provably implied by indexquals */
3099  qpqual = lappend(qpqual, rinfo);
3100  }
3101 
3102  /* Sort clauses into best execution order */
3103  qpqual = order_qual_clauses(root, qpqual);
3104 
3105  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3106  qpqual = extract_actual_clauses(qpqual, false);
3107 
3108  /*
3109  * When dealing with special operators, we will at this point have
3110  * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
3111  * 'em from bitmapqualorig, since there's no point in making the tests
3112  * twice.
3113  */
3114  bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
3115 
3116  /*
3117  * We have to replace any outer-relation variables with nestloop params in
3118  * the qpqual and bitmapqualorig expressions. (This was already done for
3119  * expressions attached to plan nodes in the bitmapqualplan tree.)
3120  */
3121  if (best_path->path.param_info)
3122  {
3123  qpqual = (List *)
3124  replace_nestloop_params(root, (Node *) qpqual);
3125  bitmapqualorig = (List *)
3126  replace_nestloop_params(root, (Node *) bitmapqualorig);
3127  }
3128 
3129  /* Finally ready to build the plan node */
3130  scan_plan = make_bitmap_heapscan(tlist,
3131  qpqual,
3132  bitmapqualplan,
3133  bitmapqualorig,
3134  baserelid);
3135 
3136  copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3137 
3138  return scan_plan;
3139 }
#define NIL
Definition: pg_list.h:65
Plan plan
Definition: plannodes.h:344
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4664
List * list_difference_ptr(const List *list1, const List *list2)
Definition: list.c:1102
bool pseudoconstant
Definition: pathnodes.h:1993
ParamPathInfo * param_info
Definition: pathnodes.h:1147
Definition: nodes.h:529
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5048
static Plan * create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual, List **qual, List **indexqual, List **indexECs)
Definition: createplan.c:3162
#define list_make1(x1)
Definition: pg_list.h:227
static BitmapHeapScan * make_bitmap_heapscan(List *qptlist, List *qpqual, Plan *lefttree, List *bitmapqualorig, Index scanrelid)
Definition: createplan.c:5356
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
bool list_member(const List *list, const void *datum)
Definition: list.c:613
EquivalenceClass * parent_ec
Definition: pathnodes.h:2019
RelOptInfo * parent
Definition: pathnodes.h:1144
Path * bitmapqual
Definition: pathnodes.h:1280
#define lfirst_node(type, lc)
Definition: pg_list.h:193
Index relid
Definition: pathnodes.h:693
List * lappend(List *list, void *datum)
Definition: list.c:321
static void bitmap_subplan_mark_shared(Plan *plan)
Definition: createplan.c:5211
Expr * clause
Definition: pathnodes.h:1985
unsigned int Index
Definition: c.h:475
RTEKind rtekind
Definition: pathnodes.h:695
bool list_member_ptr(const List *list, const void *datum)
Definition: list.c:634
#define Assert(condition)
Definition: c.h:738
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:422
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:647
bool parallel_aware
Definition: pathnodes.h:1149
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 3162 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().

3164 {
3165  Plan *plan;
3166 
3167  if (IsA(bitmapqual, BitmapAndPath))
3168  {
3169  BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
3170  List *subplans = NIL;
3171  List *subquals = NIL;
3172  List *subindexquals = NIL;
3173  List *subindexECs = NIL;
3174  ListCell *l;
3175 
3176  /*
3177  * There may well be redundant quals among the subplans, since a
3178  * top-level WHERE qual might have gotten used to form several
3179  * different index quals. We don't try exceedingly hard to eliminate
3180  * redundancies, but we do eliminate obvious duplicates by using
3181  * list_concat_unique.
3182  */
3183  foreach(l, apath->bitmapquals)
3184  {
3185  Plan *subplan;
3186  List *subqual;
3187  List *subindexqual;
3188  List *subindexEC;
3189 
3190  subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
3191  &subqual, &subindexqual,
3192  &subindexEC);
3193  subplans = lappend(subplans, subplan);
3194  subquals = list_concat_unique(subquals, subqual);
3195  subindexquals = list_concat_unique(subindexquals, subindexqual);
3196  /* Duplicates in indexECs aren't worth getting rid of */
3197  subindexECs = list_concat(subindexECs, subindexEC);
3198  }
3199  plan = (Plan *) make_bitmap_and(subplans);
3200  plan->startup_cost = apath->path.startup_cost;
3201  plan->total_cost = apath->path.total_cost;
3202  plan->plan_rows =
3203  clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
3204  plan->plan_width = 0; /* meaningless */
3205  plan->parallel_aware = false;
3206  plan->parallel_safe = apath->path.parallel_safe;
3207  *qual = subquals;
3208  *indexqual = subindexquals;
3209  *indexECs = subindexECs;
3210  }
3211  else if (IsA(bitmapqual, BitmapOrPath))
3212  {
3213  BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
3214  List *subplans = NIL;
3215  List *subquals = NIL;
3216  List *subindexquals = NIL;
3217  bool const_true_subqual = false;
3218  bool const_true_subindexqual = false;
3219  ListCell *l;
3220 
3221  /*
3222  * Here, we only detect qual-free subplans. A qual-free subplan would
3223  * cause us to generate "... OR true ..." which we may as well reduce
3224  * to just "true". We do not try to eliminate redundant subclauses
3225  * because (a) it's not as likely as in the AND case, and (b) we might
3226  * well be working with hundreds or even thousands of OR conditions,
3227  * perhaps from a long IN list. The performance of list_append_unique
3228  * would be unacceptable.
3229  */
3230  foreach(l, opath->bitmapquals)
3231  {
3232  Plan *subplan;
3233  List *subqual;
3234  List *subindexqual;
3235  List *subindexEC;
3236 
3237  subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
3238  &subqual, &subindexqual,
3239  &subindexEC);
3240  subplans = lappend(subplans, subplan);
3241  if (subqual == NIL)
3242  const_true_subqual = true;
3243  else if (!const_true_subqual)
3244  subquals = lappend(subquals,
3245  make_ands_explicit(subqual));
3246  if (subindexqual == NIL)
3247  const_true_subindexqual = true;
3248  else if (!const_true_subindexqual)
3249  subindexquals = lappend(subindexquals,
3250  make_ands_explicit(subindexqual));
3251  }
3252 
3253  /*
3254  * In the presence of ScalarArrayOpExpr quals, we might have built
3255  * BitmapOrPaths with just one subpath; don't add an OR step.
3256  */
3257  if (list_length(subplans) == 1)
3258  {
3259  plan = (Plan *) linitial(subplans);
3260  }
3261  else
3262  {
3263  plan = (Plan *) make_bitmap_or(subplans);
3264  plan->startup_cost = opath->path.startup_cost;
3265  plan->total_cost = opath->path.total_cost;
3266  plan->plan_rows =
3267  clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
3268  plan->plan_width = 0; /* meaningless */
3269  plan->parallel_aware = false;
3270  plan->parallel_safe = opath->path.parallel_safe;
3271  }
3272 
3273  /*
3274  * If there were constant-TRUE subquals, the OR reduces to constant
3275  * TRUE. Also, avoid generating one-element ORs, which could happen
3276  * due to redundancy elimination or ScalarArrayOpExpr quals.
3277  */
3278  if (const_true_subqual)
3279  *qual = NIL;
3280  else if (list_length(subquals) <= 1)
3281  *qual = subquals;
3282  else
3283  *qual = list_make1(make_orclause(subquals));
3284  if (const_true_subindexqual)
3285  *indexqual = NIL;
3286  else if (list_length(subindexquals) <= 1)
3287  *indexqual = subindexquals;
3288  else
3289  *indexqual = list_make1(make_orclause(subindexquals));
3290  *indexECs = NIL;
3291  }
3292  else if (IsA(bitmapqual, IndexPath))
3293  {
3294  IndexPath *ipath = (IndexPath *) bitmapqual;
3295  IndexScan *iscan;
3296  List *subquals;
3297  List *subindexquals;
3298  List *subindexECs;
3299  ListCell *l;
3300 
3301  /* Use the regular indexscan plan build machinery... */
3302  iscan = castNode(IndexScan,
3303  create_indexscan_plan(root, ipath,
3304  NIL, NIL, false));
3305  /* then convert to a bitmap indexscan */
3306  plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
3307  iscan->indexid,
3308  iscan->indexqual,
3309  iscan->indexqualorig);
3310  /* and set its cost/width fields appropriately */
3311  plan->startup_cost = 0.0;
3312  plan->total_cost = ipath->indextotalcost;
3313  plan->plan_rows =
3314  clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
3315  plan->plan_width = 0; /* meaningless */
3316  plan->parallel_aware = false;
3317  plan->parallel_safe = ipath->path.parallel_safe;
3318  /* Extract original index clauses, actual index quals, relevant ECs */
3319  subquals = NIL;
3320  subindexquals = NIL;
3321  subindexECs = NIL;
3322  foreach(l, ipath->indexclauses)
3323  {
3324  IndexClause *iclause = (IndexClause *) lfirst(l);
3325  RestrictInfo *rinfo = iclause->rinfo;
3326 
3327  Assert(!rinfo->pseudoconstant);
3328  subquals = lappend(subquals, rinfo->clause);
3329  subindexquals = list_concat(subindexquals,
3330  get_actual_clauses(iclause->indexquals));
3331  if (rinfo->parent_ec)
3332  subindexECs = lappend(subindexECs, rinfo->parent_ec);
3333  }
3334  /* We can add any index predicate conditions, too */
3335  foreach(l, ipath->indexinfo->indpred)
3336  {
3337  Expr *pred = (Expr *) lfirst(l);
3338 
3339  /*
3340  * We know that the index predicate must have been implied by the
3341  * query condition as a whole, but it may or may not be implied by
3342  * the conditions that got pushed into the bitmapqual. Avoid
3343  * generating redundant conditions.
3344  */
3345  if (!predicate_implied_by(list_make1(pred), subquals, false))
3346  {
3347  subquals = lappend(subquals, pred);
3348  subindexquals = lappend(subindexquals, pred);
3349  }
3350  }
3351  *qual = subquals;
3352  *indexqual = subindexquals;
3353  *indexECs = subindexECs;
3354  }
3355  else
3356  {
3357  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
3358  plan = NULL; /* keep compiler quiet */
3359  }
3360 
3361  return plan;
3362 }
#define NIL
Definition: pg_list.h:65
double plan_rows
Definition: plannodes.h:129
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
Path path
Definition: pathnodes.h:1206
IndexOptInfo * indexinfo
Definition: pathnodes.h:1207
Index scanrelid
Definition: plannodes.h:345
#define castNode(_type_, nodeptr)
Definition: nodes.h:598
double tuples
Definition: pathnodes.h:705
bool pseudoconstant
Definition: pathnodes.h:1993
List * indexqualorig
Definition: plannodes.h:407
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:399
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
List * indexclauses
Definition: pathnodes.h:1208
static Plan * create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual, List **qual, List **indexqual, List **indexECs)
Definition: createplan.c:3162
Selectivity bitmapselectivity
Definition: pathnodes.h:1293
Expr * make_orclause(List *orclauses)
Definition: makefuncs.c:651
static BitmapAnd * make_bitmap_and(List *bitmapplans)
Definition: createplan.c:5623
Oid indexid
Definition: plannodes.h:405
List * bitmapquals
Definition: pathnodes.h:1292
List * bitmapquals
Definition: pathnodes.h:1305
#define list_make1(x1)
Definition: pg_list.h:227
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
Cost indextotalcost
Definition: pathnodes.h:1212
Cost startup_cost
Definition: pathnodes.h:1155
Scan scan
Definition: plannodes.h:404
EquivalenceClass * parent_ec
Definition: pathnodes.h:2019
RelOptInfo * parent
Definition: pathnodes.h:1144
Selectivity indexselectivity
Definition: pathnodes.h:1213
struct RestrictInfo * rinfo
Definition: pathnodes.h:1253
Cost startup_cost
Definition: plannodes.h:123
bool parallel_aware
Definition: plannodes.h:135
Selectivity bitmapselectivity
Definition: pathnodes.h:1306
List * indexqual
Definition: plannodes.h:406
List * indexquals
Definition: pathnodes.h:1254
List * lappend(List *list, void *datum)
Definition: list.c:321
Expr * clause
Definition: pathnodes.h:1985
Expr * make_ands_explicit(List *andclauses)
Definition: makefuncs.c:707
static BitmapOr * make_bitmap_or(List *bitmapplans)
Definition: createplan.c:5638
Cost total_cost
Definition: pathnodes.h:1156
int plan_width
Definition: plannodes.h:130
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
bool parallel_safe
Definition: pathnodes.h:1150
static int list_length(const List *l)
Definition: pg_list.h:169
static BitmapIndexScan * make_bitmap_indexscan(Index scanrelid, Oid indexid, List *indexqual, List *indexqualorig)
Definition: createplan.c:5335
#define nodeTag(nodeptr)
Definition: nodes.h:534
#define elog(elevel,...)
Definition: elog.h:214
Cost total_cost
Definition: plannodes.h:124
bool parallel_safe
Definition: plannodes.h:136
List * indpred
Definition: pathnodes.h:844
double clamp_row_est(double nrows)
Definition: costsize.c:190
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:2859
List * list_concat_unique(List *list1, const List *list2)
Definition: list.c:1237

◆ create_ctescan_plan()

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

Definition at line 3647 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().

3649 {
3650  CteScan *scan_plan;
3651  Index scan_relid = best_path->parent->relid;
3652  RangeTblEntry *rte;
3653  SubPlan *ctesplan = NULL;
3654  int plan_id;
3655  int cte_param_id;
3656  PlannerInfo *cteroot;
3657  Index levelsup;
3658  int ndx;
3659  ListCell *lc;
3660 
3661  Assert(scan_relid > 0);
3662  rte = planner_rt_fetch(scan_relid, root);
3663  Assert(rte->rtekind == RTE_CTE);
3664  Assert(!rte->self_reference);
3665 
3666  /*
3667  * Find the referenced CTE, and locate the SubPlan previously made for it.
3668  */
3669  levelsup = rte->ctelevelsup;
3670  cteroot = root;
3671  while (levelsup-- > 0)
3672  {
3673  cteroot = cteroot->parent_root;
3674  if (!cteroot) /* shouldn't happen */
3675  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3676  }
3677 
3678  /*
3679  * Note: cte_plan_ids can be shorter than cteList, if we are still working
3680  * on planning the CTEs (ie, this is a side-reference from another CTE).
3681  * So we mustn't use forboth here.
3682  */
3683  ndx = 0;
3684  foreach(lc, cteroot->parse->cteList)
3685  {
3686  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
3687 
3688  if (strcmp(cte->ctename, rte->ctename) == 0)
3689  break;
3690  ndx++;
3691  }
3692  if (lc == NULL) /* shouldn't happen */
3693  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
3694  if (ndx >= list_length(cteroot->cte_plan_ids))
3695  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3696  plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
3697  Assert(plan_id > 0);
3698  foreach(lc, cteroot->init_plans)
3699  {
3700  ctesplan = (SubPlan *) lfirst(lc);
3701  if (ctesplan->plan_id == plan_id)
3702  break;
3703  }
3704  if (lc == NULL) /* shouldn't happen */
3705  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3706 
3707  /*
3708  * We need the CTE param ID, which is the sole member of the SubPlan's
3709  * setParam list.
3710  */
3711  cte_param_id = linitial_int(ctesplan->setParam);
3712 
3713  /* Sort clauses into best execution order */
3714  scan_clauses = order_qual_clauses(root, scan_clauses);
3715 
3716  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3717  scan_clauses = extract_actual_clauses(scan_clauses, false);
3718 
3719  /* Replace any outer-relation variables with nestloop params */
3720  if (best_path->param_info)
3721  {
3722  scan_clauses = (List *)
3723  replace_nestloop_params(root, (Node *) scan_clauses);
3724  }
3725 
3726  scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
3727  plan_id, cte_param_id);
3728 
3729  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3730 
3731  return scan_plan;
3732 }
Plan plan
Definition: plannodes.h:344
Query * parse
Definition: pathnodes.h:179
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4664
int plan_id
Definition: primnodes.h:709
PlannerInfo * parent_root
Definition: pathnodes.h:185
ParamPathInfo * param_info
Definition: pathnodes.h:1147
Definition: nodes.h:529
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5048
static int list_nth_int(const List *list, int n)
Definition: pg_list.h:288
#define linitial_int(l)
Definition: pg_list.h:196
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
#define ERROR
Definition: elog.h:43
Scan scan
Definition: plannodes.h:555
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:373
RelOptInfo * parent
Definition: pathnodes.h:1144
Index relid
Definition: pathnodes.h:693
static CteScan * make_ctescan(List *qptlist, List *qpqual, Index scanrelid, int ctePlanId, int cteParam)
Definition: createplan.c:5473
List * cte_plan_ids
Definition: pathnodes.h:261
bool self_reference
Definition: parsenodes.h:1081
unsigned int Index
Definition: c.h:475
List * init_plans
Definition: pathnodes.h:259
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
List * setParam
Definition: primnodes.h:727
static int list_length(const List *l)
Definition: pg_list.h:169
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:422
Index ctelevelsup
Definition: parsenodes.h:1080
RTEKind rtekind
Definition: parsenodes.h:976
List * cteList
Definition: parsenodes.h:135
char * ctename
Definition: parsenodes.h:1079
#define elog(elevel,...)
Definition: elog.h:214
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 4021 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().

4023 {
4024  CustomScan *cplan;
4025  RelOptInfo *rel = best_path->path.parent;
4026  List *custom_plans = NIL;
4027  ListCell *lc;
4028 
4029  /* Recursively transform child paths. */
4030  foreach(lc, best_path->custom_paths)
4031  {
4032  Plan *plan = create_plan_recurse(root, (Path *) lfirst(lc),
4033  CP_EXACT_TLIST);
4034 
4035  custom_plans = lappend(custom_plans, plan);
4036  }
4037 
4038  /*
4039  * Sort clauses into the best execution order, although custom-scan
4040  * provider can reorder them again.
4041  */
4042  scan_clauses = order_qual_clauses(root, scan_clauses);
4043 
4044  /*
4045  * Invoke custom plan provider to create the Plan node represented by the
4046  * CustomPath.
4047  */
4048  cplan = castNode(CustomScan,
4049  best_path->methods->PlanCustomPath(root,
4050  rel,
4051  best_path,
4052  tlist,
4053  scan_clauses,
4054  custom_plans));
4055 
4056  /*
4057  * Copy cost data from Path to Plan; no need to make custom-plan providers
4058  * do this
4059  */
4060  copy_generic_path_info(&cplan->scan.plan, &best_path->path);
4061 
4062  /* Likewise, copy the relids that are represented by this custom scan */
4063  cplan->custom_relids = best_path->path.parent->relids;
4064 
4065  /*
4066  * Replace any outer-relation variables with nestloop params in the qual
4067  * and custom_exprs expressions. We do this last so that the custom-plan
4068  * provider doesn't have to be involved. (Note that parts of custom_exprs
4069  * could have come from join clauses, so doing this beforehand on the
4070  * scan_clauses wouldn't work.) We assume custom_scan_tlist contains no
4071  * such variables.
4072  */
4073  if (best_path->path.param_info)
4074  {
4075  cplan->scan.plan.qual = (List *)
4076  replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
4077  cplan->custom_exprs = (List *)
4078  replace_nestloop_params(root, (Node *) cplan->custom_exprs);
4079  }
4080 
4081  return cplan;
4082 }
#define NIL
Definition: pg_list.h:65
List * qual
Definition: plannodes.h:143
Plan plan
Definition: plannodes.h:344
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4664
List * custom_paths
Definition: pathnodes.h:1379
#define castNode(_type_, nodeptr)
Definition: nodes.h:598
ParamPathInfo * param_info
Definition: pathnodes.h:1147
Definition: nodes.h:529
List * custom_exprs
Definition: plannodes.h:646
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5048
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:372
const struct CustomPathMethods * methods
Definition: pathnodes.h:1381
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
RelOptInfo * parent
Definition: pathnodes.h:1144
Relids relids
Definition: pathnodes.h:665
List * lappend(List *list, void *datum)
Definition: list.c:321
Scan scan
Definition: plannodes.h:642
struct Plan *(* PlanCustomPath)(PlannerInfo *root, RelOptInfo *rel, struct CustomPath *best_path, List *tlist, List *clauses, List *custom_plans)
Definition: extensible.h:93
#define lfirst(lc)
Definition: pg_list.h:190
Bitmapset * custom_relids
Definition: plannodes.h:649
#define CP_EXACT_TLIST
Definition: createplan.c:68
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 3877 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().

3879 {
3880  ForeignScan *scan_plan;
3881  RelOptInfo *rel = best_path->path.parent;
3882  Index scan_relid = rel->relid;
3883  Oid rel_oid = InvalidOid;
3884  Plan *outer_plan = NULL;
3885 
3886  Assert(rel->fdwroutine != NULL);
3887 
3888  /* transform the child path if any */
3889  if (best_path->fdw_outerpath)
3890  outer_plan = create_plan_recurse(root, best_path->fdw_outerpath,
3891  CP_EXACT_TLIST);
3892 
3893  /*
3894  * If we're scanning a base relation, fetch its OID. (Irrelevant if
3895  * scanning a join relation.)
3896  */
3897  if (scan_relid > 0)
3898  {
3899  RangeTblEntry *rte;
3900 
3901  Assert(rel->rtekind == RTE_RELATION);
3902  rte = planner_rt_fetch(scan_relid, root);
3903  Assert(rte->rtekind == RTE_RELATION);
3904  rel_oid = rte->relid;
3905  }
3906 
3907  /*
3908  * Sort clauses into best execution order. We do this first since the FDW
3909  * might have more info than we do and wish to adjust the ordering.
3910  */
3911  scan_clauses = order_qual_clauses(root, scan_clauses);
3912 
3913  /*
3914  * Let the FDW perform its processing on the restriction clauses and
3915  * generate the plan node. Note that the FDW might remove restriction
3916  * clauses that it intends to execute remotely, or even add more (if it
3917  * has selected some join clauses for remote use but also wants them
3918  * rechecked locally).
3919  */
3920  scan_plan = rel->fdwroutine->GetForeignPlan(root, rel, rel_oid,
3921  best_path,
3922  tlist, scan_clauses,
3923  outer_plan);
3924 
3925  /* Copy cost data from Path to Plan; no need to make FDW do this */
3926  copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3927 
3928  /* Copy foreign server OID; likewise, no need to make FDW do this */
3929  scan_plan->fs_server = rel->serverid;
3930 
3931  /*
3932  * Likewise, copy the relids that are represented by this foreign scan. An
3933  * upper rel doesn't have relids set, but it covers all the base relations
3934  * participating in the underlying scan, so use root's all_baserels.
3935  */
3936  if (rel->reloptkind == RELOPT_UPPER_REL)
3937  scan_plan->fs_relids = root->all_baserels;
3938  else
3939  scan_plan->fs_relids = best_path->path.parent->relids;
3940 
3941  /*
3942  * If this is a foreign join, and to make it valid to push down we had to
3943  * assume that the current user is the same as some user explicitly named
3944  * in the query, mark the finished plan as depending on the current user.
3945  */
3946  if (rel->useridiscurrent)
3947  root->glob->dependsOnRole = true;
3948 
3949  /*
3950  * Replace any outer-relation variables with nestloop params in the qual,
3951  * fdw_exprs and fdw_recheck_quals expressions. We do this last so that
3952  * the FDW doesn't have to be involved. (Note that parts of fdw_exprs or
3953  * fdw_recheck_quals could have come from join clauses, so doing this
3954  * beforehand on the scan_clauses wouldn't work.) We assume
3955  * fdw_scan_tlist contains no such variables.
3956  */
3957  if (best_path->path.param_info)
3958  {
3959  scan_plan->scan.plan.qual = (List *)
3960  replace_nestloop_params(root, (Node *) scan_plan->scan.plan.qual);
3961  scan_plan->fdw_exprs = (List *)
3962  replace_nestloop_params(root, (Node *) scan_plan->fdw_exprs);
3963  scan_plan->fdw_recheck_quals = (List *)
3965  (Node *) scan_plan->fdw_recheck_quals);
3966  }
3967 
3968  /*
3969  * If rel is a base relation, detect whether any system columns are
3970  * requested from the rel. (If rel is a join relation, rel->relid will be
3971  * 0, but there can be no Var with relid 0 in the rel's targetlist or the
3972  * restriction clauses, so we skip this in that case. Note that any such
3973  * columns in base relations that were joined are assumed to be contained
3974  * in fdw_scan_tlist.) This is a bit of a kluge and might go away
3975  * someday, so we intentionally leave it out of the API presented to FDWs.
3976  */
3977  scan_plan->fsSystemCol = false;
3978  if (scan_relid > 0)
3979  {
3980  Bitmapset *attrs_used = NULL;
3981  ListCell *lc;
3982  int i;
3983 
3984  /*
3985  * First, examine all the attributes needed for joins or final output.
3986  * Note: we must look at rel's targetlist, not the attr_needed data,
3987  * because attr_needed isn't computed for inheritance child rels.
3988  */
3989  pull_varattnos((Node *) rel->reltarget->exprs, scan_relid, &attrs_used);
3990 
3991  /* Add all the attributes used by restriction clauses. */
3992  foreach(lc, rel->baserestrictinfo)
3993  {
3994  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3995 
3996  pull_varattnos((Node *) rinfo->clause, scan_relid, &attrs_used);
3997  }
3998 
3999  /* Now, are any system columns requested from rel? */
4000  for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
4001  {
4003  {
4004  scan_plan->fsSystemCol = true;
4005  break;
4006  }
4007  }
4008 
4009  bms_free(attrs_used);
4010  }
4011 
4012  return scan_plan;
4013 }
GetForeignPlan_function GetForeignPlan
Definition: fdwapi.h:190
List * qual
Definition: plannodes.h:143
Plan plan
Definition: plannodes.h:344
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4664
Path * fdw_outerpath
Definition: pathnodes.h:1349
RelOptKind reloptkind
Definition: pathnodes.h:662
Oid fs_server
Definition: plannodes.h:616
List * baserestrictinfo
Definition: pathnodes.h:727
List * fdw_exprs
Definition: plannodes.h:617
ParamPathInfo * param_info
Definition: pathnodes.h:1147
Definition: nodes.h:529
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5048
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
bool useridiscurrent
Definition: pathnodes.h:716
unsigned int Oid
Definition: postgres_ext.h:31
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:219
bool dependsOnRole
Definition: pathnodes.h:141
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:372
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
Relids all_baserels
Definition: pathnodes.h:227
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:373
RelOptInfo * parent
Definition: pathnodes.h:1144
PlannerGlobal * glob
Definition: pathnodes.h:181
struct FdwRoutine * fdwroutine
Definition: pathnodes.h:718
Relids relids
Definition: pathnodes.h:665
List * fdw_recheck_quals
Definition: plannodes.h:620
Index relid
Definition: pathnodes.h:693
Expr * clause
Definition: pathnodes.h:1985
Oid serverid
Definition: pathnodes.h:714
List * exprs
Definition: pathnodes.h:1074
unsigned int Index
Definition: c.h:475
RTEKind rtekind
Definition: pathnodes.h:695
#define InvalidOid
Definition: postgres_ext.h:36
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
RTEKind rtekind
Definition: parsenodes.h:976
int i
#define CP_EXACT_TLIST
Definition: createplan.c:68
Definition: pg_list.h:50
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
struct PathTarget * reltarget
Definition: pathnodes.h:676
bool fsSystemCol
Definition: plannodes.h:622
Bitmapset * fs_relids
Definition: plannodes.h:621

◆ create_functionscan_plan()

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

Definition at line 3517 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().

3519 {
3520  FunctionScan *scan_plan;
3521  Index scan_relid = best_path->parent->relid;
3522  RangeTblEntry *rte;
3523  List *functions;
3524 
3525  /* it should be a function base rel... */
3526  Assert(scan_relid > 0);
3527  rte = planner_rt_fetch(scan_relid, root);
3528  Assert(rte->rtekind == RTE_FUNCTION);
3529  functions = rte->functions;
3530 
3531  /* Sort clauses into best execution order */
3532  scan_clauses = order_qual_clauses(root, scan_clauses);
3533 
3534  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3535  scan_clauses = extract_actual_clauses(scan_clauses, false);
3536 
3537  /* Replace any outer-relation variables with nestloop params */
3538  if (best_path->param_info)
3539  {
3540  scan_clauses = (List *)
3541  replace_nestloop_params(root, (Node *) scan_clauses);
3542  /* The function expressions could contain nestloop params, too */
3543  functions = (List *) replace_nestloop_params(root, (Node *) functions);
3544  }
3545 
3546  scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
3547  functions, rte->funcordinality);
3548 
3549  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3550 
3551  return scan_plan;
3552 }
Plan plan
Definition: plannodes.h:344
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4664
ParamPathInfo * param_info
Definition: pathnodes.h:1147
Definition: nodes.h:529
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5048
bool funcordinality
Definition: parsenodes.h:1064
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:373
RelOptInfo * parent
Definition: pathnodes.h:1144
Index relid
Definition: pathnodes.h:693
unsigned int Index
Definition: c.h:475
static FunctionScan * make_functionscan(List *qptlist, List *qpqual, Index scanrelid, List *functions, bool funcordinality)
Definition: createplan.c:5414
#define Assert(condition)
Definition: c.h:738
List * functions
Definition: parsenodes.h:1063
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:422
static const struct fns functions
Definition: regcomp.c:298
RTEKind rtekind
Definition: parsenodes.h:976
Definition: pg_list.h:50

◆ create_gather_merge_plan()

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

Definition at line 1762 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(), PlannerInfo::glob, Plan::lefttree, make_sort(), 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().

1763 {
1764  GatherMerge *gm_plan;
1765  Plan *subplan;
1766  List *pathkeys = best_path->path.pathkeys;
1767  List *tlist = build_path_tlist(root, &best_path->path);
1768 
1769  /* As with Gather, it's best to project away columns in the workers. */
1770  subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1771 
1772  /* Create a shell for a GatherMerge plan. */
1773  gm_plan = makeNode(GatherMerge);
1774  gm_plan->plan.targetlist = tlist;
1775  gm_plan->num_workers = best_path->num_workers;
1776  copy_generic_path_info(&gm_plan->plan, &best_path->path);
1777 
1778  /* Assign the rescan Param. */
1779  gm_plan->rescan_param = assign_special_exec_param(root);
1780 
1781  /* Gather Merge is pointless with no pathkeys; use Gather instead. */
1782  Assert(pathkeys != NIL);
1783 
1784  /* Compute sort column info, and adjust subplan's tlist as needed */
1785  subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1786  best_path->subpath->parent->relids,
1787  gm_plan->sortColIdx,
1788  false,
1789  &gm_plan->numCols,
1790  &gm_plan->sortColIdx,
1791  &gm_plan->sortOperators,
1792  &gm_plan->collations,
1793  &gm_plan->nullsFirst);
1794 
1795 
1796  /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1797  if (!pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys))
1798  subplan = (Plan *) make_sort(subplan, gm_plan->numCols,
1799  gm_plan->sortColIdx,
1800  gm_plan->sortOperators,
1801  gm_plan->collations,
1802  gm_plan->nullsFirst);
1803 
1804  /* Now insert the subplan under GatherMerge. */
1805  gm_plan->plan.lefttree = subplan;
1806 
1807  /* use parallel mode for parallel plans. */
1808  root->glob->parallelModeNeeded = true;
1809 
1810  return gm_plan;
1811 }
#define NIL
Definition: pg_list.h:65
Oid * collations
Definition: plannodes.h:908
AttrNumber * sortColIdx
Definition: plannodes.h:906
bool * nullsFirst
Definition: plannodes.h:909
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:372
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
bool parallelModeNeeded
Definition: pathnodes.h:145
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:777
RelOptInfo * parent
Definition: pathnodes.h:1144
PlannerGlobal * glob
Definition: pathnodes.h:181
Relids relids
Definition: pathnodes.h:665
Oid * sortOperators
Definition: plannodes.h:907
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:324
List * pathkeys
Definition: pathnodes.h:1158
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
Definition: createplan.c:5773
#define makeNode(_type_)
Definition: nodes.h:577
#define Assert(condition)
Definition: c.h:738
struct Plan * lefttree
Definition: plannodes.h:144
List * targetlist
Definition: plannodes.h:142
int num_workers
Definition: plannodes.h:902
int rescan_param
Definition: plannodes.h:903
#define CP_EXACT_TLIST
Definition: createplan.c:68
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:5869

◆ create_gather_plan()

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

Definition at line 1726 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().

1727 {
1728  Gather *gather_plan;
1729  Plan *subplan;
1730  List *tlist;
1731 
1732  /*
1733  * Although the Gather node can project, we prefer to push down such work
1734  * to its child node, so demand an exact tlist from the child.
1735  */
1736  subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1737 
1738  tlist = build_path_tlist(root, &best_path->path);
1739 
1740  gather_plan = make_gather(tlist,
1741  NIL,
1742  best_path->num_workers,
1744  best_path->single_copy,
1745  subplan);
1746 
1747  copy_generic_path_info(&gather_plan->plan, &best_path->path);
1748 
1749  /* use parallel mode for parallel plans. */
1750  root->glob->parallelModeNeeded = true;
1751 
1752  return gather_plan;
1753 }
#define NIL
Definition: pg_list.h:65
bool single_copy
Definition: pathnodes.h:1496
int assign_special_exec_param(PlannerInfo *root)
Definition: paramassign.c:584
int num_workers
Definition: pathnodes.h:1497
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:372
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
bool parallelModeNeeded
Definition: pathnodes.h:145
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:777
Plan plan
Definition: plannodes.h:886
PlannerGlobal * glob
Definition: pathnodes.h:181
Path * subpath
Definition: pathnodes.h:1495
static Gather * make_gather(List *qptlist, List *qpqual, int nworkers, int rescan_param, bool single_copy, Plan *subplan)
Definition: createplan.c:6621
#define CP_EXACT_TLIST
Definition: createplan.c:68
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 958 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().

960 {
961  Plan *gplan;
962  Plan *splan;
963 
964  Assert(gating_quals);
965 
966  /*
967  * We might have a trivial Result plan already. Stacking one Result atop
968  * another is silly, so if that applies, just discard the input plan.
969  * (We're assuming its targetlist is uninteresting; it should be either
970  * the same as the result of build_path_tlist, or a simplified version.)
971  */
972  splan = plan;
973  if (IsA(plan, Result))
974  {
975  Result *rplan = (Result *) plan;
976 
977  if (rplan->plan.lefttree == NULL &&
978  rplan->resconstantqual == NULL)
979  splan = NULL;
980  }
981 
982  /*
983  * Since we need a Result node anyway, always return the path's requested
984  * tlist; that's never a wrong choice, even if the parent node didn't ask
985  * for CP_EXACT_TLIST.
986  */
987  gplan = (Plan *) make_result(build_path_tlist(root, path),
988  (Node *) gating_quals,
989  splan);
990 
991  /*
992  * Notice that we don't change cost or size estimates when doing gating.
993  * The costs of qual eval were already included in the subplan's cost.
994  * Leaving the size alone amounts to assuming that the gating qual will
995  * succeed, which is the conservative estimate for planning upper queries.
996  * We certainly don't want to assume the output size is zero (unless the
997  * gating qual is actually constant FALSE, and that case is dealt with in
998  * clausesel.c). Interpolating between the two cases is silly, because it
999  * doesn't reflect what will really happen at runtime, and besides which
1000  * in most cases we have only a very bad idea of the probability of the
1001  * gating qual being true.
1002  */
1003  copy_plan_costsize(gplan, plan);
1004 
1005  /* Gating quals could be unsafe, so better use the Path's safety flag */
1006  gplan->parallel_safe = path->parallel_safe;
1007 
1008  return gplan;
1009 }
Plan plan
Definition: plannodes.h:188
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
Definition: nodes.h:529
Node * resconstantqual
Definition: plannodes.h:189
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:777
static void copy_plan_costsize(Plan *dest, Plan *src)
Definition: createplan.c:5157
static SPIPlanPtr splan
Definition: regress.c:259
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6755
#define Assert(condition)
Definition: c.h:738
bool parallel_safe
Definition: pathnodes.h:1150
struct Plan * lefttree
Definition: plannodes.h:144
bool parallel_safe
Definition: plannodes.h:136

◆ create_group_plan()

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

Definition at line 2043 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().

2044 {
2045  Group *plan;
2046  Plan *subplan;
2047  List *tlist;
2048  List *quals;
2049 
2050  /*
2051  * Group can project, so no need to be terribly picky about child tlist,
2052  * but we do need grouping columns to be available
2053  */
2054  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2055 
2056  tlist = build_path_tlist(root, &best_path->path);
2057 
2058  quals = order_qual_clauses(root, best_path->qual);
2059 
2060  plan = make_group(tlist,
2061  quals,
2062  list_length(best_path->groupClause),
2064  subplan->targetlist),
2065  extract_grouping_ops(best_path->groupClause),
2067  subplan->targetlist),
2068  subplan);
2069 
2070  copy_generic_path_info(&plan->plan, (Path *) best_path);
2071 
2072  return plan;
2073 }
List * qual
Definition: pathnodes.h:1673
Oid * extract_grouping_collations(List *groupClause, List *tlist)
Definition: tlist.c:511
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5048
Oid * extract_grouping_ops(List *groupClause)
Definition: tlist.c:485
AttrNumber * extract_grouping_cols(List *groupClause, List *tlist)
Definition: tlist.c:536
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:372
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
static Group * make_group(List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations, Plan *lefttree)
Definition: createplan.c:6436
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:777
Path path
Definition: pathnodes.h:1670
List * groupClause
Definition: pathnodes.h:1672
#define CP_LABEL_TLIST
Definition: createplan.c:70
Path * subpath
Definition: pathnodes.h:1671
Plan plan
Definition: plannodes.h:795
static int list_length(const List *l)
Definition: pg_list.h:169
List * targetlist
Definition: plannodes.h:142
Definition: pg_list.h:50

◆ create_group_result_plan()

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

Definition at line 1448 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().

1449 {
1450  Result *plan;
1451  List *tlist;
1452  List *quals;
1453 
1454  tlist = build_path_tlist(root, &best_path->path);
1455 
1456  /* best_path->quals is just bare clauses */
1457  quals = order_qual_clauses(root, best_path->quals);
1458 
1459  plan = make_result(tlist, (Node *) quals, NULL);
1460 
1461  copy_generic_path_info(&plan->plan, (Path *) best_path);
1462 
1463  return plan;
1464 }
Plan plan
Definition: plannodes.h:188
Definition: nodes.h:529
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5048
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:777
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6755
Definition: pg_list.h:50

◆ create_groupingsets_plan()

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

Definition at line 2204 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, CP_SMALL_TLIST, create_plan_recurse(), extract_grouping_collations(), extract_grouping_ops(), for_each_cell, get_sortgroupclause_tle(), Query::groupClause, RollupData::groupClause, PlannerInfo::grouping_map, Query::groupingSets, RollupData::gsets, INHKIND_NONE, PlannerInfo::inhTargetKind, RollupData::is_hashed, lappend(), Plan::lefttree, lfirst, linitial, list_length(), list_second_cell(), 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().

2205 {
2206  Agg *plan;
2207  Plan *subplan;
2208  List *rollups = best_path->rollups;
2209  AttrNumber *grouping_map;
2210  int maxref;
2211  List *chain;
2212  ListCell *lc;
2213  int flags;
2214 
2215  /* Shouldn't get here without grouping sets */
2216  Assert(root->parse->groupingSets);
2217  Assert(rollups != NIL);
2218 
2219  /*
2220  * Agg can project, so no need to be terribly picky about child tlist, but
2221  * we do need grouping columns to be available. We are a bit more careful
2222  * with hash aggregate, where we explicitly request small tlist to
2223  * minimize I/O needed for spilling (we can't be sure spilling won't be
2224  * necessary, so we just do it every time).
2225  */
2226  flags = CP_LABEL_TLIST;
2227 
2228  /* ensure small tlist for hash aggregate */
2229  if (best_path->aggstrategy == AGG_HASHED)
2230  flags |= CP_SMALL_TLIST;
2231 
2232  subplan = create_plan_recurse(root, best_path->subpath, flags);
2233 
2234  /*
2235  * Compute the mapping from tleSortGroupRef to column index in the child's
2236  * tlist. First, identify max SortGroupRef in groupClause, for array
2237  * sizing.
2238  */
2239  maxref = 0;
2240  foreach(lc, root->parse->groupClause)
2241  {
2242  SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
2243 
2244  if (gc->tleSortGroupRef > maxref)
2245  maxref = gc->tleSortGroupRef;
2246  }
2247 
2248  grouping_map = (AttrNumber *) palloc0((maxref + 1) * sizeof(AttrNumber));
2249 
2250  /* Now look up the column numbers in the child's tlist */
2251  foreach(lc, root->parse->groupClause)
2252  {
2253  SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
2254  TargetEntry *tle = get_sortgroupclause_tle(gc, subplan->targetlist);
2255 
2256  grouping_map[gc->tleSortGroupRef] = tle->resno;
2257  }
2258 
2259  /*
2260  * During setrefs.c, we'll need the grouping_map to fix up the cols lists
2261  * in GroupingFunc nodes. Save it for setrefs.c to use.
2262  *
2263  * This doesn't work if we're in an inheritance subtree (see notes in
2264  * create_modifytable_plan). Fortunately we can't be because there would
2265  * never be grouping in an UPDATE/DELETE; but let's Assert that.
2266  */
2267  Assert(root->inhTargetKind == INHKIND_NONE);
2268  Assert(root->grouping_map == NULL);
2269  root->grouping_map = grouping_map;
2270 
2271  /*
2272  * Generate the side nodes that describe the other sort and group
2273  * operations besides the top one. Note that we don't worry about putting
2274  * accurate cost estimates in the side nodes; only the topmost Agg node's
2275  * costs will be shown by EXPLAIN.
2276  */
2277  chain = NIL;
2278  if (list_length(rollups) > 1)
2279  {
2280  bool is_first_sort = ((RollupData *) linitial(rollups))->is_hashed;
2281 
2282  for_each_cell(lc, rollups, list_second_cell(rollups))
2283  {
2284  RollupData *rollup = lfirst(lc);
2285  AttrNumber *new_grpColIdx;
2286  Plan *sort_plan = NULL;
2287  Plan *agg_plan;
2288  AggStrategy strat;
2289 
2290  new_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2291 
2292  if (!rollup->is_hashed && !is_first_sort)
2293  {
2294  sort_plan = (Plan *)
2296  new_grpColIdx,
2297  subplan);
2298  }
2299 
2300  if (!rollup->is_hashed)
2301  is_first_sort = false;
2302 
2303  if (rollup->is_hashed)
2304  strat = AGG_HASHED;
2305  else if (list_length(linitial(rollup->gsets)) == 0)
2306  strat = AGG_PLAIN;
2307  else
2308  strat = AGG_SORTED;
2309 
2310  agg_plan = (Plan *) make_agg(NIL,
2311  NIL,
2312  strat,
2314  list_length((List *) linitial(rollup->gsets)),
2315  new_grpColIdx,
2318  rollup->gsets,
2319  NIL,
2320  rollup->numGroups,
2321  best_path->transitionSpace,
2322  sort_plan);
2323 
2324  /*
2325  * Remove stuff we don't need to avoid bloating debug output.
2326  */
2327  if (sort_plan)
2328  {
2329  sort_plan->targetlist = NIL;
2330  sort_plan->lefttree = NULL;
2331  }
2332 
2333  chain = lappend(chain, agg_plan);
2334  }
2335  }
2336 
2337  /*
2338  * Now make the real Agg node
2339  */
2340  {
2341  RollupData *rollup = linitial(rollups);
2342  AttrNumber *top_grpColIdx;
2343  int numGroupCols;
2344 
2345  top_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2346 
2347  numGroupCols = list_length((List *) linitial(rollup->gsets));
2348 
2349  plan = make_agg(build_path_tlist(root, &best_path->path),
2350  best_path->qual,
2351  best_path->aggstrategy,
2353  numGroupCols,
2354  top_grpColIdx,
2357  rollup->gsets,
2358  chain,
2359  rollup->numGroups,
2360  best_path->transitionSpace,
2361  subplan);
2362 
2363  /* Copy cost data from Path to Plan */
2364  copy_generic_path_info(&plan->plan, &best_path->path);
2365  }
2366 
2367  return (Plan *) plan;
2368 }
#define NIL
Definition: pg_list.h:65
Query * parse
Definition: pathnodes.h:179
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:389
List * groupClause
Definition: pathnodes.h:1722
Oid * extract_grouping_collations(List *groupClause, List *tlist)
Definition: tlist.c:511
static AttrNumber * remap_groupColIdx(PlannerInfo *root, List *groupClause)
Definition: createplan.c:2166
#define for_each_cell(cell, lst, initcell)
Definition: pg_list.h:390
Index tleSortGroupRef
Definition: parsenodes.h:1257
bool is_hashed
Definition: pathnodes.h:1727
#define CP_SMALL_TLIST
Definition: createplan.c:69
List * groupingSets
Definition: parsenodes.h:150
Oid * extract_grouping_ops(List *groupClause)
Definition: tlist.c:485
AttrNumber * grouping_map
Definition: pathnodes.h:328
double numGroups
Definition: pathnodes.h:1725
uint64 transitionSpace
Definition: pathnodes.h:1741
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:372
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
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:6363
#define linitial(l)
Definition: pg_list.h:195
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:777
static ListCell * list_second_cell(const List *l)
Definition: pg_list.h:139
AttrNumber resno
Definition: primnodes.h:1408
static Sort * make_sort_from_groupcols(List *groupcls, AttrNumber *grpColIdx, Plan *lefttree)
Definition: createplan.c:6266
AggStrategy aggstrategy
Definition: pathnodes.h:1738
List * lappend(List *list, void *datum)
Definition: list.c:321
Plan plan
Definition: plannodes.h:818
void * palloc0(Size size)
Definition: mcxt.c:980
#define CP_LABEL_TLIST
Definition: createplan.c:70
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
InheritanceKind inhTargetKind
Definition: pathnodes.h:342
static int list_length(const List *l)
Definition: pg_list.h:169
struct Plan * lefttree
Definition: plannodes.h:144
List * targetlist
Definition: plannodes.h:142
AggStrategy
Definition: nodes.h:758
List * groupClause
Definition: parsenodes.h:148
Definition: plannodes.h:816
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
List * gsets
Definition: pathnodes.h:1723

◆ create_hashjoin_plan()

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

Definition at line 4475 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().

4477 {
4478  HashJoin *join_plan;
4479  Hash *hash_plan;
4480  Plan *outer_plan;
4481  Plan *inner_plan;
4482  List *tlist = build_path_tlist(root, &best_path->jpath.path);
4483  List *joinclauses;
4484  List *otherclauses;
4485  List *hashclauses;
4486  List *hashoperators = NIL;
4487  List *hashcollations = NIL;
4488  List *inner_hashkeys = NIL;
4489  List *outer_hashkeys = NIL;
4490  Oid skewTable = InvalidOid;
4491  AttrNumber skewColumn = InvalidAttrNumber;
4492  bool skewInherit = false;
4493  ListCell *lc;
4494 
4495  /*
4496  * HashJoin can project, so we don't have to demand exact tlists from the
4497  * inputs. However, it's best to request a small tlist from the inner
4498  * side, so that we aren't storing more data than necessary. Likewise, if
4499  * we anticipate batching, request a small tlist from the outer side so
4500  * that we don't put extra data in the outer batch files.
4501  */
4502  outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4503  (best_path->num_batches > 1) ? CP_SMALL_TLIST : 0);
4504 
4505  inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4506  CP_SMALL_TLIST);
4507 
4508  /* Sort join qual clauses into best execution order */
4509  joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4510  /* There's no point in sorting the hash clauses ... */
4511 
4512  /* Get the join qual clauses (in plain expression form) */
4513  /* Any pseudoconstant clauses are ignored here */
4514  if (IS_OUTER_JOIN(best_path->jpath.jointype))
4515  {
4516  extract_actual_join_clauses(joinclauses,
4517  best_path->jpath.path.parent->relids,
4518  &joinclauses, &otherclauses);
4519  }
4520  else
4521  {
4522  /* We can treat all clauses alike for an inner join */
4523  joinclauses = extract_actual_clauses(joinclauses, false);
4524  otherclauses = NIL;
4525  }
4526 
4527  /*
4528  * Remove the hashclauses from the list of join qual clauses, leaving the
4529  * list of quals that must be checked as qpquals.
4530  */
4531  hashclauses = get_actual_clauses(best_path->path_hashclauses);
4532  joinclauses = list_difference(joinclauses, hashclauses);
4533 
4534  /*
4535  * Replace any outer-relation variables with nestloop params. There
4536  * should not be any in the hashclauses.
4537  */
4538  if (best_path->jpath.path.param_info)
4539  {
4540  joinclauses = (List *)
4541  replace_nestloop_params(root, (Node *) joinclauses);
4542  otherclauses = (List *)
4543  replace_nestloop_params(root, (Node *) otherclauses);
4544  }
4545 
4546  /*
4547  * Rearrange hashclauses, if needed, so that the outer variable is always
4548  * on the left.
4549  */
4550  hashclauses = get_switched_clauses(best_path->path_hashclauses,
4551  best_path->jpath.outerjoinpath->parent->relids);
4552 
4553  /*
4554  * If there is a single join clause and we can identify the outer variable
4555  * as a simple column reference, supply its identity for possible use in
4556  * skew optimization. (Note: in principle we could do skew optimization
4557  * with multiple join clauses, but we'd have to be able to determine the
4558  * most common combinations of outer values, which we don't currently have
4559  * enough stats for.)
4560  */
4561  if (list_length(hashclauses) == 1)
4562  {
4563  OpExpr *clause = (OpExpr *) linitial(hashclauses);
4564  Node *node;
4565 
4566  Assert(is_opclause(clause));
4567  node = (Node *) linitial(clause->args);
4568  if (IsA(node, RelabelType))
4569  node = (Node *) ((RelabelType *) node)->arg;
4570  if (IsA(node, Var))
4571  {
4572  Var *var = (Var *) node;
4573  RangeTblEntry *rte;
4574 
4575  rte = root->simple_rte_array[var->varno];
4576  if (rte->rtekind == RTE_RELATION)
4577  {
4578  skewTable = rte->relid;
4579  skewColumn = var->varattno;
4580  skewInherit = rte->inh;
4581  }
4582  }
4583  }
4584 
4585  /*
4586  * Collect hash related information. The hashed expressions are
4587  * deconstructed into outer/inner expressions, so they can be computed
4588  * separately (inner expressions are used to build the hashtable via Hash,
4589  * outer expressions to perform lookups of tuples from HashJoin's outer
4590  * plan in the hashtable). Also collect operator information necessary to
4591  * build the hashtable.
4592  */
4593  foreach(lc, hashclauses)
4594  {
4595  OpExpr *hclause = lfirst_node(OpExpr, lc);
4596 
4597  hashoperators = lappend_oid(hashoperators, hclause->opno);
4598  hashcollations = lappend_oid(hashcollations, hclause->inputcollid);
4599  outer_hashkeys = lappend(outer_hashkeys, linitial(hclause->args));
4600  inner_hashkeys = lappend(inner_hashkeys, lsecond(hclause->args));
4601  }
4602 
4603  /*
4604  * Build the hash node and hash join node.
4605  */
4606  hash_plan = make_hash(inner_plan,
4607  inner_hashkeys,
4608  skewTable,
4609  skewColumn,
4610  skewInherit);
4611 
4612  /*
4613  * Set Hash node's startup & total costs equal to total cost of input
4614  * plan; this only affects EXPLAIN display not decisions.
4615  */
4616  copy_plan_costsize(&hash_plan->plan, inner_plan);
4617  hash_plan->plan.startup_cost = hash_plan->plan.total_cost;
4618 
4619  /*
4620  * If parallel-aware, the executor will also need an estimate of the total
4621  * number of rows expected from all participants so that it can size the
4622  * shared hash table.
4623  */
4624  if (best_path->jpath.path.parallel_aware)
4625  {
4626  hash_plan->plan.parallel_aware = true;
4627  hash_plan->rows_total = best_path->inner_rows_total;
4628  }
4629 
4630  join_plan = make_hashjoin(tlist,
4631  joinclauses,
4632  otherclauses,
4633  hashclauses,
4634  hashoperators,
4635  hashcollations,
4636  outer_hashkeys,
4637  outer_plan,
4638  (Plan *) hash_plan,
4639  best_path->jpath.jointype,
4640  best_path->jpath.inner_unique);
4641 
4642  copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4643 
4644  return join_plan;
4645 }
double rows_total
Definition: plannodes.h:935
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
JoinPath jpath
Definition: pathnodes.h:1599
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4664
static List * get_switched_clauses(List *clauses, Relids outerrelids)
Definition: createplan.c:4971
int num_batches
Definition: pathnodes.h:1601
void extract_actual_join_clauses(List *restrictinfo_list, Relids joinrelids, List **joinquals, List **otherquals)
Definition: restrictinfo.c:449
Path * innerjoinpath
Definition: pathnodes.h:1526
static Hash * make_hash(Plan *lefttree, List *hashkeys, Oid skewTable, AttrNumber skewColumn, bool skewInherit)
Definition: createplan.c:5709
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:745
ParamPathInfo * param_info
Definition: pathnodes.h:1147
#define CP_SMALL_TLIST
Definition: createplan.c:69
Definition: nodes.h:529
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:399
AttrNumber varattno
Definition: primnodes.h:186
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5048
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:181
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
double inner_rows_total
Definition: pathnodes.h:1602
#define lsecond(l)
Definition: pg_list.h:200
Join join
Definition: plannodes.h:742
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:372
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
#define linitial(l)
Definition: pg_list.h:195
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:777
List * joinrestrictinfo
Definition: pathnodes.h:1528
RelOptInfo * parent
Definition: pathnodes.h:1144
#define lfirst_node(type, lc)
Definition: pg_list.h:193
Cost startup_cost
Definition: plannodes.h:123
static void copy_plan_costsize(Plan *dest, Plan *src)
Definition: createplan.c:5157
bool parallel_aware
Definition: plannodes.h:135
Relids relids
Definition: pathnodes.h:665
List * lappend(List *list, void *datum)
Definition: list.c:321
RangeTblEntry ** simple_rte_array
Definition: pathnodes.h:211
Index varno
Definition: primnodes.h:184
Path * outerjoinpath
Definition: pathnodes.h:1525
#define InvalidOid
Definition: postgres_ext.h:36
Path path
Definition: pathnodes.h:1518
#define Assert(condition)
Definition: c.h:738
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:5678
static int list_length(const List *l)
Definition: pg_list.h:169
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:422
Oid inputcollid
Definition: primnodes.h:521
bool inner_unique
Definition: pathnodes.h:1522
List * list_difference(const List *list1, const List *list2)
Definition: list.c:1076
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:976
Plan plan
Definition: plannodes.h:924
JoinType jointype
Definition: pathnodes.h:1520
Cost total_cost
Definition: plannodes.h:124
bool parallel_aware
Definition: pathnodes.h:1149
List * path_hashclauses
Definition: pathnodes.h:1600
Oid opno
Definition: primnodes.h:516
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:63
List * args
Definition: primnodes.h:522
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
Plan plan
Definition: plannodes.h:683

◆ create_incrementalsort_plan()

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

Definition at line 2016 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().

2018 {
2019  IncrementalSort *plan;
2020  Plan *subplan;
2021 
2022  /* See comments in create_sort_plan() above */
2023  subplan = create_plan_recurse(root, best_path->spath.subpath,
2024  flags | CP_SMALL_TLIST);
2025  plan = make_incrementalsort_from_pathkeys(subplan,
2026  best_path->spath.path.pathkeys,
2027  IS_OTHER_REL(best_path->spath.subpath->parent) ?
2028  best_path->spath.path.parent->relids : NULL,
2029  best_path->nPresortedCols);
2030 
2031  copy_generic_path_info(&plan->sort.plan, (Path *) best_path);
2032 
2033  return plan;
2034 }
Path * subpath
Definition: pathnodes.h:1648
Plan plan
Definition: plannodes.h:769
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:653
#define CP_SMALL_TLIST
Definition: createplan.c:69
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:372
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
RelOptInfo * parent
Definition: pathnodes.h:1144
static IncrementalSort * make_incrementalsort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids, int nPresortedCols)
Definition: createplan.c:6183
Relids relids
Definition: pathnodes.h:665
List * pathkeys
Definition: pathnodes.h:1158
Path path
Definition: pathnodes.h:1647

◆ create_indexscan_plan()

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

Definition at line 2859 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().

2864 {
2865  Scan *scan_plan;
2866  List *indexclauses = best_path->indexclauses;
2867  List *indexorderbys = best_path->indexorderbys;
2868  Index baserelid = best_path->path.parent->relid;
2869  Oid indexoid = best_path->indexinfo->indexoid;
2870  List *qpqual;
2871  List *stripped_indexquals;
2872  List *fixed_indexquals;
2873  List *fixed_indexorderbys;
2874  List *indexorderbyops = NIL;
2875  ListCell *l;
2876 
2877  /* it should be a base rel... */
2878  Assert(baserelid > 0);
2879  Assert(best_path->path.parent->rtekind == RTE_RELATION);
2880 
2881  /*
2882  * Extract the index qual expressions (stripped of RestrictInfos) from the
2883  * IndexClauses list, and prepare a copy with index Vars substituted for
2884  * table Vars. (This step also does replace_nestloop_params on the
2885  * fixed_indexquals.)
2886  */
2887  fix_indexqual_references(root, best_path,
2888  &stripped_indexquals,
2889  &fixed_indexquals);
2890 
2891  /*
2892  * Likewise fix up index attr references in the ORDER BY expressions.
2893  */
2894  fixed_indexorderbys = fix_indexorderby_references(root, best_path);
2895 
2896  /*
2897  * The qpqual list must contain all restrictions not automatically handled
2898  * by the index, other than pseudoconstant clauses which will be handled
2899  * by a separate gating plan node. All the predicates in the indexquals
2900  * will be checked (either by the index itself, or by nodeIndexscan.c),
2901  * but if there are any "special" operators involved then they must be
2902  * included in qpqual. The upshot is that qpqual must contain
2903  * scan_clauses minus whatever appears in indexquals.
2904  *
2905  * is_redundant_with_indexclauses() detects cases where a scan clause is
2906  * present in the indexclauses list or is generated from the same
2907  * EquivalenceClass as some indexclause, and is therefore redundant with
2908  * it, though not equal. (The latter happens when indxpath.c prefers a
2909  * different derived equality than what generate_join_implied_equalities
2910  * picked for a parameterized scan's ppi_clauses.) Note that it will not
2911  * match to lossy index clauses, which is critical because we have to
2912  * include the original clause in qpqual in that case.
2913  *
2914  * In some situations (particularly with OR'd index conditions) we may
2915  * have scan_clauses that are not equal to, but are logically implied by,
2916  * the index quals; so we also try a predicate_implied_by() check to see
2917  * if we can discard quals that way. (predicate_implied_by assumes its
2918  * first input contains only immutable functions, so we have to check
2919  * that.)
2920  *
2921  * Note: if you change this bit of code you should also look at
2922  * extract_nonindex_conditions() in costsize.c.
2923  */
2924  qpqual = NIL;
2925  foreach(l, scan_clauses)
2926  {
2927  RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
2928 
2929  if (rinfo->pseudoconstant)
2930  continue; /* we may drop pseudoconstants here */
2931  if (is_redundant_with_indexclauses(rinfo, indexclauses))
2932  continue; /* dup or derived from same EquivalenceClass */
2933  if (!contain_mutable_functions((Node *) rinfo->clause) &&
2934  predicate_implied_by(list_make1(rinfo->clause), stripped_indexquals,
2935  false))
2936  continue; /* provably implied by indexquals */
2937  qpqual = lappend(qpqual, rinfo);
2938  }
2939 
2940  /* Sort clauses into best execution order */
2941  qpqual = order_qual_clauses(root, qpqual);
2942 
2943  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2944  qpqual = extract_actual_clauses(qpqual, false);
2945 
2946  /*
2947  * We have to replace any outer-relation variables with nestloop params in
2948  * the indexqualorig, qpqual, and indexorderbyorig expressions. A bit
2949  * annoying to have to do this separately from the processing in
2950  * fix_indexqual_references --- rethink this when generalizing the inner
2951  * indexscan support. But note we can't really do this earlier because
2952  * it'd break the comparisons to predicates above ... (or would it? Those
2953  * wouldn't have outer refs)
2954  */
2955  if (best_path->path.param_info)
2956  {
2957  stripped_indexquals = (List *)
2958  replace_nestloop_params(root, (Node *) stripped_indexquals);
2959  qpqual = (List *)
2960  replace_nestloop_params(root, (Node *) qpqual);
2961  indexorderbys = (List *)
2962  replace_nestloop_params(root, (Node *) indexorderbys);
2963  }
2964 
2965  /*
2966  * If there are ORDER BY expressions, look up the sort operators for their
2967  * result datatypes.
2968  */
2969  if (indexorderbys)
2970  {
2971  ListCell *pathkeyCell,
2972  *exprCell;
2973 
2974  /*
2975  * PathKey contains OID of the btree opfamily we're sorting by, but
2976  * that's not quite enough because we need the expression's datatype
2977  * to look up the sort operator in the operator family.
2978  */
2979  Assert(list_length(best_path->path.pathkeys) == list_length(indexorderbys));
2980  forboth(pathkeyCell, best_path->path.pathkeys, exprCell, indexorderbys)
2981  {
2982  PathKey *pathkey = (PathKey *) lfirst(pathkeyCell);
2983  Node *expr = (Node *) lfirst(exprCell);
2984  Oid exprtype = exprType(expr);
2985  Oid sortop;
2986 
2987  /* Get sort operator from opfamily */
2988  sortop = get_opfamily_member(pathkey->pk_opfamily,
2989  exprtype,
2990  exprtype,
2991  pathkey->pk_strategy);
2992  if (!OidIsValid(sortop))
2993  elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
2994  pathkey->pk_strategy, exprtype, exprtype, pathkey->pk_opfamily);
2995  indexorderbyops = lappend_oid(indexorderbyops, sortop);
2996  }
2997  }
2998 
2999  /* Finally ready to build the plan node */
3000  if (indexonly)
3001  scan_plan = (Scan *) make_indexonlyscan(tlist,
3002  qpqual,
3003  baserelid,
3004  indexoid,
3005  fixed_indexquals,
3006  fixed_indexorderbys,
3007  best_path->indexinfo->indextlist,
3008  best_path->indexscandir);
3009  else
3010  scan_plan = (Scan *) make_indexscan(tlist,
3011  qpqual,
3012  baserelid,
3013  indexoid,
3014  fixed_indexquals,
3015  stripped_indexquals,
3016  fixed_indexorderbys,
3017  indexorderbys,
3018  indexorderbyops,
3019  best_path->indexscandir);
3020 
3021  copy_generic_path_info(&scan_plan->plan, &best_path->path);
3022 
3023  return scan_plan;
3024 }
#define NIL
Definition: pg_list.h:65
Plan plan
Definition: plannodes.h:344
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4664
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:419
Path path
Definition: pathnodes.h:1206
IndexOptInfo * indexinfo
Definition: pathnodes.h:1207
List * indextlist
Definition: pathnodes.h:846
bool pseudoconstant
Definition: pathnodes.h:1993
ParamPathInfo * param_info
Definition: pathnodes.h:1147
Definition: nodes.h:529
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5048
List * indexclauses
Definition: pathnodes.h:1208
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
#define OidIsValid(objectId)
Definition: c.h:644
int pk_strategy
Definition: pathnodes.h:1043
#define list_make1(x1)
Definition: pg_list.h:227
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
#define ERROR
Definition: elog.h:43
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:5277
RelOptInfo * parent
Definition: pathnodes.h:1144
bool is_redundant_with_indexclauses(RestrictInfo *rinfo, List *indexclauses)
Definition: equivclass.c:2851
#define lfirst_node(type, lc)
Definition: pg_list.h:193
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:4796
Index relid
Definition: pathnodes.h:693
List * lappend(List *list, void *datum)
Definition: list.c:321
Expr * clause
Definition: pathnodes.h:1985
List * indexorderbys
Definition: pathnodes.h:1209
unsigned int Index
Definition: c.h:475
RTEKind rtekind
Definition: pathnodes.h:695
List * pathkeys
Definition: pathnodes.h:1158
static IndexOnlyScan * make_indexonlyscan(List *qptlist, List *qpqual, Index scanrelid, Oid indexid, List *indexqual, List *indexorderby, List *indextlist, ScanDirection indexscandir)
Definition: createplan.c:5308
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:169
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:422
Oid pk_opfamily
Definition: pathnodes.h:1042
ScanDirection indexscandir
Definition: pathnodes.h:1211
static void fix_indexqual_references(PlannerInfo *root, IndexPath *index_path, List **stripped_indexquals_p, List **fixed_indexquals_p)
Definition: createplan.c:4755
#define elog(elevel,...)
Definition: elog.h:214
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:647
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 1017 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().

1018 {
1019  Plan *plan;
1020  List *gating_clauses;
1021 
1022  switch (best_path->path.pathtype)
1023  {
1024  case T_MergeJoin:
1025  plan = (Plan *) create_mergejoin_plan(root,
1026  (MergePath *) best_path);
1027  break;
1028  case T_HashJoin:
1029  plan = (Plan *) create_hashjoin_plan(root,
1030  (HashPath *) best_path);
1031  break;
1032  case T_NestLoop:
1033  plan = (Plan *) create_nestloop_plan(root,
1034  (NestPath *) best_path);
1035  break;
1036  default:
1037  elog(ERROR, "unrecognized node type: %d",
1038  (int) best_path->path.pathtype);
1039  plan = NULL; /* keep compiler quiet */
1040  break;
1041  }
1042 
1043  /*
1044  * If there are any pseudoconstant clauses attached to this node, insert a
1045  * gating Result node that evaluates the pseudoconstants as one-time
1046  * quals.
1047  */
1048  gating_clauses = get_gating_quals(root, best_path->joinrestrictinfo);
1049  if (gating_clauses)
1050  plan = create_gating_plan(root, (Path *) best_path, plan,
1051  gating_clauses);
1052 
1053 #ifdef NOT_USED
1054 
1055  /*
1056  * * Expensive function pullups may have pulled local predicates * into
1057  * this path node. Put them in the qpqual of the plan node. * JMH,
1058  * 6/15/92
1059  */
1060  if (get_loc_restrictinfo(best_path) != NIL)
1061  set_qpqual((Plan) plan,
1062  list_concat(get_qpqual((Plan) plan),
1063  get_actual_clauses(get_loc_restrictinfo(best_path))));
1064 #endif
1065 
1066  return plan;
1067 }
#define NIL
Definition: pg_list.h:65
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:399
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
static NestLoop * create_nestloop_plan(PlannerInfo *root, NestPath *best_path)
Definition: createplan.c:4092
NodeTag pathtype
Definition: pathnodes.h:1142
#define ERROR
Definition: elog.h:43
List * joinrestrictinfo
Definition: pathnodes.h:1528
Path path
Definition: pathnodes.h:1518
static HashJoin * create_hashjoin_plan(PlannerInfo *root, HashPath *best_path)
Definition: createplan.c:4475
static Plan * create_gating_plan(PlannerInfo *root, Path *path, Plan *plan, List *gating_quals)
Definition: createplan.c:958
#define elog(elevel,...)
Definition: elog.h:214
static MergeJoin * create_mergejoin_plan(PlannerInfo *root, MergePath *best_path)
Definition: createplan.c:4168
Definition: pg_list.h:50
static List * get_gating_quals(PlannerInfo *root, List *quals)
Definition: createplan.c:938

◆ create_limit_plan()

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

Definition at line 2709 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().

2710 {
2711  Limit *plan;
2712  Plan *subplan;
2713  int numUniqkeys = 0;
2714  AttrNumber *uniqColIdx = NULL;
2715  Oid *uniqOperators = NULL;
2716  Oid *uniqCollations = NULL;
2717 
2718  /* Limit doesn't project, so tlist requirements pass through */
2719  subplan = create_plan_recurse(root, best_path->subpath, flags);
2720 
2721  /* Extract information necessary for comparing rows for WITH TIES. */
2722  if (best_path->limitOption == LIMIT_OPTION_WITH_TIES)
2723  {
2724  Query *parse = root->parse;
2725  ListCell *l;
2726 
2727  numUniqkeys = list_length(parse->sortClause);
2728  uniqColIdx = (AttrNumber *) palloc(numUniqkeys * sizeof(AttrNumber));
2729  uniqOperators = (Oid *) palloc(numUniqkeys * sizeof(Oid));
2730  uniqCollations = (Oid *) palloc(numUniqkeys * sizeof(Oid));
2731 
2732  numUniqkeys = 0;
2733  foreach(l, parse->sortClause)
2734  {
2735  SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
2736  TargetEntry *tle = get_sortgroupclause_tle(sortcl, parse->targetList);
2737 
2738  uniqColIdx[numUniqkeys] = tle->resno;
2739  uniqOperators[numUniqkeys] = sortcl->eqop;
2740  uniqCollations[numUniqkeys] = exprCollation((Node *) tle->expr);
2741  numUniqkeys++;
2742  }
2743  }
2744 
2745  plan = make_limit(subplan,
2746  best_path->limitOffset,
2747  best_path->limitCount,
2748  best_path->limitOption,
2749  numUniqkeys, uniqColIdx, uniqOperators, uniqCollations);
2750 
2751  copy_generic_path_info(&plan->plan, (Path *) best_path);
2752 
2753  return plan;
2754 }
Query * parse
Definition: pathnodes.h:179
LimitOption limitOption
Definition: pathnodes.h:1837
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:389
Plan plan
Definition: plannodes.h:982
Node * limitOffset
Definition: pathnodes.h:1835
List * sortClause
Definition: parsenodes.h:158
Definition: nodes.h:529
unsigned int Oid
Definition: postgres_ext.h:31
List * targetList
Definition: parsenodes.h:140
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:372
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
Path * subpath
Definition: pathnodes.h:1834
AttrNumber resno
Definition: primnodes.h:1408
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount, LimitOption limitOption, int uniqNumCols, AttrNumber *uniqColIdx, Oid *uniqOperators, Oid *uniqCollations)
Definition: createplan.c:6727
#define lfirst(lc)
Definition: pg_list.h:190
Expr * expr
Definition: primnodes.h:1407
static int list_length(const List *l)
Definition: pg_list.h:169
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:719
void * palloc(Size size)
Definition: mcxt.c:949
Node * limitCount
Definition: pathnodes.h:1836
int16 AttrNumber
Definition: attnum.h:21
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:648

◆ create_lockrows_plan()

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

Definition at line 2625 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().

2627 {
2628  LockRows *plan;
2629  Plan *subplan;
2630 
2631  /* LockRows doesn't project, so tlist requirements pass through */
2632  subplan = create_plan_recurse(root, best_path->subpath, flags);
2633 
2634  plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
2635 
2636  copy_generic_path_info(&plan->plan, (Path *) best_path);
2637 
2638  return plan;
2639 }
Plan plan
Definition: plannodes.h:968
List * rowMarks
Definition: pathnodes.h:1799
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:372
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
Path * subpath
Definition: pathnodes.h:1798
static LockRows * make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
Definition: createplan.c:6706

◆ create_material_plan()

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

Definition at line 1499 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().

1500 {
1501  Material *plan;
1502  Plan *subplan;
1503 
1504  /*
1505  * We don't want any excess columns in the materialized tuples, so request
1506  * a smaller tlist. Otherwise, since Material doesn't project, tlist
1507  * requirements pass through.
1508  */
1509  subplan = create_plan_recurse(root, best_path->subpath,
1510  flags | CP_SMALL_TLIST);
1511 
1512  plan = make_material(subplan);
1513 
1514  copy_generic_path_info(&plan->plan, (Path *) best_path);
1515 
1516  return plan;
1517 }
#define CP_SMALL_TLIST
Definition: createplan.c:69
Path * subpath
Definition: pathnodes.h:1456
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:372
static Material * make_material(Plan *lefttree)
Definition: createplan.c:6307
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
Plan plan
Definition: plannodes.h:760

◆ create_merge_append_plan()

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

Definition at line 1287 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, RELOPT_BASEREL, RelOptInfo::reloptkind, replace_nestloop_params(), Plan::righttree, sort(), MergeAppend::sortColIdx, MergeAppend::sortOperators, subpath(), and Plan::targetlist.

Referenced by create_plan_recurse().

1289 {
1290  MergeAppend *node = makeNode(MergeAppend);
1291  Plan *plan = &node->plan;
1292  List *tlist = build_path_tlist(root, &best_path->path);
1293  int orig_tlist_length = list_length(tlist);
1294  bool tlist_was_changed;
1295  List *pathkeys = best_path->path.pathkeys;
1296  List *subplans = NIL;
1297  ListCell *subpaths;
1298  RelOptInfo *rel = best_path->path.parent;
1299  PartitionPruneInfo *partpruneinfo = NULL;
1300 
1301  /*
1302  * We don't have the actual creation of the MergeAppend node split out
1303  * into a separate make_xxx function. This is because we want to run
1304  * prepare_sort_from_pathkeys on it before we do so on the individual
1305  * child plans, to make cross-checking the sort info easier.
1306  */
1307  copy_generic_path_info(plan, (Path *) best_path);
1308  plan->targetlist = tlist;
1309  plan->qual = NIL;
1310  plan->lefttree = NULL;
1311  plan->righttree = NULL;
1312  node->apprelids = rel->relids;
1313 
1314  /*
1315  * Compute sort column info, and adjust MergeAppend's tlist as needed.
1316  * Because we pass adjust_tlist_in_place = true, we may ignore the
1317  * function result; it must be the same plan node. However, we then need
1318  * to detect whether any tlist entries were added.
1319  */
1320  (void) prepare_sort_from_pathkeys(plan, pathkeys,
1321  best_path->path.parent->relids,
1322  NULL,
1323  true,
1324  &node->numCols,
1325  &node->sortColIdx,
1326  &node->sortOperators,
1327  &node->collations,
1328  &node->nullsFirst);
1329  tlist_was_changed = (orig_tlist_length != list_length(plan->targetlist));
1330 
1331  /*
1332  * Now prepare the child plans. We must apply prepare_sort_from_pathkeys
1333  * even to subplans that don't need an explicit sort, to make sure they
1334  * are returning the same sort key columns the MergeAppend expects.
1335  */
1336  foreach(subpaths, best_path->subpaths)
1337  {
1338  Path *subpath = (Path *) lfirst(subpaths);
1339  Plan *subplan;
1340  int numsortkeys;
1341  AttrNumber *sortColIdx;
1342  Oid *sortOperators;
1343  Oid *collations;
1344  bool *nullsFirst;
1345 
1346  /* Build the child plan */
1347  /* Must insist that all children return the same tlist */
1348  subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
1349 
1350  /* Compute sort column info, and adjust subplan's tlist as needed */
1351  subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1352  subpath->parent->relids,
1353  node->sortColIdx,
1354  false,
1355  &numsortkeys,
1356  &sortColIdx,
1357  &sortOperators,
1358  &collations,
1359  &nullsFirst);
1360 
1361  /*
1362  * Check that we got the same sort key information. We just Assert
1363  * that the sortops match, since those depend only on the pathkeys;
1364  * but it seems like a good idea to check the sort column numbers
1365  * explicitly, to ensure the tlists really do match up.
1366  */
1367  Assert(numsortkeys == node->numCols);
1368  if (memcmp(sortColIdx, node->sortColIdx,
1369  numsortkeys * sizeof(AttrNumber)) != 0)
1370  elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
1371  Assert(memcmp(sortOperators, node->sortOperators,
1372  numsortkeys * sizeof(Oid)) == 0);
1373  Assert(memcmp(collations, node->collations,
1374  numsortkeys * sizeof(Oid)) == 0);
1375  Assert(memcmp(nullsFirst, node->nullsFirst,
1376  numsortkeys * sizeof(bool)) == 0);
1377 
1378  /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1379  if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
1380  {
1381  Sort *sort = make_sort(subplan, numsortkeys,
1382  sortColIdx, sortOperators,
1383  collations, nullsFirst);
1384 
1385  label_sort_with_costsize(root, sort, best_path->limit_tuples);
1386  subplan = (Plan *) sort;
1387  }
1388 
1389  subplans = lappend(subplans, subplan);
1390  }
1391 
1392  /*
1393  * If any quals exist, they may be useful to perform further partition
1394  * pruning during execution. Gather information needed by the executor to
1395  * do partition pruning.
1396  */
1398  rel->reloptkind == RELOPT_BASEREL &&
1399  best_path->partitioned_rels != NIL)
1400  {
1401  List *prunequal;
1402 
1403  prunequal = extract_actual_clauses(rel->baserestrictinfo, false);
1404 
1405  if (best_path->path.param_info)
1406  {
1407  List *prmquals = best_path->path.param_info->ppi_clauses;
1408 
1409  prmquals = extract_actual_clauses(prmquals, false);
1410  prmquals = (List *) replace_nestloop_params(root,
1411  (Node *) prmquals);
1412 
1413  prunequal = list_concat(prunequal, prmquals);
1414  }
1415 
1416  if (prunequal != NIL)
1417  partpruneinfo = make_partition_pruneinfo(root, rel,
1418  best_path->subpaths,
1419  best_path->partitioned_rels,
1420  prunequal);
1421  }
1422 
1423  node->mergeplans = subplans;
1424  node->part_prune_info = partpruneinfo;
1425 
1426  /*
1427  * If prepare_sort_from_pathkeys added sort columns, but we were told to
1428  * produce either the exact tlist or a narrow tlist, we should get rid of
1429  * the sort columns again. We must inject a projection node to do so.
1430  */
1431  if (tlist_was_changed && (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST)))
1432  {
1433  tlist = list_truncate(list_copy(plan->targetlist), orig_tlist_length);
1434  return inject_projection_plan(plan, tlist, plan->parallel_safe);
1435  }
1436  else
1437  return plan;
1438 }
#define NIL
Definition: pg_list.h:65
List * qual
Definition: plannodes.h:143
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4664
RelOptKind reloptkind
Definition: pathnodes.h:662
Oid * collations
Definition: plannodes.h:281
List * baserestrictinfo
Definition: pathnodes.h:727
List * list_truncate(List *list, int new_size)
Definition: list.c:585
ParamPathInfo * param_info
Definition: pathnodes.h:1147
#define CP_SMALL_TLIST
Definition: createplan.c:69
List * list_copy(const List *oldlist)
Definition: list.c:1403
Definition: nodes.h:529
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
struct PartitionPruneInfo * part_prune_info
Definition: plannodes.h:284
List * partitioned_rels
Definition: pathnodes.h:1429
unsigned int Oid
Definition: postgres_ext.h:31
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:372
struct Plan * righttree
Definition: plannodes.h:145
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
#define ERROR
Definition: elog.h:43
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:777
RelOptInfo * parent
Definition: pathnodes.h:1144
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:195
Relids relids
Definition: pathnodes.h:665
List * lappend(List *list, void *datum)
Definition: list.c:321
Bitmapset * apprelids
Definition: plannodes.h:275
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:324
bool enable_partition_pruning
Definition: costsize.c:143
List * pathkeys
Definition: pathnodes.h:1158
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
Definition: createplan.c:5773
#define makeNode(_type_)
Definition: nodes.h:577
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
AttrNumber * sortColIdx
Definition: plannodes.h:279
List * ppi_clauses
Definition: pathnodes.h:1105
static Plan * inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
Definition: createplan.c:1922
static int list_length(const List *l)
Definition: pg_list.h:169
PartitionPruneInfo * make_partition_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel, List *subpaths, List *partitioned_rels, List *prunequal)
Definition: partprune.c:230
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:422
struct Plan * lefttree
Definition: plannodes.h:144
List * targetlist
Definition: plannodes.h:142
bool * nullsFirst
Definition: plannodes.h:282
List * mergeplans
Definition: plannodes.h:276
#define elog(elevel,...)
Definition: elog.h:214
bool parallel_safe
Definition: plannodes.h:136
Oid * sortOperators
Definition: plannodes.h:280
#define CP_EXACT_TLIST
Definition: createplan.c:68
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:5179
double limit_tuples
Definition: pathnodes.h:1431
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:5869

◆ create_mergejoin_plan()

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

Definition at line 4168 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().

4170 {
4171  MergeJoin *join_plan;
4172  Plan *outer_plan;
4173  Plan *inner_plan;
4174  List *tlist = build_path_tlist(root, &best_path->jpath.path);
4175  List *joinclauses;
4176  List *otherclauses;
4177  List *mergeclauses;
4178  List *outerpathkeys;
4179  List *innerpathkeys;
4180  int nClauses;
4181  Oid *mergefamilies;
4182  Oid *mergecollations;
4183  int *mergestrategies;
4184  bool *mergenullsfirst;
4185  PathKey *opathkey;
4186  EquivalenceClass *opeclass;
4187  int i;
4188  ListCell *lc;
4189  ListCell *lop;
4190  ListCell *lip;
4191  Path *outer_path = best_path->jpath.outerjoinpath;
4192  Path *inner_path = best_path->jpath.innerjoinpath;
4193 
4194  /*
4195  * MergeJoin can project, so we don't have to demand exact tlists from the
4196  * inputs. However, if we're intending to sort an input's result, it's
4197  * best to request a small tlist so we aren't sorting more data than
4198  * necessary.
4199  */
4200  outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4201  (best_path->outersortkeys != NIL) ? CP_SMALL_TLIST : 0);
4202 
4203  inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4204  (best_path->innersortkeys != NIL) ? CP_SMALL_TLIST : 0);
4205 
4206  /* Sort join qual clauses into best execution order */
4207  /* NB: do NOT reorder the mergeclauses */
4208  joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4209 
4210  /* Get the join qual clauses (in plain expression form) */
4211  /* Any pseudoconstant clauses are ignored here */
4212  if (IS_OUTER_JOIN(best_path->jpath.jointype))
4213  {
4214  extract_actual_join_clauses(joinclauses,
4215  best_path->jpath.path.parent->relids,
4216  &joinclauses, &otherclauses);
4217  }
4218  else
4219  {
4220  /* We can treat all clauses alike for an inner join */
4221  joinclauses = extract_actual_clauses(joinclauses, false);
4222  otherclauses = NIL;
4223  }
4224 
4225  /*
4226  * Remove the mergeclauses from the list of join qual clauses, leaving the
4227  * list of quals that must be checked as qpquals.
4228  */
4229  mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
4230  joinclauses = list_difference(joinclauses, mergeclauses);
4231 
4232  /*
4233  * Replace any outer-relation variables with nestloop params. There
4234  * should not be any in the mergeclauses.
4235  */
4236  if (best_path->jpath.path.param_info)
4237  {
4238  joinclauses = (List *)
4239  replace_nestloop_params(root, (Node *) joinclauses);
4240  otherclauses = (List *)
4241  replace_nestloop_params(root, (Node *) otherclauses);
4242  }
4243 
4244  /*
4245  * Rearrange mergeclauses, if needed, so that the outer variable is always
4246  * on the left; mark the mergeclause restrictinfos with correct
4247  * outer_is_left status.
4248  */
4249  mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
4250  best_path->jpath.outerjoinpath->parent->relids);
4251 
4252  /*
4253  * Create explicit sort nodes for the outer and inner paths if necessary.
4254  */
4255  if (best_path->outersortkeys)
4256  {
4257  Relids outer_relids = outer_path->parent->relids;
4258  Sort *sort = make_sort_from_pathkeys(outer_plan,
4259  best_path->outersortkeys,
4260  outer_relids);
4261 
4262  label_sort_with_costsize(root, sort, -1.0);
4263  outer_plan = (Plan *) sort;
4264  outerpathkeys = best_path->outersortkeys;
4265  }
4266  else
4267  outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
4268 
4269  if (best_path->innersortkeys)
4270  {
4271  Relids inner_relids = inner_path->parent->relids;
4272  Sort *sort = make_sort_from_pathkeys(inner_plan,
4273  best_path->innersortkeys,
4274  inner_relids);
4275 
4276  label_sort_with_costsize(root, sort, -1.0);
4277  inner_plan = (Plan *) sort;
4278  innerpathkeys = best_path->innersortkeys;
4279  }
4280  else
4281  innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
4282 
4283  /*
4284  * If specified, add a materialize node to shield the inner plan from the
4285  * need to handle mark/restore.
4286  */
4287  if (best_path->materialize_inner)
4288  {
4289  Plan *matplan = (Plan *) make_material(inner_plan);
4290 
4291  /*
4292  * We assume the materialize will not spill to disk, and therefore
4293  * charge just cpu_operator_cost per tuple. (Keep this estimate in
4294  * sync with final_cost_mergejoin.)
4295  */
4296  copy_plan_costsize(matplan, inner_plan);
4297  matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
4298 
4299  inner_plan = matplan;
4300  }
4301 
4302  /*
4303  * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
4304  * executor. The information is in the pathkeys for the two inputs, but
4305  * we need to be careful about the possibility of mergeclauses sharing a
4306  * pathkey, as well as the possibility that the inner pathkeys are not in
4307  * an order matching the mergeclauses.
4308  */
4309  nClauses = list_length(mergeclauses);
4310  Assert(nClauses == list_length(best_path->path_mergeclauses));
4311  mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
4312  mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
4313  mergestrategies = (int *) palloc(nClauses * sizeof(int));
4314  mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
4315 
4316  opathkey = NULL;
4317  opeclass = NULL;
4318  lop = list_head(outerpathkeys);
4319  lip = list_head(innerpathkeys);
4320  i = 0;
4321  foreach(lc, best_path->path_mergeclauses)
4322  {
4323  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
4324  EquivalenceClass *oeclass;
4325  EquivalenceClass *ieclass;
4326  PathKey *ipathkey = NULL;
4327  EquivalenceClass *ipeclass = NULL;
4328  bool first_inner_match = false;
4329 
4330  /* fetch outer/inner eclass from mergeclause */
4331  if (rinfo->outer_is_left)
4332  {
4333  oeclass = rinfo->left_ec;
4334  ieclass = rinfo->right_ec;
4335  }
4336  else
4337  {
4338  oeclass = rinfo->right_ec;
4339  ieclass = rinfo->left_ec;
4340  }
4341  Assert(oeclass != NULL);
4342  Assert(ieclass != NULL);
4343 
4344  /*
4345  * We must identify the pathkey elements associated with this clause
4346  * by matching the eclasses (which should give a unique match, since
4347  * the pathkey lists should be canonical). In typical cases the merge
4348  * clauses are one-to-one with the pathkeys, but when dealing with
4349  * partially redundant query conditions, things are more complicated.
4350  *
4351  * lop and lip reference the first as-yet-unmatched pathkey elements.
4352  * If they're NULL then all pathkey elements have been matched.
4353  *
4354  * The ordering of the outer pathkeys should match the mergeclauses,
4355  * by construction (see find_mergeclauses_for_outer_pathkeys()). There
4356  * could be more than one mergeclause for the same outer pathkey, but
4357  * no pathkey may be entirely skipped over.
4358  */
4359  if (oeclass != opeclass) /* multiple matches are not interesting */
4360  {
4361  /* doesn't match the current opathkey, so must match the next */
4362  if (lop == NULL)
4363  elog(ERROR, "outer pathkeys do not match mergeclauses");
4364  opathkey = (PathKey *) lfirst(lop);
4365  opeclass = opathkey->pk_eclass;
4366  lop = lnext(outerpathkeys, lop);
4367  if (oeclass != opeclass)
4368  elog(ERROR, "outer pathkeys do not match mergeclauses");
4369  }
4370 
4371  /*
4372  * The inner pathkeys likewise should not have skipped-over keys, but
4373  * it's possible for a mergeclause to reference some earlier inner
4374  * pathkey if we had redundant pathkeys. For example we might have
4375  * mergeclauses like "o.a = i.x AND o.b = i.y AND o.c = i.x". The
4376  * implied inner ordering is then "ORDER BY x, y, x", but the pathkey
4377  * mechanism drops the second sort by x as redundant, and this code
4378  * must cope.
4379  *
4380  * It's also possible for the implied inner-rel ordering to be like
4381  * "ORDER BY x, y, x DESC". We still drop the second instance of x as
4382  * redundant; but this means that the sort ordering of a redundant
4383  * inner pathkey should not be considered significant. So we must
4384  * detect whether this is the first clause matching an inner pathkey.
4385  */
4386  if (lip)
4387  {
4388  ipathkey = (PathKey *) lfirst(lip);
4389  ipeclass = ipathkey->pk_eclass;
4390  if (ieclass == ipeclass)
4391  {
4392  /* successful first match to this inner pathkey */
4393  lip = lnext(innerpathkeys, lip);
4394  first_inner_match = true;
4395  }
4396  }
4397  if (!first_inner_match)
4398  {
4399  /* redundant clause ... must match something before lip */
4400  ListCell *l2;
4401 
4402  foreach(l2, innerpathkeys)
4403  {
4404  if (l2 == lip)
4405  break;
4406  ipathkey = (PathKey *) lfirst(l2);
4407  ipeclass = ipathkey->pk_eclass;
4408  if (ieclass == ipeclass)
4409  break;
4410  }
4411  if (ieclass != ipeclass)
4412  elog(ERROR, "inner pathkeys do not match mergeclauses");
4413  }
4414 
4415  /*
4416  * The pathkeys should always match each other as to opfamily and
4417  * collation (which affect equality), but if we're considering a
4418  * redundant inner pathkey, its sort ordering might not match. In
4419  * such cases we may ignore the inner pathkey's sort ordering and use
4420  * the outer's. (In effect, we're lying to the executor about the
4421  * sort direction of this inner column, but it does not matter since
4422  * the run-time row comparisons would only reach this column when
4423  * there's equality for the earlier column containing the same eclass.
4424  * There could be only one value in this column for the range of inner
4425  * rows having a given value in the earlier column, so it does not
4426  * matter which way we imagine this column to be ordered.) But a
4427  * non-redundant inner pathkey had better match outer's ordering too.
4428  */
4429  if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
4430  opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation)
4431  elog(ERROR, "left and right pathkeys do not match in mergejoin");
4432  if (first_inner_match &&
4433  (opathkey->pk_strategy != ipathkey->pk_strategy ||
4434  opathkey->pk_nulls_first != ipathkey->pk_nulls_first))
4435  elog(ERROR, "left and right pathkeys do not match in mergejoin");
4436 
4437  /* OK, save info for executor */
4438  mergefamilies[i] = opathkey->pk_opfamily;
4439  mergecollations[i] = opathkey->pk_eclass->ec_collation;
4440  mergestrategies[i] = opathkey->pk_strategy;
4441  mergenullsfirst[i] = opathkey->pk_nulls_first;
4442  i++;
4443  }
4444 
4445  /*
4446  * Note: it is not an error if we have additional pathkey elements (i.e.,
4447  * lop or lip isn't NULL here). The input paths might be better-sorted
4448  * than we need for the current mergejoin.
4449  */
4450 
4451  /*
4452  * Now we can build the mergejoin node.
4453  */
4454  join_plan = make_mergejoin(tlist,
4455  joinclauses,
4456  otherclauses,
4457  mergeclauses,
4458  mergefamilies,
4459  mergecollations,
4460  mergestrategies,
4461  mergenullsfirst,
4462  outer_plan,
4463  inner_plan,
4464  best_path->jpath.jointype,
4465  best_path->jpath.inner_unique,
4466  best_path->skip_mark_restore);
4467 
4468  /* Costs of sort and material steps are included in path cost already */
4469  copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4470 
4471  return join_plan;
4472 }
#define NIL
Definition: pg_list.h:65
List * path_mergeclauses
Definition: pathnodes.h:1581
List * outersortkeys
Definition: pathnodes.h:1582
double plan_rows
Definition: plannodes.h:129
static Sort * make_sort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids)
Definition: createplan.c:6148
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4664
static List * get_switched_clauses(List *clauses, Relids outerrelids)
Definition: createplan.c:4971
bool materialize_inner
Definition: pathnodes.h:1585
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
void extract_actual_join_clauses(List *restrictinfo_list, Relids joinrelids, List **joinquals, List **otherquals)
Definition: restrictinfo.c:449
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:5732
Path * innerjoinpath
Definition: pathnodes.h:1526
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:745
ParamPathInfo * param_info
Definition: pathnodes.h:1147
#define CP_SMALL_TLIST
Definition: createplan.c:69
Definition: nodes.h:529
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:399
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5048
EquivalenceClass * right_ec
Definition: pathnodes.h:2034
unsigned int Oid
Definition: postgres_ext.h:31
int pk_strategy
Definition: pathnodes.h:1043
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:372
static Material * make_material(Plan *lefttree)
Definition: createplan.c:6307
bool skip_mark_restore
Definition: pathnodes.h:1584
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
bool pk_nulls_first
Definition: pathnodes.h:1044
#define ERROR
Definition: elog.h:43
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:777
List * joinrestrictinfo
Definition: pathnodes.h:1528
RelOptInfo * parent
Definition: pathnodes.h:1144
#define lfirst_node(type, lc)
Definition: pg_list.h:193
bool outer_is_left
Definition: pathnodes.h:2040
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:195
static void copy_plan_costsize(Plan *dest, Plan *src)
Definition: createplan.c:5157
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
Relids relids
Definition: pathnodes.h:665
double cpu_operator_cost
Definition: costsize.c:115
Path * outerjoinpath
Definition: pathnodes.h:1525
List * pathkeys
Definition: pathnodes.h:1158
Path path
Definition: pathnodes.h:1518
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
EquivalenceClass * pk_eclass
Definition: pathnodes.h:1041
static int list_length(const List *l)
Definition: pg_list.h:169
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:422
bool inner_unique
Definition: pathnodes.h:1522
List * list_difference(const List *list1, const List *list2)
Definition: list.c:1076
List * innersortkeys
Definition: pathnodes.h:1583
Oid pk_opfamily
Definition: pathnodes.h:1042
void * palloc(Size size)
Definition: mcxt.c:949
EquivalenceClass * left_ec
Definition: pathnodes.h:2033
Join join
Definition: plannodes.h:726
JoinType jointype
Definition: pathnodes.h:1520
#define elog(elevel,...)
Definition: elog.h:214
int i
Cost total_cost
Definition: plannodes.h:124
JoinPath jpath
Definition: pathnodes.h:1580
Definition: pg_list.h:50
static void label_sort_with_costsize(PlannerInfo *root, Sort *plan, double limit_tuples)
Definition: createplan.c:5179
Plan plan
Definition: plannodes.h:683

◆ create_minmaxagg_plan()

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

Definition at line 2377 of file createplan.c.

References Assert, build_path_tlist(), copy_generic_path_info(), create_plan(), INHKIND_NONE, PlannerInfo::inhTargetKind, 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().

2378 {
2379  Result *plan;
2380  List *tlist;
2381  ListCell *lc;
2382 
2383  /* Prepare an InitPlan for each aggregate's subquery. */
2384  foreach(lc, best_path->mmaggregates)
2385  {
2386  MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
2387  PlannerInfo *subroot = mminfo->subroot;
2388  Query *subparse = subroot->parse;
2389  Plan *plan;
2390 
2391  /*
2392  * Generate the plan for the subquery. We already have a Path, but we
2393  * have to convert it to a Plan and attach a LIMIT node above it.
2394  * Since we are entering a different planner context (subroot),
2395  * recurse to create_plan not create_plan_recurse.
2396  */
2397  plan = create_plan(subroot, mminfo->path);
2398 
2399  plan = (Plan *) make_limit(plan,
2400  subparse->limitOffset,
2401  subparse->limitCount,
2402  subparse->limitOption,
2403  0, NULL, NULL, NULL);
2404 
2405  /* Must apply correct cost/width data to Limit node */
2406  plan->startup_cost = mminfo->path->startup_cost;
2407  plan->total_cost = mminfo->pathcost;
2408  plan->plan_rows = 1;
2409  plan->plan_width = mminfo->path->pathtarget->width;
2410  plan->parallel_aware = false;
2411  plan->parallel_safe = mminfo->path->parallel_safe;
2412 
2413  /* Convert the plan into an InitPlan in the outer query. */
2414  SS_make_initplan_from_plan(root, subroot, plan, mminfo->param);
2415  }
2416 
2417  /* Generate the output plan --- basically just a Result */
2418  tlist = build_path_tlist(root, &best_path->path);
2419 
2420  plan = make_result(tlist, (Node *) best_path->quals, NULL);
2421 
2422  copy_generic_path_info(&plan->plan, (Path *) best_path);
2423 
2424  /*
2425  * During setrefs.c, we'll need to replace references to the Agg nodes
2426  * with InitPlan output params. (We can't just do that locally in the
2427  * MinMaxAgg node, because path nodes above here may have Agg references
2428  * as well.) Save the mmaggregates list to tell setrefs.c to do that.
2429  *
2430  * This doesn't work if we're in an inheritance subtree (see notes in
2431  * create_modifytable_plan). Fortunately we can't be because there would
2432  * never be aggregates in an UPDATE/DELETE; but let's Assert that.
2433  */
2434  Assert(root->inhTargetKind == INHKIND_NONE);
2435  Assert(root->minmax_aggs == NIL);
2436  root->minmax_aggs = best_path->mmaggregates;
2437 
2438  return plan;
2439 }
Node * limitOffset
Definition: parsenodes.h:160
#define NIL
Definition: pg_list.h:65
double plan_rows
Definition: plannodes.h:129
PathTarget * pathtarget
Definition: pathnodes.h:1145
Query * parse
Definition: pathnodes.h:179
Param * param
Definition: pathnodes.h:2329
Definition: nodes.h:529
List * minmax_aggs
Definition: pathnodes.h:329
List * quals
Definition: pathnodes.h:1751
Plan * create_plan(PlannerInfo *root, Path *best_path)
Definition: createplan.c:321
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:777
Cost startup_cost
Definition: pathnodes.h:1155
Node * limitCount
Definition: parsenodes.h:161
Cost startup_cost
Definition: plannodes.h:123
bool parallel_aware
Definition: plannodes.h:135
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount, LimitOption limitOption, int uniqNumCols, AttrNumber *uniqColIdx, Oid *uniqOperators, Oid *uniqCollations)
Definition: createplan.c:6727
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6755
LimitOption limitOption
Definition: parsenodes.h:162
List * mmaggregates
Definition: pathnodes.h:1750
int plan_width
Definition: plannodes.h:130
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
bool parallel_safe
Definition: pathnodes.h:1150
InheritanceKind inhTargetKind
Definition: pathnodes.h:342
void SS_make_initplan_from_plan(PlannerInfo *root, PlannerInfo *subroot, Plan *plan, Param *prm)
Definition: subselect.c:2892
Cost total_cost
Definition: plannodes.h:124
bool parallel_safe
Definition: plannodes.h:136
Definition: pg_list.h:50
PlannerInfo * subroot
Definition: pathnodes.h:2326

◆ create_modifytable_plan()

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

Definition at line 2648 of file createplan.c.

References apply_tlist_labeling(), ModifyTablePath::canSetTag, copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), ModifyTablePath::epqParam, forboth, lappend(), lfirst, make_modifytable(), NIL, 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::subpaths, ModifyTablePath::subroots, Plan::targetlist, and ModifyTablePath::withCheckOptionLists.

Referenced by create_plan_recurse().

2649 {
2650  ModifyTable *plan;
2651  List *subplans = NIL;
2652  ListCell *subpaths,
2653  *subroots;
2654 
2655  /* Build the plan for each input path */
2656  forboth(subpaths, best_path->subpaths,
2657  subroots, best_path->subroots)
2658  {
2659  Path *subpath = (Path *) lfirst(subpaths);
2660  PlannerInfo *subroot = (PlannerInfo *) lfirst(subroots);
2661  Plan *subplan;
2662 
2663  /*
2664  * In an inherited UPDATE/DELETE, reference the per-child modified
2665  * subroot while creating Plans from Paths for the child rel. This is
2666  * a kluge, but otherwise it's too hard to ensure that Plan creation
2667  * functions (particularly in FDWs) don't depend on the contents of
2668  * "root" matching what they saw at Path creation time. The main
2669  * downside is that creation functions for Plans that might appear
2670  * below a ModifyTable cannot expect to modify the contents of "root"
2671  * and have it "stick" for subsequent processing such as setrefs.c.
2672  * That's not great, but it seems better than the alternative.
2673  */
2674  subplan = create_plan_recurse(subroot, subpath, CP_EXACT_TLIST);
2675 
2676  /* Transfer resname/resjunk labeling, too, to keep executor happy */
2677  apply_tlist_labeling(subplan->targetlist, subroot->processed_tlist);
2678 
2679  subplans = lappend(subplans, subplan);
2680  }
2681 
2682  plan = make_modifytable(root,
2683  best_path->operation,
2684  best_path->canSetTag,
2685  best_path->nominalRelation,
2686  best_path->rootRelation,
2687  best_path->partColsUpdated,
2688  best_path->resultRelations,
2689  subplans,
2690  best_path->subroots,
2691  best_path->withCheckOptionLists,
2692  best_path->returningLists,
2693  best_path->rowMarks,
2694  best_path->onconflict,
2695  best_path->epqParam);
2696 
2697  copy_generic_path_info(&plan->plan, &best_path->path);
2698 
2699  return plan;
2700 }
#define NIL
Definition: pg_list.h:65
void apply_tlist_labeling(List *dest_tlist, List *src_tlist)
Definition: tlist.c:340
List * returningLists
Definition: pathnodes.h:1822
OnConflictExpr * onconflict
Definition: pathnodes.h:1824
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:419
Index nominalRelation
Definition: pathnodes.h:1815
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:372
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
bool partColsUpdated
Definition: pathnodes.h:1817
static ModifyTable * make_modifytable(PlannerInfo *root, CmdType operation, bool canSetTag, Index nominalRelation, Index rootRelation, bool partColsUpdated, List *resultRelations, List *subplans, List *subroots, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, int epqParam)
Definition: createplan.c:6795
List * lappend(List *list, void *datum)
Definition: list.c:321
#define lfirst(lc)
Definition: pg_list.h:190
Index rootRelation
Definition: pathnodes.h:1816
List * targetlist
Definition: plannodes.h:142
List * withCheckOptionLists
Definition: pathnodes.h:1821
CmdType operation
Definition: pathnodes.h:1813
List * resultRelations
Definition: pathnodes.h:1818
List * processed_tlist
Definition: pathnodes.h:325
#define CP_EXACT_TLIST
Definition: createplan.c:68
Definition: pg_list.h:50
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 3741 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().

3743 {
3744  NamedTuplestoreScan *scan_plan;
3745  Index scan_relid = best_path->parent->relid;
3746  RangeTblEntry *rte;
3747 
3748  Assert(scan_relid > 0);
3749  rte = planner_rt_fetch(scan_relid, root);
3751 
3752  /* Sort clauses into best execution order */
3753  scan_clauses = order_qual_clauses(root, scan_clauses);
3754 
3755  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3756  scan_clauses = extract_actual_clauses(scan_clauses, false);
3757 
3758  /* Replace any outer-relation variables with nestloop params */
3759  if (best_path->param_info)
3760  {
3761  scan_clauses = (List *)
3762  replace_nestloop_params(root, (Node *) scan_clauses);
3763  }
3764 
3765  scan_plan = make_namedtuplestorescan(tlist, scan_clauses, scan_relid,
3766  rte->enrname);
3767 
3768  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3769 
3770  return scan_plan;
3771 }
Plan plan
Definition: plannodes.h:344
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4664
ParamPathInfo * param_info
Definition: pathnodes.h:1147
Definition: nodes.h:529
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5048
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:373
RelOptInfo * parent
Definition: pathnodes.h:1144
char * enrname
Definition: parsenodes.h:1108
Index relid
Definition: pathnodes.h:693
static NamedTuplestoreScan * make_namedtuplestorescan(List *qptlist, List *qpqual, Index scanrelid, char *enrname)
Definition: createplan.c:5494
unsigned int Index
Definition: c.h:475
#define Assert(condition)
Definition: c.h:738
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:422
RTEKind rtekind
Definition: parsenodes.h:976
Definition: pg_list.h:50

◆ create_nestloop_plan()

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

Definition at line 4092 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, 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().

4094 {
4095  NestLoop *join_plan;
4096  Plan *outer_plan;
4097  Plan *inner_plan;
4098  List *tlist = build_path_tlist(root, &best_path->path);
4099  List *joinrestrictclauses = best_path->joinrestrictinfo;
4100  List *joinclauses;
4101  List *otherclauses;
4102  Relids outerrelids;
4103  List *nestParams;
4104  Relids saveOuterRels = root->curOuterRels;
4105 
4106  /* NestLoop can project, so no need to be picky about child tlists */
4107  outer_plan = create_plan_recurse(root, best_path->outerjoinpath, 0);
4108 
4109  /* For a nestloop, include outer relids in curOuterRels for inner side */
4110  root->curOuterRels = bms_union(root->curOuterRels,
4111  best_path->outerjoinpath->parent->relids);
4112 
4113  inner_plan = create_plan_recurse(root, best_path->innerjoinpath, 0);
4114 
4115  /* Restore curOuterRels */
4116  bms_free(root->curOuterRels);
4117  root->curOuterRels = saveOuterRels;
4118 
4119  /* Sort join qual clauses into best execution order */
4120  joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
4121 
4122  /* Get the join qual clauses (in plain expression form) */
4123  /* Any pseudoconstant clauses are ignored here */
4124  if (IS_OUTER_JOIN(best_path->jointype))
4125  {
4126  extract_actual_join_clauses(joinrestrictclauses,
4127  best_path->path.parent->relids,
4128  &joinclauses, &otherclauses);
4129  }
4130  else
4131  {
4132  /* We can treat all clauses alike for an inner join */
4133  joinclauses = extract_actual_clauses(joinrestrictclauses, false);
4134  otherclauses = NIL;
4135  }
4136 
4137  /* Replace any outer-relation variables with nestloop params */
4138  if (best_path->path.param_info)
4139  {
4140  joinclauses = (List *)
4141  replace_nestloop_params(root, (Node *) joinclauses);
4142  otherclauses = (List *)
4143  replace_nestloop_params(root, (Node *) otherclauses);
4144  }
4145 
4146  /*
4147  * Identify any nestloop parameters that should be supplied by this join
4148  * node, and remove them from root->curOuterParams.
4149  */
4150  outerrelids = best_path->outerjoinpath->parent->relids;
4151  nestParams = identify_current_nestloop_params(root, outerrelids);
4152 
4153  join_plan = make_nestloop(tlist,
4154  joinclauses,
4155  otherclauses,
4156  nestParams,
4157  outer_plan,
4158  inner_plan,
4159  best_path->jointype,
4160  best_path->inner_unique);
4161 
4162  copy_generic_path_info(&join_plan->join.plan, &best_path->path);
4163 
4164  return join_plan;
4165 }
#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:4664
Relids curOuterRels
Definition: pathnodes.h:357
void extract_actual_join_clauses(List *restrictinfo_list, Relids joinrelids, List **joinquals, List **otherquals)
Definition: restrictinfo.c:449
Path * innerjoinpath
Definition: pathnodes.h:1526
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:745
ParamPathInfo * param_info
Definition: pathnodes.h:1147
Definition: nodes.h:529
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5048
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:372
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5142
Join join
Definition: plannodes.h:702
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:777
List * joinrestrictinfo
Definition: pathnodes.h:1528
RelOptInfo * parent
Definition: pathnodes.h:1144
static NestLoop * make_nestloop(List *tlist, List *joinclauses, List *otherclauses, List *nestParams, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
Definition: createplan.c:5653
Relids relids
Definition: pathnodes.h:665
Path * outerjoinpath
Definition: pathnodes.h:1525
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
Path path
Definition: pathnodes.h:1518
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:422
bool inner_unique
Definition: pathnodes.h:1522
JoinType jointype
Definition: pathnodes.h:1520
Definition: pg_list.h:50
Plan plan
Definition: plannodes.h:683

◆ create_plan()

Plan* create_plan ( PlannerInfo root,
Path best_path 
)

Definition at line 321 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().

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

◆ create_plan_recurse()

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

Definition at line 372 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_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_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_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_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().

373 {
374  Plan *plan;
375 
376  /* Guard against stack overflow due to overly complex plans */
378 
379  switch (best_path->pathtype)
380  {
381  case T_SeqScan:
382  case T_SampleScan:
383  case T_IndexScan:
384  case T_IndexOnlyScan:
385  case T_BitmapHeapScan:
386  case T_TidScan:
387  case T_SubqueryScan:
388  case T_FunctionScan:
389  case T_TableFuncScan:
390  case T_ValuesScan:
391  case T_CteScan:
392  case T_WorkTableScan:
394  case T_ForeignScan:
395  case T_CustomScan:
396  plan = create_scan_plan(root, best_path, flags);
397  break;
398  case T_HashJoin:
399  case T_MergeJoin:
400  case T_NestLoop:
401  plan = create_join_plan(root,
402  (JoinPath *) best_path);
403  break;
404  case T_Append:
405  plan = create_append_plan(root,
406  (AppendPath *) best_path,
407  flags);
408  break;
409  case T_MergeAppend:
410  plan = create_merge_append_plan(root,
411  (MergeAppendPath *) best_path,
412  flags);
413  break;
414  case T_Result:
415  if (IsA(best_path, ProjectionPath))
416  {
417  plan = create_projection_plan(root,
418  (ProjectionPath *) best_path,
419  flags);
420  }
421  else if (IsA(best_path, MinMaxAggPath))
422  {
423  plan = (Plan *) create_minmaxagg_plan(root,
424  (MinMaxAggPath *) best_path);
425  }
426  else if (IsA(best_path, GroupResultPath))
427  {
428  plan = (Plan *) create_group_result_plan(root,
429  (GroupResultPath *) best_path);
430  }
431  else
432  {
433  /* Simple RTE_RESULT base relation */
434  Assert(IsA(best_path, Path));
435  plan = create_scan_plan(root, best_path, flags);
436  }
437  break;
438  case T_ProjectSet:
439  plan = (Plan *) create_project_set_plan(root,
440  (ProjectSetPath *) best_path);
441  break;
442  case T_Material:
443  plan = (Plan *) create_material_plan(root,
444  (MaterialPath *) best_path,
445  flags);
446  break;
447  case T_Unique:
448  if (IsA(best_path, UpperUniquePath))
449  {
450  plan = (Plan *) create_upper_unique_plan(root,
451  (UpperUniquePath *) best_path,
452  flags);
453  }
454  else
455  {
456  Assert(IsA(best_path, UniquePath));
457  plan = create_unique_plan(root,
458  (UniquePath *) best_path,
459  flags);
460  }
461  break;
462  case T_Gather:
463  plan = (Plan *) create_gather_plan(root,
464  (GatherPath *) best_path);
465  break;
466  case T_Sort:
467  plan = (Plan *) create_sort_plan(root,
468  (SortPath *) best_path,
469  flags);
470  break;
471  case T_IncrementalSort:
472  plan = (Plan *) create_incrementalsort_plan(root,
473  (IncrementalSortPath *) best_path,
474  flags);
475  break;
476  case T_Group:
477  plan = (<