46 bool *istrivial_tlist);
52 bool trivial_tlist,
List *child_tlist,
53 List *interesting_pathkeys,
65 List **istrivial_tlist);
69 double dNumGroups,
double dNumOutputRows,
70 const char *construct);
81 List *refnames_tlist);
105 Query *leftmostQuery;
126 root->ec_merging_done =
true;
144 leftmostQuery = leftmostRTE->
subquery;
145 Assert(leftmostQuery != NULL);
150 if (
root->hasRecursion)
167 topop->colTypes, topop->colCollations,
175 root->processed_tlist = top_tlist;
231 List *colTypes,
List *colCollations,
235 bool *istrivial_tlist)
239 *istrivial_tlist =
true;
270 false,
root->tuple_fraction,
277 if (
root->plan_params)
278 elog(
ERROR,
"unexpected outer reference in set operation subquery");
291 *pTargetList = tlist;
292 *istrivial_tlist = trivial_tlist;
336 *istrivial_tlist = trivial_tlist;
371 elog(
ERROR,
"unrecognized node type: %d",
385 List *refnames_tlist,
395 bool lpath_trivial_tlist;
397 bool rpath_trivial_tlist;
404 elog(
ERROR,
"only UNION queries can be recursive");
413 setOp->colTypes, setOp->colCollations,
417 &lpath_trivial_tlist);
423 root->non_recursive_path = lpath;
425 setOp->colTypes, setOp->colCollations,
429 &rpath_trivial_tlist);
434 root->non_recursive_path = NULL;
443 *pTargetList = tlist;
466 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
467 errmsg(
"could not implement recursive UNION"),
468 errdetail(
"All column datatypes must be hashable.")));
474 dNumGroups = lpath->
rows + rpath->
rows * 10;
505 bool trivial_tlist,
List *child_tlist,
506 List *interesting_pathkeys,
double *pNumGroups)
516 if (interesting_pathkeys !=
NIL)
520 interesting_pathkeys);
550 if (
subpath == cheapest_input_path)
566 if (interesting_pathkeys ==
NIL)
589 if (
subpath != cheapest_input_path &&
618 if (
subpath != cheapest_input_path)
646 Path *partial_subpath;
650 partial_path = (
Path *)
697 List *refnames_tlist,
708 bool partial_paths_valid =
true;
709 bool consider_parallel =
true;
712 List *trivial_tlist_list;
717 bool try_sorted =
false;
730 &trivial_tlist_list);
740 tlist_list, refnames_tlist);
741 *pTargetList = tlist;
756 root->query_pathkeys = union_pathkeys;
764 forthree(lc, rellist, lc2, trivial_tlist_list, lc3, tlist_list)
773 union_pathkeys, NULL);
782 cheapest_pathlist =
lappend(cheapest_pathlist,
793 if (ordered_path != NULL)
794 ordered_pathlist =
lappend(ordered_pathlist, ordered_path);
807 if (consider_parallel)
811 consider_parallel =
false;
812 partial_paths_valid =
false;
815 partial_paths_valid =
false;
817 partial_pathlist =
lappend(partial_pathlist,
835 NIL,
NIL, NULL, 0,
false, -1);
848 if (partial_paths_valid)
851 int parallel_workers = 0;
854 foreach(lc, partial_pathlist)
858 parallel_workers =
Max(parallel_workers,
861 Assert(parallel_workers > 0);
872 parallel_workers =
Max(parallel_workers,
874 parallel_workers =
Min(parallel_workers,
877 Assert(parallel_workers > 0);
881 NIL, NULL, parallel_workers,
902 dNumGroups = apath->
rows;
947 if (groupList !=
NIL)
982 if (try_sorted && groupList !=
NIL)
1019 List *refnames_tlist,
1025 double save_fraction =
root->tuple_fraction;
1035 bool lpath_trivial_tlist,
1036 rpath_trivial_tlist;
1048 root->tuple_fraction = 0.0;
1052 op->colTypes, op->colCollations,
1056 &lpath_trivial_tlist);
1061 dLeftGroups = lrel->
rows;
1065 op->colTypes, op->colCollations,
1069 &rpath_trivial_tlist);
1072 NIL, &dRightGroups);
1074 dRightGroups = rrel->
rows;
1079 root->tuple_fraction = save_fraction;
1090 tlist_list =
list_make2(lpath_tlist, rpath_tlist);
1096 tlist_list =
list_make2(rpath_tlist, lpath_tlist);
1110 tlist_list, refnames_tlist);
1112 *pTargetList = tlist;
1123 NIL, NULL, 0,
false, -1);
1138 dNumGroups = dLeftGroups;
1139 dNumOutputRows = op->
all ? lpath->
rows : dNumGroups;
1143 dNumGroups =
Min(dLeftGroups, dRightGroups);
1144 dNumOutputRows = op->
all ?
Min(lpath->
rows, rpath->
rows) : dNumGroups;
1151 dNumGroups, dNumOutputRows,
1154 if (groupList && !use_hash)
1175 elog(
ERROR,
"unrecognized set op: %d", (
int) op->
op);
1186 use_hash ? firstFlag : -1,
1211 List *refnames_tlist,
1213 List **istrivial_tlist)
1221 *istrivial_tlist =
NIL;
1223 while (pending_rels !=
NIL)
1233 if (op->
op == top_union->
op &&
1234 (op->
all == top_union->
all || op->
all) &&
1235 equal(op->colTypes, top_union->colTypes))
1238 pending_rels =
lcons(op->
rarg, pending_rels);
1239 pending_rels =
lcons(op->
larg, pending_rels);
1255 top_union->colTypes,
1256 top_union->colCollations,
1261 *tlist_list =
lappend(*tlist_list, child_tlist);
1262 *istrivial_tlist =
lappend_int(*istrivial_tlist, trivial_tlist);
1292 double dNumGroups,
double dNumOutputRows,
1293 const char *construct)
1302 double tuple_fraction;
1307 if (can_hash && can_sort)
1317 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1319 errmsg(
"could not implement %s", construct),
1320 errdetail(
"Some of the datatypes only support hashing, while others only support sorting.")));
1332 if (hashentrysize * dNumGroups > hash_mem_limit)
1347 numGroupCols, dNumGroups,
1351 input_path->
rows, input_path->pathtarget->width);
1363 input_path->
rows, input_path->pathtarget->width,
1375 tuple_fraction =
root->tuple_fraction;
1376 if (tuple_fraction >= 1.0)
1377 tuple_fraction /= dNumOutputRows;
1380 tuple_fraction) < 0)
1404 bool hack_constants,
1406 List *refnames_tlist,
1407 bool *trivial_tlist)
1418 *trivial_tlist =
true;
1420 forfour(ctlc, colTypes, cclc, colCollations,
1421 itlc, input_tlist, rtlc, refnames_tlist)
1430 Assert(!inputtle->resjunk);
1431 Assert(!reftle->resjunk);
1472 "UNION/INTERSECT/EXCEPT");
1473 *trivial_tlist =
false;
1492 *trivial_tlist =
false;
1526 *trivial_tlist =
false;
1553 List *refnames_tlist)
1574 foreach(tlistl, input_tlists)
1581 foreach(subtlistl, subtlist)
1585 if (subtle->resjunk)
1587 Assert(curColType != NULL);
1594 colTypmods[colindex] = subtypmod;
1595 else if (subtypmod != colTypmods[colindex])
1596 colTypmods[colindex] = -1;
1601 colTypmods[colindex] = -1;
1603 curColType =
lnext(colTypes, curColType);
1606 Assert(curColType == NULL);
1613 forthree(curColType, colTypes, curColCollation, colCollations,
1614 ref_tl_item, refnames_tlist)
1617 int32 colTypmod = colTypmods[colindex++];
1622 Assert(!reftle->resjunk);
1685 foreach(lt, targetlist)
1703 lg =
lnext(grouplist, lg);
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
#define Assert(condition)
int max_parallel_workers_per_gather
void cost_agg(Path *path, PlannerInfo *root, AggStrategy aggstrategy, const AggClauseCosts *aggcosts, int numGroupCols, double numGroups, List *quals, int disabled_nodes, Cost input_startup_cost, Cost input_total_cost, double input_tuples, double input_width)
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)
void cost_group(Path *path, PlannerInfo *root, int numGroupCols, double numGroups, List *quals, int input_disabled_nodes, Cost input_startup_cost, Cost input_total_cost, double input_tuples)
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)
#define SizeofMinimalTupleHeader
List * lappend(List *list, void *datum)
List * lappend_int(List *list, int datum)
List * list_delete_first(List *list)
List * lcons(void *datum, List *list)
Datum subpath(PG_FUNCTION_ARGS)
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
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)
size_t get_hash_memory_limit(void)
#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)
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)
SortPath * create_sort_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *pathkeys, double limit_tuples)
ProjectionPath * create_projection_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target)
UpperUniquePath * create_upper_unique_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, int numCols, double numGroups)
GatherPath * create_gather_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, Relids required_outer, double *rows)
MergeAppendPath * create_merge_append_path(PlannerInfo *root, RelOptInfo *rel, List *subpaths, List *pathkeys, Relids required_outer)
void set_cheapest(RelOptInfo *parent_rel)
SubqueryScanPath * create_subqueryscan_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, bool trivial_pathtarget, List *pathkeys, Relids required_outer)
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
int compare_fractional_path_costs(Path *path1, Path *path2, double fraction)
SetOpPath * create_setop_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, SetOpCmd cmd, SetOpStrategy strategy, List *distinctList, AttrNumber flagColIdx, int firstFlag, double numGroups, double outputRows)
RecursiveUnionPath * create_recursiveunion_path(PlannerInfo *root, RelOptInfo *rel, Path *leftpath, Path *rightpath, PathTarget *target, List *distinctList, int wtParam, double numGroups)
IncrementalSortPath * create_incremental_sort_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *pathkeys, int presorted_keys, double limit_tuples)
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)
void add_path(RelOptInfo *parent_rel, Path *new_path)
Path * apply_projection_to_path(PlannerInfo *root, RelOptInfo *rel, Path *path, PathTarget *target)
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)
static ListCell * list_head(const List *l)
#define forfour(cell1, list1, cell2, list2, cell3, list3, cell4, list4)
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
void check_stack_depth(void)
static Datum Int32GetDatum(int32 X)
static List * generate_setop_tlist(List *colTypes, List *colCollations, int flag, Index varno, bool hack_constants, List *input_tlist, List *refnames_tlist, bool *trivial_tlist)
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 RelOptInfo * recurse_set_operations(Node *setOp, PlannerInfo *root, List *colTypes, List *colCollations, bool junkOK, int flag, List *refnames_tlist, List **pTargetList, bool *istrivial_tlist)
static void postprocess_setop_rel(PlannerInfo *root, RelOptInfo *rel)
static bool choose_hashed_setop(PlannerInfo *root, List *groupClauses, Path *input_path, double dNumGroups, double dNumOutputRows, const char *construct)
static List * generate_append_tlist(List *colTypes, List *colCollations, bool flag, List *input_tlists, List *refnames_tlist)
static RelOptInfo * generate_recursion_path(SetOperationStmt *setOp, PlannerInfo *root, List *refnames_tlist, List **pTargetList)
bool set_operation_ordered_results_useful(SetOperationStmt *setop)
RelOptInfo * plan_set_operations(PlannerInfo *root)
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 * build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset, EstimationInfo *estinfo)
struct PathTarget * reltarget
struct Path * cheapest_total_path
bool tlist_same_collations(List *tlist, List *colCollations, bool junkOK)
List * get_tlist_exprs(List *tlist, bool includeJunk)
bool grouping_is_sortable(List *groupClause)
List * make_tlist_from_pathtarget(PathTarget *target)
bool grouping_is_hashable(List *groupClause)
bool tlist_same_datatypes(List *tlist, List *colTypes, bool junkOK)
#define create_pathtarget(root, tlist)