PostgreSQL Source Code git master
createplan.c File Reference
#include "postgres.h"
#include <math.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/optimizer.h"
#include "optimizer/paramassign.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/placeholder.h"
#include "optimizer/plancat.h"
#include "optimizer/planmain.h"
#include "optimizer/prep.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/subselect.h"
#include "optimizer/tlist.h"
#include "parser/parse_clause.h"
#include "parser/parsetree.h"
#include "partitioning/partprune.h"
#include "tcop/tcopprot.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 */
 
#define CP_IGNORE_TLIST   0x0008 /* caller will replace tlist */
 

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 bool mark_async_capable_plan (Plan *plan, Path *path)
 
static Plancreate_append_plan (PlannerInfo *root, AppendPath *best_path, int flags)
 
static Plancreate_merge_append_plan (PlannerInfo *root, MergeAppendPath *best_path, int flags)
 
static Resultcreate_group_result_plan (PlannerInfo *root, GroupResultPath *best_path)
 
static ProjectSetcreate_project_set_plan (PlannerInfo *root, ProjectSetPath *best_path)
 
static Materialcreate_material_plan (PlannerInfo *root, MaterialPath *best_path, int flags)
 
static Memoizecreate_memoize_plan (PlannerInfo *root, MemoizePath *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, int flags)
 
static Planinject_projection_plan (Plan *subplan, List *tlist, bool parallel_safe)
 
static Sortcreate_sort_plan (PlannerInfo *root, SortPath *best_path, int flags)
 
static IncrementalSortcreate_incrementalsort_plan (PlannerInfo *root, IncrementalSortPath *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 LockRowscreate_lockrows_plan (PlannerInfo *root, LockRowsPath *best_path, int flags)
 
static ModifyTablecreate_modifytable_plan (PlannerInfo *root, ModifyTablePath *best_path)
 
static Limitcreate_limit_plan (PlannerInfo *root, LimitPath *best_path, int flags)
 
static SeqScancreate_seqscan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static SampleScancreate_samplescan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static Scancreate_indexscan_plan (PlannerInfo *root, IndexPath *best_path, List *tlist, List *scan_clauses, bool indexonly)
 
static BitmapHeapScancreate_bitmap_scan_plan (PlannerInfo *root, BitmapHeapPath *best_path, List *tlist, List *scan_clauses)
 
static Plancreate_bitmap_subplan (PlannerInfo *root, Path *bitmapqual, List **qual, List **indexqual, List **indexECs)
 
static void bitmap_subplan_mark_shared (Plan *plan)
 
static TidScancreate_tidscan_plan (PlannerInfo *root, TidPath *best_path, List *tlist, List *scan_clauses)
 
static TidRangeScancreate_tidrangescan_plan (PlannerInfo *root, TidRangePath *best_path, List *tlist, List *scan_clauses)
 
static SubqueryScancreate_subqueryscan_plan (PlannerInfo *root, SubqueryScanPath *best_path, List *tlist, List *scan_clauses)
 
static FunctionScancreate_functionscan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static ValuesScancreate_valuesscan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static TableFuncScancreate_tablefuncscan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static CteScancreate_ctescan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static NamedTuplestoreScancreate_namedtuplestorescan_plan (PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
 
static Resultcreate_resultscan_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 fix_indexqual_references (PlannerInfo *root, IndexPath *index_path, List **stripped_indexquals_p, List **fixed_indexquals_p)
 
static Listfix_indexorderby_references (PlannerInfo *root, IndexPath *index_path)
 
static Nodefix_indexqual_clause (PlannerInfo *root, IndexOptInfo *index, int indexcol, Node *clause, List *indexcolnos)
 
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 void label_incrementalsort_with_costsize (PlannerInfo *root, IncrementalSort *plan, List *pathkeys, 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 *recheckqual, 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 TidRangeScanmake_tidrangescan (List *qptlist, List *qpqual, Index scanrelid, List *tidrangequals)
 
static SubqueryScanmake_subqueryscan (List *qptlist, List *qpqual, Index scanrelid, Plan *subplan)
 
static FunctionScanmake_functionscan (List *qptlist, List *qpqual, Index scanrelid, List *functions, bool funcordinality)
 
static ValuesScanmake_valuesscan (List *qptlist, List *qpqual, Index scanrelid, List *values_lists)
 
static TableFuncScanmake_tablefuncscan (List *qptlist, List *qpqual, Index scanrelid, TableFunc *tablefunc)
 
static CteScanmake_ctescan (List *qptlist, List *qpqual, Index scanrelid, int ctePlanId, int cteParam)
 
static NamedTuplestoreScanmake_namedtuplestorescan (List *qptlist, List *qpqual, Index scanrelid, char *enrname)
 
static WorkTableScanmake_worktablescan (List *qptlist, List *qpqual, Index scanrelid, int wtParam)
 
static RecursiveUnionmake_recursive_union (List *tlist, Plan *lefttree, Plan *righttree, int wtParam, List *distinctList, long numGroups)
 
static BitmapAndmake_bitmap_and (List *bitmapplans)
 
static BitmapOrmake_bitmap_or (List *bitmapplans)
 
static NestLoopmake_nestloop (List *tlist, List *joinclauses, List *otherclauses, List *nestParams, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
 
static HashJoinmake_hashjoin (List *tlist, List *joinclauses, List *otherclauses, List *hashclauses, List *hashoperators, List *hashcollations, List *hashkeys, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
 
static Hashmake_hash (Plan *lefttree, List *hashkeys, Oid skewTable, AttrNumber skewColumn, bool skewInherit)
 
static MergeJoinmake_mergejoin (List *tlist, List *joinclauses, List *otherclauses, List *mergeclauses, Oid *mergefamilies, Oid *mergecollations, bool *mergereversals, bool *mergenullsfirst, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique, bool skip_mark_restore)
 
static Sortmake_sort (Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
 
static IncrementalSortmake_incrementalsort (Plan *lefttree, int numCols, int nPresortedCols, 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 Sortmake_sort_from_pathkeys (Plan *lefttree, List *pathkeys, Relids relids)
 
static IncrementalSortmake_incrementalsort_from_pathkeys (Plan *lefttree, List *pathkeys, Relids relids, int nPresortedCols)
 
static Sortmake_sort_from_groupcols (List *groupcls, AttrNumber *grpColIdx, Plan *lefttree)
 
static Materialmake_material (Plan *lefttree)
 
static Memoizemake_memoize (Plan *lefttree, Oid *hashoperators, Oid *collations, List *param_exprs, bool singlerow, bool binary_mode, uint32 est_entries, Bitmapset *keyparamids)
 
static WindowAggmake_windowagg (List *tlist, Index winref, int partNumCols, AttrNumber *partColIdx, Oid *partOperators, Oid *partCollations, int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators, Oid *ordCollations, int frameOptions, Node *startOffset, Node *endOffset, Oid startInRangeFunc, Oid endInRangeFunc, Oid inRangeColl, bool inRangeAsc, bool inRangeNullsFirst, List *runCondition, List *qual, bool topWindow, Plan *lefttree)
 
static Groupmake_group (List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations, Plan *lefttree)
 
static Uniquemake_unique_from_sortclauses (Plan *lefttree, List *distinctList)
 
static Uniquemake_unique_from_pathkeys (Plan *lefttree, List *pathkeys, int numCols)
 
static Gathermake_gather (List *qptlist, List *qpqual, int nworkers, int rescan_param, bool single_copy, Plan *subplan)
 
static SetOpmake_setop (SetOpCmd cmd, SetOpStrategy strategy, List *tlist, Plan *lefttree, Plan *righttree, List *groupList, 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, Plan *subplan, CmdType operation, bool canSetTag, Index nominalRelation, Index rootRelation, bool partColsUpdated, List *resultRelations, List *updateColnosLists, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, List *mergeActionLists, List *mergeJoinConditions, int epqParam)
 
static GatherMergecreate_gather_merge_plan (PlannerInfo *root, GatherMergePath *best_path)
 
Plancreate_plan (PlannerInfo *root, Path *best_path)
 
Planchange_plan_targetlist (Plan *subplan, List *tlist, bool tlist_parallel_safe)
 
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, Oid *grpCollations, List *groupingSets, List *chain, double dNumGroups, Size transitionSpace, Plan *lefttree)
 
Limitmake_limit (Plan *lefttree, Node *limitOffset, Node *limitCount, LimitOption limitOption, int uniqNumCols, AttrNumber *uniqColIdx, Oid *uniqOperators, Oid *uniqCollations)
 
bool is_projection_capable_path (Path *path)
 
bool is_projection_capable_plan (Plan *plan)
 

Macro Definition Documentation

◆ CP_EXACT_TLIST

#define CP_EXACT_TLIST   0x0001 /* Plan must return specified tlist */

Definition at line 70 of file createplan.c.

◆ CP_IGNORE_TLIST

#define CP_IGNORE_TLIST   0x0008 /* caller will replace tlist */

Definition at line 73 of file createplan.c.

◆ CP_LABEL_TLIST

#define CP_LABEL_TLIST   0x0004 /* tlist must contain sortgrouprefs */

Definition at line 72 of file createplan.c.

◆ CP_SMALL_TLIST

#define CP_SMALL_TLIST   0x0002 /* Prefer narrower tlists */

Definition at line 71 of file createplan.c.

Function Documentation

◆ bitmap_subplan_mark_shared()

static void bitmap_subplan_mark_shared ( Plan plan)
static

Definition at line 5554 of file createplan.c.

5555{
5556 if (IsA(plan, BitmapAnd))
5558 else if (IsA(plan, BitmapOr))
5559 {
5560 ((BitmapOr *) plan)->isshared = true;
5561 bitmap_subplan_mark_shared(linitial(((BitmapOr *) plan)->bitmapplans));
5562 }
5563 else if (IsA(plan, BitmapIndexScan))
5564 ((BitmapIndexScan *) plan)->isshared = true;
5565 else
5566 elog(ERROR, "unrecognized node type: %d", nodeTag(plan));
5567}
static void bitmap_subplan_mark_shared(Plan *plan)
Definition: createplan.c:5554
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define nodeTag(nodeptr)
Definition: nodes.h:133
#define linitial(l)
Definition: pg_list.h:178
#define plan(x)
Definition: pg_regress.c:161

References bitmap_subplan_mark_shared(), elog, ERROR, IsA, linitial, nodeTag, and plan.

Referenced by bitmap_subplan_mark_shared(), and create_bitmap_scan_plan().

◆ build_path_tlist()

static List * build_path_tlist ( PlannerInfo root,
Path path 
)
static

Definition at line 828 of file createplan.c.

829{
830 List *tlist = NIL;
831 Index *sortgrouprefs = path->pathtarget->sortgrouprefs;
832 int resno = 1;
833 ListCell *v;
834
835 foreach(v, path->pathtarget->exprs)
836 {
837 Node *node = (Node *) lfirst(v);
838 TargetEntry *tle;
839
840 /*
841 * If it's a parameterized path, there might be lateral references in
842 * the tlist, which need to be replaced with Params. There's no need
843 * to remake the TargetEntry nodes, so apply this to each list item
844 * separately.
845 */
846 if (path->param_info)
847 node = replace_nestloop_params(root, node);
848
849 tle = makeTargetEntry((Expr *) node,
850 resno,
851 NULL,
852 false);
853 if (sortgrouprefs)
854 tle->ressortgroupref = sortgrouprefs[resno - 1];
855
856 tlist = lappend(tlist, tle);
857 resno++;
858 }
859 return tlist;
860}
unsigned int Index
Definition: c.h:571
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4983
List * lappend(List *list, void *datum)
Definition: list.c:339
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:242
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
tree ctl root
Definition: radixtree.h:1857
Definition: pg_list.h:54
Definition: nodes.h:129
Index ressortgroupref
Definition: primnodes.h:2251

References lappend(), lfirst, makeTargetEntry(), NIL, replace_nestloop_params(), TargetEntry::ressortgroupref, and root.

Referenced by create_agg_plan(), create_append_plan(), create_gather_merge_plan(), create_gather_plan(), create_gating_plan(), create_group_plan(), create_group_result_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_scan_plan(), create_setop_plan(), create_unique_plan(), and create_windowagg_plan().

◆ change_plan_targetlist()

Plan * change_plan_targetlist ( Plan subplan,
List tlist,
bool  tlist_parallel_safe 
)

Definition at line 2151 of file createplan.c.

2152{
2153 /*
2154 * If the top plan node can't do projections and its existing target list
2155 * isn't already what we need, we need to add a Result node to help it
2156 * along.
2157 */
2158 if (!is_projection_capable_plan(subplan) &&
2159 !tlist_same_exprs(tlist, subplan->targetlist))
2160 subplan = inject_projection_plan(subplan, tlist,
2161 subplan->parallel_safe &&
2162 tlist_parallel_safe);
2163 else
2164 {
2165 /* Else we can just replace the plan node's tlist */
2166 subplan->targetlist = tlist;
2167 subplan->parallel_safe &= tlist_parallel_safe;
2168 }
2169 return subplan;
2170}
bool is_projection_capable_plan(Plan *plan)
Definition: createplan.c:7369
static Plan * inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
Definition: createplan.c:2119
bool parallel_safe
Definition: plannodes.h:142
List * targetlist
Definition: plannodes.h:153
bool tlist_same_exprs(List *tlist1, List *tlist2)
Definition: tlist.c:218

References inject_projection_plan(), is_projection_capable_plan(), Plan::parallel_safe, Plan::targetlist, and tlist_same_exprs().

Referenced by create_unique_plan(), and postgresGetForeignPlan().

◆ copy_generic_path_info()

static void copy_generic_path_info ( Plan dest,
Path src 
)
static

Definition at line 5455 of file createplan.c.

5456{
5457 dest->disabled_nodes = src->disabled_nodes;
5458 dest->startup_cost = src->startup_cost;
5459 dest->total_cost = src->total_cost;
5460 dest->plan_rows = src->rows;
5461 dest->plan_width = src->pathtarget->width;
5462 dest->parallel_aware = src->parallel_aware;
5463 dest->parallel_safe = src->parallel_safe;
5464}
Cardinality rows
Definition: pathnodes.h:1671
Cost startup_cost
Definition: pathnodes.h:1673
int disabled_nodes
Definition: pathnodes.h:1672
Cost total_cost
Definition: pathnodes.h:1674
bool parallel_aware
Definition: pathnodes.h:1664
bool parallel_safe
Definition: pathnodes.h:1666

References generate_unaccent_rules::dest, Path::disabled_nodes, Path::parallel_aware, Path::parallel_safe, Path::rows, Path::startup_cost, and Path::total_cost.

Referenced by create_agg_plan(), create_append_plan(), create_bitmap_scan_plan(), create_ctescan_plan(), create_customscan_plan(), create_foreignscan_plan(), create_functionscan_plan(), create_gather_merge_plan(), create_gather_plan(), create_group_plan(), create_group_result_plan(), create_groupingsets_plan(), create_hashjoin_plan(), create_incrementalsort_plan(), create_indexscan_plan(), create_limit_plan(), create_lockrows_plan(), create_material_plan(), create_memoize_plan(), create_merge_append_plan(), create_mergejoin_plan(), create_minmaxagg_plan(), create_modifytable_plan(), create_namedtuplestorescan_plan(), create_nestloop_plan(), create_project_set_plan(), create_projection_plan(), create_recursiveunion_plan(), create_resultscan_plan(), create_samplescan_plan(), create_seqscan_plan(), create_setop_plan(), create_sort_plan(), create_subqueryscan_plan(), create_tablefuncscan_plan(), create_tidrangescan_plan(), create_tidscan_plan(), create_unique_plan(), create_upper_unique_plan(), create_valuesscan_plan(), create_windowagg_plan(), and create_worktablescan_plan().

◆ copy_plan_costsize()

static void copy_plan_costsize ( Plan dest,
Plan src 
)
static

Definition at line 5471 of file createplan.c.

5472{
5473 dest->disabled_nodes = src->disabled_nodes;
5474 dest->startup_cost = src->startup_cost;
5475 dest->total_cost = src->total_cost;
5476 dest->plan_rows = src->plan_rows;
5477 dest->plan_width = src->plan_width;
5478 /* Assume the inserted node is not parallel-aware. */
5479 dest->parallel_aware = false;
5480 /* Assume the inserted node is parallel-safe, if child plan is. */
5481 dest->parallel_safe = src->parallel_safe;
5482}
Cost total_cost
Definition: plannodes.h:130
Cost startup_cost
Definition: plannodes.h:129
int plan_width
Definition: plannodes.h:136
Cardinality plan_rows
Definition: plannodes.h:135
int disabled_nodes
Definition: plannodes.h:128

References generate_unaccent_rules::dest, Plan::disabled_nodes, Plan::parallel_safe, Plan::plan_rows, Plan::plan_width, Plan::startup_cost, and Plan::total_cost.

Referenced by create_gating_plan(), create_hashjoin_plan(), create_mergejoin_plan(), and inject_projection_plan().

◆ create_agg_plan()

static Agg * create_agg_plan ( PlannerInfo root,
AggPath best_path 
)
static

Definition at line 2307 of file createplan.c.

2308{
2309 Agg *plan;
2310 Plan *subplan;
2311 List *tlist;
2312 List *quals;
2313
2314 /*
2315 * Agg can project, so no need to be terribly picky about child tlist, but
2316 * we do need grouping columns to be available
2317 */
2318 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2319
2320 tlist = build_path_tlist(root, &best_path->path);
2321
2322 quals = order_qual_clauses(root, best_path->qual);
2323
2324 plan = make_agg(tlist, quals,
2325 best_path->aggstrategy,
2326 best_path->aggsplit,
2327 list_length(best_path->groupClause),
2329 subplan->targetlist),
2332 subplan->targetlist),
2333 NIL,
2334 NIL,
2335 best_path->numGroups,
2336 best_path->transitionSpace,
2337 subplan);
2338
2339 copy_generic_path_info(&plan->plan, (Path *) best_path);
2340
2341 return plan;
2342}
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5361
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5455
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:391
static List * build_path_tlist(PlannerInfo *root, Path *path)
Definition: createplan.c:828
Agg * make_agg(List *tlist, List *qual, AggStrategy aggstrategy, AggSplit aggsplit, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations, List *groupingSets, List *chain, double dNumGroups, Size transitionSpace, Plan *lefttree)
Definition: createplan.c:6672
#define CP_LABEL_TLIST
Definition: createplan.c:72
static int list_length(const List *l)
Definition: pg_list.h:152
Path * subpath
Definition: pathnodes.h:2266
Cardinality numGroups
Definition: pathnodes.h:2269
AggSplit aggsplit
Definition: pathnodes.h:2268
List * groupClause
Definition: pathnodes.h:2271
uint64 transitionSpace
Definition: pathnodes.h:2270
AggStrategy aggstrategy
Definition: pathnodes.h:2267
Path path
Definition: pathnodes.h:2265
List * qual
Definition: pathnodes.h:2272
Oid * extract_grouping_ops(List *groupClause)
Definition: tlist.c:463
AttrNumber * extract_grouping_cols(List *groupClause, List *tlist)
Definition: tlist.c:514
Oid * extract_grouping_collations(List *groupClause, List *tlist)
Definition: tlist.c:489

References AggPath::aggsplit, AggPath::aggstrategy, build_path_tlist(), copy_generic_path_info(), CP_LABEL_TLIST, create_plan_recurse(), extract_grouping_collations(), extract_grouping_cols(), extract_grouping_ops(), AggPath::groupClause, list_length(), make_agg(), NIL, AggPath::numGroups, order_qual_clauses(), AggPath::path, plan, AggPath::qual, root, AggPath::subpath, Plan::targetlist, and AggPath::transitionSpace.

Referenced by create_plan_recurse().

◆ create_append_plan()

static Plan * create_append_plan ( PlannerInfo root,
AppendPath best_path,
int  flags 
)
static

Definition at line 1219 of file createplan.c.

1220{
1221 Append *plan;
1222 List *tlist = build_path_tlist(root, &best_path->path);
1223 int orig_tlist_length = list_length(tlist);
1224 bool tlist_was_changed = false;
1225 List *pathkeys = best_path->path.pathkeys;
1226 List *subplans = NIL;
1227 ListCell *subpaths;
1228 int nasyncplans = 0;
1229 RelOptInfo *rel = best_path->path.parent;
1230 PartitionPruneInfo *partpruneinfo = NULL;
1231 int nodenumsortkeys = 0;
1232 AttrNumber *nodeSortColIdx = NULL;
1233 Oid *nodeSortOperators = NULL;
1234 Oid *nodeCollations = NULL;
1235 bool *nodeNullsFirst = NULL;
1236 bool consider_async = false;
1237
1238 /*
1239 * The subpaths list could be empty, if every child was proven empty by
1240 * constraint exclusion. In that case generate a dummy plan that returns
1241 * no rows.
1242 *
1243 * Note that an AppendPath with no members is also generated in certain
1244 * cases where there was no appending construct at all, but we know the
1245 * relation is empty (see set_dummy_rel_pathlist and mark_dummy_rel).
1246 */
1247 if (best_path->subpaths == NIL)
1248 {
1249 /* Generate a Result plan with constant-FALSE gating qual */
1250 Plan *plan;
1251
1252 plan = (Plan *) make_result(tlist,
1253 (Node *) list_make1(makeBoolConst(false,
1254 false)),
1255 NULL);
1256
1257 copy_generic_path_info(plan, (Path *) best_path);
1258
1259 return plan;
1260 }
1261
1262 /*
1263 * Otherwise build an Append plan. Note that if there's just one child,
1264 * the Append is pretty useless; but we wait till setrefs.c to get rid of
1265 * it. Doing so here doesn't work because the varno of the child scan
1266 * plan won't match the parent-rel Vars it'll be asked to emit.
1267 *
1268 * We don't have the actual creation of the Append node split out into a
1269 * separate make_xxx function. This is because we want to run
1270 * prepare_sort_from_pathkeys on it before we do so on the individual
1271 * child plans, to make cross-checking the sort info easier.
1272 */
1273 plan = makeNode(Append);
1274 plan->plan.targetlist = tlist;
1275 plan->plan.qual = NIL;
1276 plan->plan.lefttree = NULL;
1277 plan->plan.righttree = NULL;
1278 plan->apprelids = rel->relids;
1279
1280 if (pathkeys != NIL)
1281 {
1282 /*
1283 * Compute sort column info, and adjust the Append's tlist as needed.
1284 * Because we pass adjust_tlist_in_place = true, we may ignore the
1285 * function result; it must be the same plan node. However, we then
1286 * need to detect whether any tlist entries were added.
1287 */
1288 (void) prepare_sort_from_pathkeys((Plan *) plan, pathkeys,
1289 best_path->path.parent->relids,
1290 NULL,
1291 true,
1292 &nodenumsortkeys,
1293 &nodeSortColIdx,
1294 &nodeSortOperators,
1295 &nodeCollations,
1296 &nodeNullsFirst);
1297 tlist_was_changed = (orig_tlist_length != list_length(plan->plan.targetlist));
1298 }
1299
1300 /* If appropriate, consider async append */
1301 consider_async = (enable_async_append && pathkeys == NIL &&
1302 !best_path->path.parallel_safe &&
1303 list_length(best_path->subpaths) > 1);
1304
1305 /* Build the plan for each child */
1306 foreach(subpaths, best_path->subpaths)
1307 {
1308 Path *subpath = (Path *) lfirst(subpaths);
1309 Plan *subplan;
1310
1311 /* Must insist that all children return the same tlist */
1313
1314 /*
1315 * For ordered Appends, we must insert a Sort node if subplan isn't
1316 * sufficiently ordered.
1317 */
1318 if (pathkeys != NIL)
1319 {
1320 int numsortkeys;
1321 AttrNumber *sortColIdx;
1322 Oid *sortOperators;
1323 Oid *collations;
1324 bool *nullsFirst;
1325
1326 /*
1327 * Compute sort column info, and adjust subplan's tlist as needed.
1328 * We must apply prepare_sort_from_pathkeys even to subplans that
1329 * don't need an explicit sort, to make sure they are returning
1330 * the same sort key columns the Append expects.
1331 */
1332 subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1333 subpath->parent->relids,
1334 nodeSortColIdx,
1335 false,
1336 &numsortkeys,
1337 &sortColIdx,
1338 &sortOperators,
1339 &collations,
1340 &nullsFirst);
1341
1342 /*
1343 * Check that we got the same sort key information. We just
1344 * Assert that the sortops match, since those depend only on the
1345 * pathkeys; but it seems like a good idea to check the sort
1346 * column numbers explicitly, to ensure the tlists match up.
1347 */
1348 Assert(numsortkeys == nodenumsortkeys);
1349 if (memcmp(sortColIdx, nodeSortColIdx,
1350 numsortkeys * sizeof(AttrNumber)) != 0)
1351 elog(ERROR, "Append child's targetlist doesn't match Append");
1352 Assert(memcmp(sortOperators, nodeSortOperators,
1353 numsortkeys * sizeof(Oid)) == 0);
1354 Assert(memcmp(collations, nodeCollations,
1355 numsortkeys * sizeof(Oid)) == 0);
1356 Assert(memcmp(nullsFirst, nodeNullsFirst,
1357 numsortkeys * sizeof(bool)) == 0);
1358
1359 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1360 if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
1361 {
1362 Sort *sort = make_sort(subplan, numsortkeys,
1363 sortColIdx, sortOperators,
1364 collations, nullsFirst);
1365
1367 subplan = (Plan *) sort;
1368 }
1369 }
1370
1371 /* If needed, check to see if subplan can be executed asynchronously */
1372 if (consider_async && mark_async_capable_plan(subplan, subpath))
1373 {
1374 Assert(subplan->async_capable);
1375 ++nasyncplans;
1376 }
1377
1378 subplans = lappend(subplans, subplan);
1379 }
1380
1381 /*
1382 * If any quals exist, they may be useful to perform further partition
1383 * pruning during execution. Gather information needed by the executor to
1384 * do partition pruning.
1385 */
1387 {
1388 List *prunequal;
1389
1390 prunequal = extract_actual_clauses(rel->baserestrictinfo, false);
1391
1392 if (best_path->path.param_info)
1393 {
1394 List *prmquals = best_path->path.param_info->ppi_clauses;
1395
1396 prmquals = extract_actual_clauses(prmquals, false);
1397 prmquals = (List *) replace_nestloop_params(root,
1398 (Node *) prmquals);
1399
1400 prunequal = list_concat(prunequal, prmquals);
1401 }
1402
1403 if (prunequal != NIL)
1404 partpruneinfo =
1406 best_path->subpaths,
1407 prunequal);
1408 }
1409
1410 plan->appendplans = subplans;
1411 plan->nasyncplans = nasyncplans;
1412 plan->first_partial_plan = best_path->first_partial_path;
1413 plan->part_prune_info = partpruneinfo;
1414
1415 copy_generic_path_info(&plan->plan, (Path *) best_path);
1416
1417 /*
1418 * If prepare_sort_from_pathkeys added sort columns, but we were told to
1419 * produce either the exact tlist or a narrow tlist, we should get rid of
1420 * the sort columns again. We must inject a projection node to do so.
1421 */
1422 if (tlist_was_changed && (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST)))
1423 {
1424 tlist = list_copy_head(plan->plan.targetlist, orig_tlist_length);
1425 return inject_projection_plan((Plan *) plan, tlist,
1426 plan->plan.parallel_safe);
1427 }
1428 else
1429 return (Plan *) plan;
1430}
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:195
int16 AttrNumber
Definition: attnum.h:21
#define Assert(condition)
Definition: c.h:815
bool enable_async_append
Definition: costsize.c:165
bool enable_partition_pruning
Definition: costsize.c:163
static Result * make_result(List *tlist, Node *resconstantqual, Plan *subplan)
Definition: createplan.c:7072
static void label_sort_with_costsize(PlannerInfo *root, Sort *plan, double limit_tuples)
Definition: createplan.c:5494
#define CP_SMALL_TLIST
Definition: createplan.c:71
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
Definition: createplan.c:6144
#define CP_EXACT_TLIST
Definition: createplan.c:70
static bool mark_async_capable_plan(Plan *plan, Path *path)
Definition: createplan.c:1143
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:6241
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
List * list_copy_head(const List *oldlist, int len)
Definition: list.c:1593
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:308
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:361
#define makeNode(_type_)
Definition: nodes.h:155
PartitionPruneInfo * make_partition_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel, List *subpaths, List *prunequal)
Definition: partprune.c:220
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:343
#define list_make1(x1)
Definition: pg_list.h:212
unsigned int Oid
Definition: postgres_ext.h:32
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:485
int first_partial_path
Definition: pathnodes.h:1946
Cardinality limit_tuples
Definition: pathnodes.h:1947
List * subpaths
Definition: pathnodes.h:1944
List * pathkeys
Definition: pathnodes.h:1677
bool async_capable
Definition: plannodes.h:147
List * baserestrictinfo
Definition: pathnodes.h:985
Relids relids
Definition: pathnodes.h:871

