PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
createplan.c File Reference
#include "postgres.h"
#include <limits.h>
#include <math.h>
#include "access/stratnum.h"
#include "access/sysattr.h"
#include "catalog/pg_class.h"
#include "foreign/fdwapi.h"
#include "miscadmin.h"
#include "nodes/extensible.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/paths.h"
#include "optimizer/placeholder.h"
#include "optimizer/plancat.h"
#include "optimizer/planmain.h"
#include "optimizer/planner.h"
#include "optimizer/predtest.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/subselect.h"
#include "optimizer/tlist.h"
#include "optimizer/var.h"
#include "parser/parse_clause.h"
#include "parser/parsetree.h"
#include "utils/lsyscache.h"
Include dependency graph for createplan.c:

Go to the source code of this file.

Macros

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

Functions

static Plancreate_plan_recurse (PlannerInfo *root, Path *best_path, int flags)
 
static Plancreate_scan_plan (PlannerInfo *root, Path *best_path, int flags)
 
static Listbuild_path_tlist (PlannerInfo *root, Path *path)
 
static bool use_physical_tlist (PlannerInfo *root, Path *path, int flags)
 
static Listget_gating_quals (PlannerInfo *root, List *quals)
 
static Plancreate_gating_plan (PlannerInfo *root, Path *path, Plan *plan, List *gating_quals)
 
static Plancreate_join_plan (PlannerInfo *root, JoinPath *best_path)
 
static Plancreate_append_plan (PlannerInfo *root, AppendPath *best_path)
 
static Plancreate_merge_append_plan (PlannerInfo *root, MergeAppendPath *best_path)
 
static Resultcreate_result_plan (PlannerInfo *root, ResultPath *best_path)
 
static ProjectSetcreate_project_set_plan (PlannerInfo *root, ProjectSetPath *best_path)
 
static Materialcreate_material_plan (PlannerInfo *root, MaterialPath *best_path, int flags)
 
static Plancreate_unique_plan (PlannerInfo *root, UniquePath *best_path, int flags)
 
static Gathercreate_gather_plan (PlannerInfo *root, GatherPath *best_path)
 
static Plancreate_projection_plan (PlannerInfo *root, ProjectionPath *best_path)
 
static Planinject_projection_plan (Plan *subplan, List *tlist, bool parallel_safe)
 
static Sortcreate_sort_plan (PlannerInfo *root, SortPath *best_path, int flags)
 
static Groupcreate_group_plan (PlannerInfo *root, GroupPath *best_path)
 
static Uniquecreate_upper_unique_plan (PlannerInfo *root, UpperUniquePath *best_path, int flags)
 
static Aggcreate_agg_plan (PlannerInfo *root, AggPath *best_path)
 
static Plancreate_groupingsets_plan (PlannerInfo *root, GroupingSetsPath *best_path)
 
static Resultcreate_minmaxagg_plan (PlannerInfo *root, MinMaxAggPath *best_path)
 
static WindowAggcreate_windowagg_plan (PlannerInfo *root, WindowAggPath *best_path)
 
static SetOpcreate_setop_plan (PlannerInfo *root, SetOpPath *best_path, int flags)
 
static RecursiveUnioncreate_recursiveunion_plan (PlannerInfo *root, RecursiveUnionPath *best_path)
 
static void get_column_info_for_window (PlannerInfo *root, WindowClause *wc, List *tlist, int numSortCols, AttrNumber *sortColIdx, int *partNumCols, AttrNumber **partColIdx, Oid **partOperators, int *ordNumCols, AttrNumber **ordColIdx, Oid **ordOperators)
 
static LockRowscreate_lockrows_plan (PlannerInfo *root, LockRowsPath *best_path, int flags)
 
static ModifyTablecreate_modifytable_plan (PlannerInfo *root, ModifyTablePath *best_path)
 
static Limitcreate_limit_plan (PlannerInfo *root, LimitPath *best_path, int flags)
 
static SeqScancreate_seqscan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static SampleScancreate_samplescan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static Scancreate_indexscan_plan (PlannerInfo *root, IndexPath *best_path, List *tlist, List *scan_clauses, bool indexonly)
 
static BitmapHeapScancreate_bitmap_scan_plan (PlannerInfo *root, BitmapHeapPath *best_path, List *tlist, List *scan_clauses)
 
static Plancreate_bitmap_subplan (PlannerInfo *root, Path *bitmapqual, List **qual, List **indexqual, List **indexECs)
 
static void bitmap_subplan_mark_shared (Plan *plan)
 
static TidScancreate_tidscan_plan (PlannerInfo *root, TidPath *best_path, List *tlist, List *scan_clauses)
 
static SubqueryScancreate_subqueryscan_plan (PlannerInfo *root, SubqueryScanPath *best_path, List *tlist, List *scan_clauses)
 
static FunctionScancreate_functionscan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static ValuesScancreate_valuesscan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static TableFuncScancreate_tablefuncscan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static CteScancreate_ctescan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static NamedTuplestoreScancreate_namedtuplestorescan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static WorkTableScancreate_worktablescan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static ForeignScancreate_foreignscan_plan (PlannerInfo *root, ForeignPath *best_path, List *tlist, List *scan_clauses)
 
static CustomScancreate_customscan_plan (PlannerInfo *root, CustomPath *best_path, List *tlist, List *scan_clauses)
 
static NestLoopcreate_nestloop_plan (PlannerInfo *root, NestPath *best_path)
 
static MergeJoincreate_mergejoin_plan (PlannerInfo *root, MergePath *best_path)
 
static HashJoincreate_hashjoin_plan (PlannerInfo *root, HashPath *best_path)
 
static Nodereplace_nestloop_params (PlannerInfo *root, Node *expr)
 
static Nodereplace_nestloop_params_mutator (Node *node, PlannerInfo *root)
 
static void process_subquery_nestloop_params (PlannerInfo *root, List *subplan_params)
 
static Listfix_indexqual_references (PlannerInfo *root, IndexPath *index_path)
 
static Listfix_indexorderby_references (PlannerInfo *root, IndexPath *index_path)
 
static Nodefix_indexqual_operand (Node *node, IndexOptInfo *index, int indexcol)
 
static Listget_switched_clauses (List *clauses, Relids outerrelids)
 
static Listorder_qual_clauses (PlannerInfo *root, List *clauses)
 
static void copy_generic_path_info (Plan *dest, Path *src)
 
static void copy_plan_costsize (Plan *dest, Plan *src)
 
static void label_sort_with_costsize (PlannerInfo *root, Sort *plan, double limit_tuples)
 
static SeqScanmake_seqscan (List *qptlist, List *qpqual, Index scanrelid)
 
static SampleScanmake_samplescan (List *qptlist, List *qpqual, Index scanrelid, TableSampleClause *tsc)
 
static IndexScanmake_indexscan (List *qptlist, List *qpqual, Index scanrelid, Oid indexid, List *indexqual, List *indexqualorig, List *indexorderby, List *indexorderbyorig, List *indexorderbyops, ScanDirection indexscandir)
 
static IndexOnlyScanmake_indexonlyscan (List *qptlist, List *qpqual, Index scanrelid, Oid indexid, List *indexqual, List *indexorderby, List *indextlist, ScanDirection indexscandir)
 
static BitmapIndexScanmake_bitmap_indexscan (Index scanrelid, Oid indexid, List *indexqual, List *indexqualorig)
 
static BitmapHeapScanmake_bitmap_heapscan (List *qptlist, List *qpqual, Plan *lefttree, List *bitmapqualorig, Index scanrelid)
 
static TidScanmake_tidscan (List *qptlist, List *qpqual, Index scanrelid, List *tidquals)
 
static SubqueryScanmake_subqueryscan (List *qptlist, List *qpqual, Index scanrelid, Plan *subplan)
 
