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

Go to the source code of this file.

Macros

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

Functions

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

Macro Definition Documentation

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

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

Referenced by create_bitmap_scan_plan().

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

Definition at line 728 of file createplan.c.

References PathTarget::exprs, lappend(), lfirst, makeTargetEntry(), NIL, Path::param_info, Path::pathtarget, replace_nestloop_params(), TargetEntry::ressortgroupref, and PathTarget::sortgrouprefs.

Referenced by create_agg_plan(), create_append_plan(), create_gather_merge_plan(), create_gather_plan(), create_gating_plan(), create_group_plan(), create_groupingsets_plan(), create_hashjoin_plan(), create_merge_append_plan(), create_mergejoin_plan(), create_minmaxagg_plan(), create_nestloop_plan(), create_project_set_plan(), create_projection_plan(), create_recursiveunion_plan(), create_result_plan(), create_scan_plan(), create_unique_plan(), and create_windowagg_plan().

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

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

4861 {
4862  dest->startup_cost = src->startup_cost;
4863  dest->total_cost = src->total_cost;
4864  dest->plan_rows = src->rows;
4865  dest->plan_width = src->pathtarget->width;
4866  dest->parallel_aware = src->parallel_aware;
4867  dest->parallel_safe = src->parallel_safe;
4868 }
double plan_rows
Definition: plannodes.h:131
PathTarget * pathtarget
Definition: relation.h:1009
Cost startup_cost
Definition: relation.h:1019
Cost startup_cost
Definition: plannodes.h:125
bool parallel_aware
Definition: plannodes.h:137
Cost total_cost
Definition: relation.h:1020
int plan_width
Definition: plannodes.h:132
double rows
Definition: relation.h:1018
bool parallel_safe
Definition: relation.h:1014
int width
Definition: relation.h:941
Cost total_cost
Definition: plannodes.h:126
bool parallel_aware
Definition: relation.h:1013
bool parallel_safe
Definition: plannodes.h:138
static void copy_plan_costsize ( Plan dest,
Plan src 
)
static

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

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

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

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

Definition at line 1002 of file createplan.c.

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

Referenced by create_plan_recurse().

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

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

Definition at line 2810 of file createplan.c.

References BitmapAndPath::bitmapquals, BitmapOrPath::bitmapquals, BitmapAndPath::bitmapselectivity, BitmapOrPath::bitmapselectivity, castNode, clamp_row_est(), create_indexscan_plan(), elog, ERROR, get_actual_clauses(), IndexPath::indexclauses, IndexScan::indexid, IndexPath::indexinfo, IndexScan::indexqual, IndexScan::indexqualorig, IndexPath::indexquals, IndexPath::indexselectivity, IndexPath::indextotalcost, IndexOptInfo::indpred, IsA, lappend(), lfirst, linitial, list_concat(), list_concat_unique(), list_length(), list_make1, make_ands_explicit(), make_bitmap_and(), make_bitmap_indexscan(), make_bitmap_or(), make_orclause(), NIL, nodeTag, Plan::parallel_aware, Plan::parallel_safe, Path::parallel_safe, Path::parent, RestrictInfo::parent_ec, IndexPath::path, BitmapAndPath::path, BitmapOrPath::path, Plan::plan_rows, Plan::plan_width, predicate_implied_by(), IndexScan::scan, Scan::scanrelid, Plan::startup_cost, Path::startup_cost, Plan::total_cost, Path::total_cost, and RelOptInfo::tuples.

Referenced by create_bitmap_scan_plan().

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

Definition at line 3240 of file createplan.c.

References Assert, copy_generic_path_info(), PlannerInfo::cte_plan_ids, RangeTblEntry::ctelevelsup, Query::cteList, RangeTblEntry::ctename, CommonTableExpr::ctename, elog, ERROR, extract_actual_clauses(), PlannerInfo::init_plans, lfirst, linitial_int, list_length(), list_nth_int(), make_ctescan(), order_qual_clauses(), Path::param_info, Path::parent, PlannerInfo::parent_root, PlannerInfo::parse, Scan::plan, SubPlan::plan_id, planner_rt_fetch, RelOptInfo::relid, replace_nestloop_params(), RTE_CTE, RangeTblEntry::rtekind, CteScan::scan, RangeTblEntry::self_reference, and SubPlan::setParam.

