PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 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_unique_plan (PlannerInfo *root, UniquePath *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, Cardinality 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, Cardinality est_calls, Cardinality est_unique_keys, double est_hit_ratio)
 
static WindowAggmake_windowagg (List *tlist, WindowClause *wc, int partNumCols, AttrNumber *partColIdx, Oid *partOperators, Oid *partCollations, int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators, Oid *ordCollations, 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_pathkeys (Plan *lefttree, List *pathkeys, int numCols, Relids relids)
 
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, Cardinality numGroups)
 
static LockRowsmake_lockrows (Plan *lefttree, List *rowMarks, int epqParam)
 
static Resultmake_gating_result (List *tlist, Node *resconstantqual, Plan *subplan)
 
static Resultmake_one_row_result (List *tlist, Node *resconstantqual, RelOptInfo *rel)
 
static ProjectSetmake_project_set (List *tlist, Plan *subplan)
 
static ModifyTablemake_modifytable (PlannerInfo *root, Plan *subplan, CmdType operation, bool canSetTag, Index nominalRelation, Index rootRelation, 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, Cardinality numGroups, 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 5451 of file createplan.c.

5452{
5453 if (IsA(plan, BitmapAnd))
5455 else if (IsA(plan, BitmapOr))
5456 {
5457 ((BitmapOr *) plan)->isshared = true;
5458 bitmap_subplan_mark_shared(linitial(((BitmapOr *) plan)->bitmapplans));
5459 }
5460 else if (IsA(plan, BitmapIndexScan))
5461 ((BitmapIndexScan *) plan)->isshared = true;
5462 else
5463 elog(ERROR, "unrecognized node type: %d", nodeTag(plan));
5464}
static void bitmap_subplan_mark_shared(Plan *plan)
Definition: createplan.c:5451
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
#define nodeTag(nodeptr)
Definition: nodes.h:139
#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 818 of file createplan.c.

819{
820 List *tlist = NIL;
821 Index *sortgrouprefs = path->pathtarget->sortgrouprefs;
822 int resno = 1;
823 ListCell *v;
824
825 foreach(v, path->pathtarget->exprs)
826 {
827 Node *node = (Node *) lfirst(v);
828 TargetEntry *tle;
829
830 /*
831 * If it's a parameterized path, there might be lateral references in
832 * the tlist, which need to be replaced with Params. There's no need
833 * to remake the TargetEntry nodes, so apply this to each list item
834 * separately.
835 */
836 if (path->param_info)
837 node = replace_nestloop_params(root, node);
838
839 tle = makeTargetEntry((Expr *) node,
840 resno,
841 NULL,
842 false);
843 if (sortgrouprefs)
844 tle->ressortgroupref = sortgrouprefs[resno - 1];
845
846 tlist = lappend(tlist, tle);
847 resno++;
848 }
849 return tlist;
850}
unsigned int Index
Definition: c.h:623
static Node * replace_nestloop_params(PlannerInfo *root, Node *expr)
Definition: createplan.c:4880
List * lappend(List *list, void *datum)
Definition: list.c:339
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:289
#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:135
Index ressortgroupref
Definition: primnodes.h:2245

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(), and create_windowagg_plan().

◆ change_plan_targetlist()

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

Definition at line 1991 of file createplan.c.

1992{
1993 /*
1994 * If the top plan node can't do projections and its existing target list
1995 * isn't already what we need, we need to add a Result node to help it
1996 * along.
1997 */
1998 if (!is_projection_capable_plan(subplan) &&
1999 !tlist_same_exprs(tlist, subplan->targetlist))
2000 subplan = inject_projection_plan(subplan, tlist,
2001 subplan->parallel_safe &&
2002 tlist_parallel_safe);
2003 else
2004 {
2005 /* Else we can just replace the plan node's tlist */
2006 subplan->targetlist = tlist;
2007 subplan->parallel_safe &= tlist_parallel_safe;
2008 }
2009 return subplan;
2010}
bool is_projection_capable_plan(Plan *plan)
Definition: createplan.c:7267
static Plan * inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
Definition: createplan.c:1959
bool parallel_safe
Definition: plannodes.h:215
List * targetlist
Definition: plannodes.h:229
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_nestloop_plan(), and postgresGetForeignPlan().

◆ copy_generic_path_info()

static void copy_generic_path_info ( Plan dest,
Path src 
)
static

Definition at line 5352 of file createplan.c.

5353{
5354 dest->disabled_nodes = src->disabled_nodes;
5355 dest->startup_cost = src->startup_cost;
5356 dest->total_cost = src->total_cost;
5357 dest->plan_rows = src->rows;
5358 dest->plan_width = src->pathtarget->width;
5359 dest->parallel_aware = src->parallel_aware;
5360 dest->parallel_safe = src->parallel_safe;
5361}
Cardinality rows
Definition: pathnodes.h:1906
Cost startup_cost
Definition: pathnodes.h:1908
int disabled_nodes
Definition: pathnodes.h:1907
Cost total_cost
Definition: pathnodes.h:1909
bool parallel_aware
Definition: pathnodes.h:1899
bool parallel_safe
Definition: pathnodes.h:1901

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

5369{
5370 dest->disabled_nodes = src->disabled_nodes;
5371 dest->startup_cost = src->startup_cost;
5372 dest->total_cost = src->total_cost;
5373 dest->plan_rows = src->plan_rows;
5374 dest->plan_width = src->plan_width;
5375 /* Assume the inserted node is not parallel-aware. */
5376 dest->parallel_aware = false;
5377 /* Assume the inserted node is parallel-safe, if child plan is. */
5378 dest->parallel_safe = src->parallel_safe;
5379}
Cost total_cost
Definition: plannodes.h:199
Cost startup_cost
Definition: plannodes.h:197
int plan_width
Definition: plannodes.h:207
Cardinality plan_rows
Definition: plannodes.h:205
int disabled_nodes
Definition: plannodes.h:195

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

2156{
2157 Agg *plan;
2158 Plan *subplan;
2159 List *tlist;
2160 List *quals;
2161
2162 /*
2163 * Agg can project, so no need to be terribly picky about child tlist, but
2164 * we do need grouping columns to be available
2165 */
2166 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2167
2168 tlist = build_path_tlist(root, &best_path->path);
2169
2170 quals = order_qual_clauses(root, best_path->qual);
2171
2172 plan = make_agg(tlist, quals,
2173 best_path->aggstrategy,
2174 best_path->aggsplit,
2175 list_length(best_path->groupClause),
2177 subplan->targetlist),
2180 subplan->targetlist),
2181 NIL,
2182 NIL,
2183 best_path->numGroups,
2184 best_path->transitionSpace,
2185 subplan);
2186
2187 copy_generic_path_info(&plan->plan, (Path *) best_path);
2188
2189 return plan;
2190}
static List * order_qual_clauses(PlannerInfo *root, List *clauses)
Definition: createplan.c:5258
static void copy_generic_path_info(Plan *dest, Path *src)
Definition: createplan.c:5352
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:818
Agg * make_agg(List *tlist, List *qual, AggStrategy aggstrategy, AggSplit aggsplit, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations, List *groupingSets, List *chain, Cardinality numGroups, Size transitionSpace, Plan *lefttree)
Definition: createplan.c:6574
#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:2483
Cardinality numGroups
Definition: pathnodes.h:2486
AggSplit aggsplit
Definition: pathnodes.h:2485
List * groupClause
Definition: pathnodes.h:2488
uint64 transitionSpace
Definition: pathnodes.h:2487
AggStrategy aggstrategy
Definition: pathnodes.h:2484
Path path
Definition: pathnodes.h:2482
List * qual
Definition: pathnodes.h:2489
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 1209 of file createplan.c.

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

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_incremental_sort, enable_partition_pruning, ERROR, extract_actual_clauses(), AppendPath::first_partial_path, inject_projection_plan(), label_incrementalsort_with_costsize(), label_sort_with_costsize(), lappend(), lfirst, AppendPath::limit_tuples, list_concat(), list_copy_head(), list_length(), list_make1, make_incrementalsort(), make_one_row_result(), make_partition_pruneinfo(), make_sort(), makeBoolConst(), makeNode, mark_async_capable_plan(), NIL, Path::parallel_safe, AppendPath::path, Path::pathkeys, pathkeys_count_contained_in(), plan, prepare_sort_from_pathkeys(), RelOptInfo::relids, replace_nestloop_params(), root, 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 3038 of file createplan.c.

3042{
3043 Index baserelid = best_path->path.parent->relid;
3044 Plan *bitmapqualplan;
3045 List *bitmapqualorig;
3046 List *indexquals;
3047 List *indexECs;
3048 List *qpqual;
3049 ListCell *l;
3050 BitmapHeapScan *scan_plan;
3051
3052 /* it should be a base rel... */
3053 Assert(baserelid > 0);
3054 Assert(best_path->path.parent->rtekind == RTE_RELATION);
3055
3056 /* Process the bitmapqual tree into a Plan tree and qual lists */
3057 bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
3058 &bitmapqualorig, &indexquals,
3059 &indexECs);
3060
3061 if (best_path->path.parallel_aware)
3062 bitmap_subplan_mark_shared(bitmapqualplan);
3063
3064 /*
3065 * The qpqual list must contain all restrictions not automatically handled
3066 * by the index, other than pseudoconstant clauses which will be handled
3067 * by a separate gating plan node. All the predicates in the indexquals
3068 * will be checked (either by the index itself, or by
3069 * nodeBitmapHeapscan.c), but if there are any "special" operators
3070 * involved then they must be added to qpqual. The upshot is that qpqual
3071 * must contain scan_clauses minus whatever appears in indexquals.
3072 *
3073 * This loop is similar to the comparable code in create_indexscan_plan(),
3074 * but with some differences because it has to compare the scan clauses to
3075 * stripped (no RestrictInfos) indexquals. See comments there for more
3076 * info.
3077 *
3078 * In normal cases simple equal() checks will be enough to spot duplicate
3079 * clauses, so we try that first. We next see if the scan clause is
3080 * redundant with any top-level indexqual by virtue of being generated
3081 * from the same EC. After that, try predicate_implied_by().
3082 *
3083 * Unlike create_indexscan_plan(), the predicate_implied_by() test here is
3084 * useful for getting rid of qpquals that are implied by index predicates,
3085 * because the predicate conditions are included in the "indexquals"
3086 * returned by create_bitmap_subplan(). Bitmap scans have to do it that
3087 * way because predicate conditions need to be rechecked if the scan
3088 * becomes lossy, so they have to be included in bitmapqualorig.
3089 */
3090 qpqual = NIL;
3091 foreach(l, scan_clauses)
3092 {
3094 Node *clause = (Node *) rinfo->clause;
3095
3096 if (rinfo->pseudoconstant)
3097 continue; /* we may drop pseudoconstants here */
3098 if (list_member(indexquals, clause))
3099 continue; /* simple duplicate */
3100 if (rinfo->parent_ec && list_member_ptr(indexECs, rinfo->parent_ec))
3101 continue; /* derived from same EquivalenceClass */
3102 if (!contain_mutable_functions(clause) &&
3103 predicate_implied_by(list_make1(clause), indexquals, false))
3104 continue; /* provably implied by indexquals */
3105 qpqual = lappend(qpqual, rinfo);
3106 }
3107
3108 /* Sort clauses into best execution order */
3109 qpqual = order_qual_clauses(root, qpqual);
3110
3111 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3112 qpqual = extract_actual_clauses(qpqual, false);
3113
3114 /*
3115 * When dealing with special operators, we will at this point have
3116 * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
3117 * 'em from bitmapqualorig, since there's no point in making the tests
3118 * twice.
3119 */
3120 bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
3121
3122 /*
3123 * We have to replace any outer-relation variables with nestloop params in
3124 * the qpqual and bitmapqualorig expressions. (This was already done for
3125 * expressions attached to plan nodes in the bitmapqualplan tree.)
3126 */
3127 if (best_path->path.param_info)
3128 {
3129 qpqual = (List *)
3130 replace_nestloop_params(root, (Node *) qpqual);
3131 bitmapqualorig = (List *)
3132 replace_nestloop_params(root, (Node *) bitmapqualorig);
3133 }
3134
3135 /* Finally ready to build the plan node */
3136 scan_plan = make_bitmap_heapscan(tlist,
3137 qpqual,
3138 bitmapqualplan,
3139 bitmapqualorig,
3140 baserelid);
3141
3142 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3143
3144 return scan_plan;
3145}
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:374
static Plan * create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual, List **qual, List **indexqual, List **indexECs)
Definition: createplan.c:3168
static BitmapHeapScan * make_bitmap_heapscan(List *qptlist, List *qpqual, Plan *lefttree, List *bitmapqualorig, Index scanrelid)
Definition: createplan.c:5598
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
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:1043
#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:2032
Expr * clause
Definition: pathnodes.h:2791

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

3170{
3171 Plan *plan;
3172
3173 if (IsA(bitmapqual, BitmapAndPath))
3174 {
3175 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
3176 List *subplans = NIL;
3177 List *subquals = NIL;
3178 List *subindexquals = NIL;
3179 List *subindexECs = NIL;
3180 ListCell *l;
3181
3182 /*
3183 * There may well be redundant quals among the subplans, since a
3184 * top-level WHERE qual might have gotten used to form several
3185 * different index quals. We don't try exceedingly hard to eliminate
3186 * redundancies, but we do eliminate obvious duplicates by using
3187 * list_concat_unique.
3188 */
3189 foreach(l, apath->bitmapquals)
3190 {
3191 Plan *subplan;
3192 List *subqual;
3193 List *subindexqual;
3194 List *subindexEC;
3195
3196 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
3197 &subqual, &subindexqual,
3198 &subindexEC);
3199 subplans = lappend(subplans, subplan);
3200 subquals = list_concat_unique(subquals, subqual);
3201 subindexquals = list_concat_unique(subindexquals, subindexqual);
3202 /* Duplicates in indexECs aren't worth getting rid of */
3203 subindexECs = list_concat(subindexECs, subindexEC);
3204 }
3205 plan = (Plan *) make_bitmap_and(subplans);
3206 plan->startup_cost = apath->path.startup_cost;
3207 plan->total_cost = apath->path.total_cost;
3208 plan->plan_rows =
3209 clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
3210 plan->plan_width = 0; /* meaningless */
3211 plan->parallel_aware = false;
3212 plan->parallel_safe = apath->path.parallel_safe;
3213 *qual = subquals;
3214 *indexqual = subindexquals;
3215 *indexECs = subindexECs;
3216 }
3217 else if (IsA(bitmapqual, BitmapOrPath))
3218 {
3219 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
3220 List *subplans = NIL;
3221 List *subquals = NIL;
3222 List *subindexquals = NIL;
3223 bool const_true_subqual = false;
3224 bool const_true_subindexqual = false;
3225 ListCell *l;
3226
3227 /*
3228 * Here, we only detect qual-free subplans. A qual-free subplan would
3229 * cause us to generate "... OR true ..." which we may as well reduce
3230 * to just "true". We do not try to eliminate redundant subclauses
3231 * because (a) it's not as likely as in the AND case, and (b) we might
3232 * well be working with hundreds or even thousands of OR conditions,
3233 * perhaps from a long IN list. The performance of list_append_unique
3234 * would be unacceptable.
3235 */
3236 foreach(l, opath->bitmapquals)
3237 {
3238 Plan *subplan;
3239 List *subqual;
3240 List *subindexqual;
3241 List *subindexEC;
3242
3243 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
3244 &subqual, &subindexqual,
3245 &subindexEC);
3246 subplans = lappend(subplans, subplan);
3247 if (subqual == NIL)
3248 const_true_subqual = true;
3249 else if (!const_true_subqual)
3250 subquals = lappend(subquals,
3251 make_ands_explicit(subqual));
3252 if (subindexqual == NIL)
3253 const_true_subindexqual = true;
3254 else if (!const_true_subindexqual)
3255 subindexquals = lappend(subindexquals,
3256 make_ands_explicit(subindexqual));
3257 }
3258
3259 /*
3260 * In the presence of ScalarArrayOpExpr quals, we might have built
3261 * BitmapOrPaths with just one subpath; don't add an OR step.
3262 */
3263 if (list_length(subplans) == 1)
3264 {
3265 plan = (Plan *) linitial(subplans);
3266 }
3267 else
3268 {
3269 plan = (Plan *) make_bitmap_or(subplans);
3270 plan->startup_cost = opath->path.startup_cost;
3271 plan->total_cost = opath->path.total_cost;
3272 plan->plan_rows =
3273 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
3274 plan->plan_width = 0; /* meaningless */
3275 plan->parallel_aware = false;
3276 plan->parallel_safe = opath->path.parallel_safe;
3277 }
3278
3279 /*
3280 * If there were constant-TRUE subquals, the OR reduces to constant
3281 * TRUE. Also, avoid generating one-element ORs, which could happen
3282 * due to redundancy elimination or ScalarArrayOpExpr quals.
3283 */
3284 if (const_true_subqual)
3285 *qual = NIL;
3286 else if (list_length(subquals) <= 1)
3287 *qual = subquals;
3288 else
3289 *qual = list_make1(make_orclause(subquals));
3290 if (const_true_subindexqual)
3291 *indexqual = NIL;
3292 else if (list_length(subindexquals) <= 1)
3293 *indexqual = subindexquals;
3294 else
3295 *indexqual = list_make1(make_orclause(subindexquals));
3296 *indexECs = NIL;
3297 }
3298 else if (IsA(bitmapqual, IndexPath))
3299 {
3300 IndexPath *ipath = (IndexPath *) bitmapqual;
3301 IndexScan *iscan;
3302 List *subquals;
3303 List *subindexquals;
3304 List *subindexECs;
3305 ListCell *l;
3306
3307 /* Use the regular indexscan plan build machinery... */
3308 iscan = castNode(IndexScan,
3310 NIL, NIL, false));
3311 /* then convert to a bitmap indexscan */
3313 iscan->indexid,
3314 iscan->indexqual,
3315 iscan->indexqualorig);
3316 /* and set its cost/width fields appropriately */
3317 plan->startup_cost = 0.0;
3318 plan->total_cost = ipath->indextotalcost;
3319 plan->plan_rows =
3320 clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
3321 plan->plan_width = 0; /* meaningless */
3322 plan->parallel_aware = false;
3323 plan->parallel_safe = ipath->path.parallel_safe;
3324 /* Extract original index clauses, actual index quals, relevant ECs */
3325 subquals = NIL;
3326 subindexquals = NIL;
3327 subindexECs = NIL;
3328 foreach(l, ipath->indexclauses)
3329 {
3330 IndexClause *iclause = (IndexClause *) lfirst(l);
3331 RestrictInfo *rinfo = iclause->rinfo;
3332
3333 Assert(!rinfo->pseudoconstant);
3334 subquals = lappend(subquals, rinfo->clause);
3335 subindexquals = list_concat(subindexquals,
3336 get_actual_clauses(iclause->indexquals));
3337 if (rinfo->parent_ec)
3338 subindexECs = lappend(subindexECs, rinfo->parent_ec);
3339 }
3340 /* We can add any index predicate conditions, too */
3341 foreach(l, ipath->indexinfo->indpred)
3342 {
3343 Expr *pred = (Expr *) lfirst(l);
3344
3345 /*
3346 * We know that the index predicate must have been implied by the
3347 * query condition as a whole, but it may or may not be implied by
3348 * the conditions that got pushed into the bitmapqual. Avoid
3349 * generating redundant conditions.
3350 */
3351 if (!predicate_implied_by(list_make1(pred), subquals, false))
3352 {
3353 subquals = lappend(subquals, pred);
3354 subindexquals = lappend(subindexquals, pred);
3355 }
3356 }
3357 *qual = subquals;
3358 *indexqual = subindexquals;
3359 *indexECs = subindexECs;
3360 }
3361 else
3362 {
3363 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
3364 plan = NULL; /* keep compiler quiet */
3365 }
3366
3367 return plan;
3368}
double clamp_row_est(double nrows)
Definition: costsize.c:213
static BitmapOr * make_bitmap_or(List *bitmapplans)
Definition: createplan.c:5906
static BitmapIndexScan * make_bitmap_indexscan(Index scanrelid, Oid indexid, List *indexqual, List *indexqualorig)
Definition: createplan.c:5577
static BitmapAnd * make_bitmap_and(List *bitmapplans)
Definition: createplan.c:5891
static Scan * create_indexscan_plan(PlannerInfo *root, IndexPath *best_path, List *tlist, List *scan_clauses, bool indexonly)
Definition: createplan.c:2842
List * list_concat_unique(List *list1, const List *list2)
Definition: list.c:1405
Expr * make_orclause(List *orclauses)
Definition: makefuncs.c:743
Expr * make_ands_explicit(List *andclauses)
Definition: makefuncs.c:799
#define castNode(_type_, nodeptr)
Definition: nodes.h:182
List * get_actual_clauses(List *restrictinfo_list)
Definition: restrictinfo.c:460
Selectivity bitmapselectivity
Definition: pathnodes.h:2045
List * bitmapquals
Definition: pathnodes.h:2044
Selectivity bitmapselectivity
Definition: pathnodes.h:2058
List * bitmapquals
Definition: pathnodes.h:2057
List * indexquals
Definition: pathnodes.h:2006
struct RestrictInfo * rinfo
Definition: pathnodes.h:2005
List * indpred
Definition: pathnodes.h:1310
List * indexclauses
Definition: pathnodes.h:1958
Path path
Definition: pathnodes.h:1956
Selectivity indexselectivity
Definition: pathnodes.h:1963
Cost indextotalcost
Definition: pathnodes.h:1962
IndexOptInfo * indexinfo
Definition: pathnodes.h:1957
Scan scan
Definition: plannodes.h:585
List * indexqualorig
Definition: plannodes.h:591
Oid indexid
Definition: plannodes.h:587
List * indexqual
Definition: plannodes.h:589
Index scanrelid
Definition: plannodes.h:523

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

3729{
3730 CteScan *scan_plan;
3731 Index scan_relid = best_path->parent->relid;
3732 RangeTblEntry *rte;
3733 SubPlan *ctesplan = NULL;
3734 int plan_id;
3735 int cte_param_id;
3736 PlannerInfo *cteroot;
3737 Index levelsup;
3738 int ndx;
3739 ListCell *lc;
3740
3741 Assert(scan_relid > 0);
3742 rte = planner_rt_fetch(scan_relid, root);
3743 Assert(rte->rtekind == RTE_CTE);
3744 Assert(!rte->self_reference);
3745
3746 /*
3747 * Find the referenced CTE, and locate the SubPlan previously made for it.
3748 */
3749 levelsup = rte->ctelevelsup;
3750 cteroot = root;
3751 while (levelsup-- > 0)
3752 {
3753 cteroot = cteroot->parent_root;
3754 if (!cteroot) /* shouldn't happen */
3755 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3756 }
3757
3758 /*
3759 * Note: cte_plan_ids can be shorter than cteList, if we are still working
3760 * on planning the CTEs (ie, this is a side-reference from another CTE).
3761 * So we mustn't use forboth here.
3762 */
3763 ndx = 0;
3764 foreach(lc, cteroot->parse->cteList)
3765 {
3766 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
3767
3768 if (strcmp(cte->ctename, rte->ctename) == 0)
3769 break;
3770 ndx++;
3771 }
3772 if (lc == NULL) /* shouldn't happen */
3773 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
3774 if (ndx >= list_length(cteroot->cte_plan_ids))
3775 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3776 plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
3777 if (plan_id <= 0)
3778 elog(ERROR, "no plan was made for CTE \"%s\"", rte->ctename);
3779 foreach(lc, cteroot->init_plans)
3780 {
3781 ctesplan = (SubPlan *) lfirst(lc);
3782 if (ctesplan->plan_id == plan_id)
3783 break;
3784 }
3785 if (lc == NULL) /* shouldn't happen */
3786 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
3787
3788 /*
3789 * We need the CTE param ID, which is the sole member of the SubPlan's
3790 * setParam list.
3791 */
3792 cte_param_id = linitial_int(ctesplan->setParam);
3793
3794 /* Sort clauses into best execution order */
3795 scan_clauses = order_qual_clauses(root, scan_clauses);
3796
3797 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3798 scan_clauses = extract_actual_clauses(scan_clauses, false);
3799
3800 /* Replace any outer-relation variables with nestloop params */
3801 if (best_path->param_info)
3802 {
3803 scan_clauses = (List *)
3804 replace_nestloop_params(root, (Node *) scan_clauses);
3805 }
3806
3807 scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
3808 plan_id, cte_param_id);
3809
3810 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3811
3812 return scan_plan;
3813}
static CteScan * make_ctescan(List *qptlist, List *qpqual, Index scanrelid, int ctePlanId, int cteParam)
Definition: createplan.c:5735
@ RTE_CTE
Definition: parsenodes.h:1049
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:610
#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:800
List * cte_plan_ids
Definition: pathnodes.h:333
List * init_plans
Definition: pathnodes.h:327
Query * parse
Definition: pathnodes.h:227
List * cteList
Definition: parsenodes.h:173
char * ctename
Definition: parsenodes.h:1227
Index ctelevelsup
Definition: parsenodes.h:1229
RTEKind rtekind
Definition: parsenodes.h:1078
int plan_id
Definition: primnodes.h:1102
List * setParam
Definition: primnodes.h:1121

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

4116{
4117 CustomScan *cplan;
4118 RelOptInfo *rel = best_path->path.parent;
4119 List *custom_plans = NIL;
4120 ListCell *lc;
4121
4122 /* Recursively transform child paths. */
4123 foreach(lc, best_path->custom_paths)
4124 {
4127
4128 custom_plans = lappend(custom_plans, plan);
4129 }
4130
4131 /*
4132 * Sort clauses into the best execution order, although custom-scan
4133 * provider can reorder them again.
4134 */
4135 scan_clauses = order_qual_clauses(root, scan_clauses);
4136
4137 /*
4138 * Invoke custom plan provider to create the Plan node represented by the
4139 * CustomPath.
4140 */
4141 cplan = castNode(CustomScan,
4142 best_path->methods->PlanCustomPath(root,
4143 rel,
4144 best_path,
4145 tlist,
4146 scan_clauses,
4147 custom_plans));
4148
4149 /*
4150 * Copy cost data from Path to Plan; no need to make custom-plan providers
4151 * do this
4152 */
4153 copy_generic_path_info(&cplan->scan.plan, &best_path->path);
4154
4155 /* Likewise, copy the relids that are represented by this custom scan */
4156 cplan->custom_relids = best_path->path.parent->relids;
4157
4158 /*
4159 * Replace any outer-relation variables with nestloop params in the qual
4160 * and custom_exprs expressions. We do this last so that the custom-plan
4161 * provider doesn't have to be involved. (Note that parts of custom_exprs
4162 * could have come from join clauses, so doing this beforehand on the
4163 * scan_clauses wouldn't work.) We assume custom_scan_tlist contains no
4164 * such variables.
4165 */
4166 if (best_path->path.param_info)
4167 {
4168 cplan->scan.plan.qual = (List *)
4169 replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
4170 cplan->custom_exprs = (List *)
4172 }
4173
4174 return cplan;
4175}
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:2158
List * custom_paths
Definition: pathnodes.h:2155
Scan scan
Definition: plannodes.h:913
Bitmapset * custom_relids
Definition: plannodes.h:925
List * custom_exprs
Definition: plannodes.h:919

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

3961{
3962 ForeignScan *scan_plan;
3963 RelOptInfo *rel = best_path->path.parent;
3964 Index scan_relid = rel->relid;
3965 Oid rel_oid = InvalidOid;
3966 Plan *outer_plan = NULL;
3967
3968 Assert(rel->fdwroutine != NULL);
3969
3970 /* transform the child path if any */
3971 if (best_path->fdw_outerpath)
3972 outer_plan = create_plan_recurse(root, best_path->fdw_outerpath,
3974
3975 /*
3976 * If we're scanning a base relation, fetch its OID. (Irrelevant if
3977 * scanning a join relation.)
3978 */
3979 if (scan_relid > 0)
3980 {
3981 RangeTblEntry *rte;
3982
3983 Assert(rel->rtekind == RTE_RELATION);
3984 rte = planner_rt_fetch(scan_relid, root);
3985 Assert(rte->rtekind == RTE_RELATION);
3986 rel_oid = rte->relid;
3987 }
3988
3989 /*
3990 * Sort clauses into best execution order. We do this first since the FDW
3991 * might have more info than we do and wish to adjust the ordering.
3992 */
3993 scan_clauses = order_qual_clauses(root, scan_clauses);
3994
3995 /*
3996 * Let the FDW perform its processing on the restriction clauses and
3997 * generate the plan node. Note that the FDW might remove restriction
3998 * clauses that it intends to execute remotely, or even add more (if it
3999 * has selected some join clauses for remote use but also wants them
4000 * rechecked locally).
4001 */
4002 scan_plan = rel->fdwroutine->GetForeignPlan(root, rel, rel_oid,
4003 best_path,
4004 tlist, scan_clauses,
4005 outer_plan);
4006
4007 /* Copy cost data from Path to Plan; no need to make FDW do this */
4008 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
4009
4010 /* Copy user OID to access as; likewise no need to make FDW do this */
4011 scan_plan->checkAsUser = rel->userid;
4012
4013 /* Copy foreign server OID; likewise, no need to make FDW do this */
4014 scan_plan->fs_server = rel->serverid;
4015
4016 /*
4017 * Likewise, copy the relids that are represented by this foreign scan. An
4018 * upper rel doesn't have relids set, but it covers all the relations
4019 * participating in the underlying scan/join, so use root->all_query_rels.
4020 */
4021 if (rel->reloptkind == RELOPT_UPPER_REL)
4022 scan_plan->fs_relids = root->all_query_rels;
4023 else
4024 scan_plan->fs_relids = best_path->path.parent->relids;
4025
4026 /*
4027 * Join relid sets include relevant outer joins, but FDWs may need to know
4028 * which are the included base rels. That's a bit tedious to get without
4029 * access to the plan-time data structures, so compute it here.
4030 */
4031 scan_plan->fs_base_relids = bms_difference(scan_plan->fs_relids,
4032 root->outer_join_rels);
4033
4034 /*
4035 * If this is a foreign join, and to make it valid to push down we had to
4036 * assume that the current user is the same as some user explicitly named
4037 * in the query, mark the finished plan as depending on the current user.
4038 */
4039 if (rel->useridiscurrent)
4040 root->glob->dependsOnRole = true;
4041
4042 /*
4043 * Replace any outer-relation variables with nestloop params in the qual,
4044 * fdw_exprs and fdw_recheck_quals expressions. We do this last so that
4045 * the FDW doesn't have to be involved. (Note that parts of fdw_exprs or
4046 * fdw_recheck_quals could have come from join clauses, so doing this
4047 * beforehand on the scan_clauses wouldn't work.) We assume
4048 * fdw_scan_tlist contains no such variables.
4049 */
4050 if (best_path->path.param_info)
4051 {
4052 scan_plan->scan.plan.qual = (List *)
4053 replace_nestloop_params(root, (Node *) scan_plan->scan.plan.qual);
4054 scan_plan->fdw_exprs = (List *)
4055 replace_nestloop_params(root, (Node *) scan_plan->fdw_exprs);
4056 scan_plan->fdw_recheck_quals = (List *)
4058 (Node *) scan_plan->fdw_recheck_quals);
4059 }
4060
4061 /*
4062 * If rel is a base relation, detect whether any system columns are
4063 * requested from the rel. (If rel is a join relation, rel->relid will be
4064 * 0, but there can be no Var with relid 0 in the rel's targetlist or the
4065 * restriction clauses, so we skip this in that case. Note that any such
4066 * columns in base relations that were joined are assumed to be contained
4067 * in fdw_scan_tlist.) This is a bit of a kluge and might go away
4068 * someday, so we intentionally leave it out of the API presented to FDWs.
4069 */
4070 scan_plan->fsSystemCol = false;
4071 if (scan_relid > 0)
4072 {
4073 Bitmapset *attrs_used = NULL;
4074 ListCell *lc;
4075 int i;
4076
4077 /*
4078 * First, examine all the attributes needed for joins or final output.
4079 * Note: we must look at rel's targetlist, not the attr_needed data,
4080 * because attr_needed isn't computed for inheritance child rels.
4081 */
4082 pull_varattnos((Node *) rel->reltarget->exprs, scan_relid, &attrs_used);
4083
4084 /* Add all the attributes used by restriction clauses. */
4085 foreach(lc, rel->baserestrictinfo)
4086 {
4087 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
4088
4089 pull_varattnos((Node *) rinfo->clause, scan_relid, &attrs_used);
4090 }
4091
4092 /* Now, are any system columns requested from rel? */
4093 for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
4094 {
4096 {
4097 scan_plan->fsSystemCol = true;
4098 break;
4099 }
4100 }
4101
4102 bms_free(attrs_used);
4103 }
4104
4105 return scan_plan;
4106}
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:77
@ RELOPT_UPPER_REL
Definition: pathnodes.h:887
#define InvalidOid
Definition: postgres_ext.h:37
Path * fdw_outerpath
Definition: pathnodes.h:2117
Oid checkAsUser
Definition: plannodes.h:877
Oid fs_server
Definition: plannodes.h:879
List * fdw_exprs
Definition: plannodes.h:881
bool fsSystemCol
Definition: plannodes.h:893
Bitmapset * fs_relids
Definition: plannodes.h:889
Bitmapset * fs_base_relids
Definition: plannodes.h:891
List * fdw_recheck_quals
Definition: plannodes.h:887
List * exprs
Definition: pathnodes.h:1779
bool useridiscurrent
Definition: pathnodes.h:1019
struct PathTarget * reltarget
Definition: pathnodes.h:949
Index relid
Definition: pathnodes.h:973
RelOptKind reloptkind
Definition: pathnodes.h:921
Oid serverid
Definition: pathnodes.h:1015
RTEKind rtekind
Definition: pathnodes.h:977
#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(), 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 3597 of file createplan.c.