static FunctionScanmake_functionscan (List *qptlist, List *qpqual, Index scanrelid, List *functions, bool funcordinality)
 
static ValuesScanmake_valuesscan (List *qptlist, List *qpqual, Index scanrelid, List *values_lists)
 
static TableFuncScanmake_tablefuncscan (List *qptlist, List *qpqual, Index scanrelid, TableFunc *tablefunc)
 
static CteScanmake_ctescan (List *qptlist, List *qpqual, Index scanrelid, int ctePlanId, int cteParam)
 
static NamedTuplestoreScanmake_namedtuplestorescan (List *qptlist, List *qpqual, Index scanrelid, char *enrname)
 
static WorkTableScanmake_worktablescan (List *qptlist, List *qpqual, Index scanrelid, int wtParam)
 
static Appendmake_append (List *appendplans, List *tlist, List *partitioned_rels)
 
static RecursiveUnionmake_recursive_union (List *tlist, Plan *lefttree, Plan *righttree, int wtParam, List *distinctList, long numGroups)
 
static BitmapAndmake_bitmap_and (List *bitmapplans)
 
static BitmapOrmake_bitmap_or (List *bitmapplans)
 
static NestLoopmake_nestloop (List *tlist, List *joinclauses, List *otherclauses, List *nestParams, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
 
static HashJoinmake_hashjoin (List *tlist, List *joinclauses, List *otherclauses, List *hashclauses, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
 
static Hashmake_hash (Plan *lefttree, Oid skewTable, AttrNumber skewColumn, bool skewInherit)
 
static MergeJoinmake_mergejoin (List *tlist, List *joinclauses, List *otherclauses, List *mergeclauses, Oid *mergefamilies, Oid *mergecollations, int *mergestrategies, bool *mergenullsfirst, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique, bool skip_mark_restore)
 
static Sortmake_sort (Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
 
static Planprepare_sort_from_pathkeys (Plan *lefttree, List *pathkeys, Relids relids, const AttrNumber *reqColIdx, bool adjust_tlist_in_place, int *p_numsortkeys, AttrNumber **p_sortColIdx, Oid **p_sortOperators, Oid **p_collations, bool **p_nullsFirst)
 
static EquivalenceMemberfind_ec_member_for_tle (EquivalenceClass *ec, TargetEntry *tle, Relids relids)
 
static Sortmake_sort_from_pathkeys (Plan *lefttree, List *pathkeys)
 
static Sortmake_sort_from_groupcols (List *groupcls, AttrNumber *grpColIdx, Plan *lefttree)
 
static Materialmake_material (Plan *lefttree)
 
static WindowAggmake_windowagg (List *tlist, Index winref, int partNumCols, AttrNumber *partColIdx, Oid *partOperators, int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators, int frameOptions, Node *startOffset, Node *endOffset, Plan *lefttree)
 
static Groupmake_group (List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Plan *lefttree)
 
static Uniquemake_unique_from_sortclauses (Plan *lefttree, List *distinctList)
 
static Uniquemake_unique_from_pathkeys (Plan *lefttree, List *pathkeys, int numCols)
 
static Gathermake_gather (List *qptlist, List *qpqual, int nworkers, bool single_copy, Plan *subplan)
 
static SetOpmake_setop (SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree, List *distinctList, AttrNumber flagColIdx, int firstFlag, long numGroups)
 
static LockRowsmake_lockrows (Plan *lefttree, List *rowMarks, int epqParam)
 
static Resultmake_result (List *tlist, Node *resconstantqual, Plan *subplan)
 
static ProjectSetmake_project_set (List *tlist, Plan *subplan)
 
static ModifyTablemake_modifytable (PlannerInfo *root, CmdType operation, bool canSetTag, Index nominalRelation, List *partitioned_rels, List *resultRelations, List *subplans, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, int epqParam)
 
static GatherMergecreate_gather_merge_plan (PlannerInfo *root, GatherMergePath *best_path)
 
Plancreate_plan (PlannerInfo *root, Path *best_path)
 
static AttrNumberremap_groupColIdx (PlannerInfo *root, List *groupClause)
 
ForeignScanmake_foreignscan (List *qptlist, List *qpqual, Index scanrelid, List *fdw_exprs, List *fdw_private, List *fdw_scan_tlist, List *fdw_recheck_quals, Plan *outer_plan)
 
Sortmake_sort_from_sortclauses (List *sortcls, Plan *lefttree)
 
Planmaterialize_finished_plan (Plan *subplan)
 
Aggmake_agg (List *tlist, List *qual, AggStrategy aggstrategy, AggSplit aggsplit, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, List *groupingSets, List *chain, double dNumGroups, Plan *lefttree)
 
Limitmake_limit (Plan *lefttree, Node *limitOffset, Node *limitCount)
 
bool is_projection_capable_path (Path *path)
 
bool is_projection_capable_plan (Plan *plan)
 

Macro Definition Documentation

#define CP_LABEL_TLIST   0x0004 /* tlist must contain sortgrouprefs */
#define CP_SMALL_TLIST   0x0002 /* Prefer narrower tlists */

Function Documentation

static void bitmap_subplan_mark_shared ( Plan plan)
static

Definition at line 4918 of file createplan.c.

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

Referenced by create_bitmap_scan_plan().

4919 {
4920  if (IsA(plan, BitmapAnd))
4922  linitial(((BitmapAnd *) plan)->bitmapplans));
4923  else if (IsA(plan, BitmapOr))
4924  ((BitmapOr *) plan)->isshared = true;
4925  else if (IsA(plan, BitmapIndexScan))
4926  ((BitmapIndexScan *) plan)->isshared = true;
4927  else
4928  elog(ERROR, "unrecognized node type: %d", nodeTag(plan));
4929 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
#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:4918
bool isshared
Definition: dynahash.c:203
#define nodeTag(nodeptr)
Definition: nodes.h:514
#define elog
Definition: elog.h:219
static List * build_path_tlist ( PlannerInfo root,
Path path 
)
static

Definition at line 728 of file createplan.c.

References PathTarget::exprs, lappend(), lfirst, makeTargetEntry(), NIL, NULL, 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().

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

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

4856 {
4857  dest->startup_cost = src->startup_cost;
4858  dest->total_cost = src->total_cost;
4859  dest->plan_rows = src->rows;
4860  dest->plan_width = src->pathtarget->width;
4861  dest->parallel_aware = src->parallel_aware;
4862  dest->parallel_safe = src->parallel_safe;
4863 }
double plan_rows
Definition: plannodes.h:131
PathTarget * pathtarget
Definition: relation.h:955
Cost startup_cost
Definition: relation.h:965
Cost startup_cost
Definition: plannodes.h:125
bool parallel_aware
Definition: plannodes.h:137
Cost total_cost
Definition: relation.h:966
int plan_width
Definition: plannodes.h:132
double rows
Definition: relation.h:964
bool parallel_safe
Definition: relation.h:960
int width
Definition: relation.h:887
Cost total_cost
Definition: plannodes.h:126
bool parallel_aware
Definition: relation.h:959
bool parallel_safe
Definition: plannodes.h:138
static void copy_plan_costsize ( Plan dest,
Plan src 
)
static

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

4871 {
4872  dest->startup_cost = src->startup_cost;
4873  dest->total_cost = src->total_cost;
4874  dest->plan_rows = src->plan_rows;
4875  dest->plan_width = src->plan_width;
4876  /* Assume the inserted node is not parallel-aware. */
4877  dest->parallel_aware = false;
4878  /* Assume the inserted node is parallel-safe, if child plan is. */
4879  dest->parallel_safe = src->parallel_safe;
4880 }
double plan_rows
Definition: plannodes.h:131
Cost startup_cost
Definition: plannodes.h:125
bool parallel_aware
Definition: plannodes.h:137
int plan_width
Definition: plannodes.h:132
Cost total_cost
Definition: plannodes.h:126
bool parallel_safe
Definition: plannodes.h:138
static Agg * create_agg_plan ( PlannerInfo root,
AggPath best_path 
)
static

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

1731 {
1732  Agg *plan;
1733  Plan *subplan;
1734  List *tlist;
1735  List *quals;
1736 
1737  /*
1738  * Agg can project, so no need to be terribly picky about child tlist, but
1739  * we do need grouping columns to be available
1740  */
1741  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1742 
1743  tlist = build_path_tlist(root, &best_path->path);
1744 
1745  quals = order_qual_clauses(root, best_path->qual);
1746 
1747  plan = make_agg(tlist, quals,
1748  best_path->aggstrategy,
1749  best_path->aggsplit,
1750  list_length(best_path->groupClause),
1752  subplan->targetlist),
1753  extract_grouping_ops(best_path->groupClause),
1754  NIL,
1755  NIL,
1756  best_path->numGroups,
1757  subplan);
1758 
1759  copy_generic_path_info(&plan->plan, (Path *) best_path);
1760 
1761  return plan;
1762 }
#define NIL
Definition: pg_list.h:69
AggStrategy aggstrategy
Definition: relation.h:1461
List * qual
Definition: relation.h:1465
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4761
AggSplit aggsplit
Definition: relation.h:1462
Oid * extract_grouping_ops(List *groupClause)
Definition: tlist.c:466
double numGroups
Definition: relation.h:1463
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:6000
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:354
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4855
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:728
List * groupClause
Definition: relation.h:1464
Plan plan
Definition: plannodes.h:782
#define CP_LABEL_TLIST
Definition: createplan.c:68
static int list_length(const List *l)
Definition: pg_list.h:89
Path * subpath
Definition: relation.h:1460
List * targetlist
Definition: plannodes.h:144
Definition: plannodes.h:780
Definition: pg_list.h:45
Path path
Definition: relation.h:1459
Definition: relation.h:948
static Plan * create_append_plan ( PlannerInfo root,
AppendPath best_path 
)
static

Definition at line 1002 of file createplan.c.

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

Referenced by create_plan_recurse().

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

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

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

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

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

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

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

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

3573 {
3574  CustomScan *cplan;
3575  RelOptInfo *rel = best_path->path.parent;
3576  List *custom_plans = NIL;
3577  ListCell *lc;
3578 
3579  /* Recursively transform child paths. */
3580  foreach(lc, best_path->custom_paths)
3581  {
3582  Plan *plan = create_plan_recurse(root, (Path *) lfirst(lc),
3583  CP_EXACT_TLIST);
3584 
3585  custom_plans = lappend(custom_plans, plan);
3586  }
3587 
3588  /*
3589  * Sort clauses into the best execution order, although custom-scan
3590  * provider can reorder them again.
3591  */
3592  scan_clauses = order_qual_clauses(root, scan_clauses);
3593 
3594  /*
3595  * Invoke custom plan provider to create the Plan node represented by the
3596  * CustomPath.
3597  */
3598  cplan = castNode(CustomScan,
3599  best_path->methods->PlanCustomPath(root,
3600  rel,
3601  best_path,
3602  tlist,
3603  scan_clauses,
3604  custom_plans));
3605 
3606  /*
3607  * Copy cost data from Path to Plan; no need to make custom-plan providers
3608  * do this
3609  */
3610  copy_generic_path_info(&cplan->scan.plan, &best_path->path);
3611 
3612  /* Likewise, copy the relids that are represented by this custom scan */
3613  cplan->custom_relids = best_path->path.parent->relids;
3614 
3615  /*
3616  * Replace any outer-relation variables with nestloop params in the qual
3617  * and custom_exprs expressions. We do this last so that the custom-plan
3618  * provider doesn't have to be involved. (Note that parts of custom_exprs
3619  * could have come from join clauses, so doing this beforehand on the
3620  * scan_clauses wouldn't work.) We assume custom_scan_tlist contains no
3621  * such variables.
3622  */
3623  if (best_path->path.param_info)
3624  {
3625  cplan->scan.plan.qual = (List *)
3626  replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
3627  cplan->custom_exprs = (List *)
3628  replace_nestloop_params(root, (Node *) cplan->custom_exprs);
3629  }
3630 
3631  return cplan;
3632 }
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:145
Plan plan
Definition: plannodes.h:328
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4203
List * custom_paths
Definition: relation.h:1161
#define castNode(_type_, nodeptr)
Definition: nodes.h:578
struct Plan *(* PlanCustomPath)(PlannerInfo *root, RelOptInfo *rel, struct CustomPath *best_path, List *tlist, List *clauses, List *custom_plans)
Definition: extensible.h:93
ParamPathInfo * param_info
Definition: relation.h:957
Definition: nodes.h:509
List * custom_exprs
Definition: plannodes.h:629
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4761
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:354
const struct CustomPathMethods * methods
Definition: relation.h:1163
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4855
RelOptInfo * parent
Definition: relation.h:954
Relids relids
Definition: relation.h:525
Path path
Definition: relation.h:1158
List * lappend(List *list, void *datum)
Definition: list.c:128
Scan scan
Definition: plannodes.h:625
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * custom_relids
Definition: plannodes.h:632
#define CP_EXACT_TLIST
Definition: createplan.c:66
Definition: pg_list.h:45
Definition: relation.h:948
static ForeignScan * create_foreignscan_plan ( PlannerInfo root,
ForeignPath best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3427 of file createplan.c.

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

Referenced by create_scan_plan().

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

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

3107 {
3108  FunctionScan *scan_plan;
3109  Index scan_relid = best_path->parent->relid;
3110  RangeTblEntry *rte;
3111  List *functions;
3112 
3113  /* it should be a function base rel... */
3114  Assert(scan_relid > 0);
3115  rte = planner_rt_fetch(scan_relid, root);
3116  Assert(rte->rtekind == RTE_FUNCTION);
3117  functions = rte->functions;
3118 
3119  /* Sort clauses into best execution order */
3120  scan_clauses = order_qual_clauses(root, scan_clauses);
3121 
3122  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3123  scan_clauses = extract_actual_clauses(scan_clauses, false);
3124 
3125  /* Replace any outer-relation variables with nestloop params */
3126  if (best_path->param_info)
3127  {
3128  scan_clauses = (List *)
3129  replace_nestloop_params(root, (Node *) scan_clauses);
3130  /* The function expressions could contain nestloop params, too */
3131  functions = (List *) replace_nestloop_params(root, (Node *) functions);
3132  }
3133 
3134  scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
3135  functions, rte->funcordinality);
3136 
3137  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3138 
3139  return scan_plan;
3140 }
Plan plan
Definition: plannodes.h:328
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4203
ParamPathInfo * param_info
Definition: relation.h:957
Definition: nodes.h:509
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4761
bool funcordinality
Definition: parsenodes.h:999
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4855
#define planner_rt_fetch(rti, root)
Definition: relation.h:325
RelOptInfo * parent
Definition: relation.h:954
Index relid
Definition: relation.h:553
unsigned int Index
Definition: c.h:365
static FunctionScan * make_functionscan(List *qptlist, List *qpqual, Index scanrelid, List *functions, bool funcordinality)
Definition: createplan.c:5119
#define Assert(condition)
Definition: c.h:675
List * functions
Definition: parsenodes.h:998
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:354
static const struct fns functions
Definition: regcomp.c:299
RTEKind rtekind
Definition: parsenodes.h:944
Definition: pg_list.h:45
static GatherMerge * create_gather_merge_plan ( PlannerInfo root,
GatherMergePath best_path 
)
static

Definition at line 1492 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::sortColIdx, GatherMerge::sortOperators, GatherMergePath::subpath, and Plan::targetlist.

Referenced by create_plan_recurse().

1493 {
1494  GatherMerge *gm_plan;
1495  Plan *subplan;
1496  List *pathkeys = best_path->path.pathkeys;
1497  List *tlist = build_path_tlist(root, &best_path->path);
1498 
1499  /* As with Gather, it's best to project away columns in the workers. */
1500  subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1501 
1502  /* Create a shell for a GatherMerge plan. */
1503  gm_plan = makeNode(GatherMerge);
1504  gm_plan->plan.targetlist = tlist;
1505  gm_plan->num_workers = best_path->num_workers;
1506  copy_generic_path_info(&gm_plan->plan, &best_path->path);
1507 
1508  /* Gather Merge is pointless with no pathkeys; use Gather instead. */
1509  Assert(pathkeys != NIL);
1510 
1511  /* Compute sort column info, and adjust subplan's tlist as needed */
1512  subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1513  best_path->subpath->parent->relids,
1514  gm_plan->sortColIdx,
1515  false,
1516  &gm_plan->numCols,
1517  &gm_plan->sortColIdx,
1518  &gm_plan->sortOperators,
1519  &gm_plan->collations,
1520  &gm_plan->nullsFirst);
1521 
1522 
1523  /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1524  if (!pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys))
1525  subplan = (Plan *) make_sort(subplan, gm_plan->numCols,
1526  gm_plan->sortColIdx,
1527  gm_plan->sortOperators,
1528  gm_plan->collations,
1529  gm_plan->nullsFirst);
1530 
1531  /* Now insert the subplan under GatherMerge. */
1532  gm_plan->plan.lefttree = subplan;
1533 
1534  /* use parallel mode for parallel plans. */
1535  root->glob->parallelModeNeeded = true;
1536 
1537  return gm_plan;
1538 }
#define NIL
Definition: pg_list.h:69
Oid * collations
Definition: plannodes.h:850
AttrNumber * sortColIdx
Definition: plannodes.h:848
bool * nullsFirst
Definition: plannodes.h:851
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:354
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4855
bool parallelModeNeeded
Definition: relation.h:131
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:728
RelOptInfo * parent
Definition: relation.h:954
PlannerGlobal * glob
Definition: relation.h:157
Relids relids
Definition: relation.h:525
Oid * sortOperators
Definition: plannodes.h:849
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:317
List * pathkeys
Definition: relation.h:968
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
Definition: createplan.c:5482
#define makeNode(_type_)
Definition: nodes.h:557
#define Assert(condition)
Definition: c.h:675
struct Plan * lefttree
Definition: plannodes.h:146
List * targetlist
Definition: plannodes.h:144
Path * subpath
Definition: relation.h:1278
int num_workers
Definition: plannodes.h:845
#define CP_EXACT_TLIST
Definition: createplan.c:66
Definition: pg_list.h:45
static Plan * prepare_sort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids, const AttrNumber *reqColIdx, bool adjust_tlist_in_place, int *p_numsortkeys, AttrNumber **p_sortColIdx, Oid **p_sortOperators, Oid **p_collations, bool **p_nullsFirst)
Definition: createplan.c:5543
static Gather * create_gather_plan ( PlannerInfo root,
GatherPath best_path 
)
static

