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, Oid skewColType, int32 skewColTypmod)
 
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 4925 of file createplan.c.

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

Referenced by create_bitmap_scan_plan().

4926 {
4927  if (IsA(plan, BitmapAnd))
4929  linitial(((BitmapAnd *) plan)->bitmapplans));
4930  else if (IsA(plan, BitmapOr))
4931  ((BitmapOr *) plan)->isshared = true;
4932  else if (IsA(plan, BitmapIndexScan))
4933  ((BitmapIndexScan *) plan)->isshared = true;
4934  else
4935  elog(ERROR, "unrecognized node type: %d", nodeTag(plan));
4936 }
#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:4925
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 730 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().

731 {
732  List *tlist = NIL;
733  Index *sortgrouprefs = path->pathtarget->sortgrouprefs;
734  int resno = 1;
735  ListCell *v;
736 
737  foreach(v, path->pathtarget->exprs)
738  {
739  Node *node = (Node *) lfirst(v);
740  TargetEntry *tle;
741 
742  /*
743  * If it's a parameterized path, there might be lateral references in
744  * the tlist, which need to be replaced with Params. There's no need
745  * to remake the TargetEntry nodes, so apply this to each list item
746  * separately.
747  */
748  if (path->param_info)
749  node = replace_nestloop_params(root, node);
750 
751  tle = makeTargetEntry((Expr *) node,
752  resno,
753  NULL,
754  false);
755  if (sortgrouprefs)
756  tle->ressortgroupref = sortgrouprefs[resno - 1];
757 
758  tlist = lappend(tlist, tle);
759  resno++;
760  }
761  return tlist;
762 }
#define NIL
Definition: pg_list.h:69
PathTarget * pathtarget
Definition: relation.h:953
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4210
ParamPathInfo * param_info
Definition: relation.h:955
Definition: nodes.h:509
Index * sortgrouprefs
Definition: relation.h:883
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:882
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 4862 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().

4863 {
4864  dest->startup_cost = src->startup_cost;
4865  dest->total_cost = src->total_cost;
4866  dest->plan_rows = src->rows;
4867  dest->plan_width = src->pathtarget->width;
4868  dest->parallel_aware = src->parallel_aware;
4869  dest->parallel_safe = src->parallel_safe;
4870 }
double plan_rows
Definition: plannodes.h:121
PathTarget * pathtarget
Definition: relation.h:953
Cost startup_cost
Definition: relation.h:964
Cost startup_cost
Definition: plannodes.h:115
bool parallel_aware
Definition: plannodes.h:127
Cost total_cost
Definition: relation.h:965
int plan_width
Definition: plannodes.h:122
double rows
Definition: relation.h:963
bool parallel_safe
Definition: relation.h:958
int width
Definition: relation.h:885
Cost total_cost
Definition: plannodes.h:116
bool parallel_aware
Definition: relation.h:957
bool parallel_safe
Definition: plannodes.h:128
static void copy_plan_costsize ( Plan dest,
Plan src 
)
static

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

4878 {
4879  dest->startup_cost = src->startup_cost;
4880  dest->total_cost = src->total_cost;
4881  dest->plan_rows = src->plan_rows;
4882  dest->plan_width = src->plan_width;
4883  /* Assume the inserted node is not parallel-aware. */
4884  dest->parallel_aware = false;
4885  /* Assume the inserted node is parallel-safe, if child plan is. */
4886  dest->parallel_safe = src->parallel_safe;
4887 }
double plan_rows
Definition: plannodes.h:121
Cost startup_cost
Definition: plannodes.h:115
bool parallel_aware
Definition: plannodes.h:127
int plan_width
Definition: plannodes.h:122
Cost total_cost
Definition: plannodes.h:116
bool parallel_safe
Definition: plannodes.h:128
static Agg * create_agg_plan ( PlannerInfo root,
AggPath best_path 
)
static

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

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

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

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

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

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

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

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

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

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

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

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

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