3599{
3600 FunctionScan *scan_plan;
3601 Index scan_relid = best_path->parent->relid;
3602 RangeTblEntry *rte;
3603 List *functions;
3604
3605 /* it should be a function base rel... */
3606 Assert(scan_relid > 0);
3607 rte = planner_rt_fetch(scan_relid, root);
3608 Assert(rte->rtekind == RTE_FUNCTION);
3609 functions = rte->functions;
3610
3611 /* Sort clauses into best execution order */
3612 scan_clauses = order_qual_clauses(root, scan_clauses);
3613
3614 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3615 scan_clauses = extract_actual_clauses(scan_clauses, false);
3616
3617 /* Replace any outer-relation variables with nestloop params */
3618 if (best_path->param_info)
3619 {
3620 scan_clauses = (List *)
3621 replace_nestloop_params(root, (Node *) scan_clauses);
3622 /* The function expressions could contain nestloop params, too */
3624 }
3625
3626 scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
3628
3629 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3630
3631 return scan_plan;
3632}
static FunctionScan * make_functionscan(List *qptlist, List *qpqual, Index scanrelid, List *functions, bool funcordinality)
Definition: createplan.c:5676
@ RTE_FUNCTION
Definition: parsenodes.h:1046
static const struct fns functions
Definition: regcomp.c:358
bool funcordinality
Definition: parsenodes.h:1210
List * functions
Definition: parsenodes.h:1208

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

1803{
1804 GatherMerge *gm_plan;
1805 Plan *subplan;
1806 List *pathkeys = best_path->path.pathkeys;
1807 List *tlist = build_path_tlist(root, &best_path->path);
1808
1809 /* As with Gather, project away columns in the workers. */
1810 subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1811
1812 /* Create a shell for a GatherMerge plan. */
1813 gm_plan = makeNode(GatherMerge);
1814 gm_plan->plan.targetlist = tlist;
1815 gm_plan->num_workers = best_path->num_workers;
1816 copy_generic_path_info(&gm_plan->plan, &best_path->path);
1817
1818 /* Assign the rescan Param. */
1820
1821 /* Gather Merge is pointless with no pathkeys; use Gather instead. */
1822 Assert(pathkeys != NIL);
1823
1824 /* Compute sort column info, and adjust subplan's tlist as needed */
1825 subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1826 best_path->subpath->parent->relids,
1827 gm_plan->sortColIdx,
1828 false,
1829 &gm_plan->numCols,
1830 &gm_plan->sortColIdx,
1831 &gm_plan->sortOperators,
1832 &gm_plan->collations,
1833 &gm_plan->nullsFirst);
1834
1835 /*
1836 * All gather merge paths should have already guaranteed the necessary
1837 * sort order. See create_gather_merge_path.
1838 */
1839 Assert(pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys));
1840
1841 /* Now insert the subplan under GatherMerge. */
1842 gm_plan->plan.lefttree = subplan;
1843
1844 /* use parallel mode for parallel plans. */
1845 root->glob->parallelModeNeeded = true;
1846
1847 return gm_plan;
1848}
int assign_special_exec_param(PlannerInfo *root)
Definition: paramassign.c:754
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:343
int rescan_param
Definition: plannodes.h:1366
int num_workers
Definition: plannodes.h:1363
struct Plan * lefttree
Definition: plannodes.h:233

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

1765{
1766 Gather *gather_plan;
1767 Plan *subplan;
1768 List *tlist;
1769
1770 /*
1771 * Push projection down to the child node. That way, the projection work
1772 * is parallelized, and there can be no system columns in the result (they
1773 * can't travel through a tuple queue because it uses MinimalTuple
1774 * representation).
1775 */
1776 subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
1777
1778 tlist = build_path_tlist(root, &best_path->path);
1779
1780 gather_plan = make_gather(tlist,
1781 NIL,
1782 best_path->num_workers,
1784 best_path->single_copy,
1785 subplan);
1786
1787 copy_generic_path_info(&gather_plan->plan, &best_path->path);
1788
1789 /* use parallel mode for parallel plans. */
1790 root->glob->parallelModeNeeded = true;
1791
1792 return gather_plan;
1793}
static Gather * make_gather(List *qptlist, List *qpqual, int nworkers, int rescan_param, bool single_copy, Plan *subplan)
Definition: createplan.c:6782
bool single_copy
Definition: pathnodes.h:2264
Path * subpath
Definition: pathnodes.h:2263
int num_workers
Definition: pathnodes.h:2265
Plan plan
Definition: plannodes.h:1337

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

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

References Assert(), build_path_tlist(), copy_plan_costsize(), IsA, Plan::lefttree, make_gating_result(), Path::parallel_safe, Plan::parallel_safe, Result::plan, plan, Result::relids, Result::result_type, and root.

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

2081{
2082 Group *plan;
2083 Plan *subplan;
2084 List *tlist;
2085 List *quals;
2086
2087 /*
2088 * Group can project, so no need to be terribly picky about child tlist,
2089 * but we do need grouping columns to be available
2090 */
2091 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2092
2093 tlist = build_path_tlist(root, &best_path->path);
2094
2095 quals = order_qual_clauses(root, best_path->qual);
2096
2097 plan = make_group(tlist,
2098 quals,
2099 list_length(best_path->groupClause),
2101 subplan->targetlist),
2104 subplan->targetlist),
2105 subplan);
2106
2107 copy_generic_path_info(&plan->plan, (Path *) best_path);
2108
2109 return plan;
2110}
static Group * make_group(List *tlist, List *qual, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations, Plan *lefttree)
Definition: createplan.c:6644
List * qual
Definition: pathnodes.h:2457
List * groupClause
Definition: pathnodes.h:2456
Path * subpath
Definition: pathnodes.h:2455
Path path
Definition: pathnodes.h:2454

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

1632{
1633 Result *plan;
1634 List *tlist;
1635 List *quals;
1636
1637 tlist = build_path_tlist(root, &best_path->path);
1638
1639 /* best_path->quals is just bare clauses */
1640 quals = order_qual_clauses(root, best_path->quals);
1641
1642 plan = make_one_row_result(tlist, (Node *) quals, best_path->path.parent);
1643
1644 copy_generic_path_info(&plan->plan, (Path *) best_path);
1645
1646 return plan;
1647}

References build_path_tlist(), copy_generic_path_info(), make_one_row_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 2239 of file createplan.c.

2240{
2241 Agg *plan;
2242 Plan *subplan;
2243 List *rollups = best_path->rollups;
2244 AttrNumber *grouping_map;
2245 int maxref;
2246 List *chain;
2247 ListCell *lc;
2248
2249 /* Shouldn't get here without grouping sets */
2250 Assert(root->parse->groupingSets);
2251 Assert(rollups != NIL);
2252
2253 /*
2254 * Agg can project, so no need to be terribly picky about child tlist, but
2255 * we do need grouping columns to be available
2256 */
2257 subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
2258
2259 /*
2260 * Compute the mapping from tleSortGroupRef to column index in the child's
2261 * tlist. First, identify max SortGroupRef in groupClause, for array
2262 * sizing.
2263 */
2264 maxref = 0;
2265 foreach(lc, root->processed_groupClause)
2266 {
2268
2269 if (gc->tleSortGroupRef > maxref)
2270 maxref = gc->tleSortGroupRef;
2271 }
2272
2273 grouping_map = (AttrNumber *) palloc0((maxref + 1) * sizeof(AttrNumber));
2274
2275 /* Now look up the column numbers in the child's tlist */
2276 foreach(lc, root->processed_groupClause)
2277 {
2279 TargetEntry *tle = get_sortgroupclause_tle(gc, subplan->targetlist);
2280
2281 grouping_map[gc->tleSortGroupRef] = tle->resno;
2282 }
2283
2284 /*
2285 * During setrefs.c, we'll need the grouping_map to fix up the cols lists
2286 * in GroupingFunc nodes. Save it for setrefs.c to use.
2287 */
2288 Assert(root->grouping_map == NULL);
2289 root->grouping_map = grouping_map;
2290
2291 /*
2292 * Generate the side nodes that describe the other sort and group
2293 * operations besides the top one. Note that we don't worry about putting
2294 * accurate cost estimates in the side nodes; only the topmost Agg node's
2295 * costs will be shown by EXPLAIN.
2296 */
2297 chain = NIL;
2298 if (list_length(rollups) > 1)
2299 {
2300 bool is_first_sort = ((RollupData *) linitial(rollups))->is_hashed;
2301
2302 for_each_from(lc, rollups, 1)
2303 {
2304 RollupData *rollup = lfirst(lc);
2305 AttrNumber *new_grpColIdx;
2306 Plan *sort_plan = NULL;
2307 Plan *agg_plan;
2308 AggStrategy strat;
2309
2310 new_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2311
2312 if (!rollup->is_hashed && !is_first_sort)
2313 {
2314 sort_plan = (Plan *)
2316 new_grpColIdx,
2317 subplan);
2318 }
2319
2320 if (!rollup->is_hashed)
2321 is_first_sort = false;
2322
2323 if (rollup->is_hashed)
2324 strat = AGG_HASHED;
2325 else if (linitial(rollup->gsets) == NIL)
2326 strat = AGG_PLAIN;
2327 else
2328 strat = AGG_SORTED;
2329
2330 agg_plan = (Plan *) make_agg(NIL,
2331 NIL,
2332 strat,
2334 list_length((List *) linitial(rollup->gsets)),
2335 new_grpColIdx,
2338 rollup->gsets,
2339 NIL,
2340 rollup->numGroups,
2341 best_path->transitionSpace,
2342 sort_plan);
2343
2344 /*
2345 * Remove stuff we don't need to avoid bloating debug output.
2346 */
2347 if (sort_plan)
2348 {
2349 sort_plan->targetlist = NIL;
2350 sort_plan->lefttree = NULL;
2351 }
2352
2353 chain = lappend(chain, agg_plan);
2354 }
2355 }
2356
2357 /*
2358 * Now make the real Agg node
2359 */
2360 {
2361 RollupData *rollup = linitial(rollups);
2362 AttrNumber *top_grpColIdx;
2363 int numGroupCols;
2364
2365 top_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
2366
2367 numGroupCols = list_length((List *) linitial(rollup->gsets));
2368
2369 plan = make_agg(build_path_tlist(root, &best_path->path),
2370 best_path->qual,
2371 best_path->aggstrategy,
2373 numGroupCols,
2374 top_grpColIdx,
2377 rollup->gsets,
2378 chain,
2379 rollup->numGroups,
2380 best_path->transitionSpace,
2381 subplan);
2382
2383 /* Copy cost data from Path to Plan */
2384 copy_generic_path_info(&plan->plan, &best_path->path);
2385 }
2386
2387 return (Plan *) plan;
2388}
static AttrNumber * remap_groupColIdx(PlannerInfo *root, List *groupClause)
Definition: createplan.c:2201
static Sort * make_sort_from_groupcols(List *groupcls, AttrNumber *grpColIdx, Plan *lefttree)
Definition: createplan.c:6438
void * palloc0(Size size)
Definition: mcxt.c:1395
AggStrategy
Definition: nodes.h:363
@ AGG_SORTED
Definition: nodes.h:365
@ AGG_HASHED
Definition: nodes.h:366
@ AGG_PLAIN
Definition: nodes.h:364
@ AGGSPLIT_SIMPLE
Definition: nodes.h:387
#define for_each_from(cell, lst, N)
Definition: pg_list.h:414
uint64 transitionSpace
Definition: pathnodes.h:2529
AggStrategy aggstrategy
Definition: pathnodes.h:2526
Cardinality numGroups
Definition: pathnodes.h:2513
List * groupClause
Definition: pathnodes.h:2510
List * gsets
Definition: pathnodes.h:2511
bool is_hashed
Definition: pathnodes.h:2515
Index tleSortGroupRef
Definition: parsenodes.h:1469
AttrNumber resno
Definition: primnodes.h:2241
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 4691 of file createplan.c.

4693{
4694 HashJoin *join_plan;
4695 Hash *hash_plan;
4696 Plan *outer_plan;
4697 Plan *inner_plan;
4698 List *tlist = build_path_tlist(root, &best_path->jpath.path);
4699 List *joinclauses;
4700 List *otherclauses;
4701 List *hashclauses;
4702 List *hashoperators = NIL;
4703 List *hashcollations = NIL;
4704 List *inner_hashkeys = NIL;
4705 List *outer_hashkeys = NIL;
4706 Oid skewTable = InvalidOid;
4707 AttrNumber skewColumn = InvalidAttrNumber;
4708 bool skewInherit = false;
4709 ListCell *lc;
4710
4711 /*
4712 * HashJoin can project, so we don't have to demand exact tlists from the
4713 * inputs. However, it's best to request a small tlist from the inner
4714 * side, so that we aren't storing more data than necessary. Likewise, if
4715 * we anticipate batching, request a small tlist from the outer side so
4716 * that we don't put extra data in the outer batch files.
4717 */
4718 outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4719 (best_path->num_batches > 1) ? CP_SMALL_TLIST : 0);
4720
4721 inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4723
4724 /* Sort join qual clauses into best execution order */
4725 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4726 /* There's no point in sorting the hash clauses ... */
4727
4728 /* Get the join qual clauses (in plain expression form) */
4729 /* Any pseudoconstant clauses are ignored here */
4730 if (IS_OUTER_JOIN(best_path->jpath.jointype))
4731 {
4732 extract_actual_join_clauses(joinclauses,
4733 best_path->jpath.path.parent->relids,
4734 &joinclauses, &otherclauses);
4735 }
4736 else
4737 {
4738 /* We can treat all clauses alike for an inner join */
4739 joinclauses = extract_actual_clauses(joinclauses, false);
4740 otherclauses = NIL;
4741 }
4742
4743 /*
4744 * Remove the hashclauses from the list of join qual clauses, leaving the
4745 * list of quals that must be checked as qpquals.
4746 */
4747 hashclauses = get_actual_clauses(best_path->path_hashclauses);
4748 joinclauses = list_difference(joinclauses, hashclauses);
4749
4750 /*
4751 * Replace any outer-relation variables with nestloop params. There
4752 * should not be any in the hashclauses.
4753 */
4754 if (best_path->jpath.path.param_info)
4755 {
4756 joinclauses = (List *)
4757 replace_nestloop_params(root, (Node *) joinclauses);
4758 otherclauses = (List *)
4759 replace_nestloop_params(root, (Node *) otherclauses);
4760 }
4761
4762 /*
4763 * Rearrange hashclauses, if needed, so that the outer variable is always
4764 * on the left.
4765 */
4766 hashclauses = get_switched_clauses(best_path->path_hashclauses,
4767 best_path->jpath.outerjoinpath->parent->relids);
4768
4769 /*
4770 * If there is a single join clause and we can identify the outer variable
4771 * as a simple column reference, supply its identity for possible use in
4772 * skew optimization. (Note: in principle we could do skew optimization
4773 * with multiple join clauses, but we'd have to be able to determine the
4774 * most common combinations of outer values, which we don't currently have
4775 * enough stats for.)
4776 */
4777 if (list_length(hashclauses) == 1)
4778 {
4779 OpExpr *clause = (OpExpr *) linitial(hashclauses);
4780 Node *node;
4781
4782 Assert(is_opclause(clause));
4783 node = (Node *) linitial(clause->args);
4784 if (IsA(node, RelabelType))
4785 node = (Node *) ((RelabelType *) node)->arg;
4786 if (IsA(node, Var))
4787 {
4788 Var *var = (Var *) node;
4789 RangeTblEntry *rte;
4790
4791 rte = root->simple_rte_array[var->varno];
4792 if (rte->rtekind == RTE_RELATION)
4793 {
4794 skewTable = rte->relid;
4795 skewColumn = var->varattno;
4796 skewInherit = rte->inh;
4797 }
4798 }
4799 }
4800
4801 /*
4802 * Collect hash related information. The hashed expressions are
4803 * deconstructed into outer/inner expressions, so they can be computed
4804 * separately (inner expressions are used to build the hashtable via Hash,
4805 * outer expressions to perform lookups of tuples from HashJoin's outer
4806 * plan in the hashtable). Also collect operator information necessary to
4807 * build the hashtable.
4808 */
4809 foreach(lc, hashclauses)
4810 {
4811 OpExpr *hclause = lfirst_node(OpExpr, lc);
4812
4813 hashoperators = lappend_oid(hashoperators, hclause->opno);
4814 hashcollations = lappend_oid(hashcollations, hclause->inputcollid);
4815 outer_hashkeys = lappend(outer_hashkeys, linitial(hclause->args));
4816 inner_hashkeys = lappend(inner_hashkeys, lsecond(hclause->args));
4817 }
4818
4819 /*
4820 * Build the hash node and hash join node.
4821 */
4822 hash_plan = make_hash(inner_plan,
4823 inner_hashkeys,
4824 skewTable,
4825 skewColumn,
4826 skewInherit);
4827
4828 /*
4829 * Set Hash node's startup & total costs equal to total cost of input
4830 * plan; this only affects EXPLAIN display not decisions.
4831 */
4832 copy_plan_costsize(&hash_plan->plan, inner_plan);
4833 hash_plan->plan.startup_cost = hash_plan->plan.total_cost;
4834
4835 /*
4836 * If parallel-aware, the executor will also need an estimate of the total
4837 * number of rows expected from all participants so that it can size the
4838 * shared hash table.
4839 */
4840 if (best_path->jpath.path.parallel_aware)
4841 {
4842 hash_plan->plan.parallel_aware = true;
4843 hash_plan->rows_total = best_path->inner_rows_total;
4844 }
4845
4846 join_plan = make_hashjoin(tlist,
4847 joinclauses,
4848 otherclauses,
4849 hashclauses,
4850 hashoperators,
4851 hashcollations,
4852 outer_hashkeys,
4853 outer_plan,
4854 (Plan *) hash_plan,
4855 best_path->jpath.jointype,
4856 best_path->jpath.inner_unique);
4857
4858 copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4859
4860 return join_plan;
4861}
#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:5946
static List * get_switched_clauses(List *clauses, Relids outerrelids)
Definition: createplan.c:5181
static Hash * make_hash(Plan *lefttree, List *hashkeys, Oid skewTable, AttrNumber skewColumn, bool skewInherit)
Definition: createplan.c:5977
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:348
#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:1045
List * path_hashclauses
Definition: pathnodes.h:2381
Cardinality inner_rows_total
Definition: pathnodes.h:2383
int num_batches
Definition: pathnodes.h:2382
JoinPath jpath
Definition: pathnodes.h:2380
Cardinality rows_total
Definition: plannodes.h:1418
Plan plan
Definition: plannodes.h:1402
Path * outerjoinpath
Definition: pathnodes.h:2295
Path * innerjoinpath
Definition: pathnodes.h:2296
JoinType jointype
Definition: pathnodes.h:2290
bool inner_unique
Definition: pathnodes.h:2292
List * joinrestrictinfo
Definition: pathnodes.h:2298
Oid opno
Definition: primnodes.h:850
List * args
Definition: primnodes.h:868
bool parallel_aware
Definition: plannodes.h:213
Definition: primnodes.h:262
AttrNumber varattno
Definition: primnodes.h:274
int varno
Definition: primnodes.h:269

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

2055{
2057 Plan *subplan;
2058
2059 /* See comments in create_sort_plan() above */
2060 subplan = create_plan_recurse(root, best_path->spath.subpath,
2061 flags | CP_SMALL_TLIST);
2063 best_path->spath.path.pathkeys,
2064 IS_OTHER_REL(best_path->spath.subpath->parent) ?
2065 best_path->spath.path.parent->relids : NULL,
2066 best_path->nPresortedCols);
2067
2068 copy_generic_path_info(&plan->sort.plan, (Path *) best_path);
2069
2070 return plan;
2071}
static IncrementalSort * make_incrementalsort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids, int nPresortedCols)
Definition: createplan.c:6355
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:910
Path path
Definition: pathnodes.h:2428
Path * subpath
Definition: pathnodes.h:2429

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

2847{
2848 Scan *scan_plan;
2849 List *indexclauses = best_path->indexclauses;
2850 List *indexorderbys = best_path->indexorderbys;
2851 Index baserelid = best_path->path.parent->relid;
2852 IndexOptInfo *indexinfo = best_path->indexinfo;
2853 Oid indexoid = indexinfo->indexoid;
2854 List *qpqual;
2855 List *stripped_indexquals;
2856 List *fixed_indexquals;
2857 List *fixed_indexorderbys;
2858 List *indexorderbyops = NIL;
2859 ListCell *l;
2860
2861 /* it should be a base rel... */
2862 Assert(baserelid > 0);
2863 Assert(best_path->path.parent->rtekind == RTE_RELATION);
2864 /* check the scan direction is valid */
2866 best_path->indexscandir == BackwardScanDirection);
2867
2868 /*
2869 * Extract the index qual expressions (stripped of RestrictInfos) from the
2870 * IndexClauses list, and prepare a copy with index Vars substituted for
2871 * table Vars. (This step also does replace_nestloop_params on the
2872 * fixed_indexquals.)
2873 */
2874 fix_indexqual_references(root, best_path,
2875 &stripped_indexquals,
2876 &fixed_indexquals);
2877
2878 /*
2879 * Likewise fix up index attr references in the ORDER BY expressions.
2880 */
2881 fixed_indexorderbys = fix_indexorderby_references(root, best_path);
2882
2883 /*
2884 * The qpqual list must contain all restrictions not automatically handled
2885 * by the index, other than pseudoconstant clauses which will be handled
2886 * by a separate gating plan node. All the predicates in the indexquals
2887 * will be checked (either by the index itself, or by nodeIndexscan.c),
2888 * but if there are any "special" operators involved then they must be
2889 * included in qpqual. The upshot is that qpqual must contain
2890 * scan_clauses minus whatever appears in indexquals.
2891 *
2892 * is_redundant_with_indexclauses() detects cases where a scan clause is
2893 * present in the indexclauses list or is generated from the same
2894 * EquivalenceClass as some indexclause, and is therefore redundant with
2895 * it, though not equal. (The latter happens when indxpath.c prefers a
2896 * different derived equality than what generate_join_implied_equalities
2897 * picked for a parameterized scan's ppi_clauses.) Note that it will not
2898 * match to lossy index clauses, which is critical because we have to
2899 * include the original clause in qpqual in that case.
2900 *
2901 * In some situations (particularly with OR'd index conditions) we may
2902 * have scan_clauses that are not equal to, but are logically implied by,
2903 * the index quals; so we also try a predicate_implied_by() check to see
2904 * if we can discard quals that way. (predicate_implied_by assumes its
2905 * first input contains only immutable functions, so we have to check
2906 * that.)
2907 *
2908 * Note: if you change this bit of code you should also look at
2909 * extract_nonindex_conditions() in costsize.c.
2910 */
2911 qpqual = NIL;
2912 foreach(l, scan_clauses)
2913 {
2915
2916 if (rinfo->pseudoconstant)
2917 continue; /* we may drop pseudoconstants here */
2918 if (is_redundant_with_indexclauses(rinfo, indexclauses))
2919 continue; /* dup or derived from same EquivalenceClass */
2920 if (!contain_mutable_functions((Node *) rinfo->clause) &&
2921 predicate_implied_by(list_make1(rinfo->clause), stripped_indexquals,
2922 false))
2923 continue; /* provably implied by indexquals */
2924 qpqual = lappend(qpqual, rinfo);
2925 }
2926
2927 /* Sort clauses into best execution order */
2928 qpqual = order_qual_clauses(root, qpqual);
2929
2930 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2931 qpqual = extract_actual_clauses(qpqual, false);
2932
2933 /*
2934 * We have to replace any outer-relation variables with nestloop params in
2935 * the indexqualorig, qpqual, and indexorderbyorig expressions. A bit
2936 * annoying to have to do this separately from the processing in
2937 * fix_indexqual_references --- rethink this when generalizing the inner
2938 * indexscan support. But note we can't really do this earlier because
2939 * it'd break the comparisons to predicates above ... (or would it? Those
2940 * wouldn't have outer refs)
2941 */
2942 if (best_path->path.param_info)
2943 {
2944 stripped_indexquals = (List *)
2945 replace_nestloop_params(root, (Node *) stripped_indexquals);
2946 qpqual = (List *)
2947 replace_nestloop_params(root, (Node *) qpqual);
2948 indexorderbys = (List *)
2949 replace_nestloop_params(root, (Node *) indexorderbys);
2950 }
2951
2952 /*
2953 * If there are ORDER BY expressions, look up the sort operators for their
2954 * result datatypes.
2955 */
2956 if (indexorderbys)
2957 {
2958 ListCell *pathkeyCell,
2959 *exprCell;
2960
2961 /*
2962 * PathKey contains OID of the btree opfamily we're sorting by, but
2963 * that's not quite enough because we need the expression's datatype
2964 * to look up the sort operator in the operator family.
2965 */
2966 Assert(list_length(best_path->path.pathkeys) == list_length(indexorderbys));
2967 forboth(pathkeyCell, best_path->path.pathkeys, exprCell, indexorderbys)
2968 {
2969 PathKey *pathkey = (PathKey *) lfirst(pathkeyCell);
2970 Node *expr = (Node *) lfirst(exprCell);
2971 Oid exprtype = exprType(expr);
2972 Oid sortop;
2973
2974 /* Get sort operator from opfamily */
2976 exprtype,
2977 exprtype,
2978 pathkey->pk_cmptype);
2979 if (!OidIsValid(sortop))
2980 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
2981 pathkey->pk_cmptype, exprtype, exprtype, pathkey->pk_opfamily);
2982 indexorderbyops = lappend_oid(indexorderbyops, sortop);
2983 }
2984 }
2985
2986 /*
2987 * For an index-only scan, we must mark indextlist entries as resjunk if
2988 * they are columns that the index AM can't return; this cues setrefs.c to
2989 * not generate references to those columns.
2990 */
2991 if (indexonly)
2992 {
2993 int i = 0;
2994
2995 foreach(l, indexinfo->indextlist)
2996 {
2997 TargetEntry *indextle = (TargetEntry *) lfirst(l);
2998
2999 indextle->resjunk = !indexinfo->canreturn[i];
3000 i++;
3001 }
3002 }
3003
3004 /* Finally ready to build the plan node */
3005 if (indexonly)
3006 scan_plan = (Scan *) make_indexonlyscan(tlist,
3007 qpqual,
3008 baserelid,
3009 indexoid,
3010 fixed_indexquals,
3011 stripped_indexquals,
3012 fixed_indexorderbys,
3013 indexinfo->indextlist,
3014 best_path->indexscandir);
3015 else
3016 scan_plan = (Scan *) make_indexscan(tlist,
3017 qpqual,
3018 baserelid,
3019 indexoid,
3020 fixed_indexquals,
3021 stripped_indexquals,
3022 fixed_indexorderbys,
3023 indexorderbys,
3024 indexorderbyops,
3025 best_path->indexscandir);
3026
3027 copy_generic_path_info(&scan_plan->plan, &best_path->path);
3028
3029 return scan_plan;
3030}
#define OidIsValid(objectId)
Definition: c.h:778
static void fix_indexqual_references(PlannerInfo *root, IndexPath *index_path, List **stripped_indexquals_p, List **fixed_indexquals_p)
Definition: createplan.c:4965
static List * fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path)
Definition: createplan.c:5006
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:5548
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:5517
bool is_redundant_with_indexclauses(RestrictInfo *rinfo, List *indexclauses)
Definition: equivclass.c:3577
Oid get_opfamily_member_for_cmptype(Oid opfamily, Oid lefttype, Oid righttype, CompareType cmptype)
Definition: lsyscache.c:197
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:1313
ScanDirection indexscandir
Definition: pathnodes.h:1961
List * indexorderbys
Definition: pathnodes.h:1959
CompareType pk_cmptype
Definition: pathnodes.h:1716
Oid pk_opfamily
Definition: pathnodes.h:1715

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_for_cmptype(), 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_cmptype, PathKey::pk_opfamily, 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 1074 of file createplan.c.

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

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

