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)
 
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 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 CteScancreate_ctescan_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 CteScanmake_ctescan (List *qptlist, List *qpqual, Index scanrelid, int ctePlanId, int cteParam)
 
static WorkTableScanmake_worktablescan (List *qptlist, List *qpqual, Index scanrelid, int wtParam)
 
static Appendmake_append (List *appendplans, List *tlist)
 
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)
 
static HashJoinmake_hashjoin (List *tlist, List *joinclauses, List *otherclauses, List *hashclauses, Plan *lefttree, Plan *righttree, JoinType jointype)
 
static Hashmake_hash (Plan *lefttree, Oid skewTable, AttrNumber skewColumn, bool skewInherit, Oid skewColType, int32 skewColTypmod)
 
static MergeJoinmake_mergejoin (List *tlist, List *joinclauses, List *otherclauses, List *mergeclauses, Oid *mergefamilies, Oid *mergecollations, int *mergestrategies, bool *mergenullsfirst, Plan *lefttree, Plan *righttree, JoinType jointype)
 
static Sortmake_sort (Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
 
static Planprepare_sort_from_pathkeys (Plan *lefttree, List *pathkeys, Relids relids, const AttrNumber *reqColIdx, bool adjust_tlist_in_place, int *p_numsortkeys, AttrNumber **p_sortColIdx, Oid **p_sortOperators, Oid **p_collations, bool **p_nullsFirst)
 
static EquivalenceMemberfind_ec_member_for_tle (EquivalenceClass *ec, TargetEntry *tle, Relids relids)
 
static Sortmake_sort_from_pathkeys (Plan *lefttree, List *pathkeys)
 
static Sortmake_sort_from_groupcols (List *groupcls, AttrNumber *grpColIdx, Plan *lefttree)
 
static Materialmake_material (Plan *lefttree)
 
static WindowAggmake_windowagg (List *tlist, Index winref, int partNumCols, AttrNumber *partColIdx, Oid *partOperators, int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators, int frameOptions, Node *startOffset, Node *endOffset, Plan *lefttree)
 
static Groupmake_group (List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Plan *lefttree)
 
static Uniquemake_unique_from_sortclauses (Plan *lefttree, List *distinctList)
 
static Uniquemake_unique_from_pathkeys (Plan *lefttree, List *pathkeys, int numCols)
 
static Gathermake_gather (List *qptlist, List *qpqual, int nworkers, bool single_copy, Plan *subplan)
 
static SetOpmake_setop (SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree, List *distinctList, AttrNumber flagColIdx, int firstFlag, long numGroups)
 
static LockRowsmake_lockrows (Plan *lefttree, List *rowMarks, int epqParam)
 
static Resultmake_result (List *tlist, Node *resconstantqual, Plan *subplan)
 
static ProjectSetmake_project_set (List *tlist, Plan *subplan)
 
static ModifyTablemake_modifytable (PlannerInfo *root, CmdType operation, bool canSetTag, Index nominalRelation, List *resultRelations, List *subplans, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, int epqParam)
 
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 List * build_path_tlist ( PlannerInfo root,
Path path 
)
static

Definition at line 698 of file createplan.c.

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

Referenced by create_agg_plan(), create_append_plan(), create_gather_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().

699 {
700  List *tlist = NIL;
701  Index *sortgrouprefs = path->pathtarget->sortgrouprefs;
702  int resno = 1;
703  ListCell *v;
704 
705  foreach(v, path->pathtarget->exprs)
706  {
707  Node *node = (Node *) lfirst(v);
708  TargetEntry *tle;
709 
710  /*
711  * If it's a parameterized path, there might be lateral references in
712  * the tlist, which need to be replaced with Params. There's no need
713  * to remake the TargetEntry nodes, so apply this to each list item
714  * separately.
715  */
716  if (path->param_info)
717  node = replace_nestloop_params(root, node);
718 
719  tle = makeTargetEntry((Expr *) node,
720  resno,
721  NULL,
722  false);
723  if (sortgrouprefs)
724  tle->ressortgroupref = sortgrouprefs[resno - 1];
725 
726  tlist = lappend(tlist, tle);
727  resno++;
728  }
729  return tlist;
730 }
#define NIL
Definition: pg_list.h:69
PathTarget * pathtarget
Definition: relation.h:895
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:3998
ParamPathInfo * param_info
Definition: relation.h:897
Definition: nodes.h:508
Index * sortgrouprefs
Definition: relation.h:825
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:824
unsigned int Index
Definition: c.h:362
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
Index ressortgroupref
Definition: primnodes.h:1333
Definition: pg_list.h:45
static void copy_generic_path_info ( Plan dest,
Path src 
)
static

Definition at line 4650 of file createplan.c.

References Plan::parallel_aware, Path::parallel_aware, 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_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_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_tidscan_plan(), create_unique_plan(), create_upper_unique_plan(), create_valuesscan_plan(), create_windowagg_plan(), and create_worktablescan_plan().

4651 {
4652  dest->startup_cost = src->startup_cost;
4653  dest->total_cost = src->total_cost;
4654  dest->plan_rows = src->rows;
4655  dest->plan_width = src->pathtarget->width;
4656  dest->parallel_aware = src->parallel_aware;
4657 }
double plan_rows
Definition: plannodes.h:117
PathTarget * pathtarget
Definition: relation.h:895
Cost startup_cost
Definition: relation.h:906
Cost startup_cost
Definition: plannodes.h:111
bool parallel_aware
Definition: plannodes.h:123
Cost total_cost
Definition: relation.h:907
int plan_width
Definition: plannodes.h:118
double rows
Definition: relation.h:905
int width
Definition: relation.h:827
Cost total_cost
Definition: plannodes.h:112
bool parallel_aware
Definition: relation.h:899
static void copy_plan_costsize ( Plan dest,
Plan src 
)
static

Definition at line 4664 of file createplan.c.

References Plan::parallel_aware, 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().

4665 {
4666  dest->startup_cost = src->startup_cost;
4667  dest->total_cost = src->total_cost;
4668  dest->plan_rows = src->plan_rows;
4669  dest->plan_width = src->plan_width;
4670  /* Assume the inserted node is not parallel-aware. */
4671  dest->parallel_aware = false;
4672 }
double plan_rows
Definition: plannodes.h:117
Cost startup_cost
Definition: plannodes.h:111
bool parallel_aware
Definition: plannodes.h:123
int plan_width
Definition: plannodes.h:118
Cost total_cost
Definition: plannodes.h:112
static Agg * create_agg_plan ( PlannerInfo root,
AggPath best_path 
)
static

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

1626 {
1627  Agg *plan;
1628  Plan *subplan;
1629  List *tlist;
1630  List *quals;
1631 
1632  /*
1633  * Agg can project, so no need to be terribly picky about child tlist, but
1634  * we do need grouping columns to be available
1635  */
1636  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1637 
1638  tlist = build_path_tlist(root, &best_path->path);
1639 
1640  quals = order_qual_clauses(root, best_path->qual);
1641 
1642  plan = make_agg(tlist, quals,
1643  best_path->aggstrategy,
1644  best_path->aggsplit,
1645  list_length(best_path->groupClause),
1647  subplan->targetlist),
1648  extract_grouping_ops(best_path->groupClause),
1649  NIL,
1650  NIL,
1651  best_path->numGroups,
1652  subplan);
1653 
1654  copy_generic_path_info(&plan->plan, (Path *) best_path);
1655 
1656  return plan;
1657 }
#define NIL
Definition: pg_list.h:69
AggStrategy aggstrategy
Definition: relation.h:1373
List * qual
Definition: relation.h:1377
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4556
AggSplit aggsplit
Definition: relation.h:1374
Oid * extract_grouping_ops(List *groupClause)
Definition: tlist.c:466
double numGroups
Definition: relation.h:1375
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:5727
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:698
List * groupClause
Definition: relation.h:1376
Plan plan
Definition: plannodes.h:732
#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:1372
List * targetlist
Definition: plannodes.h:129
Definition: plannodes.h:730
Definition: pg_list.h:45
Path path
Definition: relation.h:1371
Definition: relation.h:888
static Plan * create_append_plan ( PlannerInfo root,
AppendPath best_path 
)
static

Definition at line 959 of file createplan.c.

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

Referenced by create_plan_recurse().

960 {
961  Append *plan;
962  List *tlist = build_path_tlist(root, &best_path->path);
963  List *subplans = NIL;
964  ListCell *subpaths;
965 
966  /*
967  * The subpaths list could be empty, if every child was proven empty by
968  * constraint exclusion. In that case generate a dummy plan that returns
969  * no rows.
970  *
971  * Note that an AppendPath with no members is also generated in certain
972  * cases where there was no appending construct at all, but we know the
973  * relation is empty (see set_dummy_rel_pathlist).
974  */
975  if (best_path->subpaths == NIL)
976  {
977  /* Generate a Result plan with constant-FALSE gating qual */
978  Plan *plan;
979 
980  plan = (Plan *) make_result(tlist,
981  (Node *) list_make1(makeBoolConst(false,
982  false)),
983  NULL);
984 
985  copy_generic_path_info(plan, (Path *) best_path);
986 
987  return plan;
988  }
989 
990  /* Build the plan for each child */
991  foreach(subpaths, best_path->subpaths)
992  {
993  Path *subpath = (Path *) lfirst(subpaths);
994  Plan *subplan;
995 
996  /* Must insist that all children return the same tlist */
997  subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
998 
999  subplans = lappend(subplans, subplan);
1000  }
1001 
1002  /*
1003  * XXX ideally, if there's just one child, we'd not bother to generate an
1004  * Append node but just return the single child. At the moment this does
1005  * not work because the varno of the child scan plan won't match the
1006  * parent-rel Vars it'll be asked to emit.
1007  */
1008 
1009  plan = make_append(subplans, tlist);
1010 
1011  copy_generic_path_info(&plan->plan, (Path *) best_path);
1012 
1013  return (Plan *) plan;
1014 }
#define NIL
Definition: pg_list.h:69
Definition: nodes.h:508
Path path
Definition: relation.h:1118
#define list_make1(x1)
Definition: pg_list.h:133
List * subpaths
Definition: relation.h:1119
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:698
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:354
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6085
List * lappend(List *list, void *datum)
Definition: list.c:128
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
#define CP_EXACT_TLIST
Definition: createplan.c:66
Definition: pg_list.h:45
Definition: relation.h:888
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:234
static Append * make_append(List *appendplans, List *tlist)
Definition: createplan.c:5006
static BitmapHeapScan * create_bitmap_scan_plan ( PlannerInfo root,
BitmapHeapPath best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 2557 of file createplan.c.

References Assert, BitmapHeapPath::bitmapqual, castNode, RestrictInfo::clause, contain_mutable_functions(), copy_generic_path_info(), create_bitmap_subplan(), extract_actual_clauses(), lappend(), lfirst, list_difference_ptr(), list_make1, list_member(), list_member_ptr(), make_bitmap_heapscan(), NIL, order_qual_clauses(), 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().

2561 {
2562  Index baserelid = best_path->path.parent->relid;
2563  Plan *bitmapqualplan;
2564  List *bitmapqualorig;
2565  List *indexquals;
2566  List *indexECs;
2567  List *qpqual;
2568  ListCell *l;
2569  BitmapHeapScan *scan_plan;
2570 
2571  /* it should be a base rel... */
2572  Assert(baserelid > 0);
2573  Assert(best_path->path.parent->rtekind == RTE_RELATION);
2574 
2575  /* Process the bitmapqual tree into a Plan tree and qual lists */
2576  bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
2577  &bitmapqualorig, &indexquals,
2578  &indexECs);
2579 
2580  /*
2581  * The qpqual list must contain all restrictions not automatically handled
2582  * by the index, other than pseudoconstant clauses which will be handled
2583  * by a separate gating plan node. All the predicates in the indexquals
2584  * will be checked (either by the index itself, or by
2585  * nodeBitmapHeapscan.c), but if there are any "special" operators
2586  * involved then they must be added to qpqual. The upshot is that qpqual
2587  * must contain scan_clauses minus whatever appears in indexquals.
2588  *
2589  * This loop is similar to the comparable code in create_indexscan_plan(),
2590  * but with some differences because it has to compare the scan clauses to
2591  * stripped (no RestrictInfos) indexquals. See comments there for more
2592  * info.
2593  *
2594  * In normal cases simple equal() checks will be enough to spot duplicate
2595  * clauses, so we try that first. We next see if the scan clause is
2596  * redundant with any top-level indexqual by virtue of being generated
2597  * from the same EC. After that, try predicate_implied_by().
2598  *
2599  * Unlike create_indexscan_plan(), the predicate_implied_by() test here is
2600  * useful for getting rid of qpquals that are implied by index predicates,
2601  * because the predicate conditions are included in the "indexquals"
2602  * returned by create_bitmap_subplan(). Bitmap scans have to do it that
2603  * way because predicate conditions need to be rechecked if the scan
2604  * becomes lossy, so they have to be included in bitmapqualorig.
2605  */
2606  qpqual = NIL;
2607  foreach(l, scan_clauses)
2608  {
2609  RestrictInfo *rinfo = castNode(RestrictInfo, lfirst(l));
2610  Node *clause = (Node *) rinfo->clause;
2611 
2612  if (rinfo->pseudoconstant)
2613  continue; /* we may drop pseudoconstants here */
2614  if (list_member(indexquals, clause))
2615  continue; /* simple duplicate */
2616  if (rinfo->parent_ec && list_member_ptr(indexECs, rinfo->parent_ec))
2617  continue; /* derived from same EquivalenceClass */
2618  if (!contain_mutable_functions(clause) &&
2619  predicate_implied_by(list_make1(clause), indexquals))
2620  continue; /* provably implied by indexquals */
2621  qpqual = lappend(qpqual, rinfo);
2622  }
2623 
2624  /* Sort clauses into best execution order */
2625  qpqual = order_qual_clauses(root, qpqual);
2626 
2627  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2628  qpqual = extract_actual_clauses(qpqual, false);
2629 
2630  /*
2631  * When dealing with special operators, we will at this point have
2632  * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
2633  * 'em from bitmapqualorig, since there's no point in making the tests
2634  * twice.
2635  */
2636  bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
2637 
2638  /*
2639  * We have to replace any outer-relation variables with nestloop params in
2640  * the qpqual and bitmapqualorig expressions. (This was already done for
2641  * expressions attached to plan nodes in the bitmapqualplan tree.)
2642  */
2643  if (best_path->path.param_info)
2644  {
2645  qpqual = (List *)
2646  replace_nestloop_params(root, (Node *) qpqual);
2647  bitmapqualorig = (List *)
2648  replace_nestloop_params(root, (Node *) bitmapqualorig);
2649  }
2650 
2651  /* Finally ready to build the plan node */
2652  scan_plan = make_bitmap_heapscan(tlist,
2653  qpqual,
2654  bitmapqualplan,
2655  bitmapqualorig,
2656  baserelid);
2657 
2658  copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
2659 
2660  return scan_plan;
2661 }
#define NIL
Definition: pg_list.h:69
Plan plan
Definition: plannodes.h:305
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:3998
bool predicate_implied_by(List *predicate_list, List *restrictinfo_list)
Definition: predtest.c:128
#define castNode(_type_, nodeptr)
Definition: nodes.h:577
List * list_difference_ptr(const List *list1, const List *list2)
Definition: list.c:884
bool pseudoconstant
Definition: relation.h:1645
ParamPathInfo * param_info
Definition: relation.h:897
Definition: nodes.h:508
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4556
static Plan * create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual, List **qual, List **indexqual, List **indexECs)
Definition: createplan.c:2684
#define list_make1(x1)
Definition: pg_list.h:133
static BitmapHeapScan * make_bitmap_heapscan(List *qptlist, List *qpqual, Plan *lefttree, List *bitmapqualorig, Index scanrelid)
Definition: createplan.c:4834
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
bool list_member(const List *list, const void *datum)
Definition: list.c:444
EquivalenceClass * parent_ec
Definition: relation.h:1671
RelOptInfo * parent
Definition: relation.h:894
Path * bitmapqual
Definition: relation.h:1003
Index relid
Definition: relation.h:518
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1637
unsigned int Index
Definition: c.h:362
RTEKind rtekind
Definition: relation.h:520
bool list_member_ptr(const List *list, const void *datum)
Definition: list.c:465
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:354
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:877
Definition: pg_list.h:45
static Plan * create_bitmap_subplan ( PlannerInfo root,
Path bitmapqual,
List **  qual,
List **  indexqual,
List **  indexECs 
)
static

Definition at line 2684 of file createplan.c.

References BitmapAndPath::bitmapquals, BitmapOrPath::bitmapquals, BitmapAndPath::bitmapselectivity, BitmapOrPath::bitmapselectivity, castNode, clamp_row_est(), create_indexscan_plan(), elog, ERROR, get_actual_clauses(), IndexPath::indexclauses, IndexScan::indexid, IndexPath::indexinfo, IndexScan::indexqual, IndexScan::indexqualorig, IndexPath::indexquals, IndexPath::indexselectivity, IndexPath::indextotalcost, IndexOptInfo::indpred, IsA, lappend(), lfirst, linitial, list_concat(), list_concat_unique(), list_length(), list_make1, make_ands_explicit(), make_bitmap_and(), make_bitmap_indexscan(), make_bitmap_or(), make_orclause(), NIL, nodeTag, NULL, Plan::parallel_aware, 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().

