PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
planmain.h File Reference
#include "nodes/pathnodes.h"
#include "nodes/plannodes.h"
Include dependency graph for planmain.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define DEFAULT_CURSOR_TUPLE_FRACTION   0.1
 

Typedefs

typedef void(* query_pathkeys_callback) (PlannerInfo *root, void *extra)
 

Functions

RelOptInfoquery_planner (PlannerInfo *root, query_pathkeys_callback qp_callback, void *qp_extra)
 
void preprocess_minmax_aggregates (PlannerInfo *root)
 
Plancreate_plan (PlannerInfo *root, Path *best_path)
 
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)
 
Planchange_plan_targetlist (Plan *subplan, List *tlist, bool tlist_parallel_safe)
 
Planmaterialize_finished_plan (Plan *subplan)
 
bool is_projection_capable_path (Path *path)
 
bool is_projection_capable_plan (Plan *plan)
 
Sortmake_sort_from_sortclauses (List *sortcls, Plan *lefttree)
 
Aggmake_agg (List *tlist, List *qual, AggStrategy aggstrategy, AggSplit aggsplit, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations, List *groupingSets, List *chain, double dNumGroups, Size transitionSpace, Plan *lefttree)
 
Limitmake_limit (Plan *lefttree, Node *limitOffset, Node *limitCount, LimitOption limitOption, int uniqNumCols, AttrNumber *uniqColIdx, Oid *uniqOperators, Oid *uniqCollations)
 
void add_base_rels_to_query (PlannerInfo *root, Node *jtnode)
 
void add_other_rels_to_query (PlannerInfo *root)
 
void build_base_rel_tlists (PlannerInfo *root, List *final_tlist)
 
void add_vars_to_targetlist (PlannerInfo *root, List *vars, Relids where_needed)
 
void add_vars_to_attr_needed (PlannerInfo *root, List *vars, Relids where_needed)
 
void remove_useless_groupby_columns (PlannerInfo *root)
 
void find_lateral_references (PlannerInfo *root)
 
void rebuild_lateral_attr_needed (PlannerInfo *root)
 
void create_lateral_join_info (PlannerInfo *root)
 
Listdeconstruct_jointree (PlannerInfo *root)
 
bool restriction_is_always_true (PlannerInfo *root, RestrictInfo *restrictinfo)
 
bool restriction_is_always_false (PlannerInfo *root, RestrictInfo *restrictinfo)
 
void distribute_restrictinfo_to_rels (PlannerInfo *root, RestrictInfo *restrictinfo)
 
RestrictInfoprocess_implied_equality (PlannerInfo *root, Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Index security_level, bool both_const)
 
RestrictInfobuild_implied_join_equality (PlannerInfo *root, Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Index security_level)
 
void rebuild_joinclause_attr_needed (PlannerInfo *root)
 
void match_foreign_keys_to_quals (PlannerInfo *root)
 
Listremove_useless_joins (PlannerInfo *root, List *joinlist)
 
void reduce_unique_semijoins (PlannerInfo *root)
 
bool query_supports_distinctness (Query *query)
 
bool query_is_distinct_for (Query *query, List *colnos, List *opids)
 
bool innerrel_is_unique (PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache)
 
bool innerrel_is_unique_ext (PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache, List **extra_clauses)
 
Listremove_useless_self_joins (PlannerInfo *root, List *joinlist)
 
Planset_plan_references (PlannerInfo *root, Plan *plan)
 
bool trivial_subqueryscan (SubqueryScan *plan)
 
Paramfind_minmax_agg_replacement_param (PlannerInfo *root, Aggref *aggref)
 
void record_plan_function_dependency (PlannerInfo *root, Oid funcid)
 
void record_plan_type_dependency (PlannerInfo *root, Oid typid)
 
bool extract_query_dependencies_walker (Node *node, PlannerInfo *context)
 

Variables

PGDLLIMPORT double cursor_tuple_fraction
 
PGDLLIMPORT bool enable_self_join_elimination
 
PGDLLIMPORT int from_collapse_limit
 
PGDLLIMPORT int join_collapse_limit
 

Macro Definition Documentation

◆ DEFAULT_CURSOR_TUPLE_FRACTION

#define DEFAULT_CURSOR_TUPLE_FRACTION   0.1

Definition at line 21 of file planmain.h.

Typedef Documentation

◆ query_pathkeys_callback

typedef void(* query_pathkeys_callback) (PlannerInfo *root, void *extra)

Definition at line 26 of file planmain.h.

Function Documentation

◆ add_base_rels_to_query()

void add_base_rels_to_query ( PlannerInfo root,
Node jtnode 
)

Definition at line 158 of file initsplan.c.

159{
160 if (jtnode == NULL)
161 return;
162 if (IsA(jtnode, RangeTblRef))
163 {
164 int varno = ((RangeTblRef *) jtnode)->rtindex;
165
166 (void) build_simple_rel(root, varno, NULL);
167 }
168 else if (IsA(jtnode, FromExpr))
169 {
170 FromExpr *f = (FromExpr *) jtnode;
171 ListCell *l;
172
173 foreach(l, f->fromlist)
175 }
176 else if (IsA(jtnode, JoinExpr))
177 {
178 JoinExpr *j = (JoinExpr *) jtnode;
179
182 }
183 else
184 elog(ERROR, "unrecognized node type: %d",
185 (int) nodeTag(jtnode));
186}
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
void add_base_rels_to_query(PlannerInfo *root, Node *jtnode)
Definition: initsplan.c:158
int j
Definition: isn.c:78
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
#define nodeTag(nodeptr)
Definition: nodes.h:139
#define lfirst(lc)
Definition: pg_list.h:172
tree ctl root
Definition: radixtree.h:1857
RelOptInfo * build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
Definition: relnode.c:192
List * fromlist
Definition: primnodes.h:2337

References add_base_rels_to_query(), build_simple_rel(), elog, ERROR, FromExpr::fromlist, IsA, j, JoinTreeItem::jtnode, lfirst, nodeTag, and root.

Referenced by add_base_rels_to_query(), and query_planner().

◆ add_other_rels_to_query()

void add_other_rels_to_query ( PlannerInfo root)

Definition at line 196 of file initsplan.c.

197{
198 int rti;
199
200 for (rti = 1; rti < root->simple_rel_array_size; rti++)
201 {
202 RelOptInfo *rel = root->simple_rel_array[rti];
203 RangeTblEntry *rte = root->simple_rte_array[rti];
204
205 /* there may be empty slots corresponding to non-baserel RTEs */
206 if (rel == NULL)
207 continue;
208
209 /* Ignore any "otherrels" that were already added. */
210 if (rel->reloptkind != RELOPT_BASEREL)
211 continue;
212
213 /* If it's marked as inheritable, look for children. */
214 if (rte->inh)
215 expand_inherited_rtentry(root, rel, rte, rti);
216 }
217}
void expand_inherited_rtentry(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte, Index rti)
Definition: inherit.c:86
@ RELOPT_BASEREL
Definition: pathnodes.h:854
RelOptKind reloptkind
Definition: pathnodes.h:892

References expand_inherited_rtentry(), RangeTblEntry::inh, RELOPT_BASEREL, RelOptInfo::reloptkind, and root.

Referenced by query_planner().

◆ add_vars_to_attr_needed()

void add_vars_to_attr_needed ( PlannerInfo root,
List vars,
Relids  where_needed 
)

Definition at line 353 of file initsplan.c.

355{
356 ListCell *temp;
357
358 Assert(!bms_is_empty(where_needed));
359
360 foreach(temp, vars)
361 {
362 Node *node = (Node *) lfirst(temp);
363
364 if (IsA(node, Var))
365 {
366 Var *var = (Var *) node;
367 RelOptInfo *rel = find_base_rel(root, var->varno);
368 int attno = var->varattno;
369
370 if (bms_is_subset(where_needed, rel->relids))
371 continue;
372 Assert(attno >= rel->min_attr && attno <= rel->max_attr);
373 attno -= rel->min_attr;
374 rel->attr_needed[attno] = bms_add_members(rel->attr_needed[attno],
375 where_needed);
376 }
377 else if (IsA(node, PlaceHolderVar))
378 {
379 PlaceHolderVar *phv = (PlaceHolderVar *) node;
381
382 phinfo->ph_needed = bms_add_members(phinfo->ph_needed,
383 where_needed);
384 }
385 else
386 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
387 }
388}
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:412
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:917
#define bms_is_empty(a)
Definition: bitmapset.h:118
Assert(PointerIsAligned(start, uint64))
PlaceHolderInfo * find_placeholder_info(PlannerInfo *root, PlaceHolderVar *phv)
Definition: placeholder.c:83
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:414
Definition: nodes.h:135
Relids ph_needed
Definition: pathnodes.h:3230
Relids relids
Definition: pathnodes.h:898
AttrNumber min_attr
Definition: pathnodes.h:951
Definition: primnodes.h:262
AttrNumber varattno
Definition: primnodes.h:274
int varno
Definition: primnodes.h:269
Definition: regcomp.c:282

References Assert(), bms_add_members(), bms_is_empty, bms_is_subset(), elog, ERROR, find_base_rel(), find_placeholder_info(), IsA, lfirst, RelOptInfo::min_attr, nodeTag, PlaceHolderInfo::ph_needed, RelOptInfo::relids, root, Var::varattno, and Var::varno.

Referenced by rebuild_eclass_attr_needed(), rebuild_joinclause_attr_needed(), rebuild_lateral_attr_needed(), and rebuild_placeholder_attr_needed().

◆ add_vars_to_targetlist()

void add_vars_to_targetlist ( PlannerInfo root,
List vars,
Relids  where_needed 
)

Definition at line 282 of file initsplan.c.

284{
285 ListCell *temp;
286
287 Assert(!bms_is_empty(where_needed));
288
289 foreach(temp, vars)
290 {
291 Node *node = (Node *) lfirst(temp);
292
293 if (IsA(node, Var))
294 {
295 Var *var = (Var *) node;
296 RelOptInfo *rel = find_base_rel(root, var->varno);
297 int attno = var->varattno;
298
299 if (bms_is_subset(where_needed, rel->relids))
300 continue;
301 Assert(attno >= rel->min_attr && attno <= rel->max_attr);
302 attno -= rel->min_attr;
303 if (rel->attr_needed[attno] == NULL)
304 {
305 /*
306 * Variable not yet requested, so add to rel's targetlist.
307 *
308 * The value available at the rel's scan level has not been
309 * nulled by any outer join, so drop its varnullingrels.
310 * (We'll put those back as we climb up the join tree.)
311 */
312 var = copyObject(var);
313 var->varnullingrels = NULL;
314 rel->reltarget->exprs = lappend(rel->reltarget->exprs, var);
315 /* reltarget cost and width will be computed later */
316 }
317 rel->attr_needed[attno] = bms_add_members(rel->attr_needed[attno],
318 where_needed);
319 }
320 else if (IsA(node, PlaceHolderVar))
321 {
322 PlaceHolderVar *phv = (PlaceHolderVar *) node;
324
325 phinfo->ph_needed = bms_add_members(phinfo->ph_needed,
326 where_needed);
327 }
328 else
329 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
330 }
331}
List * lappend(List *list, void *datum)
Definition: list.c:339
#define copyObject(obj)
Definition: nodes.h:230
List * exprs
Definition: pathnodes.h:1669
struct PathTarget * reltarget
Definition: pathnodes.h:920

References Assert(), bms_add_members(), bms_is_empty, bms_is_subset(), copyObject, elog, ERROR, PathTarget::exprs, find_base_rel(), find_placeholder_info(), IsA, lappend(), lfirst, RelOptInfo::min_attr, nodeTag, PlaceHolderInfo::ph_needed, RelOptInfo::relids, RelOptInfo::reltarget, root, Var::varattno, and Var::varno.

Referenced by build_base_rel_tlists(), distribute_qual_to_rels(), expand_inherited_rtentry(), extract_lateral_references(), fix_placeholder_input_needed_levels(), generate_base_implied_equalities_no_const(), and process_implied_equality().

◆ build_base_rel_tlists()

void build_base_rel_tlists ( PlannerInfo root,
List final_tlist 
)

Definition at line 235 of file initsplan.c.