2693{
2694 Limit *plan;
2695 Plan *subplan;
2696 int numUniqkeys = 0;
2697 AttrNumber *uniqColIdx = NULL;
2698 Oid *uniqOperators = NULL;
2699 Oid *uniqCollations = NULL;
2700
2701 /* Limit doesn't project, so tlist requirements pass through */
2702 subplan = create_plan_recurse(root, best_path->subpath, flags);
2703
2704 /* Extract information necessary for comparing rows for WITH TIES. */
2705 if (best_path->limitOption == LIMIT_OPTION_WITH_TIES)
2706 {
2707 Query *parse = root->parse;
2708 ListCell *l;
2709
2710 numUniqkeys = list_length(parse->sortClause);
2711 uniqColIdx = (AttrNumber *) palloc(numUniqkeys * sizeof(AttrNumber));
2712 uniqOperators = (Oid *) palloc(numUniqkeys * sizeof(Oid));
2713 uniqCollations = (Oid *) palloc(numUniqkeys * sizeof(Oid));
2714
2715 numUniqkeys = 0;
2716 foreach(l, parse->sortClause)
2717 {
2718 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
2719 TargetEntry *tle = get_sortgroupclause_tle(sortcl, parse->targetList);
2720
2721 uniqColIdx[numUniqkeys] = tle->resno;
2722 uniqOperators[numUniqkeys] = sortcl->eqop;
2723 uniqCollations[numUniqkeys] = exprCollation((Node *) tle->expr);
2724 numUniqkeys++;
2725 }
2726 }
2727
2728 plan = make_limit(subplan,
2729 best_path->limitOffset,
2730 best_path->limitCount,
2731 best_path->limitOption,
2732 numUniqkeys, uniqColIdx, uniqOperators, uniqCollations);
2733
2734 copy_generic_path_info(&plan->plan, (Path *) best_path);
2735
2736 return plan;
2737}
Limit * make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount, LimitOption limitOption, int uniqNumCols, AttrNumber *uniqColIdx, Oid *uniqOperators, Oid *uniqCollations)
Definition: createplan.c:6893
void * palloc(Size size)
Definition: mcxt.c:1365
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:821
@ LIMIT_OPTION_WITH_TIES
Definition: nodes.h:442
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:2628
LimitOption limitOption
Definition: pathnodes.h:2631
Node * limitOffset
Definition: pathnodes.h:2629
Node * limitCount
Definition: pathnodes.h:2630
Expr * expr
Definition: primnodes.h:2239

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

2631{
2632 LockRows *plan;
2633 Plan *subplan;
2634
2635 /* LockRows doesn't project, so tlist requirements pass through */
2636 subplan = create_plan_recurse(root, best_path->subpath, flags);
2637
2638 plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
2639
2640 copy_generic_path_info(&plan->plan, (Path *) best_path);
2641
2642 return plan;
2643}
static LockRows * make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
Definition: createplan.c:6872
Path * subpath
Definition: pathnodes.h:2589
List * rowMarks
Definition: pathnodes.h:2590

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

1683{
1684 Material *plan;
1685 Plan *subplan;
1686
1687 /*
1688 * We don't want any excess columns in the materialized tuples, so request
1689 * a smaller tlist. Otherwise, since Material doesn't project, tlist
1690 * requirements pass through.
1691 */
1692 subplan = create_plan_recurse(root, best_path->subpath,
1693 flags | CP_SMALL_TLIST);
1694
1695 plan = make_material(subplan);
1696
1697 copy_generic_path_info(&plan->plan, (Path *) best_path);
1698
1699 return plan;
1700}
static Material * make_material(Plan *lefttree)
Definition: createplan.c:6479
Path * subpath
Definition: pathnodes.h:2229

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

1711{
1712 Memoize *plan;
1713 Bitmapset *keyparamids;
1714 Plan *subplan;
1715 Oid *operators;
1716 Oid *collations;
1717 List *param_exprs = NIL;
1718 ListCell *lc;
1719 ListCell *lc2;
1720 int nkeys;
1721 int i;
1722
1723 subplan = create_plan_recurse(root, best_path->subpath,
1724 flags | CP_SMALL_TLIST);
1725
1726 param_exprs = (List *) replace_nestloop_params(root, (Node *)
1727 best_path->param_exprs);
1728
1729 nkeys = list_length(param_exprs);
1730 Assert(nkeys > 0);
1731 operators = palloc(nkeys * sizeof(Oid));
1732 collations = palloc(nkeys * sizeof(Oid));
1733
1734 i = 0;
1735 forboth(lc, param_exprs, lc2, best_path->hash_operators)
1736 {
1737 Expr *param_expr = (Expr *) lfirst(lc);
1738 Oid opno = lfirst_oid(lc2);
1739
1740 operators[i] = opno;
1741 collations[i] = exprCollation((Node *) param_expr);
1742 i++;
1743 }
1744
1745 keyparamids = pull_paramids((Expr *) param_exprs);
1746
1747 plan = make_memoize(subplan, operators, collations, param_exprs,
1748 best_path->singlerow, best_path->binary_mode,
1749 best_path->est_entries, keyparamids, best_path->est_calls,
1750 best_path->est_unique_keys, best_path->est_hit_ratio);
1751
1752 copy_generic_path_info(&plan->plan, (Path *) best_path);
1753
1754 return plan;
1755}
Bitmapset * pull_paramids(Expr *expr)
Definition: clauses.c:5521
static Memoize * make_memoize(Plan *lefttree, Oid *hashoperators, Oid *collations, List *param_exprs, bool singlerow, bool binary_mode, uint32 est_entries, Bitmapset *keyparamids, Cardinality est_calls, Cardinality est_unique_keys, double est_hit_ratio)
Definition: createplan.c:6544
#define lfirst_oid(lc)
Definition: pg_list.h:174
Cardinality est_calls
Definition: pathnodes.h:2250
bool singlerow
Definition: pathnodes.h:2243
List * hash_operators
Definition: pathnodes.h:2241
uint32 est_entries
Definition: pathnodes.h:2247
bool binary_mode
Definition: pathnodes.h:2245
double est_hit_ratio
Definition: pathnodes.h:2252
Cardinality est_unique_keys
Definition: pathnodes.h:2251
Path * subpath
Definition: pathnodes.h:2240
List * param_exprs
Definition: pathnodes.h:2242

References Assert(), MemoizePath::binary_mode, copy_generic_path_info(), CP_SMALL_TLIST, create_plan_recurse(), MemoizePath::est_calls, MemoizePath::est_entries, MemoizePath::est_hit_ratio, MemoizePath::est_unique_keys, 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 1455 of file createplan.c.

1457{
1459 Plan *plan = &node->plan;
1460 List *tlist = build_path_tlist(root, &best_path->path);
1461 int orig_tlist_length = list_length(tlist);
1462 bool tlist_was_changed;
1463 List *pathkeys = best_path->path.pathkeys;
1464 List *subplans = NIL;
1465 ListCell *subpaths;
1466 RelOptInfo *rel = best_path->path.parent;
1467
1468 /*
1469 * We don't have the actual creation of the MergeAppend node split out
1470 * into a separate make_xxx function. This is because we want to run
1471 * prepare_sort_from_pathkeys on it before we do so on the individual
1472 * child plans, to make cross-checking the sort info easier.
1473 */
1474 copy_generic_path_info(plan, (Path *) best_path);
1475 plan->targetlist = tlist;
1476 plan->qual = NIL;
1477 plan->lefttree = NULL;
1478 plan->righttree = NULL;
1479 node->apprelids = rel->relids;
1480
1481 /*
1482 * Compute sort column info, and adjust MergeAppend's tlist as needed.
1483 * Because we pass adjust_tlist_in_place = true, we may ignore the
1484 * function result; it must be the same plan node. However, we then need
1485 * to detect whether any tlist entries were added.
1486 */
1487 (void) prepare_sort_from_pathkeys(plan, pathkeys,
1488 best_path->path.parent->relids,
1489 NULL,
1490 true,
1491 &node->numCols,
1492 &node->sortColIdx,
1493 &node->sortOperators,
1494 &node->collations,
1495 &node->nullsFirst);
1496 tlist_was_changed = (orig_tlist_length != list_length(plan->targetlist));
1497
1498 /*
1499 * Now prepare the child plans. We must apply prepare_sort_from_pathkeys
1500 * even to subplans that don't need an explicit sort, to make sure they
1501 * are returning the same sort key columns the MergeAppend expects.
1502 */
1503 foreach(subpaths, best_path->subpaths)
1504 {
1505 Path *subpath = (Path *) lfirst(subpaths);
1506 Plan *subplan;
1507 int numsortkeys;
1508 AttrNumber *sortColIdx;
1509 Oid *sortOperators;
1510 Oid *collations;
1511 bool *nullsFirst;
1512 int presorted_keys;
1513
1514 /* Build the child plan */
1515 /* Must insist that all children return the same tlist */
1517
1518 /* Compute sort column info, and adjust subplan's tlist as needed */
1519 subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
1520 subpath->parent->relids,
1521 node->sortColIdx,
1522 false,
1523 &numsortkeys,
1524 &sortColIdx,
1525 &sortOperators,
1526 &collations,
1527 &nullsFirst);
1528
1529 /*
1530 * Check that we got the same sort key information. We just Assert
1531 * that the sortops match, since those depend only on the pathkeys;
1532 * but it seems like a good idea to check the sort column numbers
1533 * explicitly, to ensure the tlists really do match up.
1534 */
1535 Assert(numsortkeys == node->numCols);
1536 if (memcmp(sortColIdx, node->sortColIdx,
1537 numsortkeys * sizeof(AttrNumber)) != 0)
1538 elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
1539 Assert(memcmp(sortOperators, node->sortOperators,
1540 numsortkeys * sizeof(Oid)) == 0);
1541 Assert(memcmp(collations, node->collations,
1542 numsortkeys * sizeof(Oid)) == 0);
1543 Assert(memcmp(nullsFirst, node->nullsFirst,
1544 numsortkeys * sizeof(bool)) == 0);
1545
1546 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1547 if (!pathkeys_count_contained_in(pathkeys, subpath->pathkeys,
1548 &presorted_keys))
1549 {
1550 Plan *sort_plan;
1551
1552 /*
1553 * We choose to use incremental sort if it is enabled and there
1554 * are presorted keys; otherwise we use full sort.
1555 */
1556 if (enable_incremental_sort && presorted_keys > 0)
1557 {
1558 sort_plan = (Plan *)
1559 make_incrementalsort(subplan, numsortkeys, presorted_keys,
1560 sortColIdx, sortOperators,
1561 collations, nullsFirst);
1562
1564 (IncrementalSort *) sort_plan,
1565 pathkeys,
1566 best_path->limit_tuples);
1567 }
1568 else
1569 {
1570 sort_plan = (Plan *) make_sort(subplan, numsortkeys,
1571 sortColIdx, sortOperators,
1572 collations, nullsFirst);
1573
1574 label_sort_with_costsize(root, (Sort *) sort_plan,
1575 best_path->limit_tuples);
1576 }
1577
1578 subplan = sort_plan;
1579 }
1580
1581 subplans = lappend(subplans, subplan);
1582 }
1583
1584 /* Set below if we find quals that we can use to run-time prune */
1585 node->part_prune_index = -1;
1586
1587 /*
1588 * If any quals exist, they may be useful to perform further partition
1589 * pruning during execution. Gather information needed by the executor to
1590 * do partition pruning.
1591 */
1593 {
1594 List *prunequal;
1595
1596 prunequal = extract_actual_clauses(rel->baserestrictinfo, false);
1597
1598 /* We don't currently generate any parameterized MergeAppend paths */
1599 Assert(best_path->path.param_info == NULL);
1600
1601 if (prunequal != NIL)
1603 best_path->subpaths,
1604 prunequal);
1605 }
1606
1607 node->mergeplans = subplans;
1608
1609 /*
1610 * If prepare_sort_from_pathkeys added sort columns, but we were told to
1611 * produce either the exact tlist or a narrow tlist, we should get rid of
1612 * the sort columns again. We must inject a projection node to do so.
1613 */
1614 if (tlist_was_changed && (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST)))
1615 {
1616 tlist = list_copy_head(plan->targetlist, orig_tlist_length);
1617 return inject_projection_plan(plan, tlist, plan->parallel_safe);
1618 }
1619 else
1620 return plan;
1621}
Cardinality limit_tuples
Definition: pathnodes.h:2204
int part_prune_index
Definition: plannodes.h:447
Bitmapset * apprelids
Definition: plannodes.h:421
List * mergeplans
Definition: plannodes.h:423

References MergeAppend::apprelids, Assert(), RelOptInfo::baserestrictinfo, build_path_tlist(), copy_generic_path_info(), CP_EXACT_TLIST, CP_SMALL_TLIST, create_plan_recurse(), elog, enable_incremental_sort, enable_partition_pruning, ERROR, extract_actual_clauses(), inject_projection_plan(), label_incrementalsort_with_costsize(), label_sort_with_costsize(), lappend(), lfirst, MergeAppendPath::limit_tuples, list_copy_head(), list_length(), make_incrementalsort(), make_partition_pruneinfo(), make_sort(), makeNode, MergeAppend::mergeplans, NIL, MergeAppend::numCols, MergeAppend::part_prune_index, MergeAppendPath::path, Path::pathkeys, pathkeys_count_contained_in(), MergeAppend::plan, plan, prepare_sort_from_pathkeys(), RelOptInfo::relids, root, 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 4337 of file createplan.c.

4339{
4340 MergeJoin *join_plan;
4341 Plan *outer_plan;
4342 Plan *inner_plan;
4343 List *tlist = build_path_tlist(root, &best_path->jpath.path);
4344 List *joinclauses;
4345 List *otherclauses;
4346 List *mergeclauses;
4347 List *outerpathkeys;
4348 List *innerpathkeys;
4349 int nClauses;
4350 Oid *mergefamilies;
4351 Oid *mergecollations;
4352 bool *mergereversals;
4353 bool *mergenullsfirst;
4354 PathKey *opathkey;
4355 EquivalenceClass *opeclass;
4356 int i;
4357 ListCell *lc;
4358 ListCell *lop;
4359 ListCell *lip;
4360 Path *outer_path = best_path->jpath.outerjoinpath;
4361 Path *inner_path = best_path->jpath.innerjoinpath;
4362
4363 /*
4364 * MergeJoin can project, so we don't have to demand exact tlists from the
4365 * inputs. However, if we're intending to sort an input's result, it's
4366 * best to request a small tlist so we aren't sorting more data than
4367 * necessary.
4368 */
4369 outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
4370 (best_path->outersortkeys != NIL) ? CP_SMALL_TLIST : 0);
4371
4372 inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
4373 (best_path->innersortkeys != NIL) ? CP_SMALL_TLIST : 0);
4374
4375 /* Sort join qual clauses into best execution order */
4376 /* NB: do NOT reorder the mergeclauses */
4377 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
4378
4379 /* Get the join qual clauses (in plain expression form) */
4380 /* Any pseudoconstant clauses are ignored here */
4381 if (IS_OUTER_JOIN(best_path->jpath.jointype))
4382 {
4383 extract_actual_join_clauses(joinclauses,
4384 best_path->jpath.path.parent->relids,
4385 &joinclauses, &otherclauses);
4386 }
4387 else
4388 {
4389 /* We can treat all clauses alike for an inner join */
4390 joinclauses = extract_actual_clauses(joinclauses, false);
4391 otherclauses = NIL;
4392 }
4393
4394 /*
4395 * Remove the mergeclauses from the list of join qual clauses, leaving the
4396 * list of quals that must be checked as qpquals.
4397 */
4398 mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
4399 joinclauses = list_difference(joinclauses, mergeclauses);
4400
4401 /*
4402 * Replace any outer-relation variables with nestloop params. There
4403 * should not be any in the mergeclauses.
4404 */
4405 if (best_path->jpath.path.param_info)
4406 {
4407 joinclauses = (List *)
4408 replace_nestloop_params(root, (Node *) joinclauses);
4409 otherclauses = (List *)
4410 replace_nestloop_params(root, (Node *) otherclauses);
4411 }
4412
4413 /*
4414 * Rearrange mergeclauses, if needed, so that the outer variable is always
4415 * on the left; mark the mergeclause restrictinfos with correct
4416 * outer_is_left status.
4417 */
4418 mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
4419 best_path->jpath.outerjoinpath->parent->relids);
4420
4421 /*
4422 * Create explicit sort nodes for the outer and inner paths if necessary.
4423 */
4424 if (best_path->outersortkeys)
4425 {
4426 Relids outer_relids = outer_path->parent->relids;
4427 Plan *sort_plan;
4428
4429 /*
4430 * We can assert that the outer path is not already ordered
4431 * appropriately for the mergejoin; otherwise, outersortkeys would
4432 * have been set to NIL.
4433 */
4435 outer_path->pathkeys));
4436
4437 /*
4438 * We choose to use incremental sort if it is enabled and there are
4439 * presorted keys; otherwise we use full sort.
4440 */
4441 if (enable_incremental_sort && best_path->outer_presorted_keys > 0)
4442 {
4443 sort_plan = (Plan *)
4445 best_path->outersortkeys,
4446 outer_relids,
4447 best_path->outer_presorted_keys);
4448
4450 (IncrementalSort *) sort_plan,
4451 best_path->outersortkeys,
4452 -1.0);
4453 }
4454 else
4455 {
4456 sort_plan = (Plan *)
4457 make_sort_from_pathkeys(outer_plan,
4458 best_path->outersortkeys,
4459 outer_relids);
4460
4461 label_sort_with_costsize(root, (Sort *) sort_plan, -1.0);
4462 }
4463
4464 outer_plan = sort_plan;
4465 outerpathkeys = best_path->outersortkeys;
4466 }
4467 else
4468 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
4469
4470 if (best_path->innersortkeys)
4471 {
4472 /*
4473 * We do not consider incremental sort for inner path, because
4474 * incremental sort does not support mark/restore.
4475 */
4476
4477 Relids inner_relids = inner_path->parent->relids;
4478 Sort *sort;
4479
4480 /*
4481 * We can assert that the inner path is not already ordered
4482 * appropriately for the mergejoin; otherwise, innersortkeys would
4483 * have been set to NIL.
4484 */
4486 inner_path->pathkeys));
4487
4488 sort = make_sort_from_pathkeys(inner_plan,
4489 best_path->innersortkeys,
4490 inner_relids);
4491
4493 inner_plan = (Plan *) sort;
4494 innerpathkeys = best_path->innersortkeys;
4495 }
4496 else
4497 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
4498
4499 /*
4500 * If specified, add a materialize node to shield the inner plan from the
4501 * need to handle mark/restore.
4502 */
4503 if (best_path->materialize_inner)
4504 {
4505 Plan *matplan = (Plan *) make_material(inner_plan);
4506
4507 /*
4508 * We assume the materialize will not spill to disk, and therefore
4509 * charge just cpu_operator_cost per tuple. (Keep this estimate in
4510 * sync with final_cost_mergejoin.)
4511 */
4512 copy_plan_costsize(matplan, inner_plan);
4513 matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
4514
4515 inner_plan = matplan;
4516 }
4517
4518 /*
4519 * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
4520 * executor. The information is in the pathkeys for the two inputs, but
4521 * we need to be careful about the possibility of mergeclauses sharing a
4522 * pathkey, as well as the possibility that the inner pathkeys are not in
4523 * an order matching the mergeclauses.
4524 */
4525 nClauses = list_length(mergeclauses);
4526 Assert(nClauses == list_length(best_path->path_mergeclauses));
4527 mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
4528 mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
4529 mergereversals = (bool *) palloc(nClauses * sizeof(bool));
4530 mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
4531
4532 opathkey = NULL;
4533 opeclass = NULL;
4534 lop = list_head(outerpathkeys);
4535 lip = list_head(innerpathkeys);
4536 i = 0;
4537 foreach(lc, best_path->path_mergeclauses)
4538 {
4540 EquivalenceClass *oeclass;
4541 EquivalenceClass *ieclass;
4542 PathKey *ipathkey = NULL;
4543 EquivalenceClass *ipeclass = NULL;
4544 bool first_inner_match = false;
4545
4546 /* fetch outer/inner eclass from mergeclause */
4547 if (rinfo->outer_is_left)
4548 {
4549 oeclass = rinfo->left_ec;
4550 ieclass = rinfo->right_ec;
4551 }
4552 else
4553 {
4554 oeclass = rinfo->right_ec;
4555 ieclass = rinfo->left_ec;
4556 }
4557 Assert(oeclass != NULL);
4558 Assert(ieclass != NULL);
4559
4560 /*
4561 * We must identify the pathkey elements associated with this clause
4562 * by matching the eclasses (which should give a unique match, since
4563 * the pathkey lists should be canonical). In typical cases the merge
4564 * clauses are one-to-one with the pathkeys, but when dealing with
4565 * partially redundant query conditions, things are more complicated.
4566 *
4567 * lop and lip reference the first as-yet-unmatched pathkey elements.
4568 * If they're NULL then all pathkey elements have been matched.
4569 *
4570 * The ordering of the outer pathkeys should match the mergeclauses,
4571 * by construction (see find_mergeclauses_for_outer_pathkeys()). There
4572 * could be more than one mergeclause for the same outer pathkey, but
4573 * no pathkey may be entirely skipped over.
4574 */
4575 if (oeclass != opeclass) /* multiple matches are not interesting */
4576 {
4577 /* doesn't match the current opathkey, so must match the next */
4578 if (lop == NULL)
4579 elog(ERROR, "outer pathkeys do not match mergeclauses");
4580 opathkey = (PathKey *) lfirst(lop);
4581 opeclass = opathkey->pk_eclass;
4582 lop = lnext(outerpathkeys, lop);
4583 if (oeclass != opeclass)
4584 elog(ERROR, "outer pathkeys do not match mergeclauses");
4585 }
4586
4587 /*
4588 * The inner pathkeys likewise should not have skipped-over keys, but
4589 * it's possible for a mergeclause to reference some earlier inner
4590 * pathkey if we had redundant pathkeys. For example we might have
4591 * mergeclauses like "o.a = i.x AND o.b = i.y AND o.c = i.x". The
4592 * implied inner ordering is then "ORDER BY x, y, x", but the pathkey
4593 * mechanism drops the second sort by x as redundant, and this code
4594 * must cope.
4595 *
4596 * It's also possible for the implied inner-rel ordering to be like
4597 * "ORDER BY x, y, x DESC". We still drop the second instance of x as
4598 * redundant; but this means that the sort ordering of a redundant
4599 * inner pathkey should not be considered significant. So we must
4600 * detect whether this is the first clause matching an inner pathkey.
4601 */
4602 if (lip)
4603 {
4604 ipathkey = (PathKey *) lfirst(lip);
4605 ipeclass = ipathkey->pk_eclass;
4606 if (ieclass == ipeclass)
4607 {
4608 /* successful first match to this inner pathkey */
4609 lip = lnext(innerpathkeys, lip);
4610 first_inner_match = true;
4611 }
4612 }
4613 if (!first_inner_match)
4614 {
4615 /* redundant clause ... must match something before lip */
4616 ListCell *l2;
4617
4618 foreach(l2, innerpathkeys)
4619 {
4620 if (l2 == lip)
4621 break;
4622 ipathkey = (PathKey *) lfirst(l2);
4623 ipeclass = ipathkey->pk_eclass;
4624 if (ieclass == ipeclass)
4625 break;
4626 }
4627 if (ieclass != ipeclass)
4628 elog(ERROR, "inner pathkeys do not match mergeclauses");
4629 }
4630
4631 /*
4632 * The pathkeys should always match each other as to opfamily and
4633 * collation (which affect equality), but if we're considering a
4634 * redundant inner pathkey, its sort ordering might not match. In
4635 * such cases we may ignore the inner pathkey's sort ordering and use
4636 * the outer's. (In effect, we're lying to the executor about the
4637 * sort direction of this inner column, but it does not matter since
4638 * the run-time row comparisons would only reach this column when
4639 * there's equality for the earlier column containing the same eclass.
4640 * There could be only one value in this column for the range of inner
4641 * rows having a given value in the earlier column, so it does not
4642 * matter which way we imagine this column to be ordered.) But a
4643 * non-redundant inner pathkey had better match outer's ordering too.
4644 */
4645 if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
4646 opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation)
4647 elog(ERROR, "left and right pathkeys do not match in mergejoin");
4648 if (first_inner_match &&
4649 (opathkey->pk_cmptype != ipathkey->pk_cmptype ||
4650 opathkey->pk_nulls_first != ipathkey->pk_nulls_first))
4651 elog(ERROR, "left and right pathkeys do not match in mergejoin");
4652
4653 /* OK, save info for executor */
4654 mergefamilies[i] = opathkey->pk_opfamily;
4655 mergecollations[i] = opathkey->pk_eclass->ec_collation;
4656 mergereversals[i] = (opathkey->pk_cmptype == COMPARE_GT ? true : false);
4657 mergenullsfirst[i] = opathkey->pk_nulls_first;
4658 i++;
4659 }
4660
4661 /*
4662 * Note: it is not an error if we have additional pathkey elements (i.e.,
4663 * lop or lip isn't NULL here). The input paths might be better-sorted
4664 * than we need for the current mergejoin.
4665 */
4666
4667 /*
4668 * Now we can build the mergejoin node.
4669 */
4670 join_plan = make_mergejoin(tlist,
4671 joinclauses,
4672 otherclauses,
4673 mergeclauses,
4674 mergefamilies,
4675 mergecollations,
4676 mergereversals,
4677 mergenullsfirst,
4678 outer_plan,
4679 inner_plan,
4680 best_path->jpath.jointype,
4681 best_path->jpath.inner_unique,
4682 best_path->skip_mark_restore);
4683
4684 /* Costs of sort and material steps are included in path cost already */
4685 copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4686
4687 return join_plan;
4688}
Datum sort(PG_FUNCTION_ARGS)
Definition: _int_op.c:198
@ COMPARE_GT
Definition: cmptype.h:38
double cpu_operator_cost
Definition: costsize.c:134
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:6000
static Sort * make_sort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids)
Definition: createplan.c:6320
return true
Definition: isn.c:130
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
Join join
Definition: plannodes.h:1016
List * outersortkeys
Definition: pathnodes.h:2361
bool skip_mark_restore
Definition: pathnodes.h:2365
List * innersortkeys
Definition: pathnodes.h:2362
JoinPath jpath
Definition: pathnodes.h:2359
bool materialize_inner
Definition: pathnodes.h:2366
int outer_presorted_keys
Definition: pathnodes.h:2363
List * path_mergeclauses
Definition: pathnodes.h:2360
bool pk_nulls_first
Definition: pathnodes.h:1717

References Assert(), build_path_tlist(), COMPARE_GT, 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(), MergePath::outer_presorted_keys, JoinPath::outerjoinpath, MergePath::outersortkeys, palloc(), MergePath::path_mergeclauses, Path::pathkeys, pathkeys_contained_in(), PathKey::pk_cmptype, PathKey::pk_nulls_first, PathKey::pk_opfamily, 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 2397 of file createplan.c.

2398{
2399 Result *plan;
2400 List *tlist;
2401 ListCell *lc;
2402
2403 /* Prepare an InitPlan for each aggregate's subquery. */
2404 foreach(lc, best_path->mmaggregates)
2405 {
2406 MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
2407 PlannerInfo *subroot = mminfo->subroot;
2408 Query *subparse = subroot->parse;
2409 Plan *plan;
2410
2411 /*
2412 * Generate the plan for the subquery. We already have a Path, but we
2413 * have to convert it to a Plan and attach a LIMIT node above it.
2414 * Since we are entering a different planner context (subroot),
2415 * recurse to create_plan not create_plan_recurse.
2416 */
2417 plan = create_plan(subroot, mminfo->path);
2418
2419 plan = (Plan *) make_limit(plan,
2420 subparse->limitOffset,
2421 subparse->limitCount,
2422 subparse->limitOption,
2423 0, NULL, NULL, NULL);
2424
2425 /* Must apply correct cost/width data to Limit node */
2426 plan->disabled_nodes = mminfo->path->disabled_nodes;
2427 plan->startup_cost = mminfo->path->startup_cost;
2428 plan->total_cost = mminfo->pathcost;
2429 plan->plan_rows = 1;
2430 plan->plan_width = mminfo->path->pathtarget->width;
2431 plan->parallel_aware = false;
2432 plan->parallel_safe = mminfo->path->parallel_safe;
2433
2434 /* Convert the plan into an InitPlan in the outer query. */
2435 SS_make_initplan_from_plan(root, subroot, plan, mminfo->param);
2436 }
2437
2438 /* Generate the output plan --- basically just a Result */
2439 tlist = build_path_tlist(root, &best_path->path);
2440
2441 plan = make_one_row_result(tlist, (Node *) best_path->quals,
2442 best_path->path.parent);
2443 plan->result_type = RESULT_TYPE_MINMAX;
2444
2445 copy_generic_path_info(&plan->plan, (Path *) best_path);
2446
2447 /*
2448 * During setrefs.c, we'll need to replace references to the Agg nodes
2449 * with InitPlan output params. (We can't just do that locally in the
2450 * MinMaxAgg node, because path nodes above here may have Agg references
2451 * as well.) Save the mmaggregates list to tell setrefs.c to do that.
2452 */
2453 Assert(root->minmax_aggs == NIL);
2454 root->minmax_aggs = best_path->mmaggregates;
2455
2456 return plan;
2457}
Plan * create_plan(PlannerInfo *root, Path *best_path)
Definition: createplan.c:340
@ RESULT_TYPE_MINMAX
Definition: plannodes.h:275
Param * param
Definition: pathnodes.h:3355
List * quals
Definition: pathnodes.h:2539
List * mmaggregates
Definition: pathnodes.h:2538
Node * limitCount
Definition: parsenodes.h:231
Node * limitOffset
Definition: parsenodes.h:230
LimitOption limitOption
Definition: parsenodes.h:232
void SS_make_initplan_from_plan(PlannerInfo *root, PlannerInfo *subroot, Plan *plan, Param *prm)
Definition: subselect.c:3165

References Assert(), build_path_tlist(), copy_generic_path_info(), create_plan(), Path::disabled_nodes, lfirst, Query::limitCount, Query::limitOffset, Query::limitOption, make_limit(), make_one_row_result(), MinMaxAggPath::mmaggregates, NIL, Path::parallel_safe, MinMaxAggInfo::param, PlannerInfo::parse, MinMaxAggPath::path, MinMaxAggInfo::path, MinMaxAggInfo::pathcost, plan, MinMaxAggPath::quals, RESULT_TYPE_MINMAX, 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 2652 of file createplan.c.

2653{
2655 Path *subpath = best_path->subpath;
2656 Plan *subplan;
2657
2658 /* Subplan must produce exactly the specified tlist */
2660
2661 /* Transfer resname/resjunk labeling, too, to keep executor happy */
2662 apply_tlist_labeling(subplan->targetlist, root->processed_tlist);
2663
2665 subplan,
2666 best_path->operation,
2667 best_path->canSetTag,
2668 best_path->nominalRelation,
2669 best_path->rootRelation,
2670 best_path->resultRelations,
2671 best_path->updateColnosLists,
2672 best_path->withCheckOptionLists,
2673 best_path->returningLists,
2674 best_path->rowMarks,
2675 best_path->onconflict,
2676 best_path->mergeActionLists,
2677 best_path->mergeJoinConditions,
2678 best_path->epqParam);
2679
2680 copy_generic_path_info(&plan->plan, &best_path->path);
2681
2682 return plan;
2683}
static ModifyTable * make_modifytable(PlannerInfo *root, Plan *subplan, CmdType operation, bool canSetTag, Index nominalRelation, Index rootRelation, List *resultRelations, List *updateColnosLists, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, List *mergeActionLists, List *mergeJoinConditions, int epqParam)
Definition: createplan.c:6996
List * returningLists
Definition: pathnodes.h:2612
List * resultRelations
Definition: pathnodes.h:2609
List * withCheckOptionLists
Definition: pathnodes.h:2611
List * mergeJoinConditions
Definition: pathnodes.h:2618
List * updateColnosLists
Definition: pathnodes.h:2610
OnConflictExpr * onconflict
Definition: pathnodes.h:2614
CmdType operation
Definition: pathnodes.h:2605
Index rootRelation
Definition: pathnodes.h:2608
Index nominalRelation
Definition: pathnodes.h:2607
List * mergeActionLists
Definition: pathnodes.h:2616
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::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 3822 of file createplan.c.