2686 {
2687  Plan *plan;
2688 
2689  if (IsA(bitmapqual, BitmapAndPath))
2690  {
2691  BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
2692  List *subplans = NIL;
2693  List *subquals = NIL;
2694  List *subindexquals = NIL;
2695  List *subindexECs = NIL;
2696  ListCell *l;
2697 
2698  /*
2699  * There may well be redundant quals among the subplans, since a
2700  * top-level WHERE qual might have gotten used to form several
2701  * different index quals. We don't try exceedingly hard to eliminate
2702  * redundancies, but we do eliminate obvious duplicates by using
2703  * list_concat_unique.
2704  */
2705  foreach(l, apath->bitmapquals)
2706  {
2707  Plan *subplan;
2708  List *subqual;
2709  List *subindexqual;
2710  List *subindexEC;
2711 
2712  subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
2713  &subqual, &subindexqual,
2714  &subindexEC);
2715  subplans = lappend(subplans, subplan);
2716  subquals = list_concat_unique(subquals, subqual);
2717  subindexquals = list_concat_unique(subindexquals, subindexqual);
2718  /* Duplicates in indexECs aren't worth getting rid of */
2719  subindexECs = list_concat(subindexECs, subindexEC);
2720  }
2721  plan = (Plan *) make_bitmap_and(subplans);
2722  plan->startup_cost = apath->path.startup_cost;
2723  plan->total_cost = apath->path.total_cost;
2724  plan->plan_rows =
2725  clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
2726  plan->plan_width = 0; /* meaningless */
2727  plan->parallel_aware = false;
2728  *qual = subquals;
2729  *indexqual = subindexquals;
2730  *indexECs = subindexECs;
2731  }
2732  else if (IsA(bitmapqual, BitmapOrPath))
2733  {
2734  BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
2735  List *subplans = NIL;
2736  List *subquals = NIL;
2737  List *subindexquals = NIL;
2738  bool const_true_subqual = false;
2739  bool const_true_subindexqual = false;
2740  ListCell *l;
2741 
2742  /*
2743  * Here, we only detect qual-free subplans. A qual-free subplan would
2744  * cause us to generate "... OR true ..." which we may as well reduce
2745  * to just "true". We do not try to eliminate redundant subclauses
2746  * because (a) it's not as likely as in the AND case, and (b) we might
2747  * well be working with hundreds or even thousands of OR conditions,
2748  * perhaps from a long IN list. The performance of list_append_unique
2749  * would be unacceptable.
2750  */
2751  foreach(l, opath->bitmapquals)
2752  {
2753  Plan *subplan;
2754  List *subqual;
2755  List *subindexqual;
2756  List *subindexEC;
2757 
2758  subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
2759  &subqual, &subindexqual,
2760  &subindexEC);
2761  subplans = lappend(subplans, subplan);
2762  if (subqual == NIL)
2763  const_true_subqual = true;
2764  else if (!const_true_subqual)
2765  subquals = lappend(subquals,
2766  make_ands_explicit(subqual));
2767  if (subindexqual == NIL)
2768  const_true_subindexqual = true;
2769  else if (!const_true_subindexqual)
2770  subindexquals = lappend(subindexquals,
2771  make_ands_explicit(subindexqual));
2772  }
2773 
2774  /*
2775  * In the presence of ScalarArrayOpExpr quals, we might have built
2776  * BitmapOrPaths with just one subpath; don't add an OR step.
2777  */
2778  if (list_length(subplans) == 1)
2779  {
2780  plan = (Plan *) linitial(subplans);
2781  }
2782  else
2783  {
2784  plan = (Plan *) make_bitmap_or(subplans);
2785  plan->startup_cost = opath->path.startup_cost;
2786  plan->total_cost = opath->path.total_cost;
2787  plan->plan_rows =
2788  clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
2789  plan->plan_width = 0; /* meaningless */
2790  plan->parallel_aware = false;
2791  }
2792 
2793  /*
2794  * If there were constant-TRUE subquals, the OR reduces to constant
2795  * TRUE. Also, avoid generating one-element ORs, which could happen
2796  * due to redundancy elimination or ScalarArrayOpExpr quals.
2797  */
2798  if (const_true_subqual)
2799  *qual = NIL;
2800  else if (list_length(subquals) <= 1)
2801  *qual = subquals;
2802  else
2803  *qual = list_make1(make_orclause(subquals));
2804  if (const_true_subindexqual)
2805  *indexqual = NIL;
2806  else if (list_length(subindexquals) <= 1)
2807  *indexqual = subindexquals;
2808  else
2809  *indexqual = list_make1(make_orclause(subindexquals));
2810  *indexECs = NIL;
2811  }
2812  else if (IsA(bitmapqual, IndexPath))
2813  {
2814  IndexPath *ipath = (IndexPath *) bitmapqual;
2815  IndexScan *iscan;
2816  List *subindexECs;
2817  ListCell *l;
2818 
2819  /* Use the regular indexscan plan build machinery... */
2820  iscan = castNode(IndexScan,
2821  create_indexscan_plan(root, ipath,
2822  NIL, NIL, false));
2823  /* then convert to a bitmap indexscan */
2824  plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
2825  iscan->indexid,
2826  iscan->indexqual,
2827  iscan->indexqualorig);
2828  /* and set its cost/width fields appropriately */
2829  plan->startup_cost = 0.0;
2830  plan->total_cost = ipath->indextotalcost;
2831  plan->plan_rows =
2832  clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
2833  plan->plan_width = 0; /* meaningless */
2834  plan->parallel_aware = false;
2835  *qual = get_actual_clauses(ipath->indexclauses);
2836  *indexqual = get_actual_clauses(ipath->indexquals);
2837  foreach(l, ipath->indexinfo->indpred)
2838  {
2839  Expr *pred = (Expr *) lfirst(l);
2840 
2841  /*
2842  * We know that the index predicate must have been implied by the
2843  * query condition as a whole, but it may or may not be implied by
2844  * the conditions that got pushed into the bitmapqual. Avoid
2845  * generating redundant conditions.
2846  */
2847  if (!predicate_implied_by(list_make1(pred), ipath->indexclauses))
2848  {
2849  *qual = lappend(*qual, pred);
2850  *indexqual = lappend(*indexqual, pred);
2851  }
2852  }
2853  subindexECs = NIL;
2854  foreach(l, ipath->indexquals)
2855  {
2856  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
2857 
2858  if (rinfo->parent_ec)
2859  subindexECs = lappend(subindexECs, rinfo->parent_ec);
2860  }
2861  *indexECs = subindexECs;
2862  }
2863  else
2864  {
2865  elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
2866  plan = NULL; /* keep compiler quiet */
2867  }
2868 
2869  return plan;
2870 }
#define NIL
Definition: pg_list.h:69
double plan_rows
Definition: plannodes.h:117
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
Path path
Definition: relation.h:971
IndexOptInfo * indexinfo
Definition: relation.h:972
Index scanrelid
Definition: plannodes.h:306
bool predicate_implied_by(List *predicate_list, List *restrictinfo_list)
Definition: predtest.c:128
#define castNode(_type_, nodeptr)
Definition: nodes.h:577
double tuples
Definition: relation.h:529
List * indexqualorig
Definition: plannodes.h:368
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:331
List * list_concat(List *list1, List *list2)
Definition: list.c:321
List * indexclauses
Definition: relation.h:973
static Plan * create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual, List **qual, List **indexqual, List **indexECs)
Definition: createplan.c:2684
Selectivity bitmapselectivity
Definition: relation.h:1016
static BitmapAnd * make_bitmap_and(List *bitmapplans)
Definition: createplan.c:5073
Oid indexid
Definition: plannodes.h:366
List * bitmapquals
Definition: relation.h:1015
List * bitmapquals
Definition: relation.h:1028
Expr * make_ands_explicit(List *andclauses)
Definition: clauses.c:366
#define list_make1(x1)
Definition: pg_list.h:133
List * indexquals
Definition: relation.h:974
#define linitial(l)
Definition: pg_list.h:110
#define ERROR
Definition: elog.h:43
Cost indextotalcost
Definition: relation.h:979
Cost startup_cost
Definition: relation.h:906
Scan scan
Definition: plannodes.h:365
EquivalenceClass * parent_ec
Definition: relation.h:1671
RelOptInfo * parent
Definition: relation.h:894
Selectivity indexselectivity
Definition: relation.h:980
Cost startup_cost
Definition: plannodes.h:111
bool parallel_aware
Definition: plannodes.h:123
Selectivity bitmapselectivity
Definition: relation.h:1029
List * indexqual
Definition: plannodes.h:367
List * lappend(List *list, void *datum)
Definition: list.c:128
static BitmapOr * make_bitmap_or(List *bitmapplans)
Definition: createplan.c:5088
Cost total_cost
Definition: relation.h:907
int plan_width
Definition: plannodes.h:118
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
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:4813
#define nodeTag(nodeptr)
Definition: nodes.h:513
Cost total_cost
Definition: plannodes.h:112
#define elog
Definition: elog.h:219
List * indpred
Definition: relation.h:611
double clamp_row_est(double nrows)
Definition: costsize.c:172
Definition: pg_list.h:45
Expr * make_orclause(List *orclauses)
Definition: clauses.c:292
static Scan * create_indexscan_plan(PlannerInfo *root, IndexPath *best_path, List *tlist, List *scan_clauses, bool indexonly)
Definition: createplan.c:2382
Definition: relation.h:888
static CteScan * create_ctescan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3067 of file createplan.c.

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

Referenced by create_scan_plan().

3069 {
3070  CteScan *scan_plan;
3071  Index scan_relid = best_path->parent->relid;
3072  RangeTblEntry *rte;
3073  SubPlan *ctesplan = NULL;
3074  int plan_id;
3075  int cte_param_id;
3076  PlannerInfo *cteroot;
3077  Index levelsup;
3078  int ndx;
3079  ListCell *lc;
3080 
3081  Assert(scan_relid > 0);
3082  rte = planner_rt_fetch(scan_relid, root);
3083  Assert(rte->rtekind == RTE_CTE);
3084  Assert(!rte->self_reference);
3085 
3086  /*
3087  * Find the referenced CTE, and locate the SubPlan previously made for it.
3088  */
3089  levelsup = rte->ctelevelsup;
3090  cteroot = root;
3091  while (levelsup-- > 0)
3092  {
3093  cteroot = cteroot->parent_root;
3094  if (!cteroot) /* shouldn't happen */
3095  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3096  }
3097 
3098  /*
3099  * Note: cte_plan_ids can be shorter than cteList, if we are still working
3100  * on planning the CTEs (ie, this is a side-reference from another CTE).
3101  * So we mustn't use forboth here.
3102  */
3103  ndx = 0;
3104  foreach(lc, cteroot->parse->cteList)
3105  {
3106  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
3107 
3108  if (strcmp(cte->ctename, rte->ctename) == 0)
3109  break;
3110  ndx++;
3111  }
3112  if (lc == NULL) /* shouldn't happen */
3113  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
3114  if (ndx >= list_length(cteroot->cte_plan_ids))
3115  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3116  plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
3117  Assert(plan_id > 0);
3118  foreach(lc, cteroot->init_plans)
3119  {
3120  ctesplan = (SubPlan *) lfirst(lc);
3121  if (ctesplan->plan_id == plan_id)
3122  break;
3123  }
3124  if (lc == NULL) /* shouldn't happen */
3125  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3126 
3127  /*
3128  * We need the CTE param ID, which is the sole member of the SubPlan's
3129  * setParam list.
3130  */
3131  cte_param_id = linitial_int(ctesplan->setParam);
3132 
3133  /* Sort clauses into best execution order */
3134  scan_clauses = order_qual_clauses(root, scan_clauses);
3135 
3136  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3137  scan_clauses = extract_actual_clauses(scan_clauses, false);
3138 
3139  /* Replace any outer-relation variables with nestloop params */
3140  if (best_path->param_info)
3141  {
3142  scan_clauses = (List *)
3143  replace_nestloop_params(root, (Node *) scan_clauses);
3144  }
3145 
3146  scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
3147  plan_id, cte_param_id);
3148 
3149  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3150 
3151  return scan_plan;
3152 }
Plan plan
Definition: plannodes.h:305
Query * parse
Definition: relation.h:152
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:3998
int plan_id
Definition: primnodes.h:666
ParamPathInfo * param_info
Definition: relation.h:897
Definition: nodes.h:508
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4556
#define linitial_int(l)
Definition: pg_list.h:111
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
#define planner_rt_fetch(rti, root)
Definition: relation.h:320
#define ERROR
Definition: elog.h:43
Scan scan
Definition: plannodes.h:504
RelOptInfo * parent
Definition: relation.h:894
Index relid
Definition: relation.h:518
struct PlannerInfo * parent_root
Definition: relation.h:158
static CteScan * make_ctescan(List *qptlist, List *qpqual, Index scanrelid, int ctePlanId, int cteParam)
Definition: createplan.c:4932
int list_nth_int(const List *list, int n)
Definition: list.c:421
List * cte_plan_ids
Definition: relation.h:227
bool self_reference
Definition: parsenodes.h:944
unsigned int Index
Definition: c.h:362
List * init_plans
Definition: relation.h:225
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
List * setParam
Definition: primnodes.h:683
static int list_length(const List *l)
Definition: pg_list.h:89
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:354
Index ctelevelsup
Definition: parsenodes.h:943
RTEKind rtekind
Definition: parsenodes.h:882
List * cteList
Definition: parsenodes.h:126
char * ctename
Definition: parsenodes.h:942
#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 3364 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().