3108 {
3109  FunctionScan *scan_plan;
3110  Index scan_relid = best_path->parent->relid;
3111  RangeTblEntry *rte;
3112  List *functions;
3113 
3114  /* it should be a function base rel... */
3115  Assert(scan_relid > 0);
3116  rte = planner_rt_fetch(scan_relid, root);
3117  Assert(rte->rtekind == RTE_FUNCTION);
3118  functions = rte->functions;
3119 
3120  /* Sort clauses into best execution order */
3121  scan_clauses = order_qual_clauses(root, scan_clauses);
3122 
3123  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3124  scan_clauses = extract_actual_clauses(scan_clauses, false);
3125 
3126  /* Replace any outer-relation variables with nestloop params */
3127  if (best_path->param_info)
3128  {
3129  scan_clauses = (List *)
3130  replace_nestloop_params(root, (Node *) scan_clauses);
3131  /* The function expressions could contain nestloop params, too */
3132  functions = (List *) replace_nestloop_params(root, (Node *) functions);
3133  }
3134 
3135  scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
3136  functions, rte->funcordinality);
3137 
3138  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3139 
3140  return scan_plan;
3141 }
Plan plan
Definition: plannodes.h:317
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4210
ParamPathInfo * param_info
Definition: relation.h:955
Definition: nodes.h:509
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4768
bool funcordinality
Definition: parsenodes.h:978
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4862
#define planner_rt_fetch(rti, root)
Definition: relation.h:324
RelOptInfo * parent
Definition: relation.h:952
Index relid
Definition: relation.h:552
unsigned int Index
Definition: c.h:365
static FunctionScan * make_functionscan(List *qptlist, List *qpqual, Index scanrelid, List *functions, bool funcordinality)
Definition: createplan.c:5126
#define Assert(condition)
Definition: c.h:675
List * functions
Definition: parsenodes.h:977
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:928
Definition: pg_list.h:45
static GatherMerge * create_gather_merge_plan ( PlannerInfo root,
GatherMergePath best_path 
)
static

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

1495 {
1496  GatherMerge *gm_plan;
1497  Plan *subplan;
1498  List *pathkeys = best_path->path.pathkeys;
1499  List *tlist = build_path_tlist(root, &best_path->path);
1500 
1501  /* As with Gather, it's best to project away columns in the workers. */
1502  subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1503 
1504  /* Create a shell for a GatherMerge plan. */
1505  gm_plan = makeNode(GatherMerge);
1506  gm_plan->plan.targetlist = tlist;
1507  gm_plan->num_workers = best_path->num_workers;
1508  copy_generic_path_info(&gm_plan->plan, &best_path->path);
1509 
1510  /* Gather Merge is pointless with no pathkeys; use Gather instead. */
1511  Assert(pathkeys != NIL);
1512 
1513  /* Compute sort column info, and adjust subplan's tlist as needed */
1514  subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1515  best_path->subpath->parent->relids,
1516  gm_plan->sortColIdx,
1517  false,
1518  &gm_plan->numCols,
1519  &gm_plan->sortColIdx,
1520  &gm_plan->sortOperators,
1521  &gm_plan->collations,
1522  &gm_plan->nullsFirst);
1523 
1524 
1525  /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1526  if (!pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys))
1527  subplan = (Plan *) make_sort(subplan, gm_plan->numCols,
1528  gm_plan->sortColIdx,
1529  gm_plan->sortOperators,
1530  gm_plan->collations,
1531  gm_plan->nullsFirst);
1532 
1533  /* Now insert the subplan under GatherMerge. */
1534  gm_plan->plan.lefttree = subplan;
1535 
1536  /* use parallel mode for parallel plans. */
1537  root->glob->parallelModeNeeded = true;
1538 
1539  return gm_plan;
1540 }
#define NIL
Definition: pg_list.h:69
Oid * collations
Definition: plannodes.h:841
AttrNumber * sortColIdx
Definition: plannodes.h:839
bool * nullsFirst
Definition: plannodes.h:842
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:356
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4862
bool parallelModeNeeded
Definition: relation.h:130
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:730
RelOptInfo * parent
Definition: relation.h:952
PlannerGlobal * glob
Definition: relation.h:156
Relids relids
Definition: relation.h:524
Oid * sortOperators
Definition: plannodes.h:840
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:317
List * pathkeys
Definition: relation.h:967
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
Definition: createplan.c:5493
#define makeNode(_type_)
Definition: nodes.h:557
#define Assert(condition)
Definition: c.h:675
struct Plan * lefttree
Definition: plannodes.h:136
List * targetlist
Definition: plannodes.h:134
Path * subpath
Definition: relation.h:1277
int num_workers
Definition: plannodes.h:836
#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:5554
static Gather * create_gather_plan ( PlannerInfo root,
GatherPath best_path 
)
static

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