Definition at line 1457 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, and GatherPath::subpath.

Referenced by create_plan_recurse().

1458 {
1459  Gather *gather_plan;
1460  Plan *subplan;
1461  List *tlist;
1462 
1463  /*
1464  * Although the Gather node can project, we prefer to push down such work
1465  * to its child node, so demand an exact tlist from the child.
1466  */
1467  subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1468 
1469  tlist = build_path_tlist(root, &best_path->path);
1470 
1471  gather_plan = make_gather(tlist,
1472  NIL,
1473  best_path->num_workers,
1474  best_path->single_copy,
1475  subplan);
1476 
1477  copy_generic_path_info(&gather_plan->plan, &best_path->path);
1478 
1479  /* use parallel mode for parallel plans. */
1480  root->glob->parallelModeNeeded = true;
1481 
1482  return gather_plan;
1483 }
#define NIL
Definition: pg_list.h:69
bool single_copy
Definition: relation.h:1266
static Gather * make_gather(List *qptlist, List *qpqual, int nworkers, bool single_copy, Plan *subplan)
Definition: createplan.c:6237
int num_workers
Definition: relation.h:1267
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:354
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4855
bool parallelModeNeeded
Definition: relation.h:131
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:728
Plan plan
Definition: plannodes.h:832
PlannerGlobal * glob
Definition: relation.h:157
Path * subpath
Definition: relation.h:1265
Path path
Definition: relation.h:1264
#define CP_EXACT_TLIST
Definition: createplan.c:66
Definition: pg_list.h:45
static Plan * create_gating_plan ( PlannerInfo root,
Path path,
Plan plan,
List gating_quals 
)
static

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

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

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