Referenced by create_scan_plan().

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

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

Definition at line 3432 of file createplan.c.

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

Referenced by create_scan_plan().

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

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

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

Definition at line 1493 of file createplan.c.

References Assert, build_path_tlist(), GatherMerge::collations, copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), PlannerInfo::glob, Plan::lefttree, make_sort(), makeNode, NIL, GatherMerge::nullsFirst, GatherMerge::num_workers, GatherMergePath::num_workers, GatherMerge::numCols, PlannerGlobal::parallelModeNeeded, Path::parent, GatherMergePath::path, Path::pathkeys, pathkeys_contained_in(), GatherMerge::plan, prepare_sort_from_pathkeys(), RelOptInfo::relids, GatherMerge::rescan_param, GatherMerge::sortColIdx, GatherMerge::sortOperators, SS_assign_special_param(), GatherMergePath::subpath, and Plan::targetlist.

Referenced by create_plan_recurse().

1494 {
1495  GatherMerge *gm_plan;
1496  Plan *subplan;
1497  List *pathkeys = best_path->path.pathkeys;
1498  List *tlist = build_path_tlist(root, &best_path->path);
1499 
1500  /* As with Gather, it's best to project away columns in the workers. */
1501  subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1502 
1503  /* Create a shell for a GatherMerge plan. */
1504  gm_plan = makeNode(GatherMerge);
1505  gm_plan->plan.targetlist = tlist;
1506  gm_plan->num_workers = best_path->num_workers;
1507  copy_generic_path_info(&gm_plan->plan, &best_path->path);
1508 
1509  /* Assign the rescan Param. */
1510  gm_plan->rescan_param = SS_assign_special_param(root);
1511 
1512  /* Gather Merge is pointless with no pathkeys; use Gather instead. */
1513  Assert(pathkeys != NIL);
1514 
1515  /* Compute sort column info, and adjust subplan's tlist as needed */
1516  subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1517  best_path->subpath->parent->relids,
1518  gm_plan->sortColIdx,
1519  false,
1520  &gm_plan->numCols,
1521  &gm_plan->sortColIdx,
1522  &gm_plan->sortOperators,
1523  &gm_plan->collations,
1524  &gm_plan->nullsFirst);
1525 
1526 
1527  /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1528  if (!pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys))
1529  subplan = (Plan *) make_sort(subplan, gm_plan->numCols,
1530  gm_plan->sortColIdx,
1531  gm_plan->sortOperators,
1532  gm_plan->collations,
1533  gm_plan->nullsFirst);
1534 
1535  /* Now insert the subplan under GatherMerge. */
1536  gm_plan->plan.lefttree = subplan;
1537 
1538  /* use parallel mode for parallel plans. */
1539  root->glob->parallelModeNeeded = true;
1540 
1541  return gm_plan;
1542 }
#define NIL
Definition: pg_list.h:69
Oid * collations
Definition: plannodes.h:859
int SS_assign_special_param(PlannerInfo *root)
Definition: subselect.c:416
AttrNumber * sortColIdx
Definition: plannodes.h:857
bool * nullsFirst
Definition: plannodes.h:860
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:354
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4860
bool parallelModeNeeded
Definition: relation.h:131
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:728
RelOptInfo * parent
Definition: relation.h:1008
PlannerGlobal * glob
Definition: relation.h:157
Relids relids
Definition: relation.h:571
Oid * sortOperators
Definition: plannodes.h:858
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:317
List * pathkeys
Definition: relation.h:1022
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
Definition: createplan.c:5487
#define makeNode(_type_)
Definition: nodes.h:557
#define Assert(condition)
Definition: c.h:664
struct Plan * lefttree
Definition: plannodes.h:146
List * targetlist
Definition: plannodes.h:144
Path * subpath
Definition: relation.h:1332
int num_workers
Definition: plannodes.h:853
int rescan_param
Definition: plannodes.h:854
#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:5548
static Gather * create_gather_plan ( PlannerInfo root,
GatherPath best_path 
)
static

Definition at line 1457 of file createplan.c.

References build_path_tlist(), copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), PlannerInfo::glob, make_gather(), NIL, GatherPath::num_workers, PlannerGlobal::parallelModeNeeded, GatherPath::path, Gather::plan, GatherPath::single_copy, SS_assign_special_param(), and GatherPath::subpath.