1460 {
1461  Gather *gather_plan;
1462  Plan *subplan;
1463  List *tlist;
1464 
1465  /*
1466  * Although the Gather node can project, we prefer to push down such work
1467  * to its child node, so demand an exact tlist from the child.
1468  */
1469  subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1470 
1471  tlist = build_path_tlist(root, &best_path->path);
1472 
1473  gather_plan = make_gather(tlist,
1474  NIL,
1475  best_path->num_workers,
1476  best_path->single_copy,
1477  subplan);
1478 
1479  copy_generic_path_info(&gather_plan->plan, &best_path->path);
1480 
1481  /* use parallel mode for parallel plans. */
1482  root->glob->parallelModeNeeded = true;
1483 
1484  return gather_plan;
1485 }
#define NIL
Definition: pg_list.h:69
bool single_copy
Definition: relation.h:1265
static Gather * make_gather(List *qptlist, List *qpqual, int nworkers, bool single_copy, Plan *subplan)
Definition: createplan.c:6248
int num_workers
Definition: relation.h:1266
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:356
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4862
bool parallelModeNeeded
Definition: relation.h:130
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:730
Plan plan
Definition: plannodes.h:823
PlannerGlobal * glob
Definition: relation.h:156
Path * subpath
Definition: relation.h:1264
Path path
Definition: relation.h:1263
#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 902 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().

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

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

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

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

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

Definition at line 4054 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, Var::varno, Var::vartype, and Var::vartypmod.

Referenced by create_join_plan().

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1974 {
1975  Result *plan;
1976  List *tlist;
1977  ListCell *lc;
1978 
1979  /* Prepare an InitPlan for each aggregate's subquery. */
1980  foreach(lc, best_path->mmaggregates)
1981  {
1982  MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
1983  PlannerInfo *subroot = mminfo->subroot;
1984  Query *subparse = subroot->parse;
1985  Plan *plan;
1986 
1987  /*
1988  * Generate the plan for the subquery. We already have a Path, but we
1989  * have to convert it to a Plan and attach a LIMIT node above it.
1990  * Since we are entering a different planner context (subroot),
1991  * recurse to create_plan not create_plan_recurse.
1992  */
1993  plan = create_plan(subroot, mminfo->path);
1994 
1995  plan = (Plan *) make_limit(plan,
1996  subparse->limitOffset,
1997  subparse->limitCount);
1998 
1999  /* Must apply correct cost/width data to Limit node */
2000  plan->startup_cost = mminfo->path->startup_cost;
2001  plan->total_cost = mminfo->pathcost;
2002  plan->plan_rows = 1;
2003  plan->plan_width = mminfo->path->pathtarget->width;
2004  plan->parallel_aware = false;
2005  plan->parallel_safe = mminfo->path->parallel_safe;
2006 
2007  /* Convert the plan into an InitPlan in the outer query. */
2008  SS_make_initplan_from_plan(root, subroot, plan, mminfo->param);
2009  }
2010 
2011  /* Generate the output plan --- basically just a Result */
2012  tlist = build_path_tlist(root, &best_path->path);
2013 
2014  plan = make_result(tlist, (Node *) best_path->quals, NULL);
2015 
2016  copy_generic_path_info(&plan->plan, (Path *) best_path);
2017 
2018  /*
2019  * During setrefs.c, we'll need to replace references to the Agg nodes
2020  * with InitPlan output params. (We can't just do that locally in the
2021  * MinMaxAgg node, because path nodes above here may have Agg references
2022  * as well.) Save the mmaggregates list to tell setrefs.c to do that.
2023  *
2024  * This doesn't work if we're in an inheritance subtree (see notes in
2025  * create_modifytable_plan). Fortunately we can't be because there would
2026  * never be aggregates in an UPDATE/DELETE; but let's Assert that.
2027  */
2028  Assert(!root->hasInheritedTarget);
2029  Assert(root->minmax_aggs == NIL);
2030  root->minmax_aggs = best_path->mmaggregates;
2031 
2032  return plan;
2033 }
Node * limitOffset
Definition: parsenodes.h:158
#define NIL
Definition: pg_list.h:69
double plan_rows
Definition: plannodes.h:121
PathTarget * pathtarget
Definition: relation.h:953
Query * parse
Definition: relation.h:154
Param * param
Definition: relation.h:2084
Definition: nodes.h:509
List * minmax_aggs
Definition: relation.h:284
List * quals
Definition: relation.h:1509
Plan * create_plan(PlannerInfo *root, Path *best_path)
Definition: createplan.c:305
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4862
Path * path
Definition: relation.h:2082
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:730
Cost startup_cost
Definition: relation.h:964
Node * limitCount
Definition: parsenodes.h:159
Cost startup_cost
Definition: plannodes.h:115
bool parallel_aware
Definition: plannodes.h:127
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6369
List * mmaggregates
Definition: relation.h:1508
int plan_width
Definition: plannodes.h:122
#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:958
bool hasInheritedTarget
Definition: relation.h:296
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:6348
int width
Definition: relation.h:885
Cost total_cost
Definition: plannodes.h:116
bool parallel_safe
Definition: plannodes.h:128
Definition: pg_list.h:45
Definition: relation.h:946
PlannerInfo * subroot
Definition: relation.h:2081
static ModifyTable * create_modifytable_plan ( PlannerInfo root,
ModifyTablePath best_path 
)
static

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

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

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

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

