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

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

Referenced by create_bitmap_scan_plan().

4918 {
4919  if (IsA(plan, BitmapAnd))
4921  linitial(((BitmapAnd *) plan)->bitmapplans));
4922  else if (IsA(plan, BitmapOr))
4923  ((BitmapOr *) plan)->isshared = true;
4924  else if (IsA(plan, BitmapIndexScan))
4925  ((BitmapIndexScan *) plan)->isshared = true;
4926  else
4927  elog(ERROR, "unrecognized node type: %d", nodeTag(plan));
4928 }
#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:4917
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:954
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4202
ParamPathInfo * param_info
Definition: relation.h:956
Definition: nodes.h:509
Index * sortgrouprefs
Definition: relation.h:884
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:883
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:1370
Definition: pg_list.h:45
static void copy_generic_path_info ( Plan dest,
Path src 
)
static

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

4855 {
4856  dest->startup_cost = src->startup_cost;
4857  dest->total_cost = src->total_cost;
4858  dest->plan_rows = src->rows;
4859  dest->plan_width = src->pathtarget->width;
4860  dest->parallel_aware = src->parallel_aware;
4861  dest->parallel_safe = src->parallel_safe;
4862 }
double plan_rows
Definition: plannodes.h:131
PathTarget * pathtarget
Definition: relation.h:954
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:959
int width
Definition: relation.h:886
Cost total_cost
Definition: plannodes.h:126
bool parallel_aware
Definition: relation.h:958
bool parallel_safe
Definition: plannodes.h:138
static void copy_plan_costsize ( Plan dest,
Plan src 
)
static

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

4870 {
4871  dest->startup_cost = src->startup_cost;
4872  dest->total_cost = src->total_cost;
4873  dest->plan_rows = src->plan_rows;
4874  dest->plan_width = src->plan_width;
4875  /* Assume the inserted node is not parallel-aware. */
4876  dest->parallel_aware = false;
4877  /* Assume the inserted node is parallel-safe, if child plan is. */
4878  dest->parallel_safe = src->parallel_safe;
4879 }
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:4760
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:5999
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:4854
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:728
List * groupClause
Definition: relation.h:1464
Plan plan
Definition: plannodes.h:784
#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:782
Definition: pg_list.h:45
Path path
Definition: relation.h:1459
Definition: relation.h:947
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:5271
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:4854
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:6357
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:947
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))
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
Plan plan
Definition: plannodes.h:328
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4202
bool predicate_implied_by(List *predicate_list, List *restrictinfo_list)
Definition: predtest.c:128
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:956
Definition: nodes.h:509
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4760
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:5060
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4854
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:953
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:4917
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:958
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  {
2973  *qual = lappend(*qual, pred);
2974  *indexqual = lappend(*indexqual, pred);
2975  }
2976  }
2977  subindexECs = NIL;
2978  foreach(l, ipath->indexquals)
2979  {
2980  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
2981 
2982  if (rinfo->parent_ec)
2983  subindexECs = lappend(subindexECs, rinfo->parent_ec);
2984  }
2985  *indexECs = subindexECs;
2986  }
2987  else
2988  {
2989  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
2990  plan = NULL; /* keep compiler quiet */
2991  }
2992 
2993  return plan;
2994 }
#define NIL
Definition: pg_list.h:69
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
bool predicate_implied_by(List *predicate_list, List *restrictinfo_list)
Definition: predtest.c:128
#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:5339
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:953
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:5354
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:959
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:5039
#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:947
static CteScan * create_ctescan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

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