3366 {
3367  CustomScan *cplan;
3368  RelOptInfo *rel = best_path->path.parent;
3369  List *custom_plans = NIL;
3370  ListCell *lc;
3371 
3372  /* Recursively transform child paths. */
3373  foreach(lc, best_path->custom_paths)
3374  {
3375  Plan *plan = create_plan_recurse(root, (Path *) lfirst(lc),
3376  CP_EXACT_TLIST);
3377 
3378  custom_plans = lappend(custom_plans, plan);
3379  }
3380 
3381  /*
3382  * Sort clauses into the best execution order, although custom-scan
3383  * provider can reorder them again.
3384  */
3385  scan_clauses = order_qual_clauses(root, scan_clauses);
3386 
3387  /*
3388  * Invoke custom plan provider to create the Plan node represented by the
3389  * CustomPath.
3390  */
3391  cplan = castNode(CustomScan,
3392  best_path->methods->PlanCustomPath(root,
3393  rel,
3394  best_path,
3395  tlist,
3396  scan_clauses,
3397  custom_plans));
3398 
3399  /*
3400  * Copy cost data from Path to Plan; no need to make custom-plan providers
3401  * do this
3402  */
3403  copy_generic_path_info(&cplan->scan.plan, &best_path->path);
3404 
3405  /* Likewise, copy the relids that are represented by this custom scan */
3406  cplan->custom_relids = best_path->path.parent->relids;
3407 
3408  /*
3409  * Replace any outer-relation variables with nestloop params in the qual
3410  * and custom_exprs expressions. We do this last so that the custom-plan
3411  * provider doesn't have to be involved. (Note that parts of custom_exprs
3412  * could have come from join clauses, so doing this beforehand on the
3413  * scan_clauses wouldn't work.) We assume custom_scan_tlist contains no
3414  * such variables.
3415  */
3416  if (best_path->path.param_info)
3417  {
3418  cplan->scan.plan.qual = (List *)
3419  replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
3420  cplan->custom_exprs = (List *)
3421  replace_nestloop_params(root, (Node *) cplan->custom_exprs);
3422  }
3423 
3424  return cplan;
3425 }
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:130
Plan plan
Definition: plannodes.h:305
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:3998
List * custom_paths
Definition: relation.h:1102
#define castNode(_type_, nodeptr)
Definition: nodes.h:577
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:897
Definition: nodes.h:508
List * custom_exprs
Definition: plannodes.h:586
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4556
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
const struct CustomPathMethods * methods
Definition: relation.h:1104
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
RelOptInfo * parent
Definition: relation.h:894
Relids relids
Definition: relation.h:490
Path path
Definition: relation.h:1099
List * lappend(List *list, void *datum)
Definition: list.c:128
Scan scan
Definition: plannodes.h:582
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * custom_relids
Definition: plannodes.h:590
#define CP_EXACT_TLIST
Definition: createplan.c:66
Definition: pg_list.h:45
Definition: relation.h:888
static ForeignScan * create_foreignscan_plan ( PlannerInfo root,
ForeignPath best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3220 of file createplan.c.

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

Referenced by create_scan_plan().

3222 {
3223  ForeignScan *scan_plan;
3224  RelOptInfo *rel = best_path->path.parent;
3225  Index scan_relid = rel->relid;
3226  Oid rel_oid = InvalidOid;
3227  Plan *outer_plan = NULL;
3228 
3229  Assert(rel->fdwroutine != NULL);
3230 
3231  /* transform the child path if any */
3232  if (best_path->fdw_outerpath)
3233  outer_plan = create_plan_recurse(root, best_path->fdw_outerpath,
3234  CP_EXACT_TLIST);
3235 
3236  /*
3237  * If we're scanning a base relation, fetch its OID. (Irrelevant if
3238  * scanning a join relation.)
3239  */
3240  if (scan_relid > 0)
3241  {
3242  RangeTblEntry *rte;
3243 
3244  Assert(rel->rtekind == RTE_RELATION);
3245  rte = planner_rt_fetch(scan_relid, root);
3246  Assert(rte->rtekind == RTE_RELATION);
3247  rel_oid = rte->relid;
3248  }
3249 
3250  /*
3251  * Sort clauses into best execution order. We do this first since the FDW
3252  * might have more info than we do and wish to adjust the ordering.
3253  */
3254  scan_clauses = order_qual_clauses(root, scan_clauses);
3255 
3256  /*
3257  * Let the FDW perform its processing on the restriction clauses and
3258  * generate the plan node. Note that the FDW might remove restriction
3259  * clauses that it intends to execute remotely, or even add more (if it
3260  * has selected some join clauses for remote use but also wants them
3261  * rechecked locally).
3262  */
3263  scan_plan = rel->fdwroutine->GetForeignPlan(root, rel, rel_oid,
3264  best_path,
3265  tlist, scan_clauses,
3266  outer_plan);
3267 
3268  /* Copy cost data from Path to Plan; no need to make FDW do this */
3269  copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3270 
3271  /* Copy foreign server OID; likewise, no need to make FDW do this */
3272  scan_plan->fs_server = rel->serverid;
3273 
3274  /*
3275  * Likewise, copy the relids that are represented by this foreign scan. An
3276  * upper rel doesn't have relids set, but it covers all the base relations
3277  * participating in the underlying scan, so use root's all_baserels.
3278  */
3279  if (rel->reloptkind == RELOPT_UPPER_REL)
3280  scan_plan->fs_relids = root->all_baserels;
3281  else
3282  scan_plan->fs_relids = best_path->path.parent->relids;
3283 
3284  /*
3285  * If this is a foreign join, and to make it valid to push down we had to
3286  * assume that the current user is the same as some user explicitly named
3287  * in the query, mark the finished plan as depending on the current user.
3288  */
3289  if (rel->useridiscurrent)
3290  root->glob->dependsOnRole = true;
3291 
3292  /*
3293  * Replace any outer-relation variables with nestloop params in the qual,
3294  * fdw_exprs and fdw_recheck_quals expressions. We do this last so that
3295  * the FDW doesn't have to be involved. (Note that parts of fdw_exprs or
3296  * fdw_recheck_quals could have come from join clauses, so doing this
3297  * beforehand on the scan_clauses wouldn't work.) We assume
3298  * fdw_scan_tlist contains no such variables.
3299  */
3300  if (best_path->path.param_info)
3301  {
3302  scan_plan->scan.plan.qual = (List *)
3303  replace_nestloop_params(root, (Node *) scan_plan->scan.plan.qual);
3304  scan_plan->fdw_exprs = (List *)
3305  replace_nestloop_params(root, (Node *) scan_plan->fdw_exprs);
3306  scan_plan->fdw_recheck_quals = (List *)
3308  (Node *) scan_plan->fdw_recheck_quals);
3309  }
3310 
3311  /*
3312  * If rel is a base relation, detect whether any system columns are
3313  * requested from the rel. (If rel is a join relation, rel->relid will be
3314  * 0, but there can be no Var with relid 0 in the rel's targetlist or the
3315  * restriction clauses, so we skip this in that case. Note that any such
3316  * columns in base relations that were joined are assumed to be contained
3317  * in fdw_scan_tlist.) This is a bit of a kluge and might go away
3318  * someday, so we intentionally leave it out of the API presented to FDWs.
3319  */
3320  scan_plan->fsSystemCol = false;
3321  if (scan_relid > 0)
3322  {
3323  Bitmapset *attrs_used = NULL;
3324  ListCell *lc;
3325  int i;
3326 
3327  /*
3328  * First, examine all the attributes needed for joins or final output.
3329  * Note: we must look at rel's targetlist, not the attr_needed data,
3330  * because attr_needed isn't computed for inheritance child rels.
3331  */
3332  pull_varattnos((Node *) rel->reltarget->exprs, scan_relid, &attrs_used);
3333 
3334  /* Add all the attributes used by restriction clauses. */
3335  foreach(lc, rel->baserestrictinfo)
3336  {
3337  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3338 
3339  pull_varattnos((Node *) rinfo->clause, scan_relid, &attrs_used);
3340  }
3341 
3342  /* Now, are any system columns requested from rel? */
3343  for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
3344  {
3346  {
3347  scan_plan->fsSystemCol = true;
3348  break;
3349  }
3350  }
3351 
3352  bms_free(attrs_used);
3353  }
3354 
3355  return scan_plan;
3356 }
GetForeignPlan_function GetForeignPlan
Definition: fdwapi.h:175
List * qual
Definition: plannodes.h:130
Plan plan
Definition: plannodes.h:305
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:3998
Path * fdw_outerpath
Definition: relation.h:1072
RelOptKind reloptkind
Definition: relation.h:487
Oid fs_server
Definition: plannodes.h:555
List * baserestrictinfo
Definition: relation.h:544
List * fdw_exprs
Definition: plannodes.h:556
ParamPathInfo * param_info
Definition: relation.h:897
Definition: nodes.h:508
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4556
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
bool useridiscurrent
Definition: relation.h:538
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:124
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
#define planner_rt_fetch(rti, root)
Definition: relation.h:320
Relids all_baserels
Definition: relation.h:193
RelOptInfo * parent
Definition: relation.h:894
PlannerGlobal * glob
Definition: relation.h:154
struct FdwRoutine * fdwroutine
Definition: relation.h:540
Relids relids
Definition: relation.h:490
List * fdw_recheck_quals
Definition: plannodes.h:559
Index relid
Definition: relation.h:518
Expr * clause
Definition: relation.h:1637
Oid serverid
Definition: relation.h:536
List * exprs
Definition: relation.h:824
unsigned int Index
Definition: c.h:362
RTEKind rtekind
Definition: relation.h:520
#define InvalidOid
Definition: postgres_ext.h:36
void bms_free(Bitmapset *a)
Definition: bitmapset.c:200
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
RTEKind rtekind
Definition: parsenodes.h:882
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:419
struct PathTarget * reltarget
Definition: relation.h:501
bool fsSystemCol
Definition: plannodes.h:562
Bitmapset * fs_relids
Definition: plannodes.h:561
static FunctionScan * create_functionscan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

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

2982 {
2983  FunctionScan *scan_plan;
2984  Index scan_relid = best_path->parent->relid;
2985  RangeTblEntry *rte;
2986  List *functions;
2987 
2988  /* it should be a function base rel... */
2989  Assert(scan_relid > 0);
2990  rte = planner_rt_fetch(scan_relid, root);
2991  Assert(rte->rtekind == RTE_FUNCTION);
2992  functions = rte->functions;
2993 
2994  /* Sort clauses into best execution order */
2995  scan_clauses = order_qual_clauses(root, scan_clauses);
2996 
2997  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2998  scan_clauses = extract_actual_clauses(scan_clauses, false);
2999 
3000  /* Replace any outer-relation variables with nestloop params */
3001  if (best_path->param_info)
3002  {
3003  scan_clauses = (List *)
3004  replace_nestloop_params(root, (Node *) scan_clauses);
3005  /* The function expressions could contain nestloop params, too */
3006  functions = (List *) replace_nestloop_params(root, (Node *) functions);
3007  }
3008 
3009  scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
3010  functions, rte->funcordinality);
3011 
3012  copy_generic_path_info(&scan_plan->scan.plan, best_path);
3013 
3014  return scan_plan;
3015 }
Plan plan
Definition: plannodes.h:305
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:3998
ParamPathInfo * param_info
Definition: relation.h:897
Definition: nodes.h:508
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4556
bool funcordinality
Definition: parsenodes.h:932
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
#define planner_rt_fetch(rti, root)
Definition: relation.h:320
RelOptInfo * parent
Definition: relation.h:894
Index relid
Definition: relation.h:518
unsigned int Index
Definition: c.h:362
static FunctionScan * make_functionscan(List *qptlist, List *qpqual, Index scanrelid, List *functions, bool funcordinality)
Definition: createplan.c:4892
#define Assert(condition)
Definition: c.h:671
List * functions
Definition: parsenodes.h:931
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:354
static const struct fns functions
Definition: regcomp.c:301
RTEKind rtekind
Definition: parsenodes.h:882
Definition: pg_list.h:45
static Gather * create_gather_plan ( PlannerInfo root,
GatherPath best_path 
)
static

Definition at line 1412 of file createplan.c.

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

Referenced by create_plan_recurse().

1413 {
1414  Gather *gather_plan;
1415  Plan *subplan;
1416  List *tlist;
1417 
1418  /*
1419  * Although the Gather node can project, we prefer to push down such work
1420  * to its child node, so demand an exact tlist from the child.
1421  */
1422  subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1423 
1424  tlist = build_path_tlist(root, &best_path->path);
1425 
1426  gather_plan = make_gather(tlist,
1427  NIL,
1428  best_path->path.parallel_workers,
1429  best_path->single_copy,
1430  subplan);
1431 
1432  copy_generic_path_info(&gather_plan->plan, &best_path->path);
1433 
1434  /* use parallel mode for parallel plans. */
1435  root->glob->parallelModeNeeded = true;
1436 
1437  return gather_plan;
1438 }
#define NIL
Definition: pg_list.h:69
int parallel_workers
Definition: relation.h:901
bool single_copy
Definition: relation.h:1203
static Gather * make_gather(List *qptlist, List *qpqual, int nworkers, bool single_copy, Plan *subplan)
Definition: createplan.c:5964
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
bool parallelModeNeeded
Definition: relation.h:128
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:698
Plan plan
Definition: plannodes.h:782
PlannerGlobal * glob
Definition: relation.h:154
Path * subpath
Definition: relation.h:1202
Path path
Definition: relation.h:1201
#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 860 of file createplan.c.

References Assert, build_path_tlist(), copy_plan_costsize(), and make_result().

Referenced by create_join_plan(), and create_scan_plan().

862 {
863  Plan *gplan;
864 
865  Assert(gating_quals);
866 
867  /*
868  * Since we need a Result node anyway, always return the path's requested
869  * tlist; that's never a wrong choice, even if the parent node didn't ask
870  * for CP_EXACT_TLIST.
871  */
872  gplan = (Plan *) make_result(build_path_tlist(root, path),
873  (Node *) gating_quals,
874  plan);
875 
876  /*
877  * Notice that we don't change cost or size estimates when doing gating.
878  * The costs of qual eval were already included in the subplan's cost.
879  * Leaving the size alone amounts to assuming that the gating qual will
880  * succeed, which is the conservative estimate for planning upper queries.
881  * We certainly don't want to assume the output size is zero (unless the
882  * gating qual is actually constant FALSE, and that case is dealt with in
883  * clausesel.c). Interpolating between the two cases is silly, because it
884  * doesn't reflect what will really happen at runtime, and besides which
885  * in most cases we have only a very bad idea of the probability of the
886  * gating qual being true.
887  */
888  copy_plan_costsize(gplan, plan);
889 
890  return gplan;
891 }
Definition: nodes.h:508
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:698
static void copy_plan_costsize(Plan *dest, Plan *src)
Definition: createplan.c:4664
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6085
#define Assert(condition)
Definition: c.h:671
static Group * create_group_plan ( PlannerInfo root,
GroupPath best_path 
)
static

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

1561 {
1562  Group *plan;
1563  Plan *subplan;
1564  List *tlist;
1565  List *quals;
1566 
1567  /*
1568  * Group can project, so no need to be terribly picky about child tlist,
1569  * but we do need grouping columns to be available
1570  */
1571  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1572 
1573  tlist = build_path_tlist(root, &best_path->path);
1574 
1575  quals = order_qual_clauses(root, best_path->qual);
1576 
1577  plan = make_group(tlist,
1578  quals,
1579  list_length(best_path->groupClause),
1581  subplan->targetlist),
1582  extract_grouping_ops(best_path->groupClause),
1583  subplan);
1584 
1585  copy_generic_path_info(&plan->plan, (Path *) best_path);
1586 
1587  return plan;
1588 }
List * qual
Definition: relation.h:1346
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4556
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:344
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:698
Path path
Definition: relation.h:1343
static Group * make_group(List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Plan *lefttree)
Definition: createplan.c:5789
List * groupClause
Definition: relation.h:1345
#define CP_LABEL_TLIST
Definition: createplan.c:68
Path * subpath
Definition: relation.h:1344
Plan plan
Definition: plannodes.h:710
static int list_length(const List *l)
Definition: pg_list.h:89
List * targetlist
Definition: plannodes.h:129
Definition: pg_list.h:45
Definition: relation.h:888
static Plan * create_groupingsets_plan ( PlannerInfo root,
GroupingSetsPath best_path 
)
static

Definition at line 1706 of file createplan.c.

References AGG_PLAIN, AGG_SORTED, AGGSPLIT_SIMPLE, Assert, build_path_tlist(), copy_generic_path_info(), CP_LABEL_TLIST, create_plan_recurse(), extract_grouping_ops(), forboth, get_sortgroupclause_tle(), Query::groupClause, PlannerInfo::grouping_map, Query::groupingSets, PlannerInfo::hasInheritedTarget, lappend(), Plan::lefttree, lfirst, linitial, list_length(), llast, lnext, make_agg(), make_sort_from_groupcols(), NIL, NULL, palloc0(), PlannerInfo::parse, GroupingSetsPath::path, Agg::plan, GroupingSetsPath::qual, remap_groupColIdx(), TargetEntry::resno, GroupingSetsPath::rollup_groupclauses, GroupingSetsPath::rollup_lists, GroupingSetsPath::subpath, Plan::targetlist, and SortGroupClause::tleSortGroupRef.

Referenced by create_plan_recurse().

1707 {
1708  Agg *plan;
1709  Plan *subplan;
1710  List *rollup_groupclauses = best_path->rollup_groupclauses;
1711  List *rollup_lists = best_path->rollup_lists;
1712  AttrNumber *grouping_map;
1713  int maxref;
1714  List *chain;
1715  ListCell *lc,
1716  *lc2;
1717 
1718  /* Shouldn't get here without grouping sets */
1719  Assert(root->parse->groupingSets);
1720  Assert(rollup_lists != NIL);
1721  Assert(list_length(rollup_lists) == list_length(rollup_groupclauses));
1722 
1723  /*
1724  * Agg can project, so no need to be terribly picky about child tlist, but
1725  * we do need grouping columns to be available
1726  */
1727  subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
1728 
1729  /*
1730  * Compute the mapping from tleSortGroupRef to column index in the child's
1731  * tlist. First, identify max SortGroupRef in groupClause, for array
1732  * sizing.
1733  */
1734  maxref = 0;
1735  foreach(lc, root->parse->groupClause)
1736  {
1737  SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
1738 
1739  if (gc->tleSortGroupRef > maxref)
1740  maxref = gc->tleSortGroupRef;
1741  }
1742 
1743  grouping_map = (AttrNumber *) palloc0((maxref + 1) * sizeof(AttrNumber));
1744 
1745  /* Now look up the column numbers in the child's tlist */
1746  foreach(lc, root->parse->groupClause)
1747  {
1748  SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
1749  TargetEntry *tle = get_sortgroupclause_tle(gc, subplan->targetlist);
1750 
1751  grouping_map[gc->tleSortGroupRef] = tle->resno;
1752  }
1753 
1754  /*
1755  * During setrefs.c, we'll need the grouping_map to fix up the cols lists
1756  * in GroupingFunc nodes. Save it for setrefs.c to use.
1757  *
1758  * This doesn't work if we're in an inheritance subtree (see notes in
1759  * create_modifytable_plan). Fortunately we can't be because there would
1760  * never be grouping in an UPDATE/DELETE; but let's Assert that.
1761  */
1762  Assert(!root->hasInheritedTarget);
1763  Assert(root->grouping_map == NULL);
1764  root->grouping_map = grouping_map;
1765 
1766  /*
1767  * Generate the side nodes that describe the other sort and group
1768  * operations besides the top one. Note that we don't worry about putting
1769  * accurate cost estimates in the side nodes; only the topmost Agg node's
1770  * costs will be shown by EXPLAIN.
1771  */
1772  chain = NIL;
1773  if (list_length(rollup_groupclauses) > 1)
1774  {
1775  forboth(lc, rollup_groupclauses, lc2, rollup_lists)
1776  {
1777  List *groupClause = (List *) lfirst(lc);
1778  List *gsets = (List *) lfirst(lc2);
1779  AttrNumber *new_grpColIdx;
1780  Plan *sort_plan;
1781  Plan *agg_plan;
1782 
1783  /* We want to iterate over all but the last rollup list elements */
1784  if (lnext(lc) == NULL)
1785  break;
1786 
1787  new_grpColIdx = remap_groupColIdx(root, groupClause);
1788 
1789  sort_plan = (Plan *)
1790  make_sort_from_groupcols(groupClause,
1791  new_grpColIdx,
1792  subplan);
1793 
1794  agg_plan = (Plan *) make_agg(NIL,
1795  NIL,
1796  AGG_SORTED,
1798  list_length((List *) linitial(gsets)),
1799  new_grpColIdx,
1800  extract_grouping_ops(groupClause),
1801  gsets,
1802  NIL,
1803  0, /* numGroups not needed */
1804  sort_plan);
1805 
1806  /*
1807  * Nuke stuff we don't need to avoid bloating debug output.
1808  */
1809  sort_plan->targetlist = NIL;
1810  sort_plan->lefttree = NULL;
1811 
1812  chain = lappend(chain, agg_plan);
1813  }
1814  }
1815 
1816  /*
1817  * Now make the final Agg node
1818  */
1819  {
1820  List *groupClause = (List *) llast(rollup_groupclauses);
1821  List *gsets = (List *) llast(rollup_lists);
1822  AttrNumber *top_grpColIdx;
1823  int numGroupCols;
1824 
1825  top_grpColIdx = remap_groupColIdx(root, groupClause);
1826 
1827  numGroupCols = list_length((List *) linitial(gsets));
1828 
1829  plan = make_agg(build_path_tlist(root, &best_path->path),
1830  best_path->qual,
1831  (numGroupCols > 0) ? AGG_SORTED : AGG_PLAIN,
1833  numGroupCols,
1834  top_grpColIdx,
1835  extract_grouping_ops(groupClause),
1836  gsets,
1837  chain,
1838  0, /* numGroups not needed */
1839  subplan);
1840 
1841  /* Copy cost data from Path to Plan */
1842  copy_generic_path_info(&plan->plan, &best_path->path);
1843  }
1844 
1845  return (Plan *) plan;
1846 }
#define NIL
Definition: pg_list.h:69
Query * parse
Definition: relation.h:152
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:370
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:174
static AttrNumber * remap_groupColIdx(PlannerInfo *root, List *groupClause)
Definition: createplan.c:1668
#define llast(l)
Definition: pg_list.h:126
Index tleSortGroupRef
Definition: parsenodes.h:1102
List * groupingSets
Definition: parsenodes.h:139
Oid * extract_grouping_ops(List *groupClause)
Definition: tlist.c:466
AttrNumber * grouping_map
Definition: relation.h:279
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:5727
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
#define linitial(l)
Definition: pg_list.h:110
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:698
List * rollup_lists
Definition: relation.h:1391
AttrNumber resno
Definition: primnodes.h:1331
static Sort * make_sort_from_groupcols(List *groupcls, AttrNumber *grpColIdx, Plan *lefttree)
Definition: createplan.c:5631
#define lnext(lc)
Definition: pg_list.h:105
List * lappend(List *list, void *datum)
Definition: list.c:128
Plan plan
Definition: plannodes.h:732
List * rollup_groupclauses
Definition: relation.h:1390
void * palloc0(Size size)
Definition: mcxt.c:920
#define CP_LABEL_TLIST
Definition: createplan.c:68
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
bool hasInheritedTarget
Definition: relation.h:292
static int list_length(const List *l)
Definition: pg_list.h:89
struct Plan * lefttree
Definition: plannodes.h:131
List * targetlist
Definition: plannodes.h:129
List * groupClause
Definition: parsenodes.h:137
Definition: plannodes.h:730
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
static HashJoin * create_hashjoin_plan ( PlannerInfo root,
HashPath best_path 
)
static