References Assert, Plan::async_capable, RelOptInfo::baserestrictinfo, build_path_tlist(), copy_generic_path_info(), CP_EXACT_TLIST, CP_SMALL_TLIST, create_plan_recurse(), elog, enable_async_append, enable_partition_pruning, ERROR, extract_actual_clauses(), AppendPath::first_partial_path, inject_projection_plan(), label_sort_with_costsize(), lappend(), lfirst, AppendPath::limit_tuples, list_concat(), list_copy_head(), list_length(), list_make1, make_partition_pruneinfo(), make_result(), make_sort(), makeBoolConst(), makeNode, mark_async_capable_plan(), NIL, Path::parallel_safe, AppendPath::path, Path::pathkeys, pathkeys_contained_in(), plan, prepare_sort_from_pathkeys(), RelOptInfo::relids, replace_nestloop_params(), root, sort(), subpath(), and AppendPath::subpaths.

Referenced by create_plan_recurse().

◆ create_bitmap_scan_plan()

static BitmapHeapScan * create_bitmap_scan_plan ( PlannerInfo root,
BitmapHeapPath best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3205 of file createplan.c.

3209{
3210 Index baserelid = best_path->path.parent->relid;
3211 Plan *bitmapqualplan;
3212 List *bitmapqualorig;
3213 List *indexquals;
3214 List *indexECs;
3215 List *qpqual;
3216 ListCell *l;
3217 BitmapHeapScan *scan_plan;
3218
3219 /* it should be a base rel... */
3220 Assert(baserelid > 0);
3221 Assert(best_path->path.parent->rtekind == RTE_RELATION);
3222
3223 /* Process the bitmapqual tree into a Plan tree and qual lists */
3224 bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
3225 &bitmapqualorig, &indexquals,
3226 &indexECs);
3227
3228 if (best_path->path.parallel_aware)
3229 bitmap_subplan_mark_shared(bitmapqualplan);
3230
3231 /*
3232 * The qpqual list must contain all restrictions not automatically handled
3233 * by the index, other than pseudoconstant clauses which will be handled
3234 * by a separate gating plan node. All the predicates in the indexquals
3235 * will be checked (either by the index itself, or by
3236 * nodeBitmapHeapscan.c), but if there are any "special" operators
3237 * involved then they must be added to qpqual. The upshot is that qpqual
3238 * must contain scan_clauses minus whatever appears in indexquals.
3239 *
3240 * This loop is similar to the comparable code in create_indexscan_plan(),
3241 * but with some differences because it has to compare the scan clauses to
3242 * stripped (no RestrictInfos) indexquals. See comments there for more
3243 * info.
3244 *
3245 * In normal cases simple equal() checks will be enough to spot duplicate
3246 * clauses, so we try that first. We next see if the scan clause is
3247 * redundant with any top-level indexqual by virtue of being generated
3248 * from the same EC. After that, try predicate_implied_by().
3249 *
3250 * Unlike create_indexscan_plan(), the predicate_implied_by() test here is
3251 * useful for getting rid of qpquals that are implied by index predicates,
3252 * because the predicate conditions are included in the "indexquals"
3253 * returned by create_bitmap_subplan(). Bitmap scans have to do it that
3254 * way because predicate conditions need to be rechecked if the scan
3255 * becomes lossy, so they have to be included in bitmapqualorig.
3256 */
3257 qpqual = NIL;
3258 foreach(l, scan_clauses)
3259 {
3261 Node *clause = (Node *) rinfo->clause;
3262
3263 if (rinfo->pseudoconstant)
3264 continue; /* we may drop pseudoconstants here */
3265 if (list_member(indexquals, clause))
3266 continue; /* simple duplicate */
3267 if (rinfo->parent_ec && list_member_ptr(indexECs, rinfo->parent_ec))
3268 continue; /* derived from same EquivalenceClass */
3269 if (!contain_mutable_functions(clause) &&
3270 predicate_implied_by(list_make1(clause), indexquals, false))
3271 continue; /* provably implied by indexquals */
3272 qpqual = lappend(qpqual, rinfo);
3273 }
3274
3275 /* Sort clauses into best execution order */
3276 qpqual = order_qual_clauses(root, qpqual);
3277
3278 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3279 qpqual = extract_actual_clauses(qpqual, false);
3280
3281 /*
3282 * When dealing with special operators, we will at this point have
3283 * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
3284 * 'em from bitmapqualorig, since there's no point in making the tests
3285 * twice.
3286 */
3287 bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
3288
3289 /*
3290 * We have to replace any outer-relation variables with nestloop params in
3291 * the qpqual and bitmapqualorig expressions. (This was already done for
3292 * expressions attached to plan nodes in the bitmapqualplan tree.)
3293 */
3294 if (best_path->path.param_info)
3295 {
3296 qpqual = (List *)
3297 replace_nestloop_params(root, (Node *) qpqual);
3298 bitmapqualorig = (List *)
3299 replace_nestloop_params(root, (Node *) bitmapqualorig);
3300 }
3301
3302 /* Finally ready to build the plan node */
3303 scan_plan = make_bitmap_heapscan(tlist,
3304 qpqual,
3305 bitmapqualplan,
3306 bitmapqualorig,
3307 baserelid);
3308
3309 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3310
3311 return scan_plan;
3312}
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:369
static Plan * create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual, List **qual, List **indexqual, List **indexECs)
Definition: createplan.c:3335
static BitmapHeapScan * make_bitmap_heapscan(List *qptlist, List *qpqual, Plan *lefttree, List *bitmapqualorig, Index scanrelid)
Definition: createplan.c:5701
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
List * list_difference_ptr(const List *list1, const List *list2)
Definition: list.c:1263
bool list_member_ptr(const List *list, const void *datum)
Definition: list.c:682
bool list_member(const List *list, const void *datum)
Definition: list.c:661
@ RTE_RELATION
Definition: parsenodes.h:1026
#define lfirst_node(type, lc)
Definition: pg_list.h:176
bool predicate_implied_by(List *predicate_list, List *clause_list, bool weak)
Definition: predtest.c:152
Path * bitmapqual
Definition: pathnodes.h:1797
Expr * clause
Definition: pathnodes.h:2575

References Assert, bitmap_subplan_mark_shared(), BitmapHeapPath::bitmapqual, RestrictInfo::clause, contain_mutable_functions(), copy_generic_path_info(), create_bitmap_subplan(), extract_actual_clauses(), if(), lappend(), lfirst_node, list_difference_ptr(), list_make1, list_member(), list_member_ptr(), make_bitmap_heapscan(), NIL, order_qual_clauses(), Path::parallel_aware, BitmapHeapPath::path, predicate_implied_by(), replace_nestloop_params(), root, RTE_RELATION, and BitmapHeapScan::scan.

Referenced by create_scan_plan().

◆ create_bitmap_subplan()

static Plan * create_bitmap_subplan ( PlannerInfo root,
Path bitmapqual,
List **  qual,
List **  indexqual,
List **  indexECs 
)
static

Definition at line 3335 of file createplan.c.

3337{
3338 Plan *plan;
3339
3340 if (IsA(bitmapqual, BitmapAndPath))
3341 {
3342 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
3343 List *subplans = NIL;
3344 List *subquals = NIL;
3345 List *subindexquals = NIL;
3346 List *subindexECs = NIL;
3347 ListCell *l;
3348
3349 /*
3350 * There may well be redundant quals among the subplans, since a
3351 * top-level WHERE qual might have gotten used to form several
3352 * different index quals. We don't try exceedingly hard to eliminate
3353 * redundancies, but we do eliminate obvious duplicates by using
3354 * list_concat_unique.
3355 */
3356 foreach(l, apath->bitmapquals)
3357 {
3358 Plan *subplan;
3359 List *subqual;
3360 List *subindexqual;
3361 List *subindexEC;
3362
3363 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
3364 &subqual, &subindexqual,
3365 &subindexEC);
3366 subplans = lappend(subplans, subplan);
3367 subquals = list_concat_unique(subquals, subqual);
3368 subindexquals = list_concat_unique(subindexquals, subindexqual);
3369 /* Duplicates in indexECs aren't worth getting rid of */
3370 subindexECs = list_concat(subindexECs, subindexEC);
3371 }
3372 plan = (Plan *) make_bitmap_and(subplans);
3373 plan->startup_cost = apath->path.startup_cost;
3374 plan->total_cost = apath->path.total_cost;
3375 plan->plan_rows =
3376 clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
3377 plan->plan_width = 0; /* meaningless */
3378 plan->parallel_aware = false;
3379 plan->parallel_safe = apath->path.parallel_safe;
3380 *qual = subquals;
3381 *indexqual = subindexquals;
3382 *indexECs = subindexECs;
3383 }
3384 else if (IsA(bitmapqual, BitmapOrPath))
3385 {
3386 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
3387 List *subplans = NIL;
3388 List *subquals = NIL;
3389 List *subindexquals = NIL;
3390 bool const_true_subqual = false;
3391 bool const_true_subindexqual = false;
3392 ListCell *l;
3393
3394 /*
3395 * Here, we only detect qual-free subplans. A qual-free subplan would
3396 * cause us to generate "... OR true ..." which we may as well reduce
3397 * to just "true". We do not try to eliminate redundant subclauses
3398 * because (a) it's not as likely as in the AND case, and (b) we might
3399 * well be working with hundreds or even thousands of OR conditions,
3400 * perhaps from a long IN list. The performance of list_append_unique
3401 * would be unacceptable.
3402 */
3403 foreach(l, opath->bitmapquals)
3404 {
3405 Plan *subplan;
3406 List *subqual;
3407 List *subindexqual;
3408 List *subindexEC;
3409
3410 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
3411 &subqual, &subindexqual,
3412 &subindexEC);
3413 subplans = lappend(subplans, subplan);
3414 if (subqual == NIL)
3415 const_true_subqual = true;
3416 else if (!const_true_subqual)
3417 subquals = lappend(subquals,
3418 make_ands_explicit(subqual));
3419 if (subindexqual == NIL)
3420 const_true_subindexqual = true;
3421 else if (!const_true_subindexqual)
3422 subindexquals = lappend(subindexquals,
3423 make_ands_explicit(subindexqual));
3424 }
3425
3426 /*
3427 * In the presence of ScalarArrayOpExpr quals, we might have built
3428 * BitmapOrPaths with just one subpath; don't add an OR step.
3429 */
3430 if (list_length(subplans) == 1)
3431 {
3432 plan = (Plan *) linitial(subplans);
3433 }
3434 else
3435 {
3436 plan = (Plan *) make_bitmap_or(subplans);
3437 plan->startup_cost = opath->path.startup_cost;
3438 plan->total_cost = opath->path.total_cost;
3439 plan->plan_rows =
3440 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
3441 plan->plan_width = 0; /* meaningless */
3442 plan->parallel_aware = false;
3443 plan->parallel_safe = opath->path.parallel_safe;
3444 }
3445
3446 /*
3447 * If there were constant-TRUE subquals, the OR reduces to constant
3448 * TRUE. Also, avoid generating one-element ORs, which could happen
3449 * due to redundancy elimination or ScalarArrayOpExpr quals.
3450 */
3451 if (const_true_subqual)
3452 *qual = NIL;
3453 else if (list_length(subquals) <= 1)
3454 *qual = subquals;
3455 else
3456 *qual = list_make1(make_orclause(subquals));
3457 if (const_true_subindexqual)
3458 *indexqual = NIL;
3459 else if (list_length(subindexquals) <= 1)
3460 *indexqual = subindexquals;
3461 else
3462 *indexqual = list_make1(make_orclause(subindexquals));
3463 *indexECs = NIL;
3464 }
3465 else if (IsA(bitmapqual, IndexPath))
3466 {
3467 IndexPath *ipath = (IndexPath *) bitmapqual;
3468 IndexScan *iscan;
3469 List *subquals;
3470 List *subindexquals;
3471 List *subindexECs;
3472 ListCell *l;
3473
3474 /* Use the regular indexscan plan build machinery... */
3475 iscan = castNode(IndexScan,
3477 NIL, NIL, false));
3478 /* then convert to a bitmap indexscan */
3480 iscan->indexid,
3481 iscan->indexqual,
3482 iscan->indexqualorig);
3483 /* and set its cost/width fields appropriately */
3484 plan->startup_cost = 0.0;
3485 plan->total_cost = ipath->indextotalcost;
3486 plan->plan_rows =
3487 clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
3488 plan->plan_width = 0; /* meaningless */
3489 plan->parallel_aware = false;
3490 plan->parallel_safe = ipath->path.parallel_safe;
3491 /* Extract original index clauses, actual index quals, relevant ECs */
3492 subquals = NIL;
3493 subindexquals = NIL;
3494 subindexECs = NIL;
3495 foreach(l, ipath->indexclauses)
3496 {
3497 IndexClause *iclause = (IndexClause *) lfirst(l);
3498 RestrictInfo *rinfo = iclause->rinfo;
3499
3500 Assert(!rinfo->pseudoconstant);
3501 subquals = lappend(subquals, rinfo->clause);
3502 subindexquals = list_concat(subindexquals,
3503 get_actual_clauses(iclause->indexquals));
3504 if (rinfo->parent_ec)
3505 subindexECs = lappend(subindexECs, rinfo->parent_ec);
3506 }
3507 /* We can add any index predicate conditions, too */
3508 foreach(l, ipath->indexinfo->indpred)
3509 {
3510 Expr *pred = (Expr *) lfirst(l);
3511
3512 /*
3513 * We know that the index predicate must have been implied by the
3514 * query condition as a whole, but it may or may not be implied by
3515 * the conditions that got pushed into the bitmapqual. Avoid
3516 * generating redundant conditions.
3517 */
3518 if (!predicate_implied_by(list_make1(pred), subquals, false))
3519 {
3520 subquals = lappend(subquals, pred);
3521 subindexquals = lappend(subindexquals, pred);
3522 }
3523 }
3524 *qual = subquals;
3525 *indexqual = subindexquals;
3526 *indexECs = subindexECs;
3527 }
3528 else
3529 {
3530 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
3531 plan = NULL; /* keep compiler quiet */
3532 }
3533
3534 return plan;
3535}
double clamp_row_est(double nrows)
Definition: costsize.c:213
static BitmapOr * make_bitmap_or(List *bitmapplans)
Definition: createplan.c:6009
static BitmapIndexScan * make_bitmap_indexscan(Index scanrelid, Oid indexid, List *indexqual, List *indexqualorig)
Definition: createplan.c:5680
static BitmapAnd * make_bitmap_and(List *bitmapplans)
Definition: createplan.c:5994
static Scan * create_indexscan_plan(PlannerInfo *root, IndexPath *best_path, List *tlist, List *scan_clauses, bool indexonly)
Definition: createplan.c:3009
List * list_concat_unique(List *list1, const List *list2)
Definition: list.c:1405
Expr * make_orclause(List *orclauses)
Definition: makefuncs.c:696
Expr * make_ands_explicit(List *andclauses)
Definition: makefuncs.c:752
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:460
Selectivity bitmapselectivity
Definition: pathnodes.h:1810
List * bitmapquals
Definition: pathnodes.h:1809
Selectivity bitmapselectivity
Definition: pathnodes.h:1823
List * bitmapquals
Definition: pathnodes.h:1822
List * indexquals
Definition: pathnodes.h:1771
struct RestrictInfo * rinfo
Definition: pathnodes.h:1770
List * indpred
Definition: pathnodes.h:1174
List * indexclauses
Definition: pathnodes.h:1723
Path path
Definition: pathnodes.h:1721
Selectivity indexselectivity
Definition: pathnodes.h:1728
Cost indextotalcost
Definition: pathnodes.h:1727
IndexOptInfo * indexinfo
Definition: pathnodes.h:1722
Scan scan
Definition: plannodes.h:454
List * indexqualorig
Definition: plannodes.h:457
Oid indexid
Definition: plannodes.h:455
List * indexqual
Definition: plannodes.h:456
Index scanrelid
Definition: plannodes.h:392

References Assert, BitmapAndPath::bitmapquals, BitmapOrPath::bitmapquals, BitmapAndPath::bitmapselectivity, BitmapOrPath::bitmapselectivity, castNode, clamp_row_est(), RestrictInfo::clause, create_bitmap_subplan(), create_indexscan_plan(), elog, ERROR, get_actual_clauses(), IndexPath::indexclauses, IndexScan::indexid, IndexPath::indexinfo, IndexScan::indexqual, IndexScan::indexqualorig, IndexClause::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, Path::parallel_safe, IndexPath::path, BitmapAndPath::path, BitmapOrPath::path, plan, predicate_implied_by(), IndexClause::rinfo, root, IndexScan::scan, Scan::scanrelid, Path::startup_cost, and Path::total_cost.

Referenced by create_bitmap_scan_plan(), and create_bitmap_subplan().

◆ create_ctescan_plan()

static CteScan * create_ctescan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3894 of file createplan.c.

