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 "partitioning/partprune.h"
#include "utils/lsyscache.h"
Include dependency graph for createplan.c:

Go to the source code of this file.

Macros

#define CP_EXACT_TLIST   0x0001 /* Plan must return specified tlist */
 
#define CP_SMALL_TLIST   0x0002 /* Prefer narrower tlists */
 
#define CP_LABEL_TLIST   0x0004 /* tlist must contain sortgrouprefs */
 
#define CP_IGNORE_TLIST   0x0008 /* caller will replace tlist */
 

Functions

static Plancreate_plan_recurse (PlannerInfo *root, Path *best_path, int flags)
 
static Plancreate_scan_plan (PlannerInfo *root, Path *best_path, int flags)
 
static Listbuild_path_tlist (PlannerInfo *root, Path *path)
 
static bool use_physical_tlist (PlannerInfo *root, Path *path, int flags)
 
static Listget_gating_quals (PlannerInfo *root, List *quals)
 
static Plancreate_gating_plan (PlannerInfo *root, Path *path, Plan *plan, List *gating_quals)
 
static Plancreate_join_plan (PlannerInfo *root, JoinPath *best_path)
 
static Plancreate_append_plan (PlannerInfo *root, AppendPath *best_path)
 
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, int flags)
 
static Planinject_projection_plan (Plan *subplan, List *tlist, bool parallel_safe)
 
static Sortcreate_sort_plan (PlannerInfo *root, SortPath *best_path, int flags)
 
static 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, int first_partial_plan, List *tlist, List *partitioned_rels, List *partpruneinfos)
 
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, Oid startInRangeFunc, Oid endInRangeFunc, Oid inRangeColl, bool inRangeAsc, bool inRangeNullsFirst, Plan *lefttree)
 
static Groupmake_group (List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, 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, bool partColsUpdated, 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_IGNORE_TLIST

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

Definition at line 73 of file createplan.c.

Referenced by create_projection_plan(), and create_scan_plan().

◆ CP_LABEL_TLIST

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

◆ CP_SMALL_TLIST

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

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

Referenced by create_bitmap_scan_plan().

5059 {
5060  if (IsA(plan, BitmapAnd))
5062  linitial(((BitmapAnd *) plan)->bitmapplans));
5063  else if (IsA(plan, BitmapOr))
5064  {
5065  ((BitmapOr *) plan)->isshared = true;
5067  linitial(((BitmapOr *) plan)->bitmapplans));
5068  }
5069  else if (IsA(plan, BitmapIndexScan))
5070  ((BitmapIndexScan *) plan)->isshared = true;
5071  else
5072  elog(ERROR, "unrecognized node type: %d", nodeTag(plan));
5073 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
#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:5058
#define nodeTag(nodeptr)
Definition: nodes.h:522
#define elog
Definition: elog.h:219

◆ build_path_tlist()

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

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

753 {
754  List *tlist = NIL;
755  Index *sortgrouprefs = path->pathtarget->sortgrouprefs;
756  int resno = 1;
757  ListCell *v;
758 
759  foreach(v, path->pathtarget->exprs)
760  {
761  Node *node = (Node *) lfirst(v);
762  TargetEntry *tle;
763 
764  /*
765  * If it's a parameterized path, there might be lateral references in
766  * the tlist, which need to be replaced with Params. There's no need
767  * to remake the TargetEntry nodes, so apply this to each list item
768  * separately.
769  */
770  if (path->param_info)
771  node = replace_nestloop_params(root, node);
772 
773  tle = makeTargetEntry((Expr *) node,
774  resno,
775  NULL,
776  false);
777  if (sortgrouprefs)
778  tle->ressortgroupref = sortgrouprefs[resno - 1];
779 
780  tlist = lappend(tlist, tle);
781  resno++;
782  }
783  return tlist;
784 }
#define NIL
Definition: pg_list.h:69
PathTarget * pathtarget
Definition: relation.h:1067
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4343
ParamPathInfo * param_info
Definition: relation.h:1069
Definition: nodes.h:517
Index * sortgrouprefs
Definition: relation.h:997
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:996
unsigned int Index
Definition: c.h:442
#define lfirst(lc)
Definition: pg_list.h:106
Index ressortgroupref
Definition: primnodes.h:1379
Definition: pg_list.h:45

◆ copy_generic_path_info()

static void copy_generic_path_info ( Plan dest,
Path src 
)
static

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

4996 {
4997  dest->startup_cost = src->startup_cost;
4998  dest->total_cost = src->total_cost;
4999  dest->plan_rows = src->rows;
5000  dest->plan_width = src->pathtarget->width;
5001  dest->parallel_aware = src->parallel_aware;
5002  dest->parallel_safe = src->parallel_safe;
5003 }
double plan_rows
Definition: plannodes.h:133
PathTarget * pathtarget
Definition: relation.h:1067
Cost startup_cost
Definition: relation.h:1077
Cost startup_cost
Definition: plannodes.h:127
bool parallel_aware
Definition: plannodes.h:139
Cost total_cost
Definition: relation.h:1078
int plan_width
Definition: plannodes.h:134
double rows
Definition: relation.h:1076
bool parallel_safe
Definition: relation.h:1072
int width
Definition: relation.h:999
Cost total_cost
Definition: plannodes.h:128
bool parallel_aware
Definition: relation.h:1071
bool parallel_safe
Definition: plannodes.h:140

◆ copy_plan_costsize()

static void copy_plan_costsize ( Plan dest,
Plan src 
)
static

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

5011 {
5012  dest->startup_cost = src->startup_cost;
5013  dest->total_cost = src->total_cost;
5014  dest->plan_rows = src->plan_rows;
5015  dest->plan_width = src->plan_width;
5016  /* Assume the inserted node is not parallel-aware. */
5017  dest->parallel_aware = false;
5018  /* Assume the inserted node is parallel-safe, if child plan is. */
5019  dest->parallel_safe = src->parallel_safe;
5020 }
double plan_rows
Definition: plannodes.h:133
Cost startup_cost
Definition: plannodes.h:127
bool parallel_aware
Definition: plannodes.h:139
int plan_width
Definition: plannodes.h:134
Cost total_cost
Definition: plannodes.h:128
bool parallel_safe
Definition: plannodes.h:140

◆ create_agg_plan()

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

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

1849 {
1850  Agg *plan;
1851  Plan *subplan;
1852  List *tlist;
1853  List *quals;
1854 
1855  /*
1856  * Agg can project, so no need to be terribly picky about child tlist, but
1857  * we do need grouping columns to be available
1858  */
1859  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1860 
1861  tlist = build_path_tlist(root, &best_path->path);
1862 
1863  quals = order_qual_clauses(root, best_path->qual);
1864 
1865  plan = make_agg(tlist, quals,
1866  best_path->aggstrategy,
1867  best_path->aggsplit,
1868  list_length(best_path->groupClause),
1870  subplan->targetlist),
1871  extract_grouping_ops(best_path->groupClause),
1872  NIL,
1873  NIL,
1874  best_path->numGroups,
1875  subplan);
1876 
1877  copy_generic_path_info(&plan->plan, (Path *) best_path);
1878 
1879  return plan;
1880 }
#define NIL
Definition: pg_list.h:69
AggStrategy aggstrategy
Definition: relation.h:1580
List * qual
Definition: relation.h:1584
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4901
AggSplit aggsplit
Definition: relation.h:1581
Oid * extract_grouping_ops(List *groupClause)
Definition: tlist.c:466
double numGroups
Definition: relation.h:1582
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:6149
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:366
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:752
List * groupClause
Definition: relation.h:1583
Plan plan
Definition: plannodes.h:796
#define CP_LABEL_TLIST
Definition: createplan.c:72
static int list_length(const List *l)
Definition: pg_list.h:89
Path * subpath
Definition: relation.h:1579
List * targetlist
Definition: plannodes.h:146
Definition: plannodes.h:794
Definition: pg_list.h:45
Path path
Definition: relation.h:1578

◆ create_append_plan()

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

Definition at line 1035 of file createplan.c.

References RelOptInfo::baserestrictinfo, build_path_tlist(), copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), extract_actual_clauses(), AppendPath::first_partial_path, lappend(), lfirst, list_concat(), list_make1, make_append(), make_partition_pruneinfo(), make_result(), makeBoolConst(), NIL, Path::param_info, Path::parent, AppendPath::partitioned_rels, AppendPath::path, Append::plan, ParamPathInfo::ppi_clauses, RELOPT_BASEREL, RelOptInfo::reloptkind, replace_nestloop_params(), subpath(), and AppendPath::subpaths.

Referenced by create_plan_recurse().

1036 {
1037  Append *plan;
1038  List *tlist = build_path_tlist(root, &best_path->path);
1039  List *subplans = NIL;
1040  ListCell *subpaths;
1041  RelOptInfo *rel = best_path->path.parent;
1042  List *partpruneinfos = NIL;
1043 
1044  /*
1045  * The subpaths list could be empty, if every child was proven empty by
1046  * constraint exclusion. In that case generate a dummy plan that returns
1047  * no rows.
1048  *
1049  * Note that an AppendPath with no members is also generated in certain
1050  * cases where there was no appending construct at all, but we know the
1051  * relation is empty (see set_dummy_rel_pathlist).
1052  */
1053  if (best_path->subpaths == NIL)
1054  {
1055  /* Generate a Result plan with constant-FALSE gating qual */
1056  Plan *plan;
1057 
1058  plan = (Plan *) make_result(tlist,
1059  (Node *) list_make1(makeBoolConst(false,
1060  false)),
1061  NULL);
1062 
1063  copy_generic_path_info(plan, (Path *) best_path);
1064 
1065  return plan;
1066  }
1067 
1068  /* Build the plan for each child */
1069  foreach(subpaths, best_path->subpaths)
1070  {
1071  Path *subpath = (Path *) lfirst(subpaths);
1072  Plan *subplan;
1073 
1074  /* Must insist that all children return the same tlist */
1075  subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
1076 
1077  subplans = lappend(subplans, subplan);
1078  }
1079 
1080  if (rel->reloptkind == RELOPT_BASEREL &&
1081  best_path->partitioned_rels != NIL)
1082  {
1083  List *prunequal;
1084 
1085  prunequal = extract_actual_clauses(rel->baserestrictinfo, false);
1086 
1087  if (best_path->path.param_info)
1088  {
1089 
1090  List *prmquals = best_path->path.param_info->ppi_clauses;
1091 
1092  prmquals = extract_actual_clauses(prmquals, false);
1093  prmquals = (List *) replace_nestloop_params(root,
1094  (Node *) prmquals);
1095 
1096  prunequal = list_concat(prunequal, prmquals);
1097  }
1098 
1099  /*
1100  * If any quals exist, they may be useful to perform further partition
1101  * pruning during execution. Generate a PartitionPruneInfo for each
1102  * partitioned rel to store these quals and allow translation of
1103  * partition indexes into subpath indexes.
1104  */
1105  if (prunequal != NIL)
1106  partpruneinfos =
1108  best_path->partitioned_rels,
1109  best_path->subpaths, prunequal);
1110  }
1111 
1112  /*
1113  * XXX ideally, if there's just one child, we'd not bother to generate an
1114  * Append node but just return the single child. At the moment this does
1115  * not work because the varno of the child scan plan won't match the
1116  * parent-rel Vars it'll be asked to emit.
1117  */
1118 
1119  plan = make_append(subplans, best_path->first_partial_path,
1120  tlist, best_path->partitioned_rels,
1121  partpruneinfos);
1122 
1123  copy_generic_path_info(&plan->plan, (Path *) best_path);
1124 
1125  return (Plan *) plan;
1126 }
#define NIL
Definition: pg_list.h:69
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4343
RelOptKind reloptkind
Definition: relation.h:597
static Append * make_append(List *appendplans, int first_partial_plan, List *tlist, List *partitioned_rels, List *partpruneinfos)
Definition: createplan.c:5416
List * baserestrictinfo
Definition: relation.h:660
ParamPathInfo * param_info
Definition: relation.h:1069
Definition: nodes.h:517
List * list_concat(List *list1, List *list2)
Definition: list.c:321
List * make_partition_pruneinfo(PlannerInfo *root, List *partition_rels, List *subpaths, List *prunequal)
Definition: partprune.c:189
Path path
Definition: relation.h:1292
int first_partial_path
Definition: relation.h:1298
#define list_make1(x1)
Definition: pg_list.h:139
List * subpaths
Definition: relation.h:1295
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:366
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:752
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:356
RelOptInfo * parent
Definition: relation.h:1066
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6516
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
List * ppi_clauses
Definition: relation.h:1027
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:356
List * partitioned_rels
Definition: relation.h:1294
#define CP_EXACT_TLIST
Definition: createplan.c:70
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 2800 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().

2804 {
2805  Index baserelid = best_path->path.parent->relid;
2806  Plan *bitmapqualplan;
2807  List *bitmapqualorig;
2808  List *indexquals;
2809  List *indexECs;
2810  List *qpqual;
2811  ListCell *l;
2812  BitmapHeapScan *scan_plan;
2813 
2814  /* it should be a base rel... */
2815  Assert(baserelid > 0);
2816  Assert(best_path->path.parent->rtekind == RTE_RELATION);
2817 
2818  /* Process the bitmapqual tree into a Plan tree and qual lists */
2819  bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
2820  &bitmapqualorig, &indexquals,
2821  &indexECs);
2822 
2823  if (best_path->path.parallel_aware)
2824  bitmap_subplan_mark_shared(bitmapqualplan);
2825 
2826  /*
2827  * The qpqual list must contain all restrictions not automatically handled
2828  * by the index, other than pseudoconstant clauses which will be handled
2829  * by a separate gating plan node. All the predicates in the indexquals
2830  * will be checked (either by the index itself, or by
2831  * nodeBitmapHeapscan.c), but if there are any "special" operators
2832  * involved then they must be added to qpqual. The upshot is that qpqual
2833  * must contain scan_clauses minus whatever appears in indexquals.
2834  *
2835  * This loop is similar to the comparable code in create_indexscan_plan(),
2836  * but with some differences because it has to compare the scan clauses to
2837  * stripped (no RestrictInfos) indexquals. See comments there for more
2838  * info.
2839  *
2840  * In normal cases simple equal() checks will be enough to spot duplicate
2841  * clauses, so we try that first. We next see if the scan clause is
2842  * redundant with any top-level indexqual by virtue of being generated
2843  * from the same EC. After that, try predicate_implied_by().
2844  *
2845  * Unlike create_indexscan_plan(), the predicate_implied_by() test here is
2846  * useful for getting rid of qpquals that are implied by index predicates,
2847  * because the predicate conditions are included in the "indexquals"
2848  * returned by create_bitmap_subplan(). Bitmap scans have to do it that
2849  * way because predicate conditions need to be rechecked if the scan
2850  * becomes lossy, so they have to be included in bitmapqualorig.
2851  */
2852  qpqual = NIL;
2853  foreach(l, scan_clauses)
2854  {
2855  RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
2856  Node *clause = (Node *) rinfo->clause;
2857 
2858  if (rinfo->pseudoconstant)
2859  continue; /* we may drop pseudoconstants here */
2860  if (list_member(indexquals, clause))
2861  continue; /* simple duplicate */
2862  if (rinfo->parent_ec && list_member_ptr(indexECs, rinfo->parent_ec))
2863  continue; /* derived from same EquivalenceClass */
2864  if (!contain_mutable_functions(clause) &&
2865  predicate_implied_by(list_make1(clause), indexquals, false))
2866  continue; /* provably implied by indexquals */
2867  qpqual = lappend(qpqual, rinfo);
2868  }
2869 
2870  /* Sort clauses into best execution order */
2871  qpqual = order_qual_clauses(root, qpqual);
2872 
2873  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2874  qpqual = extract_actual_clauses(qpqual, false);
2875 
2876  /*
2877  * When dealing with special operators, we will at this point have
2878  * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
2879  * 'em from bitmapqualorig, since there's no point in making the tests
2880  * twice.
2881  */
2882  bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
2883 
2884  /*
2885  * We have to replace any outer-relation variables with nestloop params in
2886  * the qpqual and bitmapqualorig expressions. (This was already done for
2887  * expressions attached to plan nodes in the bitmapqualplan tree.)
2888  */
2889  if (best_path->path.param_info)
2890  {
2891  qpqual = (List *)
2892  replace_nestloop_params(root, (Node *) qpqual);
2893  bitmapqualorig = (List *)
2894  replace_nestloop_params(root, (Node *) bitmapqualorig);
2895  }
2896 
2897  /* Finally ready to build the plan node */
2898  scan_plan = make_bitmap_heapscan(tlist,
2899  qpqual,
2900  bitmapqualplan,
2901  bitmapqualorig,
2902  baserelid);
2903 
2904  copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
2905 
2906  return scan_plan;
2907 }
#define NIL
Definition: pg_list.h:69
Plan plan
Definition: plannodes.h:342
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4343
List * list_difference_ptr(const List *list1, const List *list2)
Definition: list.c:884
bool pseudoconstant
Definition: relation.h:1876
ParamPathInfo * param_info
Definition: relation.h:1069
Definition: nodes.h:517
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4901
static Plan * create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual, List **qual, List **indexqual, List **indexECs)
Definition: createplan.c:2930
#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:5205
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
bool list_member(const List *list, const void *datum)
Definition: list.c:444
EquivalenceClass * parent_ec
Definition: relation.h:1902
RelOptInfo * parent
Definition: relation.h:1066
Path * bitmapqual
Definition: relation.h:1174
#define lfirst_node(type, lc)
Definition: pg_list.h:109
Index relid
Definition: relation.h:628
List * lappend(List *list, void *datum)
Definition: list.c:128
static void bitmap_subplan_mark_shared(Plan *plan)
Definition: createplan.c:5058
Expr * clause
Definition: relation.h:1868
unsigned int Index
Definition: c.h:442
RTEKind rtekind
Definition: relation.h:630
bool list_member_ptr(const List *list, const void *datum)
Definition: list.c:465
#define Assert(condition)
Definition: c.h:699
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:356
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:879
bool parallel_aware
Definition: relation.h:1071
bool predicate_implied_by(List *predicate_list, List *clause_list, bool weak)
Definition: predtest.c:149
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 2930 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().