236{
237 List *tlist_vars = pull_var_clause((Node *) final_tlist,
241
242 if (tlist_vars != NIL)
243 {
245 list_free(tlist_vars);
246 }
247
248 /*
249 * If there's a HAVING clause, we'll need the Vars it uses, too. Note
250 * that HAVING can contain Aggrefs but not WindowFuncs.
251 */
252 if (root->parse->havingQual)
253 {
254 List *having_vars = pull_var_clause(root->parse->havingQual,
257
258 if (having_vars != NIL)
259 {
260 add_vars_to_targetlist(root, having_vars,
262 list_free(having_vars);
263 }
264 }
265}
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:216
void add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed)
Definition: initsplan.c:282
void list_free(List *list)
Definition: list.c:1546
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:193
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:195
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:196
#define NIL
Definition: pg_list.h:68
Definition: pg_list.h:54
List * pull_var_clause(Node *node, int flags)
Definition: var.c:653

References add_vars_to_targetlist(), bms_make_singleton(), list_free(), NIL, pull_var_clause(), PVC_INCLUDE_PLACEHOLDERS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_WINDOWFUNCS, and root.

Referenced by distribute_row_identity_vars(), and query_planner().

◆ build_implied_join_equality()

RestrictInfo * build_implied_join_equality ( PlannerInfo root,
Oid  opno,
Oid  collation,
Expr item1,
Expr item2,
Relids  qualscope,
Index  security_level 
)

Definition at line 3456 of file initsplan.c.

3463{
3464 RestrictInfo *restrictinfo;
3465 Expr *clause;
3466
3467 /*
3468 * Build the new clause. Copy to ensure it shares no substructure with
3469 * original (this is necessary in case there are subselects in there...)
3470 */
3471 clause = make_opclause(opno,
3472 BOOLOID, /* opresulttype */
3473 false, /* opretset */
3474 copyObject(item1),
3475 copyObject(item2),
3476 InvalidOid,
3477 collation);
3478
3479 /*
3480 * Build the RestrictInfo node itself.
3481 */
3482 restrictinfo = make_restrictinfo(root,
3483 clause,
3484 true, /* is_pushed_down */
3485 false, /* !has_clone */
3486 false, /* !is_clone */
3487 false, /* pseudoconstant */
3488 security_level, /* security_level */
3489 qualscope, /* required_relids */
3490 NULL, /* incompatible_relids */
3491 NULL); /* outer_relids */
3492
3493 /* Set mergejoinability/hashjoinability flags */
3494 check_mergejoinable(restrictinfo);
3495 check_hashjoinable(restrictinfo);
3496 check_memoizable(restrictinfo);
3497
3498 return restrictinfo;
3499}
static void check_hashjoinable(RestrictInfo *restrictinfo)
Definition: initsplan.c:3833
static void check_mergejoinable(RestrictInfo *restrictinfo)
Definition: initsplan.c:3796
static void check_memoizable(RestrictInfo *restrictinfo)
Definition: initsplan.c:3861
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: makefuncs.c:701
#define InvalidOid
Definition: postgres_ext.h:35
RestrictInfo * make_restrictinfo(PlannerInfo *root, Expr *clause, bool is_pushed_down, bool has_clone, bool is_clone, bool pseudoconstant, Index security_level, Relids required_relids, Relids incompatible_relids, Relids outer_relids)
Definition: restrictinfo.c:52

References check_hashjoinable(), check_memoizable(), check_mergejoinable(), copyObject, InvalidOid, make_opclause(), make_restrictinfo(), JoinTreeItem::qualscope, and root.

Referenced by create_join_clause(), reconsider_full_join_clause(), and reconsider_outer_join_clause().

◆ change_plan_targetlist()

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

Definition at line 2149 of file createplan.c.

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

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

Referenced by create_unique_plan(), and postgresGetForeignPlan().

◆ create_lateral_join_info()

void create_lateral_join_info ( PlannerInfo root)

Definition at line 845 of file initsplan.c.

846{
847 bool found_laterals = false;
848 Index rti;
849 ListCell *lc;
850
851 /* We need do nothing if the query contains no LATERAL RTEs */
852 if (!root->hasLateralRTEs)
853 return;
854
855 /* We'll need to have the ph_eval_at values for PlaceHolderVars */
856 Assert(root->placeholdersFrozen);
857
858 /*
859 * Examine all baserels (the rel array has been set up by now).
860 */
861 for (rti = 1; rti < root->simple_rel_array_size; rti++)
862 {
863 RelOptInfo *brel = root->simple_rel_array[rti];
864 Relids lateral_relids;
865
866 /* there may be empty slots corresponding to non-baserel RTEs */
867 if (brel == NULL)
868 continue;
869
870 Assert(brel->relid == rti); /* sanity check on array */
871
872 /* ignore RTEs that are "other rels" */
873 if (brel->reloptkind != RELOPT_BASEREL)
874 continue;
875
876 lateral_relids = NULL;
877
878 /* consider each laterally-referenced Var or PHV */
879 foreach(lc, brel->lateral_vars)
880 {
881 Node *node = (Node *) lfirst(lc);
882
883 if (IsA(node, Var))
884 {
885 Var *var = (Var *) node;
886
887 found_laterals = true;
888 lateral_relids = bms_add_member(lateral_relids,
889 var->varno);
890 }
891 else if (IsA(node, PlaceHolderVar))
892 {
893 PlaceHolderVar *phv = (PlaceHolderVar *) node;
895
896 found_laterals = true;
897 lateral_relids = bms_add_members(lateral_relids,
898 phinfo->ph_eval_at);
899 }
900 else
901 Assert(false);
902 }
903
904 /* We now have all the simple lateral refs from this rel */
905 brel->direct_lateral_relids = lateral_relids;
906 brel->lateral_relids = bms_copy(lateral_relids);
907 }
908
909 /*
910 * Now check for lateral references within PlaceHolderVars, and mark their
911 * eval_at rels as having lateral references to the source rels.
912 *
913 * For a PHV that is due to be evaluated at a baserel, mark its source(s)
914 * as direct lateral dependencies of the baserel (adding onto the ones
915 * recorded above). If it's due to be evaluated at a join, mark its
916 * source(s) as indirect lateral dependencies of each baserel in the join,
917 * ie put them into lateral_relids but not direct_lateral_relids. This is
918 * appropriate because we can't put any such baserel on the outside of a
919 * join to one of the PHV's lateral dependencies, but on the other hand we
920 * also can't yet join it directly to the dependency.
921 */
922 foreach(lc, root->placeholder_list)
923 {
924 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
925 Relids eval_at = phinfo->ph_eval_at;
926 Relids lateral_refs;
927 int varno;
928
929 if (phinfo->ph_lateral == NULL)
930 continue; /* PHV is uninteresting if no lateral refs */
931
932 found_laterals = true;
933
934 /*
935 * Include only baserels not outer joins in the evaluation sites'
936 * lateral relids. This avoids problems when outer join order gets
937 * rearranged, and it should still ensure that the lateral values are
938 * available when needed.
939 */
940 lateral_refs = bms_intersect(phinfo->ph_lateral, root->all_baserels);
941 Assert(!bms_is_empty(lateral_refs));
942
943 if (bms_get_singleton_member(eval_at, &varno))
944 {
945 /* Evaluation site is a baserel */
946 RelOptInfo *brel = find_base_rel(root, varno);
947
950 lateral_refs);
951 brel->lateral_relids =
953 lateral_refs);
954 }
955 else
956 {
957 /* Evaluation site is a join */
958 varno = -1;
959 while ((varno = bms_next_member(eval_at, varno)) >= 0)
960 {
962
963 if (brel == NULL)
964 continue; /* ignore outer joins in eval_at */
966 lateral_refs);
967 }
968 }
969 }
970
971 /*
972 * If we found no actual lateral references, we're done; but reset the
973 * hasLateralRTEs flag to avoid useless work later.
974 */
975 if (!found_laterals)
976 {
977 root->hasLateralRTEs = false;
978 return;
979 }
980
981 /*
982 * Calculate the transitive closure of the lateral_relids sets, so that
983 * they describe both direct and indirect lateral references. If relation
984 * X references Y laterally, and Y references Z laterally, then we will
985 * have to scan X on the inside of a nestloop with Z, so for all intents
986 * and purposes X is laterally dependent on Z too.
987 *
988 * This code is essentially Warshall's algorithm for transitive closure.
989 * The outer loop considers each baserel, and propagates its lateral
990 * dependencies to those baserels that have a lateral dependency on it.
991 */
992 for (rti = 1; rti < root->simple_rel_array_size; rti++)
993 {
994 RelOptInfo *brel = root->simple_rel_array[rti];
995 Relids outer_lateral_relids;
996 Index rti2;
997
998 if (brel == NULL || brel->reloptkind != RELOPT_BASEREL)
999 continue;
1000
1001 /* need not consider baserel further if it has no lateral refs */
1002 outer_lateral_relids = brel->lateral_relids;
1003 if (outer_lateral_relids == NULL)
1004 continue;
1005
1006 /* else scan all baserels */
1007 for (rti2 = 1; rti2 < root->simple_rel_array_size; rti2++)
1008 {
1009 RelOptInfo *brel2 = root->simple_rel_array[rti2];
1010
1011 if (brel2 == NULL || brel2->reloptkind != RELOPT_BASEREL)
1012 continue;
1013
1014 /* if brel2 has lateral ref to brel, propagate brel's refs */
1015 if (bms_is_member(rti, brel2->lateral_relids))
1017 outer_lateral_relids);
1018 }
1019 }
1020
1021 /*
1022 * Now that we've identified all lateral references, mark each baserel
1023 * with the set of relids of rels that reference it laterally (possibly
1024 * indirectly) --- that is, the inverse mapping of lateral_relids.
1025 */
1026 for (rti = 1; rti < root->simple_rel_array_size; rti++)
1027 {
1028 RelOptInfo *brel = root->simple_rel_array[rti];
1029 Relids lateral_relids;
1030 int rti2;
1031
1032 if (brel == NULL || brel->reloptkind != RELOPT_BASEREL)
1033 continue;
1034
1035 /* Nothing to do at rels with no lateral refs */
1036 lateral_relids = brel->lateral_relids;
1037 if (bms_is_empty(lateral_relids))
1038 continue;
1039
1040 /* No rel should have a lateral dependency on itself */
1041 Assert(!bms_is_member(rti, lateral_relids));
1042
1043 /* Mark this rel's referencees */
1044 rti2 = -1;
1045 while ((rti2 = bms_next_member(lateral_relids, rti2)) >= 0)
1046 {
1047 RelOptInfo *brel2 = root->simple_rel_array[rti2];
1048
1049 if (brel2 == NULL)
1050 continue; /* must be an OJ */
1051
1052 Assert(brel2->reloptkind == RELOPT_BASEREL);
1053 brel2->lateral_referencers =
1055 }
1056 }
1057}
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:292
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:715
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:122
unsigned int Index
Definition: c.h:585
RelOptInfo * find_base_rel_ignore_join(PlannerInfo *root, int relid)
Definition: relnode.c:454
Relids ph_lateral
Definition: pathnodes.h:3227
Relids ph_eval_at
Definition: pathnodes.h:3224
Index relid
Definition: pathnodes.h:945
List * lateral_vars
Definition: pathnodes.h:967
Relids lateral_relids
Definition: pathnodes.h:940
Relids lateral_referencers
Definition: pathnodes.h:969
Relids direct_lateral_relids
Definition: pathnodes.h:938

References Assert(), bms_add_member(), bms_add_members(), bms_copy(), bms_get_singleton_member(), bms_intersect(), bms_is_empty, bms_is_member(), bms_next_member(), RelOptInfo::direct_lateral_relids, find_base_rel(), find_base_rel_ignore_join(), find_placeholder_info(), IsA, RelOptInfo::lateral_referencers, RelOptInfo::lateral_relids, RelOptInfo::lateral_vars, lfirst, PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_lateral, RelOptInfo::relid, RELOPT_BASEREL, RelOptInfo::reloptkind, root, and Var::varno.

Referenced by query_planner().

◆ create_plan()

Plan * create_plan ( PlannerInfo root,
Path best_path 
)

Definition at line 337 of file createplan.c.

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

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().

◆ deconstruct_jointree()

List * deconstruct_jointree ( PlannerInfo root)

Definition at line 1084 of file initsplan.c.

1085{
1086 List *result;
1087 JoinDomain *top_jdomain;
1088 List *item_list = NIL;
1089 ListCell *lc;
1090
1091 /*
1092 * After this point, no more PlaceHolderInfos may be made, because
1093 * make_outerjoininfo requires all active placeholders to be present in
1094 * root->placeholder_list while we crawl up the join tree.
1095 */
1096 root->placeholdersFrozen = true;
1097
1098 /* Fetch the already-created top-level join domain for the query */
1099 top_jdomain = linitial_node(JoinDomain, root->join_domains);
1100 top_jdomain->jd_relids = NULL; /* filled during deconstruct_recurse */
1101
1102 /* Start recursion at top of jointree */
1103 Assert(root->parse->jointree != NULL &&
1104 IsA(root->parse->jointree, FromExpr));
1105
1106 /* These are filled as we scan the jointree */
1107 root->all_baserels = NULL;
1108 root->outer_join_rels = NULL;
1109
1110 /* Perform the initial scan of the jointree */
1111 result = deconstruct_recurse(root, (Node *) root->parse->jointree,
1112 top_jdomain, NULL,
1113 &item_list);
1114
1115 /* Now we can form the value of all_query_rels, too */
1116 root->all_query_rels = bms_union(root->all_baserels, root->outer_join_rels);
1117
1118 /* ... which should match what we computed for the top join domain */
1119 Assert(bms_equal(root->all_query_rels, top_jdomain->jd_relids));
1120
1121 /* Now scan all the jointree nodes again, and distribute quals */
1122 foreach(lc, item_list)
1123 {
1124 JoinTreeItem *jtitem = (JoinTreeItem *) lfirst(lc);
1125
1127 }
1128
1129 /*
1130 * If there were any special joins then we may have some postponed LEFT
1131 * JOIN clauses to deal with.
1132 */
1133 if (root->join_info_list)
1134 {
1135 foreach(lc, item_list)
1136 {
1137 JoinTreeItem *jtitem = (JoinTreeItem *) lfirst(lc);
1138
1139 if (jtitem->oj_joinclauses != NIL)
1140 deconstruct_distribute_oj_quals(root, item_list, jtitem);
1141 }
1142 }
1143
1144 /* Don't need the JoinTreeItems any more */
1145 list_free_deep(item_list);
1146
1147 return result;
1148}
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:142
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:251
static List * deconstruct_recurse(PlannerInfo *root, Node *jtnode, JoinDomain *parent_domain, JoinTreeItem *parent_jtitem, List **item_list)
Definition: initsplan.c:1166
static void deconstruct_distribute_oj_quals(PlannerInfo *root, List *jtitems, JoinTreeItem *jtitem)
Definition: initsplan.c:2226
static void deconstruct_distribute(PlannerInfo *root, JoinTreeItem *jtitem)
Definition: initsplan.c:1464
void list_free_deep(List *list)
Definition: list.c:1560
#define linitial_node(type, l)
Definition: pg_list.h:181
Relids jd_relids
Definition: pathnodes.h:1358
List * oj_joinclauses
Definition: initsplan.c:78

References Assert(), bms_equal(), bms_union(), deconstruct_distribute(), deconstruct_distribute_oj_quals(), deconstruct_recurse(), IsA, JoinDomain::jd_relids, lfirst, linitial_node, list_free_deep(), NIL, JoinTreeItem::oj_joinclauses, and root.

Referenced by query_planner().

◆ distribute_restrictinfo_to_rels()

void distribute_restrictinfo_to_rels ( PlannerInfo root,
RestrictInfo restrictinfo 
)

Definition at line 3227 of file initsplan.c.

3229{
3230 Relids relids = restrictinfo->required_relids;
3231
3232 if (!bms_is_empty(relids))
3233 {
3234 int relid;
3235
3236 if (bms_get_singleton_member(relids, &relid))
3237 {
3238 /*
3239 * There is only one relation participating in the clause, so it
3240 * is a restriction clause for that relation.
3241 */
3242 add_base_clause_to_rel(root, relid, restrictinfo);
3243 }
3244 else
3245 {
3246 /*
3247 * The clause is a join clause, since there is more than one rel
3248 * in its relid set.
3249 */
3250
3251 /*
3252 * Check for hashjoinable operators. (We don't bother setting the
3253 * hashjoin info except in true join clauses.)
3254 */
3255 check_hashjoinable(restrictinfo);
3256
3257 /*
3258 * Likewise, check if the clause is suitable to be used with a
3259 * Memoize node to cache inner tuples during a parameterized
3260 * nested loop.
3261 */
3262 check_memoizable(restrictinfo);
3263
3264 /*
3265 * Add clause to the join lists of all the relevant relations.
3266 */
3267 add_join_clause_to_rels(root, restrictinfo, relids);
3268 }
3269 }
3270 else
3271 {
3272 /*
3273 * clause references no rels, and therefore we have no place to attach
3274 * it. Shouldn't get here if callers are working properly.
3275 */
3276 elog(ERROR, "cannot cope with variable-free clause");
3277 }
3278}
static void add_base_clause_to_rel(PlannerInfo *root, Index relid, RestrictInfo *restrictinfo)
Definition: initsplan.c:2980
void add_join_clause_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo, Relids join_relids)
Definition: joininfo.c:98
Relids required_relids
Definition: pathnodes.h:2731

References add_base_clause_to_rel(), add_join_clause_to_rels(), bms_get_singleton_member(), bms_is_empty, check_hashjoinable(), check_memoizable(), elog, ERROR, RestrictInfo::required_relids, and root.

Referenced by add_non_redundant_clauses(), distribute_qual_to_rels(), generate_base_implied_equalities_broken(), generate_base_implied_equalities_const(), process_implied_equality(), reconsider_outer_join_clauses(), and remove_leftjoinrel_from_query().

◆ extract_query_dependencies_walker()

bool extract_query_dependencies_walker ( Node node,
PlannerInfo context 
)

Definition at line 3669 of file setrefs.c.

3670{
3671 if (node == NULL)
3672 return false;
3673 Assert(!IsA(node, PlaceHolderVar));
3674 if (IsA(node, Query))
3675 {
3676 Query *query = (Query *) node;
3677 ListCell *lc;
3678
3679 if (query->commandType == CMD_UTILITY)
3680 {
3681 /*
3682 * This logic must handle any utility command for which parse
3683 * analysis was nontrivial (cf. stmt_requires_parse_analysis).
3684 *
3685 * Notably, CALL requires its own processing.
3686 */
3687 if (IsA(query->utilityStmt, CallStmt))
3688 {
3689 CallStmt *callstmt = (CallStmt *) query->utilityStmt;
3690
3691 /* We need not examine funccall, just the transformed exprs */
3692 (void) extract_query_dependencies_walker((Node *) callstmt->funcexpr,
3693 context);
3694 (void) extract_query_dependencies_walker((Node *) callstmt->outargs,
3695 context);
3696 return false;
3697 }
3698
3699 /*
3700 * Ignore other utility statements, except those (such as EXPLAIN)
3701 * that contain a parsed-but-not-planned query. For those, we
3702 * just need to transfer our attention to the contained query.
3703 */
3704 query = UtilityContainsQuery(query->utilityStmt);
3705 if (query == NULL)
3706 return false;
3707 }
3708
3709 /* Remember if any Query has RLS quals applied by rewriter */
3710 if (query->hasRowSecurity)
3711 context->glob->dependsOnRole = true;
3712
3713 /* Collect relation OIDs in this Query's rtable */
3714 foreach(lc, query->rtable)
3715 {
3716 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
3717
3718 if (rte->rtekind == RTE_RELATION ||
3719 (rte->rtekind == RTE_SUBQUERY && OidIsValid(rte->relid)) ||
3720 (rte->rtekind == RTE_NAMEDTUPLESTORE && OidIsValid(rte->relid)))
3721 context->glob->relationOids =
3722 lappend_oid(context->glob->relationOids, rte->relid);
3723 }
3724
3725 /* And recurse into the query's subexpressions */
3727 context, 0);
3728 }
3729 /* Extract function dependencies and check for regclass Consts */
3730 fix_expr_common(context, node);
3732 context);
3733}
#define OidIsValid(objectId)
Definition: c.h:746
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
#define query_tree_walker(q, w, c, f)
Definition: nodeFuncs.h:158
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:153
@ CMD_UTILITY
Definition: nodes.h:276
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1033
@ RTE_SUBQUERY
Definition: parsenodes.h:1027
@ RTE_RELATION
Definition: parsenodes.h:1026
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:2028
bool extract_query_dependencies_walker(Node *node, PlannerInfo *context)
Definition: setrefs.c:3669
FuncExpr * funcexpr
Definition: parsenodes.h:3604
List * outargs
Definition: parsenodes.h:3606
bool dependsOnRole
Definition: pathnodes.h:172
List * relationOids
Definition: pathnodes.h:151
PlannerGlobal * glob
Definition: pathnodes.h:229
List * rtable
Definition: parsenodes.h:170
CmdType commandType
Definition: parsenodes.h:121
Node * utilityStmt
Definition: parsenodes.h:136
RTEKind rtekind
Definition: parsenodes.h:1061
Query * UtilityContainsQuery(Node *parsetree)
Definition: utility.c:2179