3236 {
3237  CteScan *scan_plan;
3238  Index scan_relid = best_path->parent->relid;
3239  RangeTblEntry *rte;
3240  SubPlan *ctesplan = NULL;
3241  int plan_id;
3242  int cte_param_id;
3243  PlannerInfo *cteroot;
3244  Index levelsup;
3245  int ndx;
3246  ListCell *lc;
3247 
3248  Assert(scan_relid > 0);
3249  rte = planner_rt_fetch(scan_relid, root);
3250  Assert(rte->rtekind == RTE_CTE);
3251  Assert(!rte->self_reference);
3252 
3253  /*
3254  * Find the referenced CTE, and locate the SubPlan previously made for it.
3255  */
3256  levelsup = rte->ctelevelsup;
3257  cteroot = root;
3258  while (levelsup-- > 0)
3259  {
3260  cteroot = cteroot->parent_root;
3261  if (!cteroot) /* shouldn't happen */
3262  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3263  }
3264 
3265  /*
3266  * Note: cte_plan_ids can be shorter than cteList, if we are still working
3267  * on planning the CTEs (ie, this is a side-reference from another CTE).
3268  * So we mustn't use forboth here.
3269  */
3270  ndx = 0;
3271  foreach(lc, cteroot->parse->cteList)
3272  {
3273  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
3274 
3275  if (strcmp(cte->ctename, rte->ctename) == 0)
3276  break;
3277  ndx++;
3278  }
3279  if (lc == NULL) /* shouldn't happen */
3280  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
3281  if (ndx >= list_length(cteroot->cte_plan_ids))
3282  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3283  plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
3284  Assert(plan_id > 0);
3285  foreach(lc, cteroot->init_plans)
3286  {
3287  ctesplan = (SubPlan *) lfirst(lc);
3288  if (ctesplan->plan_id == plan_id)
3289  break;
3290  }
3291  if (lc == NULL) /* shouldn't happen */
3292  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3293 
3294  /*
3295  * We need the CTE param ID, which is the sole member of the SubPlan's
3296  * setParam list.
3297  */
3298  cte_param_id = linitial_int(ctesplan->setParam);
3299 
3300  /* Sort clauses into best execution order */
3301  scan_clauses = order_qual_clauses(root, scan_clauses);
3302 
3303  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3304  scan_clauses = extract_actual_clauses(scan_clauses, false);
3305 
3306  /* Replace any outer-relation variables with nestloop params */
3307  if (best_path->param_info)
3308  {
3309  scan_clauses = (List *)
3310  replace_nestloop_params(root, (Node *) scan_clauses);
3311  }
3312 
3313  scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
3314  plan_id, cte_param_id);
3315 
3316  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3317 
3318  return scan_plan;
3319 }
Plan plan
Definition: plannodes.h:328
Query * parse
Definition: relation.h:155
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4202
int plan_id
Definition: primnodes.h:688
ParamPathInfo * param_info
Definition: relation.h:956
Definition: nodes.h:509
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4760
#define linitial_int(l)
Definition: pg_list.h:112
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4854
#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:953
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:5177
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:996
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:706
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:995
RTEKind rtekind
Definition: parsenodes.h:929
List * cteList
Definition: parsenodes.h:133
char * ctename
Definition: parsenodes.h:994
#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 3570 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().

3572 {
3573  CustomScan *cplan;
3574  RelOptInfo *rel = best_path->path.parent;
3575  List *custom_plans = NIL;
3576  ListCell *lc;
3577 
3578  /* Recursively transform child paths. */
3579  foreach(lc, best_path->custom_paths)
3580  {
3581  Plan *plan = create_plan_recurse(root, (Path *) lfirst(lc),
3582  CP_EXACT_TLIST);
3583 
3584  custom_plans = lappend(custom_plans, plan);
3585  }
3586 
3587  /*
3588  * Sort clauses into the best execution order, although custom-scan
3589  * provider can reorder them again.
3590  */
3591  scan_clauses = order_qual_clauses(root, scan_clauses);
3592 
3593  /*
3594  * Invoke custom plan provider to create the Plan node represented by the
3595  * CustomPath.
3596  */
3597  cplan = castNode(CustomScan,
3598  best_path->methods->PlanCustomPath(root,
3599  rel,
3600  best_path,
3601  tlist,
3602  scan_clauses,
3603  custom_plans));
3604 
3605  /*
3606  * Copy cost data from Path to Plan; no need to make custom-plan providers
3607  * do this
3608  */
3609  copy_generic_path_info(&cplan->scan.plan, &best_path->path);
3610 
3611  /* Likewise, copy the relids that are represented by this custom scan */
3612  cplan->custom_relids = best_path->path.parent->relids;
3613 
3614  /*
3615  * Replace any outer-relation variables with nestloop params in the qual
3616  * and custom_exprs expressions. We do this last so that the custom-plan
3617  * provider doesn't have to be involved. (Note that parts of custom_exprs
3618  * could have come from join clauses, so doing this beforehand on the
3619  * scan_clauses wouldn't work.) We assume custom_scan_tlist contains no
3620  * such variables.
3621  */
3622  if (best_path->path.param_info)
3623  {
3624  cplan->scan.plan.qual = (List *)
3625  replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
3626  cplan->custom_exprs = (List *)
3627  replace_nestloop_params(root, (Node *) cplan->custom_exprs);
3628  }
3629 
3630  return cplan;
3631 }
#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:4202
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:956
Definition: nodes.h:509
List * custom_exprs
Definition: plannodes.h:630
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4760
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:4854
RelOptInfo * parent
Definition: relation.h:953
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:626
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * custom_relids
Definition: plannodes.h:634
#define CP_EXACT_TLIST
Definition: createplan.c:66
Definition: pg_list.h:45
Definition: relation.h:947
static ForeignScan * create_foreignscan_plan ( PlannerInfo root,
ForeignPath best_path,
List tlist,
List scan_clauses 
)
static

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

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

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