3824{
3825 NamedTuplestoreScan *scan_plan;
3826 Index scan_relid = best_path->parent->relid;
3827 RangeTblEntry *rte;
3828
3829 Assert(scan_relid > 0);
3830 rte = planner_rt_fetch(scan_relid, root);
3832
3833 /* Sort clauses into best execution order */
3834 scan_clauses = order_qual_clauses(root, scan_clauses);
3835
3836 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3837 scan_clauses = extract_actual_clauses(scan_clauses, false);
3838
3839 /* Replace any outer-relation variables with nestloop params */
3840 if (best_path->param_info)
3841 {
3842 scan_clauses = (List *)
3843 replace_nestloop_params(root, (Node *) scan_clauses);
3844 }
3845
3846 scan_plan = make_namedtuplestorescan(tlist, scan_clauses, scan_relid,
3847 rte->enrname);
3848
3849 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3850
3851 return scan_plan;
3852}
static NamedTuplestoreScan * make_namedtuplestorescan(List *qptlist, List *qpqual, Index scanrelid, char *enrname)
Definition: createplan.c:5756
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1050
char * enrname
Definition: parsenodes.h:1262

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

4187{
4188 NestLoop *join_plan;
4189 Plan *outer_plan;
4190 Plan *inner_plan;
4191 Relids outerrelids;
4192 List *tlist = build_path_tlist(root, &best_path->jpath.path);
4193 List *joinrestrictclauses = best_path->jpath.joinrestrictinfo;
4194 List *joinclauses;
4195 List *otherclauses;
4196 List *nestParams;
4197 List *outer_tlist;
4198 bool outer_parallel_safe;
4199 Relids saveOuterRels = root->curOuterRels;
4200 ListCell *lc;
4201
4202 /*
4203 * If the inner path is parameterized by the topmost parent of the outer
4204 * rel rather than the outer rel itself, fix that. (Nothing happens here
4205 * if it is not so parameterized.)
4206 */
4207 best_path->jpath.innerjoinpath =
4209 best_path->jpath.innerjoinpath,
4210 best_path->jpath.outerjoinpath->parent);
4211
4212 /*
4213 * Failure here probably means that reparameterize_path_by_child() is not
4214 * in sync with path_is_reparameterizable_by_child().
4215 */
4216 Assert(best_path->jpath.innerjoinpath != NULL);
4217
4218 /* NestLoop can project, so no need to be picky about child tlists */
4219 outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath, 0);
4220
4221 /* For a nestloop, include outer relids in curOuterRels for inner side */
4222 outerrelids = best_path->jpath.outerjoinpath->parent->relids;
4223 root->curOuterRels = bms_union(root->curOuterRels, outerrelids);
4224
4225 inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath, 0);
4226
4227 /* Restore curOuterRels */
4228 bms_free(root->curOuterRels);
4229 root->curOuterRels = saveOuterRels;
4230
4231 /* Sort join qual clauses into best execution order */
4232 joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
4233
4234 /* Get the join qual clauses (in plain expression form) */
4235 /* Any pseudoconstant clauses are ignored here */
4236 if (IS_OUTER_JOIN(best_path->jpath.jointype))
4237 {
4238 extract_actual_join_clauses(joinrestrictclauses,
4239 best_path->jpath.path.parent->relids,
4240 &joinclauses, &otherclauses);
4241 }
4242 else
4243 {
4244 /* We can treat all clauses alike for an inner join */
4245 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
4246 otherclauses = NIL;
4247 }
4248
4249 /* Replace any outer-relation variables with nestloop params */
4250 if (best_path->jpath.path.param_info)
4251 {
4252 joinclauses = (List *)
4253 replace_nestloop_params(root, (Node *) joinclauses);
4254 otherclauses = (List *)
4255 replace_nestloop_params(root, (Node *) otherclauses);
4256 }
4257
4258 /*
4259 * Identify any nestloop parameters that should be supplied by this join
4260 * node, and remove them from root->curOuterParams.
4261 */
4263 outerrelids,
4264 PATH_REQ_OUTER((Path *) best_path));
4265
4266 /*
4267 * While nestloop parameters that are Vars had better be available from
4268 * the outer_plan already, there are edge cases where nestloop parameters
4269 * that are PHVs won't be. In such cases we must add them to the
4270 * outer_plan's tlist, since the executor's NestLoopParam machinery
4271 * requires the params to be simple outer-Var references to that tlist.
4272 * (This is cheating a little bit, because the outer path's required-outer
4273 * relids might not be enough to allow evaluating such a PHV. But in
4274 * practice, if we could have evaluated the PHV at the nestloop node, we
4275 * can do so in the outer plan too.)
4276 */
4277 outer_tlist = outer_plan->targetlist;
4278 outer_parallel_safe = outer_plan->parallel_safe;
4279 foreach(lc, nestParams)
4280 {
4281 NestLoopParam *nlp = (NestLoopParam *) lfirst(lc);
4282 PlaceHolderVar *phv;
4283 TargetEntry *tle;
4284
4285 if (IsA(nlp->paramval, Var))
4286 continue; /* nothing to do for simple Vars */
4287 /* Otherwise it must be a PHV */
4288 phv = castNode(PlaceHolderVar, nlp->paramval);
4289
4290 if (tlist_member((Expr *) phv, outer_tlist))
4291 continue; /* already available */
4292
4293 /*
4294 * It's possible that nestloop parameter PHVs selected to evaluate
4295 * here contain references to surviving root->curOuterParams items
4296 * (that is, they reference values that will be supplied by some
4297 * higher-level nestloop). Those need to be converted to Params now.
4298 * Note: it's safe to do this after the tlist_member() check, because
4299 * equal() won't pay attention to phv->phexpr.
4300 */
4301 phv->phexpr = (Expr *) replace_nestloop_params(root,
4302 (Node *) phv->phexpr);
4303
4304 /* Make a shallow copy of outer_tlist, if we didn't already */
4305 if (outer_tlist == outer_plan->targetlist)
4306 outer_tlist = list_copy(outer_tlist);
4307 /* ... and add the needed expression */
4308 tle = makeTargetEntry((Expr *) copyObject(phv),
4309 list_length(outer_tlist) + 1,
4310 NULL,
4311 true);
4312 outer_tlist = lappend(outer_tlist, tle);
4313 /* ... and track whether tlist is (still) parallel-safe */
4314 if (outer_parallel_safe)
4315 outer_parallel_safe = is_parallel_safe(root, (Node *) phv);
4316 }
4317 if (outer_tlist != outer_plan->targetlist)
4318 outer_plan = change_plan_targetlist(outer_plan, outer_tlist,
4319 outer_parallel_safe);
4320
4321 /* And finally, we can build the join plan node */
4322 join_plan = make_nestloop(tlist,
4323 joinclauses,
4324 otherclauses,
4325 nestParams,
4326 outer_plan,
4327 inner_plan,
4328 best_path->jpath.jointype,
4329 best_path->jpath.inner_unique);
4330
4331 copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
4332
4333 return join_plan;
4334}
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:251
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:757
Plan * change_plan_targetlist(Plan *subplan, List *tlist, bool tlist_parallel_safe)
Definition: createplan.c:1991
static NestLoop * make_nestloop(List *tlist, List *joinclauses, List *otherclauses, List *nestParams, Plan *lefttree, Plan *righttree, JoinType jointype, bool inner_unique)
Definition: createplan.c:5921
List * list_copy(const List *oldlist)
Definition: list.c:1573
#define copyObject(obj)
Definition: nodes.h:232
List * identify_current_nestloop_params(PlannerInfo *root, Relids leftrelids, Relids outerrelids)
Definition: paramassign.c:622
Path * reparameterize_path_by_child(PlannerInfo *root, Path *path, RelOptInfo *child_rel)
Definition: pathnode.c:4019
#define PATH_REQ_OUTER(path)
Definition: pathnodes.h:1916
Var * paramval
Definition: plannodes.h:1000
Join join
Definition: plannodes.h:987
JoinPath jpath
Definition: pathnodes.h:2313
TargetEntry * tlist_member(Expr *node, List *targetlist)
Definition: tlist.c:79

References Assert(), bms_free(), bms_union(), build_path_tlist(), castNode, change_plan_targetlist(), copy_generic_path_info(), copyObject, create_plan_recurse(), extract_actual_clauses(), extract_actual_join_clauses(), identify_current_nestloop_params(), JoinPath::inner_unique, JoinPath::innerjoinpath, IS_OUTER_JOIN, is_parallel_safe(), IsA, NestLoop::join, JoinPath::joinrestrictinfo, JoinPath::jointype, NestPath::jpath, lappend(), lfirst, list_copy(), list_length(), make_nestloop(), makeTargetEntry(), NIL, order_qual_clauses(), JoinPath::outerjoinpath, Plan::parallel_safe, NestLoopParam::paramval, PATH_REQ_OUTER, reparameterize_path_by_child(), replace_nestloop_params(), root, Plan::targetlist, and tlist_member().

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:2389

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:
474 (UniquePath *) best_path,
475 flags);
476 break;
477 case T_Gather:
479 (GatherPath *) best_path);
480 break;
481 case T_Sort:
483 (SortPath *) best_path,
484 flags);
485 break;
486 case T_IncrementalSort:
488 (IncrementalSortPath *) best_path,
489 flags);
490 break;
491 case T_Group:
493 (GroupPath *) best_path);
494 break;
495 case T_Agg:
496 if (IsA(best_path, GroupingSetsPath))
498 (GroupingSetsPath *) best_path);
499 else
500 {
501 Assert(IsA(best_path, AggPath));
503 (AggPath *) best_path);
504 }
505 break;
506 case T_WindowAgg:
508 (WindowAggPath *) best_path);
509 break;
510 case T_SetOp:
512 (SetOpPath *) best_path,
513 flags);
514 break;
515 case T_RecursiveUnion:
517 (RecursiveUnionPath *) best_path);
518 break;
519 case T_LockRows:
521 (LockRowsPath *) best_path,
522 flags);
523 break;
524 case T_ModifyTable:
526 (ModifyTablePath *) best_path);
527 break;
528 case T_Limit:
530 (LimitPath *) best_path,
531 flags);
532 break;
533 case T_GatherMerge:
535 (GatherMergePath *) best_path);
536 break;
537 default:
538 elog(ERROR, "unrecognized node type: %d",
539 (int) best_path->pathtype);
540 plan = NULL; /* keep compiler quiet */
541 break;
542 }
543
544 return plan;
545}
static Plan * create_join_plan(PlannerInfo *root, JoinPath *best_path)
Definition: createplan.c:1074
static Plan * create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path, int flags)
Definition: createplan.c:1455
static GatherMerge * create_gather_merge_plan(PlannerInfo *root, GatherMergePath *best_path)
Definition: createplan.c:1802
static Plan * create_append_plan(PlannerInfo *root, AppendPath *best_path, int flags)
Definition: createplan.c:1209
static Unique * create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
Definition: createplan.c:2119
static Result * create_group_result_plan(PlannerInfo *root, GroupResultPath *best_path)
Definition: createplan.c:1631
static Limit * create_limit_plan(PlannerInfo *root, LimitPath *best_path, int flags)
Definition: createplan.c:2692
static Agg * create_agg_plan(PlannerInfo *root, AggPath *best_path)
Definition: createplan.c:2155
static SetOp * create_setop_plan(PlannerInfo *root, SetOpPath *best_path, int flags)
Definition: createplan.c:2561
static Sort * create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags)
Definition: createplan.c:2019
static Gather * create_gather_plan(PlannerInfo *root, GatherPath *best_path)
Definition: createplan.c:1764
static ProjectSet * create_project_set_plan(PlannerInfo *root, ProjectSetPath *best_path)
Definition: createplan.c:1656
static Group * create_group_plan(PlannerInfo *root, GroupPath *best_path)
Definition: createplan.c:2080
static ModifyTable * create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path)
Definition: createplan.c:2652
static Result * create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path)
Definition: createplan.c:2397
static LockRows * create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path, int flags)
Definition: createplan.c:2629
static Material * create_material_plan(PlannerInfo *root, MaterialPath *best_path, int flags)
Definition: createplan.c:1682
static Plan * create_scan_plan(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:552
static IncrementalSort * create_incrementalsort_plan(PlannerInfo *root, IncrementalSortPath *best_path, int flags)
Definition: createplan.c:2053
static Plan * create_projection_plan(PlannerInfo *root, ProjectionPath *best_path, int flags)
Definition: createplan.c:1858
static Memoize * create_memoize_plan(PlannerInfo *root, MemoizePath *best_path, int flags)
Definition: createplan.c:1710
static WindowAgg * create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
Definition: createplan.c:2466
static Plan * create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path)
Definition: createplan.c:2239
static RecursiveUnion * create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path)
Definition: createplan.c:2597
void check_stack_depth(void)
Definition: stack_depth.c:95
NodeTag pathtype
Definition: pathnodes.h:1872

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_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(), and create_windowagg_plan().

◆ create_project_set_plan()

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

Definition at line 1656 of file createplan.c.

1657{
1659 Plan *subplan;
1660 List *tlist;
1661
1662 /* Since we intend to project, we don't need to constrain child tlist */
1663 subplan = create_plan_recurse(root, best_path->subpath, 0);
1664
1665 tlist = build_path_tlist(root, &best_path->path);
1666
1667 plan = make_project_set(tlist, subplan);
1668
1669 copy_generic_path_info(&plan->plan, (Path *) best_path);
1670
1671 return plan;
1672}
static ProjectSet * make_project_set(List *tlist, Plan *subplan)
Definition: createplan.c:6977
Path * subpath
Definition: pathnodes.h:2415

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

1859{
1860 Plan *plan;
1861 Plan *subplan;
1862 List *tlist;
1863 bool needs_result_node = false;
1864
1865 /*
1866 * Convert our subpath to a Plan and determine whether we need a Result
1867 * node.
1868 *
1869 * In most cases where we don't need to project, create_projection_path
1870 * will have set dummypp, but not always. First, some createplan.c
1871 * routines change the tlists of their nodes. (An example is that
1872 * create_merge_append_plan might add resjunk sort columns to a
1873 * MergeAppend.) Second, create_projection_path has no way of knowing
1874 * what path node will be placed on top of the projection path and
1875 * therefore can't predict whether it will require an exact tlist. For
1876 * both of these reasons, we have to recheck here.
1877 */
1878 if (use_physical_tlist(root, &best_path->path, flags))
1879 {
1880 /*
1881 * Our caller doesn't really care what tlist we return, so we don't
1882 * actually need to project. However, we may still need to ensure
1883 * proper sortgroupref labels, if the caller cares about those.
1884 */
1885 subplan = create_plan_recurse(root, best_path->subpath, 0);
1886 tlist = subplan->targetlist;
1887 if (flags & CP_LABEL_TLIST)
1889 best_path->path.pathtarget);
1890 }
1891 else if (is_projection_capable_path(best_path->subpath))
1892 {
1893 /*
1894 * Our caller requires that we return the exact tlist, but no separate
1895 * result node is needed because the subpath is projection-capable.
1896 * Tell create_plan_recurse that we're going to ignore the tlist it
1897 * produces.
1898 */
1899 subplan = create_plan_recurse(root, best_path->subpath,
1902 tlist = build_path_tlist(root, &best_path->path);
1903 }
1904 else
1905 {
1906 /*
1907 * It looks like we need a result node, unless by good fortune the
1908 * requested tlist is exactly the one the child wants to produce.
1909 */
1910 subplan = create_plan_recurse(root, best_path->subpath, 0);
1911 tlist = build_path_tlist(root, &best_path->path);
1912 needs_result_node = !tlist_same_exprs(tlist, subplan->targetlist);
1913 }
1914
1915 /*
1916 * If we make a different decision about whether to include a Result node
1917 * than create_projection_path did, we'll have made slightly wrong cost
1918 * estimates; but label the plan with the cost estimates we actually used,
1919 * not "corrected" ones. (XXX this could be cleaned up if we moved more
1920 * of the sortcolumn setup logic into Path creation, but that would add
1921 * expense to creating Paths we might end up not using.)
1922 */
1923 if (!needs_result_node)
1924 {
1925 /* Don't need a separate Result, just assign tlist to subplan */
1926 plan = subplan;
1927 plan->targetlist = tlist;
1928
1929 /* Label plan with the estimated costs we actually used */
1930 plan->startup_cost = best_path->path.startup_cost;
1931 plan->total_cost = best_path->path.total_cost;
1932 plan->plan_rows = best_path->path.rows;
1933 plan->plan_width = best_path->path.pathtarget->width;
1934 plan->parallel_safe = best_path->path.parallel_safe;
1935 /* ... but don't change subplan's parallel_aware flag */
1936 }
1937 else
1938 {
1939 plan = (Plan *) make_gating_result(tlist, NULL, subplan);
1940
1941 copy_generic_path_info(plan, (Path *) best_path);
1942 }
1943
1944 return plan;
1945}
static bool use_physical_tlist(PlannerInfo *root, Path *path, int flags)
Definition: createplan.c:858
bool is_projection_capable_path(Path *path)
Definition: createplan.c:7217
#define CP_IGNORE_TLIST
Definition: createplan.c:73
Path * subpath
Definition: pathnodes.h:2403
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_gating_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 2597 of file createplan.c.

2598{
2600 Plan *leftplan;
2601 Plan *rightplan;
2602 List *tlist;
2603
2604 /* Need both children to produce same tlist, so force it */
2605 leftplan = create_plan_recurse(root, best_path->leftpath, CP_EXACT_TLIST);
2606 rightplan = create_plan_recurse(root, best_path->rightpath, CP_EXACT_TLIST);
2607
2608 tlist = build_path_tlist(root, &best_path->path);
2609
2610 plan = make_recursive_union(tlist,
2611 leftplan,
2612 rightplan,
2613 best_path->wtParam,
2614 best_path->distinctList,
2615 best_path->numGroups);
2616
2617 copy_generic_path_info(&plan->plan, (Path *) best_path);
2618
2619 return plan;
2620}
static RecursiveUnion * make_recursive_union(List *tlist, Plan *lefttree, Plan *righttree, int wtParam, List *distinctList, Cardinality numGroups)
Definition: createplan.c:5835
Cardinality numGroups
Definition: pathnodes.h:2580

References build_path_tlist(), 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 3861 of file createplan.c.

3863{
3864 Result *scan_plan;
3865 Index scan_relid = best_path->parent->relid;
3867
3868 Assert(scan_relid > 0);
3869 rte = planner_rt_fetch(scan_relid, root);
3870 Assert(rte->rtekind == RTE_RESULT);
3871
3872 /* Sort clauses into best execution order */
3873 scan_clauses = order_qual_clauses(root, scan_clauses);
3874
3875 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3876 scan_clauses = extract_actual_clauses(scan_clauses, false);
3877
3878 /* Replace any outer-relation variables with nestloop params */
3879 if (best_path->param_info)
3880 {
3881 scan_clauses = (List *)
3882 replace_nestloop_params(root, (Node *) scan_clauses);
3883 }
3884
3885 scan_plan = make_one_row_result(tlist, (Node *) scan_clauses,
3886 best_path->parent);
3887
3888 copy_generic_path_info(&scan_plan->plan, best_path);
3889
3890 return scan_plan;
3891}
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:227
@ RTE_RESULT
Definition: parsenodes.h:1051

References Assert(), copy_generic_path_info(), extract_actual_clauses(), make_one_row_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 2791 of file createplan.c.

2793{
2794 SampleScan *scan_plan;
2795 Index scan_relid = best_path->parent->relid;
2796 RangeTblEntry *rte;
2797 TableSampleClause *tsc;
2798
2799 /* it should be a base rel with a tablesample clause... */
2800 Assert(scan_relid > 0);
2801 rte = planner_rt_fetch(scan_relid, root);
2802 Assert(rte->rtekind == RTE_RELATION);
2803 tsc = rte->tablesample;
2804 Assert(tsc != NULL);
2805
2806 /* Sort clauses into best execution order */
2807 scan_clauses = order_qual_clauses(root, scan_clauses);
2808
2809 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2810 scan_clauses = extract_actual_clauses(scan_clauses, false);
2811
2812 /* Replace any outer-relation variables with nestloop params */
2813 if (best_path->param_info)
2814 {
2815 scan_clauses = (List *)
2816 replace_nestloop_params(root, (Node *) scan_clauses);
2817 tsc = (TableSampleClause *)
2819 }
2820
2821 scan_plan = make_samplescan(tlist,
2822 scan_clauses,
2823 scan_relid,
2824 tsc);
2825
2826 copy_generic_path_info(&scan_plan->scan.plan, best_path);
2827
2828 return scan_plan;
2829}
static SampleScan * make_samplescan(List *qptlist, List *qpqual, Index scanrelid, TableSampleClause *tsc)
Definition: createplan.c:5498
struct TableSampleClause * tablesample
Definition: parsenodes.h:1129
Scan scan
Definition: plannodes.h:541

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

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

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

2755{
2756 SeqScan *scan_plan;
2757 Index scan_relid = best_path->parent->relid;
2758
2759 /* it should be a base rel... */
2760 Assert(scan_relid > 0);
2761 Assert(best_path->parent->rtekind == RTE_RELATION);
2762
2763 /* Sort clauses into best execution order */
2764 scan_clauses = order_qual_clauses(root, scan_clauses);
2765
2766 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2767 scan_clauses = extract_actual_clauses(scan_clauses, false);
2768
2769 /* Replace any outer-relation variables with nestloop params */
2770 if (best_path->param_info)
2771 {
2772 scan_clauses = (List *)
2773 replace_nestloop_params(root, (Node *) scan_clauses);
2774 }
2775
2776 scan_plan = make_seqscan(tlist,
2777 scan_clauses,
2778 scan_relid);
2779
2780 copy_generic_path_info(&scan_plan->scan.plan, best_path);
2781
2782 return scan_plan;
2783}
static SeqScan * make_seqscan(List *qptlist, List *qpqual, Index scanrelid)
Definition: createplan.c:5481
Scan scan
Definition: plannodes.h:532

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

2562{
2563 SetOp *plan;
2564 List *tlist = build_path_tlist(root, &best_path->path);
2565 Plan *leftplan;
2566 Plan *rightplan;
2567
2568 /*
2569 * SetOp doesn't project, so tlist requirements pass through; moreover we
2570 * need grouping columns to be labeled.
2571 */
2572 leftplan = create_plan_recurse(root, best_path->leftpath,
2573 flags | CP_LABEL_TLIST);
2574 rightplan = create_plan_recurse(root, best_path->rightpath,
2575 flags | CP_LABEL_TLIST);
2576
2577 plan = make_setop(best_path->cmd,
2578 best_path->strategy,
2579 tlist,
2580 leftplan,
2581 rightplan,
2582 best_path->groupList,
2583 best_path->numGroups);
2584
2585 copy_generic_path_info(&plan->plan, (Path *) best_path);
2586
2587 return plan;
2588}
static SetOp * make_setop(SetOpCmd cmd, SetOpStrategy strategy, List *tlist, Plan *lefttree, Plan *righttree, List *groupList, Cardinality numGroups)
Definition: createplan.c:6811
Path * rightpath
Definition: pathnodes.h:2563
Cardinality numGroups
Definition: pathnodes.h:2567
Path * leftpath
Definition: pathnodes.h:2562
SetOpCmd cmd
Definition: pathnodes.h:2564
Path path
Definition: pathnodes.h:2561
SetOpStrategy strategy
Definition: pathnodes.h:2565
List * groupList
Definition: pathnodes.h:2566

References build_path_tlist(), 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 2019 of file createplan.c.

2020{
2021 Sort *plan;
2022 Plan *subplan;
2023
2024 /*
2025 * We don't want any excess columns in the sorted tuples, so request a
2026 * smaller tlist. Otherwise, since Sort doesn't project, tlist
2027 * requirements pass through.
2028 */
2029 subplan = create_plan_recurse(root, best_path->subpath,
2030 flags | CP_SMALL_TLIST);
2031
2032 /*
2033 * make_sort_from_pathkeys indirectly calls find_ec_member_matching_expr,
2034 * which will ignore any child EC members that don't belong to the given
2035 * relids. Thus, if this sort path is based on a child relation, we must
2036 * pass its relids.
2037 */
2038 plan = make_sort_from_pathkeys(subplan, best_path->path.pathkeys,
2039 IS_OTHER_REL(best_path->subpath->parent) ?
2040 best_path->path.parent->relids : NULL);
2041
2042 copy_generic_path_info(&plan->plan, (Path *) best_path);
2043
2044 return plan;
2045}

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

3540{
3541 SubqueryScan *scan_plan;
3542 RelOptInfo *rel = best_path->path.parent;
3543 Index scan_relid = rel->relid;
3544 Plan *subplan;
3545
3546 /* it should be a subquery base rel... */
3547 Assert(scan_relid > 0);
3548 Assert(rel->rtekind == RTE_SUBQUERY);
3549
3550 /*
3551 * Recursively create Plan from Path for subquery. Since we are entering
3552 * a different planner context (subroot), recurse to create_plan not
3553 * create_plan_recurse.
3554 */
3555 subplan = create_plan(rel->subroot, best_path->subpath);
3556
3557 /* Sort clauses into best execution order */
3558 scan_clauses = order_qual_clauses(root, scan_clauses);
3559
3560 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3561 scan_clauses = extract_actual_clauses(scan_clauses, false);
3562
3563 /*
3564 * Replace any outer-relation variables with nestloop params.
3565 *
3566 * We must provide nestloop params for both lateral references of the
3567 * subquery and outer vars in the scan_clauses. It's better to assign the
3568 * former first, because that code path requires specific param IDs, while
3569 * replace_nestloop_params can adapt to the IDs assigned by
3570 * process_subquery_nestloop_params. This avoids possibly duplicating
3571 * nestloop params when the same Var is needed for both reasons.
3572 */
3573 if (best_path->path.param_info)
3574 {
3576 rel->subplan_params);
3577 scan_clauses = (List *)
3578 replace_nestloop_params(root, (Node *) scan_clauses);
3579 }
3580
3581 scan_plan = make_subqueryscan(tlist,
3582 scan_clauses,
3583 scan_relid,
3584 subplan);
3585
3586 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3587
3588 return scan_plan;
3589}
static SubqueryScan * make_subqueryscan(List *qptlist, List *qpqual, Index scanrelid, Plan *subplan)
Definition: createplan.c:5656
void process_subquery_nestloop_params(PlannerInfo *root, List *subplan_params)
Definition: paramassign.c:527
@ RTE_SUBQUERY
Definition: parsenodes.h:1044
List * subplan_params
Definition: pathnodes.h:1005
PlannerInfo * subroot
Definition: pathnodes.h:1004

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

3642{
3643 TableFuncScan *scan_plan;
3644 Index scan_relid = best_path->parent->relid;
3645 RangeTblEntry *rte;
3646 TableFunc *tablefunc;
3647
3648 /* it should be a function base rel... */
3649 Assert(scan_relid > 0);
3650 rte = planner_rt_fetch(scan_relid, root);
3651 Assert(rte->rtekind == RTE_TABLEFUNC);
3652 tablefunc = rte->tablefunc;
3653
3654 /* Sort clauses into best execution order */
3655 scan_clauses = order_qual_clauses(root, scan_clauses);
3656
3657 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3658 scan_clauses = extract_actual_clauses(scan_clauses, false);
3659
3660 /* Replace any outer-relation variables with nestloop params */
3661 if (best_path->param_info)
3662 {
3663 scan_clauses = (List *)
3664 replace_nestloop_params(root, (Node *) scan_clauses);
3665 /* The function expressions could contain nestloop params, too */
3666 tablefunc = (TableFunc *) replace_nestloop_params(root, (Node *) tablefunc);
3667 }
3668
3669 scan_plan = make_tablefuncscan(tlist, scan_clauses, scan_relid,
3670 tablefunc);
3671
3672 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3673
3674 return scan_plan;
3675}
static TableFuncScan * make_tablefuncscan(List *qptlist, List *qpqual, Index scanrelid, TableFunc *tablefunc)
Definition: createplan.c:5697
@ RTE_TABLEFUNC
Definition: parsenodes.h:1047
TableFunc * tablefunc
Definition: parsenodes.h:1215

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

3475{
3476 TidRangeScan *scan_plan;
3477 Index scan_relid = best_path->path.parent->relid;
3478 List *tidrangequals = best_path->tidrangequals;
3479
3480 /* it should be a base rel... */
3481 Assert(scan_relid > 0);
3482 Assert(best_path->path.parent->rtekind == RTE_RELATION);
3483
3484 /*
3485 * The qpqual list must contain all restrictions not enforced by the
3486 * tidrangequals list. tidrangequals has AND semantics, so we can simply
3487 * remove any qual that appears in it.
3488 */
3489 {
3490 List *qpqual = NIL;
3491 ListCell *l;
3492
3493 foreach(l, scan_clauses)
3494 {
3496
3497 if (rinfo->pseudoconstant)
3498 continue; /* we may drop pseudoconstants here */
3499 if (list_member_ptr(tidrangequals, rinfo))
3500 continue; /* simple duplicate */
3501 qpqual = lappend(qpqual, rinfo);
3502 }
3503 scan_clauses = qpqual;
3504 }
3505
3506 /* Sort clauses into best execution order */
3507 scan_clauses = order_qual_clauses(root, scan_clauses);
3508
3509 /* Reduce RestrictInfo lists to bare expressions; ignore pseudoconstants */
3510 tidrangequals = extract_actual_clauses(tidrangequals, false);
3511 scan_clauses = extract_actual_clauses(scan_clauses, false);
3512
3513 /* Replace any outer-relation variables with nestloop params */
3514 if (best_path->path.param_info)
3515 {
3516 tidrangequals = (List *)
3517 replace_nestloop_params(root, (Node *) tidrangequals);
3518 scan_clauses = (List *)
3519 replace_nestloop_params(root, (Node *) scan_clauses);
3520 }
3521
3522 scan_plan = make_tidrangescan(tlist,
3523 scan_clauses,
3524 scan_relid,
3525 tidrangequals);
3526
3527 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3528
3529 return scan_plan;
3530}
static TidRangeScan * make_tidrangescan(List *qptlist, List *qpqual, Index scanrelid, List *tidrangequals)
Definition: createplan.c:5637
List * tidrangequals
Definition: pathnodes.h:2083

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