References Assert(), CMD_UTILITY, Query::commandType, PlannerGlobal::dependsOnRole, expression_tree_walker, extract_query_dependencies_walker(), fix_expr_common(), CallStmt::funcexpr, PlannerInfo::glob, IsA, lappend_oid(), lfirst, OidIsValid, CallStmt::outargs, query_tree_walker, PlannerGlobal::relationOids, Query::rtable, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_SUBQUERY, RangeTblEntry::rtekind, UtilityContainsQuery(), and Query::utilityStmt.

Referenced by expression_planner_with_deps(), extract_query_dependencies(), and extract_query_dependencies_walker().

◆ find_lateral_references()

void find_lateral_references ( PlannerInfo root)

Definition at line 658 of file initsplan.c.

659{
660 Index rti;
661
662 /* We need do nothing if the query contains no LATERAL RTEs */
663 if (!root->hasLateralRTEs)
664 return;
665
666 /*
667 * Examine all baserels (the rel array has been set up by now).
668 */
669 for (rti = 1; rti < root->simple_rel_array_size; rti++)
670 {
671 RelOptInfo *brel = root->simple_rel_array[rti];
672
673 /* there may be empty slots corresponding to non-baserel RTEs */
674 if (brel == NULL)
675 continue;
676
677 Assert(brel->relid == rti); /* sanity check on array */
678
679 /*
680 * This bit is less obvious than it might look. We ignore appendrel
681 * otherrels and consider only their parent baserels. In a case where
682 * a LATERAL-containing UNION ALL subquery was pulled up, it is the
683 * otherrel that is actually going to be in the plan. However, we
684 * want to mark all its lateral references as needed by the parent,
685 * because it is the parent's relid that will be used for join
686 * planning purposes. And the parent's RTE will contain all the
687 * lateral references we need to know, since the pulled-up member is
688 * nothing but a copy of parts of the original RTE's subquery. We
689 * could visit the parent's children instead and transform their
690 * references back to the parent's relid, but it would be much more
691 * complicated for no real gain. (Important here is that the child
692 * members have not yet received any processing beyond being pulled
693 * up.) Similarly, in appendrels created by inheritance expansion,
694 * it's sufficient to look at the parent relation.
695 */
696
697 /* ignore RTEs that are "other rels" */
698 if (brel->reloptkind != RELOPT_BASEREL)
699 continue;
700
702 }
703}
static void extract_lateral_references(PlannerInfo *root, RelOptInfo *brel, Index rtindex)
Definition: initsplan.c:706

References Assert(), extract_lateral_references(), RelOptInfo::relid, RELOPT_BASEREL, RelOptInfo::reloptkind, and root.

Referenced by query_planner().

◆ find_minmax_agg_replacement_param()

Param * find_minmax_agg_replacement_param ( PlannerInfo root,
Aggref aggref 
)

Definition at line 3519 of file setrefs.c.

3520{
3521 if (root->minmax_aggs != NIL &&
3522 list_length(aggref->args) == 1)
3523 {
3524 TargetEntry *curTarget = (TargetEntry *) linitial(aggref->args);
3525 ListCell *lc;
3526
3527 foreach(lc, root->minmax_aggs)
3528 {
3529 MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
3530
3531 if (mminfo->aggfnoid == aggref->aggfnoid &&
3532 equal(mminfo->target, curTarget->expr))
3533 return mminfo->param;
3534 }
3535 }
3536 return NULL;
3537}
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial(l)
Definition: pg_list.h:178
Oid aggfnoid
Definition: primnodes.h:461
List * args
Definition: primnodes.h:485
Param * param
Definition: pathnodes.h:3269
Expr * target
Definition: pathnodes.h:3254
Expr * expr
Definition: primnodes.h:2219

References MinMaxAggInfo::aggfnoid, Aggref::aggfnoid, Aggref::args, equal(), TargetEntry::expr, lfirst, linitial, list_length(), NIL, MinMaxAggInfo::param, root, and MinMaxAggInfo::target.

Referenced by finalize_primnode(), fix_scan_expr_mutator(), and fix_upper_expr_mutator().

◆ innerrel_is_unique()

bool innerrel_is_unique ( PlannerInfo root,
Relids  joinrelids,
Relids  outerrelids,
RelOptInfo innerrel,
JoinType  jointype,
List restrictlist,
bool  force_cache 
)

Definition at line 1288 of file analyzejoins.c.

1295{
1296 return innerrel_is_unique_ext(root, joinrelids, outerrelids, innerrel,
1297 jointype, restrictlist, force_cache, NULL);
1298}
bool innerrel_is_unique_ext(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache, List **extra_clauses)

References innerrel_is_unique_ext(), and root.

Referenced by add_paths_to_joinrel(), and reduce_unique_semijoins().

◆ innerrel_is_unique_ext()

bool innerrel_is_unique_ext ( PlannerInfo root,
Relids  joinrelids,
Relids  outerrelids,
RelOptInfo innerrel,
JoinType  jointype,
List restrictlist,
bool  force_cache,
List **  extra_clauses 
)

Definition at line 1310 of file analyzejoins.c.

1318{
1319 MemoryContext old_context;
1320 ListCell *lc;
1321 UniqueRelInfo *uniqueRelInfo;
1322 List *outer_exprs = NIL;
1323 bool self_join = (extra_clauses != NULL);
1324
1325 /* Certainly can't prove uniqueness when there are no joinclauses */
1326 if (restrictlist == NIL)
1327 return false;
1328
1329 /*
1330 * Make a quick check to eliminate cases in which we will surely be unable
1331 * to prove uniqueness of the innerrel.
1332 */
1333 if (!rel_supports_distinctness(root, innerrel))
1334 return false;
1335
1336 /*
1337 * Query the cache to see if we've managed to prove that innerrel is
1338 * unique for any subset of this outerrel. For non-self-join search, we
1339 * don't need an exact match, as extra outerrels can't make the innerrel
1340 * any less unique (or more formally, the restrictlist for a join to a
1341 * superset outerrel must be a superset of the conditions we successfully
1342 * used before). For self-join search, we require an exact match of
1343 * outerrels because we need extra clauses to be valid for our case. Also,
1344 * for self-join checking we've filtered the clauses list. Thus, we can
1345 * match only the result cached for a self-join search for another
1346 * self-join check.
1347 */
1348 foreach(lc, innerrel->unique_for_rels)
1349 {
1350 uniqueRelInfo = (UniqueRelInfo *) lfirst(lc);
1351
1352 if ((!self_join && bms_is_subset(uniqueRelInfo->outerrelids, outerrelids)) ||
1353 (self_join && bms_equal(uniqueRelInfo->outerrelids, outerrelids) &&
1354 uniqueRelInfo->self_join))
1355 {
1356 if (extra_clauses)
1357 *extra_clauses = uniqueRelInfo->extra_clauses;
1358 return true; /* Success! */
1359 }
1360 }
1361
1362 /*
1363 * Conversely, we may have already determined that this outerrel, or some
1364 * superset thereof, cannot prove this innerrel to be unique.
1365 */
1366 foreach(lc, innerrel->non_unique_for_rels)
1367 {
1368 Relids unique_for_rels = (Relids) lfirst(lc);
1369
1370 if (bms_is_subset(outerrelids, unique_for_rels))
1371 return false;
1372 }
1373
1374 /* No cached information, so try to make the proof. */
1375 if (is_innerrel_unique_for(root, joinrelids, outerrelids, innerrel,
1376 jointype, restrictlist,
1377 self_join ? &outer_exprs : NULL))
1378 {
1379 /*
1380 * Cache the positive result for future probes, being sure to keep it
1381 * in the planner_cxt even if we are working in GEQO.
1382 *
1383 * Note: one might consider trying to isolate the minimal subset of
1384 * the outerrels that proved the innerrel unique. But it's not worth
1385 * the trouble, because the planner builds up joinrels incrementally
1386 * and so we'll see the minimally sufficient outerrels before any
1387 * supersets of them anyway.
1388 */
1389 old_context = MemoryContextSwitchTo(root->planner_cxt);
1390 uniqueRelInfo = makeNode(UniqueRelInfo);
1391 uniqueRelInfo->outerrelids = bms_copy(outerrelids);
1392 uniqueRelInfo->self_join = self_join;
1393 uniqueRelInfo->extra_clauses = outer_exprs;
1394 innerrel->unique_for_rels = lappend(innerrel->unique_for_rels,
1395 uniqueRelInfo);
1396 MemoryContextSwitchTo(old_context);
1397
1398 if (extra_clauses)
1399 *extra_clauses = outer_exprs;
1400 return true; /* Success! */
1401 }
1402 else
1403 {
1404 /*
1405 * None of the join conditions for outerrel proved innerrel unique, so
1406 * we can safely reject this outerrel or any subset of it in future
1407 * checks.
1408 *
1409 * However, in normal planning mode, caching this knowledge is totally
1410 * pointless; it won't be queried again, because we build up joinrels
1411 * from smaller to larger. It is useful in GEQO mode, where the
1412 * knowledge can be carried across successive planning attempts; and
1413 * it's likely to be useful when using join-search plugins, too. Hence
1414 * cache when join_search_private is non-NULL. (Yeah, that's a hack,
1415 * but it seems reasonable.)
1416 *
1417 * Also, allow callers to override that heuristic and force caching;
1418 * that's useful for reduce_unique_semijoins, which calls here before
1419 * the normal join search starts.
1420 */
1421 if (force_cache || root->join_search_private)
1422 {
1423 old_context = MemoryContextSwitchTo(root->planner_cxt);
1424 innerrel->non_unique_for_rels =
1425 lappend(innerrel->non_unique_for_rels,
1426 bms_copy(outerrelids));
1427 MemoryContextSwitchTo(old_context);
1428 }
1429
1430 return false;
1431 }
1432}
static bool is_innerrel_unique_for(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, List **extra_clauses)
static bool rel_supports_distinctness(PlannerInfo *root, RelOptInfo *rel)
Definition: analyzejoins.c:902
#define makeNode(_type_)
Definition: nodes.h:161
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
Bitmapset * Relids
Definition: pathnodes.h:30
List * unique_for_rels
Definition: pathnodes.h:1004
List * non_unique_for_rels
Definition: pathnodes.h:1006
Relids outerrelids
Definition: pathnodes.h:3586
List * extra_clauses
Definition: pathnodes.h:3600

References bms_copy(), bms_equal(), bms_is_subset(), UniqueRelInfo::extra_clauses, is_innerrel_unique_for(), lappend(), lfirst, makeNode, MemoryContextSwitchTo(), NIL, RelOptInfo::non_unique_for_rels, UniqueRelInfo::outerrelids, rel_supports_distinctness(), root, UniqueRelInfo::self_join, and RelOptInfo::unique_for_rels.

Referenced by innerrel_is_unique(), and remove_self_joins_one_group().

◆ is_projection_capable_path()

bool is_projection_capable_path ( Path path)

Definition at line 7307 of file createplan.c.

7308{
7309 /* Most plan types can project, so just list the ones that can't */
7310 switch (path->pathtype)
7311 {
7312 case T_Hash:
7313 case T_Material:
7314 case T_Memoize:
7315 case T_Sort:
7316 case T_IncrementalSort:
7317 case T_Unique:
7318 case T_SetOp:
7319 case T_LockRows:
7320 case T_Limit:
7321 case T_ModifyTable:
7322 case T_MergeAppend:
7323 case T_RecursiveUnion:
7324 return false;
7325 case T_CustomScan:
7327 return true;
7328 return false;
7329 case T_Append:
7330
7331 /*
7332 * Append can't project, but if an AppendPath is being used to
7333 * represent a dummy path, what will actually be generated is a
7334 * Result which can project.
7335 */
7336 return IS_DUMMY_APPEND(path);
7337 case T_ProjectSet:
7338
7339 /*
7340 * Although ProjectSet certainly projects, say "no" because we
7341 * don't want the planner to randomly replace its tlist with
7342 * something else; the SRFs have to stay at top level. This might
7343 * get relaxed later.
7344 */
7345 return false;
7346 default:
7347 break;
7348 }
7349 return true;
7350}
#define CUSTOMPATH_SUPPORT_PROJECTION
Definition: extensible.h:86
#define castNode(_type_, nodeptr)
Definition: nodes.h:182
#define IS_DUMMY_APPEND(p)
Definition: pathnodes.h:2075
NodeTag pathtype
Definition: pathnodes.h:1762

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

7358{
7359 /* Most plan types can project, so just list the ones that can't */
7360 switch (nodeTag(plan))
7361 {
7362 case T_Hash:
7363 case T_Material:
7364 case T_Memoize:
7365 case T_Sort:
7366 case T_Unique:
7367 case T_SetOp:
7368 case T_LockRows:
7369 case T_Limit:
7370 case T_ModifyTable:
7371 case T_Append:
7372 case T_MergeAppend:
7373 case T_RecursiveUnion:
7374 return false;
7375 case T_CustomScan:
7377 return true;
7378 return false;
7379 case T_ProjectSet:
7380
7381 /*
7382 * Although ProjectSet certainly projects, say "no" because we
7383 * don't want the planner to randomly replace its tlist with
7384 * something else; the SRFs have to stay at top level. This might
7385 * get relaxed later.
7386 */
7387 return false;
7388 default:
7389 break;
7390 }
7391 return true;
7392}

References CUSTOMPATH_SUPPORT_PROJECTION, nodeTag, and plan.

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

◆ 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,
double  dNumGroups,
Size  transitionSpace,
Plan lefttree 
)

Definition at line 6662 of file createplan.c.

6667{
6668 Agg *node = makeNode(Agg);
6669 Plan *plan = &node->plan;
6670 long numGroups;
6671
6672 /* Reduce to long, but 'ware overflow! */
6673 numGroups = clamp_cardinality_to_long(dNumGroups);
6674
6675 node->aggstrategy = aggstrategy;
6676 node->aggsplit = aggsplit;
6677 node->numCols = numGroupCols;
6678 node->grpColIdx = grpColIdx;
6679 node->grpOperators = grpOperators;
6680 node->grpCollations = grpCollations;
6681 node->numGroups = numGroups;
6682 node->transitionSpace = transitionSpace;
6683 node->aggParams = NULL; /* SS_finalize_plan() will fill this */
6684 node->groupingSets = groupingSets;
6685 node->chain = chain;
6686
6687 plan->qual = qual;
6688 plan->targetlist = tlist;
6689 plan->lefttree = lefttree;
6690 plan->righttree = NULL;
6691
6692 return node;
6693}
long clamp_cardinality_to_long(Cardinality x)
Definition: costsize.c:265
AggSplit aggsplit
Definition: plannodes.h:1146
List * chain
Definition: plannodes.h:1173
long numGroups
Definition: plannodes.h:1159
List * groupingSets
Definition: plannodes.h:1170
Bitmapset * aggParams
Definition: plannodes.h:1165
Plan plan
Definition: plannodes.h:1140
int numCols
Definition: plannodes.h:1149
uint64 transitionSpace
Definition: plannodes.h:1162
AggStrategy aggstrategy
Definition: plannodes.h:1143

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

Referenced by create_agg_plan(), create_groupingsets_plan(), and create_unique_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 5888 of file createplan.c.