3106 {
3107  FunctionScan *scan_plan;
3108  Index scan_relid = best_path->parent->relid;
3109  RangeTblEntry *rte;
3110  List *functions;
3111 
3112  /* it should be a function base rel... */
3113  Assert(scan_relid > 0);
3114  rte = planner_rt_fetch(scan_relid, root);
3115  Assert(rte->rtekind == RTE_FUNCTION);
3116  functions = rte->functions;
3117 
3118  /* Sort clauses into best execution order */
3119  scan_clauses = order_qual_clauses(root, scan_clauses);
3120 
3121  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3122  scan_clauses = extract_actual_clauses(scan_clauses, false);
3123 
3124  /* Replace any outer-relation variables with nestloop params */
3125  if (best_path->param_info)
3126  {
3127  scan_clauses = (List *)
3128  replace_nestloop_params(root, (Node *) scan_clauses);
3129  /* The function expressions could contain nestloop params, too */
3130  functions = (List *) replace_nestloop_params(root, (Node *) functions);
3131  }
3132 
3133  scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
3134  functions, rte->funcordinality);
3135 
3136  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3137 
3138  return scan_plan;
3139 }
Plan plan
Definition: plannodes.h:328
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4202
ParamPathInfo * param_info
Definition: relation.h:956
Definition: nodes.h:509
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4760
bool funcordinality
Definition: parsenodes.h:979
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4854
#define planner_rt_fetch(rti, root)
Definition: relation.h:325
RelOptInfo * parent
Definition: relation.h:953
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:5118
#define Assert(condition)
Definition: c.h:675
List * functions
Definition: parsenodes.h:978
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:929
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:852
AttrNumber * sortColIdx
Definition: plannodes.h:850
bool * nullsFirst
Definition: plannodes.h:853
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:4854
bool parallelModeNeeded
Definition: relation.h:131
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:728
RelOptInfo * parent
Definition: relation.h:953
PlannerGlobal * glob
Definition: relation.h:157
Relids relids
Definition: relation.h:525
Oid * sortOperators
Definition: plannodes.h:851
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:5481
#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:847
#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:5542
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:6236
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:4854
bool parallelModeNeeded
Definition: relation.h:131
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:728
Plan plan
Definition: plannodes.h:834
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:4869
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6357
#define Assert(condition)
Definition: c.h:675
bool parallel_safe
Definition: relation.h:959
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:4760
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:4854
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:6061
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:762
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:947
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:1157
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:5999
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:4854
#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:1368
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:5902
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:784
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:734
List * groupClause
Definition: parsenodes.h:146
Definition: plannodes.h:782
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 4052 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().