Definition at line 3843 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::innerjoinpath, InvalidAttrNumber, InvalidOid, is_opclause, IS_OUTER_JOIN, IsA, HashJoin::join, JoinPath::joinrestrictinfo, JoinPath::jointype, HashPath::jpath, linitial, list_difference(), list_length(), make_hash(), make_hashjoin(), NIL, HashPath::num_batches, order_qual_clauses(), JoinPath::outerjoinpath, Path::param_info, Path::parent, JoinPath::path, HashPath::path_hashclauses, Join::plan, Hash::plan, RangeTblEntry::relid, RelOptInfo::relids, replace_nestloop_params(), RTE_RELATION, RangeTblEntry::rtekind, PlannerInfo::simple_rte_array, Plan::startup_cost, Plan::total_cost, RangeQueryClause::var, Var::varattno, Var::varno, Var::vartype, and Var::vartypmod.

Referenced by create_join_plan().

3845 {
3846  HashJoin *join_plan;
3847  Hash *hash_plan;
3848  Plan *outer_plan;
3849  Plan *inner_plan;
3850  List *tlist = build_path_tlist(root, &best_path->jpath.path);
3851  List *joinclauses;
3852  List *otherclauses;
3853  List *hashclauses;
3854  Oid skewTable = InvalidOid;
3855  AttrNumber skewColumn = InvalidAttrNumber;
3856  bool skewInherit = false;
3857  Oid skewColType = InvalidOid;
3858  int32 skewColTypmod = -1;
3859 
3860  /*
3861  * HashJoin can project, so we don't have to demand exact tlists from the
3862  * inputs. However, it's best to request a small tlist from the inner
3863  * side, so that we aren't storing more data than necessary. Likewise, if
3864  * we anticipate batching, request a small tlist from the outer side so
3865  * that we don't put extra data in the outer batch files.
3866  */
3867  outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
3868  (best_path->num_batches > 1) ? CP_SMALL_TLIST : 0);
3869 
3870  inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
3871  CP_SMALL_TLIST);
3872 
3873  /* Sort join qual clauses into best execution order */
3874  joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
3875  /* There's no point in sorting the hash clauses ... */
3876 
3877  /* Get the join qual clauses (in plain expression form) */
3878  /* Any pseudoconstant clauses are ignored here */
3879  if (IS_OUTER_JOIN(best_path->jpath.jointype))
3880  {
3881  extract_actual_join_clauses(joinclauses,
3882  &joinclauses, &otherclauses);
3883  }
3884  else
3885  {
3886  /* We can treat all clauses alike for an inner join */
3887  joinclauses = extract_actual_clauses(joinclauses, false);
3888  otherclauses = NIL;
3889  }
3890 
3891  /*
3892  * Remove the hashclauses from the list of join qual clauses, leaving the
3893  * list of quals that must be checked as qpquals.
3894  */
3895  hashclauses = get_actual_clauses(best_path->path_hashclauses);
3896  joinclauses = list_difference(joinclauses, hashclauses);
3897 
3898  /*
3899  * Replace any outer-relation variables with nestloop params. There
3900  * should not be any in the hashclauses.
3901  */
3902  if (best_path->jpath.path.param_info)
3903  {
3904  joinclauses = (List *)
3905  replace_nestloop_params(root, (Node *) joinclauses);
3906  otherclauses = (List *)
3907  replace_nestloop_params(root, (Node *) otherclauses);
3908  }
3909 
3910  /*
3911  * Rearrange hashclauses, if needed, so that the outer variable is always
3912  * on the left.
3913  */
3914  hashclauses = get_switched_clauses(best_path->path_hashclauses,
3915  best_path->jpath.outerjoinpath->parent->relids);
3916 
3917  /*
3918  * If there is a single join clause and we can identify the outer variable
3919  * as a simple column reference, supply its identity for possible use in
3920  * skew optimization. (Note: in principle we could do skew optimization
3921  * with multiple join clauses, but we'd have to be able to determine the
3922  * most common combinations of outer values, which we don't currently have
3923  * enough stats for.)
3924  */
3925  if (list_length(hashclauses) == 1)
3926  {
3927  OpExpr *clause = (OpExpr *) linitial(hashclauses);
3928  Node *node;
3929 
3930  Assert(is_opclause(clause));
3931  node = (Node *) linitial(clause->args);
3932  if (IsA(node, RelabelType))
3933  node = (Node *) ((RelabelType *) node)->arg;
3934  if (IsA(node, Var))
3935  {
3936  Var *var = (Var *) node;
3937  RangeTblEntry *rte;
3938 
3939  rte = root->simple_rte_array[var->varno];
3940  if (rte->rtekind == RTE_RELATION)
3941  {
3942  skewTable = rte->relid;
3943  skewColumn = var->varattno;
3944  skewInherit = rte->inh;
3945  skewColType = var->vartype;
3946  skewColTypmod = var->vartypmod;
3947  }
3948  }
3949  }
3950 
3951  /*
3952  * Build the hash node and hash join node.
3953  */
3954  hash_plan = make_hash(inner_plan,
3955  skewTable,
3956  skewColumn,
3957  skewInherit,
3958  skewColType,
3959  skewColTypmod);
3960 
3961  /*
3962  * Set Hash node's startup & total costs equal to total cost of input
3963  * plan; this only affects EXPLAIN display not decisions.
3964  */
3965  copy_plan_costsize(&hash_plan->plan, inner_plan);
3966  hash_plan->plan.startup_cost = hash_plan->plan.total_cost;
3967 
3968  join_plan = make_hashjoin(tlist,
3969  joinclauses,
3970  otherclauses,
3971  hashclauses,
3972  outer_plan,
3973  (Plan *) hash_plan,
3974  best_path->jpath.jointype);
3975 
3976  copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
3977 
3978  return join_plan;
3979 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
JoinPath jpath
Definition: relation.h:1282
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:3998
static List * get_switched_clauses(List *clauses, Relids outerrelids)
Definition: createplan.c:4479
int num_batches
Definition: relation.h:1284
void extract_actual_join_clauses(List *restrictinfo_list, List **joinquals, List **otherquals)
Definition: restrictinfo.c:381
Path * innerjoinpath
Definition: relation.h:1217
static HashJoin * make_hashjoin(List *tlist, List *joinclauses, List *otherclauses, List *hashclauses, Plan *lefttree, Plan *righttree, JoinType jointype)
Definition: createplan.c:5126
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:714
ParamPathInfo * param_info
Definition: relation.h:897
#define CP_SMALL_TLIST
Definition: createplan.c:67
Definition: nodes.h:508
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:331
AttrNumber varattno
Definition: primnodes.h:146
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4556
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:141
Join join
Definition: plannodes.h:675
signed int int32
Definition: c.h:253
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
#define linitial(l)
Definition: pg_list.h:110
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:698
#define is_opclause(clause)
Definition: clauses.h:20
Oid vartype
Definition: primnodes.h:148
List * joinrestrictinfo
Definition: relation.h:1219
RelOptInfo * parent
Definition: relation.h:894
Cost startup_cost
Definition: plannodes.h:111
static void copy_plan_costsize(Plan *dest, Plan *src)
Definition: createplan.c:4664
Relids relids
Definition: relation.h:490
RangeTblEntry ** simple_rte_array
Definition: relation.h:185
Index varno
Definition: primnodes.h:144
Path * outerjoinpath
Definition: relation.h:1216
#define InvalidOid
Definition: postgres_ext.h:36
Path path
Definition: relation.h:1212
#define Assert(condition)
Definition: c.h:671
static int list_length(const List *l)
Definition: pg_list.h:89
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:354
List * list_difference(const List *list1, const List *list2)
Definition: list.c:858
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:882
Plan plan
Definition: plannodes.h:799
static Hash * make_hash(Plan *lefttree, Oid skewTable, AttrNumber skewColumn, bool skewInherit, Oid skewColType, int32 skewColTypmod)
Definition: createplan.c:5149
JoinType jointype
Definition: relation.h:1214
Cost total_cost
Definition: plannodes.h:112
List * path_hashclauses
Definition: relation.h:1283
List * args
Definition: primnodes.h:479
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
Plan plan
Definition: plannodes.h:618
int32 vartypmod
Definition: primnodes.h:149
static Scan * create_indexscan_plan ( PlannerInfo root,
IndexPath best_path,
List tlist,
List scan_clauses,
bool  indexonly 
)
static

Definition at line 2382 of file createplan.c.

References Assert, castNode, 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, 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().

2387 {
2388  Scan *scan_plan;
2389  List *indexquals = best_path->indexquals;
2390  List *indexorderbys = best_path->indexorderbys;
2391  Index baserelid = best_path->path.parent->relid;
2392  Oid indexoid = best_path->indexinfo->indexoid;
2393  List *qpqual;
2394  List *stripped_indexquals;
2395  List *fixed_indexquals;
2396  List *fixed_indexorderbys;
2397  List *indexorderbyops = NIL;
2398  ListCell *l;
2399 
2400  /* it should be a base rel... */
2401  Assert(baserelid > 0);
2402  Assert(best_path->path.parent->rtekind == RTE_RELATION);
2403 
2404  /*
2405  * Build "stripped" indexquals structure (no RestrictInfos) to pass to
2406  * executor as indexqualorig
2407  */
2408  stripped_indexquals = get_actual_clauses(indexquals);
2409 
2410  /*
2411  * The executor needs a copy with the indexkey on the left of each clause
2412  * and with index Vars substituted for table ones.
2413  */
2414  fixed_indexquals = fix_indexqual_references(root, best_path);
2415 
2416  /*
2417  * Likewise fix up index attr references in the ORDER BY expressions.
2418  */
2419  fixed_indexorderbys = fix_indexorderby_references(root, best_path);
2420 
2421  /*
2422  * The qpqual list must contain all restrictions not automatically handled
2423  * by the index, other than pseudoconstant clauses which will be handled
2424  * by a separate gating plan node. All the predicates in the indexquals
2425  * will be checked (either by the index itself, or by nodeIndexscan.c),
2426  * but if there are any "special" operators involved then they must be
2427  * included in qpqual. The upshot is that qpqual must contain
2428  * scan_clauses minus whatever appears in indexquals.
2429  *
2430  * In normal cases simple pointer equality checks will be enough to spot
2431  * duplicate RestrictInfos, so we try that first.
2432  *
2433  * Another common case is that a scan_clauses entry is generated from the
2434  * same EquivalenceClass as some indexqual, and is therefore redundant
2435  * with it, though not equal. (This happens when indxpath.c prefers a
2436  * different derived equality than what generate_join_implied_equalities
2437  * picked for a parameterized scan's ppi_clauses.)
2438  *
2439  * In some situations (particularly with OR'd index conditions) we may
2440  * have scan_clauses that are not equal to, but are logically implied by,
2441  * the index quals; so we also try a predicate_implied_by() check to see
2442  * if we can discard quals that way. (predicate_implied_by assumes its
2443  * first input contains only immutable functions, so we have to check
2444  * that.)
2445  *
2446  * Note: if you change this bit of code you should also look at
2447  * extract_nonindex_conditions() in costsize.c.
2448  */
2449  qpqual = NIL;
2450  foreach(l, scan_clauses)
2451  {
2452  RestrictInfo *rinfo = castNode(RestrictInfo, lfirst(l));
2453 
2454  if (rinfo->pseudoconstant)
2455  continue; /* we may drop pseudoconstants here */
2456  if (list_member_ptr(indexquals, rinfo))
2457  continue; /* simple duplicate */
2458  if (is_redundant_derived_clause(rinfo, indexquals))
2459  continue; /* derived from same EquivalenceClass */
2460  if (!contain_mutable_functions((Node *) rinfo->clause) &&
2461  predicate_implied_by(list_make1(rinfo->clause), indexquals))
2462  continue; /* provably implied by indexquals */
2463  qpqual = lappend(qpqual, rinfo);
2464  }
2465 
2466  /* Sort clauses into best execution order */
2467  qpqual = order_qual_clauses(root, qpqual);
2468 
2469  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2470  qpqual = extract_actual_clauses(qpqual, false);
2471 
2472  /*
2473  * We have to replace any outer-relation variables with nestloop params in
2474  * the indexqualorig, qpqual, and indexorderbyorig expressions. A bit
2475  * annoying to have to do this separately from the processing in
2476  * fix_indexqual_references --- rethink this when generalizing the inner
2477  * indexscan support. But note we can't really do this earlier because
2478  * it'd break the comparisons to predicates above ... (or would it? Those
2479  * wouldn't have outer refs)
2480  */
2481  if (best_path->path.param_info)
2482  {
2483  stripped_indexquals = (List *)
2484  replace_nestloop_params(root, (Node *) stripped_indexquals);
2485  qpqual = (List *)
2486  replace_nestloop_params(root, (Node *) qpqual);
2487  indexorderbys = (List *)
2488  replace_nestloop_params(root, (Node *) indexorderbys);
2489  }
2490 
2491  /*
2492  * If there are ORDER BY expressions, look up the sort operators for their
2493  * result datatypes.
2494  */
2495  if (indexorderbys)
2496  {
2497  ListCell *pathkeyCell,
2498  *exprCell;
2499 
2500  /*
2501  * PathKey contains OID of the btree opfamily we're sorting by, but
2502  * that's not quite enough because we need the expression's datatype
2503  * to look up the sort operator in the operator family.
2504  */
2505  Assert(list_length(best_path->path.pathkeys) == list_length(indexorderbys));
2506  forboth(pathkeyCell, best_path->path.pathkeys, exprCell, indexorderbys)
2507  {
2508  PathKey *pathkey = (PathKey *) lfirst(pathkeyCell);
2509  Node *expr = (Node *) lfirst(exprCell);
2510  Oid exprtype = exprType(expr);
2511  Oid sortop;
2512 
2513  /* Get sort operator from opfamily */
2514  sortop = get_opfamily_member(pathkey->pk_opfamily,
2515  exprtype,
2516  exprtype,
2517  pathkey->pk_strategy);
2518  if (!OidIsValid(sortop))
2519  elog(ERROR, "failed to find sort operator for ORDER BY expression");
2520  indexorderbyops = lappend_oid(indexorderbyops, sortop);
2521  }
2522  }
2523 
2524  /* Finally ready to build the plan node */
2525  if (indexonly)
2526  scan_plan = (Scan *) make_indexonlyscan(tlist,
2527  qpqual,
2528  baserelid,
2529  indexoid,
2530  fixed_indexquals,
2531  fixed_indexorderbys,
2532  best_path->indexinfo->indextlist,
2533  best_path->indexscandir);
2534  else
2535  scan_plan = (Scan *) make_indexscan(tlist,
2536  qpqual,
2537  baserelid,
2538  indexoid,
2539  fixed_indexquals,
2540  stripped_indexquals,
2541  fixed_indexorderbys,
2542  indexorderbys,
2543  indexorderbyops,
2544  best_path->indexscandir);
2545 
2546  copy_generic_path_info(&scan_plan->plan, &best_path->path);
2547 
2548  return scan_plan;
2549 }
#define NIL
Definition: pg_list.h:69
Plan plan
Definition: plannodes.h:305
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:3998
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:174
Path path
Definition: relation.h:971
bool is_redundant_derived_clause(RestrictInfo *rinfo, List *clauselist)
Definition: equivclass.c:2448
IndexOptInfo * indexinfo
Definition: relation.h:972
bool predicate_implied_by(List *predicate_list, List *restrictinfo_list)
Definition: predtest.c:128
#define castNode(_type_, nodeptr)
Definition: nodes.h:577
List * indextlist
Definition: relation.h:613
bool pseudoconstant
Definition: relation.h:1645
ParamPathInfo * param_info
Definition: relation.h:897
Definition: nodes.h:508
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:331
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4556
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:534
int pk_strategy
Definition: relation.h:793
#define list_make1(x1)
Definition: pg_list.h:133
List * indexquals
Definition: relation.h:974
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
#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:4755
RelOptInfo * parent
Definition: relation.h:894
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:4347
static List * fix_indexqual_references(PlannerInfo *root, IndexPath *index_path)
Definition: createplan.c:4216
Index relid
Definition: relation.h:518
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1637
List * indexorderbys
Definition: relation.h:976
unsigned int Index
Definition: c.h:362
RTEKind rtekind
Definition: relation.h:520
bool list_member_ptr(const List *list, const void *datum)
Definition: list.c:465
List * pathkeys
Definition: relation.h:909
static IndexOnlyScan * make_indexonlyscan(List *qptlist, List *qpqual, Index scanrelid, Oid indexid, List *indexqual, List *indexorderby, List *indextlist, ScanDirection indexscandir)
Definition: createplan.c:4786
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:89
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:354
Oid pk_opfamily
Definition: relation.h:792
ScanDirection indexscandir
Definition: relation.h:978
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:877
#define elog
Definition: elog.h:219
Oid indexoid
Definition: relation.h:588
Definition: pg_list.h:45
static Plan * create_join_plan ( PlannerInfo root,
JoinPath best_path 
)
static