3378{
3379 TidScan *scan_plan;
3380 Index scan_relid = best_path->path.parent->relid;
3381 List *tidquals = best_path->tidquals;
3382
3383 /* it should be a base rel... */
3384 Assert(scan_relid > 0);
3385 Assert(best_path->path.parent->rtekind == RTE_RELATION);
3386
3387 /*
3388 * The qpqual list must contain all restrictions not enforced by the
3389 * tidquals list. Since tidquals has OR semantics, we have to be careful
3390 * about matching it up to scan_clauses. It's convenient to handle the
3391 * single-tidqual case separately from the multiple-tidqual case. In the
3392 * single-tidqual case, we look through the scan_clauses while they are
3393 * still in RestrictInfo form, and drop any that are redundant with the
3394 * tidqual.
3395 *
3396 * In normal cases simple pointer equality checks will be enough to spot
3397 * duplicate RestrictInfos, so we try that first.
3398 *
3399 * Another common case is that a scan_clauses entry is generated from the
3400 * same EquivalenceClass as some tidqual, and is therefore redundant with
3401 * it, though not equal.
3402 *
3403 * Unlike indexpaths, we don't bother with predicate_implied_by(); the
3404 * number of cases where it could win are pretty small.
3405 */
3406 if (list_length(tidquals) == 1)
3407 {
3408 List *qpqual = NIL;
3409 ListCell *l;
3410
3411 foreach(l, scan_clauses)
3412 {
3414
3415 if (rinfo->pseudoconstant)
3416 continue; /* we may drop pseudoconstants here */
3417 if (list_member_ptr(tidquals, rinfo))
3418 continue; /* simple duplicate */
3419 if (is_redundant_derived_clause(rinfo, tidquals))
3420 continue; /* derived from same EquivalenceClass */
3421 qpqual = lappend(qpqual, rinfo);
3422 }
3423 scan_clauses = qpqual;
3424 }
3425
3426 /* Sort clauses into best execution order */
3427 scan_clauses = order_qual_clauses(root, scan_clauses);
3428
3429 /* Reduce RestrictInfo lists to bare expressions; ignore pseudoconstants */
3430 tidquals = extract_actual_clauses(tidquals, false);
3431 scan_clauses = extract_actual_clauses(scan_clauses, false);
3432
3433 /*
3434 * If we have multiple tidquals, it's more convenient to remove duplicate
3435 * scan_clauses after stripping the RestrictInfos. In this situation,
3436 * because the tidquals represent OR sub-clauses, they could not have come
3437 * from EquivalenceClasses so we don't have to worry about matching up
3438 * non-identical clauses. On the other hand, because tidpath.c will have
3439 * extracted those sub-clauses from some OR clause and built its own list,
3440 * we will certainly not have pointer equality to any scan clause. So
3441 * convert the tidquals list to an explicit OR clause and see if we can
3442 * match it via equal() to any scan clause.
3443 */
3444 if (list_length(tidquals) > 1)
3445 scan_clauses = list_difference(scan_clauses,
3446 list_make1(make_orclause(tidquals)));
3447
3448 /* Replace any outer-relation variables with nestloop params */
3449 if (best_path->path.param_info)
3450 {
3451 tidquals = (List *)
3452 replace_nestloop_params(root, (Node *) tidquals);
3453 scan_clauses = (List *)
3454 replace_nestloop_params(root, (Node *) scan_clauses);
3455 }
3456
3457 scan_plan = make_tidscan(tlist,
3458 scan_clauses,
3459 scan_relid,
3460 tidquals);
3461
3462 copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
3463
3464 return scan_plan;
3465}
static TidScan * make_tidscan(List *qptlist, List *qpqual, Index scanrelid, List *tidquals)
Definition: createplan.c:5618
bool is_redundant_derived_clause(RestrictInfo *rinfo, List *clauselist)
Definition: equivclass.c:3550
List * tidquals
Definition: pathnodes.h:2071
Path path
Definition: pathnodes.h:2070
Scan scan
Definition: plannodes.h:706

References Assert(), copy_generic_path_info(), extract_actual_clauses(), is_redundant_derived_clause(), lappend(), lfirst_node, list_difference(), list_length(), list_make1, list_member_ptr(), make_orclause(), make_tidscan(), NIL, order_qual_clauses(), TidPath::path, replace_nestloop_params(), root, RTE_RELATION, TidScan::scan, and TidPath::tidquals.

Referenced by create_scan_plan().

◆ create_unique_plan()

static Unique * create_unique_plan ( PlannerInfo root,
UniquePath best_path,
int  flags 
)
static

Definition at line 2119 of file createplan.c.

2120{
2121 Unique *plan;
2122 Plan *subplan;
2123
2124 /*
2125 * Unique doesn't project, so tlist requirements pass through; moreover we
2126 * need grouping columns to be labeled.
2127 */
2128 subplan = create_plan_recurse(root, best_path->subpath,
2129 flags | CP_LABEL_TLIST);
2130
2131 /*
2132 * make_unique_from_pathkeys calls find_ec_member_matching_expr, which
2133 * will ignore any child EC members that don't belong to the given relids.
2134 * Thus, if this unique path is based on a child relation, we must pass
2135 * its relids.
2136 */
2138 best_path->path.pathkeys,
2139 best_path->numkeys,
2140 IS_OTHER_REL(best_path->path.parent) ?
2141 best_path->path.parent->relids : NULL);
2142
2143 copy_generic_path_info(&plan->plan, (Path *) best_path);
2144
2145 return plan;
2146}
static Unique * make_unique_from_pathkeys(Plan *lefttree, List *pathkeys, int numCols, Relids relids)
Definition: createplan.c:6675
Path * subpath
Definition: pathnodes.h:2469

References copy_generic_path_info(), CP_LABEL_TLIST, create_plan_recurse(), IS_OTHER_REL, make_unique_from_pathkeys(), UniquePath::numkeys, UniquePath::path, Path::pathkeys, plan, root, and UniquePath::subpath.

Referenced by create_plan_recurse().

◆ create_valuesscan_plan()

static ValuesScan * create_valuesscan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3683 of file createplan.c.

3685{
3686 ValuesScan *scan_plan;
3687 Index scan_relid = best_path->parent->relid;
3688 RangeTblEntry *rte;
3689 List *values_lists;
3690
3691 /* it should be a values base rel... */
3692 Assert(scan_relid > 0);
3693 rte = planner_rt_fetch(scan_relid, root);
3694 Assert(rte->rtekind == RTE_VALUES);
3695 values_lists = rte->values_lists;
3696
3697 /* Sort clauses into best execution order */
3698 scan_clauses = order_qual_clauses(root, scan_clauses);
3699
3700 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3701 scan_clauses = extract_actual_clauses(scan_clauses, false);
3702
3703 /* Replace any outer-relation variables with nestloop params */
3704 if (best_path->param_info)
3705 {
3706 scan_clauses = (List *)
3707 replace_nestloop_params(root, (Node *) scan_clauses);
3708 /* The values lists could contain nestloop params, too */
3709 values_lists = (List *)
3710 replace_nestloop_params(root, (Node *) values_lists);
3711 }
3712
3713 scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
3714 values_lists);
3715
3716 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3717
3718 return scan_plan;
3719}
static ValuesScan * make_valuesscan(List *qptlist, List *qpqual, Index scanrelid, List *values_lists)
Definition: createplan.c:5716
@ RTE_VALUES
Definition: parsenodes.h:1048
List * values_lists
Definition: parsenodes.h:1221
Scan scan
Definition: plannodes.h:778

References Assert(), copy_generic_path_info(), extract_actual_clauses(), make_valuesscan(), order_qual_clauses(), planner_rt_fetch, replace_nestloop_params(), root, RTE_VALUES, RangeTblEntry::rtekind, ValuesScan::scan, and RangeTblEntry::values_lists.

Referenced by create_scan_plan().

◆ create_windowagg_plan()

static WindowAgg * create_windowagg_plan ( PlannerInfo root,
WindowAggPath best_path 
)
static

Definition at line 2466 of file createplan.c.

2467{
2468 WindowAgg *plan;
2469 WindowClause *wc = best_path->winclause;
2470 int numPart = list_length(wc->partitionClause);
2471 int numOrder = list_length(wc->orderClause);
2472 Plan *subplan;
2473 List *tlist;
2474 int partNumCols;
2475 AttrNumber *partColIdx;
2476 Oid *partOperators;
2477 Oid *partCollations;
2478 int ordNumCols;
2479 AttrNumber *ordColIdx;
2480 Oid *ordOperators;
2481 Oid *ordCollations;
2482 ListCell *lc;
2483
2484 /*
2485 * Choice of tlist here is motivated by the fact that WindowAgg will be
2486 * storing the input rows of window frames in a tuplestore; it therefore
2487 * behooves us to request a small tlist to avoid wasting space. We do of
2488 * course need grouping columns to be available.
2489 */
2490 subplan = create_plan_recurse(root, best_path->subpath,
2492
2493 tlist = build_path_tlist(root, &best_path->path);
2494
2495 /*
2496 * Convert SortGroupClause lists into arrays of attr indexes and equality
2497 * operators, as wanted by executor.
2498 */
2499 partColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numPart);
2500 partOperators = (Oid *) palloc(sizeof(Oid) * numPart);
2501 partCollations = (Oid *) palloc(sizeof(Oid) * numPart);
2502
2503 partNumCols = 0;
2504 foreach(lc, wc->partitionClause)
2505 {
2506 SortGroupClause *sgc = (SortGroupClause *) lfirst(lc);
2507 TargetEntry *tle = get_sortgroupclause_tle(sgc, subplan->targetlist);
2508
2509 Assert(OidIsValid(sgc->eqop));
2510 partColIdx[partNumCols] = tle->resno;
2511 partOperators[partNumCols] = sgc->eqop;
2512 partCollations[partNumCols] = exprCollation((Node *) tle->expr);
2513 partNumCols++;
2514 }
2515
2516 ordColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numOrder);
2517 ordOperators = (Oid *) palloc(sizeof(Oid) * numOrder);
2518 ordCollations = (Oid *) palloc(sizeof(Oid) * numOrder);
2519
2520 ordNumCols = 0;
2521 foreach(lc, wc->orderClause)
2522 {
2523 SortGroupClause *sgc = (SortGroupClause *) lfirst(lc);
2524 TargetEntry *tle = get_sortgroupclause_tle(sgc, subplan->targetlist);
2525
2526 Assert(OidIsValid(sgc->eqop));
2527 ordColIdx[ordNumCols] = tle->resno;
2528 ordOperators[ordNumCols] = sgc->eqop;
2529 ordCollations[ordNumCols] = exprCollation((Node *) tle->expr);
2530 ordNumCols++;
2531 }
2532
2533 /* And finally we can make the WindowAgg node */
2534 plan = make_windowagg(tlist,
2535 wc,
2536 partNumCols,
2537 partColIdx,
2538 partOperators,
2539 partCollations,
2540 ordNumCols,
2541 ordColIdx,
2542 ordOperators,
2543 ordCollations,
2544 best_path->runCondition,
2545 best_path->qual,
2546 best_path->topwindow,
2547 subplan);
2548
2549 copy_generic_path_info(&plan->plan, (Path *) best_path);
2550
2551 return plan;
2552}
static WindowAgg * make_windowagg(List *tlist, WindowClause *wc, int partNumCols, AttrNumber *partColIdx, Oid *partOperators, Oid *partCollations, int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators, Oid *ordCollations, List *runCondition, List *qual, bool topWindow, Plan *lefttree)
Definition: createplan.c:6604
List * runCondition
Definition: pathnodes.h:2551
Path * subpath
Definition: pathnodes.h:2548
WindowClause * winclause
Definition: pathnodes.h:2549
List * partitionClause
Definition: parsenodes.h:1574
List * orderClause
Definition: parsenodes.h:1576

References Assert(), build_path_tlist(), copy_generic_path_info(), CP_LABEL_TLIST, CP_SMALL_TLIST, create_plan_recurse(), SortGroupClause::eqop, TargetEntry::expr, exprCollation(), get_sortgroupclause_tle(), lfirst, list_length(), make_windowagg(), OidIsValid, WindowClause::orderClause, palloc(), WindowClause::partitionClause, WindowAggPath::path, plan, WindowAggPath::qual, TargetEntry::resno, root, WindowAggPath::runCondition, WindowAggPath::subpath, Plan::targetlist, WindowAggPath::topwindow, and WindowAggPath::winclause.

Referenced by create_plan_recurse().

◆ create_worktablescan_plan()

static WorkTableScan * create_worktablescan_plan ( PlannerInfo root,
Path best_path,
List tlist,
List scan_clauses 
)
static

Definition at line 3899 of file createplan.c.

3901{
3902 WorkTableScan *scan_plan;
3903 Index scan_relid = best_path->parent->relid;
3904 RangeTblEntry *rte;
3905 Index levelsup;
3906 PlannerInfo *cteroot;
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 * We need to find the worktable param ID, which is in the plan level
3915 * that's processing the recursive UNION, which is one level *below* where
3916 * the CTE comes from.
3917 */
3918 levelsup = rte->ctelevelsup;
3919 if (levelsup == 0) /* shouldn't happen */
3920 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3921 levelsup--;
3922 cteroot = root;
3923 while (levelsup-- > 0)
3924 {
3925 cteroot = cteroot->parent_root;
3926 if (!cteroot) /* shouldn't happen */
3927 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3928 }
3929 if (cteroot->wt_param_id < 0) /* shouldn't happen */
3930 elog(ERROR, "could not find param ID for CTE \"%s\"", rte->ctename);
3931
3932 /* Sort clauses into best execution order */
3933 scan_clauses = order_qual_clauses(root, scan_clauses);
3934
3935 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
3936 scan_clauses = extract_actual_clauses(scan_clauses, false);
3937
3938 /* Replace any outer-relation variables with nestloop params */
3939 if (best_path->param_info)
3940 {
3941 scan_clauses = (List *)
3942 replace_nestloop_params(root, (Node *) scan_clauses);
3943 }
3944
3945 scan_plan = make_worktablescan(tlist, scan_clauses, scan_relid,
3946 cteroot->wt_param_id);
3947
3948 copy_generic_path_info(&scan_plan->scan.plan, best_path);
3949
3950 return scan_plan;
3951}
static WorkTableScan * make_worktablescan(List *qptlist, List *qpqual, Index scanrelid, int wtParam)
Definition: createplan.c:5776
int wt_param_id
Definition: pathnodes.h:575

References Assert(), copy_generic_path_info(), RangeTblEntry::ctelevelsup, RangeTblEntry::ctename, elog, ERROR, extract_actual_clauses(), make_worktablescan(), order_qual_clauses(), planner_rt_fetch, replace_nestloop_params(), root, RTE_CTE, RangeTblEntry::rtekind, WorkTableScan::scan, and PlannerInfo::wt_param_id.

Referenced by create_scan_plan().

◆ fix_indexorderby_references()

static List * fix_indexorderby_references ( PlannerInfo root,
IndexPath index_path 
)
static

Definition at line 5006 of file createplan.c.

5007{
5008 IndexOptInfo *index = index_path->indexinfo;
5009 List *fixed_indexorderbys;
5010 ListCell *lcc,
5011 *lci;
5012
5013 fixed_indexorderbys = NIL;
5014
5015 forboth(lcc, index_path->indexorderbys, lci, index_path->indexorderbycols)
5016 {
5017 Node *clause = (Node *) lfirst(lcc);
5018 int indexcol = lfirst_int(lci);
5019
5020 clause = fix_indexqual_clause(root, index, indexcol, clause, NIL);
5021 fixed_indexorderbys = lappend(fixed_indexorderbys, clause);
5022 }
5023
5024 return fixed_indexorderbys;
5025}
static Node * fix_indexqual_clause(PlannerInfo *root, IndexOptInfo *index, int indexcol, Node *clause, List *indexcolnos)
Definition: createplan.c:5035
#define lfirst_int(lc)
Definition: pg_list.h:173
List * indexorderbycols
Definition: pathnodes.h:1960
Definition: type.h:96

References fix_indexqual_clause(), forboth, IndexPath::indexinfo, IndexPath::indexorderbycols, IndexPath::indexorderbys, lappend(), lfirst, lfirst_int, NIL, and root.

Referenced by create_indexscan_plan().

◆ fix_indexqual_clause()

static Node * fix_indexqual_clause ( PlannerInfo root,
IndexOptInfo index,
int  indexcol,
Node clause,
List indexcolnos 
)
static

Definition at line 5035 of file createplan.c.

5037{
5038 /*
5039 * Replace any outer-relation variables with nestloop params.
5040 *
5041 * This also makes a copy of the clause, so it's safe to modify it
5042 * in-place below.
5043 */
5044 clause = replace_nestloop_params(root, clause);
5045
5046 if (IsA(clause, OpExpr))
5047 {
5048 OpExpr *op = (OpExpr *) clause;
5049
5050 /* Replace the indexkey expression with an index Var. */
5052 index,
5053 indexcol);
5054 }
5055 else if (IsA(clause, RowCompareExpr))
5056 {
5057 RowCompareExpr *rc = (RowCompareExpr *) clause;
5058 ListCell *lca,
5059 *lcai;
5060
5061 /* Replace the indexkey expressions with index Vars. */
5062 Assert(list_length(rc->largs) == list_length(indexcolnos));
5063 forboth(lca, rc->largs, lcai, indexcolnos)
5064 {
5066 index,
5067 lfirst_int(lcai));
5068 }
5069 }
5070 else if (IsA(clause, ScalarArrayOpExpr))
5071 {
5072 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
5073
5074 /* Replace the indexkey expression with an index Var. */
5076 index,
5077 indexcol);
5078 }
5079 else if (IsA(clause, NullTest))
5080 {
5081 NullTest *nt = (NullTest *) clause;
5082
5083 /* Replace the indexkey expression with an index Var. */
5084 nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
5085 index,
5086 indexcol);
5087 }
5088 else
5089 elog(ERROR, "unsupported indexqual type: %d",
5090 (int) nodeTag(clause));
5091
5092 return clause;
5093}
static Node * fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol)
Definition: createplan.c:5106
Datum lca(PG_FUNCTION_ARGS)
Definition: ltree_op.c:563
Expr * arg
Definition: primnodes.h:1983

References NullTest::arg, OpExpr::args, ScalarArrayOpExpr::args, Assert(), elog, ERROR, fix_indexqual_operand(), forboth, IsA, RowCompareExpr::largs, lca(), lfirst, lfirst_int, linitial, list_length(), nodeTag, replace_nestloop_params(), and root.

Referenced by fix_indexorderby_references(), and fix_indexqual_references().

◆ fix_indexqual_operand()

static Node * fix_indexqual_operand ( Node node,
IndexOptInfo index,
int  indexcol 
)
static

Definition at line 5106 of file createplan.c.

5107{
5108 Var *result;
5109 int pos;
5110 ListCell *indexpr_item;
5111
5112 /*
5113 * Remove any binary-compatible relabeling of the indexkey
5114 */
5115 if (IsA(node, RelabelType))
5116 node = (Node *) ((RelabelType *) node)->arg;
5117
5118 Assert(indexcol >= 0 && indexcol < index->ncolumns);
5119
5120 if (index->indexkeys[indexcol] != 0)
5121 {
5122 /* It's a simple index column */
5123 if (IsA(node, Var) &&
5124 ((Var *) node)->varno == index->rel->relid &&
5125 ((Var *) node)->varattno == index->indexkeys[indexcol])
5126 {
5127 result = (Var *) copyObject(node);
5128 result->varno = INDEX_VAR;
5129 result->varattno = indexcol + 1;
5130 return (Node *) result;
5131 }
5132 else
5133 elog(ERROR, "index key does not match expected index column");
5134 }
5135
5136 /* It's an index expression, so find and cross-check the expression */
5137 indexpr_item = list_head(index->indexprs);
5138 for (pos = 0; pos < index->ncolumns; pos++)
5139 {
5140 if (index->indexkeys[pos] == 0)
5141 {
5142 if (indexpr_item == NULL)
5143 elog(ERROR, "too few entries in indexprs list");
5144 if (pos == indexcol)
5145 {
5146 Node *indexkey;
5147
5148 indexkey = (Node *) lfirst(indexpr_item);
5149 if (indexkey && IsA(indexkey, RelabelType))
5150 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
5151 if (equal(node, indexkey))
5152 {
5153 result = makeVar(INDEX_VAR, indexcol + 1,
5154 exprType(lfirst(indexpr_item)), -1,
5155 exprCollation(lfirst(indexpr_item)),
5156 0);
5157 return (Node *) result;
5158 }
5159 else
5160 elog(ERROR, "index key does not match expected index column");
5161 }
5162 indexpr_item = lnext(index->indexprs, indexpr_item);
5163 }
5164 }
5165
5166 /* Oops... */
5167 elog(ERROR, "index key does not match expected index column");
5168 return NULL; /* keep compiler quiet */
5169}
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
void * arg
#define INDEX_VAR
Definition: primnodes.h:244

References arg, Assert(), copyObject, elog, equal(), ERROR, exprCollation(), exprType(), INDEX_VAR, IsA, lfirst, list_head(), lnext(), makeVar(), Var::varattno, and Var::varno.

Referenced by fix_indexqual_clause().

◆ fix_indexqual_references()

static void fix_indexqual_references ( PlannerInfo root,
IndexPath index_path,
List **  stripped_indexquals_p,
List **  fixed_indexquals_p 
)
static

Definition at line 4965 of file createplan.c.

4967{
4968 IndexOptInfo *index = index_path->indexinfo;
4969 List *stripped_indexquals;
4970 List *fixed_indexquals;
4971 ListCell *lc;
4972
4973 stripped_indexquals = fixed_indexquals = NIL;
4974
4975 foreach(lc, index_path->indexclauses)
4976 {
4977 IndexClause *iclause = lfirst_node(IndexClause, lc);
4978 int indexcol = iclause->indexcol;
4979 ListCell *lc2;
4980
4981 foreach(lc2, iclause->indexquals)
4982 {
4983 RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc2);
4984 Node *clause = (Node *) rinfo->clause;
4985
4986 stripped_indexquals = lappend(stripped_indexquals, clause);
4987 clause = fix_indexqual_clause(root, index, indexcol,
4988 clause, iclause->indexcols);
4989 fixed_indexquals = lappend(fixed_indexquals, clause);
4990 }
4991 }
4992
4993 *stripped_indexquals_p = stripped_indexquals;
4994 *fixed_indexquals_p = fixed_indexquals;
4995}
AttrNumber indexcol
Definition: pathnodes.h:2008
List * indexcols
Definition: pathnodes.h:2009

References RestrictInfo::clause, fix_indexqual_clause(), IndexPath::indexclauses, IndexClause::indexcol, IndexClause::indexcols, IndexPath::indexinfo, IndexClause::indexquals, lappend(), lfirst_node, NIL, and root.

Referenced by create_indexscan_plan().

◆ get_gating_quals()

static List * get_gating_quals ( PlannerInfo root,
List quals 
)
static

Definition at line 995 of file createplan.c.

996{
997 /* No need to look if we know there are no pseudoconstants */
998 if (!root->hasPseudoConstantQuals)
999 return NIL;
1000
1001 /* Sort into desirable execution order while still in RestrictInfo form */
1002 quals = order_qual_clauses(root, quals);
1003
1004 /* Pull out any pseudoconstant quals from the RestrictInfo list */
1005 return extract_actual_clauses(quals, true);
1006}

References extract_actual_clauses(), NIL, order_qual_clauses(), and root.

Referenced by create_join_plan(), and create_scan_plan().

◆ get_switched_clauses()

static List * get_switched_clauses ( List clauses,
Relids  outerrelids 
)
static

Definition at line 5181 of file createplan.c.

5182{
5183 List *t_list = NIL;
5184 ListCell *l;
5185
5186 foreach(l, clauses)
5187 {
5188 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
5189 OpExpr *clause = (OpExpr *) restrictinfo->clause;
5190
5191 Assert(is_opclause(clause));
5192 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
5193 {
5194 /*
5195 * Duplicate just enough of the structure to allow commuting the
5196 * clause without changing the original list. Could use
5197 * copyObject, but a complete deep copy is overkill.
5198 */
5199 OpExpr *temp = makeNode(OpExpr);
5200
5201 temp->opno = clause->opno;
5202 temp->opfuncid = InvalidOid;
5203 temp->opresulttype = clause->opresulttype;
5204 temp->opretset = clause->opretset;
5205 temp->opcollid = clause->opcollid;
5206 temp->inputcollid = clause->inputcollid;
5207 temp->args = list_copy(clause->args);
5208 temp->location = clause->location;
5209 /* Commute it --- note this modifies the temp node in-place. */
5210 CommuteOpExpr(temp);
5211 t_list = lappend(t_list, temp);
5212 restrictinfo->outer_is_left = false;
5213 }
5214 else
5215 {
5216 Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
5217 t_list = lappend(t_list, clause);
5218 restrictinfo->outer_is_left = true;
5219 }
5220 }
5221 return t_list;
5222}
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:412
void CommuteOpExpr(OpExpr *clause)
Definition: clauses.c:2154
ParseLoc location
Definition: primnodes.h:871

References OpExpr::args, Assert(), bms_is_subset(), RestrictInfo::clause, CommuteOpExpr(), InvalidOid, is_opclause(), lappend(), lfirst, list_copy(), OpExpr::location, makeNode, NIL, and OpExpr::opno.

Referenced by create_hashjoin_plan(), and create_mergejoin_plan().

◆ inject_projection_plan()

static Plan * inject_projection_plan ( Plan subplan,
List tlist,
bool  parallel_safe 
)
static

Definition at line 1959 of file createplan.c.

1960{
1961 Plan *plan;
1962
1963 plan = (Plan *) make_gating_result(tlist, NULL, subplan);
1964
1965 /*
1966 * In principle, we should charge tlist eval cost plus cpu_per_tuple per
1967 * row for the Result node. But the former has probably been factored in
1968 * already and the latter was not accounted for during Path construction,
1969 * so being formally correct might just make the EXPLAIN output look less
1970 * consistent not more so. Hence, just copy the subplan's cost.
1971 */
1972 copy_plan_costsize(plan, subplan);
1973 plan->parallel_safe = parallel_safe;
1974
1975 return plan;
1976}

References copy_plan_costsize(), make_gating_result(), and plan.

Referenced by change_plan_targetlist(), create_append_plan(), create_merge_append_plan(), and prepare_sort_from_pathkeys().

◆ is_projection_capable_path()

bool is_projection_capable_path ( Path path)

Definition at line 7217 of file createplan.c.

7218{
7219 /* Most plan types can project, so just list the ones that can't */
7220 switch (path->pathtype)
7221 {
7222 case T_Hash:
7223 case T_Material:
7224 case T_Memoize:
7225 case T_Sort:
7226 case T_IncrementalSort:
7227 case T_Unique:
7228 case T_SetOp:
7229 case T_LockRows:
7230 case T_Limit:
7231 case T_ModifyTable:
7232 case T_MergeAppend:
7233 case T_RecursiveUnion:
7234 return false;
7235 case T_CustomScan:
7237 return true;
7238 return false;
7239 case T_Append:
7240
7241 /*
7242 * Append can't project, but if an AppendPath is being used to
7243 * represent a dummy path, what will actually be generated is a
7244 * Result which can project.
7245 */
7246 return IS_DUMMY_APPEND(path);
7247 case T_ProjectSet:
7248
7249 /*
7250 * Although ProjectSet certainly projects, say "no" because we
7251 * don't want the planner to randomly replace its tlist with
7252 * something else; the SRFs have to stay at top level. This might
7253 * get relaxed later.
7254 */
7255 return false;
7256 default:
7257 break;
7258 }
7259 return true;
7260}
#define CUSTOMPATH_SUPPORT_PROJECTION
Definition: extensible.h:86
#define IS_DUMMY_APPEND(p)
Definition: pathnodes.h:2185

References castNode, CUSTOMPATH_SUPPORT_PROJECTION, IS_DUMMY_APPEND, and Path::pathtype.

Referenced by add_paths_with_pathkeys_for_rel(), apply_projection_to_path(), create_projection_path(), and create_projection_plan().

◆ is_projection_capable_plan()

bool is_projection_capable_plan ( Plan plan)

Definition at line 7267 of file createplan.c.

7268{
7269 /* Most plan types can project, so just list the ones that can't */
7270 switch (nodeTag(plan))
7271 {
7272 case T_Hash:
7273 case T_Material:
7274 case T_Memoize:
7275 case T_Sort:
7276 case T_Unique:
7277 case T_SetOp:
7278 case T_LockRows:
7279 case T_Limit:
7280 case T_ModifyTable:
7281 case T_Append:
7282 case T_MergeAppend:
7283 case T_RecursiveUnion:
7284 return false;
7285 case T_CustomScan:
7287 return true;
7288 return false;
7289 case T_ProjectSet:
7290
7291 /*
7292 * Although ProjectSet certainly projects, say "no" because we
7293 * don't want the planner to randomly replace its tlist with
7294 * something else; the SRFs have to stay at top level. This might
7295 * get relaxed later.
7296 */
7297 return false;
7298 default:
7299 break;
7300 }
7301 return true;
7302}

References CUSTOMPATH_SUPPORT_PROJECTION, nodeTag, and plan.

Referenced by change_plan_targetlist(), create_projection_plan(), and prepare_sort_from_pathkeys().

◆ label_incrementalsort_with_costsize()

static void label_incrementalsort_with_costsize ( PlannerInfo root,
IncrementalSort plan,
List pathkeys,
double  limit_tuples 
)
static

Definition at line 5419 of file createplan.c.

5421{
5422 Plan *lefttree = plan->sort.plan.lefttree;
5423 Path sort_path; /* dummy for result of cost_incremental_sort */
5424
5426
5427 cost_incremental_sort(&sort_path, root, pathkeys,
5428 plan->nPresortedCols,
5429 plan->sort.plan.disabled_nodes,
5430 lefttree->startup_cost,
5431 lefttree->total_cost,
5432 lefttree->plan_rows,
5433 lefttree->plan_width,
5434 0.0,
5435 work_mem,
5436 limit_tuples);
5437 plan->sort.plan.startup_cost = sort_path.startup_cost;
5438 plan->sort.plan.total_cost = sort_path.total_cost;
5439 plan->sort.plan.plan_rows = lefttree->plan_rows;
5440 plan->sort.plan.plan_width = lefttree->plan_width;
5441 plan->sort.plan.parallel_aware = false;
5442 plan->sort.plan.parallel_safe = lefttree->parallel_safe;
5443}
void cost_incremental_sort(Path *path, PlannerInfo *root, List *pathkeys, int presorted_keys, int input_disabled_nodes, Cost input_startup_cost, Cost input_total_cost, double input_tuples, int width, Cost comparison_cost, int sort_mem, double limit_tuples)
Definition: costsize.c:1974
int work_mem
Definition: globals.c:131

References Assert(), cost_incremental_sort(), IsA, Plan::parallel_safe, plan, Plan::plan_rows, Plan::plan_width, root, Path::startup_cost, Plan::startup_cost, Path::total_cost, Plan::total_cost, and work_mem.

Referenced by create_append_plan(), create_merge_append_plan(), and create_mergejoin_plan().

◆ label_sort_with_costsize()

static void label_sort_with_costsize ( PlannerInfo root,
Sort plan,
double  limit_tuples 
)
static

Definition at line 5391 of file createplan.c.

5392{
5393 Plan *lefttree = plan->plan.lefttree;
5394 Path sort_path; /* dummy for result of cost_sort */
5395
5396 Assert(IsA(plan, Sort));
5397
5398 cost_sort(&sort_path, root, NIL,
5399 plan->plan.disabled_nodes,
5400 lefttree->total_cost,
5401 lefttree->plan_rows,
5402 lefttree->plan_width,
5403 0.0,
5404 work_mem,
5405 limit_tuples);
5406 plan->plan.startup_cost = sort_path.startup_cost;
5407 plan->plan.total_cost = sort_path.total_cost;
5408 plan->plan.plan_rows = lefttree->plan_rows;
5409 plan->plan.plan_width = lefttree->plan_width;
5410 plan->plan.parallel_aware = false;
5411 plan->plan.parallel_safe = lefttree->parallel_safe;
5412}
void cost_sort(Path *path, PlannerInfo *root, List *pathkeys, int input_disabled_nodes, Cost input_cost, double tuples, int width, Cost comparison_cost, int sort_mem, double limit_tuples)
Definition: costsize.c:2118

