PostgreSQL Source Code  git master
createplan.c File Reference
#include "postgres.h"
#include <limits.h>
#include <math.h>
#include "access/stratnum.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/paths.h"
#include "optimizer/placeholder.h"
#include "optimizer/plancat.h"
#include "optimizer/planmain.h"
#include "optimizer/planner.h"
#include "optimizer/predtest.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/subselect.h"
#include "optimizer/tlist.h"
#include "optimizer/var.h"
#include "parser/parse_clause.h"
#include "parser/parsetree.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 */
 

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)
 
static Plancreate_merge_append_plan (PlannerInfo *root, MergeAppendPath *best_path)
 
static Resultcreate_result_plan (PlannerInfo *root, ResultPath *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)
 
static Planinject_projection_plan (Plan *subplan, List *tlist, bool parallel_safe)
 
static Sortcreate_sort_plan (PlannerInfo *root, SortPath *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 void get_column_info_for_window (PlannerInfo *root, WindowClause *wc, List *tlist, int numSortCols, AttrNumber *sortColIdx, int *partNumCols, AttrNumber **partColIdx, Oid **partOperators, int *ordNumCols, AttrNumber **ordColIdx, Oid **ordOperators)
 
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 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 process_subquery_nestloop_params (PlannerInfo *root, List *subplan_params)
 
static Listfix_indexqual_references (PlannerInfo *root, IndexPath *index_path)
 
static Listfix_indexorderby_references (PlannerInfo *root, IndexPath *index_path)
 
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 Appendmake_append (List *appendplans, List *tlist, List *partitioned_rels)
 
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, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
 
static Hashmake_hash (Plan *lefttree, 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 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 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, int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators, int frameOptions, Node *startOffset, Node *endOffset, Plan *lefttree)
 
static Groupmake_group (List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, 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, List *partitioned_rels, List *resultRelations, List *subplans, 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)
 
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, List *groupingSets, List *chain, double dNumGroups, Plan *lefttree)
 
Limitmake_limit (Plan *lefttree, Node *limitOffset, Node *limitCount)
 
bool is_projection_capable_path (Path *path)
 
bool is_projection_capable_plan (Plan *plan)
 

Macro Definition Documentation

◆ CP_EXACT_TLIST

◆ CP_LABEL_TLIST

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

◆ CP_SMALL_TLIST

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

Function Documentation

◆ bitmap_subplan_mark_shared()

static void bitmap_subplan_mark_shared ( Plan plan)
static

Definition at line 4939 of file createplan.c.

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

Referenced by create_bitmap_scan_plan().

4940 {
4941  if (IsA(plan, BitmapAnd))
4943  linitial(((BitmapAnd *) plan)->bitmapplans));
4944  else if (IsA(plan, BitmapOr))
4945  {
4946  ((BitmapOr *) plan)->isshared = true;
4948  linitial(((BitmapOr *) plan)->bitmapplans));
4949  }
4950  else if (IsA(plan, BitmapIndexScan))
4951  ((BitmapIndexScan *) plan)->isshared = true;
4952  else
4953  elog(ERROR, "unrecognized node type: %d", nodeTag(plan));
4954 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:562
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
static void bitmap_subplan_mark_shared(Plan *plan)
Definition: createplan.c:4939
#define nodeTag(nodeptr)
Definition: nodes.h:516
#define elog
Definition: elog.h:219

◆ build_path_tlist()

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

Definition at line 729 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_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_result_plan(), create_scan_plan(), create_unique_plan(), and create_windowagg_plan().

730 {
731  List *tlist = NIL;
732  Index *sortgrouprefs = path->pathtarget->sortgrouprefs;
733  int resno = 1;
734  ListCell *v;
735 
736  foreach(v, path->pathtarget->exprs)
737  {
738  Node *node = (Node *) lfirst(v);
739  TargetEntry *tle;
740 
741  /*
742  * If it's a parameterized path, there might be lateral references in
743  * the tlist, which need to be replaced with Params. There's no need
744  * to remake the TargetEntry nodes, so apply this to each list item
745  * separately.
746  */
747  if (path->param_info)
748  node = replace_nestloop_params(root, node);
749 
750  tle = makeTargetEntry((Expr *) node,
751  resno,
752  NULL,
753  false);
754  if (sortgrouprefs)
755  tle->ressortgroupref = sortgrouprefs[resno - 1];
756 
757  tlist = lappend(tlist, tle);
758  resno++;
759  }
760  return tlist;
761 }
#define NIL
Definition: pg_list.h:69
PathTarget * pathtarget
Definition: relation.h:1043
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4224
ParamPathInfo * param_info
Definition: relation.h:1045
Definition: nodes.h:511
Index * sortgrouprefs
Definition: relation.h:973
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:237
List * lappend(List *list, void *datum)
Definition: list.c:128
List * exprs
Definition: relation.h:972
unsigned int Index
Definition: c.h:413
#define lfirst(lc)
Definition: pg_list.h:106
Index ressortgroupref
Definition: primnodes.h:1378
Definition: pg_list.h:45

◆ copy_generic_path_info()

static void copy_generic_path_info ( Plan dest,
Path src 
)
static

Definition at line 4876 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_groupingsets_plan(), create_hashjoin_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_result_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().

4877 {
4878  dest->startup_cost = src->startup_cost;
4879  dest->total_cost = src->total_cost;
4880  dest->plan_rows = src->rows;
4881  dest->plan_width = src->pathtarget->width;
4882  dest->parallel_aware = src->parallel_aware;
4883  dest->parallel_safe = src->parallel_safe;
4884 }
double plan_rows
Definition: plannodes.h:131
PathTarget * pathtarget
Definition: relation.h:1043
Cost startup_cost
Definition: relation.h:1053
Cost startup_cost
Definition: plannodes.h:125
bool parallel_aware
Definition: plannodes.h:137
Cost total_cost
Definition: relation.h:1054
int plan_width
Definition: plannodes.h:132
double rows
Definition: relation.h:1052
bool parallel_safe
Definition: relation.h:1048
int width
Definition: relation.h:975
Cost total_cost
Definition: plannodes.h:126
bool parallel_aware
Definition: relation.h:1047
bool parallel_safe
Definition: plannodes.h:138

◆ copy_plan_costsize()

static void copy_plan_costsize ( Plan dest,
Plan src 
)
static

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

4892 {
4893  dest->startup_cost = src->startup_cost;
4894  dest->total_cost = src->total_cost;
4895  dest->plan_rows = src->plan_rows;
4896  dest->plan_width = src->plan_width;
4897  /* Assume the inserted node is not parallel-aware. */
4898  dest->parallel_aware = false;
4899  /* Assume the inserted node is parallel-safe, if child plan is. */
4900  dest->parallel_safe = src->parallel_safe;
4901 }
double plan_rows
Definition: plannodes.h:131
Cost startup_cost
Definition: plannodes.h:125
bool parallel_aware
Definition: plannodes.h:137
int plan_width
Definition: plannodes.h:132
Cost total_cost
Definition: plannodes.h:126
bool parallel_safe
Definition: plannodes.h:138

◆ create_agg_plan()

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

Definition at line 1744 of file createplan.c.

References AggPath::aggsplit, AggPath::aggstrategy, build_path_tlist(), copy_generic_path_info(), CP_LABEL_TLIST, create_plan_recurse(), 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, and Plan::targetlist.

Referenced by create_plan_recurse().

1745 {
1746  Agg *plan;
1747  Plan *subplan;
1748  List *tlist;
1749  List *quals;
1750 
1751  /*
1752  * Agg can project, so no need to be terribly picky about child tlist, but
1753  * we do need grouping columns to be available
1754  */
1755  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1756 
1757  tlist = build_path_tlist(root, &best_path->path);
1758 
1759  quals = order_qual_clauses(root, best_path->qual);
1760 
1761  plan = make_agg(tlist, quals,
1762  best_path->aggstrategy,
1763  best_path->aggsplit,
1764  list_length(best_path->groupClause),
1766  subplan->targetlist),
1767  extract_grouping_ops(best_path->groupClause),
1768  NIL,
1769  NIL,
1770  best_path->numGroups,
1771  subplan);
1772 
1773  copy_generic_path_info(&plan->plan, (Path *) best_path);
1774 
1775  return plan;
1776 }
#define NIL
Definition: pg_list.h:69
AggStrategy aggstrategy
Definition: relation.h:1549
List * qual
Definition: relation.h:1553
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4782
AggSplit aggsplit
Definition: relation.h:1550
Oid * extract_grouping_ops(List *groupClause)
Definition: tlist.c:466
double numGroups
Definition: relation.h:1551
AttrNumber * extract_grouping_cols(List *groupClause, List *tlist)
Definition: tlist.c:492
Agg * make_agg(List *tlist, List *qual, AggStrategy aggstrategy, AggSplit aggsplit, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, List *groupingSets, List *chain, double dNumGroups, Plan *lefttree)
Definition: createplan.c:6027
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:729
List * groupClause
Definition: relation.h:1552
Plan plan
Definition: plannodes.h:782
#define CP_LABEL_TLIST
Definition: createplan.c:68
static int list_length(const List *l)
Definition: pg_list.h:89
Path * subpath
Definition: relation.h:1548
List * targetlist
Definition: plannodes.h:144
Definition: plannodes.h:780
Definition: pg_list.h:45
Path path
Definition: relation.h:1547

◆ create_append_plan()

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

Definition at line 1012 of file createplan.c.

References build_path_tlist(), copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), lappend(), lfirst, list_make1, make_append(), make_result(), makeBoolConst(), NIL, AppendPath::partitioned_rels, AppendPath::path, Append::plan, subpath(), and AppendPath::subpaths.

Referenced by create_plan_recurse().

1013 {
1014  Append *plan;
1015  List *tlist = build_path_tlist(root, &best_path->path);
1016  List *subplans = NIL;
1017  ListCell *subpaths;
1018 
1019  /*
1020  * The subpaths list could be empty, if every child was proven empty by
1021  * constraint exclusion. In that case generate a dummy plan that returns
1022  * no rows.
1023  *
1024  * Note that an AppendPath with no members is also generated in certain
1025  * cases where there was no appending construct at all, but we know the
1026  * relation is empty (see set_dummy_rel_pathlist).
1027  */
1028  if (best_path->subpaths == NIL)
1029  {
1030  /* Generate a Result plan with constant-FALSE gating qual */
1031  Plan *plan;
1032 
1033  plan = (Plan *) make_result(tlist,
1034  (Node *) list_make1(makeBoolConst(false,
1035  false)),
1036  NULL);
1037 
1038  copy_generic_path_info(plan, (Path *) best_path);
1039 
1040  return plan;
1041  }
1042 
1043  /* Build the plan for each child */
1044  foreach(subpaths, best_path->subpaths)
1045  {
1046  Path *subpath = (Path *) lfirst(subpaths);
1047  Plan *subplan;
1048 
1049  /* Must insist that all children return the same tlist */
1050  subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
1051 
1052  subplans = lappend(subplans, subplan);
1053  }
1054 
1055  /*
1056  * XXX ideally, if there's just one child, we'd not bother to generate an
1057  * Append node but just return the single child. At the moment this does
1058  * not work because the varno of the child scan plan won't match the
1059  * parent-rel Vars it'll be asked to emit.
1060  */
1061 
1062  plan = make_append(subplans, tlist, best_path->partitioned_rels);
1063 
1064  copy_generic_path_info(&plan->plan, (Path *) best_path);
1065 
1066  return (Plan *) plan;
1067 }
#define NIL
Definition: pg_list.h:69
static Append * make_append(List *appendplans, List *tlist, List *partitioned_rels)
Definition: createplan.c:5297
Definition: nodes.h:511
Path path
Definition: relation.h:1265
#define list_make1(x1)
Definition: pg_list.h:139
List * subpaths
Definition: relation.h:1268
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:729
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:356
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6388
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
List * partitioned_rels
Definition: relation.h:1267
#define CP_EXACT_TLIST
Definition: createplan.c:66
Definition: pg_list.h:45
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:234

◆ create_bitmap_scan_plan()

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

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

2694 {
2695  Index baserelid = best_path->path.parent->relid;
2696  Plan *bitmapqualplan;
2697  List *bitmapqualorig;
2698  List *indexquals;
2699  List *indexECs;
2700  List *qpqual;
2701  ListCell *l;
2702  BitmapHeapScan *scan_plan;
2703 
2704  /* it should be a base rel... */
2705  Assert(baserelid > 0);
2706  Assert(best_path->path.parent->rtekind == RTE_RELATION);
2707 
2708  /* Process the bitmapqual tree into a Plan tree and qual lists */
2709  bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
2710  &bitmapqualorig, &indexquals,
2711  &indexECs);
2712 
2713  if (best_path->path.parallel_aware)
2714  bitmap_subplan_mark_shared(bitmapqualplan);
2715 
2716  /*
2717  * The qpqual list must contain all restrictions not automatically handled
2718  * by the index, other than pseudoconstant clauses which will be handled
2719  * by a separate gating plan node. All the predicates in the indexquals
2720  * will be checked (either by the index itself, or by
2721  * nodeBitmapHeapscan.c), but if there are any "special" operators
2722  * involved then they must be added to qpqual. The upshot is that qpqual
2723  * must contain scan_clauses minus whatever appears in indexquals.
2724  *
2725  * This loop is similar to the comparable code in create_indexscan_plan(),
2726  * but with some differences because it has to compare the scan clauses to
2727  * stripped (no RestrictInfos) indexquals. See comments there for more
2728  * info.
2729  *
2730  * In normal cases simple equal() checks will be enough to spot duplicate
2731  * clauses, so we try that first. We next see if the scan clause is
2732  * redundant with any top-level indexqual by virtue of being generated
2733  * from the same EC. After that, try predicate_implied_by().
2734  *
2735  * Unlike create_indexscan_plan(), the predicate_implied_by() test here is
2736  * useful for getting rid of qpquals that are implied by index predicates,
2737  * because the predicate conditions are included in the "indexquals"
2738  * returned by create_bitmap_subplan(). Bitmap scans have to do it that
2739  * way because predicate conditions need to be rechecked if the scan
2740  * becomes lossy, so they have to be included in bitmapqualorig.
2741  */
2742  qpqual = NIL;
2743  foreach(l, scan_clauses)
2744  {
2745  RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
2746  Node *clause = (Node *) rinfo->clause;
2747 
2748  if (rinfo->pseudoconstant)
2749  continue; /* we may drop pseudoconstants here */
2750  if (list_member(indexquals, clause))
2751  continue; /* simple duplicate */
2752  if (rinfo->parent_ec && list_member_ptr(indexECs, rinfo->parent_ec))
2753  continue; /* derived from same EquivalenceClass */
2754  if (!contain_mutable_functions(clause) &&
2755  predicate_implied_by(list_make1(clause), indexquals, false))
2756  continue; /* provably implied by indexquals */
2757  qpqual = lappend(qpqual, rinfo);
2758  }
2759 
2760  /* Sort clauses into best execution order */
2761  qpqual = order_qual_clauses(root, qpqual);
2762 
2763  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2764  qpqual = extract_actual_clauses(qpqual, false);
2765 
2766  /*
2767  * When dealing with special operators, we will at this point have
2768  * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
2769  * 'em from bitmapqualorig, since there's no point in making the tests
2770  * twice.
2771  */
2772  bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
2773 
2774  /*
2775  * We have to replace any outer-relation variables with nestloop params in
2776  * the qpqual and bitmapqualorig expressions. (This was already done for
2777  * expressions attached to plan nodes in the bitmapqualplan tree.)
2778  */
2779  if (best_path->path.param_info)
2780  {
2781  qpqual = (List *)
2782  replace_nestloop_params(root, (Node *) qpqual);
2783  bitmapqualorig = (List *)
2784  replace_nestloop_params(root, (Node *) bitmapqualorig);
2785  }
2786 
2787  /* Finally ready to build the plan node */
2788  scan_plan = make_bitmap_heapscan(tlist,
2789  qpqual,
2790  bitmapqualplan,
2791  bitmapqualorig,
2792  baserelid);
2793 
2794  copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
2795 
2796  return scan_plan;
2797 }
#define NIL
Definition: pg_list.h:69
bool predicate_implied_by(List *predicate_list, List *clause_list, bool clause_is_check)
Definition: predtest.c:135
Plan plan
Definition: plannodes.h:328
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4224
List * list_difference_ptr(const List *list1, const List *list2)
Definition: list.c:884
bool pseudoconstant
Definition: relation.h:1843
ParamPathInfo * param_info
Definition: relation.h:1045
Definition: nodes.h:511
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4782
static Plan * create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual, List **qual, List **indexqual, List **indexECs)
Definition: createplan.c:2820
#define list_make1(x1)
Definition: pg_list.h:139
static BitmapHeapScan * make_bitmap_heapscan(List *qptlist, List *qpqual, Plan *lefttree, List *bitmapqualorig, Index scanrelid)
Definition: createplan.c:5086
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
bool list_member(const List *list, const void *datum)
Definition: list.c:444
EquivalenceClass * parent_ec
Definition: relation.h:1869
RelOptInfo * parent
Definition: relation.h:1042
Path * bitmapqual
Definition: relation.h:1150
#define lfirst_node(type, lc)
Definition: pg_list.h:109
Index relid
Definition: relation.h:613
List * lappend(List *list, void *datum)
Definition: list.c:128
static void bitmap_subplan_mark_shared(Plan *plan)
Definition: createplan.c:4939
Expr * clause
Definition: relation.h:1835
unsigned int Index
Definition: c.h:413
RTEKind rtekind
Definition: relation.h:615
bool list_member_ptr(const List *list, const void *datum)
Definition: list.c:465
#define Assert(condition)
Definition: c.h:670
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:356
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:878
bool parallel_aware
Definition: relation.h:1047
Definition: pg_list.h:45

