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 *)
674 List *refnames_tlist,
685 bool partial_paths_valid =
true;
686 bool consider_parallel =
true;
689 List *trivial_tlist_list;
694 bool try_sorted =
false;
707 &trivial_tlist_list);
717 tlist_list, refnames_tlist);
718 *pTargetList = tlist;
733 root->query_pathkeys = union_pathkeys;
741 forthree(lc, rellist, lc2, trivial_tlist_list, lc3, tlist_list)
750 union_pathkeys, NULL);
759 cheapest_pathlist =
lappend(cheapest_pathlist,
770 if (ordered_path != NULL)
771 ordered_pathlist =
lappend(ordered_pathlist, ordered_path);
784 if (consider_parallel)
788 consider_parallel =
false;
789 partial_paths_valid =
false;
792 partial_paths_valid =
false;
794 partial_pathlist =
lappend(partial_pathlist,
812 NIL,
NIL, NULL, 0,
false, -1);
825 if (partial_paths_valid)
828 int parallel_workers = 0;
831 foreach(lc, partial_pathlist)
835 parallel_workers =
Max(parallel_workers,
838 Assert(parallel_workers > 0);
849 parallel_workers =
Max(parallel_workers,
851 parallel_workers =
Min(parallel_workers,
854 Assert(parallel_workers > 0);
858 NIL, NULL, parallel_workers,
879 dNumGroups = apath->
rows;
924 if (groupList !=
NIL)
959 if (try_sorted && groupList !=
NIL)
996 List *refnames_tlist,
1002 double save_fraction =
root->tuple_fraction;
1010 bool lpath_trivial_tlist,
1011 rpath_trivial_tlist,
1012 result_trivial_tlist;
1013 List *nonunion_pathkeys =
NIL;
1025 root->tuple_fraction = 0.0;
1030 op->colTypes, op->colCollations,
1033 &lpath_trivial_tlist);
1037 op->colTypes, op->colCollations,
1040 &rpath_trivial_tlist);
1050 0,
false, lpath_tlist, refnames_tlist,
1051 &result_trivial_tlist);
1054 Assert(result_trivial_tlist);
1056 *pTargetList = tlist;
1064 if (!can_sort && !can_hash)
1066 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1068 errmsg(
"could not implement %s",
1070 errdetail(
"Some of the datatypes only support hashing, while others only support sorting.")));
1078 root->query_pathkeys = nonunion_pathkeys;
1086 nonunion_pathkeys, &dLeftGroups);
1088 dLeftGroups = lrel->
rows;
1091 nonunion_pathkeys, &dRightGroups);
1093 dRightGroups = rrel->
rows;
1096 root->tuple_fraction = save_fraction;
1116 tmplist = lpath_tlist;
1117 lpath_tlist = rpath_tlist;
1118 rpath_tlist = tmplist;
1120 dLeftGroups = dRightGroups;
1121 dRightGroups = tmpd;
1142 dNumGroups = dLeftGroups;
1143 dNumOutputRows = op->
all ? lpath->
rows : dNumGroups;
1147 dNumGroups = dLeftGroups;
1148 dNumOutputRows = op->
all ?
Min(lpath->
rows, rpath->
rows) : dNumGroups;
1150 result_rel->
rows = dNumOutputRows;
1162 elog(
ERROR,
"unrecognized set op: %d", (
int) op->
op);
1268 List *refnames_tlist,
1270 List **istrivial_tlist)
1278 *istrivial_tlist =
NIL;
1280 while (pending_rels !=
NIL)
1290 if (op->
op == top_union->
op &&
1291 (op->
all == top_union->
all || op->
all) &&
1292 equal(op->colTypes, top_union->colTypes) &&
1293 equal(op->colCollations, top_union->colCollations))
1296 pending_rels =
lcons(op->
rarg, pending_rels);
1297 pending_rels =
lcons(op->
larg, pending_rels);
1311 top_union->
all ? NULL : top_union,
1312 top_union->colTypes,
1313 top_union->colCollations,
1317 *tlist_list =
lappend(*tlist_list, child_tlist);
1318 *istrivial_tlist =
lappend_int(*istrivial_tlist, trivial_tlist);
1356 bool hack_constants,
1358 List *refnames_tlist,
1359 bool *trivial_tlist)
1370 *trivial_tlist =
true;
1372 forfour(ctlc, colTypes, cclc, colCollations,
1373 itlc, input_tlist, rtlc, refnames_tlist)
1382 Assert(!inputtle->resjunk);
1383 Assert(!reftle->resjunk);
1424 "UNION/INTERSECT/EXCEPT");
1425 *trivial_tlist =
false;
1444 *trivial_tlist =
false;
1484 List *refnames_tlist)
1505 foreach(tlistl, input_tlists)
1512 foreach(subtlistl, subtlist)
1516 Assert(!subtle->resjunk);
1517 Assert(curColType != NULL);
1524 colTypmods[colindex] = subtypmod;
1525 else if (subtypmod != colTypmods[colindex])
1526 colTypmods[colindex] = -1;
1531 colTypmods[colindex] = -1;
1533 curColType =
lnext(colTypes, curColType);
1536 Assert(curColType == NULL);
1543 forthree(curColType, colTypes, curColCollation, colCollations,
1544 ref_tl_item, refnames_tlist)
1547 int32 colTypmod = colTypmods[colindex++];
1552 Assert(!reftle->resjunk);
1598 foreach(lt, targetlist)
1611 lg =
lnext(grouplist, lg);
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
#define Assert(condition)
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)
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)