2932 {
2933  Plan *plan;
2934 
2935  if (IsA(bitmapqual, BitmapAndPath))
2936  {
2937  BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
2938  List *subplans = NIL;
2939  List *subquals = NIL;
2940  List *subindexquals = NIL;
2941  List *subindexECs = NIL;
2942  ListCell *l;
2943 
2944  /*
2945  * There may well be redundant quals among the subplans, since a
2946  * top-level WHERE qual might have gotten used to form several
2947  * different index quals. We don't try exceedingly hard to eliminate
2948  * redundancies, but we do eliminate obvious duplicates by using
2949  * list_concat_unique.
2950  */
2951  foreach(l, apath->bitmapquals)
2952  {
2953  Plan *subplan;
2954  List *subqual;
2955  List *subindexqual;
2956  List *subindexEC;
2957 
2958  subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
2959  &subqual, &subindexqual,
2960  &subindexEC);
2961  subplans = lappend(subplans, subplan);
2962  subquals = list_concat_unique(subquals, subqual);
2963  subindexquals = list_concat_unique(subindexquals, subindexqual);
2964  /* Duplicates in indexECs aren't worth getting rid of */
2965  subindexECs = list_concat(subindexECs, subindexEC);
2966  }
2967  plan = (Plan *) make_bitmap_and(subplans);
2968  plan->startup_cost = apath->path.startup_cost;
2969  plan->total_cost = apath->path.total_cost;
2970  plan->plan_rows =
2971  clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
2972  plan->plan_width = 0; /* meaningless */
2973  plan->parallel_aware = false;
2974  plan->parallel_safe = apath->path.parallel_safe;
2975  *qual = subquals;
2976  *indexqual = subindexquals;
2977  *indexECs = subindexECs;
2978  }
2979  else if (IsA(bitmapqual, BitmapOrPath))
2980  {
2981  BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
2982  List *subplans = NIL;
2983  List *subquals = NIL;
2984  List *subindexquals = NIL;
2985  bool const_true_subqual = false;
2986  bool const_true_subindexqual = false;
2987  ListCell *l;
2988 
2989  /*
2990  * Here, we only detect qual-free subplans. A qual-free subplan would
2991  * cause us to generate "... OR true ..." which we may as well reduce
2992  * to just "true". We do not try to eliminate redundant subclauses
2993  * because (a) it's not as likely as in the AND case, and (b) we might
2994  * well be working with hundreds or even thousands of OR conditions,
2995  * perhaps from a long IN list. The performance of list_append_unique
2996  * would be unacceptable.
2997  */
2998  foreach(l, opath->bitmapquals)
2999  {
3000  Plan *subplan;
3001  List *subqual;
3002  List *subindexqual;
3003  List *subindexEC;
3004 
3005  subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
3006  &subqual, &subindexqual,
3007  &subindexEC);
3008  subplans = lappend(subplans, subplan);
3009  if (subqual == NIL)
3010  const_true_subqual = true;
3011  else if (!const_true_subqual)
3012  subquals = lappend(subquals,
3013  make_ands_explicit(subqual));
3014  if (subindexqual == NIL)
3015  const_true_subindexqual = true;
3016  else if (!const_true_subindexqual)
3017  subindexquals = lappend(subindexquals,
3018  make_ands_explicit(subindexqual));
3019  }
3020 
3021  /*
3022  * In the presence of ScalarArrayOpExpr quals, we might have built
3023  * BitmapOrPaths with just one subpath; don't add an OR step.
3024  */
3025  if (list_length(subplans) == 1)
3026  {
3027  plan = (Plan *) linitial(subplans);
3028  }
3029  else
3030  {
3031  plan = (Plan *) make_bitmap_or(subplans);
3032  plan->startup_cost = opath->path.startup_cost;
3033  plan->total_cost = opath->path.total_cost;
3034  plan->plan_rows =
3035  clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
3036  plan->plan_width = 0; /* meaningless */
3037  plan->parallel_aware = false;
3038  plan->parallel_safe = opath->path.parallel_safe;
3039  }
3040 
3041  /*
3042  * If there were constant-TRUE subquals, the OR reduces to constant
3043  * TRUE. Also, avoid generating one-element ORs, which could happen
3044  * due to redundancy elimination or ScalarArrayOpExpr quals.
3045  */
3046  if (const_true_subqual)
3047  *qual = NIL;
3048  else if (list_length(subquals) <= 1)
3049  *qual = subquals;
3050  else
3051  *qual = list_make1(make_orclause(subquals));
3052  if (const_true_subindexqual)
3053  *indexqual = NIL;
3054  else if (list_length(subindexquals) <= 1)
3055  *indexqual = subindexquals;
3056  else
3057  *indexqual = list_make1(make_orclause(subindexquals));
3058  *indexECs = NIL;
3059  }
3060  else if (IsA(bitmapqual, IndexPath))
3061  {
3062  IndexPath *ipath = (IndexPath *) bitmapqual;
3063  IndexScan *iscan;
3064  List *subindexECs;
3065  ListCell *l;
3066 
3067  /* Use the regular indexscan plan build machinery... */
3068  iscan = castNode(IndexScan,
3069  create_indexscan_plan(root, ipath,
3070  NIL, NIL, false));
3071  /* then convert to a bitmap indexscan */
3072  plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
3073  iscan->indexid,
3074  iscan->indexqual,
3075  iscan->indexqualorig);
3076  /* and set its cost/width fields appropriately */
3077  plan->startup_cost = 0.0;
3078  plan->total_cost = ipath->indextotalcost;
3079  plan->plan_rows =
3080  clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
3081  plan->plan_width = 0; /* meaningless */
3082  plan->parallel_aware = false;
3083  plan->parallel_safe = ipath->path.parallel_safe;
3084  *qual = get_actual_clauses(ipath->indexclauses);
3085  *indexqual = get_actual_clauses(ipath->indexquals);
3086  foreach(l, ipath->indexinfo->indpred)
3087  {
3088  Expr *pred = (Expr *) lfirst(l);
3089 
3090  /*
3091  * We know that the index predicate must have been implied by the
3092  * query condition as a whole, but it may or may not be implied by
3093  * the conditions that got pushed into the bitmapqual. Avoid
3094  * generating redundant conditions.
3095  */
3096  if (!predicate_implied_by(list_make1(pred), ipath->indexclauses,
3097  false))
3098  {
3099  *qual = lappend(*qual, pred);
3100  *indexqual = lappend(*indexqual, pred);
3101  }
3102  }
3103  subindexECs = NIL;
3104  foreach(l, ipath->indexquals)
3105  {
3106  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
3107 
3108  if (rinfo->parent_ec)
3109  subindexECs = lappend(subindexECs, rinfo->parent_ec);
3110  }
3111  *indexECs = subindexECs;
3112  }
3113  else
3114  {
3115  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
3116  plan = NULL; /* keep compiler quiet */
3117  }
3118 
3119  return plan;
3120 }
#define NIL
Definition: pg_list.h:69
double plan_rows
Definition: plannodes.h:133
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Path path
Definition: relation.h:1142
IndexOptInfo * indexinfo
Definition: relation.h:1143
Index scanrelid
Definition: plannodes.h:343
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
double tuples
Definition: relation.h:640
List * indexqualorig
Definition: plannodes.h:405
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:1144
static Plan * create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual, List **qual, List **indexqual, List **indexECs)
Definition: createplan.c:2930
Selectivity bitmapselectivity
Definition: relation.h:1187
static BitmapAnd * make_bitmap_and(List *bitmapplans)
Definition: createplan.c:5487
Oid indexid
Definition: plannodes.h:403
List * bitmapquals
Definition: relation.h:1186
List * bitmapquals
Definition: relation.h:1199
Expr * make_ands_explicit(List *andclauses)
Definition: clauses.c:368
#define list_make1(x1)
Definition: pg_list.h:139
List * indexquals
Definition: relation.h:1145
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
Cost indextotalcost
Definition: relation.h:1150
Cost startup_cost
Definition: relation.h:1077
Scan scan
Definition: plannodes.h:402
EquivalenceClass * parent_ec
Definition: relation.h:1902
RelOptInfo * parent
Definition: relation.h:1066
Selectivity indexselectivity
Definition: relation.h:1151
Cost startup_cost
Definition: plannodes.h:127
bool parallel_aware
Definition: plannodes.h:139
Selectivity bitmapselectivity
Definition: relation.h:1200
List * indexqual
Definition: plannodes.h:404
List * lappend(List *list, void *datum)
Definition: list.c:128
static BitmapOr * make_bitmap_or(List *bitmapplans)
Definition: createplan.c:5502
Cost total_cost
Definition: relation.h:1078
int plan_width
Definition: plannodes.h:134
#define lfirst(lc)
Definition: pg_list.h:106
bool parallel_safe
Definition: relation.h:1072
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:5184
#define nodeTag(nodeptr)
Definition: nodes.h:522
Cost total_cost
Definition: plannodes.h:128
bool parallel_safe
Definition: plannodes.h:140
#define elog
Definition: elog.h:219
List * indpred
Definition: relation.h:766
double clamp_row_est(double nrows)
Definition: costsize.c:187
bool predicate_implied_by(List *predicate_list, List *clause_list, bool weak)
Definition: predtest.c:149
Definition: pg_list.h:45
Expr * make_orclause(List *orclauses)
Definition: clauses.c:294
static Scan * create_indexscan_plan(PlannerInfo *root, IndexPath *best_path, List *tlist, List *scan_clauses, bool indexonly)
Definition: createplan.c:2624

◆ create_ctescan_plan()

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

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

3362 {
3363  CteScan *scan_plan;
3364  Index scan_relid = best_path->parent->relid;
3365  RangeTblEntry *rte;
3366  SubPlan *ctesplan = NULL;
3367  int plan_id;
3368  int cte_param_id;
3369  PlannerInfo *cteroot;
3370  Index levelsup;
3371  int ndx;
3372  ListCell *lc;
3373 
3374  Assert(scan_relid > 0);
3375  rte = planner_rt_fetch(scan_relid, root);
3376  Assert(rte->rtekind == RTE_CTE);
3377  Assert(!rte->self_reference);
3378 
3379  /*
3380  * Find the referenced CTE, and locate the SubPlan previously made for it.
3381  */
3382  levelsup = rte->ctelevelsup;
3383  cteroot = root;
3384  while (levelsup-- > 0)
3385  {
3386  cteroot = cteroot->parent_root;
3387  if (!cteroot) /* shouldn't happen */
3388  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3389  }
3390 
3391  /*
3392  * Note: cte_plan_ids can be shorter than cteList, if we are still working
3393  * on planning the CTEs (ie, this is a side-reference from another CTE).
3394  * So we mustn't use forboth here.
3395  */
3396  ndx = 0;
3397  foreach(lc, cteroot->parse->cteList)
3398  {
3399  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
3400 
3401  if (strcmp(cte->ctename, rte->ctename) == 0)
3402  break;
3403  ndx++;
3404  }
3405  if (lc == NULL) /* shouldn't happen */
3406  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
3407  if (ndx >= list_length(cteroot->cte_plan_ids))
3408  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3409  plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
3410  Assert(plan_id > 0);
3411  foreach(lc, cteroot->init_plans)
3412  {
3413  ctesplan = (SubPlan *) lfirst(lc);
3414  if (ctesplan->plan_id == plan_id)
3415  break;
3416  }
3417  if (lc == NULL) /* shouldn't happen */
3418  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3419 
3420  /*
3421  * We need the CTE param ID, which is the sole member of the SubPlan's
3422  * setParam list.
3423  */
3424  cte_param_id = linitial_int(ctesplan->setParam);
3425 
3426  /* Sort clauses into best execution order */
3427  scan_clauses = order_qual_clauses(root, scan_clauses);
3428 
3429  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3430  scan_clauses = extract_actual_clauses(scan_clauses, false);
3431 
3432  /* Replace any outer-relation variables with nestloop params */
3433  if (best_path->param_info)
3434  {
3435  scan_clauses = (List *)
3436  replace_nestloop_params(root, (Node *) scan_clauses);
3437  }
3438 
3439  scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
3440  plan_id, cte_param_id);
3441 
3442  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3443 
3444  return scan_plan;
3445 }
Plan plan
Definition: plannodes.h:342
Query * parse
Definition: relation.h:158
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4343
int plan_id
Definition: primnodes.h:690
ParamPathInfo * param_info
Definition: relation.h:1069
Definition: nodes.h:517
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4901
#define linitial_int(l)
Definition: pg_list.h:112
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
#define planner_rt_fetch(rti, root)
Definition: relation.h:332
#define ERROR
Definition: elog.h:43
Scan scan
Definition: plannodes.h:552
RelOptInfo * parent
Definition: relation.h:1066
Index relid
Definition: relation.h:628
struct PlannerInfo * parent_root
Definition: relation.h:164
static CteScan * make_ctescan(List *qptlist, List *qpqual, Index scanrelid, int ctePlanId, int cteParam)
Definition: createplan.c:5322
int list_nth_int(const List *list, int n)
Definition: list.c:421
List * cte_plan_ids
Definition: relation.h:233
bool self_reference
Definition: parsenodes.h:1034
unsigned int Index
Definition: c.h:442
List * init_plans
Definition: relation.h:231
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
List * setParam
Definition: primnodes.h:708
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:1033
RTEKind rtekind
Definition: parsenodes.h:962
List * cteList
Definition: parsenodes.h:135
char * ctename
Definition: parsenodes.h:1032
#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 3696 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().