◆ create_bitmap_subplan()

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

Definition at line 2820 of file createplan.c.

References BitmapAndPath::bitmapquals, BitmapOrPath::bitmapquals, BitmapAndPath::bitmapselectivity, BitmapOrPath::bitmapselectivity, castNode, clamp_row_est(), create_indexscan_plan(), elog, ERROR, get_actual_clauses(), IndexPath::indexclauses, IndexScan::indexid, IndexPath::indexinfo, IndexScan::indexqual, IndexScan::indexqualorig, IndexPath::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(), 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().

2822 {
2823  Plan *plan;
2824 
2825  if (IsA(bitmapqual, BitmapAndPath))
2826  {
2827  BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
2828  List *subplans = NIL;
2829  List *subquals = NIL;
2830  List *subindexquals = NIL;
2831  List *subindexECs = NIL;
2832  ListCell *l;
2833 
2834  /*
2835  * There may well be redundant quals among the subplans, since a
2836  * top-level WHERE qual might have gotten used to form several
2837  * different index quals. We don't try exceedingly hard to eliminate
2838  * redundancies, but we do eliminate obvious duplicates by using
2839  * list_concat_unique.
2840  */
2841  foreach(l, apath->bitmapquals)
2842  {
2843  Plan *subplan;
2844  List *subqual;
2845  List *subindexqual;
2846  List *subindexEC;
2847 
2848  subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
2849  &subqual, &subindexqual,
2850  &subindexEC);
2851  subplans = lappend(subplans, subplan);
2852  subquals = list_concat_unique(subquals, subqual);
2853  subindexquals = list_concat_unique(subindexquals, subindexqual);
2854  /* Duplicates in indexECs aren't worth getting rid of */
2855  subindexECs = list_concat(subindexECs, subindexEC);
2856  }
2857  plan = (Plan *) make_bitmap_and(subplans);
2858  plan->startup_cost = apath->path.startup_cost;
2859  plan->total_cost = apath->path.total_cost;
2860  plan->plan_rows =
2861  clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
2862  plan->plan_width = 0; /* meaningless */
2863  plan->parallel_aware = false;
2864  plan->parallel_safe = apath->path.parallel_safe;
2865  *qual = subquals;
2866  *indexqual = subindexquals;
2867  *indexECs = subindexECs;
2868  }
2869  else if (IsA(bitmapqual, BitmapOrPath))
2870  {
2871  BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
2872  List *subplans = NIL;
2873  List *subquals = NIL;
2874  List *subindexquals = NIL;
2875  bool const_true_subqual = false;
2876  bool const_true_subindexqual = false;
2877  ListCell *l;
2878 
2879  /*
2880  * Here, we only detect qual-free subplans. A qual-free subplan would
2881  * cause us to generate "... OR true ..." which we may as well reduce
2882  * to just "true". We do not try to eliminate redundant subclauses
2883  * because (a) it's not as likely as in the AND case, and (b) we might
2884  * well be working with hundreds or even thousands of OR conditions,
2885  * perhaps from a long IN list. The performance of list_append_unique
2886  * would be unacceptable.
2887  */
2888  foreach(l, opath->bitmapquals)
2889  {
2890  Plan *subplan;
2891  List *subqual;
2892  List *subindexqual;
2893  List *subindexEC;
2894 
2895  subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
2896  &subqual, &subindexqual,
2897  &subindexEC);
2898  subplans = lappend(subplans, subplan);
2899  if (subqual == NIL)
2900  const_true_subqual = true;
2901  else if (!const_true_subqual)
2902  subquals = lappend(subquals,
2903  make_ands_explicit(subqual));
2904  if (subindexqual == NIL)
2905  const_true_subindexqual = true;
2906  else if (!const_true_subindexqual)
2907  subindexquals = lappend(subindexquals,
2908  make_ands_explicit(subindexqual));
2909  }
2910 
2911  /*
2912  * In the presence of ScalarArrayOpExpr quals, we might have built
2913  * BitmapOrPaths with just one subpath; don't add an OR step.
2914  */
2915  if (list_length(subplans) == 1)
2916  {
2917  plan = (Plan *) linitial(subplans);
2918  }
2919  else
2920  {
2921  plan = (Plan *) make_bitmap_or(subplans);
2922  plan->startup_cost = opath->path.startup_cost;
2923  plan->total_cost = opath->path.total_cost;
2924  plan->plan_rows =
2925  clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
2926  plan->plan_width = 0; /* meaningless */
2927  plan->parallel_aware = false;
2928  plan->parallel_safe = opath->path.parallel_safe;
2929  }
2930 
2931  /*
2932  * If there were constant-TRUE subquals, the OR reduces to constant
2933  * TRUE. Also, avoid generating one-element ORs, which could happen
2934  * due to redundancy elimination or ScalarArrayOpExpr quals.
2935  */
2936  if (const_true_subqual)
2937  *qual = NIL;
2938  else if (list_length(subquals) <= 1)
2939  *qual = subquals;
2940  else
2941  *qual = list_make1(make_orclause(subquals));
2942  if (const_true_subindexqual)
2943  *indexqual = NIL;
2944  else if (list_length(subindexquals) <= 1)
2945  *indexqual = subindexquals;
2946  else
2947  *indexqual = list_make1(make_orclause(subindexquals));
2948  *indexECs = NIL;
2949  }
2950  else if (IsA(bitmapqual, IndexPath))
2951  {
2952  IndexPath *ipath = (IndexPath *) bitmapqual;
2953  IndexScan *iscan;
2954  List *subindexECs;
2955  ListCell *l;
2956 
2957  /* Use the regular indexscan plan build machinery... */
2958  iscan = castNode(IndexScan,
2959  create_indexscan_plan(root, ipath,
2960  NIL, NIL, false));
2961  /* then convert to a bitmap indexscan */
2962  plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
2963  iscan->indexid,
2964  iscan->indexqual,
2965  iscan->indexqualorig);
2966  /* and set its cost/width fields appropriately */
2967  plan->startup_cost = 0.0;
2968  plan->total_cost = ipath->indextotalcost;
2969  plan->plan_rows =
2970  clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
2971  plan->plan_width = 0; /* meaningless */
2972  plan->parallel_aware = false;
2973  plan->parallel_safe = ipath->path.parallel_safe;
2974  *qual = get_actual_clauses(ipath->indexclauses);
2975  *indexqual = get_actual_clauses(ipath->indexquals);
2976  foreach(l, ipath->indexinfo->indpred)
2977  {
2978  Expr *pred = (Expr *) lfirst(l);
2979 
2980  /*
2981  * We know that the index predicate must have been implied by the
2982  * query condition as a whole, but it may or may not be implied by
2983  * the conditions that got pushed into the bitmapqual. Avoid
2984  * generating redundant conditions.
2985  */
2986  if (!predicate_implied_by(list_make1(pred), ipath->indexclauses,
2987  false))
2988  {
2989  *qual = lappend(*qual, pred);
2990  *indexqual = lappend(*indexqual, pred);
2991  }
2992  }
2993  subindexECs = NIL;
2994  foreach(l, ipath->indexquals)
2995  {
2996  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
2997 
2998  if (rinfo->parent_ec)
2999  subindexECs = lappend(subindexECs, rinfo->parent_ec);
3000  }
3001  *indexECs = subindexECs;
3002  }
3003  else
3004  {
3005  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
3006  plan = NULL; /* keep compiler quiet */
3007  }
3008 
3009  return plan;
3010 }
#define NIL
Definition: pg_list.h:69
bool predicate_implied_by(List *predicate_list, List *clause_list, bool clause_is_check)
Definition: predtest.c:135
double plan_rows
Definition: plannodes.h:131
#define IsA(nodeptr, _type_)
Definition: nodes.h:562
Path path
Definition: relation.h:1118
IndexOptInfo * indexinfo
Definition: relation.h:1119
Index scanrelid
Definition: plannodes.h:329
#define castNode(_type_, nodeptr)
Definition: nodes.h:580
double tuples
Definition: relation.h:625
List * indexqualorig
Definition: plannodes.h:391
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:333
List * list_concat(List *list1, List *list2)
Definition: list.c:321
List * indexclauses
Definition: relation.h:1120
static Plan * create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual, List **qual, List **indexqual, List **indexECs)
Definition: createplan.c:2820
Selectivity bitmapselectivity
Definition: relation.h:1163
static BitmapAnd * make_bitmap_and(List *bitmapplans)
Definition: createplan.c:5365
Oid indexid
Definition: plannodes.h:389
List * bitmapquals
Definition: relation.h:1162
List * bitmapquals
Definition: relation.h:1175
Expr * make_ands_explicit(List *andclauses)
Definition: clauses.c:367
#define list_make1(x1)
Definition: pg_list.h:139
List * indexquals
Definition: relation.h:1121
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
Cost indextotalcost
Definition: relation.h:1126
Cost startup_cost
Definition: relation.h:1053
Scan scan
Definition: plannodes.h:388
EquivalenceClass * parent_ec
Definition: relation.h:1869
RelOptInfo * parent
Definition: relation.h:1042
Selectivity indexselectivity
Definition: relation.h:1127
Cost startup_cost
Definition: plannodes.h:125
bool parallel_aware
Definition: plannodes.h:137
Selectivity bitmapselectivity
Definition: relation.h:1176
List * indexqual
Definition: plannodes.h:390
List * lappend(List *list, void *datum)
Definition: list.c:128
static BitmapOr * make_bitmap_or(List *bitmapplans)
Definition: createplan.c:5380
Cost total_cost
Definition: relation.h:1054
int plan_width
Definition: plannodes.h:132
#define lfirst(lc)
Definition: pg_list.h:106
bool parallel_safe
Definition: relation.h:1048
static int list_length(const List *l)
Definition: pg_list.h:89
List * list_concat_unique(List *list1, List *list2)
Definition: list.c:1018
static BitmapIndexScan * make_bitmap_indexscan(Index scanrelid, Oid indexid, List *indexqual, List *indexqualorig)
Definition: createplan.c:5065
#define nodeTag(nodeptr)
Definition: nodes.h:516
Cost total_cost
Definition: plannodes.h:126
bool parallel_safe
Definition: plannodes.h:138
#define elog
Definition: elog.h:219
List * indpred
Definition: relation.h:742
double clamp_row_est(double nrows)
Definition: costsize.c:174
Definition: pg_list.h:45
Expr * make_orclause(List *orclauses)
Definition: clauses.c:293
static Scan * create_indexscan_plan(PlannerInfo *root, IndexPath *best_path, List *tlist, List *scan_clauses, bool indexonly)
Definition: createplan.c:2514

◆ create_ctescan_plan()

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

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

3252 {
3253  CteScan *scan_plan;
3254  Index scan_relid = best_path->parent->relid;
3255  RangeTblEntry *rte;
3256  SubPlan *ctesplan = NULL;
3257  int plan_id;
3258  int cte_param_id;
3259  PlannerInfo *cteroot;
3260  Index levelsup;
3261  int ndx;
3262  ListCell *lc;
3263 
3264  Assert(scan_relid > 0);
3265  rte = planner_rt_fetch(scan_relid, root);
3266  Assert(rte->rtekind == RTE_CTE);
3267  Assert(!rte->self_reference);
3268 
3269  /*
3270  * Find the referenced CTE, and locate the SubPlan previously made for it.
3271  */
3272  levelsup = rte->ctelevelsup;
3273  cteroot = root;
3274  while (levelsup-- > 0)
3275  {
3276  cteroot = cteroot->parent_root;
3277  if (!cteroot) /* shouldn't happen */
3278  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3279  }
3280 
3281  /*
3282  * Note: cte_plan_ids can be shorter than cteList, if we are still working
3283  * on planning the CTEs (ie, this is a side-reference from another CTE).
3284  * So we mustn't use forboth here.
3285  */
3286  ndx = 0;
3287  foreach(lc, cteroot->parse->cteList)
3288  {
3289  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
3290 
3291  if (strcmp(cte->ctename, rte->ctename) == 0)
3292  break;
3293  ndx++;
3294  }
3295  if (lc == NULL) /* shouldn't happen */
3296  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
3297  if (ndx >= list_length(cteroot->cte_plan_ids))
3298  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3299  plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
3300  Assert(plan_id > 0);
3301  foreach(lc, cteroot->init_plans)
3302  {
3303  ctesplan = (SubPlan *) lfirst(lc);
3304  if (ctesplan->plan_id == plan_id)
3305  break;
3306  }
3307  if (lc == NULL) /* shouldn't happen */
3308  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3309 
3310  /*
3311  * We need the CTE param ID, which is the sole member of the SubPlan's
3312  * setParam list.
3313  */
3314  cte_param_id = linitial_int(ctesplan->setParam);
3315 
3316  /* Sort clauses into best execution order */
3317  scan_clauses = order_qual_clauses(root, scan_clauses);
3318 
3319  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3320  scan_clauses = extract_actual_clauses(scan_clauses, false);
3321 
3322  /* Replace any outer-relation variables with nestloop params */
3323  if (best_path->param_info)
3324  {
3325  scan_clauses = (List *)
3326  replace_nestloop_params(root, (Node *) scan_clauses);
3327  }
3328 
3329  scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
3330  plan_id, cte_param_id);
3331 
3332  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3333 
3334  return scan_plan;
3335 }
Plan plan
Definition: plannodes.h:328
Query * parse
Definition: relation.h:155
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4224
int plan_id
Definition: primnodes.h:689
ParamPathInfo * param_info
Definition: relation.h:1045
Definition: nodes.h:511
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4782
#define linitial_int(l)
Definition: pg_list.h:112
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
#define planner_rt_fetch(rti, root)
Definition: relation.h:328
#define ERROR
Definition: elog.h:43
Scan scan
Definition: plannodes.h:538
RelOptInfo * parent
Definition: relation.h:1042
Index relid
Definition: relation.h:613
struct PlannerInfo * parent_root
Definition: relation.h:161
static CteScan * make_ctescan(List *qptlist, List *qpqual, Index scanrelid, int ctePlanId, int cteParam)
Definition: createplan.c:5203
int list_nth_int(const List *list, int n)
Definition: list.c:421
List * cte_plan_ids
Definition: relation.h:230
bool self_reference
Definition: parsenodes.h:1023
unsigned int Index
Definition: c.h:413
List * init_plans
Definition: relation.h:228
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
List * setParam
Definition: primnodes.h:707
static int list_length(const List *l)
Definition: pg_list.h:89
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:356
Index ctelevelsup
Definition: parsenodes.h:1022
RTEKind rtekind
Definition: parsenodes.h:951
List * cteList
Definition: parsenodes.h:133
char * ctename
Definition: parsenodes.h:1021
#define elog
Definition: elog.h:219
Definition: pg_list.h:45

◆ create_customscan_plan()

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

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

3588 {
3589  CustomScan *cplan;
3590  RelOptInfo *rel = best_path->path.parent;
3591  List *custom_plans = NIL;
3592  ListCell *lc;
3593 
3594  /* Recursively transform child paths. */
3595  foreach(lc, best_path->custom_paths)
3596  {
3597  Plan *plan = create_plan_recurse(root, (Path *) lfirst(lc),
3598  CP_EXACT_TLIST);
3599 
3600  custom_plans = lappend(custom_plans, plan);
3601  }
3602 
3603  /*
3604  * Sort clauses into the best execution order, although custom-scan
3605  * provider can reorder them again.
3606  */
3607  scan_clauses = order_qual_clauses(root, scan_clauses);
3608 
3609  /*
3610  * Invoke custom plan provider to create the Plan node represented by the
3611  * CustomPath.
3612  */
3613  cplan = castNode(CustomScan,
3614  best_path->methods->PlanCustomPath(root,
3615  rel,
3616  best_path,
3617  tlist,
3618  scan_clauses,
3619  custom_plans));
3620 
3621  /*
3622  * Copy cost data from Path to Plan; no need to make custom-plan providers
3623  * do this
3624  */
3625  copy_generic_path_info(&cplan->scan.plan, &best_path->path);
3626 
3627  /* Likewise, copy the relids that are represented by this custom scan */
3628  cplan->custom_relids = best_path->path.parent->relids;
3629 
3630  /*
3631  * Replace any outer-relation variables with nestloop params in the qual
3632  * and custom_exprs expressions. We do this last so that the custom-plan
3633  * provider doesn't have to be involved. (Note that parts of custom_exprs
3634  * could have come from join clauses, so doing this beforehand on the
3635  * scan_clauses wouldn't work.) We assume custom_scan_tlist contains no
3636  * such variables.
3637  */
3638  if (best_path->path.param_info)
3639  {
3640  cplan->scan.plan.qual = (List *)
3641  replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
3642  cplan->custom_exprs = (List *)
3643  replace_nestloop_params(root, (Node *) cplan->custom_exprs);
3644  }
3645 
3646  return cplan;
3647 }
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:145
Plan plan
Definition: plannodes.h:328
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4224
List * custom_paths
Definition: relation.h:1249
#define castNode(_type_, nodeptr)
Definition: nodes.h:580
ParamPathInfo * param_info
Definition: relation.h:1045
Definition: nodes.h:511
List * custom_exprs
Definition: plannodes.h:629
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4782
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
const struct CustomPathMethods * methods
Definition: relation.h:1251
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
RelOptInfo * parent
Definition: relation.h:1042
Relids relids
Definition: relation.h:585
Path path
Definition: relation.h:1246
List * lappend(List *list, void *datum)
Definition: list.c:128
Scan scan
Definition: plannodes.h:625
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:106
Bitmapset * custom_relids
Definition: plannodes.h:632
#define CP_EXACT_TLIST
Definition: createplan.c:66
Definition: pg_list.h:45

◆ create_foreignscan_plan()

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