Referenced by create_plan_recurse().

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

Definition at line 900 of file createplan.c.

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

Referenced by create_join_plan(), and create_scan_plan().

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

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

1670 {
1671  Group *plan;
1672  Plan *subplan;
1673  List *tlist;
1674  List *quals;
1675 
1676  /*
1677  * Group can project, so no need to be terribly picky about child tlist,
1678  * but we do need grouping columns to be available
1679  */
1680  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1681 
1682  tlist = build_path_tlist(root, &best_path->path);
1683 
1684  quals = order_qual_clauses(root, best_path->qual);
1685 
1686  plan = make_group(tlist,
1687  quals,
1688  list_length(best_path->groupClause),
1690  subplan->targetlist),
1691  extract_grouping_ops(best_path->groupClause),
1692  subplan);
1693 
1694  copy_generic_path_info(&plan->plan, (Path *) best_path);
1695 
1696  return plan;
1697 }
List * qual
Definition: relation.h:1488
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4766
Oid * extract_grouping_ops(List *groupClause)
Definition: tlist.c:466
AttrNumber * extract_grouping_cols(List *groupClause, List *tlist)
Definition: tlist.c:492
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:354
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4860
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:728
Path path
Definition: relation.h:1485
static Group * make_group(List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Plan *lefttree)
Definition: createplan.c:6067
List * groupClause
Definition: relation.h:1487
#define CP_LABEL_TLIST
Definition: createplan.c:68
Path * subpath
Definition: relation.h:1486
Plan plan
Definition: plannodes.h:760
static int list_length(const List *l)
Definition: pg_list.h:89
List * targetlist
Definition: plannodes.h:144
Definition: pg_list.h:45
static Plan * create_groupingsets_plan ( PlannerInfo root,
GroupingSetsPath best_path 
)
static

Definition at line 1815 of file createplan.c.

References AGG_HASHED, AGG_PLAIN, AGG_SORTED, AGGSPLIT_SIMPLE, GroupingSetsPath::aggstrategy, Assert, build_path_tlist(), copy_generic_path_info(), CP_LABEL_TLIST, create_plan_recurse(), extract_grouping_ops(), for_each_cell, get_sortgroupclause_tle(), Query::groupClause, RollupData::groupClause, PlannerInfo::grouping_map, Query::groupingSets, RollupData::gsets, PlannerInfo::hasInheritedTarget, RollupData::is_hashed, lappend(), Plan::lefttree, lfirst, linitial, list_head(), list_length(), lnext, make_agg(), make_sort_from_groupcols(), NIL, RollupData::numGroups, palloc0(), PlannerInfo::parse, GroupingSetsPath::path, Agg::plan, GroupingSetsPath::qual, remap_groupColIdx(), TargetEntry::resno, GroupingSetsPath::rollups, GroupingSetsPath::subpath, Plan::targetlist, and SortGroupClause::tleSortGroupRef.

Referenced by create_plan_recurse().

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

Definition at line 4058 of file createplan.c.

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

Referenced by create_join_plan().

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

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

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

Definition at line 942 of file createplan.c.

References create_gating_plan(), create_hashjoin_plan(), create_mergejoin_plan(), create_nestloop_plan(), elog, ERROR, get_actual_clauses(), get_gating_quals(), JoinPath::joinrestrictinfo, list_concat(), NIL, JoinPath::path, Path::pathtype, T_HashJoin, T_MergeJoin, and T_NestLoop.

Referenced by create_plan_recurse().

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

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

2385 {
2386  Limit *plan;
2387  Plan *subplan;
2388 
2389  /* Limit doesn't project, so tlist requirements pass through */
2390  subplan = create_plan_recurse(root, best_path->subpath, flags);
2391 
2392  plan = make_limit(subplan,
2393  best_path->limitOffset,
2394  best_path->limitCount);
2395 
2396  copy_generic_path_info(&plan->plan, (Path *) best_path);
2397 
2398  return plan;
2399 }
Plan plan
Definition: plannodes.h:923
Node * limitOffset
Definition: relation.h:1653
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:354
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4860
Path * subpath
Definition: relation.h:1652
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
Definition: createplan.c:6344
Node * limitCount
Definition: relation.h:1654
static LockRows * create_lockrows_plan ( PlannerInfo root,
LockRowsPath best_path,
int  flags 
)
static

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