3645 {
3646  NestLoop *join_plan;
3647  Plan *outer_plan;
3648  Plan *inner_plan;
3649  List *tlist = build_path_tlist(root, &best_path->path);
3650  List *joinrestrictclauses = best_path->joinrestrictinfo;
3651  List *joinclauses;
3652  List *otherclauses;
3653  Relids outerrelids;
3654  List *nestParams;
3655  Relids saveOuterRels = root->curOuterRels;
3656  ListCell *cell;
3657  ListCell *prev;
3658  ListCell *next;
3659 
3660  /* NestLoop can project, so no need to be picky about child tlists */
3661  outer_plan = create_plan_recurse(root, best_path->outerjoinpath, 0);
3662 
3663  /* For a nestloop, include outer relids in curOuterRels for inner side */
3664  root->curOuterRels = bms_union(root->curOuterRels,
3665  best_path->outerjoinpath->parent->relids);
3666 
3667  inner_plan = create_plan_recurse(root, best_path->innerjoinpath, 0);
3668 
3669  /* Restore curOuterRels */
3670  bms_free(root->curOuterRels);
3671  root->curOuterRels = saveOuterRels;
3672 
3673  /* Sort join qual clauses into best execution order */
3674  joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
3675 
3676  /* Get the join qual clauses (in plain expression form) */
3677  /* Any pseudoconstant clauses are ignored here */
3678  if (IS_OUTER_JOIN(best_path->jointype))
3679  {
3680  extract_actual_join_clauses(joinrestrictclauses,
3681  &joinclauses, &otherclauses);
3682  }
3683  else
3684  {
3685  /* We can treat all clauses alike for an inner join */
3686  joinclauses = extract_actual_clauses(joinrestrictclauses, false);
3687  otherclauses = NIL;
3688  }
3689 
3690  /* Replace any outer-relation variables with nestloop params */
3691  if (best_path->path.param_info)
3692  {
3693  joinclauses = (List *)
3694  replace_nestloop_params(root, (Node *) joinclauses);
3695  otherclauses = (List *)
3696  replace_nestloop_params(root, (Node *) otherclauses);
3697  }
3698 
3699  /*
3700  * Identify any nestloop parameters that should be supplied by this join
3701  * node, and move them from root->curOuterParams to the nestParams list.
3702  */
3703  outerrelids = best_path->outerjoinpath->parent->relids;
3704  nestParams = NIL;
3705  prev = NULL;
3706  for (cell = list_head(root->curOuterParams); cell; cell = next)
3707  {
3708  NestLoopParam *nlp = (NestLoopParam *) lfirst(cell);
3709 
3710  next = lnext(cell);
3711  if (IsA(nlp->paramval, Var) &&
3712  bms_is_member(nlp->paramval->varno, outerrelids))
3713  {
3715  cell, prev);
3716  nestParams = lappend(nestParams, nlp);
3717  }
3718  else if (IsA(nlp->paramval, PlaceHolderVar) &&
3719  bms_overlap(((PlaceHolderVar *) nlp->paramval)->phrels,
3720  outerrelids) &&
3722  (PlaceHolderVar *) nlp->paramval,
3723  false)->ph_eval_at,
3724  outerrelids))
3725  {
3727  cell, prev);
3728  nestParams = lappend(nestParams, nlp);
3729  }
3730  else
3731  prev = cell;
3732  }
3733 
3734  join_plan = make_nestloop(tlist,
3735  joinclauses,
3736  otherclauses,
3737  nestParams,
3738  outer_plan,
3739  inner_plan,
3740  best_path->jointype,
3741  best_path->inner_unique);
3742 
3743  copy_generic_path_info(&join_plan->join.plan, &best_path->path);
3744 
3745  return join_plan;
3746 }
#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:4210
Relids ph_eval_at
Definition: relation.h:2063
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:311
Path * innerjoinpath
Definition: relation.h:1296
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:721
ParamPathInfo * param_info
Definition: relation.h:955
Definition: nodes.h:509
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4768
Definition: primnodes.h:163
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:356
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4862
Var * paramval
Definition: plannodes.h:684
Join join
Definition: plannodes.h:676
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:730
List * joinrestrictinfo
Definition: relation.h:1298
RelOptInfo * parent
Definition: relation.h:952
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:5377
List * curOuterParams
Definition: relation.h:312
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:524
#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:1295
void bms_free(Bitmapset *a)
Definition: bitmapset.c:201
#define NULL
Definition: c.h:229
Path path
Definition: relation.h:1288
#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:1292
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
JoinType jointype
Definition: relation.h:1290
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
Plan plan
Definition: plannodes.h:657
Plan* create_plan ( PlannerInfo root,
Path best_path 
)

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

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

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

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

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

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

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

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

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

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

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

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

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