3896{
3897 CteScan *scan_plan;
3898 Index scan_relid = best_path->parent->relid;
3899 RangeTblEntry *rte;
3900 SubPlan *ctesplan = NULL;
3901 int plan_id;
3902 int cte_param_id;
3903 PlannerInfo *cteroot;
3904 Index levelsup;
3905 int ndx;
3906 ListCell *lc;
3907
3908 Assert(scan_relid > 0);
3909 rte = planner_rt_fetch(scan_relid, root);
3910 Assert(rte->rtekind == RTE_CTE);
3911 Assert(!rte->self_reference);
3912
3913 /*
3914 * Find the referenced CTE, and locate the SubPlan previously made for it.
3915 */
3916 levelsup = rte->ctelevelsup;
3917 cteroot = root;
3918 while (levelsup-- > 0)
3919 {
3920 cteroot = cteroot->parent_root;
3921 if (!cteroot) /* shouldn't happen */
3922 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3923 }
3924
3925 /*
3926 * Note: cte_plan_ids can be shorter than cteList, if we are still working
3927 * on planning the CTEs (ie, this is a side-reference from another CTE).
3928 * So we mustn't use forboth here.
3929 */
3930 ndx = 0;
3931 foreach(lc, cteroot->parse->cteList)
3932 {
3933 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
3934
3935 if (strcmp(cte->ctename, rte->ctename) == 0)
3936 break;
3937 ndx++;
3938 }
3939 if (lc == NULL) /* shouldn't happen */
3940 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
3941 if (ndx >= list_length(cteroot->cte_plan_ids))
3942 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3943 plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
3944 if (plan_id <= 0)
3945 elog(ERROR, "no plan was made for CTE \"%s\"", rte->ctename);
3946 foreach(lc, cteroot->init_plans)
3947 {
3948 ctesplan = (SubPlan *) lfirst(lc);
3949 if (ctesplan->plan_id == plan_id)
3950 break;
3951 }
3952 if (lc == NULL) /* shouldn't happen */
3953 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3954
3955 /*
3956 * We need the CTE param ID, which is the sole member of the SubPlan's
3957 * setParam list.
3958 */
3959 cte_param_id = linitial_int(ctesplan->setParam);
3960
3961 /* Sort clauses into best execution order */
3962 scan_clauses = order_qual_clauses(root, scan_clauses);
3963
3964 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3965 scan_clauses = extract_actual_clauses(scan_clauses, false);
3966
3967 /* Replace any outer-relation variables with nestloop params */
3968 if (best_path->param_info)
3969 {
3970 scan_clauses = (List *)
3971 replace_nestloop_params(root, (Node *) scan_clauses);
3972 }
3973
3974 scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
3975 plan_id, cte_param_id);
3976
3977 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3978
3979 return scan_plan;
3980}
static CteScan * make_ctescan(List *qptlist, List *qpqual, Index scanrelid, int ctePlanId, int cteParam)
Definition: createplan.c:5838
@ RTE_CTE
Definition: parsenodes.h:1032
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:570
#define linitial_int(l)
Definition: pg_list.h:179
static int list_nth_int(const List *list, int n)
Definition: pg_list.h:310
Scan scan
Definition: plannodes.h:645
List * cte_plan_ids
Definition: pathnodes.h:305
List * init_plans
Definition: pathnodes.h:299
Query * parse
Definition: pathnodes.h:202
List * cteList
Definition: parsenodes.h:168
char * ctename
Definition: parsenodes.h:1205
Index ctelevelsup
Definition: parsenodes.h:1207
RTEKind rtekind
Definition: parsenodes.h:1056
int plan_id
Definition: primnodes.h:1086
List * setParam
Definition: primnodes.h:1104

References Assert, copy_generic_path_info(), PlannerInfo::cte_plan_ids, RangeTblEntry::ctelevelsup, Query::cteList, RangeTblEntry::ctename, CommonTableExpr::ctename, elog, ERROR, extract_actual_clauses(), PlannerInfo::init_plans, lfirst, linitial_int, list_length(), list_nth_int(), make_ctescan(), order_qual_clauses(), PlannerInfo::parse, SubPlan::plan_id, planner_rt_fetch, replace_nestloop_params(), root, RTE_CTE, RangeTblEntry::rtekind, CteScan::scan, and SubPlan::setParam.

Referenced by create_scan_plan().

◆ create_customscan_plan()

static CustomScan * create_customscan_plan ( PlannerInfo root,
CustomPath best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 4280 of file createplan.c.

4282{
4283 CustomScan *cplan;
4284 RelOptInfo *rel = best_path->path.parent;
4285 List *custom_plans = NIL;
4286 ListCell *lc;
4287
4288 /* Recursively transform child paths. */
4289 foreach(lc, best_path->custom_paths)
4290 {
4293
4294 custom_plans = lappend(custom_plans, plan);
4295 }
4296
4297 /*
4298 * Sort clauses into the best execution order, although custom-scan
4299 * provider can reorder them again.
4300 */
4301 scan_clauses = order_qual_clauses(root, scan_clauses);
4302
4303 /*
4304 * Invoke custom plan provider to create the Plan node represented by the
4305 * CustomPath.
4306 */
4307 cplan = castNode(CustomScan,
4308 best_path->methods->PlanCustomPath(root,
4309 rel,
4310 best_path,
4311 tlist,
4312 scan_clauses,
4313 custom_plans));
4314
4315 /*
4316 * Copy cost data from Path to Plan; no need to make custom-plan providers
4317 * do this
4318 */
4319 copy_generic_path_info(&cplan->scan.plan, &best_path->path);
4320
4321 /* Likewise, copy the relids that are represented by this custom scan */
4322 cplan->custom_relids = best_path->path.parent->relids;
4323
4324 /*
4325 * Replace any outer-relation variables with nestloop params in the qual
4326 * and custom_exprs expressions. We do this last so that the custom-plan
4327 * provider doesn't have to be involved. (Note that parts of custom_exprs
4328 * could have come from join clauses, so doing this beforehand on the
4329 * scan_clauses wouldn't work.) We assume custom_scan_tlist contains no
4330 * such variables.
4331 */
4332 if (best_path->path.param_info)
4333 {
4334 cplan->scan.plan.qual = (List *)
4335 replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
4336 cplan->custom_exprs = (List *)
4338 }
4339
4340 return cplan;
4341}
struct Plan *(* PlanCustomPath)(PlannerInfo *root, RelOptInfo *rel, struct CustomPath *best_path, List *tlist, List *clauses, List *custom_plans)
Definition: extensible.h:97
const struct CustomPathMethods * methods
Definition: pathnodes.h:1923
List * custom_paths
Definition: pathnodes.h:1920
Scan scan
Definition: plannodes.h:744
Bitmapset * custom_relids
Definition: plannodes.h:751
List * custom_exprs
Definition: plannodes.h:748

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(), CustomPath::path, plan, CustomPathMethods::PlanCustomPath, replace_nestloop_params(), root, and CustomScan::scan.

Referenced by create_scan_plan().

◆ create_foreignscan_plan()

static ForeignScan * create_foreignscan_plan ( PlannerInfo root,
ForeignPath best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 4125 of file createplan.c.

4127{
4128 ForeignScan *scan_plan;
4129 RelOptInfo *rel = best_path->path.parent;
4130 Index scan_relid = rel->relid;
4131 Oid rel_oid = InvalidOid;
4132 Plan *outer_plan = NULL;
4133
4134 Assert(rel->fdwroutine != NULL);
4135
4136 /* transform the child path if any */
4137 if (best_path->fdw_outerpath)
4138 outer_plan = create_plan_recurse(root, best_path->fdw_outerpath,
4140
4141 /*
4142 * If we're scanning a base relation, fetch its OID. (Irrelevant if
4143 * scanning a join relation.)
4144 */
4145 if (scan_relid > 0)
4146 {
4147 RangeTblEntry *rte;
4148
4149 Assert(rel->rtekind == RTE_RELATION);
4150 rte = planner_rt_fetch(scan_relid, root);
4151 Assert(rte->rtekind == RTE_RELATION);
4152 rel_oid = rte->relid;
4153 }
4154
4155 /*
4156 * Sort clauses into best execution order. We do this first since the FDW
4157 * might have more info than we do and wish to adjust the ordering.
4158 */
4159 scan_clauses = order_qual_clauses(root, scan_clauses);
4160
4161 /*
4162 * Let the FDW perform its processing on the restriction clauses and
4163 * generate the plan node. Note that the FDW might remove restriction
4164 * clauses that it intends to execute remotely, or even add more (if it
4165 * has selected some join clauses for remote use but also wants them
4166 * rechecked locally).
4167 */
4168 scan_plan = rel->fdwroutine->GetForeignPlan(root, rel, rel_oid,
4169 best_path,
4170 tlist, scan_clauses,
4171 outer_plan);
4172
4173 /* Copy cost data from Path to Plan; no need to make FDW do this */
4174 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
4175
4176 /* Copy user OID to access as; likewise no need to make FDW do this */
4177 scan_plan->checkAsUser = rel->userid;
4178
4179 /* Copy foreign server OID; likewise, no need to make FDW do this */
4180 scan_plan->fs_server = rel->serverid;
4181
4182 /*
4183 * Likewise, copy the relids that are represented by this foreign scan. An
4184 * upper rel doesn't have relids set, but it covers all the relations
4185 * participating in the underlying scan/join, so use root->all_query_rels.
4186 */
4187 if (rel->reloptkind == RELOPT_UPPER_REL)
4188 scan_plan->fs_relids = root->all_query_rels;
4189 else
4190 scan_plan->fs_relids = best_path->path.parent->relids;
4191
4192 /*
4193 * Join relid sets include relevant outer joins, but FDWs may need to know
4194 * which are the included base rels. That's a bit tedious to get without
4195 * access to the plan-time data structures, so compute it here.
4196 */
4197 scan_plan->fs_base_relids = bms_difference(scan_plan->fs_relids,
4198 root->outer_join_rels);
4199
4200 /*
4201 * If this is a foreign join, and to make it valid to push down we had to
4202 * assume that the current user is the same as some user explicitly named
4203 * in the query, mark the finished plan as depending on the current user.
4204 */
4205 if (rel->useridiscurrent)
4206 root->glob->dependsOnRole = true;
4207
4208 /*
4209 * Replace any outer-relation variables with nestloop params in the qual,
4210 * fdw_exprs and fdw_recheck_quals expressions. We do this last so that
4211 * the FDW doesn't have to be involved. (Note that parts of fdw_exprs or
4212 * fdw_recheck_quals could have come from join clauses, so doing this
4213 * beforehand on the scan_clauses wouldn't work.) We assume
4214 * fdw_scan_tlist contains no such variables.
4215 */
4216 if (best_path->path.param_info)
4217 {
4218 scan_plan->scan.plan.qual = (List *)
4219 replace_nestloop_params(root, (Node *) scan_plan->scan.plan.qual);
4220 scan_plan->fdw_exprs = (List *)
4221 replace_nestloop_params(root, (Node *) scan_plan->fdw_exprs);
4222 scan_plan->fdw_recheck_quals = (List *)
4224 (Node *) scan_plan->fdw_recheck_quals);
4225 }
4226
4227 /*
4228 * If rel is a base relation, detect whether any system columns are
4229 * requested from the rel. (If rel is a join relation, rel->relid will be
4230 * 0, but there can be no Var with relid 0 in the rel's targetlist or the
4231 * restriction clauses, so we skip this in that case. Note that any such
4232 * columns in base relations that were joined are assumed to be contained
4233 * in fdw_scan_tlist.) This is a bit of a kluge and might go away
4234 * someday, so we intentionally leave it out of the API presented to FDWs.
4235 */
4236 scan_plan->fsSystemCol = false;
4237 if (scan_relid > 0)
4238 {
4239 Bitmapset *attrs_used = NULL;
4240 ListCell *lc;
4241 int i;
4242
4243 /*
4244 * First, examine all the attributes needed for joins or final output.
4245 * Note: we must look at rel's targetlist, not the attr_needed data,
4246 * because attr_needed isn't computed for inheritance child rels.
4247 */
4248 pull_varattnos((Node *) rel->reltarget->exprs, scan_relid, &attrs_used);
4249
4250 /* Add all the attributes used by restriction clauses. */
4251 foreach(lc, rel->baserestrictinfo)
4252 {
4253 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
4254
4255 pull_varattnos((Node *) rinfo->clause, scan_relid, &attrs_used);
4256 }
4257
4258 /* Now, are any system columns requested from rel? */
4259 for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
4260 {
4262 {
4263 scan_plan->fsSystemCol = true;
4264 break;
4265 }
4266 }
4267
4268 bms_free(attrs_used);
4269 }
4270
4271 return scan_plan;
4272}
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:346
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
int i
Definition: isn.c:72
@ RELOPT_UPPER_REL
Definition: pathnodes.h:831
#define InvalidOid
Definition: postgres_ext.h:37
Path * fdw_outerpath
Definition: pathnodes.h:1882
Oid checkAsUser
Definition: plannodes.h:715
Oid fs_server
Definition: plannodes.h:717
List * fdw_exprs
Definition: plannodes.h:718
bool fsSystemCol
Definition: plannodes.h:724
Bitmapset * fs_relids
Definition: plannodes.h:722
Bitmapset * fs_base_relids
Definition: plannodes.h:723
List * fdw_recheck_quals
Definition: plannodes.h:721
List * exprs
Definition: pathnodes.h:1544
bool useridiscurrent
Definition: pathnodes.h:968
struct PathTarget * reltarget
Definition: pathnodes.h:893
Index relid
Definition: pathnodes.h:918
RelOptKind reloptkind
Definition: pathnodes.h:865
Oid userid
Definition: pathnodes.h:966
Oid serverid
Definition: pathnodes.h:964
RTEKind rtekind
Definition: pathnodes.h:922
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:296

References Assert, RelOptInfo::baserestrictinfo, bms_difference(), bms_free(), bms_is_member(), ForeignScan::checkAsUser, RestrictInfo::clause, copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), PathTarget::exprs, ForeignScan::fdw_exprs, ForeignPath::fdw_outerpath, ForeignScan::fdw_recheck_quals, FirstLowInvalidHeapAttributeNumber, ForeignScan::fs_base_relids, ForeignScan::fs_relids, ForeignScan::fs_server, ForeignScan::fsSystemCol, i, InvalidOid, lfirst, order_qual_clauses(), ForeignPath::path, planner_rt_fetch, pull_varattnos(), RangeTblEntry::relid, RelOptInfo::relid, RELOPT_UPPER_REL, RelOptInfo::reloptkind, RelOptInfo::reltarget, replace_nestloop_params(), root, RTE_RELATION, RangeTblEntry::rtekind, RelOptInfo::rtekind, ForeignScan::scan, RelOptInfo::serverid, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by create_scan_plan().

◆ create_functionscan_plan()

static FunctionScan * create_functionscan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3764 of file createplan.c.

3766{
3767 FunctionScan *scan_plan;
3768 Index scan_relid = best_path->parent->relid;
3769 RangeTblEntry *rte;
3770 List *functions;
3771
3772 /* it should be a function base rel... */
3773 Assert(scan_relid > 0);
3774 rte = planner_rt_fetch(scan_relid, root);
3775 Assert(rte->rtekind == RTE_FUNCTION);
3776 functions = rte->functions;
3777
3778 /* Sort clauses into best execution order */
3779 scan_clauses = order_qual_clauses(root, scan_clauses);
3780
3781 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3782 scan_clauses = extract_actual_clauses(scan_clauses, false);
3783
3784 /* Replace any outer-relation variables with nestloop params */
3785 if (best_path->param_info)
3786 {
3787 scan_clauses = (List *)
3788 replace_nestloop_params(root, (Node *) scan_clauses);
3789 /* The function expressions could contain nestloop params, too */
3791 }
3792
3793 scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
3795
3796 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3797
3798 return scan_plan;
3799}
static FunctionScan * make_functionscan(List *qptlist, List *qpqual, Index scanrelid, List *functions, bool funcordinality)
Definition: createplan.c:5779
@ RTE_FUNCTION
Definition: parsenodes.h:1029
static const struct fns functions
Definition: regcomp.c:358
bool funcordinality
Definition: parsenodes.h:1188
List * functions
Definition: parsenodes.h:1186

References Assert, copy_generic_path_info(), extract_actual_clauses(), RangeTblEntry::funcordinality, functions, RangeTblEntry::functions, make_functionscan(), order_qual_clauses(), planner_rt_fetch, replace_nestloop_params(), root, RTE_FUNCTION, RangeTblEntry::rtekind, and FunctionScan::scan.

Referenced by create_scan_plan().

◆ create_gather_merge_plan()

static GatherMerge * create_gather_merge_plan ( PlannerInfo root,
GatherMergePath best_path 
)
static

Definition at line 1961 of file createplan.c.

1962{
1963 GatherMerge *gm_plan;
1964 Plan *subplan;
1965 List *pathkeys = best_path->path.pathkeys;
1966 List *tlist = build_path_tlist(root, &best_path->path);
1967
1968 /* As with Gather, project away columns in the workers. */
1969 subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1970
1971 /* Create a shell for a GatherMerge plan. */
1972 gm_plan = makeNode(GatherMerge);
1973 gm_plan->plan.targetlist = tlist;
1974 gm_plan->num_workers = best_path->num_workers;
1975 copy_generic_path_info(&gm_plan->plan, &best_path->path);
1976
1977 /* Assign the rescan Param. */
1979
1980 /* Gather Merge is pointless with no pathkeys; use Gather instead. */
1981 Assert(pathkeys != NIL);
1982
1983 /* Compute sort column info, and adjust subplan's tlist as needed */
1984 subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1985 best_path->subpath->parent->relids,
1986 gm_plan->sortColIdx,
1987 false,
1988 &gm_plan->numCols,
1989 &gm_plan->sortColIdx,
1990 &gm_plan->sortOperators,
1991 &gm_plan->collations,
1992 &gm_plan->nullsFirst);
1993
1994 /*
1995 * All gather merge paths should have already guaranteed the necessary
1996 * sort order. See create_gather_merge_path.
1997 */
1998 Assert(pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys));
1999
2000 /* Now insert the subplan under GatherMerge. */
2001 gm_plan->plan.lefttree = subplan;
2002
2003 /* use parallel mode for parallel plans. */
2004 root->glob->parallelModeNeeded = true;
2005
2006 return gm_plan;
2007}
int assign_special_exec_param(PlannerInfo *root)
Definition: paramassign.c:711
int rescan_param
Definition: plannodes.h:1166
int num_workers
Definition: plannodes.h:1163
struct Plan * lefttree
Definition: plannodes.h:155

References Assert, assign_special_exec_param(), build_path_tlist(), copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), Plan::lefttree, makeNode, NIL, GatherMergePath::num_workers, GatherMerge::num_workers, GatherMerge::numCols, GatherMergePath::path, Path::pathkeys, pathkeys_contained_in(), GatherMerge::plan, prepare_sort_from_pathkeys(), GatherMerge::rescan_param, root, GatherMergePath::subpath, and Plan::targetlist.

Referenced by create_plan_recurse().

◆ create_gather_plan()

static Gather * create_gather_plan ( PlannerInfo root,
GatherPath best_path 
)
static

Definition at line 1923 of file createplan.c.

1924{
1925 Gather *gather_plan;
1926 Plan *subplan;
1927 List *tlist;
1928
1929 /*
1930 * Push projection down to the child node. That way, the projection work
1931 * is parallelized, and there can be no system columns in the result (they
1932 * can't travel through a tuple queue because it uses MinimalTuple
1933 * representation).
1934 */
1935 subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1936
1937 tlist = build_path_tlist(root, &best_path->path);
1938
1939 gather_plan = make_gather(tlist,
1940 NIL,
1941 best_path->num_workers,
1943 best_path->single_copy,
1944 subplan);
1945
1946 copy_generic_path_info(&gather_plan->plan, &best_path->path);
1947
1948 /* use parallel mode for parallel plans. */
1949 root->glob->parallelModeNeeded = true;
1950
1951 return gather_plan;
1952}
static Gather * make_gather(List *qptlist, List *qpqual, int nworkers, int rescan_param, bool single_copy, Plan *subplan)
Definition: createplan.c:6933
bool single_copy
Definition: pathnodes.h:2055
Path * subpath
Definition: pathnodes.h:2054
int num_workers
Definition: pathnodes.h:2056
Plan plan
Definition: plannodes.h:1145

References assign_special_exec_param(), build_path_tlist(), copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), make_gather(), NIL, GatherPath::num_workers, GatherPath::path, Gather::plan, root, GatherPath::single_copy, and GatherPath::subpath.

Referenced by create_plan_recurse().

◆ create_gating_plan()

static Plan * create_gating_plan ( PlannerInfo root,
Path path,
Plan plan,
List gating_quals 
)
static

Definition at line 1025 of file createplan.c.

1027{
1028 Plan *gplan;
1029 Plan *splan;
1030
1031 Assert(gating_quals);
1032
1033 /*
1034 * We might have a trivial Result plan already. Stacking one Result atop
1035 * another is silly, so if that applies, just discard the input plan.
1036 * (We're assuming its targetlist is uninteresting; it should be either
1037 * the same as the result of build_path_tlist, or a simplified version.)
1038 */
1039 splan = plan;
1040 if (IsA(plan, Result))
1041 {
1042 Result *rplan = (Result *) plan;
1043
1044 if (rplan->plan.lefttree == NULL &&
1045 rplan->resconstantqual == NULL)
1046 splan = NULL;
1047 }
1048
1049 /*
1050 * Since we need a Result node anyway, always return the path's requested
1051 * tlist; that's never a wrong choice, even if the parent node didn't ask
1052 * for CP_EXACT_TLIST.
1053 */
1054 gplan = (Plan *) make_result(build_path_tlist(root, path),
1055 (Node *) gating_quals,
1056 splan);
1057
1058 /*
1059 * Notice that we don't change cost or size estimates when doing gating.
1060 * The costs of qual eval were already included in the subplan's cost.
1061 * Leaving the size alone amounts to assuming that the gating qual will
1062 * succeed, which is the conservative estimate for planning upper queries.
1063 * We certainly don't want to assume the output size is zero (unless the
1064 * gating qual is actually constant FALSE, and that case is dealt with in
1065 * clausesel.c). Interpolating between the two cases is silly, because it
1066 * doesn't reflect what will really happen at runtime, and besides which
1067 * in most cases we have only a very bad idea of the probability of the
1068 * gating qual being true.
1069 */
1070 copy_plan_costsize(gplan, plan);
1071
1072 /* Gating quals could be unsafe, so better use the Path's safety flag */
1073 gplan->parallel_safe = path->parallel_safe;
1074
1075 return gplan;
1076}
static void copy_plan_costsize(Plan *dest, Plan *src)
Definition: createplan.c:5471
static SPIPlanPtr splan
Definition: regress.c:267
Node * resconstantqual
Definition: plannodes.h:200
Plan plan
Definition: plannodes.h:199

References Assert, build_path_tlist(), copy_plan_costsize(), IsA, Plan::lefttree, make_result(), Path::parallel_safe, Plan::parallel_safe, Result::plan, plan, Result::resconstantqual, root, and splan.

Referenced by create_join_plan(), and create_scan_plan().

◆ create_group_plan()

static Group * create_group_plan ( PlannerInfo root,
GroupPath best_path 
)
static

Definition at line 2240 of file createplan.c.

2241{
2242 Group *plan;
2243 Plan *subplan;
2244 List *tlist;
2245 List *quals;
2246
2247 /*
2248 * Group can project, so no need to be terribly picky about child tlist,
2249 * but we do need grouping columns to be available
2250 */
2251 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2252
2253 tlist = build_path_tlist(root, &best_path->path);
2254
2255 quals = order_qual_clauses(root, best_path->qual);
2256
2257 plan = make_group(tlist,
2258 quals,
2259 list_length(best_path->groupClause),
2261 subplan->targetlist),
2264 subplan->targetlist),
2265 subplan);
2266
2267 copy_generic_path_info(&plan->plan, (Path *) best_path);
2268
2269 return plan;
2270}
static Group * make_group(List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations, Plan *lefttree)
Definition: createplan.c:6748
List * qual
Definition: pathnodes.h:2240
List * groupClause
Definition: pathnodes.h:2239
Path * subpath
Definition: pathnodes.h:2238
Path path
Definition: pathnodes.h:2237