Definition at line 899 of file createplan.c.

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

Referenced by create_plan_recurse().

900 {
901  Plan *plan;
902  List *gating_clauses;
903 
904  switch (best_path->path.pathtype)
905  {
906  case T_MergeJoin:
907  plan = (Plan *) create_mergejoin_plan(root,
908  (MergePath *) best_path);
909  break;
910  case T_HashJoin:
911  plan = (Plan *) create_hashjoin_plan(root,
912  (HashPath *) best_path);
913  break;
914  case T_NestLoop:
915  plan = (Plan *) create_nestloop_plan(root,
916  (NestPath *) best_path);
917  break;
918  default:
919  elog(ERROR, "unrecognized node type: %d",
920  (int) best_path->path.pathtype);
921  plan = NULL; /* keep compiler quiet */
922  break;
923  }
924 
925  /*
926  * If there are any pseudoconstant clauses attached to this node, insert a
927  * gating Result node that evaluates the pseudoconstants as one-time
928  * quals.
929  */
930  gating_clauses = get_gating_quals(root, best_path->joinrestrictinfo);
931  if (gating_clauses)
932  plan = create_gating_plan(root, (Path *) best_path, plan,
933  gating_clauses);
934 
935 #ifdef NOT_USED
936 
937  /*
938  * * Expensive function pullups may have pulled local predicates * into
939  * this path node. Put them in the qpqual of the plan node. * JMH,
940  * 6/15/92
941  */
942  if (get_loc_restrictinfo(best_path) != NIL)
943  set_qpqual((Plan) plan,
944  list_concat(get_qpqual((Plan) plan),
945  get_actual_clauses(get_loc_restrictinfo(best_path))));
946 #endif
947 
948  return plan;
949 }
#define NIL
Definition: pg_list.h:69
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:331
List * list_concat(List *list1, List *list2)
Definition: list.c:321
static NestLoop * create_nestloop_plan(PlannerInfo *root, NestPath *best_path)
Definition: createplan.c:3435
NodeTag pathtype
Definition: relation.h:892
#define ERROR
Definition: elog.h:43
List * joinrestrictinfo
Definition: relation.h:1219
#define NULL
Definition: c.h:226
Path path
Definition: relation.h:1212
static HashJoin * create_hashjoin_plan(PlannerInfo *root, HashPath *best_path)
Definition: createplan.c:3843
static Plan * create_gating_plan(PlannerInfo *root, Path *path, Plan *plan, List *gating_quals)
Definition: createplan.c:860
#define elog
Definition: elog.h:219
static MergeJoin * create_mergejoin_plan(PlannerInfo *root, MergePath *best_path)
Definition: createplan.c:3540
Definition: pg_list.h:45
Definition: relation.h:888
static List * get_gating_quals(PlannerInfo *root, List *quals)
Definition: createplan.c:840
static Limit * create_limit_plan ( PlannerInfo root,
LimitPath best_path,
int  flags 
)
static

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

2263 {
2264  Limit *plan;
2265  Plan *subplan;
2266 
2267  /* Limit doesn't project, so tlist requirements pass through */
2268  subplan = create_plan_recurse(root, best_path->subpath, flags);
2269 
2270  plan = make_limit(subplan,
2271  best_path->limitOffset,
2272  best_path->limitCount);
2273 
2274  copy_generic_path_info(&plan->plan, (Path *) best_path);
2275 
2276  return plan;
2277 }
Plan plan
Definition: plannodes.h:851
Node * limitOffset
Definition: relation.h:1489
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
Path * subpath
Definition: relation.h:1488
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
Definition: createplan.c:6064
Node * limitCount
Definition: relation.h:1490
Definition: relation.h:888
static LockRows * create_lockrows_plan ( PlannerInfo root,
LockRowsPath best_path,
int  flags 
)
static

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

2183 {
2184  LockRows *plan;
2185  Plan *subplan;
2186 
2187  /* LockRows doesn't project, so tlist requirements pass through */
2188  subplan = create_plan_recurse(root, best_path->subpath, flags);
2189 
2190  plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
2191 
2192  copy_generic_path_info(&plan->plan, (Path *) best_path);
2193 
2194  return plan;
2195 }
Plan plan
Definition: plannodes.h:837
List * rowMarks
Definition: relation.h:1455
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
Path * subpath
Definition: relation.h:1454
static LockRows * make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
Definition: createplan.c:6043
Definition: relation.h:888
static Material * create_material_plan ( PlannerInfo root,
MaterialPath best_path,
int  flags 
)
static

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

1183 {
1184  Material *plan;
1185  Plan *subplan;
1186 
1187  /*
1188  * We don't want any excess columns in the materialized tuples, so request
1189  * a smaller tlist. Otherwise, since Material doesn't project, tlist
1190  * requirements pass through.
1191  */
1192  subplan = create_plan_recurse(root, best_path->subpath,
1193  flags | CP_SMALL_TLIST);
1194 
1195  plan = make_material(subplan);
1196 
1197  copy_generic_path_info(&plan->plan, (Path *) best_path);
1198 
1199  return plan;
1200 }
#define CP_SMALL_TLIST
Definition: createplan.c:67
Path * subpath
Definition: relation.h:1163
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
static Material * make_material(Plan *lefttree)
Definition: createplan.c:5672
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
Plan plan
Definition: plannodes.h:685
Definition: relation.h:888
static Plan * create_merge_append_plan ( PlannerInfo root,
MergeAppendPath best_path 
)
static

Definition at line 1024 of file createplan.c.

References Assert, build_path_tlist(), MergeAppend::collations, copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), elog, ERROR, label_sort_with_costsize(), lappend(), Plan::lefttree, lfirst, MergeAppendPath::limit_tuples, make_sort(), makeNode, MergeAppend::mergeplans, NIL, NULL, MergeAppend::nullsFirst, MergeAppend::numCols, Path::parent, 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().

1025 {
1026  MergeAppend *node = makeNode(MergeAppend);
1027  Plan *plan = &node->plan;
1028  List *tlist = build_path_tlist(root, &best_path->path);
1029  List *pathkeys = best_path->path.pathkeys;
1030  List *subplans = NIL;
1031  ListCell *subpaths;
1032 
1033  /*
1034  * We don't have the actual creation of the MergeAppend node split out
1035  * into a separate make_xxx function. This is because we want to run
1036  * prepare_sort_from_pathkeys on it before we do so on the individual
1037  * child plans, to make cross-checking the sort info easier.
1038  */
1039  copy_generic_path_info(plan, (Path *) best_path);
1040  plan->targetlist = tlist;
1041  plan->qual = NIL;
1042  plan->lefttree = NULL;
1043  plan->righttree = NULL;
1044 
1045  /* Compute sort column info, and adjust MergeAppend's tlist as needed */
1046  (void) prepare_sort_from_pathkeys(plan, pathkeys,
1047  best_path->path.parent->relids,
1048  NULL,
1049  true,
1050  &node->numCols,
1051  &node->sortColIdx,
1052  &node->sortOperators,
1053  &node->collations,
1054  &node->nullsFirst);
1055 
1056  /*
1057  * Now prepare the child plans. We must apply prepare_sort_from_pathkeys
1058  * even to subplans that don't need an explicit sort, to make sure they
1059  * are returning the same sort key columns the MergeAppend expects.
1060  */
1061  foreach(subpaths, best_path->subpaths)
1062  {
1063  Path *subpath = (Path *) lfirst(subpaths);
1064  Plan *subplan;
1065  int numsortkeys;
1066  AttrNumber *sortColIdx;
1067  Oid *sortOperators;
1068  Oid *collations;
1069  bool *nullsFirst;
1070 
1071  /* Build the child plan */
1072  /* Must insist that all children return the same tlist */
1073  subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
1074 
1075  /* Compute sort column info, and adjust subplan's tlist as needed */
1076  subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1077  subpath->parent->relids,
1078  node->sortColIdx,
1079  false,
1080  &numsortkeys,
1081  &sortColIdx,
1082  &sortOperators,
1083  &collations,
1084  &nullsFirst);
1085 
1086  /*
1087  * Check that we got the same sort key information. We just Assert
1088  * that the sortops match, since those depend only on the pathkeys;
1089  * but it seems like a good idea to check the sort column numbers
1090  * explicitly, to ensure the tlists really do match up.
1091  */
1092  Assert(numsortkeys == node->numCols);
1093  if (memcmp(sortColIdx, node->sortColIdx,
1094  numsortkeys * sizeof(AttrNumber)) != 0)
1095  elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
1096  Assert(memcmp(sortOperators, node->sortOperators,
1097  numsortkeys * sizeof(Oid)) == 0);
1098  Assert(memcmp(collations, node->collations,
1099  numsortkeys * sizeof(Oid)) == 0);
1100  Assert(memcmp(nullsFirst, node->nullsFirst,
1101  numsortkeys * sizeof(bool)) == 0);
1102 
1103  /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1104  if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
1105  {
1106  Sort *sort = make_sort(subplan, numsortkeys,
1107  sortColIdx, sortOperators,
1108  collations, nullsFirst);
1109 
1110  label_sort_with_costsize(root, sort, best_path->limit_tuples);
1111  subplan = (Plan *) sort;
1112  }
1113 
1114  subplans = lappend(subplans, subplan);
1115  }
1116 
1117  node->mergeplans = subplans;
1118 
1119  return (Plan *) node;
1120 }
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:130
Oid * collations
Definition: plannodes.h:246
unsigned int Oid
Definition: postgres_ext.h:31
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
struct Plan * righttree
Definition: plannodes.h:132
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
#define ERROR
Definition: elog.h:43
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:698
RelOptInfo * parent
Definition: relation.h:894
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:200
Relids relids
Definition: relation.h:490
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:909
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
Definition: createplan.c:5211
#define makeNode(_type_)
Definition: nodes.h:556
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
AttrNumber * sortColIdx
Definition: plannodes.h:244
struct Plan * lefttree
Definition: plannodes.h:131
List * targetlist
Definition: plannodes.h:129
bool * nullsFirst
Definition: plannodes.h:247
List * mergeplans
Definition: plannodes.h:241
Oid * sortOperators
Definition: plannodes.h:245
List * subpaths
Definition: relation.h:1137
#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:4684
Definition: relation.h:888
double limit_tuples
Definition: relation.h:1138
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:5272
static MergeJoin * create_mergejoin_plan ( PlannerInfo root,
MergePath best_path 
)
static

Definition at line 3540 of file createplan.c.

References Assert, build_path_tlist(), castNode, 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::innerjoinpath, MergePath::innersortkeys, IS_OUTER_JOIN, MergeJoin::join, JoinPath::joinrestrictinfo, JoinPath::jointype, MergePath::jpath, label_sort_with_costsize(), RestrictInfo::left_ec, lfirst, list_difference(), list_head(), list_length(), lnext, make_material(), make_mergejoin(), make_sort_from_pathkeys(), MergePath::materialize_inner, NIL, NULL, order_qual_clauses(), RestrictInfo::outer_is_left, JoinPath::outerjoinpath, MergePath::outersortkeys, palloc(), Path::param_info, Path::parent, JoinPath::path, MergePath::path_mergeclauses, Path::pathkeys, PathKey::pk_eclass, PathKey::pk_nulls_first, PathKey::pk_opfamily, PathKey::pk_strategy, Join::plan, Plan::plan_rows, RelOptInfo::relids, replace_nestloop_params(), RestrictInfo::right_ec, sort(), and Plan::total_cost.

Referenced by create_join_plan().