4054 {
4055  HashJoin *join_plan;
4056  Hash *hash_plan;
4057  Plan *outer_plan;
4058  Plan *inner_plan;
4059  List *tlist = build_path_tlist(root, &best_path->jpath.path);
4060  List *joinclauses;
4061  List *otherclauses;
4062  List *hashclauses;
4063  Oid skewTable = InvalidOid;
4064  AttrNumber skewColumn = InvalidAttrNumber;
4065  bool skewInherit = false;
4066 
4067  /*
4068  * HashJoin can project, so we don't have to demand exact tlists from the
4069  * inputs. However, it's best to request a small tlist from the inner
4070  * side, so that we aren't storing more data than necessary. Likewise, if
4071  * we anticipate batching, request a small tlist from the outer side so
4072  * that we don't put extra data in the outer batch files.
4073  */
4074  outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4075  (best_path->num_batches > 1) ? CP_SMALL_TLIST : 0);
4076 
4077  inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4078  CP_SMALL_TLIST);
4079 
4080  /* Sort join qual clauses into best execution order */
4081  joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4082  /* There's no point in sorting the hash clauses ... */
4083 
4084  /* Get the join qual clauses (in plain expression form) */
4085  /* Any pseudoconstant clauses are ignored here */
4086  if (IS_OUTER_JOIN(best_path->jpath.jointype))
4087  {
4088  extract_actual_join_clauses(joinclauses,
4089  &joinclauses, &otherclauses);
4090  }
4091  else
4092  {
4093  /* We can treat all clauses alike for an inner join */
4094  joinclauses = extract_actual_clauses(joinclauses, false);
4095  otherclauses = NIL;
4096  }
4097 
4098  /*
4099  * Remove the hashclauses from the list of join qual clauses, leaving the
4100  * list of quals that must be checked as qpquals.
4101  */
4102  hashclauses = get_actual_clauses(best_path->path_hashclauses);
4103  joinclauses = list_difference(joinclauses, hashclauses);
4104 
4105  /*
4106  * Replace any outer-relation variables with nestloop params. There
4107  * should not be any in the hashclauses.
4108  */
4109  if (best_path->jpath.path.param_info)
4110  {
4111  joinclauses = (List *)
4112  replace_nestloop_params(root, (Node *) joinclauses);
4113  otherclauses = (List *)
4114  replace_nestloop_params(root, (Node *) otherclauses);
4115  }
4116 
4117  /*
4118  * Rearrange hashclauses, if needed, so that the outer variable is always
4119  * on the left.
4120  */
4121  hashclauses = get_switched_clauses(best_path->path_hashclauses,
4122  best_path->jpath.outerjoinpath->parent->relids);
4123 
4124  /*
4125  * If there is a single join clause and we can identify the outer variable
4126  * as a simple column reference, supply its identity for possible use in
4127  * skew optimization. (Note: in principle we could do skew optimization
4128  * with multiple join clauses, but we'd have to be able to determine the
4129  * most common combinations of outer values, which we don't currently have
4130  * enough stats for.)
4131  */
4132  if (list_length(hashclauses) == 1)
4133  {
4134  OpExpr *clause = (OpExpr *) linitial(hashclauses);
4135  Node *node;
4136 
4137  Assert(is_opclause(clause));
4138  node = (Node *) linitial(clause->args);
4139  if (IsA(node, RelabelType))
4140  node = (Node *) ((RelabelType *) node)->arg;
4141  if (IsA(node, Var))
4142  {
4143  Var *var = (Var *) node;
4144  RangeTblEntry *rte;
4145 
4146  rte = root->simple_rte_array[var->varno];
4147  if (rte->rtekind == RTE_RELATION)
4148  {
4149  skewTable = rte->relid;
4150  skewColumn = var->varattno;
4151  skewInherit = rte->inh;
4152  }
4153  }
4154  }
4155 
4156  /*
4157  * Build the hash node and hash join node.
4158  */
4159  hash_plan = make_hash(inner_plan,
4160  skewTable,
4161  skewColumn,
4162  skewInherit);
4163 
4164  /*
4165  * Set Hash node's startup & total costs equal to total cost of input
4166  * plan; this only affects EXPLAIN display not decisions.
4167  */
4168  copy_plan_costsize(&hash_plan->plan, inner_plan);
4169  hash_plan->plan.startup_cost = hash_plan->plan.total_cost;
4170 
4171  join_plan = make_hashjoin(tlist,
4172  joinclauses,
4173  otherclauses,
4174  hashclauses,
4175  outer_plan,
4176  (Plan *) hash_plan,
4177  best_path->jpath.jointype,
4178  best_path->jpath.inner_unique);
4179 
4180  copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4181 
4182  return join_plan;
4183 }
#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:4202
static List * get_switched_clauses(List *clauses, Relids outerrelids)
Definition: createplan.c:4683
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:5394
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:721
ParamPathInfo * param_info
Definition: relation.h:956
#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:4760
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:163
Join join
Definition: plannodes.h:727
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:4854
#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:953
Cost startup_cost
Definition: plannodes.h:125
static void copy_plan_costsize(Plan *dest, Plan *src)
Definition: createplan.c:4869
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:929
Plan plan
Definition: plannodes.h:866
JoinType jointype
Definition: relation.h:1291
static Hash * make_hash(Plan *lefttree, Oid skewTable, AttrNumber skewColumn, bool skewInherit)
Definition: createplan.c:5419
Cost total_cost
Definition: plannodes.h:126
List * path_hashclauses
Definition: relation.h:1371
List * args
Definition: primnodes.h:501
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
Plan plan
Definition: plannodes.h:668
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))
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
Plan plan
Definition: plannodes.h:328
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4202
#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
bool predicate_implied_by(List *predicate_list, List *restrictinfo_list)
Definition: predtest.c:128
List * indextlist
Definition: relation.h:656
bool pseudoconstant
Definition: relation.h:1755
ParamPathInfo * param_info
Definition: relation.h:956
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:4760
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:852
#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:4854
#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:4981
RelOptInfo * parent
Definition: relation.h:953
#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:4551
static List * fix_indexqual_references(PlannerInfo *root, IndexPath *index_path)
Definition: createplan.c:4420
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:5012
#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:851
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:3641
NodeTag pathtype
Definition: relation.h:951
#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:4052
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:3747
Definition: pg_list.h:45
Definition: relation.h:947
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:916
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:4854
Path * subpath
Definition: relation.h:1598
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
Definition: createplan.c:6336
Node * limitCount
Definition: relation.h:1600
Definition: relation.h:947
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:902
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:4854
Path * subpath
Definition: relation.h:1562
static LockRows * make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
Definition: createplan.c:6315
Definition: relation.h:947
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:5943
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4854
Plan plan
Definition: plannodes.h:737
Definition: relation.h:947
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:4854
#define ERROR
Definition: elog.h:43
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:728
RelOptInfo * parent
Definition: relation.h:953
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:5481
#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:4891
Definition: relation.h:947
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:5542
static MergeJoin * create_mergejoin_plan ( PlannerInfo root,
MergePath best_path 
)
static

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

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