References build_path_tlist(), copy_generic_path_info(), CP_LABEL_TLIST, create_plan_recurse(), extract_grouping_collations(), extract_grouping_cols(), extract_grouping_ops(), GroupPath::groupClause, list_length(), make_group(), order_qual_clauses(), GroupPath::path, plan, GroupPath::qual, root, GroupPath::subpath, and Plan::targetlist.

Referenced by create_plan_recurse().

◆ create_group_result_plan()

static Result * create_group_result_plan ( PlannerInfo root,
GroupResultPath best_path 
)
static

Definition at line 1590 of file createplan.c.

1591{
1592 Result *plan;
1593 List *tlist;
1594 List *quals;
1595
1596 tlist = build_path_tlist(root, &best_path->path);
1597
1598 /* best_path->quals is just bare clauses */
1599 quals = order_qual_clauses(root, best_path->quals);
1600
1601 plan = make_result(tlist, (Node *) quals, NULL);
1602
1603 copy_generic_path_info(&plan->plan, (Path *) best_path);
1604
1605 return plan;
1606}

References build_path_tlist(), copy_generic_path_info(), make_result(), order_qual_clauses(), GroupResultPath::path, plan, GroupResultPath::quals, and root.

Referenced by create_plan_recurse().

◆ create_groupingsets_plan()

static Plan * create_groupingsets_plan ( PlannerInfo root,
GroupingSetsPath best_path 
)
static

Definition at line 2391 of file createplan.c.

2392{
2393 Agg *plan;
2394 Plan *subplan;
2395 List *rollups = best_path->rollups;
2396 AttrNumber *grouping_map;
2397 int maxref;
2398 List *chain;
2399 ListCell *lc;
2400
2401 /* Shouldn't get here without grouping sets */
2402 Assert(root->parse->groupingSets);
2403 Assert(rollups != NIL);
2404
2405 /*
2406 * Agg can project, so no need to be terribly picky about child tlist, but
2407 * we do need grouping columns to be available
2408 */
2409 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2410
2411 /*
2412 * Compute the mapping from tleSortGroupRef to column index in the child's
2413 * tlist. First, identify max SortGroupRef in groupClause, for array
2414 * sizing.
2415 */
2416 maxref = 0;
2417 foreach(lc, root->processed_groupClause)
2418 {
2420
2421 if (gc->tleSortGroupRef > maxref)
2422 maxref = gc->tleSortGroupRef;
2423 }
2424
2425 grouping_map = (AttrNumber *) palloc0((maxref + 1) * sizeof(AttrNumber));
2426
2427 /* Now look up the column numbers in the child's tlist */
2428 foreach(lc, root->processed_groupClause)
2429 {
2431 TargetEntry *tle = get_sortgroupclause_tle(gc, subplan->targetlist);
2432
2433 grouping_map[gc->tleSortGroupRef] = tle->resno;
2434 }
2435
2436 /*
2437 * During setrefs.c, we'll need the grouping_map to fix up the cols lists
2438 * in GroupingFunc nodes. Save it for setrefs.c to use.
2439 */
2440 Assert(root->grouping_map == NULL);
2441 root->grouping_map = grouping_map;
2442
2443 /*
2444 * Generate the side nodes that describe the other sort and group
2445 * operations besides the top one. Note that we don't worry about putting
2446 * accurate cost estimates in the side nodes; only the topmost Agg node's
2447 * costs will be shown by EXPLAIN.
2448 */
2449 chain = NIL;
2450 if (list_length(rollups) > 1)
2451 {
2452 bool is_first_sort = ((RollupData *) linitial(rollups))->is_hashed;
2453
2454 for_each_from(lc, rollups, 1)
2455 {
2456 RollupData *rollup = lfirst(lc);
2457 AttrNumber *new_grpColIdx;
2458 Plan *sort_plan = NULL;
2459 Plan *agg_plan;
2460 AggStrategy strat;
2461
2462 new_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2463
2464 if (!rollup->is_hashed && !is_first_sort)
2465 {
2466 sort_plan = (Plan *)
2468 new_grpColIdx,
2469 subplan);
2470 }
2471
2472 if (!rollup->is_hashed)
2473 is_first_sort = false;
2474
2475 if (rollup->is_hashed)
2476 strat = AGG_HASHED;
2477 else if (linitial(rollup->gsets) == NIL)
2478 strat = AGG_PLAIN;
2479 else
2480 strat = AGG_SORTED;
2481
2482 agg_plan = (Plan *) make_agg(NIL,
2483 NIL,
2484 strat,
2486 list_length((List *) linitial(rollup->gsets)),
2487 new_grpColIdx,
2490 rollup->gsets,
2491 NIL,
2492 rollup->numGroups,
2493 best_path->transitionSpace,
2494 sort_plan);
2495
2496 /*
2497 * Remove stuff we don't need to avoid bloating debug output.
2498 */
2499 if (sort_plan)
2500 {
2501 sort_plan->targetlist = NIL;
2502 sort_plan->lefttree = NULL;
2503 }
2504
2505 chain = lappend(chain, agg_plan);
2506 }
2507 }
2508
2509 /*
2510 * Now make the real Agg node
2511 */
2512 {
2513 RollupData *rollup = linitial(rollups);
2514 AttrNumber *top_grpColIdx;
2515 int numGroupCols;
2516
2517 top_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2518
2519 numGroupCols = list_length((List *) linitial(rollup->gsets));
2520
2521 plan = make_agg(build_path_tlist(root, &best_path->path),
2522 best_path->qual,
2523 best_path->aggstrategy,
2525 numGroupCols,
2526 top_grpColIdx,
2529 rollup->gsets,
2530 chain,
2531 rollup->numGroups,
2532 best_path->transitionSpace,
2533 subplan);
2534
2535 /* Copy cost data from Path to Plan */
2536 copy_generic_path_info(&plan->plan, &best_path->path);
2537 }
2538
2539 return (Plan *) plan;
2540}
static AttrNumber * remap_groupColIdx(PlannerInfo *root, List *groupClause)
Definition: createplan.c:2353
static Sort * make_sort_from_groupcols(List *groupcls, AttrNumber *grpColIdx, Plan *lefttree)
Definition: createplan.c:6541
void * palloc0(Size size)
Definition: mcxt.c:1347
AggStrategy
Definition: nodes.h:353
@ AGG_SORTED
Definition: nodes.h:355
@ AGG_HASHED
Definition: nodes.h:356
@ AGG_PLAIN
Definition: nodes.h:354
@ AGGSPLIT_SIMPLE
Definition: nodes.h:377
#define for_each_from(cell, lst, N)
Definition: pg_list.h:414
uint64 transitionSpace
Definition: pathnodes.h:2312
AggStrategy aggstrategy
Definition: pathnodes.h:2309
Cardinality numGroups
Definition: pathnodes.h:2296
List * groupClause
Definition: pathnodes.h:2293
List * gsets
Definition: pathnodes.h:2294
bool is_hashed
Definition: pathnodes.h:2298
Index tleSortGroupRef
Definition: parsenodes.h:1447
AttrNumber resno
Definition: primnodes.h:2247
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:367

References AGG_HASHED, AGG_PLAIN, AGG_SORTED, AGGSPLIT_SIMPLE, GroupingSetsPath::aggstrategy, Assert, build_path_tlist(), copy_generic_path_info(), CP_LABEL_TLIST, create_plan_recurse(), extract_grouping_collations(), extract_grouping_ops(), for_each_from, get_sortgroupclause_tle(), RollupData::groupClause, RollupData::gsets, RollupData::is_hashed, lappend(), Plan::lefttree, lfirst, linitial, list_length(), make_agg(), make_sort_from_groupcols(), NIL, RollupData::numGroups, palloc0(), GroupingSetsPath::path, plan, GroupingSetsPath::qual, remap_groupColIdx(), TargetEntry::resno, GroupingSetsPath::rollups, root, GroupingSetsPath::subpath, Plan::targetlist, SortGroupClause::tleSortGroupRef, and GroupingSetsPath::transitionSpace.

Referenced by create_plan_recurse().

◆ create_hashjoin_plan()

static HashJoin * create_hashjoin_plan ( PlannerInfo root,
HashPath best_path 
)
static

Definition at line 4794 of file createplan.c.

4796{
4797 HashJoin *join_plan;
4798 Hash *hash_plan;
4799 Plan *outer_plan;
4800 Plan *inner_plan;
4801 List *tlist = build_path_tlist(root, &best_path->jpath.path);
4802 List *joinclauses;
4803 List *otherclauses;
4804 List *hashclauses;
4805 List *hashoperators = NIL;
4806 List *hashcollations = NIL;
4807 List *inner_hashkeys = NIL;
4808 List *outer_hashkeys = NIL;
4809 Oid skewTable = InvalidOid;
4810 AttrNumber skewColumn = InvalidAttrNumber;
4811 bool skewInherit = false;
4812 ListCell *lc;
4813
4814 /*
4815 * HashJoin can project, so we don't have to demand exact tlists from the
4816 * inputs. However, it's best to request a small tlist from the inner
4817 * side, so that we aren't storing more data than necessary. Likewise, if
4818 * we anticipate batching, request a small tlist from the outer side so
4819 * that we don't put extra data in the outer batch files.
4820 */
4821 outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4822 (best_path->num_batches > 1) ? CP_SMALL_TLIST : 0);
4823
4824 inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4826
4827 /* Sort join qual clauses into best execution order */
4828 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4829 /* There's no point in sorting the hash clauses ... */
4830
4831 /* Get the join qual clauses (in plain expression form) */
4832 /* Any pseudoconstant clauses are ignored here */
4833 if (IS_OUTER_JOIN(best_path->jpath.jointype))
4834 {
4835 extract_actual_join_clauses(joinclauses,
4836 best_path->jpath.path.parent->relids,
4837 &joinclauses, &otherclauses);
4838 }
4839 else
4840 {
4841 /* We can treat all clauses alike for an inner join */
4842 joinclauses = extract_actual_clauses(joinclauses, false);
4843 otherclauses = NIL;
4844 }
4845
4846 /*
4847 * Remove the hashclauses from the list of join qual clauses, leaving the
4848 * list of quals that must be checked as qpquals.
4849 */
4850 hashclauses = get_actual_clauses(best_path->path_hashclauses);
4851 joinclauses = list_difference(joinclauses, hashclauses);
4852
4853 /*
4854 * Replace any outer-relation variables with nestloop params. There
4855 * should not be any in the hashclauses.
4856 */
4857 if (best_path->jpath.path.param_info)
4858 {
4859 joinclauses = (List *)
4860 replace_nestloop_params(root, (Node *) joinclauses);
4861 otherclauses = (List *)
4862 replace_nestloop_params(root, (Node *) otherclauses);
4863 }
4864
4865 /*
4866 * Rearrange hashclauses, if needed, so that the outer variable is always
4867 * on the left.
4868 */
4869 hashclauses = get_switched_clauses(best_path->path_hashclauses,
4870 best_path->jpath.outerjoinpath->parent->relids);
4871
4872 /*
4873 * If there is a single join clause and we can identify the outer variable
4874 * as a simple column reference, supply its identity for possible use in
4875 * skew optimization. (Note: in principle we could do skew optimization
4876 * with multiple join clauses, but we'd have to be able to determine the
4877 * most common combinations of outer values, which we don't currently have
4878 * enough stats for.)
4879 */
4880 if (list_length(hashclauses) == 1)
4881 {
4882 OpExpr *clause = (OpExpr *) linitial(hashclauses);
4883 Node *node;
4884
4885 Assert(is_opclause(clause));
4886 node = (Node *) linitial(clause->args);
4887 if (IsA(node, RelabelType))
4888 node = (Node *) ((RelabelType *) node)->arg;
4889 if (IsA(node, Var))
4890 {
4891 Var *var = (Var *) node;
4892 RangeTblEntry *rte;
4893
4894 rte = root->simple_rte_array[var->varno];
4895 if (rte->rtekind == RTE_RELATION)
4896 {
4897 skewTable = rte->relid;
4898 skewColumn = var->varattno;
4899 skewInherit = rte->inh;
4900 }
4901 }
4902 }
4903
4904 /*
4905 * Collect hash related information. The hashed expressions are
4906 * deconstructed into outer/inner expressions, so they can be computed
4907 * separately (inner expressions are used to build the hashtable via Hash,
4908 * outer expressions to perform lookups of tuples from HashJoin's outer
4909 * plan in the hashtable). Also collect operator information necessary to
4910 * build the hashtable.
4911 */
4912 foreach(lc, hashclauses)
4913 {
4914 OpExpr *hclause = lfirst_node(OpExpr, lc);
4915
4916 hashoperators = lappend_oid(hashoperators, hclause->opno);
4917 hashcollations = lappend_oid(hashcollations, hclause->inputcollid);
4918 outer_hashkeys = lappend(outer_hashkeys, linitial(hclause->args));
4919 inner_hashkeys = lappend(inner_hashkeys, lsecond(hclause->args));
4920 }
4921
4922 /*
4923 * Build the hash node and hash join node.
4924 */
4925 hash_plan = make_hash(inner_plan,
4926 inner_hashkeys,
4927 skewTable,
4928 skewColumn,
4929 skewInherit);
4930
4931 /*
4932 * Set Hash node's startup & total costs equal to total cost of input
4933 * plan; this only affects EXPLAIN display not decisions.
4934 */
4935 copy_plan_costsize(&hash_plan->plan, inner_plan);
4936 hash_plan->plan.startup_cost = hash_plan->plan.total_cost;
4937
4938 /*
4939 * If parallel-aware, the executor will also need an estimate of the total
4940 * number of rows expected from all participants so that it can size the
4941 * shared hash table.
4942 */
4943 if (best_path->jpath.path.parallel_aware)
4944 {
4945 hash_plan->plan.parallel_aware = true;
4946 hash_plan->rows_total = best_path->inner_rows_total;
4947 }
4948
4949 join_plan = make_hashjoin(tlist,
4950 joinclauses,
4951 otherclauses,
4952 hashclauses,
4953 hashoperators,
4954 hashcollations,
4955 outer_hashkeys,
4956 outer_plan,
4957 (Plan *) hash_plan,
4958 best_path->jpath.jointype,
4959 best_path->jpath.inner_unique);
4960
4961 copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4962
4963 return join_plan;
4964}
#define InvalidAttrNumber
Definition: attnum.h:23
static HashJoin * make_hashjoin(List *tlist, List *joinclauses, List *otherclauses, List *hashclauses, List *hashoperators, List *hashcollations, List *hashkeys, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
Definition: createplan.c:6049
static List * get_switched_clauses(List *clauses, Relids outerrelids)
Definition: createplan.c:5284
static Hash * make_hash(Plan *lefttree, List *hashkeys, Oid skewTable, AttrNumber skewColumn, bool skewInherit)
Definition: createplan.c:6080
List * list_difference(const List *list1, const List *list2)
Definition: list.c:1237
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:76
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:338
#define lsecond(l)
Definition: pg_list.h:183
void extract_actual_join_clauses(List *restrictinfo_list, Relids joinrelids, List **joinquals, List **otherquals)
Definition: restrictinfo.c:513
Join join
Definition: plannodes.h:867
List * path_hashclauses
Definition: pathnodes.h:2164
Cardinality inner_rows_total
Definition: pathnodes.h:2166
int num_batches
Definition: pathnodes.h:2165
JoinPath jpath
Definition: pathnodes.h:2163
Cardinality rows_total
Definition: plannodes.h:1213
Plan plan
Definition: plannodes.h:1202
Path * outerjoinpath
Definition: pathnodes.h:2086
Path * innerjoinpath
Definition: pathnodes.h:2087
JoinType jointype
Definition: pathnodes.h:2081
bool inner_unique
Definition: pathnodes.h:2083
List * joinrestrictinfo
Definition: pathnodes.h:2089
Oid opno
Definition: primnodes.h:834
List * args
Definition: primnodes.h:852
bool parallel_aware
Definition: plannodes.h:141
Definition: primnodes.h:261
AttrNumber varattno
Definition: primnodes.h:273
int varno
Definition: primnodes.h:268

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, HashPath::inner_rows_total, JoinPath::inner_unique, JoinPath::innerjoinpath, InvalidAttrNumber, InvalidOid, is_opclause(), IS_OUTER_JOIN, IsA, HashJoin::join, JoinPath::joinrestrictinfo, JoinPath::jointype, HashPath::jpath, lappend(), lappend_oid(), lfirst_node, linitial, list_difference(), list_length(), lsecond, make_hash(), make_hashjoin(), NIL, HashPath::num_batches, OpExpr::opno, order_qual_clauses(), JoinPath::outerjoinpath, Plan::parallel_aware, HashPath::path_hashclauses, Hash::plan, RangeTblEntry::relid, replace_nestloop_params(), root, Hash::rows_total, RTE_RELATION, RangeTblEntry::rtekind, Plan::startup_cost, Plan::total_cost, Var::varattno, and Var::varno.

Referenced by create_join_plan().

◆ create_incrementalsort_plan()

static IncrementalSort * create_incrementalsort_plan ( PlannerInfo root,
IncrementalSortPath best_path,
int  flags 
)
static

Definition at line 2213 of file createplan.c.

2215{
2217 Plan *subplan;
2218
2219 /* See comments in create_sort_plan() above */
2220 subplan = create_plan_recurse(root, best_path->spath.subpath,
2221 flags | CP_SMALL_TLIST);
2223 best_path->spath.path.pathkeys,
2224 IS_OTHER_REL(best_path->spath.subpath->parent) ?
2225 best_path->spath.path.parent->relids : NULL,
2226 best_path->nPresortedCols);
2227
2228 copy_generic_path_info(&plan->sort.plan, (Path *) best_path);
2229
2230 return plan;
2231}
static IncrementalSort * make_incrementalsort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids, int nPresortedCols)
Definition: createplan.c:6458
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:854
Path path
Definition: pathnodes.h:2211
Path * subpath
Definition: pathnodes.h:2212

References copy_generic_path_info(), CP_SMALL_TLIST, create_plan_recurse(), IS_OTHER_REL, make_incrementalsort_from_pathkeys(), IncrementalSortPath::nPresortedCols, SortPath::path, Path::pathkeys, plan, root, IncrementalSortPath::spath, and SortPath::subpath.

Referenced by create_plan_recurse().

◆ create_indexscan_plan()

static Scan * create_indexscan_plan ( PlannerInfo root,
IndexPath best_path,
List tlist,
List scan_clauses,
bool  indexonly 
)
static

Definition at line 3009 of file createplan.c.

3014{
3015 Scan *scan_plan;
3016 List *indexclauses = best_path->indexclauses;
3017 List *indexorderbys = best_path->indexorderbys;
3018 Index baserelid = best_path->path.parent->relid;
3019 IndexOptInfo *indexinfo = best_path->indexinfo;
3020 Oid indexoid = indexinfo->indexoid;
3021 List *qpqual;
3022 List *stripped_indexquals;
3023 List *fixed_indexquals;
3024 List *fixed_indexorderbys;
3025 List *indexorderbyops = NIL;
3026 ListCell *l;
3027
3028 /* it should be a base rel... */
3029 Assert(baserelid > 0);
3030 Assert(best_path->path.parent->rtekind == RTE_RELATION);
3031 /* check the scan direction is valid */
3033 best_path->indexscandir == BackwardScanDirection);
3034
3035 /*
3036 * Extract the index qual expressions (stripped of RestrictInfos) from the
3037 * IndexClauses list, and prepare a copy with index Vars substituted for
3038 * table Vars. (This step also does replace_nestloop_params on the
3039 * fixed_indexquals.)
3040 */
3041 fix_indexqual_references(root, best_path,
3042 &stripped_indexquals,
3043 &fixed_indexquals);
3044
3045 /*
3046 * Likewise fix up index attr references in the ORDER BY expressions.
3047 */
3048 fixed_indexorderbys = fix_indexorderby_references(root, best_path);
3049
3050 /*
3051 * The qpqual list must contain all restrictions not automatically handled
3052 * by the index, other than pseudoconstant clauses which will be handled
3053 * by a separate gating plan node. All the predicates in the indexquals
3054 * will be checked (either by the index itself, or by nodeIndexscan.c),
3055 * but if there are any "special" operators involved then they must be
3056 * included in qpqual. The upshot is that qpqual must contain
3057 * scan_clauses minus whatever appears in indexquals.
3058 *
3059 * is_redundant_with_indexclauses() detects cases where a scan clause is
3060 * present in the indexclauses list or is generated from the same
3061 * EquivalenceClass as some indexclause, and is therefore redundant with
3062 * it, though not equal. (The latter happens when indxpath.c prefers a
3063 * different derived equality than what generate_join_implied_equalities
3064 * picked for a parameterized scan's ppi_clauses.) Note that it will not
3065 * match to lossy index clauses, which is critical because we have to
3066 * include the original clause in qpqual in that case.
3067 *
3068 * In some situations (particularly with OR'd index conditions) we may
3069 * have scan_clauses that are not equal to, but are logically implied by,
3070 * the index quals; so we also try a predicate_implied_by() check to see
3071 * if we can discard quals that way. (predicate_implied_by assumes its
3072 * first input contains only immutable functions, so we have to check
3073 * that.)
3074 *
3075 * Note: if you change this bit of code you should also look at
3076 * extract_nonindex_conditions() in costsize.c.
3077 */
3078 qpqual = NIL;
3079 foreach(l, scan_clauses)
3080 {
3082
3083 if (rinfo->pseudoconstant)
3084 continue; /* we may drop pseudoconstants here */
3085 if (is_redundant_with_indexclauses(rinfo, indexclauses))
3086 continue; /* dup or derived from same EquivalenceClass */
3087 if (!contain_mutable_functions((Node *) rinfo->clause) &&
3088 predicate_implied_by(list_make1(rinfo->clause), stripped_indexquals,
3089 false))
3090 continue; /* provably implied by indexquals */
3091 qpqual = lappend(qpqual, rinfo);
3092 }
3093
3094 /* Sort clauses into best execution order */
3095 qpqual = order_qual_clauses(root, qpqual);
3096
3097 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3098 qpqual = extract_actual_clauses(qpqual, false);
3099
3100 /*
3101 * We have to replace any outer-relation variables with nestloop params in
3102 * the indexqualorig, qpqual, and indexorderbyorig expressions. A bit
3103 * annoying to have to do this separately from the processing in
3104 * fix_indexqual_references --- rethink this when generalizing the inner
3105 * indexscan support. But note we can't really do this earlier because
3106 * it'd break the comparisons to predicates above ... (or would it? Those
3107 * wouldn't have outer refs)
3108 */
3109 if (best_path->path.param_info)
3110 {
3111 stripped_indexquals = (List *)
3112 replace_nestloop_params(root, (Node *) stripped_indexquals);
3113 qpqual = (List *)
3114 replace_nestloop_params(root, (Node *) qpqual);
3115 indexorderbys = (List *)
3116 replace_nestloop_params(root, (Node *) indexorderbys);
3117 }
3118
3119 /*
3120 * If there are ORDER BY expressions, look up the sort operators for their
3121 * result datatypes.
3122 */
3123 if (indexorderbys)
3124 {
3125 ListCell *pathkeyCell,
3126 *exprCell;
3127
3128 /*
3129 * PathKey contains OID of the btree opfamily we're sorting by, but
3130 * that's not quite enough because we need the expression's datatype
3131 * to look up the sort operator in the operator family.
3132 */
3133 Assert(list_length(best_path->path.pathkeys) == list_length(indexorderbys));
3134 forboth(pathkeyCell, best_path->path.pathkeys, exprCell, indexorderbys)
3135 {
3136 PathKey *pathkey = (PathKey *) lfirst(pathkeyCell);
3137 Node *expr = (Node *) lfirst(exprCell);
3138 Oid exprtype = exprType(expr);
3139 Oid sortop;
3140
3141 /* Get sort operator from opfamily */
3142 sortop = get_opfamily_member(pathkey->pk_opfamily,
3143 exprtype,
3144 exprtype,
3145 pathkey->pk_strategy);
3146 if (!OidIsValid(sortop))
3147 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
3148 pathkey->pk_strategy, exprtype, exprtype, pathkey->pk_opfamily);
3149 indexorderbyops = lappend_oid(indexorderbyops, sortop);
3150 }
3151 }
3152
3153 /*
3154 * For an index-only scan, we must mark indextlist entries as resjunk if
3155 * they are columns that the index AM can't return; this cues setrefs.c to
3156 * not generate references to those columns.
3157 */
3158 if (indexonly)
3159 {
3160 int i = 0;
3161
3162 foreach(l, indexinfo->indextlist)
3163 {
3164 TargetEntry *indextle = (TargetEntry *) lfirst(l);
3165
3166 indextle->resjunk = !indexinfo->canreturn[i];
3167 i++;
3168 }
3169 }
3170
3171 /* Finally ready to build the plan node */
3172 if (indexonly)
3173 scan_plan = (Scan *) make_indexonlyscan(tlist,
3174 qpqual,
3175 baserelid,
3176 indexoid,
3177 fixed_indexquals,
3178 stripped_indexquals,
3179 fixed_indexorderbys,
3180 indexinfo->indextlist,
3181 best_path->indexscandir);
3182 else
3183 scan_plan = (Scan *) make_indexscan(tlist,
3184 qpqual,
3185 baserelid,
3186 indexoid,
3187 fixed_indexquals,
3188 stripped_indexquals,
3189 fixed_indexorderbys,
3190 indexorderbys,
3191 indexorderbyops,
3192 best_path->indexscandir);
3193
3194 copy_generic_path_info(&scan_plan->plan, &best_path->path);
3195
3196 return scan_plan;
3197}
#define OidIsValid(objectId)
Definition: c.h:732
static void fix_indexqual_references(PlannerInfo *root, IndexPath *index_path, List **stripped_indexquals_p, List **fixed_indexquals_p)
Definition: createplan.c:5068
static List * fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path)
Definition: createplan.c:5109
static IndexOnlyScan * make_indexonlyscan(List *qptlist, List *qpqual, Index scanrelid, Oid indexid, List *indexqual, List *recheckqual, List *indexorderby, List *indextlist, ScanDirection indexscandir)
Definition: createplan.c:5651
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:5620
bool is_redundant_with_indexclauses(RestrictInfo *rinfo, List *indexclauses)
Definition: equivclass.c:3351
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:166
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
@ BackwardScanDirection
Definition: sdir.h:26
@ ForwardScanDirection
Definition: sdir.h:28
List * indextlist
Definition: pathnodes.h:1177
ScanDirection indexscandir
Definition: pathnodes.h:1726
List * indexorderbys
Definition: pathnodes.h:1724
int pk_strategy
Definition: pathnodes.h:1481
Oid pk_opfamily
Definition: pathnodes.h:1480