Definition at line 3442 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, IS_UPPER_REL, 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, RelOptInfo::reltarget, replace_nestloop_params(), RTE_RELATION, RelOptInfo::rtekind, RangeTblEntry::rtekind, ForeignScan::scan, RelOptInfo::serverid, and RelOptInfo::useridiscurrent.

Referenced by create_scan_plan().

3444 {
3445  ForeignScan *scan_plan;
3446  RelOptInfo *rel = best_path->path.parent;
3447  Index scan_relid = rel->relid;
3448  Oid rel_oid = InvalidOid;
3449  Plan *outer_plan = NULL;
3450 
3451  Assert(rel->fdwroutine != NULL);
3452 
3453  /* transform the child path if any */
3454  if (best_path->fdw_outerpath)
3455  outer_plan = create_plan_recurse(root, best_path->fdw_outerpath,
3456  CP_EXACT_TLIST);
3457 
3458  /*
3459  * If we're scanning a base relation, fetch its OID. (Irrelevant if
3460  * scanning a join relation.)
3461  */
3462  if (scan_relid > 0)
3463  {
3464  RangeTblEntry *rte;
3465 
3466  Assert(rel->rtekind == RTE_RELATION);
3467  rte = planner_rt_fetch(scan_relid, root);
3468  Assert(rte->rtekind == RTE_RELATION);
3469  rel_oid = rte->relid;
3470  }
3471 
3472  /*
3473  * Sort clauses into best execution order. We do this first since the FDW
3474  * might have more info than we do and wish to adjust the ordering.
3475  */
3476  scan_clauses = order_qual_clauses(root, scan_clauses);
3477 
3478  /*
3479  * Let the FDW perform its processing on the restriction clauses and
3480  * generate the plan node. Note that the FDW might remove restriction
3481  * clauses that it intends to execute remotely, or even add more (if it
3482  * has selected some join clauses for remote use but also wants them
3483  * rechecked locally).
3484  */
3485  scan_plan = rel->fdwroutine->GetForeignPlan(root, rel, rel_oid,
3486  best_path,
3487  tlist, scan_clauses,
3488  outer_plan);
3489 
3490  /* Copy cost data from Path to Plan; no need to make FDW do this */
3491  copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3492 
3493  /* Copy foreign server OID; likewise, no need to make FDW do this */
3494  scan_plan->fs_server = rel->serverid;
3495 
3496  /*
3497  * Likewise, copy the relids that are represented by this foreign scan. An
3498  * upper rel doesn't have relids set, but it covers all the base relations
3499  * participating in the underlying scan, so use root's all_baserels.
3500  */
3501  if (IS_UPPER_REL(rel))
3502  scan_plan->fs_relids = root->all_baserels;
3503  else
3504  scan_plan->fs_relids = best_path->path.parent->relids;
3505 
3506  /*
3507  * If this is a foreign join, and to make it valid to push down we had to
3508  * assume that the current user is the same as some user explicitly named
3509  * in the query, mark the finished plan as depending on the current user.
3510  */
3511  if (rel->useridiscurrent)
3512  root->glob->dependsOnRole = true;
3513 
3514  /*
3515  * Replace any outer-relation variables with nestloop params in the qual,
3516  * fdw_exprs and fdw_recheck_quals expressions. We do this last so that
3517  * the FDW doesn't have to be involved. (Note that parts of fdw_exprs or
3518  * fdw_recheck_quals could have come from join clauses, so doing this
3519  * beforehand on the scan_clauses wouldn't work.) We assume
3520  * fdw_scan_tlist contains no such variables.
3521  */
3522  if (best_path->path.param_info)
3523  {
3524  scan_plan->scan.plan.qual = (List *)
3525  replace_nestloop_params(root, (Node *) scan_plan->scan.plan.qual);
3526  scan_plan->fdw_exprs = (List *)
3527  replace_nestloop_params(root, (Node *) scan_plan->fdw_exprs);
3528  scan_plan->fdw_recheck_quals = (List *)
3530  (Node *) scan_plan->fdw_recheck_quals);
3531  }
3532 
3533  /*
3534  * If rel is a base relation, detect whether any system columns are
3535  * requested from the rel. (If rel is a join relation, rel->relid will be
3536  * 0, but there can be no Var with relid 0 in the rel's targetlist or the
3537  * restriction clauses, so we skip this in that case. Note that any such
3538  * columns in base relations that were joined are assumed to be contained
3539  * in fdw_scan_tlist.) This is a bit of a kluge and might go away
3540  * someday, so we intentionally leave it out of the API presented to FDWs.
3541  */
3542  scan_plan->fsSystemCol = false;
3543  if (scan_relid > 0)
3544  {
3545  Bitmapset *attrs_used = NULL;
3546  ListCell *lc;
3547  int i;
3548 
3549  /*
3550  * First, examine all the attributes needed for joins or final output.
3551  * Note: we must look at rel's targetlist, not the attr_needed data,
3552  * because attr_needed isn't computed for inheritance child rels.
3553  */
3554  pull_varattnos((Node *) rel->reltarget->exprs, scan_relid, &attrs_used);
3555 
3556  /* Add all the attributes used by restriction clauses. */
3557  foreach(lc, rel->baserestrictinfo)
3558  {
3559  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3560 
3561  pull_varattnos((Node *) rinfo->clause, scan_relid, &attrs_used);
3562  }
3563 
3564  /* Now, are any system columns requested from rel? */
3565  for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
3566  {
3568  {
3569  scan_plan->fsSystemCol = true;
3570  break;
3571  }
3572  }
3573 
3574  bms_free(attrs_used);
3575  }
3576 
3577  return scan_plan;
3578 }
GetForeignPlan_function GetForeignPlan
Definition: fdwapi.h:182
List * qual
Definition: plannodes.h:145
Plan plan
Definition: plannodes.h:328
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4224
Path * fdw_outerpath
Definition: relation.h:1219
Oid fs_server
Definition: plannodes.h:599
List * baserestrictinfo
Definition: relation.h:645
List * fdw_exprs
Definition: plannodes.h:600
#define IS_UPPER_REL(rel)
Definition: relation.h:571
ParamPathInfo * param_info
Definition: relation.h:1045
Definition: nodes.h:511
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4782
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
bool useridiscurrent
Definition: relation.h:634
unsigned int Oid
Definition: postgres_ext.h:31
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:219
bool dependsOnRole
Definition: relation.h:127
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
#define planner_rt_fetch(rti, root)
Definition: relation.h:328
Relids all_baserels
Definition: relation.h:196
RelOptInfo * parent
Definition: relation.h:1042
PlannerGlobal * glob
Definition: relation.h:157
struct FdwRoutine * fdwroutine
Definition: relation.h:636
Relids relids
Definition: relation.h:585
List * fdw_recheck_quals
Definition: plannodes.h:603
Index relid
Definition: relation.h:613
Expr * clause
Definition: relation.h:1835
Oid serverid
Definition: relation.h:632
List * exprs
Definition: relation.h:972
unsigned int Index
Definition: c.h:413
RTEKind rtekind
Definition: relation.h:615
#define InvalidOid
Definition: postgres_ext.h:36
void bms_free(Bitmapset *a)
Definition: bitmapset.c:201
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
RTEKind rtekind
Definition: parsenodes.h:951
int i
#define CP_EXACT_TLIST
Definition: createplan.c:66
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
struct PathTarget * reltarget
Definition: relation.h:596
bool fsSystemCol
Definition: plannodes.h:605
Bitmapset * fs_relids
Definition: plannodes.h:604

◆ create_functionscan_plan()

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

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

3122 {
3123  FunctionScan *scan_plan;
3124  Index scan_relid = best_path->parent->relid;
3125  RangeTblEntry *rte;
3126  List *functions;
3127 
3128  /* it should be a function base rel... */
3129  Assert(scan_relid > 0);
3130  rte = planner_rt_fetch(scan_relid, root);
3131  Assert(rte->rtekind == RTE_FUNCTION);
3132  functions = rte->functions;
3133 
3134  /* Sort clauses into best execution order */
3135  scan_clauses = order_qual_clauses(root, scan_clauses);
3136 
3137  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3138  scan_clauses = extract_actual_clauses(scan_clauses, false);
3139 
3140  /* Replace any outer-relation variables with nestloop params */
3141  if (best_path->param_info)
3142  {
3143  scan_clauses = (List *)
3144  replace_nestloop_params(root, (Node *) scan_clauses);
3145  /* The function expressions could contain nestloop params, too */
3146  functions = (List *) replace_nestloop_params(root, (Node *) functions);
3147  }
3148 
3149  scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
3150  functions, rte->funcordinality);
3151 
3152  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3153 
3154  return scan_plan;
3155 }
Plan plan
Definition: plannodes.h:328
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4224
ParamPathInfo * param_info
Definition: relation.h:1045
Definition: nodes.h:511
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4782
bool funcordinality
Definition: parsenodes.h:1006
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
#define planner_rt_fetch(rti, root)
Definition: relation.h:328
RelOptInfo * parent
Definition: relation.h:1042
Index relid
Definition: relation.h:613
unsigned int Index
Definition: c.h:413
static FunctionScan * make_functionscan(List *qptlist, List *qpqual, Index scanrelid, List *functions, bool funcordinality)
Definition: createplan.c:5144
#define Assert(condition)
Definition: c.h:670
List * functions
Definition: parsenodes.h:1005
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:356
static const struct fns functions
Definition: regcomp.c:299
RTEKind rtekind
Definition: parsenodes.h:951
Definition: pg_list.h:45

◆ create_gather_merge_plan()

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

Definition at line 1503 of file createplan.c.

References Assert, 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, SS_assign_special_param(), GatherMergePath::subpath, and Plan::targetlist.

Referenced by create_plan_recurse().

1504 {
1505  GatherMerge *gm_plan;
1506  Plan *subplan;
1507  List *pathkeys = best_path->path.pathkeys;
1508  List *tlist = build_path_tlist(root, &best_path->path);
1509 
1510  /* As with Gather, it's best to project away columns in the workers. */
1511  subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1512 
1513  /* Create a shell for a GatherMerge plan. */
1514  gm_plan = makeNode(GatherMerge);
1515  gm_plan->plan.targetlist = tlist;
1516  gm_plan->num_workers = best_path->num_workers;
1517  copy_generic_path_info(&gm_plan->plan, &best_path->path);
1518 
1519  /* Assign the rescan Param. */
1520  gm_plan->rescan_param = SS_assign_special_param(root);
1521 
1522  /* Gather Merge is pointless with no pathkeys; use Gather instead. */
1523  Assert(pathkeys != NIL);
1524 
1525  /* Compute sort column info, and adjust subplan's tlist as needed */
1526  subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1527  best_path->subpath->parent->relids,
1528  gm_plan->sortColIdx,
1529  false,
1530  &gm_plan->numCols,
1531  &gm_plan->sortColIdx,
1532  &gm_plan->sortOperators,
1533  &gm_plan->collations,
1534  &gm_plan->nullsFirst);
1535 
1536 
1537  /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1538  if (!pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys))
1539  subplan = (Plan *) make_sort(subplan, gm_plan->numCols,
1540  gm_plan->sortColIdx,
1541  gm_plan->sortOperators,
1542  gm_plan->collations,
1543  gm_plan->nullsFirst);
1544 
1545  /* Now insert the subplan under GatherMerge. */
1546  gm_plan->plan.lefttree = subplan;
1547 
1548  /* use parallel mode for parallel plans. */
1549  root->glob->parallelModeNeeded = true;
1550 
1551  return gm_plan;
1552 }
#define NIL
Definition: pg_list.h:69
Oid * collations
Definition: plannodes.h:861
int SS_assign_special_param(PlannerInfo *root)
Definition: subselect.c:429
AttrNumber * sortColIdx
Definition: plannodes.h:859
bool * nullsFirst
Definition: plannodes.h:862
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
bool parallelModeNeeded
Definition: relation.h:131
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:729
RelOptInfo * parent
Definition: relation.h:1042
PlannerGlobal * glob
Definition: relation.h:157
Relids relids
Definition: relation.h:585
Oid * sortOperators
Definition: plannodes.h:860
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:317
List * pathkeys
Definition: relation.h:1056
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
Definition: createplan.c:5507
#define makeNode(_type_)
Definition: nodes.h:559
#define Assert(condition)
Definition: c.h:670
struct Plan * lefttree
Definition: plannodes.h:146
List * targetlist
Definition: plannodes.h:144
Path * subpath
Definition: relation.h:1366
int num_workers
Definition: plannodes.h:855
int rescan_param
Definition: plannodes.h:856
#define CP_EXACT_TLIST
Definition: createplan.c:66
Definition: pg_list.h:45
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:5569

◆ create_gather_plan()

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

Definition at line 1467 of file createplan.c.

References 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, SS_assign_special_param(), and GatherPath::subpath.

Referenced by create_plan_recurse().

1468 {
1469  Gather *gather_plan;
1470  Plan *subplan;
1471  List *tlist;
1472 
1473  /*
1474  * Although the Gather node can project, we prefer to push down such work
1475  * to its child node, so demand an exact tlist from the child.
1476  */
1477  subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1478 
1479  tlist = build_path_tlist(root, &best_path->path);
1480 
1481  gather_plan = make_gather(tlist,
1482  NIL,
1483  best_path->num_workers,
1485  best_path->single_copy,
1486  subplan);
1487 
1488  copy_generic_path_info(&gather_plan->plan, &best_path->path);
1489 
1490  /* use parallel mode for parallel plans. */
1491  root->glob->parallelModeNeeded = true;
1492 
1493  return gather_plan;
1494 }
#define NIL
Definition: pg_list.h:69
int SS_assign_special_param(PlannerInfo *root)
Definition: subselect.c:429
bool single_copy
Definition: relation.h:1354
int num_workers
Definition: relation.h:1355
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
bool parallelModeNeeded
Definition: relation.h:131
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:729
Plan plan
Definition: plannodes.h:839
PlannerGlobal * glob
Definition: relation.h:157
Path * subpath
Definition: relation.h:1353
static Gather * make_gather(List *qptlist, List *qpqual, int nworkers, int rescan_param, bool single_copy, Plan *subplan)
Definition: createplan.c:6264
Path path
Definition: relation.h:1352
#define CP_EXACT_TLIST
Definition: createplan.c:66
Definition: pg_list.h:45

◆ create_gating_plan()

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

Definition at line 910 of file createplan.c.

References Assert, build_path_tlist(), copy_plan_costsize(), make_result(), Plan::parallel_safe, and Path::parallel_safe.

Referenced by create_join_plan(), and create_scan_plan().

912 {
913  Plan *gplan;
914 
915  Assert(gating_quals);
916 
917  /*
918  * Since we need a Result node anyway, always return the path's requested
919  * tlist; that's never a wrong choice, even if the parent node didn't ask
920  * for CP_EXACT_TLIST.
921  */
922  gplan = (Plan *) make_result(build_path_tlist(root, path),
923  (Node *) gating_quals,
924  plan);
925 
926  /*
927  * Notice that we don't change cost or size estimates when doing gating.
928  * The costs of qual eval were already included in the subplan's cost.
929  * Leaving the size alone amounts to assuming that the gating qual will
930  * succeed, which is the conservative estimate for planning upper queries.
931  * We certainly don't want to assume the output size is zero (unless the
932  * gating qual is actually constant FALSE, and that case is dealt with in
933  * clausesel.c). Interpolating between the two cases is silly, because it
934  * doesn't reflect what will really happen at runtime, and besides which
935  * in most cases we have only a very bad idea of the probability of the
936  * gating qual being true.
937  */
938  copy_plan_costsize(gplan, plan);
939 
940  /* Gating quals could be unsafe, so better use the Path's safety flag */
941  gplan->parallel_safe = path->parallel_safe;
942 
943  return gplan;
944 }
Definition: nodes.h:511
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:729
static void copy_plan_costsize(Plan *dest, Plan *src)
Definition: createplan.c:4891
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6388
#define Assert(condition)
Definition: c.h:670
bool parallel_safe
Definition: relation.h:1048
bool parallel_safe
Definition: plannodes.h:138

◆ create_group_plan()

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

Definition at line 1679 of file createplan.c.

References build_path_tlist(), copy_generic_path_info(), CP_LABEL_TLIST, create_plan_recurse(), 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().

1680 {
1681  Group *plan;
1682  Plan *subplan;
1683  List *tlist;
1684  List *quals;
1685 
1686  /*
1687  * Group can project, so no need to be terribly picky about child tlist,
1688  * but we do need grouping columns to be available
1689  */
1690  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1691 
1692  tlist = build_path_tlist(root, &best_path->path);
1693 
1694  quals = order_qual_clauses(root, best_path->qual);
1695 
1696  plan = make_group(tlist,
1697  quals,
1698  list_length(best_path->groupClause),
1700  subplan->targetlist),
1701  extract_grouping_ops(best_path->groupClause),
1702  subplan);
1703 
1704  copy_generic_path_info(&plan->plan, (Path *) best_path);
1705 
1706  return plan;
1707 }
List * qual
Definition: relation.h:1522
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4782
Oid * extract_grouping_ops(List *groupClause)
Definition: tlist.c:466
AttrNumber * extract_grouping_cols(List *groupClause, List *tlist)
Definition: tlist.c:492
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:729
Path path
Definition: relation.h:1519
static Group * make_group(List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Plan *lefttree)
Definition: createplan.c:6089
List * groupClause
Definition: relation.h:1521
#define CP_LABEL_TLIST
Definition: createplan.c:68
Path * subpath
Definition: relation.h:1520
Plan plan
Definition: plannodes.h:760
static int list_length(const List *l)
Definition: pg_list.h:89
List * targetlist
Definition: plannodes.h:144
Definition: pg_list.h:45

◆ create_groupingsets_plan()

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

Definition at line 1825 of file createplan.c.