3542 {
3543  MergeJoin *join_plan;
3544  Plan *outer_plan;
3545  Plan *inner_plan;
3546  List *tlist = build_path_tlist(root, &best_path->jpath.path);
3547  List *joinclauses;
3548  List *otherclauses;
3549  List *mergeclauses;
3550  List *outerpathkeys;
3551  List *innerpathkeys;
3552  int nClauses;
3553  Oid *mergefamilies;
3554  Oid *mergecollations;
3555  int *mergestrategies;
3556  bool *mergenullsfirst;
3557  int i;
3558  ListCell *lc;
3559  ListCell *lop;
3560  ListCell *lip;
3561 
3562  /*
3563  * MergeJoin can project, so we don't have to demand exact tlists from the
3564  * inputs. However, if we're intending to sort an input's result, it's
3565  * best to request a small tlist so we aren't sorting more data than
3566  * necessary.
3567  */
3568  outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
3569  (best_path->outersortkeys != NIL) ? CP_SMALL_TLIST : 0);
3570 
3571  inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
3572  (best_path->innersortkeys != NIL) ? CP_SMALL_TLIST : 0);
3573 
3574  /* Sort join qual clauses into best execution order */
3575  /* NB: do NOT reorder the mergeclauses */
3576  joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
3577 
3578  /* Get the join qual clauses (in plain expression form) */
3579  /* Any pseudoconstant clauses are ignored here */
3580  if (IS_OUTER_JOIN(best_path->jpath.jointype))
3581  {
3582  extract_actual_join_clauses(joinclauses,
3583  &joinclauses, &otherclauses);
3584  }
3585  else
3586  {
3587  /* We can treat all clauses alike for an inner join */
3588  joinclauses = extract_actual_clauses(joinclauses, false);
3589  otherclauses = NIL;
3590  }
3591 
3592  /*
3593  * Remove the mergeclauses from the list of join qual clauses, leaving the
3594  * list of quals that must be checked as qpquals.
3595  */
3596  mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
3597  joinclauses = list_difference(joinclauses, mergeclauses);
3598 
3599  /*
3600  * Replace any outer-relation variables with nestloop params. There
3601  * should not be any in the mergeclauses.
3602  */
3603  if (best_path->jpath.path.param_info)
3604  {
3605  joinclauses = (List *)
3606  replace_nestloop_params(root, (Node *) joinclauses);
3607  otherclauses = (List *)
3608  replace_nestloop_params(root, (Node *) otherclauses);
3609  }
3610 
3611  /*
3612  * Rearrange mergeclauses, if needed, so that the outer variable is always
3613  * on the left; mark the mergeclause restrictinfos with correct
3614  * outer_is_left status.
3615  */
3616  mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
3617  best_path->jpath.outerjoinpath->parent->relids);
3618 
3619  /*
3620  * Create explicit sort nodes for the outer and inner paths if necessary.
3621  */
3622  if (best_path->outersortkeys)
3623  {
3624  Sort *sort = make_sort_from_pathkeys(outer_plan,
3625  best_path->outersortkeys);
3626 
3627  label_sort_with_costsize(root, sort, -1.0);
3628  outer_plan = (Plan *) sort;
3629  outerpathkeys = best_path->outersortkeys;
3630  }
3631  else
3632  outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
3633 
3634  if (best_path->innersortkeys)
3635  {
3636  Sort *sort = make_sort_from_pathkeys(inner_plan,
3637  best_path->innersortkeys);
3638 
3639  label_sort_with_costsize(root, sort, -1.0);
3640  inner_plan = (Plan *) sort;
3641  innerpathkeys = best_path->innersortkeys;
3642  }
3643  else
3644  innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
3645 
3646  /*
3647  * If specified, add a materialize node to shield the inner plan from the
3648  * need to handle mark/restore.
3649  */
3650  if (best_path->materialize_inner)
3651  {
3652  Plan *matplan = (Plan *) make_material(inner_plan);
3653 
3654  /*
3655  * We assume the materialize will not spill to disk, and therefore
3656  * charge just cpu_operator_cost per tuple. (Keep this estimate in
3657  * sync with final_cost_mergejoin.)
3658  */
3659  copy_plan_costsize(matplan, inner_plan);
3660  matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
3661 
3662  inner_plan = matplan;
3663  }
3664 
3665  /*
3666  * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
3667  * executor. The information is in the pathkeys for the two inputs, but
3668  * we need to be careful about the possibility of mergeclauses sharing a
3669  * pathkey (compare find_mergeclauses_for_pathkeys()).
3670  */
3671  nClauses = list_length(mergeclauses);
3672  Assert(nClauses == list_length(best_path->path_mergeclauses));
3673  mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
3674  mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
3675  mergestrategies = (int *) palloc(nClauses * sizeof(int));
3676  mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
3677 
3678  lop = list_head(outerpathkeys);
3679  lip = list_head(innerpathkeys);
3680  i = 0;
3681  foreach(lc, best_path->path_mergeclauses)
3682  {
3683  RestrictInfo *rinfo = castNode(RestrictInfo, lfirst(lc));
3684  EquivalenceClass *oeclass;
3685  EquivalenceClass *ieclass;
3686  PathKey *opathkey;
3687  PathKey *ipathkey;
3688  EquivalenceClass *opeclass;
3689  EquivalenceClass *ipeclass;
3690  ListCell *l2;
3691 
3692  /* fetch outer/inner eclass from mergeclause */
3693  if (rinfo->outer_is_left)
3694  {
3695  oeclass = rinfo->left_ec;
3696  ieclass = rinfo->right_ec;
3697  }
3698  else
3699  {
3700  oeclass = rinfo->right_ec;
3701  ieclass = rinfo->left_ec;
3702  }
3703  Assert(oeclass != NULL);
3704  Assert(ieclass != NULL);
3705 
3706  /*
3707  * For debugging purposes, we check that the eclasses match the paths'
3708  * pathkeys. In typical cases the merge clauses are one-to-one with
3709  * the pathkeys, but when dealing with partially redundant query
3710  * conditions, we might have clauses that re-reference earlier path
3711  * keys. The case that we need to reject is where a pathkey is
3712  * entirely skipped over.
3713  *
3714  * lop and lip reference the first as-yet-unused pathkey elements;
3715  * it's okay to match them, or any element before them. If they're
3716  * NULL then we have found all pathkey elements to be used.
3717  */
3718  if (lop)
3719  {
3720  opathkey = (PathKey *) lfirst(lop);
3721  opeclass = opathkey->pk_eclass;
3722  if (oeclass == opeclass)
3723  {
3724  /* fast path for typical case */
3725  lop = lnext(lop);
3726  }
3727  else
3728  {
3729  /* redundant clauses ... must match something before lop */
3730  foreach(l2, outerpathkeys)
3731  {
3732  if (l2 == lop)
3733  break;
3734  opathkey = (PathKey *) lfirst(l2);
3735  opeclass = opathkey->pk_eclass;
3736  if (oeclass == opeclass)
3737  break;
3738  }
3739  if (oeclass != opeclass)
3740  elog(ERROR, "outer pathkeys do not match mergeclauses");
3741  }
3742  }
3743  else
3744  {
3745  /* redundant clauses ... must match some already-used pathkey */
3746  opathkey = NULL;
3747  opeclass = NULL;
3748  foreach(l2, outerpathkeys)
3749  {
3750  opathkey = (PathKey *) lfirst(l2);
3751  opeclass = opathkey->pk_eclass;
3752  if (oeclass == opeclass)
3753  break;
3754  }
3755  if (l2 == NULL)
3756  elog(ERROR, "outer pathkeys do not match mergeclauses");
3757  }
3758 
3759  if (lip)
3760  {
3761  ipathkey = (PathKey *) lfirst(lip);
3762  ipeclass = ipathkey->pk_eclass;
3763  if (ieclass == ipeclass)
3764  {
3765  /* fast path for typical case */
3766  lip = lnext(lip);
3767  }
3768  else
3769  {
3770  /* redundant clauses ... must match something before lip */
3771  foreach(l2, innerpathkeys)
3772  {
3773  if (l2 == lip)
3774  break;
3775  ipathkey = (PathKey *) lfirst(l2);
3776  ipeclass = ipathkey->pk_eclass;
3777  if (ieclass == ipeclass)
3778  break;
3779  }
3780  if (ieclass != ipeclass)
3781  elog(ERROR, "inner pathkeys do not match mergeclauses");
3782  }
3783  }
3784  else
3785  {
3786  /* redundant clauses ... must match some already-used pathkey */
3787  ipathkey = NULL;
3788  ipeclass = NULL;
3789  foreach(l2, innerpathkeys)
3790  {
3791  ipathkey = (PathKey *) lfirst(l2);
3792  ipeclass = ipathkey->pk_eclass;
3793  if (ieclass == ipeclass)
3794  break;
3795  }
3796  if (l2 == NULL)
3797  elog(ERROR, "inner pathkeys do not match mergeclauses");
3798  }
3799 
3800  /* pathkeys should match each other too (more debugging) */
3801  if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
3802  opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation ||
3803  opathkey->pk_strategy != ipathkey->pk_strategy ||
3804  opathkey->pk_nulls_first != ipathkey->pk_nulls_first)
3805  elog(ERROR, "left and right pathkeys do not match in mergejoin");
3806 
3807  /* OK, save info for executor */
3808  mergefamilies[i] = opathkey->pk_opfamily;
3809  mergecollations[i] = opathkey->pk_eclass->ec_collation;
3810  mergestrategies[i] = opathkey->pk_strategy;
3811  mergenullsfirst[i] = opathkey->pk_nulls_first;
3812  i++;
3813  }
3814 
3815  /*
3816  * Note: it is not an error if we have additional pathkey elements (i.e.,
3817  * lop or lip isn't NULL here). The input paths might be better-sorted
3818  * than we need for the current mergejoin.
3819  */
3820 
3821  /*
3822  * Now we can build the mergejoin node.
3823  */
3824  join_plan = make_mergejoin(tlist,
3825  joinclauses,
3826  otherclauses,
3827  mergeclauses,
3828  mergefamilies,
3829  mergecollations,
3830  mergestrategies,
3831  mergenullsfirst,
3832  outer_plan,
3833  inner_plan,
3834  best_path->jpath.jointype);
3835 
3836  /* Costs of sort and material steps are included in path cost already */
3837  copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
3838 
3839  return join_plan;
3840 }
#define NIL
Definition: pg_list.h:69
List * path_mergeclauses
Definition: relation.h:1265
List * outersortkeys
Definition: relation.h:1266
double plan_rows
Definition: plannodes.h:117
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:3998
static List * get_switched_clauses(List *clauses, Relids outerrelids)
Definition: createplan.c:4479
bool materialize_inner
Definition: relation.h:1268
void extract_actual_join_clauses(List *restrictinfo_list, List **joinquals, List **otherquals)
Definition: restrictinfo.c:381
#define castNode(_type_, nodeptr)
Definition: nodes.h:577
Path * innerjoinpath
Definition: relation.h:1217
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:714
ParamPathInfo * param_info
Definition: relation.h:897
#define CP_SMALL_TLIST
Definition: createplan.c:67
Definition: nodes.h:508
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:331
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4556
EquivalenceClass * right_ec
Definition: relation.h:1686
unsigned int Oid
Definition: postgres_ext.h:31
int pk_strategy
Definition: relation.h:793
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
static Material * make_material(Plan *lefttree)
Definition: createplan.c:5672
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
bool pk_nulls_first
Definition: relation.h:794
#define ERROR
Definition: elog.h:43
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:698
List * joinrestrictinfo
Definition: relation.h:1219
RelOptInfo * parent
Definition: relation.h:894
bool outer_is_left
Definition: relation.h:1692
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:200
static void copy_plan_costsize(Plan *dest, Plan *src)
Definition: createplan.c:4664
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
Relids relids
Definition: relation.h:490
double cpu_operator_cost
Definition: costsize.c:108
#define lnext(lc)
Definition: pg_list.h:105
Path * outerjoinpath
Definition: relation.h:1216
List * pathkeys
Definition: relation.h:909
#define NULL
Definition: c.h:226
Path path
Definition: relation.h:1212
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
EquivalenceClass * pk_eclass
Definition: relation.h:791
static int list_length(const List *l)
Definition: pg_list.h:89
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:354
List * list_difference(const List *list1, const List *list2)
Definition: list.c:858
List * innersortkeys
Definition: relation.h:1267
Oid pk_opfamily
Definition: relation.h:792
static Sort * make_sort_from_pathkeys(Plan *lefttree, List *pathkeys)
Definition: createplan.c:5549
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)
Definition: createplan.c:5174
void * palloc(Size size)
Definition: mcxt.c:891
EquivalenceClass * left_ec
Definition: relation.h:1685
Join join
Definition: plannodes.h:660
JoinType jointype
Definition: relation.h:1214
int i
Cost total_cost
Definition: plannodes.h:112
JoinPath jpath
Definition: relation.h:1264
#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:4684
Plan plan
Definition: plannodes.h:618
static Result * create_minmaxagg_plan ( PlannerInfo root,
MinMaxAggPath best_path 
)
static

Definition at line 1855 of file createplan.c.

References Assert, build_path_tlist(), copy_generic_path_info(), create_plan(), PlannerInfo::hasInheritedTarget, lfirst, Query::limitCount, Query::limitOffset, make_limit(), make_result(), PlannerInfo::minmax_aggs, MinMaxAggPath::mmaggregates, NIL, NULL, Plan::parallel_aware, 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().

1856 {
1857  Result *plan;
1858  List *tlist;
1859  ListCell *lc;
1860 
1861  /* Prepare an InitPlan for each aggregate's subquery. */
1862  foreach(lc, best_path->mmaggregates)
1863  {
1864  MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
1865  PlannerInfo *subroot = mminfo->subroot;
1866  Query *subparse = subroot->parse;
1867  Plan *plan;
1868 
1869  /*
1870  * Generate the plan for the subquery. We already have a Path, but we
1871  * have to convert it to a Plan and attach a LIMIT node above it.
1872  * Since we are entering a different planner context (subroot),
1873  * recurse to create_plan not create_plan_recurse.
1874  */
1875  plan = create_plan(subroot, mminfo->path);
1876 
1877  plan = (Plan *) make_limit(plan,
1878  subparse->limitOffset,
1879  subparse->limitCount);
1880 
1881  /* Must apply correct cost/width data to Limit node */
1882  plan->startup_cost = mminfo->path->startup_cost;
1883  plan->total_cost = mminfo->pathcost;
1884  plan->plan_rows = 1;
1885  plan->plan_width = mminfo->path->pathtarget->width;
1886  plan->parallel_aware = false;
1887 
1888  /* Convert the plan into an InitPlan in the outer query. */
1889  SS_make_initplan_from_plan(root, subroot, plan, mminfo->param);
1890  }
1891 
1892  /* Generate the output plan --- basically just a Result */
1893  tlist = build_path_tlist(root, &best_path->path);
1894 
1895  plan = make_result(tlist, (Node *) best_path->quals, NULL);
1896 
1897  copy_generic_path_info(&plan->plan, (Path *) best_path);
1898 
1899  /*
1900  * During setrefs.c, we'll need to replace references to the Agg nodes
1901  * with InitPlan output params. (We can't just do that locally in the
1902  * MinMaxAgg node, because path nodes above here may have Agg references
1903  * as well.) Save the mmaggregates list to tell setrefs.c to do that.
1904  *
1905  * This doesn't work if we're in an inheritance subtree (see notes in
1906  * create_modifytable_plan). Fortunately we can't be because there would
1907  * never be aggregates in an UPDATE/DELETE; but let's Assert that.
1908  */
1909  Assert(!root->hasInheritedTarget);
1910  Assert(root->minmax_aggs == NIL);
1911  root->minmax_aggs = best_path->mmaggregates;
1912 
1913  return plan;
1914 }
Node * limitOffset
Definition: parsenodes.h:149
#define NIL
Definition: pg_list.h:69
double plan_rows
Definition: plannodes.h:117
PathTarget * pathtarget
Definition: relation.h:895
Query * parse
Definition: relation.h:152
Param * param
Definition: relation.h:1956
Definition: nodes.h:508
List * minmax_aggs
Definition: relation.h:280
List * quals
Definition: relation.h:1402
Plan * create_plan(PlannerInfo *root, Path *best_path)
Definition: createplan.c:293
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
Path * path
Definition: relation.h:1954
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:698
Cost startup_cost
Definition: relation.h:906
Node * limitCount
Definition: parsenodes.h:150
Cost startup_cost
Definition: plannodes.h:111
bool parallel_aware
Definition: plannodes.h:123
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6085
List * mmaggregates
Definition: relation.h:1401
int plan_width
Definition: plannodes.h:118
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
bool hasInheritedTarget
Definition: relation.h:292
void SS_make_initplan_from_plan(PlannerInfo *root, PlannerInfo *subroot, Plan *plan, Param *prm)
Definition: subselect.c:2892
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
Definition: createplan.c:6064
int width
Definition: relation.h:827
Cost total_cost
Definition: plannodes.h:112
Definition: pg_list.h:45
Definition: relation.h:888
PlannerInfo * subroot
Definition: relation.h:1953
static ModifyTable * create_modifytable_plan ( PlannerInfo root,
ModifyTablePath best_path 
)
static

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

2205 {
2206  ModifyTable *plan;
2207  List *subplans = NIL;
2208  ListCell *subpaths,
2209  *subroots;
2210 
2211  /* Build the plan for each input path */
2212  forboth(subpaths, best_path->subpaths,
2213  subroots, best_path->subroots)
2214  {
2215  Path *subpath = (Path *) lfirst(subpaths);
2216  PlannerInfo *subroot = (PlannerInfo *) lfirst(subroots);
2217  Plan *subplan;
2218 
2219  /*
2220  * In an inherited UPDATE/DELETE, reference the per-child modified
2221  * subroot while creating Plans from Paths for the child rel. This is
2222  * a kluge, but otherwise it's too hard to ensure that Plan creation
2223  * functions (particularly in FDWs) don't depend on the contents of
2224  * "root" matching what they saw at Path creation time. The main
2225  * downside is that creation functions for Plans that might appear
2226  * below a ModifyTable cannot expect to modify the contents of "root"
2227  * and have it "stick" for subsequent processing such as setrefs.c.
2228  * That's not great, but it seems better than the alternative.
2229  */
2230  subplan = create_plan_recurse(subroot, subpath, CP_EXACT_TLIST);
2231 
2232  /* Transfer resname/resjunk labeling, too, to keep executor happy */
2233  apply_tlist_labeling(subplan->targetlist, subroot->processed_tlist);
2234 
2235  subplans = lappend(subplans, subplan);
2236  }
2237 
2238  plan = make_modifytable(root,
2239  best_path->operation,
2240  best_path->canSetTag,
2241  best_path->nominalRelation,
2242  best_path->resultRelations,
2243  subplans,
2244  best_path->withCheckOptionLists,
2245  best_path->returningLists,
2246  best_path->rowMarks,
2247  best_path->onconflict,
2248  best_path->epqParam);
2249 
2250  copy_generic_path_info(&plan->plan, &best_path->path);
2251 
2252  return plan;
2253 }
#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:1476
OnConflictExpr * onconflict
Definition: relation.h:1478
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:174
Index nominalRelation
Definition: relation.h:1471
List * rowMarks
Definition: relation.h:1477
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
List * subroots
Definition: relation.h:1474
List * subpaths
Definition: relation.h:1473
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
static ModifyTable * make_modifytable(PlannerInfo *root, CmdType operation, bool canSetTag, Index nominalRelation, List *resultRelations, List *subplans, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, int epqParam)
Definition: createplan.c:6125
List * targetlist
Definition: plannodes.h:129
List * withCheckOptionLists
Definition: relation.h:1475
CmdType operation
Definition: relation.h:1469
List * resultRelations
Definition: relation.h:1472
List * processed_tlist
Definition: relation.h:276
#define CP_EXACT_TLIST
Definition: createplan.c:66
Definition: pg_list.h:45
Definition: relation.h:888
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:234
static NestLoop * create_nestloop_plan ( PlannerInfo root,
NestPath best_path 
)
static

Definition at line 3435 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::innerjoinpath, IS_OUTER_JOIN, IsA, NestLoop::join, JoinPath::joinrestrictinfo, JoinPath::jointype, lappend(), lfirst, list_delete_cell(), list_head(), lnext, make_nestloop(), next, NIL, NULL, order_qual_clauses(), JoinPath::outerjoinpath, Path::param_info, NestLoopParam::paramval, Path::parent, JoinPath::path, PlaceHolderInfo::ph_eval_at, Join::plan, RelOptInfo::relids, replace_nestloop_params(), and Var::varno.

Referenced by create_join_plan().