References Assert, BackwardScanDirection, RestrictInfo::clause, contain_mutable_functions(), copy_generic_path_info(), elog, ERROR, exprType(), extract_actual_clauses(), fix_indexorderby_references(), fix_indexqual_references(), forboth, ForwardScanDirection, get_opfamily_member(), i, IndexPath::indexclauses, IndexPath::indexinfo, IndexOptInfo::indexoid, IndexPath::indexorderbys, IndexPath::indexscandir, IndexOptInfo::indextlist, is_redundant_with_indexclauses(), lappend(), lappend_oid(), lfirst, lfirst_node, list_length(), list_make1, make_indexonlyscan(), make_indexscan(), NIL, OidIsValid, order_qual_clauses(), IndexPath::path, Path::pathkeys, PathKey::pk_opfamily, PathKey::pk_strategy, predicate_implied_by(), replace_nestloop_params(), root, and RTE_RELATION.

Referenced by create_bitmap_subplan(), and create_scan_plan().

◆ create_join_plan()

static Plan * create_join_plan ( PlannerInfo root,
JoinPath best_path 
)
static

Definition at line 1084 of file createplan.c.

1085{
1086 Plan *plan;
1087 List *gating_clauses;
1088
1089 switch (best_path->path.pathtype)
1090 {
1091 case T_MergeJoin:
1093 (MergePath *) best_path);
1094 break;
1095 case T_HashJoin:
1097 (HashPath *) best_path);
1098 break;
1099 case T_NestLoop:
1101 (NestPath *) best_path);
1102 break;
1103 default:
1104 elog(ERROR, "unrecognized node type: %d",
1105 (int) best_path->path.pathtype);
1106 plan = NULL; /* keep compiler quiet */
1107 break;
1108 }
1109
1110 /*
1111 * If there are any pseudoconstant clauses attached to this node, insert a
1112 * gating Result node that evaluates the pseudoconstants as one-time
1113 * quals.
1114 */
1115 gating_clauses = get_gating_quals(root, best_path->joinrestrictinfo);
1116 if (gating_clauses)
1117 plan = create_gating_plan(root, (Path *) best_path, plan,
1118 gating_clauses);
1119
1120#ifdef NOT_USED
1121
1122 /*
1123 * * Expensive function pullups may have pulled local predicates * into
1124 * this path node. Put them in the qpqual of the plan node. * JMH,
1125 * 6/15/92
1126 */
1127 if (get_loc_restrictinfo(best_path) != NIL)
1128 set_qpqual((Plan) plan,
1129 list_concat(get_qpqual((Plan) plan),
1130 get_actual_clauses(get_loc_restrictinfo(best_path))));
1131#endif
1132
1133 return plan;
1134}
static HashJoin * create_hashjoin_plan(PlannerInfo *root, HashPath *best_path)
Definition: createplan.c:4794
static MergeJoin * create_mergejoin_plan(PlannerInfo *root, MergePath *best_path)
Definition: createplan.c:4443
static Plan * create_gating_plan(PlannerInfo *root, Path *path, Plan *plan, List *gating_quals)
Definition: createplan.c:1025
static List * get_gating_quals(PlannerInfo *root, List *quals)
Definition: createplan.c:1005
static NestLoop * create_nestloop_plan(PlannerInfo *root, NestPath *best_path)
Definition: createplan.c:4351

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, plan, and root.

Referenced by create_plan_recurse().

◆ create_limit_plan()

static Limit * create_limit_plan ( PlannerInfo root,
LimitPath best_path,
int  flags 
)
static

Definition at line 2859 of file createplan.c.

2860{
2861 Limit *plan;
2862 Plan *subplan;
2863 int numUniqkeys = 0;
2864 AttrNumber *uniqColIdx = NULL;
2865 Oid *uniqOperators = NULL;
2866 Oid *uniqCollations = NULL;
2867
2868 /* Limit doesn't project, so tlist requirements pass through */
2869 subplan = create_plan_recurse(root, best_path->subpath, flags);
2870
2871 /* Extract information necessary for comparing rows for WITH TIES. */
2872 if (best_path->limitOption == LIMIT_OPTION_WITH_TIES)
2873 {
2874 Query *parse = root->parse;
2875 ListCell *l;
2876
2877 numUniqkeys = list_length(parse->sortClause);
2878 uniqColIdx = (AttrNumber *) palloc(numUniqkeys * sizeof(AttrNumber));
2879 uniqOperators = (Oid *) palloc(numUniqkeys * sizeof(Oid));
2880 uniqCollations = (Oid *) palloc(numUniqkeys * sizeof(Oid));
2881
2882 numUniqkeys = 0;
2883 foreach(l, parse->sortClause)
2884 {
2885 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
2886 TargetEntry *tle = get_sortgroupclause_tle(sortcl, parse->targetList);
2887
2888 uniqColIdx[numUniqkeys] = tle->resno;
2889 uniqOperators[numUniqkeys] = sortcl->eqop;
2890 uniqCollations[numUniqkeys] = exprCollation((Node *) tle->expr);
2891 numUniqkeys++;
2892 }
2893 }
2894
2895 plan = make_limit(subplan,
2896 best_path->limitOffset,
2897 best_path->limitCount,
2898 best_path->limitOption,
2899 numUniqkeys, uniqColIdx, uniqOperators, uniqCollations);
2900
2901 copy_generic_path_info(&plan->plan, (Path *) best_path);
2902
2903 return plan;
2904}
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount, LimitOption limitOption, int uniqNumCols, AttrNumber *uniqColIdx, Oid *uniqOperators, Oid *uniqCollations)
Definition: createplan.c:7044
void * palloc(Size size)
Definition: mcxt.c:1317
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:821
@ LIMIT_OPTION_WITH_TIES
Definition: nodes.h:432
static struct subre * parse(struct vars *v, int stopper, int type, struct state *init, struct state *final)
Definition: regcomp.c:717
Path * subpath
Definition: pathnodes.h:2412
LimitOption limitOption
Definition: pathnodes.h:2415
Node * limitOffset
Definition: pathnodes.h:2413
Node * limitCount
Definition: pathnodes.h:2414
Expr * expr
Definition: primnodes.h:2245

References copy_generic_path_info(), create_plan_recurse(), SortGroupClause::eqop, TargetEntry::expr, exprCollation(), get_sortgroupclause_tle(), lfirst, LIMIT_OPTION_WITH_TIES, LimitPath::limitCount, LimitPath::limitOffset, LimitPath::limitOption, list_length(), make_limit(), palloc(), parse(), plan, TargetEntry::resno, root, and LimitPath::subpath.

Referenced by create_plan_recurse().

◆ create_lockrows_plan()

static LockRows * create_lockrows_plan ( PlannerInfo root,
LockRowsPath best_path,
int  flags 
)
static

Definition at line 2795 of file createplan.c.

2797{
2798 LockRows *plan;
2799 Plan *subplan;
2800
2801 /* LockRows doesn't project, so tlist requirements pass through */
2802 subplan = create_plan_recurse(root, best_path->subpath, flags);
2803
2804 plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
2805
2806 copy_generic_path_info(&plan->plan, (Path *) best_path);
2807
2808 return plan;
2809}
static LockRows * make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
Definition: createplan.c:7023
Path * subpath
Definition: pathnodes.h:2372
List * rowMarks
Definition: pathnodes.h:2373

References copy_generic_path_info(), create_plan_recurse(), LockRowsPath::epqParam, make_lockrows(), plan, root, LockRowsPath::rowMarks, and LockRowsPath::subpath.

Referenced by create_plan_recurse().

◆ create_material_plan()

static Material * create_material_plan ( PlannerInfo root,
MaterialPath best_path,
int  flags 
)
static

Definition at line 1641 of file createplan.c.

1642{
1643 Material *plan;
1644 Plan *subplan;
1645
1646 /*
1647 * We don't want any excess columns in the materialized tuples, so request
1648 * a smaller tlist. Otherwise, since Material doesn't project, tlist
1649 * requirements pass through.
1650 */
1651 subplan = create_plan_recurse(root, best_path->subpath,
1652 flags | CP_SMALL_TLIST);
1653
1654 plan = make_material(subplan);
1655
1656 copy_generic_path_info(&plan->plan, (Path *) best_path);
1657
1658 return plan;
1659}
static Material * make_material(Plan *lefttree)
Definition: createplan.c:6582
Path * subpath
Definition: pathnodes.h:1994

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

Referenced by create_plan_recurse().

◆ create_memoize_plan()

static Memoize * create_memoize_plan ( PlannerInfo root,
MemoizePath best_path,
int  flags 
)
static

Definition at line 1669 of file createplan.c.

1670{
1671 Memoize *plan;
1672 Bitmapset *keyparamids;
1673 Plan *subplan;
1674 Oid *operators;
1675 Oid *collations;
1676 List *param_exprs = NIL;
1677 ListCell *lc;
1678 ListCell *lc2;
1679 int nkeys;
1680 int i;
1681
1682 subplan = create_plan_recurse(root, best_path->subpath,
1683 flags | CP_SMALL_TLIST);
1684
1685 param_exprs = (List *) replace_nestloop_params(root, (Node *)
1686 best_path->param_exprs);
1687
1688 nkeys = list_length(param_exprs);
1689 Assert(nkeys > 0);
1690 operators = palloc(nkeys * sizeof(Oid));
1691 collations = palloc(nkeys * sizeof(Oid));
1692
1693 i = 0;
1694 forboth(lc, param_exprs, lc2, best_path->hash_operators)
1695 {
1696 Expr *param_expr = (Expr *) lfirst(lc);
1697 Oid opno = lfirst_oid(lc2);
1698
1699 operators[i] = opno;
1700 collations[i] = exprCollation((Node *) param_expr);
1701 i++;
1702 }
1703
1704 keyparamids = pull_paramids((Expr *) param_exprs);
1705
1706 plan = make_memoize(subplan, operators, collations, param_exprs,
1707 best_path->singlerow, best_path->binary_mode,
1708 best_path->est_entries, keyparamids);
1709
1710 copy_generic_path_info(&plan->plan, (Path *) best_path);
1711
1712 return plan;
1713}
Bitmapset * pull_paramids(Expr *expr)
Definition: clauses.c:5419
static Memoize * make_memoize(Plan *lefttree, Oid *hashoperators, Oid *collations, List *param_exprs, bool singlerow, bool binary_mode, uint32 est_entries, Bitmapset *keyparamids)
Definition: createplan.c:6647
#define lfirst_oid(lc)
Definition: pg_list.h:174
bool singlerow
Definition: pathnodes.h:2008
List * hash_operators
Definition: pathnodes.h:2006
uint32 est_entries
Definition: pathnodes.h:2013
bool binary_mode
Definition: pathnodes.h:2010
Path * subpath
Definition: pathnodes.h:2005
List * param_exprs
Definition: pathnodes.h:2007

References Assert, MemoizePath::binary_mode, copy_generic_path_info(), CP_SMALL_TLIST, create_plan_recurse(), MemoizePath::est_entries, exprCollation(), forboth, MemoizePath::hash_operators, i, lfirst, lfirst_oid, list_length(), make_memoize(), NIL, palloc(), MemoizePath::param_exprs, plan, pull_paramids(), replace_nestloop_params(), root, MemoizePath::singlerow, and MemoizePath::subpath.

Referenced by create_plan_recurse().

◆ create_merge_append_plan()

static Plan * create_merge_append_plan ( PlannerInfo root,
MergeAppendPath best_path,
int  flags 
)
static

Definition at line 1440 of file createplan.c.

1442{
1444 Plan *plan = &node->plan;
1445 List *tlist = build_path_tlist(root, &best_path->path);
1446 int orig_tlist_length = list_length(tlist);
1447 bool tlist_was_changed;
1448 List *pathkeys = best_path->path.pathkeys;
1449 List *subplans = NIL;
1450 ListCell *subpaths;
1451 RelOptInfo *rel = best_path->path.parent;
1452 PartitionPruneInfo *partpruneinfo = NULL;
1453
1454 /*
1455 * We don't have the actual creation of the MergeAppend node split out
1456 * into a separate make_xxx function. This is because we want to run
1457 * prepare_sort_from_pathkeys on it before we do so on the individual
1458 * child plans, to make cross-checking the sort info easier.
1459 */
1460 copy_generic_path_info(plan, (Path *) best_path);
1461 plan->targetlist = tlist;
1462 plan->qual = NIL;
1463 plan->lefttree = NULL;
1464 plan->righttree = NULL;
1465 node->apprelids = rel->relids;
1466
1467 /*
1468 * Compute sort column info, and adjust MergeAppend's tlist as needed.
1469 * Because we pass adjust_tlist_in_place = true, we may ignore the
1470 * function result; it must be the same plan node. However, we then need
1471 * to detect whether any tlist entries were added.
1472 */
1473 (void) prepare_sort_from_pathkeys(plan, pathkeys,
1474 best_path->path.parent->relids,
1475 NULL,
1476 true,
1477 &node->numCols,
1478 &node->sortColIdx,
1479 &node->sortOperators,
1480 &node->collations,
1481 &node->nullsFirst);
1482 tlist_was_changed = (orig_tlist_length != list_length(plan->targetlist));
1483
1484 /*
1485 * Now prepare the child plans. We must apply prepare_sort_from_pathkeys
1486 * even to subplans that don't need an explicit sort, to make sure they
1487 * are returning the same sort key columns the MergeAppend expects.
1488 */
1489 foreach(subpaths, best_path->subpaths)
1490 {
1491 Path *subpath = (Path *) lfirst(subpaths);
1492 Plan *subplan;
1493 int numsortkeys;
1494 AttrNumber *sortColIdx;
1495 Oid *sortOperators;
1496 Oid *collations;
1497 bool *nullsFirst;
1498
1499 /* Build the child plan */
1500 /* Must insist that all children return the same tlist */
1502
1503 /* Compute sort column info, and adjust subplan's tlist as needed */
1504 subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1505 subpath->parent->relids,
1506 node->sortColIdx,
1507 false,
1508 &numsortkeys,
1509 &sortColIdx,
1510 &sortOperators,
1511 &collations,
1512 &nullsFirst);
1513
1514 /*
1515 * Check that we got the same sort key information. We just Assert
1516 * that the sortops match, since those depend only on the pathkeys;
1517 * but it seems like a good idea to check the sort column numbers
1518 * explicitly, to ensure the tlists really do match up.
1519 */
1520 Assert(numsortkeys == node->numCols);
1521 if (memcmp(sortColIdx, node->sortColIdx,
1522 numsortkeys * sizeof(AttrNumber)) != 0)
1523 elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
1524 Assert(memcmp(sortOperators, node->sortOperators,
1525 numsortkeys * sizeof(Oid)) == 0);
1526 Assert(memcmp(collations, node->collations,
1527 numsortkeys * sizeof(Oid)) == 0);
1528 Assert(memcmp(nullsFirst, node->nullsFirst,
1529 numsortkeys * sizeof(bool)) == 0);
1530
1531 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1532 if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
1533 {
1534 Sort *sort = make_sort(subplan, numsortkeys,
1535 sortColIdx, sortOperators,
1536 collations, nullsFirst);
1537
1539 subplan = (Plan *) sort;
1540 }
1541
1542 subplans = lappend(subplans, subplan);
1543 }
1544
1545 /*
1546 * If any quals exist, they may be useful to perform further partition
1547 * pruning during execution. Gather information needed by the executor to
1548 * do partition pruning.
1549 */
1551 {
1552 List *prunequal;
1553
1554 prunequal = extract_actual_clauses(rel->baserestrictinfo, false);
1555
1556 /* We don't currently generate any parameterized MergeAppend paths */
1557 Assert(best_path->path.param_info == NULL);
1558
1559 if (prunequal != NIL)
1560 partpruneinfo = make_partition_pruneinfo(root, rel,
1561 best_path->subpaths,
1562 prunequal);
1563 }
1564
1565 node->mergeplans = subplans;
1566 node->part_prune_info = partpruneinfo;
1567
1568 /*
1569 * If prepare_sort_from_pathkeys added sort columns, but we were told to
1570 * produce either the exact tlist or a narrow tlist, we should get rid of
1571 * the sort columns again. We must inject a projection node to do so.
1572 */
1573 if (tlist_was_changed && (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST)))
1574 {
1575 tlist = list_copy_head(plan->targetlist, orig_tlist_length);
1576 return inject_projection_plan(plan, tlist, plan->parallel_safe);
1577 }
1578 else
1579 return plan;
1580}
Cardinality limit_tuples
Definition: pathnodes.h:1969
struct PartitionPruneInfo * part_prune_info
Definition: plannodes.h:317
Bitmapset * apprelids
Definition: plannodes.h:295
List * mergeplans
Definition: plannodes.h:297

References MergeAppend::apprelids, Assert, RelOptInfo::baserestrictinfo, build_path_tlist(), copy_generic_path_info(), CP_EXACT_TLIST, CP_SMALL_TLIST, create_plan_recurse(), elog, enable_partition_pruning, ERROR, extract_actual_clauses(), inject_projection_plan(), label_sort_with_costsize(), lappend(), lfirst, MergeAppendPath::limit_tuples, list_copy_head(), list_length(), make_partition_pruneinfo(), make_sort(), makeNode, MergeAppend::mergeplans, NIL, MergeAppend::numCols, MergeAppend::part_prune_info, MergeAppendPath::path, Path::pathkeys, pathkeys_contained_in(), MergeAppend::plan, plan, prepare_sort_from_pathkeys(), RelOptInfo::relids, root, sort(), subpath(), and MergeAppendPath::subpaths.

Referenced by create_plan_recurse().

◆ create_mergejoin_plan()

static MergeJoin * create_mergejoin_plan ( PlannerInfo root,
MergePath best_path 
)
static

Definition at line 4443 of file createplan.c.