1666 {
1667  Group *plan;
1668  Plan *subplan;
1669  List *tlist;
1670  List *quals;
1671 
1672  /*
1673  * Group can project, so no need to be terribly picky about child tlist,
1674  * but we do need grouping columns to be available
1675  */
1676  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1677 
1678  tlist = build_path_tlist(root, &best_path->path);
1679 
1680  quals = order_qual_clauses(root, best_path->qual);
1681 
1682  plan = make_group(tlist,
1683  quals,
1684  list_length(best_path->groupClause),
1686  subplan->targetlist),
1687  extract_grouping_ops(best_path->groupClause),
1688  subplan);
1689 
1690  copy_generic_path_info(&plan->plan, (Path *) best_path);
1691 
1692  return plan;
1693 }
List * qual
Definition: relation.h:1434
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4761
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:354
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4855
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:728
Path path
Definition: relation.h:1431
static Group * make_group(List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Plan *lefttree)
Definition: createplan.c:6062
List * groupClause
Definition: relation.h:1433
#define CP_LABEL_TLIST
Definition: createplan.c:68
Path * subpath
Definition: relation.h:1432
Plan plan
Definition: plannodes.h:760
static int list_length(const List *l)
Definition: pg_list.h:89
List * targetlist
Definition: plannodes.h:144
Definition: pg_list.h:45
Definition: relation.h:948
static Plan * create_groupingsets_plan ( PlannerInfo root,
GroupingSetsPath best_path 
)
static

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

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

Definition at line 4053 of file createplan.c.

References OpExpr::args, Assert, build_path_tlist(), copy_generic_path_info(), copy_plan_costsize(), CP_SMALL_TLIST, create_plan_recurse(), extract_actual_clauses(), extract_actual_join_clauses(), get_actual_clauses(), get_switched_clauses(), RangeTblEntry::inh, JoinPath::inner_unique, JoinPath::innerjoinpath, InvalidAttrNumber, InvalidOid, is_opclause, IS_OUTER_JOIN, IsA, HashJoin::join, JoinPath::joinrestrictinfo, JoinPath::jointype, HashPath::jpath, linitial, list_difference(), list_length(), make_hash(), make_hashjoin(), NIL, HashPath::num_batches, order_qual_clauses(), JoinPath::outerjoinpath, Path::param_info, Path::parent, JoinPath::path, HashPath::path_hashclauses, Join::plan, Hash::plan, RangeTblEntry::relid, RelOptInfo::relids, replace_nestloop_params(), RTE_RELATION, RangeTblEntry::rtekind, PlannerInfo::simple_rte_array, Plan::startup_cost, Plan::total_cost, RangeQueryClause::var, Var::varattno, and Var::varno.

Referenced by create_join_plan().

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

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

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

Definition at line 942 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, NULL, JoinPath::path, Path::pathtype, T_HashJoin, T_MergeJoin, and T_NestLoop.

Referenced by create_plan_recurse().