5896{
5898 Plan *plan = &node->scan.plan;
5899
5900 /* cost will be filled in by create_foreignscan_plan */
5901 plan->targetlist = qptlist;
5902 plan->qual = qpqual;
5903 plan->lefttree = outer_plan;
5904 plan->righttree = NULL;
5905 node->scan.scanrelid = scanrelid;
5906
5907 /* these may be overridden by the FDW's PlanDirectModify callback. */
5908 node->operation = CMD_SELECT;
5909 node->resultRelation = 0;
5910
5911 /* checkAsUser, fs_server will be filled in by create_foreignscan_plan */
5912 node->checkAsUser = InvalidOid;
5913 node->fs_server = InvalidOid;
5914 node->fdw_exprs = fdw_exprs;
5915 node->fdw_private = fdw_private;
5916 node->fdw_scan_tlist = fdw_scan_tlist;
5917 node->fdw_recheck_quals = fdw_recheck_quals;
5918 /* fs_relids, fs_base_relids will be filled by create_foreignscan_plan */
5919 node->fs_relids = NULL;
5920 node->fs_base_relids = NULL;
5921 /* fsSystemCol will be filled in by create_foreignscan_plan */
5922 node->fsSystemCol = false;
5923
5924 return node;
5925}
@ CMD_SELECT
Definition: nodes.h:271
Oid checkAsUser
Definition: plannodes.h:837
CmdType operation
Definition: plannodes.h:833
Oid fs_server
Definition: plannodes.h:839
List * fdw_exprs
Definition: plannodes.h:841
bool fsSystemCol
Definition: plannodes.h:853
Bitmapset * fs_relids
Definition: plannodes.h:849
List * fdw_private
Definition: plannodes.h:843
Bitmapset * fs_base_relids
Definition: plannodes.h:851
Index resultRelation
Definition: plannodes.h:835
List * fdw_recheck_quals
Definition: plannodes.h:847
List * fdw_scan_tlist
Definition: plannodes.h:845
Index scanrelid
Definition: plannodes.h:483

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_limit()

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

Definition at line 7032 of file createplan.c.

7035{
7036 Limit *node = makeNode(Limit);
7037 Plan *plan = &node->plan;
7038
7039 plan->targetlist = lefttree->targetlist;
7040 plan->qual = NIL;
7041 plan->lefttree = lefttree;
7042 plan->righttree = NULL;
7043
7044 node->limitOffset = limitOffset;
7045 node->limitCount = limitCount;
7046 node->limitOption = limitOption;
7047 node->uniqNumCols = uniqNumCols;
7048 node->uniqColIdx = uniqColIdx;
7049 node->uniqOperators = uniqOperators;
7050 node->uniqCollations = uniqCollations;
7051
7052 return node;
7053}
LimitOption limitOption
Definition: plannodes.h:1438
Plan plan
Definition: plannodes.h:1429
Node * limitCount
Definition: plannodes.h:1435
int uniqNumCols
Definition: plannodes.h:1441
Node * limitOffset
Definition: plannodes.h:1432

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_sort_from_sortclauses()

Sort * make_sort_from_sortclauses ( List sortcls,
Plan lefttree 
)

Definition at line 6482 of file createplan.c.

6483{
6484 List *sub_tlist = lefttree->targetlist;
6485 ListCell *l;
6486 int numsortkeys;
6487 AttrNumber *sortColIdx;
6488 Oid *sortOperators;
6489 Oid *collations;
6490 bool *nullsFirst;
6491
6492 /* Convert list-ish representation to arrays wanted by executor */
6493 numsortkeys = list_length(sortcls);
6494 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
6495 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
6496 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
6497 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
6498
6499 numsortkeys = 0;
6500 foreach(l, sortcls)
6501 {
6502 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
6503 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
6504
6505 sortColIdx[numsortkeys] = tle->resno;
6506 sortOperators[numsortkeys] = sortcl->sortop;
6507 collations[numsortkeys] = exprCollation((Node *) tle->expr);
6508 nullsFirst[numsortkeys] = sortcl->nulls_first;
6509 numsortkeys++;
6510 }
6511
6512 return make_sort(lefttree, numsortkeys,
6513 sortColIdx, sortOperators,
6514 collations, nullsFirst);
6515}
int16 AttrNumber
Definition: attnum.h:21
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
Definition: createplan.c:6134
void * palloc(Size size)
Definition: mcxt.c:1940
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:821
unsigned int Oid
Definition: postgres_ext.h:30
AttrNumber resno
Definition: primnodes.h:2221
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:367

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

Referenced by create_unique_plan().

◆ match_foreign_keys_to_quals()

void match_foreign_keys_to_quals ( PlannerInfo root)

Definition at line 3631 of file initsplan.c.

3632{
3633 List *newlist = NIL;
3634 ListCell *lc;
3635
3636 foreach(lc, root->fkey_list)
3637 {
3638 ForeignKeyOptInfo *fkinfo = (ForeignKeyOptInfo *) lfirst(lc);
3639 RelOptInfo *con_rel;
3640 RelOptInfo *ref_rel;
3641 int colno;
3642
3643 /*
3644 * Either relid might identify a rel that is in the query's rtable but
3645 * isn't referenced by the jointree, or has been removed by join
3646 * removal, so that it won't have a RelOptInfo. Hence don't use
3647 * find_base_rel() here. We can ignore such FKs.
3648 */
3649 if (fkinfo->con_relid >= root->simple_rel_array_size ||
3650 fkinfo->ref_relid >= root->simple_rel_array_size)
3651 continue; /* just paranoia */
3652 con_rel = root->simple_rel_array[fkinfo->con_relid];
3653 if (con_rel == NULL)
3654 continue;
3655 ref_rel = root->simple_rel_array[fkinfo->ref_relid];
3656 if (ref_rel == NULL)
3657 continue;
3658
3659 /*
3660 * Ignore FK unless both rels are baserels. This gets rid of FKs that
3661 * link to inheritance child rels (otherrels).
3662 */
3663 if (con_rel->reloptkind != RELOPT_BASEREL ||
3664 ref_rel->reloptkind != RELOPT_BASEREL)
3665 continue;
3666
3667 /*
3668 * Scan the columns and try to match them to eclasses and quals.
3669 *
3670 * Note: for simple inner joins, any match should be in an eclass.
3671 * "Loose" quals that syntactically match an FK equality must have
3672 * been rejected for EC status because they are outer-join quals or
3673 * similar. We can still consider them to match the FK.
3674 */
3675 for (colno = 0; colno < fkinfo->nkeys; colno++)
3676 {
3677 EquivalenceClass *ec;
3678 AttrNumber con_attno,
3679 ref_attno;
3680 Oid fpeqop;
3681 ListCell *lc2;
3682
3683 ec = match_eclasses_to_foreign_key_col(root, fkinfo, colno);
3684 /* Don't bother looking for loose quals if we got an EC match */
3685 if (ec != NULL)
3686 {
3687 fkinfo->nmatched_ec++;
3688 if (ec->ec_has_const)
3689 fkinfo->nconst_ec++;
3690 continue;
3691 }
3692
3693 /*
3694 * Scan joininfo list for relevant clauses. Either rel's joininfo
3695 * list would do equally well; we use con_rel's.
3696 */
3697 con_attno = fkinfo->conkey[colno];
3698 ref_attno = fkinfo->confkey[colno];
3699 fpeqop = InvalidOid; /* we'll look this up only if needed */
3700
3701 foreach(lc2, con_rel->joininfo)
3702 {
3703 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc2);
3704 OpExpr *clause = (OpExpr *) rinfo->clause;
3705 Var *leftvar;
3706 Var *rightvar;
3707
3708 /* Only binary OpExprs are useful for consideration */
3709 if (!IsA(clause, OpExpr) ||
3710 list_length(clause->args) != 2)
3711 continue;
3712 leftvar = (Var *) get_leftop((Expr *) clause);
3713 rightvar = (Var *) get_rightop((Expr *) clause);
3714
3715 /* Operands must be Vars, possibly with RelabelType */
3716 while (leftvar && IsA(leftvar, RelabelType))
3717 leftvar = (Var *) ((RelabelType *) leftvar)->arg;
3718 if (!(leftvar && IsA(leftvar, Var)))
3719 continue;
3720 while (rightvar && IsA(rightvar, RelabelType))
3721 rightvar = (Var *) ((RelabelType *) rightvar)->arg;
3722 if (!(rightvar && IsA(rightvar, Var)))
3723 continue;
3724
3725 /* Now try to match the vars to the current foreign key cols */
3726 if (fkinfo->ref_relid == leftvar->varno &&
3727 ref_attno == leftvar->varattno &&
3728 fkinfo->con_relid == rightvar->varno &&
3729 con_attno == rightvar->varattno)
3730 {
3731 /* Vars match, but is it the right operator? */
3732 if (clause->opno == fkinfo->conpfeqop[colno])
3733 {
3734 fkinfo->rinfos[colno] = lappend(fkinfo->rinfos[colno],
3735 rinfo);
3736 fkinfo->nmatched_ri++;
3737 }
3738 }
3739 else if (fkinfo->ref_relid == rightvar->varno &&
3740 ref_attno == rightvar->varattno &&
3741 fkinfo->con_relid == leftvar->varno &&
3742 con_attno == leftvar->varattno)
3743 {
3744 /*
3745 * Reverse match, must check commutator operator. Look it
3746 * up if we didn't already. (In the worst case we might
3747 * do multiple lookups here, but that would require an FK
3748 * equality operator without commutator, which is
3749 * unlikely.)
3750 */
3751 if (!OidIsValid(fpeqop))
3752 fpeqop = get_commutator(fkinfo->conpfeqop[colno]);
3753 if (clause->opno == fpeqop)
3754 {
3755 fkinfo->rinfos[colno] = lappend(fkinfo->rinfos[colno],
3756 rinfo);
3757 fkinfo->nmatched_ri++;
3758 }
3759 }
3760 }
3761 /* If we found any matching loose quals, count col as matched */
3762 if (fkinfo->rinfos[colno])
3763 fkinfo->nmatched_rcols++;
3764 }
3765
3766 /*
3767 * Currently, we drop multicolumn FKs that aren't fully matched to the
3768 * query. Later we might figure out how to derive some sort of
3769 * estimate from them, in which case this test should be weakened to
3770 * "if ((fkinfo->nmatched_ec + fkinfo->nmatched_rcols) > 0)".
3771 */
3772 if ((fkinfo->nmatched_ec + fkinfo->nmatched_rcols) == fkinfo->nkeys)
3773 newlist = lappend(newlist, fkinfo);
3774 }
3775 /* Replace fkey_list, thereby discarding any useless entries */
3776 root->fkey_list = newlist;
3777}
EquivalenceClass * match_eclasses_to_foreign_key_col(PlannerInfo *root, ForeignKeyOptInfo *fkinfo, int colno)
Definition: equivclass.c:2710
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1649
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:95
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:83
List * rinfos[INDEX_MAX_KEYS]
Definition: pathnodes.h:1291
Oid opno
Definition: primnodes.h:835
List * args
Definition: primnodes.h:853
List * joininfo
Definition: pathnodes.h:1018
Expr * clause
Definition: pathnodes.h:2700

References OpExpr::args, RestrictInfo::clause, ForeignKeyOptInfo::con_relid, EquivalenceClass::ec_has_const, get_commutator(), get_leftop(), get_rightop(), if(), InvalidOid, IsA, RelOptInfo::joininfo, lappend(), lfirst, list_length(), match_eclasses_to_foreign_key_col(), ForeignKeyOptInfo::nconst_ec, NIL, ForeignKeyOptInfo::nkeys, ForeignKeyOptInfo::nmatched_ec, ForeignKeyOptInfo::nmatched_rcols, ForeignKeyOptInfo::nmatched_ri, OidIsValid, OpExpr::opno, ForeignKeyOptInfo::ref_relid, RELOPT_BASEREL, RelOptInfo::reloptkind, ForeignKeyOptInfo::rinfos, and root.

Referenced by query_planner().

◆ materialize_finished_plan()

Plan * materialize_finished_plan ( Plan subplan)

Definition at line 6594 of file createplan.c.

6595{
6596 Plan *matplan;
6597 Path matpath; /* dummy for result of cost_material */
6598 Cost initplan_cost;
6599 bool unsafe_initplans;
6600
6601 matplan = (Plan *) make_material(subplan);
6602
6603 /*
6604 * XXX horrid kluge: if there are any initPlans attached to the subplan,
6605 * move them up to the Material node, which is now effectively the top
6606 * plan node in its query level. This prevents failure in
6607 * SS_finalize_plan(), which see for comments.
6608 */
6609 matplan->initPlan = subplan->initPlan;
6610 subplan->initPlan = NIL;
6611
6612 /* Move the initplans' cost delta, as well */
6614 &initplan_cost, &unsafe_initplans);
6615 subplan->startup_cost -= initplan_cost;
6616 subplan->total_cost -= initplan_cost;
6617
6618 /* Set cost data */
6619 cost_material(&matpath,
6620 subplan->disabled_nodes,
6621 subplan->startup_cost,
6622 subplan->total_cost,
6623 subplan->plan_rows,
6624 subplan->plan_width);
6625 matplan->disabled_nodes = subplan->disabled_nodes;
6626 matplan->startup_cost = matpath.startup_cost + initplan_cost;
6627 matplan->total_cost = matpath.total_cost + initplan_cost;
6628 matplan->plan_rows = subplan->plan_rows;
6629 matplan->plan_width = subplan->plan_width;
6630 matplan->parallel_aware = false;
6631 matplan->parallel_safe = subplan->parallel_safe;
6632
6633 return matplan;
6634}
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
static Material * make_material(Plan *lefttree)
Definition: createplan.c:6572
double Cost
Definition: nodes.h:257
Cost startup_cost
Definition: pathnodes.h:1798
Cost total_cost
Definition: pathnodes.h:1799
Cost total_cost
Definition: plannodes.h:179
bool parallel_aware
Definition: plannodes.h:193
Cost startup_cost
Definition: plannodes.h:177
int plan_width
Definition: plannodes.h:187
Cardinality plan_rows
Definition: plannodes.h:185
int disabled_nodes
Definition: plannodes.h:175
List * initPlan
Definition: plannodes.h:216
void SS_compute_initplan_cost(List *init_plans, Cost *initplan_cost_p, bool *unsafe_initplans_p)
Definition: subselect.c:2312

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().

◆ preprocess_minmax_aggregates()

void preprocess_minmax_aggregates ( PlannerInfo root)

Definition at line 73 of file planagg.c.