4445{
4446 MergeJoin *join_plan;
4447 Plan *outer_plan;
4448 Plan *inner_plan;
4449 List *tlist = build_path_tlist(root, &best_path->jpath.path);
4450 List *joinclauses;
4451 List *otherclauses;
4452 List *mergeclauses;
4453 List *outerpathkeys;
4454 List *innerpathkeys;
4455 int nClauses;
4456 Oid *mergefamilies;
4457 Oid *mergecollations;
4458 bool *mergereversals;
4459 bool *mergenullsfirst;
4460 PathKey *opathkey;
4461 EquivalenceClass *opeclass;
4462 int i;
4463 ListCell *lc;
4464 ListCell *lop;
4465 ListCell *lip;
4466 Path *outer_path = best_path->jpath.outerjoinpath;
4467 Path *inner_path = best_path->jpath.innerjoinpath;
4468
4469 /*
4470 * MergeJoin can project, so we don't have to demand exact tlists from the
4471 * inputs. However, if we're intending to sort an input's result, it's
4472 * best to request a small tlist so we aren't sorting more data than
4473 * necessary.
4474 */
4475 outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4476 (best_path->outersortkeys != NIL) ? CP_SMALL_TLIST : 0);
4477
4478 inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4479 (best_path->innersortkeys != NIL) ? CP_SMALL_TLIST : 0);
4480
4481 /* Sort join qual clauses into best execution order */
4482 /* NB: do NOT reorder the mergeclauses */
4483 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4484
4485 /* Get the join qual clauses (in plain expression form) */
4486 /* Any pseudoconstant clauses are ignored here */
4487 if (IS_OUTER_JOIN(best_path->jpath.jointype))
4488 {
4489 extract_actual_join_clauses(joinclauses,
4490 best_path->jpath.path.parent->relids,
4491 &joinclauses, &otherclauses);
4492 }
4493 else
4494 {
4495 /* We can treat all clauses alike for an inner join */
4496 joinclauses = extract_actual_clauses(joinclauses, false);
4497 otherclauses = NIL;
4498 }
4499
4500 /*
4501 * Remove the mergeclauses from the list of join qual clauses, leaving the
4502 * list of quals that must be checked as qpquals.
4503 */
4504 mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
4505 joinclauses = list_difference(joinclauses, mergeclauses);
4506
4507 /*
4508 * Replace any outer-relation variables with nestloop params. There
4509 * should not be any in the mergeclauses.
4510 */
4511 if (best_path->jpath.path.param_info)
4512 {
4513 joinclauses = (List *)
4514 replace_nestloop_params(root, (Node *) joinclauses);
4515 otherclauses = (List *)
4516 replace_nestloop_params(root, (Node *) otherclauses);
4517 }
4518
4519 /*
4520 * Rearrange mergeclauses, if needed, so that the outer variable is always
4521 * on the left; mark the mergeclause restrictinfos with correct
4522 * outer_is_left status.
4523 */
4524 mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
4525 best_path->jpath.outerjoinpath->parent->relids);
4526
4527 /*
4528 * Create explicit sort nodes for the outer and inner paths if necessary.
4529 */
4530 if (best_path->outersortkeys)
4531 {
4532 Relids outer_relids = outer_path->parent->relids;
4533 Plan *sort_plan;
4534 bool use_incremental_sort = false;
4535 int presorted_keys;
4536
4537 /*
4538 * We choose to use incremental sort if it is enabled and there are
4539 * presorted keys; otherwise we use full sort.
4540 */
4542 {
4543 bool is_sorted PG_USED_FOR_ASSERTS_ONLY;
4544
4545 is_sorted = pathkeys_count_contained_in(best_path->outersortkeys,
4546 outer_path->pathkeys,
4547 &presorted_keys);
4548 Assert(!is_sorted);
4549
4550 if (presorted_keys > 0)
4551 use_incremental_sort = true;
4552 }
4553
4554 if (!use_incremental_sort)
4555 {
4556 sort_plan = (Plan *)
4557 make_sort_from_pathkeys(outer_plan,
4558 best_path->outersortkeys,
4559 outer_relids);
4560
4561 label_sort_with_costsize(root, (Sort *) sort_plan, -1.0);
4562 }
4563 else
4564 {
4565 sort_plan = (Plan *)
4567 best_path->outersortkeys,
4568 outer_relids,
4569 presorted_keys);
4570
4572 (IncrementalSort *) sort_plan,
4573 best_path->outersortkeys,
4574 -1.0);
4575 }
4576
4577 outer_plan = sort_plan;
4578 outerpathkeys = best_path->outersortkeys;
4579 }
4580 else
4581 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
4582
4583 if (best_path->innersortkeys)
4584 {
4585 /*
4586 * We do not consider incremental sort for inner path, because
4587 * incremental sort does not support mark/restore.
4588 */
4589
4590 Relids inner_relids = inner_path->parent->relids;
4591 Sort *sort = make_sort_from_pathkeys(inner_plan,
4592 best_path->innersortkeys,
4593 inner_relids);
4594
4596 inner_plan = (Plan *) sort;
4597 innerpathkeys = best_path->innersortkeys;
4598 }
4599 else
4600 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
4601
4602 /*
4603 * If specified, add a materialize node to shield the inner plan from the
4604 * need to handle mark/restore.
4605 */
4606 if (best_path->materialize_inner)
4607 {
4608 Plan *matplan = (Plan *) make_material(inner_plan);
4609
4610 /*
4611 * We assume the materialize will not spill to disk, and therefore
4612 * charge just cpu_operator_cost per tuple. (Keep this estimate in
4613 * sync with final_cost_mergejoin.)
4614 */
4615 copy_plan_costsize(matplan, inner_plan);
4616 matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
4617
4618 inner_plan = matplan;
4619 }
4620
4621 /*
4622 * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
4623 * executor. The information is in the pathkeys for the two inputs, but
4624 * we need to be careful about the possibility of mergeclauses sharing a
4625 * pathkey, as well as the possibility that the inner pathkeys are not in
4626 * an order matching the mergeclauses.
4627 */
4628 nClauses = list_length(mergeclauses);
4629 Assert(nClauses == list_length(best_path->path_mergeclauses));
4630 mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
4631 mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
4632 mergereversals = (bool *) palloc(nClauses * sizeof(bool));
4633 mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
4634
4635 opathkey = NULL;
4636 opeclass = NULL;
4637 lop = list_head(outerpathkeys);
4638 lip = list_head(innerpathkeys);
4639 i = 0;
4640 foreach(lc, best_path->path_mergeclauses)
4641 {
4643 EquivalenceClass *oeclass;
4644 EquivalenceClass *ieclass;
4645 PathKey *ipathkey = NULL;
4646 EquivalenceClass *ipeclass = NULL;
4647 bool first_inner_match = false;
4648
4649 /* fetch outer/inner eclass from mergeclause */
4650 if (rinfo->outer_is_left)
4651 {
4652 oeclass = rinfo->left_ec;
4653 ieclass = rinfo->right_ec;
4654 }
4655 else
4656 {
4657 oeclass = rinfo->right_ec;
4658 ieclass = rinfo->left_ec;
4659 }
4660 Assert(oeclass != NULL);
4661 Assert(ieclass != NULL);
4662
4663 /*
4664 * We must identify the pathkey elements associated with this clause
4665 * by matching the eclasses (which should give a unique match, since
4666 * the pathkey lists should be canonical). In typical cases the merge
4667 * clauses are one-to-one with the pathkeys, but when dealing with
4668 * partially redundant query conditions, things are more complicated.
4669 *
4670 * lop and lip reference the first as-yet-unmatched pathkey elements.
4671 * If they're NULL then all pathkey elements have been matched.
4672 *
4673 * The ordering of the outer pathkeys should match the mergeclauses,
4674 * by construction (see find_mergeclauses_for_outer_pathkeys()). There
4675 * could be more than one mergeclause for the same outer pathkey, but
4676 * no pathkey may be entirely skipped over.
4677 */
4678 if (oeclass != opeclass) /* multiple matches are not interesting */
4679 {
4680 /* doesn't match the current opathkey, so must match the next */
4681 if (lop == NULL)
4682 elog(ERROR, "outer pathkeys do not match mergeclauses");
4683 opathkey = (PathKey *) lfirst(lop);
4684 opeclass = opathkey->pk_eclass;
4685 lop = lnext(outerpathkeys, lop);
4686 if (oeclass != opeclass)
4687 elog(ERROR, "outer pathkeys do not match mergeclauses");
4688 }
4689
4690 /*
4691 * The inner pathkeys likewise should not have skipped-over keys, but
4692 * it's possible for a mergeclause to reference some earlier inner
4693 * pathkey if we had redundant pathkeys. For example we might have
4694 * mergeclauses like "o.a = i.x AND o.b = i.y AND o.c = i.x". The
4695 * implied inner ordering is then "ORDER BY x, y, x", but the pathkey
4696 * mechanism drops the second sort by x as redundant, and this code
4697 * must cope.
4698 *
4699 * It's also possible for the implied inner-rel ordering to be like
4700 * "ORDER BY x, y, x DESC". We still drop the second instance of x as
4701 * redundant; but this means that the sort ordering of a redundant
4702 * inner pathkey should not be considered significant. So we must
4703 * detect whether this is the first clause matching an inner pathkey.
4704 */
4705 if (lip)
4706 {
4707 ipathkey = (PathKey *) lfirst(lip);
4708 ipeclass = ipathkey->pk_eclass;
4709 if (ieclass == ipeclass)
4710 {
4711 /* successful first match to this inner pathkey */
4712 lip = lnext(innerpathkeys, lip);
4713 first_inner_match = true;
4714 }
4715 }
4716 if (!first_inner_match)
4717 {
4718 /* redundant clause ... must match something before lip */
4719 ListCell *l2;
4720
4721 foreach(l2, innerpathkeys)
4722 {
4723 if (l2 == lip)
4724 break;
4725 ipathkey = (PathKey *) lfirst(l2);
4726 ipeclass = ipathkey->pk_eclass;
4727 if (ieclass == ipeclass)
4728 break;
4729 }
4730 if (ieclass != ipeclass)
4731 elog(ERROR, "inner pathkeys do not match mergeclauses");
4732 }
4733
4734 /*
4735 * The pathkeys should always match each other as to opfamily and
4736 * collation (which affect equality), but if we're considering a
4737 * redundant inner pathkey, its sort ordering might not match. In
4738 * such cases we may ignore the inner pathkey's sort ordering and use
4739 * the outer's. (In effect, we're lying to the executor about the
4740 * sort direction of this inner column, but it does not matter since
4741 * the run-time row comparisons would only reach this column when
4742 * there's equality for the earlier column containing the same eclass.
4743 * There could be only one value in this column for the range of inner
4744 * rows having a given value in the earlier column, so it does not
4745 * matter which way we imagine this column to be ordered.) But a
4746 * non-redundant inner pathkey had better match outer's ordering too.
4747 */
4748 if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
4749 opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation)
4750 elog(ERROR, "left and right pathkeys do not match in mergejoin");
4751 if (first_inner_match &&
4752 (opathkey->pk_strategy != ipathkey->pk_strategy ||
4753 opathkey->pk_nulls_first != ipathkey->pk_nulls_first))
4754 elog(ERROR, "left and right pathkeys do not match in mergejoin");
4755
4756 /* OK, save info for executor */
4757 mergefamilies[i] = opathkey->pk_opfamily;
4758 mergecollations[i] = opathkey->pk_eclass->ec_collation;
4759 mergereversals[i] = (opathkey->pk_strategy == BTGreaterStrategyNumber ? true : false);
4760 mergenullsfirst[i] = opathkey->pk_nulls_first;
4761 i++;
4762 }
4763
4764 /*
4765 * Note: it is not an error if we have additional pathkey elements (i.e.,
4766 * lop or lip isn't NULL here). The input paths might be better-sorted
4767 * than we need for the current mergejoin.
4768 */
4769
4770 /*
4771 * Now we can build the mergejoin node.
4772 */
4773 join_plan = make_mergejoin(tlist,
4774 joinclauses,
4775 otherclauses,
4776 mergeclauses,
4777 mergefamilies,
4778 mergecollations,
4779 mergereversals,
4780 mergenullsfirst,
4781 outer_plan,
4782 inner_plan,
4783 best_path->jpath.jointype,
4784 best_path->jpath.inner_unique,
4785 best_path->skip_mark_restore);
4786
4787 /* Costs of sort and material steps are included in path cost already */
4788 copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4789
4790 return join_plan;
4791}
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:204
double cpu_operator_cost
Definition: costsize.c:134
bool enable_incremental_sort
Definition: costsize.c:151
static MergeJoin * make_mergejoin(List *tlist, List *joinclauses, List *otherclauses, List *mergeclauses, Oid *mergefamilies, Oid *mergecollations, bool *mergereversals, bool *mergenullsfirst, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique, bool skip_mark_restore)
Definition: createplan.c:6103
static Sort * make_sort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids)
Definition: createplan.c:6423
static void label_incrementalsort_with_costsize(PlannerInfo *root, IncrementalSort *plan, List *pathkeys, double limit_tuples)
Definition: createplan.c:5522
return true
Definition: isn.c:125
bool pathkeys_count_contained_in(List *keys1, List *keys2, int *n_common)
Definition: pathkeys.c:558
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
Join join
Definition: plannodes.h:838
List * outersortkeys
Definition: pathnodes.h:2146
bool skip_mark_restore
Definition: pathnodes.h:2148
List * innersortkeys
Definition: pathnodes.h:2147
JoinPath jpath
Definition: pathnodes.h:2144
bool materialize_inner
Definition: pathnodes.h:2149
List * path_mergeclauses
Definition: pathnodes.h:2145
bool pk_nulls_first
Definition: pathnodes.h:1482

References Assert, BTGreaterStrategyNumber, build_path_tlist(), copy_generic_path_info(), copy_plan_costsize(), CP_SMALL_TLIST, cpu_operator_cost, create_plan_recurse(), elog, enable_incremental_sort, ERROR, extract_actual_clauses(), extract_actual_join_clauses(), get_actual_clauses(), get_switched_clauses(), i, JoinPath::inner_unique, JoinPath::innerjoinpath, MergePath::innersortkeys, IS_OUTER_JOIN, MergeJoin::join, JoinPath::joinrestrictinfo, JoinPath::jointype, MergePath::jpath, label_incrementalsort_with_costsize(), label_sort_with_costsize(), lfirst, lfirst_node, list_difference(), list_head(), list_length(), lnext(), make_incrementalsort_from_pathkeys(), make_material(), make_mergejoin(), make_sort_from_pathkeys(), MergePath::materialize_inner, NIL, order_qual_clauses(), JoinPath::outerjoinpath, MergePath::outersortkeys, palloc(), MergePath::path_mergeclauses, Path::pathkeys, pathkeys_count_contained_in(), PG_USED_FOR_ASSERTS_ONLY, PathKey::pk_nulls_first, PathKey::pk_opfamily, PathKey::pk_strategy, Plan::plan_rows, replace_nestloop_params(), root, MergePath::skip_mark_restore, sort(), Plan::total_cost, and true.

Referenced by create_join_plan().

◆ create_minmaxagg_plan()

static Result * create_minmaxagg_plan ( PlannerInfo root,
MinMaxAggPath best_path 
)
static

Definition at line 2549 of file createplan.c.

2550{
2551 Result *plan;
2552 List *tlist;
2553 ListCell *lc;
2554
2555 /* Prepare an InitPlan for each aggregate's subquery. */
2556 foreach(lc, best_path->mmaggregates)
2557 {
2558 MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
2559 PlannerInfo *subroot = mminfo->subroot;
2560 Query *subparse = subroot->parse;
2561 Plan *plan;
2562
2563 /*
2564 * Generate the plan for the subquery. We already have a Path, but we
2565 * have to convert it to a Plan and attach a LIMIT node above it.
2566 * Since we are entering a different planner context (subroot),
2567 * recurse to create_plan not create_plan_recurse.
2568 */
2569 plan = create_plan(subroot, mminfo->path);
2570
2571 plan = (Plan *) make_limit(plan,
2572 subparse->limitOffset,
2573 subparse->limitCount,
2574 subparse->limitOption,
2575 0, NULL, NULL, NULL);
2576
2577 /* Must apply correct cost/width data to Limit node */
2578 plan->disabled_nodes = mminfo->path->disabled_nodes;
2579 plan->startup_cost = mminfo->path->startup_cost;
2580 plan->total_cost = mminfo->pathcost;
2581 plan->plan_rows = 1;
2582 plan->plan_width = mminfo->path->pathtarget->width;
2583 plan->parallel_aware = false;
2584 plan->parallel_safe = mminfo->path->parallel_safe;
2585
2586 /* Convert the plan into an InitPlan in the outer query. */
2587 SS_make_initplan_from_plan(root, subroot, plan, mminfo->param);
2588 }
2589
2590 /* Generate the output plan --- basically just a Result */
2591 tlist = build_path_tlist(root, &best_path->path);
2592
2593 plan = make_result(tlist, (Node *) best_path->quals, NULL);
2594
2595 copy_generic_path_info(&plan->plan, (Path *) best_path);
2596
2597 /*
2598 * During setrefs.c, we'll need to replace references to the Agg nodes
2599 * with InitPlan output params. (We can't just do that locally in the
2600 * MinMaxAgg node, because path nodes above here may have Agg references
2601 * as well.) Save the mmaggregates list to tell setrefs.c to do that.
2602 */
2603 Assert(root->minmax_aggs == NIL);
2604 root->minmax_aggs = best_path->mmaggregates;
2605
2606 return plan;
2607}
Plan * create_plan(PlannerInfo *root, Path *best_path)
Definition: createplan.c:340
Param * param
Definition: pathnodes.h:3144
List * quals
Definition: pathnodes.h:2322
List * mmaggregates
Definition: pathnodes.h:2321
Node * limitCount
Definition: parsenodes.h:225
Node * limitOffset
Definition: parsenodes.h:224
LimitOption limitOption
Definition: parsenodes.h:226
void SS_make_initplan_from_plan(PlannerInfo *root, PlannerInfo *subroot, Plan *plan, Param *prm)
Definition: subselect.c:3049

References Assert, build_path_tlist(), copy_generic_path_info(), create_plan(), Path::disabled_nodes, lfirst, Query::limitCount, Query::limitOffset, Query::limitOption, make_limit(), make_result(), MinMaxAggPath::mmaggregates, NIL, Path::parallel_safe, MinMaxAggInfo::param, PlannerInfo::parse, MinMaxAggPath::path, MinMaxAggInfo::path, MinMaxAggInfo::pathcost, plan, MinMaxAggPath::quals, root, SS_make_initplan_from_plan(), and Path::startup_cost.

Referenced by create_plan_recurse().

◆ create_modifytable_plan()

static ModifyTable * create_modifytable_plan ( PlannerInfo root,
ModifyTablePath best_path 
)
static

Definition at line 2818 of file createplan.c.

2819{
2821 Path *subpath = best_path->subpath;
2822 Plan *subplan;
2823
2824 /* Subplan must produce exactly the specified tlist */
2826
2827 /* Transfer resname/resjunk labeling, too, to keep executor happy */
2828 apply_tlist_labeling(subplan->targetlist, root->processed_tlist);
2829
2831 subplan,
2832 best_path->operation,
2833 best_path->canSetTag,
2834 best_path->nominalRelation,
2835 best_path->rootRelation,
2836 best_path->partColsUpdated,
2837 best_path->resultRelations,
2838 best_path->updateColnosLists,
2839 best_path->withCheckOptionLists,
2840 best_path->returningLists,
2841 best_path->rowMarks,
2842 best_path->onconflict,
2843 best_path->mergeActionLists,
2844 best_path->mergeJoinConditions,
2845 best_path->epqParam);
2846
2847 copy_generic_path_info(&plan->plan, &best_path->path);
2848
2849 return plan;
2850}
static ModifyTable * make_modifytable(PlannerInfo *root, Plan *subplan, CmdType operation, bool canSetTag, Index nominalRelation, Index rootRelation, bool partColsUpdated, List *resultRelations, List *updateColnosLists, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, List *mergeActionLists, List *mergeJoinConditions, int epqParam)
Definition: createplan.c:7112
bool partColsUpdated
Definition: pathnodes.h:2392
List * returningLists
Definition: pathnodes.h:2396
List * resultRelations
Definition: pathnodes.h:2393
List * withCheckOptionLists
Definition: pathnodes.h:2395
List * mergeJoinConditions
Definition: pathnodes.h:2402
List * updateColnosLists
Definition: pathnodes.h:2394
OnConflictExpr * onconflict
Definition: pathnodes.h:2398
CmdType operation
Definition: pathnodes.h:2388
Index rootRelation
Definition: pathnodes.h:2391
Index nominalRelation
Definition: pathnodes.h:2390
List * mergeActionLists
Definition: pathnodes.h:2400
void apply_tlist_labeling(List *dest_tlist, List *src_tlist)
Definition: tlist.c:318

References apply_tlist_labeling(), ModifyTablePath::canSetTag, copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), ModifyTablePath::epqParam, make_modifytable(), ModifyTablePath::mergeActionLists, ModifyTablePath::mergeJoinConditions, ModifyTablePath::nominalRelation, ModifyTablePath::onconflict, ModifyTablePath::operation, ModifyTablePath::partColsUpdated, ModifyTablePath::path, plan, ModifyTablePath::resultRelations, ModifyTablePath::returningLists, root, ModifyTablePath::rootRelation, ModifyTablePath::rowMarks, subpath(), ModifyTablePath::subpath, Plan::targetlist, ModifyTablePath::updateColnosLists, and ModifyTablePath::withCheckOptionLists.

Referenced by create_plan_recurse().

◆ create_namedtuplestorescan_plan()

static NamedTuplestoreScan * create_namedtuplestorescan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3989 of file createplan.c.

3991{
3992 NamedTuplestoreScan *scan_plan;
3993 Index scan_relid = best_path->parent->relid;
3994 RangeTblEntry *rte;
3995
3996 Assert(scan_relid > 0);
3997 rte = planner_rt_fetch(scan_relid, root);
3999
4000 /* Sort clauses into best execution order */
4001 scan_clauses = order_qual_clauses(root, scan_clauses);
4002
4003 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
4004 scan_clauses = extract_actual_clauses(scan_clauses, false);
4005
4006 /* Replace any outer-relation variables with nestloop params */
4007 if (best_path->param_info)
4008 {
4009 scan_clauses = (List *)
4010 replace_nestloop_params(root, (Node *) scan_clauses);
4011 }
4012
4013 scan_plan = make_namedtuplestorescan(tlist, scan_clauses, scan_relid,
4014 rte->enrname);
4015
4016 copy_generic_path_info(&scan_plan->scan.plan, best_path);
4017
4018 return scan_plan;
4019}
static NamedTuplestoreScan * make_namedtuplestorescan(List *qptlist, List *qpqual, Index scanrelid, char *enrname)
Definition: createplan.c:5859
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1033
char * enrname
Definition: parsenodes.h:1240

References Assert, copy_generic_path_info(), RangeTblEntry::enrname, extract_actual_clauses(), make_namedtuplestorescan(), order_qual_clauses(), planner_rt_fetch, replace_nestloop_params(), root, RTE_NAMEDTUPLESTORE, RangeTblEntry::rtekind, and NamedTuplestoreScan::scan.

Referenced by create_scan_plan().

◆ create_nestloop_plan()

static NestLoop * create_nestloop_plan ( PlannerInfo root,
NestPath best_path 
)
static

Definition at line 4351 of file createplan.c.