3698 {
3699  CustomScan *cplan;
3700  RelOptInfo *rel = best_path->path.parent;
3701  List *custom_plans = NIL;
3702  ListCell *lc;
3703 
3704  /* Recursively transform child paths. */
3705  foreach(lc, best_path->custom_paths)
3706  {
3707  Plan *plan = create_plan_recurse(root, (Path *) lfirst(lc),
3708  CP_EXACT_TLIST);
3709 
3710  custom_plans = lappend(custom_plans, plan);
3711  }
3712 
3713  /*
3714  * Sort clauses into the best execution order, although custom-scan
3715  * provider can reorder them again.
3716  */
3717  scan_clauses = order_qual_clauses(root, scan_clauses);
3718 
3719  /*
3720  * Invoke custom plan provider to create the Plan node represented by the
3721  * CustomPath.
3722  */
3723  cplan = castNode(CustomScan,
3724  best_path->methods->PlanCustomPath(root,
3725  rel,
3726  best_path,
3727  tlist,
3728  scan_clauses,
3729  custom_plans));
3730 
3731  /*
3732  * Copy cost data from Path to Plan; no need to make custom-plan providers
3733  * do this
3734  */
3735  copy_generic_path_info(&cplan->scan.plan, &best_path->path);
3736 
3737  /* Likewise, copy the relids that are represented by this custom scan */
3738  cplan->custom_relids = best_path->path.parent->relids;
3739 
3740  /*
3741  * Replace any outer-relation variables with nestloop params in the qual
3742  * and custom_exprs expressions. We do this last so that the custom-plan
3743  * provider doesn't have to be involved. (Note that parts of custom_exprs
3744  * could have come from join clauses, so doing this beforehand on the
3745  * scan_clauses wouldn't work.) We assume custom_scan_tlist contains no
3746  * such variables.
3747  */
3748  if (best_path->path.param_info)
3749  {
3750  cplan->scan.plan.qual = (List *)
3751  replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
3752  cplan->custom_exprs = (List *)
3753  replace_nestloop_params(root, (Node *) cplan->custom_exprs);
3754  }
3755 
3756  return cplan;
3757 }
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:147
Plan plan
Definition: plannodes.h:342
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4343
List * custom_paths
Definition: relation.h:1273
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
ParamPathInfo * param_info
Definition: relation.h:1069
Definition: nodes.h:517
List * custom_exprs
Definition: plannodes.h:643
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4901
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:366
const struct CustomPathMethods * methods
Definition: relation.h:1275
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
RelOptInfo * parent
Definition: relation.h:1066
Relids relids
Definition: relation.h:600
Path path
Definition: relation.h:1270
List * lappend(List *list, void *datum)
Definition: list.c:128
Scan scan
Definition: plannodes.h:639
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:646
#define CP_EXACT_TLIST
Definition: createplan.c:70
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 3552 of file createplan.c.

References PlannerInfo::all_baserels, Assert, RelOptInfo::baserestrictinfo, bms_free(), bms_is_member(), RestrictInfo::clause, copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), PlannerGlobal::dependsOnRole, PathTarget::exprs, ForeignScan::fdw_exprs, ForeignPath::fdw_outerpath, ForeignScan::fdw_recheck_quals, RelOptInfo::fdwroutine, FirstLowInvalidHeapAttributeNumber, ForeignScan::fs_relids, ForeignScan::fs_server, ForeignScan::fsSystemCol, FdwRoutine::GetForeignPlan, PlannerInfo::glob, i, InvalidOid, lfirst, order_qual_clauses(), Path::param_info, Path::parent, ForeignPath::path, Scan::plan, planner_rt_fetch, pull_varattnos(), Plan::qual, RelOptInfo::relid, RangeTblEntry::relid, RelOptInfo::relids, RELOPT_UPPER_REL, RelOptInfo::reloptkind, RelOptInfo::reltarget, replace_nestloop_params(), RTE_RELATION, RelOptInfo::rtekind, RangeTblEntry::rtekind, ForeignScan::scan, RelOptInfo::serverid, and RelOptInfo::useridiscurrent.

Referenced by create_scan_plan().

3554 {
3555  ForeignScan *scan_plan;
3556  RelOptInfo *rel = best_path->path.parent;
3557  Index scan_relid = rel->relid;
3558  Oid rel_oid = InvalidOid;
3559  Plan *outer_plan = NULL;
3560 
3561  Assert(rel->fdwroutine != NULL);
3562 
3563  /* transform the child path if any */
3564  if (best_path->fdw_outerpath)
3565  outer_plan = create_plan_recurse(root, best_path->fdw_outerpath,
3566  CP_EXACT_TLIST);
3567 
3568  /*
3569  * If we're scanning a base relation, fetch its OID. (Irrelevant if
3570  * scanning a join relation.)
3571  */
3572  if (scan_relid > 0)
3573  {
3574  RangeTblEntry *rte;
3575 
3576  Assert(rel->rtekind == RTE_RELATION);
3577  rte = planner_rt_fetch(scan_relid, root);
3578  Assert(rte->rtekind == RTE_RELATION);
3579  rel_oid = rte->relid;
3580  }
3581 
3582  /*
3583  * Sort clauses into best execution order. We do this first since the FDW
3584  * might have more info than we do and wish to adjust the ordering.
3585  */
3586  scan_clauses = order_qual_clauses(root, scan_clauses);
3587 
3588  /*
3589  * Let the FDW perform its processing on the restriction clauses and
3590  * generate the plan node. Note that the FDW might remove restriction
3591  * clauses that it intends to execute remotely, or even add more (if it
3592  * has selected some join clauses for remote use but also wants them
3593  * rechecked locally).
3594  */
3595  scan_plan = rel->fdwroutine->GetForeignPlan(root, rel, rel_oid,
3596  best_path,
3597  tlist, scan_clauses,
3598  outer_plan);
3599 
3600  /* Copy cost data from Path to Plan; no need to make FDW do this */
3601  copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3602 
3603  /* Copy foreign server OID; likewise, no need to make FDW do this */
3604  scan_plan->fs_server = rel->serverid;
3605 
3606  /*
3607  * Likewise, copy the relids that are represented by this foreign scan. An
3608  * upper rel doesn't have relids set, but it covers all the base relations
3609  * participating in the underlying scan, so use root's all_baserels.
3610  */
3611  if (rel->reloptkind == RELOPT_UPPER_REL)
3612  scan_plan->fs_relids = root->all_baserels;
3613  else
3614  scan_plan->fs_relids = best_path->path.parent->relids;
3615 
3616  /*
3617  * If this is a foreign join, and to make it valid to push down we had to
3618  * assume that the current user is the same as some user explicitly named
3619  * in the query, mark the finished plan as depending on the current user.
3620  */
3621  if (rel->useridiscurrent)
3622  root->glob->dependsOnRole = true;
3623 
3624  /*
3625  * Replace any outer-relation variables with nestloop params in the qual,
3626  * fdw_exprs and fdw_recheck_quals expressions. We do this last so that
3627  * the FDW doesn't have to be involved. (Note that parts of fdw_exprs or
3628  * fdw_recheck_quals could have come from join clauses, so doing this
3629  * beforehand on the scan_clauses wouldn't work.) We assume
3630  * fdw_scan_tlist contains no such variables.
3631  */
3632  if (best_path->path.param_info)
3633  {
3634  scan_plan->scan.plan.qual = (List *)
3635  replace_nestloop_params(root, (Node *) scan_plan->scan.plan.qual);
3636  scan_plan->fdw_exprs = (List *)
3637  replace_nestloop_params(root, (Node *) scan_plan->fdw_exprs);
3638  scan_plan->fdw_recheck_quals = (List *)
3640  (Node *) scan_plan->fdw_recheck_quals);
3641  }
3642 
3643  /*
3644  * If rel is a base relation, detect whether any system columns are
3645  * requested from the rel. (If rel is a join relation, rel->relid will be
3646  * 0, but there can be no Var with relid 0 in the rel's targetlist or the
3647  * restriction clauses, so we skip this in that case. Note that any such
3648  * columns in base relations that were joined are assumed to be contained
3649  * in fdw_scan_tlist.) This is a bit of a kluge and might go away
3650  * someday, so we intentionally leave it out of the API presented to FDWs.
3651  */
3652  scan_plan->fsSystemCol = false;
3653  if (scan_relid > 0)
3654  {
3655  Bitmapset *attrs_used = NULL;
3656  ListCell *lc;
3657  int i;
3658 
3659  /*
3660  * First, examine all the attributes needed for joins or final output.
3661  * Note: we must look at rel's targetlist, not the attr_needed data,
3662  * because attr_needed isn't computed for inheritance child rels.
3663  */
3664  pull_varattnos((Node *) rel->reltarget->exprs, scan_relid, &attrs_used);
3665 
3666  /* Add all the attributes used by restriction clauses. */
3667  foreach(lc, rel->baserestrictinfo)
3668  {
3669  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3670 
3671  pull_varattnos((Node *) rinfo->clause, scan_relid, &attrs_used);
3672  }
3673 
3674  /* Now, are any system columns requested from rel? */
3675  for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
3676  {
3678  {
3679  scan_plan->fsSystemCol = true;
3680  break;
3681  }
3682  }
3683 
3684  bms_free(attrs_used);
3685  }
3686 
3687  return scan_plan;
3688 }
GetForeignPlan_function GetForeignPlan
Definition: fdwapi.h:189
List * qual
Definition: plannodes.h:147
Plan plan
Definition: plannodes.h:342
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4343
Path * fdw_outerpath
Definition: relation.h:1243
RelOptKind reloptkind
Definition: relation.h:597
Oid fs_server
Definition: plannodes.h:613
List * baserestrictinfo
Definition: relation.h:660
List * fdw_exprs
Definition: plannodes.h:614
ParamPathInfo * param_info
Definition: relation.h:1069
Definition: nodes.h:517
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4901
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
bool useridiscurrent
Definition: relation.h:649
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:130
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:366
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
#define planner_rt_fetch(rti, root)
Definition: relation.h:332
Relids all_baserels
Definition: relation.h:199
RelOptInfo * parent
Definition: relation.h:1066
PlannerGlobal * glob
Definition: relation.h:160
struct FdwRoutine * fdwroutine
Definition: relation.h:651
Relids relids
Definition: relation.h:600
List * fdw_recheck_quals
Definition: plannodes.h:617
Index relid
Definition: relation.h:628
Expr * clause
Definition: relation.h:1868
Oid serverid
Definition: relation.h:647
List * exprs
Definition: relation.h:996
unsigned int Index
Definition: c.h:442
RTEKind rtekind
Definition: relation.h:630
#define InvalidOid
Definition: postgres_ext.h:36
void bms_free(Bitmapset *a)
Definition: bitmapset.c:267
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
RTEKind rtekind
Definition: parsenodes.h:962
int i
#define CP_EXACT_TLIST
Definition: createplan.c:70
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:486
struct PathTarget * reltarget
Definition: relation.h:611
bool fsSystemCol
Definition: plannodes.h:619
Bitmapset * fs_relids
Definition: plannodes.h:618

◆ create_functionscan_plan()

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

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

3232 {
3233  FunctionScan *scan_plan;
3234  Index scan_relid = best_path->parent->relid;
3235  RangeTblEntry *rte;
3236  List *functions;
3237 
3238  /* it should be a function base rel... */
3239  Assert(scan_relid > 0);
3240  rte = planner_rt_fetch(scan_relid, root);
3241  Assert(rte->rtekind == RTE_FUNCTION);
3242  functions = rte->functions;
3243 
3244  /* Sort clauses into best execution order */
3245  scan_clauses = order_qual_clauses(root, scan_clauses);
3246 
3247  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3248  scan_clauses = extract_actual_clauses(scan_clauses, false);
3249 
3250  /* Replace any outer-relation variables with nestloop params */
3251  if (best_path->param_info)
3252  {
3253  scan_clauses = (List *)
3254  replace_nestloop_params(root, (Node *) scan_clauses);
3255  /* The function expressions could contain nestloop params, too */
3256  functions = (List *) replace_nestloop_params(root, (Node *) functions);
3257  }
3258 
3259  scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
3260  functions, rte->funcordinality);
3261 
3262  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3263 
3264  return scan_plan;
3265 }
Plan plan
Definition: plannodes.h:342
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4343
ParamPathInfo * param_info
Definition: relation.h:1069
Definition: nodes.h:517
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4901
bool funcordinality
Definition: parsenodes.h:1017
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
#define planner_rt_fetch(rti, root)
Definition: relation.h:332
RelOptInfo * parent
Definition: relation.h:1066
Index relid
Definition: relation.h:628
unsigned int Index
Definition: c.h:442
static FunctionScan * make_functionscan(List *qptlist, List *qpqual, Index scanrelid, List *functions, bool funcordinality)
Definition: createplan.c:5263
#define Assert(condition)
Definition: c.h:699
List * functions
Definition: parsenodes.h:1016
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:962
Definition: pg_list.h:45

◆ create_gather_merge_plan()

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

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

1563 {
1564  GatherMerge *gm_plan;
1565  Plan *subplan;
1566  List *pathkeys = best_path->path.pathkeys;
1567  List *tlist = build_path_tlist(root, &best_path->path);
1568 
1569  /* As with Gather, it's best to project away columns in the workers. */
1570  subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1571 
1572  /* Create a shell for a GatherMerge plan. */
1573  gm_plan = makeNode(GatherMerge);
1574  gm_plan->plan.targetlist = tlist;
1575  gm_plan->num_workers = best_path->num_workers;
1576  copy_generic_path_info(&gm_plan->plan, &best_path->path);
1577 
1578  /* Assign the rescan Param. */
1579  gm_plan->rescan_param = SS_assign_special_param(root);
1580 
1581  /* Gather Merge is pointless with no pathkeys; use Gather instead. */
1582  Assert(pathkeys != NIL);
1583 
1584  /* Compute sort column info, and adjust subplan's tlist as needed */
1585  subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1586  best_path->subpath->parent->relids,
1587  gm_plan->sortColIdx,
1588  false,
1589  &gm_plan->numCols,
1590  &gm_plan->sortColIdx,
1591  &gm_plan->sortOperators,
1592  &gm_plan->collations,
1593  &gm_plan->nullsFirst);
1594 
1595 
1596  /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1597  if (!pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys))
1598  subplan = (Plan *) make_sort(subplan, gm_plan->numCols,
1599  gm_plan->sortColIdx,
1600  gm_plan->sortOperators,
1601  gm_plan->collations,
1602  gm_plan->nullsFirst);
1603 
1604  /* Now insert the subplan under GatherMerge. */
1605  gm_plan->plan.lefttree = subplan;
1606 
1607  /* use parallel mode for parallel plans. */
1608  root->glob->parallelModeNeeded = true;
1609 
1610  return gm_plan;
1611 }
#define NIL
Definition: pg_list.h:69
Oid * collations
Definition: plannodes.h:881
int SS_assign_special_param(PlannerInfo *root)
Definition: subselect.c:429
AttrNumber * sortColIdx
Definition: plannodes.h:879
bool * nullsFirst
Definition: plannodes.h:882
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:366
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
bool parallelModeNeeded
Definition: relation.h:134
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:752
RelOptInfo * parent
Definition: relation.h:1066
PlannerGlobal * glob
Definition: relation.h:160
Relids relids
Definition: relation.h:600
Oid * sortOperators
Definition: plannodes.h:880
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:317
List * pathkeys
Definition: relation.h:1080
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
Definition: createplan.c:5629
#define makeNode(_type_)
Definition: nodes.h:565
#define Assert(condition)
Definition: c.h:699
struct Plan * lefttree
Definition: plannodes.h:148
List * targetlist
Definition: plannodes.h:146
Path * subpath
Definition: relation.h:1396
int num_workers
Definition: plannodes.h:875
int rescan_param
Definition: plannodes.h:876
#define CP_EXACT_TLIST
Definition: createplan.c:70
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:5691

◆ create_gather_plan()

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

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

1527 {
1528  Gather *gather_plan;
1529  Plan *subplan;
1530  List *tlist;
1531 
1532  /*
1533  * Although the Gather node can project, we prefer to push down such work
1534  * to its child node, so demand an exact tlist from the child.
1535  */
1536  subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1537 
1538  tlist = build_path_tlist(root, &best_path->path);
1539 
1540  gather_plan = make_gather(tlist,
1541  NIL,
1542  best_path->num_workers,
1544  best_path->single_copy,
1545  subplan);
1546 
1547  copy_generic_path_info(&gather_plan->plan, &best_path->path);
1548 
1549  /* use parallel mode for parallel plans. */
1550  root->glob->parallelModeNeeded = true;
1551 
1552  return gather_plan;
1553 }
#define NIL
Definition: pg_list.h:69
int SS_assign_special_param(PlannerInfo *root)
Definition: subselect.c:429
bool single_copy
Definition: relation.h:1384
int num_workers
Definition: relation.h:1385
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:366
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
bool parallelModeNeeded
Definition: relation.h:134
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:752
Plan plan
Definition: plannodes.h:859
PlannerGlobal * glob
Definition: relation.h:160
Path * subpath
Definition: relation.h:1383
static Gather * make_gather(List *qptlist, List *qpqual, int nworkers, int rescan_param, bool single_copy, Plan *subplan)
Definition: createplan.c:6393
Path path
Definition: relation.h:1382
#define CP_EXACT_TLIST
Definition: createplan.c:70
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 933 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().