74{
75 Query *parse = root->parse;
76 FromExpr *jtnode;
77 RangeTblRef *rtr;
78 RangeTblEntry *rte;
79 List *aggs_list;
80 RelOptInfo *grouped_rel;
81 ListCell *lc;
82
83 /* minmax_aggs list should be empty at this point */
84 Assert(root->minmax_aggs == NIL);
85
86 /* Nothing to do if query has no aggregates */
87 if (!parse->hasAggs)
88 return;
89
90 Assert(!parse->setOperations); /* shouldn't get here if a setop */
91 Assert(parse->rowMarks == NIL); /* nor if FOR UPDATE */
92
93 /*
94 * Reject unoptimizable cases.
95 *
96 * We don't handle GROUP BY or windowing, because our current
97 * implementations of grouping require looking at all the rows anyway, and
98 * so there's not much point in optimizing MIN/MAX.
99 */
100 if (parse->groupClause || list_length(parse->groupingSets) > 1 ||
101 parse->hasWindowFuncs)
102 return;
103
104 /*
105 * Reject if query contains any CTEs; there's no way to build an indexscan
106 * on one so we couldn't succeed here. (If the CTEs are unreferenced,
107 * that's not true, but it doesn't seem worth expending cycles to check.)
108 */
109 if (parse->cteList)
110 return;
111
112 /*
113 * We also restrict the query to reference exactly one table, since join
114 * conditions can't be handled reasonably. (We could perhaps handle a
115 * query containing cartesian-product joins, but it hardly seems worth the
116 * trouble.) However, the single table could be buried in several levels
117 * of FromExpr due to subqueries. Note the "single" table could be an
118 * inheritance parent, too, including the case of a UNION ALL subquery
119 * that's been flattened to an appendrel.
120 */
121 jtnode = parse->jointree;
122 while (IsA(jtnode, FromExpr))
123 {
124 if (list_length(jtnode->fromlist) != 1)
125 return;
126 jtnode = linitial(jtnode->fromlist);
127 }
128 if (!IsA(jtnode, RangeTblRef))
129 return;
130 rtr = (RangeTblRef *) jtnode;
131 rte = planner_rt_fetch(rtr->rtindex, root);
132 if (rte->rtekind == RTE_RELATION)
133 /* ordinary relation, ok */ ;
134 else if (rte->rtekind == RTE_SUBQUERY && rte->inh)
135 /* flattened UNION ALL subquery, ok */ ;
136 else
137 return;
138
139 /*
140 * Examine all the aggregates and verify all are MIN/MAX aggregates. Stop
141 * as soon as we find one that isn't.
142 */
143 aggs_list = NIL;
144 if (!can_minmax_aggs(root, &aggs_list))
145 return;
146
147 /*
148 * OK, there is at least the possibility of performing the optimization.
149 * Build an access path for each aggregate. If any of the aggregates
150 * prove to be non-indexable, give up; there is no point in optimizing
151 * just some of them.
152 */
153 foreach(lc, aggs_list)
154 {
155 MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
156 Oid eqop;
157 bool reverse;
158
159 /*
160 * We'll need the equality operator that goes with the aggregate's
161 * ordering operator.
162 */
163 eqop = get_equality_op_for_ordering_op(mminfo->aggsortop, &reverse);
164 if (!OidIsValid(eqop)) /* shouldn't happen */
165 elog(ERROR, "could not find equality operator for ordering operator %u",
166 mminfo->aggsortop);
167
168 /*
169 * We can use either an ordering that gives NULLS FIRST or one that
170 * gives NULLS LAST; furthermore there's unlikely to be much
171 * performance difference between them, so it doesn't seem worth
172 * costing out both ways if we get a hit on the first one. NULLS
173 * FIRST is more likely to be available if the operator is a
174 * reverse-sort operator, so try that first if reverse.
175 */
176 if (build_minmax_path(root, mminfo, eqop, mminfo->aggsortop, reverse, reverse))
177 continue;
178 if (build_minmax_path(root, mminfo, eqop, mminfo->aggsortop, reverse, !reverse))
179 continue;
180
181 /* No indexable path for this aggregate, so fail */
182 return;
183 }
184
185 /*
186 * OK, we can do the query this way. Prepare to create a MinMaxAggPath
187 * node.
188 *
189 * First, create an output Param node for each agg. (If we end up not
190 * using the MinMaxAggPath, we'll waste a PARAM_EXEC slot for each agg,
191 * which is not worth worrying about. We can't wait till create_plan time
192 * to decide whether to make the Param, unfortunately.)
193 */
194 foreach(lc, aggs_list)
195 {
196 MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
197
198 mminfo->param =
200 exprType((Node *) mminfo->target),
201 -1,
202 exprCollation((Node *) mminfo->target));
203 }
204
205 /*
206 * Create a MinMaxAggPath node with the appropriate estimated costs and
207 * other needed data, and add it to the UPPERREL_GROUP_AGG upperrel, where
208 * it will compete against the standard aggregate implementation. (It
209 * will likely always win, but we need not assume that here.)
210 *
211 * Note: grouping_planner won't have created this upperrel yet, but it's
212 * fine for us to create it first. We will not have inserted the correct
213 * consider_parallel value in it, but MinMaxAggPath paths are currently
214 * never parallel-safe anyway, so that doesn't matter. Likewise, it
215 * doesn't matter that we haven't filled FDW-related fields in the rel.
216 * Also, because there are no rowmarks, we know that the processed_tlist
217 * doesn't need to change anymore, so making the pathtarget now is safe.
218 */
219 grouped_rel = fetch_upper_rel(root, UPPERREL_GROUP_AGG, NULL);
220 add_path(grouped_rel, (Path *)
221 create_minmaxagg_path(root, grouped_rel,
223 root->processed_tlist),
224 aggs_list,
225 (List *) parse->havingQual));
226}
Oid get_equality_op_for_ordering_op(Oid opno, bool *reverse)
Definition: lsyscache.c:330
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
MinMaxAggPath * create_minmaxagg_path(PlannerInfo *root, RelOptInfo *rel, PathTarget *target, List *mmaggregates, List *quals)
Definition: pathnode.c:3486
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:461
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:597
@ UPPERREL_GROUP_AGG
Definition: pathnodes.h:74
static bool can_minmax_aggs(PlannerInfo *root, List **context)
Definition: planagg.c:237
static bool build_minmax_path(PlannerInfo *root, MinMaxAggInfo *mminfo, Oid eqop, Oid sortop, bool reverse_sort, bool nulls_first)
Definition: planagg.c:317
static struct subre * parse(struct vars *v, int stopper, int type, struct state *init, struct state *final)
Definition: regcomp.c:717
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:1458
Param * SS_make_initplan_output_param(PlannerInfo *root, Oid resulttype, int32 resulttypmod, Oid resultcollation)
Definition: subselect.c:3113
#define create_pathtarget(root, tlist)
Definition: tlist.h:53

References add_path(), MinMaxAggInfo::aggsortop, Assert(), build_minmax_path(), can_minmax_aggs(), create_minmaxagg_path(), create_pathtarget, elog, ERROR, exprCollation(), exprType(), fetch_upper_rel(), FromExpr::fromlist, get_equality_op_for_ordering_op(), RangeTblEntry::inh, IsA, lfirst, linitial, list_length(), NIL, OidIsValid, MinMaxAggInfo::param, parse(), planner_rt_fetch, root, RTE_RELATION, RTE_SUBQUERY, RangeTblEntry::rtekind, RangeTblRef::rtindex, SS_make_initplan_output_param(), MinMaxAggInfo::target, and UPPERREL_GROUP_AGG.

Referenced by grouping_planner().

◆ process_implied_equality()

RestrictInfo * process_implied_equality ( PlannerInfo root,
Oid  opno,
Oid  collation,
Expr item1,
Expr item2,
Relids  qualscope,
Index  security_level,
bool  both_const 
)

Definition at line 3312 of file initsplan.c.

3320{
3321 RestrictInfo *restrictinfo;
3322 Node *clause;
3323 Relids relids;
3324 bool pseudoconstant = false;
3325
3326 /*
3327 * Build the new clause. Copy to ensure it shares no substructure with
3328 * original (this is necessary in case there are subselects in there...)
3329 */
3330 clause = (Node *) make_opclause(opno,
3331 BOOLOID, /* opresulttype */
3332 false, /* opretset */
3333 copyObject(item1),
3334 copyObject(item2),
3335 InvalidOid,
3336 collation);
3337
3338 /* If both constant, try to reduce to a boolean constant. */
3339 if (both_const)
3340 {
3341 clause = eval_const_expressions(root, clause);
3342
3343 /* If we produced const TRUE, just drop the clause */
3344 if (clause && IsA(clause, Const))
3345 {
3346 Const *cclause = (Const *) clause;
3347
3348 Assert(cclause->consttype == BOOLOID);
3349 if (!cclause->constisnull && DatumGetBool(cclause->constvalue))
3350 return NULL;
3351 }
3352 }
3353
3354 /*
3355 * The rest of this is a very cut-down version of distribute_qual_to_rels.
3356 * We can skip most of the work therein, but there are a couple of special
3357 * cases we still have to handle.
3358 *
3359 * Retrieve all relids mentioned within the possibly-simplified clause.
3360 */
3361 relids = pull_varnos(root, clause);
3362 Assert(bms_is_subset(relids, qualscope));
3363
3364 /*
3365 * If the clause is variable-free, our normal heuristic for pushing it
3366 * down to just the mentioned rels doesn't work, because there are none.
3367 * Apply it as a gating qual at the appropriate level (see comments for
3368 * get_join_domain_min_rels).
3369 */
3370 if (bms_is_empty(relids))
3371 {
3372 /* eval at join domain's safe level */
3373 relids = get_join_domain_min_rels(root, qualscope);
3374 /* mark as gating qual */
3375 pseudoconstant = true;
3376 /* tell createplan.c to check for gating quals */
3377 root->hasPseudoConstantQuals = true;
3378 }
3379
3380 /*
3381 * Build the RestrictInfo node itself.
3382 */
3383 restrictinfo = make_restrictinfo(root,
3384 (Expr *) clause,
3385 true, /* is_pushed_down */
3386 false, /* !has_clone */
3387 false, /* !is_clone */
3388 pseudoconstant,
3389 security_level,
3390 relids,
3391 NULL, /* incompatible_relids */
3392 NULL); /* outer_relids */
3393
3394 /*
3395 * If it's a join clause, add vars used in the clause to targetlists of
3396 * their relations, so that they will be emitted by the plan nodes that
3397 * scan those relations (else they won't be available at the join node!).
3398 *
3399 * Typically, we'd have already done this when the component expressions
3400 * were first seen by distribute_qual_to_rels; but it is possible that
3401 * some of the Vars could have missed having that done because they only
3402 * appeared in single-relation clauses originally. So do it here for
3403 * safety.
3404 *
3405 * See also rebuild_joinclause_attr_needed, which has to partially repeat
3406 * this work after removal of an outer join. (Since we will put this
3407 * clause into the joininfo lists, that function needn't do any extra work
3408 * to find it.)
3409 */
3410 if (bms_membership(relids) == BMS_MULTIPLE)
3411 {
3412 List *vars = pull_var_clause(clause,
3416
3418 list_free(vars);
3419 }
3420
3421 /*
3422 * Check mergejoinability. This will usually succeed, since the op came
3423 * from an EquivalenceClass; but we could have reduced the original clause
3424 * to a constant.
3425 */
3426 check_mergejoinable(restrictinfo);
3427
3428 /*
3429 * Note we don't do initialize_mergeclause_eclasses(); the caller can
3430 * handle that much more cheaply than we can. It's okay to call
3431 * distribute_restrictinfo_to_rels() before that happens.
3432 */
3433
3434 /*
3435 * Push the new clause into all the appropriate restrictinfo lists.
3436 */
3438
3439 return restrictinfo;
3440}
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:781
@ BMS_MULTIPLE
Definition: bitmapset.h:73
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2256
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:3227
static Relids get_join_domain_min_rels(PlannerInfo *root, Relids domain_relids)
Definition: initsplan.c:3525
static bool DatumGetBool(Datum X)
Definition: postgres.h:95
Oid consttype
Definition: primnodes.h:329
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:114

References add_vars_to_targetlist(), Assert(), bms_is_empty, bms_is_subset(), bms_membership(), BMS_MULTIPLE, check_mergejoinable(), Const::consttype, copyObject, DatumGetBool(), distribute_restrictinfo_to_rels(), eval_const_expressions(), get_join_domain_min_rels(), InvalidOid, IsA, list_free(), make_opclause(), make_restrictinfo(), pull_var_clause(), pull_varnos(), PVC_INCLUDE_PLACEHOLDERS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_WINDOWFUNCS, JoinTreeItem::qualscope, and root.

Referenced by generate_base_implied_equalities_const(), and generate_base_implied_equalities_no_const().

◆ query_is_distinct_for()

bool query_is_distinct_for ( Query query,
List colnos,
List opids 
)

Definition at line 1099 of file analyzejoins.c.

1100{
1101 ListCell *l;
1102 Oid opid;
1103
1104 Assert(list_length(colnos) == list_length(opids));
1105
1106 /*
1107 * DISTINCT (including DISTINCT ON) guarantees uniqueness if all the
1108 * columns in the DISTINCT clause appear in colnos and operator semantics
1109 * match. This is true even if there are SRFs in the DISTINCT columns or
1110 * elsewhere in the tlist.
1111 */
1112 if (query->distinctClause)
1113 {
1114 foreach(l, query->distinctClause)
1115 {
1118 query->targetList);
1119
1120 opid = distinct_col_search(tle->resno, colnos, opids);
1121 if (!OidIsValid(opid) ||
1122 !equality_ops_are_compatible(opid, sgc->eqop))
1123 break; /* exit early if no match */
1124 }
1125 if (l == NULL) /* had matches for all? */
1126 return true;
1127 }
1128
1129 /*
1130 * Otherwise, a set-returning function in the query's targetlist can
1131 * result in returning duplicate rows, despite any grouping that might
1132 * occur before tlist evaluation. (If all tlist SRFs are within GROUP BY
1133 * columns, it would be safe because they'd be expanded before grouping.
1134 * But it doesn't currently seem worth the effort to check for that.)
1135 */
1136 if (query->hasTargetSRFs)
1137 return false;
1138
1139 /*
1140 * Similarly, GROUP BY without GROUPING SETS guarantees uniqueness if all
1141 * the grouped columns appear in colnos and operator semantics match.
1142 */
1143 if (query->groupClause && !query->groupingSets)
1144 {
1145 foreach(l, query->groupClause)
1146 {
1149 query->targetList);
1150
1151 opid = distinct_col_search(tle->resno, colnos, opids);
1152 if (!OidIsValid(opid) ||
1153 !equality_ops_are_compatible(opid, sgc->eqop))
1154 break; /* exit early if no match */
1155 }
1156 if (l == NULL) /* had matches for all? */
1157 return true;
1158 }
1159 else if (query->groupingSets)
1160 {
1161 /*
1162 * If we have grouping sets with expressions, we probably don't have
1163 * uniqueness and analysis would be hard. Punt.
1164 */
1165 if (query->groupClause)
1166 return false;
1167
1168 /*
1169 * If we have no groupClause (therefore no grouping expressions), we
1170 * might have one or many empty grouping sets. If there's just one,
1171 * then we're returning only one row and are certainly unique. But
1172 * otherwise, we know we're certainly not unique.
1173 */
1174 if (list_length(query->groupingSets) == 1 &&
1175 ((GroupingSet *) linitial(query->groupingSets))->kind == GROUPING_SET_EMPTY)
1176 return true;
1177 else
1178 return false;
1179 }
1180 else
1181 {
1182 /*
1183 * If we have no GROUP BY, but do have aggregates or HAVING, then the
1184 * result is at most one row so it's surely unique, for any operators.
1185 */
1186 if (query->hasAggs || query->havingQual)
1187 return true;
1188 }
1189
1190 /*
1191 * UNION, INTERSECT, EXCEPT guarantee uniqueness of the whole output row,
1192 * except with ALL.
1193 */
1194 if (query->setOperations)
1195 {
1197
1198 Assert(topop->op != SETOP_NONE);
1199
1200 if (!topop->all)
1201 {
1202 ListCell *lg;
1203
1204 /* We're good if all the nonjunk output columns are in colnos */
1205 lg = list_head(topop->groupClauses);
1206 foreach(l, query->targetList)
1207 {
1208 TargetEntry *tle = (TargetEntry *) lfirst(l);
1209 SortGroupClause *sgc;
1210
1211 if (tle->resjunk)
1212 continue; /* ignore resjunk columns */
1213
1214 /* non-resjunk columns should have grouping clauses */
1215 Assert(lg != NULL);
1216 sgc = (SortGroupClause *) lfirst(lg);
1217 lg = lnext(topop->groupClauses, lg);
1218
1219 opid = distinct_col_search(tle->resno, colnos, opids);
1220 if (!OidIsValid(opid) ||
1221 !equality_ops_are_compatible(opid, sgc->eqop))
1222 break; /* exit early if no match */
1223 }
1224 if (l == NULL) /* had matches for all? */
1225 return true;
1226 }
1227 }
1228
1229 /*
1230 * XXX Are there any other cases in which we can easily see the result
1231 * must be distinct?
1232 *
1233 * If you do add more smarts to this function, be sure to update
1234 * query_supports_distinctness() to match.
1235 */
1236
1237 return false;
1238}
static Oid distinct_col_search(int colno, List *colnos, List *opids)
bool equality_ops_are_compatible(Oid opno1, Oid opno2)
Definition: lsyscache.c:779
@ GROUPING_SET_EMPTY
Definition: parsenodes.h:1513
@ SETOP_NONE
Definition: parsenodes.h:2167
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
Node * setOperations
Definition: parsenodes.h:230
List * groupClause
Definition: parsenodes.h:211
Node * havingQual
Definition: parsenodes.h:216
List * targetList
Definition: parsenodes.h:193
List * groupingSets
Definition: parsenodes.h:214
List * distinctClause
Definition: parsenodes.h:220
SetOperation op
Definition: parsenodes.h:2247