References AGG_HASHED, AGG_PLAIN, AGG_SORTED, AGGSPLIT_SIMPLE, GroupingSetsPath::aggstrategy, Assert, build_path_tlist(), copy_generic_path_info(), CP_LABEL_TLIST, create_plan_recurse(), extract_grouping_ops(), for_each_cell, get_sortgroupclause_tle(), Query::groupClause, RollupData::groupClause, PlannerInfo::grouping_map, Query::groupingSets, RollupData::gsets, PlannerInfo::hasInheritedTarget, RollupData::is_hashed, lappend(), Plan::lefttree, lfirst, linitial, list_head(), list_length(), lnext, 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, and SortGroupClause::tleSortGroupRef.

Referenced by create_plan_recurse().

1826 {
1827  Agg *plan;
1828  Plan *subplan;
1829  List *rollups = best_path->rollups;
1830  AttrNumber *grouping_map;
1831  int maxref;
1832  List *chain;
1833  ListCell *lc;
1834 
1835  /* Shouldn't get here without grouping sets */
1836  Assert(root->parse->groupingSets);
1837  Assert(rollups != NIL);
1838 
1839  /*
1840  * Agg can project, so no need to be terribly picky about child tlist, but
1841  * we do need grouping columns to be available
1842  */
1843  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1844 
1845  /*
1846  * Compute the mapping from tleSortGroupRef to column index in the child's
1847  * tlist. First, identify max SortGroupRef in groupClause, for array
1848  * sizing.
1849  */
1850  maxref = 0;
1851  foreach(lc, root->parse->groupClause)
1852  {
1853  SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
1854 
1855  if (gc->tleSortGroupRef > maxref)
1856  maxref = gc->tleSortGroupRef;
1857  }
1858 
1859  grouping_map = (AttrNumber *) palloc0((maxref + 1) * sizeof(AttrNumber));
1860 
1861  /* Now look up the column numbers in the child's tlist */
1862  foreach(lc, root->parse->groupClause)
1863  {
1864  SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
1865  TargetEntry *tle = get_sortgroupclause_tle(gc, subplan->targetlist);
1866 
1867  grouping_map[gc->tleSortGroupRef] = tle->resno;
1868  }
1869 
1870  /*
1871  * During setrefs.c, we'll need the grouping_map to fix up the cols lists
1872  * in GroupingFunc nodes. Save it for setrefs.c to use.
1873  *
1874  * This doesn't work if we're in an inheritance subtree (see notes in
1875  * create_modifytable_plan). Fortunately we can't be because there would
1876  * never be grouping in an UPDATE/DELETE; but let's Assert that.
1877  */
1878  Assert(!root->hasInheritedTarget);
1879  Assert(root->grouping_map == NULL);
1880  root->grouping_map = grouping_map;
1881 
1882  /*
1883  * Generate the side nodes that describe the other sort and group
1884  * operations besides the top one. Note that we don't worry about putting
1885  * accurate cost estimates in the side nodes; only the topmost Agg node's
1886  * costs will be shown by EXPLAIN.
1887  */
1888  chain = NIL;
1889  if (list_length(rollups) > 1)
1890  {
1891  ListCell *lc2 = lnext(list_head(rollups));
1892  bool is_first_sort = ((RollupData *) linitial(rollups))->is_hashed;
1893 
1894  for_each_cell(lc, lc2)
1895  {
1896  RollupData *rollup = lfirst(lc);
1897  AttrNumber *new_grpColIdx;
1898  Plan *sort_plan = NULL;
1899  Plan *agg_plan;
1900  AggStrategy strat;
1901 
1902  new_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
1903 
1904  if (!rollup->is_hashed && !is_first_sort)
1905  {
1906  sort_plan = (Plan *)
1908  new_grpColIdx,
1909  subplan);
1910  }
1911 
1912  if (!rollup->is_hashed)
1913  is_first_sort = false;
1914 
1915  if (rollup->is_hashed)
1916  strat = AGG_HASHED;
1917  else if (list_length(linitial(rollup->gsets)) == 0)
1918  strat = AGG_PLAIN;
1919  else
1920  strat = AGG_SORTED;
1921 
1922  agg_plan = (Plan *) make_agg(NIL,
1923  NIL,
1924  strat,
1926  list_length((List *) linitial(rollup->gsets)),
1927  new_grpColIdx,
1929  rollup->gsets,
1930  NIL,
1931  rollup->numGroups,
1932  sort_plan);
1933 
1934  /*
1935  * Remove stuff we don't need to avoid bloating debug output.
1936  */
1937  if (sort_plan)
1938  {
1939  sort_plan->targetlist = NIL;
1940  sort_plan->lefttree = NULL;
1941  }
1942 
1943  chain = lappend(chain, agg_plan);
1944  }
1945  }
1946 
1947  /*
1948  * Now make the real Agg node
1949  */
1950  {
1951  RollupData *rollup = linitial(rollups);
1952  AttrNumber *top_grpColIdx;
1953  int numGroupCols;
1954 
1955  top_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
1956 
1957  numGroupCols = list_length((List *) linitial(rollup->gsets));
1958 
1959  plan = make_agg(build_path_tlist(root, &best_path->path),
1960  best_path->qual,
1961  best_path->aggstrategy,
1963  numGroupCols,
1964  top_grpColIdx,
1966  rollup->gsets,
1967  chain,
1968  rollup->numGroups,
1969  subplan);
1970 
1971  /* Copy cost data from Path to Plan */
1972  copy_generic_path_info(&plan->plan, &best_path->path);
1973  }
1974 
1975  return (Plan *) plan;
1976 }
#define NIL
Definition: pg_list.h:69
Query * parse
Definition: relation.h:155
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:370
List * groupClause
Definition: relation.h:1570
static AttrNumber * remap_groupColIdx(PlannerInfo *root, List *groupClause)
Definition: createplan.c:1787
Index tleSortGroupRef
Definition: parsenodes.h:1196
bool is_hashed
Definition: relation.h:1575
List * groupingSets
Definition: parsenodes.h:148
Oid * extract_grouping_ops(List *groupClause)
Definition: tlist.c:466
AttrNumber * grouping_map
Definition: relation.h:287
double numGroups
Definition: relation.h:1573
Agg * make_agg(List *tlist, List *qual, AggStrategy aggstrategy, AggSplit aggsplit, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, List *groupingSets, List *chain, double dNumGroups, Plan *lefttree)
Definition: createplan.c:6027
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
#define linitial(l)
Definition: pg_list.h:111
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:729
AttrNumber resno
Definition: primnodes.h:1376
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
static Sort * make_sort_from_groupcols(List *groupcls, AttrNumber *grpColIdx, Plan *lefttree)
Definition: createplan.c:5930
AggStrategy aggstrategy
Definition: relation.h:1586
#define lnext(lc)
Definition: pg_list.h:105
List * lappend(List *list, void *datum)
Definition: list.c:128
Plan plan
Definition: plannodes.h:782
void * palloc0(Size size)
Definition: mcxt.c:877
#define CP_LABEL_TLIST
Definition: createplan.c:68
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
bool hasInheritedTarget
Definition: relation.h:300
static int list_length(const List *l)
Definition: pg_list.h:89
#define for_each_cell(cell, initcell)
Definition: pg_list.h:169
struct Plan * lefttree
Definition: plannodes.h:146
List * targetlist
Definition: plannodes.h:144
AggStrategy
Definition: nodes.h:737
List * groupClause
Definition: parsenodes.h:146
Definition: plannodes.h:780
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
List * gsets
Definition: relation.h:1571

◆ create_hashjoin_plan()

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

Definition at line 4074 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, JoinPath::inner_unique, JoinPath::innerjoinpath, InvalidAttrNumber, InvalidOid, is_opclause, IS_OUTER_JOIN, IsA, HashJoin::join, JoinPath::joinrestrictinfo, JoinPath::jointype, HashPath::jpath, linitial, list_difference(), list_length(), make_hash(), make_hashjoin(), NIL, HashPath::num_batches, order_qual_clauses(), JoinPath::outerjoinpath, Path::param_info, Path::parent, JoinPath::path, HashPath::path_hashclauses, Join::plan, Hash::plan, RangeTblEntry::relid, RelOptInfo::relids, replace_nestloop_params(), 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().

4076 {
4077  HashJoin *join_plan;
4078  Hash *hash_plan;
4079  Plan *outer_plan;
4080  Plan *inner_plan;
4081  List *tlist = build_path_tlist(root, &best_path->jpath.path);
4082  List *joinclauses;
4083  List *otherclauses;
4084  List *hashclauses;
4085  Oid skewTable = InvalidOid;
4086  AttrNumber skewColumn = InvalidAttrNumber;
4087  bool skewInherit = false;
4088 
4089  /*
4090  * HashJoin can project, so we don't have to demand exact tlists from the
4091  * inputs. However, it's best to request a small tlist from the inner
4092  * side, so that we aren't storing more data than necessary. Likewise, if
4093  * we anticipate batching, request a small tlist from the outer side so
4094  * that we don't put extra data in the outer batch files.
4095  */
4096  outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4097  (best_path->num_batches > 1) ? CP_SMALL_TLIST : 0);
4098 
4099  inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4100  CP_SMALL_TLIST);
4101 
4102  /* Sort join qual clauses into best execution order */
4103  joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4104  /* There's no point in sorting the hash clauses ... */
4105 
4106  /* Get the join qual clauses (in plain expression form) */
4107  /* Any pseudoconstant clauses are ignored here */
4108  if (IS_OUTER_JOIN(best_path->jpath.jointype))
4109  {
4110  extract_actual_join_clauses(joinclauses,
4111  &joinclauses, &otherclauses);
4112  }
4113  else
4114  {
4115  /* We can treat all clauses alike for an inner join */
4116  joinclauses = extract_actual_clauses(joinclauses, false);
4117  otherclauses = NIL;
4118  }
4119 
4120  /*
4121  * Remove the hashclauses from the list of join qual clauses, leaving the
4122  * list of quals that must be checked as qpquals.
4123  */
4124  hashclauses = get_actual_clauses(best_path->path_hashclauses);
4125  joinclauses = list_difference(joinclauses, hashclauses);
4126 
4127  /*
4128  * Replace any outer-relation variables with nestloop params. There
4129  * should not be any in the hashclauses.
4130  */
4131  if (best_path->jpath.path.param_info)
4132  {
4133  joinclauses = (List *)
4134  replace_nestloop_params(root, (Node *) joinclauses);
4135  otherclauses = (List *)
4136  replace_nestloop_params(root, (Node *) otherclauses);
4137  }
4138 
4139  /*
4140  * Rearrange hashclauses, if needed, so that the outer variable is always
4141  * on the left.
4142  */
4143  hashclauses = get_switched_clauses(best_path->path_hashclauses,
4144  best_path->jpath.outerjoinpath->parent->relids);
4145 
4146  /*
4147  * If there is a single join clause and we can identify the outer variable
4148  * as a simple column reference, supply its identity for possible use in
4149  * skew optimization. (Note: in principle we could do skew optimization
4150  * with multiple join clauses, but we'd have to be able to determine the
4151  * most common combinations of outer values, which we don't currently have
4152  * enough stats for.)
4153  */
4154  if (list_length(hashclauses) == 1)
4155  {
4156  OpExpr *clause = (OpExpr *) linitial(hashclauses);
4157  Node *node;
4158 
4159  Assert(is_opclause(clause));
4160  node = (Node *) linitial(clause->args);
4161  if (IsA(node, RelabelType))
4162  node = (Node *) ((RelabelType *) node)->arg;
4163  if (IsA(node, Var))
4164  {
4165  Var *var = (Var *) node;
4166  RangeTblEntry *rte;
4167 
4168  rte = root->simple_rte_array[var->varno];
4169  if (rte->rtekind == RTE_RELATION)
4170  {
4171  skewTable = rte->relid;
4172  skewColumn = var->varattno;
4173  skewInherit = rte->inh;
4174  }
4175  }
4176  }
4177 
4178  /*
4179  * Build the hash node and hash join node.
4180  */
4181  hash_plan = make_hash(inner_plan,
4182  skewTable,
4183  skewColumn,
4184  skewInherit);
4185 
4186  /*
4187  * Set Hash node's startup & total costs equal to total cost of input
4188  * plan; this only affects EXPLAIN display not decisions.
4189  */
4190  copy_plan_costsize(&hash_plan->plan, inner_plan);
4191  hash_plan->plan.startup_cost = hash_plan->plan.total_cost;
4192 
4193  join_plan = make_hashjoin(tlist,
4194  joinclauses,
4195  otherclauses,
4196  hashclauses,
4197  outer_plan,
4198  (Plan *) hash_plan,
4199  best_path->jpath.jointype,
4200  best_path->jpath.inner_unique);
4201 
4202  copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4203 
4204  return join_plan;
4205 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:562
JoinPath jpath
Definition: relation.h:1458
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4224
static List * get_switched_clauses(List *clauses, Relids outerrelids)
Definition: createplan.c:4705
int num_batches
Definition: relation.h:1460
static HashJoin * make_hashjoin(List *tlist, List *joinclauses, List *otherclauses, List *hashclauses, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
Definition: createplan.c:5420
void extract_actual_join_clauses(List *restrictinfo_list, List **joinquals, List **otherquals)
Definition: restrictinfo.c:383
Path * innerjoinpath
Definition: relation.h:1385
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:724
ParamPathInfo * param_info
Definition: relation.h:1045
#define CP_SMALL_TLIST
Definition: createplan.c:67
Definition: nodes.h:511
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:333
AttrNumber varattno
Definition: primnodes.h:168
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4782
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:163
Join join
Definition: plannodes.h:725
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
#define linitial(l)
Definition: pg_list.h:111
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:729
#define is_opclause(clause)
Definition: clauses.h:20
List * joinrestrictinfo
Definition: relation.h:1387
RelOptInfo * parent
Definition: relation.h:1042
Cost startup_cost
Definition: plannodes.h:125
static void copy_plan_costsize(Plan *dest, Plan *src)
Definition: createplan.c:4891
Relids relids
Definition: relation.h:585
RangeTblEntry ** simple_rte_array
Definition: relation.h:188
Index varno
Definition: primnodes.h:166
Path * outerjoinpath
Definition: relation.h:1384
#define InvalidOid
Definition: postgres_ext.h:36
Path path
Definition: relation.h:1377
#define Assert(condition)
Definition: c.h:670
static int list_length(const List *l)
Definition: pg_list.h:89
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:356
bool inner_unique
Definition: relation.h:1381
List * list_difference(const List *list1, const List *list2)
Definition: list.c:858
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:951
Plan plan
Definition: plannodes.h:877
JoinType jointype
Definition: relation.h:1379
static Hash * make_hash(Plan *lefttree, Oid skewTable, AttrNumber skewColumn, bool skewInherit)
Definition: createplan.c:5445
Cost total_cost
Definition: plannodes.h:126
List * path_hashclauses
Definition: relation.h:1459
List * args
Definition: primnodes.h:502
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
Plan plan
Definition: plannodes.h:666

◆ create_indexscan_plan()

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

Definition at line 2514 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_actual_clauses(), get_opfamily_member(), IndexPath::indexinfo, IndexOptInfo::indexoid, IndexPath::indexorderbys, IndexPath::indexquals, IndexPath::indexscandir, IndexOptInfo::indextlist, is_redundant_derived_clause(), lappend(), lappend_oid(), lfirst, lfirst_node, list_length(), list_make1, list_member_ptr(), 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().

2519 {
2520  Scan *scan_plan;
2521  List *indexquals = best_path->indexquals;
2522  List *indexorderbys = best_path->indexorderbys;
2523  Index baserelid = best_path->path.parent->relid;
2524  Oid indexoid = best_path->indexinfo->indexoid;
2525  List *qpqual;
2526  List *stripped_indexquals;
2527  List *fixed_indexquals;
2528  List *fixed_indexorderbys;
2529  List *indexorderbyops = NIL;
2530  ListCell *l;
2531 
2532  /* it should be a base rel... */
2533  Assert(baserelid > 0);
2534  Assert(best_path->path.parent->rtekind == RTE_RELATION);
2535 
2536  /*
2537  * Build "stripped" indexquals structure (no RestrictInfos) to pass to
2538  * executor as indexqualorig
2539  */
2540  stripped_indexquals = get_actual_clauses(indexquals);
2541 
2542  /*
2543  * The executor needs a copy with the indexkey on the left of each clause
2544  * and with index Vars substituted for table ones.
2545  */
2546  fixed_indexquals = fix_indexqual_references(root, best_path);
2547 
2548  /*
2549  * Likewise fix up index attr references in the ORDER BY expressions.
2550  */
2551  fixed_indexorderbys = fix_indexorderby_references(root, best_path);
2552 
2553  /*
2554  * The qpqual list must contain all restrictions not automatically handled
2555  * by the index, other than pseudoconstant clauses which will be handled
2556  * by a separate gating plan node. All the predicates in the indexquals
2557  * will be checked (either by the index itself, or by nodeIndexscan.c),
2558  * but if there are any "special" operators involved then they must be
2559  * included in qpqual. The upshot is that qpqual must contain
2560  * scan_clauses minus whatever appears in indexquals.
2561  *
2562  * In normal cases simple pointer equality checks will be enough to spot
2563  * duplicate RestrictInfos, so we try that first.
2564  *
2565  * Another common case is that a scan_clauses entry is generated from the
2566  * same EquivalenceClass as some indexqual, and is therefore redundant
2567  * with it, though not equal. (This happens when indxpath.c prefers a
2568  * different derived equality than what generate_join_implied_equalities
2569  * picked for a parameterized scan's ppi_clauses.)
2570  *
2571  * In some situations (particularly with OR'd index conditions) we may
2572  * have scan_clauses that are not equal to, but are logically implied by,
2573  * the index quals; so we also try a predicate_implied_by() check to see
2574  * if we can discard quals that way. (predicate_implied_by assumes its
2575  * first input contains only immutable functions, so we have to check
2576  * that.)
2577  *
2578  * Note: if you change this bit of code you should also look at
2579  * extract_nonindex_conditions() in costsize.c.
2580  */
2581  qpqual = NIL;
2582  foreach(l, scan_clauses)
2583  {
2584  RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
2585 
2586  if (rinfo->pseudoconstant)
2587  continue; /* we may drop pseudoconstants here */
2588  if (list_member_ptr(indexquals, rinfo))
2589  continue; /* simple duplicate */
2590  if (is_redundant_derived_clause(rinfo, indexquals))
2591  continue; /* derived from same EquivalenceClass */
2592  if (!contain_mutable_functions((Node *) rinfo->clause) &&
2593  predicate_implied_by(list_make1(rinfo->clause), indexquals, false))
2594  continue; /* provably implied by indexquals */
2595  qpqual = lappend(qpqual, rinfo);
2596  }
2597 
2598  /* Sort clauses into best execution order */
2599  qpqual = order_qual_clauses(root, qpqual);
2600 
2601  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2602  qpqual = extract_actual_clauses(qpqual, false);
2603 
2604  /*
2605  * We have to replace any outer-relation variables with nestloop params in
2606  * the indexqualorig, qpqual, and indexorderbyorig expressions. A bit
2607  * annoying to have to do this separately from the processing in
2608  * fix_indexqual_references --- rethink this when generalizing the inner
2609  * indexscan support. But note we can't really do this earlier because
2610  * it'd break the comparisons to predicates above ... (or would it? Those
2611  * wouldn't have outer refs)
2612  */
2613  if (best_path->path.param_info)
2614  {
2615  stripped_indexquals = (List *)
2616  replace_nestloop_params(root, (Node *) stripped_indexquals);
2617  qpqual = (List *)
2618  replace_nestloop_params(root, (Node *) qpqual);
2619  indexorderbys = (List *)
2620  replace_nestloop_params(root, (Node *) indexorderbys);
2621  }
2622 
2623  /*
2624  * If there are ORDER BY expressions, look up the sort operators for their
2625  * result datatypes.
2626  */
2627  if (indexorderbys)
2628  {
2629  ListCell *pathkeyCell,
2630  *exprCell;
2631 
2632  /*
2633  * PathKey contains OID of the btree opfamily we're sorting by, but
2634  * that's not quite enough because we need the expression's datatype
2635  * to look up the sort operator in the operator family.
2636  */
2637  Assert(list_length(best_path->path.pathkeys) == list_length(indexorderbys));
2638  forboth(pathkeyCell, best_path->path.pathkeys, exprCell, indexorderbys)
2639  {
2640  PathKey *pathkey = (PathKey *) lfirst(pathkeyCell);
2641  Node *expr = (Node *) lfirst(exprCell);
2642  Oid exprtype = exprType(expr);
2643  Oid sortop;
2644 
2645  /* Get sort operator from opfamily */
2646  sortop = get_opfamily_member(pathkey->pk_opfamily,
2647  exprtype,
2648  exprtype,
2649  pathkey->pk_strategy);
2650  if (!OidIsValid(sortop))
2651  elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
2652  pathkey->pk_strategy, exprtype, exprtype, pathkey->pk_opfamily);
2653  indexorderbyops = lappend_oid(indexorderbyops, sortop);
2654  }
2655  }
2656 
2657  /* Finally ready to build the plan node */
2658  if (indexonly)
2659  scan_plan = (Scan *) make_indexonlyscan(tlist,
2660  qpqual,
2661  baserelid,
2662  indexoid,
2663  fixed_indexquals,
2664  fixed_indexorderbys,
2665  best_path->indexinfo->indextlist,
2666  best_path->indexscandir);
2667  else
2668  scan_plan = (Scan *) make_indexscan(tlist,
2669  qpqual,
2670  baserelid,
2671  indexoid,
2672  fixed_indexquals,
2673  stripped_indexquals,
2674  fixed_indexorderbys,
2675  indexorderbys,
2676  indexorderbyops,
2677  best_path->indexscandir);
2678 
2679  copy_generic_path_info(&scan_plan->plan, &best_path->path);
2680 
2681  return scan_plan;
2682 }
#define NIL
Definition: pg_list.h:69
bool predicate_implied_by(List *predicate_list, List *clause_list, bool clause_is_check)
Definition: predtest.c:135
Plan plan
Definition: plannodes.h:328
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4224
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
Path path
Definition: relation.h:1118
bool is_redundant_derived_clause(RestrictInfo *rinfo, List *clauselist)
Definition: equivclass.c:2494
IndexOptInfo * indexinfo
Definition: relation.h:1119
List * indextlist
Definition: relation.h:744
bool pseudoconstant
Definition: relation.h:1843
ParamPathInfo * param_info
Definition: relation.h:1045
Definition: nodes.h:511
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:333
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4782
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
#define OidIsValid(objectId)
Definition: c.h:576
int pk_strategy
Definition: relation.h:941
#define list_make1(x1)
Definition: pg_list.h:139
List * indexquals
Definition: relation.h:1121
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
#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:5007
RelOptInfo * parent
Definition: relation.h:1042
#define lfirst_node(type, lc)
Definition: pg_list.h:109
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
static List * fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path)
Definition: createplan.c:4573
static List * fix_indexqual_references(PlannerInfo *root, IndexPath *index_path)
Definition: createplan.c:4442
Index relid
Definition: relation.h:613
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1835
List * indexorderbys
Definition: relation.h:1123
unsigned int Index
Definition: c.h:413
RTEKind rtekind
Definition: relation.h:615
bool list_member_ptr(const List *list, const void *datum)
Definition: list.c:465
List * pathkeys
Definition: relation.h:1056
static IndexOnlyScan * make_indexonlyscan(List *qptlist, List *qpqual, Index scanrelid, Oid indexid, List *indexqual, List *indexorderby, List *indextlist, ScanDirection indexscandir)
Definition: createplan.c:5038
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:89
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:356
Oid pk_opfamily
Definition: relation.h:940
ScanDirection indexscandir
Definition: relation.h:1125
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:878
#define elog
Definition: elog.h:219
Oid indexoid
Definition: relation.h:719
Definition: pg_list.h:45