2304 {
2305  LockRows *plan;
2306  Plan *subplan;
2307 
2308  /* LockRows doesn't project, so tlist requirements pass through */
2309  subplan = create_plan_recurse(root, best_path->subpath, flags);
2310 
2311  plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
2312 
2313  copy_generic_path_info(&plan->plan, (Path *) best_path);
2314 
2315  return plan;
2316 }
Plan plan
Definition: plannodes.h:909
List * rowMarks
Definition: relation.h:1617
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:354
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4860
Path * subpath
Definition: relation.h:1616
static LockRows * make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
Definition: createplan.c:6323
static Material * create_material_plan ( PlannerInfo root,
MaterialPath best_path,
int  flags 
)
static

Definition at line 1226 of file createplan.c.

References copy_generic_path_info(), CP_SMALL_TLIST, create_plan_recurse(), make_material(), Material::plan, and MaterialPath::subpath.

Referenced by create_plan_recurse().

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

Definition at line 1067 of file createplan.c.

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

Referenced by create_plan_recurse().

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

Definition at line 3753 of file createplan.c.

References Assert, build_path_tlist(), copy_generic_path_info(), copy_plan_costsize(), CP_SMALL_TLIST, cpu_operator_cost, create_plan_recurse(), EquivalenceClass::ec_collation, elog, ERROR, extract_actual_clauses(), extract_actual_join_clauses(), get_actual_clauses(), get_switched_clauses(), i, JoinPath::inner_unique, JoinPath::innerjoinpath, MergePath::innersortkeys, IS_OUTER_JOIN, MergeJoin::join, JoinPath::joinrestrictinfo, JoinPath::jointype, MergePath::jpath, label_sort_with_costsize(), RestrictInfo::left_ec, lfirst, lfirst_node, list_difference(), list_head(), list_length(), lnext, make_material(), make_mergejoin(), make_sort_from_pathkeys(), MergePath::materialize_inner, NIL, order_qual_clauses(), RestrictInfo::outer_is_left, JoinPath::outerjoinpath, MergePath::outersortkeys, palloc(), Path::param_info, Path::parent, JoinPath::path, MergePath::path_mergeclauses, Path::pathkeys, PathKey::pk_eclass, PathKey::pk_nulls_first, PathKey::pk_opfamily, PathKey::pk_strategy, Join::plan, Plan::plan_rows, RelOptInfo::relids, replace_nestloop_params(), RestrictInfo::right_ec, MergePath::skip_mark_restore, sort(), and Plan::total_cost.

Referenced by create_join_plan().

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

Definition at line 1975 of file createplan.c.

References Assert, build_path_tlist(), copy_generic_path_info(), create_plan(), PlannerInfo::hasInheritedTarget, lfirst, Query::limitCount, Query::limitOffset, make_limit(), make_result(), PlannerInfo::minmax_aggs, MinMaxAggPath::mmaggregates, NIL, Plan::parallel_aware, Plan::parallel_safe, Path::parallel_safe, MinMaxAggInfo::param, PlannerInfo::parse, MinMaxAggPath::path, MinMaxAggInfo::path, MinMaxAggInfo::pathcost, Path::pathtarget, Result::plan, Plan::plan_rows, Plan::plan_width, MinMaxAggPath::quals, SS_make_initplan_from_plan(), Plan::startup_cost, Path::startup_cost, MinMaxAggInfo::subroot, Plan::total_cost, and PathTarget::width.

Referenced by create_plan_recurse().

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

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

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

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