943 {
944  Plan *plan;
945  List *gating_clauses;
946 
947  switch (best_path->path.pathtype)
948  {
949  case T_MergeJoin:
950  plan = (Plan *) create_mergejoin_plan(root,
951  (MergePath *) best_path);
952  break;
953  case T_HashJoin:
954  plan = (Plan *) create_hashjoin_plan(root,
955  (HashPath *) best_path);
956  break;
957  case T_NestLoop:
958  plan = (Plan *) create_nestloop_plan(root,
959  (NestPath *) best_path);
960  break;
961  default:
962  elog(ERROR, "unrecognized node type: %d",
963  (int) best_path->path.pathtype);
964  plan = NULL; /* keep compiler quiet */
965  break;
966  }
967 
968  /*
969  * If there are any pseudoconstant clauses attached to this node, insert a
970  * gating Result node that evaluates the pseudoconstants as one-time
971  * quals.
972  */
973  gating_clauses = get_gating_quals(root, best_path->joinrestrictinfo);
974  if (gating_clauses)
975  plan = create_gating_plan(root, (Path *) best_path, plan,
976  gating_clauses);
977 
978 #ifdef NOT_USED
979 
980  /*
981  * * Expensive function pullups may have pulled local predicates * into
982  * this path node. Put them in the qpqual of the plan node. * JMH,
983  * 6/15/92
984  */
985  if (get_loc_restrictinfo(best_path) != NIL)
986  set_qpqual((Plan) plan,
987  list_concat(get_qpqual((Plan) plan),
988  get_actual_clauses(get_loc_restrictinfo(best_path))));
989 #endif
990 
991  return plan;
992 }
#define NIL
Definition: pg_list.h:69
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:331
List * list_concat(List *list1, List *list2)
Definition: list.c:321
static NestLoop * create_nestloop_plan(PlannerInfo *root, NestPath *best_path)
Definition: createplan.c:3642
NodeTag pathtype
Definition: relation.h:952
#define ERROR
Definition: elog.h:43
List * joinrestrictinfo
Definition: relation.h:1299
#define NULL
Definition: c.h:229
Path path
Definition: relation.h:1289
static HashJoin * create_hashjoin_plan(PlannerInfo *root, HashPath *best_path)
Definition: createplan.c:4053
static Plan * create_gating_plan(PlannerInfo *root, Path *path, Plan *plan, List *gating_quals)
Definition: createplan.c:900
#define elog
Definition: elog.h:219
static MergeJoin * create_mergejoin_plan(PlannerInfo *root, MergePath *best_path)
Definition: createplan.c:3748
Definition: pg_list.h:45
Definition: relation.h:948
static List * get_gating_quals(PlannerInfo *root, List *quals)
Definition: createplan.c:880
static Limit * create_limit_plan ( PlannerInfo root,
LimitPath best_path,
int  flags 
)
static

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

2381 {
2382  Limit *plan;
2383  Plan *subplan;
2384 
2385  /* Limit doesn't project, so tlist requirements pass through */
2386  subplan = create_plan_recurse(root, best_path->subpath, flags);
2387 
2388  plan = make_limit(subplan,
2389  best_path->limitOffset,
2390  best_path->limitCount);
2391 
2392  copy_generic_path_info(&plan->plan, (Path *) best_path);
2393 
2394  return plan;
2395 }
Plan plan
Definition: plannodes.h:914
Node * limitOffset
Definition: relation.h:1599
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:354
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4855
Path * subpath
Definition: relation.h:1598
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
Definition: createplan.c:6337
Node * limitCount
Definition: relation.h:1600
Definition: relation.h:948
static LockRows * create_lockrows_plan ( PlannerInfo root,
LockRowsPath best_path,
int  flags 
)
static

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

2300 {
2301  LockRows *plan;
2302  Plan *subplan;
2303 
2304  /* LockRows doesn't project, so tlist requirements pass through */
2305  subplan = create_plan_recurse(root, best_path->subpath, flags);
2306 
2307  plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
2308 
2309  copy_generic_path_info(&plan->plan, (Path *) best_path);
2310 
2311  return plan;
2312 }
Plan plan
Definition: plannodes.h:900
List * rowMarks
Definition: relation.h:1563
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:354
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4855
Path * subpath
Definition: relation.h:1562
static LockRows * make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
Definition: createplan.c:6316
Definition: relation.h:948
static Material * create_material_plan ( PlannerInfo root,
MaterialPath best_path,
int  flags 
)
static

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

1227 {
1228  Material *plan;
1229  Plan *subplan;
1230 
1231  /*
1232  * We don't want any excess columns in the materialized tuples, so request
1233  * a smaller tlist. Otherwise, since Material doesn't project, tlist
1234  * requirements pass through.
1235  */
1236  subplan = create_plan_recurse(root, best_path->subpath,
1237  flags | CP_SMALL_TLIST);
1238 
1239  plan = make_material(subplan);
1240 
1241  copy_generic_path_info(&plan->plan, (Path *) best_path);
1242 
1243  return plan;
1244 }
#define CP_SMALL_TLIST
Definition: createplan.c:67
Path * subpath
Definition: relation.h:1226
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:354
static Material * make_material(Plan *lefttree)
Definition: createplan.c:5944
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4855
Plan plan
Definition: plannodes.h:735
Definition: relation.h:948
static Plan * create_merge_append_plan ( PlannerInfo root,
MergeAppendPath best_path 
)
static

Definition at line 1067 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, MergeAppendPath::limit_tuples, make_sort(), makeNode, MergeAppend::mergeplans, NIL, NULL, MergeAppend::nullsFirst, MergeAppend::numCols, Path::parent, MergeAppend::partitioned_rels, MergeAppendPath::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(), MergeAppendPath::subpaths, and Plan::targetlist.

Referenced by create_plan_recurse().

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

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

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

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