2453 {
2454  SampleScan *scan_plan;
2455  Index scan_relid = best_path->parent->relid;
2456  RangeTblEntry *rte;
2457  TableSampleClause *tsc;
2458 
2459  /* it should be a base rel with a tablesample clause... */
2460  Assert(scan_relid > 0);
2461  rte = planner_rt_fetch(scan_relid, root);
2462  Assert(rte->rtekind == RTE_RELATION);
2463  tsc = rte->tablesample;
2464  Assert(tsc != NULL);
2465 
2466  /* Sort clauses into best execution order */
2467  scan_clauses = order_qual_clauses(root, scan_clauses);
2468 
2469  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2470  scan_clauses = extract_actual_clauses(scan_clauses, false);
2471 
2472  /* Replace any outer-relation variables with nestloop params */
2473  if (best_path->param_info)
2474  {
2475  scan_clauses = (List *)
2476  replace_nestloop_params(root, (Node *) scan_clauses);
2477  tsc = (TableSampleClause *)
2478  replace_nestloop_params(root, (Node *) tsc);
2479  }
2480 
2481  scan_plan = make_samplescan(tlist,
2482  scan_clauses,
2483  scan_relid,
2484  tsc);
2485 
2486  copy_generic_path_info(&scan_plan->scan.plan, best_path);
2487 
2488  return scan_plan;
2489 }
Plan plan
Definition: plannodes.h:317
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4210
ParamPathInfo * param_info
Definition: relation.h:955
Definition: nodes.h:509
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4768
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4862
#define planner_rt_fetch(rti, root)
Definition: relation.h:324
Scan scan
Definition: plannodes.h:333
RelOptInfo * parent
Definition: relation.h:952
Index relid
Definition: relation.h:552
static SampleScan * make_samplescan(List *qptlist, List *qpqual, Index scanrelid, TableSampleClause *tsc)
Definition: createplan.c:4970
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:928
Definition: pg_list.h:45
struct TableSampleClause * tablesample
Definition: parsenodes.h:941
static Plan * create_scan_plan ( PlannerInfo root,
Path best_path,
int  flags 
)
static

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

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