3336 {
3337  NamedTuplestoreScan *scan_plan;
3338  Index scan_relid = best_path->parent->relid;
3339  RangeTblEntry *rte;
3340 
3341  Assert(scan_relid > 0);
3342  rte = planner_rt_fetch(scan_relid, root);
3344 
3345  /* Sort clauses into best execution order */
3346  scan_clauses = order_qual_clauses(root, scan_clauses);
3347 
3348  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3349  scan_clauses = extract_actual_clauses(scan_clauses, false);
3350 
3351  /* Replace any outer-relation variables with nestloop params */
3352  if (best_path->param_info)
3353  {
3354  scan_clauses = (List *)
3355  replace_nestloop_params(root, (Node *) scan_clauses);
3356  }
3357 
3358  scan_plan = make_namedtuplestorescan(tlist, scan_clauses, scan_relid,
3359  rte->enrname);
3360 
3361  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3362 
3363  return scan_plan;
3364 }
Plan plan
Definition: plannodes.h:328
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4208
ParamPathInfo * param_info
Definition: relation.h:1011
Definition: nodes.h:509
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4766
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4860
#define planner_rt_fetch(rti, root)
Definition: relation.h:328
RelOptInfo * parent
Definition: relation.h:1008
char * enrname
Definition: parsenodes.h:1042
Index relid
Definition: relation.h:599
static NamedTuplestoreScan * make_namedtuplestorescan(List *qptlist, List *qpqual, Index scanrelid, char *enrname)
Definition: createplan.c:5204
unsigned int Index
Definition: c.h:359
#define Assert(condition)
Definition: c.h:664
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:356
RTEKind rtekind
Definition: parsenodes.h:945
Definition: pg_list.h:45
static NestLoop * create_nestloop_plan ( PlannerInfo root,
NestPath best_path 
)
static

Definition at line 3647 of file createplan.c.

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

Referenced by create_join_plan().

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

Definition at line 303 of file createplan.c.

References apply_tlist_labeling(), Assert, CP_EXACT_TLIST, create_plan_recurse(), PlannerInfo::curOuterParams, PlannerInfo::curOuterRels, elog, ERROR, IsA, NIL, PlannerInfo::plan_params, PlannerInfo::processed_tlist, SS_attach_initplans(), and Plan::targetlist.

Referenced by create_minmaxagg_plan(), create_subqueryscan_plan(), make_subplan(), SS_process_ctes(), and standard_planner().

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

Definition at line 354 of file createplan.c.

References Assert, create_agg_plan(), create_append_plan(), create_gather_merge_plan(), create_gather_plan(), create_group_plan(), create_groupingsets_plan(), create_join_plan(), create_limit_plan(), create_lockrows_plan(), create_material_plan(), create_merge_append_plan(), create_minmaxagg_plan(), create_modifytable_plan(), create_project_set_plan(), create_projection_plan(), create_recursiveunion_plan(), create_result_plan(), create_scan_plan(), create_setop_plan(), create_sort_plan(), create_unique_plan(), create_upper_unique_plan(), create_windowagg_plan(), elog, ERROR, IsA, Path::pathtype, T_Agg, T_Append, T_BitmapHeapScan, T_CteScan, T_CustomScan, T_ForeignScan, T_FunctionScan, T_Gather, T_GatherMerge, T_Group, T_HashJoin, T_IndexOnlyScan, T_IndexScan, T_Limit, T_LockRows, T_Material, T_MergeAppend, T_MergeJoin, T_ModifyTable, T_NamedTuplestoreScan, T_NestLoop, T_ProjectSet, T_RecursiveUnion, T_Result, T_SampleScan, T_SeqScan, T_SetOp, T_Sort, T_SubqueryScan, T_TableFuncScan, T_TidScan, T_Unique, T_ValuesScan, T_WindowAgg, and T_WorkTableScan.

Referenced by create_agg_plan(), create_append_plan(), create_customscan_plan(), create_foreignscan_plan(), create_gather_merge_plan(), create_gather_plan(), create_group_plan(), create_groupingsets_plan(), create_hashjoin_plan(), create_limit_plan(), create_lockrows_plan(), create_material_plan(), create_merge_append_plan(), create_mergejoin_plan(), create_modifytable_plan(), create_nestloop_plan(), create_plan(), create_project_set_plan(), create_projection_plan(), create_recursiveunion_plan(), create_setop_plan(), create_sort_plan(), create_unique_plan(), create_upper_unique_plan(), and create_windowagg_plan().

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

Definition at line 1200 of file createplan.c.

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

Referenced by create_plan_recurse().

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

Definition at line 1552 of file createplan.c.

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

Referenced by create_plan_recurse().

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

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

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

Definition at line 1175 of file createplan.c.

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

Referenced by create_plan_recurse().

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

Definition at line 2453 of file createplan.c.

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

Referenced by create_scan_plan().

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

Definition at line 503 of file createplan.c.

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

Referenced by create_plan_recurse().

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