935 {
936  Plan *gplan;
937 
938  Assert(gating_quals);
939 
940  /*
941  * Since we need a Result node anyway, always return the path's requested
942  * tlist; that's never a wrong choice, even if the parent node didn't ask
943  * for CP_EXACT_TLIST.
944  */
945  gplan = (Plan *) make_result(build_path_tlist(root, path),
946  (Node *) gating_quals,
947  plan);
948 
949  /*
950  * Notice that we don't change cost or size estimates when doing gating.
951  * The costs of qual eval were already included in the subplan's cost.
952  * Leaving the size alone amounts to assuming that the gating qual will
953  * succeed, which is the conservative estimate for planning upper queries.
954  * We certainly don't want to assume the output size is zero (unless the
955  * gating qual is actually constant FALSE, and that case is dealt with in
956  * clausesel.c). Interpolating between the two cases is silly, because it
957  * doesn't reflect what will really happen at runtime, and besides which
958  * in most cases we have only a very bad idea of the probability of the
959  * gating qual being true.
960  */
961  copy_plan_costsize(gplan, plan);
962 
963  /* Gating quals could be unsafe, so better use the Path's safety flag */
964  gplan->parallel_safe = path->parallel_safe;
965 
966  return gplan;
967 }
Definition: nodes.h:517
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:752
static void copy_plan_costsize(Plan *dest, Plan *src)
Definition: createplan.c:5010
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6516
#define Assert(condition)
Definition: c.h:699
bool parallel_safe
Definition: relation.h:1072
bool parallel_safe
Definition: plannodes.h:140

◆ create_group_plan()

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

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

1784 {
1785  Group *plan;
1786  Plan *subplan;
1787  List *tlist;
1788  List *quals;
1789 
1790  /*
1791  * Group can project, so no need to be terribly picky about child tlist,
1792  * but we do need grouping columns to be available
1793  */
1794  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1795 
1796  tlist = build_path_tlist(root, &best_path->path);
1797 
1798  quals = order_qual_clauses(root, best_path->qual);
1799 
1800  plan = make_group(tlist,
1801  quals,
1802  list_length(best_path->groupClause),
1804  subplan->targetlist),
1805  extract_grouping_ops(best_path->groupClause),
1806  subplan);
1807 
1808  copy_generic_path_info(&plan->plan, (Path *) best_path);
1809 
1810  return plan;
1811 }
List * qual
Definition: relation.h:1553
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4901
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:366
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:752
Path path
Definition: relation.h:1550
static Group * make_group(List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Plan *lefttree)
Definition: createplan.c:6218
List * groupClause
Definition: relation.h:1552
#define CP_LABEL_TLIST
Definition: createplan.c:72
Path * subpath
Definition: relation.h:1551
Plan plan
Definition: plannodes.h:774
static int list_length(const List *l)
Definition: pg_list.h:89
List * targetlist
Definition: plannodes.h:146
Definition: pg_list.h:45

◆ create_groupingsets_plan()

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

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

1930 {
1931  Agg *plan;
1932  Plan *subplan;
1933  List *rollups = best_path->rollups;
1934  AttrNumber *grouping_map;
1935  int maxref;
1936  List *chain;
1937  ListCell *lc;
1938 
1939  /* Shouldn't get here without grouping sets */
1940  Assert(root->parse->groupingSets);
1941  Assert(rollups != NIL);
1942 
1943  /*
1944  * Agg can project, so no need to be terribly picky about child tlist, but
1945  * we do need grouping columns to be available
1946  */
1947  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1948 
1949  /*
1950  * Compute the mapping from tleSortGroupRef to column index in the child's
1951  * tlist. First, identify max SortGroupRef in groupClause, for array
1952  * sizing.
1953  */
1954  maxref = 0;
1955  foreach(lc, root->parse->groupClause)
1956  {
1957  SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
1958 
1959  if (gc->tleSortGroupRef > maxref)
1960  maxref = gc->tleSortGroupRef;
1961  }
1962 
1963  grouping_map = (AttrNumber *) palloc0((maxref + 1) * sizeof(AttrNumber));
1964 
1965  /* Now look up the column numbers in the child's tlist */
1966  foreach(lc, root->parse->groupClause)
1967  {
1968  SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
1969  TargetEntry *tle = get_sortgroupclause_tle(gc, subplan->targetlist);
1970 
1971  grouping_map[gc->tleSortGroupRef] = tle->resno;
1972  }
1973 
1974  /*
1975  * During setrefs.c, we'll need the grouping_map to fix up the cols lists
1976  * in GroupingFunc nodes. Save it for setrefs.c to use.
1977  *
1978  * This doesn't work if we're in an inheritance subtree (see notes in
1979  * create_modifytable_plan). Fortunately we can't be because there would
1980  * never be grouping in an UPDATE/DELETE; but let's Assert that.
1981  */
1982  Assert(!root->hasInheritedTarget);
1983  Assert(root->grouping_map == NULL);
1984  root->grouping_map = grouping_map;
1985 
1986  /*
1987  * Generate the side nodes that describe the other sort and group
1988  * operations besides the top one. Note that we don't worry about putting
1989  * accurate cost estimates in the side nodes; only the topmost Agg node's
1990  * costs will be shown by EXPLAIN.
1991  */
1992  chain = NIL;
1993  if (list_length(rollups) > 1)
1994  {
1995  ListCell *lc2 = lnext(list_head(rollups));
1996  bool is_first_sort = ((RollupData *) linitial(rollups))->is_hashed;
1997 
1998  for_each_cell(lc, lc2)
1999  {
2000  RollupData *rollup = lfirst(lc);
2001  AttrNumber *new_grpColIdx;
2002  Plan *sort_plan = NULL;
2003  Plan *agg_plan;
2004  AggStrategy strat;
2005 
2006  new_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2007 
2008  if (!rollup->is_hashed && !is_first_sort)
2009  {
2010  sort_plan = (Plan *)
2012  new_grpColIdx,
2013  subplan);
2014  }
2015 
2016  if (!rollup->is_hashed)
2017  is_first_sort = false;
2018 
2019  if (rollup->is_hashed)
2020  strat = AGG_HASHED;
2021  else if (list_length(linitial(rollup->gsets)) == 0)
2022  strat = AGG_PLAIN;
2023  else
2024  strat = AGG_SORTED;
2025 
2026  agg_plan = (Plan *) make_agg(NIL,
2027  NIL,
2028  strat,
2030  list_length((List *) linitial(rollup->gsets)),
2031  new_grpColIdx,
2033  rollup->gsets,
2034  NIL,
2035  rollup->numGroups,
2036  sort_plan);
2037 
2038  /*
2039  * Remove stuff we don't need to avoid bloating debug output.
2040  */
2041  if (sort_plan)
2042  {
2043  sort_plan->targetlist = NIL;
2044  sort_plan->lefttree = NULL;
2045  }
2046 
2047  chain = lappend(chain, agg_plan);
2048  }
2049  }
2050 
2051  /*
2052  * Now make the real Agg node
2053  */
2054  {
2055  RollupData *rollup = linitial(rollups);
2056  AttrNumber *top_grpColIdx;
2057  int numGroupCols;
2058 
2059  top_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2060 
2061  numGroupCols = list_length((List *) linitial(rollup->gsets));
2062 
2063  plan = make_agg(build_path_tlist(root, &best_path->path),
2064  best_path->qual,
2065  best_path->aggstrategy,
2067  numGroupCols,
2068  top_grpColIdx,
2070  rollup->gsets,
2071  chain,
2072  rollup->numGroups,
2073  subplan);
2074 
2075  /* Copy cost data from Path to Plan */
2076  copy_generic_path_info(&plan->plan, &best_path->path);
2077  }
2078 
2079  return (Plan *) plan;
2080 }
#define NIL
Definition: pg_list.h:69
Query * parse
Definition: relation.h:158
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:370
List * groupClause
Definition: relation.h:1601
static AttrNumber * remap_groupColIdx(PlannerInfo *root, List *groupClause)
Definition: createplan.c:1891
Index tleSortGroupRef
Definition: parsenodes.h:1207
bool is_hashed
Definition: relation.h:1606
List * groupingSets
Definition: parsenodes.h:150
Oid * extract_grouping_ops(List *groupClause)
Definition: tlist.c:466
AttrNumber * grouping_map
Definition: relation.h:288
double numGroups
Definition: relation.h:1604
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:6149
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:366
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
#define linitial(l)
Definition: pg_list.h:111
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:752
AttrNumber resno
Definition: primnodes.h:1377
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:6052
AggStrategy aggstrategy
Definition: relation.h:1617
#define lnext(lc)
Definition: pg_list.h:105
List * lappend(List *list, void *datum)
Definition: list.c:128
Plan plan
Definition: plannodes.h:796
void * palloc0(Size size)
Definition: mcxt.c:955
#define CP_LABEL_TLIST
Definition: createplan.c:72
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
bool hasInheritedTarget
Definition: relation.h:301
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:148
List * targetlist
Definition: plannodes.h:146
AggStrategy
Definition: nodes.h:743
List * groupClause
Definition: parsenodes.h:148
Definition: plannodes.h:794
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
List * gsets
Definition: relation.h:1602

◆ create_hashjoin_plan()

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

Definition at line 4181 of file createplan.c.

References OpExpr::args, Assert, build_path_tlist(), copy_generic_path_info(), copy_plan_costsize(), CP_SMALL_TLIST, create_plan_recurse(), extract_actual_clauses(), extract_actual_join_clauses(), get_actual_clauses(), get_switched_clauses(), RangeTblEntry::inh, HashPath::inner_rows_total, JoinPath::inner_unique, JoinPath::innerjoinpath, 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, Plan::parallel_aware, Path::parallel_aware, Path::param_info, Path::parent, JoinPath::path, HashPath::path_hashclauses, Join::plan, Hash::plan, RangeTblEntry::relid, RelOptInfo::relids, replace_nestloop_params(), Hash::rows_total, RTE_RELATION, RangeTblEntry::rtekind, PlannerInfo::simple_rte_array, Plan::startup_cost, Plan::total_cost, RangeQueryClause::var, Var::varattno, and Var::varno.

Referenced by create_join_plan().