4353{
4354 NestLoop *join_plan;
4355 Plan *outer_plan;
4356 Plan *inner_plan;
4357 List *tlist = build_path_tlist(root, &best_path->jpath.path);
4358 List *joinrestrictclauses = best_path->jpath.joinrestrictinfo;
4359 List *joinclauses;
4360 List *otherclauses;
4361 Relids outerrelids;
4362 List *nestParams;
4363 Relids saveOuterRels = root->curOuterRels;
4364
4365 /*
4366 * If the inner path is parameterized by the topmost parent of the outer
4367 * rel rather than the outer rel itself, fix that. (Nothing happens here
4368 * if it is not so parameterized.)
4369 */
4370 best_path->jpath.innerjoinpath =
4372 best_path->jpath.innerjoinpath,
4373 best_path->jpath.outerjoinpath->parent);
4374
4375 /*
4376 * Failure here probably means that reparameterize_path_by_child() is not
4377 * in sync with path_is_reparameterizable_by_child().
4378 */
4379 Assert(best_path->jpath.innerjoinpath != NULL);
4380
4381 /* NestLoop can project, so no need to be picky about child tlists */
4382 outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath, 0);
4383
4384 /* For a nestloop, include outer relids in curOuterRels for inner side */
4385 root->curOuterRels = bms_union(root->curOuterRels,
4386 best_path->jpath.outerjoinpath->parent->relids);
4387
4388 inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath, 0);
4389
4390 /* Restore curOuterRels */
4391 bms_free(root->curOuterRels);
4392 root->curOuterRels = saveOuterRels;
4393
4394 /* Sort join qual clauses into best execution order */
4395 joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
4396
4397 /* Get the join qual clauses (in plain expression form) */
4398 /* Any pseudoconstant clauses are ignored here */
4399 if (IS_OUTER_JOIN(best_path->jpath.jointype))
4400 {
4401 extract_actual_join_clauses(joinrestrictclauses,
4402 best_path->jpath.path.parent->relids,
4403 &joinclauses, &otherclauses);
4404 }
4405 else
4406 {
4407 /* We can treat all clauses alike for an inner join */
4408 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
4409 otherclauses = NIL;
4410 }
4411
4412 /* Replace any outer-relation variables with nestloop params */
4413 if (best_path->jpath.path.param_info)
4414 {
4415 joinclauses = (List *)
4416 replace_nestloop_params(root, (Node *) joinclauses);
4417 otherclauses = (List *)
4418 replace_nestloop_params(root, (Node *) otherclauses);
4419 }
4420
4421 /*
4422 * Identify any nestloop parameters that should be supplied by this join
4423 * node, and remove them from root->curOuterParams.
4424 */
4425 outerrelids = best_path->jpath.outerjoinpath->parent->relids;
4426 nestParams = identify_current_nestloop_params(root, outerrelids);
4427
4428 join_plan = make_nestloop(tlist,
4429 joinclauses,
4430 otherclauses,
4431 nestParams,
4432 outer_plan,
4433 inner_plan,
4434 best_path->jpath.jointype,
4435 best_path->jpath.inner_unique);
4436
4437 copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4438
4439 return join_plan;
4440}
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:251
static NestLoop * make_nestloop(List *tlist, List *joinclauses, List *otherclauses, List *nestParams, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
Definition: createplan.c:6024
List * identify_current_nestloop_params(PlannerInfo *root, Relids leftrelids)
Definition: paramassign.c:629
Path * reparameterize_path_by_child(PlannerInfo *root, Path *path, RelOptInfo *child_rel)
Definition: pathnode.c:4269
Join join
Definition: plannodes.h:812
JoinPath jpath
Definition: pathnodes.h:2104

References Assert, bms_free(), bms_union(), build_path_tlist(), copy_generic_path_info(), create_plan_recurse(), extract_actual_clauses(), extract_actual_join_clauses(), identify_current_nestloop_params(), JoinPath::inner_unique, JoinPath::innerjoinpath, IS_OUTER_JOIN, NestLoop::join, JoinPath::joinrestrictinfo, JoinPath::jointype, NestPath::jpath, make_nestloop(), NIL, order_qual_clauses(), JoinPath::outerjoinpath, reparameterize_path_by_child(), replace_nestloop_params(), and root.

Referenced by create_join_plan().

◆ create_plan()

Plan * create_plan ( PlannerInfo root,
Path best_path 
)

Definition at line 340 of file createplan.c.

341{
342 Plan *plan;
343
344 /* plan_params should not be in use in current query level */
345 Assert(root->plan_params == NIL);
346
347 /* Initialize this module's workspace in PlannerInfo */
348 root->curOuterRels = NULL;
349 root->curOuterParams = NIL;
350
351 /* Recursively process the path tree, demanding the correct tlist result */
353
354 /*
355 * Make sure the topmost plan node's targetlist exposes the original
356 * column names and other decorative info. Targetlists generated within
357 * the planner don't bother with that stuff, but we must have it on the
358 * top-level tlist seen at execution time. However, ModifyTable plan
359 * nodes don't have a tlist matching the querytree targetlist.
360 */
361 if (!IsA(plan, ModifyTable))
362 apply_tlist_labeling(plan->targetlist, root->processed_tlist);
363
364 /*
365 * Attach any initPlans created in this query level to the topmost plan
366 * node. (In principle the initplans could go in any plan node at or
367 * above where they're referenced, but there seems no reason to put them
368 * any lower than the topmost node for the query level. Also, see
369 * comments for SS_finalize_plan before you try to change this.)
370 */
372
373 /* Check we successfully assigned all NestLoopParams to plan nodes */
374 if (root->curOuterParams != NIL)
375 elog(ERROR, "failed to assign all NestLoopParams to plan nodes");
376
377 /*
378 * Reset plan_params to ensure param IDs used for nestloop params are not
379 * re-used later
380 */
381 root->plan_params = NIL;
382
383 return plan;
384}
void SS_attach_initplans(PlannerInfo *root, Plan *plan)
Definition: subselect.c:2273

References apply_tlist_labeling(), Assert, CP_EXACT_TLIST, create_plan_recurse(), elog, ERROR, IsA, NIL, plan, root, and SS_attach_initplans().

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

◆ create_plan_recurse()

static Plan * create_plan_recurse ( PlannerInfo root,
Path best_path,
int  flags 
)
static

Definition at line 391 of file createplan.c.

392{
393 Plan *plan;
394
395 /* Guard against stack overflow due to overly complex plans */
397
398 switch (best_path->pathtype)
399 {
400 case T_SeqScan:
401 case T_SampleScan:
402 case T_IndexScan:
403 case T_IndexOnlyScan:
404 case T_BitmapHeapScan:
405 case T_TidScan:
406 case T_TidRangeScan:
407 case T_SubqueryScan:
408 case T_FunctionScan:
409 case T_TableFuncScan:
410 case T_ValuesScan:
411 case T_CteScan:
412 case T_WorkTableScan:
413 case T_NamedTuplestoreScan:
414 case T_ForeignScan:
415 case T_CustomScan:
416 plan = create_scan_plan(root, best_path, flags);
417 break;
418 case T_HashJoin:
419 case T_MergeJoin:
420 case T_NestLoop:
422 (JoinPath *) best_path);
423 break;
424 case T_Append:
426 (AppendPath *) best_path,
427 flags);
428 break;
429 case T_MergeAppend:
431 (MergeAppendPath *) best_path,
432 flags);
433 break;
434 case T_Result:
435 if (IsA(best_path, ProjectionPath))
436 {
438 (ProjectionPath *) best_path,
439 flags);
440 }
441 else if (IsA(best_path, MinMaxAggPath))
442 {
444 (MinMaxAggPath *) best_path);
445 }
446 else if (IsA(best_path, GroupResultPath))
447 {
449 (GroupResultPath *) best_path);
450 }
451 else
452 {
453 /* Simple RTE_RESULT base relation */
454 Assert(IsA(best_path, Path));
455 plan = create_scan_plan(root, best_path, flags);
456 }
457 break;
458 case T_ProjectSet:
460 (ProjectSetPath *) best_path);
461 break;
462 case T_Material:
464 (MaterialPath *) best_path,
465 flags);
466 break;
467 case T_Memoize:
469 (MemoizePath *) best_path,
470 flags);
471 break;
472 case T_Unique:
473 if (IsA(best_path, UpperUniquePath))
474 {
476 (UpperUniquePath *) best_path,
477 flags);
478 }
479 else
480 {
481 Assert(IsA(best_path, UniquePath));
483 (UniquePath *) best_path,
484 flags);
485 }
486 break;
487 case T_Gather:
489 (GatherPath *) best_path);
490 break;
491 case T_Sort:
493 (SortPath *) best_path,
494 flags);
495 break;
496 case T_IncrementalSort:
498 (IncrementalSortPath *) best_path,
499 flags);
500 break;
501 case T_Group:
503 (GroupPath *) best_path);
504 break;
505 case T_Agg:
506 if (IsA(best_path, GroupingSetsPath))
508 (GroupingSetsPath *) best_path);
509 else
510 {
511 Assert(IsA(best_path, AggPath));
513 (AggPath *) best_path);
514 }
515 break;
516 case T_WindowAgg:
518 (WindowAggPath *) best_path);
519 break;
520 case T_SetOp:
522 (SetOpPath *) best_path,
523 flags);
524 break;
525 case T_RecursiveUnion:
527 (RecursiveUnionPath *) best_path);
528 break;
529 case T_LockRows:
531 (LockRowsPath *) best_path,
532 flags);
533 break;
534 case T_ModifyTable:
536 (ModifyTablePath *) best_path);
537 break;
538 case T_Limit:
540 (LimitPath *) best_path,
541 flags);
542 break;
543 case T_GatherMerge:
545 (GatherMergePath *) best_path);
546 break;
547 default:
548 elog(ERROR, "unrecognized node type: %d",
549 (int) best_path->pathtype);
550 plan = NULL; /* keep compiler quiet */
551 break;
552 }
553
554 return plan;
555}
static Plan * create_join_plan(PlannerInfo *root, JoinPath *best_path)
Definition: createplan.c:1084
static Plan * create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path, int flags)
Definition: createplan.c:1440
static GatherMerge * create_gather_merge_plan(PlannerInfo *root, GatherMergePath *best_path)
Definition: createplan.c:1961
static Plan * create_append_plan(PlannerInfo *root, AppendPath *best_path, int flags)
Definition: createplan.c:1219
static Result * create_group_result_plan(PlannerInfo *root, GroupResultPath *best_path)
Definition: createplan.c:1590
static Limit * create_limit_plan(PlannerInfo *root, LimitPath *best_path, int flags)
Definition: createplan.c:2859
static Agg * create_agg_plan(PlannerInfo *root, AggPath *best_path)
Definition: createplan.c:2307
static SetOp * create_setop_plan(PlannerInfo *root, SetOpPath *best_path, int flags)
Definition: createplan.c:2719
static Sort * create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags)
Definition: createplan.c:2179
static Unique * create_upper_unique_plan(PlannerInfo *root, UpperUniquePath *best_path, int flags)
Definition: createplan.c:2279
static Gather * create_gather_plan(PlannerInfo *root, GatherPath *best_path)
Definition: createplan.c:1923
static ProjectSet * create_project_set_plan(PlannerInfo *root, ProjectSetPath *best_path)
Definition: createplan.c:1615
static Group * create_group_plan(PlannerInfo *root, GroupPath *best_path)
Definition: createplan.c:2240
static ModifyTable * create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path)
Definition: createplan.c:2818
static Result * create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path)
Definition: createplan.c:2549
static LockRows * create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path, int flags)
Definition: createplan.c:2795
static Material * create_material_plan(PlannerInfo *root, MaterialPath *best_path, int flags)
Definition: createplan.c:1641
static Plan * create_scan_plan(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:562
static IncrementalSort * create_incrementalsort_plan(PlannerInfo *root, IncrementalSortPath *best_path, int flags)
Definition: createplan.c:2213
static Plan * create_projection_plan(PlannerInfo *root, ProjectionPath *best_path, int flags)
Definition: createplan.c:2017
static Memoize * create_memoize_plan(PlannerInfo *root, MemoizePath *best_path, int flags)
Definition: createplan.c:1669
static WindowAgg * create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
Definition: createplan.c:2616
static Plan * create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path)
Definition: createplan.c:2391
static RecursiveUnion * create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path)
Definition: createplan.c:2759
static Plan * create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
Definition: createplan.c:1723
void check_stack_depth(void)
Definition: stack_depth.c:95
NodeTag pathtype
Definition: pathnodes.h:1637

References Assert, check_stack_depth(), create_agg_plan(), create_append_plan(), create_gather_merge_plan(), create_gather_plan(), create_group_plan(), create_group_result_plan(), create_groupingsets_plan(), create_incrementalsort_plan(), create_join_plan(), create_limit_plan(), create_lockrows_plan(), create_material_plan(), create_memoize_plan(), create_merge_append_plan(), create_minmaxagg_plan(), create_modifytable_plan(), create_project_set_plan(), create_projection_plan(), create_recursiveunion_plan(), create_scan_plan(), create_setop_plan(), create_sort_plan(), create_unique_plan(), create_upper_unique_plan(), create_windowagg_plan(), elog, ERROR, IsA, Path::pathtype, plan, and root.

Referenced by create_agg_plan(), create_append_plan(), create_customscan_plan(), create_foreignscan_plan(), create_gather_merge_plan(), create_gather_plan(), create_group_plan(), create_groupingsets_plan(), create_hashjoin_plan(), create_incrementalsort_plan(), create_limit_plan(), create_lockrows_plan(), create_material_plan(), create_memoize_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().

◆ create_project_set_plan()

static ProjectSet * create_project_set_plan ( PlannerInfo root,
ProjectSetPath best_path 
)
static

Definition at line 1615 of file createplan.c.

1616{
1618 Plan *subplan;
1619 List *tlist;
1620
1621 /* Since we intend to project, we don't need to constrain child tlist */
1622 subplan = create_plan_recurse(root, best_path->subpath, 0);
1623
1624 tlist = build_path_tlist(root, &best_path->path);
1625
1626 plan = make_project_set(tlist, subplan);
1627
1628 copy_generic_path_info(&plan->plan, (Path *) best_path);
1629
1630 return plan;
1631}
static ProjectSet * make_project_set(List *tlist, Plan *subplan)
Definition: createplan.c:7093
Path * subpath
Definition: pathnodes.h:2198

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

Referenced by create_plan_recurse().

◆ create_projection_plan()

static Plan * create_projection_plan ( PlannerInfo root,
ProjectionPath best_path,
int  flags 
)
static

Definition at line 2017 of file createplan.c.

2018{
2019 Plan *plan;
2020 Plan *subplan;
2021 List *tlist;
2022 bool needs_result_node = false;
2023
2024 /*
2025 * Convert our subpath to a Plan and determine whether we need a Result
2026 * node.
2027 *
2028 * In most cases where we don't need to project, create_projection_path
2029 * will have set dummypp, but not always. First, some createplan.c
2030 * routines change the tlists of their nodes. (An example is that
2031 * create_merge_append_plan might add resjunk sort columns to a
2032 * MergeAppend.) Second, create_projection_path has no way of knowing
2033 * what path node will be placed on top of the projection path and
2034 * therefore can't predict whether it will require an exact tlist. For
2035 * both of these reasons, we have to recheck here.
2036 */
2037 if (use_physical_tlist(root, &best_path->path, flags))
2038 {
2039 /*
2040 * Our caller doesn't really care what tlist we return, so we don't
2041 * actually need to project. However, we may still need to ensure
2042 * proper sortgroupref labels, if the caller cares about those.
2043 */
2044 subplan = create_plan_recurse(root, best_path->subpath, 0);
2045 tlist = subplan->targetlist;
2046 if (flags & CP_LABEL_TLIST)
2048 best_path->path.pathtarget);
2049 }
2050 else if (is_projection_capable_path(best_path->subpath))
2051 {
2052 /*
2053 * Our caller requires that we return the exact tlist, but no separate
2054 * result node is needed because the subpath is projection-capable.
2055 * Tell create_plan_recurse that we're going to ignore the tlist it
2056 * produces.
2057 */
2058 subplan = create_plan_recurse(root, best_path->subpath,
2061 tlist = build_path_tlist(root, &best_path->path);
2062 }
2063 else
2064 {
2065 /*
2066 * It looks like we need a result node, unless by good fortune the
2067 * requested tlist is exactly the one the child wants to produce.
2068 */
2069 subplan = create_plan_recurse(root, best_path->subpath, 0);
2070 tlist = build_path_tlist(root, &best_path->path);
2071 needs_result_node = !tlist_same_exprs(tlist, subplan->targetlist);
2072 }
2073
2074 /*
2075 * If we make a different decision about whether to include a Result node
2076 * than create_projection_path did, we'll have made slightly wrong cost
2077 * estimates; but label the plan with the cost estimates we actually used,
2078 * not "corrected" ones. (XXX this could be cleaned up if we moved more
2079 * of the sortcolumn setup logic into Path creation, but that would add
2080 * expense to creating Paths we might end up not using.)
2081 */
2082 if (!needs_result_node)
2083 {
2084 /* Don't need a separate Result, just assign tlist to subplan */
2085 plan = subplan;
2086 plan->targetlist = tlist;
2087
2088 /* Label plan with the estimated costs we actually used */
2089 plan->startup_cost = best_path->path.startup_cost;
2090 plan->total_cost = best_path->path.total_cost;
2091 plan->plan_rows = best_path->path.rows;
2092 plan->plan_width = best_path->path.pathtarget->width;
2093 plan->parallel_safe = best_path->path.parallel_safe;
2094 /* ... but don't change subplan's parallel_aware flag */
2095 }
2096 else
2097 {
2098 /* We need a Result node */
2099 plan = (Plan *) make_result(tlist, NULL, subplan);
2100
2101 copy_generic_path_info(plan, (Path *) best_path);
2102 }
2103
2104 return plan;
2105}
static bool use_physical_tlist(PlannerInfo *root, Path *path, int flags)
Definition: createplan.c:868
bool is_projection_capable_path(Path *path)
Definition: createplan.c:7319
#define CP_IGNORE_TLIST
Definition: createplan.c:73
Path * subpath
Definition: pathnodes.h:2186
void apply_pathtarget_labeling_to_tlist(List *tlist, PathTarget *target)
Definition: tlist.c:774

References apply_pathtarget_labeling_to_tlist(), Assert, build_path_tlist(), copy_generic_path_info(), CP_IGNORE_TLIST, CP_LABEL_TLIST, create_plan_recurse(), is_projection_capable_path(), is_projection_capable_plan(), make_result(), Path::parallel_safe, ProjectionPath::path, plan, root, Path::rows, Path::startup_cost, ProjectionPath::subpath, Plan::targetlist, tlist_same_exprs(), Path::total_cost, and use_physical_tlist().

Referenced by create_plan_recurse().

◆ create_recursiveunion_plan()

static RecursiveUnion * create_recursiveunion_plan ( PlannerInfo root,
RecursiveUnionPath best_path 
)
static

Definition at line 2759 of file createplan.c.

2760{
2762 Plan *leftplan;
2763 Plan *rightplan;
2764 List *tlist;
2765 long numGroups;
2766
2767 /* Need both children to produce same tlist, so force it */
2768 leftplan = create_plan_recurse(root, best_path->leftpath, CP_EXACT_TLIST);
2769 rightplan = create_plan_recurse(root, best_path->rightpath, CP_EXACT_TLIST);
2770
2771 tlist = build_path_tlist(root, &best_path->path);
2772
2773 /* Convert numGroups to long int --- but 'ware overflow! */
2774 numGroups = clamp_cardinality_to_long(best_path->numGroups);
2775
2776 plan = make_recursive_union(tlist,
2777 leftplan,
2778 rightplan,
2779 best_path->wtParam,
2780 best_path->distinctList,
2781 numGroups);
2782
2783 copy_generic_path_info(&plan->plan, (Path *) best_path);
2784
2785 return plan;
2786}
long clamp_cardinality_to_long(Cardinality x)
Definition: costsize.c:265
static RecursiveUnion * make_recursive_union(List *tlist, Plan *lefttree, Plan *righttree, int wtParam, List *distinctList, long numGroups)
Definition: createplan.c:5938
Cardinality numGroups
Definition: pathnodes.h:2363

References build_path_tlist(), clamp_cardinality_to_long(), copy_generic_path_info(), CP_EXACT_TLIST, create_plan_recurse(), RecursiveUnionPath::distinctList, RecursiveUnionPath::leftpath, make_recursive_union(), RecursiveUnionPath::numGroups, RecursiveUnionPath::path, plan, RecursiveUnionPath::rightpath, root, and RecursiveUnionPath::wtParam.

Referenced by create_plan_recurse().

◆ create_resultscan_plan()

static Result * create_resultscan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 4028 of file createplan.c.

4030{
4031 Result *scan_plan;
4032 Index scan_relid = best_path->parent->relid;
4034
4035 Assert(scan_relid > 0);
4036 rte = planner_rt_fetch(scan_relid, root);
4037 Assert(rte->rtekind == RTE_RESULT);
4038
4039 /* Sort clauses into best execution order */
4040 scan_clauses = order_qual_clauses(root, scan_clauses);
4041
4042 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
4043 scan_clauses = extract_actual_clauses(scan_clauses, false);
4044
4045 /* Replace any outer-relation variables with nestloop params */
4046 if (best_path->param_info)
4047 {
4048 scan_clauses = (List *)
4049 replace_nestloop_params(root, (Node *) scan_clauses);
4050 }
4051
4052 scan_plan = make_result(tlist, (Node *) scan_clauses, NULL);
4053
4054 copy_generic_path_info(&scan_plan->plan, best_path);
4055
4056 return scan_plan;
4057}
@ RTE_RESULT
Definition: parsenodes.h:1034

References Assert, copy_generic_path_info(), extract_actual_clauses(), make_result(), order_qual_clauses(), PG_USED_FOR_ASSERTS_ONLY, Result::plan, planner_rt_fetch, replace_nestloop_params(), root, and RTE_RESULT.

Referenced by create_scan_plan().

◆ create_samplescan_plan()

static SampleScan * create_samplescan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 2958 of file createplan.c.

2960{
2961 SampleScan *scan_plan;
2962 Index scan_relid = best_path->parent->relid;
2963 RangeTblEntry *rte;
2964 TableSampleClause *tsc;
2965
2966 /* it should be a base rel with a tablesample clause... */
2967 Assert(scan_relid > 0);
2968 rte = planner_rt_fetch(scan_relid, root);
2969 Assert(rte->rtekind == RTE_RELATION);
2970 tsc = rte->tablesample;
2971 Assert(tsc != NULL);
2972
2973 /* Sort clauses into best execution order */
2974 scan_clauses = order_qual_clauses(root, scan_clauses);
2975
2976 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2977 scan_clauses = extract_actual_clauses(scan_clauses, false);
2978
2979 /* Replace any outer-relation variables with nestloop params */
2980 if (best_path->param_info)
2981 {
2982 scan_clauses = (List *)
2983 replace_nestloop_params(root, (Node *) scan_clauses);
2984 tsc = (TableSampleClause *)
2986 }
2987
2988 scan_plan = make_samplescan(tlist,
2989 scan_clauses,
2990 scan_relid,
2991 tsc);
2992
2993 copy_generic_path_info(&scan_plan->scan.plan, best_path);
2994
2995 return scan_plan;
2996}
static SampleScan * make_samplescan(List *qptlist, List *qpqual, Index scanrelid, TableSampleClause *tsc)
Definition: createplan.c:5601
struct TableSampleClause * tablesample
Definition: parsenodes.h:1107
Scan scan
Definition: plannodes.h:410

References Assert, copy_generic_path_info(), extract_actual_clauses(), make_samplescan(), order_qual_clauses(), planner_rt_fetch, replace_nestloop_params(), root, RTE_RELATION, RangeTblEntry::rtekind, SampleScan::scan, and RangeTblEntry::tablesample.

Referenced by create_scan_plan().

◆ create_scan_plan()

static Plan * create_scan_plan ( PlannerInfo root,
Path best_path,
int  flags 
)
static

Definition at line 562 of file createplan.c.