References Assert(), cost_sort(), IsA, NIL, Plan::parallel_safe, plan, Plan::plan_rows, Plan::plan_width, root, Path::startup_cost, Path::total_cost, Plan::total_cost, and work_mem.

Referenced by create_append_plan(), create_merge_append_plan(), and create_mergejoin_plan().

◆ make_agg()

Agg * make_agg ( List tlist,
List qual,
AggStrategy  aggstrategy,
AggSplit  aggsplit,
int  numGroupCols,
AttrNumber grpColIdx,
Oid grpOperators,
Oid grpCollations,
List groupingSets,
List chain,
Cardinality  numGroups,
Size  transitionSpace,
Plan lefttree 
)

Definition at line 6574 of file createplan.c.

6579{
6580 Agg *node = makeNode(Agg);
6581 Plan *plan = &node->plan;
6582
6583 node->aggstrategy = aggstrategy;
6584 node->aggsplit = aggsplit;
6585 node->numCols = numGroupCols;
6586 node->grpColIdx = grpColIdx;
6587 node->grpOperators = grpOperators;
6588 node->grpCollations = grpCollations;
6589 node->numGroups = numGroups;
6590 node->transitionSpace = transitionSpace;
6591 node->aggParams = NULL; /* SS_finalize_plan() will fill this */
6592 node->groupingSets = groupingSets;
6593 node->chain = chain;
6594
6595 plan->qual = qual;
6596 plan->targetlist = tlist;
6597 plan->lefttree = lefttree;
6598 plan->righttree = NULL;
6599
6600 return node;
6601}
AggSplit aggsplit
Definition: plannodes.h:1196
List * chain
Definition: plannodes.h:1223
List * groupingSets
Definition: plannodes.h:1220
Bitmapset * aggParams
Definition: plannodes.h:1215
Cardinality numGroups
Definition: plannodes.h:1209
Plan plan
Definition: plannodes.h:1190
int numCols
Definition: plannodes.h:1199
uint64 transitionSpace
Definition: plannodes.h:1212
AggStrategy aggstrategy
Definition: plannodes.h:1193

References Agg::aggParams, Agg::aggsplit, Agg::aggstrategy, Agg::chain, Agg::groupingSets, makeNode, Agg::numCols, Agg::numGroups, Agg::plan, plan, and Agg::transitionSpace.

Referenced by create_agg_plan(), and create_groupingsets_plan().

◆ make_bitmap_and()

static BitmapAnd * make_bitmap_and ( List bitmapplans)
static

Definition at line 5891 of file createplan.c.

5892{
5893 BitmapAnd *node = makeNode(BitmapAnd);
5894 Plan *plan = &node->plan;
5895
5896 plan->targetlist = NIL;
5897 plan->qual = NIL;
5898 plan->lefttree = NULL;
5899 plan->righttree = NULL;
5900 node->bitmapplans = bitmapplans;
5901
5902 return node;
5903}
Plan plan
Definition: plannodes.h:491
List * bitmapplans
Definition: plannodes.h:492

References BitmapAnd::bitmapplans, makeNode, NIL, BitmapAnd::plan, and plan.

Referenced by create_bitmap_subplan().

◆ make_bitmap_heapscan()

static BitmapHeapScan * make_bitmap_heapscan ( List qptlist,
List qpqual,
Plan lefttree,
List bitmapqualorig,
Index  scanrelid 
)
static

Definition at line 5598 of file createplan.c.

5603{
5605 Plan *plan = &node->scan.plan;
5606
5607 plan->targetlist = qptlist;
5608 plan->qual = qpqual;
5609 plan->lefttree = lefttree;
5610 plan->righttree = NULL;
5611 node->scan.scanrelid = scanrelid;
5612 node->bitmapqualorig = bitmapqualorig;
5613
5614 return node;
5615}
List * bitmapqualorig
Definition: plannodes.h:693

References BitmapHeapScan::bitmapqualorig, makeNode, plan, BitmapHeapScan::scan, and Scan::scanrelid.

Referenced by create_bitmap_scan_plan().

◆ make_bitmap_indexscan()

static BitmapIndexScan * make_bitmap_indexscan ( Index  scanrelid,
Oid  indexid,
List indexqual,
List indexqualorig 
)
static

Definition at line 5577 of file createplan.c.

5581{
5583 Plan *plan = &node->scan.plan;
5584
5585 plan->targetlist = NIL; /* not used */
5586 plan->qual = NIL; /* not used */
5587 plan->lefttree = NULL;
5588 plan->righttree = NULL;
5589 node->scan.scanrelid = scanrelid;
5590 node->indexid = indexid;
5591 node->indexqual = indexqual;
5592 node->indexqualorig = indexqualorig;
5593
5594 return node;
5595}
List * indexqualorig
Definition: plannodes.h:677
List * indexqual
Definition: plannodes.h:675

References BitmapIndexScan::indexid, BitmapIndexScan::indexqual, BitmapIndexScan::indexqualorig, makeNode, NIL, plan, BitmapIndexScan::scan, and Scan::scanrelid.

Referenced by create_bitmap_subplan().

◆ make_bitmap_or()

static BitmapOr * make_bitmap_or ( List bitmapplans)
static

Definition at line 5906 of file createplan.c.

5907{
5908 BitmapOr *node = makeNode(BitmapOr);
5909 Plan *plan = &node->plan;
5910
5911 plan->targetlist = NIL;
5912 plan->qual = NIL;
5913 plan->lefttree = NULL;
5914 plan->righttree = NULL;
5915 node->bitmapplans = bitmapplans;
5916
5917 return node;
5918}
List * bitmapplans
Definition: plannodes.h:507
Plan plan
Definition: plannodes.h:505

References BitmapOr::bitmapplans, makeNode, NIL, BitmapOr::plan, and plan.

Referenced by create_bitmap_subplan().

◆ make_ctescan()

static CteScan * make_ctescan ( List qptlist,
List qpqual,
Index  scanrelid,
int  ctePlanId,
int  cteParam 
)
static

Definition at line 5735 of file createplan.c.

5740{
5741 CteScan *node = makeNode(CteScan);
5742 Plan *plan = &node->scan.plan;
5743
5744 plan->targetlist = qptlist;
5745 plan->qual = qpqual;
5746 plan->lefttree = NULL;
5747 plan->righttree = NULL;
5748 node->scan.scanrelid = scanrelid;
5749 node->ctePlanId = ctePlanId;
5750 node->cteParam = cteParam;
5751
5752 return node;
5753}
int ctePlanId
Definition: plannodes.h:802
int cteParam
Definition: plannodes.h:804

References CteScan::cteParam, CteScan::ctePlanId, makeNode, plan, CteScan::scan, and Scan::scanrelid.

Referenced by create_ctescan_plan().

◆ make_foreignscan()

ForeignScan * make_foreignscan ( List qptlist,
List qpqual,
Index  scanrelid,
List fdw_exprs,
List fdw_private,
List fdw_scan_tlist,
List fdw_recheck_quals,
Plan outer_plan 
)

Definition at line 5795 of file createplan.c.

5803{
5805 Plan *plan = &node->scan.plan;
5806
5807 /* cost will be filled in by create_foreignscan_plan */
5808 plan->targetlist = qptlist;
5809 plan->qual = qpqual;
5810 plan->lefttree = outer_plan;
5811 plan->righttree = NULL;
5812 node->scan.scanrelid = scanrelid;
5813
5814 /* these may be overridden by the FDW's PlanDirectModify callback. */
5815 node->operation = CMD_SELECT;
5816 node->resultRelation = 0;
5817
5818 /* checkAsUser, fs_server will be filled in by create_foreignscan_plan */
5819 node->checkAsUser = InvalidOid;
5820 node->fs_server = InvalidOid;
5821 node->fdw_exprs = fdw_exprs;
5822 node->fdw_private = fdw_private;
5823 node->fdw_scan_tlist = fdw_scan_tlist;
5824 node->fdw_recheck_quals = fdw_recheck_quals;
5825 /* fs_relids, fs_base_relids will be filled by create_foreignscan_plan */
5826 node->fs_relids = NULL;
5827 node->fs_base_relids = NULL;
5828 /* fsSystemCol will be filled in by create_foreignscan_plan */
5829 node->fsSystemCol = false;
5830
5831 return node;
5832}
@ CMD_SELECT
Definition: nodes.h:275
CmdType operation
Definition: plannodes.h:873
List * fdw_private
Definition: plannodes.h:883
Index resultRelation
Definition: plannodes.h:875
List * fdw_scan_tlist
Definition: plannodes.h:885

References ForeignScan::checkAsUser, CMD_SELECT, ForeignScan::fdw_exprs, ForeignScan::fdw_private, ForeignScan::fdw_recheck_quals, ForeignScan::fdw_scan_tlist, ForeignScan::fs_base_relids, ForeignScan::fs_relids, ForeignScan::fs_server, ForeignScan::fsSystemCol, InvalidOid, makeNode, ForeignScan::operation, plan, ForeignScan::resultRelation, ForeignScan::scan, and Scan::scanrelid.

Referenced by fileGetForeignPlan(), and postgresGetForeignPlan().

◆ make_functionscan()

static FunctionScan * make_functionscan ( List qptlist,
List qpqual,
Index  scanrelid,
List functions,
bool  funcordinality 
)
static

Definition at line 5676 of file createplan.c.

5681{
5683 Plan *plan = &node->scan.plan;
5684
5685 plan->targetlist = qptlist;
5686 plan->qual = qpqual;
5687 plan->lefttree = NULL;
5688 plan->righttree = NULL;
5689 node->scan.scanrelid = scanrelid;
5690 node->functions = functions;
5691 node->funcordinality = funcordinality;
5692
5693 return node;
5694}
List * functions
Definition: plannodes.h:767
bool funcordinality
Definition: plannodes.h:769

References FunctionScan::funcordinality, functions, FunctionScan::functions, makeNode, plan, FunctionScan::scan, and Scan::scanrelid.

Referenced by create_functionscan_plan().

◆ make_gather()

static Gather * make_gather ( List qptlist,
List qpqual,
int  nworkers,
int  rescan_param,
bool  single_copy,
Plan subplan 
)
static

Definition at line 6782 of file createplan.c.

6788{
6789 Gather *node = makeNode(Gather);
6790 Plan *plan = &node->plan;
6791
6792 plan->targetlist = qptlist;
6793 plan->qual = qpqual;
6794 plan->lefttree = subplan;
6795 plan->righttree = NULL;
6796 node->num_workers = nworkers;
6797 node->rescan_param = rescan_param;
6798 node->single_copy = single_copy;
6799 node->invisible = false;
6800 node->initParam = NULL;
6801
6802 return node;
6803}
int num_workers
Definition: plannodes.h:1339
bool invisible
Definition: plannodes.h:1345
Bitmapset * initParam
Definition: plannodes.h:1351
bool single_copy
Definition: plannodes.h:1343
int rescan_param
Definition: plannodes.h:1341

References Gather::initParam, Gather::invisible, makeNode, Gather::num_workers, Gather::plan, plan, Gather::rescan_param, and Gather::single_copy.

Referenced by create_gather_plan().

◆ make_gating_result()

static Result * make_gating_result ( List tlist,
Node resconstantqual,
Plan subplan 
)
static

Definition at line 6922 of file createplan.c.

6925{
6926 Result *node = makeNode(Result);
6927 Plan *plan = &node->plan;
6928
6929 Assert(subplan != NULL);
6930
6931 plan->targetlist = tlist;
6932 plan->qual = NIL;
6933 plan->lefttree = subplan;
6934 plan->righttree = NULL;
6936 node->resconstantqual = resconstantqual;
6937 node->relids = NULL;
6938
6939 return node;
6940}
@ RESULT_TYPE_GATING
Definition: plannodes.h:271
Node * resconstantqual
Definition: plannodes.h:299

References Assert(), makeNode, NIL, Result::plan, plan, Result::relids, Result::resconstantqual, Result::result_type, and RESULT_TYPE_GATING.

Referenced by create_gating_plan(), create_projection_plan(), and inject_projection_plan().

◆ make_group()

static Group * make_group ( List tlist,
List qual,
int  numGroupCols,
AttrNumber grpColIdx,
Oid grpOperators,
Oid grpCollations,
Plan lefttree 
)
static

Definition at line 6644 of file createplan.c.

6651{
6652 Group *node = makeNode(Group);
6653 Plan *plan = &node->plan;
6654
6655 node->numCols = numGroupCols;
6656 node->grpColIdx = grpColIdx;
6657 node->grpOperators = grpOperators;
6658 node->grpCollations = grpCollations;
6659
6660 plan->qual = qual;
6661 plan->targetlist = tlist;
6662 plan->lefttree = lefttree;
6663 plan->righttree = NULL;
6664
6665 return node;
6666}
int numCols
Definition: plannodes.h:1164
Plan plan
Definition: plannodes.h:1161

References makeNode, Group::numCols, Group::plan, and plan.

Referenced by create_group_plan().

◆ make_hash()

static Hash * make_hash ( Plan lefttree,
List hashkeys,
Oid  skewTable,
AttrNumber  skewColumn,
bool  skewInherit 
)
static

Definition at line 5977 of file createplan.c.

5982{
5983 Hash *node = makeNode(Hash);
5984 Plan *plan = &node->plan;
5985
5986 plan->targetlist = lefttree->targetlist;
5987 plan->qual = NIL;
5988 plan->lefttree = lefttree;
5989 plan->righttree = NULL;
5990
5991 node->hashkeys = hashkeys;
5992 node->skewTable = skewTable;
5993 node->skewColumn = skewColumn;
5994 node->skewInherit = skewInherit;
5995
5996 return node;
5997}
AttrNumber skewColumn
Definition: plannodes.h:1413
List * hashkeys
Definition: plannodes.h:1409
Oid skewTable
Definition: plannodes.h:1411
bool skewInherit
Definition: plannodes.h:1415

References Hash::hashkeys, makeNode, NIL, Hash::plan, plan, Hash::skewColumn, Hash::skewInherit, Hash::skewTable, and Plan::targetlist.

Referenced by create_hashjoin_plan().

◆ make_hashjoin()

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 
)
static

Definition at line 5946 of file createplan.c.

5957{
5958 HashJoin *node = makeNode(HashJoin);
5959 Plan *plan = &node->join.plan;
5960
5961 plan->targetlist = tlist;
5962 plan->qual = otherclauses;
5963 plan->lefttree = lefttree;
5964 plan->righttree = righttree;
5965 node->hashclauses = hashclauses;
5966 node->hashoperators = hashoperators;
5967 node->hashcollations = hashcollations;
5968 node->hashkeys = hashkeys;
5969 node->join.jointype = jointype;
5970 node->join.inner_unique = inner_unique;
5971 node->join.joinqual = joinclauses;
5972
5973 return node;
5974}
List * hashcollations
Definition: plannodes.h:1048
List * hashclauses
Definition: plannodes.h:1046
List * hashoperators
Definition: plannodes.h:1047
List * hashkeys
Definition: plannodes.h:1054
List * joinqual
Definition: plannodes.h:971
JoinType jointype
Definition: plannodes.h:968
bool inner_unique
Definition: plannodes.h:969

References HashJoin::hashclauses, HashJoin::hashcollations, HashJoin::hashkeys, HashJoin::hashoperators, Join::inner_unique, HashJoin::join, Join::joinqual, Join::jointype, makeNode, and plan.

Referenced by create_hashjoin_plan().

◆ make_incrementalsort()

static IncrementalSort * make_incrementalsort ( Plan lefttree,
int  numCols,
int  nPresortedCols,
AttrNumber sortColIdx,
Oid sortOperators,
Oid collations,
bool *  nullsFirst 
)
static

Definition at line 6072 of file createplan.c.

6075{
6076 IncrementalSort *node;
6077 Plan *plan;
6078
6079 node = makeNode(IncrementalSort);
6080
6081 plan = &node->sort.plan;
6082 plan->targetlist = lefttree->targetlist;
6083 plan->qual = NIL;
6084 plan->lefttree = lefttree;
6085 plan->righttree = NULL;
6086 node->nPresortedCols = nPresortedCols;
6087 node->sort.numCols = numCols;
6088 node->sort.sortColIdx = sortColIdx;
6089 node->sort.sortOperators = sortOperators;
6090 node->sort.collations = collations;
6091 node->sort.nullsFirst = nullsFirst;
6092
6093 return node;
6094}
int numCols
Definition: plannodes.h:1127
Plan plan
Definition: plannodes.h:1124

References makeNode, NIL, IncrementalSort::nPresortedCols, Sort::numCols, Sort::plan, plan, IncrementalSort::sort, and Plan::targetlist.

Referenced by create_append_plan(), create_merge_append_plan(), and make_incrementalsort_from_pathkeys().

◆ make_incrementalsort_from_pathkeys()

static IncrementalSort * make_incrementalsort_from_pathkeys ( Plan lefttree,
List pathkeys,
Relids  relids,
int  nPresortedCols 
)
static

Definition at line 6355 of file createplan.c.

6357{
6358 int numsortkeys;
6359 AttrNumber *sortColIdx;
6360 Oid *sortOperators;
6361 Oid *collations;
6362 bool *nullsFirst;
6363
6364 /* Compute sort column info, and adjust lefttree as needed */
6365 lefttree = prepare_sort_from_pathkeys(lefttree, pathkeys,
6366 relids,
6367 NULL,
6368 false,
6369 &numsortkeys,
6370 &sortColIdx,
6371 &sortOperators,
6372 &collations,
6373 &nullsFirst);
6374
6375 /* Now build the Sort node */
6376 return make_incrementalsort(lefttree, numsortkeys, nPresortedCols,
6377 sortColIdx, sortOperators,
6378 collations, nullsFirst);
6379}

References make_incrementalsort(), and prepare_sort_from_pathkeys().

Referenced by create_incrementalsort_plan(), and create_mergejoin_plan().

◆ make_indexonlyscan()

static IndexOnlyScan * make_indexonlyscan ( List qptlist,
List qpqual,
Index  scanrelid,
Oid  indexid,
List indexqual,
List recheckqual,
List indexorderby,
List indextlist,
ScanDirection  indexscandir 
)
static

Definition at line 5548 of file createplan.c.

5557{
5559 Plan *plan = &node->scan.plan;
5560
5561 plan->targetlist = qptlist;
5562 plan->qual = qpqual;
5563 plan->lefttree = NULL;
5564 plan->righttree = NULL;
5565 node->scan.scanrelid = scanrelid;
5566 node->indexid = indexid;
5567 node->indexqual = indexqual;
5568 node->recheckqual = recheckqual;
5569 node->indexorderby = indexorderby;
5570 node->indextlist = indextlist;
5571 node->indexorderdir = indexscandir;
5572
5573 return node;
5574}
List * indexqual
Definition: plannodes.h:639
List * recheckqual
Definition: plannodes.h:641
List * indextlist
Definition: plannodes.h:645
ScanDirection indexorderdir
Definition: plannodes.h:647
List * indexorderby
Definition: plannodes.h:643

References IndexOnlyScan::indexid, IndexOnlyScan::indexorderby, IndexOnlyScan::indexorderdir, IndexOnlyScan::indexqual, IndexOnlyScan::indextlist, makeNode, plan, IndexOnlyScan::recheckqual, IndexOnlyScan::scan, and Scan::scanrelid.

Referenced by create_indexscan_plan().

◆ make_indexscan()

static IndexScan * make_indexscan ( List qptlist,
List qpqual,
Index  scanrelid,
Oid  indexid,
List indexqual,
List indexqualorig,
List indexorderby,
List indexorderbyorig,
List indexorderbyops,
ScanDirection  indexscandir 
)
static

Definition at line 5517 of file createplan.c.

5527{
5528 IndexScan *node = makeNode(IndexScan);
5529 Plan *plan = &node->scan.plan;
5530
5531 plan->targetlist = qptlist;
5532 plan->qual = qpqual;
5533 plan->lefttree = NULL;
5534 plan->righttree = NULL;
5535 node->scan.scanrelid = scanrelid;
5536 node->indexid = indexid;
5537 node->indexqual = indexqual;
5538 node->indexqualorig = indexqualorig;
5539 node->indexorderby = indexorderby;
5540 node->indexorderbyorig = indexorderbyorig;
5541 node->indexorderbyops = indexorderbyops;
5542 node->indexorderdir = indexscandir;
5543
5544 return node;
5545}
List * indexorderby
Definition: plannodes.h:593
List * indexorderbyops
Definition: plannodes.h:597
ScanDirection indexorderdir
Definition: plannodes.h:599
List * indexorderbyorig
Definition: plannodes.h:595

References IndexScan::indexid, IndexScan::indexorderby, IndexScan::indexorderbyops, IndexScan::indexorderbyorig, IndexScan::indexorderdir, IndexScan::indexqual, IndexScan::indexqualorig, makeNode, plan, IndexScan::scan, and Scan::scanrelid.

Referenced by create_indexscan_plan().

◆ make_limit()

Limit * make_limit ( Plan lefttree,
Node limitOffset,
Node limitCount,
LimitOption  limitOption,
int  uniqNumCols,
AttrNumber uniqColIdx,
Oid uniqOperators,
Oid uniqCollations 
)

Definition at line 6893 of file createplan.c.

6896{
6897 Limit *node = makeNode(Limit);
6898 Plan *plan = &node->plan;
6899
6900 plan->targetlist = lefttree->targetlist;
6901 plan->qual = NIL;
6902 plan->lefttree = lefttree;
6903 plan->righttree = NULL;
6904
6905 node->limitOffset = limitOffset;
6906 node->limitCount = limitCount;
6907 node->limitOption = limitOption;
6908 node->uniqNumCols = uniqNumCols;
6909 node->uniqColIdx = uniqColIdx;
6910 node->uniqOperators = uniqOperators;
6911 node->uniqCollations = uniqCollations;
6912
6913 return node;
6914}
LimitOption limitOption
Definition: plannodes.h:1488
Plan plan
Definition: plannodes.h:1479
Node * limitCount
Definition: plannodes.h:1485
int uniqNumCols
Definition: plannodes.h:1491
Node * limitOffset
Definition: plannodes.h:1482

References Limit::limitCount, Limit::limitOffset, Limit::limitOption, makeNode, NIL, Limit::plan, plan, Plan::targetlist, and Limit::uniqNumCols.

Referenced by create_limit_plan(), and create_minmaxagg_plan().

◆ make_lockrows()

static LockRows * make_lockrows ( Plan lefttree,
List rowMarks,
int  epqParam 
)
static

Definition at line 6872 of file createplan.c.

6873{
6874 LockRows *node = makeNode(LockRows);
6875 Plan *plan = &node->plan;
6876
6877 plan->targetlist = lefttree->targetlist;
6878 plan->qual = NIL;
6879 plan->lefttree = lefttree;
6880 plan->righttree = NULL;
6881
6882 node->rowMarks = rowMarks;
6883 node->epqParam = epqParam;
6884
6885 return node;
6886}
int epqParam
Definition: plannodes.h:1467
List * rowMarks
Definition: plannodes.h:1465
Plan plan
Definition: plannodes.h:1463

References LockRows::epqParam, makeNode, NIL, LockRows::plan, plan, LockRows::rowMarks, and Plan::targetlist.

Referenced by create_lockrows_plan().

◆ make_material()

static Material * make_material ( Plan lefttree)
static

Definition at line 6479 of file createplan.c.

6480{
6481 Material *node = makeNode(Material);
6482 Plan *plan = &node->plan;
6483
6484 plan->targetlist = lefttree->targetlist;
6485 plan->qual = NIL;
6486 plan->lefttree = lefttree;
6487 plan->righttree = NULL;
6488
6489 return node;
6490}
Plan plan
Definition: plannodes.h:1063

References makeNode, NIL, Material::plan, plan, and Plan::targetlist.

Referenced by create_material_plan(), create_mergejoin_plan(), and materialize_finished_plan().

◆ make_memoize()

static Memoize * make_memoize ( Plan lefttree,
Oid hashoperators,
Oid collations,
List param_exprs,
bool  singlerow,
bool  binary_mode,
uint32  est_entries,
Bitmapset keyparamids,
Cardinality  est_calls,
Cardinality  est_unique_keys,
double  est_hit_ratio 
)
static

Definition at line 6544 of file createplan.c.

6549{
6550 Memoize *node = makeNode(Memoize);
6551 Plan *plan = &node->plan;
6552
6553 plan->targetlist = lefttree->targetlist;
6554 plan->qual = NIL;
6555 plan->lefttree = lefttree;
6556 plan->righttree = NULL;
6557
6558 node->numKeys = list_length(param_exprs);
6559 node->hashOperators = hashoperators;
6560 node->collations = collations;
6561 node->param_exprs = param_exprs;
6562 node->singlerow = singlerow;
6563 node->binary_mode = binary_mode;
6564 node->est_entries = est_entries;
6565 node->keyparamids = keyparamids;
6566 node->est_calls = est_calls;
6567 node->est_unique_keys = est_unique_keys;
6568 node->est_hit_ratio = est_hit_ratio;
6569
6570 return node;
6571}
Plan plan
Definition: plannodes.h:1072
bool singlerow
Definition: plannodes.h:1090
Cardinality est_calls
Definition: plannodes.h:1108
Bitmapset * keyparamids
Definition: plannodes.h:1105
bool binary_mode
Definition: plannodes.h:1096
int numKeys
Definition: plannodes.h:1075
Cardinality est_unique_keys
Definition: plannodes.h:1111
List * param_exprs
Definition: plannodes.h:1084
double est_hit_ratio
Definition: plannodes.h:1114
uint32 est_entries
Definition: plannodes.h:1102

References Memoize::binary_mode, Memoize::est_calls, Memoize::est_entries, Memoize::est_hit_ratio, Memoize::est_unique_keys, Memoize::keyparamids, list_length(), makeNode, NIL, Memoize::numKeys, Memoize::param_exprs, Memoize::plan, plan, Memoize::singlerow, and Plan::targetlist.

Referenced by create_memoize_plan().

◆ make_mergejoin()

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 
)
static

Definition at line 6000 of file createplan.c.

6013{
6014 MergeJoin *node = makeNode(MergeJoin);
6015 Plan *plan = &node->join.plan;
6016
6017 plan->targetlist = tlist;
6018 plan->qual = otherclauses;
6019 plan->lefttree = lefttree;
6020 plan->righttree = righttree;
6021 node->skip_mark_restore = skip_mark_restore;
6022 node->mergeclauses = mergeclauses;
6023 node->mergeFamilies = mergefamilies;
6024 node->mergeCollations = mergecollations;
6025 node->mergeReversals = mergereversals;
6026 node->mergeNullsFirst = mergenullsfirst;
6027 node->join.jointype = jointype;
6028 node->join.inner_unique = inner_unique;
6029 node->join.joinqual = joinclauses;
6030
6031 return node;
6032}
List * mergeclauses
Definition: plannodes.h:1022
bool skip_mark_restore
Definition: plannodes.h:1019

References Join::inner_unique, MergeJoin::join, Join::joinqual, Join::jointype, makeNode, MergeJoin::mergeclauses, plan, and MergeJoin::skip_mark_restore.

Referenced by create_mergejoin_plan().

◆ make_modifytable()

static ModifyTable * make_modifytable ( PlannerInfo root,
Plan subplan,
CmdType  operation,
bool  canSetTag,
Index  nominalRelation,
Index  rootRelation,
List resultRelations,
List updateColnosLists,
List withCheckOptionLists,
List returningLists,
List rowMarks,
OnConflictExpr onconflict,
List mergeActionLists,
List mergeJoinConditions,
int  epqParam 
)
static

Definition at line 6996 of file createplan.c.