4183 {
4184  HashJoin *join_plan;
4185  Hash *hash_plan;
4186  Plan *outer_plan;
4187  Plan *inner_plan;
4188  List *tlist = build_path_tlist(root, &best_path->jpath.path);
4189  List *joinclauses;
4190  List *otherclauses;
4191  List *hashclauses;
4192  Oid skewTable = InvalidOid;
4193  AttrNumber skewColumn = InvalidAttrNumber;
4194  bool skewInherit = false;
4195 
4196  /*
4197  * HashJoin can project, so we don't have to demand exact tlists from the
4198  * inputs. However, it's best to request a small tlist from the inner
4199  * side, so that we aren't storing more data than necessary. Likewise, if
4200  * we anticipate batching, request a small tlist from the outer side so
4201  * that we don't put extra data in the outer batch files.
4202  */
4203  outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4204  (best_path->num_batches > 1) ? CP_SMALL_TLIST : 0);
4205 
4206  inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4207  CP_SMALL_TLIST);
4208 
4209  /* Sort join qual clauses into best execution order */
4210  joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4211  /* There's no point in sorting the hash clauses ... */
4212 
4213  /* Get the join qual clauses (in plain expression form) */
4214  /* Any pseudoconstant clauses are ignored here */
4215  if (IS_OUTER_JOIN(best_path->jpath.jointype))
4216  {
4217  extract_actual_join_clauses(joinclauses,
4218  best_path->jpath.path.parent->relids,
4219  &joinclauses, &otherclauses);
4220  }
4221  else
4222  {
4223  /* We can treat all clauses alike for an inner join */
4224  joinclauses = extract_actual_clauses(joinclauses, false);
4225  otherclauses = NIL;
4226  }
4227 
4228  /*
4229  * Remove the hashclauses from the list of join qual clauses, leaving the
4230  * list of quals that must be checked as qpquals.
4231  */
4232  hashclauses = get_actual_clauses(best_path->path_hashclauses);
4233  joinclauses = list_difference(joinclauses, hashclauses);
4234 
4235  /*
4236  * Replace any outer-relation variables with nestloop params. There
4237  * should not be any in the hashclauses.
4238  */
4239  if (best_path->jpath.path.param_info)
4240  {
4241  joinclauses = (List *)
4242  replace_nestloop_params(root, (Node *) joinclauses);
4243  otherclauses = (List *)
4244  replace_nestloop_params(root, (Node *) otherclauses);
4245  }
4246 
4247  /*
4248  * Rearrange hashclauses, if needed, so that the outer variable is always
4249  * on the left.
4250  */
4251  hashclauses = get_switched_clauses(best_path->path_hashclauses,
4252  best_path->jpath.outerjoinpath->parent->relids);
4253 
4254  /*
4255  * If there is a single join clause and we can identify the outer variable
4256  * as a simple column reference, supply its identity for possible use in
4257  * skew optimization. (Note: in principle we could do skew optimization
4258  * with multiple join clauses, but we'd have to be able to determine the
4259  * most common combinations of outer values, which we don't currently have
4260  * enough stats for.)
4261  */
4262  if (list_length(hashclauses) == 1)
4263  {
4264  OpExpr *clause = (OpExpr *) linitial(hashclauses);
4265  Node *node;
4266 
4267  Assert(is_opclause(clause));
4268  node = (Node *) linitial(clause->args);
4269  if (IsA(node, RelabelType))
4270  node = (Node *) ((RelabelType *) node)->arg;
4271  if (IsA(node, Var))
4272  {
4273  Var *var = (Var *) node;
4274  RangeTblEntry *rte;
4275 
4276  rte = root->simple_rte_array[var->varno];
4277  if (rte->rtekind == RTE_RELATION)
4278  {
4279  skewTable = rte->relid;
4280  skewColumn = var->varattno;
4281  skewInherit = rte->inh;
4282  }
4283  }
4284  }
4285 
4286  /*
4287  * Build the hash node and hash join node.
4288  */
4289  hash_plan = make_hash(inner_plan,
4290  skewTable,
4291  skewColumn,
4292  skewInherit);
4293 
4294  /*
4295  * Set Hash node's startup & total costs equal to total cost of input
4296  * plan; this only affects EXPLAIN display not decisions.
4297  */
4298  copy_plan_costsize(&hash_plan->plan, inner_plan);
4299  hash_plan->plan.startup_cost = hash_plan->plan.total_cost;
4300 
4301  /*
4302  * If parallel-aware, the executor will also need an estimate of the total
4303  * number of rows expected from all participants so that it can size the
4304  * shared hash table.
4305  */
4306  if (best_path->jpath.path.parallel_aware)
4307  {
4308  hash_plan->plan.parallel_aware = true;
4309  hash_plan->rows_total = best_path->inner_rows_total;
4310  }
4311 
4312  join_plan = make_hashjoin(tlist,
4313  joinclauses,
4314  otherclauses,
4315  hashclauses,
4316  outer_plan,
4317  (Plan *) hash_plan,
4318  best_path->jpath.jointype,
4319  best_path->jpath.inner_unique);
4320 
4321  copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4322 
4323  return join_plan;
4324 }
double rows_total
Definition: plannodes.h:902
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
JoinPath jpath
Definition: relation.h:1488
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4343
static List * get_switched_clauses(List *clauses, Relids outerrelids)
Definition: createplan.c:4824
int num_batches
Definition: relation.h:1490
static HashJoin * make_hashjoin(List *tlist, List *joinclauses, List *otherclauses, List *hashclauses, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
Definition: createplan.c:5542
void extract_actual_join_clauses(List *restrictinfo_list, Relids joinrelids, List **joinquals, List **otherquals)
Definition: restrictinfo.c:383
Path * innerjoinpath
Definition: relation.h:1415
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:730
ParamPathInfo * param_info
Definition: relation.h:1069
#define CP_SMALL_TLIST
Definition: createplan.c:71
Definition: nodes.h:517
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:333
AttrNumber varattno
Definition: primnodes.h:169
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4901
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:164
double inner_rows_total
Definition: relation.h:1491
Join join
Definition: plannodes.h:739
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:366
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
#define linitial(l)
Definition: pg_list.h:111
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:752
#define is_opclause(clause)
Definition: clauses.h:20
List * joinrestrictinfo
Definition: relation.h:1417
RelOptInfo * parent
Definition: relation.h:1066
Cost startup_cost
Definition: plannodes.h:127
static void copy_plan_costsize(Plan *dest, Plan *src)
Definition: createplan.c:5010
bool parallel_aware
Definition: plannodes.h:139
Relids relids
Definition: relation.h:600
RangeTblEntry ** simple_rte_array
Definition: relation.h:191
Index varno
Definition: primnodes.h:167
Path * outerjoinpath
Definition: relation.h:1414
#define InvalidOid
Definition: postgres_ext.h:36
Path path
Definition: relation.h:1407
#define Assert(condition)
Definition: c.h:699
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:1411
List * list_difference(const List *list1, const List *list2)
Definition: list.c:858
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:962
Plan plan
Definition: plannodes.h:897
JoinType jointype
Definition: relation.h:1409
static Hash * make_hash(Plan *lefttree, Oid skewTable, AttrNumber skewColumn, bool skewInherit)
Definition: createplan.c:5567
Cost total_cost
Definition: plannodes.h:128
bool parallel_aware
Definition: relation.h:1071
List * path_hashclauses
Definition: relation.h:1489
List * args
Definition: primnodes.h:503
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
Plan plan
Definition: plannodes.h:680

◆ create_indexscan_plan()

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

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

2629 {
2630  Scan *scan_plan;
2631  List *indexquals = best_path->indexquals;
2632  List *indexorderbys = best_path->indexorderbys;
2633  Index baserelid = best_path->path.parent->relid;
2634  Oid indexoid = best_path->indexinfo->indexoid;
2635  List *qpqual;
2636  List *stripped_indexquals;
2637  List *fixed_indexquals;
2638  List *fixed_indexorderbys;
2639  List *indexorderbyops = NIL;
2640  ListCell *l;
2641 
2642  /* it should be a base rel... */
2643  Assert(baserelid > 0);
2644  Assert(best_path->path.parent->rtekind == RTE_RELATION);
2645 
2646  /*
2647  * Build "stripped" indexquals structure (no RestrictInfos) to pass to
2648  * executor as indexqualorig
2649  */
2650  stripped_indexquals = get_actual_clauses(indexquals);
2651 
2652  /*
2653  * The executor needs a copy with the indexkey on the left of each clause
2654  * and with index Vars substituted for table ones.
2655  */
2656  fixed_indexquals = fix_indexqual_references(root, best_path);
2657 
2658  /*
2659  * Likewise fix up index attr references in the ORDER BY expressions.
2660  */
2661  fixed_indexorderbys = fix_indexorderby_references(root, best_path);
2662 
2663  /*
2664  * The qpqual list must contain all restrictions not automatically handled
2665  * by the index, other than pseudoconstant clauses which will be handled
2666  * by a separate gating plan node. All the predicates in the indexquals
2667  * will be checked (either by the index itself, or by nodeIndexscan.c),
2668  * but if there are any "special" operators involved then they must be
2669  * included in qpqual. The upshot is that qpqual must contain
2670  * scan_clauses minus whatever appears in indexquals.
2671  *
2672  * In normal cases simple pointer equality checks will be enough to spot
2673  * duplicate RestrictInfos, so we try that first.
2674  *
2675  * Another common case is that a scan_clauses entry is generated from the
2676  * same EquivalenceClass as some indexqual, and is therefore redundant
2677  * with it, though not equal. (This happens when indxpath.c prefers a
2678  * different derived equality than what generate_join_implied_equalities
2679  * picked for a parameterized scan's ppi_clauses.)
2680  *
2681  * In some situations (particularly with OR'd index conditions) we may
2682  * have scan_clauses that are not equal to, but are logically implied by,
2683  * the index quals; so we also try a predicate_implied_by() check to see
2684  * if we can discard quals that way. (predicate_implied_by assumes its
2685  * first input contains only immutable functions, so we have to check
2686  * that.)
2687  *
2688  * Note: if you change this bit of code you should also look at
2689  * extract_nonindex_conditions() in costsize.c.
2690  */
2691  qpqual = NIL;
2692  foreach(l, scan_clauses)
2693  {
2694  RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
2695 
2696  if (rinfo->pseudoconstant)
2697  continue; /* we may drop pseudoconstants here */
2698  if (list_member_ptr(indexquals, rinfo))
2699  continue; /* simple duplicate */
2700  if (is_redundant_derived_clause(rinfo, indexquals))
2701  continue; /* derived from same EquivalenceClass */
2702  if (!contain_mutable_functions((Node *) rinfo->clause) &&
2703  predicate_implied_by(list_make1(rinfo->clause), indexquals, false))
2704  continue; /* provably implied by indexquals */
2705  qpqual = lappend(qpqual, rinfo);
2706  }
2707 
2708  /* Sort clauses into best execution order */
2709  qpqual = order_qual_clauses(root, qpqual);
2710 
2711  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2712  qpqual = extract_actual_clauses(qpqual, false);
2713 
2714  /*
2715  * We have to replace any outer-relation variables with nestloop params in
2716  * the indexqualorig, qpqual, and indexorderbyorig expressions. A bit
2717  * annoying to have to do this separately from the processing in
2718  * fix_indexqual_references --- rethink this when generalizing the inner
2719  * indexscan support. But note we can't really do this earlier because
2720  * it'd break the comparisons to predicates above ... (or would it? Those
2721  * wouldn't have outer refs)
2722  */
2723  if (best_path->path.param_info)
2724  {
2725  stripped_indexquals = (List *)
2726  replace_nestloop_params(root, (Node *) stripped_indexquals);
2727  qpqual = (List *)
2728  replace_nestloop_params(root, (Node *) qpqual);
2729  indexorderbys = (List *)
2730  replace_nestloop_params(root, (Node *) indexorderbys);
2731  }
2732 
2733  /*
2734  * If there are ORDER BY expressions, look up the sort operators for their
2735  * result datatypes.
2736  */
2737  if (indexorderbys)
2738  {
2739  ListCell *pathkeyCell,
2740  *exprCell;
2741 
2742  /*
2743  * PathKey contains OID of the btree opfamily we're sorting by, but
2744  * that's not quite enough because we need the expression's datatype
2745  * to look up the sort operator in the operator family.
2746  */
2747  Assert(list_length(best_path->path.pathkeys) == list_length(indexorderbys));
2748  forboth(pathkeyCell, best_path->path.pathkeys, exprCell, indexorderbys)
2749  {
2750  PathKey *pathkey = (PathKey *) lfirst(pathkeyCell);
2751  Node *expr = (Node *) lfirst(exprCell);
2752  Oid exprtype = exprType(expr);
2753  Oid sortop;
2754 
2755  /* Get sort operator from opfamily */
2756  sortop = get_opfamily_member(pathkey->pk_opfamily,
2757  exprtype,
2758  exprtype,
2759  pathkey->pk_strategy);
2760  if (!OidIsValid(sortop))
2761  elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
2762  pathkey->pk_strategy, exprtype, exprtype, pathkey->pk_opfamily);
2763  indexorderbyops = lappend_oid(indexorderbyops, sortop);
2764  }
2765  }
2766 
2767  /* Finally ready to build the plan node */
2768  if (indexonly)
2769  scan_plan = (Scan *) make_indexonlyscan(tlist,
2770  qpqual,
2771  baserelid,
2772  indexoid,
2773  fixed_indexquals,
2774  fixed_indexorderbys,
2775  best_path->indexinfo->indextlist,
2776  best_path->indexscandir);
2777  else
2778  scan_plan = (Scan *) make_indexscan(tlist,
2779  qpqual,
2780  baserelid,
2781  indexoid,
2782  fixed_indexquals,
2783  stripped_indexquals,
2784  fixed_indexorderbys,
2785  indexorderbys,
2786  indexorderbyops,
2787  best_path->indexscandir);
2788 
2789  copy_generic_path_info(&scan_plan->plan, &best_path->path);
2790 
2791  return scan_plan;
2792 }
#define NIL
Definition: pg_list.h:69
Plan plan
Definition: plannodes.h:342
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4343
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
Path path
Definition: relation.h:1142
bool is_redundant_derived_clause(RestrictInfo *rinfo, List *clauselist)
Definition: equivclass.c:2494
IndexOptInfo * indexinfo
Definition: relation.h:1143
List * indextlist
Definition: relation.h:768
bool pseudoconstant
Definition: relation.h:1876
ParamPathInfo * param_info
Definition: relation.h:1069
Definition: nodes.h:517
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:333
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4901
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:605
int pk_strategy
Definition: relation.h:965
#define list_make1(x1)
Definition: pg_list.h:139
List * indexquals
Definition: relation.h:1145
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
#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:5126
RelOptInfo * parent
Definition: relation.h:1066
#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:4692
static List * fix_indexqual_references(PlannerInfo *root, IndexPath *index_path)
Definition: createplan.c:4561
Index relid
Definition: relation.h:628
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1868
List * indexorderbys
Definition: relation.h:1147
unsigned int Index
Definition: c.h:442
RTEKind rtekind
Definition: relation.h:630
bool list_member_ptr(const List *list, const void *datum)
Definition: list.c:465
List * pathkeys
Definition: relation.h:1080
static IndexOnlyScan * make_indexonlyscan(List *qptlist, List *qpqual, Index scanrelid, Oid indexid, List *indexqual, List *indexorderby, List *indextlist, ScanDirection indexscandir)
Definition: createplan.c:5157
#define Assert(condition)
Definition: c.h:699
#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:964
ScanDirection indexscandir
Definition: relation.h:1149
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:879
#define elog
Definition: elog.h:219
Oid indexoid
Definition: relation.h:741
bool predicate_implied_by(List *predicate_list, List *clause_list, bool weak)
Definition: predtest.c:149
Definition: pg_list.h:45

◆ create_join_plan()

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

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

976 {
977  Plan *plan;
978  List *gating_clauses;
979 
980  switch (best_path->path.pathtype)
981  {
982  case T_MergeJoin:
983  plan = (Plan *) create_mergejoin_plan(root,
984  (MergePath *) best_path);
985  break;
986  case T_HashJoin:
987  plan = (Plan *) create_hashjoin_plan(root,
988  (HashPath *) best_path);
989  break;
990  case T_NestLoop:
991  plan = (Plan *) create_nestloop_plan(root,
992  (NestPath *) best_path);
993  break;
994  default:
995  elog(ERROR, "unrecognized node type: %d",
996  (int) best_path->path.pathtype);
997  plan = NULL; /* keep compiler quiet */
998  break;
999  }
1000 
1001  /*
1002  * If there are any pseudoconstant clauses attached to this node, insert a
1003  * gating Result node that evaluates the pseudoconstants as one-time
1004  * quals.
1005  */
1006  gating_clauses = get_gating_quals(root, best_path->joinrestrictinfo);
1007  if (gating_clauses)
1008  plan = create_gating_plan(root, (Path *) best_path, plan,
1009  gating_clauses);
1010 
1011 #ifdef NOT_USED
1012 
1013  /*
1014  * * Expensive function pullups may have pulled local predicates * into
1015  * this path node. Put them in the qpqual of the plan node. * JMH,
1016  * 6/15/92
1017  */
1018  if (get_loc_restrictinfo(best_path) != NIL)
1019  set_qpqual((Plan) plan,
1020  list_concat(get_qpqual((Plan) plan),
1021  get_actual_clauses(get_loc_restrictinfo(best_path))));
1022 #endif
1023 
1024  return plan;
1025 }
#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:3767
NodeTag pathtype
Definition: relation.h:1064
#define ERROR
Definition: elog.h:43
List * joinrestrictinfo
Definition: relation.h:1417
Path path
Definition: relation.h:1407
static HashJoin * create_hashjoin_plan(PlannerInfo *root, HashPath *best_path)
Definition: createplan.c:4181
static Plan * create_gating_plan(PlannerInfo *root, Path *path, Plan *plan, List *gating_quals)
Definition: createplan.c:933
#define elog
Definition: elog.h:219
static MergeJoin * create_mergejoin_plan(PlannerInfo *root, MergePath *best_path)
Definition: createplan.c:3874
Definition: pg_list.h:45
static List * get_gating_quals(PlannerInfo *root, List *quals)
Definition: createplan.c:913

◆ create_limit_plan()

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

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

2505 {
2506  Limit *plan;
2507  Plan *subplan;
2508 
2509  /* Limit doesn't project, so tlist requirements pass through */
2510  subplan = create_plan_recurse(root, best_path->subpath, flags);
2511 
2512  plan = make_limit(subplan,
2513  best_path->limitOffset,
2514  best_path->limitCount);
2515 
2516  copy_generic_path_info(&plan->plan, (Path *) best_path);
2517 
2518  return plan;
2519 }
Plan plan
Definition: plannodes.h:948
Node * limitOffset
Definition: relation.h:1719
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:366
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
Path * subpath
Definition: relation.h:1718
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
Definition: createplan.c:6495
Node * limitCount
Definition: relation.h:1720

◆ create_lockrows_plan()

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

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

2423 {
2424  LockRows *plan;
2425  Plan *subplan;
2426 
2427  /* LockRows doesn't project, so tlist requirements pass through */
2428  subplan = create_plan_recurse(root, best_path->subpath, flags);
2429 
2430  plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
2431 
2432  copy_generic_path_info(&plan->plan, (Path *) best_path);
2433 
2434  return plan;
2435 }
Plan plan
Definition: plannodes.h:934
List * rowMarks
Definition: relation.h:1682
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:366
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
Path * subpath
Definition: relation.h:1681
static LockRows * make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
Definition: createplan.c:6474

◆ create_material_plan()

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

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

1296 {
1297  Material *plan;
1298  Plan *subplan;
1299 
1300  /*
1301  * We don't want any excess columns in the materialized tuples, so request
1302  * a smaller tlist. Otherwise, since Material doesn't project, tlist
1303  * requirements pass through.
1304  */
1305  subplan = create_plan_recurse(root, best_path->subpath,
1306  flags | CP_SMALL_TLIST);
1307 
1308  plan = make_material(subplan);
1309 
1310  copy_generic_path_info(&plan->plan, (Path *) best_path);
1311 
1312  return plan;
1313 }
#define CP_SMALL_TLIST
Definition: createplan.c:71
Path * subpath
Definition: relation.h:1344
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:366
static Material * make_material(Plan *lefttree)
Definition: createplan.c:6093
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
Plan plan
Definition: plannodes.h:749

◆ create_merge_append_plan()

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

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