563{
564 RelOptInfo *rel = best_path->parent;
565 List *scan_clauses;
566 List *gating_clauses;
567 List *tlist;
568 Plan *plan;
569
570 /*
571 * Extract the relevant restriction clauses from the parent relation. The
572 * executor must apply all these restrictions during the scan, except for
573 * pseudoconstants which we'll take care of below.
574 *
575 * If this is a plain indexscan or index-only scan, we need not consider
576 * restriction clauses that are implied by the index's predicate, so use
577 * indrestrictinfo not baserestrictinfo. Note that we can't do that for
578 * bitmap indexscans, since there's not necessarily a single index
579 * involved; but it doesn't matter since create_bitmap_scan_plan() will be
580 * able to get rid of such clauses anyway via predicate proof.
581 */
582 switch (best_path->pathtype)
583 {
584 case T_IndexScan:
585 case T_IndexOnlyScan:
586 scan_clauses = castNode(IndexPath, best_path)->indexinfo->indrestrictinfo;
587 break;
588 default:
589 scan_clauses = rel->baserestrictinfo;
590 break;
591 }
592
593 /*
594 * If this is a parameterized scan, we also need to enforce all the join
595 * clauses available from the outer relation(s).
596 *
597 * For paranoia's sake, don't modify the stored baserestrictinfo list.
598 */
599 if (best_path->param_info)
600 scan_clauses = list_concat_copy(scan_clauses,
601 best_path->param_info->ppi_clauses);
602
603 /*
604 * Detect whether we have any pseudoconstant quals to deal with. Then, if
605 * we'll need a gating Result node, it will be able to project, so there
606 * are no requirements on the child's tlist.
607 *
608 * If this replaces a join, it must be a foreign scan or a custom scan,
609 * and the FDW or the custom scan provider would have stored in the best
610 * path the list of RestrictInfo nodes to apply to the join; check against
611 * that list in that case.
612 */
613 if (IS_JOIN_REL(rel))
614 {
615 List *join_clauses;
616
617 Assert(best_path->pathtype == T_ForeignScan ||
618 best_path->pathtype == T_CustomScan);
619 if (best_path->pathtype == T_ForeignScan)
620 join_clauses = ((ForeignPath *) best_path)->fdw_restrictinfo;
621 else
622 join_clauses = ((CustomPath *) best_path)->custom_restrictinfo;
623
624 gating_clauses = get_gating_quals(root, join_clauses);
625 }
626 else
627 gating_clauses = get_gating_quals(root, scan_clauses);
628 if (gating_clauses)
629 flags = 0;
630
631 /*
632 * For table scans, rather than using the relation targetlist (which is
633 * only those Vars actually needed by the query), we prefer to generate a
634 * tlist containing all Vars in order. This will allow the executor to
635 * optimize away projection of the table tuples, if possible.
636 *
637 * But if the caller is going to ignore our tlist anyway, then don't
638 * bother generating one at all. We use an exact equality test here, so
639 * that this only applies when CP_IGNORE_TLIST is the only flag set.
640 */
641 if (flags == CP_IGNORE_TLIST)
642 {
643 tlist = NULL;
644 }
645 else if (use_physical_tlist(root, best_path, flags))
646 {
647 if (best_path->pathtype == T_IndexOnlyScan)
648 {
649 /* For index-only scan, the preferred tlist is the index's */
650 tlist = copyObject(((IndexPath *) best_path)->indexinfo->indextlist);
651
652 /*
653 * Transfer sortgroupref data to the replacement tlist, if
654 * requested (use_physical_tlist checked that this will work).
655 */
656 if (flags & CP_LABEL_TLIST)
657 apply_pathtarget_labeling_to_tlist(tlist, best_path->pathtarget);
658 }
659 else
660 {
661 tlist = build_physical_tlist(root, rel);
662 if (tlist == NIL)
663 {
664 /* Failed because of dropped cols, so use regular method */
665 tlist = build_path_tlist(root, best_path);
666 }
667 else
668 {
669 /* As above, transfer sortgroupref data to replacement tlist */
670 if (flags & CP_LABEL_TLIST)
671 apply_pathtarget_labeling_to_tlist(tlist, best_path->pathtarget);
672 }
673 }
674 }
675 else
676 {
677 tlist = build_path_tlist(root, best_path);
678 }
679
680 switch (best_path->pathtype)
681 {
682 case T_SeqScan:
684 best_path,
685 tlist,
686 scan_clauses);
687 break;
688
689 case T_SampleScan:
691 best_path,
692 tlist,
693 scan_clauses);
694 break;
695
696 case T_IndexScan:
698 (IndexPath *) best_path,
699 tlist,
700 scan_clauses,
701 false);
702 break;
703
704 case T_IndexOnlyScan:
706 (IndexPath *) best_path,
707 tlist,
708 scan_clauses,
709 true);
710 break;
711
712 case T_BitmapHeapScan:
714 (BitmapHeapPath *) best_path,
715 tlist,
716 scan_clauses);
717 break;
718
719 case T_TidScan:
721 (TidPath *) best_path,
722 tlist,
723 scan_clauses);
724 break;
725
726 case T_TidRangeScan:
728 (TidRangePath *) best_path,
729 tlist,
730 scan_clauses);
731 break;
732
733 case T_SubqueryScan:
735 (SubqueryScanPath *) best_path,
736 tlist,
737 scan_clauses);
738 break;
739
740 case T_FunctionScan:
742 best_path,
743 tlist,
744 scan_clauses);
745 break;
746
747 case T_TableFuncScan:
749 best_path,
750 tlist,
751 scan_clauses);
752 break;
753
754 case T_ValuesScan:
756 best_path,
757 tlist,
758 scan_clauses);
759 break;
760
761 case T_CteScan:
763 best_path,
764 tlist,
765 scan_clauses);
766 break;
767
768 case T_NamedTuplestoreScan:
770 best_path,
771 tlist,
772 scan_clauses);
773 break;
774
775 case T_Result:
777 best_path,
778 tlist,
779 scan_clauses);
780 break;
781
782 case T_WorkTableScan:
784 best_path,
785 tlist,
786 scan_clauses);
787 break;
788
789 case T_ForeignScan:
791 (ForeignPath *) best_path,
792 tlist,
793 scan_clauses);
794 break;
795
796 case T_CustomScan:
798 (CustomPath *) best_path,
799 tlist,
800 scan_clauses);
801 break;
802
803 default:
804 elog(ERROR, "unrecognized node type: %d",
805 (int) best_path->pathtype);
806 plan = NULL; /* keep compiler quiet */
807 break;
808 }
809
810 /*
811 * If there are any pseudoconstant clauses attached to this node, insert a
812 * gating Result node that evaluates the pseudoconstants as one-time
813 * quals.
814 */
815 if (gating_clauses)
816 plan = create_gating_plan(root, best_path, plan, gating_clauses);
817
818 return plan;
819}
static SeqScan * create_seqscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:2920
static ValuesScan * create_valuesscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3850
static TableFuncScan * create_tablefuncscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3807
static CustomScan * create_customscan_plan(PlannerInfo *root, CustomPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:4280
static ForeignScan * create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:4125
static BitmapHeapScan * create_bitmap_scan_plan(PlannerInfo *root, BitmapHeapPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3205
static TidScan * create_tidscan_plan(PlannerInfo *root, TidPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3543
static WorkTableScan * create_worktablescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:4065
static FunctionScan * create_functionscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3764
static Result * create_resultscan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:4028
static CteScan * create_ctescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3894
static NamedTuplestoreScan * create_namedtuplestorescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3989
static SubqueryScan * create_subqueryscan_plan(PlannerInfo *root, SubqueryScanPath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3705
static TidRangeScan * create_tidrangescan_plan(PlannerInfo *root, TidRangePath *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:3640
static SampleScan * create_samplescan_plan(PlannerInfo *root, Path *best_path, List *tlist, List *scan_clauses)
Definition: createplan.c:2958
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:598
#define copyObject(obj)
Definition: nodes.h:224
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:844
List * build_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
Definition: plancat.c:1784

References apply_pathtarget_labeling_to_tlist(), Assert, RelOptInfo::baserestrictinfo, build_path_tlist(), build_physical_tlist(), castNode, copyObject, CP_IGNORE_TLIST, CP_LABEL_TLIST, create_bitmap_scan_plan(), create_ctescan_plan(), create_customscan_plan(), create_foreignscan_plan(), create_functionscan_plan(), create_gating_plan(), create_indexscan_plan(), create_namedtuplestorescan_plan(), create_resultscan_plan(), create_samplescan_plan(), create_seqscan_plan(), create_subqueryscan_plan(), create_tablefuncscan_plan(), create_tidrangescan_plan(), create_tidscan_plan(), create_valuesscan_plan(), create_worktablescan_plan(), elog, ERROR, get_gating_quals(), IS_JOIN_REL, list_concat_copy(), NIL, Path::pathtype, plan, root, and use_physical_tlist().

Referenced by create_plan_recurse().

◆ create_seqscan_plan()

static SeqScan * create_seqscan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 2920 of file createplan.c.

2922{
2923 SeqScan *scan_plan;
2924 Index scan_relid = best_path->parent->relid;
2925
2926 /* it should be a base rel... */
2927 Assert(scan_relid > 0);
2928 Assert(best_path->parent->rtekind == RTE_RELATION);
2929
2930 /* Sort clauses into best execution order */
2931 scan_clauses = order_qual_clauses(root, scan_clauses);
2932
2933 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2934 scan_clauses = extract_actual_clauses(scan_clauses, false);
2935
2936 /* Replace any outer-relation variables with nestloop params */
2937 if (best_path->param_info)
2938 {
2939 scan_clauses = (List *)
2940 replace_nestloop_params(root, (Node *) scan_clauses);
2941 }
2942
2943 scan_plan = make_seqscan(tlist,
2944 scan_clauses,
2945 scan_relid);
2946
2947 copy_generic_path_info(&scan_plan->scan.plan, best_path);
2948
2949 return scan_plan;
2950}
static SeqScan * make_seqscan(List *qptlist, List *qpqual, Index scanrelid)
Definition: createplan.c:5584
Scan scan
Definition: plannodes.h:401

References Assert, copy_generic_path_info(), extract_actual_clauses(), make_seqscan(), order_qual_clauses(), replace_nestloop_params(), root, RTE_RELATION, and SeqScan::scan.

Referenced by create_scan_plan().

◆ create_setop_plan()

static SetOp * create_setop_plan ( PlannerInfo root,
SetOpPath best_path,
int  flags 
)
static

Definition at line 2719 of file createplan.c.

2720{
2721 SetOp *plan;
2722 List *tlist = build_path_tlist(root, &best_path->path);
2723 Plan *leftplan;
2724 Plan *rightplan;
2725 long numGroups;
2726
2727 /*
2728 * SetOp doesn't project, so tlist requirements pass through; moreover we
2729 * need grouping columns to be labeled.
2730 */
2731 leftplan = create_plan_recurse(root, best_path->leftpath,
2732 flags | CP_LABEL_TLIST);
2733 rightplan = create_plan_recurse(root, best_path->rightpath,
2734 flags | CP_LABEL_TLIST);
2735
2736 /* Convert numGroups to long int --- but 'ware overflow! */
2737 numGroups = clamp_cardinality_to_long(best_path->numGroups);
2738
2739 plan = make_setop(best_path->cmd,
2740 best_path->strategy,
2741 tlist,
2742 leftplan,
2743 rightplan,
2744 best_path->groupList,
2745 numGroups);
2746
2747 copy_generic_path_info(&plan->plan, (Path *) best_path);
2748
2749 return plan;
2750}
static SetOp * make_setop(SetOpCmd cmd, SetOpStrategy strategy, List *tlist, Plan *lefttree, Plan *righttree, List *groupList, long numGroups)
Definition: createplan.c:6962
Path * rightpath
Definition: pathnodes.h:2346
Cardinality numGroups
Definition: pathnodes.h:2350
Path * leftpath
Definition: pathnodes.h:2345
SetOpCmd cmd
Definition: pathnodes.h:2347
Path path
Definition: pathnodes.h:2344
SetOpStrategy strategy
Definition: pathnodes.h:2348
List * groupList
Definition: pathnodes.h:2349

References build_path_tlist(), clamp_cardinality_to_long(), SetOpPath::cmd, copy_generic_path_info(), CP_LABEL_TLIST, create_plan_recurse(), SetOpPath::groupList, SetOpPath::leftpath, make_setop(), SetOpPath::numGroups, SetOpPath::path, plan, SetOpPath::rightpath, root, and SetOpPath::strategy.

Referenced by create_plan_recurse().

◆ create_sort_plan()

static Sort * create_sort_plan ( PlannerInfo root,
SortPath best_path,
int  flags 
)
static

Definition at line 2179 of file createplan.c.

2180{
2181 Sort *plan;
2182 Plan *subplan;
2183
2184 /*
2185 * We don't want any excess columns in the sorted tuples, so request a
2186 * smaller tlist. Otherwise, since Sort doesn't project, tlist
2187 * requirements pass through.
2188 */
2189 subplan = create_plan_recurse(root, best_path->subpath,
2190 flags | CP_SMALL_TLIST);
2191
2192 /*
2193 * make_sort_from_pathkeys indirectly calls find_ec_member_matching_expr,
2194 * which will ignore any child EC members that don't belong to the given
2195 * relids. Thus, if this sort path is based on a child relation, we must
2196 * pass its relids.
2197 */
2198 plan = make_sort_from_pathkeys(subplan, best_path->path.pathkeys,
2199 IS_OTHER_REL(best_path->subpath->parent) ?
2200 best_path->path.parent->relids : NULL);
2201
2202 copy_generic_path_info(&plan->plan, (Path *) best_path);
2203
2204 return plan;
2205}

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

Referenced by create_plan_recurse().

◆ create_subqueryscan_plan()

static SubqueryScan * create_subqueryscan_plan ( PlannerInfo root,
SubqueryScanPath best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3705 of file createplan.c.

3707{
3708 SubqueryScan *scan_plan;
3709 RelOptInfo *rel = best_path->path.parent;
3710 Index scan_relid = rel->relid;
3711 Plan *subplan;
3712
3713 /* it should be a subquery base rel... */
3714 Assert(scan_relid > 0);
3715 Assert(rel->rtekind == RTE_SUBQUERY);
3716
3717 /*
3718 * Recursively create Plan from Path for subquery. Since we are entering
3719 * a different planner context (subroot), recurse to create_plan not
3720 * create_plan_recurse.
3721 */
3722 subplan = create_plan(rel->subroot, best_path->subpath);
3723
3724 /* Sort clauses into best execution order */
3725 scan_clauses = order_qual_clauses(root, scan_clauses);
3726
3727 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3728 scan_clauses = extract_actual_clauses(scan_clauses, false);
3729
3730 /*
3731 * Replace any outer-relation variables with nestloop params.
3732 *
3733 * We must provide nestloop params for both lateral references of the
3734 * subquery and outer vars in the scan_clauses. It's better to assign the
3735 * former first, because that code path requires specific param IDs, while
3736 * replace_nestloop_params can adapt to the IDs assigned by
3737 * process_subquery_nestloop_params. This avoids possibly duplicating
3738 * nestloop params when the same Var is needed for both reasons.
3739 */
3740 if (best_path->path.param_info)
3741 {
3743 rel->subplan_params);
3744 scan_clauses = (List *)
3745 replace_nestloop_params(root, (Node *) scan_clauses);
3746 }
3747
3748 scan_plan = make_subqueryscan(tlist,
3749 scan_clauses,
3750 scan_relid,
3751 subplan);
3752
3753 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3754
3755 return scan_plan;
3756}
static SubqueryScan * make_subqueryscan(List *qptlist, List *qpqual, Index scanrelid, Plan *subplan)
Definition: createplan.c:5759
void process_subquery_nestloop_params(PlannerInfo *root, List *subplan_params)
Definition: paramassign.c:527
@ RTE_SUBQUERY
Definition: parsenodes.h:1027
List * subplan_params
Definition: pathnodes.h:954
PlannerInfo * subroot
Definition: pathnodes.h:953

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

Referenced by create_scan_plan().

◆ create_tablefuncscan_plan()

static TableFuncScan * create_tablefuncscan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3807 of file createplan.c.

3809{
3810 TableFuncScan *scan_plan;
3811 Index scan_relid = best_path->parent->relid;
3812 RangeTblEntry *rte;
3813 TableFunc *tablefunc;
3814
3815 /* it should be a function base rel... */
3816 Assert(scan_relid > 0);
3817 rte = planner_rt_fetch(scan_relid, root);
3818 Assert(rte->rtekind == RTE_TABLEFUNC);
3819 tablefunc = rte->tablefunc;
3820
3821 /* Sort clauses into best execution order */
3822 scan_clauses = order_qual_clauses(root, scan_clauses);
3823
3824 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3825 scan_clauses = extract_actual_clauses(scan_clauses, false);
3826
3827 /* Replace any outer-relation variables with nestloop params */
3828 if (best_path->param_info)
3829 {
3830 scan_clauses = (List *)
3831 replace_nestloop_params(root, (Node *) scan_clauses);
3832 /* The function expressions could contain nestloop params, too */
3833 tablefunc = (TableFunc *) replace_nestloop_params(root, (Node *) tablefunc);
3834 }
3835
3836 scan_plan = make_tablefuncscan(tlist, scan_clauses, scan_relid,
3837 tablefunc);
3838
3839 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3840
3841 return scan_plan;
3842}
static TableFuncScan * make_tablefuncscan(List *qptlist, List *qpqual, Index scanrelid, TableFunc *tablefunc)
Definition: createplan.c:5800
@ RTE_TABLEFUNC
Definition: parsenodes.h:1030
TableFunc * tablefunc
Definition: parsenodes.h:1193

References Assert, copy_generic_path_info(), extract_actual_clauses(), make_tablefuncscan(), order_qual_clauses(), planner_rt_fetch, replace_nestloop_params(), root, RTE_TABLEFUNC, RangeTblEntry::rtekind, TableFuncScan::scan, and RangeTblEntry::tablefunc.

Referenced by create_scan_plan().

◆ create_tidrangescan_plan()

static TidRangeScan * create_tidrangescan_plan ( PlannerInfo root,
TidRangePath best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3640 of file createplan.c.

3642{
3643 TidRangeScan *scan_plan;
3644 Index scan_relid = best_path->path.parent->relid;
3645 List *tidrangequals = best_path->tidrangequals;
3646
3647 /* it should be a base rel... */
3648 Assert(scan_relid > 0);
3649 Assert(best_path->path.parent->rtekind == RTE_RELATION);
3650
3651 /*
3652 * The qpqual list must contain all restrictions not enforced by the
3653 * tidrangequals list. tidrangequals has AND semantics, so we can simply
3654 * remove any qual that appears in it.
3655 */
3656 {
3657 List *qpqual = NIL;
3658 ListCell *l;
3659
3660 foreach(l, scan_clauses)
3661 {
3663
3664 if (rinfo->pseudoconstant)
3665 continue; /* we may drop pseudoconstants here */
3666 if (list_member_ptr(tidrangequals, rinfo))
3667 continue; /* simple duplicate */
3668 qpqual = lappend(qpqual, rinfo);
3669 }
3670 scan_clauses = qpqual;
3671 }
3672
3673 /* Sort clauses into best execution order */
3674 scan_clauses = order_qual_clauses(root, scan_clauses);
3675
3676 /* Reduce RestrictInfo lists to bare expressions; ignore pseudoconstants */
3677 tidrangequals = extract_actual_clauses(tidrangequals, false);
3678 scan_clauses = extract_actual_clauses(scan_clauses, false);
3679
3680 /* Replace any outer-relation variables with nestloop params */
3681 if (best_path->path.param_info)
3682 {
3683 tidrangequals = (List *)
3684 replace_nestloop_params(root, (Node *) tidrangequals);
3685 scan_clauses = (List *)
3686 replace_nestloop_params(root, (Node *) scan_clauses);
3687 }
3688
3689 scan_plan = make_tidrangescan(tlist,
3690 scan_clauses,
3691 scan_relid,
3692 tidrangequals);
3693
3694 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3695
3696 return scan_plan;
3697}
static TidRangeScan * make_tidrangescan(List *qptlist, List *qpqual, Index scanrelid, List *tidrangequals)
Definition: createplan.c:5740
List * tidrangequals
Definition: pathnodes.h:1848

References Assert, copy_generic_path_info(), extract_actual_clauses(), lappend(), lfirst_node, list_member_ptr(), make_tidrangescan(), NIL, order_qual_clauses(), TidRangePath::path, replace_nestloop_params(), root, RTE_RELATION, TidRangeScan::scan, and TidRangePath::tidrangequals.

Referenced by create_scan_plan().

◆ create_tidscan_plan()

static TidScan * create_tidscan_plan ( PlannerInfo root,
TidPath best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3543 of file createplan.c.

3545{
3546 TidScan *scan_plan;
3547 Index scan_relid = best_path->path.parent->relid;
3548 List *tidquals = best_path->tidquals;
3549
3550 /* it should be a base rel... */
3551 Assert(scan_relid > 0);
3552 Assert(best_path->path.parent->rtekind == RTE_RELATION);
3553
3554 /*
3555 * The qpqual list must contain all restrictions not enforced by the
3556 * tidquals list. Since tidquals has OR semantics, we have to be careful
3557 * about matching it up to scan_clauses. It's convenient to handle the
3558 * single-tidqual case separately from the multiple-tidqual case. In the
3559 * single-tidqual case, we look through the scan_clauses while they are
3560 * still in RestrictInfo form, and drop any that are redundant with the
3561 * tidqual.
3562 *
3563 * In normal cases simple pointer equality checks will be enough to spot
3564 * duplicate RestrictInfos, so we try that first.
3565 *
3566 * Another common case is that a scan_clauses entry is generated from the
3567 * same EquivalenceClass as some tidqual, and is therefore redundant with
3568 * it, though not equal.
3569 *
3570 * Unlike indexpaths, we don't bother with predicate_implied_by(); the
3571 * number of cases where it could win are pretty small.
3572 */
3573 if (list_length(tidquals) == 1)
3574 {
3575 List *qpqual = NIL;
3576 ListCell *l;
3577
3578 foreach(l, scan_clauses)
3579 {
3581
3582 if (rinfo->pseudoconstant)
3583 continue; /* we may drop pseudoconstants here */
3584 if (list_member_ptr(tidquals, rinfo))
3585 continue; /* simple duplicate */
3586 if (is_redundant_derived_clause(rinfo, tidquals))
3587 continue; /* derived from same EquivalenceClass */
3588 qpqual = lappend(qpqual, rinfo);
3589 }
3590 scan_clauses = qpqual;
3591 }
3592
3593 /* Sort clauses into best execution order */
3594 scan_clauses = order_qual_clauses(root, scan_clauses);
3595
3596 /* Reduce RestrictInfo lists to bare expressions; ignore pseudoconstants */
3597 tidquals = extract_actual_clauses(tidquals, false);
3598 scan_clauses = extract_actual_clauses(scan_clauses, false);
3599
3600 /*
3601 * If we have multiple tidquals, it's more convenient to remove duplicate
3602 * scan_clauses after stripping the RestrictInfos. In this situation,
3603 * because the tidquals represent OR sub-clauses, they could not have come
3604 * from EquivalenceClasses so we don't have to worry about matching up
3605 * non-identical clauses. On the other hand, because tidpath.c will have
3606 * extracted those sub-clauses from some OR clause and built its own list,
3607 * we will certainly not have pointer equality to any scan clause. So
3608 * convert the tidquals list to an explicit OR clause and see if we can
3609 * match it via equal() to any scan clause.
3610 */
3611 if (list_length(tidquals) > 1)
3612 scan_clauses = list_difference(scan_clauses,
3613 list_make1(make_orclause(tidquals)));
3614
3615 /* Replace any outer-relation variables with nestloop params */
3616 if (best_path->path.param_info)
3617 {
3618 tidquals = (List *)
3619 replace_nestloop_params(root, (Node *) tidquals);
3620 scan_clauses = (List *)