◆ create_join_plan()

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

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

953 {
954  Plan *plan;
955  List *gating_clauses;
956 
957  switch (best_path->path.pathtype)
958  {
959  case T_MergeJoin:
960  plan = (Plan *) create_mergejoin_plan(root,
961  (MergePath *) best_path);
962  break;
963  case T_HashJoin:
964  plan = (Plan *) create_hashjoin_plan(root,
965  (HashPath *) best_path);
966  break;
967  case T_NestLoop:
968  plan = (Plan *) create_nestloop_plan(root,
969  (NestPath *) best_path);
970  break;
971  default:
972  elog(ERROR, "unrecognized node type: %d",
973  (int) best_path->path.pathtype);
974  plan = NULL; /* keep compiler quiet */
975  break;
976  }
977 
978  /*
979  * If there are any pseudoconstant clauses attached to this node, insert a
980  * gating Result node that evaluates the pseudoconstants as one-time
981  * quals.
982  */
983  gating_clauses = get_gating_quals(root, best_path->joinrestrictinfo);
984  if (gating_clauses)
985  plan = create_gating_plan(root, (Path *) best_path, plan,
986  gating_clauses);
987 
988 #ifdef NOT_USED
989 
990  /*
991  * * Expensive function pullups may have pulled local predicates * into
992  * this path node. Put them in the qpqual of the plan node. * JMH,
993  * 6/15/92
994  */
995  if (get_loc_restrictinfo(best_path) != NIL)
996  set_qpqual((Plan) plan,
997  list_concat(get_qpqual((Plan) plan),
998  get_actual_clauses(get_loc_restrictinfo(best_path))));
999 #endif
1000 
1001  return plan;
1002 }
#define NIL
Definition: pg_list.h:69
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:333
List * list_concat(List *list1, List *list2)
Definition: list.c:321
static NestLoop * create_nestloop_plan(PlannerInfo *root, NestPath *best_path)
Definition: createplan.c:3657
NodeTag pathtype
Definition: relation.h:1040
#define ERROR
Definition: elog.h:43
List * joinrestrictinfo
Definition: relation.h:1387
Path path
Definition: relation.h:1377
static HashJoin * create_hashjoin_plan(PlannerInfo *root, HashPath *best_path)
Definition: createplan.c:4074
static Plan * create_gating_plan(PlannerInfo *root, Path *path, Plan *plan, List *gating_quals)
Definition: createplan.c:910
#define elog
Definition: elog.h:219
static MergeJoin * create_mergejoin_plan(PlannerInfo *root, MergePath *best_path)
Definition: createplan.c:3763
Definition: pg_list.h:45
static List * get_gating_quals(PlannerInfo *root, List *quals)
Definition: createplan.c:890

◆ create_limit_plan()

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

Definition at line 2394 of file createplan.c.

References copy_generic_path_info(), create_plan_recurse(), LimitPath::limitCount, LimitPath::limitOffset, make_limit(), Limit::plan, and LimitPath::subpath.

Referenced by create_plan_recurse().

2395 {
2396  Limit *plan;
2397  Plan *subplan;
2398 
2399  /* Limit doesn't project, so tlist requirements pass through */
2400  subplan = create_plan_recurse(root, best_path->subpath, flags);
2401 
2402  plan = make_limit(subplan,
2403  best_path->limitOffset,
2404  best_path->limitCount);
2405 
2406  copy_generic_path_info(&plan->plan, (Path *) best_path);
2407 
2408  return plan;
2409 }
Plan plan
Definition: plannodes.h:927
Node * limitOffset
Definition: relation.h:1687
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
Path * subpath
Definition: relation.h:1686
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
Definition: createplan.c:6367
Node * limitCount
Definition: relation.h:1688

◆ create_lockrows_plan()

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

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

2314 {
2315  LockRows *plan;
2316  Plan *subplan;
2317 
2318  /* LockRows doesn't project, so tlist requirements pass through */
2319  subplan = create_plan_recurse(root, best_path->subpath, flags);
2320 
2321  plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
2322 
2323  copy_generic_path_info(&plan->plan, (Path *) best_path);
2324 
2325  return plan;
2326 }
Plan plan
Definition: plannodes.h:913
List * rowMarks
Definition: relation.h:1651
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
Path * subpath
Definition: relation.h:1650
static LockRows * make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
Definition: createplan.c:6346

◆ create_material_plan()

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

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

1237 {
1238  Material *plan;
1239  Plan *subplan;
1240 
1241  /*
1242  * We don't want any excess columns in the materialized tuples, so request
1243  * a smaller tlist. Otherwise, since Material doesn't project, tlist
1244  * requirements pass through.
1245  */
1246  subplan = create_plan_recurse(root, best_path->subpath,
1247  flags | CP_SMALL_TLIST);
1248 
1249  plan = make_material(subplan);
1250 
1251  copy_generic_path_info(&plan->plan, (Path *) best_path);
1252 
1253  return plan;
1254 }
#define CP_SMALL_TLIST
Definition: createplan.c:67
Path * subpath
Definition: relation.h:1314
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
static Material * make_material(Plan *lefttree)
Definition: createplan.c:5971
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
Plan plan
Definition: plannodes.h:735

◆ create_merge_append_plan()

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

Definition at line 1077 of file createplan.c.

References Assert, build_path_tlist(), MergeAppend::collations, copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), elog, ERROR, label_sort_with_costsize(), lappend(), Plan::lefttree, lfirst, make_sort(), makeNode, MergeAppend::mergeplans, NIL, MergeAppend::nullsFirst, MergeAppend::numCols, Path::parent, MergeAppend::partitioned_rels, MergeAppendPath::path, Path::pathkeys, pathkeys_contained_in(), MergeAppend::plan, prepare_sort_from_pathkeys(), Plan::qual, RelOptInfo::relids, Plan::righttree, sort(), MergeAppend::sortColIdx, MergeAppend::sortOperators, subpath(), and Plan::targetlist.

Referenced by create_plan_recurse().

1078 {
1079  MergeAppend *node = makeNode(MergeAppend);
1080  Plan *plan = &node->plan;
1081  List *tlist = build_path_tlist(root, &best_path->path);
1082  List *pathkeys = best_path->path.pathkeys;
1083  List *subplans = NIL;
1084  ListCell *subpaths;
1085 
1086  /*
1087  * We don't have the actual creation of the MergeAppend node split out
1088  * into a separate make_xxx function. This is because we want to run
1089  * prepare_sort_from_pathkeys on it before we do so on the individual
1090  * child plans, to make cross-checking the sort info easier.
1091  */
1092  copy_generic_path_info(plan, (Path *) best_path);
1093  plan->targetlist = tlist;
1094  plan->qual = NIL;
1095  plan->lefttree = NULL;
1096  plan->righttree = NULL;
1097 
1098  /* Compute sort column info, and adjust MergeAppend's tlist as needed */
1099  (void) prepare_sort_from_pathkeys(plan, pathkeys,
1100  best_path->path.parent->relids,
1101  NULL,
1102  true,
1103  &node->numCols,
1104  &node->sortColIdx,
1105  &node->sortOperators,
1106  &node->collations,
1107  &node->nullsFirst);
1108 
1109  /*
1110  * Now prepare the child plans. We must apply prepare_sort_from_pathkeys
1111  * even to subplans that don't need an explicit sort, to make sure they
1112  * are returning the same sort key columns the MergeAppend expects.
1113  */
1114  foreach(subpaths, best_path->subpaths)
1115  {
1116  Path *subpath = (Path *) lfirst(subpaths);
1117  Plan *subplan;
1118  int numsortkeys;
1119  AttrNumber *sortColIdx;
1120  Oid *sortOperators;
1121  Oid *collations;
1122  bool *nullsFirst;
1123 
1124  /* Build the child plan */
1125  /* Must insist that all children return the same tlist */
1126  subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
1127 
1128  /* Compute sort column info, and adjust subplan's tlist as needed */
1129  subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1130  subpath->parent->relids,
1131  node->sortColIdx,
1132  false,
1133  &numsortkeys,
1134  &sortColIdx,
1135  &sortOperators,
1136  &collations,
1137  &nullsFirst);
1138 
1139  /*
1140  * Check that we got the same sort key information. We just Assert
1141  * that the sortops match, since those depend only on the pathkeys;
1142  * but it seems like a good idea to check the sort column numbers
1143  * explicitly, to ensure the tlists really do match up.
1144  */
1145  Assert(numsortkeys == node->numCols);
1146  if (memcmp(sortColIdx, node->sortColIdx,
1147  numsortkeys * sizeof(AttrNumber)) != 0)
1148  elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
1149  Assert(memcmp(sortOperators, node->sortOperators,
1150  numsortkeys * sizeof(Oid)) == 0);
1151  Assert(memcmp(collations, node->collations,
1152  numsortkeys * sizeof(Oid)) == 0);
1153  Assert(memcmp(nullsFirst, node->nullsFirst,
1154  numsortkeys * sizeof(bool)) == 0);
1155 
1156  /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1157  if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
1158  {
1159  Sort *sort = make_sort(subplan, numsortkeys,
1160  sortColIdx, sortOperators,
1161  collations, nullsFirst);
1162 
1163  label_sort_with_costsize(root, sort, best_path->limit_tuples);
1164  subplan = (Plan *) sort;
1165  }
1166 
1167  subplans = lappend(subplans, subplan);
1168  }
1169 
1170  node->partitioned_rels = best_path->partitioned_rels;
1171  node->mergeplans = subplans;
1172 
1173  return (Plan *) node;
1174 }
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:145
Oid * collations
Definition: plannodes.h:268
List * partitioned_rels
Definition: relation.h:1287
unsigned int Oid
Definition: postgres_ext.h:31
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
struct Plan * righttree
Definition: plannodes.h:147
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
#define ERROR
Definition: elog.h:43
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:729
RelOptInfo * parent
Definition: relation.h:1042
List * partitioned_rels
Definition: plannodes.h:262
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:200
Relids relids
Definition: relation.h:585
List * lappend(List *list, void *datum)
Definition: list.c:128
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:317
List * pathkeys
Definition: relation.h:1056
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
Definition: createplan.c:5507
#define makeNode(_type_)
Definition: nodes.h:559
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
AttrNumber * sortColIdx
Definition: plannodes.h:266
struct Plan * lefttree
Definition: plannodes.h:146
List * targetlist
Definition: plannodes.h:144
bool * nullsFirst
Definition: plannodes.h:269
List * mergeplans
Definition: plannodes.h:263
Oid * sortOperators
Definition: plannodes.h:267
List * subpaths
Definition: relation.h:1288
#define elog
Definition: elog.h:219
#define CP_EXACT_TLIST
Definition: createplan.c:66
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
static void label_sort_with_costsize(PlannerInfo *root, Sort *plan, double limit_tuples)
Definition: createplan.c:4913
double limit_tuples
Definition: relation.h:1289
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:234
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:5569

◆ create_mergejoin_plan()

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

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