1137 {
1138  MergeAppend *node = makeNode(MergeAppend);
1139  Plan *plan = &node->plan;
1140  List *tlist = build_path_tlist(root, &best_path->path);
1141  List *pathkeys = best_path->path.pathkeys;
1142  List *subplans = NIL;
1143  ListCell *subpaths;
1144 
1145  /*
1146  * We don't have the actual creation of the MergeAppend node split out
1147  * into a separate make_xxx function. This is because we want to run
1148  * prepare_sort_from_pathkeys on it before we do so on the individual
1149  * child plans, to make cross-checking the sort info easier.
1150  */
1151  copy_generic_path_info(plan, (Path *) best_path);
1152  plan->targetlist = tlist;
1153  plan->qual = NIL;
1154  plan->lefttree = NULL;
1155  plan->righttree = NULL;
1156 
1157  /* Compute sort column info, and adjust MergeAppend's tlist as needed */
1158  (void) prepare_sort_from_pathkeys(plan, pathkeys,
1159  best_path->path.parent->relids,
1160  NULL,
1161  true,
1162  &node->numCols,
1163  &node->sortColIdx,
1164  &node->sortOperators,
1165  &node->collations,
1166  &node->nullsFirst);
1167 
1168  /*
1169  * Now prepare the child plans. We must apply prepare_sort_from_pathkeys
1170  * even to subplans that don't need an explicit sort, to make sure they
1171  * are returning the same sort key columns the MergeAppend expects.
1172  */
1173  foreach(subpaths, best_path->subpaths)
1174  {
1175  Path *subpath = (Path *) lfirst(subpaths);
1176  Plan *subplan;
1177  int numsortkeys;
1178  AttrNumber *sortColIdx;
1179  Oid *sortOperators;
1180  Oid *collations;
1181  bool *nullsFirst;
1182 
1183  /* Build the child plan */
1184  /* Must insist that all children return the same tlist */
1185  subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
1186 
1187  /* Compute sort column info, and adjust subplan's tlist as needed */
1188  subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1189  subpath->parent->relids,
1190  node->sortColIdx,
1191  false,
1192  &numsortkeys,
1193  &sortColIdx,
1194  &sortOperators,
1195  &collations,
1196  &nullsFirst);
1197 
1198  /*
1199  * Check that we got the same sort key information. We just Assert
1200  * that the sortops match, since those depend only on the pathkeys;
1201  * but it seems like a good idea to check the sort column numbers
1202  * explicitly, to ensure the tlists really do match up.
1203  */
1204  Assert(numsortkeys == node->numCols);
1205  if (memcmp(sortColIdx, node->sortColIdx,
1206  numsortkeys * sizeof(AttrNumber)) != 0)
1207  elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
1208  Assert(memcmp(sortOperators, node->sortOperators,
1209  numsortkeys * sizeof(Oid)) == 0);
1210  Assert(memcmp(collations, node->collations,
1211  numsortkeys * sizeof(Oid)) == 0);
1212  Assert(memcmp(nullsFirst, node->nullsFirst,
1213  numsortkeys * sizeof(bool)) == 0);
1214 
1215  /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1216  if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
1217  {
1218  Sort *sort = make_sort(subplan, numsortkeys,
1219  sortColIdx, sortOperators,
1220  collations, nullsFirst);
1221 
1222  label_sort_with_costsize(root, sort, best_path->limit_tuples);
1223  subplan = (Plan *) sort;
1224  }
1225 
1226  subplans = lappend(subplans, subplan);
1227  }
1228 
1229  node->partitioned_rels = best_path->partitioned_rels;
1230  node->mergeplans = subplans;
1231 
1232  return (Plan *) node;
1233 }
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:147
Oid * collations
Definition: plannodes.h:282
List * partitioned_rels
Definition: relation.h:1317
unsigned int Oid
Definition: postgres_ext.h:31
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:366
struct Plan * righttree
Definition: plannodes.h:149
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
#define ERROR
Definition: elog.h:43
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:752
RelOptInfo * parent
Definition: relation.h:1066
List * partitioned_rels
Definition: plannodes.h:276
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:200
Relids relids
Definition: relation.h:600
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:1080
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
Definition: createplan.c:5629
#define makeNode(_type_)
Definition: nodes.h:565
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
AttrNumber * sortColIdx
Definition: plannodes.h:280
struct Plan * lefttree
Definition: plannodes.h:148
List * targetlist
Definition: plannodes.h:146
bool * nullsFirst
Definition: plannodes.h:283
List * mergeplans
Definition: plannodes.h:277
Oid * sortOperators
Definition: plannodes.h:281
List * subpaths
Definition: relation.h:1318
#define elog
Definition: elog.h:219
#define CP_EXACT_TLIST
Definition: createplan.c:70
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:5032
double limit_tuples
Definition: relation.h:1319
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:5691

◆ create_mergejoin_plan()

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

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

3876 {
3877  MergeJoin *join_plan;
3878  Plan *outer_plan;
3879  Plan *inner_plan;
3880  List *tlist = build_path_tlist(root, &best_path->jpath.path);
3881  List *joinclauses;
3882  List *otherclauses;
3883  List *mergeclauses;
3884  List *outerpathkeys;
3885  List *innerpathkeys;
3886  int nClauses;
3887  Oid *mergefamilies;
3888  Oid *mergecollations;
3889  int *mergestrategies;
3890  bool *mergenullsfirst;
3891  PathKey *opathkey;
3892  EquivalenceClass *opeclass;
3893  int i;
3894  ListCell *lc;
3895  ListCell *lop;
3896  ListCell *lip;
3897  Path *outer_path = best_path->jpath.outerjoinpath;
3898  Path *inner_path = best_path->jpath.innerjoinpath;
3899 
3900  /*
3901  * MergeJoin can project, so we don't have to demand exact tlists from the
3902  * inputs. However, if we're intending to sort an input's result, it's
3903  * best to request a small tlist so we aren't sorting more data than
3904  * necessary.
3905  */
3906  outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
3907  (best_path->outersortkeys != NIL) ? CP_SMALL_TLIST : 0);
3908 
3909  inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
3910  (best_path->innersortkeys != NIL) ? CP_SMALL_TLIST : 0);
3911 
3912  /* Sort join qual clauses into best execution order */
3913  /* NB: do NOT reorder the mergeclauses */
3914  joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
3915 
3916  /* Get the join qual clauses (in plain expression form) */
3917  /* Any pseudoconstant clauses are ignored here */
3918  if (IS_OUTER_JOIN(best_path->jpath.jointype))
3919  {
3920  extract_actual_join_clauses(joinclauses,
3921  best_path->jpath.path.parent->relids,
3922  &joinclauses, &otherclauses);
3923  }
3924  else
3925  {
3926  /* We can treat all clauses alike for an inner join */
3927  joinclauses = extract_actual_clauses(joinclauses, false);
3928  otherclauses = NIL;
3929  }
3930 
3931  /*
3932  * Remove the mergeclauses from the list of join qual clauses, leaving the
3933  * list of quals that must be checked as qpquals.
3934  */
3935  mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
3936  joinclauses = list_difference(joinclauses, mergeclauses);
3937 
3938  /*
3939  * Replace any outer-relation variables with nestloop params. There
3940  * should not be any in the mergeclauses.
3941  */
3942  if (best_path->jpath.path.param_info)
3943  {
3944  joinclauses = (List *)
3945  replace_nestloop_params(root, (Node *) joinclauses);
3946  otherclauses = (List *)
3947  replace_nestloop_params(root, (Node *) otherclauses);
3948  }
3949 
3950  /*
3951  * Rearrange mergeclauses, if needed, so that the outer variable is always
3952  * on the left; mark the mergeclause restrictinfos with correct
3953  * outer_is_left status.
3954  */
3955  mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
3956  best_path->jpath.outerjoinpath->parent->relids);
3957 
3958  /*
3959  * Create explicit sort nodes for the outer and inner paths if necessary.
3960  */
3961  if (best_path->outersortkeys)
3962  {
3963  Relids outer_relids = outer_path->parent->relids;
3964  Sort *sort = make_sort_from_pathkeys(outer_plan,
3965  best_path->outersortkeys,
3966  outer_relids);
3967 
3968  label_sort_with_costsize(root, sort, -1.0);
3969  outer_plan = (Plan *) sort;
3970  outerpathkeys = best_path->outersortkeys;
3971  }
3972  else
3973  outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
3974 
3975  if (best_path->innersortkeys)
3976  {
3977  Relids inner_relids = inner_path->parent->relids;
3978  Sort *sort = make_sort_from_pathkeys(inner_plan,
3979  best_path->innersortkeys,
3980  inner_relids);
3981 
3982  label_sort_with_costsize(root, sort, -1.0);
3983  inner_plan = (Plan *) sort;
3984  innerpathkeys = best_path->innersortkeys;
3985  }
3986  else
3987  innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
3988 
3989  /*
3990  * If specified, add a materialize node to shield the inner plan from the
3991  * need to handle mark/restore.
3992  */
3993  if (best_path->materialize_inner)
3994  {
3995  Plan *matplan = (Plan *) make_material(inner_plan);
3996 
3997  /*
3998  * We assume the materialize will not spill to disk, and therefore
3999  * charge just cpu_operator_cost per tuple. (Keep this estimate in
4000  * sync with final_cost_mergejoin.)
4001  */
4002  copy_plan_costsize(matplan, inner_plan);
4003  matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
4004 
4005  inner_plan = matplan;
4006  }
4007 
4008  /*
4009  * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
4010  * executor. The information is in the pathkeys for the two inputs, but
4011  * we need to be careful about the possibility of mergeclauses sharing a
4012  * pathkey, as well as the possibility that the inner pathkeys are not in
4013  * an order matching the mergeclauses.
4014  */
4015  nClauses = list_length(mergeclauses);
4016  Assert(nClauses == list_length(best_path->path_mergeclauses));
4017  mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
4018  mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
4019  mergestrategies = (int *) palloc(nClauses * sizeof(int));
4020  mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
4021 
4022  opathkey = NULL;
4023  opeclass = NULL;
4024  lop = list_head(outerpathkeys);
4025  lip = list_head(innerpathkeys);
4026  i = 0;
4027  foreach(lc, best_path->path_mergeclauses)
4028  {
4029  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
4030  EquivalenceClass *oeclass;
4031  EquivalenceClass *ieclass;
4032  PathKey *ipathkey = NULL;
4033  EquivalenceClass *ipeclass = NULL;
4034  bool first_inner_match = false;
4035 
4036  /* fetch outer/inner eclass from mergeclause */
4037  if (rinfo->outer_is_left)
4038  {
4039  oeclass = rinfo->left_ec;
4040  ieclass = rinfo->right_ec;
4041  }
4042  else
4043  {
4044  oeclass = rinfo->right_ec;
4045  ieclass = rinfo->left_ec;
4046  }
4047  Assert(oeclass != NULL);
4048  Assert(ieclass != NULL);
4049 
4050  /*
4051  * We must identify the pathkey elements associated with this clause
4052  * by matching the eclasses (which should give a unique match, since
4053  * the pathkey lists should be canonical). In typical cases the merge
4054  * clauses are one-to-one with the pathkeys, but when dealing with
4055  * partially redundant query conditions, things are more complicated.
4056  *
4057  * lop and lip reference the first as-yet-unmatched pathkey elements.
4058  * If they're NULL then all pathkey elements have been matched.
4059  *
4060  * The ordering of the outer pathkeys should match the mergeclauses,
4061  * by construction (see find_mergeclauses_for_outer_pathkeys()). There
4062  * could be more than one mergeclause for the same outer pathkey, but
4063  * no pathkey may be entirely skipped over.
4064  */
4065  if (oeclass != opeclass) /* multiple matches are not interesting */
4066  {
4067  /* doesn't match the current opathkey, so must match the next */
4068  if (lop == NULL)
4069  elog(ERROR, "outer pathkeys do not match mergeclauses");
4070  opathkey = (PathKey *) lfirst(lop);
4071  opeclass = opathkey->pk_eclass;
4072  lop = lnext(lop);
4073  if (oeclass != opeclass)
4074  elog(ERROR, "outer pathkeys do not match mergeclauses");
4075  }
4076 
4077  /*
4078  * The inner pathkeys likewise should not have skipped-over keys, but
4079  * it's possible for a mergeclause to reference some earlier inner
4080  * pathkey if we had redundant pathkeys. For example we might have
4081  * mergeclauses like "o.a = i.x AND o.b = i.y AND o.c = i.x". The
4082  * implied inner ordering is then "ORDER BY x, y, x", but the pathkey
4083  * mechanism drops the second sort by x as redundant, and this code
4084  * must cope.
4085  *
4086  * It's also possible for the implied inner-rel ordering to be like
4087  * "ORDER BY x, y, x DESC". We still drop the second instance of x as
4088  * redundant; but this means that the sort ordering of a redundant
4089  * inner pathkey should not be considered significant. So we must
4090  * detect whether this is the first clause matching an inner pathkey.
4091  */
4092  if (lip)
4093  {
4094  ipathkey = (PathKey *) lfirst(lip);
4095  ipeclass = ipathkey->pk_eclass;
4096  if (ieclass == ipeclass)
4097  {
4098  /* successful first match to this inner pathkey */
4099  lip = lnext(lip);
4100  first_inner_match = true;
4101  }
4102  }
4103  if (!first_inner_match)
4104  {
4105  /* redundant clause ... must match something before lip */
4106  ListCell *l2;
4107 
4108  foreach(l2, innerpathkeys)
4109  {
4110  if (l2 == lip)
4111  break;
4112  ipathkey = (PathKey *) lfirst(l2);
4113  ipeclass = ipathkey->pk_eclass;
4114  if (ieclass == ipeclass)
4115  break;
4116  }
4117  if (ieclass != ipeclass)
4118  elog(ERROR, "inner pathkeys do not match mergeclauses");
4119  }
4120 
4121  /*
4122  * The pathkeys should always match each other as to opfamily and
4123  * collation (which affect equality), but if we're considering a
4124  * redundant inner pathkey, its sort ordering might not match. In
4125  * such cases we may ignore the inner pathkey's sort ordering and use
4126  * the outer's. (In effect, we're lying to the executor about the
4127  * sort direction of this inner column, but it does not matter since
4128  * the run-time row comparisons would only reach this column when
4129  * there's equality for the earlier column containing the same eclass.
4130  * There could be only one value in this column for the range of inner
4131  * rows having a given value in the earlier column, so it does not
4132  * matter which way we imagine this column to be ordered.) But a
4133  * non-redundant inner pathkey had better match outer's ordering too.
4134  */
4135  if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
4136  opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation)
4137  elog(ERROR, "left and right pathkeys do not match in mergejoin");
4138  if (first_inner_match &&
4139  (opathkey->pk_strategy != ipathkey->pk_strategy ||
4140  opathkey->pk_nulls_first != ipathkey->pk_nulls_first))
4141  elog(ERROR, "left and right pathkeys do not match in mergejoin");
4142 
4143  /* OK, save info for executor */
4144  mergefamilies[i] = opathkey->pk_opfamily;
4145  mergecollations[i] = opathkey->pk_eclass->ec_collation;
4146  mergestrategies[i] = opathkey->pk_strategy;
4147  mergenullsfirst[i] = opathkey->pk_nulls_first;
4148  i++;
4149  }
4150 
4151  /*
4152  * Note: it is not an error if we have additional pathkey elements (i.e.,
4153  * lop or lip isn't NULL here). The input paths might be better-sorted
4154  * than we need for the current mergejoin.
4155  */
4156 
4157  /*
4158  * Now we can build the mergejoin node.
4159  */
4160  join_plan = make_mergejoin(tlist,
4161  joinclauses,
4162  otherclauses,
4163  mergeclauses,
4164  mergefamilies,
4165  mergecollations,
4166  mergestrategies,
4167  mergenullsfirst,
4168  outer_plan,
4169  inner_plan,
4170  best_path->jpath.jointype,
4171  best_path->jpath.inner_unique,
4172  best_path->skip_mark_restore);
4173 
4174  /* Costs of sort and material steps are included in path cost already */
4175  copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4176 
4177  return join_plan;
4178 }
#define NIL
Definition: pg_list.h:69
List * path_mergeclauses
Definition: relation.h:1470
List * outersortkeys
Definition: relation.h:1471
double plan_rows
Definition: plannodes.h:133
static Sort * make_sort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids)
Definition: createplan.c:5970
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4343
static List * get_switched_clauses(List *clauses, Relids outerrelids)
Definition: createplan.c:4824
bool materialize_inner
Definition: relation.h:1474
void extract_actual_join_clauses(List *restrictinfo_list, Relids joinrelids, 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:5588
Path * innerjoinpath
Definition: relation.h:1415
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:730
ParamPathInfo * param_info
Definition: relation.h:1069
#define CP_SMALL_TLIST
Definition: createplan.c:71
Definition: nodes.h:517
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:333
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4901
EquivalenceClass * right_ec
Definition: relation.h:1917
unsigned int Oid
Definition: postgres_ext.h:31
int pk_strategy
Definition: relation.h:965
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:366
static Material * make_material(Plan *lefttree)
Definition: createplan.c:6093
bool skip_mark_restore
Definition: relation.h:1473
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
bool pk_nulls_first
Definition: relation.h:966
#define ERROR
Definition: elog.h:43
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:752
List * joinrestrictinfo
Definition: relation.h:1417
RelOptInfo * parent
Definition: relation.h:1066
#define lfirst_node(type, lc)
Definition: pg_list.h:109
bool outer_is_left
Definition: relation.h:1923
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:200
static void copy_plan_costsize(Plan *dest, Plan *src)
Definition: createplan.c:5010
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
Relids relids
Definition: relation.h:600
double cpu_operator_cost
Definition: costsize.c:115
#define lnext(lc)
Definition: pg_list.h:105
Path * outerjoinpath
Definition: relation.h:1414
List * pathkeys
Definition: relation.h:1080
Path path
Definition: relation.h:1407
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
EquivalenceClass * pk_eclass
Definition: relation.h:963
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:1411
List * list_difference(const List *list1, const List *list2)
Definition: list.c:858
List * innersortkeys
Definition: relation.h:1472
Oid pk_opfamily
Definition: relation.h:964
void * palloc(Size size)
Definition: mcxt.c:924
EquivalenceClass * left_ec
Definition: relation.h:1916
Join join
Definition: plannodes.h:723
JoinType jointype
Definition: relation.h:1409
int i
Cost total_cost
Definition: plannodes.h:128
JoinPath jpath
Definition: relation.h:1469
#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:5032
Plan plan
Definition: plannodes.h:680

◆ create_minmaxagg_plan()

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

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

2090 {
2091  Result *plan;
2092  List *tlist;
2093  ListCell *lc;
2094 
2095  /* Prepare an InitPlan for each aggregate's subquery. */
2096  foreach(lc, best_path->mmaggregates)
2097  {
2098  MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
2099  PlannerInfo *subroot = mminfo->subroot;
2100  Query *subparse = subroot->parse;
2101  Plan *plan;
2102 
2103  /*
2104  * Generate the plan for the subquery. We already have a Path, but we
2105  * have to convert it to a Plan and attach a LIMIT node above it.
2106  * Since we are entering a different planner context (subroot),
2107  * recurse to create_plan not create_plan_recurse.
2108  */
2109  plan = create_plan(subroot, mminfo->path);
2110 
2111  plan = (Plan *) make_limit(plan,
2112  subparse->limitOffset,
2113  subparse->limitCount);
2114 
2115  /* Must apply correct cost/width data to Limit node */
2116  plan->startup_cost = mminfo->path->startup_cost;
2117  plan->total_cost = mminfo->pathcost;
2118  plan->plan_rows = 1;
2119  plan->plan_width = mminfo->path->pathtarget->width;
2120  plan->parallel_aware = false;
2121  plan->parallel_safe = mminfo->path->parallel_safe;
2122 
2123  /* Convert the plan into an InitPlan in the outer query. */
2124  SS_make_initplan_from_plan(root, subroot, plan, mminfo->param);
2125  }
2126 
2127  /* Generate the output plan --- basically just a Result */
2128  tlist = build_path_tlist(root, &best_path->path);
2129 
2130  plan = make_result(tlist, (Node *) best_path->quals, NULL);
2131 
2132  copy_generic_path_info(&plan->plan, (Path *) best_path);
2133 
2134  /*
2135  * During setrefs.c, we'll need to replace references to the Agg nodes
2136  * with InitPlan output params. (We can't just do that locally in the
2137  * MinMaxAgg node, because path nodes above here may have Agg references
2138  * as well.) Save the mmaggregates list to tell setrefs.c to do that.
2139  *
2140  * This doesn't work if we're in an inheritance subtree (see notes in
2141  * create_modifytable_plan). Fortunately we can't be because there would
2142  * never be aggregates in an UPDATE/DELETE; but let's Assert that.
2143  */
2144  Assert(!root->hasInheritedTarget);
2145  Assert(root->minmax_aggs == NIL);
2146  root->minmax_aggs = best_path->mmaggregates;
2147 
2148  return plan;
2149 }
Node * limitOffset
Definition: parsenodes.h:160
#define NIL
Definition: pg_list.h:69
double plan_rows
Definition: plannodes.h:133
PathTarget * pathtarget
Definition: relation.h:1067
Query * parse
Definition: relation.h:158
Param * param
Definition: relation.h:2203
Definition: nodes.h:517
List * minmax_aggs
Definition: relation.h:289
List * quals
Definition: relation.h:1629
Plan * create_plan(PlannerInfo *root, Path *best_path)
Definition: createplan.c:315
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
Path * path
Definition: relation.h:2201
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:752
Cost startup_cost
Definition: relation.h:1077
Node * limitCount
Definition: parsenodes.h:161
Cost startup_cost
Definition: plannodes.h:127
bool parallel_aware
Definition: plannodes.h:139
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6516
List * mmaggregates
Definition: relation.h:1628
int plan_width
Definition: plannodes.h:134
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
bool parallel_safe
Definition: relation.h:1072
bool hasInheritedTarget
Definition: relation.h:301
void SS_make_initplan_from_plan(PlannerInfo *root, PlannerInfo *subroot, Plan *plan, Param *prm)
Definition: subselect.c:2997
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
Definition: createplan.c:6495
int width
Definition: relation.h:999
Cost total_cost
Definition: plannodes.h:128
bool parallel_safe
Definition: plannodes.h:140
Definition: pg_list.h:45
PlannerInfo * subroot
Definition: relation.h:2200

◆ create_modifytable_plan()

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

Definition at line 2444 of file createplan.c.

References apply_tlist_labeling(), ModifyTablePath::canSetTag, copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), ModifyTablePath::epqParam, forboth, lappend(), lfirst, make_modifytable(), NIL, ModifyTablePath::nominalRelation, ModifyTablePath::onconflict, ModifyTablePath::operation, ModifyTablePath::partColsUpdated, ModifyTablePath::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().