3330 {
3331  NamedTuplestoreScan *scan_plan;
3332  Index scan_relid = best_path->parent->relid;
3333  RangeTblEntry *rte;
3334 
3335  Assert(scan_relid > 0);
3336  rte = planner_rt_fetch(scan_relid, root);
3338 
3339  /* Sort clauses into best execution order */
3340  scan_clauses = order_qual_clauses(root, scan_clauses);
3341 
3342  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3343  scan_clauses = extract_actual_clauses(scan_clauses, false);
3344 
3345  /* Replace any outer-relation variables with nestloop params */
3346  if (best_path->param_info)
3347  {
3348  scan_clauses = (List *)
3349  replace_nestloop_params(root, (Node *) scan_clauses);
3350  }
3351 
3352  scan_plan = make_namedtuplestorescan(tlist, scan_clauses, scan_relid,
3353  rte->enrname);
3354 
3355  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3356 
3357  return scan_plan;
3358 }
Plan plan
Definition: plannodes.h:328
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4202
ParamPathInfo * param_info
Definition: relation.h:956
Definition: nodes.h:509
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4760
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4854
#define planner_rt_fetch(rti, root)
Definition: relation.h:325
RelOptInfo * parent
Definition: relation.h:953
char * enrname
Definition: parsenodes.h:1009
Index relid
Definition: relation.h:553
static NamedTuplestoreScan * make_namedtuplestorescan(List *qptlist, List *qpqual, Index scanrelid, char *enrname)
Definition: createplan.c:5198
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:929
Definition: pg_list.h:45
static NestLoop * create_nestloop_plan ( PlannerInfo root,
NestPath best_path 
)
static

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