7005{
7007 bool returning_old_or_new = false;
7008 bool returning_old_or_new_valid = false;
7009 bool transition_tables = false;
7010 bool transition_tables_valid = false;
7011 List *fdw_private_list;
7012 Bitmapset *direct_modify_plans;
7013 ListCell *lc;
7014 int i;
7015
7016 Assert(operation == CMD_MERGE ||
7017 (operation == CMD_UPDATE ?
7018 list_length(resultRelations) == list_length(updateColnosLists) :
7019 updateColnosLists == NIL));
7020 Assert(withCheckOptionLists == NIL ||
7021 list_length(resultRelations) == list_length(withCheckOptionLists));
7022 Assert(returningLists == NIL ||
7023 list_length(resultRelations) == list_length(returningLists));
7024
7025 node->plan.lefttree = subplan;
7026 node->plan.righttree = NULL;
7027 node->plan.qual = NIL;
7028 /* setrefs.c will fill in the targetlist, if needed */
7029 node->plan.targetlist = NIL;
7030
7031 node->operation = operation;
7032 node->canSetTag = canSetTag;
7033 node->nominalRelation = nominalRelation;
7034 node->rootRelation = rootRelation;
7035 node->resultRelations = resultRelations;
7036 if (!onconflict)
7037 {
7039 node->onConflictSet = NIL;
7040 node->onConflictCols = NIL;
7041 node->onConflictWhere = NULL;
7042 node->arbiterIndexes = NIL;
7043 node->exclRelRTI = 0;
7044 node->exclRelTlist = NIL;
7045 }
7046 else
7047 {
7048 node->onConflictAction = onconflict->action;
7049
7050 /*
7051 * Here we convert the ON CONFLICT UPDATE tlist, if any, to the
7052 * executor's convention of having consecutive resno's. The actual
7053 * target column numbers are saved in node->onConflictCols. (This
7054 * could be done earlier, but there seems no need to.)
7055 */
7056 node->onConflictSet = onconflict->onConflictSet;
7057 node->onConflictCols =
7059 node->onConflictWhere = onconflict->onConflictWhere;
7060
7061 /*
7062 * If a set of unique index inference elements was provided (an
7063 * INSERT...ON CONFLICT "inference specification"), then infer
7064 * appropriate unique indexes (or throw an error if none are
7065 * available).
7066 */
7068
7069 node->exclRelRTI = onconflict->exclRelIndex;
7070 node->exclRelTlist = onconflict->exclRelTlist;
7071 }
7072 node->updateColnosLists = updateColnosLists;
7073 node->withCheckOptionLists = withCheckOptionLists;
7074 node->returningOldAlias = root->parse->returningOldAlias;
7075 node->returningNewAlias = root->parse->returningNewAlias;
7076 node->returningLists = returningLists;
7077 node->rowMarks = rowMarks;
7078 node->mergeActionLists = mergeActionLists;
7079 node->mergeJoinConditions = mergeJoinConditions;
7080 node->epqParam = epqParam;
7081
7082 /*
7083 * For each result relation that is a foreign table, allow the FDW to
7084 * construct private plan data, and accumulate it all into a list.
7085 */
7086 fdw_private_list = NIL;
7087 direct_modify_plans = NULL;
7088 i = 0;
7089 foreach(lc, resultRelations)
7090 {
7091 Index rti = lfirst_int(lc);
7092 FdwRoutine *fdwroutine;
7093 List *fdw_private;
7094 bool direct_modify;
7095
7096 /*
7097 * If possible, we want to get the FdwRoutine from our RelOptInfo for
7098 * the table. But sometimes we don't have a RelOptInfo and must get
7099 * it the hard way. (In INSERT, the target relation is not scanned,
7100 * so it's not a baserel; and there are also corner cases for
7101 * updatable views where the target rel isn't a baserel.)
7102 */
7103 if (rti < root->simple_rel_array_size &&
7104 root->simple_rel_array[rti] != NULL)
7105 {
7106 RelOptInfo *resultRel = root->simple_rel_array[rti];
7107
7108 fdwroutine = resultRel->fdwroutine;
7109 }
7110 else
7111 {
7112 RangeTblEntry *rte = planner_rt_fetch(rti, root);
7113
7114 if (rte->rtekind == RTE_RELATION &&
7115 rte->relkind == RELKIND_FOREIGN_TABLE)
7116 {
7117 /* Check if the access to foreign tables is restricted */
7119 {
7120 /* there must not be built-in foreign tables */
7121 Assert(rte->relid >= FirstNormalObjectId);
7122 ereport(ERROR,
7123 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
7124 errmsg("access to non-system foreign table is restricted")));
7125 }
7126
7127 fdwroutine = GetFdwRoutineByRelId(rte->relid);
7128 }
7129 else
7130 fdwroutine = NULL;
7131 }
7132
7133 /*
7134 * MERGE is not currently supported for foreign tables. We already
7135 * checked that when the table mentioned in the query is foreign; but
7136 * we can still get here if a partitioned table has a foreign table as
7137 * partition. Disallow that now, to avoid an uglier error message
7138 * later.
7139 */
7140 if (operation == CMD_MERGE && fdwroutine != NULL)
7141 {
7142 RangeTblEntry *rte = planner_rt_fetch(rti, root);
7143
7144 ereport(ERROR,
7145 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
7146 errmsg("cannot execute MERGE on relation \"%s\"",
7147 get_rel_name(rte->relid)),
7148 errdetail_relkind_not_supported(rte->relkind));
7149 }
7150
7151 /*
7152 * Try to modify the foreign table directly if (1) the FDW provides
7153 * callback functions needed for that and (2) there are no local
7154 * structures that need to be run for each modified row: row-level
7155 * triggers on the foreign table, stored generated columns, WITH CHECK
7156 * OPTIONs from parent views, Vars returning OLD/NEW in the RETURNING
7157 * list, or transition tables on the named relation.
7158 */
7159 direct_modify = false;
7160 if (fdwroutine != NULL &&
7161 fdwroutine->PlanDirectModify != NULL &&
7162 fdwroutine->BeginDirectModify != NULL &&
7163 fdwroutine->IterateDirectModify != NULL &&
7164 fdwroutine->EndDirectModify != NULL &&
7165 withCheckOptionLists == NIL &&
7166 !has_row_triggers(root, rti, operation) &&
7168 {
7169 /*
7170 * returning_old_or_new and transition_tables are the same for all
7171 * result relations, respectively
7172 */
7173 if (!returning_old_or_new_valid)
7174 {
7175 returning_old_or_new =
7177 root->parse->returningList);
7178 returning_old_or_new_valid = true;
7179 }
7180 if (!returning_old_or_new)
7181 {
7182 if (!transition_tables_valid)
7183 {
7184 transition_tables = has_transition_tables(root,
7185 nominalRelation,
7186 operation);
7187 transition_tables_valid = true;
7188 }
7189 if (!transition_tables)
7190 direct_modify = fdwroutine->PlanDirectModify(root, node,
7191 rti, i);
7192 }
7193 }
7194 if (direct_modify)
7195 direct_modify_plans = bms_add_member(direct_modify_plans, i);
7196
7197 if (!direct_modify &&
7198 fdwroutine != NULL &&
7199 fdwroutine->PlanForeignModify != NULL)
7200 fdw_private = fdwroutine->PlanForeignModify(root, node, rti, i);
7201 else
7202 fdw_private = NIL;
7203 fdw_private_list = lappend(fdw_private_list, fdw_private);
7204 i++;
7205 }
7206 node->fdwPrivLists = fdw_private_list;
7207 node->fdwDirectModifyPlans = direct_modify_plans;
7208
7209 return node;
7210}
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
#define unlikely(x)
Definition: c.h:406
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ereport(elevel,...)
Definition: elog.h:150
FdwRoutine * GetFdwRoutineByRelId(Oid relid)
Definition: foreign.c:420
char * get_rel_name(Oid relid)
Definition: lsyscache.c:2095
@ ONCONFLICT_NONE
Definition: nodes.h:428
@ CMD_MERGE
Definition: nodes.h:279
@ CMD_UPDATE
Definition: nodes.h:276
int errdetail_relkind_not_supported(char relkind)
Definition: pg_class.c:24
bool has_stored_generated_columns(PlannerInfo *root, Index rti)
Definition: plancat.c:2466
bool has_row_triggers(PlannerInfo *root, Index rti, CmdType event)
Definition: plancat.c:2362
bool has_transition_tables(PlannerInfo *root, Index rti, CmdType event)
Definition: plancat.c:2412
List * infer_arbiter_indexes(PlannerInfo *root)
Definition: plancat.c:799
int restrict_nonsystem_relation_kind
Definition: postgres.c:106
List * extract_update_targetlist_colnos(List *tlist)
Definition: preptlist.c:348
BeginDirectModify_function BeginDirectModify
Definition: fdwapi.h:242
PlanForeignModify_function PlanForeignModify
Definition: fdwapi.h:230
PlanDirectModify_function PlanDirectModify
Definition: fdwapi.h:241
IterateDirectModify_function IterateDirectModify
Definition: fdwapi.h:243
EndDirectModify_function EndDirectModify
Definition: fdwapi.h:244
List * updateColnosLists
Definition: plannodes.h:344
Index nominalRelation
Definition: plannodes.h:338
List * arbiterIndexes
Definition: plannodes.h:364
List * onConflictCols
Definition: plannodes.h:368
List * mergeJoinConditions
Definition: plannodes.h:378
char * returningOldAlias
Definition: plannodes.h:348
char * returningNewAlias
Definition: plannodes.h:350
CmdType operation
Definition: plannodes.h:334
int epqParam
Definition: plannodes.h:360
List * resultRelations
Definition: plannodes.h:342
Bitmapset * fdwDirectModifyPlans
Definition: plannodes.h:356
List * onConflictSet
Definition: plannodes.h:366
List * exclRelTlist
Definition: plannodes.h:374
List * mergeActionLists
Definition: plannodes.h:376
bool canSetTag
Definition: plannodes.h:336
List * fdwPrivLists
Definition: plannodes.h:354
List * returningLists
Definition: plannodes.h:352
List * withCheckOptionLists
Definition: plannodes.h:346
Index rootRelation
Definition: plannodes.h:340
Node * onConflictWhere
Definition: plannodes.h:370
List * rowMarks
Definition: plannodes.h:358
OnConflictAction onConflictAction
Definition: plannodes.h:362
Index exclRelRTI
Definition: plannodes.h:372
OnConflictAction action
Definition: primnodes.h:2373
List * onConflictSet
Definition: primnodes.h:2382
List * exclRelTlist
Definition: primnodes.h:2385
Node * onConflictWhere
Definition: primnodes.h:2383
struct Plan * righttree
Definition: plannodes.h:234
List * qual
Definition: plannodes.h:231
#define RESTRICT_RELKIND_FOREIGN_TABLE
Definition: tcopprot.h:45
#define FirstNormalObjectId
Definition: transam.h:197
bool contain_vars_returning_old_or_new(Node *node)
Definition: var.c:511

References OnConflictExpr::action, ModifyTable::arbiterIndexes, Assert(), FdwRoutine::BeginDirectModify, bms_add_member(), ModifyTable::canSetTag, CMD_MERGE, CMD_UPDATE, contain_vars_returning_old_or_new(), FdwRoutine::EndDirectModify, ModifyTable::epqParam, ereport, errcode(), errdetail_relkind_not_supported(), errmsg(), ERROR, OnConflictExpr::exclRelIndex, ModifyTable::exclRelRTI, ModifyTable::exclRelTlist, OnConflictExpr::exclRelTlist, extract_update_targetlist_colnos(), ModifyTable::fdwDirectModifyPlans, ModifyTable::fdwPrivLists, FirstNormalObjectId, get_rel_name(), GetFdwRoutineByRelId(), has_row_triggers(), has_stored_generated_columns(), has_transition_tables(), i, infer_arbiter_indexes(), FdwRoutine::IterateDirectModify, lappend(), Plan::lefttree, lfirst_int, list_length(), makeNode, ModifyTable::mergeActionLists, ModifyTable::mergeJoinConditions, NIL, ModifyTable::nominalRelation, ONCONFLICT_NONE, ModifyTable::onConflictAction, ModifyTable::onConflictCols, ModifyTable::onConflictSet, OnConflictExpr::onConflictSet, ModifyTable::onConflictWhere, OnConflictExpr::onConflictWhere, ModifyTable::operation, ModifyTable::plan, FdwRoutine::PlanDirectModify, FdwRoutine::PlanForeignModify, planner_rt_fetch, Plan::qual, restrict_nonsystem_relation_kind, RESTRICT_RELKIND_FOREIGN_TABLE, ModifyTable::resultRelations, ModifyTable::returningLists, ModifyTable::returningNewAlias, ModifyTable::returningOldAlias, Plan::righttree, root, ModifyTable::rootRelation, ModifyTable::rowMarks, RTE_RELATION, RangeTblEntry::rtekind, Plan::targetlist, unlikely, ModifyTable::updateColnosLists, and ModifyTable::withCheckOptionLists.

Referenced by create_modifytable_plan().

◆ make_namedtuplestorescan()

static NamedTuplestoreScan * make_namedtuplestorescan ( List qptlist,
List qpqual,
Index  scanrelid,
char *  enrname 
)
static

Definition at line 5756 of file createplan.c.

5760{
5762 Plan *plan = &node->scan.plan;
5763
5764 /* cost should be inserted by caller */
5765 plan->targetlist = qptlist;
5766 plan->qual = qpqual;
5767 plan->lefttree = NULL;
5768 plan->righttree = NULL;
5769 node->scan.scanrelid = scanrelid;
5770 node->enrname = enrname;
5771
5772 return node;
5773}

References NamedTuplestoreScan::enrname, makeNode, plan, NamedTuplestoreScan::scan, and Scan::scanrelid.

Referenced by create_namedtuplestorescan_plan().

◆ make_nestloop()

static NestLoop * make_nestloop ( List tlist,
List joinclauses,
List otherclauses,
List nestParams,
Plan lefttree,
Plan righttree,
JoinType  jointype,
bool  inner_unique 
)
static

Definition at line 5921 of file createplan.c.

5929{
5930 NestLoop *node = makeNode(NestLoop);
5931 Plan *plan = &node->join.plan;
5932
5933 plan->targetlist = tlist;
5934 plan->qual = otherclauses;
5935 plan->lefttree = lefttree;
5936 plan->righttree = righttree;
5937 node->join.jointype = jointype;
5938 node->join.inner_unique = inner_unique;
5939 node->join.joinqual = joinclauses;
5940 node->nestParams = nestParams;
5941
5942 return node;
5943}
List * nestParams
Definition: plannodes.h:989

References Join::inner_unique, NestLoop::join, Join::joinqual, Join::jointype, makeNode, NestLoop::nestParams, and plan.

Referenced by create_nestloop_plan().

◆ make_one_row_result()

static Result * make_one_row_result ( List tlist,
Node resconstantqual,
RelOptInfo rel 
)
static

Definition at line 6953 of file createplan.c.

6956{
6957 Result *node = makeNode(Result);
6958 Plan *plan = &node->plan;
6959
6960 plan->targetlist = tlist;
6961 plan->qual = NIL;
6962 plan->lefttree = NULL;
6963 plan->righttree = NULL;
6966 node->resconstantqual = resconstantqual;
6967 node->relids = rel->relids;
6968
6969 return node;
6970}
#define IS_UPPER_REL(rel)
Definition: pathnodes.h:905
@ RESULT_TYPE_UPPER
Definition: plannodes.h:274
@ RESULT_TYPE_SCAN
Definition: plannodes.h:272
@ RESULT_TYPE_JOIN
Definition: plannodes.h:273

References IS_JOIN_REL, IS_UPPER_REL, makeNode, NIL, Result::plan, plan, RelOptInfo::relids, Result::relids, Result::resconstantqual, Result::result_type, RESULT_TYPE_JOIN, RESULT_TYPE_SCAN, and RESULT_TYPE_UPPER.

Referenced by create_append_plan(), create_group_result_plan(), create_minmaxagg_plan(), and create_resultscan_plan().

◆ make_project_set()

static ProjectSet * make_project_set ( List tlist,
Plan subplan 
)
static

Definition at line 6977 of file createplan.c.

6979{
6981 Plan *plan = &node->plan;
6982
6983 plan->targetlist = tlist;
6984 plan->qual = NIL;
6985 plan->lefttree = subplan;
6986 plan->righttree = NULL;
6987
6988 return node;
6989}
Plan plan
Definition: plannodes.h:311

References makeNode, NIL, ProjectSet::plan, and plan.

Referenced by create_project_set_plan().

◆ make_recursive_union()

static RecursiveUnion * make_recursive_union ( List tlist,
Plan lefttree,
Plan righttree,
int  wtParam,
List distinctList,
Cardinality  numGroups 
)
static

Definition at line 5835 of file createplan.c.

5841{
5843 Plan *plan = &node->plan;
5844 int numCols = list_length(distinctList);
5845
5846 plan->targetlist = tlist;
5847 plan->qual = NIL;
5848 plan->lefttree = lefttree;
5849 plan->righttree = righttree;
5850 node->wtParam = wtParam;
5851
5852 /*
5853 * convert SortGroupClause list into arrays of attr indexes and equality
5854 * operators, as wanted by executor
5855 */
5856 node->numCols = numCols;
5857 if (numCols > 0)
5858 {
5859 int keyno = 0;
5860 AttrNumber *dupColIdx;
5861 Oid *dupOperators;
5862 Oid *dupCollations;
5863 ListCell *slitem;
5864
5865 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
5866 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
5867 dupCollations = (Oid *) palloc(sizeof(Oid) * numCols);
5868
5869 foreach(slitem, distinctList)
5870 {
5871 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
5873 plan->targetlist);
5874
5875 dupColIdx[keyno] = tle->resno;
5876 dupOperators[keyno] = sortcl->eqop;
5877 dupCollations[keyno] = exprCollation((Node *) tle->expr);
5878 Assert(OidIsValid(dupOperators[keyno]));
5879 keyno++;
5880 }
5881 node->dupColIdx = dupColIdx;
5882 node->dupOperators = dupOperators;
5883 node->dupCollations = dupCollations;
5884 }
5885 node->numGroups = numGroups;
5886
5887 return node;
5888}
Cardinality numGroups
Definition: plannodes.h:478

References Assert(), SortGroupClause::eqop, TargetEntry::expr, exprCollation(), get_sortgroupclause_tle(), lfirst, list_length(), makeNode, NIL, RecursiveUnion::numCols, RecursiveUnion::numGroups, OidIsValid, palloc(), RecursiveUnion::plan, plan, TargetEntry::resno, and RecursiveUnion::wtParam.

Referenced by create_recursiveunion_plan().

◆ make_samplescan()

static SampleScan * make_samplescan ( List qptlist,
List qpqual,
Index  scanrelid,
TableSampleClause tsc 
)
static

Definition at line 5498 of file createplan.c.

5502{
5504 Plan *plan = &node->scan.plan;
5505
5506 plan->targetlist = qptlist;
5507 plan->qual = qpqual;
5508 plan->lefttree = NULL;
5509 plan->righttree = NULL;
5510 node->scan.scanrelid = scanrelid;
5511 node->tablesample = tsc;
5512
5513 return node;
5514}
struct TableSampleClause * tablesample
Definition: plannodes.h:543

References makeNode, plan, SampleScan::scan, Scan::scanrelid, and SampleScan::tablesample.

Referenced by create_samplescan_plan().

◆ make_seqscan()

static SeqScan * make_seqscan ( List qptlist,
List qpqual,
Index  scanrelid 
)
static

Definition at line 5481 of file createplan.c.

5484{
5485 SeqScan *node = makeNode(SeqScan);
5486 Plan *plan = &node->scan.plan;
5487
5488 plan->targetlist = qptlist;
5489 plan->qual = qpqual;
5490 plan->lefttree = NULL;
5491 plan->righttree = NULL;
5492 node->scan.scanrelid = scanrelid;
5493
5494 return node;
5495}

References makeNode, plan, SeqScan::scan, and Scan::scanrelid.

Referenced by create_seqscan_plan().

◆ make_setop()

static SetOp * make_setop ( SetOpCmd  cmd,
SetOpStrategy  strategy,
List tlist,
Plan lefttree,
Plan righttree,
List groupList,
Cardinality  numGroups 
)
static

Definition at line 6811 of file createplan.c.

6814{
6815 SetOp *node = makeNode(SetOp);
6816 Plan *plan = &node->plan;
6817 int numCols = list_length(groupList);
6818 int keyno = 0;
6819 AttrNumber *cmpColIdx;
6820 Oid *cmpOperators;
6821 Oid *cmpCollations;
6822 bool *cmpNullsFirst;
6823 ListCell *slitem;
6824
6825 plan->targetlist = tlist;
6826 plan->qual = NIL;
6827 plan->lefttree = lefttree;
6828 plan->righttree = righttree;
6829
6830 /*
6831 * convert SortGroupClause list into arrays of attr indexes and comparison
6832 * operators, as wanted by executor
6833 */
6834 cmpColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
6835 cmpOperators = (Oid *) palloc(sizeof(Oid) * numCols);
6836 cmpCollations = (Oid *) palloc(sizeof(Oid) * numCols);
6837 cmpNullsFirst = (bool *) palloc(sizeof(bool) * numCols);
6838
6839 foreach(slitem, groupList)
6840 {
6841 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
6842 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
6843
6844 cmpColIdx[keyno] = tle->resno;
6845 if (strategy == SETOP_HASHED)
6846 cmpOperators[keyno] = sortcl->eqop;
6847 else
6848 cmpOperators[keyno] = sortcl->sortop;
6849 Assert(OidIsValid(cmpOperators[keyno]));
6850 cmpCollations[keyno] = exprCollation((Node *) tle->expr);
6851 cmpNullsFirst[keyno] = sortcl->nulls_first;
6852 keyno++;
6853 }
6854
6855 node->cmd = cmd;
6856 node->strategy = strategy;
6857 node->numCols = numCols;
6858 node->cmpColIdx = cmpColIdx;
6859 node->cmpOperators = cmpOperators;
6860 node->cmpCollations = cmpCollations;
6861 node->cmpNullsFirst = cmpNullsFirst;
6862 node->numGroups = numGroups;
6863
6864 return node;
6865}
@ SETOP_HASHED
Definition: nodes.h:417
SetOpStrategy strategy
Definition: plannodes.h:1433
SetOpCmd cmd
Definition: plannodes.h:1430
int numCols
Definition: plannodes.h:1436
Plan plan
Definition: plannodes.h:1427
Cardinality numGroups
Definition: plannodes.h:1449

References Assert(), SetOp::cmd, SortGroupClause::eqop, TargetEntry::expr, exprCollation(), get_sortgroupclause_tle(), lfirst, list_length(), makeNode, NIL, SortGroupClause::nulls_first, SetOp::numCols, SetOp::numGroups, OidIsValid, palloc(), SetOp::plan, plan, TargetEntry::resno, SETOP_HASHED, SortGroupClause::sortop, and SetOp::strategy.

Referenced by create_setop_plan().

◆ make_sort()

static Sort * make_sort ( Plan lefttree,
int  numCols,
AttrNumber sortColIdx,
Oid sortOperators,
Oid collations,
bool *  nullsFirst 
)
static

Definition at line 6041 of file createplan.c.

6044{
6045 Sort *node;
6046 Plan *plan;
6047
6048 node = makeNode(Sort);
6049
6050 plan = &node->plan;
6051 plan->targetlist = lefttree->targetlist;
6052 plan->disabled_nodes = lefttree->disabled_nodes + (enable_sort == false);
6053 plan->qual = NIL;
6054 plan->lefttree = lefttree;
6055 plan->righttree = NULL;
6056 node->numCols = numCols;
6057 node->sortColIdx = sortColIdx;
6058 node->sortOperators = sortOperators;
6059 node->collations = collations;
6060 node->nullsFirst = nullsFirst;
6061
6062 return node;
6063}
bool enable_sort
Definition: costsize.c:150

References Plan::disabled_nodes, enable_sort, makeNode, NIL, Sort::numCols, Sort::plan, plan, and Plan::targetlist.

Referenced by create_append_plan(), create_merge_append_plan(), make_sort_from_groupcols(), make_sort_from_pathkeys(), and make_sort_from_sortclauses().

◆ make_sort_from_groupcols()

static Sort * make_sort_from_groupcols ( List groupcls,
AttrNumber grpColIdx,
Plan lefttree 
)
static

Definition at line 6438 of file createplan.c.

6441{
6442 List *sub_tlist = lefttree->targetlist;
6443 ListCell *l;
6444 int numsortkeys;
6445 AttrNumber *sortColIdx;
6446 Oid *sortOperators;
6447 Oid *collations;
6448 bool *nullsFirst;
6449
6450 /* Convert list-ish representation to arrays wanted by executor */
6451 numsortkeys = list_length(groupcls);
6452 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
6453 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
6454 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
6455 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
6456
6457 numsortkeys = 0;
6458 foreach(l, groupcls)
6459 {
6460 SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
6461 TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[numsortkeys]);
6462
6463 if (!tle)
6464 elog(ERROR, "could not retrieve tle for sort-from-groupcols");
6465
6466 sortColIdx[numsortkeys] = tle->resno;
6467 sortOperators[numsortkeys] = grpcl->sortop;
6468 collations[numsortkeys] = exprCollation((Node *) tle->expr);
6469 nullsFirst[numsortkeys] = grpcl->nulls_first;
6470 numsortkeys++;
6471 }
6472
6473 return make_sort(lefttree, numsortkeys,
6474 sortColIdx, sortOperators,
6475 collations, nullsFirst);
6476}
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)

References elog, ERROR, TargetEntry::expr, exprCollation(), get_tle_by_resno(), lfirst, list_length(), make_sort(), SortGroupClause::nulls_first, palloc(), TargetEntry::resno, SortGroupClause::sortop, and Plan::targetlist.

Referenced by create_groupingsets_plan().

◆ make_sort_from_pathkeys()

static Sort * make_sort_from_pathkeys ( Plan lefttree,
List pathkeys,
Relids  relids 
)
static

Definition at line 6320 of file createplan.c.

6321{
6322 int numsortkeys;
6323 AttrNumber *sortColIdx;
6324 Oid *sortOperators;
6325 Oid *collations;
6326 bool *nullsFirst;
6327
6328 /* Compute sort column info, and adjust lefttree as needed */
6329 lefttree = prepare_sort_from_pathkeys(lefttree, pathkeys,
6330 relids,
6331 NULL,
6332 false,
6333 &numsortkeys,
6334 &sortColIdx,
6335 &sortOperators,
6336 &collations,
6337 &nullsFirst);
6338
6339 /* Now build the Sort node */
6340 return make_sort(lefttree, numsortkeys,
6341 sortColIdx, sortOperators,
6342 collations, nullsFirst);
6343}

References make_sort(), and prepare_sort_from_pathkeys().

Referenced by create_mergejoin_plan(), and create_sort_plan().

◆ make_sort_from_sortclauses()

Sort * make_sort_from_sortclauses ( List sortcls,
Plan lefttree 
)

Definition at line 6389 of file createplan.c.

6390{
6391 List *sub_tlist = lefttree->targetlist;
6392 ListCell *l;
6393 int numsortkeys;
6394 AttrNumber *sortColIdx;
6395 Oid *sortOperators;
6396 Oid *collations;
6397 bool *nullsFirst;
6398
6399 /* Convert list-ish representation to arrays wanted by executor */
6400 numsortkeys = list_length(sortcls);
6401 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
6402 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
6403 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
6404 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
6405
6406 numsortkeys = 0;
6407 foreach(l, sortcls)
6408 {
6409 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
6410 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
6411
6412 sortColIdx[numsortkeys] = tle->resno;
6413 sortOperators[numsortkeys] = sortcl->sortop;
6414 collations[numsortkeys] = exprCollation((Node *) tle->expr);
6415 nullsFirst[numsortkeys] = sortcl->nulls_first;
6416 numsortkeys++;
6417 }
6418
6419 return make_sort(lefttree, numsortkeys,
6420 sortColIdx, sortOperators,
6421 collations, nullsFirst);
6422}

References TargetEntry::expr, exprCollation(), get_sortgroupclause_tle(), lfirst, list_length(), make_sort(), SortGroupClause::nulls_first, palloc(), TargetEntry::resno, SortGroupClause::sortop, and Plan::targetlist.

◆ make_subqueryscan()

static SubqueryScan * make_subqueryscan ( List qptlist,
List qpqual,
Index  scanrelid,
Plan subplan 
)
static

Definition at line 5656 of file createplan.c.

5660{
5662 Plan *plan = &node->scan.plan;
5663
5664 plan->targetlist = qptlist;
5665 plan->qual = qpqual;
5666 plan->lefttree = NULL;
5667 plan->righttree = NULL;
5668 node->scan.scanrelid = scanrelid;
5669 node->subplan = subplan;
5671
5672 return node;
5673}
@ SUBQUERY_SCAN_UNKNOWN
Definition: plannodes.h:747
SubqueryScanStatus scanstatus
Definition: plannodes.h:756
Plan * subplan
Definition: plannodes.h:755

References makeNode, plan, SubqueryScan::scan, Scan::scanrelid, SubqueryScan::scanstatus, SubqueryScan::subplan, and SUBQUERY_SCAN_UNKNOWN.

Referenced by create_subqueryscan_plan().

◆ make_tablefuncscan()

static TableFuncScan * make_tablefuncscan ( List qptlist,
List qpqual,
Index  scanrelid,
TableFunc tablefunc 
)
static

Definition at line 5697 of file createplan.c.

5701{
5703 Plan *plan = &node->scan.plan;
5704
5705 plan->targetlist = qptlist;
5706 plan->qual = qpqual;
5707 plan->lefttree = NULL;
5708 plan->righttree = NULL;
5709 node->scan.scanrelid = scanrelid;
5710 node->tablefunc = tablefunc;
5711
5712 return node;
5713}
TableFunc * tablefunc
Definition: plannodes.h:791

References makeNode, plan, TableFuncScan::scan, Scan::scanrelid, and TableFuncScan::tablefunc.

Referenced by create_tablefuncscan_plan().

◆ make_tidrangescan()

static TidRangeScan * make_tidrangescan ( List qptlist,
List qpqual,
Index  scanrelid,
List tidrangequals 
)
static

Definition at line 5637 of file createplan.c.

5641{
5643 Plan *plan = &node->scan.plan;
5644
5645 plan->targetlist = qptlist;
5646 plan->qual = qpqual;
5647 plan->lefttree = NULL;
5648 plan->righttree = NULL;
5649 node->scan.scanrelid = scanrelid;
5650 node->tidrangequals = tidrangequals;
5651
5652 return node;
5653}
List * tidrangequals
Definition: plannodes.h:722

References makeNode, plan, TidRangeScan::scan, Scan::scanrelid, and TidRangeScan::tidrangequals.

Referenced by create_tidrangescan_plan().

◆ make_tidscan()

static TidScan * make_tidscan ( List qptlist,
List qpqual,
Index  scanrelid,
List tidquals 
)
static

Definition at line 5618 of file createplan.c.

5622{
5623 TidScan *node = makeNode(TidScan);
5624 Plan *plan = &node->scan.plan;
5625
5626 plan->targetlist = qptlist;
5627 plan->qual = qpqual;
5628 plan->lefttree = NULL;
5629 plan->righttree = NULL;
5630 node->scan.scanrelid = scanrelid;
5631 node->tidquals = tidquals;
5632
5633 return node;
5634}
List * tidquals
Definition: plannodes.h:708

References makeNode, plan, TidScan::scan, Scan::scanrelid, and TidScan::tidquals.

Referenced by create_tidscan_plan().

◆ make_unique_from_pathkeys()

static Unique * make_unique_from_pathkeys ( Plan lefttree,
List pathkeys,
int  numCols,
Relids  relids 
)
static

Definition at line 6675 of file createplan.c.

6677{
6678 Unique *node = makeNode(Unique);
6679 Plan *plan = &node->plan;
6680 int keyno = 0;
6681 AttrNumber *uniqColIdx;
6682 Oid *uniqOperators;
6683 Oid *uniqCollations;
6684 ListCell *lc;
6685
6686 plan->targetlist = lefttree->targetlist;
6687 plan->qual = NIL;
6688 plan->lefttree = lefttree;
6689 plan->righttree = NULL;
6690
6691 /*
6692 * Convert pathkeys list into arrays of attr indexes and equality
6693 * operators, as wanted by executor. This has a lot in common with
6694 * prepare_sort_from_pathkeys ... maybe unify sometime?
6695 */
6696 Assert(numCols >= 0 && numCols <= list_length(pathkeys));
6697 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
6698 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
6699 uniqCollations = (Oid *) palloc(sizeof(Oid) * numCols);
6700
6701 foreach(lc, pathkeys)
6702 {
6703 PathKey *pathkey = (PathKey *) lfirst(lc);
6704 EquivalenceClass *ec = pathkey->pk_eclass;
6706 TargetEntry *tle = NULL;
6707 Oid pk_datatype = InvalidOid;
6708 Oid eqop;
6709 ListCell *j;
6710
6711 /* Ignore pathkeys beyond the specified number of columns */
6712 if (keyno >= numCols)
6713 break;
6714
6715 if (ec->ec_has_volatile)
6716 {
6717 /*
6718 * If the pathkey's EquivalenceClass is volatile, then it must
6719 * have come from an ORDER BY clause, and we have to match it to
6720 * that same targetlist entry.
6721 */
6722 if (ec->ec_sortref == 0) /* can't happen */
6723 elog(ERROR, "volatile EquivalenceClass has no sortref");
6724 tle = get_sortgroupref_tle(ec->ec_sortref, plan->targetlist);
6725 Assert(tle);
6726 Assert(list_length(ec->ec_members) == 1);
6727 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
6728 }
6729 else
6730 {
6731 /*
6732 * Otherwise, we can use any non-constant expression listed in the
6733 * pathkey's EquivalenceClass. For now, we take the first tlist
6734 * item found in the EC.
6735 */
6736 foreach(j, plan->targetlist)
6737 {
6738 tle = (TargetEntry *) lfirst(j);
6739 em = find_ec_member_matching_expr(ec, tle->expr, relids);
6740 if (em)
6741 {
6742 /* found expr already in tlist */
6743 pk_datatype = em->em_datatype;
6744 break;
6745 }
6746 tle = NULL;
6747 }
6748 }
6749
6750 if (!tle)
6751 elog(ERROR, "could not find pathkey item to sort");
6752
6753 /*
6754 * Look up the correct equality operator from the PathKey's slightly
6755 * abstracted representation.
6756 */
6758 pk_datatype,
6759 pk_datatype,
6760 COMPARE_EQ);
6761 if (!OidIsValid(eqop)) /* should not happen */
6762 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
6763 COMPARE_EQ, pk_datatype, pk_datatype,
6764 pathkey->pk_opfamily);
6765
6766 uniqColIdx[keyno] = tle->resno;
6767 uniqOperators[keyno] = eqop;
6768 uniqCollations[keyno] = ec->ec_collation;
6769
6770 keyno++;
6771 }
6772
6773 node->numCols = numCols;
6774 node->uniqColIdx = uniqColIdx;
6775 node->uniqOperators = uniqOperators;
6776 node->uniqCollations = uniqCollations;
6777
6778 return node;
6779}
@ COMPARE_EQ
Definition: cmptype.h:36
EquivalenceMember * find_ec_member_matching_expr(EquivalenceClass *ec, Expr *expr, Relids relids)
Definition: equivclass.c:916
int j
Definition: isn.c:78
Plan plan
Definition: plannodes.h:1309
int numCols
Definition: plannodes.h:1312
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition: tlist.c:345