2445 {
2446  ModifyTable *plan;
2447  List *subplans = NIL;
2448  ListCell *subpaths,
2449  *subroots;
2450 
2451  /* Build the plan for each input path */
2452  forboth(subpaths, best_path->subpaths,
2453  subroots, best_path->subroots)
2454  {
2455  Path *subpath = (Path *) lfirst(subpaths);
2456  PlannerInfo *subroot = (PlannerInfo *) lfirst(subroots);
2457  Plan *subplan;
2458 
2459  /*
2460  * In an inherited UPDATE/DELETE, reference the per-child modified
2461  * subroot while creating Plans from Paths for the child rel. This is
2462  * a kluge, but otherwise it's too hard to ensure that Plan creation
2463  * functions (particularly in FDWs) don't depend on the contents of
2464  * "root" matching what they saw at Path creation time. The main
2465  * downside is that creation functions for Plans that might appear
2466  * below a ModifyTable cannot expect to modify the contents of "root"
2467  * and have it "stick" for subsequent processing such as setrefs.c.
2468  * That's not great, but it seems better than the alternative.
2469  */
2470  subplan = create_plan_recurse(subroot, subpath, CP_EXACT_TLIST);
2471 
2472  /* Transfer resname/resjunk labeling, too, to keep executor happy */
2473  apply_tlist_labeling(subplan->targetlist, subroot->processed_tlist);
2474 
2475  subplans = lappend(subplans, subplan);
2476  }
2477 
2478  plan = make_modifytable(root,
2479  best_path->operation,
2480  best_path->canSetTag,
2481  best_path->nominalRelation,
2482  best_path->partitioned_rels,
2483  best_path->partColsUpdated,
2484  best_path->resultRelations,
2485  subplans,
2486  best_path->withCheckOptionLists,
2487  best_path->returningLists,
2488  best_path->rowMarks,
2489  best_path->onconflict,
2490  best_path->epqParam);
2491 
2492  copy_generic_path_info(&plan->plan, &best_path->path);
2493 
2494  return plan;
2495 }
#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:1706
OnConflictExpr * onconflict
Definition: relation.h:1708
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
Index nominalRelation
Definition: relation.h:1698
List * rowMarks
Definition: relation.h:1707
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:366
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
bool partColsUpdated
Definition: relation.h:1701
List * subroots
Definition: relation.h:1704
List * subpaths
Definition: relation.h:1703
List * lappend(List *list, void *datum)
Definition: list.c:128
List * partitioned_rels
Definition: relation.h:1700
#define lfirst(lc)
Definition: pg_list.h:106
static ModifyTable * make_modifytable(PlannerInfo *root, CmdType operation, bool canSetTag, Index nominalRelation, List *partitioned_rels, bool partColsUpdated, List *resultRelations, List *subplans, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, int epqParam)
Definition: createplan.c:6556
List * targetlist
Definition: plannodes.h:146
List * withCheckOptionLists
Definition: relation.h:1705
CmdType operation
Definition: relation.h:1696
List * resultRelations
Definition: relation.h:1702
List * processed_tlist
Definition: relation.h:285
#define CP_EXACT_TLIST
Definition: createplan.c:70
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 3454 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().

3456 {
3457  NamedTuplestoreScan *scan_plan;
3458  Index scan_relid = best_path->parent->relid;
3459  RangeTblEntry *rte;
3460 
3461  Assert(scan_relid > 0);
3462  rte = planner_rt_fetch(scan_relid, root);
3464 
3465  /* Sort clauses into best execution order */
3466  scan_clauses = order_qual_clauses(root, scan_clauses);
3467 
3468  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3469  scan_clauses = extract_actual_clauses(scan_clauses, false);
3470 
3471  /* Replace any outer-relation variables with nestloop params */
3472  if (best_path->param_info)
3473  {
3474  scan_clauses = (List *)
3475  replace_nestloop_params(root, (Node *) scan_clauses);
3476  }
3477 
3478  scan_plan = make_namedtuplestorescan(tlist, scan_clauses, scan_relid,
3479  rte->enrname);
3480 
3481  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3482 
3483  return scan_plan;
3484 }
Plan plan
Definition: plannodes.h:342
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4343
ParamPathInfo * param_info
Definition: relation.h:1069
Definition: nodes.h:517
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4901
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
#define planner_rt_fetch(rti, root)
Definition: relation.h:332
RelOptInfo * parent
Definition: relation.h:1066
char * enrname
Definition: parsenodes.h:1059
Index relid
Definition: relation.h:628
static NamedTuplestoreScan * make_namedtuplestorescan(List *qptlist, List *qpqual, Index scanrelid, char *enrname)
Definition: createplan.c:5343
unsigned int Index
Definition: c.h:442
#define Assert(condition)
Definition: c.h:699
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:356
RTEKind rtekind
Definition: parsenodes.h:962
Definition: pg_list.h:45

◆ create_nestloop_plan()

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

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