1972 {
1973  Result *plan;
1974  List *tlist;
1975  ListCell *lc;
1976 
1977  /* Prepare an InitPlan for each aggregate's subquery. */
1978  foreach(lc, best_path->mmaggregates)
1979  {
1980  MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
1981  PlannerInfo *subroot = mminfo->subroot;
1982  Query *subparse = subroot->parse;
1983  Plan *plan;
1984 
1985  /*
1986  * Generate the plan for the subquery. We already have a Path, but we
1987  * have to convert it to a Plan and attach a LIMIT node above it.
1988  * Since we are entering a different planner context (subroot),
1989  * recurse to create_plan not create_plan_recurse.
1990  */
1991  plan = create_plan(subroot, mminfo->path);
1992 
1993  plan = (Plan *) make_limit(plan,
1994  subparse->limitOffset,
1995  subparse->limitCount);
1996 
1997  /* Must apply correct cost/width data to Limit node */
1998  plan->startup_cost = mminfo->path->startup_cost;
1999  plan->total_cost = mminfo->pathcost;
2000  plan->plan_rows = 1;
2001  plan->plan_width = mminfo->path->pathtarget->width;
2002  plan->parallel_aware = false;
2003  plan->parallel_safe = mminfo->path->parallel_safe;
2004 
2005  /* Convert the plan into an InitPlan in the outer query. */
2006  SS_make_initplan_from_plan(root, subroot, plan, mminfo->param);
2007  }
2008 
2009  /* Generate the output plan --- basically just a Result */
2010  tlist = build_path_tlist(root, &best_path->path);
2011 
2012  plan = make_result(tlist, (Node *) best_path->quals, NULL);
2013 
2014  copy_generic_path_info(&plan->plan, (Path *) best_path);
2015 
2016  /*
2017  * During setrefs.c, we'll need to replace references to the Agg nodes
2018  * with InitPlan output params. (We can't just do that locally in the
2019  * MinMaxAgg node, because path nodes above here may have Agg references
2020  * as well.) Save the mmaggregates list to tell setrefs.c to do that.
2021  *
2022  * This doesn't work if we're in an inheritance subtree (see notes in
2023  * create_modifytable_plan). Fortunately we can't be because there would
2024  * never be aggregates in an UPDATE/DELETE; but let's Assert that.
2025  */
2026  Assert(!root->hasInheritedTarget);
2027  Assert(root->minmax_aggs == NIL);
2028  root->minmax_aggs = best_path->mmaggregates;
2029 
2030  return plan;
2031 }
Node * limitOffset
Definition: parsenodes.h:158
#define NIL
Definition: pg_list.h:69
double plan_rows
Definition: plannodes.h:131
PathTarget * pathtarget
Definition: relation.h:955
Query * parse
Definition: relation.h:155
Param * param
Definition: relation.h:2085
Definition: nodes.h:509
List * minmax_aggs
Definition: relation.h:285
List * quals
Definition: relation.h:1510
Plan * create_plan(PlannerInfo *root, Path *best_path)
Definition: createplan.c:303
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4855
Path * path
Definition: relation.h:2083
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:728
Cost startup_cost
Definition: relation.h:965
Node * limitCount
Definition: parsenodes.h:159
Cost startup_cost
Definition: plannodes.h:125
bool parallel_aware
Definition: plannodes.h:137
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6358
List * mmaggregates
Definition: relation.h:1509
int plan_width
Definition: plannodes.h:132
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
bool parallel_safe
Definition: relation.h:960
bool hasInheritedTarget
Definition: relation.h:297
void SS_make_initplan_from_plan(PlannerInfo *root, PlannerInfo *subroot, Plan *plan, Param *prm)
Definition: subselect.c:2902
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
Definition: createplan.c:6337
int width
Definition: relation.h:887
Cost total_cost
Definition: plannodes.h:126
bool parallel_safe
Definition: plannodes.h:138
Definition: pg_list.h:45
Definition: relation.h:948
PlannerInfo * subroot
Definition: relation.h:2082
static ModifyTable * create_modifytable_plan ( PlannerInfo root,
ModifyTablePath best_path 
)
static

Definition at line 2321 of file createplan.c.

References apply_tlist_labeling(), ModifyTablePath::canSetTag, copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), ModifyTablePath::epqParam, forboth, lappend(), lfirst, make_modifytable(), NIL, ModifyTablePath::nominalRelation, ModifyTablePath::onconflict, ModifyTablePath::operation, ModifyTablePath::partitioned_rels, ModifyTablePath::path, ModifyTable::plan, PlannerInfo::processed_tlist, ModifyTablePath::resultRelations, ModifyTablePath::returningLists, ModifyTablePath::rowMarks, subpath(), ModifyTablePath::subpaths, ModifyTablePath::subroots, Plan::targetlist, and ModifyTablePath::withCheckOptionLists.

Referenced by create_plan_recurse().

2322 {
2323  ModifyTable *plan;
2324  List *subplans = NIL;
2325  ListCell *subpaths,
2326  *subroots;
2327 
2328  /* Build the plan for each input path */
2329  forboth(subpaths, best_path->subpaths,
2330  subroots, best_path->subroots)
2331  {
2332  Path *subpath = (Path *) lfirst(subpaths);
2333  PlannerInfo *subroot = (PlannerInfo *) lfirst(subroots);
2334  Plan *subplan;
2335 
2336  /*
2337  * In an inherited UPDATE/DELETE, reference the per-child modified
2338  * subroot while creating Plans from Paths for the child rel. This is
2339  * a kluge, but otherwise it's too hard to ensure that Plan creation
2340  * functions (particularly in FDWs) don't depend on the contents of
2341  * "root" matching what they saw at Path creation time. The main
2342  * downside is that creation functions for Plans that might appear
2343  * below a ModifyTable cannot expect to modify the contents of "root"
2344  * and have it "stick" for subsequent processing such as setrefs.c.
2345  * That's not great, but it seems better than the alternative.
2346  */
2347  subplan = create_plan_recurse(subroot, subpath, CP_EXACT_TLIST);
2348 
2349  /* Transfer resname/resjunk labeling, too, to keep executor happy */
2350  apply_tlist_labeling(subplan->targetlist, subroot->processed_tlist);
2351 
2352  subplans = lappend(subplans, subplan);
2353  }
2354 
2355  plan = make_modifytable(root,
2356  best_path->operation,
2357  best_path->canSetTag,
2358  best_path->nominalRelation,
2359  best_path->partitioned_rels,
2360  best_path->resultRelations,
2361  subplans,
2362  best_path->withCheckOptionLists,
2363  best_path->returningLists,
2364  best_path->rowMarks,
2365  best_path->onconflict,
2366  best_path->epqParam);
2367 
2368  copy_generic_path_info(&plan->plan, &best_path->path);
2369 
2370  return plan;
2371 }
#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:1586
OnConflictExpr * onconflict
Definition: relation.h:1588
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
Index nominalRelation
Definition: relation.h:1579
List * rowMarks
Definition: relation.h:1587
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:354
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4855
List * subroots
Definition: relation.h:1584
List * subpaths
Definition: relation.h:1583
List * lappend(List *list, void *datum)
Definition: list.c:128
List * partitioned_rels
Definition: relation.h:1581
static ModifyTable * make_modifytable(PlannerInfo *root, CmdType operation, bool canSetTag, Index nominalRelation, List *partitioned_rels, List *resultRelations, List *subplans, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, int epqParam)
Definition: createplan.c:6398
#define lfirst(lc)
Definition: pg_list.h:106
List * targetlist
Definition: plannodes.h:144
List * withCheckOptionLists
Definition: relation.h:1585
CmdType operation
Definition: relation.h:1577
List * resultRelations
Definition: relation.h:1582
List * processed_tlist
Definition: relation.h:281
#define CP_EXACT_TLIST
Definition: createplan.c:66
Definition: pg_list.h:45
Definition: relation.h:948
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:234
static NamedTuplestoreScan * create_namedtuplestorescan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

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

3331 {
3332  NamedTuplestoreScan *scan_plan;
3333  Index scan_relid = best_path->parent->relid;
3334  RangeTblEntry *rte;
3335 
3336  Assert(scan_relid > 0);
3337  rte = planner_rt_fetch(scan_relid, root);
3339 
3340  /* Sort clauses into best execution order */
3341  scan_clauses = order_qual_clauses(root, scan_clauses);
3342 
3343  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3344  scan_clauses = extract_actual_clauses(scan_clauses, false);
3345 
3346  /* Replace any outer-relation variables with nestloop params */
3347  if (best_path->param_info)
3348  {
3349  scan_clauses = (List *)
3350  replace_nestloop_params(root, (Node *) scan_clauses);
3351  }
3352 
3353  scan_plan = make_namedtuplestorescan(tlist, scan_clauses, scan_relid,
3354  rte->enrname);
3355 
3356  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3357 
3358  return scan_plan;
3359 }
Plan plan
Definition: plannodes.h:328
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4203
ParamPathInfo * param_info
Definition: relation.h:957
Definition: nodes.h:509
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4761
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4855
#define planner_rt_fetch(rti, root)
Definition: relation.h:325
RelOptInfo * parent
Definition: relation.h:954
char * enrname
Definition: parsenodes.h:1036
Index relid
Definition: relation.h:553
static NamedTuplestoreScan * make_namedtuplestorescan(List *qptlist, List *qpqual, Index scanrelid, char *enrname)
Definition: createplan.c:5199
unsigned int Index
Definition: c.h:365
#define Assert(condition)
Definition: c.h:675
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:354
RTEKind rtekind
Definition: parsenodes.h:944
Definition: pg_list.h:45
static NestLoop * create_nestloop_plan ( PlannerInfo root,
NestPath best_path 
)
static

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