3437 {
3438  NestLoop *join_plan;
3439  Plan *outer_plan;
3440  Plan *inner_plan;
3441  List *tlist = build_path_tlist(root, &best_path->path);
3442  List *joinrestrictclauses = best_path->joinrestrictinfo;
3443  List *joinclauses;
3444  List *otherclauses;
3445  Relids outerrelids;
3446  List *nestParams;
3447  Relids saveOuterRels = root->curOuterRels;
3448  ListCell *cell;
3449  ListCell *prev;
3450  ListCell *next;
3451 
3452  /* NestLoop can project, so no need to be picky about child tlists */
3453  outer_plan = create_plan_recurse(root, best_path->outerjoinpath, 0);
3454 
3455  /* For a nestloop, include outer relids in curOuterRels for inner side */
3456  root->curOuterRels = bms_union(root->curOuterRels,
3457  best_path->outerjoinpath->parent->relids);
3458 
3459  inner_plan = create_plan_recurse(root, best_path->innerjoinpath, 0);
3460 
3461  /* Restore curOuterRels */
3462  bms_free(root->curOuterRels);
3463  root->curOuterRels = saveOuterRels;
3464 
3465  /* Sort join qual clauses into best execution order */
3466  joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
3467 
3468  /* Get the join qual clauses (in plain expression form) */
3469  /* Any pseudoconstant clauses are ignored here */
3470  if (IS_OUTER_JOIN(best_path->jointype))
3471  {
3472  extract_actual_join_clauses(joinrestrictclauses,
3473  &joinclauses, &otherclauses);
3474  }
3475  else
3476  {
3477  /* We can treat all clauses alike for an inner join */
3478  joinclauses = extract_actual_clauses(joinrestrictclauses, false);
3479  otherclauses = NIL;
3480  }
3481 
3482  /* Replace any outer-relation variables with nestloop params */
3483  if (best_path->path.param_info)
3484  {
3485  joinclauses = (List *)
3486  replace_nestloop_params(root, (Node *) joinclauses);
3487  otherclauses = (List *)
3488  replace_nestloop_params(root, (Node *) otherclauses);
3489  }
3490 
3491  /*
3492  * Identify any nestloop parameters that should be supplied by this join
3493  * node, and move them from root->curOuterParams to the nestParams list.
3494  */
3495  outerrelids = best_path->outerjoinpath->parent->relids;
3496  nestParams = NIL;
3497  prev = NULL;
3498  for (cell = list_head(root->curOuterParams); cell; cell = next)
3499  {
3500  NestLoopParam *nlp = (NestLoopParam *) lfirst(cell);
3501 
3502  next = lnext(cell);
3503  if (IsA(nlp->paramval, Var) &&
3504  bms_is_member(nlp->paramval->varno, outerrelids))
3505  {
3507  cell, prev);
3508  nestParams = lappend(nestParams, nlp);
3509  }
3510  else if (IsA(nlp->paramval, PlaceHolderVar) &&
3511  bms_overlap(((PlaceHolderVar *) nlp->paramval)->phrels,
3512  outerrelids) &&
3514  (PlaceHolderVar *) nlp->paramval,
3515  false)->ph_eval_at,
3516  outerrelids))
3517  {
3519  cell, prev);
3520  nestParams = lappend(nestParams, nlp);
3521  }
3522  else
3523  prev = cell;
3524  }
3525 
3526  join_plan = make_nestloop(tlist,
3527  joinclauses,
3528  otherclauses,
3529  nestParams,
3530  outer_plan,
3531  inner_plan,
3532  best_path->jointype);
3533 
3534  copy_generic_path_info(&join_plan->join.plan, &best_path->path);
3535 
3536  return join_plan;
3537 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:3998
Relids ph_eval_at
Definition: relation.h:1935
static int32 next
Definition: blutils.c:210
void extract_actual_join_clauses(List *restrictinfo_list, List **joinquals, List **otherquals)
Definition: restrictinfo.c:381
Relids curOuterRels
Definition: relation.h:307
Path * innerjoinpath
Definition: relation.h:1217
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:714
ParamPathInfo * param_info
Definition: relation.h:897
Definition: nodes.h:508
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4556
Definition: primnodes.h:141
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
Var * paramval
Definition: plannodes.h:644
Join join
Definition: plannodes.h:636
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:698
List * joinrestrictinfo
Definition: relation.h:1219
RelOptInfo * parent
Definition: relation.h:894
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:307
List * curOuterParams
Definition: relation.h:308
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:490
#define lnext(lc)
Definition: pg_list.h:105
List * lappend(List *list, void *datum)
Definition: list.c:128
static NestLoop * make_nestloop(List *tlist, List *joinclauses, List *otherclauses, List *nestParams, Plan *lefttree, Plan *righttree, JoinType jointype)
Definition: createplan.c:5103
Index varno
Definition: primnodes.h:144
List * list_delete_cell(List *list, ListCell *cell, ListCell *prev)
Definition: list.c:528
Path * outerjoinpath
Definition: relation.h:1216
void bms_free(Bitmapset *a)
Definition: bitmapset.c:200
#define NULL
Definition: c.h:226
Path path
Definition: relation.h:1212
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:217
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:354
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:442
JoinType jointype
Definition: relation.h:1214
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:419
Plan plan
Definition: plannodes.h:618
Plan* create_plan ( PlannerInfo root,
Path best_path 
)

Definition at line 293 of file createplan.c.

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

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

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

Definition at line 344 of file createplan.c.

References Assert, create_agg_plan(), create_append_plan(), create_gather_plan(), create_group_plan(), create_groupingsets_plan(), create_join_plan(), create_limit_plan(), create_lockrows_plan(), create_material_plan(), create_merge_append_plan(), create_minmaxagg_plan(), create_modifytable_plan(), create_project_set_plan(), create_projection_plan(), create_recursiveunion_plan(), create_result_plan(), create_scan_plan(), create_setop_plan(), create_sort_plan(), create_unique_plan(), create_upper_unique_plan(), create_windowagg_plan(), elog, ERROR, IsA, NULL, Path::pathtype, T_Agg, T_Append, T_BitmapHeapScan, T_CteScan, T_CustomScan, T_ForeignScan, T_FunctionScan, T_Gather, T_Group, T_HashJoin, T_IndexOnlyScan, T_IndexScan, T_Limit, T_LockRows, T_Material, T_MergeAppend, T_MergeJoin, T_ModifyTable, T_NestLoop, T_ProjectSet, T_RecursiveUnion, T_Result, T_SampleScan, T_SeqScan, T_SetOp, T_Sort, T_SubqueryScan, 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_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().

345 {
346  Plan *plan;
347 
348  switch (best_path->pathtype)
349  {
350  case T_SeqScan:
351  case T_SampleScan:
352  case T_IndexScan:
353  case T_IndexOnlyScan:
354  case T_BitmapHeapScan:
355  case T_TidScan:
356  case T_SubqueryScan:
357  case T_FunctionScan:
358  case T_ValuesScan:
359  case T_CteScan:
360  case T_WorkTableScan:
361  case T_ForeignScan:
362  case T_CustomScan:
363  plan = create_scan_plan(root, best_path, flags);
364  break;
365  case T_HashJoin:
366  case T_MergeJoin:
367  case T_NestLoop:
368  plan = create_join_plan(root,
369  (JoinPath *) best_path);
370  break;
371  case T_Append:
372  plan = create_append_plan(root,
373  (AppendPath *) best_path);
374  break;
375  case T_MergeAppend:
376  plan = create_merge_append_plan(root,
377  (MergeAppendPath *) best_path);
378  break;
379  case T_Result:
380  if (IsA(best_path, ProjectionPath))
381  {
382  plan = create_projection_plan(root,
383  (ProjectionPath *) best_path);
384  }
385  else if (IsA(best_path, MinMaxAggPath))
386  {
387  plan = (Plan *) create_minmaxagg_plan(root,
388  (MinMaxAggPath *) best_path);
389  }
390  else
391  {
392  Assert(IsA(best_path, ResultPath));
393  plan = (Plan *) create_result_plan(root,
394  (ResultPath *) best_path);
395  }
396  break;
397  case T_ProjectSet:
398  plan = (Plan *) create_project_set_plan(root,
399  (ProjectSetPath *) best_path);
400  break;
401  case T_Material:
402  plan = (Plan *) create_material_plan(root,
403  (MaterialPath *) best_path,
404  flags);
405  break;
406  case T_Unique:
407  if (IsA(best_path, UpperUniquePath))
408  {
409  plan = (Plan *) create_upper_unique_plan(root,
410  (UpperUniquePath *) best_path,
411  flags);
412  }
413  else
414  {
415  Assert(IsA(best_path, UniquePath));
416  plan = create_unique_plan(root,
417  (UniquePath *) best_path,
418  flags);
419  }
420  break;
421  case T_Gather:
422  plan = (Plan *) create_gather_plan(root,
423  (GatherPath *) best_path);
424  break;
425  case T_Sort:
426  plan = (Plan *) create_sort_plan(root,
427  (SortPath *) best_path,
428  flags);
429  break;
430  case T_Group:
431  plan = (Plan *) create_group_plan(root,
432  (GroupPath *) best_path);
433  break;
434  case T_Agg:
435  if (IsA(best_path, GroupingSetsPath))
436  plan = create_groupingsets_plan(root,
437  (GroupingSetsPath *) best_path);
438  else
439  {
440  Assert(IsA(best_path, AggPath));
441  plan = (Plan *) create_agg_plan(root,
442  (AggPath *) best_path);
443  }
444  break;
445  case T_WindowAgg:
446  plan = (Plan *) create_windowagg_plan(root,
447  (WindowAggPath *) best_path);
448  break;
449  case T_SetOp:
450  plan = (Plan *) create_setop_plan(root,
451  (SetOpPath *) best_path,
452  flags);
453  break;
454  case T_RecursiveUnion:
455  plan = (Plan *) create_recursiveunion_plan(root,
456  (RecursiveUnionPath *) best_path);
457  break;
458  case T_LockRows:
459  plan = (Plan *) create_lockrows_plan(root,
460  (LockRowsPath *) best_path,
461  flags);
462  break;
463  case T_ModifyTable:
464  plan = (Plan *) create_modifytable_plan(root,
465  (ModifyTablePath *) best_path);
466  break;
467  case T_Limit:
468  plan = (Plan *) create_limit_plan(root,
469  (LimitPath *) best_path,
470  flags);
471  break;
472  default:
473  elog(ERROR, "unrecognized node type: %d",
474  (int) best_path->pathtype);
475  plan = NULL; /* keep compiler quiet */
476  break;
477  }
478 
479  return plan;
480 }
static Plan * create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
Definition: createplan.c:1210
static Result * create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path)
Definition: createplan.c:1855
static Result * create_result_plan(PlannerInfo *root, ResultPath *best_path)
Definition: createplan.c:1131
Definition: nodes.h:75
static Group * create_group_plan(PlannerInfo *root, GroupPath *best_path)
Definition: createplan.c:1560
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
Definition: nodes.h:77
static Sort * create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags)
Definition: createplan.c:1533
static Plan * create_scan_plan(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:487
static LockRows * create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path, int flags)
Definition: createplan.c:2181
static ProjectSet * create_project_set_plan(PlannerInfo *root, ProjectSetPath *best_path)
Definition: createplan.c:1156
static RecursiveUnion * create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path)
Definition: createplan.c:2145
static Agg * create_agg_plan(PlannerInfo *root, AggPath *best_path)
Definition: createplan.c:1625
static Unique * create_upper_unique_plan(PlannerInfo *root, UpperUniquePath *best_path, int flags)
Definition: createplan.c:1597
Definition: nodes.h:48
Definition: nodes.h:73
static ModifyTable * create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path)
Definition: createplan.c:2204
NodeTag pathtype
Definition: relation.h:892
Definition: nodes.h:45
#define ERROR
Definition: elog.h:43
static Plan * create_projection_plan(PlannerInfo *root, ProjectionPath *best_path)
Definition: createplan.c:1448
Definition: nodes.h:74
static Plan * create_join_plan(PlannerInfo *root, JoinPath *best_path)
Definition: createplan.c:899
static SetOp * create_setop_plan(PlannerInfo *root, SetOpPath *best_path, int flags)
Definition: createplan.c:2109
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
static Gather * create_gather_plan(PlannerInfo *root, GatherPath *best_path)
Definition: createplan.c:1412
static Plan * create_append_plan(PlannerInfo *root, AppendPath *best_path)
Definition: createplan.c:959
static WindowAgg * create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
Definition: createplan.c:1923
static Material * create_material_plan(PlannerInfo *root, MaterialPath *best_path, int flags)
Definition: createplan.c:1182
Definition: nodes.h:80
Definition: nodes.h:78
static Plan * create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path)
Definition: createplan.c:1706
static Limit * create_limit_plan(PlannerInfo *root, LimitPath *best_path, int flags)
Definition: createplan.c:2262
#define elog
Definition: elog.h:219
static Plan * create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path)
Definition: createplan.c:1024
Definition: nodes.h:82
static ProjectSet * create_project_set_plan ( PlannerInfo root,
ProjectSetPath best_path 
)
static

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

1157 {
1158  ProjectSet *plan;
1159  Plan *subplan;
1160  List *tlist;
1161 
1162  /* Since we intend to project, we don't need to constrain child tlist */
1163  subplan = create_plan_recurse(root, best_path->subpath, 0);
1164 
1165  tlist = build_path_tlist(root, &best_path->path);
1166 
1167  plan = make_project_set(tlist, subplan);
1168 
1169  copy_generic_path_info(&plan->plan, (Path *) best_path);
1170 
1171  return plan;
1172 }
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
static ProjectSet * make_project_set(List *tlist, Plan *subplan)
Definition: createplan.c:6106
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:698
Path * subpath
Definition: relation.h:1316
Definition: pg_list.h:45
Definition: relation.h:888
Plan plan
Definition: plannodes.h:187
static Plan * create_projection_plan ( PlannerInfo root,
ProjectionPath best_path 
)
static

Definition at line 1448 of file createplan.c.

References build_path_tlist(), copy_generic_path_info(), create_plan_recurse(), is_projection_capable_path(), make_result(), NULL, 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().

1449 {
1450  Plan *plan;
1451  Plan *subplan;
1452  List *tlist;
1453 
1454  /* Since we intend to project, we don't need to constrain child tlist */
1455  subplan = create_plan_recurse(root, best_path->subpath, 0);
1456 
1457  tlist = build_path_tlist(root, &best_path->path);
1458 
1459  /*
1460  * We might not really need a Result node here, either because the subplan
1461  * can project or because it's returning the right list of expressions
1462  * anyway. Usually create_projection_path will have detected that and set
1463  * dummypp if we don't need a Result; but its decision can't be final,
1464  * because some createplan.c routines change the tlists of their nodes.
1465  * (An example is that create_merge_append_plan might add resjunk sort
1466  * columns to a MergeAppend.) So we have to recheck here. If we do
1467  * arrive at a different answer than create_projection_path did, we'll
1468  * have made slightly wrong cost estimates; but label the plan with the
1469  * cost estimates we actually used, not "corrected" ones. (XXX this could
1470  * be cleaned up if we moved more of the sortcolumn setup logic into Path
1471  * creation, but that would add expense to creating Paths we might end up
1472  * not using.)
1473  */
1474  if (is_projection_capable_path(best_path->subpath) ||
1475  tlist_same_exprs(tlist, subplan->targetlist))
1476  {
1477  /* Don't need a separate Result, just assign tlist to subplan */
1478  plan = subplan;
1479  plan->targetlist = tlist;
1480 
1481  /* Label plan with the estimated costs we actually used */
1482  plan->startup_cost = best_path->path.startup_cost;
1483  plan->total_cost = best_path->path.total_cost;
1484  plan->plan_rows = best_path->path.rows;
1485  plan->plan_width = best_path->path.pathtarget->width;
1486  /* ... but be careful not to munge subplan's parallel-aware flag */
1487  }
1488  else
1489  {
1490  /* We need a Result node */
1491  plan = (Plan *) make_result(tlist, NULL, subplan);
1492 
1493  copy_generic_path_info(plan, (Path *) best_path);
1494  }
1495 
1496  return plan;
1497 }
double plan_rows
Definition: plannodes.h:117
PathTarget * pathtarget
Definition: relation.h:895
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:698
Cost startup_cost
Definition: relation.h:906
Cost startup_cost
Definition: plannodes.h:111
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6085
Cost total_cost
Definition: relation.h:907
int plan_width
Definition: plannodes.h:118
#define NULL
Definition: c.h:226
double rows
Definition: relation.h:905
bool tlist_same_exprs(List *tlist1, List *tlist2)
Definition: tlist.c:221
List * targetlist
Definition: plannodes.h:129
int width
Definition: relation.h:827
bool is_projection_capable_path(Path *path)
Definition: createplan.c:6261
Cost total_cost
Definition: plannodes.h:112
Path * subpath
Definition: relation.h:1304
Definition: pg_list.h:45
Definition: relation.h:888
static RecursiveUnion * create_recursiveunion_plan ( PlannerInfo root,
RecursiveUnionPath best_path 
)
static

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

2146 {
2147  RecursiveUnion *plan;
2148  Plan *leftplan;
2149  Plan *rightplan;
2150  List *tlist;
2151  long numGroups;
2152 
2153  /* Need both children to produce same tlist, so force it */
2154  leftplan = create_plan_recurse(root, best_path->leftpath, CP_EXACT_TLIST);
2155  rightplan = create_plan_recurse(root, best_path->rightpath, CP_EXACT_TLIST);
2156 
2157  tlist = build_path_tlist(root, &best_path->path);
2158 
2159  /* Convert numGroups to long int --- but 'ware overflow! */
2160  numGroups = (long) Min(best_path->numGroups, (double) LONG_MAX);
2161 
2162  plan = make_recursive_union(tlist,
2163  leftplan,
2164  rightplan,
2165  best_path->wtParam,
2166  best_path->distinctList,
2167  numGroups);
2168 
2169  copy_generic_path_info(&plan->plan, (Path *) best_path);
2170 
2171  return plan;
2172 }
static RecursiveUnion * make_recursive_union(List *tlist, Plan *lefttree, Plan *righttree, int wtParam, List *distinctList, long numGroups)
Definition: createplan.c:5021
#define Min(x, y)
Definition: c.h:802
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:698
#define CP_EXACT_TLIST
Definition: createplan.c:66
Definition: pg_list.h:45
Definition: relation.h:888
static Result * create_result_plan ( PlannerInfo root,
ResultPath best_path 
)
static

Definition at line 1131 of file createplan.c.

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

Referenced by create_plan_recurse().

1132 {
1133  Result *plan;
1134  List *tlist;
1135  List *quals;
1136 
1137  tlist = build_path_tlist(root, &best_path->path);
1138 
1139  /* best_path->quals is just bare clauses */
1140  quals = order_qual_clauses(root, best_path->quals);
1141 
1142  plan = make_result(tlist, (Node *) quals, NULL);
1143 
1144  copy_generic_path_info(&plan->plan, (Path *) best_path);
1145 
1146  return plan;
1147 }
Plan plan
Definition: plannodes.h:175
Definition: nodes.h:508
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4556
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
Path path
Definition: relation.h:1150
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:698
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:6085
#define NULL
Definition: c.h:226
List * quals
Definition: relation.h:1151
Definition: pg_list.h:45
Definition: relation.h:888
static SampleScan * create_samplescan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 2331 of file createplan.c.

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

Referenced by create_scan_plan().