References SetOperationStmt::all, Assert(), castNode, distinct_col_search(), Query::distinctClause, SortGroupClause::eqop, equality_ops_are_compatible(), get_sortgroupclause_tle(), Query::groupClause, GROUPING_SET_EMPTY, Query::groupingSets, Query::havingQual, lfirst, linitial, list_head(), list_length(), lnext(), OidIsValid, SetOperationStmt::op, TargetEntry::resno, SETOP_NONE, Query::setOperations, and Query::targetList.

Referenced by create_unique_path(), and rel_is_distinct_for().

◆ query_planner()

RelOptInfo * query_planner ( PlannerInfo root,
query_pathkeys_callback  qp_callback,
void *  qp_extra 
)

Definition at line 54 of file planmain.c.

56{
57 Query *parse = root->parse;
58 List *joinlist;
59 RelOptInfo *final_rel;
60
61 /*
62 * Init planner lists to empty.
63 *
64 * NOTE: append_rel_list was set up by subquery_planner, so do not touch
65 * here.
66 */
67 root->join_rel_list = NIL;
68 root->join_rel_hash = NULL;
69 root->join_rel_level = NULL;
70 root->join_cur_level = 0;
71 root->canon_pathkeys = NIL;
72 root->left_join_clauses = NIL;
73 root->right_join_clauses = NIL;
74 root->full_join_clauses = NIL;
75 root->join_info_list = NIL;
76 root->placeholder_list = NIL;
77 root->placeholder_array = NULL;
78 root->placeholder_array_size = 0;
79 root->fkey_list = NIL;
80 root->initial_rels = NIL;
81
82 /*
83 * Set up arrays for accessing base relations and AppendRelInfos.
84 */
86
87 /*
88 * In the trivial case where the jointree is a single RTE_RESULT relation,
89 * bypass all the rest of this function and just make a RelOptInfo and its
90 * one access path. This is worth optimizing because it applies for
91 * common cases like "SELECT expression" and "INSERT ... VALUES()".
92 */
93 Assert(parse->jointree->fromlist != NIL);
94 if (list_length(parse->jointree->fromlist) == 1)
95 {
96 Node *jtnode = (Node *) linitial(parse->jointree->fromlist);
97
98 if (IsA(jtnode, RangeTblRef))
99 {
100 int varno = ((RangeTblRef *) jtnode)->rtindex;
101 RangeTblEntry *rte = root->simple_rte_array[varno];
102
103 Assert(rte != NULL);
104 if (rte->rtekind == RTE_RESULT)
105 {
106 /* Make the RelOptInfo for it directly */
107 final_rel = build_simple_rel(root, varno, NULL);
108
109 /*
110 * If query allows parallelism in general, check whether the
111 * quals are parallel-restricted. (We need not check
112 * final_rel->reltarget because it's empty at this point.
113 * Anything parallel-restricted in the query tlist will be
114 * dealt with later.) We should always do this in a subquery,
115 * since it might be useful to use the subquery in parallel
116 * paths in the parent level. At top level this is normally
117 * not worth the cycles, because a Result-only plan would
118 * never be interesting to parallelize. However, if
119 * debug_parallel_query is on, then we want to execute the
120 * Result in a parallel worker if possible, so we must check.
121 */
122 if (root->glob->parallelModeOK &&
123 (root->query_level > 1 ||
125 final_rel->consider_parallel =
126 is_parallel_safe(root, parse->jointree->quals);
127
128 /*
129 * The only path for it is a trivial Result path. We cheat a
130 * bit here by using a GroupResultPath, because that way we
131 * can just jam the quals into it without preprocessing them.
132 * (But, if you hold your head at the right angle, a FROM-less
133 * SELECT is a kind of degenerate-grouping case, so it's not
134 * that much of a cheat.)
135 */
136 add_path(final_rel, (Path *)
138 final_rel->reltarget,
139 (List *) parse->jointree->quals));
140
141 /* Select cheapest path (pretty easy in this case...) */
142 set_cheapest(final_rel);
143
144 /*
145 * We don't need to run generate_base_implied_equalities, but
146 * we do need to pretend that EC merging is complete.
147 */
148 root->ec_merging_done = true;
149
150 /*
151 * We still are required to call qp_callback, in case it's
152 * something like "SELECT 2+2 ORDER BY 1".
153 */
154 (*qp_callback) (root, qp_extra);
155
156 return final_rel;
157 }
158 }
159 }
160
161 /*
162 * Construct RelOptInfo nodes for all base relations used in the query.
163 * Appendrel member relations ("other rels") will be added later.
164 *
165 * Note: the reason we find the baserels by searching the jointree, rather
166 * than scanning the rangetable, is that the rangetable may contain RTEs
167 * for rels not actively part of the query, for example views. We don't
168 * want to make RelOptInfos for them.
169 */
170 add_base_rels_to_query(root, (Node *) parse->jointree);
171
172 /* Remove any redundant GROUP BY columns */
174
175 /*
176 * Examine the targetlist and join tree, adding entries to baserel
177 * targetlists for all referenced Vars, and generating PlaceHolderInfo
178 * entries for all referenced PlaceHolderVars. Restrict and join clauses
179 * are added to appropriate lists belonging to the mentioned relations. We
180 * also build EquivalenceClasses for provably equivalent expressions. The
181 * SpecialJoinInfo list is also built to hold information about join order
182 * restrictions. Finally, we form a target joinlist for make_one_rel() to
183 * work from.
184 */
185 build_base_rel_tlists(root, root->processed_tlist);
186
188
190
191 joinlist = deconstruct_jointree(root);
192
193 /*
194 * Reconsider any postponed outer-join quals now that we have built up
195 * equivalence classes. (This could result in further additions or
196 * mergings of classes.)
197 */
199
200 /*
201 * If we formed any equivalence classes, generate additional restriction
202 * clauses as appropriate. (Implied join clauses are formed on-the-fly
203 * later.)
204 */
206
207 /*
208 * We have completed merging equivalence sets, so it's now possible to
209 * generate pathkeys in canonical form; so compute query_pathkeys and
210 * other pathkeys fields in PlannerInfo.
211 */
212 (*qp_callback) (root, qp_extra);
213
214 /*
215 * Examine any "placeholder" expressions generated during subquery pullup.
216 * Make sure that the Vars they need are marked as needed at the relevant
217 * join level. This must be done before join removal because it might
218 * cause Vars or placeholders to be needed above a join when they weren't
219 * so marked before.
220 */
222
223 /*
224 * Remove any useless outer joins. Ideally this would be done during
225 * jointree preprocessing, but the necessary information isn't available
226 * until we've built baserel data structures and classified qual clauses.
227 */
228 joinlist = remove_useless_joins(root, joinlist);
229
230 /*
231 * Also, reduce any semijoins with unique inner rels to plain inner joins.
232 * Likewise, this can't be done until now for lack of needed info.
233 */
235
236 /*
237 * Remove self joins on a unique column.
238 */
239 joinlist = remove_useless_self_joins(root, joinlist);
240
241 /*
242 * Now distribute "placeholders" to base rels as needed. This has to be
243 * done after join removal because removal could change whether a
244 * placeholder is evaluable at a base rel.
245 */
247
248 /*
249 * Construct the lateral reference sets now that we have finalized
250 * PlaceHolderVar eval levels.
251 */
253
254 /*
255 * Match foreign keys to equivalence classes and join quals. This must be
256 * done after finalizing equivalence classes, and it's useful to wait till
257 * after join removal so that we can skip processing foreign keys
258 * involving removed relations.
259 */
261
262 /*
263 * Look for join OR clauses that we can extract single-relation
264 * restriction OR clauses from.
265 */
267
268 /*
269 * Now expand appendrels by adding "otherrels" for their children. We
270 * delay this to the end so that we have as much information as possible
271 * available for each baserel, including all restriction clauses. That
272 * let us prune away partitions that don't satisfy a restriction clause.
273 * Also note that some information such as lateral_relids is propagated
274 * from baserels to otherrels here, so we must have computed it already.
275 */
277
278 /*
279 * Distribute any UPDATE/DELETE/MERGE row identity variables to the target
280 * relations. This can't be done till we've finished expansion of
281 * appendrels.
282 */
284
285 /*
286 * Ready to do the primary planning.
287 */
288 final_rel = make_one_rel(root, joinlist);
289
290 /* Check that we got at least one usable path */
291 if (!final_rel || !final_rel->cheapest_total_path ||
292 final_rel->cheapest_total_path->param_info != NULL)
293 elog(ERROR, "failed to construct the join relation");
294
295 return final_rel;
296}
RelOptInfo * make_one_rel(PlannerInfo *root, List *joinlist)
Definition: allpaths.c:171
List * remove_useless_joins(PlannerInfo *root, List *joinlist)
Definition: analyzejoins.c:88
List * remove_useless_self_joins(PlannerInfo *root, List *joinlist)
void reduce_unique_semijoins(PlannerInfo *root)
Definition: analyzejoins.c:826
void distribute_row_identity_vars(PlannerInfo *root)
Definition: appendinfo.c:985
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:754
void generate_base_implied_equalities(PlannerInfo *root)
Definition: equivclass.c:1188
void reconsider_outer_join_clauses(PlannerInfo *root)
Definition: equivclass.c:2135
void match_foreign_keys_to_quals(PlannerInfo *root)
Definition: initsplan.c:3631
void find_lateral_references(PlannerInfo *root)
Definition: initsplan.c:658
void remove_useless_groupby_columns(PlannerInfo *root)
Definition: initsplan.c:412
void build_base_rel_tlists(PlannerInfo *root, List *final_tlist)
Definition: initsplan.c:235
List * deconstruct_jointree(PlannerInfo *root)
Definition: initsplan.c:1084
void add_other_rels_to_query(PlannerInfo *root)
Definition: initsplan.c:196
void create_lateral_join_info(PlannerInfo *root)
Definition: initsplan.c:845
@ DEBUG_PARALLEL_OFF
Definition: optimizer.h:106
void extract_restriction_or_clauses(PlannerInfo *root)
Definition: orclauses.c:75
@ RTE_RESULT
Definition: parsenodes.h:1034
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:269
GroupResultPath * create_group_result_path(PlannerInfo *root, RelOptInfo *rel, PathTarget *target, List *havingqual)
Definition: pathnode.c:1586
void add_placeholders_to_base_rels(PlannerInfo *root)
Definition: placeholder.c:356
void fix_placeholder_input_needed_levels(PlannerInfo *root)
Definition: placeholder.c:300
void find_placeholders_in_jointree(PlannerInfo *root)
Definition: placeholder.c:185
int debug_parallel_query
Definition: planner.c:68
void setup_simple_rel_arrays(PlannerInfo *root)
Definition: relnode.c:94
bool consider_parallel
Definition: pathnodes.h:914
struct Path * cheapest_total_path
Definition: pathnodes.h:929

References add_base_rels_to_query(), add_other_rels_to_query(), add_path(), add_placeholders_to_base_rels(), Assert(), build_base_rel_tlists(), build_simple_rel(), RelOptInfo::cheapest_total_path, RelOptInfo::consider_parallel, create_group_result_path(), create_lateral_join_info(), DEBUG_PARALLEL_OFF, debug_parallel_query, deconstruct_jointree(), distribute_row_identity_vars(), elog, ERROR, extract_restriction_or_clauses(), find_lateral_references(), find_placeholders_in_jointree(), fix_placeholder_input_needed_levels(), generate_base_implied_equalities(), is_parallel_safe(), IsA, linitial, list_length(), make_one_rel(), match_foreign_keys_to_quals(), NIL, parse(), reconsider_outer_join_clauses(), reduce_unique_semijoins(), RelOptInfo::reltarget, remove_useless_groupby_columns(), remove_useless_joins(), remove_useless_self_joins(), root, RTE_RESULT, RangeTblEntry::rtekind, set_cheapest(), and setup_simple_rel_arrays().

Referenced by build_minmax_path(), and grouping_planner().

◆ query_supports_distinctness()

bool query_supports_distinctness ( Query query)

Definition at line 1061 of file analyzejoins.c.

1062{
1063 /* SRFs break distinctness except with DISTINCT, see below */
1064 if (query->hasTargetSRFs && query->distinctClause == NIL)
1065 return false;
1066
1067 /* check for features we can prove distinctness with */
1068 if (query->distinctClause != NIL ||
1069 query->groupClause != NIL ||
1070 query->groupingSets != NIL ||
1071 query->hasAggs ||
1072 query->havingQual ||
1073 query->setOperations)
1074 return true;
1075
1076 return false;
1077}

References Query::distinctClause, Query::groupClause, Query::groupingSets, Query::havingQual, NIL, and Query::setOperations.

Referenced by create_unique_path(), and rel_supports_distinctness().

◆ rebuild_joinclause_attr_needed()

void rebuild_joinclause_attr_needed ( PlannerInfo root)

Definition at line 3559 of file initsplan.c.

3560{
3561 /*
3562 * We must examine all join clauses, but there's no value in processing
3563 * any join clause more than once. So it's slightly annoying that we have
3564 * to find them via the per-base-relation joininfo lists. Avoid duplicate
3565 * processing by tracking the rinfo_serial numbers of join clauses we've
3566 * already seen. (This doesn't work for is_clone clauses, so we must
3567 * waste effort on them.)
3568 */
3569 Bitmapset *seen_serials = NULL;
3570 Index rti;
3571
3572 /* Scan all baserels for join clauses */
3573 for (rti = 1; rti < root->simple_rel_array_size; rti++)
3574 {
3575 RelOptInfo *brel = root->simple_rel_array[rti];
3576 ListCell *lc;
3577
3578 if (brel == NULL)
3579 continue;
3580 if (brel->reloptkind != RELOPT_BASEREL)
3581 continue;
3582
3583 foreach(lc, brel->joininfo)
3584 {
3585 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3586 Relids relids = rinfo->required_relids;
3587
3588 if (!rinfo->is_clone) /* else serial number is not unique */
3589 {
3590 if (bms_is_member(rinfo->rinfo_serial, seen_serials))
3591 continue; /* saw it already */
3592 seen_serials = bms_add_member(seen_serials,
3593 rinfo->rinfo_serial);
3594 }
3595
3596 if (bms_membership(relids) == BMS_MULTIPLE)
3597 {
3598 List *vars = pull_var_clause((Node *) rinfo->clause,
3602 Relids where_needed;
3603
3604 if (rinfo->is_clone)
3605 where_needed = bms_intersect(relids, root->all_baserels);
3606 else
3607 where_needed = relids;
3608 add_vars_to_attr_needed(root, vars, where_needed);
3609 list_free(vars);
3610 }
3611 }
3612 }
3613}
void add_vars_to_attr_needed(PlannerInfo *root, List *vars, Relids where_needed)
Definition: initsplan.c:353
int rinfo_serial
Definition: pathnodes.h:2772

References add_vars_to_attr_needed(), bms_add_member(), bms_intersect(), bms_is_member(), bms_membership(), BMS_MULTIPLE, RestrictInfo::clause, RestrictInfo::is_clone, RelOptInfo::joininfo, lfirst, list_free(), pull_var_clause(), PVC_INCLUDE_PLACEHOLDERS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_WINDOWFUNCS, RELOPT_BASEREL, RelOptInfo::reloptkind, RestrictInfo::required_relids, RestrictInfo::rinfo_serial, and root.

Referenced by remove_leftjoinrel_from_query(), and remove_self_join_rel().

◆ rebuild_lateral_attr_needed()

void rebuild_lateral_attr_needed ( PlannerInfo root)

Definition at line 807 of file initsplan.c.

808{
809 Index rti;
810
811 /* We need do nothing if the query contains no LATERAL RTEs */
812 if (!root->hasLateralRTEs)
813 return;
814
815 /* Examine the same baserels that find_lateral_references did */
816 for (rti = 1; rti < root->simple_rel_array_size; rti++)
817 {
818 RelOptInfo *brel = root->simple_rel_array[rti];
819 Relids where_needed;
820
821 if (brel == NULL)
822 continue;
823 if (brel->reloptkind != RELOPT_BASEREL)
824 continue;
825
826 /*
827 * We don't need to repeat all of extract_lateral_references, since it
828 * kindly saved the extracted Vars/PHVs in lateral_vars.
829 */
830 if (brel->lateral_vars == NIL)
831 continue;
832
833 where_needed = bms_make_singleton(rti);
834
835 add_vars_to_attr_needed(root, brel->lateral_vars, where_needed);
836 }
837}

References add_vars_to_attr_needed(), bms_make_singleton(), RelOptInfo::lateral_vars, NIL, RELOPT_BASEREL, RelOptInfo::reloptkind, and root.

Referenced by remove_leftjoinrel_from_query(), and remove_self_join_rel().

◆ record_plan_function_dependency()

void record_plan_function_dependency ( PlannerInfo root,
Oid  funcid 
)

Definition at line 3552 of file setrefs.c.

3553{
3554 /*
3555 * For performance reasons, we don't bother to track built-in functions;
3556 * we just assume they'll never change (or at least not in ways that'd
3557 * invalidate plans using them). For this purpose we can consider a
3558 * built-in function to be one with OID less than FirstUnpinnedObjectId.
3559 * Note that the OID generator guarantees never to generate such an OID
3560 * after startup, even at OID wraparound.
3561 */
3562 if (funcid >= (Oid) FirstUnpinnedObjectId)
3563 {
3564 PlanInvalItem *inval_item = makeNode(PlanInvalItem);
3565
3566 /*
3567 * It would work to use any syscache on pg_proc, but the easiest is
3568 * PROCOID since we already have the function's OID at hand. Note
3569 * that plancache.c knows we use PROCOID.
3570 */
3571 inval_item->cacheId = PROCOID;
3572 inval_item->hashValue = GetSysCacheHashValue1(PROCOID,
3573 ObjectIdGetDatum(funcid));
3574
3575 root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
3576 }
3577}
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
uint32 hashValue
Definition: plannodes.h:1754
#define GetSysCacheHashValue1(cacheId, key1)
Definition: syscache.h:118
#define FirstUnpinnedObjectId
Definition: transam.h:196

References PlanInvalItem::cacheId, FirstUnpinnedObjectId, GetSysCacheHashValue1, PlanInvalItem::hashValue, lappend(), makeNode, ObjectIdGetDatum(), and root.

Referenced by fix_expr_common(), inline_function(), and inline_set_returning_function().

◆ record_plan_type_dependency()

void record_plan_type_dependency ( PlannerInfo root,
Oid  typid 
)

Definition at line 3592 of file setrefs.c.

3593{
3594 /*
3595 * As in record_plan_function_dependency, ignore the possibility that
3596 * someone would change a built-in domain.
3597 */
3598 if (typid >= (Oid) FirstUnpinnedObjectId)
3599 {
3600 PlanInvalItem *inval_item = makeNode(PlanInvalItem);
3601
3602 /*
3603 * It would work to use any syscache on pg_type, but the easiest is
3604 * TYPEOID since we already have the type's OID at hand. Note that
3605 * plancache.c knows we use TYPEOID.
3606 */
3607 inval_item->cacheId = TYPEOID;
3608 inval_item->hashValue = GetSysCacheHashValue1(TYPEOID,
3609 ObjectIdGetDatum(typid));
3610
3611 root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
3612 }
3613}

References PlanInvalItem::cacheId, FirstUnpinnedObjectId, GetSysCacheHashValue1, PlanInvalItem::hashValue, lappend(), makeNode, ObjectIdGetDatum(), and root.

Referenced by eval_const_expressions_mutator().

◆ reduce_unique_semijoins()

void reduce_unique_semijoins ( PlannerInfo root)

Definition at line 826 of file analyzejoins.c.

827{
828 ListCell *lc;
829
830 /*
831 * Scan the join_info_list to find semijoins.
832 */
833 foreach(lc, root->join_info_list)
834 {
835 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
836 int innerrelid;
837 RelOptInfo *innerrel;
838 Relids joinrelids;
839 List *restrictlist;
840
841 /*
842 * Must be a semijoin to a single baserel, else we aren't going to be
843 * able to do anything with it.
844 */
845 if (sjinfo->jointype != JOIN_SEMI)
846 continue;
847
848 if (!bms_get_singleton_member(sjinfo->min_righthand, &innerrelid))
849 continue;
850
851 innerrel = find_base_rel(root, innerrelid);
852
853 /*
854 * Before we trouble to run generate_join_implied_equalities, make a
855 * quick check to eliminate cases in which we will surely be unable to
856 * prove uniqueness of the innerrel.
857 */
858 if (!rel_supports_distinctness(root, innerrel))
859 continue;
860
861 /* Compute the relid set for the join we are considering */
862 joinrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
863 Assert(sjinfo->ojrelid == 0); /* SEMI joins don't have RT indexes */
864
865 /*
866 * Since we're only considering a single-rel RHS, any join clauses it
867 * has must be clauses linking it to the semijoin's min_lefthand. We
868 * can also consider EC-derived join clauses.
869 */
870 restrictlist =
872 joinrelids,
873 sjinfo->min_lefthand,
874 innerrel,
875 NULL),
876 innerrel->joininfo);
877
878 /* Test whether the innerrel is unique for those clauses. */
880 joinrelids, sjinfo->min_lefthand, innerrel,
881 JOIN_SEMI, restrictlist, true))
882 continue;
883
884 /* OK, remove the SpecialJoinInfo from the list. */
885 root->join_info_list = foreach_delete_current(root->join_info_list, lc);
886 }
887}
bool innerrel_is_unique(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache)
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo)
Definition: equivclass.c:1550
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
@ JOIN_SEMI
Definition: nodes.h:313
#define foreach_delete_current(lst, var_or_cell)
Definition: pg_list.h:391
Relids min_righthand
Definition: pathnodes.h:3031
JoinType jointype
Definition: pathnodes.h:3034
Relids min_lefthand
Definition: pathnodes.h:3030