3765 {
3766  MergeJoin *join_plan;
3767  Plan *outer_plan;
3768  Plan *inner_plan;
3769  List *tlist = build_path_tlist(root, &best_path->jpath.path);
3770  List *joinclauses;
3771  List *otherclauses;
3772  List *mergeclauses;
3773  List *outerpathkeys;
3774  List *innerpathkeys;
3775  int nClauses;
3776  Oid *mergefamilies;
3777  Oid *mergecollations;
3778  int *mergestrategies;
3779  bool *mergenullsfirst;
3780  int i;
3781  ListCell *lc;
3782  ListCell *lop;
3783  ListCell *lip;
3784  Path *outer_path = best_path->jpath.outerjoinpath;
3785  Path *inner_path = best_path->jpath.innerjoinpath;
3786 
3787  /*
3788  * MergeJoin can project, so we don't have to demand exact tlists from the
3789  * inputs. However, if we're intending to sort an input's result, it's
3790  * best to request a small tlist so we aren't sorting more data than
3791  * necessary.
3792  */
3793  outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
3794  (best_path->outersortkeys != NIL) ? CP_SMALL_TLIST : 0);
3795 
3796  inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
3797  (best_path->innersortkeys != NIL) ? CP_SMALL_TLIST : 0);
3798 
3799  /* Sort join qual clauses into best execution order */
3800  /* NB: do NOT reorder the mergeclauses */
3801  joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
3802 
3803  /* Get the join qual clauses (in plain expression form) */
3804  /* Any pseudoconstant clauses are ignored here */
3805  if (IS_OUTER_JOIN(best_path->jpath.jointype))
3806  {
3807  extract_actual_join_clauses(joinclauses,
3808  &joinclauses, &otherclauses);
3809  }
3810  else
3811  {
3812  /* We can treat all clauses alike for an inner join */
3813  joinclauses = extract_actual_clauses(joinclauses, false);
3814  otherclauses = NIL;
3815  }
3816 
3817  /*
3818  * Remove the mergeclauses from the list of join qual clauses, leaving the
3819  * list of quals that must be checked as qpquals.
3820  */
3821  mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
3822  joinclauses = list_difference(joinclauses, mergeclauses);
3823 
3824  /*
3825  * Replace any outer-relation variables with nestloop params. There
3826  * should not be any in the mergeclauses.
3827  */
3828  if (best_path->jpath.path.param_info)
3829  {
3830  joinclauses = (List *)
3831  replace_nestloop_params(root, (Node *) joinclauses);
3832  otherclauses = (List *)
3833  replace_nestloop_params(root, (Node *) otherclauses);
3834  }
3835 
3836  /*
3837  * Rearrange mergeclauses, if needed, so that the outer variable is always
3838  * on the left; mark the mergeclause restrictinfos with correct
3839  * outer_is_left status.
3840  */
3841  mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
3842  best_path->jpath.outerjoinpath->parent->relids);
3843 
3844  /*
3845  * Create explicit sort nodes for the outer and inner paths if necessary.
3846  */
3847  if (best_path->outersortkeys)
3848  {
3849  Relids outer_relids = outer_path->parent->relids;
3850  Sort *sort = make_sort_from_pathkeys(outer_plan,
3851  best_path->outersortkeys,
3852  outer_relids);
3853 
3854  label_sort_with_costsize(root, sort, -1.0);
3855  outer_plan = (Plan *) sort;
3856  outerpathkeys = best_path->outersortkeys;
3857  }
3858  else
3859  outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
3860 
3861  if (best_path->innersortkeys)
3862  {
3863  Relids inner_relids = inner_path->parent->relids;
3864  Sort *sort = make_sort_from_pathkeys(inner_plan,
3865  best_path->innersortkeys,
3866  inner_relids);
3867 
3868  label_sort_with_costsize(root, sort, -1.0);
3869  inner_plan = (Plan *) sort;
3870  innerpathkeys = best_path->innersortkeys;
3871  }
3872  else
3873  innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
3874 
3875  /*
3876  * If specified, add a materialize node to shield the inner plan from the
3877  * need to handle mark/restore.
3878  */
3879  if (best_path->materialize_inner)
3880  {
3881  Plan *matplan = (Plan *) make_material(inner_plan);
3882 
3883  /*
3884  * We assume the materialize will not spill to disk, and therefore
3885  * charge just cpu_operator_cost per tuple. (Keep this estimate in
3886  * sync with final_cost_mergejoin.)
3887  */
3888  copy_plan_costsize(matplan, inner_plan);
3889  matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
3890 
3891  inner_plan = matplan;
3892  }
3893 
3894  /*
3895  * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
3896  * executor. The information is in the pathkeys for the two inputs, but
3897  * we need to be careful about the possibility of mergeclauses sharing a
3898  * pathkey (compare find_mergeclauses_for_pathkeys()).
3899  */
3900  nClauses = list_length(mergeclauses);
3901  Assert(nClauses == list_length(best_path->path_mergeclauses));
3902  mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
3903  mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
3904  mergestrategies = (int *) palloc(nClauses * sizeof(int));
3905  mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
3906 
3907  lop = list_head(outerpathkeys);
3908  lip = list_head(innerpathkeys);
3909  i = 0;
3910  foreach(lc, best_path->path_mergeclauses)
3911  {
3912  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
3913  EquivalenceClass *oeclass;
3914  EquivalenceClass *ieclass;
3915  PathKey *opathkey;
3916  PathKey *ipathkey;
3917  EquivalenceClass *opeclass;
3918  EquivalenceClass *ipeclass;
3919  ListCell *l2;
3920 
3921  /* fetch outer/inner eclass from mergeclause */
3922  if (rinfo->outer_is_left)
3923  {
3924  oeclass = rinfo->left_ec;
3925  ieclass = rinfo->right_ec;
3926  }
3927  else
3928  {
3929  oeclass = rinfo->right_ec;
3930  ieclass = rinfo->left_ec;
3931  }
3932  Assert(oeclass != NULL);
3933  Assert(ieclass != NULL);
3934 
3935  /*
3936  * For debugging purposes, we check that the eclasses match the paths'
3937  * pathkeys. In typical cases the merge clauses are one-to-one with
3938  * the pathkeys, but when dealing with partially redundant query
3939  * conditions, we might have clauses that re-reference earlier path
3940  * keys. The case that we need to reject is where a pathkey is
3941  * entirely skipped over.
3942  *
3943  * lop and lip reference the first as-yet-unused pathkey elements;
3944  * it's okay to match them, or any element before them. If they're
3945  * NULL then we have found all pathkey elements to be used.
3946  */
3947  if (lop)
3948  {
3949  opathkey = (PathKey *) lfirst(lop);
3950  opeclass = opathkey->pk_eclass;
3951  if (oeclass == opeclass)
3952  {
3953  /* fast path for typical case */
3954  lop = lnext(lop);
3955  }
3956  else
3957  {
3958  /* redundant clauses ... must match something before lop */
3959  foreach(l2, outerpathkeys)
3960  {
3961  if (l2 == lop)
3962  break;
3963  opathkey = (PathKey *) lfirst(l2);
3964  opeclass = opathkey->pk_eclass;
3965  if (oeclass == opeclass)
3966  break;
3967  }
3968  if (oeclass != opeclass)
3969  elog(ERROR, "outer pathkeys do not match mergeclauses");
3970  }
3971  }
3972  else
3973  {
3974  /* redundant clauses ... must match some already-used pathkey */
3975  opathkey = NULL;
3976  opeclass = NULL;
3977  foreach(l2, outerpathkeys)
3978  {
3979  opathkey = (PathKey *) lfirst(l2);
3980  opeclass = opathkey->pk_eclass;
3981  if (oeclass == opeclass)
3982  break;
3983  }
3984  if (l2 == NULL)
3985  elog(ERROR, "outer pathkeys do not match mergeclauses");
3986  }
3987 
3988  if (lip)
3989  {
3990  ipathkey = (PathKey *) lfirst(lip);
3991  ipeclass = ipathkey->pk_eclass;
3992  if (ieclass == ipeclass)
3993  {
3994  /* fast path for typical case */
3995  lip = lnext(lip);
3996  }
3997  else
3998  {
3999  /* redundant clauses ... must match something before lip */
4000  foreach(l2, innerpathkeys)
4001  {
4002  if (l2 == lip)
4003  break;
4004  ipathkey = (PathKey *) lfirst(l2);
4005  ipeclass = ipathkey->pk_eclass;
4006  if (ieclass == ipeclass)
4007  break;
4008  }
4009  if (ieclass != ipeclass)
4010  elog(ERROR, "inner pathkeys do not match mergeclauses");
4011  }
4012  }
4013  else
4014  {
4015  /* redundant clauses ... must match some already-used pathkey */
4016  ipathkey = NULL;
4017  ipeclass = NULL;
4018  foreach(l2, innerpathkeys)
4019  {
4020  ipathkey = (PathKey *) lfirst(l2);
4021  ipeclass = ipathkey->pk_eclass;
4022  if (ieclass == ipeclass)
4023  break;
4024  }
4025  if (l2 == NULL)
4026  elog(ERROR, "inner pathkeys do not match mergeclauses");
4027  }
4028 
4029  /* pathkeys should match each other too (more debugging) */
4030  if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
4031  opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation ||
4032  opathkey->pk_strategy != ipathkey->pk_strategy ||
4033  opathkey->pk_nulls_first != ipathkey->pk_nulls_first)
4034  elog(ERROR, "left and right pathkeys do not match in mergejoin");
4035 
4036  /* OK, save info for executor */
4037  mergefamilies[i] = opathkey->pk_opfamily;
4038  mergecollations[i] = opathkey->pk_eclass->ec_collation;
4039  mergestrategies[i] = opathkey->pk_strategy;
4040  mergenullsfirst[i] = opathkey->pk_nulls_first;
4041  i++;
4042  }
4043 
4044  /*
4045  * Note: it is not an error if we have additional pathkey elements (i.e.,
4046  * lop or lip isn't NULL here). The input paths might be better-sorted
4047  * than we need for the current mergejoin.
4048  */
4049 
4050  /*
4051  * Now we can build the mergejoin node.
4052  */
4053  join_plan = make_mergejoin(tlist,
4054  joinclauses,
4055  otherclauses,
4056  mergeclauses,
4057  mergefamilies,
4058  mergecollations,
4059  mergestrategies,
4060  mergenullsfirst,
4061  outer_plan,
4062  inner_plan,
4063  best_path->jpath.jointype,
4064  best_path->jpath.inner_unique,
4065  best_path->skip_mark_restore);
4066 
4067  /* Costs of sort and material steps are included in path cost already */
4068  copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4069 
4070  return join_plan;
4071 }
#define NIL
Definition: pg_list.h:69
List * path_mergeclauses
Definition: relation.h:1440
List * outersortkeys
Definition: relation.h:1441
double plan_rows
Definition: plannodes.h:131
static Sort * make_sort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids)
Definition: createplan.c:5848
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4224
static List * get_switched_clauses(List *clauses, Relids outerrelids)
Definition: createplan.c:4705
bool materialize_inner
Definition: relation.h:1444
void extract_actual_join_clauses(List *restrictinfo_list, List **joinquals, List **otherquals)
Definition: restrictinfo.c:383
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:5466
Path * innerjoinpath
Definition: relation.h:1385
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:724
ParamPathInfo * param_info
Definition: relation.h:1045
#define CP_SMALL_TLIST
Definition: createplan.c:67
Definition: nodes.h:511
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:333
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4782
EquivalenceClass * right_ec
Definition: relation.h:1884
unsigned int Oid
Definition: postgres_ext.h:31
int pk_strategy
Definition: relation.h:941
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
static Material * make_material(Plan *lefttree)
Definition: createplan.c:5971
bool skip_mark_restore
Definition: relation.h:1443
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
bool pk_nulls_first
Definition: relation.h:942
#define ERROR
Definition: elog.h:43
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:729
List * joinrestrictinfo
Definition: relation.h:1387
RelOptInfo * parent
Definition: relation.h:1042
#define lfirst_node(type, lc)
Definition: pg_list.h:109
bool outer_is_left
Definition: relation.h:1890
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:200
static void copy_plan_costsize(Plan *dest, Plan *src)
Definition: createplan.c:4891
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
Relids relids
Definition: relation.h:585
double cpu_operator_cost
Definition: costsize.c:108
#define lnext(lc)
Definition: pg_list.h:105
Path * outerjoinpath
Definition: relation.h:1384
List * pathkeys
Definition: relation.h:1056
Path path
Definition: relation.h:1377
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
EquivalenceClass * pk_eclass
Definition: relation.h:939
static int list_length(const List *l)
Definition: pg_list.h:89
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:356
bool inner_unique
Definition: relation.h:1381
List * list_difference(const List *list1, const List *list2)
Definition: list.c:858
List * innersortkeys
Definition: relation.h:1442
Oid pk_opfamily
Definition: relation.h:940
void * palloc(Size size)
Definition: mcxt.c:848
EquivalenceClass * left_ec
Definition: relation.h:1883
Join join
Definition: plannodes.h:709
JoinType jointype
Definition: relation.h:1379
int i
Cost total_cost
Definition: plannodes.h:126
JoinPath jpath
Definition: relation.h:1439
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
static void label_sort_with_costsize(PlannerInfo *root, Sort *plan, double limit_tuples)
Definition: createplan.c:4913
Plan plan
Definition: plannodes.h:666

◆ create_minmaxagg_plan()

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

Definition at line 1985 of file createplan.c.

References Assert, build_path_tlist(), copy_generic_path_info(), create_plan(), PlannerInfo::hasInheritedTarget, lfirst, Query::limitCount, Query::limitOffset, 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().

1986 {
1987  Result *plan;
1988  List *tlist;
1989  ListCell *lc;
1990 
1991  /* Prepare an InitPlan for each aggregate's subquery. */
1992  foreach(lc, best_path->mmaggregates)
1993  {
1994  MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
1995  PlannerInfo *subroot = mminfo->subroot;
1996  Query *subparse = subroot->parse;
1997  Plan *plan;
1998 
1999  /*
2000  * Generate the plan for the subquery. We already have a Path, but we
2001  * have to convert it to a Plan and attach a LIMIT node above it.
2002  * Since we are entering a different planner context (subroot),
2003  * recurse to create_plan not create_plan_recurse.
2004  */
2005  plan = create_plan(subroot, mminfo->path);
2006 
2007  plan = (Plan *) make_limit(plan,
2008  subparse->limitOffset,
2009  subparse->limitCount);
2010 
2011  /* Must apply correct cost/width data to Limit node */
2012  plan->startup_cost = mminfo->path->startup_cost;
2013  plan->total_cost = mminfo->pathcost;
2014  plan->plan_rows = 1;
2015  plan->plan_width = mminfo->path->pathtarget->width;
2016  plan->parallel_aware = false;
2017  plan->parallel_safe = mminfo->path->parallel_safe;
2018 
2019  /* Convert the plan into an InitPlan in the outer query. */
2020  SS_make_initplan_from_plan(root, subroot, plan, mminfo->param);
2021  }
2022 
2023  /* Generate the output plan --- basically just a Result */
2024  tlist = build_path_tlist(root, &best_path->path);
2025 
2026  plan = make_result(tlist, (Node *) best_path->quals, NULL);
2027 
2028  copy_generic_path_info(&plan->plan, (Path *) best_path);
2029 
2030  /*
2031  * During setrefs.c, we'll need to replace references to the Agg nodes
2032  * with InitPlan output params. (We can't just do that locally in the
2033  * MinMaxAgg node, because path nodes above here may have Agg references
2034  * as well.) Save the mmaggregates list to tell setrefs.c to do that.
2035  *
2036  * This doesn't work if we're in an inheritance subtree (see notes in
2037  * create_modifytable_plan). Fortunately we can't be because there would
2038  * never be aggregates in an UPDATE/DELETE; but let's Assert that.
2039  */
2040  Assert(!root->hasInheritedTarget);
2041  Assert(root->minmax_aggs == NIL);
2042  root->minmax_aggs = best_path->mmaggregates;
2043 
2044  return plan;
2045 }
Node * limitOffset
Definition: parsenodes.h:158
#define NIL
Definition: pg_list.h:69
double plan_rows
Definition: plannodes.h:131
PathTarget * pathtarget
Definition: relation.h:1043
Query * parse
Definition: relation.h:155
Param * param
Definition: relation.h:2175
Definition: nodes.h:511
List * minmax_aggs
Definition: relation.h:288
List * quals
Definition: relation.h:1598
Plan * create_plan(PlannerInfo *root, Path *best_path)
Definition: createplan.c:304
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
Path * path
Definition: relation.h:2173
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:729
Cost startup_cost
Definition: relation.h:1053
Node * limitCount
Definition: parsenodes.h:159
Cost startup_cost
Definition: plannodes.h:125
bool parallel_aware
Definition: plannodes.h:137
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6388
List * mmaggregates
Definition: relation.h:1597
int plan_width
Definition: plannodes.h:132
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
bool parallel_safe
Definition: relation.h:1048
bool hasInheritedTarget
Definition: relation.h:300
void SS_make_initplan_from_plan(PlannerInfo *root, PlannerInfo *subroot, Plan *plan, Param *prm)
Definition: subselect.c:2984
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
Definition: createplan.c:6367
int width
Definition: relation.h:975
Cost total_cost
Definition: plannodes.h:126
bool parallel_safe
Definition: plannodes.h:138
Definition: pg_list.h:45
PlannerInfo * subroot
Definition: relation.h:2172

◆ create_modifytable_plan()

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

Definition at line 2335 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::partitioned_rels, ModifyTablePath::path, ModifyTable::plan, PlannerInfo::processed_tlist, ModifyTablePath::resultRelations, ModifyTablePath::returningLists, ModifyTablePath::rowMarks, subpath(), ModifyTablePath::subpaths, ModifyTablePath::subroots, Plan::targetlist, and ModifyTablePath::withCheckOptionLists.

Referenced by create_plan_recurse().