2333 {
2334  SampleScan *scan_plan;
2335  Index scan_relid = best_path->parent->relid;
2336  RangeTblEntry *rte;
2337  TableSampleClause *tsc;
2338 
2339  /* it should be a base rel with a tablesample clause... */
2340  Assert(scan_relid > 0);
2341  rte = planner_rt_fetch(scan_relid, root);
2342  Assert(rte->rtekind == RTE_RELATION);
2343  tsc = rte->tablesample;
2344  Assert(tsc != NULL);
2345 
2346  /* Sort clauses into best execution order */
2347  scan_clauses = order_qual_clauses(root, scan_clauses);
2348 
2349  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2350  scan_clauses = extract_actual_clauses(scan_clauses, false);
2351 
2352  /* Replace any outer-relation variables with nestloop params */
2353  if (best_path->param_info)
2354  {
2355  scan_clauses = (List *)
2356  replace_nestloop_params(root, (Node *) scan_clauses);
2357  tsc = (TableSampleClause *)
2358  replace_nestloop_params(root, (Node *) tsc);
2359  }
2360 
2361  scan_plan = make_samplescan(tlist,
2362  scan_clauses,
2363  scan_relid,
2364  tsc);
2365 
2366  copy_generic_path_info(&scan_plan->scan.plan, best_path);
2367 
2368  return scan_plan;
2369 }
Plan plan
Definition: plannodes.h:305
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:3998
ParamPathInfo * param_info
Definition: relation.h:897
Definition: nodes.h:508
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4556
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
#define planner_rt_fetch(rti, root)
Definition: relation.h:320
Scan scan
Definition: plannodes.h:321
RelOptInfo * parent
Definition: relation.h:894
Index relid
Definition: relation.h:518
static SampleScan * make_samplescan(List *qptlist, List *qpqual, Index scanrelid, TableSampleClause *tsc)
Definition: createplan.c:4736
unsigned int Index
Definition: c.h:362
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:354
RTEKind rtekind
Definition: parsenodes.h:882
Definition: pg_list.h:45
struct TableSampleClause * tablesample
Definition: parsenodes.h:895
static Plan * create_scan_plan ( PlannerInfo root,
Path best_path,
int  flags 
)
static

Definition at line 487 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_samplescan_plan(), create_seqscan_plan(), create_subqueryscan_plan(), create_tidscan_plan(), create_valuesscan_plan(), create_worktablescan_plan(), elog, ERROR, get_gating_quals(), list_concat(), list_copy(), NIL, NULL, Path::param_info, Path::parent, Path::pathtarget, Path::pathtype, ParamPathInfo::ppi_clauses, T_BitmapHeapScan, T_CteScan, T_CustomScan, T_ForeignScan, T_FunctionScan, T_IndexOnlyScan, T_IndexScan, T_SampleScan, T_SeqScan, T_SubqueryScan, T_TidScan, T_ValuesScan, T_WorkTableScan, and use_physical_tlist().

Referenced by create_plan_recurse().

488 {
489  RelOptInfo *rel = best_path->parent;
490  List *scan_clauses;
491  List *gating_clauses;
492  List *tlist;
493  Plan *plan;
494 
495  /*
496  * Extract the relevant restriction clauses from the parent relation. The
497  * executor must apply all these restrictions during the scan, except for
498  * pseudoconstants which we'll take care of below.
499  *
500  * If this is a plain indexscan or index-only scan, we need not consider
501  * restriction clauses that are implied by the index's predicate, so use
502  * indrestrictinfo not baserestrictinfo. Note that we can't do that for
503  * bitmap indexscans, since there's not necessarily a single index
504  * involved; but it doesn't matter since create_bitmap_scan_plan() will be
505  * able to get rid of such clauses anyway via predicate proof.
506  */
507  switch (best_path->pathtype)
508  {
509  case T_IndexScan:
510  case T_IndexOnlyScan:
511  scan_clauses = castNode(IndexPath, best_path)->indexinfo->indrestrictinfo;
512  break;
513  default:
514  scan_clauses = rel->baserestrictinfo;
515  break;
516  }
517 
518  /*
519  * If this is a parameterized scan, we also need to enforce all the join
520  * clauses available from the outer relation(s).
521  *
522  * For paranoia's sake, don't modify the stored baserestrictinfo list.
523  */
524  if (best_path->param_info)
525  scan_clauses = list_concat(list_copy(scan_clauses),
526  best_path->param_info->ppi_clauses);
527 
528  /*
529  * Detect whether we have any pseudoconstant quals to deal with. Then, if
530  * we'll need a gating Result node, it will be able to project, so there
531  * are no requirements on the child's tlist.
532  */
533  gating_clauses = get_gating_quals(root, scan_clauses);
534  if (gating_clauses)
535  flags = 0;
536 
537  /*
538  * For table scans, rather than using the relation targetlist (which is
539  * only those Vars actually needed by the query), we prefer to generate a
540  * tlist containing all Vars in order. This will allow the executor to
541  * optimize away projection of the table tuples, if possible.
542  */
543  if (use_physical_tlist(root, best_path, flags))
544  {
545  if (best_path->pathtype == T_IndexOnlyScan)
546  {
547  /* For index-only scan, the preferred tlist is the index's */
548  tlist = copyObject(((IndexPath *) best_path)->indexinfo->indextlist);
549 
550  /*
551  * Transfer any sortgroupref data to the replacement tlist, unless
552  * we don't care because the gating Result will handle it.
553  */
554  if (!gating_clauses)
556  }
557  else
558  {
559  tlist = build_physical_tlist(root, rel);
560  if (tlist == NIL)
561  {
562  /* Failed because of dropped cols, so use regular method */
563  tlist = build_path_tlist(root, best_path);
564  }
565  else
566  {
567  /* As above, transfer sortgroupref data to replacement tlist */
568  if (!gating_clauses)
570  }
571  }
572  }
573  else
574  {
575  tlist = build_path_tlist(root, best_path);
576  }
577 
578  switch (best_path->pathtype)
579  {
580  case T_SeqScan:
581  plan = (Plan *) create_seqscan_plan(root,
582  best_path,
583  tlist,
584  scan_clauses);
585  break;
586 
587  case T_SampleScan:
588  plan = (Plan *) create_samplescan_plan(root,
589  best_path,
590  tlist,
591  scan_clauses);
592  break;
593 
594  case T_IndexScan:
595  plan = (Plan *) create_indexscan_plan(root,
596  (IndexPath *) best_path,
597  tlist,
598  scan_clauses,
599  false);
600  break;
601 
602  case T_IndexOnlyScan:
603  plan = (Plan *) create_indexscan_plan(root,
604  (IndexPath *) best_path,
605  tlist,
606  scan_clauses,
607  true);
608  break;
609 
610  case T_BitmapHeapScan:
611  plan = (Plan *) create_bitmap_scan_plan(root,
612  (BitmapHeapPath *) best_path,
613  tlist,
614  scan_clauses);
615  break;
616 
617  case T_TidScan:
618  plan = (Plan *) create_tidscan_plan(root,
619  (TidPath *) best_path,
620  tlist,
621  scan_clauses);
622  break;
623 
624  case T_SubqueryScan:
625  plan = (Plan *) create_subqueryscan_plan(root,
626  (SubqueryScanPath *) best_path,
627  tlist,
628  scan_clauses);
629  break;
630 
631  case T_FunctionScan:
632  plan = (Plan *) create_functionscan_plan(root,
633  best_path,
634  tlist,
635  scan_clauses);
636  break;
637 
638  case T_ValuesScan:
639  plan = (Plan *) create_valuesscan_plan(root,
640  best_path,
641  tlist,
642  scan_clauses);
643  break;
644 
645  case T_CteScan:
646  plan = (Plan *) create_ctescan_plan(root,
647  best_path,
648  tlist,
649  scan_clauses);
650  break;
651 
652  case T_WorkTableScan:
653  plan = (Plan *) create_worktablescan_plan(root,
654  best_path,
655  tlist,
656  scan_clauses);
657  break;
658 
659  case T_ForeignScan:
660  plan = (Plan *) create_foreignscan_plan(root,
661  (ForeignPath *) best_path,
662  tlist,
663  scan_clauses);
664  break;
665 
666  case T_CustomScan:
667  plan = (Plan *) create_customscan_plan(root,
668  (CustomPath *) best_path,
669  tlist,
670  scan_clauses);
671  break;
672 
673  default:
674  elog(ERROR, "unrecognized node type: %d",
675  (int) best_path->pathtype);
676  plan = NULL; /* keep compiler quiet */
677  break;
678  }
679 
680  /*
681  * If there are any pseudoconstant clauses attached to this node, insert a
682  * gating Result node that evaluates the pseudoconstants as one-time
683  * quals.
684  */
685  if (gating_clauses)
686  plan = create_gating_plan(root, best_path, plan, gating_clauses);
687 
688  return plan;
689 }
#define NIL
Definition: pg_list.h:69
static BitmapHeapScan * create_bitmap_scan_plan(PlannerInfo *root, BitmapHeapPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:2557
static ForeignScan * create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3220
PathTarget * pathtarget
Definition: relation.h:895
static CustomScan * create_customscan_plan(PlannerInfo *root, CustomPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3364
#define castNode(_type_, nodeptr)
Definition: nodes.h:577
List * baserestrictinfo
Definition: relation.h:544
ParamPathInfo * param_info
Definition: relation.h:897
List * list_copy(const List *oldlist)
Definition: list.c:1160
List * list_concat(List *list1, List *list2)
Definition: list.c:321
void * copyObject(const void *from)
Definition: copyfuncs.c:4475
NodeTag pathtype
Definition: relation.h:892
#define ERROR
Definition: elog.h:43
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:698
RelOptInfo * parent
Definition: relation.h:894
static SeqScan * create_seqscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:2293
static SubqueryScan * create_subqueryscan_plan(PlannerInfo *root, SubqueryScanPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:2930
static CteScan * create_ctescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3067
List * build_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
Definition: plancat.c:1388
void apply_pathtarget_labeling_to_tlist(List *tlist, PathTarget *target)
Definition: tlist.c:736
static TidScan * create_tidscan_plan(PlannerInfo *root, TidPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:2878
#define NULL
Definition: c.h:226
List * ppi_clauses
Definition: relation.h:855
static SampleScan * create_samplescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:2331
static FunctionScan * create_functionscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:2980
static Plan * create_gating_plan(PlannerInfo *root, Path *path, Plan *plan, List *gating_quals)
Definition: createplan.c:860
static bool use_physical_tlist(PlannerInfo *root, Path *path, int flags)
Definition: createplan.c:738
#define elog
Definition: elog.h:219
static WorkTableScan * create_worktablescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3160
Definition: pg_list.h:45
static ValuesScan * create_valuesscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3023
static Scan * create_indexscan_plan(PlannerInfo *root, IndexPath *best_path, List *tlist, List *scan_clauses, bool indexonly)
Definition: createplan.c:2382
static List * get_gating_quals(PlannerInfo *root, List *quals)
Definition: createplan.c:840
static SeqScan * create_seqscan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 2293 of file createplan.c.

References Assert, copy_generic_path_info(), extract_actual_clauses(), make_seqscan(), order_qual_clauses(), Path::param_info, Path::parent, Scan::plan, RelOptInfo::relid, replace_nestloop_params(), RTE_RELATION, and RelOptInfo::rtekind.

Referenced by create_scan_plan().

2295 {
2296  SeqScan *scan_plan;
2297  Index scan_relid = best_path->parent->relid;
2298 
2299  /* it should be a base rel... */
2300  Assert(scan_relid > 0);
2301  Assert(best_path->parent->rtekind == RTE_RELATION);
2302 
2303  /* Sort clauses into best execution order */
2304  scan_clauses = order_qual_clauses(root, scan_clauses);
2305 
2306  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2307  scan_clauses = extract_actual_clauses(scan_clauses, false);
2308 
2309  /* Replace any outer-relation variables with nestloop params */
2310  if (best_path->param_info)
2311  {
2312  scan_clauses = (List *)
2313  replace_nestloop_params(root, (Node *) scan_clauses);
2314  }
2315 
2316  scan_plan = make_seqscan(tlist,
2317  scan_clauses,
2318  scan_relid);
2319 
2320  copy_generic_path_info(&scan_plan->plan, best_path);
2321 
2322  return scan_plan;
2323 }
Plan plan
Definition: plannodes.h:305
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:3998
static SeqScan * make_seqscan(List *qptlist, List *qpqual, Index scanrelid)
Definition: createplan.c:4719
ParamPathInfo * param_info
Definition: relation.h:897
Definition: nodes.h:508
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:4556
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
RelOptInfo * parent
Definition: relation.h:894
Index relid
Definition: relation.h:518
unsigned int Index
Definition: c.h:362
RTEKind rtekind
Definition: relation.h:520
#define Assert(condition)
Definition: c.h:671
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:354
Definition: pg_list.h:45
static SetOp * create_setop_plan ( PlannerInfo root,
SetOpPath best_path,
int  flags 
)
static

Definition at line 2109 of file createplan.c.

References SetOpPath::cmd, copy_generic_path_info(), CP_LABEL_TLIST, create_plan_recurse(), SetOpPath::distinctList, SetOpPath::firstFlag, SetOpPath::flagColIdx, make_setop(), Min, SetOpPath::numGroups, SetOp::plan, SetOpPath::strategy, and SetOpPath::subpath.

Referenced by create_plan_recurse().

2110 {
2111  SetOp *plan;
2112  Plan *subplan;
2113  long numGroups;
2114 
2115  /*
2116  * SetOp doesn't project, so tlist requirements pass through; moreover we
2117  * need grouping columns to be labeled.
2118  */
2119  subplan = create_plan_recurse(root, best_path->subpath,
2120  flags | CP_LABEL_TLIST);
2121 
2122  /* Convert numGroups to long int --- but 'ware overflow! */
2123  numGroups = (long) Min(best_path->numGroups, (double) LONG_MAX);
2124 
2125  plan = make_setop(best_path->cmd,
2126  best_path->strategy,
2127  subplan,
2128  best_path->distinctList,
2129  best_path->flagColIdx,
2130  best_path->firstFlag,
2131  numGroups);
2132 
2133  copy_generic_path_info(&plan->plan, (Path *) best_path);
2134 
2135  return plan;
2136 }
List * distinctList
Definition: relation.h:1429
SetOpStrategy strategy
Definition: relation.h:1428
Path * subpath
Definition: relation.h:1426
#define Min(x, y)
Definition: c.h:802
double numGroups
Definition: relation.h:1432
SetOpCmd cmd
Definition: relation.h:1427
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
AttrNumber flagColIdx
Definition: relation.h:1430
static SetOp * make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree, List *distinctList, AttrNumber flagColIdx, int firstFlag, long numGroups)
Definition: createplan.c:5990
Plan plan
Definition: plannodes.h:814
#define CP_LABEL_TLIST
Definition: createplan.c:68
int firstFlag
Definition: relation.h:1431
Definition: relation.h:888
static Sort * create_sort_plan ( PlannerInfo root,
SortPath best_path,
int  flags 
)
static

Definition at line 1533 of file createplan.c.

References copy_generic_path_info(), CP_SMALL_TLIST, create_plan_recurse(), make_sort_from_pathkeys(), SortPath::path, Path::pathkeys, Sort::plan, and SortPath::subpath.

Referenced by create_plan_recurse().

1534 {
1535  Sort *plan;
1536  Plan *subplan;
1537 
1538  /*
1539  * We don't want any excess columns in the sorted tuples, so request a
1540  * smaller tlist. Otherwise, since Sort doesn't project, tlist
1541  * requirements pass through.
1542  */
1543  subplan = create_plan_recurse(root, best_path->subpath,
1544  flags | CP_SMALL_TLIST);
1545 
1546  plan = make_sort_from_pathkeys(subplan, best_path->path.pathkeys);
1547 
1548  copy_generic_path_info(&plan->plan, (Path *) best_path);
1549 
1550  return plan;
1551 }
Path * subpath
Definition: relation.h:1330
Plan plan
Definition: plannodes.h:694
#define CP_SMALL_TLIST
Definition: createplan.c:67
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:344
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:4650
List * pathkeys
Definition: relation.h:909
static Sort * make_sort_from_pathkeys(Plan *lefttree, List *pathkeys)
Definition: createplan.c:5549
Path path
Definition: relation.h:1329
Definition: relation.h:888
static SubqueryScan * create_subqueryscan_plan ( PlannerInfo root,
SubqueryScanPath best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 2930 of file createplan.c.

References Assert, copy_generic_path_info(), create_plan(), extract_actual_clauses(), make_subqueryscan(), order_qual_clauses(), Path::param_info, Path::parent, SubqueryScanPath::path, Scan::plan, process_subquery_nestloop_params(), RelOptInfo::relid, replace_nestloop_params(), RTE_SUBQUERY, RelOptInfo::rtekind, SubqueryScan::scan, SubqueryScanPath::subpath, RelOptInfo::subplan_params, and RelOptInfo::subroot.

Referenced by create_scan_plan().

2932 {
2933  SubqueryScan *scan_plan;
2934  RelOptInfo *rel = best_path->path.parent;
2935  Index scan_relid = rel->relid;
2936  Plan *subplan;
2937 
2938  /* it should be a subquery base rel... */
2939  Assert(scan_relid > 0);
2940  Assert(rel->rtekind == RTE_SUBQUERY);
2941 
2942  /*
2943  * Recursively create Plan from Path for subquery. Since we are entering
2944  * a different planner context (subroot), recurse to create_plan not
2945  * create_plan_recurse.
2946  */
2947  subplan = create_plan(rel->subroot, best_path->subpath);
2948 
2949  /* Sort clauses into best execution order */
2950  scan_clauses = order_qual_clauses(root, scan_clauses);
2951 
2952  /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2953  scan_clauses = extract_actual_clauses(scan_clauses, false);
2954 
2955  /* Replace any outer-relation variables with nestloop params */
2956