References Assert(), COMPARE_EQ, EquivalenceClass::ec_collation, EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_members, EquivalenceClass::ec_sortref, elog, EquivalenceMember::em_datatype, ERROR, TargetEntry::expr, find_ec_member_matching_expr(), get_opfamily_member_for_cmptype(), get_sortgroupref_tle(), InvalidOid, j, lfirst, linitial, list_length(), makeNode, NIL, Unique::numCols, OidIsValid, palloc(), PathKey::pk_opfamily, Unique::plan, plan, TargetEntry::resno, and Plan::targetlist.

Referenced by create_unique_plan().

◆ make_valuesscan()

static ValuesScan * make_valuesscan ( List qptlist,
List qpqual,
Index  scanrelid,
List values_lists 
)
static

Definition at line 5716 of file createplan.c.

5720{
5722 Plan *plan = &node->scan.plan;
5723
5724 plan->targetlist = qptlist;
5725 plan->qual = qpqual;
5726 plan->lefttree = NULL;
5727 plan->righttree = NULL;
5728 node->scan.scanrelid = scanrelid;
5729 node->values_lists = values_lists;
5730
5731 return node;
5732}
List * values_lists
Definition: plannodes.h:780

References makeNode, plan, ValuesScan::scan, Scan::scanrelid, and ValuesScan::values_lists.

Referenced by create_valuesscan_plan().

◆ make_windowagg()

static WindowAgg * make_windowagg ( List tlist,
WindowClause wc,
int  partNumCols,
AttrNumber partColIdx,
Oid partOperators,
Oid partCollations,
int  ordNumCols,
AttrNumber ordColIdx,
Oid ordOperators,
Oid ordCollations,
List runCondition,
List qual,
bool  topWindow,
Plan lefttree 
)
static

Definition at line 6604 of file createplan.c.

6608{
6609 WindowAgg *node = makeNode(WindowAgg);
6610 Plan *plan = &node->plan;
6611
6612 node->winname = wc->name;
6613 node->winref = wc->winref;
6614 node->partNumCols = partNumCols;
6615 node->partColIdx = partColIdx;
6616 node->partOperators = partOperators;
6617 node->partCollations = partCollations;
6618 node->ordNumCols = ordNumCols;
6619 node->ordColIdx = ordColIdx;
6620 node->ordOperators = ordOperators;
6621 node->ordCollations = ordCollations;
6622 node->frameOptions = wc->frameOptions;
6623 node->startOffset = wc->startOffset;
6624 node->endOffset = wc->endOffset;
6625 node->runCondition = runCondition;
6626 /* a duplicate of the above for EXPLAIN */
6627 node->runConditionOrig = runCondition;
6628 node->startInRangeFunc = wc->startInRangeFunc;
6629 node->endInRangeFunc = wc->endInRangeFunc;
6630 node->inRangeColl = wc->inRangeColl;
6631 node->inRangeAsc = wc->inRangeAsc;
6632 node->inRangeNullsFirst = wc->inRangeNullsFirst;
6633 node->topWindow = topWindow;
6634
6635 plan->targetlist = tlist;
6636 plan->lefttree = lefttree;
6637 plan->righttree = NULL;
6638 plan->qual = qual;
6639
6640 return node;
6641}
char * winname
Definition: plannodes.h:1235
int partNumCols
Definition: plannodes.h:1241
Oid endInRangeFunc
Definition: plannodes.h:1285
Node * endOffset
Definition: plannodes.h:1271
bool topWindow
Definition: plannodes.h:1300
Plan plan
Definition: plannodes.h:1232
List * runConditionOrig
Definition: plannodes.h:1277
Oid inRangeColl
Definition: plannodes.h:1288
Node * startOffset
Definition: plannodes.h:1268
List * runCondition
Definition: plannodes.h:1274
Oid startInRangeFunc
Definition: plannodes.h:1282
bool inRangeAsc
Definition: plannodes.h:1291
Index winref
Definition: plannodes.h:1238
bool inRangeNullsFirst
Definition: plannodes.h:1294
int ordNumCols
Definition: plannodes.h:1253
int frameOptions
Definition: plannodes.h:1265
Node * startOffset
Definition: parsenodes.h:1578
Node * endOffset
Definition: parsenodes.h:1579

References WindowAgg::endInRangeFunc, WindowClause::endOffset, WindowAgg::endOffset, WindowClause::frameOptions, WindowAgg::frameOptions, WindowAgg::inRangeAsc, WindowAgg::inRangeColl, WindowAgg::inRangeNullsFirst, makeNode, WindowAgg::ordNumCols, WindowAgg::partNumCols, WindowAgg::plan, plan, WindowAgg::runCondition, WindowAgg::runConditionOrig, WindowAgg::startInRangeFunc, WindowClause::startOffset, WindowAgg::startOffset, WindowAgg::topWindow, WindowAgg::winname, WindowClause::winref, and WindowAgg::winref.

Referenced by create_windowagg_plan().

◆ make_worktablescan()

static WorkTableScan * make_worktablescan ( List qptlist,
List qpqual,
Index  scanrelid,
int  wtParam 
)
static

Definition at line 5776 of file createplan.c.

5780{
5782 Plan *plan = &node->scan.plan;
5783
5784 plan->targetlist = qptlist;
5785 plan->qual = qpqual;
5786 plan->lefttree = NULL;
5787 plan->righttree = NULL;
5788 node->scan.scanrelid = scanrelid;
5789 node->wtParam = wtParam;
5790
5791 return node;
5792}

References makeNode, plan, WorkTableScan::scan, Scan::scanrelid, and WorkTableScan::wtParam.

Referenced by create_worktablescan_plan().

◆ mark_async_capable_plan()

static bool mark_async_capable_plan ( Plan plan,
Path path 
)
static

Definition at line 1133 of file createplan.c.

1134{
1135 switch (nodeTag(path))
1136 {
1137 case T_SubqueryScanPath:
1138 {
1139 SubqueryScan *scan_plan = (SubqueryScan *) plan;
1140
1141 /*
1142 * If the generated plan node includes a gating Result node,
1143 * we can't execute it asynchronously.
1144 */
1145 if (IsA(plan, Result))
1146 return false;
1147
1148 /*
1149 * If a SubqueryScan node atop of an async-capable plan node
1150 * is deletable, consider it as async-capable.
1151 */
1152 if (trivial_subqueryscan(scan_plan) &&
1154 ((SubqueryScanPath *) path)->subpath))
1155 break;
1156 return false;
1157 }
1158 case T_ForeignPath:
1159 {
1160 FdwRoutine *fdwroutine = path->parent->fdwroutine;
1161
1162 /*
1163 * If the generated plan node includes a gating Result node,
1164 * we can't execute it asynchronously.
1165 */
1166 if (IsA(plan, Result))
1167 return false;
1168
1169 Assert(fdwroutine != NULL);
1170 if (fdwroutine->IsForeignPathAsyncCapable != NULL &&
1171 fdwroutine->IsForeignPathAsyncCapable((ForeignPath *) path))
1172 break;
1173 return false;
1174 }
1175 case T_ProjectionPath:
1176
1177 /*
1178 * If the generated plan node includes a Result node for the
1179 * projection, we can't execute it asynchronously.
1180 */
1181 if (IsA(plan, Result))
1182 return false;
1183
1184 /*
1185 * create_projection_plan() would have pulled up the subplan, so
1186 * check the capability using the subpath.
1187 */
1189 ((ProjectionPath *) path)->subpath))
1190 return true;
1191 return false;
1192 default:
1193 return false;
1194 }
1195
1196 plan->async_capable = true;
1197
1198 return true;
1199}
bool trivial_subqueryscan(SubqueryScan *plan)
Definition: setrefs.c:1497
IsForeignPathAsyncCapable_function IsForeignPathAsyncCapable
Definition: fdwapi.h:277

References Assert(), IsA, FdwRoutine::IsForeignPathAsyncCapable, mark_async_capable_plan(), nodeTag, plan, subpath(), SubqueryScan::subplan, and trivial_subqueryscan().

Referenced by create_append_plan(), and mark_async_capable_plan().

◆ materialize_finished_plan()

Plan * materialize_finished_plan ( Plan subplan)

Definition at line 6501 of file createplan.c.

6502{
6503 Plan *matplan;
6504 Path matpath; /* dummy for result of cost_material */
6505 Cost initplan_cost;
6506 bool unsafe_initplans;
6507
6508 matplan = (Plan *) make_material(subplan);
6509
6510 /*
6511 * XXX horrid kluge: if there are any initPlans attached to the subplan,
6512 * move them up to the Material node, which is now effectively the top
6513 * plan node in its query level. This prevents failure in
6514 * SS_finalize_plan(), which see for comments.
6515 */
6516 matplan->initPlan = subplan->initPlan;
6517 subplan->initPlan = NIL;
6518
6519 /* Move the initplans' cost delta, as well */
6521 &initplan_cost, &unsafe_initplans);
6522 subplan->startup_cost -= initplan_cost;
6523 subplan->total_cost -= initplan_cost;
6524
6525 /* Set cost data */
6526 cost_material(&matpath,
6527 subplan->disabled_nodes,
6528 subplan->startup_cost,
6529 subplan->total_cost,
6530 subplan->plan_rows,
6531 subplan->plan_width);
6532 matplan->disabled_nodes = subplan->disabled_nodes;
6533 matplan->startup_cost = matpath.startup_cost + initplan_cost;
6534 matplan->total_cost = matpath.total_cost + initplan_cost;
6535 matplan->plan_rows = subplan->plan_rows;
6536 matplan->plan_width = subplan->plan_width;
6537 matplan->parallel_aware = false;
6538 matplan->parallel_safe = subplan->parallel_safe;
6539
6540 return matplan;
6541}
void cost_material(Path *path, int input_disabled_nodes, Cost input_startup_cost, Cost input_total_cost, double tuples, int width)
Definition: costsize.c:2483
double Cost
Definition: nodes.h:261
List * initPlan
Definition: plannodes.h:236
void SS_compute_initplan_cost(List *init_plans, Cost *initplan_cost_p, bool *unsafe_initplans_p)
Definition: subselect.c:2348

References cost_material(), Plan::disabled_nodes, Plan::initPlan, make_material(), NIL, Plan::parallel_aware, Plan::parallel_safe, Plan::plan_rows, Plan::plan_width, SS_compute_initplan_cost(), Path::startup_cost, Plan::startup_cost, Path::total_cost, and Plan::total_cost.

Referenced by build_subplan(), and standard_planner().

◆ order_qual_clauses()

static List * order_qual_clauses ( PlannerInfo root,
List clauses 
)
static

Definition at line 5258 of file createplan.c.

5259{
5260 typedef struct
5261 {
5262 Node *clause;
5263 Cost cost;
5264 Index security_level;
5265 } QualItem;
5266 int nitems = list_length(clauses);
5267 QualItem *items;
5268 ListCell *lc;
5269 int i;
5270 List *result;
5271
5272 /* No need to work hard for 0 or 1 clause */
5273 if (nitems <= 1)
5274 return clauses;
5275
5276 /*
5277 * Collect the items and costs into an array. This is to avoid repeated
5278 * cost_qual_eval work if the inputs aren't RestrictInfos.
5279 */
5280 items = (QualItem *) palloc(nitems * sizeof(QualItem));
5281 i = 0;
5282 foreach(lc, clauses)
5283 {
5284 Node *clause = (Node *) lfirst(lc);
5285 QualCost qcost;
5286
5287 cost_qual_eval_node(&qcost, clause, root);
5288 items[i].clause = clause;
5289 items[i].cost = qcost.per_tuple;
5290 if (IsA(clause, RestrictInfo))
5291 {
5292 RestrictInfo *rinfo = (RestrictInfo *) clause;
5293
5294 /*
5295 * If a clause is leakproof, it doesn't have to be constrained by
5296 * its nominal security level. If it's also reasonably cheap
5297 * (here defined as 10X cpu_operator_cost), pretend it has
5298 * security_level 0, which will allow it to go in front of
5299 * more-expensive quals of lower security levels. Of course, that
5300 * will also force it to go in front of cheaper quals of its own
5301 * security level, which is not so great, but we can alleviate
5302 * that risk by applying the cost limit cutoff.
5303 */
5304 if (rinfo->leakproof && items[i].cost < 10 * cpu_operator_cost)
5305 items[i].security_level = 0;
5306 else
5307 items[i].security_level = rinfo->security_level;
5308 }
5309 else
5310 items[i].security_level = 0;
5311 i++;
5312 }
5313
5314 /*
5315 * Sort. We don't use qsort() because it's not guaranteed stable for
5316 * equal keys. The expected number of entries is small enough that a
5317 * simple insertion sort should be good enough.
5318 */
5319 for (i = 1; i < nitems; i++)
5320 {
5321 QualItem newitem = items[i];
5322 int j;
5323
5324 /* insert newitem into the already-sorted subarray */
5325 for (j = i; j > 0; j--)
5326 {
5327 QualItem *olditem = &items[j - 1];
5328
5329 if (newitem.security_level > olditem->security_level ||
5330 (newitem.security_level == olditem->security_level &&
5331 newitem.cost >= olditem->cost))
5332 break;
5333 items[j] = *olditem;
5334 }
5335 items[j] = newitem;
5336 }
5337
5338 /* Convert back to a list */
5339 result = NIL;
5340 for (i = 0; i < nitems; i++)
5341 result = lappend(result, items[i].clause);
5342
5343 return result;
5344}
void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
Definition: costsize.c:4791
#define nitems(x)
Definition: indent.h:31
Cost per_tuple
Definition: pathnodes.h:48
Index security_level
Definition: pathnodes.h:2813
static ItemArray items
Definition: test_tidstore.c:48

References cost_qual_eval_node(), cpu_operator_cost, i, IsA, items, j, lappend(), lfirst, list_length(), NIL, nitems, palloc(), QualCost::per_tuple, root, and RestrictInfo::security_level.

Referenced by create_agg_plan(), create_bitmap_scan_plan(), create_ctescan_plan(), create_customscan_plan(), create_foreignscan_plan(), create_functionscan_plan(), create_group_plan(), create_group_result_plan(), create_hashjoin_plan(), create_indexscan_plan(), create_mergejoin_plan(), create_namedtuplestorescan_plan(), create_nestloop_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(), and get_gating_quals().

◆ prepare_sort_from_pathkeys()

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 
)
static

Definition at line 6138 of file createplan.c.

6147{
6148 List *tlist = lefttree->targetlist;
6149 ListCell *i;
6150 int numsortkeys;
6151 AttrNumber *sortColIdx;
6152 Oid *sortOperators;
6153 Oid *collations;
6154 bool *nullsFirst;
6155
6156 /*
6157 * We will need at most list_length(pathkeys) sort columns; possibly less
6158 */
6159 numsortkeys = list_length(pathkeys);
6160 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
6161 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
6162 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
6163 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
6164
6165 numsortkeys = 0;
6166
6167 foreach(i, pathkeys)
6168 {
6169 PathKey *pathkey = (PathKey *) lfirst(i);
6170 EquivalenceClass *ec = pathkey->pk_eclass;
6172 TargetEntry *tle = NULL;
6173 Oid pk_datatype = InvalidOid;
6174 Oid sortop;
6175 ListCell *j;
6176
6177 if (ec->ec_has_volatile)
6178 {
6179 /*
6180 * If the pathkey's EquivalenceClass is volatile, then it must
6181 * have come from an ORDER BY clause, and we have to match it to
6182 * that same targetlist entry.
6183 */
6184 if (ec->ec_sortref == 0) /* can't happen */
6185 elog(ERROR, "volatile EquivalenceClass has no sortref");
6186 tle = get_sortgroupref_tle(ec->ec_sortref, tlist);
6187 Assert(tle);
6188 Assert(list_length(ec->ec_members) == 1);
6189 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
6190 }
6191 else if (reqColIdx != NULL)
6192 {
6193 /*
6194 * If we are given a sort column number to match, only consider
6195 * the single TLE at that position. It's possible that there is
6196 * no such TLE, in which case fall through and generate a resjunk
6197 * targetentry (we assume this must have happened in the parent
6198 * plan as well). If there is a TLE but it doesn't match the
6199 * pathkey's EC, we do the same, which is probably the wrong thing
6200 * but we'll leave it to caller to complain about the mismatch.
6201 */
6202 tle = get_tle_by_resno(tlist, reqColIdx[numsortkeys]);
6203 if (tle)
6204 {
6205 em = find_ec_member_matching_expr(ec, tle->expr, relids);
6206 if (em)
6207 {
6208 /* found expr at right place in tlist */
6209 pk_datatype = em->em_datatype;
6210 }
6211 else
6212 tle = NULL;
6213 }
6214 }
6215 else
6216 {
6217 /*
6218 * Otherwise, we can sort by any non-constant expression listed in
6219 * the pathkey's EquivalenceClass. For now, we take the first
6220 * tlist item found in the EC. If there's no match, we'll generate
6221 * a resjunk entry using the first EC member that is an expression
6222 * in the input's vars.
6223 *
6224 * XXX if we have a choice, is there any way of figuring out which
6225 * might be cheapest to execute? (For example, int4lt is likely
6226 * much cheaper to execute than numericlt, but both might appear
6227 * in the same equivalence class...) Not clear that we ever will
6228 * have an interesting choice in practice, so it may not matter.
6229 */
6230 foreach(j, tlist)
6231 {
6232 tle = (TargetEntry *) lfirst(j);
6233 em = find_ec_member_matching_expr(ec, tle->expr, relids);
6234 if (em)
6235 {
6236 /* found expr already in tlist */
6237 pk_datatype = em->em_datatype;
6238 break;
6239 }
6240 tle = NULL;
6241 }
6242 }
6243
6244 if (!tle)
6245 {
6246 /*
6247 * No matching tlist item; look for a computable expression.
6248 */
6249 em = find_computable_ec_member(NULL, ec, tlist, relids, false);
6250 if (!em)
6251 elog(ERROR, "could not find pathkey item to sort");
6252 pk_datatype = em->em_datatype;
6253
6254 /*
6255 * Do we need to insert a Result node?
6256 */
6257 if (!adjust_tlist_in_place &&
6258 !is_projection_capable_plan(lefttree))
6259 {
6260 /* copy needed so we don't modify input's tlist below */
6261 tlist = copyObject(tlist);
6262 lefttree = inject_projection_plan(lefttree, tlist,
6263 lefttree->parallel_safe);
6264 }
6265
6266 /* Don't bother testing is_projection_capable_plan again */
6267 adjust_tlist_in_place = true;
6268
6269 /*
6270 * Add resjunk entry to input's tlist
6271 */
6273 list_length(tlist) + 1,
6274 NULL,
6275 true);
6276 tlist = lappend(tlist, tle);
6277 lefttree->targetlist = tlist; /* just in case NIL before */
6278 }
6279
6280 /*
6281 * Look up the correct sort operator from the PathKey's slightly
6282 * abstracted representation.
6283 */
6285 pk_datatype,
6286 pk_datatype,
6287 pathkey->pk_cmptype);
6288 if (!OidIsValid(sortop)) /* should not happen */
6289 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
6290 pathkey->pk_cmptype, pk_datatype, pk_datatype,
6291 pathkey->pk_opfamily);
6292
6293 /* Add the column to the sort arrays */
6294 sortColIdx[numsortkeys] = tle->resno;
6295 sortOperators[numsortkeys] = sortop;
6296 collations[numsortkeys] = ec->ec_collation;
6297 nullsFirst[numsortkeys] = pathkey->pk_nulls_first;
6298 numsortkeys++;
6299 }
6300
6301 /* Return results */
6302 *p_numsortkeys = numsortkeys;
6303 *p_sortColIdx = sortColIdx;
6304 *p_sortOperators = sortOperators;
6305 *p_collations = collations;
6306 *p_nullsFirst = nullsFirst;
6307
6308 return lefttree;
6309}
EquivalenceMember * find_computable_ec_member(PlannerInfo *root, EquivalenceClass *ec, List *exprs, Relids relids, bool require_parallel_safe)
Definition: equivclass.c:991

References Assert(), copyObject, EquivalenceClass::ec_collation, EquivalenceClass::ec_has_volatile, EquivalenceClass::ec_members, EquivalenceClass::ec_sortref, elog, EquivalenceMember::em_datatype, EquivalenceMember::em_expr, ERROR, TargetEntry::expr, find_computable_ec_member(), find_ec_member_matching_expr(), get_opfamily_member_for_cmptype(), get_sortgroupref_tle(), get_tle_by_resno(), i, inject_projection_plan(), InvalidOid, is_projection_capable_plan(), j, lappend(), lfirst, linitial, list_length(), makeTargetEntry(), OidIsValid, palloc(), Plan::parallel_safe, PathKey::pk_cmptype, PathKey::pk_nulls_first, PathKey::pk_opfamily, TargetEntry::resno, and Plan::targetlist.

Referenced by create_append_plan(), create_gather_merge_plan(), create_merge_append_plan(), make_incrementalsort_from_pathkeys(), and make_sort_from_pathkeys().

◆ remap_groupColIdx()

static AttrNumber * remap_groupColIdx ( PlannerInfo root,
List groupClause 
)
static

Definition at line 2201 of file createplan.c.

2202{
2203 AttrNumber *grouping_map = root->grouping_map;
2204 AttrNumber *new_grpColIdx;
2205 ListCell *lc;
2206 int i;
2207
2208 Assert(grouping_map);
2209
2210 new_grpColIdx = palloc0(sizeof(AttrNumber) * list_length(groupClause));
2211
2212 i = 0;
2213 foreach(lc, groupClause)
2214 {
2215 SortGroupClause *clause = lfirst(lc);
2216
2217 new_grpColIdx[i++] = grouping_map[clause->tleSortGroupRef];
2218 }
2219
2220 return new_grpColIdx;
2221}

References Assert(), i, lfirst, list_length(), palloc0(), root, and SortGroupClause::tleSortGroupRef.

Referenced by create_groupingsets_plan().

◆ replace_nestloop_params()

◆ replace_nestloop_params_mutator()

static Node * replace_nestloop_params_mutator ( Node node,
PlannerInfo root 
)
static

Definition at line 4887 of file createplan.c.

4888{
4889 if (node == NULL)
4890 return NULL;
4891 if (IsA(node, Var))
4892 {
4893 Var *var = (Var *) node;
4894
4895 /* Upper-level Vars should be long gone at this point */
4896 Assert(var->varlevelsup == 0);
4897 /* If not to be replaced, we can just return the Var unmodified */
4898 if (IS_SPECIAL_VARNO(var->varno) ||
4899 !bms_is_member(var->varno, root->curOuterRels))
4900 return node;
4901 /* Replace the Var with a nestloop Param */
4902 return (Node *) replace_nestloop_param_var(root, var);
4903 }
4904 if (IsA(node, PlaceHolderVar))
4905 {
4906 PlaceHolderVar *phv = (PlaceHolderVar *) node;
4907
4908 /* Upper-level PlaceHolderVars should be long gone at this point */
4909 Assert(phv->phlevelsup == 0);
4910
4911 /* Check whether we need to replace the PHV */
4912 if (!bms_is_subset(find_placeholder_info(root, phv)->ph_eval_at,
4913 root->curOuterRels))
4914 {
4915 /*
4916 * We can't replace the whole PHV, but we might still need to
4917 * replace Vars or PHVs within its expression, in case it ends up
4918 * actually getting evaluated here. (It might get evaluated in
4919 * this plan node, or some child node; in the latter case we don't
4920 * really need to process the expression here, but we haven't got
4921 * enough info to tell if that's the case.) Flat-copy the PHV
4922 * node and then recurse on its expression.
4923 *
4924 * Note that after doing this, we might have different
4925 * representations of the contents of the same PHV in different
4926 * parts of the plan tree. This is OK because equal() will just
4927 * match on phid/phlevelsup, so setrefs.c will still recognize an
4928 * upper-level reference to a lower-level copy of the same PHV.
4929 */
4931
4932 memcpy(newphv, phv, sizeof(PlaceHolderVar));
4933 newphv->phexpr = (Expr *)
4934 replace_nestloop_params_mutator((Node *) phv->phexpr,
4935 root);
4936 return (Node *) newphv;
4937 }
4938 /* Replace the PlaceHolderVar with a nestloop Param */
4940 }
4942}
#define expression_tree_mutator(n, m, c)
Definition: nodeFuncs.h:155
Param * replace_nestloop_param_placeholdervar(PlannerInfo *root, PlaceHolderVar *phv)
Definition: paramassign.c:463
Param * replace_nestloop_param_var(PlannerInfo *root, Var *var)
Definition: paramassign.c:414
PlaceHolderInfo * find_placeholder_info(PlannerInfo *root, PlaceHolderVar *phv)
Definition: placeholder.c:83
#define IS_SPECIAL_VARNO(varno)
Definition: primnodes.h:247
Index phlevelsup
Definition: pathnodes.h:3024
Index varlevelsup
Definition: primnodes.h:294

References Assert(), bms_is_member(), bms_is_subset(), expression_tree_mutator, find_placeholder_info(), IS_SPECIAL_VARNO, IsA, makeNode, PlaceHolderVar::phlevelsup, replace_nestloop_param_placeholdervar(), replace_nestloop_param_var(), replace_nestloop_params_mutator(), root, Var::varlevelsup, and Var::varno.

Referenced by replace_nestloop_params(), and replace_nestloop_params_mutator().

◆ use_physical_tlist()

static bool use_physical_tlist ( PlannerInfo root,
Path path,
int  flags 
)
static

Definition at line 858 of file createplan.c.

859{
860 RelOptInfo *rel = path->parent;
861 int i;
862 ListCell *lc;
863
864 /*
865 * Forget it if either exact tlist or small tlist is demanded.
866 */
867 if (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST))
868 return false;
869
870 /*
871 * We can do this for real relation scans, subquery scans, function scans,
872 * tablefunc scans, values scans, and CTE scans (but not for, eg, joins).
873 */
874 if (rel->rtekind != RTE_RELATION &&
875 rel->rtekind != RTE_SUBQUERY &&
876 rel->rtekind != RTE_FUNCTION &&
877 rel->rtekind != RTE_TABLEFUNC &&
878 rel->rtekind != RTE_VALUES &&
879 rel->rtekind != RTE_CTE)
880 return false;
881
882 /*
883 * Can't do it with inheritance cases either (mainly because Append
884 * doesn't project; this test may be unnecessary now that
885 * create_append_plan instructs its children to return an exact tlist).
886 */
887 if (rel->reloptkind != RELOPT_BASEREL)
888 return false;
889
890 /*
891 * Also, don't do it to a CustomPath; the premise that we're extracting
892 * columns from a simple physical tuple is unlikely to hold for those.
893 * (When it does make sense, the custom path creator can set up the path's
894 * pathtarget that way.)
895 */
896 if (IsA(path, CustomPath))
897 return false;
898
899 /*
900 * If a bitmap scan's tlist is empty, keep it as-is. This may allow the
901 * executor to skip heap page fetches, and in any case, the benefit of
902 * using a physical tlist instead would be minimal.
903 */
904 if (IsA(path, BitmapHeapPath) &&
905 path->pathtarget->exprs == NIL)
906 return false;
907
908 /*
909 * Can't do it if any system columns or whole-row Vars are requested.
910 * (This could possibly be fixed but would take some fragile assumptions
911 * in setrefs.c, I think.)
912 */
913 for (i = rel->min_attr; i <= 0; i++)
914 {
915 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
916 return false;
917 }
918
919 /*
920 * Can't do it if the rel is required to emit any placeholder expressions,
921 * either.
922 */
923 foreach(lc, root->placeholder_list)
924 {
925 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
926
927 if (bms_nonempty_difference(phinfo->ph_needed, rel->relids) &&
928 bms_is_subset(phinfo->ph_eval_at, rel->relids))
929 return false;
930 }
931
932 /*
933 * For an index-only scan, the "physical tlist" is the index's indextlist.
934 * We can only return that without a projection if all the index's columns
935 * are returnable.
936 */
937 if (path->pathtype == T_IndexOnlyScan)
938 {
939 IndexOptInfo *indexinfo = ((IndexPath *) path)->indexinfo;
940
941 for (i = 0; i < indexinfo->ncolumns; i++)
942 {
943 if (!indexinfo->canreturn[i])
944 return false;
945 }
946 }
947
948 /*
949 * Also, can't do it if CP_LABEL_TLIST is specified and path is requested
950 * to emit any sort/group columns that are not simple Vars. (If they are
951 * simple Vars, they should appear in the physical tlist, and
952 * apply_pathtarget_labeling_to_tlist will take care of getting them
953 * labeled again.) We also have to check that no two sort/group columns
954 * are the same Var, else that element of the physical tlist would need
955 * conflicting ressortgroupref labels.
956 */
957 if ((flags & CP_LABEL_TLIST) && path->pathtarget->sortgrouprefs)
958 {
959 Bitmapset *sortgroupatts = NULL;
960
961 i = 0;
962 foreach(lc, path->pathtarget->exprs)
963 {
964 Expr *expr = (Expr *) lfirst(lc);
965
966 if (path->pathtarget->sortgrouprefs[i])
967 {
968 if (expr && IsA(expr, Var))
969 {
970 int attno = ((Var *) expr)->varattno;
971
973 if (bms_is_member(attno, sortgroupatts))
974 return false;
975 sortgroupatts = bms_add_member(sortgroupatts, attno);
976 }
977 else
978 return false;
979 }
980 i++;
981 }
982 }
983
984 return true;
985}
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:641
#define bms_is_empty(a)
Definition: bitmapset.h:118
@ RELOPT_BASEREL
Definition: pathnodes.h:883
Relids ph_needed
Definition: pathnodes.h:3316
Relids ph_eval_at
Definition: pathnodes.h:3310
AttrNumber min_attr
Definition: pathnodes.h:979

References bms_add_member(), bms_is_empty, bms_is_member(), bms_is_subset(), bms_nonempty_difference(), CP_EXACT_TLIST, CP_LABEL_TLIST, CP_SMALL_TLIST, FirstLowInvalidHeapAttributeNumber, i, IsA, lfirst, RelOptInfo::min_attr, IndexOptInfo::ncolumns, NIL, Path::pathtype, PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_needed, RelOptInfo::relids, RELOPT_BASEREL, RelOptInfo::reloptkind, root, RTE_CTE, RTE_FUNCTION, RTE_RELATION, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, and RelOptInfo::rtekind.

Referenced by create_projection_plan(), and create_scan_plan().