3643 {
3644  NestLoop *join_plan;
3645  Plan *outer_plan;
3646  Plan *inner_plan;
3647  List *tlist = build_path_tlist(root, &best_path->path);
3648  List *joinrestrictclauses = best_path->joinrestrictinfo;
3649  List *joinclauses;
3650  List *otherclauses;
3651  Relids outerrelids;
3652  List *nestParams;
3653  Relids saveOuterRels = root->curOuterRels;
3654  ListCell *cell;
3655  ListCell *prev;
3656  ListCell *next;
3657 
3658  /* NestLoop can project, so no need to be picky about child tlists */
3659  outer_plan = create_plan_recurse(root, best_path->outerjoinpath, 0);
3660 
3661  /* For a nestloop, include outer relids in curOuterRels for inner side */
3662  root->curOuterRels = bms_union(root->curOuterRels,
3663  best_path->outerjoinpath->parent->relids);
3664 
3665  inner_plan = create_plan_recurse(root, best_path->innerjoinpath, 0);
3666 
3667  /* Restore curOuterRels */
3668  bms_free(root->curOuterRels);
3669  root->curOuterRels = saveOuterRels;
3670 
3671  /* Sort join qual clauses into best execution order */
3672  joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
3673 
3674  /* Get the join qual clauses (in plain expression form) */
3675  /* Any pseudoconstant clauses are ignored here */
3676  if (IS_OUTER_JOIN(best_path->jointype))
3677  {
3678  extract_actual_join_clauses(joinrestrictclauses,
3679  &joinclauses, &otherclauses);
3680  }
3681  else
3682  {
3683  /* We can treat all clauses alike for an inner join */
3684  joinclauses = extract_actual_clauses(joinrestrictclauses, false);
3685  otherclauses = NIL;
3686  }
3687 
3688  /* Replace any outer-relation variables with nestloop params */
3689  if (best_path->path.param_info)
3690  {
3691  joinclauses = (List *)
3692  replace_nestloop_params(root, (Node *) joinclauses);
3693  otherclauses = (List *)
3694  replace_nestloop_params(root, (Node *) otherclauses);
3695  }
3696 
3697  /*
3698  * Identify any nestloop parameters that should be supplied by this join
3699  * node, and move them from root->curOuterParams to the nestParams list.
3700  */
3701  outerrelids = best_path->outerjoinpath->parent->relids;
3702  nestParams = NIL;
3703  prev = NULL;
3704  for (cell = list_head(root->curOuterParams); cell; cell = next)
3705  {
3706  NestLoopParam *nlp = (NestLoopParam *) lfirst(cell);
3707 
3708  next = lnext(cell);
3709  if (IsA(nlp->paramval, Var) &&
3710  bms_is_member(nlp->paramval->varno, outerrelids))
3711  {
3713  cell, prev);
3714  nestParams = lappend(nestParams, nlp);
3715  }
3716  else if (IsA(nlp->paramval, PlaceHolderVar) &&
3717  bms_overlap(((PlaceHolderVar *) nlp->paramval)->phrels,
3718  outerrelids) &&
3720  (PlaceHolderVar *) nlp->paramval,
3721  false)->ph_eval_at,
3722  outerrelids))
3723  {
3725  cell, prev);
3726  nestParams = lappend(nestParams, nlp);
3727  }
3728  else
3729  prev = cell;
3730  }
3731 
3732  join_plan = make_nestloop(tlist,
3733  joinclauses,
3734  otherclauses,
3735  nestParams,
3736  outer_plan,
3737  inner_plan,
3738  best_path->jointype,
3739  best_path->inner_unique);
3740 
3741  copy_generic_path_info(&join_plan->join.plan, &best_path->path);
3742 
3743  return join_plan;
3744 }
#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:4202
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:721
ParamPathInfo * param_info
Definition: relation.h:956
Definition: nodes.h:509
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4760
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:4854
Var * paramval
Definition: plannodes.h:695
Join join
Definition: plannodes.h:687
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:728
List * joinrestrictinfo
Definition: relation.h:1299
RelOptInfo * parent
Definition: relation.h:953
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:5369
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:668
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:951
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:6378
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4854
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:947
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:954
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:4854
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:6357
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:959
bool tlist_same_exprs(List *tlist1, List *tlist2)
Definition: tlist.c:221
List * targetlist
Definition: plannodes.h:144
int width
Definition: relation.h:886
bool is_projection_capable_path(Path *path)
Definition: createplan.c:6535
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:947
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:5287
#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:4854
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:947
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:4760
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4854
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:6357
#define NULL
Definition: c.h:229
List * quals
Definition: relation.h:1214
Definition: pg_list.h:45
Definition: relation.h:947
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:4202
ParamPathInfo * param_info
Definition: relation.h:956
Definition: nodes.h:509
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4760
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4854
#define planner_rt_fetch(rti, root)
Definition: relation.h:325
Scan scan
Definition: plannodes.h:344
RelOptInfo * parent
Definition: relation.h:953
Index relid
Definition: relation.h:553
static SampleScan * make_samplescan(List *qptlist, List *qpqual, Index scanrelid, TableSampleClause *tsc)
Definition: createplan.c:4962
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:929
Definition: pg_list.h:45
struct TableSampleClause * tablesample
Definition: parsenodes.h:942
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  (SubqueryScanPath *) best_path,
643  tlist,
644  scan_clauses);<