3644 {
3645  NestLoop *join_plan;
3646  Plan *outer_plan;
3647  Plan *inner_plan;
3648  List *tlist = build_path_tlist(root, &best_path->path);
3649  List *joinrestrictclauses = best_path->joinrestrictinfo;
3650  List *joinclauses;
3651  List *otherclauses;
3652  Relids outerrelids;
3653  List *nestParams;
3654  Relids saveOuterRels = root->curOuterRels;
3655  ListCell *cell;
3656  ListCell *prev;
3657  ListCell *next;
3658 
3659  /* NestLoop can project, so no need to be picky about child tlists */
3660  outer_plan = create_plan_recurse(root, best_path->outerjoinpath, 0);
3661 
3662  /* For a nestloop, include outer relids in curOuterRels for inner side */
3663  root->curOuterRels = bms_union(root->curOuterRels,
3664  best_path->outerjoinpath->parent->relids);
3665 
3666  inner_plan = create_plan_recurse(root, best_path->innerjoinpath, 0);
3667 
3668  /* Restore curOuterRels */
3669  bms_free(root->curOuterRels);
3670  root->curOuterRels = saveOuterRels;
3671 
3672  /* Sort join qual clauses into best execution order */
3673  joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
3674 
3675  /* Get the join qual clauses (in plain expression form) */
3676  /* Any pseudoconstant clauses are ignored here */
3677  if (IS_OUTER_JOIN(best_path->jointype))
3678  {
3679  extract_actual_join_clauses(joinrestrictclauses,
3680  &joinclauses, &otherclauses);
3681  }
3682  else
3683  {
3684  /* We can treat all clauses alike for an inner join */
3685  joinclauses = extract_actual_clauses(joinrestrictclauses, false);
3686  otherclauses = NIL;
3687  }
3688 
3689  /* Replace any outer-relation variables with nestloop params */
3690  if (best_path->path.param_info)
3691  {
3692  joinclauses = (List *)
3693  replace_nestloop_params(root, (Node *) joinclauses);
3694  otherclauses = (List *)
3695  replace_nestloop_params(root, (Node *) otherclauses);
3696  }
3697 
3698  /*
3699  * Identify any nestloop parameters that should be supplied by this join
3700  * node, and move them from root->curOuterParams to the nestParams list.
3701  */
3702  outerrelids = best_path->outerjoinpath->parent->relids;
3703  nestParams = NIL;
3704  prev = NULL;
3705  for (cell = list_head(root->curOuterParams); cell; cell = next)
3706  {
3707  NestLoopParam *nlp = (NestLoopParam *) lfirst(cell);
3708 
3709  next = lnext(cell);
3710  if (IsA(nlp->paramval, Var) &&
3711  bms_is_member(nlp->paramval->varno, outerrelids))
3712  {
3714  cell, prev);
3715  nestParams = lappend(nestParams, nlp);
3716  }
3717  else if (IsA(nlp->paramval, PlaceHolderVar) &&
3718  bms_overlap(((PlaceHolderVar *) nlp->paramval)->phrels,
3719  outerrelids) &&
3721  (PlaceHolderVar *) nlp->paramval,
3722  false)->ph_eval_at,
3723  outerrelids))
3724  {
3726  cell, prev);
3727  nestParams = lappend(nestParams, nlp);
3728  }
3729  else
3730  prev = cell;
3731  }
3732 
3733  join_plan = make_nestloop(tlist,
3734  joinclauses,
3735  otherclauses,
3736  nestParams,
3737  outer_plan,
3738  inner_plan,
3739  best_path->jointype,
3740  best_path->inner_unique);
3741 
3742  copy_generic_path_info(&join_plan->join.plan, &best_path->path);
3743 
3744  return join_plan;
3745 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4203
Relids ph_eval_at
Definition: relation.h:2064
static int32 next
Definition: blutils.c:210
void extract_actual_join_clauses(List *restrictinfo_list, List **joinquals, List **otherquals)
Definition: restrictinfo.c:381
Relids curOuterRels
Definition: relation.h:312
Path * innerjoinpath
Definition: relation.h:1297
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:722
ParamPathInfo * param_info
Definition: relation.h:957
Definition: nodes.h:509
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4761
Definition: primnodes.h:163
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:354
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4855
Var * paramval
Definition: plannodes.h:693
Join join
Definition: plannodes.h:685
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:728
List * joinrestrictinfo
Definition: relation.h:1299
RelOptInfo * parent
Definition: relation.h:954
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
static NestLoop * make_nestloop(List *tlist, List *joinclauses, List *otherclauses, List *nestParams, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
Definition: createplan.c:5370
List * curOuterParams
Definition: relation.h:313
PlaceHolderInfo * find_placeholder_info(PlannerInfo *root, PlaceHolderVar *phv, bool create_new_ph)
Definition: placeholder.c:69
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
Relids relids
Definition: relation.h:525
#define lnext(lc)
Definition: pg_list.h:105
List * lappend(List *list, void *datum)
Definition: list.c:128
Index varno
Definition: primnodes.h:166
List * list_delete_cell(List *list, ListCell *cell, ListCell *prev)
Definition: list.c:528
Path * outerjoinpath
Definition: relation.h:1296
void bms_free(Bitmapset *a)
Definition: bitmapset.c:201
#define NULL
Definition: c.h:229
Path path
Definition: relation.h:1289
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:218
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:354
bool inner_unique
Definition: relation.h:1293
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
JoinType jointype
Definition: relation.h:1291
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
Plan plan
Definition: plannodes.h:666
Plan* create_plan ( PlannerInfo root,
Path best_path 
)

Definition at line 303 of file createplan.c.

References apply_tlist_labeling(), Assert, CP_EXACT_TLIST, create_plan_recurse(), PlannerInfo::curOuterParams, PlannerInfo::curOuterRels, elog, ERROR, IsA, NIL, NULL, 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().

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

Definition at line 354 of file createplan.c.

References Assert, create_agg_plan(), create_append_plan(), create_gather_merge_plan(), create_gather_plan(), create_group_plan(), create_groupingsets_plan(), create_join_plan(), create_limit_plan(), create_lockrows_plan(), create_material_plan(), create_merge_append_plan(), create_minmaxagg_plan(), create_modifytable_plan(), create_project_set_plan(), create_projection_plan(), create_recursiveunion_plan(), create_result_plan(), create_scan_plan(), create_setop_plan(), create_sort_plan(), create_unique_plan(), create_upper_unique_plan(), create_windowagg_plan(), elog, ERROR, IsA, NULL, 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().

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

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

1201 {
1202  ProjectSet *plan;
1203  Plan *subplan;
1204  List *tlist;
1205 
1206  /* Since we intend to project, we don't need to constrain child tlist */
1207  subplan = create_plan_recurse(root, best_path->subpath, 0);
1208 
1209  tlist = build_path_tlist(root, &best_path->path);
1210 
1211  plan = make_project_set(tlist, subplan);
1212 
1213  copy_generic_path_info(&plan->plan, (Path *) best_path);
1214 
1215  return plan;
1216 }
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:354
static ProjectSet * make_project_set(List *tlist, Plan *subplan)
Definition: createplan.c:6379
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4855
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:728
Path * subpath
Definition: relation.h:1404
Definition: pg_list.h:45
Definition: relation.h:948
Plan plan
Definition: plannodes.h:202
static Plan * create_projection_plan ( PlannerInfo root,
ProjectionPath best_path 
)
static

Definition at line 1548 of file createplan.c.

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

Referenced by create_plan_recurse().

1549 {
1550  Plan *plan;
1551  Plan *subplan;
1552  List *tlist;
1553 
1554  /* Since we intend to project, we don't need to constrain child tlist */
1555  subplan = create_plan_recurse(root, best_path->subpath, 0);
1556 
1557  tlist = build_path_tlist(root, &best_path->path);
1558 
1559  /*
1560  * We might not really need a Result node here, either because the subplan
1561  * can project or because it's returning the right list of expressions
1562  * anyway. Usually create_projection_path will have detected that and set
1563  * dummypp if we don't need a Result; but its decision can't be final,
1564  * because some createplan.c routines change the tlists of their nodes.
1565  * (An example is that create_merge_append_plan might add resjunk sort
1566  * columns to a MergeAppend.) So we have to recheck here. If we do
1567  * arrive at a different answer than create_projection_path did, we'll
1568  * have made slightly wrong cost estimates; but label the plan with the
1569  * cost estimates we actually used, not "corrected" ones. (XXX this could
1570  * be cleaned up if we moved more of the sortcolumn setup logic into Path
1571  * creation, but that would add expense to creating Paths we might end up
1572  * not using.)
1573  */
1574  if (is_projection_capable_path(best_path->subpath) ||
1575  tlist_same_exprs(tlist, subplan->targetlist))
1576  {
1577  /* Don't need a separate Result, just assign tlist to subplan */
1578  plan = subplan;
1579  plan->targetlist = tlist;
1580 
1581  /* Label plan with the estimated costs we actually used */
1582  plan->startup_cost = best_path->path.startup_cost;
1583  plan->total_cost = best_path->path.total_cost;
1584  plan->plan_rows = best_path->path.rows;
1585  plan->plan_width = best_path->path.pathtarget->width;
1586  plan->parallel_safe = best_path->path.parallel_safe;
1587  /* ... but don't change subplan's parallel_aware flag */
1588  }
1589  else
1590  {
1591  /* We need a Result node */
1592  plan = (Plan *) make_result(tlist, NULL, subplan);
1593 
1594  copy_generic_path_info(plan, (Path *) best_path);
1595  }
1596 
1597  return plan;
1598 }
double plan_rows
Definition: plannodes.h:131
PathTarget * pathtarget
Definition: relation.h:955
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:354
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4855
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:728
Cost startup_cost
Definition: relation.h:965
Cost startup_cost
Definition: plannodes.h:125
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6358
Cost total_cost
Definition: relation.h:966
int plan_width
Definition: plannodes.h:132
#define NULL
Definition: c.h:229
double rows
Definition: relation.h:964
bool parallel_safe
Definition: relation.h:960
bool tlist_same_exprs(List *tlist1, List *tlist2)
Definition: tlist.c:221
List * targetlist
Definition: plannodes.h:144
int width
Definition: relation.h:887
bool is_projection_capable_path(Path *path)
Definition: createplan.c:6536
Cost total_cost
Definition: plannodes.h:126
bool parallel_safe
Definition: plannodes.h:138
Path * subpath
Definition: relation.h:1392
Definition: pg_list.h:45
Definition: relation.h:948
static RecursiveUnion * create_recursiveunion_plan ( PlannerInfo root,
RecursiveUnionPath best_path 
)
static

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

2263 {
2264  RecursiveUnion *plan;
2265  Plan *leftplan;
2266  Plan *rightplan;
2267  List *tlist;
2268  long numGroups;
2269 
2270  /* Need both children to produce same tlist, so force it */
2271  leftplan = create_plan_recurse(root, best_path->leftpath, CP_EXACT_TLIST);
2272  rightplan = create_plan_recurse(root, best_path->rightpath, CP_EXACT_TLIST);
2273 
2274  tlist = build_path_tlist(root, &best_path->path);
2275 
2276  /* Convert numGroups to long int --- but 'ware overflow! */
2277  numGroups = (long) Min(best_path->numGroups, (double) LONG_MAX);
2278 
2279  plan = make_recursive_union(tlist,
2280  leftplan,
2281  rightplan,
2282  best_path->wtParam,
2283  best_path->distinctList,
2284  numGroups);
2285 
2286  copy_generic_path_info(&plan->plan, (Path *) best_path);
2287 
2288  return plan;
2289 }
static RecursiveUnion * make_recursive_union(List *tlist, Plan *lefttree, Plan *righttree, int wtParam, List *distinctList, long numGroups)
Definition: createplan.c:5288
#define Min(x, y)
Definition: c.h:806
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:354
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4855
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:728
#define CP_EXACT_TLIST
Definition: createplan.c:66
Definition: pg_list.h:45
Definition: relation.h:948
static Result * create_result_plan ( PlannerInfo root,
ResultPath best_path 
)
static

Definition at line 1175 of file createplan.c.

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

Referenced by create_plan_recurse().

1176 {
1177  Result *plan;
1178  List *tlist;
1179  List *quals;
1180 
1181  tlist = build_path_tlist(root, &best_path->path);
1182 
1183  /* best_path->quals is just bare clauses */
1184  quals = order_qual_clauses(root, best_path->quals);
1185 
1186  plan = make_result(tlist, (Node *) quals, NULL);
1187 
1188  copy_generic_path_info(&plan->plan, (Path *) best_path);
1189 
1190  return plan;
1191 }
Plan plan
Definition: plannodes.h:190
Definition: nodes.h:509
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4761
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4855
Path path
Definition: relation.h:1213
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:728
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6358
#define NULL
Definition: c.h:229
List * quals
Definition: relation.h:1214
Definition: pg_list.h:45
Definition: relation.h:948
static SampleScan * create_samplescan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 2449 of file createplan.c.

References Assert, copy_generic_path_info(), extract_actual_clauses(), make_samplescan(), NULL, 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().

2451 {
2452  SampleScan *scan_plan;
2453  Index scan_relid = best_path->parent->relid;
2454  RangeTblEntry *rte;
2455  TableSampleClause *tsc;
2456 
2457  /* it should be a base rel with a tablesample clause... */
2458  Assert(scan_relid > 0);
2459  rte = planner_rt_fetch(scan_relid, root);
2460  Assert(rte->rtekind == RTE_RELATION);
2461  tsc = rte->tablesample;
2462  Assert(tsc != NULL);
2463 
2464  /* Sort clauses into best execution order */
2465  scan_clauses = order_qual_clauses(root, scan_clauses);
2466 
2467  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2468  scan_clauses = extract_actual_clauses(scan_clauses, false);
2469 
2470  /* Replace any outer-relation variables with nestloop params */
2471  if (best_path->param_info)
2472  {
2473  scan_clauses = (List *)
2474  replace_nestloop_params(root, (Node *) scan_clauses);
2475  tsc = (TableSampleClause *)
2476  replace_nestloop_params(root, (Node *) tsc);
2477  }
2478 
2479  scan_plan = make_samplescan(tlist,
2480  scan_clauses,
2481  scan_relid,
2482  tsc);
2483 
2484  copy_generic_path_info(&scan_plan->scan.plan, best_path);
2485 
2486  return scan_plan;
2487 }
Plan plan
Definition: plannodes.h:328
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4203
ParamPathInfo * param_info
Definition: relation.h:957
Definition: nodes.h:509
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4761
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4855
#define planner_rt_fetch(rti, root)
Definition: relation.h:325
Scan scan
Definition: plannodes.h:344
RelOptInfo * parent
Definition: relation.h:954
Index relid
Definition: relation.h:553
static SampleScan * make_samplescan(List *qptlist, List *qpqual, Index scanrelid, TableSampleClause *tsc)
Definition: createplan.c:4963
unsigned int Index
Definition: c.h:365
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:354
RTEKind rtekind
Definition: parsenodes.h:944
Definition: pg_list.h:45
struct TableSampleClause * tablesample
Definition: parsenodes.h:962
static Plan * create_scan_plan ( PlannerInfo root,
Path best_path,
int  flags 
)
static

Definition at line 503 of file createplan.c.

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

Referenced by create_plan_recurse().

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