2336 {
2337  ModifyTable *plan;
2338  List *subplans = NIL;
2339  ListCell *subpaths,
2340  *subroots;
2341 
2342  /* Build the plan for each input path */
2343  forboth(subpaths, best_path->subpaths,
2344  subroots, best_path->subroots)
2345  {
2346  Path *subpath = (Path *) lfirst(subpaths);
2347  PlannerInfo *subroot = (PlannerInfo *) lfirst(subroots);
2348  Plan *subplan;
2349 
2350  /*
2351  * In an inherited UPDATE/DELETE, reference the per-child modified
2352  * subroot while creating Plans from Paths for the child rel. This is
2353  * a kluge, but otherwise it's too hard to ensure that Plan creation
2354  * functions (particularly in FDWs) don't depend on the contents of
2355  * "root" matching what they saw at Path creation time. The main
2356  * downside is that creation functions for Plans that might appear
2357  * below a ModifyTable cannot expect to modify the contents of "root"
2358  * and have it "stick" for subsequent processing such as setrefs.c.
2359  * That's not great, but it seems better than the alternative.
2360  */
2361  subplan = create_plan_recurse(subroot, subpath, CP_EXACT_TLIST);
2362 
2363  /* Transfer resname/resjunk labeling, too, to keep executor happy */
2364  apply_tlist_labeling(subplan->targetlist, subroot->processed_tlist);
2365 
2366  subplans = lappend(subplans, subplan);
2367  }
2368 
2369  plan = make_modifytable(root,
2370  best_path->operation,
2371  best_path->canSetTag,
2372  best_path->nominalRelation,
2373  best_path->partitioned_rels,
2374  best_path->resultRelations,
2375  subplans,
2376  best_path->withCheckOptionLists,
2377  best_path->returningLists,
2378  best_path->rowMarks,
2379  best_path->onconflict,
2380  best_path->epqParam);
2381 
2382  copy_generic_path_info(&plan->plan, &best_path->path);
2383 
2384  return plan;
2385 }
#define NIL
Definition: pg_list.h:69
void apply_tlist_labeling(List *dest_tlist, List *src_tlist)
Definition: tlist.c:321
List * returningLists
Definition: relation.h:1674
OnConflictExpr * onconflict
Definition: relation.h:1676
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
Index nominalRelation
Definition: relation.h:1667
List * rowMarks
Definition: relation.h:1675
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
List * subroots
Definition: relation.h:1672
List * subpaths
Definition: relation.h:1671
List * lappend(List *list, void *datum)
Definition: list.c:128
List * partitioned_rels
Definition: relation.h:1669
static ModifyTable * make_modifytable(PlannerInfo *root, CmdType operation, bool canSetTag, Index nominalRelation, List *partitioned_rels, List *resultRelations, List *subplans, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, int epqParam)
Definition: createplan.c:6428
#define lfirst(lc)
Definition: pg_list.h:106
List * targetlist
Definition: plannodes.h:144
List * withCheckOptionLists
Definition: relation.h:1673
CmdType operation
Definition: relation.h:1665
List * resultRelations
Definition: relation.h:1670
List * processed_tlist
Definition: relation.h:284
#define CP_EXACT_TLIST
Definition: createplan.c:66
Definition: pg_list.h:45
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:234

◆ create_namedtuplestorescan_plan()

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

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

3346 {
3347  NamedTuplestoreScan *scan_plan;
3348  Index scan_relid = best_path->parent->relid;
3349  RangeTblEntry *rte;
3350 
3351  Assert(scan_relid > 0);
3352  rte = planner_rt_fetch(scan_relid, root);
3354 
3355  /* Sort clauses into best execution order */
3356  scan_clauses = order_qual_clauses(root, scan_clauses);
3357 
3358  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3359  scan_clauses = extract_actual_clauses(scan_clauses, false);
3360 
3361  /* Replace any outer-relation variables with nestloop params */
3362  if (best_path->param_info)
3363  {
3364  scan_clauses = (List *)
3365  replace_nestloop_params(root, (Node *) scan_clauses);
3366  }
3367 
3368  scan_plan = make_namedtuplestorescan(tlist, scan_clauses, scan_relid,
3369  rte->enrname);
3370 
3371  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3372 
3373  return scan_plan;
3374 }
Plan plan
Definition: plannodes.h:328
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4224
ParamPathInfo * param_info
Definition: relation.h:1045
Definition: nodes.h:511
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4782
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
#define planner_rt_fetch(rti, root)
Definition: relation.h:328
RelOptInfo * parent
Definition: relation.h:1042
char * enrname
Definition: parsenodes.h:1048
Index relid
Definition: relation.h:613
static NamedTuplestoreScan * make_namedtuplestorescan(List *qptlist, List *qpqual, Index scanrelid, char *enrname)
Definition: createplan.c:5224
unsigned int Index
Definition: c.h:413
#define Assert(condition)
Definition: c.h:670
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:356
RTEKind rtekind
Definition: parsenodes.h:951
Definition: pg_list.h:45

◆ create_nestloop_plan()

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

Definition at line 3657 of file createplan.c.

References bms_free(), bms_is_member(), bms_is_subset(), bms_overlap(), bms_union(), build_path_tlist(), copy_generic_path_info(), create_plan_recurse(), PlannerInfo::curOuterParams, PlannerInfo::curOuterRels, extract_actual_clauses(), extract_actual_join_clauses(), find_placeholder_info(), JoinPath::inner_unique, JoinPath::innerjoinpath, IS_OUTER_JOIN, IsA, NestLoop::join, JoinPath::joinrestrictinfo, JoinPath::jointype, lappend(), lfirst, list_delete_cell(), list_head(), lnext, make_nestloop(), next, NIL, order_qual_clauses(), JoinPath::outerjoinpath, Path::param_info, NestLoopParam::paramval, Path::parent, JoinPath::path, PlaceHolderInfo::ph_eval_at, Join::plan, RelOptInfo::relids, replace_nestloop_params(), and Var::varno.

Referenced by create_join_plan().

3659 {
3660  NestLoop *join_plan;
3661  Plan *outer_plan;
3662  Plan *inner_plan;
3663  List *tlist = build_path_tlist(root, &best_path->path);
3664  List *joinrestrictclauses = best_path->joinrestrictinfo;
3665  List *joinclauses;
3666  List *otherclauses;
3667  Relids outerrelids;
3668  List *nestParams;
3669  Relids saveOuterRels = root->curOuterRels;
3670  ListCell *cell;
3671  ListCell *prev;
3672  ListCell *next;
3673 
3674  /* NestLoop can project, so no need to be picky about child tlists */
3675  outer_plan = create_plan_recurse(root, best_path->outerjoinpath, 0);
3676 
3677  /* For a nestloop, include outer relids in curOuterRels for inner side */
3678  root->curOuterRels = bms_union(root->curOuterRels,
3679  best_path->outerjoinpath->parent->relids);
3680 
3681  inner_plan = create_plan_recurse(root, best_path->innerjoinpath, 0);
3682 
3683  /* Restore curOuterRels */
3684  bms_free(root->curOuterRels);
3685  root->curOuterRels = saveOuterRels;
3686 
3687  /* Sort join qual clauses into best execution order */
3688  joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
3689 
3690  /* Get the join qual clauses (in plain expression form) */
3691  /* Any pseudoconstant clauses are ignored here */
3692  if (IS_OUTER_JOIN(best_path->jointype))
3693  {
3694  extract_actual_join_clauses(joinrestrictclauses,
3695  &joinclauses, &otherclauses);
3696  }
3697  else
3698  {
3699  /* We can treat all clauses alike for an inner join */
3700  joinclauses = extract_actual_clauses(joinrestrictclauses, false);
3701  otherclauses = NIL;
3702  }
3703 
3704  /* Replace any outer-relation variables with nestloop params */
3705  if (best_path->path.param_info)
3706  {
3707  joinclauses = (List *)
3708  replace_nestloop_params(root, (Node *) joinclauses);
3709  otherclauses = (List *)
3710  replace_nestloop_params(root, (Node *) otherclauses);
3711  }
3712 
3713  /*
3714  * Identify any nestloop parameters that should be supplied by this join
3715  * node, and move them from root->curOuterParams to the nestParams list.
3716  */
3717  outerrelids = best_path->outerjoinpath->parent->relids;
3718  nestParams = NIL;
3719  prev = NULL;
3720  for (cell = list_head(root->curOuterParams); cell; cell = next)
3721  {
3722  NestLoopParam *nlp = (NestLoopParam *) lfirst(cell);
3723 
3724  next = lnext(cell);
3725  if (IsA(nlp->paramval, Var) &&
3726  bms_is_member(nlp->paramval->varno, outerrelids))
3727  {
3729  cell, prev);
3730  nestParams = lappend(nestParams, nlp);
3731  }
3732  else if (IsA(nlp->paramval, PlaceHolderVar) &&
3733  bms_overlap(((PlaceHolderVar *) nlp->paramval)->phrels,
3734  outerrelids) &&
3736  (PlaceHolderVar *) nlp->paramval,
3737  false)->ph_eval_at,
3738  outerrelids))
3739  {
3741  cell, prev);
3742  nestParams = lappend(nestParams, nlp);
3743  }
3744  else
3745  prev = cell;
3746  }
3747 
3748  join_plan = make_nestloop(tlist,
3749  joinclauses,
3750  otherclauses,
3751  nestParams,
3752  outer_plan,
3753  inner_plan,
3754  best_path->jointype,
3755  best_path->inner_unique);
3756 
3757  copy_generic_path_info(&join_plan->join.plan, &best_path->path);
3758 
3759  return join_plan;
3760 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:562
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4224
Relids ph_eval_at
Definition: relation.h:2154
static int32 next
Definition: blutils.c:210
void extract_actual_join_clauses(List *restrictinfo_list, List **joinquals, List **otherquals)
Definition: restrictinfo.c:383
Relids curOuterRels
Definition: relation.h:315
Path * innerjoinpath
Definition: relation.h:1385
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:724
ParamPathInfo * param_info
Definition: relation.h:1045
Definition: nodes.h:511
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4782
Definition: primnodes.h:163
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
Var * paramval
Definition: plannodes.h:693
Join join
Definition: plannodes.h:685
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:729
List * joinrestrictinfo
Definition: relation.h:1387
RelOptInfo * parent
Definition: relation.h:1042
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
static NestLoop * make_nestloop(List *tlist, List *joinclauses, List *otherclauses, List *nestParams, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
Definition: createplan.c:5395
List * curOuterParams
Definition: relation.h:316
PlaceHolderInfo * find_placeholder_info(PlannerInfo *root, PlaceHolderVar *phv, bool create_new_ph)
Definition: placeholder.c:70
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
Relids relids
Definition: relation.h:585
#define lnext(lc)
Definition: pg_list.h:105
List * lappend(List *list, void *datum)
Definition: list.c:128
Index varno
Definition: primnodes.h:166
List * list_delete_cell(List *list, ListCell *cell, ListCell *prev)
Definition: list.c:528
Path * outerjoinpath
Definition: relation.h:1384
void bms_free(Bitmapset *a)
Definition: bitmapset.c:201
Path path
Definition: relation.h:1377
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:218
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:356
bool inner_unique
Definition: relation.h:1381
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
JoinType jointype
Definition: relation.h:1379
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
Plan plan
Definition: plannodes.h:666

◆ create_plan()

Plan* create_plan ( PlannerInfo root,
Path best_path 
)

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

305 {
306  Plan *plan;
307 
308  /* plan_params should not be in use in current query level */
309  Assert(root->plan_params == NIL);
310 
311  /* Initialize this module's private workspace in PlannerInfo */
312  root->curOuterRels = NULL;
313  root->curOuterParams = NIL;
314 
315  /* Recursively process the path tree, demanding the correct tlist result */
316  plan = create_plan_recurse(root, best_path, CP_EXACT_TLIST);
317 
318  /*
319  * Make sure the topmost plan node's targetlist exposes the original
320  * column names and other decorative info. Targetlists generated within
321  * the planner don't bother with that stuff, but we must have it on the
322  * top-level tlist seen at execution time. However, ModifyTable plan
323  * nodes don't have a tlist matching the querytree targetlist.
324  */
325  if (!IsA(plan, ModifyTable))
327 
328  /*
329  * Attach any initPlans created in this query level to the topmost plan
330  * node. (In principle the initplans could go in any plan node at or
331  * above where they're referenced, but there seems no reason to put them
332  * any lower than the topmost node for the query level. Also, see
333  * comments for SS_finalize_plan before you try to change this.)
334  */
335  SS_attach_initplans(root, plan);
336 
337  /* Check we successfully assigned all NestLoopParams to plan nodes */
338  if (root->curOuterParams != NIL)
339  elog(ERROR, "failed to assign all NestLoopParams to plan nodes");
340 
341  /*
342  * Reset plan_params to ensure param IDs used for nestloop params are not
343  * re-used later
344  */
345  root->plan_params = NIL;
346 
347  return plan;
348 }
#define NIL
Definition: pg_list.h:69
void apply_tlist_labeling(List *dest_tlist, List *src_tlist)
Definition: tlist.c:321
#define IsA(nodeptr, _type_)
Definition: nodes.h:562
List * plan_params
Definition: relation.h:169
Relids curOuterRels
Definition: relation.h:315
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
#define ERROR
Definition: elog.h:43
List * curOuterParams
Definition: relation.h:316
#define Assert(condition)
Definition: c.h:670
void SS_attach_initplans(PlannerInfo *root, Plan *plan)
Definition: subselect.c:2220
List * targetlist
Definition: plannodes.h:144
#define elog
Definition: elog.h:219
List * processed_tlist
Definition: relation.h:284
#define CP_EXACT_TLIST
Definition: createplan.c:66

◆ create_plan_recurse()

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

Definition at line 355 of file createplan.c.

References Assert, create_agg_plan(), create_append_plan(), create_gather_merge_plan(), create_gather_plan(), create_group_plan(), create_groupingsets_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_result_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_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_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().

356 {
357  Plan *plan;
358 
359  switch (best_path->pathtype)
360  {
361  case T_SeqScan:
362  case T_SampleScan:
363  case T_IndexScan:
364  case T_IndexOnlyScan:
365  case T_BitmapHeapScan:
366  case T_TidScan:
367  case T_SubqueryScan:
368  case T_FunctionScan:
369  case T_TableFuncScan:
370  case T_ValuesScan:
371  case T_CteScan:
372  case T_WorkTableScan:
374  case T_ForeignScan:
375  case T_CustomScan:
376  plan = create_scan_plan(root, best_path, flags);
377  break;
378  case T_HashJoin:
379  case T_MergeJoin:
380  case T_NestLoop:
381  plan = create_join_plan(root,
382  (JoinPath *) best_path);
383  break;
384  case T_Append:
385  plan = create_append_plan(root,
386  (AppendPath *) best_path);
387  break;
388  case T_MergeAppend:
389  plan = create_merge_append_plan(root,
390  (MergeAppendPath *) best_path);
391  break;
392  case T_Result:
393  if (IsA(best_path, ProjectionPath))
394  {
395  plan = create_projection_plan(root,
396  (ProjectionPath *) best_path);
397  }
398  else if (IsA(best_path, MinMaxAggPath))
399  {
400  plan = (Plan *) create_minmaxagg_plan(root,
401  (MinMaxAggPath *) best_path);
402  }
403  else
404  {
405  Assert(IsA(best_path, ResultPath));
406  plan = (Plan *) create_result_plan(root,
407  (ResultPath *) best_path);
408  }
409  break;
410  case T_ProjectSet:
411  plan = (Plan *) create_project_set_plan(root,
412  (ProjectSetPath *) best_path);
413  break;
414  case T_Material:
415  plan = (Plan *) create_material_plan(root,
416  (MaterialPath *) best_path,
417  flags);
418  break;
419  case T_Unique:
420  if (IsA(best_path, UpperUniquePath))
421  {
422  plan = (Plan *) create_upper_unique_plan(root,
423  (UpperUniquePath *) best_path,
424  flags);
425  }
426  else
427  {
428  Assert(IsA(best_path, UniquePath));
429  plan = create_unique_plan(root,
430  (UniquePath *) best_path,
431  flags);
432  }
433  break;
434  case T_Gather:
435  plan = (Plan *) create_gather_plan(root,
436  (GatherPath *) best_path);
437  break;
438  case T_Sort:
439  plan = (Plan *) create_sort_plan(root,
440  (SortPath *) best_path,
441  flags);
442  break;
443  case T_Group:
444  plan = (Plan *) create_group_plan(root,
445  (GroupPath *) best_path);
446  break;
447  case T_Agg:
448  if (IsA(best_path, GroupingSetsPath))
449  plan = create_groupingsets_plan(root,
450  (GroupingSetsPath *) best_path);
451  else
452  {
453  Assert(IsA(best_path, AggPath));
454  plan = (Plan *) create_agg_plan(root,
455  (AggPath *) best_path);
456  }
457  break;
458  case T_WindowAgg:
459  plan = (Plan *) create_windowagg_plan(root,
460  (WindowAggPath *) best_path);
461  break;
462  case T_SetOp:
463  plan = (Plan *) create_setop_plan(root,
464  (SetOpPath *) best_path,
465  flags);
466  break;
467  case T_RecursiveUnion:
468  plan = (Plan *) create_recursiveunion_plan(root,
469  (RecursiveUnionPath *) best_path);
470  break;
471  case T_LockRows:
472  plan = (Plan *) create_lockrows_plan(root,
473  (LockRowsPath *) best_path,
474  flags);
475  break;
476  case T_ModifyTable:
477  plan = (Plan *) create_modifytable_plan(root,
478  (ModifyTablePath *) best_path);
479  break;
480  case T_Limit:
481  plan = (Plan *) create_limit_plan(root,
482  (LimitPath *) best_path,
483  flags);
484  break;
485  case T_GatherMerge:
486  plan = (Plan *) create_gather_merge_plan(root,
487  (GatherMergePath *) best_path);
488  break;
489  default:
490  elog(ERROR, "unrecognized node type: %d",
491  (int) best_path->pathtype);
492  plan = NULL; /* keep compiler quiet */
493  break;
494  }
495 
496  return plan;
497 }
static Plan * create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
Definition: createplan.c:1264
static Result * create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path)
Definition: createplan.c:1985
static Result * create_result_plan(PlannerInfo *root, ResultPath *best_path)
Definition: createplan.c:1185
Definition: nodes.h:77
static Group * create_group_plan(PlannerInfo *root, GroupPath *best_path)
Definition: createplan.c:1679
#define IsA(nodeptr, _type_)
Definition: nodes.h:562
Definition: nodes.h:79
static Sort * create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags)
Definition: createplan.c:1652
static Plan * create_scan_plan(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:504
static LockRows * create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path, int flags)
Definition: createplan.c:2312
static ProjectSet * create_project_set_plan(PlannerInfo *root, ProjectSetPath *best_path)
Definition: createplan.c:1210
static RecursiveUnion * create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path)
Definition: createplan.c:2276
static Agg * create_agg_plan(PlannerInfo *root, AggPath *best_path)
Definition: createplan.c:1744
static Unique * create_upper_unique_plan(PlannerInfo *root, UpperUniquePath *best_path, int flags)
Definition: createplan.c:1716
Definition: nodes.h:48
Definition: nodes.h:75
static ModifyTable * create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path)
Definition: createplan.c:2335
NodeTag pathtype
Definition: relation.h:1040
Definition: nodes.h:45
#define ERROR
Definition: elog.h:43
static Plan * create_projection_plan(PlannerInfo *root, ProjectionPath *best_path)
Definition: createplan.c:1562
Definition: nodes.h:76
static Plan * create_join_plan(PlannerInfo *root, JoinPath *best_path)
Definition: createplan.c:952
static GatherMerge * create_gather_merge_plan(PlannerInfo *root, GatherMergePath *best_path)
Definition: createplan.c:1503
static SetOp * create_setop_plan(PlannerInfo *root, SetOpPath *best_path, int flags)
Definition: createplan.c:2240
#define Assert(condition)
Definition: c.h:670
static Gather * create_gather_plan(PlannerInfo *root, GatherPath *best_path)
Definition: createplan.c:1467
static Plan * create_append_plan(PlannerInfo *root, AppendPath *best_path)
Definition: createplan.c:1012
static WindowAgg * create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
Definition: createplan.c:2054
static Material * create_material_plan(PlannerInfo *root, MaterialPath *best_path, int flags)
Definition: createplan.c:1236
Definition: nodes.h:83
Definition: nodes.h:80
static Plan * create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path)
Definition: createplan.c:1825
static Limit * create_limit_plan(PlannerInfo *root, LimitPath *best_path, int flags)
Definition: createplan.c:2394
#define elog
Definition: elog.h:219
static Plan * create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path)
Definition: createplan.c:1077
Definition: nodes.h:85