3769 {
3770  NestLoop *join_plan;
3771  Plan *outer_plan;
3772  Plan *inner_plan;
3773  List *tlist = build_path_tlist(root, &best_path->path);
3774  List *joinrestrictclauses = best_path->joinrestrictinfo;
3775  List *joinclauses;
3776  List *otherclauses;
3777  Relids outerrelids;
3778  List *nestParams;
3779  Relids saveOuterRels = root->curOuterRels;
3780  ListCell *cell;
3781  ListCell *prev;
3782  ListCell *next;
3783 
3784  /* NestLoop can project, so no need to be picky about child tlists */
3785  outer_plan = create_plan_recurse(root, best_path->outerjoinpath, 0);
3786 
3787  /* For a nestloop, include outer relids in curOuterRels for inner side */
3788  root->curOuterRels = bms_union(root->curOuterRels,
3789  best_path->outerjoinpath->parent->relids);
3790 
3791  inner_plan = create_plan_recurse(root, best_path->innerjoinpath, 0);
3792 
3793  /* Restore curOuterRels */
3794  bms_free(root->curOuterRels);
3795  root->curOuterRels = saveOuterRels;
3796 
3797  /* Sort join qual clauses into best execution order */
3798  joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
3799 
3800  /* Get the join qual clauses (in plain expression form) */
3801  /* Any pseudoconstant clauses are ignored here */
3802  if (IS_OUTER_JOIN(best_path->jointype))
3803  {
3804  extract_actual_join_clauses(joinrestrictclauses,
3805  best_path->path.parent->relids,
3806  &joinclauses, &otherclauses);
3807  }
3808  else
3809  {
3810  /* We can treat all clauses alike for an inner join */
3811  joinclauses = extract_actual_clauses(joinrestrictclauses, false);
3812  otherclauses = NIL;
3813  }
3814 
3815  /* Replace any outer-relation variables with nestloop params */
3816  if (best_path->path.param_info)
3817  {
3818  joinclauses = (List *)
3819  replace_nestloop_params(root, (Node *) joinclauses);
3820  otherclauses = (List *)
3821  replace_nestloop_params(root, (Node *) otherclauses);
3822  }
3823 
3824  /*
3825  * Identify any nestloop parameters that should be supplied by this join
3826  * node, and move them from root->curOuterParams to the nestParams list.
3827  */
3828  outerrelids = best_path->outerjoinpath->parent->relids;
3829  nestParams = NIL;
3830  prev = NULL;
3831  for (cell = list_head(root->curOuterParams); cell; cell = next)
3832  {
3833  NestLoopParam *nlp = (NestLoopParam *) lfirst(cell);
3834 
3835  next = lnext(cell);
3836  if (IsA(nlp->paramval, Var) &&
3837  bms_is_member(nlp->paramval->varno, outerrelids))
3838  {
3840  cell, prev);
3841  nestParams = lappend(nestParams, nlp);
3842  }
3843  else if (IsA(nlp->paramval, PlaceHolderVar) &&
3844  bms_overlap(((PlaceHolderVar *) nlp->paramval)->phrels,
3845  outerrelids) &&
3847  (PlaceHolderVar *) nlp->paramval,
3848  false)->ph_eval_at,
3849  outerrelids))
3850  {
3852  cell, prev);
3853  nestParams = lappend(nestParams, nlp);
3854  }
3855  else
3856  prev = cell;
3857  }
3858 
3859  join_plan = make_nestloop(tlist,
3860  joinclauses,
3861  otherclauses,
3862  nestParams,
3863  outer_plan,
3864  inner_plan,
3865  best_path->jointype,
3866  best_path->inner_unique);
3867 
3868  copy_generic_path_info(&join_plan->join.plan, &best_path->path);
3869 
3870  return join_plan;
3871 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4343
Relids ph_eval_at
Definition: relation.h:2182
static int32 next
Definition: blutils.c:211
Relids curOuterRels
Definition: relation.h:316
void extract_actual_join_clauses(List *restrictinfo_list, Relids joinrelids, List **joinquals, List **otherquals)
Definition: restrictinfo.c:383
Path * innerjoinpath
Definition: relation.h:1415
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:730
ParamPathInfo * param_info
Definition: relation.h:1069
Definition: nodes.h:517
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4901
Definition: primnodes.h:164
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:366
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
Var * paramval
Definition: plannodes.h:707
Join join
Definition: plannodes.h:699
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:752
List * joinrestrictinfo
Definition: relation.h:1417
RelOptInfo * parent
Definition: relation.h:1066
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:374
static NestLoop * make_nestloop(List *tlist, List *joinclauses, List *otherclauses, List *nestParams, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
Definition: createplan.c:5517
List * curOuterParams
Definition: relation.h:317
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:600
#define lnext(lc)
Definition: pg_list.h:105
List * lappend(List *list, void *datum)
Definition: list.c:128
Index varno
Definition: primnodes.h:167
List * list_delete_cell(List *list, ListCell *cell, ListCell *prev)
Definition: list.c:528
Path * outerjoinpath
Definition: relation.h:1414
void bms_free(Bitmapset *a)
Definition: bitmapset.c:267
Path path
Definition: relation.h:1407
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:284
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:356
bool inner_unique
Definition: relation.h:1411
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:509
JoinType jointype
Definition: relation.h:1409
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:486
Plan plan
Definition: plannodes.h:680

◆ create_plan()

Plan* create_plan ( PlannerInfo root,
Path best_path 
)

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

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

◆ create_plan_recurse()

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

Definition at line 366 of file createplan.c.

References Assert, check_stack_depth(), create_agg_plan(), create_append_plan(), create_gather_merge_plan(), create_gather_plan(), create_group_plan(), create_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().

367 {
368  Plan *plan;
369 
370  /* Guard against stack overflow due to overly complex plans */
372 
373  switch (best_path->pathtype)
374  {
375  case T_SeqScan:
376  case T_SampleScan:
377  case T_IndexScan:
378  case T_IndexOnlyScan:
379  case T_BitmapHeapScan:
380  case T_TidScan:
381  case T_SubqueryScan:
382  case T_FunctionScan:
383  case T_TableFuncScan:
384  case T_ValuesScan:
385  case T_CteScan:
386  case T_WorkTableScan:
388  case T_ForeignScan:
389  case T_CustomScan:
390  plan = create_scan_plan(root, best_path, flags);
391  break;
392  case T_HashJoin:
393  case T_MergeJoin:
394  case T_NestLoop:
395  plan = create_join_plan(root,
396  (JoinPath *) best_path);
397  break;
398  case T_Append:
399  plan = create_append_plan(root,
400  (AppendPath *) best_path);
401  break;
402  case T_MergeAppend:
403  plan = create_merge_append_plan(root,
404  (MergeAppendPath *) best_path);
405  break;
406  case T_Result:
407  if (IsA(best_path, ProjectionPath))
408  {
409  plan = create_projection_plan(root,
410  (ProjectionPath *) best_path,
411  flags);
412  }
413  else if (IsA(best_path, MinMaxAggPath))
414  {
415  plan = (Plan *) create_minmaxagg_plan(root,
416  (MinMaxAggPath *) best_path);
417  }
418  else
419  {
420  Assert(IsA(best_path, ResultPath));
421  plan = (Plan *) create_result_plan(root,
422  (ResultPath *) best_path);
423  }
424  break;
425  case T_ProjectSet:
426  plan = (Plan *) create_project_set_plan(root,
427  (ProjectSetPath *) best_path);
428  break;
429  case T_Material:
430  plan = (Plan *) create_material_plan(root,
431  (MaterialPath *) best_path,
432  flags);
433  break;
434  case T_Unique:
435  if (IsA(best_path, UpperUniquePath))
436  {
437  plan = (Plan *) create_upper_unique_plan(root,
438  (UpperUniquePath *) best_path,
439  flags);
440  }
441  else
442  {
443  Assert(IsA(best_path, UniquePath));
444  plan = create_unique_plan(root,
445  (UniquePath *) best_path,
446  flags);
447  }
448  break;
449  case T_Gather:
450  plan = (Plan *) create_gather_plan(root,
451  (GatherPath *) best_path);
452  break;
453  case T_Sort:
454  plan = (Plan *) create_sort_plan(root,
455  (SortPath *) best_path,
456  flags);
457  break;
458  case T_Group:
459  plan = (Plan *) create_group_plan(root,
460  (GroupPath *) best_path);
461  break;
462  case T_Agg:
463  if (IsA(best_path, GroupingSetsPath))
464  plan = create_groupingsets_plan(root,
465  (GroupingSetsPath *) best_path);
466  else
467  {
468  Assert(IsA(best_path, AggPath));
469  plan = (Plan *) create_agg_plan(root,
470  (AggPath *) best_path);
471  }
472  break;
473  case T_WindowAgg:
474  plan = (Plan *) create_windowagg_plan(root,
475  (WindowAggPath *) best_path);
476  break;
477  case T_SetOp:
478  plan = (Plan *) create_setop_plan(root,
479  (SetOpPath *) best_path,
480  flags);
481  break;
482  case T_RecursiveUnion:
483  plan = (Plan *) create_recursiveunion_plan(root,
484  (RecursiveUnionPath *) best_path);
485  break;
486  case T_LockRows:
487  plan = (Plan *) create_lockrows_plan(root,
488  (LockRowsPath *) best_path,
489  flags);
490  break;
491  case T_ModifyTable:
492  plan = (Plan *) create_modifytable_plan(root,
493  (ModifyTablePath *) best_path);
494  break;
495  case T_Limit:
496  plan = (Plan *) create_limit_plan(root,
497  (LimitPath *) best_path,
498  flags);
499  break;
500  case T_GatherMerge:
501  plan = (Plan *) create_gather_merge_plan(root,
502  (GatherMergePath *) best_path);
503  break;
504  default:
505  elog(ERROR, "unrecognized node type: %d",
506  (int) best_path->pathtype);
507  plan = NULL; /* keep compiler quiet */
508  break;
509  }
510 
511  return plan;
512 }
static Plan * create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
Definition: createplan.c:1323
static Result * create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path)
Definition: createplan.c:2089
static Result * create_result_plan(PlannerInfo *root, ResultPath *best_path)
Definition: createplan.c:1244
Definition: nodes.h:78
static Group * create_group_plan(PlannerInfo *root, GroupPath *best_path)
Definition: createplan.c:1783
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
static Plan * create_projection_plan(PlannerInfo *root, ProjectionPath *best_path, int flags)
Definition: createplan.c:1621
Definition: nodes.h:80
static Sort * create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags)
Definition: createplan.c:1748
static Plan * create_scan_plan(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:519
static LockRows * create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path, int flags)
Definition: createplan.c:2421
static ProjectSet * create_project_set_plan(PlannerInfo *root, ProjectSetPath *best_path)
Definition: createplan.c:1269
static RecursiveUnion * create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path)
Definition: createplan.c:2385
static Agg * create_agg_plan(PlannerInfo *root, AggPath *best_path)
Definition: createplan.c:1848
static Unique * create_upper_unique_plan(PlannerInfo *root, UpperUniquePath *best_path, int flags)
Definition: createplan.c:1820
Definition: nodes.h:49
Definition: nodes.h:76
static ModifyTable * create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path)
Definition: createplan.c:2444
NodeTag pathtype
Definition: relation.h:1064
Definition: nodes.h:46
#define ERROR
Definition: elog.h:43
Definition: nodes.h:77
static Plan * create_join_plan(PlannerInfo *root, JoinPath *best_path)
Definition: createplan.c:975
void check_stack_depth(void)
Definition: postgres.c:3155
static GatherMerge * create_gather_merge_plan(PlannerInfo *root, GatherMergePath *best_path)
Definition: createplan.c:1562
static SetOp * create_setop_plan(PlannerInfo *root, SetOpPath *best_path, int flags)
Definition: createplan.c:2349
#define Assert(condition)
Definition: c.h:699
static Gather * create_gather_plan(PlannerInfo *root, GatherPath *best_path)
Definition: createplan.c:1526
static Plan * create_append_plan(PlannerInfo *root, AppendPath *best_path)
Definition: createplan.c:1035
static WindowAgg * create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
Definition: createplan.c:2158
static Material * create_material_plan(PlannerInfo *root, MaterialPath *best_path, int flags)
Definition: createplan.c:1295
Definition: nodes.h:84
Definition: nodes.h:81
static Plan * create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path)
Definition: createplan.c:1929
static Limit * create_limit_plan(PlannerInfo *root, LimitPath *best_path, int flags)
Definition: createplan.c:2504
#define elog
Definition: elog.h:219
static Plan * create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path)
Definition: createplan.c:1136
Definition: nodes.h:86

◆ create_project_set_plan()

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

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

1270 {
1271  ProjectSet *plan;
1272  Plan *subplan;
1273  List *tlist;
1274 
1275  /* Since we intend to project, we don't need to constrain child tlist */
1276  subplan = create_plan_recurse(root, best_path->subpath, 0);
1277 
1278  tlist = build_path_tlist(root, &best_path->path);
1279 
1280  plan = make_project_set(tlist, subplan);
1281 
1282  copy_generic_path_info(&plan->plan, (Path *) best_path);
1283 
1284  return plan;
1285 }
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:366
static ProjectSet * make_project_set(List *tlist, Plan *subplan)
Definition: createplan.c:6537
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:752
Path * subpath
Definition: relation.h:1523
Definition: pg_list.h:45
Plan plan
Definition: plannodes.h:204

◆ create_projection_plan()

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

Definition at line 1621 of file createplan.c.

References apply_pathtarget_labeling_to_tlist(), build_path_tlist(), copy_generic_path_info(), CP_IGNORE_TLIST, CP_LABEL_TLIST, 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, use_physical_tlist(), and PathTarget::width.

Referenced by create_plan_recurse().

1622 {
1623  Plan *plan;
1624  Plan *subplan;
1625  List *tlist;
1626  bool needs_result_node = false;
1627 
1628  /*
1629  * Convert our subpath to a Plan and determine whether we need a Result
1630  * node.
1631  *
1632  * In most cases where we don't need to project, creation_projection_path
1633  * will have set dummypp, but not always. First, some createplan.c
1634  * routines change the tlists of their nodes. (An example is that
1635  * create_merge_append_plan might add resjunk sort columns to a
1636  * MergeAppend.) Second, create_projection_path has no way of knowing
1637  * what path node will be placed on top of the projection path and
1638  * therefore can't predict whether it will require an exact tlist. For
1639  * both of these reasons, we have to recheck here.
1640  */
1641  if (use_physical_tlist(root, &best_path->path, flags))
1642  {
1643  /*
1644  * Our caller doesn't really care what tlist we return, so we don't
1645  * actually need to project. However, we may still need to ensure
1646  * proper sortgroupref labels, if the caller cares about those.
1647  */
1648  subplan = create_plan_recurse(root, best_path->subpath, 0);
1649  tlist = subplan->targetlist;
1650  if ((flags & CP_LABEL_TLIST) != 0)
1652  best_path->path.pathtarget);
1653  }
1654  else if (is_projection_capable_path(best_path->subpath))
1655  {
1656  /*
1657  * Our caller requires that we return the exact tlist, but no separate
1658  * result node is needed because the subpath is projection-capable.
1659  * Tell create_plan_recurse that we're going to ignore the tlist it
1660  * produces.
1661  */
1662  subplan = create_plan_recurse(root, best_path->subpath,
1663  CP_IGNORE_TLIST);
1664  tlist = build_path_tlist(root, &best_path->path);
1665  }
1666  else
1667  {
1668  /*
1669  * It looks like we need a result node, unless by good fortune the
1670  * requested tlist is exactly the one the child wants to produce.
1671  */
1672  subplan = create_plan_recurse(root, best_path->subpath, 0);
1673  tlist = build_path_tlist(root, &best_path->path);
1674  needs_result_node = !tlist_same_exprs(tlist, subplan->targetlist);
1675  }
1676 
1677  /*
1678  * If we make a different decision about whether to include a Result node
1679  * than create_projection_path did, we'll have made slightly wrong cost
1680  * estimates; but label the plan with the cost estimates we actually used,
1681  * not "corrected" ones. (XXX this could be cleaned up if we moved more
1682  * of the sortcolumn setup logic into Path creation, but that would add
1683  * expense to creating Paths we might end up not using.)
1684  */
1685  if (!needs_result_node)
1686  {
1687  /* Don't need a separate Result, just assign tlist to subplan */
1688  plan = subplan;
1689  plan->targetlist = tlist;
1690 
1691  /* Label plan with the estimated costs we actually used */
1692  plan->startup_cost = best_path->path.startup_cost;
1693  plan->total_cost = best_path->path.total_cost;
1694  plan->plan_rows = best_path->path.rows;
1695  plan->plan_width = best_path->path.pathtarget->width;
1696  plan->parallel_safe = best_path->path.parallel_safe;
1697  /* ... but don't change subplan's parallel_aware flag */
1698  }
1699  else
1700  {
1701  /* We need a Result node */
1702  plan = (Plan *) make_result(tlist, NULL, subplan);
1703 
1704  copy_generic_path_info(plan, (Path *) best_path);
1705  }
1706 
1707  return plan;
1708 }
double plan_rows
Definition: plannodes.h:133
PathTarget * pathtarget
Definition: relation.h:1067
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:366
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:752
#define CP_IGNORE_TLIST
Definition: createplan.c:73
Cost startup_cost
Definition: relation.h:1077
Cost startup_cost
Definition: plannodes.h:127
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6516
void apply_pathtarget_labeling_to_tlist(List *tlist, PathTarget *target)
Definition: tlist.c:736
#define CP_LABEL_TLIST
Definition: createplan.c:72
Cost total_cost
Definition: relation.h:1078
int plan_width
Definition: plannodes.h:134
double rows
Definition: relation.h:1076
bool parallel_safe
Definition: relation.h:1072
bool tlist_same_exprs(List *tlist1, List *tlist2)
Definition: tlist.c:221
List * targetlist
Definition: plannodes.h:146
static bool use_physical_tlist(PlannerInfo *root, Path *path, int flags)
Definition: createplan.c:792
int width
Definition: relation.h:999
bool is_projection_capable_path(Path *path)
Definition: createplan.c:6699
Cost total_cost
Definition: plannodes.h:128
bool parallel_safe
Definition: plannodes.h:140
Path * subpath
Definition: relation.h:1511
Definition: pg_list.h:45

◆ create_recursiveunion_plan()

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

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

2386 {
2387  RecursiveUnion *plan;
2388  Plan *leftplan;
2389  Plan *rightplan;
2390  List *tlist;
2391  long numGroups;
2392 
2393  /* Need both children to produce same tlist, so force it */
2394  leftplan = create_plan_recurse(root, best_path->leftpath, CP_EXACT_TLIST);
2395  rightplan = create_plan_recurse(root, best_path->rightpath, CP_EXACT_TLIST);
2396 
2397  tlist = build_path_tlist(root, &best_path->path);
2398 
2399  /* Convert numGroups to long int --- but 'ware overflow! */
2400  numGroups = (long) Min(best_path->numGroups, (double) LONG_MAX);
2401 
2402  plan = make_recursive_union(tlist,
2403  leftplan,
2404  rightplan,
2405  best_path->wtParam,
2406  best_path->distinctList,
2407  numGroups);
2408 
2409  copy_generic_path_info(&plan->plan, (Path *) best_path);
2410 
2411  return plan;
2412 }
static RecursiveUnion * make_recursive_union(List *tlist, Plan *lefttree, Plan *righttree, int wtParam, List *distinctList, long numGroups)
Definition: createplan.c:5435
#define Min(x, y)
Definition: c.h:857
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:366
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:752
#define CP_EXACT_TLIST
Definition: createplan.c:70
Definition: pg_list.h:45

◆ create_result_plan()

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

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

1245 {
1246  Result *plan;
1247  List *tlist;
1248  List *quals;
1249 
1250  tlist = build_path_tlist(root, &best_path->path);
1251 
1252  /* best_path->quals is just bare clauses */
1253  quals = order_qual_clauses(root, best_path->quals);
1254 
1255  plan = make_result(tlist, (Node *) quals, NULL);
1256 
1257  copy_generic_path_info(&plan->plan, (Path *) best_path);
1258 
1259  return plan;
1260 }
Plan plan
Definition: plannodes.h:192
Definition: nodes.h:517
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4901
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
Path path
Definition: relation.h:1331
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:752
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6516
List * quals
Definition: relation.h:1332
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 2573 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().

2575 {
2576  SampleScan *scan_plan;
2577  Index scan_relid = best_path->parent->relid;
2578  RangeTblEntry *rte;
2579  TableSampleClause *tsc;
2580 
2581  /* it should be a base rel with a tablesample clause... */
2582  Assert(scan_relid > 0);
2583  rte = planner_rt_fetch(scan_relid, root);
2584  Assert(rte->rtekind == RTE_RELATION);
2585  tsc = rte->tablesample;
2586  Assert(tsc != NULL);
2587 
2588  /* Sort clauses into best execution order */
2589  scan_clauses = order_qual_clauses(root, scan_clauses);
2590 
2591  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2592  scan_clauses = extract_actual_clauses(scan_clauses, false);
2593 
2594  /* Replace any outer-relation variables with nestloop params */
2595  if (best_path->param_info)
2596  {
2597  scan_clauses = (List *)
2598  replace_nestloop_params(root, (Node *) scan_clauses);
2599  tsc = (TableSampleClause *)
2600  replace_nestloop_params(root, (Node *) tsc);
2601  }
2602 
2603  scan_plan = make_samplescan(tlist,
2604  scan_clauses,
2605  scan_relid,
2606  tsc);
2607 
2608  copy_generic_path_info(&scan_plan->scan.plan, best_path);
2609 
2610  return scan_plan;
2611 }
Plan plan
Definition: plannodes.h:342
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4343
ParamPathInfo * param_info
Definition: relation.h:1069
Definition: nodes.h:517
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4901
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4995
#define planner_rt_fetch(rti, root)
Definition: relation.h:332
Scan scan
Definition: plannodes.h:358
RelOptInfo * parent
Definition: relation.h:1066
Index relid
Definition: relation.h:628
static SampleScan * make_samplescan(List *qptlist, List *qpqual, Index scanrelid, TableSampleClause *tsc)
Definition: createplan.c:5107
unsigned int Index
Definition: c.h:442
#define Assert(condition)
Definition: c.h:699