46 bool *istrivial_tlist);
52 bool trivial_tlist,
List *child_tlist,
53 List *interesting_pathkeys,
65 List **istrivial_tlist);
75 List *refnames_tlist);
120 root->ec_merging_done =
true;
138 leftmostQuery = leftmostRTE->
subquery;
139 Assert(leftmostQuery != NULL);
144 if (
root->hasRecursion)
161 topop->colTypes, topop->colCollations,
168 root->processed_tlist = top_tlist;
211 List *colTypes,
List *colCollations,
212 List *refnames_tlist,
214 bool *istrivial_tlist)
218 *istrivial_tlist =
true;
246 false,
root->tuple_fraction,
253 if (
root->plan_params)
254 elog(
ERROR,
"unexpected outer reference in set operation subquery");
266 *pTargetList = tlist;
267 *istrivial_tlist = trivial_tlist;
309 *istrivial_tlist = trivial_tlist;
344 elog(
ERROR,
"unrecognized node type: %d",
358 List *refnames_tlist,
368 bool lpath_trivial_tlist;
370 bool rpath_trivial_tlist;
377 elog(
ERROR,
"only UNION queries can be recursive");
387 setOp->colTypes, setOp->colCollations,
390 &lpath_trivial_tlist);
396 root->non_recursive_path = lpath;
399 setOp->colTypes, setOp->colCollations,
402 &rpath_trivial_tlist);
407 root->non_recursive_path = NULL;
416 *pTargetList = tlist;
439 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
440 errmsg(
"could not implement recursive UNION"),
441 errdetail(
"All column datatypes must be hashable.")));
447 dNumGroups = lpath->
rows + rpath->
rows * 10;
482 bool trivial_tlist,
List *child_tlist,
483 List *interesting_pathkeys,
double *pNumGroups)
493 if (interesting_pathkeys !=
NIL)
497 interesting_pathkeys);
527 if (
subpath == cheapest_input_path)
543 if (interesting_pathkeys ==
NIL)
566 if (
subpath != cheapest_input_path &&
595 if (
subpath != cheapest_input_path)
623 Path *partial_subpath;
627 partial_path = (
Path *)
677 List *refnames_tlist,
688 bool partial_paths_valid =
true;
689 bool consider_parallel =
true;
692 List *trivial_tlist_list;
697 bool try_sorted =
false;
710 &trivial_tlist_list);
720 tlist_list, refnames_tlist);
721 *pTargetList = tlist;
736 root->query_pathkeys = union_pathkeys;
744 forthree(lc, rellist, lc2, trivial_tlist_list, lc3, tlist_list)
753 union_pathkeys, NULL);
762 cheapest_pathlist =
lappend(cheapest_pathlist,
773 if (ordered_path != NULL)
774 ordered_pathlist =
lappend(ordered_pathlist, ordered_path);
787 if (consider_parallel)
791 consider_parallel =
false;
792 partial_paths_valid =
false;
795 partial_paths_valid =
false;
797 partial_pathlist =
lappend(partial_pathlist,
815 NIL,
NIL, NULL, 0,
false, -1);
828 if (partial_paths_valid)
831 int parallel_workers = 0;
834 foreach(lc, partial_pathlist)
838 parallel_workers =
Max(parallel_workers,
841 Assert(parallel_workers > 0);
852 parallel_workers =
Max(parallel_workers,
854 parallel_workers =
Min(parallel_workers,
857 Assert(parallel_workers > 0);
861 NIL, NULL, parallel_workers,
882 dNumGroups = apath->
rows;
927 if (groupList !=
NIL)
962 if (try_sorted && groupList !=
NIL)
999 List *refnames_tlist,
1005 double save_fraction =
root->tuple_fraction;
1013 bool lpath_trivial_tlist,
1014 rpath_trivial_tlist,
1015 result_trivial_tlist;
1016 List *nonunion_pathkeys =
NIL;
1028 root->tuple_fraction = 0.0;
1033 op->colTypes, op->colCollations,
1036 &lpath_trivial_tlist);
1040 op->colTypes, op->colCollations,
1043 &rpath_trivial_tlist);
1053 0,
false, lpath_tlist, refnames_tlist,
1054 &result_trivial_tlist);
1057 Assert(result_trivial_tlist);
1059 *pTargetList = tlist;
1067 if (!can_sort && !can_hash)
1069 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1071 errmsg(
"could not implement %s",
1073 errdetail(
"Some of the datatypes only support hashing, while others only support sorting.")));
1081 root->query_pathkeys = nonunion_pathkeys;
1089 nonunion_pathkeys, &dLeftGroups);
1091 dLeftGroups = lrel->
rows;
1094 nonunion_pathkeys, &dRightGroups);
1096 dRightGroups = rrel->
rows;
1099 root->tuple_fraction = save_fraction;
1119 tmplist = lpath_tlist;
1120 lpath_tlist = rpath_tlist;
1121 rpath_tlist = tmplist;
1123 dLeftGroups = dRightGroups;
1124 dRightGroups = tmpd;
1145 dNumGroups = dLeftGroups;
1146 dNumOutputRows = op->
all ? lpath->
rows : dNumGroups;
1150 dNumGroups = dLeftGroups;
1151 dNumOutputRows = op->
all ?
Min(lpath->
rows, rpath->
rows) : dNumGroups;
1153 result_rel->
rows = dNumOutputRows;
1165 elog(
ERROR,
"unrecognized set op: %d", (
int) op->
op);
1271 List *refnames_tlist,
1273 List **istrivial_tlist)
1281 *istrivial_tlist =
NIL;
1283 while (pending_rels !=
NIL)
1293 if (op->
op == top_union->
op &&
1294 (op->
all == top_union->
all || op->
all) &&
1295 equal(op->colTypes, top_union->colTypes) &&
1296 equal(op->colCollations, top_union->colCollations))
1299 pending_rels =
lcons(op->
rarg, pending_rels);
1300 pending_rels =
lcons(op->
larg, pending_rels);
1314 top_union->
all ? NULL : top_union,
1315 top_union->colTypes,
1316 top_union->colCollations,
1320 *tlist_list =
lappend(*tlist_list, child_tlist);
1321 *istrivial_tlist =
lappend_int(*istrivial_tlist, trivial_tlist);
1359 bool hack_constants,
1361 List *refnames_tlist,
1362 bool *trivial_tlist)
1373 *trivial_tlist =
true;
1375 forfour(ctlc, colTypes, cclc, colCollations,
1376 itlc, input_tlist, rtlc, refnames_tlist)
1385 Assert(!inputtle->resjunk);
1386 Assert(!reftle->resjunk);
1427 "UNION/INTERSECT/EXCEPT");
1428 *trivial_tlist =
false;
1447 *trivial_tlist =
false;
1487 List *refnames_tlist)
1508 foreach(tlistl, input_tlists)
1515 foreach(subtlistl, subtlist)
1519 Assert(!subtle->resjunk);
1520 Assert(curColType != NULL);
1527 colTypmods[colindex] = subtypmod;
1528 else if (subtypmod != colTypmods[colindex])
1529 colTypmods[colindex] = -1;
1534 colTypmods[colindex] = -1;
1536 curColType =
lnext(colTypes, curColType);
1539 Assert(curColType == NULL);
1546 forthree(curColType, colTypes, curColCollation, colCollations,
1547 ref_tl_item, refnames_tlist)
1550 int32 colTypmod = colTypmods[colindex++];
1555 Assert(!reftle->resjunk);
1601 foreach(lt, targetlist)
1614 lg =
lnext(grouplist, lg);
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
int max_parallel_workers_per_gather
void set_subquery_size_estimates(PlannerInfo *root, RelOptInfo *rel)
bool enable_parallel_append
bool enable_incremental_sort
int errdetail(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
bool equal(const void *a, const void *b)
void add_setop_child_rel_equivalences(PlannerInfo *root, RelOptInfo *child_rel, List *child_tlist, List *setop_pathkeys)
Assert(PointerIsAligned(start, uint64))
List * lappend(List *list, void *datum)
List * list_delete_first(List *list)
List * lappend_int(List *list, int datum)
List * lcons(void *datum, List *list)
Datum subpath(PG_FUNCTION_ARGS)
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
char * pstrdup(const char *in)
void pfree(void *pointer)
Oid exprType(const Node *expr)
int32 exprTypmod(const Node *expr)
Oid exprCollation(const Node *expr)
Node * applyRelabelType(Node *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat, int rlocation, bool overwrite_ok)
#define IsA(nodeptr, _type_)
#define castNode(_type_, nodeptr)
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
Path * get_cheapest_path_for_pathkeys(List *paths, List *pathkeys, Relids required_outer, CostSelector cost_criterion, bool require_parallel_safe)
bool pathkeys_count_contained_in(List *keys1, List *keys2, int *n_common)
List * make_pathkeys_for_sortclauses(PlannerInfo *root, List *sortclauses, List *tlist)
List * convert_subquery_pathkeys(PlannerInfo *root, RelOptInfo *rel, List *subquery_pathkeys, List *subquery_tlist)
bool pathkeys_contained_in(List *keys1, List *keys2)
SetOpPath * create_setop_path(PlannerInfo *root, RelOptInfo *rel, Path *leftpath, Path *rightpath, SetOpCmd cmd, SetOpStrategy strategy, List *groupList, double numGroups, double outputRows)
ProjectionPath * create_projection_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target)
Path * apply_projection_to_path(PlannerInfo *root, RelOptInfo *rel, Path *path, PathTarget *target)
void set_cheapest(RelOptInfo *parent_rel)
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
AppendPath * create_append_path(PlannerInfo *root, RelOptInfo *rel, List *subpaths, List *partial_subpaths, List *pathkeys, Relids required_outer, int parallel_workers, bool parallel_aware, double rows)
SubqueryScanPath * create_subqueryscan_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, bool trivial_pathtarget, List *pathkeys, Relids required_outer)
IncrementalSortPath * create_incremental_sort_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *pathkeys, int presorted_keys, double limit_tuples)
SortPath * create_sort_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *pathkeys, double limit_tuples)
GatherPath * create_gather_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, Relids required_outer, double *rows)
void add_path(RelOptInfo *parent_rel, Path *new_path)
UpperUniquePath * create_upper_unique_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, int numCols, double numGroups)
AggPath * create_agg_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, AggStrategy aggstrategy, AggSplit aggsplit, List *groupClause, List *qual, const AggClauseCosts *aggcosts, double numGroups)
RecursiveUnionPath * create_recursiveunion_path(PlannerInfo *root, RelOptInfo *rel, Path *leftpath, Path *rightpath, PathTarget *target, List *distinctList, int wtParam, double numGroups)
MergeAppendPath * create_merge_append_path(PlannerInfo *root, RelOptInfo *rel, List *subpaths, List *pathkeys, Relids required_outer)
static int pg_leftmost_one_pos32(uint32 word)
#define lfirst_node(type, lc)
static int list_length(const List *l)
#define forthree(cell1, list1, cell2, list2, cell3, list3)
#define forfour(cell1, list1, cell2, list2, cell3, list3, cell4, list4)
static ListCell * list_head(const List *l)
static ListCell * lnext(const List *l, const ListCell *c)
#define list_make2(x1, x2)
PlannerInfo * subquery_planner(PlannerGlobal *glob, Query *parse, PlannerInfo *parent_root, bool hasRecursion, double tuple_fraction, SetOperationStmt *setops)
create_upper_paths_hook_type create_upper_paths_hook
RelOptInfo * plan_set_operations(PlannerInfo *root)
static List * generate_setop_grouplist(SetOperationStmt *op, List *targetlist)
static RelOptInfo * generate_union_paths(SetOperationStmt *op, PlannerInfo *root, List *refnames_tlist, List **pTargetList)
static RelOptInfo * generate_nonunion_paths(SetOperationStmt *op, PlannerInfo *root, List *refnames_tlist, List **pTargetList)
static void postprocess_setop_rel(PlannerInfo *root, RelOptInfo *rel)
static List * generate_setop_tlist(List *colTypes, List *colCollations, Index varno, bool hack_constants, List *input_tlist, List *refnames_tlist, bool *trivial_tlist)
static RelOptInfo * recurse_set_operations(Node *setOp, PlannerInfo *root, SetOperationStmt *parentOp, List *colTypes, List *colCollations, List *refnames_tlist, List **pTargetList, bool *istrivial_tlist)
static RelOptInfo * generate_recursion_path(SetOperationStmt *setOp, PlannerInfo *root, List *refnames_tlist, List **pTargetList)
static List * generate_append_tlist(List *colTypes, List *colCollations, List *input_tlists, List *refnames_tlist)
static List * plan_union_children(PlannerInfo *root, SetOperationStmt *top_union, List *refnames_tlist, List **tlist_list, List **istrivial_tlist)
static void build_setop_child_paths(PlannerInfo *root, RelOptInfo *rel, bool trivial_tlist, List *child_tlist, List *interesting_pathkeys, double *pNumGroups)
static struct subre * parse(struct vars *v, int stopper, int type, struct state *init, struct state *final)
void setup_simple_rel_arrays(PlannerInfo *root)
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
RelOptInfo * build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset, EstimationInfo *estinfo)
void check_stack_depth(void)
struct PathTarget * reltarget
struct Path * cheapest_total_path
bool tlist_same_collations(List *tlist, List *colCollations, bool junkOK)
bool grouping_is_sortable(List *groupClause)
List * make_tlist_from_pathtarget(PathTarget *target)
List * get_tlist_exprs(List *tlist, bool includeJunk)
bool grouping_is_hashable(List *groupClause)
bool tlist_same_datatypes(List *tlist, List *colTypes, bool junkOK)
#define create_pathtarget(root, tlist)