◆ create_project_set_plan()

static ProjectSet * create_project_set_plan ( PlannerInfo root,
ProjectSetPath best_path 
)
static

Definition at line 1210 of file createplan.c.

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

Referenced by create_plan_recurse().

1211 {
1212  ProjectSet *plan;
1213  Plan *subplan;
1214  List *tlist;
1215 
1216  /* Since we intend to project, we don't need to constrain child tlist */
1217  subplan = create_plan_recurse(root, best_path->subpath, 0);
1218 
1219  tlist = build_path_tlist(root, &best_path->path);
1220 
1221  plan = make_project_set(tlist, subplan);
1222 
1223  copy_generic_path_info(&plan->plan, (Path *) best_path);
1224 
1225  return plan;
1226 }
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
static ProjectSet * make_project_set(List *tlist, Plan *subplan)
Definition: createplan.c:6409
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:729
Path * subpath
Definition: relation.h:1492
Definition: pg_list.h:45
Plan plan
Definition: plannodes.h:202

◆ create_projection_plan()

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

Definition at line 1562 of file createplan.c.

References build_path_tlist(), copy_generic_path_info(), create_plan_recurse(), is_projection_capable_path(), make_result(), Plan::parallel_safe, Path::parallel_safe, ProjectionPath::path, Path::pathtarget, Plan::plan_rows, Plan::plan_width, Path::rows, Plan::startup_cost, Path::startup_cost, ProjectionPath::subpath, Plan::targetlist, tlist_same_exprs(), Plan::total_cost, Path::total_cost, and PathTarget::width.

Referenced by create_plan_recurse().

1563 {
1564  Plan *plan;
1565  Plan *subplan;
1566  List *tlist;
1567 
1568  /* Since we intend to project, we don't need to constrain child tlist */
1569  subplan = create_plan_recurse(root, best_path->subpath, 0);
1570 
1571  tlist = build_path_tlist(root, &best_path->path);
1572 
1573  /*
1574  * We might not really need a Result node here, either because the subplan
1575  * can project or because it's returning the right list of expressions
1576  * anyway. Usually create_projection_path will have detected that and set
1577  * dummypp if we don't need a Result; but its decision can't be final,
1578  * because some createplan.c routines change the tlists of their nodes.
1579  * (An example is that create_merge_append_plan might add resjunk sort
1580  * columns to a MergeAppend.) So we have to recheck here. If we do
1581  * arrive at a different answer than create_projection_path did, we'll
1582  * have made slightly wrong cost estimates; but label the plan with the
1583  * cost estimates we actually used, not "corrected" ones. (XXX this could
1584  * be cleaned up if we moved more of the sortcolumn setup logic into Path
1585  * creation, but that would add expense to creating Paths we might end up
1586  * not using.)
1587  */
1588  if (is_projection_capable_path(best_path->subpath) ||
1589  tlist_same_exprs(tlist, subplan->targetlist))
1590  {
1591  /* Don't need a separate Result, just assign tlist to subplan */
1592  plan = subplan;
1593  plan->targetlist = tlist;
1594 
1595  /* Label plan with the estimated costs we actually used */
1596  plan->startup_cost = best_path->path.startup_cost;
1597  plan->total_cost = best_path->path.total_cost;
1598  plan->plan_rows = best_path->path.rows;
1599  plan->plan_width = best_path->path.pathtarget->width;
1600  plan->parallel_safe = best_path->path.parallel_safe;
1601  /* ... but don't change subplan's parallel_aware flag */
1602  }
1603  else
1604  {
1605  /* We need a Result node */
1606  plan = (Plan *) make_result(tlist, NULL, subplan);
1607 
1608  copy_generic_path_info(plan, (Path *) best_path);
1609  }
1610 
1611  return plan;
1612 }
double plan_rows
Definition: plannodes.h:131
PathTarget * pathtarget
Definition: relation.h:1043
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:729
Cost startup_cost
Definition: relation.h:1053
Cost startup_cost
Definition: plannodes.h:125
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6388
Cost total_cost
Definition: relation.h:1054
int plan_width
Definition: plannodes.h:132
double rows
Definition: relation.h:1052
bool parallel_safe
Definition: relation.h:1048
bool tlist_same_exprs(List *tlist1, List *tlist2)
Definition: tlist.c:221
List * targetlist
Definition: plannodes.h:144
int width
Definition: relation.h:975
bool is_projection_capable_path(Path *path)
Definition: createplan.c:6569
Cost total_cost
Definition: plannodes.h:126
bool parallel_safe
Definition: plannodes.h:138
Path * subpath
Definition: relation.h:1480
Definition: pg_list.h:45

◆ create_recursiveunion_plan()

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

Definition at line 2276 of file createplan.c.

References build_path_tlist(), copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), RecursiveUnionPath::distinctList, RecursiveUnionPath::leftpath, make_recursive_union(), Min, RecursiveUnionPath::numGroups, RecursiveUnionPath::path, RecursiveUnion::plan, RecursiveUnionPath::rightpath, and RecursiveUnionPath::wtParam.

Referenced by create_plan_recurse().

2277 {
2278  RecursiveUnion *plan;
2279  Plan *leftplan;
2280  Plan *rightplan;
2281  List *tlist;
2282  long numGroups;
2283 
2284  /* Need both children to produce same tlist, so force it */
2285  leftplan = create_plan_recurse(root, best_path->leftpath, CP_EXACT_TLIST);
2286  rightplan = create_plan_recurse(root, best_path->rightpath, CP_EXACT_TLIST);
2287 
2288  tlist = build_path_tlist(root, &best_path->path);
2289 
2290  /* Convert numGroups to long int --- but 'ware overflow! */
2291  numGroups = (long) Min(best_path->numGroups, (double) LONG_MAX);
2292 
2293  plan = make_recursive_union(tlist,
2294  leftplan,
2295  rightplan,
2296  best_path->wtParam,
2297  best_path->distinctList,
2298  numGroups);
2299 
2300  copy_generic_path_info(&plan->plan, (Path *) best_path);
2301 
2302  return plan;
2303 }
static RecursiveUnion * make_recursive_union(List *tlist, Plan *lefttree, Plan *righttree, int wtParam, List *distinctList, long numGroups)
Definition: createplan.c:5313
#define Min(x, y)
Definition: c.h:802
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:355
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:729
#define CP_EXACT_TLIST
Definition: createplan.c:66
Definition: pg_list.h:45

◆ create_result_plan()

static Result * create_result_plan ( PlannerInfo root,
ResultPath best_path 
)
static

Definition at line 1185 of file createplan.c.

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

Referenced by create_plan_recurse().

1186 {
1187  Result *plan;
1188  List *tlist;
1189  List *quals;
1190 
1191  tlist = build_path_tlist(root, &best_path->path);
1192 
1193  /* best_path->quals is just bare clauses */
1194  quals = order_qual_clauses(root, best_path->quals);
1195 
1196  plan = make_result(tlist, (Node *) quals, NULL);
1197 
1198  copy_generic_path_info(&plan->plan, (Path *) best_path);
1199 
1200  return plan;
1201 }
Plan plan
Definition: plannodes.h:190
Definition: nodes.h:511
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4782
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
Path path
Definition: relation.h:1301
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:729
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6388
List * quals
Definition: relation.h:1302
Definition: pg_list.h:45

◆ create_samplescan_plan()

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

Definition at line 2463 of file createplan.c.

References Assert, copy_generic_path_info(), extract_actual_clauses(), make_samplescan(), order_qual_clauses(), Path::param_info, Path::parent, Scan::plan, planner_rt_fetch, RelOptInfo::relid, replace_nestloop_params(), RTE_RELATION, RangeTblEntry::rtekind, SampleScan::scan, and RangeTblEntry::tablesample.

Referenced by create_scan_plan().

2465 {
2466  SampleScan *scan_plan;
2467  Index scan_relid = best_path->parent->relid;
2468  RangeTblEntry *rte;
2469  TableSampleClause *tsc;
2470 
2471  /* it should be a base rel with a tablesample clause... */
2472  Assert(scan_relid > 0);
2473  rte = planner_rt_fetch(scan_relid, root);
2474  Assert(rte->rtekind == RTE_RELATION);
2475  tsc = rte->tablesample;
2476  Assert(tsc != NULL);
2477 
2478  /* Sort clauses into best execution order */
2479  scan_clauses = order_qual_clauses(root, scan_clauses);
2480 
2481  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2482  scan_clauses = extract_actual_clauses(scan_clauses, false);
2483 
2484  /* Replace any outer-relation variables with nestloop params */
2485  if (best_path->param_info)
2486  {
2487  scan_clauses = (List *)
2488  replace_nestloop_params(root, (Node *) scan_clauses);
2489  tsc = (TableSampleClause *)
2490  replace_nestloop_params(root, (Node *) tsc);
2491  }
2492 
2493  scan_plan = make_samplescan(tlist,
2494  scan_clauses,
2495  scan_relid,
2496  tsc);
2497 
2498  copy_generic_path_info(&scan_plan->scan.plan, best_path);
2499 
2500  return scan_plan;
2501 }
Plan plan
Definition: plannodes.h:328
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4224
ParamPathInfo * param_info
Definition: relation.h:1045
Definition: nodes.h:511
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4782
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4876
#define planner_rt_fetch(rti, root)
Definition: relation.h:328
Scan scan
Definition: plannodes.h:344
RelOptInfo * parent
Definition: relation.h:1042
Index relid
Definition: relation.h:613
static SampleScan * make_samplescan(List *qptlist, List *qpqual, Index scanrelid, TableSampleClause *tsc)
Definition: createplan.c:4988
unsigned int Index
Definition: c.h:413
#define Assert(condition)
Definition: c.h:670
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:356
RTEKind rtekind
Definition: parsenodes.h:951
Definition: pg_list.h:45
struct TableSampleClause * tablesample
Definition: parsenodes.h:969

◆ create_scan_plan()

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

Definition at line 504 of file createplan.c.

References apply_pathtarget_labeling_to_tlist(), RelOptInfo::baserestrictinfo, build_path_tlist(), build_physical_tlist(), castNode, copyObject, create_bitmap_scan_plan(), create_ctescan_plan(), create_customscan_plan(), create_foreignscan_plan(), create_functionscan_plan(), create_gating_plan(), create_indexscan_plan(), create_namedtuplestorescan_plan(), create_samplescan_plan(), create_seqscan_plan(), create_subqueryscan_plan(), create_tablefuncscan_plan(), create_tidscan_plan(), create_valuesscan_plan(), create_worktablescan_plan(), elog, ERROR, get_gating_quals(), list_concat(), list_copy(), NIL, Path::param_info, Path::parent, Path::pathtarget, Path::pathtype, ParamPathInfo::ppi_clauses, T_BitmapHeapScan, T_CteScan, T_CustomScan, T_ForeignScan, T_FunctionScan, T_IndexOnlyScan, T_IndexScan, T_NamedTuplestoreScan, T_SampleScan, T_SeqScan, T_SubqueryScan, T_TableFuncScan, T_TidScan, T_ValuesScan, T_WorkTableScan, and use_physical_tlist().

Referenced by create_plan_recurse().

505 {
506  RelOptInfo *rel = best_path->parent;
507  List *scan_clauses;
508  List *gating_clauses;
509  List *tlist;
510  Plan *plan;
511 
512  /*
513  * Extract the relevant restriction clauses from the parent relation. The
514  * executor must apply all these restrictions during the scan, except for
515  * pseudoconstants which we'll take care of below.
516  *
517  * If this is a plain indexscan or index-only scan, we need not consider
518  * restriction clauses that are implied by the index's predicate, so use
519  * indrestrictinfo not baserestrictinfo. Note that we can't do that for
520  * bitmap indexscans, since there's not necessarily a single index
521  * involved; but it doesn't matter since create_bitmap_scan_plan() will be
522  * able to get rid of such clauses anyway via predicate proof.
523  */
524  switch (best_path->pathtype)
525  {
526  case T_IndexScan:
527  case T_IndexOnlyScan:
528  scan_clauses = castNode(IndexPath, best_path)->indexinfo->indrestrictinfo;
529  break;
530  default:
531  scan_clauses = rel->baserestrictinfo;
532  break;
533  }
534 
535  /*
536  * If this is a parameterized scan, we also need to enforce all the join
537  * clauses available from the outer relation(s).
538  *
539  * For paranoia's sake, don't modify the stored baserestrictinfo list.
540  */
541  if (best_path->param_info)
542  scan_clauses = list_concat(list_copy(scan_clauses),
543  best_path->param_info->ppi_clauses);
544 
545  /*
546  * Detect whether we have any pseudoconstant quals to deal with. Then, if
547  * we'll need a gating Result node, it will be able to project, so there
548  * are no requirements on the child's tlist.
549  */
550  gating_clauses = get_gating_quals(root, scan_clauses);
551  if (gating_clauses)
552  flags = 0;
553 
554  /*
555  * For table scans, rather than using the relation targetlist (which is
556  * only those Vars actually needed by the query), we prefer to generate a
557  * tlist containing all Vars in order. This will allow the executor to
558  * optimize away projection of the table tuples, if possible.
559  */
560  if (use_physical_tlist(root, best_path, flags))
561  {
562  if (best_path->pathtype == T_IndexOnlyScan)
563  {
564  /* For index-only scan, the preferred tlist is the index's */
565  tlist = copyObject(((IndexPath *) best_path)->indexinfo->indextlist);
566 
567  /*
568  * Transfer any sortgroupref data to the replacement tlist, unless
569  * we don't care because the gating Result will handle it.
570  */
571  if (!gating_clauses)
573  }
574  else
575  {
576  tlist = build_physical_tlist(root, rel);
577  if (tlist == NIL)
578  {
579  /* Failed because of dropped cols, so use regular method */
580  tlist = build_path_tlist(root, best_path);
581  }
582  else
583  {
584  /* As above, transfer sortgroupref data to replacement tlist */
585  if (!gating_clauses)
587  }
588  }
589  }
590  else
591  {
592  tlist = build_path_tlist(root, best_path);
593  }
594 
595  switch (best_path->pathtype)
596  {
597  case T_SeqScan:
598  plan = (Plan *) create_seqscan_plan(root,
599  best_path,
600  tlist,
601  scan_clauses);
602  break;
603 
604  case T_SampleScan:
605  plan = (Plan *) create_samplescan_plan(root,
606  best_path,
607  tlist,
608  scan_clauses);
609  break;
610 
611  case T_IndexScan:
612  plan = (Plan *) create_indexscan_plan(root,
613  (IndexPath *) best_path,
614  tlist,
615  scan_clauses,
616  false);
617  break;
618 
619  case T_IndexOnlyScan:
620  plan = (Plan *) create_indexscan_plan(root,
621  (IndexPath *) best_path,
622  tlist,
623  scan_clauses,
624  true);
625  break;
626 
627  case T_BitmapHeapScan:
628  plan = (Plan *) create_bitmap_scan_plan(root,
629  (BitmapHeapPath *) best_path,
630  tlist,
631  scan_clauses);
632  break;
633 
634  case T_TidScan:
635  plan = (Plan *) create_tidscan_plan(root,
636  (TidPath *) best_path,
637  tlist,
638  scan_clauses);
639  break;
640 
641  case T_SubqueryScan:
642  plan = (Plan *) create_subqueryscan_plan(root,
643  (SubqueryScanPath *) best_path,
644  tlist,
645  scan_clauses);
646