References Assert(), bms_get_singleton_member(), bms_union(), find_base_rel(), foreach_delete_current, generate_join_implied_equalities(), innerrel_is_unique(), JOIN_SEMI, RelOptInfo::joininfo, SpecialJoinInfo::jointype, lfirst, list_concat(), SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, SpecialJoinInfo::ojrelid, rel_supports_distinctness(), and root.

Referenced by query_planner().

◆ remove_useless_groupby_columns()

void remove_useless_groupby_columns ( PlannerInfo root)

Definition at line 412 of file initsplan.c.

413{
414 Query *parse = root->parse;
415 Bitmapset **groupbyattnos;
416 Bitmapset **surplusvars;
417 bool tryremove = false;
418 ListCell *lc;
419 int relid;
420
421 /* No chance to do anything if there are less than two GROUP BY items */
422 if (list_length(root->processed_groupClause) < 2)
423 return;
424
425 /* Don't fiddle with the GROUP BY clause if the query has grouping sets */
426 if (parse->groupingSets)
427 return;
428
429 /*
430 * Scan the GROUP BY clause to find GROUP BY items that are simple Vars.
431 * Fill groupbyattnos[k] with a bitmapset of the column attnos of RTE k
432 * that are GROUP BY items.
433 */
434 groupbyattnos = (Bitmapset **) palloc0(sizeof(Bitmapset *) *
435 (list_length(parse->rtable) + 1));
436 foreach(lc, root->processed_groupClause)
437 {
439 TargetEntry *tle = get_sortgroupclause_tle(sgc, parse->targetList);
440 Var *var = (Var *) tle->expr;
441
442 /*
443 * Ignore non-Vars and Vars from other query levels.
444 *
445 * XXX in principle, stable expressions containing Vars could also be
446 * removed, if all the Vars are functionally dependent on other GROUP
447 * BY items. But it's not clear that such cases occur often enough to
448 * be worth troubling over.
449 */
450 if (!IsA(var, Var) ||
451 var->varlevelsup > 0)
452 continue;
453
454 /* OK, remember we have this Var */
455 relid = var->varno;
456 Assert(relid <= list_length(parse->rtable));
457
458 /*
459 * If this isn't the first column for this relation then we now have
460 * multiple columns. That means there might be some that can be
461 * removed.
462 */
463 tryremove |= !bms_is_empty(groupbyattnos[relid]);
464 groupbyattnos[relid] = bms_add_member(groupbyattnos[relid],
466 }
467
468 /*
469 * No Vars or didn't find multiple Vars for any relation in the GROUP BY?
470 * If so, nothing can be removed, so don't waste more effort trying.
471 */
472 if (!tryremove)
473 return;
474
475 /*
476 * Consider each relation and see if it is possible to remove some of its
477 * Vars from GROUP BY. For simplicity and speed, we do the actual removal
478 * in a separate pass. Here, we just fill surplusvars[k] with a bitmapset
479 * of the column attnos of RTE k that are removable GROUP BY items.
480 */
481 surplusvars = NULL; /* don't allocate array unless required */
482 relid = 0;
483 foreach(lc, parse->rtable)
484 {
486 RelOptInfo *rel;
487 Bitmapset *relattnos;
488 Bitmapset *best_keycolumns = NULL;
489 int32 best_nkeycolumns = PG_INT32_MAX;
490
491 relid++;
492
493 /* Only plain relations could have primary-key constraints */
494 if (rte->rtekind != RTE_RELATION)
495 continue;
496
497 /*
498 * We must skip inheritance parent tables as some of the child rels
499 * may cause duplicate rows. This cannot happen with partitioned
500 * tables, however.
501 */
502 if (rte->inh && rte->relkind != RELKIND_PARTITIONED_TABLE)
503 continue;
504
505 /* Nothing to do unless this rel has multiple Vars in GROUP BY */
506 relattnos = groupbyattnos[relid];
507 if (bms_membership(relattnos) != BMS_MULTIPLE)
508 continue;
509
510 rel = root->simple_rel_array[relid];
511
512 /*
513 * Now check each index for this relation to see if there are any with
514 * columns which are a proper subset of the grouping columns for this
515 * relation.
516 */
518 {
519 Bitmapset *ind_attnos;
520 bool nulls_check_ok;
521
522 /*
523 * Skip any non-unique and deferrable indexes. Predicate indexes
524 * have not been checked yet, so we must skip those too as the
525 * predOK check that's done later might fail.
526 */
527 if (!index->unique || !index->immediate || index->indpred != NIL)
528 continue;
529
530 /* For simplicity, we currently don't support expression indexes */
531 if (index->indexprs != NIL)
532 continue;
533
534 ind_attnos = NULL;
535 nulls_check_ok = true;
536 for (int i = 0; i < index->nkeycolumns; i++)
537 {
538 /*
539 * We must insist that the index columns are all defined NOT
540 * NULL otherwise duplicate NULLs could exist. However, we
541 * can relax this check when the index is defined with NULLS
542 * NOT DISTINCT as there can only be 1 NULL row, therefore
543 * functional dependency on the unique columns is maintained,
544 * despite the NULL.
545 */
546 if (!index->nullsnotdistinct &&
547 !bms_is_member(index->indexkeys[i],
548 rel->notnullattnums))
549 {
550 nulls_check_ok = false;
551 break;
552 }
553
554 ind_attnos =
555 bms_add_member(ind_attnos,
556 index->indexkeys[i] -
558 }
559
560 if (!nulls_check_ok)
561 continue;
562
563 /*
564 * Skip any indexes where the indexed columns aren't a proper
565 * subset of the GROUP BY.
566 */
567 if (bms_subset_compare(ind_attnos, relattnos) != BMS_SUBSET1)
568 continue;
569
570 /*
571 * Record the attribute numbers from the index with the fewest
572 * columns. This allows the largest number of columns to be
573 * removed from the GROUP BY clause. In the future, we may wish
574 * to consider using the narrowest set of columns and looking at
575 * pg_statistic.stawidth as it might be better to use an index
576 * with, say two INT4s, rather than, say, one long varlena column.
577 */
578 if (index->nkeycolumns < best_nkeycolumns)
579 {
580 best_keycolumns = ind_attnos;
581 best_nkeycolumns = index->nkeycolumns;
582 }
583 }
584
585 /* Did we find a suitable index? */
586 if (!bms_is_empty(best_keycolumns))
587 {
588 /*
589 * To easily remember whether we've found anything to do, we don't
590 * allocate the surplusvars[] array until we find something.
591 */
592 if (surplusvars == NULL)
593 surplusvars = (Bitmapset **) palloc0(sizeof(Bitmapset *) *
594 (list_length(parse->rtable) + 1));
595
596 /* Remember the attnos of the removable columns */
597 surplusvars[relid] = bms_difference(relattnos, best_keycolumns);
598 }
599 }
600
601 /*
602 * If we found any surplus Vars, build a new GROUP BY clause without them.
603 * (Note: this may leave some TLEs with unreferenced ressortgroupref
604 * markings, but that's harmless.)
605 */
606 if (surplusvars != NULL)
607 {
608 List *new_groupby = NIL;
609
610 foreach(lc, root->processed_groupClause)
611 {
613 TargetEntry *tle = get_sortgroupclause_tle(sgc, parse->targetList);
614 Var *var = (Var *) tle->expr;
615
616 /*
617 * New list must include non-Vars, outer Vars, and anything not
618 * marked as surplus.
619 */
620 if (!IsA(var, Var) ||
621 var->varlevelsup > 0 ||
623 surplusvars[var->varno]))
624 new_groupby = lappend(new_groupby, sgc);
625 }
626
627 root->processed_groupClause = new_groupby;
628 }
629}
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:346
BMS_Comparison bms_subset_compare(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:445
@ BMS_SUBSET1
Definition: bitmapset.h:63
#define PG_INT32_MAX
Definition: c.h:560
int32_t int32
Definition: c.h:498
int i
Definition: isn.c:77
void * palloc0(Size size)
Definition: mcxt.c:1970
#define lfirst_node(type, lc)
Definition: pg_list.h:176
#define foreach_node(type, var, lst)
Definition: pg_list.h:496
Bitmapset * notnullattnums
Definition: pathnodes.h:963
List * indexlist
Definition: pathnodes.h:971
Index varlevelsup
Definition: primnodes.h:294
Definition: type.h:96
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27

References Assert(), bms_add_member(), bms_difference(), bms_is_empty, bms_is_member(), bms_membership(), BMS_MULTIPLE, BMS_SUBSET1, bms_subset_compare(), TargetEntry::expr, FirstLowInvalidHeapAttributeNumber, foreach_node, get_sortgroupclause_tle(), i, if(), RelOptInfo::indexlist, RangeTblEntry::inh, IsA, lappend(), lfirst_node, list_length(), NIL, RelOptInfo::notnullattnums, palloc0(), parse(), PG_INT32_MAX, root, RTE_RELATION, RangeTblEntry::rtekind, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by query_planner().

◆ remove_useless_joins()

List * remove_useless_joins ( PlannerInfo root,
List joinlist 
)

Definition at line 88 of file analyzejoins.c.

89{
90 ListCell *lc;
91
92 /*
93 * We are only interested in relations that are left-joined to, so we can
94 * scan the join_info_list to find them easily.
95 */
96restart:
97 foreach(lc, root->join_info_list)
98 {
99 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
100 int innerrelid;
101 int nremoved;
102
103 /* Skip if not removable */
104 if (!join_is_removable(root, sjinfo))
105 continue;
106
107 /*
108 * Currently, join_is_removable can only succeed when the sjinfo's
109 * righthand is a single baserel. Remove that rel from the query and
110 * joinlist.
111 */
112 innerrelid = bms_singleton_member(sjinfo->min_righthand);
113
114 remove_leftjoinrel_from_query(root, innerrelid, sjinfo);
115
116 /* We verify that exactly one reference gets removed from joinlist */
117 nremoved = 0;
118 joinlist = remove_rel_from_joinlist(joinlist, innerrelid, &nremoved);
119 if (nremoved != 1)
120 elog(ERROR, "failed to find relation %d in joinlist", innerrelid);
121
122 /*
123 * We can delete this SpecialJoinInfo from the list too, since it's no
124 * longer of interest. (Since we'll restart the foreach loop
125 * immediately, we don't bother with foreach_delete_current.)
126 */
127 root->join_info_list = list_delete_cell(root->join_info_list, lc);
128
129 /*
130 * Restart the scan. This is necessary to ensure we find all
131 * removable joins independently of ordering of the join_info_list
132 * (note that removal of attr_needed bits may make a join appear
133 * removable that did not before).
134 */
135 goto restart;
136 }
137
138 return joinlist;
139}
static List * remove_rel_from_joinlist(List *joinlist, int relid, int *nremoved)
Definition: analyzejoins.c:772
static void remove_leftjoinrel_from_query(PlannerInfo *root, int relid, SpecialJoinInfo *sjinfo)
Definition: analyzejoins.c:528
static bool join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
Definition: analyzejoins.c:153
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:672
List * list_delete_cell(List *list, ListCell *cell)
Definition: list.c:841

References bms_singleton_member(), elog, ERROR, join_is_removable(), lfirst, list_delete_cell(), SpecialJoinInfo::min_righthand, remove_leftjoinrel_from_query(), remove_rel_from_joinlist(), and root.

Referenced by query_planner().

◆ remove_useless_self_joins()

List * remove_useless_self_joins ( PlannerInfo root,
List joinlist 
)

Definition at line 2344 of file analyzejoins.c.

2345{
2346 Relids toRemove = NULL;
2347 int relid = -1;
2348
2349 if (!enable_self_join_elimination || joinlist == NIL ||
2350 (list_length(joinlist) == 1 && !IsA(linitial(joinlist), List)))
2351 return joinlist;
2352
2353 /*
2354 * Merge pairs of relations participated in self-join. Remove unnecessary
2355 * range table entries.
2356 */
2357 toRemove = remove_self_joins_recurse(root, joinlist, toRemove);
2358
2359 if (unlikely(toRemove != NULL))
2360 {
2361 /* At the end, remove orphaned relation links */
2362 while ((relid = bms_next_member(toRemove, relid)) >= 0)
2363 {
2364 int nremoved = 0;
2365
2366 joinlist = remove_rel_from_joinlist(joinlist, relid, &nremoved);
2367 if (nremoved != 1)
2368 elog(ERROR, "failed to find relation %d in joinlist", relid);
2369 }
2370 }
2371
2372 return joinlist;
2373}
static Relids remove_self_joins_recurse(PlannerInfo *root, List *joinlist, Relids toRemove)
bool enable_self_join_elimination
Definition: analyzejoins.c:53
#define unlikely(x)
Definition: c.h:347

References bms_next_member(), elog, enable_self_join_elimination, ERROR, IsA, linitial, list_length(), NIL, remove_rel_from_joinlist(), remove_self_joins_recurse(), root, and unlikely.

Referenced by query_planner().

◆ restriction_is_always_false()

bool restriction_is_always_false ( PlannerInfo root,
RestrictInfo restrictinfo 
)

Definition at line 3156 of file initsplan.c.

3158{
3159 /*
3160 * For a clone clause, we don't have a reliable way to determine if the
3161 * input expression of a NullTest is non-nullable: nullingrel bits in
3162 * clone clauses may not reflect reality, so we dare not draw conclusions
3163 * from clones about whether Vars are guaranteed not-null.
3164 */
3165 if (restrictinfo->has_clone || restrictinfo->is_clone)
3166 return false;
3167
3168 /* Check for NullTest qual */
3169 if (IsA(restrictinfo->clause, NullTest))
3170 {
3171 NullTest *nulltest = (NullTest *) restrictinfo->clause;
3172
3173 /* is this NullTest an IS_NULL qual? */
3174 if (nulltest->nulltesttype != IS_NULL)
3175 return false;
3176
3177 /*
3178 * Empty rows can appear NULL in some contexts and NOT NULL in others,
3179 * so avoid this optimization for row expressions.
3180 */
3181 if (nulltest->argisrow)
3182 return false;
3183
3184 return expr_is_nonnullable(root, nulltest->arg);
3185 }
3186
3187 /* If it's an OR, check its sub-clauses */
3188 if (restriction_is_or_clause(restrictinfo))
3189 {
3190 ListCell *lc;
3191
3192 Assert(is_orclause(restrictinfo->orclause));
3193
3194 /*
3195 * Currently, when processing OR expressions, we only return true when
3196 * all of the OR branches are always false. This could perhaps be
3197 * expanded to remove OR branches that are provably false. This may
3198 * be a useful thing to do as it could result in the OR being left
3199 * with a single arg. That's useful as it would allow the OR
3200 * condition to be replaced with its single argument which may allow
3201 * use of an index for faster filtering on the remaining condition.
3202 */
3203 foreach(lc, ((BoolExpr *) restrictinfo->orclause)->args)
3204 {
3205 Node *orarg = (Node *) lfirst(lc);
3206
3207 if (!IsA(orarg, RestrictInfo) ||
3209 return false;
3210 }
3211 return true;
3212 }
3213
3214 return false;
3215}
bool restriction_is_always_false(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:3156
static bool expr_is_nonnullable(PlannerInfo *root, Expr *expr)
Definition: initsplan.c:3055
static bool is_orclause(const void *clause)
Definition: nodeFuncs.h:116
@ IS_NULL
Definition: primnodes.h:1957
bool restriction_is_or_clause(RestrictInfo *restrictinfo)
Definition: restrictinfo.c:407
NullTestType nulltesttype
Definition: primnodes.h:1964
Expr * arg
Definition: primnodes.h:1963
bool has_clone
Definition: pathnodes.h:2712

References NullTest::arg, Assert(), RestrictInfo::clause, expr_is_nonnullable(), RestrictInfo::has_clone, if(), RestrictInfo::is_clone, IS_NULL, is_orclause(), IsA, lfirst, NullTest::nulltesttype, restriction_is_always_false(), restriction_is_or_clause(), and root.

Referenced by add_base_clause_to_rel(), add_join_clause_to_rels(), apply_child_basequals(), and restriction_is_always_false().

◆ restriction_is_always_true()

bool restriction_is_always_true ( PlannerInfo root,
RestrictInfo restrictinfo 
)

Definition at line 3091 of file initsplan.c.

3093{
3094 /*
3095 * For a clone clause, we don't have a reliable way to determine if the
3096 * input expression of a NullTest is non-nullable: nullingrel bits in
3097 * clone clauses may not reflect reality, so we dare not draw conclusions
3098 * from clones about whether Vars are guaranteed not-null.
3099 */
3100 if (restrictinfo->has_clone || restrictinfo->is_clone)
3101 return false;
3102
3103 /* Check for NullTest qual */
3104 if (IsA(restrictinfo->clause, NullTest))
3105 {
3106 NullTest *nulltest = (NullTest *) restrictinfo->clause;
3107
3108 /* is this NullTest an IS_NOT_NULL qual? */
3109 if (nulltest->nulltesttype != IS_NOT_NULL)
3110 return false;
3111
3112 /*
3113 * Empty rows can appear NULL in some contexts and NOT NULL in others,
3114 * so avoid this optimization for row expressions.
3115 */
3116 if (nulltest->argisrow)
3117 return false;
3118
3119 return expr_is_nonnullable(root, nulltest->arg);
3120 }
3121
3122 /* If it's an OR, check its sub-clauses */
3123 if (restriction_is_or_clause(restrictinfo))
3124 {
3125 ListCell *lc;
3126
3127 Assert(is_orclause(restrictinfo->orclause));
3128
3129 /*
3130 * if any of the given OR branches is provably always true then the
3131 * entire condition is true.
3132 */
3133 foreach(lc, ((BoolExpr *) restrictinfo->orclause)->args)
3134 {
3135 Node *orarg = (Node *) lfirst(lc);
3136
3137 if (!IsA(orarg, RestrictInfo))
3138 continue;
3139
3141 return true;
3142 }
3143 }
3144
3145 return false;
3146}
bool restriction_is_always_true(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:3091
@ IS_NOT_NULL
Definition: primnodes.h:1957

References NullTest::arg, Assert(), RestrictInfo::clause, expr_is_nonnullable(), RestrictInfo::has_clone, if(), RestrictInfo::is_clone, IS_NOT_NULL, is_orclause(), IsA, lfirst, NullTest::nulltesttype, restriction_is_always_true(), restriction_is_or_clause(), and root.

Referenced by add_base_clause_to_rel(), add_join_clause_to_rels(), apply_child_basequals(), and restriction_is_always_true().

◆ set_plan_references()

Plan * set_plan_references ( PlannerInfo root,
Plan plan 
)

Definition at line 288 of file setrefs.c.

289{
290 Plan *result;
291 PlannerGlobal *glob = root->glob;
292 int rtoffset = list_length(glob->finalrtable);
293 ListCell *lc;
294
295 /*
296 * Add all the query's RTEs to the flattened rangetable. The live ones
297 * will have their rangetable indexes increased by rtoffset. (Additional
298 * RTEs, not referenced by the Plan tree, might get added after those.)
299 */
301
302 /*
303 * Adjust RT indexes of PlanRowMarks and add to final rowmarks list
304 */
305 foreach(lc, root->rowMarks)
306 {
308 PlanRowMark *newrc;
309
310 /* flat copy is enough since all fields are scalars */
311 newrc = (PlanRowMark *) palloc(sizeof(PlanRowMark));
312 memcpy(newrc, rc, sizeof(PlanRowMark));
313
314 /* adjust indexes ... but *not* the rowmarkId */
315 newrc->rti += rtoffset;
316 newrc->prti += rtoffset;
317
318 glob->finalrowmarks = lappend(glob->finalrowmarks, newrc);
319 }
320
321 /*
322 * Adjust RT indexes of AppendRelInfos and add to final appendrels list.
323 * We assume the AppendRelInfos were built during planning and don't need
324 * to be copied.
325 */
326 foreach(lc, root->append_rel_list)
327 {
329
330 /* adjust RT indexes */
331 appinfo->parent_relid += rtoffset;
332 appinfo->child_relid += rtoffset;
333
334 /*
335 * Rather than adjust the translated_vars entries, just drop 'em.
336 * Neither the executor nor EXPLAIN currently need that data.
337 */
338 appinfo->translated_vars = NIL;
339
340 glob->appendRelations = lappend(glob->appendRelations, appinfo);
341 }
342
343 /* If needed, create workspace for processing AlternativeSubPlans */
344 if (root->hasAlternativeSubPlans)
345 {
346 root->isAltSubplan = (bool *)
347 palloc0(list_length(glob->subplans) * sizeof(bool));
348 root->isUsedSubplan = (bool *)
349 palloc0(list_length(glob->subplans) * sizeof(bool));
350 }
351
352 /* Now fix the Plan tree */
353 result = set_plan_refs(root, plan, rtoffset);
354
355 /*
356 * If we have AlternativeSubPlans, it is likely that we now have some
357 * unreferenced subplans in glob->subplans. To avoid expending cycles on
358 * those subplans later, get rid of them by setting those list entries to
359 * NULL. (Note: we can't do this immediately upon processing an
360 * AlternativeSubPlan, because there may be multiple copies of the
361 * AlternativeSubPlan, and they can get resolved differently.)
362 */
363 if (root->hasAlternativeSubPlans)
364 {
365 foreach(lc, glob->subplans)
366 {
367 int ndx = foreach_current_index(lc);
368
369 /*
370 * If it was used by some AlternativeSubPlan in this query level,
371 * but wasn't selected as best by any AlternativeSubPlan, then we
372 * don't need it. Do not touch subplans that aren't parts of
373 * AlternativeSubPlans.
374 */
375 if (root->isAltSubplan[ndx] && !root->isUsedSubplan[ndx])
376 lfirst(lc) = NULL;
377 }
378 }
379
380 return result;
381}
#define foreach_current_index(var_or_cell)
Definition: pg_list.h:403
static void add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing)
Definition: setrefs.c:392
static Plan * set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:615
Index child_relid
Definition: pathnodes.h:3106
List * translated_vars
Definition: pathnodes.h:3133
Index parent_relid
Definition: pathnodes.h:3105
Index prti
Definition: plannodes.h:1542
List * subplans
Definition: pathnodes.h:105
List * appendRelations
Definition: pathnodes.h:145
List * finalrowmarks
Definition: pathnodes.h:136
List * finalrtable
Definition: pathnodes.h:117

References add_rtes_to_flat_rtable(), PlannerGlobal::appendRelations, AppendRelInfo::child_relid, PlannerGlobal::finalrowmarks, PlannerGlobal::finalrtable, foreach_current_index, lappend(), lfirst, lfirst_node, list_length(), NIL, palloc(), palloc0(), AppendRelInfo::parent_relid, plan, PlanRowMark::prti, root, PlanRowMark::rti, set_plan_refs(), PlannerGlobal::subplans, and AppendRelInfo::translated_vars.

Referenced by set_subqueryscan_references(), and standard_planner().

◆ trivial_subqueryscan()

bool trivial_subqueryscan ( SubqueryScan plan)

Definition at line 1474 of file setrefs.c.

1475{
1476 int attrno;
1477 ListCell *lp,
1478 *lc;
1479
1480 /* We might have detected this already; in which case reuse the result */
1481 if (plan->scanstatus == SUBQUERY_SCAN_TRIVIAL)
1482 return true;
1483 if (plan->scanstatus == SUBQUERY_SCAN_NONTRIVIAL)
1484 return false;
1485 Assert(plan->scanstatus == SUBQUERY_SCAN_UNKNOWN);
1486 /* Initially, mark the SubqueryScan as non-deletable from the plan tree */
1487 plan->scanstatus = SUBQUERY_SCAN_NONTRIVIAL;
1488
1489 if (plan->scan.plan.qual != NIL)
1490 return false;
1491
1492 if (list_length(plan->scan.plan.targetlist) !=
1493 list_length(plan->subplan->targetlist))
1494 return false; /* tlists not same length */
1495
1496 attrno = 1;
1497 forboth(lp, plan->scan.plan.targetlist, lc, plan->subplan->targetlist)
1498 {
1499 TargetEntry *ptle = (TargetEntry *) lfirst(lp);
1500 TargetEntry *ctle = (TargetEntry *) lfirst(lc);
1501
1502 if (ptle->resjunk != ctle->resjunk)
1503 return false; /* tlist doesn't match junk status */
1504
1505 /*
1506 * We accept either a Var referencing the corresponding element of the
1507 * subplan tlist, or a Const equaling the subplan element. See
1508 * generate_setop_tlist() for motivation.
1509 */
1510 if (ptle->expr && IsA(ptle->expr, Var))
1511 {
1512 Var *var = (Var *) ptle->expr;
1513
1514 Assert(var->varno == plan->scan.scanrelid);
1515 Assert(var->varlevelsup == 0);
1516 if (var->varattno != attrno)
1517 return false; /* out of order */
1518 }
1519 else if (ptle->expr && IsA(ptle->expr, Const))
1520 {
1521 if (!equal(ptle->expr, ctle->expr))
1522 return false;
1523 }
1524 else
1525 return false;
1526
1527 attrno++;
1528 }
1529
1530 /* Re-mark the SubqueryScan as deletable from the plan tree */
1531 plan->scanstatus = SUBQUERY_SCAN_TRIVIAL;
1532
1533 return true;
1534}
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
@ SUBQUERY_SCAN_NONTRIVIAL
Definition: plannodes.h:709
@ SUBQUERY_SCAN_UNKNOWN
Definition: plannodes.h:707
@ SUBQUERY_SCAN_TRIVIAL
Definition: plannodes.h:708

References Assert(), equal(), TargetEntry::expr, forboth, IsA, lfirst, list_length(), NIL, plan, SUBQUERY_SCAN_NONTRIVIAL, SUBQUERY_SCAN_TRIVIAL, SUBQUERY_SCAN_UNKNOWN, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by mark_async_capable_plan(), and set_subqueryscan_references().

Variable Documentation

◆ cursor_tuple_fraction

PGDLLIMPORT double cursor_tuple_fraction
extern

Definition at line 67 of file planner.c.

Referenced by standard_planner().

◆ enable_self_join_elimination

PGDLLIMPORT bool enable_self_join_elimination
extern

Definition at line 53 of file analyzejoins.c.

Referenced by remove_useless_self_joins().

◆ from_collapse_limit

PGDLLIMPORT int from_collapse_limit
extern

Definition at line 39 of file initsplan.c.

Referenced by deconstruct_recurse().

◆ join_collapse_limit

PGDLLIMPORT int join_collapse_limit
extern

Definition at line 40 of file initsplan.c.

Referenced by deconstruct_recurse().